Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 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 | |
| 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" |
Michael J. Spencer | 081a194 | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 18 | #include "llvm/Object/ELFObjectFile.h" |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Ahmed Bougacha | b54d297 | 2013-05-30 18:18:36 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | using namespace object; |
| 24 | |
| 25 | //===- MCMachObjectSymbolizer ---------------------------------------------===// |
| 26 | |
| 27 | namespace { |
| 28 | class MCMachObjectSymbolizer : public MCObjectSymbolizer { |
Ahmed Bougacha | 9bfc062 | 2013-08-21 07:28:13 +0000 | [diff] [blame^] | 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 | |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 36 | public: |
| 37 | MCMachObjectSymbolizer(MCContext &Ctx, OwningPtr<MCRelocationInfo> &RelInfo, |
Ahmed Bougacha | 9bfc062 | 2013-08-21 07:28:13 +0000 | [diff] [blame^] | 38 | const MachOObjectFile *MOOF); |
| 39 | |
| 40 | StringRef findExternalFunctionAt(uint64_t Addr) LLVM_OVERRIDE; |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 41 | |
| 42 | void tryAddingPcLoadReferenceComment(raw_ostream &cStream, |
Ahmed Bougacha | cdef37a | 2013-08-21 07:28:07 +0000 | [diff] [blame] | 43 | int64_t Value, |
| 44 | uint64_t Address) LLVM_OVERRIDE; |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 45 | }; |
| 46 | } // End unnamed namespace |
| 47 | |
Ahmed Bougacha | cdef37a | 2013-08-21 07:28:07 +0000 | [diff] [blame] | 48 | |
Ahmed Bougacha | 9bfc062 | 2013-08-21 07:28:13 +0000 | [diff] [blame^] | 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::Section64 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 | macho::IndirectSymbolTableEntry ISTE = |
| 90 | MOOF->getIndirectSymbolTableEntry(MOOF->getDysymtabLoadCommand(), StubIdx); |
| 91 | uint32_t SymtabIdx = ISTE.Index; |
| 92 | |
| 93 | StringRef SymName; |
| 94 | symbol_iterator SI = MOOF->begin_symbols(); |
| 95 | error_code ec; |
| 96 | for (uint32_t i = 0; i != SymtabIdx; ++i) { |
| 97 | SI.increment(ec); |
| 98 | } |
| 99 | SI->getName(SymName); |
| 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); |
| 103 | } |
| 104 | |
Ahmed Bougacha | cdef37a | 2013-08-21 07:28:07 +0000 | [diff] [blame] | 105 | void MCMachObjectSymbolizer:: |
| 106 | tryAddingPcLoadReferenceComment(raw_ostream &cStream, int64_t Value, |
| 107 | uint64_t Address) { |
| 108 | if (const RelocationRef *R = findRelocationAt(Address)) { |
| 109 | const MCExpr *RelExpr = RelInfo->createExprForRelocation(*R); |
| 110 | if (!RelExpr || RelExpr->EvaluateAsAbsolute(Value) == false) |
| 111 | return; |
| 112 | } |
| 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") { |
| 118 | StringRef Contents; |
| 119 | S->getContents(Contents); |
| 120 | Contents = Contents.substr(Addr - SAddr); |
| 121 | cStream << " ## literal pool for: " |
| 122 | << Contents.substr(0, Contents.find_first_of(0)); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 127 | //===- MCObjectSymbolizer -------------------------------------------------===// |
| 128 | |
| 129 | MCObjectSymbolizer::MCObjectSymbolizer(MCContext &Ctx, |
| 130 | OwningPtr<MCRelocationInfo> &RelInfo, |
| 131 | const ObjectFile *Obj) |
Ahmed Bougacha | b54d297 | 2013-05-30 18:18:36 +0000 | [diff] [blame] | 132 | : MCSymbolizer(Ctx, RelInfo), Obj(Obj), SortedSections(), AddrToReloc() { |
Ahmed Bougacha | cdef37a | 2013-08-21 07:28:07 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 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) { |
Ahmed Bougacha | 9bfc062 | 2013-08-21 07:28:13 +0000 | [diff] [blame^] | 139 | if (IsBranch) { |
| 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)); |
| 145 | return true; |
| 146 | } |
| 147 | } |
| 148 | |
Ahmed Bougacha | cdef37a | 2013-08-21 07:28:07 +0000 | [diff] [blame] | 149 | if (const RelocationRef *R = findRelocationAt(Address + Offset)) { |
| 150 | if (const MCExpr *RelExpr = RelInfo->createExprForRelocation(*R)) { |
| 151 | MI.addOperand(MCOperand::CreateExpr(RelExpr)); |
| 152 | return true; |
| 153 | } |
| 154 | // Only try to create a symbol+offset expression if there is no relocation. |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | // Interpret Value as a branch target. |
| 159 | if (IsBranch == false) |
| 160 | return false; |
| 161 | uint64_t UValue = Value; |
| 162 | // FIXME: map instead of looping each time? |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 163 | error_code ec; |
Ahmed Bougacha | cdef37a | 2013-08-21 07:28:07 +0000 | [diff] [blame] | 164 | for (symbol_iterator SI = Obj->begin_symbols(), SE = Obj->end_symbols(); |
| 165 | SI != SE; SI.increment(ec)) { |
| 166 | if (ec) break; |
| 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) |
| 173 | continue; |
| 174 | |
| 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); |
| 182 | } |
| 183 | MI.addOperand(MCOperand::CreateExpr(Expr)); |
| 184 | return true; |
| 185 | } |
| 186 | } |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | void MCObjectSymbolizer:: |
| 191 | tryAddingPcLoadReferenceComment(raw_ostream &cStream, |
| 192 | int64_t Value, uint64_t Address) { |
| 193 | } |
| 194 | |
| 195 | StringRef MCObjectSymbolizer::findExternalFunctionAt(uint64_t Addr) { |
| 196 | return StringRef(); |
| 197 | } |
| 198 | |
| 199 | MCObjectSymbolizer * |
| 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); |
| 206 | } |
| 207 | |
| 208 | // SortedSections implementation. |
| 209 | |
| 210 | static bool SectionStartsBefore(const SectionRef &S, uint64_t Addr) { |
| 211 | uint64_t SAddr; S.getAddress(SAddr); |
| 212 | return SAddr < Addr; |
| 213 | } |
| 214 | |
| 215 | const SectionRef *MCObjectSymbolizer::findSectionContaining(uint64_t Addr) { |
| 216 | if (SortedSections.empty()) |
| 217 | buildSectionList(); |
| 218 | |
| 219 | SortedSectionList::iterator |
| 220 | EndIt = SortedSections.end(), |
| 221 | It = std::lower_bound(SortedSections.begin(), EndIt, |
| 222 | Addr, SectionStartsBefore); |
| 223 | if (It == EndIt) |
| 224 | return 0; |
| 225 | uint64_t SAddr; It->getAddress(SAddr); |
| 226 | uint64_t SSize; It->getSize(SSize); |
| 227 | if (Addr >= SAddr + SSize) |
| 228 | return 0; |
| 229 | return &*It; |
| 230 | } |
| 231 | |
| 232 | const RelocationRef *MCObjectSymbolizer::findRelocationAt(uint64_t Addr) { |
| 233 | if (AddrToReloc.empty()) |
| 234 | buildRelocationByAddrMap(); |
| 235 | |
| 236 | AddrToRelocMap::const_iterator RI = AddrToReloc.find(Addr); |
| 237 | if (RI == AddrToReloc.end()) |
| 238 | return 0; |
| 239 | return &RI->second; |
| 240 | } |
| 241 | |
| 242 | void MCObjectSymbolizer::buildSectionList() { |
| 243 | error_code ec; |
| 244 | for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections(); |
| 245 | SI != SE; SI.increment(ec)) { |
| 246 | if (ec) break; |
| 247 | |
| 248 | bool RequiredForExec; SI->isRequiredForExecution(RequiredForExec); |
| 249 | if (RequiredForExec == false) |
| 250 | continue; |
| 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(), |
| 255 | SAddr, |
| 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"); |
| 261 | } |
| 262 | SortedSections.insert(It, *SI); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | void MCObjectSymbolizer::buildRelocationByAddrMap() { |
| 267 | error_code ec; |
| 268 | for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections(); |
| 269 | SI != SE; SI.increment(ec)) { |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 270 | if (ec) break; |
Rafael Espindola | 7486d92 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 271 | |
| 272 | section_iterator RelSecI = SI->getRelocatedSection(); |
| 273 | if (RelSecI == Obj->end_sections()) |
| 274 | continue; |
| 275 | |
| 276 | uint64_t StartAddr; RelSecI->getAddress(StartAddr); |
| 277 | uint64_t Size; RelSecI->getSize(Size); |
| 278 | bool RequiredForExec; RelSecI->isRequiredForExecution(RequiredForExec); |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 279 | if (RequiredForExec == false || Size == 0) |
| 280 | continue; |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 281 | for (relocation_iterator RI = SI->begin_relocations(), |
| 282 | RE = SI->end_relocations(); |
| 283 | RI != RE; |
| 284 | RI.increment(ec)) { |
| 285 | if (ec) break; |
| 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. |
| 291 | uint64_t Offset; |
| 292 | if (Obj->isELF()) { |
| 293 | const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj); |
| 294 | if (ELFObj == 0) |
| 295 | break; |
Michael J. Spencer | 081a194 | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 296 | if (ELFObj->getELFFile()->getHeader()->e_type == ELF::ET_REL) { |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 297 | RI->getOffset(Offset); |
| 298 | Offset += StartAddr; |
| 299 | } else { |
| 300 | RI->getAddress(Offset); |
| 301 | } |
| 302 | } else { |
| 303 | RI->getOffset(Offset); |
| 304 | Offset += StartAddr; |
| 305 | } |
| 306 | // At a specific address, only keep the first relocation. |
| 307 | if (AddrToReloc.find(Offset) == AddrToReloc.end()) |
| 308 | AddrToReloc[Offset] = *RI; |
| 309 | } |
| 310 | } |
| 311 | } |