Skip to content

Commit

Permalink
Merge pull request #182 from rafzi/pr3_arch_from_elf
Browse files Browse the repository at this point in the history
Determine the architecture of an ELF by looking at the file header in…
  • Loading branch information
wrwilliams committed Nov 11, 2016
2 parents 1f08ea8 + c368836 commit b7bc63f
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 102 deletions.
1 change: 1 addition & 0 deletions common/h/SymReader.h
Expand Up @@ -91,6 +91,7 @@ class COMMON_EXPORT SymReader
virtual unsigned getAddressWidth() = 0;
virtual bool getABIVersion(int &major, int &minor) const = 0;
virtual bool isBigEndianDataEncoding() const = 0;
virtual Architecture getArchitecture() const = 0;

virtual unsigned numSegments() = 0;
virtual bool getSegment(unsigned num, SymSegment &reg) = 0;
Expand Down
3 changes: 3 additions & 0 deletions elf/h/Elf_X.h
Expand Up @@ -36,6 +36,7 @@
#include <map>
#include <vector>
#include "util.h"
#include "dyn_regs.h"

namespace Dyninst {

Expand Down Expand Up @@ -117,6 +118,8 @@ class DYNELF_EXPORT Elf_X {

bool findDebugFile(std::string origfilename, std::string &output_name, char* &output_buffer, unsigned long &output_buffer_size);

Dyninst::Architecture getArch() const;

protected:
Elf *elf;
Elf32_Ehdr *ehdr32;
Expand Down
33 changes: 33 additions & 0 deletions elf/src/Elf_X.C
Expand Up @@ -1742,6 +1742,39 @@ bool Elf_X::findDebugFile(std::string origfilename, string &output_name, char* &
return false;
}

// Add definitions that may not be in all elf.h files
#if !defined(EM_K10M)
#define EM_K10M 180
#endif
#if !defined(EM_L10M)
#define EM_L10M 181
#endif
#if !defined(EM_AARCH64)
#define EM_AARCH64 183
#endif
Dyninst::Architecture Elf_X::getArch() const
{
switch(e_machine())
{
case EM_PPC:
return Dyninst::Arch_ppc32;
case EM_PPC64:
return Dyninst::Arch_ppc64;
case EM_386:
return Dyninst::Arch_x86;
case EM_X86_64:
case EM_K10M:
case EM_L10M:
return Dyninst::Arch_x86_64;
case EM_ARM:
return Dyninst::Arch_aarch32;
case EM_AARCH64:
return Dyninst::Arch_aarch64;
default:
return Dyninst::Arch_none;
}
}

// ------------------------------------------------------------------------
// Class Elf_X_Nhdr simulates the Elf(32|64)_Nhdr structure.
Elf_X_Nhdr::Elf_X_Nhdr()
Expand Down
32 changes: 2 additions & 30 deletions parseAPI/src/SymLiteCodeSource.C
Expand Up @@ -141,21 +141,7 @@ SymReaderCodeRegion::getAddressWidth() const
Architecture
SymReaderCodeRegion::getArch() const
{
#if defined(arch_power)
if(getAddressWidth() == 8)
return Arch_ppc64;
else
return Arch_ppc32;
#elif defined(arch_x86) || defined(arch_x86_64)
if(getAddressWidth() == 8)
return Arch_x86_64;
else
return Arch_x86;
#elif defined(arch_aarch64)
return Arch_aarch64;
#else
return Arch_none;
#endif
return _symtab->getArchitecture();
}

bool
Expand Down Expand Up @@ -473,21 +459,7 @@ SymReaderCodeSource::getAddressWidth() const
Architecture
SymReaderCodeSource::getArch() const
{
#if defined(arch_power)
if(getAddressWidth() == 8)
return Arch_ppc64;
else
return Arch_ppc32;
#elif defined(arch_x86) || defined(arch_x86_64)
if(getAddressWidth() == 8)
return Arch_x86_64;
else
return Arch_x86;
#elif defined(arch_aarch64)
return Arch_aarch64;
#else
return Arch_none;
#endif
return _symtab->getArchitecture();
}

bool
Expand Down
32 changes: 2 additions & 30 deletions parseAPI/src/SymtabCodeSource.C
Expand Up @@ -146,21 +146,7 @@ SymtabCodeRegion::getAddressWidth() const
Architecture
SymtabCodeRegion::getArch() const
{
#if defined(arch_power)
if(getAddressWidth() == 8)
return Arch_ppc64;
else
return Arch_ppc32;
#elif defined(arch_x86) || defined(arch_x86_64)
if(getAddressWidth() == 8)
return Arch_x86_64;
else
return Arch_x86;
#elif defined(arch_aarch64)
return Arch_aarch64;
#else
return Arch_none;
#endif
return _symtab->getArchitecture();
}

bool
Expand Down Expand Up @@ -653,21 +639,7 @@ SymtabCodeSource::getAddressWidth() const
Architecture
SymtabCodeSource::getArch() const
{
#if defined(arch_power)
if(getAddressWidth() == 8)
return Arch_ppc64;
else
return Arch_ppc32;
#elif defined(arch_x86) || defined(arch_x86_64)
if(getAddressWidth() == 8)
return Arch_x86_64;
else
return Arch_x86;
#elif defined(arch_aarch64)
return Arch_aarch64;
#else
return Arch_none;
#endif
return _symtab->getArchitecture();
}

bool
Expand Down
1 change: 1 addition & 0 deletions symlite/h/SymLite-elf.h
Expand Up @@ -88,6 +88,7 @@ class SYMLITE_EXPORT SymElf : public Dyninst::SymReader
virtual unsigned getAddressWidth();
virtual bool getABIVersion(int &major, int &minor) const;
virtual bool isBigEndianDataEncoding() const;
virtual Architecture getArchitecture() const;

virtual unsigned long getSymbolSize(const Symbol_t &sym);
virtual Section_t getSectionByName(std::string name);
Expand Down
5 changes: 5 additions & 0 deletions symlite/src/SymLite-elf.C
Expand Up @@ -311,6 +311,11 @@ bool SymElf::isBigEndianDataEncoding() const
return (elf->e_endian() != 0);
}

Architecture SymElf::getArchitecture() const
{
return elf->getArch();
}

unsigned long SymElf::getSymbolSize(const Symbol_t &sym)
{
GET_SYMBOL(sym, shdr, symbol, name, idx);
Expand Down
6 changes: 4 additions & 2 deletions symtabAPI/h/Symtab.h
Expand Up @@ -235,7 +235,7 @@ class SYMTAB_EXPORT Symtab : public LookupInterface,
bool isExec() const;
bool isStripped();
ObjectType getObjectType() const;
Dyninst::Architecture getArchitecture();
Dyninst::Architecture getArchitecture() const;
bool isCode(const Offset where) const;
bool isData(const Offset where) const;
bool isValidOffset(const Offset where) const;
Expand Down Expand Up @@ -604,7 +604,9 @@ class SYMTAB_EXPORT Symtab : public LookupInterface,
//Don't use obj_private, use getObject() instead.
public:
Object *getObject();
ModRangeLookup* mod_lookup();
const Object *getObject() const;
ModRangeLookup* mod_lookup();

private:
Object *obj_private;

Expand Down
1 change: 1 addition & 0 deletions symtabAPI/h/SymtabReader.h
Expand Up @@ -80,6 +80,7 @@ class SYMTAB_EXPORT SymtabReader : public SymReader {
virtual unsigned getAddressWidth();
virtual bool isBigEndianDataEncoding() const;
virtual bool getABIVersion(int &major, int &minor) const;
virtual Architecture getArchitecture() const;

virtual unsigned numSegments();
virtual bool getSegment(unsigned num, SymSegment &seg);
Expand Down
38 changes: 4 additions & 34 deletions symtabAPI/src/Object-elf.C
Expand Up @@ -1105,9 +1105,8 @@ bool Object::get_relocation_entries( Elf_X_Shdr *&rel_plt_scnp,

} else if (plt_entry_size_ == 16) {
// New style secure PLT
Region *plt = NULL, *dynamic = NULL,
Region *plt = NULL, *relplt = NULL, *dynamic = NULL,
*got = NULL, *glink = NULL;
// Region *relplt = NULL;
unsigned int glink_addr = 0;
unsigned int stub_addr = 0;

Expand All @@ -1117,7 +1116,7 @@ bool Object::get_relocation_entries( Elf_X_Shdr *&rel_plt_scnp,
for (unsigned iter = 0; iter < regions_.size(); ++iter) {
std::string name = regions_[iter]->getRegionName();
if (name == PLT_NAME) plt = regions_[iter];
// else if (name == REL_PLT_NAME) relplt = regions_[iter];
else if (name == REL_PLT_NAME) relplt = regions_[iter];
else if (name == DYNAMIC_NAME) dynamic = regions_[iter];
else if (name == GOT_NAME) got = regions_[iter];
}
Expand Down Expand Up @@ -5118,38 +5117,9 @@ bool Object::getTruncateLinePaths()
return truncateLineFilenames;
}

// Add definitions that may not be in all elf.h files
#if !defined(EM_K10M)
#define EM_K10M 180
#endif
#if !defined(EM_L10M)
#define EM_L10M 181
#endif
#if !defined(EM_AARCH64)
#define EM_AARCH64 183
#endif

Dyninst::Architecture Object::getArch()
Dyninst::Architecture Object::getArch() const
{
switch(elfHdr->e_machine())
{
case EM_PPC:
return Dyninst::Arch_ppc32;
case EM_PPC64:
return Dyninst::Arch_ppc64;
case EM_386:
return Dyninst::Arch_x86;
case EM_X86_64:
case EM_K10M:
case EM_L10M:
return Dyninst::Arch_x86_64;
case EM_ARM:
return Dyninst::Arch_aarch32;
case EM_AARCH64:
return Dyninst::Arch_aarch64;
default:
return Dyninst::Arch_none;
}
return elfHdr->getArch();
}

bool Object::getABIVersion(int &major, int &minor) const
Expand Down
2 changes: 1 addition & 1 deletion symtabAPI/src/Object-elf.h
Expand Up @@ -349,7 +349,7 @@ class Object;
return false;
}

Dyninst::Architecture getArch();
Dyninst::Architecture getArch() const;
bool isBigEndianDataEncoding() const;
bool getABIVersion(int &major, int &minor) const;
bool is_offset_in_plt(Offset offset) const;
Expand Down
12 changes: 10 additions & 2 deletions symtabAPI/src/Object-nt.C
Expand Up @@ -2351,9 +2351,17 @@ void Object::insertPrereqLibrary(std::string lib)
getRegionPermissions() == RP_RWX);
}

Dyninst::Architecture Object::getArch()
Dyninst::Architecture Object::getArch() const
{
return Dyninst::Arch_x86;
switch (peHdr->FileHeader.Machine)
{
case IMAGE_FILE_MACHINE_I386:
return Dyninst::Arch_x86;
case IMAGE_FILE_MACHINE_AMD64:
return Dyninst::Arch_x86_64;
default:
return Dyninst::Arch_none;
}
}

/*
Expand Down
2 changes: 1 addition & 1 deletion symtabAPI/src/Object-nt.h
Expand Up @@ -202,7 +202,7 @@ class Object : public AObject
SYMTAB_EXPORT const char *interpreter_name() const { return NULL; }
SYMTAB_EXPORT dyn_hash_map <std::string, LineInformation> &getLineInfo();
SYMTAB_EXPORT void parseTypeInfo();
SYMTAB_EXPORT virtual Dyninst::Architecture getArch();
SYMTAB_EXPORT virtual Dyninst::Architecture getArch() const;
SYMTAB_EXPORT void ParseGlobalSymbol(PSYMBOL_INFO pSymInfo);
SYMTAB_EXPORT const std::vector<Offset> &getPossibleMains() const { return possible_mains; }
SYMTAB_EXPORT void getModuleLanguageInfo(dyn_hash_map<std::string, supportedLanguages> *mod_langs);
Expand Down
2 changes: 1 addition & 1 deletion symtabAPI/src/Object.h
Expand Up @@ -128,7 +128,7 @@ class AObject {
Dyninst::MachRegisterVal & /*reg_result*/,
Dyninst::SymtabAPI::MemRegReader * /*reader*/) {return false;}

SYMTAB_EXPORT virtual Dyninst::Architecture getArch() { return Arch_none; };
SYMTAB_EXPORT virtual Dyninst::Architecture getArch() const { return Arch_none; };
SYMTAB_EXPORT const std::string findModuleForSym(Symbol *sym);
SYMTAB_EXPORT void setModuleForOffset(Offset sym_off, std::string module);
SYMTAB_EXPORT void clearSymsToMods();
Expand Down
7 changes: 6 additions & 1 deletion symtabAPI/src/Symtab.C
Expand Up @@ -2847,7 +2847,7 @@ SYMTAB_EXPORT ObjectType Symtab::getObjectType() const
return object_type_;
}

SYMTAB_EXPORT Dyninst::Architecture Symtab::getArchitecture()
SYMTAB_EXPORT Dyninst::Architecture Symtab::getArchitecture() const
{
return getObject()->getArch();
}
Expand Down Expand Up @@ -3300,6 +3300,11 @@ Object *Symtab::getObject()
//return obj_private;
}

const Object *Symtab::getObject() const
{
return obj_private;
}

void Symtab::parseTypesNow()
{
if (isTypeInfoValid_)
Expand Down
5 changes: 5 additions & 0 deletions symtabAPI/src/SymtabReader.C
Expand Up @@ -141,6 +141,11 @@ bool SymtabReader::isBigEndianDataEncoding() const
return symtab->isBigEndianDataEncoding();
}

Architecture SymtabReader::getArchitecture() const
{
return symtab->getArchitecture();
}

unsigned SymtabReader::numSegments()
{
buildSegments();
Expand Down

0 comments on commit b7bc63f

Please sign in to comment.