dlelf.c
17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018/08/29 Bernard first version
* 2021/04/23 chunyexixiaoyu distinguish 32-bit and 64-bit
*/
#include "dlmodule.h"
#include "dlelf.h"
#define DBG_TAG "DLMD"
#define DBG_LVL DBG_INFO
#include <rtdbg.h> // must after of DEBUG_ENABLE or some other options
rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_ptr)
{
rt_bool_t linked = RT_FALSE;
rt_ubase_t index, module_size = 0;
Elf_Addr vstart_addr, vend_addr;
rt_bool_t has_vstart;
RT_ASSERT(module_ptr != RT_NULL);
if (rt_memcmp(elf_module->e_ident, RTMMAG, SELFMAG) == 0)
{
/* rtmlinker finished */
linked = RT_TRUE;
}
/* get the ELF image size */
has_vstart = RT_FALSE;
vstart_addr = vend_addr = RT_NULL;
for (index = 0; index < elf_module->e_phnum; index++)
{
if (phdr[index].p_type != PT_LOAD)
continue;
LOG_D("LOAD segment: %d, 0x%p, 0x%08x", index, phdr[index].p_vaddr, phdr[index].p_memsz);
if (phdr[index].p_memsz < phdr[index].p_filesz)
{
rt_kprintf("invalid elf: segment %d: p_memsz: %d, p_filesz: %d\n",
index, phdr[index].p_memsz, phdr[index].p_filesz);
return RT_NULL;
}
if (!has_vstart)
{
vstart_addr = phdr[index].p_vaddr;
vend_addr = phdr[index].p_vaddr + phdr[index].p_memsz;
has_vstart = RT_TRUE;
if (vend_addr < vstart_addr)
{
LOG_E("invalid elf: segment %d: p_vaddr: %d, p_memsz: %d\n",
index, phdr[index].p_vaddr, phdr[index].p_memsz);
return RT_NULL;
}
}
else
{
if (phdr[index].p_vaddr < vend_addr)
{
LOG_E("invalid elf: segment should be sorted and not overlapped\n");
return RT_NULL;
}
if (phdr[index].p_vaddr > vend_addr + 16)
{
/* There should not be too much padding in the object files. */
LOG_W("warning: too much padding before segment %d", index);
}
vend_addr = phdr[index].p_vaddr + phdr[index].p_memsz;
if (vend_addr < phdr[index].p_vaddr)
{
LOG_E("invalid elf: "
"segment %d address overflow\n", index);
return RT_NULL;
}
}
}
module_size = vend_addr - vstart_addr;
LOG_D("module size: %d, vstart_addr: 0x%p", module_size, vstart_addr);
if (module_size == 0)
{
LOG_E("Module: size error\n");
return -RT_ERROR;
}
module->vstart_addr = vstart_addr;
module->nref = 0;
/* allocate module space */
module->mem_space = rt_malloc(module_size);
if (module->mem_space == RT_NULL)
{
LOG_E("Module: allocate space failed.\n");
return -RT_ERROR;
}
module->mem_size = module_size;
/* zero all space */
rt_memset(module->mem_space, 0, module_size);
for (index = 0; index < elf_module->e_phnum; index++)
{
if (phdr[index].p_type == PT_LOAD)
{
rt_memcpy(module->mem_space + phdr[index].p_vaddr - vstart_addr,
(rt_uint8_t *)elf_module + phdr[index].p_offset,
phdr[index].p_filesz);
}
}
/* set module entry */
module->entry_addr = module->mem_space + elf_module->e_entry - vstart_addr;
/* handle relocation section */
for (index = 0; index < elf_module->e_shnum; index ++)
{
rt_ubase_t i, nr_reloc;
Elf_Sym *symtab;
Elf_Rel *rel;
rt_uint8_t *strtab;
static rt_bool_t unsolved = RT_FALSE;
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
if (!IS_REL(shdr[index]))
continue;
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
if (!IS_RELA(shdr[index]))
continue;
#endif
/* get relocate item */
rel = (Elf_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
/* locate .rel.plt and .rel.dyn section */
symtab = (Elf_Sym *)((rt_uint8_t *)module_ptr +
shdr[shdr[index].sh_link].sh_offset);
strtab = (rt_uint8_t *)module_ptr +
shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
nr_reloc = (rt_ubase_t)(shdr[index].sh_size / sizeof(Elf_Rel));
/* relocate every items */
for (i = 0; i < nr_reloc; i ++)
{
#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);
if ((sym->st_shndx != SHT_NULL) ||(ELF_ST_BIND(sym->st_info) == STB_LOCAL))
{
Elf_Addr addr;
addr = (Elf_Addr)(module->mem_space + sym->st_value - vstart_addr);
dlmodule_relocate(module, rel, addr);
}
else if (!linked)
{
Elf_Addr addr;
LOG_D("relocate symbol: %s", strtab + sym->st_name);
/* need to resolve symbol in kernel symbol table */
addr = dlmodule_symbol_find((const char *)(strtab + sym->st_name));
if (addr == 0)
{
LOG_E("Module: can't find %s in kernel symbol table", strtab + sym->st_name);
unsolved = RT_TRUE;
}
else
{
dlmodule_relocate(module, rel, addr);
}
}
rel ++;
}
if (unsolved)
return -RT_ERROR;
}
/* construct module symbol table */
for (index = 0; index < elf_module->e_shnum; index ++)
{
/* find .dynsym section */
rt_uint8_t *shstrab;
shstrab = (rt_uint8_t *)module_ptr +
shdr[elf_module->e_shstrndx].sh_offset;
if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0)
break;
}
/* found .dynsym section */
if (index != elf_module->e_shnum)
{
int i, count = 0;
Elf_Sym *symtab = RT_NULL;
rt_uint8_t *strtab = RT_NULL;
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;
for (i = 0; i < shdr[index].sh_size / sizeof(Elf_Sym); i++)
{
if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) &&
(ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
count ++;
}
module->symtab = (struct rt_module_symtab *)rt_malloc
(count * sizeof(struct rt_module_symtab));
module->nsym = count;
for (i = 0, count = 0; i < shdr[index].sh_size / sizeof(Elf_Sym); i++)
{
rt_size_t length;
if ((ELF_ST_BIND(symtab[i].st_info) != STB_GLOBAL) ||
(ELF_ST_TYPE(symtab[i].st_info) != STT_FUNC))
continue;
length = rt_strlen((const char *)(strtab + symtab[i].st_name)) + 1;
module->symtab[count].addr =
(void *)(module->mem_space + symtab[i].st_value - module->vstart_addr);
module->symtab[count].name = rt_malloc(length);
rt_memset((void *)module->symtab[count].name, 0, length);
rt_memcpy((void *)module->symtab[count].name,
strtab + symtab[i].st_name,
length);
count ++;
}
/* get priority & stack size params*/
rt_uint32_t flag = 0;
rt_uint16_t priority;
rt_uint32_t stacksize;
for (i = 0; i < shdr[index].sh_size / sizeof(Elf_Sym); i++)
{
if (((flag & 0x01) == 0) &&
(rt_strcmp((const char *)(strtab + symtab[i].st_name), "dlmodule_thread_priority") == 0))
{
flag |= 0x01;
priority = *(rt_uint16_t*)(module->mem_space + symtab[i].st_value - module->vstart_addr);
if (priority < RT_THREAD_PRIORITY_MAX)
{
module->priority = priority;
}
}
if (((flag & 0x02) == 0) &&
(rt_strcmp((const char *)(strtab + symtab[i].st_name), "dlmodule_thread_stacksize") == 0))
{
flag |= 0x02;
stacksize = *(rt_uint32_t*)(module->mem_space + symtab[i].st_value - module->vstart_addr);
if ((stacksize < 2048) || (stacksize > 1024 * 32))
{
module->stack_size = stacksize;
}
}
if ((flag & 0x03) == 0x03)
{
break;
}
}
}
return RT_EOK;
}
rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module_ptr)
{
rt_ubase_t index, rodata_addr = 0, bss_addr = 0, data_addr = 0;
rt_ubase_t module_addr = 0, module_size = 0;
rt_uint8_t *ptr, *strtab, *shstrab;
/* get the ELF image size */
for (index = 0; index < elf_module->e_shnum; index ++)
{
/* text */
if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
{
module_size += shdr[index].sh_size;
module_addr = shdr[index].sh_addr;
}
/* rodata */
if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
{
module_size += shdr[index].sh_size;
}
/* data */
if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
{
module_size += shdr[index].sh_size;
}
/* bss */
if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
{
module_size += shdr[index].sh_size;
}
}
/* no text, data and bss on image */
if (module_size == 0) return RT_NULL;
module->vstart_addr = 0;
/* allocate module space */
module->mem_space = rt_malloc(module_size);
if (module->mem_space == RT_NULL)
{
LOG_E("Module: allocate space failed.\n");
return -RT_ERROR;
}
module->mem_size = module_size;
/* zero all space */
ptr = module->mem_space;
rt_memset(ptr, 0, module_size);
/* load text and data section */
for (index = 0; index < elf_module->e_shnum; index ++)
{
/* load text section */
if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
{
rt_memcpy(ptr,
(rt_uint8_t *)elf_module + shdr[index].sh_offset,
shdr[index].sh_size);
LOG_D("load text 0x%x, size %d", ptr, shdr[index].sh_size);
ptr += shdr[index].sh_size;
}
/* load rodata section */
if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
{
rt_memcpy(ptr,
(rt_uint8_t *)elf_module + shdr[index].sh_offset,
shdr[index].sh_size);
rodata_addr = (rt_uint32_t)ptr;
LOG_D("load rodata 0x%x, size %d, rodata 0x%x", ptr,
shdr[index].sh_size, *(rt_uint32_t *)data_addr);
ptr += shdr[index].sh_size;
}
/* load data section */
if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
{
rt_memcpy(ptr,
(rt_uint8_t *)elf_module + shdr[index].sh_offset,
shdr[index].sh_size);
data_addr = (rt_uint32_t)ptr;
LOG_D("load data 0x%x, size %d, data 0x%x", ptr,
shdr[index].sh_size, *(rt_uint32_t *)data_addr);
ptr += shdr[index].sh_size;
}
/* load bss section */
if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
{
rt_memset(ptr, 0, shdr[index].sh_size);
bss_addr = (rt_uint32_t)ptr;
LOG_D("load bss 0x%x, size %d", ptr, shdr[index].sh_size);
}
}
/* set module entry */
module->entry_addr = (rt_dlmodule_entry_func_t)((rt_uint8_t *)module->mem_space + elf_module->e_entry - module_addr);
/* handle relocation section */
for (index = 0; index < elf_module->e_shnum; index ++)
{
rt_ubase_t i, nr_reloc;
Elf_Sym *symtab;
Elf_Rel *rel;
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
if (!IS_REL(shdr[index]))
continue;
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
if (!IS_RELA(shdr[index]))
continue;
#endif
/* get relocate item */
rel = (Elf_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
/* locate .dynsym and .dynstr */
symtab = (Elf_Sym *)((rt_uint8_t *)module_ptr +
shdr[shdr[index].sh_link].sh_offset);
strtab = (rt_uint8_t *)module_ptr +
shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
shstrab = (rt_uint8_t *)module_ptr +
shdr[elf_module->e_shstrndx].sh_offset;
nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf_Rel));
/* relocate every items */
for (i = 0; i < nr_reloc; i ++)
{
#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);
if (sym->st_shndx != STN_UNDEF)
{
Elf_Addr addr = 0;
if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION) ||
(ELF_ST_TYPE(sym->st_info) == STT_OBJECT))
{
if (rt_strncmp((const char *)(shstrab +
shdr[sym->st_shndx].sh_name), ELF_RODATA, 8) == 0)
{
/* relocate rodata section */
LOG_D("rodata");
addr = (Elf_Addr)(rodata_addr + sym->st_value);
}
else if (rt_strncmp((const char *)
(shstrab + shdr[sym->st_shndx].sh_name), ELF_BSS, 5) == 0)
{
/* relocate bss section */
LOG_D("bss");
addr = (Elf_Addr)bss_addr + sym->st_value;
}
else if (rt_strncmp((const char *)(shstrab + shdr[sym->st_shndx].sh_name),
ELF_DATA, 6) == 0)
{
/* relocate data section */
LOG_D("data");
addr = (Elf_Addr)data_addr + sym->st_value;
}
if (addr != 0) dlmodule_relocate(module, rel, addr);
}
else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
{
addr = (Elf_Addr)((rt_uint8_t *) module->mem_space - module_addr + sym->st_value);
/* relocate function */
dlmodule_relocate(module, rel, addr);
}
}
else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
{
/* relocate function */
dlmodule_relocate(module, rel,
(Elf_Addr)((rt_uint8_t *)
module->mem_space
- module_addr
+ sym->st_value));
}
else
{
Elf_Addr addr;
if (ELF32_R_TYPE(rel->r_info) != R_ARM_V4BX)
{
LOG_D("relocate symbol: %s", strtab + sym->st_name);
/* need to resolve symbol in kernel symbol table */
addr = dlmodule_symbol_find((const char *)(strtab + sym->st_name));
if (addr != (Elf_Addr)RT_NULL)
{
dlmodule_relocate(module, rel, addr);
LOG_D("symbol addr 0x%x", addr);
}
else
LOG_E("Module: can't find %s in kernel symbol table",
strtab + sym->st_name);
}
else
{
addr = (Elf_Addr)((rt_uint8_t *) module->mem_space - module_addr + sym->st_value);
dlmodule_relocate(module, rel, addr);
}
}
rel ++;
}
}
return RT_EOK;
}