OLD | NEW |
| (Empty) |
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-o/loader.h> | |
38 #include <sys/types.h> | |
39 | |
40 namespace MacFileUtilities { | |
41 | |
42 class MachoWalker { | |
43 public: | |
44 // A callback function executed when a new load command is read. If no | |
45 // further processing of load commands is desired, return false. Otherwise, | |
46 // return true. | |
47 // |cmd| is the current command, and |offset| is the location relative to the | |
48 // beginning of the file (not header) where the command was read. If |swap| | |
49 // is set, then any command data (other than the returned load_command) should | |
50 // be swapped when read | |
51 typedef bool (*LoadCommandCallback)(MachoWalker *walker, load_command *cmd, | |
52 off_t offset, bool swap, void *context); | |
53 | |
54 MachoWalker(const char *path, LoadCommandCallback callback, void *context); | |
55 MachoWalker(int file_descriptor, LoadCommandCallback callback, void *context); | |
56 ~MachoWalker(); | |
57 | |
58 // Begin walking the header for |cpu_type|. If |cpu_type| is 0, then the | |
59 // native cpu type is used. Otherwise, accepted values are listed in | |
60 // /usr/include/mach/machine.h (e.g., CPU_TYPE_X86 or CPU_TYPE_POWERPC). | |
61 // Returns false if opening the file failed or if the |cpu_type| is not | |
62 // present in the file. | |
63 bool WalkHeader(int cpu_type); | |
64 | |
65 // Locate (if any) the header offset for |cpu_type| and return in |offset|. | |
66 // Return true if found, false otherwise. | |
67 bool FindHeader(int cpu_type, off_t &offset); | |
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 CurrentHeader(struct mach_header_64 *header, off_t *offset); | |
74 | |
75 private: | |
76 // Validate the |cpu_type| | |
77 int ValidateCPUType(int cpu_type); | |
78 | |
79 // Process an individual header starting at |offset| from the start of the | |
80 // file. Return true if successful, false otherwise. | |
81 bool WalkHeaderAtOffset(off_t offset); | |
82 bool WalkHeader64AtOffset(off_t offset); | |
83 | |
84 // Bottleneck for walking the load commands | |
85 bool WalkHeaderCore(off_t offset, uint32_t number_of_commands, bool swap); | |
86 | |
87 // File descriptor to the opened file | |
88 int file_; | |
89 | |
90 // User specified callback & context | |
91 LoadCommandCallback callback_; | |
92 void *callback_context_; | |
93 | |
94 // Current header, size, and offset. The mach_header_64 is used for both | |
95 // 32-bit and 64-bit headers because they only differ in their last field | |
96 // (reserved). By adding the |current_header_size_| and the | |
97 // |current_header_offset_|, you can determine the offset in the file just | |
98 // after the header. | |
99 struct mach_header_64 *current_header_; | |
100 unsigned long current_header_size_; | |
101 off_t current_header_offset_; | |
102 }; | |
103 | |
104 } // namespace MacFileUtilities | |
105 | |
106 #endif // COMMON_MAC_MACHO_WALKER_H__ | |
OLD | NEW |