michael@0: // Copyright (c) 2006, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // macho_walker.h: Iterate over the load commands in a mach-o file michael@0: // michael@0: // Author: Dan Waylonis michael@0: michael@0: #ifndef COMMON_MAC_MACHO_WALKER_H__ michael@0: #define COMMON_MAC_MACHO_WALKER_H__ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: namespace MacFileUtilities { michael@0: michael@0: class MachoWalker { michael@0: public: michael@0: // A callback function executed when a new load command is read. If no michael@0: // further processing of load commands is desired, return false. Otherwise, michael@0: // return true. michael@0: // |cmd| is the current command, and |offset| is the location relative to the michael@0: // beginning of the file (not header) where the command was read. If |swap| michael@0: // is set, then any command data (other than the returned load_command) should michael@0: // be swapped when read michael@0: typedef bool (*LoadCommandCallback)(MachoWalker *walker, load_command *cmd, michael@0: off_t offset, bool swap, void *context); michael@0: michael@0: MachoWalker(const char *path, LoadCommandCallback callback, void *context); michael@0: MachoWalker(void *memory, size_t size, LoadCommandCallback callback, michael@0: void *context); michael@0: ~MachoWalker(); michael@0: michael@0: // Begin walking the header for |cpu_type| and |cpu_subtype|. If |cpu_type| michael@0: // is 0, then the native cpu type is used. Otherwise, accepted values are michael@0: // listed in /usr/include/mach/machine.h (e.g., CPU_TYPE_X86 or michael@0: // CPU_TYPE_POWERPC). If |cpu_subtype| is CPU_SUBTYPE_MULTIPLE, the match is michael@0: // only done on |cpu_type|. michael@0: // Returns false if opening the file failed or if the |cpu_type|/|cpu_subtype| michael@0: // is not present in the file. michael@0: bool WalkHeader(cpu_type_t cpu_type, cpu_subtype_t cpu_subtype); michael@0: michael@0: // Read |size| bytes from the opened file at |offset| into |buffer| michael@0: bool ReadBytes(void *buffer, size_t size, off_t offset); michael@0: michael@0: // Return the current header and header offset michael@0: bool CurrentHeader(struct mach_header_64 *header, off_t *offset); michael@0: michael@0: private: michael@0: // Locate (if any) the header offset for |cpu_type| and return in |offset|. michael@0: // Return true if found, false otherwise. michael@0: bool FindHeader(cpu_type_t cpu_type, michael@0: cpu_subtype_t cpu_subtype, michael@0: off_t &offset); michael@0: michael@0: // Process an individual header starting at |offset| from the start of the michael@0: // file. Return true if successful, false otherwise. michael@0: bool WalkHeaderAtOffset(off_t offset); michael@0: bool WalkHeader64AtOffset(off_t offset); michael@0: michael@0: // Bottleneck for walking the load commands michael@0: bool WalkHeaderCore(off_t offset, uint32_t number_of_commands, bool swap); michael@0: michael@0: // File descriptor to the opened file michael@0: int file_; michael@0: michael@0: // Memory location to read from. michael@0: void *memory_; michael@0: michael@0: // Size of the memory segment we can read from. michael@0: size_t memory_size_; michael@0: michael@0: // User specified callback & context michael@0: LoadCommandCallback callback_; michael@0: void *callback_context_; michael@0: michael@0: // Current header, size, and offset. The mach_header_64 is used for both michael@0: // 32-bit and 64-bit headers because they only differ in their last field michael@0: // (reserved). By adding the |current_header_size_| and the michael@0: // |current_header_offset_|, you can determine the offset in the file just michael@0: // after the header. michael@0: struct mach_header_64 *current_header_; michael@0: unsigned long current_header_size_; michael@0: off_t current_header_offset_; michael@0: michael@0: private: michael@0: MachoWalker(const MachoWalker &); michael@0: MachoWalker &operator=(const MachoWalker &); michael@0: }; michael@0: michael@0: } // namespace MacFileUtilities michael@0: michael@0: #endif // COMMON_MAC_MACHO_WALKER_H__