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" |
| 18 | #include "llvm/Object/ELF.h" |
| 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 { |
| 29 | public: |
| 30 | MCMachObjectSymbolizer(MCContext &Ctx, OwningPtr<MCRelocationInfo> &RelInfo, |
| 31 | const object::MachOObjectFile *MachOOF) |
| 32 | : MCObjectSymbolizer(Ctx, RelInfo, MachOOF) |
| 33 | {} |
| 34 | |
| 35 | void tryAddingPcLoadReferenceComment(raw_ostream &cStream, |
| 36 | int64_t Value, uint64_t Address) { |
| 37 | AddrToRelocMap::iterator RI = AddrToReloc.find(Address); |
| 38 | if (RI != AddrToReloc.end()) { |
| 39 | const MCExpr *RelExpr = RelInfo->createExprForRelocation(RI->second); |
| 40 | if (!RelExpr || RelExpr->EvaluateAsAbsolute(Value) == false) |
| 41 | return; |
| 42 | } |
| 43 | uint64_t Addr = Value; |
Ahmed Bougacha | b54d297 | 2013-05-30 18:18:36 +0000 | [diff] [blame^] | 44 | SortedSectionList::const_iterator SI = findSectionContaining(Addr); |
| 45 | errs() << " looking for sec " << Addr << "\n"; |
| 46 | if (SI != SortedSections.end()) { |
| 47 | const SectionRef &S = *SI; |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 48 | StringRef Name; S.getName(Name); |
Ahmed Bougacha | b54d297 | 2013-05-30 18:18:36 +0000 | [diff] [blame^] | 49 | uint64_t SAddr; S.getAddress(SAddr); |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 50 | if (Name == "__cstring") { |
| 51 | StringRef Contents; |
| 52 | S.getContents(Contents); |
Ahmed Bougacha | b54d297 | 2013-05-30 18:18:36 +0000 | [diff] [blame^] | 53 | Contents = Contents.substr(Addr - SAddr); |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 54 | cStream << " ## literal pool for: " |
| 55 | << Contents.substr(0, Contents.find_first_of(0)); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | }; |
| 60 | } // End unnamed namespace |
| 61 | |
| 62 | //===- MCObjectSymbolizer -------------------------------------------------===// |
| 63 | |
| 64 | MCObjectSymbolizer::MCObjectSymbolizer(MCContext &Ctx, |
| 65 | OwningPtr<MCRelocationInfo> &RelInfo, |
| 66 | const ObjectFile *Obj) |
Ahmed Bougacha | b54d297 | 2013-05-30 18:18:36 +0000 | [diff] [blame^] | 67 | : MCSymbolizer(Ctx, RelInfo), Obj(Obj), SortedSections(), AddrToReloc() { |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 68 | error_code ec; |
| 69 | for (section_iterator SI = Obj->begin_sections(), |
| 70 | SE = Obj->end_sections(); |
| 71 | SI != SE; |
| 72 | SI.increment(ec)) { |
| 73 | if (ec) break; |
Rafael Espindola | 7486d92 | 2013-05-30 03:05:14 +0000 | [diff] [blame] | 74 | |
| 75 | section_iterator RelSecI = SI->getRelocatedSection(); |
| 76 | if (RelSecI == Obj->end_sections()) |
| 77 | continue; |
| 78 | |
| 79 | uint64_t StartAddr; RelSecI->getAddress(StartAddr); |
| 80 | uint64_t Size; RelSecI->getSize(Size); |
| 81 | bool RequiredForExec; RelSecI->isRequiredForExecution(RequiredForExec); |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 82 | if (RequiredForExec == false || Size == 0) |
| 83 | continue; |
Ahmed Bougacha | b54d297 | 2013-05-30 18:18:36 +0000 | [diff] [blame^] | 84 | insertSection(*SI); |
Ahmed Bougacha | 2c94d0f | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 85 | for (relocation_iterator RI = SI->begin_relocations(), |
| 86 | RE = SI->end_relocations(); |
| 87 | RI != RE; |
| 88 | RI.increment(ec)) { |
| 89 | if (ec) break; |
| 90 | // FIXME: libObject is inconsistent regarding error handling. The |
| 91 | // overwhelming majority of methods always return object_error::success, |
| 92 | // and assert for simple errors.. Here, ELFObjectFile::getRelocationOffset |
| 93 | // asserts when the file type isn't ET_REL. |
| 94 | // This workaround handles x86-64 elf, the only one that has a relocinfo. |
| 95 | uint64_t Offset; |
| 96 | if (Obj->isELF()) { |
| 97 | const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj); |
| 98 | if (ELFObj == 0) |
| 99 | break; |
| 100 | if (ELFObj->getElfHeader()->e_type == ELF::ET_REL) { |
| 101 | RI->getOffset(Offset); |
| 102 | Offset += StartAddr; |
| 103 | } else { |
| 104 | RI->getAddress(Offset); |
| 105 | } |
| 106 | } else { |
| 107 | RI->getOffset(Offset); |
| 108 | Offset += StartAddr; |
| 109 | } |
| 110 | // At a specific address, only keep the first relocation. |
| 111 | if (AddrToReloc.find(Offset) == AddrToReloc.end()) |
| 112 | AddrToReloc[Offset] = *RI; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | bool MCObjectSymbolizer:: |
| 118 | tryAddingSymbolicOperand(MCInst &MI, raw_ostream &cStream, |
| 119 | int64_t Value, uint64_t Address, bool IsBranch, |
| 120 | uint64_t Offset, uint64_t InstSize) { |
| 121 | AddrToRelocMap::iterator RI = AddrToReloc.find(Address + Offset); |
| 122 | if (RI != AddrToReloc.end()) { |
| 123 | if (const MCExpr *RelExpr = RelInfo->createExprForRelocation(RI->second)) { |
| 124 | MI.addOperand(MCOperand::CreateExpr(RelExpr)); |
| 125 | return true; |
| 126 | } |
| 127 | // Only try to create a symbol+offset expression if there is no relocation. |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | // Interpret Value as a branch target. |
| 132 | if (IsBranch == false) |
| 133 | return false; |
| 134 | uint64_t UValue = Value; |
| 135 | // FIXME: map instead of looping each time? |
| 136 | error_code ec; |
| 137 | for (symbol_iterator SI = Obj->begin_symbols(), |
| 138 | SE = Obj->end_symbols(); |
| 139 | SI != SE; |
| 140 | SI.increment(ec)) { |
| 141 | if (ec) break; |
| 142 | uint64_t SymAddr; SI->getAddress(SymAddr); |
| 143 | uint64_t SymSize; SI->getSize(SymSize); |
| 144 | StringRef SymName; SI->getName(SymName); |
| 145 | SymbolRef::Type SymType; SI->getType(SymType); |
| 146 | if (SymAddr == UnknownAddressOrSize || SymSize == UnknownAddressOrSize |
| 147 | || SymName.empty() || SymType != SymbolRef::ST_Function) |
| 148 | continue; |
| 149 | |
| 150 | if ( SymAddr == UValue || |
| 151 | (SymAddr <= UValue && SymAddr + SymSize > UValue)) { |
| 152 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(SymName); |
| 153 | const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx); |
| 154 | if (SymAddr != UValue) { |
| 155 | const MCExpr *Off = MCConstantExpr::Create(UValue - SymAddr, Ctx); |
| 156 | Expr = MCBinaryExpr::CreateAdd(Expr, Off, Ctx); |
| 157 | } |
| 158 | MI.addOperand(MCOperand::CreateExpr(Expr)); |
| 159 | return true; |
| 160 | } |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | void MCObjectSymbolizer:: |
| 166 | tryAddingPcLoadReferenceComment(raw_ostream &cStream, |
| 167 | int64_t Value, uint64_t Address) { |
| 168 | } |
| 169 | |
| 170 | MCObjectSymbolizer * |
| 171 | MCObjectSymbolizer::createObjectSymbolizer(MCContext &Ctx, |
| 172 | OwningPtr<MCRelocationInfo> &RelInfo, |
| 173 | const ObjectFile *Obj) { |
| 174 | if (const MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(Obj)) { |
| 175 | return new MCMachObjectSymbolizer(Ctx, RelInfo, MachOOF); |
| 176 | } |
| 177 | return new MCObjectSymbolizer(Ctx, RelInfo, Obj); |
| 178 | } |
Ahmed Bougacha | b54d297 | 2013-05-30 18:18:36 +0000 | [diff] [blame^] | 179 | |
| 180 | // SortedSections implementation. |
| 181 | |
| 182 | static bool SectionStartsBefore(const SectionRef &S, uint64_t Addr) { |
| 183 | uint64_t SAddr; S.getAddress(SAddr); |
| 184 | return SAddr < Addr; |
| 185 | } |
| 186 | |
| 187 | MCObjectSymbolizer::SortedSectionList::const_iterator |
| 188 | MCObjectSymbolizer::findSectionContaining(uint64_t Addr) const { |
| 189 | SortedSectionList::const_iterator |
| 190 | EndIt = SortedSections.end(), |
| 191 | It = std::lower_bound(SortedSections.begin(), EndIt, |
| 192 | Addr, SectionStartsBefore); |
| 193 | if (It == EndIt) |
| 194 | return It; |
| 195 | uint64_t SAddr; It->getAddress(SAddr); |
| 196 | uint64_t SSize; It->getSize(SSize); |
| 197 | if (Addr >= SAddr + SSize) |
| 198 | return EndIt; |
| 199 | return It; |
| 200 | } |
| 201 | |
| 202 | void MCObjectSymbolizer::insertSection(SectionRef Sec) { |
| 203 | uint64_t SAddr; Sec.getAddress(SAddr); |
| 204 | uint64_t SSize; Sec.getSize(SSize); |
| 205 | SortedSectionList::iterator It = std::lower_bound(SortedSections.begin(), |
| 206 | SortedSections.end(), |
| 207 | SAddr, |
| 208 | SectionStartsBefore); |
| 209 | if (It != SortedSections.end()) { |
| 210 | uint64_t FoundSAddr; It->getAddress(FoundSAddr); |
| 211 | if (FoundSAddr < SAddr + SSize) |
| 212 | llvm_unreachable("Inserting overlapping sections"); |
| 213 | } |
| 214 | SortedSections.insert(It, Sec); |
| 215 | } |