aboutsummaryrefslogtreecommitdiff
path: root/src/libmach/elf.h
blob: cda2f80782ea06e5580b968586d8d07458fb2247 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * Copyright (c) 2004 Russ Cox.  See LICENSE.
 */

/* /home/rsc/papers/elfXXelf.pdf */

typedef struct Elf Elf;
typedef struct ElfHdr ElfHdr;
typedef struct ElfSect ElfSect;
typedef struct ElfProg ElfProg;
typedef struct ElfNote ElfNote;
typedef struct ElfSym ElfSym;

enum
{
	ElfClassNone = 0,
	ElfClass32,
	ElfClass64,

	ElfDataNone = 0,
	ElfDataLsb,
	ElfDataMsb,

	ElfTypeNone = 0,
	ElfTypeRelocatable,
	ElfTypeExecutable,
	ElfTypeSharedObject,
	ElfTypeCore,
	/* 0xFF00 - 0xFFFF reserved for processor-specific types */

	ElfMachNone = 0,
	ElfMach32100,		/* AT&T WE 32100 */
	ElfMachSparc,		/* SPARC */
	ElfMach386,		/* Intel 80386 */
	ElfMach68000,		/* Motorola 68000 */
	ElfMach88000,		/* Motorola 88000 */
	ElfMach486,		/* Intel 80486, no longer used */
	ElfMach860,		/* Intel 80860 */
	ElfMachMips,		/* MIPS RS3000 */
	ElfMachS370,		/* IBM System/370 */
	ElfMachMipsLe,	/* MIPS RS3000 LE */
	ElfMachParisc = 15,		/* HP PA RISC */
	ElfMachVpp500 = 17,	/* Fujitsu VPP500 */
	ElfMachSparc32Plus,	/* SPARC V8+ */
	ElfMach960,		/* Intel 80960 */
	ElfMachPower,		/* PowerPC */
	ElfMachPower64,	/* PowerPC 64 */
	ElfMachS390,		/* IBM System/390 */
	ElfMachV800 = 36,	/* NEC V800 */
	ElfMachFr20,		/* Fujitsu FR20 */
	ElfMachRh32,		/* TRW RH-32 */
	ElfMachRce,		/* Motorola RCE */
	ElfMachArm,		/* ARM */
	ElfMachAlpha,		/* Digital Alpha */
	ElfMachSH,		/* Hitachi SH */
	ElfMachSparc9,		/* SPARC V9 */
	ElfMachAmd64 = 62,	/* x86-64 */
	/* and the list goes on... */

	ElfAbiNone = 0,
	ElfAbiSystemV = 0,	/* [sic] */
	ElfAbiHPUX,
	ElfAbiNetBSD,
	ElfAbiLinux,
	ElfAbiSolaris = 6,
	ElfAbiAix,
	ElfAbiIrix,
	ElfAbiFreeBSD,
	ElfAbiTru64,
	ElfAbiModesto,
	ElfAbiOpenBSD,
	ElfAbiARM = 97,
	ElfAbiEmbedded = 255,

	/* some of sections 0xFF00 - 0xFFFF reserved for various things */
	ElfSectNone = 0,
	ElfSectProgbits,
	ElfSectSymtab,
	ElfSectStrtab,
	ElfSectRela,
	ElfSectHash,
	ElfSectDynamic,
	ElfSectNote,
	ElfSectNobits,
	ElfSectRel,
	ElfSectShlib,
	ElfSectDynsym,

	ElfSectFlagWrite = 0x1,
	ElfSectFlagAlloc = 0x2,
	ElfSectFlagExec = 0x4,
	/* 0xF0000000 are reserved for processor specific */

	ElfSymBindLocal = 0,
	ElfSymBindGlobal,
	ElfSymBindWeak,
	/* 13-15 reserved */

	ElfSymTypeNone = 0,
	ElfSymTypeObject,
	ElfSymTypeFunc,
	ElfSymTypeSection,
	ElfSymTypeFile,
	/* 13-15 reserved */

	ElfSymShnNone = 0,
	ElfSymShnAbs = 0xFFF1,
	ElfSymShnCommon = 0xFFF2,
	/* 0xFF00-0xFF1F reserved for processors */
	/* 0xFF20-0xFF3F reserved for operating systems */

	ElfProgNone = 0,
	ElfProgLoad,
	ElfProgDynamic,
	ElfProgInterp,
	ElfProgNote,
	ElfProgShlib,
	ElfProgPhdr,

	ElfProgFlagExec = 0x1,
	ElfProgFlagWrite = 0x2,
	ElfProgFlagRead = 0x4,

	ElfNotePrStatus = 1,
	ElfNotePrFpreg = 2,
	ElfNotePrPsinfo = 3,
	ElfNotePrTaskstruct = 4,
	ElfNotePrAuxv = 6,
	ElfNotePrXfpreg = 0x46e62b7f	/* for gdb/386 */
};

struct ElfHdr
{
	uchar	magic[4];
	uchar	class;
	uchar	encoding;
	uchar	version;
	uchar	abi;
	uchar	abiversion;
	u32int	type;
	u32int	machine;
	u64int	entry;
	u64int	phoff;
	u64int	shoff;
	u32int	flags;
	u32int	ehsize;
	u32int	phentsize;
	u32int	phnum;
	u32int	shentsize;
	u32int	shnum;
	u32int	shstrndx;
	u16int	(*e2)(uchar*);
	u32int	(*e4)(uchar*);
	u64int	(*e8)(uchar*);
};

struct ElfSect
{
	char		*name;
	u32int	type;
	u64int	flags;
	u64int	addr;
	u64int	offset;
	u64int	size;
	u32int	link;
	u32int	info;
	u64int	align;
	u64int	entsize;
	uchar	*base;
};

struct ElfProg
{
	u32int	type;
	u64int	offset;
	u64int	vaddr;
	u64int	paddr;
	u64int	filesz;
	u64int	memsz;
	u32int	flags;
	u64int	align;
};

struct ElfNote
{
	u32int	namesz;
	u32int	descsz;
	u32int	type;
	char	*name;
	uchar	*desc;
	u32int	offset;	/* in-memory only */
};

struct ElfSym
{
	char*	name;
	u64int	value;
	u64int	size;
	uchar	bind;
	uchar	type;
	uchar	other;
	u16int	shndx;
};

struct Elf
{
	int		fd;
	ElfHdr	hdr;
	ElfSect	*sect;
	uint		nsect;
	ElfProg	*prog;
	uint		nprog;
	char		*shstrtab;

	int		nsymtab;
	ElfSect	*symtab;
	ElfSect	*symstr;
	int		ndynsym;
	ElfSect	*dynsym;
	ElfSect	*dynstr;
	ElfSect	*bss;
	ulong	dynamic;		/* offset to elf dynamic crap */

	int		(*coreregs)(Elf*, ElfNote*, uchar**);
	int		(*corecmd)(Elf*, ElfNote*, char**);
};

Elf*	elfopen(char*);
Elf*	elfinit(int);
ElfSect *elfsection(Elf*, char*);
void	elfclose(Elf*);
int	elfsym(Elf*, int, ElfSym*);
int	elfsymlookup(Elf*, char*, ulong*);
int	elfmap(Elf*, ElfSect*);

struct Fhdr;
void	elfcorelinux386(struct Fhdr*, Elf*, ElfNote*);
void	elfcorefreebsd386(struct Fhdr*, Elf*, ElfNote*);
void	elfcorefreebsdamd64(struct Fhdr*, Elf*, ElfNote*);
void	elfdl386mapdl(int);