7 static int mapelf(Fhdr *fp, ulong base, Map *map, Regs**);
8 static int unpacknote(Elf *elf, uchar *a, uchar *ea, ElfNote *note, uchar **pa);
18 ElfMachSparc, MSPARC, nil, "sparc",
19 ElfMach386, M386, &mach386, "386",
20 ElfMachMips, MMIPS, nil, "mips",
21 ElfMachArm, MARM, nil, "arm",
22 ElfMachPower, MPOWER, nil, "powerpc",
23 ElfMachPower64, MNONE, nil, "powerpc64",
33 ElfAbiSystemV, ALINUX, "linux", /* [sic] */
34 ElfAbiLinux, ALINUX, "linux",
35 ElfAbiFreeBSD, AFREEBSD, "freebsd",
42 void (*elfcore)(Fhdr*, Elf*, ElfNote*);
45 M386, ALINUX, elfcorelinux386,
46 M386, ANONE, elfcorelinux386, /* [sic] */
47 /* M386, AFREEBSD, elfcorefreebsd386, */
51 crackelf(int fd, Fhdr *fp)
54 int i, havetext, havedata, n;
59 void (*elfcore)(Fhdr*, Elf*, ElfNote*);
61 if((elf = elfinit(fd)) == nil)
66 fp->dwarf = dwarfopen(elf); /* okay to fail */
69 if((s1 = elfsection(elf, ".stab")) != nil && s1->link!=0 && s1->link < elf->nsect){
70 s2 = &elf->sect[s1->link];
71 if(elfmap(elf, s1) >= 0 && elfmap(elf, s2) >= 0){
72 fp->stabs.stabbase = s1->base;
73 fp->stabs.stabsize = s1->size;
74 fp->stabs.strbase = (char*)s2->base;
75 fp->stabs.strsize = s2->size;
76 fp->stabs.e2 = elf->hdr.e2;
77 fp->stabs.e4 = elf->hdr.e4;
81 for(i=0; i<nelem(mtab); i++){
82 if(elf->hdr.machine != mtab[i].etype)
84 fp->mach = mtab[i].mach;
85 fp->mname = mtab[i].name;
86 fp->mtype = mtab[i].mtype;
90 werrstr("unsupported machine type %d", elf->hdr.machine);
97 fp->aname = "unknown";
98 for(i=0; i<nelem(atab); i++){
99 if(elf->hdr.abi != atab[i].etype)
101 fp->atype = atab[i].atype;
102 fp->aname = atab[i].aname;
106 switch(elf->hdr.type){
108 werrstr("unknown file type %d", elf->hdr.type);
110 case ElfTypeExecutable:
112 fp->fname = "executable";
114 case ElfTypeRelocatable:
116 fp->fname = "relocatable";
118 case ElfTypeSharedObject:
120 fp->fname = "shared object";
124 fp->fname = "core dump";
130 if(fp->ftype == FCORE){
132 for(i=0; i<nelem(ctab); i++){
133 if(ctab[i].atype != fp->atype
134 || ctab[i].mtype != fp->mtype)
136 elfcore = ctab[i].elfcore;
140 for(i=0; i<elf->nprog; i++){
142 if(p->type != ElfProgNote)
148 if(seek(fp->fd, p->offset, 0) < 0 || readn(fp->fd, a, n) != n){
155 note.offset = (a-sa) + p->offset;
156 if(unpacknote(elf, a, ea, ¬e, &a) < 0)
158 elfcore(fp, elf, ¬e);
165 fp->entry = elf->hdr.entry;
167 /* First r-x section we find is the text and initialized data */
168 /* First rw- section we find is the r/w data */
171 for(i=0; i<elf->nprog; i++){
173 if(p->type != ElfProgLoad)
175 if(!havetext && p->flags == (ElfProgFlagRead|ElfProgFlagExec) && p->align >= mach->pgsize){
177 fp->txtaddr = p->vaddr;
178 fp->txtsz = p->memsz;
179 fp->txtoff = p->offset;
181 if(!havedata && p->flags == (ElfProgFlagRead|ElfProgFlagWrite) && p->align >= mach->pgsize){
183 fp->dataddr = p->vaddr;
184 fp->datsz = p->filesz;
185 fp->datoff = p->offset;
186 fp->bsssz = p->memsz - p->filesz;
190 werrstr("did not find text segment in elf binary");
194 werrstr("did not find data segment in elf binary");
205 mapelf(Fhdr *fp, ulong base, Map *map, Regs **regs)
216 werrstr("not an elf file");
220 for(i=0; i<elf->nprog; i++){
222 if(p->type != ElfProgLoad)
224 if(p->align < mach->pgsize)
227 memset(&s, 0, sizeof s);
228 s.file = fp->filename;
230 if(fp->ftype == FCORE)
232 else if(p->flags == 5)
236 s.base = base+p->vaddr;
238 s.offset = p->offset;
239 if(addseg(map, s) < 0)
243 * If memsz > filesz, we're supposed to zero fill.
244 * Core files have zeroed sections where the pages
245 * can be filled in from the text file, so if this is a core
246 * we only fill in that which isn't yet mapped.
248 if(fp->ftype == FCORE){
250 while(sz < p->memsz){
251 if(addrtoseg(map, base+p->vaddr+sz, &s) < 0){
252 lim = base + p->vaddr + p->memsz;
253 if(addrtosegafter(map, base+p->vaddr+sz, &s) >= 0 && s.base < lim)
255 memset(&s, 0, sizeof s);
257 s.base = base + p->vaddr + sz;
258 s.size = lim - s.base;
259 s.offset = p->offset;
260 if(addseg(map, s) < 0)
263 sz = (s.base+s.size) - (base + p->vaddr);
266 if(p->filesz < p->memsz){
267 memset(&s, 0, sizeof s);
269 s.base = base + p->vaddr + p->filesz;
270 s.size = p->memsz - p->filesz;
271 if(addseg(map, s) < 0)
277 if(fp->nthread && regs)
278 *regs = coreregs(fp, fp->thread[0].id);
284 unpacknote(Elf *elf, uchar *a, uchar *ea, ElfNote *note, uchar **pa)
288 note->namesz = elf->hdr.e4(a);
289 note->descsz = elf->hdr.e4(a+4);
290 note->type = elf->hdr.e4(a+8);
292 note->name = (char*)a;
293 /* XXX fetch alignment constants from elsewhere */
294 a += (note->namesz+3)&~3;
295 note->desc = (uchar*)a;
296 a += (note->descsz+3)&~3;