2013-08-09 00:27:13 +02:00
|
|
|
//===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file declares the ELFObjectFile template class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 18:26:38 +02:00
|
|
|
#ifndef LLVM_OBJECT_ELFOBJECTFILE_H
|
|
|
|
#define LLVM_OBJECT_ELFOBJECTFILE_H
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2016-05-10 01:11:38 +02:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2018-06-07 15:30:55 +02:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2013-08-09 00:27:13 +02:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-05-10 01:11:38 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2013-08-09 00:27:13 +02:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2016-05-10 01:11:38 +02:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2017-06-07 05:48:56 +02:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2016-06-16 11:17:03 +02:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
2016-05-10 01:11:38 +02:00
|
|
|
#include "llvm/Object/Binary.h"
|
2013-08-09 00:27:13 +02:00
|
|
|
#include "llvm/Object/ELF.h"
|
2016-05-10 01:11:38 +02:00
|
|
|
#include "llvm/Object/ELFTypes.h"
|
|
|
|
#include "llvm/Object/Error.h"
|
2013-08-09 00:27:13 +02:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2016-05-10 01:11:38 +02:00
|
|
|
#include "llvm/Object/SymbolicFile.h"
|
2017-01-18 14:52:12 +01:00
|
|
|
#include "llvm/Support/ARMAttributeParser.h"
|
2017-04-25 01:21:38 +02:00
|
|
|
#include "llvm/Support/ARMBuildAttributes.h"
|
2013-08-09 00:27:13 +02:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
2016-05-10 01:11:38 +02:00
|
|
|
#include "llvm/Support/Error.h"
|
2013-08-09 00:27:13 +02:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2016-05-10 01:11:38 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <system_error>
|
2013-08-09 00:27:13 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace object {
|
|
|
|
|
2015-06-26 00:10:04 +02:00
|
|
|
class elf_symbol_iterator;
|
|
|
|
|
2014-08-17 19:52:10 +02:00
|
|
|
class ELFObjectFileBase : public ObjectFile {
|
2015-06-30 02:33:59 +02:00
|
|
|
friend class ELFRelocationRef;
|
2017-04-25 01:21:38 +02:00
|
|
|
friend class ELFSectionRef;
|
|
|
|
friend class ELFSymbolRef;
|
2015-06-26 00:10:04 +02:00
|
|
|
|
2014-08-17 19:52:10 +02:00
|
|
|
protected:
|
2014-08-19 20:44:46 +02:00
|
|
|
ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
|
2014-08-17 19:52:10 +02:00
|
|
|
|
2016-06-16 11:17:03 +02:00
|
|
|
virtual uint16_t getEMachine() const = 0;
|
2015-06-26 00:10:04 +02:00
|
|
|
virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
|
2015-06-26 13:39:57 +02:00
|
|
|
virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
|
2015-06-26 15:11:15 +02:00
|
|
|
virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
|
|
|
|
|
2015-06-26 14:33:37 +02:00
|
|
|
virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
|
2015-06-26 14:44:10 +02:00
|
|
|
virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
|
2016-10-31 12:03:01 +01:00
|
|
|
virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
|
2015-06-26 00:10:04 +02:00
|
|
|
|
2017-10-11 18:56:33 +02:00
|
|
|
virtual Expected<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
|
2015-06-19 22:58:43 +02:00
|
|
|
|
2016-05-10 01:11:38 +02:00
|
|
|
public:
|
2017-04-25 01:21:38 +02:00
|
|
|
using elf_symbol_iterator_range = iterator_range<elf_symbol_iterator>;
|
|
|
|
|
2015-06-26 00:10:04 +02:00
|
|
|
virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
|
2014-08-17 19:52:10 +02:00
|
|
|
|
2018-01-29 19:27:30 +01:00
|
|
|
/// Returns platform-specific object flags, if any.
|
|
|
|
virtual unsigned getPlatformFlags() const = 0;
|
|
|
|
|
2015-06-26 00:10:04 +02:00
|
|
|
elf_symbol_iterator_range symbols() const;
|
2015-06-24 12:20:30 +02:00
|
|
|
|
2017-06-29 21:35:17 +02:00
|
|
|
static bool classof(const Binary *v) { return v->isELF(); }
|
2016-06-16 11:17:03 +02:00
|
|
|
|
|
|
|
SubtargetFeatures getFeatures() const override;
|
2017-01-18 14:52:12 +01:00
|
|
|
|
2017-01-31 15:35:01 +01:00
|
|
|
SubtargetFeatures getMIPSFeatures() const;
|
2017-01-18 16:52:11 +01:00
|
|
|
|
2017-01-31 15:35:01 +01:00
|
|
|
SubtargetFeatures getARMFeatures() const;
|
2017-01-18 16:52:11 +01:00
|
|
|
|
2018-02-02 07:01:02 +01:00
|
|
|
SubtargetFeatures getRISCVFeatures() const;
|
|
|
|
|
2017-01-18 14:52:12 +01:00
|
|
|
void setARMSubArch(Triple &TheTriple) const override;
|
2018-06-07 15:30:55 +02:00
|
|
|
|
|
|
|
virtual uint16_t getEType() const = 0;
|
2018-08-24 17:21:56 +02:00
|
|
|
|
|
|
|
std::vector<std::pair<DataRefImpl, uint64_t>> getPltAddresses() const;
|
2014-08-17 19:52:10 +02:00
|
|
|
};
|
|
|
|
|
2015-06-26 14:33:37 +02:00
|
|
|
class ELFSectionRef : public SectionRef {
|
|
|
|
public:
|
|
|
|
ELFSectionRef(const SectionRef &B) : SectionRef(B) {
|
|
|
|
assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
|
|
|
|
}
|
|
|
|
|
|
|
|
const ELFObjectFileBase *getObject() const {
|
|
|
|
return cast<ELFObjectFileBase>(SectionRef::getObject());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getType() const {
|
|
|
|
return getObject()->getSectionType(getRawDataRefImpl());
|
|
|
|
}
|
2015-06-26 14:44:10 +02:00
|
|
|
|
|
|
|
uint64_t getFlags() const {
|
|
|
|
return getObject()->getSectionFlags(getRawDataRefImpl());
|
|
|
|
}
|
2016-10-31 12:03:01 +01:00
|
|
|
|
|
|
|
uint64_t getOffset() const {
|
|
|
|
return getObject()->getSectionOffset(getRawDataRefImpl());
|
|
|
|
}
|
2015-06-26 14:33:37 +02:00
|
|
|
};
|
|
|
|
|
2015-06-26 15:11:15 +02:00
|
|
|
class elf_section_iterator : public section_iterator {
|
|
|
|
public:
|
|
|
|
elf_section_iterator(const section_iterator &B) : section_iterator(B) {
|
|
|
|
assert(isa<ELFObjectFileBase>(B->getObject()));
|
|
|
|
}
|
|
|
|
|
|
|
|
const ELFSectionRef *operator->() const {
|
|
|
|
return static_cast<const ELFSectionRef *>(section_iterator::operator->());
|
|
|
|
}
|
|
|
|
|
|
|
|
const ELFSectionRef &operator*() const {
|
|
|
|
return static_cast<const ELFSectionRef &>(section_iterator::operator*());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-26 00:10:04 +02:00
|
|
|
class ELFSymbolRef : public SymbolRef {
|
|
|
|
public:
|
|
|
|
ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
|
|
|
|
assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
|
|
|
|
}
|
|
|
|
|
|
|
|
const ELFObjectFileBase *getObject() const {
|
|
|
|
return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t getSize() const {
|
|
|
|
return getObject()->getSymbolSize(getRawDataRefImpl());
|
|
|
|
}
|
2015-06-26 13:39:57 +02:00
|
|
|
|
|
|
|
uint8_t getOther() const {
|
|
|
|
return getObject()->getSymbolOther(getRawDataRefImpl());
|
|
|
|
}
|
2015-06-26 15:11:15 +02:00
|
|
|
|
|
|
|
uint8_t getELFType() const {
|
|
|
|
return getObject()->getSymbolELFType(getRawDataRefImpl());
|
|
|
|
}
|
2015-06-26 00:10:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class elf_symbol_iterator : public symbol_iterator {
|
|
|
|
public:
|
|
|
|
elf_symbol_iterator(const basic_symbol_iterator &B)
|
|
|
|
: symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
|
|
|
|
cast<ELFObjectFileBase>(B->getObject()))) {}
|
|
|
|
|
|
|
|
const ELFSymbolRef *operator->() const {
|
|
|
|
return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
|
|
|
|
}
|
|
|
|
|
|
|
|
const ELFSymbolRef &operator*() const {
|
|
|
|
return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-30 02:33:59 +02:00
|
|
|
class ELFRelocationRef : public RelocationRef {
|
|
|
|
public:
|
|
|
|
ELFRelocationRef(const RelocationRef &B) : RelocationRef(B) {
|
|
|
|
assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
|
|
|
|
}
|
|
|
|
|
|
|
|
const ELFObjectFileBase *getObject() const {
|
|
|
|
return cast<ELFObjectFileBase>(RelocationRef::getObject());
|
|
|
|
}
|
|
|
|
|
2017-10-11 18:56:33 +02:00
|
|
|
Expected<int64_t> getAddend() const {
|
2015-06-30 02:33:59 +02:00
|
|
|
return getObject()->getRelocationAddend(getRawDataRefImpl());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class elf_relocation_iterator : public relocation_iterator {
|
|
|
|
public:
|
|
|
|
elf_relocation_iterator(const relocation_iterator &B)
|
|
|
|
: relocation_iterator(RelocationRef(
|
|
|
|
B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
|
|
|
|
|
|
|
|
const ELFRelocationRef *operator->() const {
|
|
|
|
return static_cast<const ELFRelocationRef *>(
|
|
|
|
relocation_iterator::operator->());
|
|
|
|
}
|
|
|
|
|
|
|
|
const ELFRelocationRef &operator*() const {
|
|
|
|
return static_cast<const ELFRelocationRef &>(
|
|
|
|
relocation_iterator::operator*());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-26 00:10:04 +02:00
|
|
|
inline ELFObjectFileBase::elf_symbol_iterator_range
|
|
|
|
ELFObjectFileBase::symbols() const {
|
|
|
|
return elf_symbol_iterator_range(symbol_begin(), symbol_end());
|
|
|
|
}
|
|
|
|
|
2014-08-17 19:52:10 +02:00
|
|
|
template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
|
2016-06-16 11:17:03 +02:00
|
|
|
uint16_t getEMachine() const override;
|
2018-06-07 15:30:55 +02:00
|
|
|
uint16_t getEType() const override;
|
2015-06-26 00:10:04 +02:00
|
|
|
uint64_t getSymbolSize(DataRefImpl Sym) const override;
|
|
|
|
|
2013-08-09 00:27:13 +02:00
|
|
|
public:
|
|
|
|
LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
|
|
|
|
|
2018-01-12 03:28:31 +01:00
|
|
|
using uintX_t = typename ELFT::uint;
|
|
|
|
|
|
|
|
using Elf_Sym = typename ELFT::Sym;
|
|
|
|
using Elf_Shdr = typename ELFT::Shdr;
|
|
|
|
using Elf_Ehdr = typename ELFT::Ehdr;
|
|
|
|
using Elf_Rel = typename ELFT::Rel;
|
|
|
|
using Elf_Rela = typename ELFT::Rela;
|
|
|
|
using Elf_Dyn = typename ELFT::Dyn;
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2017-10-10 23:21:16 +02:00
|
|
|
private:
|
2017-10-11 00:17:49 +02:00
|
|
|
ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
|
|
|
|
const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
|
|
|
|
ArrayRef<Elf_Word> ShndxTable);
|
2017-10-10 23:21:16 +02:00
|
|
|
|
2013-08-09 00:27:13 +02:00
|
|
|
protected:
|
|
|
|
ELFFile<ELFT> EF;
|
|
|
|
|
2015-08-07 22:11:08 +02:00
|
|
|
const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
|
2015-08-10 22:25:04 +02:00
|
|
|
const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
|
2015-08-10 23:29:35 +02:00
|
|
|
ArrayRef<Elf_Word> ShndxTable;
|
2015-08-07 22:11:08 +02:00
|
|
|
|
2014-03-02 10:09:27 +01:00
|
|
|
void moveSymbolNext(DataRefImpl &Symb) const override;
|
2016-04-20 23:24:34 +02:00
|
|
|
Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
|
2016-06-24 20:24:42 +02:00
|
|
|
Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
|
2015-07-07 19:12:59 +02:00
|
|
|
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
|
2015-06-01 01:52:50 +02:00
|
|
|
uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
|
2015-06-24 12:20:30 +02:00
|
|
|
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
|
2014-03-02 10:09:27 +01:00
|
|
|
uint32_t getSymbolFlags(DataRefImpl Symb) const override;
|
2015-06-26 13:39:57 +02:00
|
|
|
uint8_t getSymbolOther(DataRefImpl Symb) const override;
|
2015-06-26 15:11:15 +02:00
|
|
|
uint8_t getSymbolELFType(DataRefImpl Symb) const override;
|
2016-05-02 22:28:12 +02:00
|
|
|
Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
|
|
|
|
Expected<section_iterator> getSymbolSection(const Elf_Sym *Symb,
|
|
|
|
const Elf_Shdr *SymTab) const;
|
|
|
|
Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
|
2014-01-30 02:17:42 +01:00
|
|
|
|
2014-03-02 10:09:27 +01:00
|
|
|
void moveSectionNext(DataRefImpl &Sec) const override;
|
2014-06-12 23:46:39 +02:00
|
|
|
std::error_code getSectionName(DataRefImpl Sec,
|
|
|
|
StringRef &Res) const override;
|
2014-10-08 17:28:58 +02:00
|
|
|
uint64_t getSectionAddress(DataRefImpl Sec) const override;
|
2017-05-27 20:10:23 +02:00
|
|
|
uint64_t getSectionIndex(DataRefImpl Sec) const override;
|
2014-10-08 17:28:58 +02:00
|
|
|
uint64_t getSectionSize(DataRefImpl Sec) const override;
|
2014-06-12 23:46:39 +02:00
|
|
|
std::error_code getSectionContents(DataRefImpl Sec,
|
|
|
|
StringRef &Res) const override;
|
2014-10-08 17:28:58 +02:00
|
|
|
uint64_t getSectionAlignment(DataRefImpl Sec) const override;
|
2016-05-24 14:48:46 +02:00
|
|
|
bool isSectionCompressed(DataRefImpl Sec) const override;
|
2014-10-08 17:28:58 +02:00
|
|
|
bool isSectionText(DataRefImpl Sec) const override;
|
|
|
|
bool isSectionData(DataRefImpl Sec) const override;
|
|
|
|
bool isSectionBSS(DataRefImpl Sec) const override;
|
|
|
|
bool isSectionVirtual(DataRefImpl Sec) const override;
|
2018-12-13 20:40:12 +01:00
|
|
|
bool isBerkeleyText(DataRefImpl Sec) const override;
|
|
|
|
bool isBerkeleyData(DataRefImpl Sec) const override;
|
2014-03-05 08:52:44 +01:00
|
|
|
relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
|
2014-03-02 10:09:27 +01:00
|
|
|
relocation_iterator section_rel_end(DataRefImpl Sec) const override;
|
2018-06-07 15:30:55 +02:00
|
|
|
std::vector<SectionRef> dynamic_relocation_sections() const override;
|
2014-03-02 10:09:27 +01:00
|
|
|
section_iterator getRelocatedSection(DataRefImpl Sec) const override;
|
|
|
|
|
|
|
|
void moveRelocationNext(DataRefImpl &Rel) const override;
|
2015-06-30 01:29:12 +02:00
|
|
|
uint64_t getRelocationOffset(DataRefImpl Rel) const override;
|
2014-03-02 10:09:27 +01:00
|
|
|
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
|
2015-06-30 03:53:01 +02:00
|
|
|
uint64_t getRelocationType(DataRefImpl Rel) const override;
|
2015-06-30 06:08:37 +02:00
|
|
|
void getRelocationTypeName(DataRefImpl Rel,
|
|
|
|
SmallVectorImpl<char> &Result) const override;
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2015-06-26 14:33:37 +02:00
|
|
|
uint32_t getSectionType(DataRefImpl Sec) const override;
|
2015-06-26 14:44:10 +02:00
|
|
|
uint64_t getSectionFlags(DataRefImpl Sec) const override;
|
2016-10-31 12:03:01 +01:00
|
|
|
uint64_t getSectionOffset(DataRefImpl Sec) const override;
|
2013-08-09 00:27:13 +02:00
|
|
|
StringRef getRelocationTypeName(uint32_t Type) const;
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Get the relocation section that contains \a Rel.
|
2013-08-09 00:27:13 +02:00
|
|
|
const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
|
2016-11-16 06:10:28 +01:00
|
|
|
auto RelSecOrErr = EF.getSection(Rel.d.a);
|
|
|
|
if (!RelSecOrErr)
|
|
|
|
report_fatal_error(errorToErrorCode(RelSecOrErr.takeError()).message());
|
|
|
|
return *RelSecOrErr;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
2015-07-03 14:00:05 +02:00
|
|
|
DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
|
2013-08-09 00:27:13 +02:00
|
|
|
DataRefImpl DRI;
|
2015-07-03 14:00:05 +02:00
|
|
|
if (!SymTable) {
|
|
|
|
DRI.d.a = 0;
|
|
|
|
DRI.d.b = 0;
|
|
|
|
return DRI;
|
|
|
|
}
|
2015-07-03 14:53:50 +02:00
|
|
|
assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
|
|
|
|
SymTable->sh_type == ELF::SHT_DYNSYM);
|
2015-07-03 14:00:05 +02:00
|
|
|
|
2016-11-16 06:10:28 +01:00
|
|
|
auto SectionsOrErr = EF.sections();
|
|
|
|
if (!SectionsOrErr) {
|
|
|
|
DRI.d.a = 0;
|
|
|
|
DRI.d.b = 0;
|
|
|
|
return DRI;
|
|
|
|
}
|
|
|
|
uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
|
2015-07-03 14:00:05 +02:00
|
|
|
unsigned SymTableIndex =
|
|
|
|
(reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
|
|
|
|
|
|
|
|
DRI.d.a = SymTableIndex;
|
|
|
|
DRI.d.b = SymbolNum;
|
2013-08-09 00:27:13 +02:00
|
|
|
return DRI;
|
|
|
|
}
|
|
|
|
|
2015-06-30 21:58:10 +02:00
|
|
|
const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
|
|
|
|
return reinterpret_cast<const Elf_Shdr *>(Sec.p);
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DataRefImpl toDRI(const Elf_Shdr *Sec) const {
|
|
|
|
DataRefImpl DRI;
|
|
|
|
DRI.p = reinterpret_cast<uintptr_t>(Sec);
|
|
|
|
return DRI;
|
|
|
|
}
|
|
|
|
|
2015-07-20 23:23:29 +02:00
|
|
|
DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
|
2013-08-09 00:27:13 +02:00
|
|
|
DataRefImpl DRI;
|
2015-07-20 23:23:29 +02:00
|
|
|
DRI.p = reinterpret_cast<uintptr_t>(Dyn);
|
2013-08-09 00:27:13 +02:00
|
|
|
return DRI;
|
|
|
|
}
|
|
|
|
|
2015-01-15 23:33:30 +01:00
|
|
|
bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
|
|
|
|
unsigned char Binding = ESym->getBinding();
|
|
|
|
unsigned char Visibility = ESym->getVisibility();
|
|
|
|
|
|
|
|
// A symbol is exported if its binding is either GLOBAL or WEAK, and its
|
|
|
|
// visibility is either DEFAULT or PROTECTED. All other symbols are not
|
|
|
|
// exported.
|
2018-11-23 02:33:19 +01:00
|
|
|
return (
|
|
|
|
(Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK ||
|
|
|
|
Binding == ELF::STB_GNU_UNIQUE) &&
|
|
|
|
(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
|
2015-01-15 23:33:30 +01:00
|
|
|
}
|
|
|
|
|
2013-08-09 00:27:13 +02:00
|
|
|
// This flag is used for classof, to distinguish ELFObjectFile from
|
|
|
|
// its subclass. If more subclasses will be created, this flag will
|
|
|
|
// have to become an enum.
|
|
|
|
bool isDyldELFObject;
|
|
|
|
|
|
|
|
public:
|
2017-10-10 23:21:16 +02:00
|
|
|
ELFObjectFile(ELFObjectFile<ELFT> &&Other);
|
|
|
|
static Expected<ELFObjectFile<ELFT>> create(MemoryBufferRef Object);
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2015-07-02 16:21:38 +02:00
|
|
|
const Elf_Rel *getRel(DataRefImpl Rel) const;
|
|
|
|
const Elf_Rela *getRela(DataRefImpl Rela) const;
|
|
|
|
|
2015-08-10 21:07:56 +02:00
|
|
|
const Elf_Sym *getSymbol(DataRefImpl Sym) const {
|
2016-11-03 18:37:28 +01:00
|
|
|
auto Ret = EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
|
2016-11-16 06:10:28 +01:00
|
|
|
if (!Ret)
|
|
|
|
report_fatal_error(errorToErrorCode(Ret.takeError()).message());
|
2016-11-03 18:37:28 +01:00
|
|
|
return *Ret;
|
2015-08-10 21:07:56 +02:00
|
|
|
}
|
2013-11-02 22:16:09 +01:00
|
|
|
|
2015-08-10 21:10:37 +02:00
|
|
|
const Elf_Shdr *getSection(DataRefImpl Sec) const {
|
|
|
|
return reinterpret_cast<const Elf_Shdr *>(Sec.p);
|
|
|
|
}
|
|
|
|
|
2016-11-22 04:38:40 +01:00
|
|
|
basic_symbol_iterator symbol_begin() const override;
|
|
|
|
basic_symbol_iterator symbol_end() const override;
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2015-06-26 00:10:04 +02:00
|
|
|
elf_symbol_iterator dynamic_symbol_begin() const;
|
|
|
|
elf_symbol_iterator dynamic_symbol_end() const;
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2014-03-02 10:09:27 +01:00
|
|
|
section_iterator section_begin() const override;
|
|
|
|
section_iterator section_end() const override;
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2017-10-11 18:56:33 +02:00
|
|
|
Expected<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
|
2014-01-30 02:17:42 +01:00
|
|
|
|
2014-03-02 10:09:27 +01:00
|
|
|
uint8_t getBytesInAddress() const override;
|
|
|
|
StringRef getFileFormatName() const override;
|
2017-12-14 23:07:03 +01:00
|
|
|
Triple::ArchType getArch() const override;
|
2018-07-04 17:25:03 +02:00
|
|
|
Expected<uint64_t> getStartAddress() const override;
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2018-01-29 19:27:30 +01:00
|
|
|
unsigned getPlatformFlags() const override { return EF.getHeader()->e_flags; }
|
2014-07-21 01:53:14 +02:00
|
|
|
|
2017-01-18 14:52:12 +01:00
|
|
|
std::error_code getBuildAttributes(ARMAttributeParser &Attributes) const override {
|
|
|
|
auto SectionsOrErr = EF.sections();
|
|
|
|
if (!SectionsOrErr)
|
|
|
|
return errorToErrorCode(SectionsOrErr.takeError());
|
|
|
|
|
|
|
|
for (const Elf_Shdr &Sec : *SectionsOrErr) {
|
|
|
|
if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES) {
|
|
|
|
auto ErrorOrContents = EF.getSectionContents(&Sec);
|
|
|
|
if (!ErrorOrContents)
|
|
|
|
return errorToErrorCode(ErrorOrContents.takeError());
|
|
|
|
|
|
|
|
auto Contents = ErrorOrContents.get();
|
|
|
|
if (Contents[0] != ARMBuildAttrs::Format_Version || Contents.size() == 1)
|
|
|
|
return std::error_code();
|
|
|
|
|
|
|
|
Attributes.Parse(Contents, ELFT::TargetEndianness == support::little);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2013-08-09 00:27:13 +02:00
|
|
|
const ELFFile<ELFT> *getELFFile() const { return &EF; }
|
|
|
|
|
|
|
|
bool isDyldType() const { return isDyldELFObject; }
|
2017-06-29 21:35:17 +02:00
|
|
|
static bool classof(const Binary *v) {
|
2013-08-09 00:27:13 +02:00
|
|
|
return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
|
|
|
|
ELFT::Is64Bits);
|
|
|
|
}
|
2014-08-17 19:52:10 +02:00
|
|
|
|
2015-06-26 00:10:04 +02:00
|
|
|
elf_symbol_iterator_range getDynamicSymbolIterators() const override;
|
2014-08-17 21:09:37 +02:00
|
|
|
|
|
|
|
bool isRelocatableObject() const override;
|
2013-08-09 00:27:13 +02:00
|
|
|
};
|
|
|
|
|
2018-01-12 02:40:32 +01:00
|
|
|
using ELF32LEObjectFile = ELFObjectFile<ELF32LE>;
|
|
|
|
using ELF64LEObjectFile = ELFObjectFile<ELF64LE>;
|
|
|
|
using ELF32BEObjectFile = ELFObjectFile<ELF32BE>;
|
|
|
|
using ELF64BEObjectFile = ELFObjectFile<ELF64BE>;
|
2013-08-09 00:27:13 +02:00
|
|
|
|
|
|
|
template <class ELFT>
|
2015-06-29 14:38:31 +02:00
|
|
|
void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const {
|
2015-07-03 14:00:05 +02:00
|
|
|
++Sym.d.b;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-04-20 23:24:34 +02:00
|
|
|
Expected<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const {
|
2015-08-10 21:07:56 +02:00
|
|
|
const Elf_Sym *ESym = getSymbol(Sym);
|
2016-11-16 06:10:28 +01:00
|
|
|
auto SymTabOrErr = EF.getSection(Sym.d.a);
|
|
|
|
if (!SymTabOrErr)
|
|
|
|
return SymTabOrErr.takeError();
|
|
|
|
const Elf_Shdr *SymTableSec = *SymTabOrErr;
|
|
|
|
auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
|
|
|
|
if (!StrTabOrErr)
|
|
|
|
return StrTabOrErr.takeError();
|
|
|
|
const Elf_Shdr *StringTableSec = *StrTabOrErr;
|
|
|
|
auto SymStrTabOrErr = EF.getStringTable(StringTableSec);
|
|
|
|
if (!SymStrTabOrErr)
|
|
|
|
return SymStrTabOrErr.takeError();
|
|
|
|
return ESym->getName(*SymStrTabOrErr);
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
2014-12-10 21:46:55 +01:00
|
|
|
template <class ELFT>
|
2015-06-26 14:44:10 +02:00
|
|
|
uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
|
2015-08-10 21:10:37 +02:00
|
|
|
return getSection(Sec)->sh_flags;
|
2014-12-10 21:46:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-06-26 14:33:37 +02:00
|
|
|
uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
|
2015-08-10 21:10:37 +02:00
|
|
|
return getSection(Sec)->sh_type;
|
2014-12-10 21:46:55 +01:00
|
|
|
}
|
|
|
|
|
2016-10-31 12:03:01 +01:00
|
|
|
template <class ELFT>
|
|
|
|
uint64_t ELFObjectFile<ELFT>::getSectionOffset(DataRefImpl Sec) const {
|
|
|
|
return getSection(Sec)->sh_offset;
|
|
|
|
}
|
|
|
|
|
2013-08-09 00:27:13 +02:00
|
|
|
template <class ELFT>
|
2015-07-07 19:12:59 +02:00
|
|
|
uint64_t ELFObjectFile<ELFT>::getSymbolValueImpl(DataRefImpl Symb) const {
|
2013-08-09 00:27:13 +02:00
|
|
|
const Elf_Sym *ESym = getSymbol(Symb);
|
2015-06-24 21:11:10 +02:00
|
|
|
uint64_t Ret = ESym->st_value;
|
2015-07-07 19:12:59 +02:00
|
|
|
if (ESym->st_shndx == ELF::SHN_ABS)
|
|
|
|
return Ret;
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2015-07-07 19:12:59 +02:00
|
|
|
const Elf_Ehdr *Header = EF.getHeader();
|
2014-11-25 06:57:55 +01:00
|
|
|
// Clear the ARM/Thumb or microMIPS indicator flag.
|
|
|
|
if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
|
|
|
|
ESym->getType() == ELF::STT_FUNC)
|
2015-06-24 21:11:10 +02:00
|
|
|
Ret &= ~1;
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-06-24 20:24:42 +02:00
|
|
|
Expected<uint64_t>
|
2015-07-03 20:19:00 +02:00
|
|
|
ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
|
|
|
|
uint64_t Result = getSymbolValue(Symb);
|
2015-06-24 21:11:10 +02:00
|
|
|
const Elf_Sym *ESym = getSymbol(Symb);
|
|
|
|
switch (ESym->st_shndx) {
|
|
|
|
case ELF::SHN_COMMON:
|
|
|
|
case ELF::SHN_UNDEF:
|
|
|
|
case ELF::SHN_ABS:
|
2015-07-03 20:19:00 +02:00
|
|
|
return Result;
|
2015-06-24 21:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Elf_Ehdr *Header = EF.getHeader();
|
2016-11-16 06:10:28 +01:00
|
|
|
auto SymTabOrErr = EF.getSection(Symb.d.a);
|
|
|
|
if (!SymTabOrErr)
|
|
|
|
return SymTabOrErr.takeError();
|
|
|
|
const Elf_Shdr *SymTab = *SymTabOrErr;
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2015-02-19 00:00:22 +01:00
|
|
|
if (Header->e_type == ELF::ET_REL) {
|
2016-11-16 06:10:28 +01:00
|
|
|
auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable);
|
|
|
|
if (!SectionOrErr)
|
|
|
|
return SectionOrErr.takeError();
|
2015-07-01 14:56:27 +02:00
|
|
|
const Elf_Shdr *Section = *SectionOrErr;
|
|
|
|
if (Section)
|
2015-02-19 00:00:22 +01:00
|
|
|
Result += Section->sh_addr;
|
|
|
|
}
|
2014-04-03 03:51:15 +02:00
|
|
|
|
2015-07-03 20:19:00 +02:00
|
|
|
return Result;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-06-01 01:52:50 +02:00
|
|
|
uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
|
2015-08-10 21:07:56 +02:00
|
|
|
const Elf_Sym *Sym = getSymbol(Symb);
|
2013-08-09 00:27:13 +02:00
|
|
|
if (Sym->st_shndx == ELF::SHN_COMMON)
|
2015-06-01 01:52:50 +02:00
|
|
|
return Sym->st_value;
|
|
|
|
return 0;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
2016-06-16 11:17:03 +02:00
|
|
|
template <class ELFT>
|
|
|
|
uint16_t ELFObjectFile<ELFT>::getEMachine() const {
|
|
|
|
return EF.getHeader()->e_machine;
|
|
|
|
}
|
|
|
|
|
2018-06-07 15:30:55 +02:00
|
|
|
template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
|
|
|
|
return EF.getHeader()->e_type;
|
|
|
|
}
|
|
|
|
|
2013-08-09 00:27:13 +02:00
|
|
|
template <class ELFT>
|
2015-06-26 00:10:04 +02:00
|
|
|
uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
|
2015-08-10 21:07:56 +02:00
|
|
|
return getSymbol(Sym)->st_size;
|
2015-06-24 12:20:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
|
2015-08-10 21:07:56 +02:00
|
|
|
return getSymbol(Symb)->st_size;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
2014-07-21 01:53:14 +02:00
|
|
|
template <class ELFT>
|
2015-06-26 13:39:57 +02:00
|
|
|
uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
|
2015-08-10 21:07:56 +02:00
|
|
|
return getSymbol(Symb)->st_other;
|
2014-07-21 01:53:14 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:11:15 +02:00
|
|
|
template <class ELFT>
|
|
|
|
uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
|
2015-08-10 21:07:56 +02:00
|
|
|
return getSymbol(Symb)->getType();
|
2015-06-26 15:11:15 +02:00
|
|
|
}
|
|
|
|
|
2013-08-09 00:27:13 +02:00
|
|
|
template <class ELFT>
|
2016-05-02 22:28:12 +02:00
|
|
|
Expected<SymbolRef::Type>
|
2016-03-23 21:27:00 +01:00
|
|
|
ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
|
2013-08-09 00:27:13 +02:00
|
|
|
const Elf_Sym *ESym = getSymbol(Symb);
|
|
|
|
|
|
|
|
switch (ESym->getType()) {
|
|
|
|
case ELF::STT_NOTYPE:
|
2015-06-26 14:18:49 +02:00
|
|
|
return SymbolRef::ST_Unknown;
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::STT_SECTION:
|
2015-06-26 14:18:49 +02:00
|
|
|
return SymbolRef::ST_Debug;
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::STT_FILE:
|
2015-06-26 14:18:49 +02:00
|
|
|
return SymbolRef::ST_File;
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::STT_FUNC:
|
2015-06-26 14:18:49 +02:00
|
|
|
return SymbolRef::ST_Function;
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::STT_OBJECT:
|
|
|
|
case ELF::STT_COMMON:
|
|
|
|
case ELF::STT_TLS:
|
2015-06-26 14:18:49 +02:00
|
|
|
return SymbolRef::ST_Data;
|
2013-08-09 00:27:13 +02:00
|
|
|
default:
|
2015-06-26 14:18:49 +02:00
|
|
|
return SymbolRef::ST_Other;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-06-29 14:38:31 +02:00
|
|
|
uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
|
2015-08-10 21:07:56 +02:00
|
|
|
const Elf_Sym *ESym = getSymbol(Sym);
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2014-01-31 21:57:12 +01:00
|
|
|
uint32_t Result = SymbolRef::SF_None;
|
2013-08-09 00:27:13 +02:00
|
|
|
|
|
|
|
if (ESym->getBinding() != ELF::STB_LOCAL)
|
|
|
|
Result |= SymbolRef::SF_Global;
|
|
|
|
|
|
|
|
if (ESym->getBinding() == ELF::STB_WEAK)
|
|
|
|
Result |= SymbolRef::SF_Weak;
|
|
|
|
|
|
|
|
if (ESym->st_shndx == ELF::SHN_ABS)
|
|
|
|
Result |= SymbolRef::SF_Absolute;
|
|
|
|
|
2016-11-16 06:10:28 +01:00
|
|
|
if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
|
|
|
|
Result |= SymbolRef::SF_FormatSpecific;
|
|
|
|
|
|
|
|
auto DotSymtabSecSyms = EF.symbols(DotSymtabSec);
|
|
|
|
if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin())
|
|
|
|
Result |= SymbolRef::SF_FormatSpecific;
|
|
|
|
auto DotDynSymSecSyms = EF.symbols(DotDynSymSec);
|
|
|
|
if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin())
|
2013-08-09 00:27:13 +02:00
|
|
|
Result |= SymbolRef::SF_FormatSpecific;
|
|
|
|
|
2015-06-25 23:00:51 +02:00
|
|
|
if (EF.getHeader()->e_machine == ELF::EM_ARM) {
|
2016-04-20 23:24:34 +02:00
|
|
|
if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
|
2015-06-25 23:00:51 +02:00
|
|
|
StringRef Name = *NameOrErr;
|
|
|
|
if (Name.startswith("$d") || Name.startswith("$t") ||
|
|
|
|
Name.startswith("$a"))
|
|
|
|
Result |= SymbolRef::SF_FormatSpecific;
|
2016-04-20 23:24:34 +02:00
|
|
|
} else {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
consumeError(NameOrErr.takeError());
|
2015-06-25 23:00:51 +02:00
|
|
|
}
|
2016-04-05 14:11:40 +02:00
|
|
|
if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
|
|
|
|
Result |= SymbolRef::SF_Thumb;
|
2015-06-25 23:00:51 +02:00
|
|
|
}
|
|
|
|
|
2015-06-24 16:48:54 +02:00
|
|
|
if (ESym->st_shndx == ELF::SHN_UNDEF)
|
2013-08-09 00:27:13 +02:00
|
|
|
Result |= SymbolRef::SF_Undefined;
|
|
|
|
|
2015-06-24 16:48:54 +02:00
|
|
|
if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
|
2013-08-09 00:27:13 +02:00
|
|
|
Result |= SymbolRef::SF_Common;
|
|
|
|
|
2015-01-15 23:33:30 +01:00
|
|
|
if (isExportedToOtherDSO(ESym))
|
|
|
|
Result |= SymbolRef::SF_Exported;
|
|
|
|
|
2015-05-01 01:08:53 +02:00
|
|
|
if (ESym->getVisibility() == ELF::STV_HIDDEN)
|
|
|
|
Result |= SymbolRef::SF_Hidden;
|
|
|
|
|
2014-01-31 21:57:12 +01:00
|
|
|
return Result;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-05-02 22:28:12 +02:00
|
|
|
Expected<section_iterator>
|
2015-08-10 23:29:35 +02:00
|
|
|
ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym,
|
|
|
|
const Elf_Shdr *SymTab) const {
|
2016-11-16 06:10:28 +01:00
|
|
|
auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
|
|
|
|
if (!ESecOrErr)
|
|
|
|
return ESecOrErr.takeError();
|
2015-07-01 14:56:27 +02:00
|
|
|
|
|
|
|
const Elf_Shdr *ESec = *ESecOrErr;
|
2013-08-09 00:27:13 +02:00
|
|
|
if (!ESec)
|
2015-05-21 23:24:32 +02:00
|
|
|
return section_end();
|
2015-07-01 14:56:27 +02:00
|
|
|
|
|
|
|
DataRefImpl Sec;
|
|
|
|
Sec.p = reinterpret_cast<intptr_t>(ESec);
|
|
|
|
return section_iterator(SectionRef(Sec, this));
|
2015-05-21 23:24:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-05-02 22:28:12 +02:00
|
|
|
Expected<section_iterator>
|
2015-08-08 01:27:14 +02:00
|
|
|
ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb) const {
|
2015-08-10 23:29:35 +02:00
|
|
|
const Elf_Sym *Sym = getSymbol(Symb);
|
2016-11-16 06:10:28 +01:00
|
|
|
auto SymTabOrErr = EF.getSection(Symb.d.a);
|
|
|
|
if (!SymTabOrErr)
|
|
|
|
return SymTabOrErr.takeError();
|
|
|
|
const Elf_Shdr *SymTab = *SymTabOrErr;
|
2015-08-10 23:29:35 +02:00
|
|
|
return getSymbolSection(Sym, SymTab);
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2014-01-30 03:49:50 +01:00
|
|
|
void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
|
2015-08-10 21:10:37 +02:00
|
|
|
const Elf_Shdr *ESec = getSection(Sec);
|
2015-06-30 21:58:10 +02:00
|
|
|
Sec = toDRI(++ESec);
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2014-06-12 23:46:39 +02:00
|
|
|
std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
|
|
|
|
StringRef &Result) const {
|
2016-11-16 06:10:28 +01:00
|
|
|
auto Name = EF.getSectionName(&*getSection(Sec));
|
2013-08-09 00:27:13 +02:00
|
|
|
if (!Name)
|
2016-11-16 06:10:28 +01:00
|
|
|
return errorToErrorCode(Name.takeError());
|
2013-08-09 00:27:13 +02:00
|
|
|
Result = *Name;
|
2015-06-09 17:20:42 +02:00
|
|
|
return std::error_code();
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2014-10-08 17:28:58 +02:00
|
|
|
uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
|
2015-08-10 21:10:37 +02:00
|
|
|
return getSection(Sec)->sh_addr;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
2017-05-27 20:10:23 +02:00
|
|
|
template <class ELFT>
|
|
|
|
uint64_t ELFObjectFile<ELFT>::getSectionIndex(DataRefImpl Sec) const {
|
|
|
|
auto SectionsOrErr = EF.sections();
|
|
|
|
handleAllErrors(std::move(SectionsOrErr.takeError()),
|
|
|
|
[](const ErrorInfoBase &) {
|
|
|
|
llvm_unreachable("unable to get section index");
|
|
|
|
});
|
|
|
|
const Elf_Shdr *First = SectionsOrErr->begin();
|
|
|
|
return getSection(Sec) - First;
|
|
|
|
}
|
|
|
|
|
2013-08-09 00:27:13 +02:00
|
|
|
template <class ELFT>
|
2014-10-08 17:28:58 +02:00
|
|
|
uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
|
2015-08-10 21:10:37 +02:00
|
|
|
return getSection(Sec)->sh_size;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2014-06-12 23:46:39 +02:00
|
|
|
std::error_code
|
|
|
|
ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
|
|
|
|
StringRef &Result) const {
|
2015-08-10 21:10:37 +02:00
|
|
|
const Elf_Shdr *EShdr = getSection(Sec);
|
2017-08-31 14:27:10 +02:00
|
|
|
if (std::error_code EC =
|
|
|
|
checkOffset(getMemoryBufferRef(),
|
|
|
|
(uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
|
|
|
|
return EC;
|
2013-08-09 00:27:13 +02:00
|
|
|
Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
|
2015-06-09 17:20:42 +02:00
|
|
|
return std::error_code();
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2014-10-08 17:28:58 +02:00
|
|
|
uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
|
2015-08-10 21:10:37 +02:00
|
|
|
return getSection(Sec)->sh_addralign;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
2016-05-24 14:48:46 +02:00
|
|
|
template <class ELFT>
|
|
|
|
bool ELFObjectFile<ELFT>::isSectionCompressed(DataRefImpl Sec) const {
|
|
|
|
return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
|
|
|
|
}
|
|
|
|
|
2013-08-09 00:27:13 +02:00
|
|
|
template <class ELFT>
|
2014-10-08 17:28:58 +02:00
|
|
|
bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
|
2015-08-10 21:10:37 +02:00
|
|
|
return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2014-10-08 17:28:58 +02:00
|
|
|
bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
|
2015-08-10 21:10:37 +02:00
|
|
|
const Elf_Shdr *EShdr = getSection(Sec);
|
2018-06-23 02:15:33 +02:00
|
|
|
return EShdr->sh_type == ELF::SHT_PROGBITS &&
|
|
|
|
EShdr->sh_flags & ELF::SHF_ALLOC &&
|
|
|
|
!(EShdr->sh_flags & ELF::SHF_EXECINSTR);
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2014-10-08 17:28:58 +02:00
|
|
|
bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
|
2015-08-10 21:10:37 +02:00
|
|
|
const Elf_Shdr *EShdr = getSection(Sec);
|
2014-10-08 17:28:58 +02:00
|
|
|
return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
|
|
|
|
EShdr->sh_type == ELF::SHT_NOBITS;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
2018-06-07 15:30:55 +02:00
|
|
|
template <class ELFT>
|
|
|
|
std::vector<SectionRef>
|
|
|
|
ELFObjectFile<ELFT>::dynamic_relocation_sections() const {
|
|
|
|
std::vector<SectionRef> Res;
|
|
|
|
std::vector<uintptr_t> Offsets;
|
|
|
|
|
|
|
|
auto SectionsOrErr = EF.sections();
|
|
|
|
if (!SectionsOrErr)
|
|
|
|
return Res;
|
|
|
|
|
|
|
|
for (const Elf_Shdr &Sec : *SectionsOrErr) {
|
|
|
|
if (Sec.sh_type != ELF::SHT_DYNAMIC)
|
|
|
|
continue;
|
|
|
|
Elf_Dyn *Dynamic =
|
|
|
|
reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
|
|
|
|
for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
|
|
|
|
if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
|
|
|
|
Dynamic->d_tag == ELF::DT_JMPREL) {
|
|
|
|
Offsets.push_back(Dynamic->d_un.d_val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const Elf_Shdr &Sec : *SectionsOrErr) {
|
|
|
|
if (is_contained(Offsets, Sec.sh_offset))
|
|
|
|
Res.emplace_back(toDRI(&Sec), this);
|
|
|
|
}
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2013-08-09 00:27:13 +02:00
|
|
|
template <class ELFT>
|
2014-10-08 17:28:58 +02:00
|
|
|
bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
|
2015-08-10 21:10:37 +02:00
|
|
|
return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2018-12-13 20:40:12 +01:00
|
|
|
bool ELFObjectFile<ELFT>::isBerkeleyText(DataRefImpl Sec) const {
|
|
|
|
return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
|
|
|
|
(getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
|
|
|
|
!(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
bool ELFObjectFile<ELFT>::isBerkeleyData(DataRefImpl Sec) const {
|
|
|
|
const Elf_Shdr *EShdr = getSection(Sec);
|
|
|
|
return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
|
|
|
|
EShdr->sh_flags & ELF::SHF_ALLOC;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2013-08-09 00:27:13 +02:00
|
|
|
relocation_iterator
|
2013-09-27 23:47:05 +02:00
|
|
|
ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
|
2013-08-09 00:27:13 +02:00
|
|
|
DataRefImpl RelData;
|
2016-11-16 06:10:28 +01:00
|
|
|
auto SectionsOrErr = EF.sections();
|
|
|
|
if (!SectionsOrErr)
|
|
|
|
return relocation_iterator(RelocationRef());
|
|
|
|
uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
|
2013-08-09 00:27:13 +02:00
|
|
|
RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
|
|
|
|
RelData.d.b = 0;
|
|
|
|
return relocation_iterator(RelocationRef(RelData, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
relocation_iterator
|
2013-09-27 23:47:05 +02:00
|
|
|
ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
|
2013-08-09 00:27:13 +02:00
|
|
|
const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
|
2015-07-02 16:27:07 +02:00
|
|
|
relocation_iterator Begin = section_rel_begin(Sec);
|
2013-08-09 00:27:13 +02:00
|
|
|
if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
|
2015-07-02 16:27:07 +02:00
|
|
|
return Begin;
|
|
|
|
DataRefImpl RelData = Begin->getRawDataRefImpl();
|
2016-03-28 21:23:51 +02:00
|
|
|
const Elf_Shdr *RelSec = getRelSection(RelData);
|
|
|
|
|
|
|
|
// Error check sh_link here so that getRelocationSymbol can just use it.
|
2016-11-16 06:10:28 +01:00
|
|
|
auto SymSecOrErr = EF.getSection(RelSec->sh_link);
|
|
|
|
if (!SymSecOrErr)
|
|
|
|
report_fatal_error(errorToErrorCode(SymSecOrErr.takeError()).message());
|
2016-03-28 21:23:51 +02:00
|
|
|
|
|
|
|
RelData.d.b += S->sh_size / S->sh_entsize;
|
2013-08-09 00:27:13 +02:00
|
|
|
return relocation_iterator(RelocationRef(RelData, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
section_iterator
|
|
|
|
ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
|
2016-03-21 21:59:15 +01:00
|
|
|
if (EF.getHeader()->e_type != ELF::ET_REL)
|
|
|
|
return section_end();
|
|
|
|
|
2015-08-10 21:10:37 +02:00
|
|
|
const Elf_Shdr *EShdr = getSection(Sec);
|
2013-08-09 00:27:13 +02:00
|
|
|
uintX_t Type = EShdr->sh_type;
|
|
|
|
if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
|
2014-02-10 21:24:04 +01:00
|
|
|
return section_end();
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2016-11-16 06:10:28 +01:00
|
|
|
auto R = EF.getSection(EShdr->sh_info);
|
|
|
|
if (!R)
|
|
|
|
report_fatal_error(errorToErrorCode(R.takeError()).message());
|
2015-07-01 14:56:27 +02:00
|
|
|
return section_iterator(SectionRef(toDRI(*R), this));
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Relocations
|
|
|
|
template <class ELFT>
|
2014-01-30 03:49:50 +01:00
|
|
|
void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
|
2016-03-28 21:23:51 +02:00
|
|
|
++Rel.d.b;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
symbol_iterator
|
|
|
|
ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
|
|
|
|
uint32_t symbolIdx;
|
|
|
|
const Elf_Shdr *sec = getRelSection(Rel);
|
2015-06-30 01:55:05 +02:00
|
|
|
if (sec->sh_type == ELF::SHT_REL)
|
2013-08-09 00:27:13 +02:00
|
|
|
symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
|
2015-06-30 01:55:05 +02:00
|
|
|
else
|
2013-08-09 00:27:13 +02:00
|
|
|
symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
|
|
|
|
if (!symbolIdx)
|
2014-02-10 21:24:04 +01:00
|
|
|
return symbol_end();
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2016-03-28 21:23:51 +02:00
|
|
|
// FIXME: error check symbolIdx
|
2013-08-09 00:27:13 +02:00
|
|
|
DataRefImpl SymbolData;
|
2016-03-28 21:23:51 +02:00
|
|
|
SymbolData.d.a = sec->sh_link;
|
|
|
|
SymbolData.d.b = symbolIdx;
|
2013-08-09 00:27:13 +02:00
|
|
|
return symbol_iterator(SymbolRef(SymbolData, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-03-21 21:59:15 +01:00
|
|
|
uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
|
|
|
|
const Elf_Shdr *sec = getRelSection(Rel);
|
|
|
|
if (sec->sh_type == ELF::SHT_REL)
|
|
|
|
return getRel(Rel)->r_offset;
|
|
|
|
|
|
|
|
return getRela(Rel)->r_offset;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-06-30 03:53:01 +02:00
|
|
|
uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
|
2013-08-09 00:27:13 +02:00
|
|
|
const Elf_Shdr *sec = getRelSection(Rel);
|
2015-06-30 01:55:05 +02:00
|
|
|
if (sec->sh_type == ELF::SHT_REL)
|
2015-06-30 03:53:01 +02:00
|
|
|
return getRel(Rel)->getType(EF.isMips64EL());
|
2015-06-30 01:55:05 +02:00
|
|
|
else
|
2015-06-30 03:53:01 +02:00
|
|
|
return getRela(Rel)->getType(EF.isMips64EL());
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
|
|
|
|
return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-06-30 06:08:37 +02:00
|
|
|
void ELFObjectFile<ELFT>::getRelocationTypeName(
|
2013-08-09 00:27:13 +02:00
|
|
|
DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
|
2015-06-30 06:08:37 +02:00
|
|
|
uint32_t type = getRelocationType(Rel);
|
2013-08-09 00:27:13 +02:00
|
|
|
EF.getRelocationTypeName(type, Result);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2017-10-11 18:56:33 +02:00
|
|
|
Expected<int64_t>
|
2015-06-19 22:58:43 +02:00
|
|
|
ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
|
|
|
|
if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
|
2017-10-11 18:56:33 +02:00
|
|
|
return createError("Section is not SHT_RELA");
|
2015-06-19 22:58:43 +02:00
|
|
|
return (int64_t)getRela(Rel)->r_addend;
|
|
|
|
}
|
|
|
|
|
2013-08-09 00:27:13 +02:00
|
|
|
template <class ELFT>
|
|
|
|
const typename ELFObjectFile<ELFT>::Elf_Rel *
|
|
|
|
ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
|
2015-06-30 01:55:05 +02:00
|
|
|
assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
|
2016-11-03 18:37:28 +01:00
|
|
|
auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
|
2016-11-16 06:10:28 +01:00
|
|
|
if (!Ret)
|
|
|
|
report_fatal_error(errorToErrorCode(Ret.takeError()).message());
|
2016-11-03 18:37:28 +01:00
|
|
|
return *Ret;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
const typename ELFObjectFile<ELFT>::Elf_Rela *
|
|
|
|
ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
|
2015-06-30 01:55:05 +02:00
|
|
|
assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
|
2016-11-03 18:37:28 +01:00
|
|
|
auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
|
2016-11-16 06:10:28 +01:00
|
|
|
if (!Ret)
|
|
|
|
report_fatal_error(errorToErrorCode(Ret.takeError()).message());
|
2016-11-03 18:37:28 +01:00
|
|
|
return *Ret;
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2017-10-10 23:21:16 +02:00
|
|
|
Expected<ELFObjectFile<ELFT>>
|
|
|
|
ELFObjectFile<ELFT>::create(MemoryBufferRef Object) {
|
2017-10-11 00:17:49 +02:00
|
|
|
auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
|
|
|
|
if (Error E = EFOrErr.takeError())
|
|
|
|
return std::move(E);
|
|
|
|
auto EF = std::move(*EFOrErr);
|
2017-10-10 23:21:16 +02:00
|
|
|
|
2016-11-02 15:10:57 +01:00
|
|
|
auto SectionsOrErr = EF.sections();
|
2017-10-10 23:21:16 +02:00
|
|
|
if (!SectionsOrErr)
|
|
|
|
return SectionsOrErr.takeError();
|
|
|
|
|
|
|
|
const Elf_Shdr *DotDynSymSec = nullptr;
|
|
|
|
const Elf_Shdr *DotSymtabSec = nullptr;
|
|
|
|
ArrayRef<Elf_Word> ShndxTable;
|
2016-11-02 15:10:57 +01:00
|
|
|
for (const Elf_Shdr &Sec : *SectionsOrErr) {
|
2015-08-07 22:11:08 +02:00
|
|
|
switch (Sec.sh_type) {
|
|
|
|
case ELF::SHT_DYNSYM: {
|
2017-10-10 23:21:16 +02:00
|
|
|
if (DotDynSymSec)
|
|
|
|
return createError("More than one dynamic symbol table!");
|
2015-08-07 22:11:08 +02:00
|
|
|
DotDynSymSec = &Sec;
|
|
|
|
break;
|
|
|
|
}
|
2015-08-10 22:25:04 +02:00
|
|
|
case ELF::SHT_SYMTAB: {
|
2017-10-10 23:21:16 +02:00
|
|
|
if (DotSymtabSec)
|
|
|
|
return createError("More than one static symbol table!");
|
2015-08-10 22:25:04 +02:00
|
|
|
DotSymtabSec = &Sec;
|
|
|
|
break;
|
|
|
|
}
|
2015-08-10 23:29:35 +02:00
|
|
|
case ELF::SHT_SYMTAB_SHNDX: {
|
2016-11-16 06:10:28 +01:00
|
|
|
auto TableOrErr = EF.getSHNDXTable(Sec);
|
2017-10-10 23:21:16 +02:00
|
|
|
if (!TableOrErr)
|
|
|
|
return TableOrErr.takeError();
|
2015-08-10 23:29:35 +02:00
|
|
|
ShndxTable = *TableOrErr;
|
|
|
|
break;
|
|
|
|
}
|
2015-08-07 22:11:08 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-11 00:17:49 +02:00
|
|
|
return ELFObjectFile<ELFT>(Object, EF, DotDynSymSec, DotSymtabSec,
|
|
|
|
ShndxTable);
|
2015-08-07 22:11:08 +02:00
|
|
|
}
|
2017-10-10 23:21:16 +02:00
|
|
|
|
|
|
|
template <class ELFT>
|
2017-10-11 00:17:49 +02:00
|
|
|
ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
|
2017-10-10 23:21:16 +02:00
|
|
|
const Elf_Shdr *DotDynSymSec,
|
|
|
|
const Elf_Shdr *DotSymtabSec,
|
|
|
|
ArrayRef<Elf_Word> ShndxTable)
|
|
|
|
: ELFObjectFileBase(
|
|
|
|
getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
|
|
|
|
Object),
|
2017-10-11 00:17:49 +02:00
|
|
|
EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
|
|
|
|
ShndxTable(ShndxTable) {}
|
2017-10-10 23:21:16 +02:00
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other)
|
2017-10-11 00:17:49 +02:00
|
|
|
: ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
|
|
|
|
Other.DotSymtabSec, Other.ShndxTable) {}
|
2013-08-09 00:27:13 +02:00
|
|
|
|
|
|
|
template <class ELFT>
|
2016-11-22 04:38:40 +01:00
|
|
|
basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin() const {
|
2015-08-10 22:25:04 +02:00
|
|
|
DataRefImpl Sym = toDRI(DotSymtabSec, 0);
|
2015-06-29 14:38:31 +02:00
|
|
|
return basic_symbol_iterator(SymbolRef(Sym, this));
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-11-22 04:38:40 +01:00
|
|
|
basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end() const {
|
2015-08-10 22:25:04 +02:00
|
|
|
const Elf_Shdr *SymTab = DotSymtabSec;
|
2015-07-03 14:00:05 +02:00
|
|
|
if (!SymTab)
|
2016-11-22 04:38:40 +01:00
|
|
|
return symbol_begin();
|
2015-07-03 14:00:05 +02:00
|
|
|
DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
|
2015-06-29 14:38:31 +02:00
|
|
|
return basic_symbol_iterator(SymbolRef(Sym, this));
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-06-26 00:10:04 +02:00
|
|
|
elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
|
2015-08-07 22:11:08 +02:00
|
|
|
DataRefImpl Sym = toDRI(DotDynSymSec, 0);
|
2015-06-29 14:38:31 +02:00
|
|
|
return symbol_iterator(SymbolRef(Sym, this));
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-06-26 00:10:04 +02:00
|
|
|
elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
|
2015-08-07 22:11:08 +02:00
|
|
|
const Elf_Shdr *SymTab = DotDynSymSec;
|
2017-02-03 17:53:55 +01:00
|
|
|
if (!SymTab)
|
|
|
|
return dynamic_symbol_begin();
|
2015-07-03 14:00:05 +02:00
|
|
|
DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
|
|
|
|
return basic_symbol_iterator(SymbolRef(Sym, this));
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2014-02-10 21:24:04 +01:00
|
|
|
section_iterator ELFObjectFile<ELFT>::section_begin() const {
|
2016-11-16 06:10:28 +01:00
|
|
|
auto SectionsOrErr = EF.sections();
|
|
|
|
if (!SectionsOrErr)
|
|
|
|
return section_iterator(SectionRef());
|
|
|
|
return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2014-02-10 21:24:04 +01:00
|
|
|
section_iterator ELFObjectFile<ELFT>::section_end() const {
|
2016-11-16 06:10:28 +01:00
|
|
|
auto SectionsOrErr = EF.sections();
|
|
|
|
if (!SectionsOrErr)
|
|
|
|
return section_iterator(SectionRef());
|
|
|
|
return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
|
2013-08-09 00:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
|
|
|
|
return ELFT::Is64Bits ? 8 : 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
|
2014-10-06 09:06:03 +02:00
|
|
|
bool IsLittleEndian = ELFT::TargetEndianness == support::little;
|
2013-08-09 00:27:13 +02:00
|
|
|
switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
|
|
|
|
case ELF::ELFCLASS32:
|
|
|
|
switch (EF.getHeader()->e_machine) {
|
|
|
|
case ELF::EM_386:
|
|
|
|
return "ELF32-i386";
|
2015-11-04 12:21:50 +01:00
|
|
|
case ELF::EM_IAMCU:
|
|
|
|
return "ELF32-iamcu";
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::EM_X86_64:
|
|
|
|
return "ELF32-x86-64";
|
|
|
|
case ELF::EM_ARM:
|
2014-10-06 09:06:03 +02:00
|
|
|
return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
|
2015-10-23 08:05:55 +02:00
|
|
|
case ELF::EM_AVR:
|
|
|
|
return "ELF32-avr";
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::EM_HEXAGON:
|
|
|
|
return "ELF32-hexagon";
|
2016-03-01 22:21:42 +01:00
|
|
|
case ELF::EM_LANAI:
|
|
|
|
return "ELF32-lanai";
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::EM_MIPS:
|
|
|
|
return "ELF32-mips";
|
2018-11-15 13:29:43 +01:00
|
|
|
case ELF::EM_MSP430:
|
|
|
|
return "ELF32-msp430";
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::EM_PPC:
|
|
|
|
return "ELF32-ppc";
|
2016-11-01 17:59:37 +01:00
|
|
|
case ELF::EM_RISCV:
|
|
|
|
return "ELF32-riscv";
|
2014-01-26 04:21:28 +01:00
|
|
|
case ELF::EM_SPARC:
|
|
|
|
case ELF::EM_SPARC32PLUS:
|
|
|
|
return "ELF32-sparc";
|
2016-03-09 18:08:19 +01:00
|
|
|
case ELF::EM_AMDGPU:
|
|
|
|
return "ELF32-amdgpu";
|
2013-08-09 00:27:13 +02:00
|
|
|
default:
|
|
|
|
return "ELF32-unknown";
|
|
|
|
}
|
|
|
|
case ELF::ELFCLASS64:
|
|
|
|
switch (EF.getHeader()->e_machine) {
|
|
|
|
case ELF::EM_386:
|
|
|
|
return "ELF64-i386";
|
|
|
|
case ELF::EM_X86_64:
|
|
|
|
return "ELF64-x86-64";
|
|
|
|
case ELF::EM_AARCH64:
|
2014-10-06 09:06:03 +02:00
|
|
|
return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::EM_PPC64:
|
|
|
|
return "ELF64-ppc64";
|
2016-11-01 17:59:37 +01:00
|
|
|
case ELF::EM_RISCV:
|
|
|
|
return "ELF64-riscv";
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::EM_S390:
|
|
|
|
return "ELF64-s390";
|
2014-01-26 04:21:28 +01:00
|
|
|
case ELF::EM_SPARCV9:
|
|
|
|
return "ELF64-sparc";
|
2014-02-18 16:57:52 +01:00
|
|
|
case ELF::EM_MIPS:
|
|
|
|
return "ELF64-mips";
|
2016-03-09 18:08:19 +01:00
|
|
|
case ELF::EM_AMDGPU:
|
2017-10-03 22:54:07 +02:00
|
|
|
return "ELF64-amdgpu";
|
2016-07-16 00:27:55 +02:00
|
|
|
case ELF::EM_BPF:
|
|
|
|
return "ELF64-BPF";
|
2013-08-09 00:27:13 +02:00
|
|
|
default:
|
|
|
|
return "ELF64-unknown";
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// FIXME: Proper error handling.
|
|
|
|
report_fatal_error("Invalid ELFCLASS!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 23:07:03 +01:00
|
|
|
template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
|
2014-06-27 13:36:45 +02:00
|
|
|
bool IsLittleEndian = ELFT::TargetEndianness == support::little;
|
2013-08-09 00:27:13 +02:00
|
|
|
switch (EF.getHeader()->e_machine) {
|
|
|
|
case ELF::EM_386:
|
2015-11-04 12:21:50 +01:00
|
|
|
case ELF::EM_IAMCU:
|
2013-08-09 00:27:13 +02:00
|
|
|
return Triple::x86;
|
|
|
|
case ELF::EM_X86_64:
|
|
|
|
return Triple::x86_64;
|
|
|
|
case ELF::EM_AARCH64:
|
2016-12-15 23:36:53 +01:00
|
|
|
return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::EM_ARM:
|
|
|
|
return Triple::arm;
|
2015-10-23 08:05:55 +02:00
|
|
|
case ELF::EM_AVR:
|
|
|
|
return Triple::avr;
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::EM_HEXAGON:
|
|
|
|
return Triple::hexagon;
|
2016-03-01 22:21:42 +01:00
|
|
|
case ELF::EM_LANAI:
|
|
|
|
return Triple::lanai;
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::EM_MIPS:
|
2014-06-27 13:36:45 +02:00
|
|
|
switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
|
|
|
|
case ELF::ELFCLASS32:
|
|
|
|
return IsLittleEndian ? Triple::mipsel : Triple::mips;
|
|
|
|
case ELF::ELFCLASS64:
|
|
|
|
return IsLittleEndian ? Triple::mips64el : Triple::mips64;
|
|
|
|
default:
|
|
|
|
report_fatal_error("Invalid ELFCLASS!");
|
|
|
|
}
|
2018-11-15 13:29:43 +01:00
|
|
|
case ELF::EM_MSP430:
|
|
|
|
return Triple::msp430;
|
2014-10-06 08:55:55 +02:00
|
|
|
case ELF::EM_PPC:
|
|
|
|
return Triple::ppc;
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::EM_PPC64:
|
2014-06-27 13:36:45 +02:00
|
|
|
return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
|
2016-11-01 17:59:37 +01:00
|
|
|
case ELF::EM_RISCV:
|
|
|
|
switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
|
|
|
|
case ELF::ELFCLASS32:
|
|
|
|
return Triple::riscv32;
|
|
|
|
case ELF::ELFCLASS64:
|
|
|
|
return Triple::riscv64;
|
|
|
|
default:
|
|
|
|
report_fatal_error("Invalid ELFCLASS!");
|
|
|
|
}
|
2013-08-09 00:27:13 +02:00
|
|
|
case ELF::EM_S390:
|
|
|
|
return Triple::systemz;
|
2014-01-26 04:21:28 +01:00
|
|
|
|
|
|
|
case ELF::EM_SPARC:
|
|
|
|
case ELF::EM_SPARC32PLUS:
|
2015-04-29 22:30:57 +02:00
|
|
|
return IsLittleEndian ? Triple::sparcel : Triple::sparc;
|
2014-01-26 04:21:28 +01:00
|
|
|
case ELF::EM_SPARCV9:
|
|
|
|
return Triple::sparcv9;
|
|
|
|
|
2017-10-05 18:19:18 +02:00
|
|
|
case ELF::EM_AMDGPU: {
|
2017-10-03 22:54:07 +02:00
|
|
|
if (!IsLittleEndian)
|
|
|
|
return Triple::UnknownArch;
|
|
|
|
|
2018-02-16 23:33:59 +01:00
|
|
|
unsigned MACH = EF.getHeader()->e_flags & ELF::EF_AMDGPU_MACH;
|
|
|
|
if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
|
|
|
|
MACH <= ELF::EF_AMDGPU_MACH_R600_LAST)
|
2017-10-05 18:19:18 +02:00
|
|
|
return Triple::r600;
|
2018-02-16 23:33:59 +01:00
|
|
|
if (MACH >= ELF::EF_AMDGPU_MACH_AMDGCN_FIRST &&
|
|
|
|
MACH <= ELF::EF_AMDGPU_MACH_AMDGCN_LAST)
|
2017-10-05 18:19:18 +02:00
|
|
|
return Triple::amdgcn;
|
2018-02-16 23:33:59 +01:00
|
|
|
|
|
|
|
return Triple::UnknownArch;
|
2017-10-05 18:19:18 +02:00
|
|
|
}
|
2016-03-09 18:08:19 +01:00
|
|
|
|
2016-07-16 00:27:55 +02:00
|
|
|
case ELF::EM_BPF:
|
|
|
|
return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
|
|
|
|
|
2013-08-09 00:27:13 +02:00
|
|
|
default:
|
|
|
|
return Triple::UnknownArch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 17:25:03 +02:00
|
|
|
template <class ELFT>
|
|
|
|
Expected<uint64_t> ELFObjectFile<ELFT>::getStartAddress() const {
|
|
|
|
return EF.getHeader()->e_entry;
|
|
|
|
}
|
|
|
|
|
2014-08-17 19:52:10 +02:00
|
|
|
template <class ELFT>
|
2015-06-26 00:10:04 +02:00
|
|
|
ELFObjectFileBase::elf_symbol_iterator_range
|
2015-06-25 16:39:35 +02:00
|
|
|
ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
|
|
|
|
return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
|
2014-08-17 19:52:10 +02:00
|
|
|
}
|
|
|
|
|
2014-08-17 21:09:37 +02:00
|
|
|
template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
|
|
|
|
return EF.getHeader()->e_type == ELF::ET_REL;
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:38 +02:00
|
|
|
} // end namespace object
|
|
|
|
} // end namespace llvm
|
2013-08-09 00:27:13 +02:00
|
|
|
|
2016-05-10 01:11:38 +02:00
|
|
|
#endif // LLVM_OBJECT_ELFOBJECTFILE_H
|