OLD | NEW |
1 // Copyright (c) 2006, Google Inc. | 1 // Copyright (c) 2006, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 15 matching lines...) Expand all Loading... |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
30 // macho_walker.cc: Iterate over the load commands in a mach-o file | 30 // macho_walker.cc: Iterate over the load commands in a mach-o file |
31 // | 31 // |
32 // See macho_walker.h for documentation | 32 // See macho_walker.h for documentation |
33 // | 33 // |
34 // Author: Dan Waylonis | 34 // Author: Dan Waylonis |
35 | 35 |
36 extern "C" { // necessary for Leopard | 36 #include <assert.h> |
37 #include <assert.h> | 37 #include <fcntl.h> |
38 #include <fcntl.h> | 38 #include <mach-o/arch.h> |
39 #include <mach-o/arch.h> | 39 #include <mach-o/fat.h> |
40 #include <mach-o/loader.h> | 40 #include <mach-o/loader.h> |
41 #include <mach-o/swap.h> | 41 #include <string.h> |
42 #include <string.h> | 42 #include <unistd.h> |
43 #include <unistd.h> | |
44 } | |
45 | 43 |
46 #include "common/mac/byteswap.h" | 44 #include "common/mac/byteswap.h" |
47 #include "common/mac/macho_walker.h" | 45 #include "common/mac/macho_walker.h" |
48 #include "common/mac/macho_utilities.h" | 46 #include "common/mac/macho_utilities.h" |
49 | 47 |
50 namespace MacFileUtilities { | 48 namespace MacFileUtilities { |
51 | 49 |
52 MachoWalker::MachoWalker(const char *path, LoadCommandCallback callback, | 50 MachoWalker::MachoWalker(const char *path, LoadCommandCallback callback, |
53 void *context) | 51 void *context) |
54 : file_(-1), | 52 : file_(-1), |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 } | 147 } |
150 | 148 |
151 if (!is_fat) { | 149 if (!is_fat) { |
152 // If we don't have a fat header, check if the cpu type matches the single | 150 // If we don't have a fat header, check if the cpu type matches the single |
153 // header | 151 // header |
154 struct mach_header header; | 152 struct mach_header header; |
155 if (!ReadBytes(&header, sizeof(header), 0)) | 153 if (!ReadBytes(&header, sizeof(header), 0)) |
156 return false; | 154 return false; |
157 | 155 |
158 if (magic == MH_CIGAM || magic == MH_CIGAM_64) | 156 if (magic == MH_CIGAM || magic == MH_CIGAM_64) |
159 swap_mach_header(&header, NXHostByteOrder()); | 157 breakpad_swap_mach_header(&header); |
160 | 158 |
161 if (cpu_type != header.cputype || | 159 if (cpu_type != header.cputype || |
162 (cpu_subtype != CPU_SUBTYPE_MULTIPLE && | 160 (cpu_subtype != CPU_SUBTYPE_MULTIPLE && |
163 cpu_subtype != header.cpusubtype)) { | 161 cpu_subtype != header.cpusubtype)) { |
164 return false; | 162 return false; |
165 } | 163 } |
166 | 164 |
167 offset = 0; | 165 offset = 0; |
168 return true; | 166 return true; |
169 } else { | 167 } else { |
170 // Read the fat header and find an appropriate architecture | 168 // Read the fat header and find an appropriate architecture |
171 offset = 0; | 169 offset = 0; |
172 struct fat_header fat; | 170 struct fat_header fat; |
173 if (!ReadBytes(&fat, sizeof(fat), offset)) | 171 if (!ReadBytes(&fat, sizeof(fat), offset)) |
174 return false; | 172 return false; |
175 | 173 |
176 if (NXHostByteOrder() != NX_BigEndian) | 174 if (NXHostByteOrder() != NX_BigEndian) |
177 swap_fat_header(&fat, NXHostByteOrder()); | 175 breakpad_swap_fat_header(&fat); |
178 | 176 |
179 offset += sizeof(fat); | 177 offset += sizeof(fat); |
180 | 178 |
181 // Search each architecture for the desired one | 179 // Search each architecture for the desired one |
182 struct fat_arch arch; | 180 struct fat_arch arch; |
183 for (uint32_t i = 0; i < fat.nfat_arch; ++i) { | 181 for (uint32_t i = 0; i < fat.nfat_arch; ++i) { |
184 if (!ReadBytes(&arch, sizeof(arch), offset)) | 182 if (!ReadBytes(&arch, sizeof(arch), offset)) |
185 return false; | 183 return false; |
186 | 184 |
187 if (NXHostByteOrder() != NX_BigEndian) | 185 if (NXHostByteOrder() != NX_BigEndian) |
188 swap_fat_arch(&arch, 1, NXHostByteOrder()); | 186 breakpad_swap_fat_arch(&arch, 1); |
189 | 187 |
190 if (arch.cputype == cpu_type && | 188 if (arch.cputype == cpu_type && |
191 (cpu_subtype == CPU_SUBTYPE_MULTIPLE || | 189 (cpu_subtype == CPU_SUBTYPE_MULTIPLE || |
192 arch.cpusubtype == cpu_subtype)) { | 190 arch.cpusubtype == cpu_subtype)) { |
193 offset = arch.offset; | 191 offset = arch.offset; |
194 return true; | 192 return true; |
195 } | 193 } |
196 | 194 |
197 offset += sizeof(arch); | 195 offset += sizeof(arch); |
198 } | 196 } |
199 } | 197 } |
200 | 198 |
201 return false; | 199 return false; |
202 } | 200 } |
203 | 201 |
204 bool MachoWalker::WalkHeaderAtOffset(off_t offset) { | 202 bool MachoWalker::WalkHeaderAtOffset(off_t offset) { |
205 struct mach_header header; | 203 struct mach_header header; |
206 if (!ReadBytes(&header, sizeof(header), offset)) | 204 if (!ReadBytes(&header, sizeof(header), offset)) |
207 return false; | 205 return false; |
208 | 206 |
209 bool swap = (header.magic == MH_CIGAM); | 207 bool swap = (header.magic == MH_CIGAM); |
210 if (swap) | 208 if (swap) |
211 swap_mach_header(&header, NXHostByteOrder()); | 209 breakpad_swap_mach_header(&header); |
212 | 210 |
213 // Copy the data into the mach_header_64 structure. Since the 32-bit and | 211 // Copy the data into the mach_header_64 structure. Since the 32-bit and |
214 // 64-bit only differ in the last field (reserved), this is safe to do. | 212 // 64-bit only differ in the last field (reserved), this is safe to do. |
215 struct mach_header_64 header64; | 213 struct mach_header_64 header64; |
216 memcpy((void *)&header64, (const void *)&header, sizeof(header)); | 214 memcpy((void *)&header64, (const void *)&header, sizeof(header)); |
217 header64.reserved = 0; | 215 header64.reserved = 0; |
218 | 216 |
219 current_header_ = &header64; | 217 current_header_ = &header64; |
220 current_header_size_ = sizeof(header); // 32-bit, not 64-bit | 218 current_header_size_ = sizeof(header); // 32-bit, not 64-bit |
221 current_header_offset_ = offset; | 219 current_header_offset_ = offset; |
222 offset += current_header_size_; | 220 offset += current_header_size_; |
223 bool result = WalkHeaderCore(offset, header.ncmds, swap); | 221 bool result = WalkHeaderCore(offset, header.ncmds, swap); |
224 current_header_ = NULL; | 222 current_header_ = NULL; |
225 current_header_size_ = 0; | 223 current_header_size_ = 0; |
226 current_header_offset_ = 0; | 224 current_header_offset_ = 0; |
227 return result; | 225 return result; |
228 } | 226 } |
229 | 227 |
230 bool MachoWalker::WalkHeader64AtOffset(off_t offset) { | 228 bool MachoWalker::WalkHeader64AtOffset(off_t offset) { |
231 struct mach_header_64 header; | 229 struct mach_header_64 header; |
232 if (!ReadBytes(&header, sizeof(header), offset)) | 230 if (!ReadBytes(&header, sizeof(header), offset)) |
233 return false; | 231 return false; |
234 | 232 |
235 bool swap = (header.magic == MH_CIGAM_64); | 233 bool swap = (header.magic == MH_CIGAM_64); |
236 if (swap) | 234 if (swap) |
237 breakpad_swap_mach_header_64(&header, NXHostByteOrder()); | 235 breakpad_swap_mach_header_64(&header); |
238 | 236 |
239 current_header_ = &header; | 237 current_header_ = &header; |
240 current_header_size_ = sizeof(header); | 238 current_header_size_ = sizeof(header); |
241 current_header_offset_ = offset; | 239 current_header_offset_ = offset; |
242 offset += current_header_size_; | 240 offset += current_header_size_; |
243 bool result = WalkHeaderCore(offset, header.ncmds, swap); | 241 bool result = WalkHeaderCore(offset, header.ncmds, swap); |
244 current_header_ = NULL; | 242 current_header_ = NULL; |
245 current_header_size_ = 0; | 243 current_header_size_ = 0; |
246 current_header_offset_ = 0; | 244 current_header_offset_ = 0; |
247 return result; | 245 return result; |
248 } | 246 } |
249 | 247 |
250 bool MachoWalker::WalkHeaderCore(off_t offset, uint32_t number_of_commands, | 248 bool MachoWalker::WalkHeaderCore(off_t offset, uint32_t number_of_commands, |
251 bool swap) { | 249 bool swap) { |
252 for (uint32_t i = 0; i < number_of_commands; ++i) { | 250 for (uint32_t i = 0; i < number_of_commands; ++i) { |
253 struct load_command cmd; | 251 struct load_command cmd; |
254 if (!ReadBytes(&cmd, sizeof(cmd), offset)) | 252 if (!ReadBytes(&cmd, sizeof(cmd), offset)) |
255 return false; | 253 return false; |
256 | 254 |
257 if (swap) | 255 if (swap) |
258 swap_load_command(&cmd, NXHostByteOrder()); | 256 breakpad_swap_load_command(&cmd); |
259 | 257 |
260 // Call the user callback | 258 // Call the user callback |
261 if (callback_ && !callback_(this, &cmd, offset, swap, callback_context_)) | 259 if (callback_ && !callback_(this, &cmd, offset, swap, callback_context_)) |
262 break; | 260 break; |
263 | 261 |
264 offset += cmd.cmdsize; | 262 offset += cmd.cmdsize; |
265 } | 263 } |
266 | 264 |
267 return true; | 265 return true; |
268 } | 266 } |
269 | 267 |
270 } // namespace MacFileUtilities | 268 } // namespace MacFileUtilities |
OLD | NEW |