LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCObjectSymbolizer.cpp
Go to the documentation of this file.
1 //===-- lib/MC/MCObjectSymbolizer.cpp -------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCInst.h"
16 #include "llvm/MC/MCSymbol.h"
17 #include "llvm/Object/MachO.h"
20 #include <algorithm>
21 
22 using namespace llvm;
23 using namespace object;
24 
25 //===- MCMachObjectSymbolizer ---------------------------------------------===//
26 
27 namespace {
28 class MCMachObjectSymbolizer : public MCObjectSymbolizer {
29  const MachOObjectFile *MOOF;
30  // __TEXT;__stubs support.
31  uint64_t StubsStart;
32  uint64_t StubsCount;
33  uint64_t StubSize;
34  uint64_t StubsIndSymIndex;
35 
36 public:
37  MCMachObjectSymbolizer(MCContext &Ctx, OwningPtr<MCRelocationInfo> &RelInfo,
38  const MachOObjectFile *MOOF);
39 
40  StringRef findExternalFunctionAt(uint64_t Addr) LLVM_OVERRIDE;
41 
43  int64_t Value,
44  uint64_t Address) LLVM_OVERRIDE;
45 };
46 } // End unnamed namespace
47 
48 
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) {
54 
55  error_code ec;
56  for (section_iterator SI = MOOF->begin_sections(), SE = MOOF->end_sections();
57  SI != SE; SI.increment(ec)) {
58  if (ec) break;
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;
66  } else {
67  MachO::section S = MOOF->getSection(StubsSec.getRawDataRefImpl());
68  StubsIndSymIndex = S.reserved1;
69  StubSize = S.reserved2;
70  }
71  assert(StubSize && "Mach-O stub entry size can't be zero!");
72  StubsSec.getAddress(StubsStart);
73  StubsSec.getSize(StubsCount);
74  StubsCount /= StubSize;
75  }
76  }
77 }
78 
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
82  // though?
83  if (!StubSize)
84  return StringRef();
85  uint64_t StubIdx = (Addr - StubsStart) / StubSize;
86  if (StubIdx >= StubsCount)
87  return StringRef();
88 
89  uint32_t SymtabIdx =
91 
92  StringRef SymName;
93  symbol_iterator SI = MOOF->begin_symbols();
94  error_code ec;
95  for (uint32_t i = 0; i != SymtabIdx; ++i) {
96  SI.increment(ec);
97  }
98  SI->getName(SymName);
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);
102 }
103 
106  uint64_t Address) {
107  if (const RelocationRef *R = findRelocationAt(Address)) {
108  const MCExpr *RelExpr = RelInfo->createExprForRelocation(*R);
109  if (!RelExpr || RelExpr->EvaluateAsAbsolute(Value) == false)
110  return;
111  }
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") {
117  StringRef Contents;
118  S->getContents(Contents);
119  Contents = Contents.substr(Addr - SAddr);
120  cStream << " ## literal pool for: "
121  << Contents.substr(0, Contents.find_first_of(0));
122  }
123  }
124 }
125 
126 //===- MCObjectSymbolizer -------------------------------------------------===//
127 
130  const ObjectFile *Obj)
131  : MCSymbolizer(Ctx, RelInfo), Obj(Obj), SortedSections(), AddrToReloc() {
132 }
133 
136  int64_t Value, uint64_t Address, bool IsBranch,
137  uint64_t Offset, uint64_t InstSize) {
138  if (IsBranch) {
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);
144  return true;
145  }
146  }
147 
148  if (const RelocationRef *R = findRelocationAt(Address + Offset)) {
149  if (const MCExpr *RelExpr = RelInfo->createExprForRelocation(*R)) {
150  MI.addOperand(MCOperand::CreateExpr(RelExpr));
151  return true;
152  }
153  // Only try to create a symbol+offset expression if there is no relocation.
154  return false;
155  }
156 
157  // Interpret Value as a branch target.
158  if (IsBranch == false)
159  return false;
160  uint64_t UValue = Value;
161  // FIXME: map instead of looping each time?
162  error_code ec;
163  for (symbol_iterator SI = Obj->begin_symbols(), SE = Obj->end_symbols();
164  SI != SE; SI.increment(ec)) {
165  if (ec) break;
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)
172  continue;
173 
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);
181  }
183  return true;
184  }
185  }
186  return false;
187 }
188 
191  int64_t Value, uint64_t Address) {
192 }
193 
195  return StringRef();
196 }
197 
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);
205 }
206 
207 // SortedSections implementation.
208 
209 static bool SectionStartsBefore(const SectionRef &S, uint64_t Addr) {
210  uint64_t SAddr; S.getAddress(SAddr);
211  return SAddr < Addr;
212 }
213 
215  if (SortedSections.empty())
216  buildSectionList();
217 
218  SortedSectionList::iterator
219  EndIt = SortedSections.end(),
220  It = std::lower_bound(SortedSections.begin(), EndIt,
221  Addr, SectionStartsBefore);
222  if (It == EndIt)
223  return 0;
224  uint64_t SAddr; It->getAddress(SAddr);
225  uint64_t SSize; It->getSize(SSize);
226  if (Addr >= SAddr + SSize)
227  return 0;
228  return &*It;
229 }
230 
232  if (AddrToReloc.empty())
233  buildRelocationByAddrMap();
234 
235  AddrToRelocMap::const_iterator RI = AddrToReloc.find(Addr);
236  if (RI == AddrToReloc.end())
237  return 0;
238  return &RI->second;
239 }
240 
241 void MCObjectSymbolizer::buildSectionList() {
242  error_code ec;
243  for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections();
244  SI != SE; SI.increment(ec)) {
245  if (ec) break;
246 
247  bool RequiredForExec; SI->isRequiredForExecution(RequiredForExec);
248  if (RequiredForExec == false)
249  continue;
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(),
254  SAddr,
256  if (It != SortedSections.end()) {
257  uint64_t FoundSAddr; It->getAddress(FoundSAddr);
258  if (FoundSAddr < SAddr + SSize)
259  llvm_unreachable("Inserting overlapping sections");
260  }
261  SortedSections.insert(It, *SI);
262  }
263 }
264 
265 void MCObjectSymbolizer::buildRelocationByAddrMap() {
266  error_code ec;
267  for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections();
268  SI != SE; SI.increment(ec)) {
269  if (ec) break;
270 
271  section_iterator RelSecI = SI->getRelocatedSection();
272  if (RelSecI == Obj->end_sections())
273  continue;
274 
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)
279  continue;
280  for (relocation_iterator RI = SI->begin_relocations(),
281  RE = SI->end_relocations();
282  RI != RE;
283  RI.increment(ec)) {
284  if (ec) break;
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.
290  uint64_t Offset;
291  if (Obj->isELF()) {
293  if (ELFObj == 0)
294  break;
295  if (ELFObj->getELFFile()->getHeader()->e_type == ELF::ET_REL) {
296  RI->getOffset(Offset);
297  Offset += StartAddr;
298  } else {
299  RI->getAddress(Offset);
300  }
301  } else {
302  RI->getOffset(Offset);
303  Offset += StartAddr;
304  }
305  // At a specific address, only keep the first relocation.
306  if (AddrToReloc.find(Offset) == AddrToReloc.end())
307  AddrToReloc[Offset] = *RI;
308  }
309  }
310 }
bool isELF() const
Definition: Binary.h:95
static const MCConstantExpr * Create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:152
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
const object::SectionRef * findSectionContaining(uint64_t Addr)
error_code getSize(uint64_t &Result) const
Definition: ObjectFile.h:476
static MCOperand CreateExpr(const MCExpr *Val)
Definition: MCInst.h:129
StringRef substr(size_t Start, size_t N=npos) const
Definition: StringRef.h:392
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:533
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) const
virtual section_iterator end_sections() const
MCSymbol * GetOrCreateSymbol(StringRef Name)
Definition: MCContext.cpp:118
#define llvm_unreachable(msg)
virtual section_iterator end_sections() const =0
virtual section_iterator begin_sections() const
MCObjectSymbolizer(MCContext &Ctx, OwningPtr< MCRelocationInfo > &RelInfo, const object::ObjectFile *Obj)
static bool SectionStartsBefore(const SectionRef &S, uint64_t Addr)
const ELFFile< ELFT > * getELFFile() const
const object::ObjectFile * Obj
MachO::section_64 getSection64(DataRefImpl DRI) const
static const MCSymbolRefExpr * Create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:270
virtual section_iterator begin_sections() const =0
const object::RelocationRef * findRelocationAt(uint64_t Addr)
Symbolize and annotate disassembled instructions.
Definition: MCSymbolizer.h:39
MachO::section getSection(DataRefImpl DRI) const
error_code getAddress(uint64_t &Result) const
Definition: ObjectFile.h:472
MachO::dysymtab_command getDysymtabLoadCommand() const
void tryAddingPcLoadReferenceComment(raw_ostream &cStream, int64_t Value, uint64_t Address)
Try to add a comment on the PC-relative load. For instance, in Mach-O, this is used to add annotation...
iterator end()
Definition: DenseMap.h:57
virtual symbol_iterator end_symbols() const =0
content_iterator & increment(error_code &err)
Definition: ObjectFile.h:65
virtual symbol_iterator begin_symbols() const
virtual const MCExpr * createExprForRelocation(object::RelocationRef Rel)
Create an MCExpr for the relocation Rel.
An ObjectFile-backed symbolizer.
virtual symbol_iterator end_symbols() const
const uint64_t UnknownAddressOrSize
Definition: ObjectFile.h:261
static MCObjectSymbolizer * createObjectSymbolizer(MCContext &Ctx, OwningPtr< MCRelocationInfo > &RelInfo, const object::ObjectFile *Obj)
Create an object symbolizer for Obj.
static const MCBinaryExpr * CreateAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:396
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: DenseMap.h:67
bool tryAddingSymbolicOperand(MCInst &MI, raw_ostream &cStream, int64_t Value, uint64_t Address, bool IsBranch, uint64_t Offset, uint64_t InstSize)
Try to add a symbolic operand instead of Value to the MCInst.
size_t find_first_of(char C, size_t From=0) const
Definition: StringRef.h:269
char front() const
front - Get the first character in the string.
Definition: StringRef.h:116
virtual symbol_iterator begin_symbols() const =0
LLVM Value Representation.
Definition: Value.h:66
void addOperand(const MCOperand &Op)
Definition: MCInst.h:167
static void tryAddingPcLoadReferenceComment(uint64_t Address, int Value, const void *Decoder)
iterator find(const KeyT &Val)
Definition: DenseMap.h:108
virtual StringRef findExternalFunctionAt(uint64_t Addr)
Look for an external function symbol at Addr. (References through the ELF PLT, Mach-O stubs...
#define LLVM_OVERRIDE
Definition: Compiler.h:155
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110