xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-objdump/XCOFFDump.cpp (revision 963f5dc7a30624e95d72fb7f87b8892651164e46)
1 //===-- XCOFFDump.cpp - XCOFF-specific dumper -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements the XCOFF-specific dumper for llvm-objdump.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #include "XCOFFDump.h"
15 
16 #include "llvm-objdump.h"
17 #include "llvm/Demangle/Demangle.h"
18 
19 using namespace llvm;
20 using namespace llvm::object;
21 
22 Error objdump::getXCOFFRelocationValueString(const XCOFFObjectFile *Obj,
23                                              const RelocationRef &Rel,
24                                              SmallVectorImpl<char> &Result) {
25   symbol_iterator SymI = Rel.getSymbol();
26   if (SymI == Obj->symbol_end())
27     return make_error<GenericBinaryError>(
28         "invalid symbol reference in relocation entry",
29         object_error::parse_failed);
30 
31   Expected<StringRef> SymNameOrErr = SymI->getName();
32   if (!SymNameOrErr)
33     return SymNameOrErr.takeError();
34 
35   std::string SymName = (*SymNameOrErr).str();
36   if (Demangle)
37     SymName = demangle(SymName);
38 
39   if (SymbolDescription)
40     SymName = getXCOFFSymbolDescription(createSymbolInfo(Obj, *SymI), SymName);
41 
42   Result.append(SymName.begin(), SymName.end());
43   return Error::success();
44 }
45 
46 Optional<XCOFF::StorageMappingClass>
47 objdump::getXCOFFSymbolCsectSMC(const XCOFFObjectFile *Obj,
48                                 const SymbolRef &Sym) {
49   const XCOFFSymbolRef SymRef = Obj->toSymbolRef(Sym.getRawDataRefImpl());
50 
51   if (!SymRef.isCsectSymbol())
52     return None;
53 
54   auto CsectAuxEntOrErr = SymRef.getXCOFFCsectAuxRef();
55   if (!CsectAuxEntOrErr)
56     return None;
57 
58   return CsectAuxEntOrErr.get().getStorageMappingClass();
59 }
60 
61 bool objdump::isLabel(const XCOFFObjectFile *Obj, const SymbolRef &Sym) {
62 
63   const XCOFFSymbolRef SymRef = Obj->toSymbolRef(Sym.getRawDataRefImpl());
64 
65   if (!SymRef.isCsectSymbol())
66     return false;
67 
68   auto CsectAuxEntOrErr = SymRef.getXCOFFCsectAuxRef();
69   if (!CsectAuxEntOrErr)
70     return false;
71 
72   return CsectAuxEntOrErr.get().isLabel();
73 }
74 
75 std::string objdump::getXCOFFSymbolDescription(const SymbolInfoTy &SymbolInfo,
76                                                StringRef SymbolName) {
77   assert(SymbolInfo.isXCOFF() && "Must be a XCOFFSymInfo.");
78 
79   std::string Result;
80   // Dummy symbols have no symbol index.
81   if (SymbolInfo.XCOFFSymInfo.Index)
82     Result = ("(idx: " + Twine(SymbolInfo.XCOFFSymInfo.Index.getValue()) +
83               ") " + SymbolName)
84                  .str();
85   else
86     Result.append(SymbolName.begin(), SymbolName.end());
87 
88   if (SymbolInfo.XCOFFSymInfo.StorageMappingClass &&
89       !SymbolInfo.XCOFFSymInfo.IsLabel) {
90     const XCOFF::StorageMappingClass Smc =
91         SymbolInfo.XCOFFSymInfo.StorageMappingClass.getValue();
92     Result.append(("[" + XCOFF::getMappingClassString(Smc) + "]").str());
93   }
94 
95   return Result;
96 }
97