LLVM API Documentation
00001 //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares the ELFObjectFile template class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_OBJECT_ELFOBJECTFILE_H 00015 #define LLVM_OBJECT_ELFOBJECTFILE_H 00016 00017 #include "llvm/ADT/DenseMap.h" 00018 #include "llvm/ADT/PointerIntPair.h" 00019 #include "llvm/ADT/SmallVector.h" 00020 #include "llvm/ADT/StringSwitch.h" 00021 #include "llvm/ADT/Triple.h" 00022 #include "llvm/Object/ELF.h" 00023 #include "llvm/Object/ObjectFile.h" 00024 #include "llvm/Support/Casting.h" 00025 #include "llvm/Support/ELF.h" 00026 #include "llvm/Support/Endian.h" 00027 #include "llvm/Support/ErrorHandling.h" 00028 #include "llvm/Support/MemoryBuffer.h" 00029 #include "llvm/Support/raw_ostream.h" 00030 #include <algorithm> 00031 #include <cctype> 00032 #include <limits> 00033 #include <utility> 00034 00035 namespace llvm { 00036 namespace object { 00037 00038 class ELFObjectFileBase : public ObjectFile { 00039 protected: 00040 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source); 00041 00042 public: 00043 virtual std::error_code getRelocationAddend(DataRefImpl Rel, 00044 int64_t &Res) const = 0; 00045 virtual std::pair<symbol_iterator, symbol_iterator> 00046 getELFDynamicSymbolIterators() const = 0; 00047 00048 virtual std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version, 00049 bool &IsDefault) const = 0; 00050 }; 00051 00052 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase { 00053 public: 00054 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 00055 00056 typedef typename ELFFile<ELFT>::uintX_t uintX_t; 00057 00058 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym; 00059 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr; 00060 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr; 00061 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel; 00062 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela; 00063 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn; 00064 00065 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter; 00066 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter; 00067 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter; 00068 00069 protected: 00070 ELFFile<ELFT> EF; 00071 00072 void moveSymbolNext(DataRefImpl &Symb) const override; 00073 std::error_code getSymbolName(DataRefImpl Symb, 00074 StringRef &Res) const override; 00075 std::error_code getSymbolAddress(DataRefImpl Symb, 00076 uint64_t &Res) const override; 00077 std::error_code getSymbolAlignment(DataRefImpl Symb, 00078 uint32_t &Res) const override; 00079 std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override; 00080 uint32_t getSymbolFlags(DataRefImpl Symb) const override; 00081 std::error_code getSymbolOther(DataRefImpl Symb, uint8_t &Res) const override; 00082 std::error_code getSymbolType(DataRefImpl Symb, 00083 SymbolRef::Type &Res) const override; 00084 std::error_code getSymbolSection(DataRefImpl Symb, 00085 section_iterator &Res) const override; 00086 00087 void moveSectionNext(DataRefImpl &Sec) const override; 00088 std::error_code getSectionName(DataRefImpl Sec, 00089 StringRef &Res) const override; 00090 std::error_code getSectionAddress(DataRefImpl Sec, 00091 uint64_t &Res) const override; 00092 std::error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override; 00093 std::error_code getSectionContents(DataRefImpl Sec, 00094 StringRef &Res) const override; 00095 std::error_code getSectionAlignment(DataRefImpl Sec, 00096 uint64_t &Res) const override; 00097 std::error_code isSectionText(DataRefImpl Sec, bool &Res) const override; 00098 std::error_code isSectionData(DataRefImpl Sec, bool &Res) const override; 00099 std::error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override; 00100 std::error_code isSectionRequiredForExecution(DataRefImpl Sec, 00101 bool &Res) const override; 00102 std::error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override; 00103 std::error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override; 00104 std::error_code isSectionReadOnlyData(DataRefImpl Sec, 00105 bool &Res) const override; 00106 std::error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, 00107 bool &Result) const override; 00108 relocation_iterator section_rel_begin(DataRefImpl Sec) const override; 00109 relocation_iterator section_rel_end(DataRefImpl Sec) const override; 00110 section_iterator getRelocatedSection(DataRefImpl Sec) const override; 00111 00112 void moveRelocationNext(DataRefImpl &Rel) const override; 00113 std::error_code getRelocationAddress(DataRefImpl Rel, 00114 uint64_t &Res) const override; 00115 std::error_code getRelocationOffset(DataRefImpl Rel, 00116 uint64_t &Res) const override; 00117 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override; 00118 std::error_code getRelocationType(DataRefImpl Rel, 00119 uint64_t &Res) const override; 00120 std::error_code 00121 getRelocationTypeName(DataRefImpl Rel, 00122 SmallVectorImpl<char> &Result) const override; 00123 std::error_code 00124 getRelocationValueString(DataRefImpl Rel, 00125 SmallVectorImpl<char> &Result) const override; 00126 00127 uint64_t getROffset(DataRefImpl Rel) const; 00128 StringRef getRelocationTypeName(uint32_t Type) const; 00129 00130 /// \brief Get the relocation section that contains \a Rel. 00131 const Elf_Shdr *getRelSection(DataRefImpl Rel) const { 00132 return EF.getSection(Rel.d.a); 00133 } 00134 00135 const Elf_Rel *getRel(DataRefImpl Rel) const; 00136 const Elf_Rela *getRela(DataRefImpl Rela) const; 00137 00138 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const { 00139 bool IsDynamic = Symb.p & 1; 00140 if (IsDynamic) 00141 return Elf_Sym_Iter( 00142 EF.begin_dynamic_symbols().getEntSize(), 00143 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic); 00144 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(), 00145 reinterpret_cast<const char *>(Symb.p), IsDynamic); 00146 } 00147 00148 DataRefImpl toDRI(Elf_Sym_Iter Symb) const { 00149 DataRefImpl DRI; 00150 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) | 00151 static_cast<uintptr_t>(Symb.isDynamic()); 00152 return DRI; 00153 } 00154 00155 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const { 00156 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize, 00157 reinterpret_cast<const char *>(Sec.p)); 00158 } 00159 00160 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const { 00161 DataRefImpl DRI; 00162 DRI.p = reinterpret_cast<uintptr_t>(Sec.get()); 00163 return DRI; 00164 } 00165 00166 DataRefImpl toDRI(const Elf_Shdr *Sec) const { 00167 DataRefImpl DRI; 00168 DRI.p = reinterpret_cast<uintptr_t>(Sec); 00169 return DRI; 00170 } 00171 00172 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const { 00173 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(), 00174 reinterpret_cast<const char *>(Dyn.p)); 00175 } 00176 00177 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const { 00178 DataRefImpl DRI; 00179 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get()); 00180 return DRI; 00181 } 00182 00183 // This flag is used for classof, to distinguish ELFObjectFile from 00184 // its subclass. If more subclasses will be created, this flag will 00185 // have to become an enum. 00186 bool isDyldELFObject; 00187 00188 public: 00189 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC); 00190 00191 const Elf_Sym *getSymbol(DataRefImpl Symb) const; 00192 00193 basic_symbol_iterator symbol_begin_impl() const override; 00194 basic_symbol_iterator symbol_end_impl() const override; 00195 00196 symbol_iterator dynamic_symbol_begin() const; 00197 symbol_iterator dynamic_symbol_end() const; 00198 00199 section_iterator section_begin() const override; 00200 section_iterator section_end() const override; 00201 00202 std::error_code getRelocationAddend(DataRefImpl Rel, 00203 int64_t &Res) const override; 00204 std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version, 00205 bool &IsDefault) const override; 00206 00207 uint8_t getBytesInAddress() const override; 00208 StringRef getFileFormatName() const override; 00209 unsigned getArch() const override; 00210 StringRef getLoadName() const; 00211 00212 std::error_code getPlatformFlags(unsigned &Result) const override { 00213 Result = EF.getHeader()->e_flags; 00214 return object_error::success; 00215 } 00216 00217 const ELFFile<ELFT> *getELFFile() const { return &EF; } 00218 00219 bool isDyldType() const { return isDyldELFObject; } 00220 static inline bool classof(const Binary *v) { 00221 return v->getType() == getELFType(ELFT::TargetEndianness == support::little, 00222 ELFT::Is64Bits); 00223 } 00224 00225 std::pair<symbol_iterator, symbol_iterator> 00226 getELFDynamicSymbolIterators() const override; 00227 00228 bool isRelocatableObject() const override; 00229 }; 00230 00231 // Use an alignment of 2 for the typedefs since that is the worst case for 00232 // ELF files in archives. 00233 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile; 00234 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile; 00235 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile; 00236 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile; 00237 00238 template <class ELFT> 00239 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Symb) const { 00240 Symb = toDRI(++toELFSymIter(Symb)); 00241 } 00242 00243 template <class ELFT> 00244 std::error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb, 00245 StringRef &Result) const { 00246 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb)); 00247 if (!Name) 00248 return Name.getError(); 00249 Result = *Name; 00250 return object_error::success; 00251 } 00252 00253 template <class ELFT> 00254 std::error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef, 00255 StringRef &Version, 00256 bool &IsDefault) const { 00257 DataRefImpl Symb = SymRef.getRawDataRefImpl(); 00258 const Elf_Sym *symb = getSymbol(Symb); 00259 ErrorOr<StringRef> Ver = 00260 EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault); 00261 if (!Ver) 00262 return Ver.getError(); 00263 Version = *Ver; 00264 return object_error::success; 00265 } 00266 00267 template <class ELFT> 00268 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb, 00269 uint64_t &Result) const { 00270 const Elf_Sym *ESym = getSymbol(Symb); 00271 switch (EF.getSymbolTableIndex(ESym)) { 00272 case ELF::SHN_COMMON: 00273 case ELF::SHN_UNDEF: 00274 Result = UnknownAddressOrSize; 00275 return object_error::success; 00276 case ELF::SHN_ABS: 00277 Result = ESym->st_value; 00278 return object_error::success; 00279 default: 00280 break; 00281 } 00282 00283 const Elf_Ehdr *Header = EF.getHeader(); 00284 Result = ESym->st_value; 00285 00286 // Clear the ARM/Thumb indicator flag. 00287 if (Header->e_machine == ELF::EM_ARM && ESym->getType() == ELF::STT_FUNC) 00288 Result &= ~1; 00289 00290 if (Header->e_type == ELF::ET_REL) 00291 Result += EF.getSection(ESym)->sh_addr; 00292 00293 return object_error::success; 00294 } 00295 00296 template <class ELFT> 00297 std::error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb, 00298 uint32_t &Res) const { 00299 Elf_Sym_Iter Sym = toELFSymIter(Symb); 00300 if (Sym->st_shndx == ELF::SHN_COMMON) 00301 Res = Sym->st_value; 00302 else 00303 Res = 0; 00304 return object_error::success; 00305 } 00306 00307 template <class ELFT> 00308 std::error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb, 00309 uint64_t &Result) const { 00310 Result = toELFSymIter(Symb)->st_size; 00311 return object_error::success; 00312 } 00313 00314 template <class ELFT> 00315 std::error_code ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb, 00316 uint8_t &Result) const { 00317 Result = toELFSymIter(Symb)->st_other; 00318 return object_error::success; 00319 } 00320 00321 template <class ELFT> 00322 std::error_code 00323 ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb, 00324 SymbolRef::Type &Result) const { 00325 const Elf_Sym *ESym = getSymbol(Symb); 00326 00327 switch (ESym->getType()) { 00328 case ELF::STT_NOTYPE: 00329 Result = SymbolRef::ST_Unknown; 00330 break; 00331 case ELF::STT_SECTION: 00332 Result = SymbolRef::ST_Debug; 00333 break; 00334 case ELF::STT_FILE: 00335 Result = SymbolRef::ST_File; 00336 break; 00337 case ELF::STT_FUNC: 00338 Result = SymbolRef::ST_Function; 00339 break; 00340 case ELF::STT_OBJECT: 00341 case ELF::STT_COMMON: 00342 case ELF::STT_TLS: 00343 Result = SymbolRef::ST_Data; 00344 break; 00345 default: 00346 Result = SymbolRef::ST_Other; 00347 break; 00348 } 00349 return object_error::success; 00350 } 00351 00352 template <class ELFT> 00353 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb) const { 00354 Elf_Sym_Iter EIter = toELFSymIter(Symb); 00355 const Elf_Sym *ESym = &*EIter; 00356 00357 uint32_t Result = SymbolRef::SF_None; 00358 00359 if (ESym->getBinding() != ELF::STB_LOCAL) 00360 Result |= SymbolRef::SF_Global; 00361 00362 if (ESym->getBinding() == ELF::STB_WEAK) 00363 Result |= SymbolRef::SF_Weak; 00364 00365 if (ESym->st_shndx == ELF::SHN_ABS) 00366 Result |= SymbolRef::SF_Absolute; 00367 00368 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION || 00369 EIter == EF.begin_symbols() || EIter == EF.begin_dynamic_symbols()) 00370 Result |= SymbolRef::SF_FormatSpecific; 00371 00372 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF) 00373 Result |= SymbolRef::SF_Undefined; 00374 00375 if (ESym->getType() == ELF::STT_COMMON || 00376 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON) 00377 Result |= SymbolRef::SF_Common; 00378 00379 return Result; 00380 } 00381 00382 template <class ELFT> 00383 std::error_code 00384 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb, 00385 section_iterator &Res) const { 00386 const Elf_Sym *ESym = getSymbol(Symb); 00387 const Elf_Shdr *ESec = EF.getSection(ESym); 00388 if (!ESec) 00389 Res = section_end(); 00390 else { 00391 DataRefImpl Sec; 00392 Sec.p = reinterpret_cast<intptr_t>(ESec); 00393 Res = section_iterator(SectionRef(Sec, this)); 00394 } 00395 return object_error::success; 00396 } 00397 00398 template <class ELFT> 00399 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const { 00400 Sec = toDRI(++toELFShdrIter(Sec)); 00401 } 00402 00403 template <class ELFT> 00404 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec, 00405 StringRef &Result) const { 00406 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec)); 00407 if (!Name) 00408 return Name.getError(); 00409 Result = *Name; 00410 return object_error::success; 00411 } 00412 00413 template <class ELFT> 00414 std::error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec, 00415 uint64_t &Result) const { 00416 Result = toELFShdrIter(Sec)->sh_addr; 00417 return object_error::success; 00418 } 00419 00420 template <class ELFT> 00421 std::error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec, 00422 uint64_t &Result) const { 00423 Result = toELFShdrIter(Sec)->sh_size; 00424 return object_error::success; 00425 } 00426 00427 template <class ELFT> 00428 std::error_code 00429 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec, 00430 StringRef &Result) const { 00431 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec); 00432 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size); 00433 return object_error::success; 00434 } 00435 00436 template <class ELFT> 00437 std::error_code 00438 ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec, 00439 uint64_t &Result) const { 00440 Result = toELFShdrIter(Sec)->sh_addralign; 00441 return object_error::success; 00442 } 00443 00444 template <class ELFT> 00445 std::error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec, 00446 bool &Result) const { 00447 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR; 00448 return object_error::success; 00449 } 00450 00451 template <class ELFT> 00452 std::error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec, 00453 bool &Result) const { 00454 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec); 00455 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) && 00456 EShdr->sh_type == ELF::SHT_PROGBITS; 00457 return object_error::success; 00458 } 00459 00460 template <class ELFT> 00461 std::error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec, 00462 bool &Result) const { 00463 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec); 00464 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) && 00465 EShdr->sh_type == ELF::SHT_NOBITS; 00466 return object_error::success; 00467 } 00468 00469 template <class ELFT> 00470 std::error_code 00471 ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec, 00472 bool &Result) const { 00473 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC; 00474 return object_error::success; 00475 } 00476 00477 template <class ELFT> 00478 std::error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec, 00479 bool &Result) const { 00480 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS; 00481 return object_error::success; 00482 } 00483 00484 template <class ELFT> 00485 std::error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec, 00486 bool &Result) const { 00487 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS; 00488 return object_error::success; 00489 } 00490 00491 template <class ELFT> 00492 std::error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec, 00493 bool &Result) const { 00494 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec); 00495 Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR)); 00496 return object_error::success; 00497 } 00498 00499 template <class ELFT> 00500 std::error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec, 00501 DataRefImpl Symb, 00502 bool &Result) const { 00503 Elf_Sym_Iter ESym = toELFSymIter(Symb); 00504 00505 uintX_t Index = ESym->st_shndx; 00506 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE; 00507 00508 Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx)); 00509 return object_error::success; 00510 } 00511 00512 template <class ELFT> 00513 relocation_iterator 00514 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const { 00515 DataRefImpl RelData; 00516 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get()); 00517 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize; 00518 RelData.d.b = 0; 00519 return relocation_iterator(RelocationRef(RelData, this)); 00520 } 00521 00522 template <class ELFT> 00523 relocation_iterator 00524 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const { 00525 DataRefImpl RelData; 00526 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get()); 00527 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p); 00528 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize; 00529 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL) 00530 RelData.d.b = 0; 00531 else 00532 RelData.d.b = S->sh_size / S->sh_entsize; 00533 00534 return relocation_iterator(RelocationRef(RelData, this)); 00535 } 00536 00537 template <class ELFT> 00538 section_iterator 00539 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const { 00540 if (EF.getHeader()->e_type != ELF::ET_REL) 00541 return section_end(); 00542 00543 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec); 00544 uintX_t Type = EShdr->sh_type; 00545 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA) 00546 return section_end(); 00547 00548 const Elf_Shdr *R = EF.getSection(EShdr->sh_info); 00549 return section_iterator(SectionRef(toDRI(R), this)); 00550 } 00551 00552 // Relocations 00553 template <class ELFT> 00554 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const { 00555 ++Rel.d.b; 00556 } 00557 00558 template <class ELFT> 00559 symbol_iterator 00560 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const { 00561 uint32_t symbolIdx; 00562 const Elf_Shdr *sec = getRelSection(Rel); 00563 switch (sec->sh_type) { 00564 default: 00565 report_fatal_error("Invalid section type in Rel!"); 00566 case ELF::SHT_REL: { 00567 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL()); 00568 break; 00569 } 00570 case ELF::SHT_RELA: { 00571 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL()); 00572 break; 00573 } 00574 } 00575 if (!symbolIdx) 00576 return symbol_end(); 00577 00578 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link); 00579 00580 DataRefImpl SymbolData; 00581 switch (SymSec->sh_type) { 00582 default: 00583 report_fatal_error("Invalid symbol table section type!"); 00584 case ELF::SHT_SYMTAB: 00585 SymbolData = toDRI(EF.begin_symbols() + symbolIdx); 00586 break; 00587 case ELF::SHT_DYNSYM: 00588 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx); 00589 break; 00590 } 00591 00592 return symbol_iterator(SymbolRef(SymbolData, this)); 00593 } 00594 00595 template <class ELFT> 00596 std::error_code 00597 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel, 00598 uint64_t &Result) const { 00599 uint64_t ROffset = getROffset(Rel); 00600 const Elf_Ehdr *Header = EF.getHeader(); 00601 00602 if (Header->e_type == ELF::ET_REL) { 00603 const Elf_Shdr *RelocationSec = getRelSection(Rel); 00604 const Elf_Shdr *RelocatedSec = EF.getSection(RelocationSec->sh_info); 00605 Result = ROffset + RelocatedSec->sh_addr; 00606 } else { 00607 Result = ROffset; 00608 } 00609 00610 return object_error::success; 00611 } 00612 00613 template <class ELFT> 00614 std::error_code 00615 ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel, 00616 uint64_t &Result) const { 00617 assert(EF.getHeader()->e_type == ELF::ET_REL && 00618 "Only relocatable object files have relocation offsets"); 00619 Result = getROffset(Rel); 00620 return object_error::success; 00621 } 00622 00623 template <class ELFT> 00624 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const { 00625 const Elf_Shdr *sec = getRelSection(Rel); 00626 switch (sec->sh_type) { 00627 default: 00628 report_fatal_error("Invalid section type in Rel!"); 00629 case ELF::SHT_REL: 00630 return getRel(Rel)->r_offset; 00631 case ELF::SHT_RELA: 00632 return getRela(Rel)->r_offset; 00633 } 00634 } 00635 00636 template <class ELFT> 00637 std::error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel, 00638 uint64_t &Result) const { 00639 const Elf_Shdr *sec = getRelSection(Rel); 00640 switch (sec->sh_type) { 00641 default: 00642 report_fatal_error("Invalid section type in Rel!"); 00643 case ELF::SHT_REL: { 00644 Result = getRel(Rel)->getType(EF.isMips64EL()); 00645 break; 00646 } 00647 case ELF::SHT_RELA: { 00648 Result = getRela(Rel)->getType(EF.isMips64EL()); 00649 break; 00650 } 00651 } 00652 return object_error::success; 00653 } 00654 00655 template <class ELFT> 00656 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const { 00657 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type); 00658 } 00659 00660 template <class ELFT> 00661 std::error_code ELFObjectFile<ELFT>::getRelocationTypeName( 00662 DataRefImpl Rel, SmallVectorImpl<char> &Result) const { 00663 const Elf_Shdr *sec = getRelSection(Rel); 00664 uint32_t type; 00665 switch (sec->sh_type) { 00666 default: 00667 return object_error::parse_failed; 00668 case ELF::SHT_REL: { 00669 type = getRel(Rel)->getType(EF.isMips64EL()); 00670 break; 00671 } 00672 case ELF::SHT_RELA: { 00673 type = getRela(Rel)->getType(EF.isMips64EL()); 00674 break; 00675 } 00676 } 00677 00678 EF.getRelocationTypeName(type, Result); 00679 return object_error::success; 00680 } 00681 00682 template <class ELFT> 00683 std::error_code 00684 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel, 00685 int64_t &Result) const { 00686 const Elf_Shdr *sec = getRelSection(Rel); 00687 switch (sec->sh_type) { 00688 default: 00689 report_fatal_error("Invalid section type in Rel!"); 00690 case ELF::SHT_REL: { 00691 Result = 0; 00692 return object_error::success; 00693 } 00694 case ELF::SHT_RELA: { 00695 Result = getRela(Rel)->r_addend; 00696 return object_error::success; 00697 } 00698 } 00699 } 00700 00701 template <class ELFT> 00702 std::error_code ELFObjectFile<ELFT>::getRelocationValueString( 00703 DataRefImpl Rel, SmallVectorImpl<char> &Result) const { 00704 const Elf_Shdr *sec = getRelSection(Rel); 00705 uint8_t type; 00706 StringRef res; 00707 int64_t addend = 0; 00708 uint16_t symbol_index = 0; 00709 switch (sec->sh_type) { 00710 default: 00711 return object_error::parse_failed; 00712 case ELF::SHT_REL: { 00713 type = getRel(Rel)->getType(EF.isMips64EL()); 00714 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL()); 00715 // TODO: Read implicit addend from section data. 00716 break; 00717 } 00718 case ELF::SHT_RELA: { 00719 type = getRela(Rel)->getType(EF.isMips64EL()); 00720 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL()); 00721 addend = getRela(Rel)->r_addend; 00722 break; 00723 } 00724 } 00725 const Elf_Sym *symb = 00726 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index); 00727 ErrorOr<StringRef> SymName = 00728 EF.getSymbolName(EF.getSection(sec->sh_link), symb); 00729 if (!SymName) 00730 return SymName.getError(); 00731 switch (EF.getHeader()->e_machine) { 00732 case ELF::EM_X86_64: 00733 switch (type) { 00734 case ELF::R_X86_64_PC8: 00735 case ELF::R_X86_64_PC16: 00736 case ELF::R_X86_64_PC32: { 00737 std::string fmtbuf; 00738 raw_string_ostream fmt(fmtbuf); 00739 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P"; 00740 fmt.flush(); 00741 Result.append(fmtbuf.begin(), fmtbuf.end()); 00742 } break; 00743 case ELF::R_X86_64_8: 00744 case ELF::R_X86_64_16: 00745 case ELF::R_X86_64_32: 00746 case ELF::R_X86_64_32S: 00747 case ELF::R_X86_64_64: { 00748 std::string fmtbuf; 00749 raw_string_ostream fmt(fmtbuf); 00750 fmt << *SymName << (addend < 0 ? "" : "+") << addend; 00751 fmt.flush(); 00752 Result.append(fmtbuf.begin(), fmtbuf.end()); 00753 } break; 00754 default: 00755 res = "Unknown"; 00756 } 00757 break; 00758 case ELF::EM_AARCH64: { 00759 std::string fmtbuf; 00760 raw_string_ostream fmt(fmtbuf); 00761 fmt << *SymName; 00762 if (addend != 0) 00763 fmt << (addend < 0 ? "" : "+") << addend; 00764 fmt.flush(); 00765 Result.append(fmtbuf.begin(), fmtbuf.end()); 00766 break; 00767 } 00768 case ELF::EM_ARM: 00769 case ELF::EM_HEXAGON: 00770 case ELF::EM_MIPS: 00771 res = *SymName; 00772 break; 00773 default: 00774 res = "Unknown"; 00775 } 00776 if (Result.empty()) 00777 Result.append(res.begin(), res.end()); 00778 return object_error::success; 00779 } 00780 00781 template <class ELFT> 00782 const typename ELFFile<ELFT>::Elf_Sym * 00783 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const { 00784 return &*toELFSymIter(Symb); 00785 } 00786 00787 template <class ELFT> 00788 const typename ELFObjectFile<ELFT>::Elf_Rel * 00789 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const { 00790 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b); 00791 } 00792 00793 template <class ELFT> 00794 const typename ELFObjectFile<ELFT>::Elf_Rela * 00795 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const { 00796 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b); 00797 } 00798 00799 template <class ELFT> 00800 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC) 00801 : ELFObjectFileBase( 00802 getELFType(static_cast<endianness>(ELFT::TargetEndianness) == 00803 support::little, 00804 ELFT::Is64Bits), 00805 Object), 00806 EF(Data.getBuffer(), EC) {} 00807 00808 template <class ELFT> 00809 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const { 00810 return basic_symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this)); 00811 } 00812 00813 template <class ELFT> 00814 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const { 00815 return basic_symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this)); 00816 } 00817 00818 template <class ELFT> 00819 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const { 00820 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this)); 00821 } 00822 00823 template <class ELFT> 00824 symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const { 00825 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this)); 00826 } 00827 00828 template <class ELFT> 00829 section_iterator ELFObjectFile<ELFT>::section_begin() const { 00830 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this)); 00831 } 00832 00833 template <class ELFT> 00834 section_iterator ELFObjectFile<ELFT>::section_end() const { 00835 return section_iterator(SectionRef(toDRI(EF.end_sections()), this)); 00836 } 00837 00838 template <class ELFT> 00839 StringRef ELFObjectFile<ELFT>::getLoadName() const { 00840 Elf_Dyn_Iter DI = EF.begin_dynamic_table(); 00841 Elf_Dyn_Iter DE = EF.end_dynamic_table(); 00842 00843 while (DI != DE && DI->getTag() != ELF::DT_SONAME) 00844 ++DI; 00845 00846 if (DI != DE) 00847 return EF.getDynamicString(DI->getVal()); 00848 return ""; 00849 } 00850 00851 template <class ELFT> 00852 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const { 00853 return ELFT::Is64Bits ? 8 : 4; 00854 } 00855 00856 template <class ELFT> 00857 StringRef ELFObjectFile<ELFT>::getFileFormatName() const { 00858 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) { 00859 case ELF::ELFCLASS32: 00860 switch (EF.getHeader()->e_machine) { 00861 case ELF::EM_386: 00862 return "ELF32-i386"; 00863 case ELF::EM_X86_64: 00864 return "ELF32-x86-64"; 00865 case ELF::EM_ARM: 00866 return "ELF32-arm"; 00867 case ELF::EM_HEXAGON: 00868 return "ELF32-hexagon"; 00869 case ELF::EM_MIPS: 00870 return "ELF32-mips"; 00871 case ELF::EM_PPC: 00872 return "ELF32-ppc"; 00873 case ELF::EM_SPARC: 00874 case ELF::EM_SPARC32PLUS: 00875 return "ELF32-sparc"; 00876 default: 00877 return "ELF32-unknown"; 00878 } 00879 case ELF::ELFCLASS64: 00880 switch (EF.getHeader()->e_machine) { 00881 case ELF::EM_386: 00882 return "ELF64-i386"; 00883 case ELF::EM_X86_64: 00884 return "ELF64-x86-64"; 00885 case ELF::EM_AARCH64: 00886 return "ELF64-aarch64"; 00887 case ELF::EM_PPC64: 00888 return "ELF64-ppc64"; 00889 case ELF::EM_S390: 00890 return "ELF64-s390"; 00891 case ELF::EM_SPARCV9: 00892 return "ELF64-sparc"; 00893 case ELF::EM_MIPS: 00894 return "ELF64-mips"; 00895 default: 00896 return "ELF64-unknown"; 00897 } 00898 default: 00899 // FIXME: Proper error handling. 00900 report_fatal_error("Invalid ELFCLASS!"); 00901 } 00902 } 00903 00904 template <class ELFT> 00905 unsigned ELFObjectFile<ELFT>::getArch() const { 00906 bool IsLittleEndian = ELFT::TargetEndianness == support::little; 00907 switch (EF.getHeader()->e_machine) { 00908 case ELF::EM_386: 00909 return Triple::x86; 00910 case ELF::EM_X86_64: 00911 return Triple::x86_64; 00912 case ELF::EM_AARCH64: 00913 return Triple::aarch64; 00914 case ELF::EM_ARM: 00915 return Triple::arm; 00916 case ELF::EM_HEXAGON: 00917 return Triple::hexagon; 00918 case ELF::EM_MIPS: 00919 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) { 00920 case ELF::ELFCLASS32: 00921 return IsLittleEndian ? Triple::mipsel : Triple::mips; 00922 case ELF::ELFCLASS64: 00923 return IsLittleEndian ? Triple::mips64el : Triple::mips64; 00924 default: 00925 report_fatal_error("Invalid ELFCLASS!"); 00926 } 00927 case ELF::EM_PPC64: 00928 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64; 00929 case ELF::EM_S390: 00930 return Triple::systemz; 00931 00932 case ELF::EM_SPARC: 00933 case ELF::EM_SPARC32PLUS: 00934 return Triple::sparc; 00935 case ELF::EM_SPARCV9: 00936 return Triple::sparcv9; 00937 00938 default: 00939 return Triple::UnknownArch; 00940 } 00941 } 00942 00943 template <class ELFT> 00944 std::pair<symbol_iterator, symbol_iterator> 00945 ELFObjectFile<ELFT>::getELFDynamicSymbolIterators() const { 00946 return std::make_pair(dynamic_symbol_begin(), dynamic_symbol_end()); 00947 } 00948 00949 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const { 00950 return EF.getHeader()->e_type == ELF::ET_REL; 00951 } 00952 00953 inline std::error_code getELFRelocationAddend(const RelocationRef R, 00954 int64_t &Addend) { 00955 const ObjectFile *Obj = R.getObjectFile(); 00956 DataRefImpl DRI = R.getRawDataRefImpl(); 00957 return cast<ELFObjectFileBase>(Obj)->getRelocationAddend(DRI, Addend); 00958 } 00959 00960 inline std::pair<symbol_iterator, symbol_iterator> 00961 getELFDynamicSymbolIterators(SymbolicFile *Obj) { 00962 return cast<ELFObjectFileBase>(Obj)->getELFDynamicSymbolIterators(); 00963 } 00964 00965 inline std::error_code GetELFSymbolVersion(const ObjectFile *Obj, 00966 const SymbolRef &Sym, 00967 StringRef &Version, 00968 bool &IsDefault) { 00969 return cast<ELFObjectFileBase>(Obj) 00970 ->getSymbolVersion(Sym, Version, IsDefault); 00971 } 00972 } 00973 } 00974 00975 #endif