Skip to content

Commit e17fd49

Browse files
committed
[elf] Rearrange code to adapt Arduino build process
1 parent 531b8a5 commit e17fd49

File tree

19 files changed

+166
-56
lines changed

19 files changed

+166
-56
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=RT-Thread
2-
version=0.5.1
2+
version=0.6.0
33
author=Bernard Xiong <bernard.xiong@gmail.com>, onelife <onelife.real@gmail.com>
44
maintainer=onelife <onelife.real@gmail.com>
55
sentence=Real Time Operating System porting for Arduino SAM and SAMD boards

src/components/finsh/msh.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#endif
2323

2424
#ifdef RT_USING_MODULE
25-
#include <dlmodule.h>
25+
#include "components/libc/libdl/dlmodule.h"
2626
#endif
2727

2828
#ifndef FINSH_ARG_MAX

components/libc/libdl/arch/arm.c renamed to src/components/libc/libdl/arch/arm.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
* 2018/08/29 Bernard first version
99
*/
1010

11-
#include "../dlmodule.h"
11+
#include "include/rtthread.h"
12+
13+
#ifdef RT_USING_MODULE
14+
#ifdef __arm__
15+
1216
#include "../dlelf.h"
1317

1418
#ifdef __arm__
@@ -118,4 +122,6 @@ int dlmodule_relocate(struct rt_dlmodule *module, Elf32_Rel *rel, Elf32_Addr sym
118122

119123
return 0;
120124
}
121-
#endif
125+
126+
#endif /* __arm__ */
127+
#endif /* RT_USING_MODULE */

components/libc/libdl/dlclose.c renamed to src/components/libc/libdl/dlclose.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
* 2010-11-17 yi.qiu first version
99
*/
1010

11-
#include <rtthread.h>
12-
#include <rtm.h>
11+
#include "include/rtthread.h"
12+
13+
#ifdef RT_USING_MODULE
1314

1415
#include "dlmodule.h"
1516

@@ -37,3 +38,5 @@ int dlclose(void *handle)
3738
return RT_TRUE;
3839
}
3940
RTM_EXPORT(dlclose)
41+
42+
#endif /* RT_USING_MODULE */

components/libc/libdl/dlelf.c renamed to src/components/libc/libdl/dlelf.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
* 2018/08/29 Bernard first version
99
*/
1010

11-
#include "dlmodule.h"
11+
#include "include/rtthread.h"
12+
13+
#ifdef RT_USING_MODULE
14+
1215
#include "dlelf.h"
1316

1417
#define DBG_SECTION_NAME "DLMD"
@@ -441,3 +444,6 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
441444

442445
return RT_EOK;
443446
}
447+
448+
449+
#endif /* RT_USING_MODULE */

components/libc/libdl/dlelf.h renamed to src/components/libc/libdl/dlelf.h

Lines changed: 93 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#ifndef DL_ELF_H__
1212
#define DL_ELF_H__
1313

14+
#include "dlmodule.h"
15+
1416
typedef rt_uint8_t Elf_Byte;
1517

1618
typedef 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

components/libc/libdl/dlerror.c renamed to src/components/libc/libdl/dlerror.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
* 2010-11-17 yi.qiu first version
99
*/
1010

11-
#include <rtthread.h>
12-
#include <rtm.h>
11+
#include "include/rtthread.h"
1312

14-
const char *dlerror(void)
15-
{
16-
return "TODO";
13+
#ifdef RT_USING_MODULE
14+
15+
16+
const char *dlerror(void) {
17+
return "TODO";
1718
}
1819
RTM_EXPORT(dlerror)
20+
21+
#endif /* RT_USING_MODULE */
File renamed without changes.

components/libc/libdl/dlmodule.c renamed to src/components/libc/libdl/dlmodule.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
* 2018/08/29 Bernard first version
99
*/
1010

11-
#include <rthw.h>
11+
#include "include/rtthread.h"
1212

13+
#ifdef RT_USING_MODULE
14+
15+
#include "include/rthw.h"
1316
#include "dlfcn.h"
1417
#include "dlmodule.h"
1518
#include "dlelf.h"
19+
#include "components/dfs/include/dfs_posix.h"
1620

17-
#include <dfs_posix.h>
1821

1922
#define DBG_SECTION_NAME "DLMD"
2023
#define DBG_ENABLE // enable debug macro
@@ -697,3 +700,5 @@ int list_module(void)
697700
return 0;
698701
}
699702
MSH_CMD_EXPORT(list_module, list modules in system);
703+
704+
#endif /* RT_USING_MODULE */

components/libc/libdl/dlmodule.h renamed to src/components/libc/libdl/dlmodule.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#ifndef RT_DL_MODULE_H__
1212
#define RT_DL_MODULE_H__
1313

14-
#include <rtthread.h>
15-
1614
#define RT_DLMODULE_STAT_INIT 0x00
1715
#define RT_DLMODULE_STAT_RUNNING 0x01
1816
#define RT_DLMODULE_STAT_CLOSING 0x02

0 commit comments

Comments
 (0)