Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===// |
| 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 | // This file declares the ELFObjectFile template class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_OBJECT_ELFOBJECTFILE_H |
| 15 | #define LLVM_OBJECT_ELFOBJECTFILE_H |
| 16 | |
| 17 | #include "llvm/ADT/ArrayRef.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/ADT/Triple.h" |
| 22 | #include "llvm/ADT/iterator_range.h" |
| 23 | #include "llvm/BinaryFormat/ELF.h" |
| 24 | #include "llvm/MC/SubtargetFeature.h" |
| 25 | #include "llvm/Object/Binary.h" |
| 26 | #include "llvm/Object/ELF.h" |
| 27 | #include "llvm/Object/ELFTypes.h" |
| 28 | #include "llvm/Object/Error.h" |
| 29 | #include "llvm/Object/ObjectFile.h" |
| 30 | #include "llvm/Object/SymbolicFile.h" |
| 31 | #include "llvm/Support/ARMAttributeParser.h" |
| 32 | #include "llvm/Support/ARMBuildAttributes.h" |
| 33 | #include "llvm/Support/Casting.h" |
| 34 | #include "llvm/Support/Endian.h" |
| 35 | #include "llvm/Support/Error.h" |
| 36 | #include "llvm/Support/ErrorHandling.h" |
| 37 | #include "llvm/Support/MemoryBuffer.h" |
| 38 | #include <cassert> |
| 39 | #include <cstdint> |
| 40 | #include <system_error> |
| 41 | |
| 42 | namespace llvm { |
| 43 | namespace object { |
| 44 | |
| 45 | class elf_symbol_iterator; |
| 46 | |
| 47 | class ELFObjectFileBase : public ObjectFile { |
| 48 | friend class ELFRelocationRef; |
| 49 | friend class ELFSectionRef; |
| 50 | friend class ELFSymbolRef; |
| 51 | |
| 52 | protected: |
| 53 | ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source); |
| 54 | |
| 55 | virtual uint16_t getEMachine() const = 0; |
| 56 | virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0; |
| 57 | virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0; |
| 58 | virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0; |
| 59 | |
| 60 | virtual uint32_t getSectionType(DataRefImpl Sec) const = 0; |
| 61 | virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0; |
| 62 | virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0; |
| 63 | |
| 64 | virtual Expected<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0; |
| 65 | |
| 66 | public: |
| 67 | using elf_symbol_iterator_range = iterator_range<elf_symbol_iterator>; |
| 68 | |
| 69 | virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0; |
| 70 | |
| 71 | /// Returns platform-specific object flags, if any. |
| 72 | virtual unsigned getPlatformFlags() const = 0; |
| 73 | |
| 74 | elf_symbol_iterator_range symbols() const; |
| 75 | |
| 76 | static bool classof(const Binary *v) { return v->isELF(); } |
| 77 | |
| 78 | SubtargetFeatures getFeatures() const override; |
| 79 | |
| 80 | SubtargetFeatures getMIPSFeatures() const; |
| 81 | |
| 82 | SubtargetFeatures getARMFeatures() const; |
| 83 | |
| 84 | SubtargetFeatures getRISCVFeatures() const; |
| 85 | |
| 86 | void setARMSubArch(Triple &TheTriple) const override; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 87 | |
| 88 | virtual uint16_t getEType() const = 0; |
| 89 | |
| 90 | std::vector<std::pair<DataRefImpl, uint64_t>> getPltAddresses() const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | class ELFSectionRef : public SectionRef { |
| 94 | public: |
| 95 | ELFSectionRef(const SectionRef &B) : SectionRef(B) { |
| 96 | assert(isa<ELFObjectFileBase>(SectionRef::getObject())); |
| 97 | } |
| 98 | |
| 99 | const ELFObjectFileBase *getObject() const { |
| 100 | return cast<ELFObjectFileBase>(SectionRef::getObject()); |
| 101 | } |
| 102 | |
| 103 | uint32_t getType() const { |
| 104 | return getObject()->getSectionType(getRawDataRefImpl()); |
| 105 | } |
| 106 | |
| 107 | uint64_t getFlags() const { |
| 108 | return getObject()->getSectionFlags(getRawDataRefImpl()); |
| 109 | } |
| 110 | |
| 111 | uint64_t getOffset() const { |
| 112 | return getObject()->getSectionOffset(getRawDataRefImpl()); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | class elf_section_iterator : public section_iterator { |
| 117 | public: |
| 118 | elf_section_iterator(const section_iterator &B) : section_iterator(B) { |
| 119 | assert(isa<ELFObjectFileBase>(B->getObject())); |
| 120 | } |
| 121 | |
| 122 | const ELFSectionRef *operator->() const { |
| 123 | return static_cast<const ELFSectionRef *>(section_iterator::operator->()); |
| 124 | } |
| 125 | |
| 126 | const ELFSectionRef &operator*() const { |
| 127 | return static_cast<const ELFSectionRef &>(section_iterator::operator*()); |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | class ELFSymbolRef : public SymbolRef { |
| 132 | public: |
| 133 | ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) { |
| 134 | assert(isa<ELFObjectFileBase>(SymbolRef::getObject())); |
| 135 | } |
| 136 | |
| 137 | const ELFObjectFileBase *getObject() const { |
| 138 | return cast<ELFObjectFileBase>(BasicSymbolRef::getObject()); |
| 139 | } |
| 140 | |
| 141 | uint64_t getSize() const { |
| 142 | return getObject()->getSymbolSize(getRawDataRefImpl()); |
| 143 | } |
| 144 | |
| 145 | uint8_t getOther() const { |
| 146 | return getObject()->getSymbolOther(getRawDataRefImpl()); |
| 147 | } |
| 148 | |
| 149 | uint8_t getELFType() const { |
| 150 | return getObject()->getSymbolELFType(getRawDataRefImpl()); |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | class elf_symbol_iterator : public symbol_iterator { |
| 155 | public: |
| 156 | elf_symbol_iterator(const basic_symbol_iterator &B) |
| 157 | : symbol_iterator(SymbolRef(B->getRawDataRefImpl(), |
| 158 | cast<ELFObjectFileBase>(B->getObject()))) {} |
| 159 | |
| 160 | const ELFSymbolRef *operator->() const { |
| 161 | return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->()); |
| 162 | } |
| 163 | |
| 164 | const ELFSymbolRef &operator*() const { |
| 165 | return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*()); |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | class ELFRelocationRef : public RelocationRef { |
| 170 | public: |
| 171 | ELFRelocationRef(const RelocationRef &B) : RelocationRef(B) { |
| 172 | assert(isa<ELFObjectFileBase>(RelocationRef::getObject())); |
| 173 | } |
| 174 | |
| 175 | const ELFObjectFileBase *getObject() const { |
| 176 | return cast<ELFObjectFileBase>(RelocationRef::getObject()); |
| 177 | } |
| 178 | |
| 179 | Expected<int64_t> getAddend() const { |
| 180 | return getObject()->getRelocationAddend(getRawDataRefImpl()); |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | class elf_relocation_iterator : public relocation_iterator { |
| 185 | public: |
| 186 | elf_relocation_iterator(const relocation_iterator &B) |
| 187 | : relocation_iterator(RelocationRef( |
| 188 | B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {} |
| 189 | |
| 190 | const ELFRelocationRef *operator->() const { |
| 191 | return static_cast<const ELFRelocationRef *>( |
| 192 | relocation_iterator::operator->()); |
| 193 | } |
| 194 | |
| 195 | const ELFRelocationRef &operator*() const { |
| 196 | return static_cast<const ELFRelocationRef &>( |
| 197 | relocation_iterator::operator*()); |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | inline ELFObjectFileBase::elf_symbol_iterator_range |
| 202 | ELFObjectFileBase::symbols() const { |
| 203 | return elf_symbol_iterator_range(symbol_begin(), symbol_end()); |
| 204 | } |
| 205 | |
| 206 | template <class ELFT> class ELFObjectFile : public ELFObjectFileBase { |
| 207 | uint16_t getEMachine() const override; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 208 | uint16_t getEType() const override; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 209 | uint64_t getSymbolSize(DataRefImpl Sym) const override; |
| 210 | |
| 211 | public: |
| 212 | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
| 213 | |
| 214 | using uintX_t = typename ELFT::uint; |
| 215 | |
| 216 | using Elf_Sym = typename ELFT::Sym; |
| 217 | using Elf_Shdr = typename ELFT::Shdr; |
| 218 | using Elf_Ehdr = typename ELFT::Ehdr; |
| 219 | using Elf_Rel = typename ELFT::Rel; |
| 220 | using Elf_Rela = typename ELFT::Rela; |
| 221 | using Elf_Dyn = typename ELFT::Dyn; |
| 222 | |
| 223 | private: |
| 224 | ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF, |
| 225 | const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec, |
| 226 | ArrayRef<Elf_Word> ShndxTable); |
| 227 | |
| 228 | protected: |
| 229 | ELFFile<ELFT> EF; |
| 230 | |
| 231 | const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section. |
| 232 | const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section. |
| 233 | ArrayRef<Elf_Word> ShndxTable; |
| 234 | |
| 235 | void moveSymbolNext(DataRefImpl &Symb) const override; |
| 236 | Expected<StringRef> getSymbolName(DataRefImpl Symb) const override; |
| 237 | Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override; |
| 238 | uint64_t getSymbolValueImpl(DataRefImpl Symb) const override; |
| 239 | uint32_t getSymbolAlignment(DataRefImpl Symb) const override; |
| 240 | uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override; |
| 241 | uint32_t getSymbolFlags(DataRefImpl Symb) const override; |
| 242 | uint8_t getSymbolOther(DataRefImpl Symb) const override; |
| 243 | uint8_t getSymbolELFType(DataRefImpl Symb) const override; |
| 244 | Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override; |
| 245 | Expected<section_iterator> getSymbolSection(const Elf_Sym *Symb, |
| 246 | const Elf_Shdr *SymTab) const; |
| 247 | Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override; |
| 248 | |
| 249 | void moveSectionNext(DataRefImpl &Sec) const override; |
| 250 | std::error_code getSectionName(DataRefImpl Sec, |
| 251 | StringRef &Res) const override; |
| 252 | uint64_t getSectionAddress(DataRefImpl Sec) const override; |
| 253 | uint64_t getSectionIndex(DataRefImpl Sec) const override; |
| 254 | uint64_t getSectionSize(DataRefImpl Sec) const override; |
| 255 | std::error_code getSectionContents(DataRefImpl Sec, |
| 256 | StringRef &Res) const override; |
| 257 | uint64_t getSectionAlignment(DataRefImpl Sec) const override; |
| 258 | bool isSectionCompressed(DataRefImpl Sec) const override; |
| 259 | bool isSectionText(DataRefImpl Sec) const override; |
| 260 | bool isSectionData(DataRefImpl Sec) const override; |
| 261 | bool isSectionBSS(DataRefImpl Sec) const override; |
| 262 | bool isSectionVirtual(DataRefImpl Sec) const override; |
| 263 | relocation_iterator section_rel_begin(DataRefImpl Sec) const override; |
| 264 | relocation_iterator section_rel_end(DataRefImpl Sec) const override; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 265 | std::vector<SectionRef> dynamic_relocation_sections() const override; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 266 | section_iterator getRelocatedSection(DataRefImpl Sec) const override; |
| 267 | |
| 268 | void moveRelocationNext(DataRefImpl &Rel) const override; |
| 269 | uint64_t getRelocationOffset(DataRefImpl Rel) const override; |
| 270 | symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override; |
| 271 | uint64_t getRelocationType(DataRefImpl Rel) const override; |
| 272 | void getRelocationTypeName(DataRefImpl Rel, |
| 273 | SmallVectorImpl<char> &Result) const override; |
| 274 | |
| 275 | uint32_t getSectionType(DataRefImpl Sec) const override; |
| 276 | uint64_t getSectionFlags(DataRefImpl Sec) const override; |
| 277 | uint64_t getSectionOffset(DataRefImpl Sec) const override; |
| 278 | StringRef getRelocationTypeName(uint32_t Type) const; |
| 279 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 280 | /// Get the relocation section that contains \a Rel. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 281 | const Elf_Shdr *getRelSection(DataRefImpl Rel) const { |
| 282 | auto RelSecOrErr = EF.getSection(Rel.d.a); |
| 283 | if (!RelSecOrErr) |
| 284 | report_fatal_error(errorToErrorCode(RelSecOrErr.takeError()).message()); |
| 285 | return *RelSecOrErr; |
| 286 | } |
| 287 | |
| 288 | DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const { |
| 289 | DataRefImpl DRI; |
| 290 | if (!SymTable) { |
| 291 | DRI.d.a = 0; |
| 292 | DRI.d.b = 0; |
| 293 | return DRI; |
| 294 | } |
| 295 | assert(SymTable->sh_type == ELF::SHT_SYMTAB || |
| 296 | SymTable->sh_type == ELF::SHT_DYNSYM); |
| 297 | |
| 298 | auto SectionsOrErr = EF.sections(); |
| 299 | if (!SectionsOrErr) { |
| 300 | DRI.d.a = 0; |
| 301 | DRI.d.b = 0; |
| 302 | return DRI; |
| 303 | } |
| 304 | uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin()); |
| 305 | unsigned SymTableIndex = |
| 306 | (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr); |
| 307 | |
| 308 | DRI.d.a = SymTableIndex; |
| 309 | DRI.d.b = SymbolNum; |
| 310 | return DRI; |
| 311 | } |
| 312 | |
| 313 | const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const { |
| 314 | return reinterpret_cast<const Elf_Shdr *>(Sec.p); |
| 315 | } |
| 316 | |
| 317 | DataRefImpl toDRI(const Elf_Shdr *Sec) const { |
| 318 | DataRefImpl DRI; |
| 319 | DRI.p = reinterpret_cast<uintptr_t>(Sec); |
| 320 | return DRI; |
| 321 | } |
| 322 | |
| 323 | DataRefImpl toDRI(const Elf_Dyn *Dyn) const { |
| 324 | DataRefImpl DRI; |
| 325 | DRI.p = reinterpret_cast<uintptr_t>(Dyn); |
| 326 | return DRI; |
| 327 | } |
| 328 | |
| 329 | bool isExportedToOtherDSO(const Elf_Sym *ESym) const { |
| 330 | unsigned char Binding = ESym->getBinding(); |
| 331 | unsigned char Visibility = ESym->getVisibility(); |
| 332 | |
| 333 | // A symbol is exported if its binding is either GLOBAL or WEAK, and its |
| 334 | // visibility is either DEFAULT or PROTECTED. All other symbols are not |
| 335 | // exported. |
| 336 | return ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) && |
| 337 | (Visibility == ELF::STV_DEFAULT || |
| 338 | Visibility == ELF::STV_PROTECTED)); |
| 339 | } |
| 340 | |
| 341 | // This flag is used for classof, to distinguish ELFObjectFile from |
| 342 | // its subclass. If more subclasses will be created, this flag will |
| 343 | // have to become an enum. |
| 344 | bool isDyldELFObject; |
| 345 | |
| 346 | public: |
| 347 | ELFObjectFile(ELFObjectFile<ELFT> &&Other); |
| 348 | static Expected<ELFObjectFile<ELFT>> create(MemoryBufferRef Object); |
| 349 | |
| 350 | const Elf_Rel *getRel(DataRefImpl Rel) const; |
| 351 | const Elf_Rela *getRela(DataRefImpl Rela) const; |
| 352 | |
| 353 | const Elf_Sym *getSymbol(DataRefImpl Sym) const { |
| 354 | auto Ret = EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b); |
| 355 | if (!Ret) |
| 356 | report_fatal_error(errorToErrorCode(Ret.takeError()).message()); |
| 357 | return *Ret; |
| 358 | } |
| 359 | |
| 360 | const Elf_Shdr *getSection(DataRefImpl Sec) const { |
| 361 | return reinterpret_cast<const Elf_Shdr *>(Sec.p); |
| 362 | } |
| 363 | |
| 364 | basic_symbol_iterator symbol_begin() const override; |
| 365 | basic_symbol_iterator symbol_end() const override; |
| 366 | |
| 367 | elf_symbol_iterator dynamic_symbol_begin() const; |
| 368 | elf_symbol_iterator dynamic_symbol_end() const; |
| 369 | |
| 370 | section_iterator section_begin() const override; |
| 371 | section_iterator section_end() const override; |
| 372 | |
| 373 | Expected<int64_t> getRelocationAddend(DataRefImpl Rel) const override; |
| 374 | |
| 375 | uint8_t getBytesInAddress() const override; |
| 376 | StringRef getFileFormatName() const override; |
| 377 | Triple::ArchType getArch() const override; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 378 | Expected<uint64_t> getStartAddress() const override; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 379 | |
| 380 | unsigned getPlatformFlags() const override { return EF.getHeader()->e_flags; } |
| 381 | |
| 382 | std::error_code getBuildAttributes(ARMAttributeParser &Attributes) const override { |
| 383 | auto SectionsOrErr = EF.sections(); |
| 384 | if (!SectionsOrErr) |
| 385 | return errorToErrorCode(SectionsOrErr.takeError()); |
| 386 | |
| 387 | for (const Elf_Shdr &Sec : *SectionsOrErr) { |
| 388 | if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES) { |
| 389 | auto ErrorOrContents = EF.getSectionContents(&Sec); |
| 390 | if (!ErrorOrContents) |
| 391 | return errorToErrorCode(ErrorOrContents.takeError()); |
| 392 | |
| 393 | auto Contents = ErrorOrContents.get(); |
| 394 | if (Contents[0] != ARMBuildAttrs::Format_Version || Contents.size() == 1) |
| 395 | return std::error_code(); |
| 396 | |
| 397 | Attributes.Parse(Contents, ELFT::TargetEndianness == support::little); |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | return std::error_code(); |
| 402 | } |
| 403 | |
| 404 | const ELFFile<ELFT> *getELFFile() const { return &EF; } |
| 405 | |
| 406 | bool isDyldType() const { return isDyldELFObject; } |
| 407 | static bool classof(const Binary *v) { |
| 408 | return v->getType() == getELFType(ELFT::TargetEndianness == support::little, |
| 409 | ELFT::Is64Bits); |
| 410 | } |
| 411 | |
| 412 | elf_symbol_iterator_range getDynamicSymbolIterators() const override; |
| 413 | |
| 414 | bool isRelocatableObject() const override; |
| 415 | }; |
| 416 | |
| 417 | using ELF32LEObjectFile = ELFObjectFile<ELF32LE>; |
| 418 | using ELF64LEObjectFile = ELFObjectFile<ELF64LE>; |
| 419 | using ELF32BEObjectFile = ELFObjectFile<ELF32BE>; |
| 420 | using ELF64BEObjectFile = ELFObjectFile<ELF64BE>; |
| 421 | |
| 422 | template <class ELFT> |
| 423 | void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const { |
| 424 | ++Sym.d.b; |
| 425 | } |
| 426 | |
| 427 | template <class ELFT> |
| 428 | Expected<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const { |
| 429 | const Elf_Sym *ESym = getSymbol(Sym); |
| 430 | auto SymTabOrErr = EF.getSection(Sym.d.a); |
| 431 | if (!SymTabOrErr) |
| 432 | return SymTabOrErr.takeError(); |
| 433 | const Elf_Shdr *SymTableSec = *SymTabOrErr; |
| 434 | auto StrTabOrErr = EF.getSection(SymTableSec->sh_link); |
| 435 | if (!StrTabOrErr) |
| 436 | return StrTabOrErr.takeError(); |
| 437 | const Elf_Shdr *StringTableSec = *StrTabOrErr; |
| 438 | auto SymStrTabOrErr = EF.getStringTable(StringTableSec); |
| 439 | if (!SymStrTabOrErr) |
| 440 | return SymStrTabOrErr.takeError(); |
| 441 | return ESym->getName(*SymStrTabOrErr); |
| 442 | } |
| 443 | |
| 444 | template <class ELFT> |
| 445 | uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const { |
| 446 | return getSection(Sec)->sh_flags; |
| 447 | } |
| 448 | |
| 449 | template <class ELFT> |
| 450 | uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const { |
| 451 | return getSection(Sec)->sh_type; |
| 452 | } |
| 453 | |
| 454 | template <class ELFT> |
| 455 | uint64_t ELFObjectFile<ELFT>::getSectionOffset(DataRefImpl Sec) const { |
| 456 | return getSection(Sec)->sh_offset; |
| 457 | } |
| 458 | |
| 459 | template <class ELFT> |
| 460 | uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const { |
| 461 | const Elf_Sym *ESym = getSymbol(Symb); |
| 462 | uint64_t Ret = ESym->st_value; |
| 463 | if (ESym->st_shndx == ELF::SHN_ABS) |
| 464 | return Ret; |
| 465 | |
| 466 | const Elf_Ehdr *Header = EF.getHeader(); |
| 467 | // Clear the ARM/Thumb or microMIPS indicator flag. |
| 468 | if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) && |
| 469 | ESym->getType() == ELF::STT_FUNC) |
| 470 | Ret &= ~1; |
| 471 | |
| 472 | return Ret; |
| 473 | } |
| 474 | |
| 475 | template <class ELFT> |
| 476 | Expected<uint64_t> |
| 477 | ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const { |
| 478 | uint64_t Result = getSymbolValue(Symb); |
| 479 | const Elf_Sym *ESym = getSymbol(Symb); |
| 480 | switch (ESym->st_shndx) { |
| 481 | case ELF::SHN_COMMON: |
| 482 | case ELF::SHN_UNDEF: |
| 483 | case ELF::SHN_ABS: |
| 484 | return Result; |
| 485 | } |
| 486 | |
| 487 | const Elf_Ehdr *Header = EF.getHeader(); |
| 488 | auto SymTabOrErr = EF.getSection(Symb.d.a); |
| 489 | if (!SymTabOrErr) |
| 490 | return SymTabOrErr.takeError(); |
| 491 | const Elf_Shdr *SymTab = *SymTabOrErr; |
| 492 | |
| 493 | if (Header->e_type == ELF::ET_REL) { |
| 494 | auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable); |
| 495 | if (!SectionOrErr) |
| 496 | return SectionOrErr.takeError(); |
| 497 | const Elf_Shdr *Section = *SectionOrErr; |
| 498 | if (Section) |
| 499 | Result += Section->sh_addr; |
| 500 | } |
| 501 | |
| 502 | return Result; |
| 503 | } |
| 504 | |
| 505 | template <class ELFT> |
| 506 | uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const { |
| 507 | const Elf_Sym *Sym = getSymbol(Symb); |
| 508 | if (Sym->st_shndx == ELF::SHN_COMMON) |
| 509 | return Sym->st_value; |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | template <class ELFT> |
| 514 | uint16_t ELFObjectFile<ELFT>::getEMachine() const { |
| 515 | return EF.getHeader()->e_machine; |
| 516 | } |
| 517 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 518 | template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const { |
| 519 | return EF.getHeader()->e_type; |
| 520 | } |
| 521 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 522 | template <class ELFT> |
| 523 | uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const { |
| 524 | return getSymbol(Sym)->st_size; |
| 525 | } |
| 526 | |
| 527 | template <class ELFT> |
| 528 | uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const { |
| 529 | return getSymbol(Symb)->st_size; |
| 530 | } |
| 531 | |
| 532 | template <class ELFT> |
| 533 | uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const { |
| 534 | return getSymbol(Symb)->st_other; |
| 535 | } |
| 536 | |
| 537 | template <class ELFT> |
| 538 | uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const { |
| 539 | return getSymbol(Symb)->getType(); |
| 540 | } |
| 541 | |
| 542 | template <class ELFT> |
| 543 | Expected<SymbolRef::Type> |
| 544 | ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const { |
| 545 | const Elf_Sym *ESym = getSymbol(Symb); |
| 546 | |
| 547 | switch (ESym->getType()) { |
| 548 | case ELF::STT_NOTYPE: |
| 549 | return SymbolRef::ST_Unknown; |
| 550 | case ELF::STT_SECTION: |
| 551 | return SymbolRef::ST_Debug; |
| 552 | case ELF::STT_FILE: |
| 553 | return SymbolRef::ST_File; |
| 554 | case ELF::STT_FUNC: |
| 555 | return SymbolRef::ST_Function; |
| 556 | case ELF::STT_OBJECT: |
| 557 | case ELF::STT_COMMON: |
| 558 | case ELF::STT_TLS: |
| 559 | return SymbolRef::ST_Data; |
| 560 | default: |
| 561 | return SymbolRef::ST_Other; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | template <class ELFT> |
| 566 | uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const { |
| 567 | const Elf_Sym *ESym = getSymbol(Sym); |
| 568 | |
| 569 | uint32_t Result = SymbolRef::SF_None; |
| 570 | |
| 571 | if (ESym->getBinding() != ELF::STB_LOCAL) |
| 572 | Result |= SymbolRef::SF_Global; |
| 573 | |
| 574 | if (ESym->getBinding() == ELF::STB_WEAK) |
| 575 | Result |= SymbolRef::SF_Weak; |
| 576 | |
| 577 | if (ESym->st_shndx == ELF::SHN_ABS) |
| 578 | Result |= SymbolRef::SF_Absolute; |
| 579 | |
| 580 | if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION) |
| 581 | Result |= SymbolRef::SF_FormatSpecific; |
| 582 | |
| 583 | auto DotSymtabSecSyms = EF.symbols(DotSymtabSec); |
| 584 | if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin()) |
| 585 | Result |= SymbolRef::SF_FormatSpecific; |
| 586 | auto DotDynSymSecSyms = EF.symbols(DotDynSymSec); |
| 587 | if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin()) |
| 588 | Result |= SymbolRef::SF_FormatSpecific; |
| 589 | |
| 590 | if (EF.getHeader()->e_machine == ELF::EM_ARM) { |
| 591 | if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) { |
| 592 | StringRef Name = *NameOrErr; |
| 593 | if (Name.startswith("$d") || Name.startswith("$t") || |
| 594 | Name.startswith("$a")) |
| 595 | Result |= SymbolRef::SF_FormatSpecific; |
| 596 | } else { |
| 597 | // TODO: Actually report errors helpfully. |
| 598 | consumeError(NameOrErr.takeError()); |
| 599 | } |
| 600 | if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1) |
| 601 | Result |= SymbolRef::SF_Thumb; |
| 602 | } |
| 603 | |
| 604 | if (ESym->st_shndx == ELF::SHN_UNDEF) |
| 605 | Result |= SymbolRef::SF_Undefined; |
| 606 | |
| 607 | if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON) |
| 608 | Result |= SymbolRef::SF_Common; |
| 609 | |
| 610 | if (isExportedToOtherDSO(ESym)) |
| 611 | Result |= SymbolRef::SF_Exported; |
| 612 | |
| 613 | if (ESym->getVisibility() == ELF::STV_HIDDEN) |
| 614 | Result |= SymbolRef::SF_Hidden; |
| 615 | |
| 616 | return Result; |
| 617 | } |
| 618 | |
| 619 | template <class ELFT> |
| 620 | Expected<section_iterator> |
| 621 | ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym, |
| 622 | const Elf_Shdr *SymTab) const { |
| 623 | auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable); |
| 624 | if (!ESecOrErr) |
| 625 | return ESecOrErr.takeError(); |
| 626 | |
| 627 | const Elf_Shdr *ESec = *ESecOrErr; |
| 628 | if (!ESec) |
| 629 | return section_end(); |
| 630 | |
| 631 | DataRefImpl Sec; |
| 632 | Sec.p = reinterpret_cast<intptr_t>(ESec); |
| 633 | return section_iterator(SectionRef(Sec, this)); |
| 634 | } |
| 635 | |
| 636 | template <class ELFT> |
| 637 | Expected<section_iterator> |
| 638 | ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb) const { |
| 639 | const Elf_Sym *Sym = getSymbol(Symb); |
| 640 | auto SymTabOrErr = EF.getSection(Symb.d.a); |
| 641 | if (!SymTabOrErr) |
| 642 | return SymTabOrErr.takeError(); |
| 643 | const Elf_Shdr *SymTab = *SymTabOrErr; |
| 644 | return getSymbolSection(Sym, SymTab); |
| 645 | } |
| 646 | |
| 647 | template <class ELFT> |
| 648 | void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const { |
| 649 | const Elf_Shdr *ESec = getSection(Sec); |
| 650 | Sec = toDRI(++ESec); |
| 651 | } |
| 652 | |
| 653 | template <class ELFT> |
| 654 | std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec, |
| 655 | StringRef &Result) const { |
| 656 | auto Name = EF.getSectionName(&*getSection(Sec)); |
| 657 | if (!Name) |
| 658 | return errorToErrorCode(Name.takeError()); |
| 659 | Result = *Name; |
| 660 | return std::error_code(); |
| 661 | } |
| 662 | |
| 663 | template <class ELFT> |
| 664 | uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const { |
| 665 | return getSection(Sec)->sh_addr; |
| 666 | } |
| 667 | |
| 668 | template <class ELFT> |
| 669 | uint64_t ELFObjectFile<ELFT>::getSectionIndex(DataRefImpl Sec) const { |
| 670 | auto SectionsOrErr = EF.sections(); |
| 671 | handleAllErrors(std::move(SectionsOrErr.takeError()), |
| 672 | [](const ErrorInfoBase &) { |
| 673 | llvm_unreachable("unable to get section index"); |
| 674 | }); |
| 675 | const Elf_Shdr *First = SectionsOrErr->begin(); |
| 676 | return getSection(Sec) - First; |
| 677 | } |
| 678 | |
| 679 | template <class ELFT> |
| 680 | uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const { |
| 681 | return getSection(Sec)->sh_size; |
| 682 | } |
| 683 | |
| 684 | template <class ELFT> |
| 685 | std::error_code |
| 686 | ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec, |
| 687 | StringRef &Result) const { |
| 688 | const Elf_Shdr *EShdr = getSection(Sec); |
| 689 | if (std::error_code EC = |
| 690 | checkOffset(getMemoryBufferRef(), |
| 691 | (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size)) |
| 692 | return EC; |
| 693 | Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size); |
| 694 | return std::error_code(); |
| 695 | } |
| 696 | |
| 697 | template <class ELFT> |
| 698 | uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const { |
| 699 | return getSection(Sec)->sh_addralign; |
| 700 | } |
| 701 | |
| 702 | template <class ELFT> |
| 703 | bool ELFObjectFile<ELFT>::isSectionCompressed(DataRefImpl Sec) const { |
| 704 | return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED; |
| 705 | } |
| 706 | |
| 707 | template <class ELFT> |
| 708 | bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const { |
| 709 | return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR; |
| 710 | } |
| 711 | |
| 712 | template <class ELFT> |
| 713 | bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const { |
| 714 | const Elf_Shdr *EShdr = getSection(Sec); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 715 | return EShdr->sh_type == ELF::SHT_PROGBITS && |
| 716 | EShdr->sh_flags & ELF::SHF_ALLOC && |
| 717 | !(EShdr->sh_flags & ELF::SHF_EXECINSTR); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | template <class ELFT> |
| 721 | bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const { |
| 722 | const Elf_Shdr *EShdr = getSection(Sec); |
| 723 | return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) && |
| 724 | EShdr->sh_type == ELF::SHT_NOBITS; |
| 725 | } |
| 726 | |
| 727 | template <class ELFT> |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 728 | std::vector<SectionRef> |
| 729 | ELFObjectFile<ELFT>::dynamic_relocation_sections() const { |
| 730 | std::vector<SectionRef> Res; |
| 731 | std::vector<uintptr_t> Offsets; |
| 732 | |
| 733 | auto SectionsOrErr = EF.sections(); |
| 734 | if (!SectionsOrErr) |
| 735 | return Res; |
| 736 | |
| 737 | for (const Elf_Shdr &Sec : *SectionsOrErr) { |
| 738 | if (Sec.sh_type != ELF::SHT_DYNAMIC) |
| 739 | continue; |
| 740 | Elf_Dyn *Dynamic = |
| 741 | reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset); |
| 742 | for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) { |
| 743 | if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA || |
| 744 | Dynamic->d_tag == ELF::DT_JMPREL) { |
| 745 | Offsets.push_back(Dynamic->d_un.d_val); |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | for (const Elf_Shdr &Sec : *SectionsOrErr) { |
| 750 | if (is_contained(Offsets, Sec.sh_offset)) |
| 751 | Res.emplace_back(toDRI(&Sec), this); |
| 752 | } |
| 753 | return Res; |
| 754 | } |
| 755 | |
| 756 | template <class ELFT> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 757 | bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const { |
| 758 | return getSection(Sec)->sh_type == ELF::SHT_NOBITS; |
| 759 | } |
| 760 | |
| 761 | template <class ELFT> |
| 762 | relocation_iterator |
| 763 | ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const { |
| 764 | DataRefImpl RelData; |
| 765 | auto SectionsOrErr = EF.sections(); |
| 766 | if (!SectionsOrErr) |
| 767 | return relocation_iterator(RelocationRef()); |
| 768 | uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin()); |
| 769 | RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize; |
| 770 | RelData.d.b = 0; |
| 771 | return relocation_iterator(RelocationRef(RelData, this)); |
| 772 | } |
| 773 | |
| 774 | template <class ELFT> |
| 775 | relocation_iterator |
| 776 | ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const { |
| 777 | const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p); |
| 778 | relocation_iterator Begin = section_rel_begin(Sec); |
| 779 | if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL) |
| 780 | return Begin; |
| 781 | DataRefImpl RelData = Begin->getRawDataRefImpl(); |
| 782 | const Elf_Shdr *RelSec = getRelSection(RelData); |
| 783 | |
| 784 | // Error check sh_link here so that getRelocationSymbol can just use it. |
| 785 | auto SymSecOrErr = EF.getSection(RelSec->sh_link); |
| 786 | if (!SymSecOrErr) |
| 787 | report_fatal_error(errorToErrorCode(SymSecOrErr.takeError()).message()); |
| 788 | |
| 789 | RelData.d.b += S->sh_size / S->sh_entsize; |
| 790 | return relocation_iterator(RelocationRef(RelData, this)); |
| 791 | } |
| 792 | |
| 793 | template <class ELFT> |
| 794 | section_iterator |
| 795 | ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const { |
| 796 | if (EF.getHeader()->e_type != ELF::ET_REL) |
| 797 | return section_end(); |
| 798 | |
| 799 | const Elf_Shdr *EShdr = getSection(Sec); |
| 800 | uintX_t Type = EShdr->sh_type; |
| 801 | if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA) |
| 802 | return section_end(); |
| 803 | |
| 804 | auto R = EF.getSection(EShdr->sh_info); |
| 805 | if (!R) |
| 806 | report_fatal_error(errorToErrorCode(R.takeError()).message()); |
| 807 | return section_iterator(SectionRef(toDRI(*R), this)); |
| 808 | } |
| 809 | |
| 810 | // Relocations |
| 811 | template <class ELFT> |
| 812 | void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const { |
| 813 | ++Rel.d.b; |
| 814 | } |
| 815 | |
| 816 | template <class ELFT> |
| 817 | symbol_iterator |
| 818 | ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const { |
| 819 | uint32_t symbolIdx; |
| 820 | const Elf_Shdr *sec = getRelSection(Rel); |
| 821 | if (sec->sh_type == ELF::SHT_REL) |
| 822 | symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL()); |
| 823 | else |
| 824 | symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL()); |
| 825 | if (!symbolIdx) |
| 826 | return symbol_end(); |
| 827 | |
| 828 | // FIXME: error check symbolIdx |
| 829 | DataRefImpl SymbolData; |
| 830 | SymbolData.d.a = sec->sh_link; |
| 831 | SymbolData.d.b = symbolIdx; |
| 832 | return symbol_iterator(SymbolRef(SymbolData, this)); |
| 833 | } |
| 834 | |
| 835 | template <class ELFT> |
| 836 | uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 837 | const Elf_Shdr *sec = getRelSection(Rel); |
| 838 | if (sec->sh_type == ELF::SHT_REL) |
| 839 | return getRel(Rel)->r_offset; |
| 840 | |
| 841 | return getRela(Rel)->r_offset; |
| 842 | } |
| 843 | |
| 844 | template <class ELFT> |
| 845 | uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const { |
| 846 | const Elf_Shdr *sec = getRelSection(Rel); |
| 847 | if (sec->sh_type == ELF::SHT_REL) |
| 848 | return getRel(Rel)->getType(EF.isMips64EL()); |
| 849 | else |
| 850 | return getRela(Rel)->getType(EF.isMips64EL()); |
| 851 | } |
| 852 | |
| 853 | template <class ELFT> |
| 854 | StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const { |
| 855 | return getELFRelocationTypeName(EF.getHeader()->e_machine, Type); |
| 856 | } |
| 857 | |
| 858 | template <class ELFT> |
| 859 | void ELFObjectFile<ELFT>::getRelocationTypeName( |
| 860 | DataRefImpl Rel, SmallVectorImpl<char> &Result) const { |
| 861 | uint32_t type = getRelocationType(Rel); |
| 862 | EF.getRelocationTypeName(type, Result); |
| 863 | } |
| 864 | |
| 865 | template <class ELFT> |
| 866 | Expected<int64_t> |
| 867 | ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const { |
| 868 | if (getRelSection(Rel)->sh_type != ELF::SHT_RELA) |
| 869 | return createError("Section is not SHT_RELA"); |
| 870 | return (int64_t)getRela(Rel)->r_addend; |
| 871 | } |
| 872 | |
| 873 | template <class ELFT> |
| 874 | const typename ELFObjectFile<ELFT>::Elf_Rel * |
| 875 | ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const { |
| 876 | assert(getRelSection(Rel)->sh_type == ELF::SHT_REL); |
| 877 | auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b); |
| 878 | if (!Ret) |
| 879 | report_fatal_error(errorToErrorCode(Ret.takeError()).message()); |
| 880 | return *Ret; |
| 881 | } |
| 882 | |
| 883 | template <class ELFT> |
| 884 | const typename ELFObjectFile<ELFT>::Elf_Rela * |
| 885 | ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const { |
| 886 | assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA); |
| 887 | auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b); |
| 888 | if (!Ret) |
| 889 | report_fatal_error(errorToErrorCode(Ret.takeError()).message()); |
| 890 | return *Ret; |
| 891 | } |
| 892 | |
| 893 | template <class ELFT> |
| 894 | Expected<ELFObjectFile<ELFT>> |
| 895 | ELFObjectFile<ELFT>::create(MemoryBufferRef Object) { |
| 896 | auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer()); |
| 897 | if (Error E = EFOrErr.takeError()) |
| 898 | return std::move(E); |
| 899 | auto EF = std::move(*EFOrErr); |
| 900 | |
| 901 | auto SectionsOrErr = EF.sections(); |
| 902 | if (!SectionsOrErr) |
| 903 | return SectionsOrErr.takeError(); |
| 904 | |
| 905 | const Elf_Shdr *DotDynSymSec = nullptr; |
| 906 | const Elf_Shdr *DotSymtabSec = nullptr; |
| 907 | ArrayRef<Elf_Word> ShndxTable; |
| 908 | for (const Elf_Shdr &Sec : *SectionsOrErr) { |
| 909 | switch (Sec.sh_type) { |
| 910 | case ELF::SHT_DYNSYM: { |
| 911 | if (DotDynSymSec) |
| 912 | return createError("More than one dynamic symbol table!"); |
| 913 | DotDynSymSec = &Sec; |
| 914 | break; |
| 915 | } |
| 916 | case ELF::SHT_SYMTAB: { |
| 917 | if (DotSymtabSec) |
| 918 | return createError("More than one static symbol table!"); |
| 919 | DotSymtabSec = &Sec; |
| 920 | break; |
| 921 | } |
| 922 | case ELF::SHT_SYMTAB_SHNDX: { |
| 923 | auto TableOrErr = EF.getSHNDXTable(Sec); |
| 924 | if (!TableOrErr) |
| 925 | return TableOrErr.takeError(); |
| 926 | ShndxTable = *TableOrErr; |
| 927 | break; |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | return ELFObjectFile<ELFT>(Object, EF, DotDynSymSec, DotSymtabSec, |
| 932 | ShndxTable); |
| 933 | } |
| 934 | |
| 935 | template <class ELFT> |
| 936 | ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF, |
| 937 | const Elf_Shdr *DotDynSymSec, |
| 938 | const Elf_Shdr *DotSymtabSec, |
| 939 | ArrayRef<Elf_Word> ShndxTable) |
| 940 | : ELFObjectFileBase( |
| 941 | getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits), |
| 942 | Object), |
| 943 | EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec), |
| 944 | ShndxTable(ShndxTable) {} |
| 945 | |
| 946 | template <class ELFT> |
| 947 | ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other) |
| 948 | : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec, |
| 949 | Other.DotSymtabSec, Other.ShndxTable) {} |
| 950 | |
| 951 | template <class ELFT> |
| 952 | basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin() const { |
| 953 | DataRefImpl Sym = toDRI(DotSymtabSec, 0); |
| 954 | return basic_symbol_iterator(SymbolRef(Sym, this)); |
| 955 | } |
| 956 | |
| 957 | template <class ELFT> |
| 958 | basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end() const { |
| 959 | const Elf_Shdr *SymTab = DotSymtabSec; |
| 960 | if (!SymTab) |
| 961 | return symbol_begin(); |
| 962 | DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym)); |
| 963 | return basic_symbol_iterator(SymbolRef(Sym, this)); |
| 964 | } |
| 965 | |
| 966 | template <class ELFT> |
| 967 | elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const { |
| 968 | DataRefImpl Sym = toDRI(DotDynSymSec, 0); |
| 969 | return symbol_iterator(SymbolRef(Sym, this)); |
| 970 | } |
| 971 | |
| 972 | template <class ELFT> |
| 973 | elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const { |
| 974 | const Elf_Shdr *SymTab = DotDynSymSec; |
| 975 | if (!SymTab) |
| 976 | return dynamic_symbol_begin(); |
| 977 | DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym)); |
| 978 | return basic_symbol_iterator(SymbolRef(Sym, this)); |
| 979 | } |
| 980 | |
| 981 | template <class ELFT> |
| 982 | section_iterator ELFObjectFile<ELFT>::section_begin() const { |
| 983 | auto SectionsOrErr = EF.sections(); |
| 984 | if (!SectionsOrErr) |
| 985 | return section_iterator(SectionRef()); |
| 986 | return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this)); |
| 987 | } |
| 988 | |
| 989 | template <class ELFT> |
| 990 | section_iterator ELFObjectFile<ELFT>::section_end() const { |
| 991 | auto SectionsOrErr = EF.sections(); |
| 992 | if (!SectionsOrErr) |
| 993 | return section_iterator(SectionRef()); |
| 994 | return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this)); |
| 995 | } |
| 996 | |
| 997 | template <class ELFT> |
| 998 | uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const { |
| 999 | return ELFT::Is64Bits ? 8 : 4; |
| 1000 | } |
| 1001 | |
| 1002 | template <class ELFT> |
| 1003 | StringRef ELFObjectFile<ELFT>::getFileFormatName() const { |
| 1004 | bool IsLittleEndian = ELFT::TargetEndianness == support::little; |
| 1005 | switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) { |
| 1006 | case ELF::ELFCLASS32: |
| 1007 | switch (EF.getHeader()->e_machine) { |
| 1008 | case ELF::EM_386: |
| 1009 | return "ELF32-i386"; |
| 1010 | case ELF::EM_IAMCU: |
| 1011 | return "ELF32-iamcu"; |
| 1012 | case ELF::EM_X86_64: |
| 1013 | return "ELF32-x86-64"; |
| 1014 | case ELF::EM_ARM: |
| 1015 | return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big"); |
| 1016 | case ELF::EM_AVR: |
| 1017 | return "ELF32-avr"; |
| 1018 | case ELF::EM_HEXAGON: |
| 1019 | return "ELF32-hexagon"; |
| 1020 | case ELF::EM_LANAI: |
| 1021 | return "ELF32-lanai"; |
| 1022 | case ELF::EM_MIPS: |
| 1023 | return "ELF32-mips"; |
| 1024 | case ELF::EM_PPC: |
| 1025 | return "ELF32-ppc"; |
| 1026 | case ELF::EM_RISCV: |
| 1027 | return "ELF32-riscv"; |
| 1028 | case ELF::EM_SPARC: |
| 1029 | case ELF::EM_SPARC32PLUS: |
| 1030 | return "ELF32-sparc"; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1031 | case ELF::EM_AMDGPU: |
| 1032 | return "ELF32-amdgpu"; |
| 1033 | default: |
| 1034 | return "ELF32-unknown"; |
| 1035 | } |
| 1036 | case ELF::ELFCLASS64: |
| 1037 | switch (EF.getHeader()->e_machine) { |
| 1038 | case ELF::EM_386: |
| 1039 | return "ELF64-i386"; |
| 1040 | case ELF::EM_X86_64: |
| 1041 | return "ELF64-x86-64"; |
| 1042 | case ELF::EM_AARCH64: |
| 1043 | return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big"); |
| 1044 | case ELF::EM_PPC64: |
| 1045 | return "ELF64-ppc64"; |
| 1046 | case ELF::EM_RISCV: |
| 1047 | return "ELF64-riscv"; |
| 1048 | case ELF::EM_S390: |
| 1049 | return "ELF64-s390"; |
| 1050 | case ELF::EM_SPARCV9: |
| 1051 | return "ELF64-sparc"; |
| 1052 | case ELF::EM_MIPS: |
| 1053 | return "ELF64-mips"; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1054 | case ELF::EM_AMDGPU: |
| 1055 | return "ELF64-amdgpu"; |
| 1056 | case ELF::EM_BPF: |
| 1057 | return "ELF64-BPF"; |
| 1058 | default: |
| 1059 | return "ELF64-unknown"; |
| 1060 | } |
| 1061 | default: |
| 1062 | // FIXME: Proper error handling. |
| 1063 | report_fatal_error("Invalid ELFCLASS!"); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const { |
| 1068 | bool IsLittleEndian = ELFT::TargetEndianness == support::little; |
| 1069 | switch (EF.getHeader()->e_machine) { |
| 1070 | case ELF::EM_386: |
| 1071 | case ELF::EM_IAMCU: |
| 1072 | return Triple::x86; |
| 1073 | case ELF::EM_X86_64: |
| 1074 | return Triple::x86_64; |
| 1075 | case ELF::EM_AARCH64: |
| 1076 | return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be; |
| 1077 | case ELF::EM_ARM: |
| 1078 | return Triple::arm; |
| 1079 | case ELF::EM_AVR: |
| 1080 | return Triple::avr; |
| 1081 | case ELF::EM_HEXAGON: |
| 1082 | return Triple::hexagon; |
| 1083 | case ELF::EM_LANAI: |
| 1084 | return Triple::lanai; |
| 1085 | case ELF::EM_MIPS: |
| 1086 | switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) { |
| 1087 | case ELF::ELFCLASS32: |
| 1088 | return IsLittleEndian ? Triple::mipsel : Triple::mips; |
| 1089 | case ELF::ELFCLASS64: |
| 1090 | return IsLittleEndian ? Triple::mips64el : Triple::mips64; |
| 1091 | default: |
| 1092 | report_fatal_error("Invalid ELFCLASS!"); |
| 1093 | } |
| 1094 | case ELF::EM_PPC: |
| 1095 | return Triple::ppc; |
| 1096 | case ELF::EM_PPC64: |
| 1097 | return IsLittleEndian ? Triple::ppc64le : Triple::ppc64; |
| 1098 | case ELF::EM_RISCV: |
| 1099 | switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) { |
| 1100 | case ELF::ELFCLASS32: |
| 1101 | return Triple::riscv32; |
| 1102 | case ELF::ELFCLASS64: |
| 1103 | return Triple::riscv64; |
| 1104 | default: |
| 1105 | report_fatal_error("Invalid ELFCLASS!"); |
| 1106 | } |
| 1107 | case ELF::EM_S390: |
| 1108 | return Triple::systemz; |
| 1109 | |
| 1110 | case ELF::EM_SPARC: |
| 1111 | case ELF::EM_SPARC32PLUS: |
| 1112 | return IsLittleEndian ? Triple::sparcel : Triple::sparc; |
| 1113 | case ELF::EM_SPARCV9: |
| 1114 | return Triple::sparcv9; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1115 | |
| 1116 | case ELF::EM_AMDGPU: { |
| 1117 | if (!IsLittleEndian) |
| 1118 | return Triple::UnknownArch; |
| 1119 | |
| 1120 | unsigned MACH = EF.getHeader()->e_flags & ELF::EF_AMDGPU_MACH; |
| 1121 | if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST && |
| 1122 | MACH <= ELF::EF_AMDGPU_MACH_R600_LAST) |
| 1123 | return Triple::r600; |
| 1124 | if (MACH >= ELF::EF_AMDGPU_MACH_AMDGCN_FIRST && |
| 1125 | MACH <= ELF::EF_AMDGPU_MACH_AMDGCN_LAST) |
| 1126 | return Triple::amdgcn; |
| 1127 | |
| 1128 | return Triple::UnknownArch; |
| 1129 | } |
| 1130 | |
| 1131 | case ELF::EM_BPF: |
| 1132 | return IsLittleEndian ? Triple::bpfel : Triple::bpfeb; |
| 1133 | |
| 1134 | default: |
| 1135 | return Triple::UnknownArch; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | template <class ELFT> |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 1140 | Expected<uint64_t> ELFObjectFile<ELFT>::getStartAddress() const { |
| 1141 | return EF.getHeader()->e_entry; |
| 1142 | } |
| 1143 | |
| 1144 | template <class ELFT> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1145 | ELFObjectFileBase::elf_symbol_iterator_range |
| 1146 | ELFObjectFile<ELFT>::getDynamicSymbolIterators() const { |
| 1147 | return make_range(dynamic_symbol_begin(), dynamic_symbol_end()); |
| 1148 | } |
| 1149 | |
| 1150 | template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const { |
| 1151 | return EF.getHeader()->e_type == ELF::ET_REL; |
| 1152 | } |
| 1153 | |
| 1154 | } // end namespace object |
| 1155 | } // end namespace llvm |
| 1156 | |
| 1157 | #endif // LLVM_OBJECT_ELFOBJECTFILE_H |