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,
38 std::unique_ptr<MCRelocationInfo> &RelInfo,
39 const MachOObjectFile *MOOF);
41 StringRef findExternalFunctionAt(uint64_t Addr) override;
43 void tryAddingPcLoadReferenceComment(raw_ostream &cStream, int64_t Value,
44 uint64_t Address) override;
46 } // End unnamed namespace
48 MCMachObjectSymbolizer::MCMachObjectSymbolizer(
49 MCContext &Ctx, std::unique_ptr<MCRelocationInfo> &RelInfo,
50 const MachOObjectFile *MOOF)
51 : MCObjectSymbolizer(Ctx, RelInfo, MOOF), MOOF(MOOF), StubsStart(0),
52 StubsCount(0), StubSize(0), StubsIndSymIndex(0) {
54 for (const SectionRef &Section : MOOF->sections()) {
56 Section.getName(Name);
57 if (Name == "__stubs") {
58 SectionRef StubsSec = Section;
59 if (MOOF->is64Bit()) {
60 MachO::section_64 S = MOOF->getSection64(StubsSec.getRawDataRefImpl());
61 StubsIndSymIndex = S.reserved1;
62 StubSize = S.reserved2;
64 MachO::section S = MOOF->getSection(StubsSec.getRawDataRefImpl());
65 StubsIndSymIndex = S.reserved1;
66 StubSize = S.reserved2;
68 assert(StubSize && "Mach-O stub entry size can't be zero!");
69 StubsSec.getAddress(StubsStart);
70 StubsSec.getSize(StubsCount);
71 StubsCount /= StubSize;
76 StringRef MCMachObjectSymbolizer::findExternalFunctionAt(uint64_t Addr) {
77 // FIXME: also, this can all be done at the very beginning, by iterating over
78 // all stubs and creating the calls to outside functions. Is it worth it
82 uint64_t StubIdx = (Addr - StubsStart) / StubSize;
83 if (StubIdx >= StubsCount)
87 MOOF->getIndirectSymbolTableEntry(MOOF->getDysymtabLoadCommand(), StubIdx);
90 symbol_iterator SI = MOOF->symbol_begin();
91 for (uint32_t i = 0; i != SymtabIdx; ++i)
94 assert(SI != MOOF->symbol_end() && "Stub wasn't found in the symbol table!");
95 assert(SymName.front() == '_' && "Mach-O symbol doesn't start with '_'!");
96 return SymName.substr(1);
99 void MCMachObjectSymbolizer::
100 tryAddingPcLoadReferenceComment(raw_ostream &cStream, int64_t Value,
102 if (const RelocationRef *R = findRelocationAt(Address)) {
103 const MCExpr *RelExpr = RelInfo->createExprForRelocation(*R);
104 if (!RelExpr || RelExpr->EvaluateAsAbsolute(Value) == false)
107 uint64_t Addr = Value;
108 if (const SectionRef *S = findSectionContaining(Addr)) {
109 StringRef Name; S->getName(Name);
110 uint64_t SAddr; S->getAddress(SAddr);
111 if (Name == "__cstring") {
113 S->getContents(Contents);
114 Contents = Contents.substr(Addr - SAddr);
115 cStream << " ## literal pool for: "
116 << Contents.substr(0, Contents.find_first_of(0));
121 //===- MCObjectSymbolizer -------------------------------------------------===//
123 MCObjectSymbolizer::MCObjectSymbolizer(
124 MCContext &Ctx, std::unique_ptr<MCRelocationInfo> &RelInfo,
125 const ObjectFile *Obj)
126 : MCSymbolizer(Ctx, RelInfo), Obj(Obj), SortedSections(), AddrToReloc() {}
128 bool MCObjectSymbolizer::
129 tryAddingSymbolicOperand(MCInst &MI, raw_ostream &cStream,
130 int64_t Value, uint64_t Address, bool IsBranch,
131 uint64_t Offset, uint64_t InstSize) {
133 StringRef ExtFnName = findExternalFunctionAt((uint64_t)Value);
134 if (!ExtFnName.empty()) {
135 MCSymbol *Sym = Ctx.GetOrCreateSymbol(ExtFnName);
136 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
137 MI.addOperand(MCOperand::CreateExpr(Expr));
142 if (const RelocationRef *R = findRelocationAt(Address + Offset)) {
143 if (const MCExpr *RelExpr = RelInfo->createExprForRelocation(*R)) {
144 MI.addOperand(MCOperand::CreateExpr(RelExpr));
147 // Only try to create a symbol+offset expression if there is no relocation.
151 // Interpret Value as a branch target.
152 if (IsBranch == false)
154 uint64_t UValue = Value;
155 // FIXME: map instead of looping each time?
156 for (symbol_iterator SI = Obj->symbol_begin(), SE = Obj->symbol_end();
158 uint64_t SymAddr; SI->getAddress(SymAddr);
159 uint64_t SymSize; SI->getSize(SymSize);
160 StringRef SymName; SI->getName(SymName);
161 SymbolRef::Type SymType; SI->getType(SymType);
162 if (SymAddr == UnknownAddressOrSize || SymSize == UnknownAddressOrSize
163 || SymName.empty() || SymType != SymbolRef::ST_Function)
166 if ( SymAddr == UValue ||
167 (SymAddr <= UValue && SymAddr + SymSize > UValue)) {
168 MCSymbol *Sym = Ctx.GetOrCreateSymbol(SymName);
169 const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx);
170 if (SymAddr != UValue) {
171 const MCExpr *Off = MCConstantExpr::Create(UValue - SymAddr, Ctx);
172 Expr = MCBinaryExpr::CreateAdd(Expr, Off, Ctx);
174 MI.addOperand(MCOperand::CreateExpr(Expr));
181 void MCObjectSymbolizer::
182 tryAddingPcLoadReferenceComment(raw_ostream &cStream,
183 int64_t Value, uint64_t Address) {
186 StringRef MCObjectSymbolizer::findExternalFunctionAt(uint64_t Addr) {
190 MCObjectSymbolizer *MCObjectSymbolizer::createObjectSymbolizer(
191 MCContext &Ctx, std::unique_ptr<MCRelocationInfo> &RelInfo,
192 const ObjectFile *Obj) {
193 if (const MachOObjectFile *MOOF = dyn_cast<MachOObjectFile>(Obj))
194 return new MCMachObjectSymbolizer(Ctx, RelInfo, MOOF);
195 return new MCObjectSymbolizer(Ctx, RelInfo, Obj);
198 // SortedSections implementation.
200 static bool SectionStartsBefore(const SectionRef &S, uint64_t Addr) {
201 uint64_t SAddr; S.getAddress(SAddr);
205 const SectionRef *MCObjectSymbolizer::findSectionContaining(uint64_t Addr) {
206 if (SortedSections.empty())
209 SortedSectionList::iterator
210 EndIt = SortedSections.end(),
211 It = std::lower_bound(SortedSections.begin(), EndIt,
212 Addr, SectionStartsBefore);
215 uint64_t SAddr; It->getAddress(SAddr);
216 uint64_t SSize; It->getSize(SSize);
217 if (Addr >= SAddr + SSize)
222 const RelocationRef *MCObjectSymbolizer::findRelocationAt(uint64_t Addr) {
223 if (AddrToReloc.empty())
224 buildRelocationByAddrMap();
226 AddrToRelocMap::const_iterator RI = AddrToReloc.find(Addr);
227 if (RI == AddrToReloc.end())
232 void MCObjectSymbolizer::buildSectionList() {
233 for (const SectionRef &Section : Obj->sections()) {
234 bool RequiredForExec;
235 Section.isRequiredForExecution(RequiredForExec);
236 if (RequiredForExec == false)
239 Section.getAddress(SAddr);
241 Section.getSize(SSize);
242 SortedSectionList::iterator It =
243 std::lower_bound(SortedSections.begin(), SortedSections.end(), SAddr,
244 SectionStartsBefore);
245 if (It != SortedSections.end()) {
246 uint64_t FoundSAddr; It->getAddress(FoundSAddr);
247 if (FoundSAddr < SAddr + SSize)
248 llvm_unreachable("Inserting overlapping sections");
250 SortedSections.insert(It, Section);
254 void MCObjectSymbolizer::buildRelocationByAddrMap() {
255 for (const SectionRef &Section : Obj->sections()) {
256 section_iterator RelSecI = Section.getRelocatedSection();
257 if (RelSecI == Obj->section_end())
260 uint64_t StartAddr; RelSecI->getAddress(StartAddr);
261 uint64_t Size; RelSecI->getSize(Size);
262 bool RequiredForExec;
263 RelSecI->isRequiredForExecution(RequiredForExec);
264 if (RequiredForExec == false || Size == 0)
266 for (const RelocationRef &Reloc : Section.relocations()) {
267 // FIXME: libObject is inconsistent regarding error handling. The
268 // overwhelming majority of methods always return object_error::success,
269 // and assert for simple errors.. Here, ELFObjectFile::getRelocationOffset
270 // asserts when the file type isn't ET_REL.
271 // This workaround handles x86-64 elf, the only one that has a relocinfo.
274 const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj);
277 if (ELFObj->getELFFile()->getHeader()->e_type == ELF::ET_REL) {
278 Reloc.getOffset(Offset);
281 Reloc.getAddress(Offset);
284 Reloc.getOffset(Offset);
287 // At a specific address, only keep the first relocation.
288 if (AddrToReloc.find(Offset) == AddrToReloc.end())
289 AddrToReloc[Offset] = Reloc;