#include "loadelf.h"
#include <stdio.h>
#include <string.h> // strcmp
typedef unsigned __int16 word;
typedef unsigned __int32 dword;
struct ELFHDR {
union{
struct{
byte magic[4]; // "\x7FELF"
byte eclass; // 1 for 32-bit
byte data; // 1 for little endian, 2 for big endian
byte version; // (1)
byte pad[9]; // (0xFF, then 0x00)
};
dword dw[4];
}ident;
word type;
enum{
T_REL = 1, // object file
T_EXEC = 2, // executable file
T_DYN = 3, // shared object
T_CORE = 4, // core file
};
word machine;
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,
};
dword version; // (1) [following values seen for MSP430]
dword entry; // (00004400) start address
dword phoff; // (00000034) program header table follows this struct
dword shoff; // (0000AF78) section header table
dword flags; // (00000036) (machine dependent)
word ehsize; // (0034) this size
word phentsize; // (0020) header table entry size (32 byte)
word phnum; // (0002) header table entries (2)
word shentsize; // (0028) section header entry size (40 byte)
word shnum; // (000D) section header entries (13)
word shstrndx; // (000A) index of section name string table (10)
};
struct Proghdr32 {
dword type, // must be PT_LOAD == 1
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
};
bool loadelf(const byte*fdata,unsigned fsize) {
if (fsize<32) return false;
const ELFHDR&e=*reinterpret_cast<const ELFHDR*>(fdata);
if (e.ident.dw[0]!=0x464C457F) return false; // 'FLE\x7F'
for (int i=0; i<e.phnum; i++) {
const Proghdr32&ph=*reinterpret_cast<const Proghdr32*>(fdata+e.phoff+i*e.phentsize);
if (ph.type==1 && ph.filesz) {
store_buffer(fdata+ph.offset,ph.filesz,ph.paddr);
}
}
return true;
}
Detected encoding: ASCII (7 bit) | 2
|