1 /* od-macho.c -- dump information about an Mach-O object file.
2 Copyright (C) 2011-2017 Free Software Foundation, Inc.
3 Written by Tristan Gingold, Adacore.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
25 #include "safe-ctype.h"
32 #include "mach-o/external.h"
33 #include "mach-o/codesign.h"
34 #include "mach-o/unwind.h"
36 /* Index of the options in the options[] array. */
41 #define OPT_DYSYMTAB 4
42 #define OPT_CODESIGN 5
43 #define OPT_SEG_SPLIT_INFO 6
44 #define OPT_COMPACT_UNWIND 7
45 #define OPT_FUNCTION_STARTS 8
46 #define OPT_DATA_IN_CODE 9
47 #define OPT_TWOLEVEL_HINTS 10
48 #define OPT_DYLD_INFO 11
50 /* List of actions. */
51 static struct objdump_private_option options[] =
59 { "seg_split_info", 0 },
60 { "compact_unwind", 0 },
61 { "function_starts", 0 },
62 { "data_in_code", 0 },
63 { "twolevel_hints", 0 },
71 mach_o_help (FILE *stream)
75 header Display the file header\n\
76 section Display the segments and sections commands\n\
77 map Display the section map\n\
78 load Display the load commands\n\
79 dysymtab Display the dynamic symbol table\n\
80 codesign Display code signature\n\
81 seg_split_info Display segment split info\n\
82 compact_unwind Display compact unwinding info\n\
83 function_starts Display start address of functions\n\
84 data_in_code Display data in code entries\n\
85 twolevel_hints Display the two-level namespace lookup hints table\n\
86 dyld_info Display dyld information\n\
90 /* Return TRUE if ABFD is handled. */
93 mach_o_filter (bfd *abfd)
95 return bfd_get_flavour (abfd) == bfd_target_mach_o_flavour;
98 static const bfd_mach_o_xlat_name bfd_mach_o_cpu_name[] =
100 { "vax", BFD_MACH_O_CPU_TYPE_VAX },
101 { "mc680x0", BFD_MACH_O_CPU_TYPE_MC680x0 },
102 { "i386", BFD_MACH_O_CPU_TYPE_I386 },
103 { "mips", BFD_MACH_O_CPU_TYPE_MIPS },
104 { "mc98000", BFD_MACH_O_CPU_TYPE_MC98000 },
105 { "hppa", BFD_MACH_O_CPU_TYPE_HPPA },
106 { "arm", BFD_MACH_O_CPU_TYPE_ARM },
107 { "mc88000", BFD_MACH_O_CPU_TYPE_MC88000 },
108 { "sparc", BFD_MACH_O_CPU_TYPE_SPARC },
109 { "i860", BFD_MACH_O_CPU_TYPE_I860 },
110 { "alpha", BFD_MACH_O_CPU_TYPE_ALPHA },
111 { "powerpc", BFD_MACH_O_CPU_TYPE_POWERPC },
112 { "powerpc_64", BFD_MACH_O_CPU_TYPE_POWERPC_64 },
113 { "x86_64", BFD_MACH_O_CPU_TYPE_X86_64 },
114 { "arm64", BFD_MACH_O_CPU_TYPE_ARM64 },
118 static const bfd_mach_o_xlat_name bfd_mach_o_filetype_name[] =
120 { "object", BFD_MACH_O_MH_OBJECT },
121 { "execute", BFD_MACH_O_MH_EXECUTE },
122 { "fvmlib", BFD_MACH_O_MH_FVMLIB },
123 { "core", BFD_MACH_O_MH_CORE },
124 { "preload", BFD_MACH_O_MH_PRELOAD },
125 { "dylib", BFD_MACH_O_MH_DYLIB },
126 { "dylinker", BFD_MACH_O_MH_DYLINKER },
127 { "bundle", BFD_MACH_O_MH_BUNDLE },
128 { "dylib_stub", BFD_MACH_O_MH_DYLIB_STUB },
129 { "dym", BFD_MACH_O_MH_DSYM },
130 { "kext_bundle", BFD_MACH_O_MH_KEXT_BUNDLE },
134 static const bfd_mach_o_xlat_name bfd_mach_o_header_flags_name[] =
136 { "noundefs", BFD_MACH_O_MH_NOUNDEFS },
137 { "incrlink", BFD_MACH_O_MH_INCRLINK },
138 { "dyldlink", BFD_MACH_O_MH_DYLDLINK },
139 { "bindatload", BFD_MACH_O_MH_BINDATLOAD },
140 { "prebound", BFD_MACH_O_MH_PREBOUND },
141 { "split_segs", BFD_MACH_O_MH_SPLIT_SEGS },
142 { "lazy_init", BFD_MACH_O_MH_LAZY_INIT },
143 { "twolevel", BFD_MACH_O_MH_TWOLEVEL },
144 { "force_flat", BFD_MACH_O_MH_FORCE_FLAT },
145 { "nomultidefs", BFD_MACH_O_MH_NOMULTIDEFS },
146 { "nofixprebinding", BFD_MACH_O_MH_NOFIXPREBINDING },
147 { "prebindable", BFD_MACH_O_MH_PREBINDABLE },
148 { "allmodsbound", BFD_MACH_O_MH_ALLMODSBOUND },
149 { "subsections_via_symbols", BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS },
150 { "canonical", BFD_MACH_O_MH_CANONICAL },
151 { "weak_defines", BFD_MACH_O_MH_WEAK_DEFINES },
152 { "binds_to_weak", BFD_MACH_O_MH_BINDS_TO_WEAK },
153 { "allow_stack_execution", BFD_MACH_O_MH_ALLOW_STACK_EXECUTION },
154 { "root_safe", BFD_MACH_O_MH_ROOT_SAFE },
155 { "setuid_safe", BFD_MACH_O_MH_SETUID_SAFE },
156 { "no_reexported_dylibs", BFD_MACH_O_MH_NO_REEXPORTED_DYLIBS },
157 { "pie", BFD_MACH_O_MH_PIE },
158 { "dead_strippable_dylib", BFD_MACH_O_MH_DEAD_STRIPPABLE_DYLIB },
159 { "has_tlv", BFD_MACH_O_MH_HAS_TLV_DESCRIPTORS },
160 { "no_heap_execution", BFD_MACH_O_MH_NO_HEAP_EXECUTION },
161 { "app_extension_safe", BFD_MACH_O_MH_APP_EXTENSION_SAFE },
165 static const bfd_mach_o_xlat_name bfd_mach_o_load_command_name[] =
167 { "segment", BFD_MACH_O_LC_SEGMENT},
168 { "symtab", BFD_MACH_O_LC_SYMTAB},
169 { "symseg", BFD_MACH_O_LC_SYMSEG},
170 { "thread", BFD_MACH_O_LC_THREAD},
171 { "unixthread", BFD_MACH_O_LC_UNIXTHREAD},
172 { "loadfvmlib", BFD_MACH_O_LC_LOADFVMLIB},
173 { "idfvmlib", BFD_MACH_O_LC_IDFVMLIB},
174 { "ident", BFD_MACH_O_LC_IDENT},
175 { "fvmfile", BFD_MACH_O_LC_FVMFILE},
176 { "prepage", BFD_MACH_O_LC_PREPAGE},
177 { "dysymtab", BFD_MACH_O_LC_DYSYMTAB},
178 { "load_dylib", BFD_MACH_O_LC_LOAD_DYLIB},
179 { "id_dylib", BFD_MACH_O_LC_ID_DYLIB},
180 { "load_dylinker", BFD_MACH_O_LC_LOAD_DYLINKER},
181 { "id_dylinker", BFD_MACH_O_LC_ID_DYLINKER},
182 { "prebound_dylib", BFD_MACH_O_LC_PREBOUND_DYLIB},
183 { "routines", BFD_MACH_O_LC_ROUTINES},
184 { "sub_framework", BFD_MACH_O_LC_SUB_FRAMEWORK},
185 { "sub_umbrella", BFD_MACH_O_LC_SUB_UMBRELLA},
186 { "sub_client", BFD_MACH_O_LC_SUB_CLIENT},
187 { "sub_library", BFD_MACH_O_LC_SUB_LIBRARY},
188 { "twolevel_hints", BFD_MACH_O_LC_TWOLEVEL_HINTS},
189 { "prebind_cksum", BFD_MACH_O_LC_PREBIND_CKSUM},
190 { "load_weak_dylib", BFD_MACH_O_LC_LOAD_WEAK_DYLIB},
191 { "segment_64", BFD_MACH_O_LC_SEGMENT_64},
192 { "routines_64", BFD_MACH_O_LC_ROUTINES_64},
193 { "uuid", BFD_MACH_O_LC_UUID},
194 { "rpath", BFD_MACH_O_LC_RPATH},
195 { "code_signature", BFD_MACH_O_LC_CODE_SIGNATURE},
196 { "segment_split_info", BFD_MACH_O_LC_SEGMENT_SPLIT_INFO},
197 { "reexport_dylib", BFD_MACH_O_LC_REEXPORT_DYLIB},
198 { "lazy_load_dylib", BFD_MACH_O_LC_LAZY_LOAD_DYLIB},
199 { "encryption_info", BFD_MACH_O_LC_ENCRYPTION_INFO},
200 { "dyld_info", BFD_MACH_O_LC_DYLD_INFO},
201 { "load_upward_lib", BFD_MACH_O_LC_LOAD_UPWARD_DYLIB},
202 { "version_min_macosx", BFD_MACH_O_LC_VERSION_MIN_MACOSX},
203 { "version_min_iphoneos", BFD_MACH_O_LC_VERSION_MIN_IPHONEOS},
204 { "function_starts", BFD_MACH_O_LC_FUNCTION_STARTS},
205 { "dyld_environment", BFD_MACH_O_LC_DYLD_ENVIRONMENT},
206 { "main", BFD_MACH_O_LC_MAIN},
207 { "data_in_code", BFD_MACH_O_LC_DATA_IN_CODE},
208 { "source_version", BFD_MACH_O_LC_SOURCE_VERSION},
209 { "dylib_code_sign_drs", BFD_MACH_O_LC_DYLIB_CODE_SIGN_DRS},
210 { "encryption_info_64", BFD_MACH_O_LC_ENCRYPTION_INFO_64},
211 { "linker_options", BFD_MACH_O_LC_LINKER_OPTIONS},
212 { "linker_optimization_hint", BFD_MACH_O_LC_LINKER_OPTIMIZATION_HINT},
216 static const bfd_mach_o_xlat_name bfd_mach_o_thread_x86_name[] =
218 { "thread_state32", BFD_MACH_O_x86_THREAD_STATE32},
219 { "float_state32", BFD_MACH_O_x86_FLOAT_STATE32},
220 { "exception_state32", BFD_MACH_O_x86_EXCEPTION_STATE32},
221 { "thread_state64", BFD_MACH_O_x86_THREAD_STATE64},
222 { "float_state64", BFD_MACH_O_x86_FLOAT_STATE64},
223 { "exception_state64", BFD_MACH_O_x86_EXCEPTION_STATE64},
224 { "thread_state", BFD_MACH_O_x86_THREAD_STATE},
225 { "float_state", BFD_MACH_O_x86_FLOAT_STATE},
226 { "exception_state", BFD_MACH_O_x86_EXCEPTION_STATE},
227 { "debug_state32", BFD_MACH_O_x86_DEBUG_STATE32},
228 { "debug_state64", BFD_MACH_O_x86_DEBUG_STATE64},
229 { "debug_state", BFD_MACH_O_x86_DEBUG_STATE},
230 { "state_none", BFD_MACH_O_x86_THREAD_STATE_NONE},
235 bfd_mach_o_print_flags (const bfd_mach_o_xlat_name *table,
240 for (; table->name; table++)
242 if (table->val & val)
246 printf ("%s", table->name);
255 printf ("0x%lx", val);
262 /* Print a bfd_uint64_t, using a platform independent style. */
265 printf_uint64 (bfd_uint64_t v)
267 printf ("0x%08lx%08lx",
268 (unsigned long)((v >> 16) >> 16), (unsigned long)(v & 0xffffffffUL));
272 bfd_mach_o_get_name_or_null (const bfd_mach_o_xlat_name *table,
275 for (; table->name; table++)
276 if (table->val == val)
282 bfd_mach_o_get_name (const bfd_mach_o_xlat_name *table, unsigned long val)
284 const char *res = bfd_mach_o_get_name_or_null (table, val);
293 dump_header (bfd *abfd)
295 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
296 bfd_mach_o_header *h = &mdata->header;
298 fputs (_("Mach-O header:\n"), stdout);
299 printf (_(" magic : %08lx\n"), h->magic);
300 printf (_(" cputype : %08lx (%s)\n"), h->cputype,
301 bfd_mach_o_get_name (bfd_mach_o_cpu_name, h->cputype));
302 printf (_(" cpusubtype: %08lx\n"), h->cpusubtype);
303 printf (_(" filetype : %08lx (%s)\n"),
305 bfd_mach_o_get_name (bfd_mach_o_filetype_name, h->filetype));
306 printf (_(" ncmds : %08lx (%lu)\n"), h->ncmds, h->ncmds);
307 printf (_(" sizeofcmds: %08lx (%lu)\n"), h->sizeofcmds, h->sizeofcmds);
308 printf (_(" flags : %08lx ("), h->flags);
309 bfd_mach_o_print_flags (bfd_mach_o_header_flags_name, h->flags);
310 fputs (_(")\n"), stdout);
311 printf (_(" reserved : %08x\n"), h->reserved);
316 disp_segment_prot (unsigned int prot)
318 putchar (prot & BFD_MACH_O_PROT_READ ? 'r' : '-');
319 putchar (prot & BFD_MACH_O_PROT_WRITE ? 'w' : '-');
320 putchar (prot & BFD_MACH_O_PROT_EXECUTE ? 'x' : '-');
324 dump_section_map (bfd *abfd)
326 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
327 bfd_mach_o_load_command *cmd;
328 unsigned int sec_nbr = 0;
330 fputs (_("Segments and Sections:\n"), stdout);
331 fputs (_(" #: Segment name Section name Address\n"), stdout);
333 for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
335 bfd_mach_o_segment_command *seg;
336 bfd_mach_o_section *sec;
338 if (cmd->type != BFD_MACH_O_LC_SEGMENT
339 && cmd->type != BFD_MACH_O_LC_SEGMENT_64)
342 seg = &cmd->command.segment;
344 printf ("[Segment %-16s ", seg->segname);
345 printf_vma (seg->vmaddr);
347 printf_vma (seg->vmaddr + seg->vmsize - 1);
349 disp_segment_prot (seg->initprot);
352 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
354 printf ("%02u: %-16s %-16s ", ++sec_nbr,
355 sec->segname, sec->sectname);
356 printf_vma (sec->addr);
358 printf_vma (sec->size);
359 printf (" %08lx\n", sec->flags);
365 dump_section_header (bfd *abfd ATTRIBUTE_UNUSED, bfd_mach_o_section *sec)
367 printf (" Section: %-16s %-16s (bfdname: %s)\n",
368 sec->sectname, sec->segname, sec->bfdsection->name);
370 printf_vma (sec->addr);
372 printf_vma (sec->size);
373 printf (" offset: ");
374 printf_vma (sec->offset);
376 printf (" align: %ld", sec->align);
377 printf (" nreloc: %lu reloff: ", sec->nreloc);
378 printf_vma (sec->reloff);
380 printf (" flags: %08lx (type: %s", sec->flags,
381 bfd_mach_o_get_name (bfd_mach_o_section_type_name,
382 sec->flags & BFD_MACH_O_SECTION_TYPE_MASK));
384 bfd_mach_o_print_flags (bfd_mach_o_section_attribute_name,
385 sec->flags & BFD_MACH_O_SECTION_ATTRIBUTES_MASK);
387 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
389 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
390 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
391 case BFD_MACH_O_S_SYMBOL_STUBS:
392 printf (" first indirect sym: %lu", sec->reserved1);
393 printf (" (%u entries)",
394 bfd_mach_o_section_get_nbr_indirect (abfd, sec));
397 printf (" reserved1: 0x%lx", sec->reserved1);
400 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
402 case BFD_MACH_O_S_SYMBOL_STUBS:
403 printf (" stub size: %lu", sec->reserved2);
406 printf (" reserved2: 0x%lx", sec->reserved2);
409 printf (" reserved3: 0x%lx\n", sec->reserved3);
413 dump_segment (bfd *abfd ATTRIBUTE_UNUSED, bfd_mach_o_load_command *cmd)
415 bfd_mach_o_segment_command *seg = &cmd->command.segment;
416 bfd_mach_o_section *sec;
418 printf (" name: %16s", *seg->segname ? seg->segname : "*none*");
419 printf (" nsects: %lu", seg->nsects);
420 printf (" flags: %lx", seg->flags);
421 printf (" initprot: ");
422 disp_segment_prot (seg->initprot);
423 printf (" maxprot: ");
424 disp_segment_prot (seg->maxprot);
426 printf (" vmaddr: ");
427 printf_vma (seg->vmaddr);
428 printf (" vmsize: ");
429 printf_vma (seg->vmsize);
431 printf (" fileoff: ");
432 printf_vma (seg->fileoff);
433 printf (" filesize: ");
434 printf_vma ((bfd_vma)seg->filesize);
435 printf (" endoff: ");
436 printf_vma ((bfd_vma)(seg->fileoff + seg->filesize));
438 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
439 dump_section_header (abfd, sec);
443 dump_dysymtab (bfd *abfd, bfd_mach_o_load_command *cmd, bfd_boolean verbose)
445 bfd_mach_o_dysymtab_command *dysymtab = &cmd->command.dysymtab;
446 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
449 printf (" local symbols: idx: %10lu num: %-8lu",
450 dysymtab->ilocalsym, dysymtab->nlocalsym);
451 printf (" (nxtidx: %lu)\n",
452 dysymtab->ilocalsym + dysymtab->nlocalsym);
453 printf (" external symbols: idx: %10lu num: %-8lu",
454 dysymtab->iextdefsym, dysymtab->nextdefsym);
455 printf (" (nxtidx: %lu)\n",
456 dysymtab->iextdefsym + dysymtab->nextdefsym);
457 printf (" undefined symbols: idx: %10lu num: %-8lu",
458 dysymtab->iundefsym, dysymtab->nundefsym);
459 printf (" (nxtidx: %lu)\n",
460 dysymtab->iundefsym + dysymtab->nundefsym);
461 printf (" table of content: off: 0x%08lx num: %-8lu",
462 dysymtab->tocoff, dysymtab->ntoc);
463 printf (" (endoff: 0x%08lx)\n",
464 dysymtab->tocoff + dysymtab->ntoc * BFD_MACH_O_TABLE_OF_CONTENT_SIZE);
465 printf (" module table: off: 0x%08lx num: %-8lu",
466 dysymtab->modtaboff, dysymtab->nmodtab);
467 printf (" (endoff: 0x%08lx)\n",
468 dysymtab->modtaboff + dysymtab->nmodtab
469 * (mdata->header.version == 2 ?
470 BFD_MACH_O_DYLIB_MODULE_64_SIZE : BFD_MACH_O_DYLIB_MODULE_SIZE));
471 printf (" external reference table: off: 0x%08lx num: %-8lu",
472 dysymtab->extrefsymoff, dysymtab->nextrefsyms);
473 printf (" (endoff: 0x%08lx)\n",
474 dysymtab->extrefsymoff
475 + dysymtab->nextrefsyms * BFD_MACH_O_REFERENCE_SIZE);
476 printf (" indirect symbol table: off: 0x%08lx num: %-8lu",
477 dysymtab->indirectsymoff, dysymtab->nindirectsyms);
478 printf (" (endoff: 0x%08lx)\n",
479 dysymtab->indirectsymoff
480 + dysymtab->nindirectsyms * BFD_MACH_O_INDIRECT_SYMBOL_SIZE);
481 printf (" external relocation table: off: 0x%08lx num: %-8lu",
482 dysymtab->extreloff, dysymtab->nextrel);
483 printf (" (endoff: 0x%08lx)\n",
484 dysymtab->extreloff + dysymtab->nextrel * BFD_MACH_O_RELENT_SIZE);
485 printf (" local relocation table: off: 0x%08lx num: %-8lu",
486 dysymtab->locreloff, dysymtab->nlocrel);
487 printf (" (endoff: 0x%08lx)\n",
488 dysymtab->locreloff + dysymtab->nlocrel * BFD_MACH_O_RELENT_SIZE);
493 if (dysymtab->ntoc > 0
494 || dysymtab->nindirectsyms > 0
495 || dysymtab->nextrefsyms > 0)
497 /* Try to read the symbols to display the toc or indirect symbols. */
498 bfd_mach_o_read_symtab_symbols (abfd);
500 else if (dysymtab->nmodtab > 0)
502 /* Try to read the strtab to display modules name. */
503 bfd_mach_o_read_symtab_strtab (abfd);
506 for (i = 0; i < dysymtab->nmodtab; i++)
508 bfd_mach_o_dylib_module *module = &dysymtab->dylib_module[i];
509 printf (" module %u:\n", i);
510 printf (" name: %lu", module->module_name_idx);
511 if (mdata->symtab && mdata->symtab->strtab)
513 mdata->symtab->strtab + module->module_name_idx);
515 printf (" extdefsym: idx: %8lu num: %lu\n",
516 module->iextdefsym, module->nextdefsym);
517 printf (" refsym: idx: %8lu num: %lu\n",
518 module->irefsym, module->nrefsym);
519 printf (" localsym: idx: %8lu num: %lu\n",
520 module->ilocalsym, module->nlocalsym);
521 printf (" extrel: idx: %8lu num: %lu\n",
522 module->iextrel, module->nextrel);
523 printf (" init: idx: %8u num: %u\n",
524 module->iinit, module->ninit);
525 printf (" term: idx: %8u num: %u\n",
526 module->iterm, module->nterm);
527 printf (" objc_module_info: addr: ");
528 printf_vma (module->objc_module_info_addr);
529 printf (" size: %lu\n", module->objc_module_info_size);
532 if (dysymtab->ntoc > 0)
534 bfd_mach_o_symtab_command *symtab = mdata->symtab;
536 printf (" table of content: (symbol/module)\n");
537 for (i = 0; i < dysymtab->ntoc; i++)
539 bfd_mach_o_dylib_table_of_content *toc = &dysymtab->dylib_toc[i];
541 printf (" %4u: ", i);
542 if (symtab && symtab->symbols && toc->symbol_index < symtab->nsyms)
544 const char *name = symtab->symbols[toc->symbol_index].symbol.name;
545 printf ("%s (%lu)", name ? name : "*invalid*",
549 printf ("%lu", toc->symbol_index);
552 if (symtab && symtab->strtab
553 && toc->module_index < dysymtab->nmodtab)
555 bfd_mach_o_dylib_module *mod;
556 mod = &dysymtab->dylib_module[toc->module_index];
558 symtab->strtab + mod->module_name_idx,
562 printf ("%lu", toc->module_index);
568 if (dysymtab->nindirectsyms != 0)
570 printf (" indirect symbols:\n");
572 for (i = 0; i < mdata->nsects; i++)
574 bfd_mach_o_section *sec = mdata->sections[i];
575 unsigned int j, first, last;
576 bfd_mach_o_symtab_command *symtab = mdata->symtab;
580 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
582 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
583 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
584 case BFD_MACH_O_S_SYMBOL_STUBS:
585 first = sec->reserved1;
586 last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
588 entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
589 printf (" for section %s.%s:\n",
590 sec->segname, sec->sectname);
591 for (j = first; j < last; j++)
593 unsigned int isym = dysymtab->indirect_syms[j];
597 printf (" %5u: 0x%08x", j, isym);
598 if (isym & BFD_MACH_O_INDIRECT_SYMBOL_LOCAL)
600 if (isym & BFD_MACH_O_INDIRECT_SYMBOL_ABS)
601 printf (" ABSOLUTE");
602 if (symtab && symtab->symbols
603 && isym < symtab->nsyms
604 && symtab->symbols[isym].symbol.name)
605 printf (" %s", symtab->symbols[isym].symbol.name);
615 if (dysymtab->nextrefsyms > 0)
617 bfd_mach_o_symtab_command *symtab = mdata->symtab;
619 printf (" external reference table: (symbol flags)\n");
620 for (i = 0; i < dysymtab->nextrefsyms; i++)
622 bfd_mach_o_dylib_reference *ref = &dysymtab->ext_refs[i];
624 printf (" %4u: %5lu 0x%02lx", i, ref->isym, ref->flags);
625 if (symtab && symtab->symbols
626 && ref->isym < symtab->nsyms
627 && symtab->symbols[ref->isym].symbol.name)
628 printf (" %s", symtab->symbols[ref->isym].symbol.name);
636 load_and_dump (bfd *abfd, ufile_ptr off, unsigned int len,
637 void (*dump)(bfd *abfd, unsigned char *buf, unsigned int len,
647 if (bfd_seek (abfd, off, SEEK_SET) == 0
648 && bfd_bread (buf, len, abfd) == len)
649 dump (abfd, buf, len, off);
657 static const bfd_mach_o_xlat_name bfd_mach_o_dyld_rebase_type_name[] =
659 { "pointer", BFD_MACH_O_REBASE_TYPE_POINTER },
660 { "text_abs32", BFD_MACH_O_REBASE_TYPE_TEXT_ABSOLUTE32 },
661 { "text_pcrel32", BFD_MACH_O_REBASE_TYPE_TEXT_PCREL32 },
666 dump_dyld_info_rebase (bfd *abfd, unsigned char *buf, unsigned int len,
667 ufile_ptr off ATTRIBUTE_UNUSED)
670 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
671 unsigned int ptrsize = mdata->header.version == 2 ? 8 : 4;
673 for (i = 0; i < len; )
675 unsigned char b = buf[i++];
676 unsigned char imm = b & BFD_MACH_O_REBASE_IMMEDIATE_MASK;
680 printf (" [0x%04x] 0x%02x: ", i, b);
681 switch (b & BFD_MACH_O_REBASE_OPCODE_MASK)
683 case BFD_MACH_O_REBASE_OPCODE_DONE:
686 case BFD_MACH_O_REBASE_OPCODE_SET_TYPE_IMM:
687 printf ("set_type %s\n",
688 bfd_mach_o_get_name (bfd_mach_o_dyld_rebase_type_name, imm));
690 case BFD_MACH_O_REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
691 leb = read_leb128 (buf + i, &leblen, 0, buf + len);
692 printf ("set segment: %u and offset: 0x%08x\n",
693 imm, (unsigned) leb);
696 case BFD_MACH_O_REBASE_OPCODE_ADD_ADDR_ULEB:
697 leb = read_leb128 (buf + i, &leblen, 0, buf + len);
698 printf ("add addr uleb: 0x%08x\n", (unsigned) leb);
701 case BFD_MACH_O_REBASE_OPCODE_ADD_ADDR_IMM_SCALED:
702 printf ("add addr imm scaled: %u\n", imm * ptrsize);
704 case BFD_MACH_O_REBASE_OPCODE_DO_REBASE_IMM_TIMES:
705 printf ("rebase imm times: %u\n", imm);
707 case BFD_MACH_O_REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
708 leb = read_leb128 (buf + i, &leblen, 0, buf + len);
709 printf ("rebase uleb times: %u\n", (unsigned) leb);
712 case BFD_MACH_O_REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
713 leb = read_leb128 (buf + i, &leblen, 0, buf + len);
714 printf ("rebase add addr uleb: %u\n", (unsigned) leb);
717 case BFD_MACH_O_REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
718 leb = read_leb128 (buf + i, &leblen, 0, buf + len);
719 printf ("rebase uleb times (%u)", (unsigned) leb);
721 leb = read_leb128 (buf + i, &leblen, 0, buf + len);
722 printf (" skipping uleb (%u)\n", (unsigned) leb);
726 printf ("unknown\n");
730 printf (" rebase commands without end!\n");
734 dump_dyld_info_bind (bfd *abfd, unsigned char *buf, unsigned int len,
735 ufile_ptr off ATTRIBUTE_UNUSED)
738 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
739 unsigned int ptrsize = mdata->header.version == 2 ? 8 : 4;
741 for (i = 0; i < len; )
743 unsigned char b = buf[i++];
744 unsigned char imm = b & BFD_MACH_O_BIND_IMMEDIATE_MASK;
748 printf (" [0x%04x] 0x%02x: ", i, b);
749 switch (b & BFD_MACH_O_BIND_OPCODE_MASK)
751 case BFD_MACH_O_BIND_OPCODE_DONE:
754 case BFD_MACH_O_BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
755 printf ("set dylib ordinal imm: %u\n", imm);
757 case BFD_MACH_O_BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
758 leb = read_leb128 (buf + i, &leblen, 0, buf + len);
759 printf ("set dylib ordinal uleb: %u\n", imm);
762 case BFD_MACH_O_BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
763 imm = (imm != 0) ? imm | BFD_MACH_O_BIND_OPCODE_MASK : imm;
764 printf ("set dylib special imm: %d\n", imm);
766 case BFD_MACH_O_BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
767 printf ("set symbol trailing flags imm: 0x%02x, ", imm);
768 for (; i < len && buf[i] != 0; i++)
769 putchar (buf[i] >= ' ' && buf[i] < 0x7f ? buf[i] : '?');
773 case BFD_MACH_O_BIND_OPCODE_SET_TYPE_IMM:
774 /* Kludge: use the same table as rebase type. */
775 printf ("set_type %s\n",
776 bfd_mach_o_get_name (bfd_mach_o_dyld_rebase_type_name, imm));
778 case BFD_MACH_O_BIND_OPCODE_SET_ADDEND_SLEB:
781 svma = read_leb128 (buf + i, &leblen, 0, buf + len);
782 printf ("set addend sleb: 0x%08x\n", (unsigned) svma);
786 case BFD_MACH_O_BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
787 leb = read_leb128 (buf + i, &leblen, 0, buf + len);
788 printf ("set segment: %u and offset: 0x%08x\n",
789 imm, (unsigned) leb);
792 case BFD_MACH_O_BIND_OPCODE_ADD_ADDR_ULEB:
793 leb = read_leb128 (buf + i, &leblen, 0, buf + len);
794 printf ("add addr uleb: 0x%08x\n", (unsigned) leb);
797 case BFD_MACH_O_BIND_OPCODE_DO_BIND:
798 printf ("do bind\n");
800 case BFD_MACH_O_BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
801 leb = read_leb128 (buf + i, &leblen, 0, buf + len);
802 printf ("do bind add addr uleb: 0x%08x\n", (unsigned) leb);
805 case BFD_MACH_O_BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
806 printf ("do bind add addr imm scaled: %u\n", imm * ptrsize);
808 case BFD_MACH_O_BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
809 leb = read_leb128 (buf + i, &leblen, 0, buf + len);
810 printf ("do bind uleb times (%u)", (unsigned) leb);
812 leb = read_leb128 (buf + i, &leblen, 0, buf + len);
813 printf (" skipping uleb (%u)\n", (unsigned) leb);
817 printf ("unknown\n");
821 printf (" bind commands without end!\n");
824 struct export_info_data
826 const unsigned char *name;
827 struct export_info_data *next;
831 dump_dyld_info_export_1 (bfd *abfd, unsigned char *buf, unsigned int len,
832 unsigned int off, struct export_info_data *parent,
833 struct export_info_data *base)
837 unsigned int child_count;
840 size = read_leb128 (buf + off, &leblen, 0, buf + len);
846 struct export_info_data *d;
848 flags = read_leb128 (buf + off, &leblen, 0, buf + len);
852 switch (flags & BFD_MACH_O_EXPORT_SYMBOL_FLAGS_KIND_MASK)
854 case BFD_MACH_O_EXPORT_SYMBOL_FLAGS_KIND_REGULAR:
857 case BFD_MACH_O_EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL:
864 putchar ((flags & BFD_MACH_O_EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION) ?
867 if (flags & BFD_MACH_O_EXPORT_SYMBOL_FLAGS_REEXPORT)
871 lib = read_leb128 (buf + off, &leblen, 0, buf + len);
874 fputs (" [reexport] ", stdout);
875 for (d = base; d != NULL; d = d->next)
876 printf ("%s", d->name);
878 fputs (" (", stdout);
881 fputs ((const char *)buf + off, stdout);
883 off += strlen ((const char *)buf + off);
885 printf ("from dylib %u)\n", (unsigned) lib);
893 offset = read_leb128 (buf + off, &leblen, 0, buf + len);
896 if (flags & BFD_MACH_O_EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
898 resolv = read_leb128 (buf + off, &leblen, 0, buf + len);
902 printf (" 0x%08x ", (unsigned) offset);
903 for (d = base; d != NULL; d = d->next)
904 printf ("%s", d->name);
905 if (flags & BFD_MACH_O_EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
906 printf (" [resolv: 0x%08x]", (unsigned) resolv);
911 child_count = read_leb128 (buf + off, &leblen, 0, buf + len);
914 for (i = 0; i < child_count; i++)
916 struct export_info_data sub_data;
919 sub_data.name = buf + off;
920 sub_data.next = NULL;
921 parent->next = &sub_data;
923 off += strlen ((const char *)buf + off) + 1;
925 sub_off = read_leb128 (buf + off, &leblen, 0, buf + len);
928 dump_dyld_info_export_1 (abfd, buf, len, sub_off, &sub_data, base);
933 dump_dyld_info_export (bfd *abfd, unsigned char *buf, unsigned int len,
934 ufile_ptr off ATTRIBUTE_UNUSED)
936 struct export_info_data data;
938 data.name = (const unsigned char *) "";
941 printf (" fl offset sym (Flags: Tls Weak)\n");
942 dump_dyld_info_export_1 (abfd, buf, len, 0, &data, &data);
946 dump_dyld_info (bfd *abfd, bfd_mach_o_load_command *cmd,
949 bfd_mach_o_dyld_info_command *dinfo = &cmd->command.dyld_info;
951 printf (" rebase: off: 0x%08x size: %-8u (endoff: 0x%08x)\n",
952 dinfo->rebase_off, dinfo->rebase_size,
953 dinfo->rebase_off + dinfo->rebase_size);
954 printf (" bind: off: 0x%08x size: %-8u (endoff: 0x%08x)\n",
955 dinfo->bind_off, dinfo->bind_size,
956 dinfo->bind_off + dinfo->bind_size);
957 printf (" weak bind: off: 0x%08x size: %-8u (endoff: 0x%08x)\n",
958 dinfo->weak_bind_off, dinfo->weak_bind_size,
959 dinfo->weak_bind_off + dinfo->weak_bind_size);
960 printf (" lazy bind: off: 0x%08x size: %-8u (endoff: 0x%08x)\n",
961 dinfo->lazy_bind_off, dinfo->lazy_bind_size,
962 dinfo->lazy_bind_off + dinfo->lazy_bind_size);
963 printf (" export: off: 0x%08x size: %-8u (endoff: 0x%08x)\n",
964 dinfo->export_off, dinfo->export_size,
965 dinfo->export_off + dinfo->export_size);
970 printf (" rebase:\n");
971 if (!load_and_dump (abfd, dinfo->rebase_off, dinfo->rebase_size,
972 dump_dyld_info_rebase))
973 non_fatal (_("cannot read rebase dyld info"));
976 if (!load_and_dump (abfd, dinfo->bind_off, dinfo->bind_size,
977 dump_dyld_info_bind))
978 non_fatal (_("cannot read bind dyld info"));
980 printf (" weak bind:\n");
981 if (!load_and_dump (abfd, dinfo->weak_bind_off, dinfo->weak_bind_size,
982 dump_dyld_info_bind))
983 non_fatal (_("cannot read weak bind dyld info"));
985 printf (" lazy bind:\n");
986 if (!load_and_dump (abfd, dinfo->lazy_bind_off, dinfo->lazy_bind_size,
987 dump_dyld_info_bind))
988 non_fatal (_("cannot read lazy bind dyld info"));
990 printf (" exported symbols:\n");
991 if (!load_and_dump (abfd, dinfo->export_off, dinfo->export_size,
992 dump_dyld_info_export))
993 non_fatal (_("cannot read export symbols dyld info"));
997 dump_thread (bfd *abfd, bfd_mach_o_load_command *cmd)
999 bfd_mach_o_thread_command *thread = &cmd->command.thread;
1001 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
1002 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1004 printf (" nflavours: %lu\n", thread->nflavours);
1005 for (j = 0; j < thread->nflavours; j++)
1007 bfd_mach_o_thread_flavour *flavour = &thread->flavours[j];
1008 const bfd_mach_o_xlat_name *name_table;
1010 printf (" %2u: flavour: 0x%08lx", j, flavour->flavour);
1011 switch (mdata->header.cputype)
1013 case BFD_MACH_O_CPU_TYPE_I386:
1014 case BFD_MACH_O_CPU_TYPE_X86_64:
1015 name_table = bfd_mach_o_thread_x86_name;
1021 if (name_table != NULL)
1022 printf (": %s", bfd_mach_o_get_name (name_table, flavour->flavour));
1025 printf (" offset: 0x%08lx size: 0x%08lx\n",
1026 flavour->offset, flavour->size);
1027 if (bed->_bfd_mach_o_print_thread)
1029 char *buf = xmalloc (flavour->size);
1031 if (bfd_seek (abfd, flavour->offset, SEEK_SET) == 0
1032 && bfd_bread (buf, flavour->size, abfd) == flavour->size)
1033 (*bed->_bfd_mach_o_print_thread)(abfd, flavour, stdout, buf);
1040 static const bfd_mach_o_xlat_name bfd_mach_o_cs_magic[] =
1042 { "embedded signature", BFD_MACH_O_CS_MAGIC_EMBEDDED_SIGNATURE },
1043 { "requirement", BFD_MACH_O_CS_MAGIC_REQUIREMENT },
1044 { "requirements", BFD_MACH_O_CS_MAGIC_REQUIREMENTS },
1045 { "code directory", BFD_MACH_O_CS_MAGIC_CODEDIRECTORY },
1046 { "embedded entitlements", BFD_MACH_O_CS_MAGIC_EMBEDDED_ENTITLEMENTS },
1047 { "blob wrapper", BFD_MACH_O_CS_MAGIC_BLOB_WRAPPER },
1051 static const bfd_mach_o_xlat_name bfd_mach_o_cs_hash_type[] =
1053 { "no-hash", BFD_MACH_O_CS_NO_HASH },
1054 { "sha1", BFD_MACH_O_CS_HASH_SHA1 },
1055 { "sha256", BFD_MACH_O_CS_HASH_SHA256 },
1056 { "skein 160", BFD_MACH_O_CS_HASH_PRESTANDARD_SKEIN_160x256 },
1057 { "skein 256", BFD_MACH_O_CS_HASH_PRESTANDARD_SKEIN_256x512 },
1062 dump_code_signature_blob (bfd *abfd, const unsigned char *buf, unsigned int len);
1065 dump_code_signature_superblob (bfd *abfd ATTRIBUTE_UNUSED,
1066 const unsigned char *buf, unsigned int len)
1073 printf (_(" [bad block length]\n"));
1076 count = bfd_getb32 (buf + 8);
1077 printf (ngettext (" %u index entry:\n",
1078 " %u index entries:\n",
1081 if (len < 12 + 8 * count)
1083 printf (_(" [bad block length]\n"));
1086 for (i = 0; i < count; i++)
1091 type = bfd_getb32 (buf + 12 + 8 * i);
1092 off = bfd_getb32 (buf + 12 + 8 * i + 4);
1093 printf (_(" index entry %u: type: %08x, offset: %08x\n"),
1096 dump_code_signature_blob (abfd, buf + off, len - off);
1101 swap_code_codedirectory_v1_in
1102 (const struct mach_o_codesign_codedirectory_external_v1 *src,
1103 struct mach_o_codesign_codedirectory_v1 *dst)
1105 dst->version = bfd_getb32 (src->version);
1106 dst->flags = bfd_getb32 (src->flags);
1107 dst->hash_offset = bfd_getb32 (src->hash_offset);
1108 dst->ident_offset = bfd_getb32 (src->ident_offset);
1109 dst->nbr_special_slots = bfd_getb32 (src->nbr_special_slots);
1110 dst->nbr_code_slots = bfd_getb32 (src->nbr_code_slots);
1111 dst->code_limit = bfd_getb32 (src->code_limit);
1112 dst->hash_size = src->hash_size[0];
1113 dst->hash_type = src->hash_type[0];
1114 dst->spare1 = src->spare1[0];
1115 dst->page_size = src->page_size[0];
1116 dst->spare2 = bfd_getb32 (src->spare2);
1120 hexdump (unsigned int start, unsigned int len,
1121 const unsigned char *buf)
1125 for (i = 0; i < len; i += 16)
1127 printf ("%08x:", start + i);
1128 for (j = 0; j < 16; j++)
1130 fputc (j == 8 ? '-' : ' ', stdout);
1132 printf ("%02x", buf[i + j]);
1134 fputs (" ", stdout);
1136 fputc (' ', stdout);
1137 for (j = 0; j < 16; j++)
1140 fputc (ISPRINT (buf[i + j]) ? buf[i + j] : '.', stdout);
1142 fputc (' ', stdout);
1144 fputc ('\n', stdout);
1149 dump_code_signature_codedirectory (bfd *abfd ATTRIBUTE_UNUSED,
1150 const unsigned char *buf, unsigned int len)
1152 struct mach_o_codesign_codedirectory_v1 cd;
1155 if (len < sizeof (struct mach_o_codesign_codedirectory_external_v1))
1157 printf (_(" [bad block length]\n"));
1161 swap_code_codedirectory_v1_in
1162 ((const struct mach_o_codesign_codedirectory_external_v1 *) (buf + 8), &cd);
1164 printf (_(" version: %08x\n"), cd.version);
1165 printf (_(" flags: %08x\n"), cd.flags);
1166 printf (_(" hash offset: %08x\n"), cd.hash_offset);
1167 id = (const char *) buf + cd.ident_offset;
1168 printf (_(" ident offset: %08x (- %08x)\n"),
1169 cd.ident_offset, cd.ident_offset + (unsigned) strlen (id) + 1);
1170 printf (_(" identity: %s\n"), id);
1171 printf (_(" nbr special slots: %08x (at offset %08x)\n"),
1172 cd.nbr_special_slots,
1173 cd.hash_offset - cd.nbr_special_slots * cd.hash_size);
1174 printf (_(" nbr code slots: %08x\n"), cd.nbr_code_slots);
1175 printf (_(" code limit: %08x\n"), cd.code_limit);
1176 printf (_(" hash size: %02x\n"), cd.hash_size);
1177 printf (_(" hash type: %02x (%s)\n"),
1179 bfd_mach_o_get_name (bfd_mach_o_cs_hash_type, cd.hash_type));
1180 printf (_(" spare1: %02x\n"), cd.spare1);
1181 printf (_(" page size: %02x\n"), cd.page_size);
1182 printf (_(" spare2: %08x\n"), cd.spare2);
1183 if (cd.version >= 0x20100)
1184 printf (_(" scatter offset: %08x\n"),
1185 (unsigned) bfd_getb32 (buf + 44));
1189 dump_code_signature_blob (bfd *abfd, const unsigned char *buf, unsigned int len)
1192 unsigned int length;
1196 printf (_(" [truncated block]\n"));
1199 magic = bfd_getb32 (buf);
1200 length = bfd_getb32 (buf + 4);
1201 if (magic == 0 || length == 0)
1204 printf (_(" magic : %08x (%s)\n"), magic,
1205 bfd_mach_o_get_name (bfd_mach_o_cs_magic, magic));
1206 printf (_(" length: %08x\n"), length);
1209 printf (_(" [bad block length]\n"));
1215 case BFD_MACH_O_CS_MAGIC_EMBEDDED_SIGNATURE:
1216 dump_code_signature_superblob (abfd, buf, length);
1218 case BFD_MACH_O_CS_MAGIC_CODEDIRECTORY:
1219 dump_code_signature_codedirectory (abfd, buf, length);
1222 hexdump (0, length - 8, buf + 8);
1229 dump_code_signature (bfd *abfd, bfd_mach_o_linkedit_command *cmd)
1231 unsigned char *buf = xmalloc (cmd->datasize);
1234 if (bfd_seek (abfd, cmd->dataoff, SEEK_SET) != 0
1235 || bfd_bread (buf, cmd->datasize, abfd) != cmd->datasize)
1237 non_fatal (_("cannot read code signature data"));
1241 for (off = 0; off < cmd->datasize;)
1245 len = dump_code_signature_blob (abfd, buf + off, cmd->datasize - off);
1255 dump_segment_split_info (bfd *abfd, bfd_mach_o_linkedit_command *cmd)
1257 unsigned char *buf = xmalloc (cmd->datasize);
1262 if (bfd_seek (abfd, cmd->dataoff, SEEK_SET) != 0
1263 || bfd_bread (buf, cmd->datasize, abfd) != cmd->datasize)
1265 non_fatal (_("cannot read segment split info"));
1269 if (buf[cmd->datasize - 1] != 0)
1271 non_fatal (_("segment split info is not nul terminated"));
1279 printf (_(" 32 bit pointers:\n"));
1282 printf (_(" 64 bit pointers:\n"));
1285 printf (_(" PPC hi-16:\n"));
1288 printf (_(" Unhandled location type %u\n"), buf[0]);
1291 for (p = buf + 1; *p != 0; p += len)
1293 addr += read_leb128 (p, &len, 0, buf + cmd->datasize);
1294 fputs (" ", stdout);
1295 bfd_printf_vma (abfd, addr);
1302 dump_function_starts (bfd *abfd, bfd_mach_o_linkedit_command *cmd)
1304 unsigned char *buf = xmalloc (cmd->datasize);
1305 unsigned char *end_buf = buf + cmd->datasize;
1309 if (bfd_seek (abfd, cmd->dataoff, SEEK_SET) != 0
1310 || bfd_bread (buf, cmd->datasize, abfd) != cmd->datasize)
1312 non_fatal (_("cannot read function starts"));
1317 /* Function starts are delta encoded, starting from the base address. */
1318 addr = bfd_mach_o_get_base_address (abfd);
1323 unsigned int shift = 0;
1325 if (*p == 0 || p == end_buf)
1329 unsigned char b = *p++;
1331 delta |= (b & 0x7f) << shift;
1332 if ((b & 0x80) == 0)
1336 fputs (" [truncated]\n", stdout);
1343 fputs (" ", stdout);
1344 bfd_printf_vma (abfd, addr);
1350 static const bfd_mach_o_xlat_name data_in_code_kind_name[] =
1352 { "data", BFD_MACH_O_DICE_KIND_DATA },
1353 { "1 byte jump table", BFD_MACH_O_DICE_JUMP_TABLES8 },
1354 { "2 bytes jump table", BFD_MACH_O_DICE_JUMP_TABLES16 },
1355 { "4 bytes jump table", BFD_MACH_O_DICE_JUMP_TABLES32 },
1356 { "4 bytes abs jump table", BFD_MACH_O_DICE_ABS_JUMP_TABLES32 },
1361 dump_data_in_code (bfd *abfd, bfd_mach_o_linkedit_command *cmd)
1366 if (cmd->datasize == 0)
1368 printf (" no data_in_code entries\n");
1372 buf = xmalloc (cmd->datasize);
1373 if (bfd_seek (abfd, cmd->dataoff, SEEK_SET) != 0
1374 || bfd_bread (buf, cmd->datasize, abfd) != cmd->datasize)
1376 non_fatal (_("cannot read data_in_code"));
1381 printf (" offset length kind\n");
1382 for (p = buf; p < buf + cmd->datasize; )
1384 struct mach_o_data_in_code_entry_external *dice;
1385 unsigned int offset;
1386 unsigned int length;
1389 dice = (struct mach_o_data_in_code_entry_external *) p;
1391 offset = bfd_get_32 (abfd, dice->offset);
1392 length = bfd_get_16 (abfd, dice->length);
1393 kind = bfd_get_16 (abfd, dice->kind);
1395 printf (" 0x%08x 0x%04x 0x%04x %s\n", offset, length, kind,
1396 bfd_mach_o_get_name (data_in_code_kind_name, kind));
1398 p += sizeof (*dice);
1404 dump_twolevel_hints (bfd *abfd, bfd_mach_o_twolevel_hints_command *cmd)
1406 size_t sz = 4 * cmd->nhints;
1411 if (bfd_seek (abfd, cmd->offset, SEEK_SET) != 0
1412 || bfd_bread (buf, sz, abfd) != sz)
1414 non_fatal (_("cannot read twolevel hints"));
1419 for (p = buf; p < buf + sz; p += 4)
1422 unsigned int isub_image;
1425 v = bfd_get_32 (abfd, p);
1426 if (bfd_big_endian (abfd))
1428 isub_image = (v >> 24) & 0xff;
1429 itoc = v & 0xffffff;
1433 isub_image = v & 0xff;
1434 itoc = (v >> 8) & 0xffffff;
1437 printf (" %3u %8u\n", isub_image, itoc);
1443 dump_load_command (bfd *abfd, bfd_mach_o_load_command *cmd,
1444 unsigned int idx, bfd_boolean verbose)
1446 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1447 const char *cmd_name;
1449 cmd_name = bfd_mach_o_get_name_or_null
1450 (bfd_mach_o_load_command_name, cmd->type);
1451 printf ("Load command #%-2u (size: %3u, offset: %4u): ",
1452 idx, cmd->len, cmd->offset);
1453 if (cmd_name == NULL)
1454 printf ("0x%02x\n", cmd->type);
1456 printf ("%s\n", cmd_name);
1460 case BFD_MACH_O_LC_SEGMENT:
1461 case BFD_MACH_O_LC_SEGMENT_64:
1462 dump_segment (abfd, cmd);
1464 case BFD_MACH_O_LC_UUID:
1466 bfd_mach_o_uuid_command *uuid = &cmd->command.uuid;
1470 for (j = 0; j < sizeof (uuid->uuid); j ++)
1471 printf (" %02x", uuid->uuid[j]);
1475 case BFD_MACH_O_LC_LOAD_DYLIB:
1476 case BFD_MACH_O_LC_LAZY_LOAD_DYLIB:
1477 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1478 case BFD_MACH_O_LC_REEXPORT_DYLIB:
1479 case BFD_MACH_O_LC_ID_DYLIB:
1480 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
1482 bfd_mach_o_dylib_command *dylib = &cmd->command.dylib;
1483 printf (" name: %s\n", dylib->name_str);
1484 printf (" time stamp: 0x%08lx\n",
1486 printf (" current version: 0x%08lx\n",
1487 dylib->current_version);
1488 printf (" comptibility version: 0x%08lx\n",
1489 dylib->compatibility_version);
1492 case BFD_MACH_O_LC_LOAD_DYLINKER:
1493 case BFD_MACH_O_LC_ID_DYLINKER:
1494 printf (" %s\n", cmd->command.dylinker.name_str);
1496 case BFD_MACH_O_LC_DYLD_ENVIRONMENT:
1497 printf (" %s\n", cmd->command.dylinker.name_str);
1499 case BFD_MACH_O_LC_SYMTAB:
1501 bfd_mach_o_symtab_command *symtab = &cmd->command.symtab;
1502 printf (" symoff: 0x%08x nsyms: %8u (endoff: 0x%08x)\n",
1503 symtab->symoff, symtab->nsyms,
1504 symtab->symoff + symtab->nsyms
1505 * (mdata->header.version == 2
1506 ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE));
1507 printf (" stroff: 0x%08x strsize: %8u (endoff: 0x%08x)\n",
1508 symtab->stroff, symtab->strsize,
1509 symtab->stroff + symtab->strsize);
1512 case BFD_MACH_O_LC_DYSYMTAB:
1513 dump_dysymtab (abfd, cmd, verbose);
1515 case BFD_MACH_O_LC_LOADFVMLIB:
1516 case BFD_MACH_O_LC_IDFVMLIB:
1518 bfd_mach_o_fvmlib_command *fvmlib = &cmd->command.fvmlib;
1519 printf (" fvmlib: %s\n", fvmlib->name_str);
1520 printf (" minor version: 0x%08x\n", fvmlib->minor_version);
1521 printf (" header address: 0x%08x\n", fvmlib->header_addr);
1524 case BFD_MACH_O_LC_CODE_SIGNATURE:
1525 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
1526 case BFD_MACH_O_LC_FUNCTION_STARTS:
1527 case BFD_MACH_O_LC_DATA_IN_CODE:
1528 case BFD_MACH_O_LC_DYLIB_CODE_SIGN_DRS:
1530 bfd_mach_o_linkedit_command *linkedit = &cmd->command.linkedit;
1532 (" dataoff: 0x%08lx datasize: 0x%08lx (endoff: 0x%08lx)\n",
1533 linkedit->dataoff, linkedit->datasize,
1534 linkedit->dataoff + linkedit->datasize);
1539 case BFD_MACH_O_LC_CODE_SIGNATURE:
1540 dump_code_signature (abfd, linkedit);
1542 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
1543 dump_segment_split_info (abfd, linkedit);
1545 case BFD_MACH_O_LC_FUNCTION_STARTS:
1546 dump_function_starts (abfd, linkedit);
1548 case BFD_MACH_O_LC_DATA_IN_CODE:
1549 dump_data_in_code (abfd, linkedit);
1556 case BFD_MACH_O_LC_SUB_FRAMEWORK:
1557 case BFD_MACH_O_LC_SUB_UMBRELLA:
1558 case BFD_MACH_O_LC_SUB_LIBRARY:
1559 case BFD_MACH_O_LC_SUB_CLIENT:
1560 case BFD_MACH_O_LC_RPATH:
1562 bfd_mach_o_str_command *strc = &cmd->command.str;
1563 printf (" %s\n", strc->str);
1566 case BFD_MACH_O_LC_THREAD:
1567 case BFD_MACH_O_LC_UNIXTHREAD:
1568 dump_thread (abfd, cmd);
1570 case BFD_MACH_O_LC_ENCRYPTION_INFO:
1572 bfd_mach_o_encryption_info_command *cryp =
1573 &cmd->command.encryption_info;
1574 printf (" cryptoff: 0x%08x cryptsize: 0x%08x (endoff 0x%08x)"
1576 cryp->cryptoff, cryp->cryptsize,
1577 cryp->cryptoff + cryp->cryptsize,
1581 case BFD_MACH_O_LC_DYLD_INFO:
1582 dump_dyld_info (abfd, cmd, verbose);
1584 case BFD_MACH_O_LC_VERSION_MIN_MACOSX:
1585 case BFD_MACH_O_LC_VERSION_MIN_IPHONEOS:
1587 bfd_mach_o_version_min_command *ver = &cmd->command.version_min;
1589 printf (" %u.%u.%u\n", ver->rel, ver->maj, ver->min);
1592 case BFD_MACH_O_LC_SOURCE_VERSION:
1594 bfd_mach_o_source_version_command *version =
1595 &cmd->command.source_version;
1596 printf (" version a.b.c.d.e: %u.%u.%u.%u.%u\n",
1597 version->a, version->b, version->c, version->d, version->e);
1600 case BFD_MACH_O_LC_PREBOUND_DYLIB:
1602 bfd_mach_o_prebound_dylib_command *pbdy = &cmd->command.prebound_dylib;
1603 unsigned char *lm = pbdy->linked_modules;
1607 printf (" dylib: %s\n", pbdy->name_str);
1608 printf (" nmodules: %u\n", pbdy->nmodules);
1609 printf (" linked modules (at %u): ",
1610 pbdy->linked_modules_offset - cmd->offset);
1611 last = pbdy->nmodules > 32 ? 32 : pbdy->nmodules;
1612 for (j = 0; j < last; j++)
1613 printf ("%u", (lm[j >> 3] >> (j & 7)) & 1);
1614 if (last < pbdy->nmodules)
1619 case BFD_MACH_O_LC_PREBIND_CKSUM:
1621 bfd_mach_o_prebind_cksum_command *cksum = &cmd->command.prebind_cksum;
1622 printf (" 0x%08x\n", cksum->cksum);
1625 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
1627 bfd_mach_o_twolevel_hints_command *hints =
1628 &cmd->command.twolevel_hints;
1630 printf (" table offset: 0x%08x nbr hints: %u\n",
1631 hints->offset, hints->nhints);
1633 dump_twolevel_hints (abfd, hints);
1636 case BFD_MACH_O_LC_MAIN:
1638 bfd_mach_o_main_command *entry = &cmd->command.main;
1639 printf (" entry offset: ");
1640 printf_uint64 (entry->entryoff);
1643 printf_uint64 (entry->stacksize);
1654 dump_load_commands (bfd *abfd, unsigned int cmd32, unsigned int cmd64)
1656 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1657 bfd_mach_o_load_command *cmd;
1660 for (cmd = mdata->first_command, i = 0; cmd != NULL; cmd = cmd->next, i++)
1663 dump_load_command (abfd, cmd, i, FALSE);
1664 else if (cmd->type == cmd32 || cmd->type == cmd64)
1665 dump_load_command (abfd, cmd, i, TRUE);
1669 static const char * const unwind_x86_64_regs[] =
1670 {"", "rbx", "r12", "r13", "r14", "r15", "rbp", "???" };
1672 static const char * const unwind_x86_regs[] =
1673 {"", "ebx", "ecx", "edx", "edi", "edi", "ebp", "???" };
1675 /* Dump x86 or x86-64 compact unwind encoding. Works for both architecture,
1676 as the encoding is the same (but not register names). */
1679 dump_unwind_encoding_x86 (unsigned int encoding, unsigned int sz,
1680 const char * const regs_name[])
1684 mode = encoding & MACH_O_UNWIND_X86_64_MODE_MASK;
1687 case MACH_O_UNWIND_X86_64_MODE_RBP_FRAME:
1690 char pfx = sz == 8 ? 'R' : 'E';
1692 regs = encoding & MACH_O_UNWIND_X86_64_RBP_FRAME_REGSITERS;
1693 printf (" %cSP frame", pfx);
1696 unsigned int offset;
1699 offset = (encoding & MACH_O_UNWIND_X86_64_RBP_FRAME_OFFSET) >> 16;
1700 printf (" at %cBP-%u:", pfx, offset * sz);
1701 for (i = 0; i < 5; i++)
1703 unsigned int reg = (regs >> (i * 3)) & 0x7;
1704 if (reg != MACH_O_UNWIND_X86_64_REG_NONE)
1705 printf (" %s", regs_name[reg]);
1710 case MACH_O_UNWIND_X86_64_MODE_STACK_IMMD:
1711 case MACH_O_UNWIND_X86_64_MODE_STACK_IND:
1713 unsigned int stack_size;
1714 unsigned int reg_count;
1715 unsigned int reg_perm;
1716 unsigned int regs[6];
1719 printf (" frameless");
1721 (encoding & MACH_O_UNWIND_X86_64_FRAMELESS_STACK_SIZE) >> 16;
1723 (encoding & MACH_O_UNWIND_X86_64_FRAMELESS_REG_COUNT) >> 10;
1724 reg_perm = encoding & MACH_O_UNWIND_X86_64_FRAMELESS_REG_PERMUTATION;
1726 if (mode == MACH_O_UNWIND_X86_64_MODE_STACK_IMMD)
1727 printf (" size: 0x%03x", stack_size * sz);
1730 unsigned int stack_adj;
1733 (encoding & MACH_O_UNWIND_X86_64_FRAMELESS_STACK_ADJUST) >> 13;
1734 printf (" size at 0x%03x + 0x%02x", stack_size, stack_adj * sz);
1736 /* Registers are coded using arithmetic compression: the register
1737 is indexed in range 0-6, the second in range 0-5, the third in
1738 range 0-4, etc. Already used registers are removed in next
1740 #define DO_PERM(R, NUM) R = reg_perm / NUM; reg_perm -= R * NUM
1745 DO_PERM (regs[0], 120);
1746 DO_PERM (regs[1], 24);
1747 DO_PERM (regs[2], 6);
1748 DO_PERM (regs[3], 2);
1749 DO_PERM (regs[4], 1);
1750 regs[5] = 0; /* Not used if reg_count = 5. */
1753 DO_PERM (regs[0], 60);
1754 DO_PERM (regs[1], 12);
1755 DO_PERM (regs[2], 3);
1756 DO_PERM (regs[3], 1);
1759 DO_PERM (regs[0], 20);
1760 DO_PERM (regs[1], 4);
1761 DO_PERM (regs[2], 1);
1764 DO_PERM (regs[0], 5);
1765 DO_PERM (regs[1], 1);
1768 DO_PERM (regs[0], 1);
1773 printf (" [bad reg count]");
1778 for (i = reg_count - 1; i >= 0; i--)
1780 unsigned int inc = 1;
1781 for (j = 0; j < i; j++)
1782 if (regs[i] >= regs[j])
1787 for (i = 0; i < (int) reg_count; i++)
1788 printf (" %s", regs_name[regs[i]]);
1791 case MACH_O_UNWIND_X86_64_MODE_DWARF:
1792 printf (" Dwarf offset: 0x%06x",
1793 encoding & MACH_O_UNWIND_X86_64_DWARF_SECTION_OFFSET);
1796 printf (" [unhandled mode]");
1801 /* Dump arm64 compact unwind entries. */
1804 dump_unwind_encoding_arm64 (unsigned int encoding)
1806 switch (encoding & MACH_O_UNWIND_ARM64_MODE_MASK)
1808 case MACH_O_UNWIND_ARM64_MODE_FRAMELESS:
1809 printf (" frameless");
1811 case MACH_O_UNWIND_ARM64_MODE_DWARF:
1812 printf (" Dwarf offset: 0x%06x",
1813 encoding & MACH_O_UNWIND_ARM64_DWARF_SECTION_OFFSET);
1815 case MACH_O_UNWIND_ARM64_MODE_FRAME:
1819 printf (" [unhandled mode]");
1822 switch (encoding & MACH_O_UNWIND_ARM64_MODE_MASK)
1824 case MACH_O_UNWIND_ARM64_MODE_FRAMELESS:
1825 case MACH_O_UNWIND_ARM64_MODE_FRAME:
1826 if (encoding & MACH_O_UNWIND_ARM64_FRAME_X19_X20_PAIR)
1827 printf (" x19-x20");
1828 if (encoding & MACH_O_UNWIND_ARM64_FRAME_X21_X22_PAIR)
1829 printf (" x21-x22");
1830 if (encoding & MACH_O_UNWIND_ARM64_FRAME_X23_X24_PAIR)
1831 printf (" x23-x24");
1832 if (encoding & MACH_O_UNWIND_ARM64_FRAME_X25_X26_PAIR)
1833 printf (" x25-x26");
1834 if (encoding & MACH_O_UNWIND_ARM64_FRAME_X27_X28_PAIR)
1835 printf (" x27-x28");
1838 switch (encoding & MACH_O_UNWIND_ARM64_MODE_MASK)
1840 case MACH_O_UNWIND_ARM64_MODE_FRAME:
1841 if (encoding & MACH_O_UNWIND_ARM64_FRAME_D8_D9_PAIR)
1843 if (encoding & MACH_O_UNWIND_ARM64_FRAME_D10_D11_PAIR)
1844 printf (" d10-d11");
1845 if (encoding & MACH_O_UNWIND_ARM64_FRAME_D12_D13_PAIR)
1846 printf (" d12-d13");
1847 if (encoding & MACH_O_UNWIND_ARM64_FRAME_D14_D15_PAIR)
1848 printf (" d14-d15");
1850 case MACH_O_UNWIND_ARM64_MODE_FRAMELESS:
1851 printf (" size: %u",
1852 (encoding & MACH_O_UNWIND_ARM64_FRAMELESS_STACK_SIZE_MASK) >> 8);
1858 dump_unwind_encoding (bfd_mach_o_data_struct *mdata, unsigned int encoding)
1860 printf ("0x%08x", encoding);
1864 switch (mdata->header.cputype)
1866 case BFD_MACH_O_CPU_TYPE_X86_64:
1867 dump_unwind_encoding_x86 (encoding, 8, unwind_x86_64_regs);
1869 case BFD_MACH_O_CPU_TYPE_I386:
1870 dump_unwind_encoding_x86 (encoding, 4, unwind_x86_regs);
1872 case BFD_MACH_O_CPU_TYPE_ARM64:
1873 dump_unwind_encoding_arm64 (encoding);
1876 printf (" [unhandled cpu]");
1879 if (encoding & MACH_O_UNWIND_HAS_LSDA)
1881 if (encoding & MACH_O_UNWIND_PERSONALITY_MASK)
1882 printf (" PERS(%u)",
1883 ((encoding & MACH_O_UNWIND_PERSONALITY_MASK)
1884 >> MACH_O_UNWIND_PERSONALITY_SHIFT));
1888 dump_obj_compact_unwind (bfd *abfd,
1889 const unsigned char *content, bfd_size_type size)
1891 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1892 int is_64 = mdata->header.version == 2;
1893 const unsigned char *p;
1895 printf ("Compact unwind info:\n");
1896 printf (" start length personality lsda\n");
1900 struct mach_o_compact_unwind_64 *e =
1901 (struct mach_o_compact_unwind_64 *) content;
1903 for (p = content; p < content + size; p += sizeof (*e))
1905 e = (struct mach_o_compact_unwind_64 *) p;
1908 printf_uint64 (bfd_get_64 (abfd, e->start));
1909 printf (" %08lx", bfd_get_32 (abfd, e->length));
1911 printf_uint64 (bfd_get_64 (abfd, e->personality));
1913 printf_uint64 (bfd_get_64 (abfd, e->lsda));
1916 printf (" encoding: ");
1917 dump_unwind_encoding (mdata, bfd_get_32 (abfd, e->encoding));
1923 printf ("unhandled\n");
1928 dump_exe_compact_unwind (bfd *abfd,
1929 const unsigned char *content, bfd_size_type size)
1931 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1932 struct mach_o_unwind_info_header *hdr;
1933 unsigned int version;
1934 unsigned int encodings_offset;
1935 unsigned int encodings_count;
1936 unsigned int personality_offset;
1937 unsigned int personality_count;
1938 unsigned int index_offset;
1939 unsigned int index_count;
1940 struct mach_o_unwind_index_entry *index_entry;
1944 printf ("Compact unwind info:\n");
1946 hdr = (struct mach_o_unwind_info_header *) content;
1947 if (size < sizeof (*hdr))
1949 printf (" truncated!\n");
1953 version = bfd_get_32 (abfd, hdr->version);
1954 if (version != MACH_O_UNWIND_SECTION_VERSION)
1956 printf (" unknown version: %u\n", version);
1959 encodings_offset = bfd_get_32 (abfd, hdr->encodings_array_offset);
1960 encodings_count = bfd_get_32 (abfd, hdr->encodings_array_count);
1961 personality_offset = bfd_get_32 (abfd, hdr->personality_array_offset);
1962 personality_count = bfd_get_32 (abfd, hdr->personality_array_count);
1963 index_offset = bfd_get_32 (abfd, hdr->index_offset);
1964 index_count = bfd_get_32 (abfd, hdr->index_count);
1965 printf (" %u encodings, %u personalities, %u level-1 indexes:\n",
1966 encodings_count, personality_count, index_count);
1969 if (personality_count > 0)
1971 const unsigned char *pers = content + personality_offset;
1973 printf (" personalities\n");
1974 for (i = 0; i < personality_count; i++)
1975 printf (" %u: 0x%08x\n", i,
1976 (unsigned) bfd_get_32 (abfd, pers + 4 * i));
1979 /* Level-1 index. */
1980 printf (" idx function level2 off lsda off\n");
1982 index_entry = (struct mach_o_unwind_index_entry *) (content + index_offset);
1983 for (i = 0; i < index_count; i++)
1985 unsigned int func_offset;
1986 unsigned int level2_offset;
1987 unsigned int lsda_offset;
1989 func_offset = bfd_get_32 (abfd, index_entry->function_offset);
1990 level2_offset = bfd_get_32 (abfd, index_entry->second_level_offset);
1991 lsda_offset = bfd_get_32 (abfd, index_entry->lsda_index_offset);
1992 printf (" %3u 0x%08x 0x%08x 0x%08x\n",
1993 i, func_offset, level2_offset, lsda_offset);
1997 /* Level-1 index. */
1998 index_entry = (struct mach_o_unwind_index_entry *) (content + index_offset);
1999 for (i = 0; i < index_count; i++)
2001 unsigned int func_offset;
2002 unsigned int level2_offset;
2003 const unsigned char *level2;
2006 func_offset = bfd_get_32 (abfd, index_entry->function_offset);
2007 level2_offset = bfd_get_32 (abfd, index_entry->second_level_offset);
2009 /* No level-2 for this index (should be the last index). */
2010 if (level2_offset == 0)
2013 level2 = content + level2_offset;
2014 kind = bfd_get_32 (abfd, level2);
2017 case MACH_O_UNWIND_SECOND_LEVEL_COMPRESSED:
2019 struct mach_o_unwind_compressed_second_level_page_header *l2;
2020 unsigned int entry_offset;
2021 unsigned int entry_count;
2022 unsigned int l2_encodings_offset;
2023 unsigned int l2_encodings_count;
2024 const unsigned char *en;
2027 l2 = (struct mach_o_unwind_compressed_second_level_page_header *)
2029 entry_offset = bfd_get_16 (abfd, l2->entry_page_offset);
2030 entry_count = bfd_get_16 (abfd, l2->entry_count);
2031 l2_encodings_offset = bfd_get_16 (abfd, l2->encodings_offset);
2032 l2_encodings_count = bfd_get_16 (abfd, l2->encodings_count);
2034 printf (" index %2u: compressed second level: "
2035 "%u entries, %u encodings (at 0x%08x)\n",
2036 i, entry_count, l2_encodings_count, l2_encodings_offset);
2037 printf (" # function eidx encoding\n");
2039 en = level2 + entry_offset;
2040 for (j = 0; j < entry_count; j++)
2043 unsigned int en_func;
2044 unsigned int enc_idx;
2045 unsigned int encoding;
2046 const unsigned char *enc_addr;
2048 entry = bfd_get_32 (abfd, en);
2050 MACH_O_UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET (entry);
2052 MACH_O_UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX (entry);
2053 if (enc_idx < encodings_count)
2054 enc_addr = content + encodings_offset
2057 enc_addr = level2 + l2_encodings_offset
2058 + 4 * (enc_idx - encodings_count);
2059 encoding = bfd_get_32 (abfd, enc_addr);
2061 printf (" %4u 0x%08x [%3u] ", j,
2062 func_offset + en_func, enc_idx);
2063 dump_unwind_encoding (mdata, encoding);
2071 case MACH_O_UNWIND_SECOND_LEVEL_REGULAR:
2073 struct mach_o_unwind_regular_second_level_page_header *l2;
2074 struct mach_o_unwind_regular_second_level_entry *en;
2075 unsigned int entry_offset;
2076 unsigned int entry_count;
2079 l2 = (struct mach_o_unwind_regular_second_level_page_header *)
2082 entry_offset = bfd_get_16 (abfd, l2->entry_page_offset);
2083 entry_count = bfd_get_16 (abfd, l2->entry_count);
2084 printf (" index %2u: regular level 2 at 0x%04x, %u entries\n",
2085 i, entry_offset, entry_count);
2086 printf (" # function encoding\n");
2088 en = (struct mach_o_unwind_regular_second_level_entry *)
2089 (level2 + entry_offset);
2090 for (j = 0; j < entry_count; j++)
2092 unsigned int en_func;
2093 unsigned int encoding;
2095 en_func = bfd_get_32 (abfd, en->function_offset);
2096 encoding = bfd_get_32 (abfd, en->encoding);
2097 printf (" %-4u 0x%08x ", j, en_func);
2098 dump_unwind_encoding (mdata, encoding);
2106 printf (" index %2u: unhandled second level format (%u)\n",
2112 struct mach_o_unwind_lsda_index_entry *lsda;
2113 unsigned int lsda_offset;
2114 unsigned int next_lsda_offset;
2115 unsigned int nbr_lsda;
2118 lsda_offset = bfd_get_32 (abfd, index_entry->lsda_index_offset);
2119 next_lsda_offset = bfd_get_32 (abfd, index_entry[1].lsda_index_offset);
2120 lsda = (struct mach_o_unwind_lsda_index_entry *)
2121 (content + lsda_offset);
2122 nbr_lsda = (next_lsda_offset - lsda_offset) / sizeof (*lsda);
2123 for (j = 0; j < nbr_lsda; j++)
2125 printf (" lsda %3u: function 0x%08x lsda 0x%08x\n",
2126 j, (unsigned int) bfd_get_32 (abfd, lsda->function_offset),
2127 (unsigned int) bfd_get_32 (abfd, lsda->lsda_offset));
2136 dump_section_content (bfd *abfd,
2137 const char *segname, const char *sectname,
2138 void (*dump)(bfd*, const unsigned char*, bfd_size_type))
2140 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2141 bfd_mach_o_load_command *cmd;
2143 for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
2145 if (cmd->type == BFD_MACH_O_LC_SEGMENT
2146 || cmd->type == BFD_MACH_O_LC_SEGMENT_64)
2148 bfd_mach_o_segment_command *seg = &cmd->command.segment;
2149 bfd_mach_o_section *sec;
2150 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
2151 if (strcmp (sec->segname, segname) == 0
2152 && strcmp (sec->sectname, sectname) == 0)
2155 asection *bfdsec = sec->bfdsection;
2156 unsigned char *content;
2158 size = bfd_get_section_size (bfdsec);
2159 content = (unsigned char *) xmalloc (size);
2160 bfd_get_section_contents (abfd, bfdsec, content, 0, size);
2162 (*dump)(abfd, content, size);
2170 /* Dump ABFD (according to the options[] array). */
2173 mach_o_dump (bfd *abfd)
2175 if (options[OPT_HEADER].selected)
2177 if (options[OPT_SECTION].selected)
2178 dump_load_commands (abfd, BFD_MACH_O_LC_SEGMENT, BFD_MACH_O_LC_SEGMENT_64);
2179 if (options[OPT_MAP].selected)
2180 dump_section_map (abfd);
2181 if (options[OPT_LOAD].selected)
2182 dump_load_commands (abfd, 0, 0);
2183 if (options[OPT_DYSYMTAB].selected)
2184 dump_load_commands (abfd, BFD_MACH_O_LC_DYSYMTAB, 0);
2185 if (options[OPT_CODESIGN].selected)
2186 dump_load_commands (abfd, BFD_MACH_O_LC_CODE_SIGNATURE, 0);
2187 if (options[OPT_SEG_SPLIT_INFO].selected)
2188 dump_load_commands (abfd, BFD_MACH_O_LC_SEGMENT_SPLIT_INFO, 0);
2189 if (options[OPT_FUNCTION_STARTS].selected)
2190 dump_load_commands (abfd, BFD_MACH_O_LC_FUNCTION_STARTS, 0);
2191 if (options[OPT_DATA_IN_CODE].selected)
2192 dump_load_commands (abfd, BFD_MACH_O_LC_DATA_IN_CODE, 0);
2193 if (options[OPT_TWOLEVEL_HINTS].selected)
2194 dump_load_commands (abfd, BFD_MACH_O_LC_TWOLEVEL_HINTS, 0);
2195 if (options[OPT_COMPACT_UNWIND].selected)
2197 dump_section_content (abfd, "__LD", "__compact_unwind",
2198 dump_obj_compact_unwind);
2199 dump_section_content (abfd, "__TEXT", "__unwind_info",
2200 dump_exe_compact_unwind);
2202 if (options[OPT_DYLD_INFO].selected)
2203 dump_load_commands (abfd, BFD_MACH_O_LC_DYLD_INFO, 0);
2206 /* Vector for Mach-O. */
2208 const struct objdump_private_desc objdump_private_desc_mach_o =