debugger: read file type and machine type from binary

Change-Id: I3faa92ef8add807389ca3d87197e83df948f149a
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
hjk
2012-06-08 14:40:18 +02:00
committed by hjk
parent c310f1671c
commit d693df391c
3 changed files with 113 additions and 59 deletions

View File

@@ -53,23 +53,11 @@ typedef quint32 qelfword_t;
typedef quintptr qelfoff_t; typedef quintptr qelfoff_t;
typedef quintptr qelfaddr_t; typedef quintptr qelfaddr_t;
enum {
PT_NULL = 0,
PT_LOAD = 1,
PT_DYNAMIC = 2,
PT_INTERP = 3,
PT_NOTE = 4,
PT_SHLIB = 5,
PT_PHDR = 6,
PT_TLS = 7,
PT_NUM = 8
};
template <typename T> template <typename T>
T get(const unsigned char *s, ElfReader::ElfEndian endian) T get(const unsigned char *s, ElfEndian endian)
{ {
if (endian == ElfReader::ElfBigEndian) if (endian == ElfBigEndian)
return qFromBigEndian<T>(s); return qFromBigEndian<T>(s);
else else
return qFromLittleEndian<T>(s); return qFromLittleEndian<T>(s);
@@ -85,7 +73,7 @@ public:
qelfoff_t size; qelfoff_t size;
}; };
static void parseSectionHeader(const uchar *data, RawElfSectionHeader *sh, ElfReader::ElfEndian endian) static void parseSectionHeader(const uchar *data, RawElfSectionHeader *sh, ElfEndian endian)
{ {
sh->name = get<qelfword_t>(data, endian); sh->name = get<qelfword_t>(data, endian);
data += sizeof(qelfword_t); // sh_name data += sizeof(qelfword_t); // sh_name
@@ -109,7 +97,7 @@ public:
qelfword_t memsz; qelfword_t memsz;
}; };
static void parseProgramHeader(const uchar *data, RawElfProgramHeader *sh, ElfReader::ElfEndian endian) static void parseProgramHeader(const uchar *data, RawElfProgramHeader *sh, ElfEndian endian)
{ {
sh->type = get<qelfword_t>(data, endian); sh->type = get<qelfword_t>(data, endian);
data += sizeof(qelfword_t); // p_type data += sizeof(qelfword_t); // p_type
@@ -134,11 +122,11 @@ public:
return false; return false;
fdlen = file.size(); fdlen = file.size();
start = file.map(0, fdlen); ustart = file.map(0, fdlen);
if (start == 0) { if (ustart == 0) {
// Try reading the data into memory instead. // Try reading the data into memory instead.
raw = file.readAll(); raw = file.readAll();
start = (uchar *)raw.constData(); start = raw.constData();
fdlen = raw.size(); fdlen = raw.size();
} }
return true; return true;
@@ -147,7 +135,7 @@ public:
public: public:
QFile file; QFile file;
QByteArray raw; QByteArray raw;
uchar *start; union { const char *start; const uchar *ustart; };
quint64 fdlen; quint64 fdlen;
}; };
@@ -181,7 +169,7 @@ ElfReader::Result ElfReader::readIt()
return NotElf; return NotElf;
} }
const uchar *data = mapper.start; const uchar *data = mapper.ustart;
if (strncmp((const char *)data, "\177ELF", 4) != 0) { if (strncmp((const char *)data, "\177ELF", 4) != 0) {
m_errorString = QLibrary::tr("'%1' is not an ELF object") m_errorString = QLibrary::tr("'%1' is not an ELF object")
.arg(m_binary); .arg(m_binary);
@@ -194,6 +182,7 @@ ElfReader::Result ElfReader::readIt()
.arg(m_binary).arg(QLatin1String("odd cpu architecture")); .arg(m_binary).arg(QLatin1String("odd cpu architecture"));
return Corrupt; return Corrupt;
} }
m_elfData.elfclass = (data[4] == 1 ? Elf_ELFCLASS32 : Elf_ELFCLASS64);
// int bits = (data[4] << 5); // int bits = (data[4] << 5);
// If you remove this check to read ELF objects of a different arch, // If you remove this check to read ELF objects of a different arch,
@@ -213,22 +202,26 @@ ElfReader::Result ElfReader::readIt()
.arg(m_binary).arg(QLatin1String("odd endianess")); .arg(m_binary).arg(QLatin1String("odd endianess"));
return Corrupt; return Corrupt;
} }
m_endian = (data[5] == 1 ? ElfLittleEndian : ElfBigEndian); m_elfData.endian = (data[5] == 1 ? ElfLittleEndian : ElfBigEndian);
data += 16 // e_ident data += 16; // e_ident
+ sizeof(qelfhalf_t) // e_type m_elfData.elftype = ElfType(get<qelfhalf_t>(data, m_elfData.endian));
+ sizeof(qelfhalf_t) // e_machine data += sizeof(qelfhalf_t); // e_type
+ sizeof(qelfword_t) // e_version
+ sizeof(qelfaddr_t); // e_entry
qelfoff_t e_phoff = get<qelfoff_t>(data, m_endian); m_elfData.elfmachine = ElfMachine(get<qelfhalf_t>(data, m_elfData.endian));
data += sizeof(qelfhalf_t); // e_machine
data += sizeof(qelfword_t); // e_version
data += sizeof(qelfaddr_t); // e_entry
qelfoff_t e_phoff = get<qelfoff_t>(data, m_elfData.endian);
data += sizeof(qelfoff_t); // e_phoff data += sizeof(qelfoff_t); // e_phoff
qelfoff_t e_shoff = get<qelfoff_t>(data, m_endian); qelfoff_t e_shoff = get<qelfoff_t>(data, m_elfData.endian);
data += sizeof(qelfoff_t) // e_shoff data += sizeof(qelfoff_t); // e_shoff
+ sizeof(qelfword_t); // e_flags data += sizeof(qelfword_t); // e_flags
qelfhalf_t e_shsize = get<qelfhalf_t>(data, m_endian); qelfhalf_t e_shsize = get<qelfhalf_t>(data, m_elfData.endian);
data += sizeof(qelfhalf_t); // e_ehsize data += sizeof(qelfhalf_t); // e_ehsize
if (e_shsize > fdlen) { if (e_shsize > fdlen) {
@@ -237,13 +230,13 @@ ElfReader::Result ElfReader::readIt()
return Corrupt; return Corrupt;
} }
qelfhalf_t e_phentsize = get<qelfhalf_t>(data, m_endian); qelfhalf_t e_phentsize = get<qelfhalf_t>(data, m_elfData.endian);
data += sizeof(qelfhalf_t); // e_phentsize data += sizeof(qelfhalf_t); // e_phentsize
qelfhalf_t e_phnum = get<qelfhalf_t>(data, m_endian); qelfhalf_t e_phnum = get<qelfhalf_t>(data, m_elfData.endian);
data += sizeof(qelfhalf_t); // e_phnum data += sizeof(qelfhalf_t); // e_phnum
qelfhalf_t e_shentsize = get<qelfhalf_t>(data, m_endian); qelfhalf_t e_shentsize = get<qelfhalf_t>(data, m_elfData.endian);
data += sizeof(qelfhalf_t); // e_shentsize data += sizeof(qelfhalf_t); // e_shentsize
if (e_shentsize % 4) { if (e_shentsize % 4) {
@@ -252,9 +245,9 @@ ElfReader::Result ElfReader::readIt()
return Corrupt; return Corrupt;
} }
qelfhalf_t e_shnum = get<qelfhalf_t>(data, m_endian); qelfhalf_t e_shnum = get<qelfhalf_t>(data, m_elfData.endian);
data += sizeof(qelfhalf_t); // e_shnum data += sizeof(qelfhalf_t); // e_shnum
qelfhalf_t e_shtrndx = get<qelfhalf_t>(data, m_endian); qelfhalf_t e_shtrndx = get<qelfhalf_t>(data, m_elfData.endian);
data += sizeof(qelfhalf_t); // e_shtrndx data += sizeof(qelfhalf_t); // e_shtrndx
if (quint32(e_shnum * e_shentsize) > fdlen) { if (quint32(e_shnum * e_shentsize) > fdlen) {
@@ -265,7 +258,6 @@ ElfReader::Result ElfReader::readIt()
return Corrupt; return Corrupt;
} }
RawElfSectionHeader strtab;
qulonglong soff = e_shoff + e_shentsize * (e_shtrndx); qulonglong soff = e_shoff + e_shentsize * (e_shtrndx);
// if ((soff + e_shentsize) > fdlen || soff % 4 || soff == 0) { // if ((soff + e_shentsize) > fdlen || soff % 4 || soff == 0) {
@@ -277,7 +269,8 @@ ElfReader::Result ElfReader::readIt()
// } // }
if (e_shoff) { if (e_shoff) {
parseSectionHeader(mapper.start + soff, &strtab, m_endian); RawElfSectionHeader strtab;
parseSectionHeader(mapper.ustart + soff, &strtab, m_elfData.endian);
const int stringTableFileOffset = strtab.offset; const int stringTableFileOffset = strtab.offset;
if (quint32(stringTableFileOffset + e_shentsize) >= fdlen if (quint32(stringTableFileOffset + e_shentsize) >= fdlen
@@ -289,10 +282,10 @@ ElfReader::Result ElfReader::readIt()
return Corrupt; return Corrupt;
} }
const uchar *s = mapper.start + e_shoff; const uchar *s = mapper.ustart + e_shoff;
for (int i = 0; i < e_shnum; ++i) { for (int i = 0; i < e_shnum; ++i) {
RawElfSectionHeader sh; RawElfSectionHeader sh;
parseSectionHeader(s, &sh, m_endian); parseSectionHeader(s, &sh, m_elfData.endian);
if (sh.name == 0) { if (sh.name == 0) {
s += e_shentsize; s += e_shentsize;
continue; continue;
@@ -307,7 +300,7 @@ ElfReader::Result ElfReader::readIt()
} }
ElfSectionHeader header; ElfSectionHeader header;
header.name = ((const char *)mapper.start) + stringTableFileOffset + sh.name; header.name = mapper.start + stringTableFileOffset + sh.name;
header.index = sh.name; header.index = sh.name;
header.offset = sh.offset; header.offset = sh.offset;
header.size = sh.size; header.size = sh.size;
@@ -317,14 +310,13 @@ ElfReader::Result ElfReader::readIt()
} else if (header.name == ".debug_info") { } else if (header.name == ".debug_info") {
m_elfData.symbolsType = PlainSymbols; m_elfData.symbolsType = PlainSymbols;
} else if (header.name == ".gnu_debuglink") { } else if (header.name == ".gnu_debuglink") {
m_elfData.debugLink = QByteArray((const char *)mapper.start m_elfData.debugLink = QByteArray(mapper.start + header.offset);
+ header.offset);
m_elfData.symbolsType = LinkedSymbols; m_elfData.symbolsType = LinkedSymbols;
} else if (header.name == ".note.gnu.build-id") { } else if (header.name == ".note.gnu.build-id") {
m_elfData.symbolsType = BuildIdSymbols; m_elfData.symbolsType = BuildIdSymbols;
if (header.size > 16) if (header.size > 16)
m_elfData.buildId = QByteArray((const char *)mapper.start m_elfData.buildId = QByteArray(mapper.start + header.offset + 16,
+ header.offset + 16, header.size - 16).toHex(); header.size - 16).toHex();
} }
m_elfData.sectionHeaders.append(header); m_elfData.sectionHeaders.append(header);
@@ -333,11 +325,11 @@ ElfReader::Result ElfReader::readIt()
} }
if (e_phoff) { if (e_phoff) {
const uchar *s = mapper.start + e_phoff; const uchar *s = mapper.ustart + e_phoff;
for (int i = 0; i < e_phnum; ++i) { for (int i = 0; i < e_phnum; ++i) {
RawElfProgramHeader ph; RawElfProgramHeader ph;
parseProgramHeader(s, &ph, m_endian); parseProgramHeader(s, &ph, m_elfData.endian);
ElfProgramHeader header; ElfProgramHeader header;
header.type = ph.type; header.type = ph.type;
@@ -364,27 +356,37 @@ QByteArray ElfReader::readSection(const QByteArray &name)
return QByteArray(); return QByteArray();
const ElfSectionHeader &section = m_elfData.sectionHeaders.at(i); const ElfSectionHeader &section = m_elfData.sectionHeaders.at(i);
return QByteArray((const char *)mapper.start + section.offset, section.size); return QByteArray((const char *)mapper.ustart + section.offset, section.size);
} }
QByteArray ElfReader::readCoreName() QByteArray ElfReader::readCoreName(bool *isCore)
{ {
*isCore = false;
readIt(); readIt();
ElfMapper mapper(this); ElfMapper mapper(this);
if (!mapper.map()) if (!mapper.map())
return QByteArray(); return QByteArray();
*isCore = (m_elfData.elftype == Elf_ET_CORE);
if (!*isCore)
return QByteArray();
for (int i = 0, n = m_elfData.sectionHeaders.size(); i != n; ++i) for (int i = 0, n = m_elfData.sectionHeaders.size(); i != n; ++i)
if (m_elfData.sectionHeaders.at(i).type == PT_NOTE) { if (m_elfData.sectionHeaders.at(i).type == Elf_PT_NOTE) {
const ElfSectionHeader &header = m_elfData.sectionHeaders.at(i); const ElfSectionHeader &header = m_elfData.sectionHeaders.at(i);
return QByteArray((const char *)mapper.start + header.offset + 0x40); const char *s = mapper.start + header.offset + 0x30; // short name
const char *t = s + strlen(s) + 1; // command line
int n = strlen(t);
return QByteArray(t, n);
} }
for (int i = 0, n = m_elfData.programHeaders.size(); i != n; ++i) for (int i = 0, n = m_elfData.programHeaders.size(); i != n; ++i)
if (m_elfData.programHeaders.at(i).type == PT_NOTE) { if (m_elfData.programHeaders.at(i).type == Elf_PT_NOTE) {
const ElfProgramHeader &header = m_elfData.programHeaders.at(i); const ElfProgramHeader &header = m_elfData.programHeaders.at(i);
QByteArray ba((const char *)mapper.start + header.offset + 0xec); const char *s = mapper.start + header.offset;
QByteArray ba(s + 0xec);
return ba; return ba;
} }

View File

@@ -50,6 +50,47 @@
namespace Utils { namespace Utils {
enum ElfProgramHeaderType
{
Elf_PT_NULL = 0,
Elf_PT_LOAD = 1,
Elf_PT_DYNAMIC = 2,
Elf_PT_INTERP = 3,
Elf_PT_NOTE = 4,
Elf_PT_SHLIB = 5,
Elf_PT_PHDR = 6,
Elf_PT_TLS = 7,
Elf_PT_NUM = 8
};
enum ElfEndian
{
ElfLittleEndian = 0,
ElfBigEndian = 1
};
enum ElfClass
{
Elf_ELFCLASS32 = 1,
Elf_ELFCLASS64 = 2
};
enum ElfType
{
Elf_ET_NONE = 0,
Elf_ET_REL = 1,
Elf_ET_EXEC = 2,
Elf_ET_DYN = 3,
Elf_ET_CORE = 4
};
enum ElfMachine
{
Elf_EM_386 = 3,
Elf_EM_ARM = 40,
Elf_EM_X86_64 = 62
};
enum DebugSymbolsType enum DebugSymbolsType
{ {
UnknownSymbols = 0, // Unknown. UnknownSymbols = 0, // Unknown.
@@ -87,6 +128,10 @@ public:
int indexOf(const QByteArray &name) const; int indexOf(const QByteArray &name) const;
public: public:
ElfEndian endian;
ElfType elftype;
ElfMachine elfmachine;
ElfClass elfclass;
QByteArray debugLink; QByteArray debugLink;
QByteArray buildId; QByteArray buildId;
DebugSymbolsType symbolsType; DebugSymbolsType symbolsType;
@@ -100,11 +145,10 @@ public:
explicit ElfReader(const QString &binary); explicit ElfReader(const QString &binary);
enum Result { Ok, NotElf, Corrupt }; enum Result { Ok, NotElf, Corrupt };
enum ElfEndian { ElfLittleEndian = 0, ElfBigEndian = 1 };
ElfData readHeaders(); ElfData readHeaders();
QByteArray readSection(const QByteArray &sectionName); QByteArray readSection(const QByteArray &sectionName);
QString errorString() const { return m_errorString; } QString errorString() const { return m_errorString; }
QByteArray readCoreName(); QByteArray readCoreName(bool *isCore);
private: private:
friend class ElfMapper; friend class ElfMapper;
@@ -112,7 +156,6 @@ private:
QString m_binary; QString m_binary;
QString m_errorString; QString m_errorString;
ElfEndian m_endian;
ElfData m_elfData; ElfData m_elfData;
}; };

View File

@@ -95,7 +95,16 @@ void CoreGdbAdapter::continueAdapterStart()
if (m_executable.isEmpty()) { if (m_executable.isEmpty()) {
// Read executable from core. // Read executable from core.
ElfReader reader(coreFileName()); ElfReader reader(coreFileName());
m_executable = reader.readCoreName(); bool isCore = false;
m_executable = reader.readCoreName(&isCore);
if (!isCore) {
showMessageBox(QMessageBox::Warning,
tr("Error Loading Core File"),
tr("The specified file does not appear to be a core file."));
m_engine->notifyEngineSetupFailed();
return;
}
// Strip off command line arguments. FIXME: make robust. // Strip off command line arguments. FIXME: make robust.
int idx = m_executable.indexOf(QLatin1Char(' ')); int idx = m_executable.indexOf(QLatin1Char(' '));
@@ -104,7 +113,7 @@ void CoreGdbAdapter::continueAdapterStart()
if (m_executable.isEmpty()) { if (m_executable.isEmpty()) {
showMessageBox(QMessageBox::Warning, showMessageBox(QMessageBox::Warning,
tr("Error Loading Symbols"), tr("Error Loading Symbols"),
tr("No executable to load symbols from specified.")); tr("No executable to load symbols from specified core."));
m_engine->notifyEngineSetupFailed(); m_engine->notifyEngineSetupFailed();
return; return;
} }