elfloadmap.c - scc - simple c99 compiler
(HTM) git clone git://git.simple-cc.org/scc
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Submodules
(DIR) README
(DIR) LICENSE
---
elfloadmap.c (921B)
---
1 #include <stdio.h>
2 #include <string.h>
3
4 #include <scc/mach.h>
5 #include <scc/elf.h>
6
7 #include "../libmach.h"
8 #include "fun.h"
9
10 Map *
11 elfloadmap(Obj *obj, FILE *fp)
12 {
13 int i;
14 int nsec, nseg;
15 Map *map;
16 Section sec;
17 FILE *src;
18 Elf *elf = obj->data;
19 Elfhdr *hdr = &elf->hdr;
20 Elfphdr *phdr;
21
22 nseg = hdr->phnum;
23 nsec = elf->nsec;
24 if ((map = newmap(nsec, nseg)) == NULL)
25 return NULL;
26
27 memset(&sec, 0, sizeof(sec));
28 for (i = 0; i < nseg; ++i) {
29 phdr = &elf->phdr[i];
30 sec.name = NULL;
31 sec.load = phdr->vaddr;
32 sec.base = phdr->paddr;
33 sec.offset = obj->pos + phdr->offset;
34 sec.align = phdr->align;
35 sec.size = phdr->memsz;
36 sec.index = i;
37
38 if (mapseg(map, &sec, fp, phdr->filesz) < 0)
39 return NULL;
40 }
41
42 for (i = 0; getsec(obj, &i, &sec); ++i) {
43 sec.offset += obj->pos;
44 src = ((sec.flags & SALLOC) != 0) ? fp : NULL;
45
46 if (mapsec(map, &sec, src, sec.size) < 0)
47 return NULL;
48 }
49
50 return map;
51 }