2 * Parse 32-bit ELF files.
3 * Copyright (c) 2004 Russ Cox. See LICENSE.
11 typedef struct ElfHdrBytes ElfHdrBytes;
12 typedef struct ElfSectBytes ElfSectBytes;
13 typedef struct ElfProgBytes ElfProgBytes;
14 typedef struct ElfSymBytes ElfSymBytes;
53 uchar info; /* top4: bind, bottom4: type */
70 uchar ElfMagic[4] = { 0x7F, 'E', 'L', 'F' };
72 static void unpackhdr(ElfHdr*, ElfHdrBytes*);
73 static void unpackprog(ElfHdr*, ElfProg*, ElfProgBytes*);
74 static void unpacksect(ElfHdr*, ElfSect*, ElfSectBytes*);
76 static char *elftypes[] = {
87 if(t < 0 || t >= nelem(elftypes))
92 static char *elfmachs[] = {
107 if(t < 0 || t >= nelem(elfmachs))
118 if((fd = open(name, OREAD)) < 0)
120 if((e = elfinit(fd)) == nil)
136 e = mallocz(sizeof(Elf), 1);
145 if(readn(fd, &hdrb, sizeof hdrb) != sizeof hdrb)
149 if(h->class != ElfClass32){
150 werrstr("bad ELF class - not 32-bit");
153 if(h->encoding != ElfDataLsb && h->encoding != ElfDataMsb){
154 werrstr("bad ELF encoding - not LSB, MSB");
157 if(hdrb.ident[6] != h->version){
158 werrstr("bad ELF encoding - version mismatch %02ux and %08ux",
159 (uint)hdrb.ident[6], (uint)h->version);
164 * the prog+section info is almost always small - just load it into memory.
167 e->prog = mallocz(sizeof(ElfProg)*e->nprog, 1);
168 for(i=0; i<e->nprog; i++){
169 if(seek(fd, h->phoff+i*h->phentsize, 0) < 0
170 || readn(fd, &progb, sizeof progb) != sizeof progb)
172 unpackprog(h, &e->prog[i], &progb);
178 e->sect = mallocz(sizeof(ElfSect)*e->nsect, 1);
179 for(i=0; i<e->nsect; i++){
180 if(seek(fd, h->shoff+i*h->shentsize, 0) < 0
181 || readn(fd, §b, sizeof sectb) != sizeof sectb)
183 unpacksect(h, &e->sect[i], §b);
186 if(h->shstrndx >= e->nsect){
187 fprint(2, "warning: bad string section index %d >= %d", h->shstrndx, e->nsect);
192 s = &e->sect[h->shstrndx];
196 for(i=0; i<e->nsect; i++)
198 e->sect[i].name = (char*)s->base + (ulong)e->sect[i].name;
200 e->symtab = elfsection(e, ".symtab");
202 if(e->symtab->link >= e->nsect)
205 e->symstr = &e->sect[e->symtab->link];
206 e->nsymtab = e->symtab->size / sizeof(ElfSymBytes);
209 e->dynsym = elfsection(e, ".dynsym");
211 if(e->dynsym->link >= e->nsect)
214 e->dynstr = &e->sect[e->dynsym->link];
215 e->ndynsym = e->dynsym->size / sizeof(ElfSymBytes);
219 e->bss = elfsection(e, ".bss");
237 for(i=0; i<elf->nsect; i++)
238 free(elf->sect[i].base);
246 unpackhdr(ElfHdr *h, ElfHdrBytes *b)
248 u16int (*e2)(uchar*);
249 u32int (*e4)(uchar*);
250 u64int (*e8)(uchar*);
252 memmove(h->magic, b->ident, 4);
253 h->class = b->ident[4];
254 h->encoding = b->ident[5];
269 h->abi = b->ident[7];
270 h->abiversion = b->ident[8];
276 h->type = e2(b->type);
277 h->machine = e2(b->machine);
278 h->version = e4(b->version);
279 h->entry = e4(b->entry);
280 h->phoff = e4(b->phoff);
281 h->shoff = e4(b->shoff);
282 h->flags = e4(b->flags);
283 h->ehsize = e2(b->ehsize);
284 h->phentsize = e2(b->phentsize);
285 h->phnum = e2(b->phnum);
286 h->shentsize = e2(b->shentsize);
287 h->shnum = e2(b->shnum);
288 h->shstrndx = e2(b->shstrndx);
292 unpackprog(ElfHdr *h, ElfProg *p, ElfProgBytes *b)
294 u32int (*e4)(uchar*);
297 p->type = e4(b->type);
298 p->offset = e4(b->offset);
299 p->vaddr = e4(b->vaddr);
300 p->paddr = e4(b->paddr);
301 p->filesz = e4(b->filesz);
302 p->memsz = e4(b->memsz);
303 p->flags = e4(b->flags);
304 p->align = e4(b->align);
308 unpacksect(ElfHdr *h, ElfSect *s, ElfSectBytes *b)
310 u32int (*e4)(uchar*);
313 s->name = (char*)(uintptr)e4(b->name);
314 s->type = e4(b->type);
315 s->flags = e4(b->flags);
316 s->addr = e4(b->addr);
317 s->offset = e4(b->offset);
318 s->size = e4(b->size);
319 s->link = e4(b->link);
320 s->info = e4(b->info);
321 s->align = e4(b->align);
322 s->entsize = e4(b->entsize);
326 elfsection(Elf *elf, char *name)
330 for(i=0; i<elf->nsect; i++){
331 if(elf->sect[i].name == name)
332 return &elf->sect[i];
333 if(elf->sect[i].name && name
334 && strcmp(elf->sect[i].name, name) == 0)
335 return &elf->sect[i];
337 werrstr("elf section '%s' not found", name);
342 elfmap(Elf *elf, ElfSect *sect)
346 if((sect->base = malloc(sect->size)) == nil)
348 werrstr("short read");
349 if(seek(elf->fd, sect->offset, 0) < 0
350 || readn(elf->fd, sect->base, sect->size) != sect->size){
359 elfsym(Elf *elf, int i, ElfSym *sym)
361 ElfSect *symtab, *strtab;
367 werrstr("bad index %d in elfsym", i);
371 if(i < elf->nsymtab){
372 symtab = elf->symtab;
373 strtab = elf->symstr;
375 if(elfmap(elf, symtab) < 0 || elfmap(elf, strtab) < 0)
377 p = symtab->base + i * sizeof(ElfSymBytes);
378 s = (char*)strtab->base;
380 if(x >= strtab->size){
381 werrstr("bad symbol name offset 0x%lux", x);
385 sym->value = elf->hdr.e4(p+4);
386 sym->size = elf->hdr.e4(p+8);
391 sym->shndx = elf->hdr.e2(p+14);
395 if(i < elf->ndynsym){
396 symtab = elf->dynsym;
397 strtab = elf->dynstr;
400 /* i -= elf->ndynsym */
402 werrstr("symbol index out of range");