1 //===-- lib/MC/MCObjectSymbolizer.cpp -------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCObjectSymbolizer.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCInst.h"
15 #include "llvm/MC/MCRelocationInfo.h"
16 #include "llvm/MC/MCSymbol.h"
17 #include "llvm/Object/MachO.h"
18 #include "llvm/Object/ELF.h"
19 #include "llvm/Support/raw_ostream.h"
22 using namespace object;
24 //===- MCMachObjectSymbolizer ---------------------------------------------===//
27 class MCMachObjectSymbolizer : public MCObjectSymbolizer {
29 MCMachObjectSymbolizer(MCContext &Ctx, OwningPtr<MCRelocationInfo> &RelInfo,
30 const object::MachOObjectFile *MachOOF)
31 : MCObjectSymbolizer(Ctx, RelInfo, MachOOF)
34 void tryAddingPcLoadReferenceComment(raw_ostream &cStream,
35 int64_t Value, uint64_t Address) {
36 AddrToRelocMap::iterator RI = AddrToReloc.find(Address);
37 if (RI != AddrToReloc.end()) {
38 const MCExpr *RelExpr = RelInfo->createExprForRelocation(RI->second);
39 if (!RelExpr || RelExpr->EvaluateAsAbsolute(Value) == false)
42 uint64_t Addr = Value;
43 AddrToSectionMap::const_iterator SI = AddrToSection.find(Addr);
45 DataRefImpl DRI; DRI.p = *SI;
46 SectionRef S(DRI, Obj);
47 StringRef Name; S.getName(Name);
48 if (Name == "__cstring") {
50 S.getContents(Contents);
51 Contents = Contents.substr(Addr - SI.start());
52 cStream << " ## literal pool for: "
53 << Contents.substr(0, Contents.find_first_of(0));
58 } // End unnamed namespace
60 //===- MCObjectSymbolizer -------------------------------------------------===//
62 MCObjectSymbolizer::MCObjectSymbolizer(MCContext &Ctx,
63 OwningPtr<MCRelocationInfo> &RelInfo,
64 const ObjectFile *Obj)
65 : MCSymbolizer(Ctx, RelInfo), Obj(Obj),
66 AddrToSectionAllocator(), AddrToSection(AddrToSectionAllocator),
69 for (section_iterator SI = Obj->begin_sections(),
70 SE = Obj->end_sections();
74 uint64_t StartAddr; SI->getAddress(StartAddr);
75 uint64_t Size; SI->getSize(Size);
76 StringRef SecName; SI->getName(SecName);
77 bool RequiredForExec; SI->isRequiredForExecution(RequiredForExec);
78 if (RequiredForExec == false || Size == 0)
80 AddrToSection.insert(StartAddr, StartAddr + Size - 1,
81 SI->getRawDataRefImpl().p);
82 for (relocation_iterator RI = SI->begin_relocations(),
83 RE = SI->end_relocations();
87 // FIXME: libObject is inconsistent regarding error handling. The
88 // overwhelming majority of methods always return object_error::success,
89 // and assert for simple errors.. Here, ELFObjectFile::getRelocationOffset
90 // asserts when the file type isn't ET_REL.
91 // This workaround handles x86-64 elf, the only one that has a relocinfo.
94 const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj);
97 if (ELFObj->getElfHeader()->e_type == ELF::ET_REL) {
98 RI->getOffset(Offset);
101 RI->getAddress(Offset);
104 RI->getOffset(Offset);
107 // At a specific address, only keep the first relocation.
108 if (AddrToReloc.find(Offset) == AddrToReloc.end())
109 AddrToReloc[Offset] = *RI;
114 bool MCObjectSymbolizer::
115 tryAddingSymbolicOperand(MCInst &MI, raw_ostream &cStream,
116 int64_t Value, uint64_t Address, bool IsBranch,
117 uint64_t Offset, uint64_t InstSize) {
118 AddrToRelocMap::iterator RI = AddrToReloc.find(Address + Offset);
119 if (RI != AddrToReloc.end()) {
120 if (const MCExpr *RelExpr = RelInfo->createExprForRelocation(RI->second)) {
121 MI.addOperand(MCOperand::CreateExpr(RelExpr));
124 // Only try to create a symbol+offset expression if there is no relocation.
128 // Interpret Value as a branch target.
129 if (IsBranch == false)
131 uint64_t UValue = Value;
132 // FIXME: map instead of looping each time?
134 for (symbol_iterator SI = Obj->begin_symbols(),
135 SE = Obj->end_symbols();
139 uint64_t SymAddr; SI->getAddress(SymAddr);
140 uint64_t SymSize; SI->getSize(SymSize);
141 StringRef SymName; SI->getName(SymName);
142 SymbolRef::Type SymType; SI->getType(SymType);
143 if (SymAddr == UnknownAddressOrSize || SymSize == UnknownAddressOrSize
144 || SymName.empty() || SymType != SymbolRef::ST_Function)
147 if ( SymAddr == UValue ||
148 (SymAddr <= UValue && SymAddr + SymSize > UValue)) {
149 MCSymbol *Sym = Ctx.GetOrCreateSymbol(SymName);
150 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
151 if (SymAddr != UValue) {
152 const MCExpr *Off = MCConstantExpr::Create(UValue - SymAddr, Ctx);
153 Expr = MCBinaryExpr::CreateAdd(Expr, Off, Ctx);
155 MI.addOperand(MCOperand::CreateExpr(Expr));
162 void MCObjectSymbolizer::
163 tryAddingPcLoadReferenceComment(raw_ostream &cStream,
164 int64_t Value, uint64_t Address) {
168 MCObjectSymbolizer::createObjectSymbolizer(MCContext &Ctx,
169 OwningPtr<MCRelocationInfo> &RelInfo,
170 const ObjectFile *Obj) {
171 if (const MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(Obj)) {
172 return new MCMachObjectSymbolizer(Ctx, RelInfo, MachOOF);
174 return new MCObjectSymbolizer(Ctx, RelInfo, Obj);