fix typo, thanks to Luka_S for catching this
[kugel-rb.git] / utils / sbinfo / elf.h
blobeb990389d9ace8cf3936c94f891781211c230217
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <string.h>
4 #include <stdbool.h>
5 #include <stdlib.h>
6 #include <unistd.h>
8 /**
9 * Definitions
10 * taken from elf.h linux header
11 * based on ELF specification
12 * based on ARM ELF specification
14 typedef uint16_t Elf32_Half;
16 typedef uint32_t Elf32_Word;
17 typedef int32_t Elf32_Sword;
18 typedef uint32_t Elf32_Addr;
19 typedef uint32_t Elf32_Off;
20 typedef uint16_t Elf32_Section;
22 #define EI_NIDENT 16
24 typedef struct
26 unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
27 Elf32_Half e_type; /* Object file type */
28 Elf32_Half e_machine; /* Architecture */
29 Elf32_Word e_version; /* Object file version */
30 Elf32_Addr e_entry; /* Entry point virtual address */
31 Elf32_Off e_phoff; /* Program header table file offset */
32 Elf32_Off e_shoff; /* Section header table file offset */
33 Elf32_Word e_flags; /* Processor-specific flags */
34 Elf32_Half e_ehsize; /* ELF header size in bytes */
35 Elf32_Half e_phentsize; /* Program header table entry size */
36 Elf32_Half e_phnum; /* Program header table entry count */
37 Elf32_Half e_shentsize; /* Section header table entry size */
38 Elf32_Half e_shnum; /* Section header table entry count */
39 Elf32_Half e_shstrndx; /* Section header string table index */
40 }Elf32_Ehdr;
42 #define EI_MAG0 0 /* File identification byte 0 index */
43 #define ELFMAG0 0x7f /* Magic number byte 0 */
45 #define EI_MAG1 1 /* File identification byte 1 index */
46 #define ELFMAG1 'E' /* Magic number byte 1 */
48 #define EI_MAG2 2 /* File identification byte 2 index */
49 #define ELFMAG2 'L' /* Magic number byte 2 */
51 #define EI_MAG3 3 /* File identification byte 3 index */
52 #define ELFMAG3 'F' /* Magic number byte 3 */
54 #define EI_CLASS 4 /* File class byte index */
55 #define ELFCLASS32 1 /* 32-bit objects */
57 #define EI_DATA 5 /* Data encoding byte index */
58 #define ELFDATA2LSB 1 /* 2's complement, little endian */
60 #define EI_VERSION 6 /* File version byte index, Value must be EV_CURRENT */
62 #define EI_OSABI 7 /* OS ABI identification */
63 #define ELFOSABI_NONE 0 /* UNIX System V ABI */
64 #define ELFOSABI_ARM_AEABI 64 /* ARM EABI */
65 #define ELFOSABI_ARM 97 /* ARM */
67 #define EI_ABIVERSION 8 /* ABI version */
69 #define EI_PAD 9 /* Byte index of padding bytes */
71 #define ET_EXEC 2 /* Executable file */
73 #define EM_ARM 40 /* ARM */
75 #define EV_CURRENT 1 /* Current version */
77 #define EF_ARM_HASENTRY 0x00000002
79 #define SHN_UNDEF 0 /* Undefined section */
81 typedef struct
83 Elf32_Word p_type; /* Segment type */
84 Elf32_Off p_offset; /* Segment file offset */
85 Elf32_Addr p_vaddr; /* Segment virtual address */
86 Elf32_Addr p_paddr; /* Segment physical address */
87 Elf32_Word p_filesz; /* Segment size in file */
88 Elf32_Word p_memsz; /* Segment size in memory */
89 Elf32_Word p_flags; /* Segment flags */
90 Elf32_Word p_align; /* Segment alignment */
91 }Elf32_Phdr;
93 #define PT_LOAD 1 /* Loadable program segment */
95 #define PF_X (1 << 0) /* Segment is executable */
96 #define PF_W (1 << 1) /* Segment is writable */
97 #define PF_R (1 << 2) /* Segment is readable */
99 /**
100 * API
102 enum elf_section_type_t
104 EST_LOAD,
105 EST_FILL
108 struct elf_section_t
110 uint32_t addr;
111 uint32_t size;
112 enum elf_section_type_t type;
113 /* <union> */
114 void *section;
115 uint32_t pattern;
116 /* </union> */
117 struct elf_section_t *next;
118 /* Internal to elf_output */
119 uint32_t offset;
122 struct elf_params_t
124 bool has_start_addr;
125 uint32_t start_addr;
126 struct elf_section_t *first_section;
127 struct elf_section_t *last_section;
130 typedef void (*elf_write_fn_t)(void *user, uint32_t addr, const void *buf, size_t count);
132 void elf_init(struct elf_params_t *params);
133 void elf_add_load_section(struct elf_params_t *params,
134 uint32_t load_addr, uint32_t size, const void *section);
135 void elf_add_fill_section(struct elf_params_t *params,
136 uint32_t fill_addr, uint32_t size, uint32_t pattern);
137 void elf_output(struct elf_params_t *params, elf_write_fn_t write, void *user);
138 bool elf_is_empty(struct elf_params_t *params);
139 void elf_set_start_addr(struct elf_params_t *params, uint32_t addr);
140 void elf_release(struct elf_params_t *params);