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/ELFObjectFile.h"
19 #include "llvm/Support/raw_ostream.h"
23 using namespace object;
25 //===- MCMachObjectSymbolizer ---------------------------------------------===//
28 class MCMachObjectSymbolizer : public MCObjectSymbolizer {
29 const MachOObjectFile *MOOF;
30 // __TEXT;__stubs support.
34 uint64_t StubsIndSymIndex;
37 MCMachObjectSymbolizer(MCContext &Ctx, OwningPtr<MCRelocationInfo> &RelInfo,
38 const MachOObjectFile *MOOF);
40 StringRef findExternalFunctionAt(uint64_t Addr) LLVM_OVERRIDE;
42 void tryAddingPcLoadReferenceComment(raw_ostream &cStream,
44 uint64_t Address) LLVM_OVERRIDE;
46 } // End unnamed namespace
49 MCMachObjectSymbolizer::
50 MCMachObjectSymbolizer(MCContext &Ctx, OwningPtr<MCRelocationInfo> &RelInfo,
51 const MachOObjectFile *MOOF)
52 : MCObjectSymbolizer(Ctx, RelInfo, MOOF), MOOF(MOOF),
53 StubsStart(0), StubsCount(0), StubSize(0), StubsIndSymIndex(0) {
56 for (section_iterator SI = MOOF->begin_sections(), SE = MOOF->end_sections();
57 SI != SE; SI.increment(ec)) {
59 StringRef Name; SI->getName(Name);
60 if (Name == "__stubs") {
61 SectionRef StubsSec = *SI;
62 if (MOOF->is64Bit()) {
63 macho::Section64 S = MOOF->getSection64(StubsSec.getRawDataRefImpl());
64 StubsIndSymIndex = S.Reserved1;
65 StubSize = S.Reserved2;
67 macho::Section S = MOOF->getSection(StubsSec.getRawDataRefImpl());
68 StubsIndSymIndex = S.Reserved1;
69 StubSize = S.Reserved2;
71 assert(StubSize && "Mach-O stub entry size can't be zero!");
72 StubsSec.getAddress(StubsStart);
73 StubsSec.getSize(StubsCount);
74 StubsCount /= StubSize;
79 StringRef MCMachObjectSymbolizer::findExternalFunctionAt(uint64_t Addr) {
80 // FIXME: also, this can all be done at the very beginning, by iterating over
81 // all stubs and creating the calls to outside functions. Is it worth it
85 uint64_t StubIdx = (Addr - StubsStart) / StubSize;
86 if (StubIdx >= StubsCount)
89 macho::IndirectSymbolTableEntry ISTE =
90 MOOF->getIndirectSymbolTableEntry(MOOF->getDysymtabLoadCommand(), StubIdx);
91 uint32_t SymtabIdx = ISTE.Index;
94 symbol_iterator SI = MOOF->begin_symbols();
96 for (uint32_t i = 0; i != SymtabIdx; ++i) {
100 assert(SI != MOOF->end_symbols() && "Stub wasn't found in the symbol table!");
101 assert(SymName.front() == '_' && "Mach-O symbol doesn't start with '_'!");
102 return SymName.substr(1);
105 void MCMachObjectSymbolizer::
106 tryAddingPcLoadReferenceComment(raw_ostream &cStream, int64_t Value,
108 if (const RelocationRef *R = findRelocationAt(Address)) {
109 const MCExpr *RelExpr = RelInfo->createExprForRelocation(*R);
110 if (!RelExpr || RelExpr->EvaluateAsAbsolute(Value) == false)
113 uint64_t Addr = Value;
114 if (const SectionRef *S = findSectionContaining(Addr)) {
115 StringRef Name; S->getName(Name);
116 uint64_t SAddr; S->getAddress(SAddr);
117 if (Name == "__cstring") {
119 S->getContents(Contents);
120 Contents = Contents.substr(Addr - SAddr);
121 cStream << " ## literal pool for: "
122 << Contents.substr(0, Contents.find_first_of(0));
127 //===- MCObjectSymbolizer -------------------------------------------------===//
129 MCObjectSymbolizer::MCObjectSymbolizer(MCContext &Ctx,
130 OwningPtr<MCRelocationInfo> &RelInfo,
131 const ObjectFile *Obj)
132 : MCSymbolizer(Ctx, RelInfo), Obj(Obj), SortedSections(), AddrToReloc() {
135 bool MCObjectSymbolizer::
136 tryAddingSymbolicOperand(MCInst &MI, raw_ostream &cStream,
137 int64_t Value, uint64_t Address, bool IsBranch,
138 uint64_t Offset, uint64_t InstSize) {
140 StringRef ExtFnName = findExternalFunctionAt((uint64_t)Value);
141 if (!ExtFnName.empty()) {
142 MCSymbol *Sym = Ctx.GetOrCreateSymbol(ExtFnName);
143 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
144 MI.addOperand(MCOperand::CreateExpr(Expr));
149 if (const RelocationRef *R = findRelocationAt(Address + Offset)) {
150 if (const MCExpr *RelExpr = RelInfo->createExprForRelocation(*R)) {
151 MI.addOperand(MCOperand::CreateExpr(RelExpr));
154 // Only try to create a symbol+offset expression if there is no relocation.
158 // Interpret Value as a branch target.
159 if (IsBranch == false)
161 uint64_t UValue = Value;
162 // FIXME: map instead of looping each time?
164 for (symbol_iterator SI = Obj->begin_symbols(), SE = Obj->end_symbols();
165 SI != SE; SI.increment(ec)) {
167 uint64_t SymAddr; SI->getAddress(SymAddr);
168 uint64_t SymSize; SI->getSize(SymSize);
169 StringRef SymName; SI->getName(SymName);
170 SymbolRef::Type SymType; SI->getType(SymType);
171 if (SymAddr == UnknownAddressOrSize || SymSize == UnknownAddressOrSize
172 || SymName.empty() || SymType != SymbolRef::ST_Function)
175 if ( SymAddr == UValue ||
176 (SymAddr <= UValue && SymAddr + SymSize > UValue)) {
177 MCSymbol *Sym = Ctx.GetOrCreateSymbol(SymName);
178 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
179 if (SymAddr != UValue) {
180 const MCExpr *Off = MCConstantExpr::Create(UValue - SymAddr, Ctx);
181 Expr = MCBinaryExpr::CreateAdd(Expr, Off, Ctx);
183 MI.addOperand(MCOperand::CreateExpr(Expr));
190 void MCObjectSymbolizer::
191 tryAddingPcLoadReferenceComment(raw_ostream &cStream,
192 int64_t Value, uint64_t Address) {
195 StringRef MCObjectSymbolizer::findExternalFunctionAt(uint64_t Addr) {
200 MCObjectSymbolizer::createObjectSymbolizer(MCContext &Ctx,
201 OwningPtr<MCRelocationInfo> &RelInfo,
202 const ObjectFile *Obj) {
203 if (const MachOObjectFile *MOOF = dyn_cast<MachOObjectFile>(Obj))
204 return new MCMachObjectSymbolizer(Ctx, RelInfo, MOOF);
205 return new MCObjectSymbolizer(Ctx, RelInfo, Obj);
208 // SortedSections implementation.
210 static bool SectionStartsBefore(const SectionRef &S, uint64_t Addr) {
211 uint64_t SAddr; S.getAddress(SAddr);
215 const SectionRef *MCObjectSymbolizer::findSectionContaining(uint64_t Addr) {
216 if (SortedSections.empty())
219 SortedSectionList::iterator
220 EndIt = SortedSections.end(),
221 It = std::lower_bound(SortedSections.begin(), EndIt,
222 Addr, SectionStartsBefore);
225 uint64_t SAddr; It->getAddress(SAddr);
226 uint64_t SSize; It->getSize(SSize);
227 if (Addr >= SAddr + SSize)
232 const RelocationRef *MCObjectSymbolizer::findRelocationAt(uint64_t Addr) {
233 if (AddrToReloc.empty())
234 buildRelocationByAddrMap();
236 AddrToRelocMap::const_iterator RI = AddrToReloc.find(Addr);
237 if (RI == AddrToReloc.end())
242 void MCObjectSymbolizer::buildSectionList() {
244 for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections();
245 SI != SE; SI.increment(ec)) {
248 bool RequiredForExec; SI->isRequiredForExecution(RequiredForExec);
249 if (RequiredForExec == false)
251 uint64_t SAddr; SI->getAddress(SAddr);
252 uint64_t SSize; SI->getSize(SSize);
253 SortedSectionList::iterator It = std::lower_bound(SortedSections.begin(),
254 SortedSections.end(),
256 SectionStartsBefore);
257 if (It != SortedSections.end()) {
258 uint64_t FoundSAddr; It->getAddress(FoundSAddr);
259 if (FoundSAddr < SAddr + SSize)
260 llvm_unreachable("Inserting overlapping sections");
262 SortedSections.insert(It, *SI);
266 void MCObjectSymbolizer::buildRelocationByAddrMap() {
268 for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections();
269 SI != SE; SI.increment(ec)) {
272 section_iterator RelSecI = SI->getRelocatedSection();
273 if (RelSecI == Obj->end_sections())
276 uint64_t StartAddr; RelSecI->getAddress(StartAddr);
277 uint64_t Size; RelSecI->getSize(Size);
278 bool RequiredForExec; RelSecI->isRequiredForExecution(RequiredForExec);
279 if (RequiredForExec == false || Size == 0)
281 for (relocation_iterator RI = SI->begin_relocations(),
282 RE = SI->end_relocations();
286 // FIXME: libObject is inconsistent regarding error handling. The
287 // overwhelming majority of methods always return object_error::success,
288 // and assert for simple errors.. Here, ELFObjectFile::getRelocationOffset
289 // asserts when the file type isn't ET_REL.
290 // This workaround handles x86-64 elf, the only one that has a relocinfo.
293 const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj);
296 if (ELFObj->getELFFile()->getHeader()->e_type == ELF::ET_REL) {
297 RI->getOffset(Offset);
300 RI->getAddress(Offset);
303 RI->getOffset(Offset);
306 // At a specific address, only keep the first relocation.
307 if (AddrToReloc.find(Offset) == AddrToReloc.end())
308 AddrToReloc[Offset] = *RI;