Blob


1 /*
2 * Copyright (c) 2004 Russ Cox. See LICENSE.
3 */
5 /* /home/rsc/papers/elfXXelf.pdf */
7 typedef struct Elf Elf;
8 typedef struct ElfHdr ElfHdr;
9 typedef struct ElfSect ElfSect;
10 typedef struct ElfProg ElfProg;
11 typedef struct ElfNote ElfNote;
12 typedef struct ElfSym ElfSym;
14 enum
15 {
16 ElfClassNone = 0,
17 ElfClass32,
18 ElfClass64,
20 ElfDataNone = 0,
21 ElfDataLsb,
22 ElfDataMsb,
24 ElfTypeNone = 0,
25 ElfTypeRelocatable,
26 ElfTypeExecutable,
27 ElfTypeSharedObject,
28 ElfTypeCore,
29 /* 0xFF00 - 0xFFFF reserved for processor-specific types */
31 ElfMachNone = 0,
32 ElfMach32100, /* AT&T WE 32100 */
33 ElfMachSparc, /* SPARC */
34 ElfMach386, /* Intel 80386 */
35 ElfMach68000, /* Motorola 68000 */
36 ElfMach88000, /* Motorola 88000 */
37 ElfMach486, /* Intel 80486, no longer used */
38 ElfMach860, /* Intel 80860 */
39 ElfMachMips, /* MIPS RS3000 */
40 ElfMachS370, /* IBM System/370 */
41 ElfMachMipsLe, /* MIPS RS3000 LE */
42 ElfMachParisc = 15, /* HP PA RISC */
43 ElfMachVpp500 = 17, /* Fujitsu VPP500 */
44 ElfMachSparc32Plus, /* SPARC V8+ */
45 ElfMach960, /* Intel 80960 */
46 ElfMachPower, /* PowerPC */
47 ElfMachPower64, /* PowerPC 64 */
48 ElfMachS390, /* IBM System/390 */
49 ElfMachV800 = 36, /* NEC V800 */
50 ElfMachFr20, /* Fujitsu FR20 */
51 ElfMachRh32, /* TRW RH-32 */
52 ElfMachRce, /* Motorola RCE */
53 ElfMachArm, /* ARM */
54 ElfMachAlpha, /* Digital Alpha */
55 ElfMachSH, /* Hitachi SH */
56 ElfMachSparc9, /* SPARC V9 */
57 ElfMachAmd64 = 62, /* x86-64 */
58 /* and the list goes on... */
60 ElfAbiNone = 0,
61 ElfAbiSystemV = 0, /* [sic] */
62 ElfAbiHPUX,
63 ElfAbiNetBSD,
64 ElfAbiLinux,
65 ElfAbiSolaris = 6,
66 ElfAbiAix,
67 ElfAbiIrix,
68 ElfAbiFreeBSD,
69 ElfAbiTru64,
70 ElfAbiModesto,
71 ElfAbiOpenBSD,
72 ElfAbiARM = 97,
73 ElfAbiEmbedded = 255,
75 /* some of sections 0xFF00 - 0xFFFF reserved for various things */
76 ElfSectNone = 0,
77 ElfSectProgbits,
78 ElfSectSymtab,
79 ElfSectStrtab,
80 ElfSectRela,
81 ElfSectHash,
82 ElfSectDynamic,
83 ElfSectNote,
84 ElfSectNobits,
85 ElfSectRel,
86 ElfSectShlib,
87 ElfSectDynsym,
89 ElfSectFlagWrite = 0x1,
90 ElfSectFlagAlloc = 0x2,
91 ElfSectFlagExec = 0x4,
92 /* 0xF0000000 are reserved for processor specific */
94 ElfSymBindLocal = 0,
95 ElfSymBindGlobal,
96 ElfSymBindWeak,
97 /* 13-15 reserved */
99 ElfSymTypeNone = 0,
100 ElfSymTypeObject,
101 ElfSymTypeFunc,
102 ElfSymTypeSection,
103 ElfSymTypeFile,
104 /* 13-15 reserved */
106 ElfSymShnNone = 0,
107 ElfSymShnAbs = 0xFFF1,
108 ElfSymShnCommon = 0xFFF2,
109 /* 0xFF00-0xFF1F reserved for processors */
110 /* 0xFF20-0xFF3F reserved for operating systems */
112 ElfProgNone = 0,
113 ElfProgLoad,
114 ElfProgDynamic,
115 ElfProgInterp,
116 ElfProgNote,
117 ElfProgShlib,
118 ElfProgPhdr,
120 ElfProgFlagExec = 0x1,
121 ElfProgFlagWrite = 0x2,
122 ElfProgFlagRead = 0x4,
124 ElfNotePrStatus = 1,
125 ElfNotePrFpreg = 2,
126 ElfNotePrPsinfo = 3,
127 ElfNotePrTaskstruct = 4,
128 ElfNotePrAuxv = 6,
129 ElfNotePrXfpreg = 0x46e62b7f /* for gdb/386 */
130 };
132 struct ElfHdr
134 uchar magic[4];
135 uchar class;
136 uchar encoding;
137 uchar version;
138 uchar abi;
139 uchar abiversion;
140 u32int type;
141 u32int machine;
142 u64int entry;
143 u64int phoff;
144 u64int shoff;
145 u32int flags;
146 u32int ehsize;
147 u32int phentsize;
148 u32int phnum;
149 u32int shentsize;
150 u32int shnum;
151 u32int shstrndx;
152 u16int (*e2)(uchar*);
153 u32int (*e4)(uchar*);
154 u64int (*e8)(uchar*);
155 };
157 struct ElfSect
159 char *name;
160 u32int type;
161 u64int flags;
162 u64int addr;
163 u64int offset;
164 u64int size;
165 u32int link;
166 u32int info;
167 u64int align;
168 u64int entsize;
169 uchar *base;
170 };
172 struct ElfProg
174 u32int type;
175 u64int offset;
176 u64int vaddr;
177 u64int paddr;
178 u64int filesz;
179 u64int memsz;
180 u32int flags;
181 u64int align;
182 };
184 struct ElfNote
186 u32int namesz;
187 u32int descsz;
188 u32int type;
189 char *name;
190 uchar *desc;
191 u32int offset; /* in-memory only */
192 };
194 struct ElfSym
196 char* name;
197 u64int value;
198 u64int size;
199 uchar bind;
200 uchar type;
201 uchar other;
202 u16int shndx;
203 };
205 struct Elf
207 int fd;
208 ElfHdr hdr;
209 ElfSect *sect;
210 uint nsect;
211 ElfProg *prog;
212 uint nprog;
213 char *shstrtab;
215 int nsymtab;
216 ElfSect *symtab;
217 ElfSect *symstr;
218 int ndynsym;
219 ElfSect *dynsym;
220 ElfSect *dynstr;
221 ElfSect *bss;
222 ulong dynamic; /* offset to elf dynamic crap */
224 int (*coreregs)(Elf*, ElfNote*, uchar**);
225 int (*corecmd)(Elf*, ElfNote*, char**);
226 };
228 Elf* elfopen(char*);
229 Elf* elfinit(int);
230 ElfSect *elfsection(Elf*, char*);
231 void elfclose(Elf*);
232 int elfsym(Elf*, int, ElfSym*);
233 int elfsymlookup(Elf*, char*, ulong*);
234 int elfmap(Elf*, ElfSect*);
236 struct Fhdr;
237 void elfcorelinux386(struct Fhdr*, Elf*, ElfNote*);
238 void elfcorefreebsd386(struct Fhdr*, Elf*, ElfNote*);
239 void elfcorefreebsdamd64(struct Fhdr*, Elf*, ElfNote*);
240 void elfdl386mapdl(int);