1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/mac/macho_walker.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,119 @@ 1.4 +// Copyright (c) 2006, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +// macho_walker.h: Iterate over the load commands in a mach-o file 1.34 +// 1.35 +// Author: Dan Waylonis 1.36 + 1.37 +#ifndef COMMON_MAC_MACHO_WALKER_H__ 1.38 +#define COMMON_MAC_MACHO_WALKER_H__ 1.39 + 1.40 +#include <mach/machine.h> 1.41 +#include <mach-o/loader.h> 1.42 +#include <sys/types.h> 1.43 + 1.44 +namespace MacFileUtilities { 1.45 + 1.46 +class MachoWalker { 1.47 + public: 1.48 + // A callback function executed when a new load command is read. If no 1.49 + // further processing of load commands is desired, return false. Otherwise, 1.50 + // return true. 1.51 + // |cmd| is the current command, and |offset| is the location relative to the 1.52 + // beginning of the file (not header) where the command was read. If |swap| 1.53 + // is set, then any command data (other than the returned load_command) should 1.54 + // be swapped when read 1.55 + typedef bool (*LoadCommandCallback)(MachoWalker *walker, load_command *cmd, 1.56 + off_t offset, bool swap, void *context); 1.57 + 1.58 + MachoWalker(const char *path, LoadCommandCallback callback, void *context); 1.59 + MachoWalker(void *memory, size_t size, LoadCommandCallback callback, 1.60 + void *context); 1.61 + ~MachoWalker(); 1.62 + 1.63 + // Begin walking the header for |cpu_type| and |cpu_subtype|. If |cpu_type| 1.64 + // is 0, then the native cpu type is used. Otherwise, accepted values are 1.65 + // listed in /usr/include/mach/machine.h (e.g., CPU_TYPE_X86 or 1.66 + // CPU_TYPE_POWERPC). If |cpu_subtype| is CPU_SUBTYPE_MULTIPLE, the match is 1.67 + // only done on |cpu_type|. 1.68 + // Returns false if opening the file failed or if the |cpu_type|/|cpu_subtype| 1.69 + // is not present in the file. 1.70 + bool WalkHeader(cpu_type_t cpu_type, cpu_subtype_t cpu_subtype); 1.71 + 1.72 + // Read |size| bytes from the opened file at |offset| into |buffer| 1.73 + bool ReadBytes(void *buffer, size_t size, off_t offset); 1.74 + 1.75 + // Return the current header and header offset 1.76 + bool CurrentHeader(struct mach_header_64 *header, off_t *offset); 1.77 + 1.78 + private: 1.79 + // Locate (if any) the header offset for |cpu_type| and return in |offset|. 1.80 + // Return true if found, false otherwise. 1.81 + bool FindHeader(cpu_type_t cpu_type, 1.82 + cpu_subtype_t cpu_subtype, 1.83 + off_t &offset); 1.84 + 1.85 + // Process an individual header starting at |offset| from the start of the 1.86 + // file. Return true if successful, false otherwise. 1.87 + bool WalkHeaderAtOffset(off_t offset); 1.88 + bool WalkHeader64AtOffset(off_t offset); 1.89 + 1.90 + // Bottleneck for walking the load commands 1.91 + bool WalkHeaderCore(off_t offset, uint32_t number_of_commands, bool swap); 1.92 + 1.93 + // File descriptor to the opened file 1.94 + int file_; 1.95 + 1.96 + // Memory location to read from. 1.97 + void *memory_; 1.98 + 1.99 + // Size of the memory segment we can read from. 1.100 + size_t memory_size_; 1.101 + 1.102 + // User specified callback & context 1.103 + LoadCommandCallback callback_; 1.104 + void *callback_context_; 1.105 + 1.106 + // Current header, size, and offset. The mach_header_64 is used for both 1.107 + // 32-bit and 64-bit headers because they only differ in their last field 1.108 + // (reserved). By adding the |current_header_size_| and the 1.109 + // |current_header_offset_|, you can determine the offset in the file just 1.110 + // after the header. 1.111 + struct mach_header_64 *current_header_; 1.112 + unsigned long current_header_size_; 1.113 + off_t current_header_offset_; 1.114 + 1.115 + private: 1.116 + MachoWalker(const MachoWalker &); 1.117 + MachoWalker &operator=(const MachoWalker &); 1.118 +}; 1.119 + 1.120 +} // namespace MacFileUtilities 1.121 + 1.122 +#endif // COMMON_MAC_MACHO_WALKER_H__