#pragma once
#include "types.h"
struct ELFFile {
enum{
MAGIC = 0x464C457F, // 7F "ELF"
};
// ELF file start with this struct.
// Size of HDR may vary, following structures have pointers inside HDR
struct HDR {
uint32 magic; // 7F "ELF"
byte eclass; // 1 for 32-bit
byte data; // 1 for little endian, 2 for big endian
byte ver; // (1)
byte pad; // (0xFF)
uint32 pad2[2];// 0
uint16 type; // 2
enum{
T_REL = 1, // object file
T_EXEC = 2, // executable file
T_DYN = 3, // shared object
T_CORE = 4, // core file
};
uint16 machine; // 94
enum{
M_386 = 3,
M_68K = 4,
M_PPC = 20,
M_68HC11 = 70,
M_AVR = 83,
M_XTENSA = 94,
M_MSP430 =105,
M_BLACKFIN =106,
M_C166 =116,
M_8051 =165,
M_AVR32 =185,
M_STM8 =186,
M_MCHP_PIC =204,
};
uint32 version, // (1) [following values seen for MSP430]
entry, // (00004400) start address
phoff, // (00000034) program header table (follows this struct)
shoff, // (0000AF78) section header table
flags; // (00000036) (machine dependent)
uint16 ehsize, // (0034) this size
phentsize, // (0020) header table entry size (32 byte)
phnum, // (0002) header table entries (2)
shentsize, // (0028) section header entry size (40 byte)
shnum, // (000D) section header entries (13)
shstrndx; // (000A) index of section name string table (10)
};
// Loaders (i.e. the OS) use the Program Header and ignore segment names.
// Size of ProgHdr32 may vary, use HDR.phentsize
struct ProgHdr32 { // size = 32
uint32 type, // Headers with type != PT_LOAD (=1) are ignored
offset, // offset of data bits in ELF file
vaddr, // not needed (VMA)
paddr, // offset where to load into flash (LMA)
filesz, // size of section in file (0 for .bss)
memsz, // not needed
flags, // not needed
align; // not needed
};
// Parsers (i.e. the linker, objcopy etc.) use the Section Header
// Size of SecHdr32 may vary, use HDR.shentsize
struct SecHdr32{ // size = 40
uint32 name,
type,
flags,
addr,
offset,
size,
link,
info,
addralign,
entsize;
};
const char*name;
struct SYM{
const char*name;
uint32 value;
}*symbols;
uint32 nsym;
ELFFile(const char*n);
static bool valid(byte*fdata,uint32 fsize);
static bool parse(Dumpfile&,cb2_t cb,void*cbd,uint32*sadr);
static SecHdr32*findSection(byte*fdata,const char*name);
static bool is_lx106();
void add_sym(const char*n,uint32 v);
void _fetch_symbols();
uint32 get_symbol_addr(const char*sym);
uint32 get_entry_point();
static bool load_section_cb(void*,const Dumpfile&);
bool load_section(const void*section,const void*&data);
~ELFFile();
};
Detected encoding: ASCII (7 bit) | 2
|