llvm.org GIT mirror llvm / c61aa02
Add printing of Mach-O stabs in llvm-nm. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213327 91177308-0d34-0410-b5e6-96231b3b80d8 Kevin Enderby 6 years ago
4 changed file(s) with 103 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
385385
386386 enum StabType {
387387 // Constant values for the "n_type" field in llvm::MachO::nlist and
388 // llvm::MachO::nlist_64 when "(n_type & NlistMaskStab) != 0"
388 // llvm::MachO::nlist_64 when "(n_type & N_STAB) != 0"
389389 N_GSYM = 0x20u,
390390 N_FNAME = 0x22u,
391391 N_FUN = 0x24u,
392392 N_STSYM = 0x26u,
393393 N_LCSYM = 0x28u,
394394 N_BNSYM = 0x2Eu,
395 N_PC = 0x30u,
396 N_AST = 0x32u,
395397 N_OPT = 0x3Cu,
396398 N_RSYM = 0x40u,
397399 N_SLINE = 0x44u,
2525 RUN: | FileCheck %s -check-prefix macho-s
2626 RUN: llvm-nm -x %p/Inputs/macho-text-data-bss.macho-x86_64 \
2727 RUN: | FileCheck %s -check-prefix macho-x
28 RUN: llvm-nm -p -a %p/Inputs/macho-hello-g.macho-x86_64 \
29 RUN: | FileCheck %s -check-prefix macho-pa
2830 RUN: llvm-nm %p/Inputs/common.coff-i386 \
2931 RUN: | FileCheck %s -check-prefix COFF-COMMON
3032 RUN: llvm-nm %p/Inputs/relocatable-with-section-address.elf-x86-64 \
114116 macho-x: 0000000000000000 0f 01 0000 00000001 _t
115117 macho-x: 0000000000000048 0f 05 0000 00000007 _t.eh
116118
119 macho-pa: 0000000000000000 - 00 0000 SO /Volumes/SandBox/
120 macho-pa: 0000000000000000 - 00 0000 SO hello.c
121 macho-pa: 0000000053c8408d - 03 0001 OSO /Volumes/SandBox/hello.o
122 macho-pa: 0000000100000f30 - 01 0000 BNSYM
123 macho-pa: 0000000100000f30 - 01 0000 FUN _main
124 macho-pa: 000000000000003b - 00 0000 FUN
125 macho-pa: 000000000000003b - 01 0000 ENSYM
126 macho-pa: 0000000000000000 - 01 0000 SO
127 macho-pa: 0000000100000000 T __mh_execute_header
128 macho-pa: 0000000100000f30 T _main
129 macho-pa: U _printf
130 macho-pa: U dyld_stub_binder
131
117132 Test that nm uses addresses even with ELF .o files.
118133 ELF-SEC-ADDR64: 0000000000000058 D a
119134 ELF-SEC-ADDR64-NEXT: 000000000000005c D b
456456 outs() << "\n";
457457 }
458458
459 // Table that maps Darwin's Mach-O stab constants to strings to allow printing.
460 struct DarwinStabName {
461 uint8_t NType;
462 const char *Name;
463 };
464 static const struct DarwinStabName DarwinStabNames[] = {
465 {MachO::N_GSYM, "GSYM"},
466 {MachO::N_FNAME, "FNAME"},
467 {MachO::N_FUN, "FUN"},
468 {MachO::N_STSYM, "STSYM"},
469 {MachO::N_LCSYM, "LCSYM"},
470 {MachO::N_BNSYM, "BNSYM"},
471 {MachO::N_PC, "PC"},
472 {MachO::N_AST, "AST"},
473 {MachO::N_OPT, "OPT"},
474 {MachO::N_RSYM, "RSYM"},
475 {MachO::N_SLINE, "SLINE"},
476 {MachO::N_ENSYM, "ENSYM"},
477 {MachO::N_SSYM, "SSYM"},
478 {MachO::N_SO, "SO"},
479 {MachO::N_OSO, "OSO"},
480 {MachO::N_LSYM, "LSYM"},
481 {MachO::N_BINCL, "BINCL"},
482 {MachO::N_SOL, "SOL"},
483 {MachO::N_PARAMS, "PARAM"},
484 {MachO::N_VERSION, "VERS"},
485 {MachO::N_OLEVEL, "OLEV"},
486 {MachO::N_PSYM, "PSYM"},
487 {MachO::N_EINCL, "EINCL"},
488 {MachO::N_ENTRY, "ENTRY"},
489 {MachO::N_LBRAC, "LBRAC"},
490 {MachO::N_EXCL, "EXCL"},
491 {MachO::N_RBRAC, "RBRAC"},
492 {MachO::N_BCOMM, "BCOMM"},
493 {MachO::N_ECOMM, "ECOMM"},
494 {MachO::N_ECOML, "ECOML"},
495 {MachO::N_LENG, "LENG"},
496 {0, 0}};
497 static const char *getDarwinStabString(uint8_t NType) {
498 for (unsigned i = 0; DarwinStabNames[i].Name; i++) {
499 if (DarwinStabNames[i].NType == NType)
500 return DarwinStabNames[i].Name;
501 }
502 return 0;
503 }
504
505 // darwinPrintStab() prints the n_sect, n_desc along with a symbolic name of
506 // a stab n_type value in a Mach-O file.
507 static void darwinPrintStab(MachOObjectFile *MachO, SymbolListT::iterator I) {
508 MachO::nlist_64 STE_64;
509 MachO::nlist STE;
510 uint8_t NType;
511 uint8_t NSect;
512 uint16_t NDesc;
513 if (MachO->is64Bit()) {
514 STE_64 = MachO->getSymbol64TableEntry(I->Symb);
515 NType = STE_64.n_type;
516 NSect = STE_64.n_sect;
517 NDesc = STE_64.n_desc;
518 } else {
519 STE = MachO->getSymbolTableEntry(I->Symb);
520 NType = STE.n_type;
521 NSect = STE.n_sect;
522 NDesc = STE.n_desc;
523 }
524
525 char Str[18] = "";
526 format("%02x", NSect).print(Str, sizeof(Str));
527 outs() << ' ' << Str << ' ';
528 format("%04x", NDesc).print(Str, sizeof(Str));
529 outs() << Str << ' ';
530 if (const char *stabString = getDarwinStabString(NType))
531 format("%5.5s", stabString).print(Str, sizeof(Str));
532 else
533 format(" %02x", NType).print(Str, sizeof(Str));
534 outs() << Str;
535 }
536
459537 static void sortAndPrintSymbolList(SymbolicFile *Obj, bool printName) {
460538 if (!NoSort) {
461539 if (NumericSort)
531609 if (I->Size != UnknownAddressOrSize)
532610 outs() << ' ';
533611 }
534 outs() << I->TypeChar << " " << I->Name << "\n";
612 outs() << I->TypeChar;
613 if (I->TypeChar == '-' && MachO)
614 darwinPrintStab(MachO, I);
615 outs() << " " << I->Name << "\n";
535616 } else if (OutputFormat == sysv) {
536617 std::string PaddedName(I->Name);
537618 while (PaddedName.length() < 20)
655736 static char getSymbolNMTypeChar(MachOObjectFile &Obj, basic_symbol_iterator I) {
656737 DataRefImpl Symb = I->getRawDataRefImpl();
657738 uint8_t NType = getNType(Obj, Symb);
739
740 if (NType & MachO::N_STAB)
741 return '-';
658742
659743 switch (NType & MachO::N_TYPE) {
660744 case MachO::N_ABS: