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