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) {
55 for (section_iterator SI = MOOF->section_begin(), SE = MOOF->section_end();
57 StringRef Name; SI->getName(Name);
58 if (Name == "__stubs") {
59 SectionRef StubsSec = *SI;
60 if (MOOF->is64Bit()) {
61 MachO::section_64 S = MOOF->getSection64(StubsSec.getRawDataRefImpl());
62 StubsIndSymIndex = S.reserved1;
63 StubSize = S.reserved2;
65 MachO::section S = MOOF->getSection(StubsSec.getRawDataRefImpl());
66 StubsIndSymIndex = S.reserved1;
67 StubSize = S.reserved2;
69 assert(StubSize && "Mach-O stub entry size can't be zero!");
70 StubsSec.getAddress(StubsStart);
71 StubsSec.getSize(StubsCount);
72 StubsCount /= StubSize;
77 StringRef MCMachObjectSymbolizer::findExternalFunctionAt(uint64_t Addr) {
78 // FIXME: also, this can all be done at the very beginning, by iterating over
79 // all stubs and creating the calls to outside functions. Is it worth it
83 uint64_t StubIdx = (Addr - StubsStart) / StubSize;
84 if (StubIdx >= StubsCount)
88 MOOF->getIndirectSymbolTableEntry(MOOF->getDysymtabLoadCommand(), StubIdx);
91 symbol_iterator SI = MOOF->symbol_begin();
92 for (uint32_t i = 0; i != SymtabIdx; ++i)
95 assert(SI != MOOF->symbol_end() && "Stub wasn't found in the symbol table!");
96 assert(SymName.front() == '_' && "Mach-O symbol doesn't start with '_'!");
97 return SymName.substr(1);
100 void MCMachObjectSymbolizer::
101 tryAddingPcLoadReferenceComment(raw_ostream &cStream, int64_t Value,
103 if (const RelocationRef *R = findRelocationAt(Address)) {
104 const MCExpr *RelExpr = RelInfo->createExprForRelocation(*R);
105 if (!RelExpr || RelExpr->EvaluateAsAbsolute(Value) == false)
108 uint64_t Addr = Value;
109 if (const SectionRef *S = findSectionContaining(Addr)) {
110 StringRef Name; S->getName(Name);
111 uint64_t SAddr; S->getAddress(SAddr);
112 if (Name == "__cstring") {
114 S->getContents(Contents);
115 Contents = Contents.substr(Addr - SAddr);
116 cStream << " ## literal pool for: "
117 << Contents.substr(0, Contents.find_first_of(0));
122 //===- MCObjectSymbolizer -------------------------------------------------===//
124 MCObjectSymbolizer::MCObjectSymbolizer(MCContext &Ctx,
125 OwningPtr<MCRelocationInfo> &RelInfo,
126 const ObjectFile *Obj)
127 : MCSymbolizer(Ctx, RelInfo), Obj(Obj), SortedSections(), AddrToReloc() {
130 bool MCObjectSymbolizer::
131 tryAddingSymbolicOperand(MCInst &MI, raw_ostream &cStream,
132 int64_t Value, uint64_t Address, bool IsBranch,
133 uint64_t Offset, uint64_t InstSize) {
135 StringRef ExtFnName = findExternalFunctionAt((uint64_t)Value);
136 if (!ExtFnName.empty()) {
137 MCSymbol *Sym = Ctx.GetOrCreateSymbol(ExtFnName);
138 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
139 MI.addOperand(MCOperand::CreateExpr(Expr));
144 if (const RelocationRef *R = findRelocationAt(Address + Offset)) {
145 if (const MCExpr *RelExpr = RelInfo->createExprForRelocation(*R)) {
146 MI.addOperand(MCOperand::CreateExpr(RelExpr));
149 // Only try to create a symbol+offset expression if there is no relocation.
153 // Interpret Value as a branch target.
154 if (IsBranch == false)
156 uint64_t UValue = Value;
157 // FIXME: map instead of looping each time?
158 for (symbol_iterator SI = Obj->symbol_begin(), SE = Obj->symbol_end();
160 uint64_t SymAddr; SI->getAddress(SymAddr);
161 uint64_t SymSize; SI->getSize(SymSize);
162 StringRef SymName; SI->getName(SymName);
163 SymbolRef::Type SymType; SI->getType(SymType);
164 if (SymAddr == UnknownAddressOrSize || SymSize == UnknownAddressOrSize
165 || SymName.empty() || SymType != SymbolRef::ST_Function)
168 if ( SymAddr == UValue ||
169 (SymAddr <= UValue && SymAddr + SymSize > UValue)) {
170 MCSymbol *Sym = Ctx.GetOrCreateSymbol(SymName);
171 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
172 if (SymAddr != UValue) {
173 const MCExpr *Off = MCConstantExpr::Create(UValue - SymAddr, Ctx);
174 Expr = MCBinaryExpr::CreateAdd(Expr, Off, Ctx);
176 MI.addOperand(MCOperand::CreateExpr(Expr));
183 void MCObjectSymbolizer::
184 tryAddingPcLoadReferenceComment(raw_ostream &cStream,
185 int64_t Value, uint64_t Address) {
188 StringRef MCObjectSymbolizer::findExternalFunctionAt(uint64_t Addr) {
193 MCObjectSymbolizer::createObjectSymbolizer(MCContext &Ctx,
194 OwningPtr<MCRelocationInfo> &RelInfo,
195 const ObjectFile *Obj) {
196 if (const MachOObjectFile *MOOF = dyn_cast<MachOObjectFile>(Obj))
197 return new MCMachObjectSymbolizer(Ctx, RelInfo, MOOF);
198 return new MCObjectSymbolizer(Ctx, RelInfo, Obj);
201 // SortedSections implementation.
203 static bool SectionStartsBefore(const SectionRef &S, uint64_t Addr) {
204 uint64_t SAddr; S.getAddress(SAddr);
208 const SectionRef *MCObjectSymbolizer::findSectionContaining(uint64_t Addr) {
209 if (SortedSections.empty())
212 SortedSectionList::iterator
213 EndIt = SortedSections.end(),
214 It = std::lower_bound(SortedSections.begin(), EndIt,
215 Addr, SectionStartsBefore);
218 uint64_t SAddr; It->getAddress(SAddr);
219 uint64_t SSize; It->getSize(SSize);
220 if (Addr >= SAddr + SSize)
225 const RelocationRef *MCObjectSymbolizer::findRelocationAt(uint64_t Addr) {
226 if (AddrToReloc.empty())
227 buildRelocationByAddrMap();
229 AddrToRelocMap::const_iterator RI = AddrToReloc.find(Addr);
230 if (RI == AddrToReloc.end())
235 void MCObjectSymbolizer::buildSectionList() {
236 for (section_iterator SI = Obj->section_begin(), SE = Obj->section_end();
238 bool RequiredForExec; SI->isRequiredForExecution(RequiredForExec);
239 if (RequiredForExec == false)
241 uint64_t SAddr; SI->getAddress(SAddr);
242 uint64_t SSize; SI->getSize(SSize);
243 SortedSectionList::iterator It = std::lower_bound(SortedSections.begin(),
244 SortedSections.end(),
246 SectionStartsBefore);
247 if (It != SortedSections.end()) {
248 uint64_t FoundSAddr; It->getAddress(FoundSAddr);
249 if (FoundSAddr < SAddr + SSize)
250 llvm_unreachable("Inserting overlapping sections");
252 SortedSections.insert(It, *SI);
256 void MCObjectSymbolizer::buildRelocationByAddrMap() {
257 for (section_iterator SI = Obj->section_begin(), SE = Obj->section_end();
259 section_iterator RelSecI = SI->getRelocatedSection();
260 if (RelSecI == Obj->section_end())
263 uint64_t StartAddr; RelSecI->getAddress(StartAddr);
264 uint64_t Size; RelSecI->getSize(Size);
265 bool RequiredForExec; RelSecI->isRequiredForExecution(RequiredForExec);
266 if (RequiredForExec == false || Size == 0)
268 for (relocation_iterator RI = SI->relocation_begin(),
269 RE = SI->relocation_end();
271 // FIXME: libObject is inconsistent regarding error handling. The
272 // overwhelming majority of methods always return object_error::success,
273 // and assert for simple errors.. Here, ELFObjectFile::getRelocationOffset
274 // asserts when the file type isn't ET_REL.
275 // This workaround handles x86-64 elf, the only one that has a relocinfo.
278 const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj);
281 if (ELFObj->getELFFile()->getHeader()->e_type == ELF::ET_REL) {
282 RI->getOffset(Offset);
285 RI->getAddress(Offset);
288 RI->getOffset(Offset);
291 // At a specific address, only keep the first relocation.
292 if (AddrToReloc.find(Offset) == AddrToReloc.end())
293 AddrToReloc[Offset] = *RI;