Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
红岛
rt-thread
提交
072899e1
R
rt-thread
项目概览
红岛
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
072899e1
编写于
4月 29, 2021
作者:
B
Bernard Xiong
提交者:
GitHub
4月 29, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4636 from chunyexixiaoyu/dev
dlmodule can support riscv architecture
上级
1c58d9f3
2afca007
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
236 addition
and
45 deletion
+236
-45
components/libc/libdl/arch/riscv.c
components/libc/libdl/arch/riscv.c
+60
-0
components/libc/libdl/dlelf.c
components/libc/libdl/dlelf.c
+61
-43
components/libc/libdl/dlelf.h
components/libc/libdl/dlelf.h
+114
-1
components/libc/libdl/dlmodule.c
components/libc/libdl/dlmodule.c
+1
-1
未找到文件。
components/libc/libdl/arch/riscv.c
0 → 100644
浏览文件 @
072899e1
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021/04/23 chunyexixiaoyu first version
*/
#include "../dlmodule.h"
#include "../dlelf.h"
#if (__riscv_xlen == 64)
#define R_RISCV_NONE 0
#define R_RISCV_32 1
#define R_RISCV_64 2
#define R_RISCV_RELATIVE 3
#define R_RISCV_COPY 4
#define R_RISCV_JUMP_SLOT 5
#define R_RISCV_TLS_DTPMOD32 6
#define R_RISCV_TLS_DTPMOD64 7
#define R_RISCV_TLS_DTPREL32 8
#define R_RISCV_TLS_DTPREL64 9
#define R_RISCV_TLS_TPREL32 10
#define R_RISCV_TLS_TPREL64 11
int
dlmodule_relocate
(
struct
rt_dlmodule
*
module
,
Elf_Rel
*
rel
,
Elf_Addr
sym_val
)
{
Elf64_Addr
*
where
,
tmp
;
Elf64_Sword
addend
,
offset
;
rt_uint64_t
upper
,
lower
,
sign
,
j1
,
j2
;
where
=
(
Elf64_Addr
*
)((
rt_uint8_t
*
)
module
->
mem_space
+
rel
->
r_offset
-
module
->
vstart_addr
);
switch
(
ELF64_R_TYPE
(
rel
->
r_info
))
{
case
R_RISCV_NONE
:
break
;
case
R_RISCV_64
:
*
where
=
(
Elf64_Addr
)(
sym_val
+
rel
->
r_addend
);
RT_DEBUG_LOG
(
RT_DEBUG_MODULE
,
(
"R_RISCV_64: %x -> %x
\n
"
,
where
,
*
where
));
break
;
case
R_RISCV_RELATIVE
:
*
where
=
(
Elf64_Addr
)((
rt_uint8_t
*
)
module
->
mem_space
-
module
->
vstart_addr
+
rel
->
r_addend
);
RT_DEBUG_LOG
(
RT_DEBUG_MODULE
,
(
"R_RISCV_RELATIVE: %x -> %x
\n
"
,
where
,
*
where
));
break
;
case
R_RISCV_JUMP_SLOT
:
*
where
=
(
Elf64_Addr
)
sym_val
;
RT_DEBUG_LOG
(
RT_DEBUG_MODULE
,
(
"R_RISCV_JUMP_SLOT: %x -> %x
\n
"
,
where
,
*
where
));
break
;
default:
RT_DEBUG_LOG
(
RT_DEBUG_MODULE
,
(
"__riscv__ELF: invalid relocate TYPE %d
\n
"
,
ELF64_R_TYPE
(
rel
->
r_info
)));
return
-
1
;
}
return
0
;
}
#endif
components/libc/libdl/dlelf.c
浏览文件 @
072899e1
...
@@ -6,6 +6,7 @@
...
@@ -6,6 +6,7 @@
* Change Logs:
* Change Logs:
* Date Author Notes
* Date Author Notes
* 2018/08/29 Bernard first version
* 2018/08/29 Bernard first version
* 2021/04/23 chunyexixiaoyu distinguish 32-bit and 64-bit
*/
*/
#include "dlmodule.h"
#include "dlmodule.h"
...
@@ -18,8 +19,8 @@
...
@@ -18,8 +19,8 @@
rt_err_t
dlmodule_load_shared_object
(
struct
rt_dlmodule
*
module
,
void
*
module_ptr
)
rt_err_t
dlmodule_load_shared_object
(
struct
rt_dlmodule
*
module
,
void
*
module_ptr
)
{
{
rt_bool_t
linked
=
RT_FALSE
;
rt_bool_t
linked
=
RT_FALSE
;
rt_u
int32_t
index
,
module_size
=
0
;
rt_u
base_t
index
,
module_size
=
0
;
Elf
32
_Addr
vstart_addr
,
vend_addr
;
Elf_Addr
vstart_addr
,
vend_addr
;
rt_bool_t
has_vstart
;
rt_bool_t
has_vstart
;
RT_ASSERT
(
module_ptr
!=
RT_NULL
);
RT_ASSERT
(
module_ptr
!=
RT_NULL
);
...
@@ -53,7 +54,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
...
@@ -53,7 +54,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
has_vstart
=
RT_TRUE
;
has_vstart
=
RT_TRUE
;
if
(
vend_addr
<
vstart_addr
)
if
(
vend_addr
<
vstart_addr
)
{
{
rt_kprintf
(
"invalid elf: segment %d: p_vaddr: %d, p_memsz: %d
\n
"
,
LOG_E
(
"invalid elf: segment %d: p_vaddr: %d, p_memsz: %d
\n
"
,
index
,
phdr
[
index
].
p_vaddr
,
phdr
[
index
].
p_memsz
);
index
,
phdr
[
index
].
p_vaddr
,
phdr
[
index
].
p_memsz
);
return
RT_NULL
;
return
RT_NULL
;
}
}
...
@@ -62,7 +63,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
...
@@ -62,7 +63,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
{
{
if
(
phdr
[
index
].
p_vaddr
<
vend_addr
)
if
(
phdr
[
index
].
p_vaddr
<
vend_addr
)
{
{
rt_kprintf
(
"invalid elf: segment should be sorted and not overlapped
\n
"
);
LOG_E
(
"invalid elf: segment should be sorted and not overlapped
\n
"
);
return
RT_NULL
;
return
RT_NULL
;
}
}
if
(
phdr
[
index
].
p_vaddr
>
vend_addr
+
16
)
if
(
phdr
[
index
].
p_vaddr
>
vend_addr
+
16
)
...
@@ -74,7 +75,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
...
@@ -74,7 +75,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
vend_addr
=
phdr
[
index
].
p_vaddr
+
phdr
[
index
].
p_memsz
;
vend_addr
=
phdr
[
index
].
p_vaddr
+
phdr
[
index
].
p_memsz
;
if
(
vend_addr
<
phdr
[
index
].
p_vaddr
)
if
(
vend_addr
<
phdr
[
index
].
p_vaddr
)
{
{
rt_kprintf
(
"invalid elf: "
LOG_E
(
"invalid elf: "
"segment %d address overflow
\n
"
,
index
);
"segment %d address overflow
\n
"
,
index
);
return
RT_NULL
;
return
RT_NULL
;
}
}
...
@@ -85,7 +86,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
...
@@ -85,7 +86,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
LOG_D
(
"module size: %d, vstart_addr: 0x%p"
,
module_size
,
vstart_addr
);
LOG_D
(
"module size: %d, vstart_addr: 0x%p"
,
module_size
,
vstart_addr
);
if
(
module_size
==
0
)
if
(
module_size
==
0
)
{
{
rt_kprintf
(
"Module: size error
\n
"
);
LOG_E
(
"Module: size error
\n
"
);
return
-
RT_ERROR
;
return
-
RT_ERROR
;
}
}
...
@@ -96,7 +97,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
...
@@ -96,7 +97,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
module
->
mem_space
=
rt_malloc
(
module_size
);
module
->
mem_space
=
rt_malloc
(
module_size
);
if
(
module
->
mem_space
==
RT_NULL
)
if
(
module
->
mem_space
==
RT_NULL
)
{
{
rt_kprintf
(
"Module: allocate space failed.
\n
"
);
LOG_E
(
"Module: allocate space failed.
\n
"
);
return
-
RT_ERROR
;
return
-
RT_ERROR
;
}
}
module
->
mem_size
=
module_size
;
module
->
mem_size
=
module_size
;
...
@@ -119,42 +120,49 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
...
@@ -119,42 +120,49 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
/* handle relocation section */
/* handle relocation section */
for
(
index
=
0
;
index
<
elf_module
->
e_shnum
;
index
++
)
for
(
index
=
0
;
index
<
elf_module
->
e_shnum
;
index
++
)
{
{
rt_u
int32
_t
i
,
nr_reloc
;
rt_u
base
_t
i
,
nr_reloc
;
Elf
32
_Sym
*
symtab
;
Elf_Sym
*
symtab
;
Elf
32
_Rel
*
rel
;
Elf_Rel
*
rel
;
rt_uint8_t
*
strtab
;
rt_uint8_t
*
strtab
;
static
rt_bool_t
unsolved
=
RT_FALSE
;
static
rt_bool_t
unsolved
=
RT_FALSE
;
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
if
(
!
IS_REL
(
shdr
[
index
]))
if
(
!
IS_REL
(
shdr
[
index
]))
continue
;
continue
;
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
if
(
!
IS_RELA
(
shdr
[
index
]))
continue
;
#endif
/* get relocate item */
/* get relocate item */
rel
=
(
Elf
32
_Rel
*
)((
rt_uint8_t
*
)
module_ptr
+
shdr
[
index
].
sh_offset
);
rel
=
(
Elf_Rel
*
)((
rt_uint8_t
*
)
module_ptr
+
shdr
[
index
].
sh_offset
);
/* locate .rel.plt and .rel.dyn section */
/* locate .rel.plt and .rel.dyn section */
symtab
=
(
Elf
32
_Sym
*
)((
rt_uint8_t
*
)
module_ptr
+
symtab
=
(
Elf_Sym
*
)((
rt_uint8_t
*
)
module_ptr
+
shdr
[
shdr
[
index
].
sh_link
].
sh_offset
);
shdr
[
shdr
[
index
].
sh_link
].
sh_offset
);
strtab
=
(
rt_uint8_t
*
)
module_ptr
+
strtab
=
(
rt_uint8_t
*
)
module_ptr
+
shdr
[
shdr
[
shdr
[
index
].
sh_link
].
sh_link
].
sh_offset
;
shdr
[
shdr
[
shdr
[
index
].
sh_link
].
sh_link
].
sh_offset
;
nr_reloc
=
(
rt_u
int32_t
)(
shdr
[
index
].
sh_size
/
sizeof
(
Elf32
_Rel
));
nr_reloc
=
(
rt_u
base_t
)(
shdr
[
index
].
sh_size
/
sizeof
(
Elf
_Rel
));
/* relocate every items */
/* relocate every items */
for
(
i
=
0
;
i
<
nr_reloc
;
i
++
)
for
(
i
=
0
;
i
<
nr_reloc
;
i
++
)
{
{
Elf32_Sym
*
sym
=
&
symtab
[
ELF32_R_SYM
(
rel
->
r_info
)];
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
Elf_Sym
*
sym
=
&
symtab
[
ELF32_R_SYM
(
rel
->
r_info
)];
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
Elf_Sym
*
sym
=
&
symtab
[
ELF64_R_SYM
(
rel
->
r_info
)];
#endif
LOG_D
(
"relocate symbol %s shndx %d"
,
strtab
+
sym
->
st_name
,
sym
->
st_shndx
);
LOG_D
(
"relocate symbol %s shndx %d"
,
strtab
+
sym
->
st_name
,
sym
->
st_shndx
);
if
((
sym
->
st_shndx
!=
SHT_NULL
)
||
(
ELF_ST_BIND
(
sym
->
st_info
)
==
STB_LOCAL
))
if
((
sym
->
st_shndx
!=
SHT_NULL
)
||
(
ELF_ST_BIND
(
sym
->
st_info
)
==
STB_LOCAL
))
{
{
Elf
32
_Addr
addr
;
Elf_Addr
addr
;
addr
=
(
Elf
32
_Addr
)(
module
->
mem_space
+
sym
->
st_value
-
vstart_addr
);
addr
=
(
Elf_Addr
)(
module
->
mem_space
+
sym
->
st_value
-
vstart_addr
);
dlmodule_relocate
(
module
,
rel
,
addr
);
dlmodule_relocate
(
module
,
rel
,
addr
);
}
}
else
if
(
!
linked
)
else
if
(
!
linked
)
{
{
Elf
32
_Addr
addr
;
Elf_Addr
addr
;
LOG_D
(
"relocate symbol: %s"
,
strtab
+
sym
->
st_name
);
LOG_D
(
"relocate symbol: %s"
,
strtab
+
sym
->
st_name
);
/* need to resolve symbol in kernel symbol table */
/* need to resolve symbol in kernel symbol table */
...
@@ -191,13 +199,13 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
...
@@ -191,13 +199,13 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
if
(
index
!=
elf_module
->
e_shnum
)
if
(
index
!=
elf_module
->
e_shnum
)
{
{
int
i
,
count
=
0
;
int
i
,
count
=
0
;
Elf
32
_Sym
*
symtab
=
RT_NULL
;
Elf_Sym
*
symtab
=
RT_NULL
;
rt_uint8_t
*
strtab
=
RT_NULL
;
rt_uint8_t
*
strtab
=
RT_NULL
;
symtab
=
(
Elf
32
_Sym
*
)((
rt_uint8_t
*
)
module_ptr
+
shdr
[
index
].
sh_offset
);
symtab
=
(
Elf_Sym
*
)((
rt_uint8_t
*
)
module_ptr
+
shdr
[
index
].
sh_offset
);
strtab
=
(
rt_uint8_t
*
)
module_ptr
+
shdr
[
shdr
[
index
].
sh_link
].
sh_offset
;
strtab
=
(
rt_uint8_t
*
)
module_ptr
+
shdr
[
shdr
[
index
].
sh_link
].
sh_offset
;
for
(
i
=
0
;
i
<
shdr
[
index
].
sh_size
/
sizeof
(
Elf
32
_Sym
);
i
++
)
for
(
i
=
0
;
i
<
shdr
[
index
].
sh_size
/
sizeof
(
Elf_Sym
);
i
++
)
{
{
if
((
ELF_ST_BIND
(
symtab
[
i
].
st_info
)
==
STB_GLOBAL
)
&&
if
((
ELF_ST_BIND
(
symtab
[
i
].
st_info
)
==
STB_GLOBAL
)
&&
(
ELF_ST_TYPE
(
symtab
[
i
].
st_info
)
==
STT_FUNC
))
(
ELF_ST_TYPE
(
symtab
[
i
].
st_info
)
==
STT_FUNC
))
...
@@ -207,7 +215,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
...
@@ -207,7 +215,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
module
->
symtab
=
(
struct
rt_module_symtab
*
)
rt_malloc
module
->
symtab
=
(
struct
rt_module_symtab
*
)
rt_malloc
(
count
*
sizeof
(
struct
rt_module_symtab
));
(
count
*
sizeof
(
struct
rt_module_symtab
));
module
->
nsym
=
count
;
module
->
nsym
=
count
;
for
(
i
=
0
,
count
=
0
;
i
<
shdr
[
index
].
sh_size
/
sizeof
(
Elf
32
_Sym
);
i
++
)
for
(
i
=
0
,
count
=
0
;
i
<
shdr
[
index
].
sh_size
/
sizeof
(
Elf_Sym
);
i
++
)
{
{
rt_size_t
length
;
rt_size_t
length
;
...
@@ -231,7 +239,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
...
@@ -231,7 +239,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
rt_uint32_t
flag
=
0
;
rt_uint32_t
flag
=
0
;
rt_uint16_t
priority
;
rt_uint16_t
priority
;
rt_uint32_t
stacksize
;
rt_uint32_t
stacksize
;
for
(
i
=
0
;
i
<
shdr
[
index
].
sh_size
/
sizeof
(
Elf
32
_Sym
);
i
++
)
for
(
i
=
0
;
i
<
shdr
[
index
].
sh_size
/
sizeof
(
Elf_Sym
);
i
++
)
{
{
if
(((
flag
&
0x01
)
==
0
)
&&
if
(((
flag
&
0x01
)
==
0
)
&&
(
rt_strcmp
((
const
char
*
)(
strtab
+
symtab
[
i
].
st_name
),
"dlmodule_thread_priority"
)
==
0
))
(
rt_strcmp
((
const
char
*
)(
strtab
+
symtab
[
i
].
st_name
),
"dlmodule_thread_priority"
)
==
0
))
...
@@ -267,8 +275,8 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
...
@@ -267,8 +275,8 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
rt_err_t
dlmodule_load_relocated_object
(
struct
rt_dlmodule
*
module
,
void
*
module_ptr
)
rt_err_t
dlmodule_load_relocated_object
(
struct
rt_dlmodule
*
module
,
void
*
module_ptr
)
{
{
rt_u
int32
_t
index
,
rodata_addr
=
0
,
bss_addr
=
0
,
data_addr
=
0
;
rt_u
base
_t
index
,
rodata_addr
=
0
,
bss_addr
=
0
,
data_addr
=
0
;
rt_u
int32
_t
module_addr
=
0
,
module_size
=
0
;
rt_u
base
_t
module_addr
=
0
,
module_size
=
0
;
rt_uint8_t
*
ptr
,
*
strtab
,
*
shstrab
;
rt_uint8_t
*
ptr
,
*
strtab
,
*
shstrab
;
/* get the ELF image size */
/* get the ELF image size */
...
@@ -306,7 +314,7 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
...
@@ -306,7 +314,7 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
module
->
mem_space
=
rt_malloc
(
module_size
);
module
->
mem_space
=
rt_malloc
(
module_size
);
if
(
module
->
mem_space
==
RT_NULL
)
if
(
module
->
mem_space
==
RT_NULL
)
{
{
rt_kprintf
(
"Module: allocate space failed.
\n
"
);
LOG_E
(
"Module: allocate space failed.
\n
"
);
return
-
RT_ERROR
;
return
-
RT_ERROR
;
}
}
module
->
mem_size
=
module_size
;
module
->
mem_size
=
module_size
;
...
@@ -367,35 +375,45 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
...
@@ -367,35 +375,45 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
/* handle relocation section */
/* handle relocation section */
for
(
index
=
0
;
index
<
elf_module
->
e_shnum
;
index
++
)
for
(
index
=
0
;
index
<
elf_module
->
e_shnum
;
index
++
)
{
{
rt_u
int32
_t
i
,
nr_reloc
;
rt_u
base
_t
i
,
nr_reloc
;
Elf
32
_Sym
*
symtab
;
Elf_Sym
*
symtab
;
Elf
32
_Rel
*
rel
;
Elf_Rel
*
rel
;
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
if
(
!
IS_REL
(
shdr
[
index
]))
if
(
!
IS_REL
(
shdr
[
index
]))
continue
;
continue
;
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
if
(
!
IS_RELA
(
shdr
[
index
]))
continue
;
#endif
/* get relocate item */
/* get relocate item */
rel
=
(
Elf
32
_Rel
*
)((
rt_uint8_t
*
)
module_ptr
+
shdr
[
index
].
sh_offset
);
rel
=
(
Elf_Rel
*
)((
rt_uint8_t
*
)
module_ptr
+
shdr
[
index
].
sh_offset
);
/* locate .dynsym and .dynstr */
/* locate .dynsym and .dynstr */
symtab
=
(
Elf
32
_Sym
*
)((
rt_uint8_t
*
)
module_ptr
+
symtab
=
(
Elf_Sym
*
)((
rt_uint8_t
*
)
module_ptr
+
shdr
[
shdr
[
index
].
sh_link
].
sh_offset
);
shdr
[
shdr
[
index
].
sh_link
].
sh_offset
);
strtab
=
(
rt_uint8_t
*
)
module_ptr
+
strtab
=
(
rt_uint8_t
*
)
module_ptr
+
shdr
[
shdr
[
shdr
[
index
].
sh_link
].
sh_link
].
sh_offset
;
shdr
[
shdr
[
shdr
[
index
].
sh_link
].
sh_link
].
sh_offset
;
shstrab
=
(
rt_uint8_t
*
)
module_ptr
+
shstrab
=
(
rt_uint8_t
*
)
module_ptr
+
shdr
[
elf_module
->
e_shstrndx
].
sh_offset
;
shdr
[
elf_module
->
e_shstrndx
].
sh_offset
;
nr_reloc
=
(
rt_uint32_t
)(
shdr
[
index
].
sh_size
/
sizeof
(
Elf
32
_Rel
));
nr_reloc
=
(
rt_uint32_t
)(
shdr
[
index
].
sh_size
/
sizeof
(
Elf_Rel
));
/* relocate every items */
/* relocate every items */
for
(
i
=
0
;
i
<
nr_reloc
;
i
++
)
for
(
i
=
0
;
i
<
nr_reloc
;
i
++
)
{
{
Elf32_Sym
*
sym
=
&
symtab
[
ELF32_R_SYM
(
rel
->
r_info
)];
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
Elf_Sym
*
sym
=
&
symtab
[
ELF32_R_SYM
(
rel
->
r_info
)];
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
Elf_Sym
*
sym
=
&
symtab
[
ELF64_R_SYM
(
rel
->
r_info
)];
#endif
LOG_D
(
"relocate symbol: %s"
,
strtab
+
sym
->
st_name
);
LOG_D
(
"relocate symbol: %s"
,
strtab
+
sym
->
st_name
);
if
(
sym
->
st_shndx
!=
STN_UNDEF
)
if
(
sym
->
st_shndx
!=
STN_UNDEF
)
{
{
Elf
32
_Addr
addr
=
0
;
Elf_Addr
addr
=
0
;
if
((
ELF_ST_TYPE
(
sym
->
st_info
)
==
STT_SECTION
)
||
if
((
ELF_ST_TYPE
(
sym
->
st_info
)
==
STT_SECTION
)
||
(
ELF_ST_TYPE
(
sym
->
st_info
)
==
STT_OBJECT
))
(
ELF_ST_TYPE
(
sym
->
st_info
)
==
STT_OBJECT
))
...
@@ -405,28 +423,28 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
...
@@ -405,28 +423,28 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
{
{
/* relocate rodata section */
/* relocate rodata section */
LOG_D
(
"rodata"
);
LOG_D
(
"rodata"
);
addr
=
(
Elf
32
_Addr
)(
rodata_addr
+
sym
->
st_value
);
addr
=
(
Elf_Addr
)(
rodata_addr
+
sym
->
st_value
);
}
}
else
if
(
rt_strncmp
((
const
char
*
)
else
if
(
rt_strncmp
((
const
char
*
)
(
shstrab
+
shdr
[
sym
->
st_shndx
].
sh_name
),
ELF_BSS
,
5
)
==
0
)
(
shstrab
+
shdr
[
sym
->
st_shndx
].
sh_name
),
ELF_BSS
,
5
)
==
0
)
{
{
/* relocate bss section */
/* relocate bss section */
LOG_D
(
"bss"
);
LOG_D
(
"bss"
);
addr
=
(
Elf
32
_Addr
)
bss_addr
+
sym
->
st_value
;
addr
=
(
Elf_Addr
)
bss_addr
+
sym
->
st_value
;
}
}
else
if
(
rt_strncmp
((
const
char
*
)(
shstrab
+
shdr
[
sym
->
st_shndx
].
sh_name
),
else
if
(
rt_strncmp
((
const
char
*
)(
shstrab
+
shdr
[
sym
->
st_shndx
].
sh_name
),
ELF_DATA
,
6
)
==
0
)
ELF_DATA
,
6
)
==
0
)
{
{
/* relocate data section */
/* relocate data section */
LOG_D
(
"data"
);
LOG_D
(
"data"
);
addr
=
(
Elf
32
_Addr
)
data_addr
+
sym
->
st_value
;
addr
=
(
Elf_Addr
)
data_addr
+
sym
->
st_value
;
}
}
if
(
addr
!=
0
)
dlmodule_relocate
(
module
,
rel
,
addr
);
if
(
addr
!=
0
)
dlmodule_relocate
(
module
,
rel
,
addr
);
}
}
else
if
(
ELF_ST_TYPE
(
sym
->
st_info
)
==
STT_FUNC
)
else
if
(
ELF_ST_TYPE
(
sym
->
st_info
)
==
STT_FUNC
)
{
{
addr
=
(
Elf
32
_Addr
)((
rt_uint8_t
*
)
module
->
mem_space
-
module_addr
+
sym
->
st_value
);
addr
=
(
Elf_Addr
)((
rt_uint8_t
*
)
module
->
mem_space
-
module_addr
+
sym
->
st_value
);
/* relocate function */
/* relocate function */
dlmodule_relocate
(
module
,
rel
,
addr
);
dlmodule_relocate
(
module
,
rel
,
addr
);
...
@@ -436,14 +454,14 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
...
@@ -436,14 +454,14 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
{
{
/* relocate function */
/* relocate function */
dlmodule_relocate
(
module
,
rel
,
dlmodule_relocate
(
module
,
rel
,
(
Elf
32
_Addr
)((
rt_uint8_t
*
)
(
Elf_Addr
)((
rt_uint8_t
*
)
module
->
mem_space
module
->
mem_space
-
module_addr
-
module_addr
+
sym
->
st_value
));
+
sym
->
st_value
));
}
}
else
else
{
{
Elf
32
_Addr
addr
;
Elf_Addr
addr
;
if
(
ELF32_R_TYPE
(
rel
->
r_info
)
!=
R_ARM_V4BX
)
if
(
ELF32_R_TYPE
(
rel
->
r_info
)
!=
R_ARM_V4BX
)
{
{
...
@@ -451,7 +469,7 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
...
@@ -451,7 +469,7 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
/* need to resolve symbol in kernel symbol table */
/* need to resolve symbol in kernel symbol table */
addr
=
dlmodule_symbol_find
((
const
char
*
)(
strtab
+
sym
->
st_name
));
addr
=
dlmodule_symbol_find
((
const
char
*
)(
strtab
+
sym
->
st_name
));
if
(
addr
!=
(
Elf
32
_Addr
)
RT_NULL
)
if
(
addr
!=
(
Elf_Addr
)
RT_NULL
)
{
{
dlmodule_relocate
(
module
,
rel
,
addr
);
dlmodule_relocate
(
module
,
rel
,
addr
);
LOG_D
(
"symbol addr 0x%x"
,
addr
);
LOG_D
(
"symbol addr 0x%x"
,
addr
);
...
@@ -462,7 +480,7 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
...
@@ -462,7 +480,7 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
}
}
else
else
{
{
addr
=
(
Elf
32
_Addr
)((
rt_uint8_t
*
)
module
->
mem_space
-
module_addr
+
sym
->
st_value
);
addr
=
(
Elf_Addr
)((
rt_uint8_t
*
)
module
->
mem_space
-
module_addr
+
sym
->
st_value
);
dlmodule_relocate
(
module
,
rel
,
addr
);
dlmodule_relocate
(
module
,
rel
,
addr
);
}
}
}
}
...
...
components/libc/libdl/dlelf.h
浏览文件 @
072899e1
...
@@ -6,6 +6,7 @@
...
@@ -6,6 +6,7 @@
* Change Logs:
* Change Logs:
* Date Author Notes
* Date Author Notes
* 2018/08/29 Bernard first version
* 2018/08/29 Bernard first version
* 2021/04/23 chunyexixiaoyu distinguish 32-bit and 64-bit
*/
*/
#ifndef DL_ELF_H__
#ifndef DL_ELF_H__
...
@@ -19,6 +20,21 @@ typedef rt_int32_t Elf32_Sword; /* Signed large integer */
...
@@ -19,6 +20,21 @@ typedef rt_int32_t Elf32_Sword; /* Signed large integer */
typedef
rt_uint32_t
Elf32_Word
;
/* Unsigned large integer */
typedef
rt_uint32_t
Elf32_Word
;
/* Unsigned large integer */
typedef
rt_uint16_t
Elf32_Half
;
/* Unsigned medium integer */
typedef
rt_uint16_t
Elf32_Half
;
/* Unsigned medium integer */
typedef
rt_uint64_t
Elf64_Addr
;
typedef
rt_uint16_t
Elf64_Half
;
typedef
rt_int16_t
Elf64_SHalf
;
typedef
rt_uint64_t
Elf64_Off
;
typedef
rt_int32_t
Elf64_Sword
;
typedef
rt_uint32_t
Elf64_Word
;
typedef
rt_uint64_t
Elf64_Xword
;
typedef
rt_int64_t
Elf64_Sxword
;
typedef
uint16_t
Elf64_Section
;
/* e_ident[] magic number */
/* e_ident[] magic number */
#define ELFMAG0 0x7f
/* e_ident[EI_MAG0] */
#define ELFMAG0 0x7f
/* e_ident[EI_MAG0] */
#define ELFMAG1 'E'
/* e_ident[EI_MAG1] */
#define ELFMAG1 'E'
/* e_ident[EI_MAG1] */
...
@@ -75,6 +91,25 @@ typedef struct elfhdr
...
@@ -75,6 +91,25 @@ typedef struct elfhdr
header string table" entry offset */
header string table" entry offset */
}
Elf32_Ehdr
;
}
Elf32_Ehdr
;
typedef
struct
elf64_hdr
{
unsigned
char
e_ident
[
EI_NIDENT
];
/* ELF Identification */
Elf64_Half
e_type
;
/* object file type */
Elf64_Half
e_machine
;
/* machine */
Elf64_Word
e_version
;
/* object file version */
Elf64_Addr
e_entry
;
/* virtual entry point */
Elf64_Off
e_phoff
;
/* program header table offset */
Elf64_Off
e_shoff
;
/* section header table offset */
Elf64_Word
e_flags
;
/* processor-specific flags */
Elf64_Half
e_ehsize
;
/* ELF header size */
Elf64_Half
e_phentsize
;
/* program header entry size */
Elf64_Half
e_phnum
;
/* number of program header entries */
Elf64_Half
e_shentsize
;
/* section header entry size */
Elf64_Half
e_shnum
;
/* number of section header entries */
Elf64_Half
e_shstrndx
;
/* section header table's "section
header string table" entry offset */
}
Elf64_Ehdr
;
/* Section Header */
/* Section Header */
typedef
struct
typedef
struct
{
{
...
@@ -91,6 +126,21 @@ typedef struct
...
@@ -91,6 +126,21 @@ typedef struct
Elf32_Word
sh_entsize
;
/* section entry size */
Elf32_Word
sh_entsize
;
/* section entry size */
}
Elf32_Shdr
;
}
Elf32_Shdr
;
typedef
struct
{
Elf64_Word
sh_name
;
/* Section name (string tbl index) */
Elf64_Word
sh_type
;
/* Section type */
Elf64_Xword
sh_flags
;
/* Section flags */
Elf64_Addr
sh_addr
;
/* Section virtual addr at execution */
Elf64_Off
sh_offset
;
/* Section file offset */
Elf64_Xword
sh_size
;
/* Section size in bytes */
Elf64_Word
sh_link
;
/* Link to another section */
Elf64_Word
sh_info
;
/* Additional section information */
Elf64_Xword
sh_addralign
;
/* Section alignment */
Elf64_Xword
sh_entsize
;
/* Entry size if section holds table */
}
Elf64_Shdr
;
/* Section names */
/* Section names */
#define ELF_BSS ".bss"
/* uninitialized data */
#define ELF_BSS ".bss"
/* uninitialized data */
#define ELF_DATA ".data"
/* initialized data */
#define ELF_DATA ".data"
/* initialized data */
...
@@ -126,6 +176,19 @@ typedef struct elf32_sym
...
@@ -126,6 +176,19 @@ typedef struct elf32_sym
Elf32_Half
st_shndx
;
/* section header index */
Elf32_Half
st_shndx
;
/* section header index */
}
Elf32_Sym
;
}
Elf32_Sym
;
typedef
struct
{
Elf64_Word
st_name
;
/* Symbol name (string tbl index) */
unsigned
char
st_info
;
/* Symbol type and binding */
unsigned
char
st_other
;
/* Symbol visibility */
Elf64_Section
st_shndx
;
/* Section index */
Elf64_Addr
st_value
;
/* Symbol value */
Elf64_Xword
st_size
;
/* Symbol size */
}
Elf64_Sym
;
#define STB_LOCAL 0
/* BIND */
#define STB_LOCAL 0
/* BIND */
#define STB_GLOBAL 1
#define STB_GLOBAL 1
#define STB_WEAK 2
#define STB_WEAK 2
...
@@ -160,6 +223,12 @@ typedef struct
...
@@ -160,6 +223,12 @@ typedef struct
Elf32_Word
r_info
;
/* symbol table index and type */
Elf32_Word
r_info
;
/* symbol table index and type */
}
Elf32_Rel
;
}
Elf32_Rel
;
typedef
struct
{
Elf64_Addr
r_offset
;
/* Address */
Elf64_Xword
r_info
;
/* Relocation type and symbol index */
}
Elf64_Rel
;
/* Relocation entry with explicit addend */
/* Relocation entry with explicit addend */
typedef
struct
typedef
struct
{
{
...
@@ -168,11 +237,24 @@ typedef struct
...
@@ -168,11 +237,24 @@ typedef struct
Elf32_Sword
r_addend
;
Elf32_Sword
r_addend
;
}
Elf32_Rela
;
}
Elf32_Rela
;
typedef
struct
{
Elf64_Addr
r_offset
;
/* Address */
Elf64_Xword
r_info
;
/* Relocation type and symbol index */
Elf64_Sxword
r_addend
;
/* Addend */
}
Elf64_Rela
;
/* Extract relocation info - r_info */
/* Extract relocation info - r_info */
#define ELF32_R_SYM(i) ((i) >> 8)
#define ELF32_R_SYM(i) ((i) >> 8)
#define ELF32_R_TYPE(i) ((unsigned char) (i))
#define ELF32_R_TYPE(i) ((unsigned char) (i))
#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t))
#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t))
#define ELF64_R_SYM(i) ((i) >> 32)
#define ELF64_R_TYPE(i) ((i) & 0xffffffff)
#define ELF64_R_INFO(sym,type) ((((Elf64_Xword) (sym)) << 32) + (type))
/*
/*
* Relocation type for arm
* Relocation type for arm
*/
*/
...
@@ -219,6 +301,23 @@ typedef struct
...
@@ -219,6 +301,23 @@ typedef struct
Elf32_Word
p_align
;
/* memory alignment */
Elf32_Word
p_align
;
/* memory alignment */
}
Elf32_Phdr
;
}
Elf32_Phdr
;
typedef
struct
{
Elf64_Word
p_type
;
/* Segment type */
Elf64_Word
p_flags
;
/* Segment flags */
Elf64_Off
p_offset
;
/* Segment file offset */
Elf64_Addr
p_vaddr
;
/* Segment virtual address */
Elf64_Addr
p_paddr
;
/* Segment physical address */
Elf64_Xword
p_filesz
;
/* Segment size in file */
Elf64_Xword
p_memsz
;
/* Segment size in memory */
Elf64_Xword
p_align
;
/* Segment alignment */
}
Elf64_Phdr
;
/* p_type */
/* p_type */
#define PT_NULL 0
#define PT_NULL 0
#define PT_LOAD 1
#define PT_LOAD 1
...
@@ -273,13 +372,27 @@ typedef struct
...
@@ -273,13 +372,27 @@ typedef struct
#define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
#define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR))
#define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
#define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE))
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
#define elf_module ((Elf32_Ehdr *)module_ptr)
#define elf_module ((Elf32_Ehdr *)module_ptr)
#define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
#define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
#define phdr ((Elf32_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff))
#define phdr ((Elf32_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff))
typedef
Elf32_Sym
Elf_Sym
;
typedef
Elf32_Rel
Elf_Rel
;
typedef
Elf32_Addr
Elf_Addr
;
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
#define elf_module ((Elf64_Ehdr *)module_ptr)
#define shdr ((Elf64_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff))
#define phdr ((Elf64_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff))
typedef
Elf64_Sym
Elf_Sym
;
typedef
Elf64_Rela
Elf_Rel
;
typedef
Elf64_Addr
Elf_Addr
;
#endif
int
dlmodule_relocate
(
struct
rt_dlmodule
*
module
,
Elf_Rel
*
rel
,
Elf_Addr
sym_val
);
rt_err_t
dlmodule_load_shared_object
(
struct
rt_dlmodule
*
module
,
void
*
module_ptr
);
rt_err_t
dlmodule_load_shared_object
(
struct
rt_dlmodule
*
module
,
void
*
module_ptr
);
rt_err_t
dlmodule_load_relocated_object
(
struct
rt_dlmodule
*
module
,
void
*
module_ptr
);
rt_err_t
dlmodule_load_relocated_object
(
struct
rt_dlmodule
*
module
,
void
*
module_ptr
);
int
dlmodule_relocate
(
struct
rt_dlmodule
*
module
,
Elf32_Rel
*
rel
,
Elf32_Addr
sym_val
);
#endif
#endif
components/libc/libdl/dlmodule.c
浏览文件 @
072899e1
...
@@ -464,7 +464,7 @@ struct rt_dlmodule* dlmodule_load(const char* filename)
...
@@ -464,7 +464,7 @@ struct rt_dlmodule* dlmodule_load(const char* filename)
}
}
/* check ELF class */
/* check ELF class */
if
(
elf_module
->
e_ident
[
EI_CLASS
]
!=
ELFCLASS32
)
if
(
(
elf_module
->
e_ident
[
EI_CLASS
]
!=
ELFCLASS32
)
&&
(
elf_module
->
e_ident
[
EI_CLASS
]
!=
ELFCLASS64
)
)
{
{
rt_kprintf
(
"Module: ELF class error
\n
"
);
rt_kprintf
(
"Module: ELF class error
\n
"
);
goto
__exit
;
goto
__exit
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录