1#include <stdio.h>2#include <string.h>34#include <scc/mach.h>5#include <scc/elf.h>67#include "../libmach.h"8#include "fun.h"910Map *11elfloadmap(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;2122 nseg = hdr->phnum;23 nsec = elf->nsec;24 if ((map = newmap(nsec, nseg)) == NULL)25 return NULL;2627 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;3738 if (mapseg(map, &sec, fp, phdr->filesz) < 0)39 return NULL;40 }4142 for (i = 0; getsec(obj, &i, &sec); ++i) {43 sec.offset += obj->pos;44 src = ((sec.flags & SALLOC) != 0) ? fp : NULL;4546 if (mapsec(map, &sec, src, sec.size) < 0)47 return NULL;48 }4950 return map;51}