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 @@ -9,18 +9,39 @@ #ifndef LLVM_MC_MCDISASSEMBLER_MCDISASSEMBLER_H #define LLVM_MC_MCDISASSEMBLER_MCDISASSEMBLER_H +#include "llvm/ADT/Optional.h" #include "llvm/ADT/StringRef.h" +#include "llvm/BinaryFormat/XCOFF.h" #include "llvm/MC/MCDisassembler/MCSymbolizer.h" + #include #include #include namespace llvm { +struct XCOFFSymbolInfo { + uint64_t Index; + Optional StorageMappingClass; + bool ShowSymDesc; + XCOFFSymbolInfo(uint64_t Idx, Optional Smc, + bool Show) + : Index(Idx), StorageMappingClass(Smc), ShowSymDesc(Show){}; +}; + struct SymbolInfoTy { uint64_t Addr; StringRef Name; - uint8_t Type; + + union { + uint8_t Type; + XCOFFSymbolInfo XCoffSymInfo; + }; + + SymbolInfoTy(uint64_t Addr, StringRef Name, + Optional Smc, int64_t Idx, + bool Show) + : Addr(Addr), Name(Name), XCoffSymInfo(Idx, Smc, Show){}; SymbolInfoTy(uint64_t Addr, StringRef Name, uint8_t Type) : Addr(Addr), Name(Name), Type(Type){}; @@ -33,7 +54,6 @@ using SectionSymbolsTy = std::vector; - template class ArrayRef; class MCContext; class MCInst; diff --git a/llvm/include/llvm/Object/XCOFFObjectFile.h b/llvm/include/llvm/Object/XCOFFObjectFile.h --- a/llvm/include/llvm/Object/XCOFFObjectFile.h +++ b/llvm/include/llvm/Object/XCOFFObjectFile.h @@ -128,6 +128,12 @@ }; struct XCOFFCsectAuxEnt32 { + enum { + SymbolTypeMask = 0x07, + SymbolAlignmentMask = 0xF8, + SymbolAlignmentBitOffset = 3 + }; + support::ubig32_t SectionOrLength; // If the symbol type is XTY_SD or XTY_CM, the csect // length. @@ -140,6 +146,17 @@ XCOFF::StorageMappingClass StorageMappingClass; support::ubig32_t StabInfoIndex; support::ubig16_t StabSectNum; + + uint16_t getAlignmentLog2() const { + return (SymbolAlignmentAndType & SymbolAlignmentMask) >> + SymbolAlignmentBitOffset; + }; + + uint8_t getSymbolType() const { + return SymbolAlignmentAndType & SymbolTypeMask; + }; + + bool isLabel() const { return getSymbolType() == XCOFF::XTY_LD; } }; struct XCOFFFileAuxEnt { diff --git a/llvm/lib/BinaryFormat/XCOFF.cpp b/llvm/lib/BinaryFormat/XCOFF.cpp --- a/llvm/lib/BinaryFormat/XCOFF.cpp +++ b/llvm/lib/BinaryFormat/XCOFF.cpp @@ -10,24 +10,33 @@ using namespace llvm; +#define SMC_CASE(A) \ + case XCOFF::XMC_##A: \ + return #A; StringRef XCOFF::getMappingClassString(XCOFF::StorageMappingClass SMC) { switch (SMC) { - case XCOFF::XMC_DS: - return "DS"; - case XCOFF::XMC_RW: - return "RW"; - case XCOFF::XMC_PR: - return "PR"; - case XCOFF::XMC_TC0: - return "TC0"; - case XCOFF::XMC_BS: - return "BS"; - case XCOFF::XMC_RO: - return "RO"; - case XCOFF::XMC_UA: - return "UA"; - case XCOFF::XMC_TC: - return "TC"; + SMC_CASE(PR) + SMC_CASE(RO) + SMC_CASE(DB) + SMC_CASE(GL) + SMC_CASE(XO) + SMC_CASE(SV) + SMC_CASE(SV64) + SMC_CASE(SV3264) + SMC_CASE(TI) + SMC_CASE(TB) + SMC_CASE(RW) + SMC_CASE(TC0) + SMC_CASE(TC) + SMC_CASE(TD) + SMC_CASE(DS) + SMC_CASE(UA) + SMC_CASE(BS) + SMC_CASE(UC) + SMC_CASE(TL) + SMC_CASE(UL) + SMC_CASE(TE) +#undef SMC_CASE default: report_fatal_error("Unhandled storage-mapping class."); } diff --git a/llvm/test/tools/llvm-objdump/xcoff-disassemble-all.test b/llvm/test/tools/llvm-objdump/xcoff-disassemble-all.test --- a/llvm/test/tools/llvm-objdump/xcoff-disassemble-all.test +++ b/llvm/test/tools/llvm-objdump/xcoff-disassemble-all.test @@ -1,6 +1,9 @@ -# RUN: llvm-objdump -D %p/Inputs/xcoff-section-headers.o | \ +# RUN: llvm-objdump -D --symbol-description %p/Inputs/xcoff-section-headers.o | \ # RUN: FileCheck %s +# RUN: llvm-objdump -D %p/Inputs/xcoff-section-headers.o | \ +# RUN: FileCheck --check-prefix=CHECK-GNU %s + # xcoff-section-headers.o Compiled with IBM XL C/C++ for AIX, V16.1.0 # compiler command: xlc -qtls -o xcoff-section-headers.o -c test.c # test.c: @@ -15,7 +18,7 @@ ; REQUIRES: powerpc-registered-target CHECK: Inputs/xcoff-section-headers.o: file format aixcoff-rs6000 CHECK: Disassembly of section .text: -CHECK: 00000000 .text: +CHECK: 00000000 (idx: 4) .text: CHECK-NEXT: 0: 80 62 00 04 lwz 3, 4(2) CHECK-NEXT: 4: 80 63 00 00 lwz 3, 0(3) CHECK-NEXT: 8: 4e 80 00 20 blr @@ -27,29 +30,72 @@ CHECK-NEXT: 20: 6e 63 00 00 xoris 3, 19, 0 CHECK-NEXT: ... CHECK: Disassembly of section .data: -CHECK: 00000080 func: +CHECK: 00000080 (idx: 22) func[TC]: CHECK-NEXT: 80: 00 00 00 94 -CHECK: 00000084 a: +CHECK: 00000084 (idx: 26) a[TC]: CHECK-NEXT: 84: 00 00 00 a4 -CHECK: 00000088 b: +CHECK: 00000088 (idx: 30) b[TC]: CHECK-NEXT: 88: 00 00 00 a0 -CHECK: 0000008c c: +CHECK: 0000008c (idx: 34) c[TC]: CHECK-NEXT: 8c: 00 00 00 08 -CHECK: 00000090 d: +CHECK: 00000090 (idx: 38) d[TC]: CHECK-NEXT: 90: 00 00 00 00 -CHECK: 00000094 func: +CHECK: 00000094 (idx: 20) func[DS]: CHECK-NEXT: 94: 00 00 00 00 CHECK-NEXT: 98: 00 00 00 80 CHECK-NEXT: 9c: 00 00 00 00 -CHECK: 000000a0 b: +CHECK: 000000a0 (idx: 28) b[RW]: CHECK-NEXT: a0: 00 00 30 39 CHECK: Disassembly of section .bss: -CHECK: 000000a4 a: +CHECK: 000000a4 (idx: 24) a[RW]: CHECK-NEXT: ... CHECK: Disassembly of section .tdata: -CHECK: 00000000 d: +CHECK: 00000000 (idx: 36) d[TL]: CHECK-NEXT: 0: 40 09 21 f9 bdnzfl 9, .+8696 CHECK-NEXT: 4: f0 1b 86 6e CHECK: Disassembly of section .tbss: -CHECK: 00000008 c: +CHECK: 00000008 (idx: 32) c[UL]: CHECK-NEXT: ... + + + +CHECK-GNU: Inputs/xcoff-section-headers.o: file format aixcoff-rs6000 +CHECK-GNU: Disassembly of section .text: +CHECK-GNU: 00000000 .text: +CHECK-GNU-NEXT: 0: 80 62 00 04 lwz 3, 4(2) +CHECK-GNU-NEXT: 4: 80 63 00 00 lwz 3, 0(3) +CHECK-GNU-NEXT: 8: 4e 80 00 20 blr +CHECK-GNU-NEXT: c: 00 00 00 00 +CHECK-GNU-NEXT: 10: 00 00 20 40 +CHECK-GNU-NEXT: 14: 00 00 00 01 +CHECK-GNU-NEXT: 18: 00 00 00 0c +CHECK-GNU-NEXT: 1c: 00 04 66 75 +CHECK-GNU-NEXT: 20: 6e 63 00 00 xoris 3, 19, 0 +CHECK-GNU-NEXT: ... +CHECK-GNU: Disassembly of section .data: +CHECK-GNU: 00000080 func: +CHECK-GNU-NEXT: 80: 00 00 00 94 +CHECK-GNU: 00000084 a: +CHECK-GNU-NEXT: 84: 00 00 00 a4 +CHECK-GNU: 00000088 b: +CHECK-GNU-NEXT: 88: 00 00 00 a0 +CHECK-GNU: 0000008c c: +CHECK-GNU-NEXT: 8c: 00 00 00 08 +CHECK-GNU: 00000090 d: +CHECK-GNU-NEXT: 90: 00 00 00 00 +CHECK-GNU: 00000094 func: +CHECK-GNU-NEXT: 94: 00 00 00 00 +CHECK-GNU-NEXT: 98: 00 00 00 80 +CHECK-GNU-NEXT: 9c: 00 00 00 00 +CHECK-GNU: 000000a0 b: +CHECK-GNU-NEXT: a0: 00 00 30 39 +CHECK-GNU: Disassembly of section .bss: +CHECK-GNU: 000000a4 a: +CHECK-GNU-NEXT: ... +CHECK-GNU: Disassembly of section .tdata: +CHECK-GNU: 00000000 d: +CHECK-GNU-NEXT: 0: 40 09 21 f9 bdnzfl 9, .+8696 +CHECK-GNU-NEXT: 4: f0 1b 86 6e +CHECK-GNU: Disassembly of section .tbss: +CHECK-GNU: 00000008 c: +CHECK-GNU-NEXT: ... diff --git a/llvm/tools/llvm-objdump/CMakeLists.txt b/llvm/tools/llvm-objdump/CMakeLists.txt --- a/llvm/tools/llvm-objdump/CMakeLists.txt +++ b/llvm/tools/llvm-objdump/CMakeLists.txt @@ -20,6 +20,7 @@ ELFDump.cpp MachODump.cpp WasmDump.cpp + XCOFFDump.cpp ) if(HAVE_LIBXAR) diff --git a/llvm/tools/llvm-objdump/XCOFFDump.cpp b/llvm/tools/llvm-objdump/XCOFFDump.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-objdump/XCOFFDump.cpp @@ -0,0 +1,82 @@ +//===-- XCOFFDump.cpp - Object file dumping utility for llvm --------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements XCOFF object dump information. +// +//===----------------------------------------------------------------------===// + +#include "llvm-objdump.h" +#include "llvm/Object/XCOFFObjectFile.h" + +namespace llvm { + +using namespace llvm::object; + +Optional +getXCOFFSymbolCsectSMC(const ObjectFile *Obj, const SymbolRef &Sym) { + assert(Obj->isXCOFF() && "Not XCOFF object file."); + DataRefImpl SymbolDRI = Sym.getRawDataRefImpl(); + + XCOFFSymbolRef SymRef(SymbolDRI, static_cast(Obj)); + + if (SymRef.hasCsectAuxEnt()) { + assert(SymRef.getNumberOfAuxEntries() && + "No CSECT Auxiliary Entry is found."); + const XCOFFCsectAuxEnt32 *AuxEntPtr = SymRef.getXCOFFCsectAuxEnt32(); + return Optional(AuxEntPtr->StorageMappingClass); + } + return Optional(); +} + +int64_t getSymbolIndex(const ObjectFile *Obj, const SymbolRef &Sym) { + if (!Obj->isXCOFF()) + return No_Sym_Index; + + const auto *XCOFFObj = dyn_cast(Obj); + DataRefImpl SymbolDRI = Sym.getRawDataRefImpl(); + return XCOFFObj->getSymbolIndex(SymbolDRI.p); +} + +bool isSymbolDescriptionDisplay(const ObjectFile *Obj, const SymbolRef &Sym) { + if (!Obj->isXCOFF()) + return false; + + DataRefImpl SymbolDRI = Sym.getRawDataRefImpl(); + + XCOFFSymbolRef SymRef(SymbolDRI, static_cast(Obj)); + + if (SymRef.hasCsectAuxEnt()) { + assert(SymRef.getNumberOfAuxEntries() && + "No CSECT Auxiliary Entry is found."); + + const XCOFFCsectAuxEnt32 *AuxEntPtr = SymRef.getXCOFFCsectAuxEnt32(); + return !AuxEntPtr->isLabel(); + } + + return false; +} + +void printXCOFFSymbolDescription(SymbolInfoTy &SymbolInfo, + std::string &SymbolName) { + + const int64_t SymIndex = SymbolInfo.XCoffSymInfo.Index; + if (SymIndex >= 0) + outs() << "(idx: " << SymIndex << ") "; + + outs() << SymbolName; + + if (!SymbolInfo.XCoffSymInfo.StorageMappingClass) + return; + + const XCOFF::StorageMappingClass Smc = + SymbolInfo.XCoffSymInfo.StorageMappingClass.getValue(); + if (SymIndex >= 0 && SymbolInfo.XCoffSymInfo.ShowSymDesc) + outs() << "[" << XCOFF::getMappingClassString(Smc).str() << "]"; +} + +} // namespace llvm diff --git a/llvm/tools/llvm-objdump/llvm-objdump.h b/llvm/tools/llvm-objdump/llvm-objdump.h --- a/llvm/tools/llvm-objdump/llvm-objdump.h +++ b/llvm/tools/llvm-objdump/llvm-objdump.h @@ -8,11 +8,14 @@ #ifndef LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H #define LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H +#include "llvm/ADT/Optional.h" +#include "llvm/BinaryFormat/XCOFF.h" #include "llvm/DebugInfo/DIContext.h" +#include "llvm/MC/MCDisassembler/MCDisassembler.h" +#include "llvm/Object/Archive.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/DataTypes.h" -#include "llvm/Object/Archive.h" namespace llvm { class StringRef; @@ -27,6 +30,8 @@ class RelocationRef; } +static const int64_t No_Sym_Index = -1; + extern cl::opt Demangle; typedef std::function FilterPredicate; @@ -139,6 +144,17 @@ void printSectionContents(const object::ObjectFile *O); void printSymbolTable(const object::ObjectFile *O, StringRef ArchiveName, StringRef ArchitectureName = StringRef()); + +Optional +getXCOFFSymbolCsectSMC(const object::ObjectFile *Obj, + const object::SymbolRef &Sym); +bool isSymbolDescriptionDisplay(const object::ObjectFile *Obj, + const object::SymbolRef &Sym); +int64_t getSymbolIndex(const object::ObjectFile *Obj, + const object::SymbolRef &Sym); +void printXCOFFSymbolDescription(SymbolInfoTy &SymbolInfo, + std::string &SymbolName); + LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Twine Message); LLVM_ATTRIBUTE_NORETURN void reportError(Error E, StringRef FileName, StringRef ArchiveName = "", diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -46,6 +46,7 @@ #include "llvm/Object/MachOUniversal.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Object/Wasm.h" +#include "llvm/Object/XCOFFObjectFile.h" #include "llvm/Support/Casting.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" @@ -143,6 +144,11 @@ cl::NotHidden, cl::Grouping, cl::aliasopt(DisassembleAll)); +cl::opt + SymbolDescription("symbol-description", + cl::desc("Add symbol description for disassembly"), + cl::init(false), cl::cat(ObjdumpCat)); + static cl::list DisassembleFunctions("disassemble-functions", cl::CommaSeparated, cl::desc("List of functions to disassemble. " @@ -1138,6 +1144,37 @@ } } +SymbolInfoTy CreateSymbolInfoTy(const ObjectFile *Obj, + const SymbolRef &Symbol) { + const StringRef FileName = Obj->getFileName(); + const uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName); + const StringRef Name = unwrapOrError(Symbol.getName(), FileName); + + uint8_t SymbolType = ELF::STT_NOTYPE; + + if (Obj->isELF()) { + SymbolType = getElfSymbolType(Obj, Symbol); + } + + if (Obj->isXCOFF()) { + const int64_t SymbolIndex = getSymbolIndex(Obj, Symbol); + const bool SymDescDis = isSymbolDescriptionDisplay(Obj, Symbol); + Optional Smc = + getXCOFFSymbolCsectSMC(Obj, Symbol); + return SymbolInfoTy(Addr, Name, Smc, SymbolIndex, SymDescDis); + } else + return SymbolInfoTy(Addr, Name, SymbolType); +} + +SymbolInfoTy CreateSymbolInfoTy(const ObjectFile *Obj, const uint64_t Addr, + StringRef &Name) { + if (Obj->isXCOFF()) + return SymbolInfoTy(Addr, Name, Optional(), + No_Sym_Index, false); + else + return SymbolInfoTy(Addr, Name, ELF::STT_NOTYPE); +} + static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, MCContext &Ctx, MCDisassembler *PrimaryDisAsm, MCDisassembler *SecondaryDisAsm, @@ -1164,8 +1201,6 @@ const StringRef FileName = Obj->getFileName(); const MachOObjectFile *MachO = dyn_cast(Obj); for (const SymbolRef &Symbol : Obj->symbols()) { - uint64_t Address = unwrapOrError(Symbol.getAddress(), FileName); - StringRef Name = unwrapOrError(Symbol.getName(), FileName); if (Name.empty()) continue; @@ -1177,7 +1212,7 @@ continue; } - // Don't ask a Mach-O STAB symbol for its section unless you know that + // Don't ask a Mach-O STAB symbol for its section unless you know that // STAB symbol's section field refers to a valid section index. Otherwise // the symbol may error trying to load a section that does not exist. if (MachO) { @@ -1191,10 +1226,11 @@ section_iterator SecI = unwrapOrError(Symbol.getSection(), FileName); if (SecI != Obj->section_end()) - AllSymbols[*SecI].emplace_back(Address, Name, SymbolType); + AllSymbols[*SecI].push_back(CreateSymbolInfoTy(Obj, Symbol)); else - AbsoluteSymbols.emplace_back(Address, Name, SymbolType); + AbsoluteSymbols.push_back(CreateSymbolInfoTy(Obj, Symbol)); } + if (AllSymbols.empty() && Obj->isELF()) addDynamicElfSymbols(Obj, AllSymbols); @@ -1293,10 +1329,14 @@ StringRef SectionName = unwrapOrError(Section.getName(), Obj->getFileName()); // If the section has no symbol at the start, just insert a dummy one. if (Symbols.empty() || Symbols[0].Addr != 0) { - Symbols.insert( - Symbols.begin(), - SymbolInfoTy(SectionAddr, SectionName, - Section.isText() ? ELF::STT_FUNC : ELF::STT_OBJECT)); + if (Obj->isXCOFF()) + Symbols.insert(Symbols.begin(), + CreateSymbolInfoTy(Obj, SectionAddr, SectionName)); + else + Symbols.insert( + Symbols.begin(), + SymbolInfoTy(SectionAddr, SectionName, + Section.isText() ? ELF::STT_FUNC : ELF::STT_OBJECT)); } SmallString<40> Comments; @@ -1371,8 +1411,11 @@ if (!NoLeadingAddr) outs() << format(Is64Bits ? "%016" PRIx64 " " : "%08" PRIx64 " ", SectionAddr + Start + VMAAdjustment); - - outs() << SymbolName << ":\n"; + if (Obj->isXCOFF() && SymbolDescription) { + printXCOFFSymbolDescription(Symbols[SI], SymbolName); + outs() << ":\n"; + } else + outs() << SymbolName << ":\n"; // Don't print raw contents of a virtual section. A virtual section // doesn't have any contents in the file.