Removed static data from elf32.

This commit is contained in:
Daniel Beer 2010-04-30 22:09:57 +12:00
parent 0cd82ffc48
commit 53fb83aae7
1 changed files with 75 additions and 81 deletions

156
elf32.c
View File

@ -31,51 +31,52 @@ static const u_int8_t elf32_id[] = {
#define MAX_PHDRS 32 #define MAX_PHDRS 32
#define MAX_SHDRS 32 #define MAX_SHDRS 32
static Elf32_Ehdr file_ehdr; struct elf32_info {
static Elf32_Phdr file_phdrs[MAX_PHDRS]; Elf32_Ehdr file_ehdr;
static Elf32_Shdr file_shdrs[MAX_SHDRS]; Elf32_Phdr file_phdrs[MAX_PHDRS];
Elf32_Shdr file_shdrs[MAX_SHDRS];
static char *string_tab; char *string_tab;
static int string_len; int string_len;
};
static int read_ehdr(FILE *in) static int read_ehdr(struct elf32_info *info, FILE *in)
{ {
int i;
/* Read and check the ELF header */ /* Read and check the ELF header */
rewind(in); rewind(in);
if (fread(&file_ehdr, sizeof(file_ehdr), 1, in) < 0) { if (fread(&info->file_ehdr, sizeof(info->file_ehdr), 1, in) < 0) {
perror("elf32: couldn't read ELF header"); perror("elf32: couldn't read ELF header");
return -1; return -1;
} }
for (i = 0; i < sizeof(elf32_id); i++) if (memcmp(info->file_ehdr.e_ident, elf32_id, sizeof(elf32_id))) {
if (file_ehdr.e_ident[i] != elf32_id[i]) { fprintf(stderr, "elf32: not an ELF32 file\n");
fprintf(stderr, "elf32: not an ELF32 file\n"); return -1;
return -1; }
}
return 0; return 0;
} }
static int read_phdr(FILE *in) static int read_phdr(struct elf32_info *info, FILE *in)
{ {
int i; int i;
if (file_ehdr.e_phnum > MAX_PHDRS) { if (info->file_ehdr.e_phnum > MAX_PHDRS) {
fprintf(stderr, "elf32: too many program headers: %d\n", fprintf(stderr, "elf32: too many program headers: %d\n",
file_ehdr.e_phnum); info->file_ehdr.e_phnum);
return -1; return -1;
} }
for (i = 0; i < file_ehdr.e_phnum; i++) { for (i = 0; i < info->file_ehdr.e_phnum; i++) {
if (fseek(in, i * file_ehdr.e_phentsize + file_ehdr.e_phoff, if (fseek(in, i * info->file_ehdr.e_phentsize +
info->file_ehdr.e_phoff,
SEEK_SET) < 0) { SEEK_SET) < 0) {
fprintf(stderr, "elf32: can't seek to phdr %d\n", i); fprintf(stderr, "elf32: can't seek to phdr %d\n", i);
return -1; return -1;
} }
if (fread(&file_phdrs[i], sizeof(file_phdrs[0]), 1, in) < 0) { if (fread(&info->file_phdrs[i],
sizeof(info->file_phdrs[0]), 1, in) < 0) {
fprintf(stderr, "elf32: can't read phdr %d: %s\n", fprintf(stderr, "elf32: can't read phdr %d: %s\n",
i, strerror(errno)); i, strerror(errno));
return -1; return -1;
@ -85,24 +86,26 @@ static int read_phdr(FILE *in)
return 0; return 0;
} }
static int read_shdr(FILE *in) static int read_shdr(struct elf32_info *info, FILE *in)
{ {
int i; int i;
if (file_ehdr.e_shnum > MAX_SHDRS) { if (info->file_ehdr.e_shnum > MAX_SHDRS) {
fprintf(stderr, "elf32: too many section headers: %d\n", fprintf(stderr, "elf32: too many section headers: %d\n",
file_ehdr.e_shnum); info->file_ehdr.e_shnum);
return -1; return -1;
} }
for (i = 0; i < file_ehdr.e_shnum; i++) { for (i = 0; i < info->file_ehdr.e_shnum; i++) {
if (fseek(in, i * file_ehdr.e_shentsize + file_ehdr.e_shoff, if (fseek(in, i * info->file_ehdr.e_shentsize +
info->file_ehdr.e_shoff,
SEEK_SET) < 0) { SEEK_SET) < 0) {
fprintf(stderr, "elf32: can't seek to shdr %d\n", i); fprintf(stderr, "elf32: can't seek to shdr %d\n", i);
return -1; return -1;
} }
if (fread(&file_shdrs[i], sizeof(file_shdrs[0]), 1, in) < 0) { if (fread(&info->file_shdrs[i],
sizeof(info->file_shdrs[0]), 1, in) < 0) {
fprintf(stderr, "elf32: can't read shdr %d: %s\n", fprintf(stderr, "elf32: can't read shdr %d: %s\n",
i, strerror(errno)); i, strerror(errno));
return -1; return -1;
@ -112,12 +115,12 @@ static int read_shdr(FILE *in)
return 0; return 0;
} }
static u_int32_t file_to_phys(u_int32_t v) static u_int32_t file_to_phys(struct elf32_info *info, u_int32_t v)
{ {
int i; int i;
for (i = 0; i < file_ehdr.e_phnum; i++) { for (i = 0; i < info->file_ehdr.e_phnum; i++) {
Elf32_Phdr *p = &file_phdrs[i]; Elf32_Phdr *p = &info->file_phdrs[i];
if (v >= p->p_offset && v - p->p_offset < p->p_filesz) if (v >= p->p_offset && v - p->p_offset < p->p_filesz)
return v - p->p_offset + p->p_paddr; return v - p->p_offset + p->p_paddr;
@ -126,11 +129,12 @@ static u_int32_t file_to_phys(u_int32_t v)
return v; return v;
} }
static int feed_section(FILE *in, int offset, int size, imgfunc_t cb, static int feed_section(struct elf32_info *info,
FILE *in, int offset, int size, imgfunc_t cb,
void *user_data) void *user_data)
{ {
u_int8_t buf[1024]; u_int8_t buf[1024];
u_int16_t addr = file_to_phys(offset); u_int16_t addr = file_to_phys(info, offset);
if (fseek(in, offset, SEEK_SET) < 0) { if (fseek(in, offset, SEEK_SET) < 0) {
perror("elf32: can't seek to section"); perror("elf32: can't seek to section");
@ -157,18 +161,20 @@ static int feed_section(FILE *in, int offset, int size, imgfunc_t cb,
return 0; return 0;
} }
static int read_all(FILE *in) static int read_all(struct elf32_info *info, FILE *in)
{ {
if (read_ehdr(in) < 0) memset(info, 0, sizeof(info));
if (read_ehdr(info, in) < 0)
return -1; return -1;
if (file_ehdr.e_machine != EM_MSP430) { if (info->file_ehdr.e_machine != EM_MSP430) {
fprintf(stderr, "elf32: this is not an MSP430 ELF32\n"); fprintf(stderr, "elf32: this is not an MSP430 ELF32\n");
return -1; return -1;
} }
if (read_phdr(in) < 0) if (read_phdr(info, in) < 0)
return -1; return -1;
if (read_shdr(in) < 0) if (read_shdr(info, in) < 0)
return -1; return -1;
return 0; return 0;
@ -176,17 +182,18 @@ static int read_all(FILE *in)
int elf32_extract(FILE *in, imgfunc_t cb, void *user_data) int elf32_extract(FILE *in, imgfunc_t cb, void *user_data)
{ {
struct elf32_info info;
int i; int i;
if (read_all(in) < 0) if (read_all(&info, in) < 0)
return -1; return -1;
for (i = 0; i < file_ehdr.e_shnum; i++) { for (i = 0; i < info.file_ehdr.e_shnum; i++) {
Elf32_Shdr *s = &file_shdrs[i]; Elf32_Shdr *s = &info.file_shdrs[i];
if (s->sh_type == SHT_PROGBITS && s->sh_flags & SHF_ALLOC && if (s->sh_type == SHT_PROGBITS && s->sh_flags & SHF_ALLOC &&
feed_section(in, s->sh_offset, s->sh_size, feed_section(&info, in, s->sh_offset, s->sh_size,
cb, user_data) < 0) cb, user_data) < 0)
return -1; return -1;
} }
@ -205,12 +212,12 @@ int elf32_check(FILE *in)
return 1; return 1;
} }
static Elf32_Shdr *find_shdr(Elf32_Word type) static Elf32_Shdr *find_shdr(struct elf32_info *info, Elf32_Word type)
{ {
int i; int i;
for (i = 0; i < file_ehdr.e_shnum; i++) { for (i = 0; i < info->file_ehdr.e_shnum; i++) {
Elf32_Shdr *s = &file_shdrs[i]; Elf32_Shdr *s = &info->file_shdrs[i];
if (s->sh_type == type) if (s->sh_type == type)
return s; return s;
@ -219,10 +226,9 @@ static Elf32_Shdr *find_shdr(Elf32_Word type)
return NULL; return NULL;
} }
static int syms_load_strings(FILE *in, Elf32_Shdr *s) static int syms_load_strings(struct elf32_info *info, FILE *in, Elf32_Shdr *s)
{ {
int len = s->sh_size; int len = s->sh_size;
int offset = 0;
if (!len) if (!len)
return 0; return 0;
@ -232,41 +238,32 @@ static int syms_load_strings(FILE *in, Elf32_Shdr *s)
return -1; return -1;
} }
string_len = len; info->string_len = len;
string_tab = malloc(len + 1); info->string_tab = malloc(len + 1);
if (!string_tab) { if (!info->string_tab) {
perror("elf32: can't allocate string table memory"); perror("elf32: can't allocate string table memory");
return -1; return -1;
} }
while (len) { if (!fread(info->string_tab, 1, info->string_len, in)) {
char buf[1024]; if (ferror(in)) {
int req = sizeof(buf) > len ? len : sizeof(buf);
int count = fread(buf, 1, req, in);
if (!count) {
fprintf(stderr, "elf32: eof reading strings\n");
return -1;
}
if (count < 0) {
perror("elf32: error reading strings"); perror("elf32: error reading strings");
return -1; return -1;
} }
memcpy(string_tab + offset, buf, count); fprintf(stderr, "elf32: eof reading strings\n");
offset += count; return -1;
len -= count;
} }
string_tab[string_len] = 0; info->string_tab[info->string_len] = 0;
return 0; return 0;
} }
#define N_SYMS 128 #define N_SYMS 128
static int syms_load_syms(FILE *in, Elf32_Shdr *s, symfunc_t cb) static int syms_load_syms(struct elf32_info *info, FILE *in,
Elf32_Shdr *s, symfunc_t cb)
{ {
Elf32_Sym syms[N_SYMS]; Elf32_Sym syms[N_SYMS];
int len = s->sh_size / sizeof(syms[0]); int len = s->sh_size / sizeof(syms[0]);
@ -294,13 +291,13 @@ static int syms_load_syms(FILE *in, Elf32_Shdr *s, symfunc_t cb)
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
Elf32_Sym *y = &syms[i]; Elf32_Sym *y = &syms[i];
if (y->st_name > string_len) { if (y->st_name > info->string_len) {
fprintf(stderr, "elf32: symbol out of " fprintf(stderr, "elf32: symbol out of "
"bounds\n"); "bounds\n");
return -1; return -1;
} }
if (cb(string_tab + y->st_name, y->st_value) < 0) if (cb(info->string_tab + y->st_name, y->st_value) < 0)
return -1; return -1;
} }
@ -312,33 +309,30 @@ static int syms_load_syms(FILE *in, Elf32_Shdr *s, symfunc_t cb)
int elf32_syms(FILE *in, symfunc_t cb) int elf32_syms(FILE *in, symfunc_t cb)
{ {
struct elf32_info info;
Elf32_Shdr *s; Elf32_Shdr *s;
int ret = 0;
if (read_all(in) < 0) if (read_all(&info, in) < 0)
return -1; return -1;
s = find_shdr(SHT_SYMTAB); s = find_shdr(&info, SHT_SYMTAB);
if (!s) { if (!s) {
fprintf(stderr, "elf32: no symbol table\n"); fprintf(stderr, "elf32: no symbol table\n");
return -1; return -1;
} }
if (s->sh_link <= 0 || s->sh_link >= file_ehdr.e_shnum) { if (s->sh_link <= 0 || s->sh_link >= info.file_ehdr.e_shnum) {
fprintf(stderr, "elf32: no string table\n"); fprintf(stderr, "elf32: no string table\n");
return -1; return -1;
} }
string_tab = NULL; if (syms_load_strings(&info, in, &info.file_shdrs[s->sh_link]) < 0 ||
string_len = 0; syms_load_syms(&info, in, s, cb) < 0)
ret = -1;
if (syms_load_strings(in, &file_shdrs[s->sh_link]) < 0 || if (info.string_tab)
syms_load_syms(in, s, cb) < 0) { free(info.string_tab);
if (string_tab)
free(string_tab);
return -1;
}
if (string_tab) return ret;
free(string_tab);
return 0;
} }