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/ELFObjectFile.h"
18 #include "llvm/Object/MachO.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::section_64 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)
90 MOOF->getIndirectSymbolTableEntry(MOOF->getDysymtabLoadCommand(), StubIdx);
93 symbol_iterator SI = MOOF->begin_symbols();
95 for (uint32_t i = 0; i != SymtabIdx; ++i) {
99 assert(SI != MOOF->end_symbols() && "Stub wasn't found in the symbol table!");
100 assert(SymName.front() == '_' && "Mach-O symbol doesn't start with '_'!");
101 return SymName.substr(1);
104 void MCMachObjectSymbolizer::
105 tryAddingPcLoadReferenceComment(raw_ostream &cStream, int64_t Value,
107 if (const RelocationRef *R = findRelocationAt(Address)) {
108 const MCExpr *RelExpr = RelInfo->createExprForRelocation(*R);
109 if (!RelExpr || RelExpr->EvaluateAsAbsolute(Value) == false)
112 uint64_t Addr = Value;
113 if (const SectionRef *S = findSectionContaining(Addr)) {
114 StringRef Name; S->getName(Name);
115 uint64_t SAddr; S->getAddress(SAddr);
116 if (Name == "__cstring") {
118 S->getContents(Contents);
119 Contents = Contents.substr(Addr - SAddr);
120 cStream << " ## literal pool for: "
121 << Contents.substr(0, Contents.find_first_of(0));
126 //===- MCObjectSymbolizer -------------------------------------------------===//
128 MCObjectSymbolizer::MCObjectSymbolizer(MCContext &Ctx,
129 OwningPtr<MCRelocationInfo> &RelInfo,
130 const ObjectFile *Obj)
131 : MCSymbolizer(Ctx, RelInfo), Obj(Obj), SortedSections(), AddrToReloc() {
134 bool MCObjectSymbolizer::
135 tryAddingSymbolicOperand(MCInst &MI, raw_ostream &cStream,
136 int64_t Value, uint64_t Address, bool IsBranch,
137 uint64_t Offset, uint64_t InstSize) {
139 StringRef ExtFnName = findExternalFunctionAt((uint64_t)Value);
140 if (!ExtFnName.empty()) {
141 MCSymbol *Sym = Ctx.GetOrCreateSymbol(ExtFnName);
142 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
143 MI.addOperand(MCOperand::CreateExpr(Expr));
148 if (const RelocationRef *R = findRelocationAt(Address + Offset)) {
149 if (const MCExpr *RelExpr = RelInfo->createExprForRelocation(*R)) {
150 MI.addOperand(MCOperand::CreateExpr(RelExpr));
153 // Only try to create a symbol+offset expression if there is no relocation.
157 // Interpret Value as a branch target.
158 if (IsBranch == false)
160 uint64_t UValue = Value;
161 // FIXME: map instead of looping each time?
163 for (symbol_iterator SI = Obj->begin_symbols(), SE = Obj->end_symbols();
164 SI != SE; SI.increment(ec)) {
166 uint64_t SymAddr; SI->getAddress(SymAddr);
167 uint64_t SymSize; SI->getSize(SymSize);
168 StringRef SymName; SI->getName(SymName);
169 SymbolRef::Type SymType; SI->getType(SymType);
170 if (SymAddr == UnknownAddressOrSize || SymSize == UnknownAddressOrSize
171 || SymName.empty() || SymType != SymbolRef::ST_Function)
174 if ( SymAddr == UValue ||
175 (SymAddr <= UValue && SymAddr + SymSize > UValue)) {
176 MCSymbol *Sym = Ctx.GetOrCreateSymbol(SymName);
177 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
178 if (SymAddr != UValue) {
179 const MCExpr *Off = MCConstantExpr::Create(UValue - SymAddr, Ctx);
180 Expr = MCBinaryExpr::CreateAdd(Expr, Off, Ctx);
182 MI.addOperand(MCOperand::CreateExpr(Expr));
189 void MCObjectSymbolizer::
190 tryAddingPcLoadReferenceComment(raw_ostream &cStream,
191 int64_t Value, uint64_t Address) {
194 StringRef MCObjectSymbolizer::findExternalFunctionAt(uint64_t Addr) {
199 MCObjectSymbolizer::createObjectSymbolizer(MCContext &Ctx,
200 OwningPtr<MCRelocationInfo> &RelInfo,
201 const ObjectFile *Obj) {
202 if (const MachOObjectFile *MOOF = dyn_cast<MachOObjectFile>(Obj))
203 return new MCMachObjectSymbolizer(Ctx, RelInfo, MOOF);
204 return new MCObjectSymbolizer(Ctx, RelInfo, Obj);
207 // SortedSections implementation.
209 static bool SectionStartsBefore(const SectionRef &S, uint64_t Addr) {
210 uint64_t SAddr; S.getAddress(SAddr);
214 const SectionRef *MCObjectSymbolizer::findSectionContaining(uint64_t Addr) {
215 if (SortedSections.empty())
218 SortedSectionList::iterator
219 EndIt = SortedSections.end(),
220 It = std::lower_bound(SortedSections.begin(), EndIt,
221 Addr, SectionStartsBefore);
224 uint64_t SAddr; It->getAddress(SAddr);
225 uint64_t SSize; It->getSize(SSize);
226 if (Addr >= SAddr + SSize)
231 const RelocationRef *MCObjectSymbolizer::findRelocationAt(uint64_t Addr) {
232 if (AddrToReloc.empty())
233 buildRelocationByAddrMap();
235 AddrToRelocMap::const_iterator RI = AddrToReloc.find(Addr);
236 if (RI == AddrToReloc.end())
241 void MCObjectSymbolizer::buildSectionList() {
243 for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections();
244 SI != SE; SI.increment(ec)) {
247 bool RequiredForExec; SI->isRequiredForExecution(RequiredForExec);
248 if (RequiredForExec == false)
250 uint64_t SAddr; SI->getAddress(SAddr);
251 uint64_t SSize; SI->getSize(SSize);
252 SortedSectionList::iterator It = std::lower_bound(SortedSections.begin(),
253 SortedSections.end(),
255 SectionStartsBefore);
256 if (It != SortedSections.end()) {
257 uint64_t FoundSAddr; It->getAddress(FoundSAddr);
258 if (FoundSAddr < SAddr + SSize)
259 llvm_unreachable("Inserting overlapping sections");
261 SortedSections.insert(It, *SI);
265 void MCObjectSymbolizer::buildRelocationByAddrMap() {
267 for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections();
268 SI != SE; SI.increment(ec)) {
271 section_iterator RelSecI = SI->getRelocatedSection();
272 if (RelSecI == Obj->end_sections())
275 uint64_t StartAddr; RelSecI->getAddress(StartAddr);
276 uint64_t Size; RelSecI->getSize(Size);
277 bool RequiredForExec; RelSecI->isRequiredForExecution(RequiredForExec);
278 if (RequiredForExec == false || Size == 0)
280 for (relocation_iterator RI = SI->begin_relocations(),
281 RE = SI->end_relocations();
285 // FIXME: libObject is inconsistent regarding error handling. The
286 // overwhelming majority of methods always return object_error::success,
287 // and assert for simple errors.. Here, ELFObjectFile::getRelocationOffset
288 // asserts when the file type isn't ET_REL.
289 // This workaround handles x86-64 elf, the only one that has a relocinfo.
292 const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj);
295 if (ELFObj->getELFFile()->getHeader()->e_type == ELF::ET_REL) {
296 RI->getOffset(Offset);
299 RI->getAddress(Offset);
302 RI->getOffset(Offset);
305 // At a specific address, only keep the first relocation.
306 if (AddrToReloc.find(Offset) == AddrToReloc.end())
307 AddrToReloc[Offset] = *RI;