diff --git a/llvm/include/llvm/MC/MCDisassembler/MCDisassembler.h b/llvm/include/llvm/MC/MCDisassembler/MCDisassembler.h --- a/llvm/include/llvm/MC/MCDisassembler/MCDisassembler.h +++ b/llvm/include/llvm/MC/MCDisassembler/MCDisassembler.h @@ -26,6 +26,8 @@ XCOFFSymbolInfo(Optional Smc, Optional Idx, bool Label) : StorageMappingClass(Smc), Index(Idx), IsLabel(Label) {} + + bool operator<(const XCOFFSymbolInfo &SymInfo) const; }; struct SymbolInfoTy { @@ -53,9 +55,10 @@ assert(P1.IsXCOFF == P2.IsXCOFF && "P1.IsXCOFF should be equal to P2.IsXCOFF."); if (P1.IsXCOFF) - return std::tie(P1.Addr, P1.Name) < std::tie(P2.Addr, P2.Name); - else - return std::tie(P1.Addr, P1.Name, P1.Type) < + return std::tie(P1.Addr, P1.XCOFFSymInfo, P1.Name) < + std::tie(P2.Addr, P2.XCOFFSymInfo, P2.Name); + + return std::tie(P1.Addr, P1.Name, P1.Type) < std::tie(P2.Addr, P2.Name, P2.Type); } }; diff --git a/llvm/lib/MC/MCDisassembler/MCDisassembler.cpp b/llvm/lib/MC/MCDisassembler/MCDisassembler.cpp --- a/llvm/lib/MC/MCDisassembler/MCDisassembler.cpp +++ b/llvm/lib/MC/MCDisassembler/MCDisassembler.cpp @@ -43,3 +43,58 @@ void MCDisassembler::setSymbolizer(std::unique_ptr Symzer) { Symbolizer = std::move(Symzer); } + +#define SMC_PCASE(A, P) \ + case XCOFF::XMC_##A: \ + return P; + +uint8_t getSMCPriority(XCOFF::StorageMappingClass SMC) { + switch (SMC) { + SMC_PCASE(PR, 1) + SMC_PCASE(RO, 1) + SMC_PCASE(DB, 1) + SMC_PCASE(GL, 1) + SMC_PCASE(XO, 1) + SMC_PCASE(SV, 1) + SMC_PCASE(SV64, 1) + SMC_PCASE(SV3264, 1) + SMC_PCASE(TI, 1) + SMC_PCASE(TB, 1) + SMC_PCASE(RW, 1) + SMC_PCASE(TC0, 0) + SMC_PCASE(TC, 1) + SMC_PCASE(TD, 1) + SMC_PCASE(DS, 1) + SMC_PCASE(UA, 1) + SMC_PCASE(BS, 1) + SMC_PCASE(UC, 1) + SMC_PCASE(TL, 1) + SMC_PCASE(UL, 1) + SMC_PCASE(TE, 1) +#undef SMC_PCASE + } + return 0; +} + +/// The function is for symbol sorting when symbols have the same address. +/// The symbols in the same section are sorted in ascending order. +/// llvm-objdump -D will choose high order symbol to display when there are +/// symbols have the same address. + +bool XCOFFSymbolInfo::operator<(const XCOFFSymbolInfo &SymInfo) const { + // Compare Label first. If the symbol is not label, it is lower than label + // symbol. + if (IsLabel != SymInfo.IsLabel) + return SymInfo.IsLabel; + + // If symbol have not StorageMappingClass, it is lower than a symbol which has + // StorageMappingClass. + if (!StorageMappingClass) + return true; + + if (!SymInfo.StorageMappingClass) + return false; + + return getSMCPriority(StorageMappingClass.getValue()) < + getSMCPriority(SymInfo.StorageMappingClass.getValue()); +} diff --git a/llvm/test/tools/llvm-objdump/XCOFF/disassemble-symbol-description.test b/llvm/test/tools/llvm-objdump/XCOFF/disassemble-symbol-description.test --- a/llvm/test/tools/llvm-objdump/XCOFF/disassemble-symbol-description.test +++ b/llvm/test/tools/llvm-objdump/XCOFF/disassemble-symbol-description.test @@ -22,7 +22,7 @@ COMMON: Inputs/xcoff-section-headers.o: file format aixcoff-rs6000 COMMON: Disassembly of section .text: PLAIN: 00000000 <.text>: -DESC: 00000000 (idx: 4) .text: +DESC: 00000000 (idx: 16) .func: COMMON-NEXT: 0: 80 62 00 04 lwz 3, 4(2) COMMON-NEXT: 4: 80 63 00 00 lwz 3, 0(3) COMMON-NEXT: 8: 4e 80 00 20 blr