1111#ifndef DL_ELF_H__
1212#define DL_ELF_H__
1313
14+ #include "dlmodule.h"
15+
1416typedef rt_uint8_t Elf_Byte ;
1517
1618typedef rt_uint32_t Elf32_Addr ; /* Unsigned program address */
@@ -29,6 +31,7 @@ typedef rt_uint16_t Elf32_Half; /* Unsigned medium integer */
2931#define SELFMAG 4 /* size of magic */
3032
3133#define EI_CLASS 4 /* file class */
34+ #define EI_DATA 5 /* data encoding */
3235#define EI_NIDENT 16 /* Size of e_ident[] */
3336
3437/* e_ident[] file class */
@@ -56,8 +59,7 @@ typedef rt_uint16_t Elf32_Half; /* Unsigned medium integer */
5659#define ET_CORE 4 /* Core file */
5760
5861/* ELF Header */
59- typedef struct elfhdr
60- {
62+ typedef struct elfhdr {
6163 unsigned char e_ident [EI_NIDENT ]; /* ELF Identification */
6264 Elf32_Half e_type ; /* object file type */
6365 Elf32_Half e_machine ; /* machine */
@@ -76,8 +78,7 @@ typedef struct elfhdr
7678} Elf32_Ehdr ;
7779
7880/* Section Header */
79- typedef struct
80- {
81+ typedef struct {
8182 Elf32_Word sh_name ; /* name - index into section header
8283 string table section */
8384 Elf32_Word sh_type ; /* type */
@@ -116,8 +117,7 @@ typedef struct
116117#define ELF_RTMSYMTAB "RTMSymTab"
117118
118119/* Symbol Table Entry */
119- typedef struct elf32_sym
120- {
120+ typedef struct elf32_sym {
121121 Elf32_Word st_name ; /* name - index into string table */
122122 Elf32_Addr st_value ; /* symbol value */
123123 Elf32_Word st_size ; /* symbol size */
@@ -147,22 +147,18 @@ typedef struct elf32_sym
147147#define STT_LOPROC 13 /* processor specific range */
148148#define STT_HIPROC 15
149149
150- #define STN_UNDEF 0 /* undefined */
151-
152150#define ELF_ST_BIND (info ) ((info) >> 4)
153151#define ELF_ST_TYPE (info ) ((info) & 0xf)
154152#define ELF_ST_INFO (bind , type ) (((bind)<<4)+((type)&0xf))
155153
156154/* Relocation entry with implicit addend */
157- typedef struct
158- {
155+ typedef struct {
159156 Elf32_Addr r_offset ; /* offset of relocation */
160157 Elf32_Word r_info ; /* symbol table index and type */
161158} Elf32_Rel ;
162159
163160/* Relocation entry with explicit addend */
164- typedef struct
165- {
161+ typedef struct {
166162 Elf32_Addr r_offset ; /* offset of relocation */
167163 Elf32_Word r_info ; /* symbol table index and type */
168164 Elf32_Sword r_addend ;
@@ -173,6 +169,9 @@ typedef struct
173169#define ELF32_R_TYPE (i ) ((unsigned char) (i))
174170#define ELF32_R_INFO (s ,t ) (((s) << 8) + (unsigned char)(t))
175171
172+ /* ELF32_R_SYM */
173+ #define STN_UNDEF 0
174+
176175/*
177176 * Relocation type for arm
178177 */
@@ -207,8 +206,7 @@ typedef struct
207206#define R_386_GOTPC 10
208207
209208/* Program Header */
210- typedef struct
211- {
209+ typedef struct {
212210 Elf32_Word p_type ; /* segment type */
213211 Elf32_Off p_offset ; /* segment offset */
214212 Elf32_Addr p_vaddr ; /* virtual address of segment */
@@ -219,6 +217,63 @@ typedef struct
219217 Elf32_Word p_align ; /* memory alignment */
220218} Elf32_Phdr ;
221219
220+ /* d_tag */
221+ #define DT_NULL 0
222+ #define DT_NEEDED 1
223+ #define DT_PLTRELSZ 2
224+ #define DT_PLTGOT 3
225+ #define DT_HASH 4
226+ #define DT_STRTAB 5
227+ #define DT_SYMTAB 6
228+ #define DT_RELA 7
229+ #define DT_RELASZ 8
230+ #define DT_RELAENT 9
231+ #define DT_STRSZ 10
232+ #define DT_SYMENT 11
233+ #define DT_INIT 12
234+ #define DT_FINI 13
235+ #define DT_SONAME 14
236+ #define DT_RPATH 15
237+ #define DT_SYMBOLIC 16
238+ #define DT_REL 17
239+ #define DT_RELSZ 18
240+ #define DT_RELENT 19
241+ #define DT_PLTREL 20
242+ #define DT_DEBUG 21
243+ #define DT_TEXTREL 22
244+ #define DT_JMPREL 23
245+ #define DT_INIT_ARRAY 25
246+ #define DT_FINI_ARRAY 26
247+ #define DT_INIT_ARRAYSZ 27
248+ #define DT_FINI_ARRAYSZ 28
249+ #define DT_ENCODING 32
250+ #define OLD_DT_LOOS 0x60000000
251+ #define DT_LOOS 0x6000000d
252+ #define DT_HIOS 0x6ffff000
253+ #define DT_VALRNGLO 0x6ffffd00
254+ #define DT_VALRNGHI 0x6ffffdff
255+ #define DT_ADDRRNGLO 0x6ffffe00
256+ #define DT_ADDRRNGHI 0x6ffffeff
257+ #define DT_VERSYM 0x6ffffff0
258+ #define DT_RELACOUNT 0x6ffffff9
259+ #define DT_RELCOUNT 0x6ffffffa
260+ #define DT_FLAGS_1 0x6ffffffb
261+ #define DT_VERDEF 0x6ffffffc
262+ #define DT_VERDEFNUM 0x6ffffffd
263+ #define DT_VERNEED 0x6ffffffe
264+ #define DT_VERNEEDNUM 0x6fffffff
265+ #define OLD_DT_HIOS 0x6fffffff
266+ #define DT_LOPROC 0x70000000
267+ #define DT_HIPROC 0x7fffffff
268+
269+ typedef struct {
270+ Elf32_Sword d_tag ;
271+ union {
272+ Elf32_Word d_val ;
273+ Elf32_Addr d_ptr ;
274+ } d_un ;
275+ } Elf32_Dyn ;
276+
222277/* p_type */
223278#define PT_NULL 0
224279#define PT_LOAD 1
@@ -244,7 +299,7 @@ typedef struct
244299#define SHT_PROGBITS 1 /* program defined information */
245300#define SHT_SYMTAB 2 /* symbol table section */
246301#define SHT_STRTAB 3 /* string table section */
247- #define SHT_RELA 4 /* relocation section with addends*/
302+ #define SHT_RELA 4 /* relocation section with addends */
248303#define SHT_HASH 5 /* symbol hash table section */
249304#define SHT_DYNAMIC 6 /* dynamic section */
250305#define SHT_NOTE 7 /* note section */
@@ -258,28 +313,35 @@ typedef struct
258313#define SHT_LOUSER 0x80000000 /* reserved range for application */
259314#define SHT_HIUSER 0xffffffff /* specific indexes */
260315
316+ /* st_shndx */
317+ #define SHN_UNDEF 0 /* undefined */
318+
261319/* Section Attribute Flags - sh_flags */
262320#define SHF_WRITE 0x1 /* Writable */
263321#define SHF_ALLOC 0x2 /* occupies memory */
264322#define SHF_EXECINSTR 0x4 /* executable */
265323#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */
266324/* specific section attributes */
267325
268- #define IS_PROG (s ) (s.sh_type == SHT_PROGBITS)
269- #define IS_NOPROG (s ) (s.sh_type == SHT_NOBITS)
270- #define IS_REL (s ) (s.sh_type == SHT_REL)
271- #define IS_RELA (s ) (s.sh_type == SHT_RELA)
272- #define IS_ALLOC (s ) (s.sh_flags == SHF_ALLOC)
273- #define IS_AX (s ) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
274- #define IS_AW (s ) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
275-
276- #define elf_module ((Elf32_Ehdr *)module_ptr)
277- #define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
278- #define phdr ((Elf32_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff))
279-
280- rt_err_t dlmodule_load_shared_object (struct rt_dlmodule * module , void * module_ptr );
281- rt_err_t dlmodule_load_relocated_object (struct rt_dlmodule * module , void * module_ptr );
282-
283- int dlmodule_relocate (struct rt_dlmodule * module , Elf32_Rel * rel , Elf32_Addr sym_val );
326+ #define IS_PROG (sh ) (sh->sh_type == SHT_PROGBITS)
327+ #define IS_NOPROG (sh ) (sh->sh_type == SHT_NOBITS)
328+ #define IS_REL (sh ) (sh->sh_type == SHT_REL)
329+ #define IS_RELA (sh ) (sh->sh_type == SHT_RELA)
330+ #define IS_DYNSYM (sh ) (sh->sh_type == SHT_DYNSYM)
331+ #define IS_ALLOC (sh ) (sh->sh_flags == SHF_ALLOC)
332+ #define IS_AX (sh ) ((sh->sh_flags & SHF_ALLOC) && \
333+ (sh->sh_flags & SHF_EXECINSTR))
334+ #define IS_AW (sh ) ((sh->sh_flags & SHF_ALLOC) && \
335+ (sh->sh_flags & SHF_WRITE))
336+
337+
338+ rt_err_t dlmodule_load_shared_object (int fd , Elf32_Ehdr * elf_hdr ,
339+ rt_dlmodule_t * module );
340+ #ifndef __arm__
341+ rt_err_t dlmodule_load_relocated_object (int fd , Elf32_Ehdr * elf_hdr ,
342+ rt_dlmodule_t * module );
343+ #endif
344+ Elf32_Addr dlmodule_relocate (rt_uint8_t rel_type , Elf32_Addr * where ,
345+ Elf32_Addr sym_val );
284346
285347#endif
0 commit comments