7 static int mapelf(Fhdr *fp, u64int 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",
24 ElfMachAmd64, MAMD64, &machamd64, "amd64",
34 ElfAbiSystemV, ALINUX, "linux", /* [sic] */
35 ElfAbiLinux, ALINUX, "linux",
36 ElfAbiFreeBSD, AFREEBSD, "freebsd",
43 void (*elfcore)(Fhdr*, Elf*, ElfNote*);
46 M386, ALINUX, elfcorelinux386,
47 M386, ANONE, elfcorelinux386, /* [sic] */
48 // M386, AFREEBSD, elfcorefreebsd386,
49 MAMD64, AFREEBSD, elfcorefreebsdamd64,
53 crackelf(int fd, Fhdr *fp)
56 int i, havetext, havedata, n;
61 void (*elfcore)(Fhdr*, Elf*, ElfNote*);
63 if((elf = elfinit(fd)) == nil)
68 fp->dwarf = dwarfopen(elf); /* okay to fail */
71 if((s1 = elfsection(elf, ".stab")) != nil && s1->link!=0 && s1->link < elf->nsect){
72 s2 = &elf->sect[s1->link];
73 if(elfmap(elf, s1) >= 0 && elfmap(elf, s2) >= 0){
74 fp->stabs.stabbase = s1->base;
75 fp->stabs.stabsize = s1->size;
76 fp->stabs.strbase = (char*)s2->base;
77 fp->stabs.strsize = s2->size;
78 fp->stabs.e2 = elf->hdr.e2;
79 fp->stabs.e4 = elf->hdr.e4;
83 for(i=0; i<nelem(mtab); i++){
84 if(elf->hdr.machine != mtab[i].etype)
86 fp->mach = mtab[i].mach;
87 fp->mname = mtab[i].name;
88 fp->mtype = mtab[i].mtype;
92 werrstr("unsupported machine type %d", elf->hdr.machine);
99 fp->aname = "unknown";
100 for(i=0; i<nelem(atab); i++){
101 if(elf->hdr.abi != atab[i].etype)
103 fp->atype = atab[i].atype;
104 fp->aname = atab[i].aname;
108 switch(elf->hdr.type){
110 werrstr("unknown file type %d", elf->hdr.type);
112 case ElfTypeExecutable:
114 fp->fname = "executable";
116 case ElfTypeRelocatable:
118 fp->fname = "relocatable";
120 case ElfTypeSharedObject:
122 fp->fname = "shared object";
126 fp->fname = "core dump";
132 if(fp->ftype == FCORE){
134 for(i=0; i<nelem(ctab); i++){
135 if(ctab[i].atype != fp->atype
136 || ctab[i].mtype != fp->mtype)
138 elfcore = ctab[i].elfcore;
142 for(i=0; i<elf->nprog; i++){
144 if(p->type != ElfProgNote)
150 if(seek(fp->fd, p->offset, 0) < 0 || readn(fp->fd, a, n) != n){
157 note.offset = (a-sa) + p->offset;
158 if(unpacknote(elf, a, ea, ¬e, &a) < 0)
160 elfcore(fp, elf, ¬e);
167 fp->entry = elf->hdr.entry;
169 /* First r-x section we find is the text and initialized data */
170 /* First rw- section we find is the r/w data */
173 for(i=0; i<elf->nprog; i++){
175 if(p->type != ElfProgLoad)
177 if(!havetext && p->flags == (ElfProgFlagRead|ElfProgFlagExec) && p->align >= mach->pgsize){
179 fp->txtaddr = p->vaddr;
180 fp->txtsz = p->memsz;
181 fp->txtoff = p->offset;
183 if(!havedata && p->flags == (ElfProgFlagRead|ElfProgFlagWrite) && p->align >= mach->pgsize){
185 fp->dataddr = p->vaddr;
186 fp->datsz = p->filesz;
187 fp->datoff = p->offset;
188 fp->bsssz = p->memsz - p->filesz;
192 werrstr("did not find text segment in elf binary");
196 werrstr("did not find data segment in elf binary");
207 mapelf(Fhdr *fp, u64int base, Map *map, Regs **regs)
218 werrstr("not an elf file");
222 for(i=0; i<elf->nprog; i++){
224 if(p->type != ElfProgLoad)
226 if(p->align < mach->pgsize)
229 memset(&s, 0, sizeof s);
230 s.file = fp->filename;
232 if(fp->ftype == FCORE)
234 else if(p->flags == 5)
238 s.base = base+p->vaddr;
240 s.offset = p->offset;
241 if(addseg(map, s) < 0)
245 * If memsz > filesz, we're supposed to zero fill.
246 * Core files have zeroed sections where the pages
247 * can be filled in from the text file, so if this is a core
248 * we only fill in that which isn't yet mapped.
250 if(fp->ftype == FCORE){
252 while(sz < p->memsz){
253 if(addrtoseg(map, base+p->vaddr+sz, &s) < 0){
254 lim = base + p->vaddr + p->memsz;
255 if(addrtosegafter(map, base+p->vaddr+sz, &s) >= 0 && s.base < lim)
257 memset(&s, 0, sizeof s);
259 s.base = base + p->vaddr + sz;
260 s.size = lim - s.base;
261 s.offset = p->offset;
262 if(addseg(map, s) < 0)
265 sz = (s.base+s.size) - (base + p->vaddr);
268 if(p->filesz < p->memsz){
269 memset(&s, 0, sizeof s);
271 s.base = base + p->vaddr + p->filesz;
272 s.size = p->memsz - p->filesz;
273 if(addseg(map, s) < 0)
279 if(fp->nthread && regs)
280 *regs = coreregs(fp, fp->thread[0].id);
286 unpacknote(Elf *elf, uchar *a, uchar *ea, ElfNote *note, uchar **pa)
290 note->namesz = elf->hdr.e4(a);
291 note->descsz = elf->hdr.e4(a+4);
292 note->type = elf->hdr.e4(a+8);
294 note->name = (char*)a;
295 /* XXX fetch alignment constants from elsewhere */
296 a += (note->namesz+3)&~3;
297 note->desc = (uchar*)a;
298 a += (note->descsz+3)&~3;