diff options
Diffstat (limited to 'utils/sbtools/elf.h')
-rw-r--r-- | utils/sbtools/elf.h | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/utils/sbtools/elf.h b/utils/sbtools/elf.h new file mode 100644 index 0000000000..d2bf210c5c --- /dev/null +++ b/utils/sbtools/elf.h | |||
@@ -0,0 +1,179 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdint.h> | ||
3 | #include <string.h> | ||
4 | #include <stdbool.h> | ||
5 | #include <stdlib.h> | ||
6 | #include <unistd.h> | ||
7 | |||
8 | /** | ||
9 | * Definitions | ||
10 | * taken from elf.h linux header | ||
11 | * based on ELF specification | ||
12 | * based on ARM ELF specification | ||
13 | */ | ||
14 | typedef uint16_t Elf32_Half; | ||
15 | |||
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; | ||
21 | |||
22 | #define EI_NIDENT 16 | ||
23 | |||
24 | typedef struct | ||
25 | { | ||
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; | ||
41 | |||
42 | #define EI_MAG0 0 /* File identification byte 0 index */ | ||
43 | #define ELFMAG0 0x7f /* Magic number byte 0 */ | ||
44 | |||
45 | #define EI_MAG1 1 /* File identification byte 1 index */ | ||
46 | #define ELFMAG1 'E' /* Magic number byte 1 */ | ||
47 | |||
48 | #define EI_MAG2 2 /* File identification byte 2 index */ | ||
49 | #define ELFMAG2 'L' /* Magic number byte 2 */ | ||
50 | |||
51 | #define EI_MAG3 3 /* File identification byte 3 index */ | ||
52 | #define ELFMAG3 'F' /* Magic number byte 3 */ | ||
53 | |||
54 | #define EI_CLASS 4 /* File class byte index */ | ||
55 | #define ELFCLASS32 1 /* 32-bit objects */ | ||
56 | |||
57 | #define EI_DATA 5 /* Data encoding byte index */ | ||
58 | #define ELFDATA2LSB 1 /* 2's complement, little endian */ | ||
59 | |||
60 | #define EI_VERSION 6 /* File version byte index, Value must be EV_CURRENT */ | ||
61 | |||
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 */ | ||
66 | |||
67 | #define EI_ABIVERSION 8 /* ABI version */ | ||
68 | |||
69 | #define EI_PAD 9 /* Byte index of padding bytes */ | ||
70 | |||
71 | #define ET_EXEC 2 /* Executable file */ | ||
72 | |||
73 | #define EM_ARM 40 /* ARM */ | ||
74 | |||
75 | #define EV_CURRENT 1 /* Current version */ | ||
76 | |||
77 | #define EF_ARM_HASENTRY 0x00000002 | ||
78 | |||
79 | #define SHN_UNDEF 0 /* Undefined section */ | ||
80 | |||
81 | typedef struct | ||
82 | { | ||
83 | Elf32_Word sh_name; /* Section name (string tbl index) */ | ||
84 | Elf32_Word sh_type; /* Section type */ | ||
85 | Elf32_Word sh_flags; /* Section flags */ | ||
86 | Elf32_Addr sh_addr; /* Section virtual addr at execution */ | ||
87 | Elf32_Off sh_offset; /* Section file offset */ | ||
88 | Elf32_Word sh_size; /* Section size in bytes */ | ||
89 | Elf32_Word sh_link; /* Link to another section */ | ||
90 | Elf32_Word sh_info; /* Additional section information */ | ||
91 | Elf32_Word sh_addralign; /* Section alignment */ | ||
92 | Elf32_Word sh_entsize; /* Entry size if section holds table */ | ||
93 | }Elf32_Shdr; | ||
94 | |||
95 | #define SHT_NULL 0 /* Section header table entry unused */ | ||
96 | #define SHT_PROGBITS 1 /* Program data */ | ||
97 | #define SHT_SYMTAB 2 /* Symbol table */ | ||
98 | #define SHT_STRTAB 3 /* String table */ | ||
99 | #define SHT_RELA 4 /* Relocation entries with addends */ | ||
100 | #define SHT_HASH 5 /* Symbol hash table */ | ||
101 | #define SHT_DYNAMIC 6 /* Dynamic linking information */ | ||
102 | #define SHT_NOTE 7 /* Notes */ | ||
103 | #define SHT_NOBITS 8 /* Program space with no data (bss) */ | ||
104 | #define SHT_REL 9 /* Relocation entries, no addends */ | ||
105 | #define SHT_SHLIB 10 /* Reserved */ | ||
106 | #define SHT_DYNSYM 11 /* Dynamic linker symbol table */ | ||
107 | #define SHT_INIT_ARRAY 14 /* Array of constructors */ | ||
108 | #define SHT_FINI_ARRAY 15 /* Array of destructors */ | ||
109 | #define SHT_PREINIT_ARRAY 16 /* Array of pre-constructors */ | ||
110 | #define SHT_GROUP 17 /* Section group */ | ||
111 | #define SHT_SYMTAB_SHNDX 18 /* Extended section indeces */ | ||
112 | #define SHT_NUM 19 /* Number of defined types. */ | ||
113 | |||
114 | #define SHF_WRITE (1 << 0) /* Writable */ | ||
115 | #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */ | ||
116 | #define SHF_EXECINSTR (1 << 2) /* Executable */ | ||
117 | #define SHF_MERGE (1 << 4) /* Might be merged */ | ||
118 | #define SHF_STRINGS (1 << 5) /* Contains nul-terminated strings */ | ||
119 | |||
120 | typedef struct | ||
121 | { | ||
122 | Elf32_Word p_type; /* Segment type */ | ||
123 | Elf32_Off p_offset; /* Segment file offset */ | ||
124 | Elf32_Addr p_vaddr; /* Segment virtual address */ | ||
125 | Elf32_Addr p_paddr; /* Segment physical address */ | ||
126 | Elf32_Word p_filesz; /* Segment size in file */ | ||
127 | Elf32_Word p_memsz; /* Segment size in memory */ | ||
128 | Elf32_Word p_flags; /* Segment flags */ | ||
129 | Elf32_Word p_align; /* Segment alignment */ | ||
130 | }Elf32_Phdr; | ||
131 | |||
132 | #define PT_LOAD 1 /* Loadable program segment */ | ||
133 | |||
134 | #define PF_X (1 << 0) /* Segment is executable */ | ||
135 | #define PF_W (1 << 1) /* Segment is writable */ | ||
136 | #define PF_R (1 << 2) /* Segment is readable */ | ||
137 | |||
138 | /** | ||
139 | * API | ||
140 | */ | ||
141 | enum elf_section_type_t | ||
142 | { | ||
143 | EST_LOAD, | ||
144 | EST_FILL | ||
145 | }; | ||
146 | |||
147 | struct elf_section_t | ||
148 | { | ||
149 | uint32_t addr; | ||
150 | uint32_t size; | ||
151 | enum elf_section_type_t type; | ||
152 | /* <union> */ | ||
153 | void *section; | ||
154 | uint32_t pattern; | ||
155 | /* </union> */ | ||
156 | struct elf_section_t *next; | ||
157 | /* Internal to elf_output */ | ||
158 | uint32_t offset; | ||
159 | }; | ||
160 | |||
161 | struct elf_params_t | ||
162 | { | ||
163 | bool has_start_addr; | ||
164 | uint32_t start_addr; | ||
165 | struct elf_section_t *first_section; | ||
166 | struct elf_section_t *last_section; | ||
167 | }; | ||
168 | |||
169 | typedef void (*elf_write_fn_t)(void *user, uint32_t addr, const void *buf, size_t count); | ||
170 | |||
171 | void elf_init(struct elf_params_t *params); | ||
172 | void elf_add_load_section(struct elf_params_t *params, | ||
173 | uint32_t load_addr, uint32_t size, const void *section); | ||
174 | void elf_add_fill_section(struct elf_params_t *params, | ||
175 | uint32_t fill_addr, uint32_t size, uint32_t pattern); | ||
176 | void elf_output(struct elf_params_t *params, elf_write_fn_t write, void *user); | ||
177 | bool elf_is_empty(struct elf_params_t *params); | ||
178 | void elf_set_start_addr(struct elf_params_t *params, uint32_t addr); | ||
179 | void elf_release(struct elf_params_t *params); | ||