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" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace object; |
| 23 | |
| 24 | //===- MCMachObjectSymbolizer ---------------------------------------------===// |
| 25 | |
| 26 | namespace { |
| 27 | class MCMachObjectSymbolizer : public MCObjectSymbolizer { |
| 28 | public: |
| 29 | MCMachObjectSymbolizer(MCContext &Ctx, OwningPtr<MCRelocationInfo> &RelInfo, |
| 30 | const object::MachOObjectFile *MachOOF) |
| 31 | : MCObjectSymbolizer(Ctx, RelInfo, MachOOF) |
| 32 | {} |
| 33 | |
| 34 | void tryAddingPcLoadReferenceComment(raw_ostream &cStream, |
| 35 | int64_t Value, uint64_t Address) { |
| 36 | AddrToRelocMap::iterator RI = AddrToReloc.find(Address); |
| 37 | if (RI != AddrToReloc.end()) { |
| 38 | const MCExpr *RelExpr = RelInfo->createExprForRelocation(RI->second); |
| 39 | if (!RelExpr || RelExpr->EvaluateAsAbsolute(Value) == false) |
| 40 | return; |
| 41 | } |
| 42 | uint64_t Addr = Value; |
| 43 | AddrToSectionMap::const_iterator SI = AddrToSection.find(Addr); |
| 44 | if (SI.valid()) { |
| 45 | DataRefImpl DRI; DRI.p = *SI; |
| 46 | SectionRef S(DRI, Obj); |
| 47 | StringRef Name; S.getName(Name); |
| 48 | if (Name == "__cstring") { |
| 49 | StringRef Contents; |
| 50 | S.getContents(Contents); |
| 51 | Contents = Contents.substr(Addr - SI.start()); |
| 52 | cStream << " ## literal pool for: " |
| 53 | << Contents.substr(0, Contents.find_first_of(0)); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | }; |
| 58 | } // End unnamed namespace |
| 59 | |
| 60 | //===- MCObjectSymbolizer -------------------------------------------------===// |
| 61 | |
| 62 | MCObjectSymbolizer::MCObjectSymbolizer(MCContext &Ctx, |
| 63 | OwningPtr<MCRelocationInfo> &RelInfo, |
| 64 | const ObjectFile *Obj) |
| 65 | : MCSymbolizer(Ctx, RelInfo), Obj(Obj), |
| 66 | AddrToSectionAllocator(), AddrToSection(AddrToSectionAllocator), |
| 67 | AddrToReloc() { |
| 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; |
| 74 | uint64_t StartAddr; SI->getAddress(StartAddr); |
| 75 | uint64_t Size; SI->getSize(Size); |
| 76 | StringRef SecName; SI->getName(SecName); |
| 77 | bool RequiredForExec; SI->isRequiredForExecution(RequiredForExec); |
| 78 | if (RequiredForExec == false || Size == 0) |
| 79 | continue; |
| 80 | AddrToSection.insert(StartAddr, StartAddr + Size - 1, |
| 81 | SI->getRawDataRefImpl().p); |
| 82 | for (relocation_iterator RI = SI->begin_relocations(), |
| 83 | RE = SI->end_relocations(); |
| 84 | RI != RE; |
| 85 | RI.increment(ec)) { |
| 86 | if (ec) break; |
| 87 | // FIXME: libObject is inconsistent regarding error handling. The |
| 88 | // overwhelming majority of methods always return object_error::success, |
| 89 | // and assert for simple errors.. Here, ELFObjectFile::getRelocationOffset |
| 90 | // asserts when the file type isn't ET_REL. |
| 91 | // This workaround handles x86-64 elf, the only one that has a relocinfo. |
| 92 | uint64_t Offset; |
| 93 | if (Obj->isELF()) { |
| 94 | const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj); |
| 95 | if (ELFObj == 0) |
| 96 | break; |
| 97 | if (ELFObj->getElfHeader()->e_type == ELF::ET_REL) { |
| 98 | RI->getOffset(Offset); |
| 99 | Offset += StartAddr; |
| 100 | } else { |
| 101 | RI->getAddress(Offset); |
| 102 | } |
| 103 | } else { |
| 104 | RI->getOffset(Offset); |
| 105 | Offset += StartAddr; |
| 106 | } |
| 107 | // At a specific address, only keep the first relocation. |
| 108 | if (AddrToReloc.find(Offset) == AddrToReloc.end()) |
| 109 | AddrToReloc[Offset] = *RI; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | bool MCObjectSymbolizer:: |
| 115 | tryAddingSymbolicOperand(MCInst &MI, raw_ostream &cStream, |
| 116 | int64_t Value, uint64_t Address, bool IsBranch, |
| 117 | uint64_t Offset, uint64_t InstSize) { |
| 118 | AddrToRelocMap::iterator RI = AddrToReloc.find(Address + Offset); |
| 119 | if (RI != AddrToReloc.end()) { |
| 120 | if (const MCExpr *RelExpr = RelInfo->createExprForRelocation(RI->second)) { |
| 121 | MI.addOperand(MCOperand::CreateExpr(RelExpr)); |
| 122 | return true; |
| 123 | } |
| 124 | // Only try to create a symbol+offset expression if there is no relocation. |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | // Interpret Value as a branch target. |
| 129 | if (IsBranch == false) |
| 130 | return false; |
| 131 | uint64_t UValue = Value; |
| 132 | // FIXME: map instead of looping each time? |
| 133 | error_code ec; |
| 134 | for (symbol_iterator SI = Obj->begin_symbols(), |
| 135 | SE = Obj->end_symbols(); |
| 136 | SI != SE; |
| 137 | SI.increment(ec)) { |
| 138 | if (ec) break; |
| 139 | uint64_t SymAddr; SI->getAddress(SymAddr); |
| 140 | uint64_t SymSize; SI->getSize(SymSize); |
| 141 | StringRef SymName; SI->getName(SymName); |
| 142 | SymbolRef::Type SymType; SI->getType(SymType); |
| 143 | if (SymAddr == UnknownAddressOrSize || SymSize == UnknownAddressOrSize |
| 144 | || SymName.empty() || SymType != SymbolRef::ST_Function) |
| 145 | continue; |
| 146 | |
| 147 | if ( SymAddr == UValue || |
| 148 | (SymAddr <= UValue && SymAddr + SymSize > UValue)) { |
| 149 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(SymName); |
| 150 | const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, Ctx); |
| 151 | if (SymAddr != UValue) { |
| 152 | const MCExpr *Off = MCConstantExpr::Create(UValue - SymAddr, Ctx); |
| 153 | Expr = MCBinaryExpr::CreateAdd(Expr, Off, Ctx); |
| 154 | } |
| 155 | MI.addOperand(MCOperand::CreateExpr(Expr)); |
| 156 | return true; |
| 157 | } |
| 158 | } |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | void MCObjectSymbolizer:: |
| 163 | tryAddingPcLoadReferenceComment(raw_ostream &cStream, |
| 164 | int64_t Value, uint64_t Address) { |
| 165 | } |
| 166 | |
| 167 | MCObjectSymbolizer * |
| 168 | MCObjectSymbolizer::createObjectSymbolizer(MCContext &Ctx, |
| 169 | OwningPtr<MCRelocationInfo> &RelInfo, |
| 170 | const ObjectFile *Obj) { |
| 171 | if (const MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(Obj)) { |
| 172 | return new MCMachObjectSymbolizer(Ctx, RelInfo, MachOOF); |
| 173 | } |
| 174 | return new MCObjectSymbolizer(Ctx, RelInfo, Obj); |
| 175 | } |