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