]>
Commit | Line | Data |
---|---|---|
55e3d2f6 A |
1 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- |
2 | * | |
3 | * Copyright (c) 2008 Apple Inc. All rights reserved. | |
4 | * | |
5 | * @APPLE_LICENSE_HEADER_START@ | |
6 | * | |
7 | * This file contains Original Code and/or Modifications of Original Code | |
8 | * as defined in and that are subject to the Apple Public Source License | |
9 | * Version 2.0 (the 'License'). You may not use this file except in | |
10 | * compliance with the License. Please obtain a copy of the License at | |
11 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
12 | * file. | |
13 | * | |
14 | * The Original Code and all software distributed under the License are | |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
19 | * Please see the License for the specific language governing rights and | |
20 | * limitations under the License. | |
21 | * | |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
24 | ||
25 | #include <sys/types.h> | |
26 | #include <sys/stat.h> | |
27 | #include <sys/mman.h> | |
28 | #include <stdarg.h> | |
29 | #include <stdio.h> | |
30 | #include <fcntl.h> | |
31 | #include <unistd.h> | |
32 | #include <errno.h> | |
33 | ||
34 | #include <vector> | |
35 | #include <set> | |
36 | #include <ext/hash_set> | |
37 | ||
38 | ||
39 | #include "MachOFileAbstraction.hpp" | |
40 | #include "Architectures.hpp" | |
41 | ||
42 | ||
43 | __attribute__((noreturn)) | |
44 | void throwf(const char* format, ...) | |
45 | { | |
46 | va_list list; | |
47 | char* p; | |
48 | va_start(list, format); | |
49 | vasprintf(&p, format, list); | |
50 | va_end(list); | |
51 | ||
52 | const char* t = p; | |
53 | throw t; | |
54 | } | |
55 | ||
56 | ||
57 | template <typename A> | |
58 | class UnwindPrinter | |
59 | { | |
60 | public: | |
61 | static bool validFile(const uint8_t* fileContent); | |
a645023d A |
62 | static UnwindPrinter<A>* make(const uint8_t* fileContent, uint32_t fileLength, |
63 | const char* path, bool showFunctionNames) | |
64 | { return new UnwindPrinter<A>(fileContent, fileLength, | |
65 | path, showFunctionNames); } | |
55e3d2f6 A |
66 | virtual ~UnwindPrinter() {} |
67 | ||
68 | ||
69 | private: | |
70 | typedef typename A::P P; | |
71 | typedef typename A::P::E E; | |
72 | typedef typename A::P::uint_t pint_t; | |
73 | ||
74 | class CStringEquals | |
75 | { | |
76 | public: | |
77 | bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); } | |
78 | }; | |
79 | ||
80 | typedef __gnu_cxx::hash_set<const char*, __gnu_cxx::hash<const char*>, CStringEquals> StringSet; | |
81 | ||
a645023d A |
82 | UnwindPrinter(const uint8_t* fileContent, uint32_t fileLength, |
83 | const char* path, bool showFunctionNames); | |
55e3d2f6 | 84 | bool findUnwindSection(); |
a645023d | 85 | void printUnwindSection(bool showFunctionNames); |
55e3d2f6 A |
86 | void getSymbolTableInfo(); |
87 | const char* functionName(pint_t addr); | |
88 | static const char* archName(); | |
fb24a050 | 89 | static void decode(uint32_t encoding, const uint8_t* funcStart, char* str); |
55e3d2f6 A |
90 | |
91 | const char* fPath; | |
92 | const macho_header<P>* fHeader; | |
93 | uint64_t fLength; | |
94 | const macho_section<P>* fUnwindSection; | |
95 | const char* fStrings; | |
96 | const char* fStringsEnd; | |
97 | const macho_nlist<P>* fSymbols; | |
98 | uint32_t fSymbolCount; | |
99 | pint_t fMachHeaderAddress; | |
100 | }; | |
101 | ||
102 | ||
103 | template <> const char* UnwindPrinter<ppc>::archName() { return "ppc"; } | |
104 | template <> const char* UnwindPrinter<ppc64>::archName() { return "ppc64"; } | |
105 | template <> const char* UnwindPrinter<x86>::archName() { return "i386"; } | |
106 | template <> const char* UnwindPrinter<x86_64>::archName() { return "x86_64"; } | |
107 | template <> const char* UnwindPrinter<arm>::archName() { return "arm"; } | |
108 | ||
109 | template <> | |
110 | bool UnwindPrinter<ppc>::validFile(const uint8_t* fileContent) | |
111 | { | |
112 | const macho_header<P>* header = (const macho_header<P>*)fileContent; | |
113 | if ( header->magic() != MH_MAGIC ) | |
114 | return false; | |
115 | if ( header->cputype() != CPU_TYPE_POWERPC ) | |
116 | return false; | |
117 | switch (header->filetype()) { | |
118 | case MH_EXECUTE: | |
119 | case MH_DYLIB: | |
120 | case MH_BUNDLE: | |
121 | case MH_DYLINKER: | |
122 | return true; | |
123 | } | |
124 | return false; | |
125 | } | |
126 | ||
127 | template <> | |
128 | bool UnwindPrinter<ppc64>::validFile(const uint8_t* fileContent) | |
129 | { | |
130 | const macho_header<P>* header = (const macho_header<P>*)fileContent; | |
131 | if ( header->magic() != MH_MAGIC_64 ) | |
132 | return false; | |
133 | if ( header->cputype() != CPU_TYPE_POWERPC64 ) | |
134 | return false; | |
135 | switch (header->filetype()) { | |
136 | case MH_EXECUTE: | |
137 | case MH_DYLIB: | |
138 | case MH_BUNDLE: | |
139 | case MH_DYLINKER: | |
140 | return true; | |
141 | } | |
142 | return false; | |
143 | } | |
144 | ||
145 | template <> | |
146 | bool UnwindPrinter<x86>::validFile(const uint8_t* fileContent) | |
147 | { | |
148 | const macho_header<P>* header = (const macho_header<P>*)fileContent; | |
149 | if ( header->magic() != MH_MAGIC ) | |
150 | return false; | |
151 | if ( header->cputype() != CPU_TYPE_I386 ) | |
152 | return false; | |
153 | switch (header->filetype()) { | |
154 | case MH_EXECUTE: | |
155 | case MH_DYLIB: | |
156 | case MH_BUNDLE: | |
157 | case MH_DYLINKER: | |
158 | return true; | |
159 | } | |
160 | return false; | |
161 | } | |
162 | ||
163 | template <> | |
164 | bool UnwindPrinter<x86_64>::validFile(const uint8_t* fileContent) | |
165 | { | |
166 | const macho_header<P>* header = (const macho_header<P>*)fileContent; | |
167 | if ( header->magic() != MH_MAGIC_64 ) | |
168 | return false; | |
169 | if ( header->cputype() != CPU_TYPE_X86_64 ) | |
170 | return false; | |
171 | switch (header->filetype()) { | |
172 | case MH_EXECUTE: | |
173 | case MH_DYLIB: | |
174 | case MH_BUNDLE: | |
175 | case MH_DYLINKER: | |
176 | return true; | |
177 | } | |
178 | return false; | |
179 | } | |
180 | ||
181 | template <> | |
182 | bool UnwindPrinter<arm>::validFile(const uint8_t* fileContent) | |
183 | { | |
184 | const macho_header<P>* header = (const macho_header<P>*)fileContent; | |
185 | if ( header->magic() != MH_MAGIC ) | |
186 | return false; | |
187 | if ( header->cputype() != CPU_TYPE_ARM ) | |
188 | return false; | |
189 | switch (header->filetype()) { | |
190 | case MH_EXECUTE: | |
191 | case MH_DYLIB: | |
192 | case MH_BUNDLE: | |
193 | case MH_DYLINKER: | |
194 | return true; | |
195 | } | |
196 | return false; | |
197 | } | |
198 | ||
199 | ||
200 | template <typename A> | |
a645023d | 201 | UnwindPrinter<A>::UnwindPrinter(const uint8_t* fileContent, uint32_t fileLength, const char* path, bool showFunctionNames) |
55e3d2f6 A |
202 | : fHeader(NULL), fLength(fileLength), fUnwindSection(NULL), |
203 | fStrings(NULL), fStringsEnd(NULL), fSymbols(NULL), fSymbolCount(0), fMachHeaderAddress(0) | |
204 | { | |
205 | // sanity check | |
206 | if ( ! validFile(fileContent) ) | |
207 | throw "not a mach-o file that can be checked"; | |
208 | ||
209 | fPath = strdup(path); | |
210 | fHeader = (const macho_header<P>*)fileContent; | |
211 | ||
212 | getSymbolTableInfo(); | |
213 | ||
214 | if ( findUnwindSection() ) | |
a645023d | 215 | printUnwindSection(showFunctionNames); |
55e3d2f6 A |
216 | } |
217 | ||
218 | ||
219 | template <typename A> | |
220 | void UnwindPrinter<A>::getSymbolTableInfo() | |
221 | { | |
222 | const uint8_t* const endOfFile = (uint8_t*)fHeader + fLength; | |
223 | const uint8_t* const endOfLoadCommands = (uint8_t*)fHeader + sizeof(macho_header<P>) + fHeader->sizeofcmds(); | |
224 | const uint32_t cmd_count = fHeader->ncmds(); | |
225 | const macho_load_command<P>* const cmds = (macho_load_command<P>*)((uint8_t*)fHeader + sizeof(macho_header<P>)); | |
226 | const macho_load_command<P>* cmd = cmds; | |
227 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
228 | uint32_t size = cmd->cmdsize(); | |
229 | const uint8_t* endOfCmd = ((uint8_t*)cmd)+cmd->cmdsize(); | |
230 | if ( endOfCmd > endOfLoadCommands ) | |
231 | throwf("load command #%d extends beyond the end of the load commands", i); | |
232 | if ( endOfCmd > endOfFile ) | |
233 | throwf("load command #%d extends beyond the end of the file", i); | |
234 | if ( cmd->cmd() == LC_SYMTAB) { | |
235 | const macho_symtab_command<P>* symtab = (macho_symtab_command<P>*)cmd; | |
236 | fSymbolCount = symtab->nsyms(); | |
237 | fSymbols = (const macho_nlist<P>*)((char*)fHeader + symtab->symoff()); | |
238 | fStrings = (char*)fHeader + symtab->stroff(); | |
239 | fStringsEnd = fStrings + symtab->strsize(); | |
240 | } | |
241 | cmd = (const macho_load_command<P>*)endOfCmd; | |
242 | } | |
243 | } | |
244 | ||
245 | template <typename A> | |
246 | const char* UnwindPrinter<A>::functionName(pint_t addr) | |
247 | { | |
248 | for (uint32_t i=0; i < fSymbolCount; ++i) { | |
249 | uint8_t type = fSymbols[i].n_type(); | |
250 | if ( ((type & N_STAB) == 0) && ((type & N_TYPE) == N_SECT) ) { | |
251 | if ( fSymbols[i].n_value() == addr ) { | |
252 | const char* r = &fStrings[fSymbols[i].n_strx()]; | |
253 | //fprintf(stderr, "addr=0x%08llX, i=%u, n_type=0x%0X, r=%s\n", (long long)(fSymbols[i].n_value()), i, fSymbols[i].n_type(), r); | |
254 | return r; | |
255 | } | |
256 | } | |
257 | } | |
fb24a050 | 258 | return "--anonymous function--"; |
55e3d2f6 A |
259 | } |
260 | ||
261 | ||
262 | ||
263 | template <typename A> | |
264 | bool UnwindPrinter<A>::findUnwindSection() | |
265 | { | |
266 | const uint8_t* const endOfFile = (uint8_t*)fHeader + fLength; | |
267 | const uint8_t* const endOfLoadCommands = (uint8_t*)fHeader + sizeof(macho_header<P>) + fHeader->sizeofcmds(); | |
268 | const uint32_t cmd_count = fHeader->ncmds(); | |
269 | const macho_load_command<P>* const cmds = (macho_load_command<P>*)((uint8_t*)fHeader + sizeof(macho_header<P>)); | |
270 | const macho_load_command<P>* cmd = cmds; | |
271 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
272 | uint32_t size = cmd->cmdsize(); | |
273 | const uint8_t* endOfCmd = ((uint8_t*)cmd)+cmd->cmdsize(); | |
274 | if ( endOfCmd > endOfLoadCommands ) | |
275 | throwf("load command #%d extends beyond the end of the load commands", i); | |
276 | if ( endOfCmd > endOfFile ) | |
277 | throwf("load command #%d extends beyond the end of the file", i); | |
278 | if ( cmd->cmd() == macho_segment_command<P>::CMD ) { | |
279 | const macho_segment_command<P>* segCmd = (const macho_segment_command<P>*)cmd; | |
280 | const macho_section<P>* const sectionsStart = (macho_section<P>*)((char*)segCmd + sizeof(macho_segment_command<P>)); | |
281 | const macho_section<P>* const sectionsEnd = §ionsStart[segCmd->nsects()]; | |
282 | for(const macho_section<P>* sect = sectionsStart; sect < sectionsEnd; ++sect) { | |
283 | if ( (strcmp(sect->sectname(), "__unwind_info") == 0) && (strcmp(sect->segname(), "__TEXT") == 0) ) { | |
284 | fUnwindSection = sect; | |
285 | fMachHeaderAddress = segCmd->vmaddr(); | |
286 | return fUnwindSection; | |
287 | } | |
288 | } | |
289 | } | |
290 | cmd = (const macho_load_command<P>*)endOfCmd; | |
291 | } | |
292 | return false; | |
293 | } | |
294 | ||
fb24a050 A |
295 | #define EXTRACT_BITS(value, mask) \ |
296 | ( (value >> __builtin_ctz(mask)) & (((1 << __builtin_popcount(mask)))-1) ) | |
55e3d2f6 A |
297 | |
298 | ||
fb24a050 A |
299 | template <> |
300 | void UnwindPrinter<x86_64>::decode(uint32_t encoding, const uint8_t* funcStart, char* str) | |
301 | { | |
302 | *str = '\0'; | |
303 | switch ( encoding & UNWIND_X86_64_MODE_MASK ) { | |
304 | case UNWIND_X86_64_MODE_RBP_FRAME: | |
305 | { | |
306 | uint32_t savedRegistersOffset = EXTRACT_BITS(encoding, UNWIND_X86_64_RBP_FRAME_OFFSET); | |
307 | uint32_t savedRegistersLocations = EXTRACT_BITS(encoding, UNWIND_X86_64_RBP_FRAME_REGISTERS); | |
308 | if ( savedRegistersLocations == 0 ) { | |
309 | strcpy(str, "rbp frame, no saved registers"); | |
310 | } | |
311 | else { | |
312 | sprintf(str, "rbp frame, at -%d:", savedRegistersOffset*8); | |
313 | bool needComma = false; | |
314 | for (int i=0; i < 5; ++i) { | |
315 | if ( needComma ) | |
316 | strcat(str, ","); | |
317 | else | |
318 | needComma = true; | |
319 | switch (savedRegistersLocations & 0x7) { | |
320 | case UNWIND_X86_64_REG_NONE: | |
321 | strcat(str, "-"); | |
322 | break; | |
323 | case UNWIND_X86_64_REG_RBX: | |
324 | strcat(str, "rbx"); | |
325 | break; | |
326 | case UNWIND_X86_64_REG_R12: | |
327 | strcat(str, "r12"); | |
328 | break; | |
329 | case UNWIND_X86_64_REG_R13: | |
330 | strcat(str, "r13"); | |
331 | break; | |
332 | case UNWIND_X86_64_REG_R14: | |
333 | strcat(str, "r14"); | |
334 | break; | |
335 | case UNWIND_X86_64_REG_R15: | |
336 | strcat(str, "r15"); | |
337 | break; | |
338 | default: | |
339 | strcat(str, "r?"); | |
340 | } | |
341 | savedRegistersLocations = (savedRegistersLocations >> 3); | |
342 | if ( savedRegistersLocations == 0 ) | |
343 | break; | |
344 | } | |
345 | } | |
346 | } | |
347 | break; | |
348 | case UNWIND_X86_64_MODE_STACK_IMMD: | |
349 | case UNWIND_X86_64_MODE_STACK_IND: | |
350 | { | |
351 | uint32_t stackSize = EXTRACT_BITS(encoding, UNWIND_X86_64_FRAMELESS_STACK_SIZE); | |
352 | uint32_t stackAdjust = EXTRACT_BITS(encoding, UNWIND_X86_64_FRAMELESS_STACK_ADJUST); | |
353 | uint32_t regCount = EXTRACT_BITS(encoding, UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT); | |
354 | uint32_t permutation = EXTRACT_BITS(encoding, UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION); | |
355 | if ( (encoding & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_STACK_IND ) { | |
356 | // stack size is encoded in subl $xxx,%esp instruction | |
357 | uint32_t subl = x86_64::P::E::get32(*((uint32_t*)(funcStart+stackSize))); | |
358 | sprintf(str, "stack size=0x%08X, ", subl + 8*stackAdjust); | |
359 | } | |
360 | else { | |
361 | sprintf(str, "stack size=%d, ", stackSize*8); | |
362 | } | |
363 | if ( regCount == 0 ) { | |
364 | strcat(str, "no registers saved"); | |
365 | } | |
366 | else { | |
367 | int permunreg[6]; | |
368 | switch ( regCount ) { | |
369 | case 6: | |
370 | permunreg[0] = permutation/120; | |
371 | permutation -= (permunreg[0]*120); | |
372 | permunreg[1] = permutation/24; | |
373 | permutation -= (permunreg[1]*24); | |
374 | permunreg[2] = permutation/6; | |
375 | permutation -= (permunreg[2]*6); | |
376 | permunreg[3] = permutation/2; | |
377 | permutation -= (permunreg[3]*2); | |
378 | permunreg[4] = permutation; | |
379 | permunreg[5] = 0; | |
380 | break; | |
381 | case 5: | |
382 | permunreg[0] = permutation/120; | |
383 | permutation -= (permunreg[0]*120); | |
384 | permunreg[1] = permutation/24; | |
385 | permutation -= (permunreg[1]*24); | |
386 | permunreg[2] = permutation/6; | |
387 | permutation -= (permunreg[2]*6); | |
388 | permunreg[3] = permutation/2; | |
389 | permutation -= (permunreg[3]*2); | |
390 | permunreg[4] = permutation; | |
391 | break; | |
392 | case 4: | |
393 | permunreg[0] = permutation/60; | |
394 | permutation -= (permunreg[0]*60); | |
395 | permunreg[1] = permutation/12; | |
396 | permutation -= (permunreg[1]*12); | |
397 | permunreg[2] = permutation/3; | |
398 | permutation -= (permunreg[2]*3); | |
399 | permunreg[3] = permutation; | |
400 | break; | |
401 | case 3: | |
402 | permunreg[0] = permutation/20; | |
403 | permutation -= (permunreg[0]*20); | |
404 | permunreg[1] = permutation/4; | |
405 | permutation -= (permunreg[1]*4); | |
406 | permunreg[2] = permutation; | |
407 | break; | |
408 | case 2: | |
409 | permunreg[0] = permutation/5; | |
410 | permutation -= (permunreg[0]*5); | |
411 | permunreg[1] = permutation; | |
412 | break; | |
413 | case 1: | |
414 | permunreg[0] = permutation; | |
415 | break; | |
416 | } | |
417 | // renumber registers back to standard numbers | |
418 | int registers[6]; | |
419 | bool used[7] = { false, false, false, false, false, false, false }; | |
420 | for (int i=0; i < regCount; ++i) { | |
421 | int renum = 0; | |
422 | for (int u=1; u < 7; ++u) { | |
423 | if ( !used[u] ) { | |
424 | if ( renum == permunreg[i] ) { | |
425 | registers[i] = u; | |
426 | used[u] = true; | |
427 | break; | |
428 | } | |
429 | ++renum; | |
430 | } | |
431 | } | |
432 | } | |
433 | bool needComma = false; | |
434 | for (int i=0; i < regCount; ++i) { | |
435 | if ( needComma ) | |
436 | strcat(str, ","); | |
437 | else | |
438 | needComma = true; | |
439 | switch ( registers[i] ) { | |
440 | case UNWIND_X86_64_REG_RBX: | |
441 | strcat(str, "rbx"); | |
442 | break; | |
443 | case UNWIND_X86_64_REG_R12: | |
444 | strcat(str, "r12"); | |
445 | break; | |
446 | case UNWIND_X86_64_REG_R13: | |
447 | strcat(str, "r13"); | |
448 | break; | |
449 | case UNWIND_X86_64_REG_R14: | |
450 | strcat(str, "r14"); | |
451 | break; | |
452 | case UNWIND_X86_64_REG_R15: | |
453 | strcat(str, "r15"); | |
454 | break; | |
455 | case UNWIND_X86_64_REG_RBP: | |
456 | strcat(str, "rbp"); | |
457 | break; | |
458 | default: | |
459 | strcat(str, "r??"); | |
460 | } | |
461 | } | |
462 | } | |
463 | } | |
464 | break; | |
465 | case UNWIND_X86_64_MODE_DWARF: | |
466 | sprintf(str, "dwarf offset 0x%08X, ", encoding & UNWIND_X86_64_DWARF_SECTION_OFFSET); | |
467 | break; | |
468 | default: | |
469 | if ( encoding == 0 ) | |
470 | strcat(str, "no unwind information"); | |
471 | else | |
472 | strcat(str, "tbd "); | |
473 | } | |
474 | if ( encoding & UNWIND_HAS_LSDA ) { | |
475 | strcat(str, " LSDA"); | |
476 | } | |
477 | ||
478 | } | |
479 | ||
480 | template <> | |
481 | void UnwindPrinter<x86>::decode(uint32_t encoding, const uint8_t* funcStart, char* str) | |
482 | { | |
483 | *str = '\0'; | |
484 | switch ( encoding & UNWIND_X86_MODE_MASK ) { | |
485 | case UNWIND_X86_MODE_EBP_FRAME: | |
486 | { | |
487 | uint32_t savedRegistersOffset = EXTRACT_BITS(encoding, UNWIND_X86_EBP_FRAME_OFFSET); | |
488 | uint32_t savedRegistersLocations = EXTRACT_BITS(encoding, UNWIND_X86_EBP_FRAME_REGISTERS); | |
489 | if ( savedRegistersLocations == 0 ) { | |
490 | strcpy(str, "ebp frame, no saved registers"); | |
491 | } | |
492 | else { | |
493 | sprintf(str, "ebp frame, at -%d:", savedRegistersOffset*4); | |
494 | bool needComma = false; | |
495 | for (int i=0; i < 5; ++i) { | |
496 | if ( needComma ) | |
497 | strcat(str, ","); | |
498 | else | |
499 | needComma = true; | |
500 | switch (savedRegistersLocations & 0x7) { | |
501 | case UNWIND_X86_REG_NONE: | |
502 | strcat(str, "-"); | |
503 | break; | |
504 | case UNWIND_X86_REG_EBX: | |
505 | strcat(str, "ebx"); | |
506 | break; | |
507 | case UNWIND_X86_REG_ECX: | |
508 | strcat(str, "ecx"); | |
509 | break; | |
510 | case UNWIND_X86_REG_EDX: | |
511 | strcat(str, "edx"); | |
512 | break; | |
513 | case UNWIND_X86_REG_EDI: | |
514 | strcat(str, "edi"); | |
515 | break; | |
516 | case UNWIND_X86_REG_ESI: | |
517 | strcat(str, "esi"); | |
518 | break; | |
519 | default: | |
520 | strcat(str, "e??"); | |
521 | } | |
522 | savedRegistersLocations = (savedRegistersLocations >> 3); | |
523 | if ( savedRegistersLocations == 0 ) | |
524 | break; | |
525 | } | |
526 | } | |
527 | } | |
528 | break; | |
529 | case UNWIND_X86_MODE_STACK_IMMD: | |
530 | case UNWIND_X86_MODE_STACK_IND: | |
531 | { | |
532 | uint32_t stackSize = EXTRACT_BITS(encoding, UNWIND_X86_FRAMELESS_STACK_SIZE); | |
533 | uint32_t stackAdjust = EXTRACT_BITS(encoding, UNWIND_X86_FRAMELESS_STACK_ADJUST); | |
534 | uint32_t regCount = EXTRACT_BITS(encoding, UNWIND_X86_FRAMELESS_STACK_REG_COUNT); | |
535 | uint32_t permutation = EXTRACT_BITS(encoding, UNWIND_X86_FRAMELESS_STACK_REG_PERMUTATION); | |
536 | if ( (encoding & UNWIND_X86_MODE_MASK) == UNWIND_X86_MODE_STACK_IND ) { | |
537 | // stack size is encoded in subl $xxx,%esp instruction | |
538 | uint32_t subl = x86::P::E::get32(*((uint32_t*)(funcStart+stackSize))); | |
539 | sprintf(str, "stack size=0x%08X, ", subl+4*stackAdjust); | |
540 | } | |
541 | else { | |
542 | sprintf(str, "stack size=%d, ", stackSize*4); | |
543 | } | |
544 | if ( regCount == 0 ) { | |
545 | strcat(str, "no saved regs"); | |
546 | } | |
547 | else { | |
548 | int permunreg[6]; | |
549 | switch ( regCount ) { | |
550 | case 6: | |
551 | permunreg[0] = permutation/120; | |
552 | permutation -= (permunreg[0]*120); | |
553 | permunreg[1] = permutation/24; | |
554 | permutation -= (permunreg[1]*24); | |
555 | permunreg[2] = permutation/6; | |
556 | permutation -= (permunreg[2]*6); | |
557 | permunreg[3] = permutation/2; | |
558 | permutation -= (permunreg[3]*2); | |
559 | permunreg[4] = permutation; | |
560 | permunreg[5] = 0; | |
561 | break; | |
562 | case 5: | |
563 | permunreg[0] = permutation/120; | |
564 | permutation -= (permunreg[0]*120); | |
565 | permunreg[1] = permutation/24; | |
566 | permutation -= (permunreg[1]*24); | |
567 | permunreg[2] = permutation/6; | |
568 | permutation -= (permunreg[2]*6); | |
569 | permunreg[3] = permutation/2; | |
570 | permutation -= (permunreg[3]*2); | |
571 | permunreg[4] = permutation; | |
572 | break; | |
573 | case 4: | |
574 | permunreg[0] = permutation/60; | |
575 | permutation -= (permunreg[0]*60); | |
576 | permunreg[1] = permutation/12; | |
577 | permutation -= (permunreg[1]*12); | |
578 | permunreg[2] = permutation/3; | |
579 | permutation -= (permunreg[2]*3); | |
580 | permunreg[3] = permutation; | |
581 | break; | |
582 | case 3: | |
583 | permunreg[0] = permutation/20; | |
584 | permutation -= (permunreg[0]*20); | |
585 | permunreg[1] = permutation/4; | |
586 | permutation -= (permunreg[1]*4); | |
587 | permunreg[2] = permutation; | |
588 | break; | |
589 | case 2: | |
590 | permunreg[0] = permutation/5; | |
591 | permutation -= (permunreg[0]*5); | |
592 | permunreg[1] = permutation; | |
593 | break; | |
594 | case 1: | |
595 | permunreg[0] = permutation; | |
596 | break; | |
597 | } | |
598 | // renumber registers back to standard numbers | |
599 | int registers[6]; | |
600 | bool used[7] = { false, false, false, false, false, false, false }; | |
601 | for (int i=0; i < regCount; ++i) { | |
602 | int renum = 0; | |
603 | for (int u=1; u < 7; ++u) { | |
604 | if ( !used[u] ) { | |
605 | if ( renum == permunreg[i] ) { | |
606 | registers[i] = u; | |
607 | used[u] = true; | |
608 | break; | |
609 | } | |
610 | ++renum; | |
611 | } | |
612 | } | |
613 | } | |
614 | bool needComma = false; | |
615 | for (int i=0; i < regCount; ++i) { | |
616 | if ( needComma ) | |
617 | strcat(str, ","); | |
618 | else | |
619 | needComma = true; | |
620 | switch ( registers[i] ) { | |
621 | case UNWIND_X86_REG_EBX: | |
622 | strcat(str, "ebx"); | |
623 | break; | |
624 | case UNWIND_X86_REG_ECX: | |
625 | strcat(str, "ecx"); | |
626 | break; | |
627 | case UNWIND_X86_REG_EDX: | |
628 | strcat(str, "edx"); | |
629 | break; | |
630 | case UNWIND_X86_REG_EDI: | |
631 | strcat(str, "edi"); | |
632 | break; | |
633 | case UNWIND_X86_REG_ESI: | |
634 | strcat(str, "esi"); | |
635 | break; | |
636 | case UNWIND_X86_REG_EBP: | |
637 | strcat(str, "ebp"); | |
638 | break; | |
639 | default: | |
640 | strcat(str, "e??"); | |
641 | } | |
642 | } | |
643 | } | |
644 | } | |
645 | break; | |
646 | case UNWIND_X86_MODE_DWARF: | |
647 | sprintf(str, "dwarf offset 0x%08X, ", encoding & UNWIND_X86_DWARF_SECTION_OFFSET); | |
648 | break; | |
649 | default: | |
650 | if ( encoding == 0 ) | |
651 | strcat(str, "no unwind information"); | |
652 | else | |
653 | strcat(str, "tbd "); | |
654 | } | |
655 | if ( encoding & UNWIND_HAS_LSDA ) { | |
656 | strcat(str, " LSDA"); | |
657 | } | |
658 | ||
659 | } | |
660 | ||
661 | ||
662 | template <typename A> | |
663 | void UnwindPrinter<A>::decode(uint32_t encoding, const uint8_t* funcStart, char* str) | |
664 | { | |
665 | ||
666 | ||
667 | } | |
55e3d2f6 A |
668 | |
669 | template <typename A> | |
a645023d | 670 | void UnwindPrinter<A>::printUnwindSection(bool showFunctionNames) |
55e3d2f6 A |
671 | { |
672 | const uint8_t* sectionContent = (uint8_t*)fHeader + fUnwindSection->offset(); | |
673 | macho_unwind_info_section_header<P>* sectionHeader = (macho_unwind_info_section_header<P>*)(sectionContent); | |
674 | ||
675 | printf("Arch: %s, Section: __TEXT,__unwind_info (addr=0x%08llX, size=0x%08llX, fileOffset=0x%08X)\n", | |
676 | archName(), fUnwindSection->addr(), fUnwindSection->size(), fUnwindSection->offset()); | |
677 | printf("\tversion=0x%08X\n", sectionHeader->version()); | |
678 | printf("\tcommonEncodingsArraySectionOffset=0x%08X\n", sectionHeader->commonEncodingsArraySectionOffset()); | |
679 | printf("\tcommonEncodingsArrayCount=0x%08X\n", sectionHeader->commonEncodingsArrayCount()); | |
680 | printf("\tpersonalityArraySectionOffset=0x%08X\n", sectionHeader->personalityArraySectionOffset()); | |
681 | printf("\tpersonalityArrayCount=0x%08X\n", sectionHeader->personalityArrayCount()); | |
682 | printf("\tindexSectionOffset=0x%08X\n", sectionHeader->indexSectionOffset()); | |
683 | printf("\tindexCount=0x%08X\n", sectionHeader->indexCount()); | |
684 | printf("\tcommon encodings: (count=%u)\n", sectionHeader->commonEncodingsArrayCount()); | |
685 | const uint32_t* commonEncodings = (uint32_t*)§ionContent[sectionHeader->commonEncodingsArraySectionOffset()]; | |
686 | for (uint32_t i=0; i < sectionHeader->commonEncodingsArrayCount(); ++i) { | |
fb24a050 | 687 | printf("\t\tencoding[%3u]=0x%08X\n", i, A::P::E::get32(commonEncodings[i])); |
55e3d2f6 A |
688 | } |
689 | printf("\tpersonalities: (count=%u)\n", sectionHeader->personalityArrayCount()); | |
690 | const uint32_t* personalityArray = (uint32_t*)§ionContent[sectionHeader->personalityArraySectionOffset()]; | |
691 | for (uint32_t i=0; i < sectionHeader->personalityArrayCount(); ++i) { | |
692 | printf("\t\t[%2u]=0x%08X\n", i+1, A::P::E::get32(personalityArray[i])); | |
693 | } | |
694 | printf("\tfirst level index: (count=%u)\n", sectionHeader->indexCount()); | |
695 | macho_unwind_info_section_header_index_entry<P>* indexes = (macho_unwind_info_section_header_index_entry<P>*)§ionContent[sectionHeader->indexSectionOffset()]; | |
696 | for (uint32_t i=0; i < sectionHeader->indexCount(); ++i) { | |
697 | printf("\t\t[%2u] funcOffset=0x%08X, pageOffset=0x%08X, lsdaOffset=0x%08X\n", | |
698 | i, indexes[i].functionOffset(), indexes[i].secondLevelPagesSectionOffset(), indexes[i].lsdaIndexArraySectionOffset()); | |
699 | } | |
700 | uint32_t lsdaIndexArraySectionOffset = indexes[0].lsdaIndexArraySectionOffset(); | |
701 | uint32_t lsdaIndexArrayEndSectionOffset = indexes[sectionHeader->indexCount()-1].lsdaIndexArraySectionOffset(); | |
702 | uint32_t lsdaIndexArrayCount = (lsdaIndexArrayEndSectionOffset-lsdaIndexArraySectionOffset)/sizeof(macho_unwind_info_section_header_lsda_index_entry<P>); | |
703 | printf("\tLSDA table: (section offset 0x%08X, count=%u)\n", lsdaIndexArraySectionOffset, lsdaIndexArrayCount); | |
704 | macho_unwind_info_section_header_lsda_index_entry<P>* lindex = (macho_unwind_info_section_header_lsda_index_entry<P>*)§ionContent[lsdaIndexArraySectionOffset]; | |
705 | for (uint32_t i=0; i < lsdaIndexArrayCount; ++i) { | |
a645023d A |
706 | const char* name = showFunctionNames ? functionName(lindex[i].functionOffset()+fMachHeaderAddress) : ""; |
707 | printf("\t\t[%3u] funcOffset=0x%08X, lsdaOffset=0x%08X, %s\n", i, lindex[i].functionOffset(), lindex[i].lsdaOffset(), name); | |
55e3d2f6 A |
708 | if ( *(((uint8_t*)fHeader) + lindex[i].lsdaOffset()) != 0xFF ) |
709 | fprintf(stderr, "BAD LSDA entry (does not start with 0xFF) for %s\n", functionName(lindex[i].functionOffset()+fMachHeaderAddress)); | |
710 | } | |
711 | for (uint32_t i=0; i < sectionHeader->indexCount()-1; ++i) { | |
712 | printf("\tsecond level index[%u] sectionOffset=0x%08X, count=%u, fileOffset=0x%08X\n", i, indexes[i].secondLevelPagesSectionOffset(), | |
713 | sectionHeader->indexCount(), fUnwindSection->offset()+indexes[i].secondLevelPagesSectionOffset()); | |
714 | macho_unwind_info_regular_second_level_page_header<P>* page = (macho_unwind_info_regular_second_level_page_header<P>*)§ionContent[indexes[i].secondLevelPagesSectionOffset()]; | |
715 | if ( page->kind() == UNWIND_SECOND_LEVEL_REGULAR ) { | |
716 | printf("\t\tkind=UNWIND_SECOND_LEVEL_REGULAR\n"); | |
717 | printf("\t\tentryPageOffset=0x%08X\n", page->entryPageOffset()); | |
718 | printf("\t\tentryCount=0x%08X\n", page->entryCount()); | |
719 | const macho_unwind_info_regular_second_level_entry<P>* entry = (macho_unwind_info_regular_second_level_entry<P>*)((char*)page+page->entryPageOffset()); | |
720 | for (uint32_t j=0; j < page->entryCount(); ++j) { | |
fb24a050 | 721 | uint32_t funcOffset = entry[j].functionOffset(); |
55e3d2f6 A |
722 | if ( entry[j].encoding() & UNWIND_HAS_LSDA ) { |
723 | // verify there is a corresponding entry in lsda table | |
724 | bool found = false; | |
725 | for (uint32_t k=0; k < lsdaIndexArrayCount; ++k) { | |
726 | if ( lindex[k].functionOffset() == funcOffset ) { | |
727 | found = true; | |
728 | break; | |
729 | } | |
730 | } | |
731 | if ( !found ) { | |
732 | fprintf(stderr, "MISSING LSDA entry for %s\n", functionName(funcOffset+fMachHeaderAddress)); | |
733 | } | |
734 | } | |
fb24a050 A |
735 | char encodingString[100]; |
736 | decode(entry[j].encoding(), ((const uint8_t*)fHeader)+funcOffset, encodingString); | |
a645023d | 737 | const char* name = showFunctionNames ? functionName(funcOffset+fMachHeaderAddress) : ""; |
fb24a050 | 738 | printf("\t\t\t[%3u] funcOffset=0x%08X, encoding=0x%08X (%-40s) %s\n", |
a645023d | 739 | j, funcOffset, entry[j].encoding(), encodingString, name); |
55e3d2f6 A |
740 | } |
741 | } | |
742 | else if ( page->kind() == UNWIND_SECOND_LEVEL_COMPRESSED ) { | |
743 | macho_unwind_info_compressed_second_level_page_header<P>* cp = (macho_unwind_info_compressed_second_level_page_header<P>*)page; | |
744 | printf("\t\tkind=UNWIND_SECOND_LEVEL_COMPRESSED\n"); | |
745 | printf("\t\tentryPageOffset=0x%08X\n", cp->entryPageOffset()); | |
746 | printf("\t\tentryCount=0x%08X\n", cp->entryCount()); | |
747 | printf("\t\tencodingsPageOffset=0x%08X\n", cp->encodingsPageOffset()); | |
748 | printf("\t\tencodingsCount=0x%08X\n", cp->encodingsCount()); | |
749 | const uint32_t* entries = (uint32_t*)(((uint8_t*)page)+cp->entryPageOffset()); | |
750 | const uint32_t* encodings = (uint32_t*)(((uint8_t*)page)+cp->encodingsPageOffset()); | |
751 | const uint32_t baseFunctionOffset = indexes[i].functionOffset(); | |
752 | for (uint32_t j=0; j < cp->entryCount(); ++j) { | |
753 | uint8_t encodingIndex = UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(entries[j]); | |
754 | uint32_t encoding; | |
755 | if ( encodingIndex < sectionHeader->commonEncodingsArrayCount() ) | |
756 | encoding = A::P::E::get32(commonEncodings[encodingIndex]); | |
757 | else | |
758 | encoding = A::P::E::get32(encodings[encodingIndex-sectionHeader->commonEncodingsArrayCount()]); | |
fb24a050 | 759 | char encodingString[100]; |
55e3d2f6 | 760 | uint32_t funcOff = UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(entries[j])+baseFunctionOffset; |
fb24a050 | 761 | decode(encoding, ((const uint8_t*)fHeader)+funcOff, encodingString); |
a645023d | 762 | const char* name = showFunctionNames ? functionName(funcOff+fMachHeaderAddress) : ""; |
55e3d2f6 A |
763 | if ( encoding & UNWIND_HAS_LSDA ) { |
764 | // verify there is a corresponding entry in lsda table | |
765 | bool found = false; | |
766 | for (uint32_t k=0; k < lsdaIndexArrayCount; ++k) { | |
767 | if ( lindex[k].functionOffset() == funcOff ) { | |
768 | found = true; | |
769 | break; | |
770 | } | |
771 | } | |
772 | if ( !found ) { | |
773 | fprintf(stderr, "MISSING LSDA entry for %s\n", name); | |
774 | } | |
775 | } | |
fb24a050 A |
776 | printf("\t\t\t[%3u] funcOffset=0x%08X, encoding[%3u]=0x%08X (%-40s) %s\n", |
777 | j, funcOff, encodingIndex, encoding, encodingString, name); | |
55e3d2f6 A |
778 | } |
779 | } | |
780 | else { | |
781 | fprintf(stderr, "\t\tbad page header\n"); | |
782 | } | |
783 | } | |
784 | ||
785 | } | |
786 | ||
a645023d | 787 | static void dump(const char* path, const std::set<cpu_type_t>& onlyArchs, bool showFunctionNames) |
55e3d2f6 A |
788 | { |
789 | struct stat stat_buf; | |
790 | ||
791 | try { | |
792 | int fd = ::open(path, O_RDONLY, 0); | |
793 | if ( fd == -1 ) | |
794 | throw "cannot open file"; | |
795 | if ( ::fstat(fd, &stat_buf) != 0 ) | |
796 | throwf("fstat(%s) failed, errno=%d\n", path, errno); | |
797 | uint32_t length = stat_buf.st_size; | |
798 | uint8_t* p = (uint8_t*)::mmap(NULL, stat_buf.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); | |
799 | if ( p == ((uint8_t*)(-1)) ) | |
800 | throw "cannot map file"; | |
801 | ::close(fd); | |
802 | const mach_header* mh = (mach_header*)p; | |
803 | if ( mh->magic == OSSwapBigToHostInt32(FAT_MAGIC) ) { | |
804 | const struct fat_header* fh = (struct fat_header*)p; | |
805 | const struct fat_arch* archs = (struct fat_arch*)(p + sizeof(struct fat_header)); | |
806 | for (unsigned long i=0; i < OSSwapBigToHostInt32(fh->nfat_arch); ++i) { | |
807 | size_t offset = OSSwapBigToHostInt32(archs[i].offset); | |
808 | size_t size = OSSwapBigToHostInt32(archs[i].size); | |
809 | unsigned int cputype = OSSwapBigToHostInt32(archs[i].cputype); | |
810 | if ( onlyArchs.count(cputype) ) { | |
811 | switch(cputype) { | |
812 | case CPU_TYPE_POWERPC: | |
813 | if ( UnwindPrinter<ppc>::validFile(p + offset) ) | |
a645023d | 814 | UnwindPrinter<ppc>::make(p + offset, size, path, showFunctionNames); |
55e3d2f6 A |
815 | else |
816 | throw "in universal file, ppc slice does not contain ppc mach-o"; | |
817 | break; | |
818 | case CPU_TYPE_I386: | |
819 | if ( UnwindPrinter<x86>::validFile(p + offset) ) | |
a645023d | 820 | UnwindPrinter<x86>::make(p + offset, size, path, showFunctionNames); |
55e3d2f6 A |
821 | else |
822 | throw "in universal file, i386 slice does not contain i386 mach-o"; | |
823 | break; | |
824 | case CPU_TYPE_POWERPC64: | |
825 | if ( UnwindPrinter<ppc64>::validFile(p + offset) ) | |
a645023d | 826 | UnwindPrinter<ppc64>::make(p + offset, size, path, showFunctionNames); |
55e3d2f6 A |
827 | else |
828 | throw "in universal file, ppc64 slice does not contain ppc64 mach-o"; | |
829 | break; | |
830 | case CPU_TYPE_X86_64: | |
831 | if ( UnwindPrinter<x86_64>::validFile(p + offset) ) | |
a645023d | 832 | UnwindPrinter<x86_64>::make(p + offset, size, path, showFunctionNames); |
55e3d2f6 A |
833 | else |
834 | throw "in universal file, x86_64 slice does not contain x86_64 mach-o"; | |
835 | break; | |
836 | case CPU_TYPE_ARM: | |
837 | if ( UnwindPrinter<arm>::validFile(p + offset) ) | |
a645023d | 838 | UnwindPrinter<arm>::make(p + offset, size, path, showFunctionNames); |
55e3d2f6 A |
839 | else |
840 | throw "in universal file, arm slice does not contain arm mach-o"; | |
841 | break; | |
842 | default: | |
843 | throwf("in universal file, unknown architecture slice 0x%x\n", cputype); | |
844 | } | |
845 | } | |
846 | } | |
847 | } | |
848 | else if ( UnwindPrinter<x86>::validFile(p) && onlyArchs.count(CPU_TYPE_I386) ) { | |
a645023d | 849 | UnwindPrinter<x86>::make(p, length, path, showFunctionNames); |
55e3d2f6 A |
850 | } |
851 | else if ( UnwindPrinter<ppc>::validFile(p) && onlyArchs.count(CPU_TYPE_POWERPC) ) { | |
a645023d | 852 | UnwindPrinter<ppc>::make(p, length, path, showFunctionNames); |
55e3d2f6 A |
853 | } |
854 | else if ( UnwindPrinter<ppc64>::validFile(p) && onlyArchs.count(CPU_TYPE_POWERPC64) ) { | |
a645023d | 855 | UnwindPrinter<ppc64>::make(p, length, path, showFunctionNames); |
55e3d2f6 A |
856 | } |
857 | else if ( UnwindPrinter<x86_64>::validFile(p) && onlyArchs.count(CPU_TYPE_X86_64) ) { | |
a645023d | 858 | UnwindPrinter<x86_64>::make(p, length, path, showFunctionNames); |
55e3d2f6 A |
859 | } |
860 | else if ( UnwindPrinter<arm>::validFile(p) && onlyArchs.count(CPU_TYPE_ARM) ) { | |
a645023d | 861 | UnwindPrinter<arm>::make(p, length, path, showFunctionNames); |
55e3d2f6 A |
862 | } |
863 | else { | |
864 | throw "not a known file type"; | |
865 | } | |
866 | } | |
867 | catch (const char* msg) { | |
868 | throwf("%s in %s", msg, path); | |
869 | } | |
870 | } | |
871 | ||
872 | ||
873 | int main(int argc, const char* argv[]) | |
874 | { | |
875 | std::set<cpu_type_t> onlyArchs; | |
876 | std::vector<const char*> files; | |
a645023d | 877 | bool showFunctionNames = true; |
55e3d2f6 A |
878 | |
879 | try { | |
880 | for(int i=1; i < argc; ++i) { | |
881 | const char* arg = argv[i]; | |
882 | if ( arg[0] == '-' ) { | |
883 | if ( strcmp(arg, "-arch") == 0 ) { | |
884 | const char* arch = argv[++i]; | |
885 | if ( strcmp(arch, "ppc") == 0 ) | |
886 | onlyArchs.insert(CPU_TYPE_POWERPC); | |
887 | else if ( strcmp(arch, "ppc64") == 0 ) | |
888 | onlyArchs.insert(CPU_TYPE_POWERPC64); | |
889 | else if ( strcmp(arch, "i386") == 0 ) | |
890 | onlyArchs.insert(CPU_TYPE_I386); | |
891 | else if ( strcmp(arch, "x86_64") == 0 ) | |
892 | onlyArchs.insert(CPU_TYPE_X86_64); | |
893 | else if ( strcmp(arch, "arm") == 0 ) | |
894 | onlyArchs.insert(CPU_TYPE_ARM); | |
895 | else | |
896 | throwf("unknown architecture %s", arch); | |
897 | } | |
a645023d A |
898 | else if ( strcmp(arg, "-no_symbols") == 0 ) { |
899 | showFunctionNames = false; | |
900 | } | |
55e3d2f6 A |
901 | else { |
902 | throwf("unknown option: %s\n", arg); | |
903 | } | |
904 | } | |
905 | else { | |
906 | files.push_back(arg); | |
907 | } | |
908 | } | |
909 | ||
910 | // use all architectures if no restrictions specified | |
911 | if ( onlyArchs.size() == 0 ) { | |
912 | onlyArchs.insert(CPU_TYPE_POWERPC); | |
913 | onlyArchs.insert(CPU_TYPE_POWERPC64); | |
914 | onlyArchs.insert(CPU_TYPE_I386); | |
915 | onlyArchs.insert(CPU_TYPE_X86_64); | |
916 | onlyArchs.insert(CPU_TYPE_ARM); | |
917 | } | |
918 | ||
919 | // process each file | |
920 | for(std::vector<const char*>::iterator it=files.begin(); it != files.end(); ++it) { | |
a645023d | 921 | dump(*it, onlyArchs, showFunctionNames); |
55e3d2f6 A |
922 | } |
923 | ||
924 | } | |
925 | catch (const char* msg) { | |
926 | fprintf(stderr, "UnwindDump failed: %s\n", msg); | |
927 | return 1; | |
928 | } | |
929 | ||
930 | return 0; | |
931 | } | |
932 | ||
933 | ||
934 |