Skip to content

Commit efe4523

Browse files
committed
[Kernel] move module tolibc/libdl.
1 parent 240451b commit efe4523

File tree

23 files changed

+1499
-1559
lines changed

23 files changed

+1499
-1559
lines changed

components/finsh/cmd.c

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -584,38 +584,6 @@ FINSH_FUNCTION_EXPORT(list_device, list device in system);
584584
MSH_CMD_EXPORT(list_device, list device in system);
585585
#endif
586586

587-
#ifdef RT_USING_MODULE
588-
#include <rtm.h>
589-
590-
int list_module(void)
591-
{
592-
int maxlen;
593-
struct rt_module *module;
594-
struct rt_list_node *list, *node;
595-
struct rt_object_information *info;
596-
const char *item_title = "module";
597-
598-
info = rt_object_get_information(RT_Object_Class_Module);
599-
list = &info->object_list;
600-
601-
maxlen = object_name_maxlen(item_title, list);
602-
603-
rt_kprintf("%-*.s ref address \n", maxlen, item_title); object_split(maxlen);
604-
rt_kprintf( " -------- ------------\n");
605-
for (node = list->next; node != list; node = node->next)
606-
{
607-
module = (struct rt_module *)(rt_list_entry(node, struct rt_object, list));
608-
rt_kprintf("%-*.*s %-04d 0x%08x\n",
609-
maxlen, RT_NAME_MAX,
610-
module->parent.name, module->nref, module->module_space);
611-
}
612-
613-
return 0;
614-
}
615-
FINSH_FUNCTION_EXPORT(list_module, list module in system);
616-
MSH_CMD_EXPORT(list_module, list module in system);
617-
#endif
618-
619587
long list(void)
620588
{
621589
#ifndef FINSH_USING_MSH_ONLY

components/finsh/msh.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@
3737
#include <dfs_posix.h>
3838
#endif
3939

40+
#ifdef RT_USING_MODULE
41+
#include <dlmodule.h>
42+
#endif
43+
4044
#ifndef FINSH_ARG_MAX
41-
#define FINSH_ARG_MAX 10
45+
#define FINSH_ARG_MAX 8
4246
#endif
4347

4448
typedef int (*cmd_function_t)(int argc, char **argv);
@@ -64,7 +68,6 @@ static int msh_exit(int argc, char **argv)
6468
{
6569
/* return to finsh shell mode */
6670
__msh_state = RT_FALSE;
67-
6871
return 0;
6972
}
7073
FINSH_FUNCTION_EXPORT_ALIAS(msh_exit, __cmd_exit, return to RT-Thread shell mode.);
@@ -254,7 +257,7 @@ int msh_exec_module(const char *cmd_line, int size)
254257
{
255258
/* found program */
256259
close(fd);
257-
rt_module_exec_cmd(pg_name, cmd_line, size);
260+
dlmodule_exec(pg_name, cmd_line, size);
258261
ret = 0;
259262
}
260263
else

components/libc/Kconfig

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ if RT_USING_LIBC && RT_USING_DFS
2828
default n
2929
endif
3030

31-
if RT_USING_MODULE
32-
config RT_USING_LIBDL
33-
bool "Enable dlopen/dlsym/dlclose feature"
31+
config RT_USING_MODULE
32+
bool "Enable dynamic module with dlopen/dlsym/dlclose feature"
3433
default n
35-
endif
3634
endif
3735

3836
endmenu

components/libc/compilers/newlib/syscalls.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <pthread.h>
1212
#endif
1313

14+
#ifdef RT_USING_MODULE
15+
#include <dlmodule.h>
16+
#endif
17+
1418
/* Reentrant versions of system calls. */
1519

1620
int
@@ -379,18 +383,12 @@ _free_r (struct _reent *ptr, void *addr)
379383
}
380384

381385
void
382-
_exit (int status)
386+
exit (int status)
383387
{
384388
#ifdef RT_USING_MODULE
385-
rt_module_t module;
386-
387-
module = rt_module_self();
388-
if (module != RT_NULL)
389+
if (dlmodule_self())
389390
{
390-
rt_thread_suspend(rt_thread_self());
391-
392-
/* re-schedule */
393-
rt_schedule();
391+
dlmodule_exit(status);
394392
}
395393
#endif
396394

components/libc/libdl/SConscript

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from building import *
22
Import('rtconfig')
33

4-
src = Glob('*.c') + Glob('*.cpp')
4+
src = Glob('*.c') + Glob('*.cpp') + Glob('arch/*.c')
55
cwd = GetCurrentDir()
66
group = []
77
CPPPATH = [cwd]
88

99
if rtconfig.PLATFORM == 'gcc':
1010
group = DefineGroup('libc', src,
11-
depend = ['RT_USING_MODULE', 'RT_USING_LIBDL'],
11+
depend = ['RT_USING_MODULE'],
1212
CPPPATH = CPPPATH)
1313

1414
Return('group')

components/libc/libdl/arch/arm.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2018/08/29 Bernard first version
9+
*/
10+
11+
#include "../dlmodule.h"
12+
#include "../dlelf.h"
13+
14+
#ifdef __arm__
15+
int dlmodule_relocate(struct rt_dlmodule *module, Elf32_Rel *rel, Elf32_Addr sym_val)
16+
{
17+
Elf32_Addr *where, tmp;
18+
Elf32_Sword addend, offset;
19+
rt_uint32_t upper, lower, sign, j1, j2;
20+
21+
where = (Elf32_Addr *)((rt_uint8_t *)module->mem_space
22+
+ rel->r_offset
23+
- module->vstart_addr);
24+
switch (ELF32_R_TYPE(rel->r_info))
25+
{
26+
case R_ARM_NONE:
27+
break;
28+
case R_ARM_ABS32:
29+
*where += (Elf32_Addr)sym_val;
30+
RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_ABS32: %x -> %x\n",
31+
where, *where));
32+
break;
33+
case R_ARM_PC24:
34+
case R_ARM_PLT32:
35+
case R_ARM_CALL:
36+
case R_ARM_JUMP24:
37+
addend = *where & 0x00ffffff;
38+
if (addend & 0x00800000)
39+
addend |= 0xff000000;
40+
tmp = sym_val - (Elf32_Addr)where + (addend << 2);
41+
tmp >>= 2;
42+
*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
43+
RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_PC24: %x -> %x\n",
44+
where, *where));
45+
break;
46+
case R_ARM_REL32:
47+
*where += sym_val - (Elf32_Addr)where;
48+
RT_DEBUG_LOG(RT_DEBUG_MODULE,
49+
("R_ARM_REL32: %x -> %x, sym %x, offset %x\n",
50+
where, *where, sym_val, rel->r_offset));
51+
break;
52+
case R_ARM_V4BX:
53+
*where &= 0xf000000f;
54+
*where |= 0x01a0f000;
55+
break;
56+
57+
case R_ARM_GLOB_DAT:
58+
case R_ARM_JUMP_SLOT:
59+
*where = (Elf32_Addr)sym_val;
60+
RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_JUMP_SLOT: 0x%x -> 0x%x 0x%x\n",
61+
where, *where, sym_val));
62+
break;
63+
#if 0 /* To do */
64+
case R_ARM_GOT_BREL:
65+
temp = (Elf32_Addr)sym_val;
66+
*where = (Elf32_Addr)&temp;
67+
RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_GOT_BREL: 0x%x -> 0x%x 0x%x\n",
68+
where, *where, sym_val));
69+
break;
70+
#endif
71+
72+
case R_ARM_RELATIVE:
73+
*where = (Elf32_Addr)sym_val + *where;
74+
RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_RELATIVE: 0x%x -> 0x%x 0x%x\n",
75+
where, *where, sym_val));
76+
break;
77+
case R_ARM_THM_CALL:
78+
case R_ARM_THM_JUMP24:
79+
upper = *(rt_uint16_t *)where;
80+
lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);
81+
82+
sign = (upper >> 10) & 1;
83+
j1 = (lower >> 13) & 1;
84+
j2 = (lower >> 11) & 1;
85+
offset = (sign << 24) |
86+
((~(j1 ^ sign) & 1) << 23) |
87+
((~(j2 ^ sign) & 1) << 22) |
88+
((upper & 0x03ff) << 12) |
89+
((lower & 0x07ff) << 1);
90+
if (offset & 0x01000000)
91+
offset -= 0x02000000;
92+
offset += sym_val - (Elf32_Addr)where;
93+
94+
if (!(offset & 1) ||
95+
offset <= (rt_int32_t)0xff000000 ||
96+
offset >= (rt_int32_t)0x01000000)
97+
{
98+
rt_kprintf("Module: Only Thumb addresses allowed\n");
99+
100+
return -1;
101+
}
102+
103+
sign = (offset >> 24) & 1;
104+
j1 = sign ^ (~(offset >> 23) & 1);
105+
j2 = sign ^ (~(offset >> 22) & 1);
106+
*(rt_uint16_t *)where = (rt_uint16_t)((upper & 0xf800) |
107+
(sign << 10) |
108+
((offset >> 12) & 0x03ff));
109+
*(rt_uint16_t *)(where + 2) = (rt_uint16_t)((lower & 0xd000) |
110+
(j1 << 13) | (j2 << 11) |
111+
((offset >> 1) & 0x07ff));
112+
upper = *(rt_uint16_t *)where;
113+
lower = *(rt_uint16_t *)((Elf32_Addr)where + 2);
114+
break;
115+
default:
116+
return -1;
117+
}
118+
119+
return 0;
120+
}
121+
#endif

components/libc/libdl/dlclose.c

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
/*
2-
* File : dlclose.c
3-
* This file is part of RT-Thread RTOS
4-
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
53
*
6-
* The license and distribution terms for this file may be
7-
* found in the file LICENSE in this distribution or at
8-
* http://www.rt-thread.org/license/LICENSE
4+
* SPDX-License-Identifier: Apache-2.0
95
*
106
* Change Logs:
11-
* Date Author Notes
12-
* 2010-11-17 yi.qiu first version
7+
* Date Author Notes
8+
* 2010-11-17 yi.qiu first version
139
*/
1410

1511
#include <rtthread.h>
1612
#include <rtm.h>
1713

18-
int dlclose (void *handle)
14+
#include "dlmodule.h"
15+
16+
int dlclose(void *handle)
1917
{
20-
rt_module_t module;
21-
22-
RT_ASSERT(handle != RT_NULL);
23-
24-
module = (rt_module_t)handle;
25-
module->nref--;
26-
27-
if(module->nref <= 0)
28-
{
29-
rt_module_unload(module);
30-
}
31-
32-
return RT_TRUE;
33-
}
18+
struct rt_dlmodule *module;
3419

35-
RTM_EXPORT(dlclose)
20+
RT_ASSERT(handle != RT_NULL);
21+
22+
module = (struct rt_dlmodule *)handle;
3623

24+
rt_enter_critical();
25+
module->nref--;
26+
if (module->nref <= 0)
27+
{
28+
rt_exit_critical();
29+
30+
dlmodule_destroy(module);
31+
}
32+
else
33+
{
34+
rt_exit_critical();
35+
}
36+
37+
return RT_TRUE;
38+
}
39+
RTM_EXPORT(dlclose)

0 commit comments

Comments
 (0)