1 | // Copyright (c) 2006, Google Inc. |
2 | // All rights reserved. |
3 | // |
4 | // Redistribution and use in source and binary forms, with or without |
5 | // modification, are permitted provided that the following conditions are |
6 | // met: |
7 | // |
8 | // * Redistributions of source code must retain the above copyright |
9 | // notice, this list of conditions and the following disclaimer. |
10 | // * Redistributions in binary form must reproduce the above |
11 | // copyright notice, this list of conditions and the following disclaimer |
12 | // in the documentation and/or other materials provided with the |
13 | // distribution. |
14 | // * Neither the name of Google Inc. nor the names of its |
15 | // contributors may be used to endorse or promote products derived from |
16 | // this software without specific prior written permission. |
17 | // |
18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | |
30 | // macho_walker.h: Iterate over the load commands in a mach-o file |
31 | // |
32 | // Author: Dan Waylonis |
33 | |
34 | #ifndef COMMON_MAC_MACHO_WALKER_H__ |
35 | #define COMMON_MAC_MACHO_WALKER_H__ |
36 | |
37 | #include <mach/machine.h> |
38 | #include <mach-o/loader.h> |
39 | #include <sys/types.h> |
40 | |
41 | namespace MacFileUtilities { |
42 | |
43 | class MachoWalker { |
44 | public: |
45 | // A callback function executed when a new load command is read. If no |
46 | // further processing of load commands is desired, return false. Otherwise, |
47 | // return true. |
48 | // |cmd| is the current command, and |offset| is the location relative to the |
49 | // beginning of the file (not header) where the command was read. If |swap| |
50 | // is set, then any command data (other than the returned load_command) should |
51 | // be swapped when read |
52 | typedef bool (*LoadCommandCallback)(MachoWalker* walker, load_command* cmd, |
53 | off_t offset, bool swap, void* context); |
54 | |
55 | MachoWalker(const char* path, LoadCommandCallback callback, void* context); |
56 | MachoWalker(void* memory, size_t size, LoadCommandCallback callback, |
57 | void* context); |
58 | ~MachoWalker(); |
59 | |
60 | // Begin walking the header for |cpu_type| and |cpu_subtype|. If |cpu_type| |
61 | // is 0, then the native cpu type is used. Otherwise, accepted values are |
62 | // listed in /usr/include/mach/machine.h (e.g., CPU_TYPE_X86 or |
63 | // CPU_TYPE_POWERPC). If |cpu_subtype| is CPU_SUBTYPE_MULTIPLE, the match is |
64 | // only done on |cpu_type|. |
65 | // Returns false if opening the file failed or if the |cpu_type|/|cpu_subtype| |
66 | // is not present in the file. |
67 | bool (cpu_type_t cpu_type, cpu_subtype_t cpu_subtype); |
68 | |
69 | // Read |size| bytes from the opened file at |offset| into |buffer| |
70 | bool ReadBytes(void* buffer, size_t size, off_t offset); |
71 | |
72 | // Return the current header and header offset |
73 | bool (struct mach_header_64* , off_t* offset); |
74 | |
75 | private: |
76 | // Locate (if any) the header offset for |cpu_type| and return in |offset|. |
77 | // Return true if found, false otherwise. |
78 | bool (cpu_type_t cpu_type, |
79 | cpu_subtype_t cpu_subtype, |
80 | off_t& offset); |
81 | |
82 | // Process an individual header starting at |offset| from the start of the |
83 | // file. Return true if successful, false otherwise. |
84 | bool (off_t offset); |
85 | bool (off_t offset); |
86 | |
87 | // Bottleneck for walking the load commands |
88 | bool (off_t offset, uint32_t number_of_commands, bool swap); |
89 | |
90 | // File descriptor to the opened file |
91 | int file_; |
92 | |
93 | // Memory location to read from. |
94 | void* memory_; |
95 | |
96 | // Size of the memory segment we can read from. |
97 | size_t memory_size_; |
98 | |
99 | // User specified callback & context |
100 | LoadCommandCallback callback_; |
101 | void* callback_context_; |
102 | |
103 | // Current header, size, and offset. The mach_header_64 is used for both |
104 | // 32-bit and 64-bit headers because they only differ in their last field |
105 | // (reserved). By adding the |current_header_size_| and the |
106 | // |current_header_offset_|, you can determine the offset in the file just |
107 | // after the header. |
108 | struct mach_header_64* ; |
109 | unsigned long ; |
110 | off_t ; |
111 | |
112 | private: |
113 | MachoWalker(const MachoWalker&); |
114 | MachoWalker& operator=(const MachoWalker&); |
115 | }; |
116 | |
117 | } // namespace MacFileUtilities |
118 | |
119 | #endif // COMMON_MAC_MACHO_WALKER_H__ |
120 | |