aboutsummaryrefslogtreecommitdiff
path: root/include/apos/elf.h
blob: ad5c4da379bbc6c4ef3c5970fd9e7dc8b18f868e (plain) (blame)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#ifndef APOS_ELF_H
#define APOS_ELF_H

/**
 * @file elf.h
 * ELF file handling.
 *
 * \todo Other file formats?
 */

#include <apos/attrs.h>
#include <apos/types.h>
#include <apos/vmem.h>

/** @name Magic. */
/** @{ */

/** Magic ELF header bytes. */
#define EI_MAGIC 0x7f454c46

/** @} */

/** @name ELF classification. */
/** @{ */

/** ELF class none, likely invalid binary. */
#define ELFCLASSNONE 0x0

/** ELF class 32, 32bit binary. */
#define ELFCLASS32 0x1

/** ELF class 64, 64bit binary. */
#define ELFCLASS64 0x2

/** @} */

/** @name ELF data formats. */
/** @{ */

/** ELF data none, likely invalid binary. */
#define ELFDATANONE 0x0

/** ELF data little endian. */
#define ELFDATA2LSB 0x1

/** ELF data big endian. */
#define ELFDATA2MSB 0x2

/** @} */

/**
 * @name ELF ABIs.
 * Missing apos, of course, not sure which of these I should use instead. Some
 * BSD?
 */
/** @{ */

/** ELF operating system ABI none. Likely standalone binary. */
#define ELFOSABI_NONE 0x0

/** ELF UNIX SystemV ABI. */
#define ELFOSABI_SYSV 0x0

/** ELF NetBSD ABI. */
#define ELFOSABI_NETBSD 0x2

/** ELF Linux ABI. */
#define ELFOSABI_LINUX 0x3

/** ELF GNU Hurd ABI. */
#define ELFOSABI_HURD 0x4

/** ELF FreeBSD ABI. */
#define ELFOSABI_FREEBSD 0x9

/** ELF OpenBSD ABI. */
#define ELFOSABI_OPENBSD 0xc

/** @} */

/** @name ELF binary types. */
/** @{ */

/** Binary type none. Likely invalid binary. */
#define ET_NONE 0x0

/** Relocatable file. */
#define ET_REL 0x1

/** Executable file. */
#define ET_EXEC 0x2

/** Shared object file. */
#define ET_DYN 0x3

/** Core file. */
#define ET_CORE 0x4

/** @} */

/**
 * @name Machine architecture.
 * Currently only RISCV supported.
 */
/** @{ */

/** Riscv. */
#define EM_RISCV 0xf3

/** @} */

/** ELF identification data. */
struct __packed elf_ident {
	/** Magic. \see EI_MAGIC. */
	uint32_t ei_magic;

	/** ELF class. \see ELFCLASSNONE, ELFCLASS32, ELFCLASS64. */
	uint8_t ei_class;

	/** ELF data formats. \see ELFDATANONE, ELFDATA2LSB, ELFDATA2MSB. */
	uint8_t ei_data;

	/** ELF version. \c 1 for current, \c 0 for undefined. */
	uint8_t ei_version;

	/** ELF ABI. \see ELFOSABI_NONE, ELFOSABI_SYSV, ELFOSABI_NETBSD,
	 * ELFOSABI_LINUX, ELFOSABI_HURD, ELFOSABI_FREEBSD. */
	uint8_t ei_osabi;

	/** ELF ABI version. Depends on \c ei_osabi. */
	uint8_t ei_abiversion;

	/** Padding. */
	uint8_t ei_pad[7];
};

/** ELF32 header for ELF binaries of class \ref ELFCLASS32. */
struct __packed elf32_header {
	/** Common identity header. */
	struct elf_ident e_ident;

	/** ELF type. \see ET_NONE, ET_REL, ET_EXEC, ET_DYN, ET_CORE. */
	uint16_t e_type;

	/** Machine architecture. \see EM_RISCV. */
	uint16_t e_machine;

	/** ELF version. \c 1 for current, \c 0 for undefined. */
	uint32_t e_version;

	/** Entrypoint virtual address. */
	uint32_t e_entry;

	/** Start of program header table within binary image. */
	uint32_t e_phoff;

	/** Start of section header table within binary image. */
	uint32_t e_shoff;

	/** Architecture dependent flags. */
	uint32_t e_flags;

	/** Size of this header. 52 bytes for ELF32. */
	uint16_t e_ehsize;

	/** Size of one program header table entry. */
	uint16_t e_phentsize;

	/** Number of program header table entries. */
	uint16_t e_phnum;

	/** Size of one section header table entry. */
	uint16_t e_shentsize;

	/** Number of section header table entries. */
	uint16_t e_shnum;

	/** Index into section header table where section names are kept. */
	uint16_t e_shstrndx;
};

/** ELF64 header for ELF binaries of class \ref ELFCLASS64. */
struct __packed elf64_header {
	/** Common identity header. */
	struct elf_ident e_ident;

	/** ELF type. \see ET_NONE, ET_REL, ET_EXEC, ET_DYN, ET_CORE. */
	uint16_t e_type;

	/** Machine architecture. \see EM_RISCV. */
	uint16_t e_machine;

	/** ELF version. \c 1 for current, \c 0 for undefined. */
	uint32_t e_version;

	/** Entrypoint virtual address. */
	uint64_t e_entry;

	/** Start of program header table within binary image. */
	uint64_t e_phoff;

	/** Start of section header table within binary image. */
	uint64_t e_shoff;

	/** Architecture dependent flags. */
	uint32_t e_flags;

	/** Size of this header. 64 bytes for ELF64. */
	uint16_t e_ehsize;

	/** Size of one program header table entry. */
	uint16_t e_phentsize;

	/** Number of program header table entries. */
	uint16_t e_phnum;

	/** Size of one section header table entry. */
	uint16_t e_shentsize;

	/** Number of section header table entries. */
	uint16_t e_shnum;

	/** Index into section header table where section names are kept. */
	uint16_t e_shstrndx;
};

/** @name Program header table entry types. */
/** @{ */

/** Null header type. */
#define PT_NULL 0x0

/** Load header type. */
#define PT_LOAD 0x1

/** Dynamic header type. */
#define PT_DYNAMIC 0x2

/** Interpreter header type. */
#define PT_INTERP 0x3

/** Note header type. */
#define PT_NOTE 0x4

/** Reserved, unsepcifier header type. */
#define PT_SHLIB 0x5

/** Program header table header type. */
#define PT_PHDR 0x6

/** Thread local storage header type. */
#define PT_TLS 0x7

/** Operating system specific low fence. */
#define PT_LOOS 0x60000000

/** Operating system specific high fence. */
#define PT_HIOS 0x6fffffff

/** Processor specific low fence. */
#define PT_LOPROC 0x70000000

/** Processor specific high fence. */
#define PT_HIPROC 0x7fffffff

/** @} */

/** @name Program header entry access types. */
/** @{ */

/** Executable. */
#define PF_X (1 << 0)

/** Writable. */
#define PF_W (1 << 1)

/** Readable. */
#define PF_R (1 << 2)

/** @} */

/** ELF32 program header table entry. \see ELFCLASS32. */
struct __packed program32_header {
	/** Program header entry type. \see PT_NULL, PT_LOAD, PT_DYNAMIC,
	 * PT_INTERP, PT_NOTE, PT_SHLIB, PT_PHDR, PT_TLS, PT_LOOS, PT_HIOS,
	 * PT_LOPROC, PT_HIPROC. */
	uint32_t p_type;

	/** Offset in the binary image where the associated segment lies. */
	uint32_t p_offset;

	/** Virtual address to first byte in segment. */
	uint32_t p_vaddr;

	/** Physical address to first byte in segment. Currently ignored. */
	uint32_t p_paddr;

	/** Segment size as bytes in binary image. */
	uint32_t p_filesz;

	/** Segment size as bytes in memory. */
	uint32_t p_memsz;

	/** Access flags for segment. \see PF_X, PF_W, PF_R. */
	uint32_t p_flags;

	/** Alignment of segment. */
	uint32_t p_align;
};

/** ELF64 program header table entry. \see ELFCLASS64. */
struct __packed program64_header {
	/** Program header entry type. \see PT_NULL, PT_LOAD, PT_DYNAMIC,
	 * PT_INTERP, PT_NOTE, PT_SHLIB, PT_PHDR, PT_TLS, PT_LOOS, PT_HIOS,
	 * PT_LOPROC, PT_HIPROC. */
	uint32_t p_type;

	/** Access flags for segment. \see PF_X, PF_W, PF_R. */
	uint32_t p_flags;

	/** Offset in the binary image where the associated segment lies. */
	uint64_t p_offset;

	/** Virtual address to first byte in segment. */
	uint64_t p_vaddr;

	/** Physical address to first byte in segment. Currently ignored. */
	uint64_t p_paddr;

	/** Segment size as bytes in binary image. */
	uint64_t p_filesz;

	/** Segment size as bytes in memory. */
	uint64_t p_memsz;

	/** Alignment of segment. */
	uint64_t p_align;
};

/** @name Section header types. */
/** @{ */

/** Null type. */
#define SHT_NULL 0x0

/** Section contains information defined by the program. */
#define SHT_PROGBITS 0x1

/** Section contains symbol table. */
#define SHT_SYMTAB 0x2

/** Section contains string table. */
#define SHT_STRTAB 0x3

/** Section contains relocation table with explicit addends. */
#define SHT_RELA 0x4

/** Section contains hashes. */
#define SHT_HASH 0x5

/** Section contains dynamic linking information. */
#define SHT_DYNAMIC 0x6

/** Section contains notes. */
#define SHT_NOTE 0x7

/** Section occupies no space in binary image. */
#define SHT_NOBITS 0x8

/** Section contains relocation table. */
#define SHT_REL 0x9

/** Reserved, unspecified semantincs. */
#define SHT_SHLIB 0x0a

/** Sections hold symbol table. */
#define SHT_DYNSYM 0x0b

/** Section contains array of pointers to initialization functions. */
#define SHT_INIT_ARRAY 0x0e

/** Section contains array of pointers to finalization functions. */
#define SHT_FINI_ARRAY 0x0f

/** Section contains array of pointers to preinitialization functions. */
#define SHT_PREINIT_ARRAY 0x10

/** Section group. */
#define SHT_GROUP 0x11

/** Section contains extended symbol table index. */
#define SHT_SYMTAB_SHNDX 0x12

/** Number of reserved SHT_* values. */
#define SHT_NUM 0x13

/** @} */

/** @name Section header flags. */
/** @{ */

/** Section should be writable during execution. */
#define SHF_WRITE 0x1

/** Section occupies memory during execution. */
#define SHF_ALLOC 0x2

/** Section contains executable machine instructions. */
#define SHF_EXECINSTR 0x4

/** Section might be merged. */
#define SHF_MERGE 0x10

/** Section contains null-terminated strings. */
#define SHF_STRINGS 0x20

/** Section contains section header table index. */
#define SHF_INFO_LINK 0x40

/** Preserve order after combining. */
#define SHF_LINK_ORDER 0x80

/** Non-standard OS specific handling. */
#define SHF_OS_NONCONFORMING 0x100

/** Section is a member of group. */
#define SHF_GROUP 0x200

/** Section hold thread local data. */
#define SHF_TLS 0x400

/** Operating system mask. */
#define SHF_MASKOS 0x0ff00000

/** Processor mask. */
#define SHF_MASKPROC 0xf0000000

/** @} */

/** ELF32 section header table entry. \see ELFCLASS32. */
struct __packed section32_header {
	/** Section header name. */
	uint32_t sh_name;

	/** Section header type. \see SHT_NULL, SHT_PROGBITS, SHT_SYMTAB,
	 * SHT_STRTAB, SHT_RELA, SHT_HASH, SHT_DYNAMIC, SHT_NOTE, SHT_NOBITS,
	 * SHT_REL, SHT_SHLIB, SHT_DYNSYM, SHT_INIT_ARRAY, SHT_FINI_ARRAY,
	 * SHT_PREINIT_ARRAY, SHT_GROUP, SHT_SYMTAB_SHNDX, SHT_NUM. */
	uint32_t sh_type;

	/** Section header flags. \see SHF_WRITE, SHF_ALLOC, SHF_EXECINSTR,
	 * SHF_MERGE, SHF_STRINGS, SHF_INFO_LINK, SHF_LINK_ORDER,
	 * SHF_OS_NONCONFORMING, SHF_GROUP, SHF_TLS, SHF_MASKOS, SHF_MASKPROC.
	 */
	uint32_t sh_flags;

	/** Section virtual address. */
	uint32_t sh_addr;

	/** Offset of section in binary image. */
	uint32_t sh_offset;

	/** Size as bytes in binary image. */
	uint32_t sh_size;

	/** Interpretation depends on section header type. */
	uint32_t sh_link;

	/** Interpretation depends on section header type. */
	uint32_t sh_info;

	/** Alignment of section address. */
	uint32_t sh_addralign;

	/** If section holds an array, each entry is of this size. */
	uint32_t sh_entsize;
};

/** ELF64 section header table entry. \see ELFCLASS64. */
struct __packed section64_header {
	/** Section header name. */
	uint32_t sh_name;

	/** Section header type. \see SHT_NULL, SHT_PROGBITS, SHT_SYMTAB,
	 * SHT_STRTAB, SHT_RELA, SHT_HASH, SHT_DYNAMIC, SHT_NOTE, SHT_NOBITS,
	 * SHT_REL, SHT_SHLIB, SHT_DYNSYM, SHT_INIT_ARRAY, SHT_FINI_ARRAY,
	 * SHT_PREINIT_ARRAY, SHT_GROUP, SHT_SYMTAB_SHNDX, SHT_NUM. */
	uint32_t sh_type;

	/** Section header flags. \see SHF_WRITE, SHF_ALLOC, SHF_EXECINSTR,
	 * SHF_MERGE, SHF_STRINGS, SHF_INFO_LINK, SHF_LINK_ORDER,
	 * SHF_OS_NONCONFORMING, SHF_GROUP, SHF_TLS, SHF_MASKOS, SHF_MASKPROC.
	 */
	uint64_t sh_flags;

	/** Section virtual address. */
	uint64_t sh_addr;

	/** Offset of section in binary image. */
	uint64_t sh_offset;

	/** Size as bytes in binary image. */
	uint64_t sh_size;

	/** Interpretation depends on section header type. */
	uint32_t sh_link;

	/** Interpretation depends on section header type. */
	uint32_t sh_info;

	/** Alignment of section address. */
	uint64_t sh_addralign;

	/** If section holds an array, each entry is of this size. */
	uint64_t s_entsize;
};

/**
 * Get ELF identity from pointer.
 *
 * @param e Pointer to ELF identity.
 * @return \c e as ELF identity.
 * \see elf_ident.
 */
#define elf_indent(e) ((struct elf_ident *)e)

/**
 * Get ELF64 header from pointer.
 *
 * @param e Pointer to ELF64 header.
 * @return \c e as ELF64 header.
 * \see elf64_header.
 */
#define elf64_header(e) ((struct elf64_header *)e)

/**
 * Get ELF32 header from pointer.
 *
 * @param e Pointer to ELF32 header.
 * @return \c e as ELF32 header.
 * \see elf32_header.
 */
#define elf32_header(e) ((struct elf32_header *)e)

/** Get ELF64 program header from pointer.
 *
 * @param p Pointer to ELF64 program header.
 * @return \c p as ELF64 program header.
 * \see program64_header.
 */
#define program64_header(p) ((struct program64_header *)p)

/**
 * Get ELF32 program header from pointer.
 *
 * @param p Pointer to ELF32 program header.
 * @return \c p as ELF32 program header.
 * \see program32_header.
 */
#define program32_header(p) ((struct program32_header *)p)

/**
 * Get ELF64 section header from pointer.
 *
 * @param s Pointer to ELF64 section header.
 * @return \c s as ELF64 section header.
 * \see section64_header.
 */
#define section64_header(s) ((struct section64_header *)s)

/**
 * Get ELF32 section header from pointer.
 *
 * @param s Pointer to ELF32 section header.
 * @return \c s as ELF32 section header.
 * \see section32_header.
 */
#define section32_header(s) ((struct section32_header *)s)

/**
 * Get either ELF32 or ELF64 header property, depending on the class.
 *
 * @param c ELF class. \see ELFCLASS32, ELFCLASS64.
 * @param e ELF header. \see elf32_header, elf64_header.
 * @param p Property to get.
 * @return \c p
 */
#define elf_header_prop(c, e, p) \
	(c == ELFCLASS64 ? elf64_header(e)->p : elf32_header(e)->p)

/**
 * Get Either ELF32 or ELF64 program header property, depending on the class.
 *
 * @param c ELF class. \see ELFCLASS32, ELFCLASS64.
 * @param e ELF program header. \see program32_header, program64_header.
 * @param p Property to get.
 * @return \c p.
 */
#define program_header_prop(c, e, p) \
	(c == ELFCLASS64 ? program64_header(e)->p : program32_header(e)->p)

/**
 * Get either ELF32 or ELF64 section header property, depending on the class.
 *
 * @param c ELF class. \see ELFCLASS32, ELFCLASS64.
 * @param e ELF section header. \see section32_header, section64_header.
 * @param p Property to get.
 * @return \c p.
 */
#define section_header_prop(c, e, p) \
	(c == ELFCLASS64 ? section64_header(e)->p : section32_header(e)->p)

/**
 * Build up ELF memory image from binary image.
 *
 * @param p Process space in which to build the memory image.
 * @param binary Virtual address of binary.
 * @param interp Virtual address of optional interpreter.
 * @return Virtual address of entry point.
 */
vm_t load_elf(struct tcb *p, vm_t binary, vm_t interp);

#endif /* APOS_ELF_H */