Skip to content

Commit b75d355

Browse files
style: format code with clang-format [skip ci]
1 parent cbef04a commit b75d355

1 file changed

Lines changed: 60 additions & 59 deletions

File tree

components/libc/posix/libdl/dlelf.c

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include "dlmodule.h"
1313
#include "dlelf.h"
1414

15-
#define DBG_TAG "DLMD"
16-
#define DBG_LVL DBG_INFO
15+
#define DBG_TAG "DLMD"
16+
#define DBG_LVL DBG_INFO
1717
#include <rtdbg.h> /* must after of DEBUG_ENABLE or some other options*/
1818

1919
/**
@@ -40,10 +40,10 @@
4040
* 7. Extracting Additional Parameters: It extracts additional parameters, such as thread priority (dlmodule_thread_priority) and stack size (dlmodule_thread_stacksize),
4141
* from the symbol table and assigns them to the module if valid.
4242
*/
43-
rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_ptr)
43+
rt_err_t dlmodule_load_shared_object(struct rt_dlmodule *module, void *module_ptr)
4444
{
45-
rt_bool_t linked = RT_FALSE;
46-
rt_ubase_t index, module_size = 0;
45+
rt_bool_t linked = RT_FALSE;
46+
rt_ubase_t index, module_size = 0;
4747
Elf_Addr vstart_addr, vend_addr;
4848
rt_bool_t has_vstart;
4949

@@ -79,7 +79,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
7979
if (vend_addr < vstart_addr)
8080
{
8181
LOG_E("invalid elf: segment %d: p_vaddr: %d, p_memsz: %d\n",
82-
index, phdr[index].p_vaddr, phdr[index].p_memsz);
82+
index, phdr[index].p_vaddr, phdr[index].p_memsz);
8383
return RT_NULL;
8484
}
8585
}
@@ -100,7 +100,8 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
100100
if (vend_addr < phdr[index].p_vaddr)
101101
{
102102
LOG_E("invalid elf: "
103-
"segment %d address overflow\n", index);
103+
"segment %d address overflow\n",
104+
index);
104105
return RT_NULL;
105106
}
106107
}
@@ -142,42 +143,42 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
142143
module->entry_addr = module->mem_space + elf_module->e_entry - vstart_addr;
143144

144145
/* handle relocation section */
145-
for (index = 0; index < elf_module->e_shnum; index ++)
146+
for (index = 0; index < elf_module->e_shnum; index++)
146147
{
147148
rt_ubase_t i, nr_reloc;
148149
Elf_Sym *symtab;
149150
Elf_Rel *rel;
150151
rt_uint8_t *strtab;
151152
static rt_bool_t unsolved = RT_FALSE;
152-
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
153+
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
153154
if (!IS_REL(shdr[index]))
154155
continue;
155-
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
156+
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
156157
if (!IS_RELA(shdr[index]))
157158
continue;
158-
#endif
159+
#endif
159160

160161
/* get relocate item */
161162
rel = (Elf_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
162163

163164
/* locate .rel.plt and .rel.dyn section */
164165
symtab = (Elf_Sym *)((rt_uint8_t *)module_ptr +
165-
shdr[shdr[index].sh_link].sh_offset);
166+
shdr[shdr[index].sh_link].sh_offset);
166167
strtab = (rt_uint8_t *)module_ptr +
167168
shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
168169
nr_reloc = (rt_ubase_t)(shdr[index].sh_size / sizeof(Elf_Rel));
169170

170171
/* relocate every items */
171-
for (i = 0; i < nr_reloc; i ++)
172+
for (i = 0; i < nr_reloc; i++)
172173
{
173-
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
174+
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
174175
Elf_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
175-
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
176+
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
176177
Elf_Sym *sym = &symtab[ELF64_R_SYM(rel->r_info)];
177-
#endif
178+
#endif
178179
LOG_D("relocate symbol %s shndx %d", strtab + sym->st_name, sym->st_shndx);
179180

180-
if ((sym->st_shndx != SHT_NULL) ||(ELF_ST_BIND(sym->st_info) == STB_LOCAL))
181+
if ((sym->st_shndx != SHT_NULL) || (ELF_ST_BIND(sym->st_info) == STB_LOCAL))
181182
{
182183
Elf_Addr addr;
183184

@@ -201,7 +202,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
201202
dlmodule_relocate(module, rel, addr);
202203
}
203204
}
204-
rel ++;
205+
rel++;
205206
}
206207

207208
if (unsolved)
@@ -211,8 +212,8 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
211212
/* construct module symbol table */
212213
rt_uint8_t *shstrab;
213214
shstrab = (rt_uint8_t *)module_ptr +
214-
shdr[elf_module->e_shstrndx].sh_offset;
215-
for (index = 0; index < elf_module->e_shnum; index ++)
215+
shdr[elf_module->e_shstrndx].sh_offset;
216+
for (index = 0; index < elf_module->e_shnum; index++)
216217
{
217218
/* find .dynsym section */
218219
if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0)
@@ -223,7 +224,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
223224
if (index != elf_module->e_shnum)
224225
{
225226
int i, count = 0;
226-
Elf_Sym *symtab = RT_NULL;
227+
Elf_Sym *symtab = RT_NULL;
227228
rt_uint8_t *strtab = RT_NULL;
228229

229230
symtab = (Elf_Sym *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
@@ -233,11 +234,10 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
233234
{
234235
if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) &&
235236
(ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
236-
count ++;
237+
count++;
237238
}
238239

239-
module->symtab = (struct rt_module_symtab *)rt_malloc
240-
(count * sizeof(struct rt_module_symtab));
240+
module->symtab = (struct rt_module_symtab *)rt_malloc(count * sizeof(struct rt_module_symtab));
241241
module->nsym = count;
242242
for (i = 0, count = 0; i < shdr[index].sh_size / sizeof(Elf_Sym); i++)
243243
{
@@ -256,7 +256,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
256256
rt_memcpy((void *)module->symtab[count].name,
257257
strtab + symtab[i].st_name,
258258
length);
259-
count ++;
259+
count++;
260260
}
261261

262262
/* get priority & stack size params*/
@@ -269,7 +269,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
269269
(rt_strcmp((const char *)(strtab + symtab[i].st_name), "dlmodule_thread_priority") == 0))
270270
{
271271
flag |= 0x01;
272-
priority = *(rt_uint16_t*)(module->mem_space + symtab[i].st_value - module->vstart_addr);
272+
priority = *(rt_uint16_t *)(module->mem_space + symtab[i].st_value - module->vstart_addr);
273273
if (priority < RT_THREAD_PRIORITY_MAX)
274274
{
275275
module->priority = priority;
@@ -280,7 +280,7 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
280280
(rt_strcmp((const char *)(strtab + symtab[i].st_name), "dlmodule_thread_stacksize") == 0))
281281
{
282282
flag |= 0x02;
283-
stacksize = *(rt_uint32_t*)(module->mem_space + symtab[i].st_value - module->vstart_addr);
283+
stacksize = *(rt_uint32_t *)(module->mem_space + symtab[i].st_value - module->vstart_addr);
284284
if ((stacksize < 2048) || (stacksize > 1024 * 32))
285285
{
286286
module->stack_size = stacksize;
@@ -314,14 +314,14 @@ rt_err_t dlmodule_load_shared_object(struct rt_dlmodule* module, void *module_pt
314314
* 5. Handle Relocation: It processes the relocation entries, resolving symbol addresses and relocating them as needed.
315315
* This includes functions, sections (rodata, bss, data), and external symbols from the kernel symbol table.
316316
*/
317-
rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module_ptr)
317+
rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule *module, void *module_ptr)
318318
{
319319
rt_ubase_t index, rodata_addr = 0, bss_addr = 0, data_addr = 0;
320320
rt_ubase_t module_addr = 0, module_size = 0;
321321
rt_uint8_t *ptr, *strtab, *shstrab;
322322

323323
/* get the ELF image size */
324-
for (index = 0; index < elf_module->e_shnum; index ++)
324+
for (index = 0; index < elf_module->e_shnum; index++)
325325
{
326326
/* text */
327327
if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
@@ -347,7 +347,8 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
347347
}
348348

349349
/* no text, data and bss on image */
350-
if (module_size == 0) return RT_NULL;
350+
if (module_size == 0)
351+
return RT_NULL;
351352

352353
module->vstart_addr = 0;
353354

@@ -365,7 +366,7 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
365366
rt_memset(ptr, 0, module_size);
366367

367368
/* load text and data section */
368-
for (index = 0; index < elf_module->e_shnum; index ++)
369+
for (index = 0; index < elf_module->e_shnum; index++)
369370
{
370371
/* load text section */
371372
if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
@@ -385,7 +386,7 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
385386
shdr[index].sh_size);
386387
rodata_addr = (rt_ubase_t)ptr;
387388
LOG_D("load rodata 0x%x, size %d, rodata 0x%x", ptr,
388-
shdr[index].sh_size, *(rt_ubase_t *)rodata_addr);
389+
shdr[index].sh_size, *(rt_ubase_t *)rodata_addr);
389390
ptr += shdr[index].sh_size;
390391
}
391392

@@ -397,7 +398,7 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
397398
shdr[index].sh_size);
398399
data_addr = (rt_ubase_t)ptr;
399400
LOG_D("load data 0x%x, size %d, data 0x%x", ptr,
400-
shdr[index].sh_size, *(rt_ubase_t *)data_addr);
401+
shdr[index].sh_size, *(rt_ubase_t *)data_addr);
401402
ptr += shdr[index].sh_size;
402403
}
403404

@@ -414,41 +415,41 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
414415
module->entry_addr = (rt_dlmodule_entry_func_t)((rt_uint8_t *)module->mem_space + elf_module->e_entry - module_addr);
415416

416417
/* handle relocation section */
417-
for (index = 0; index < elf_module->e_shnum; index ++)
418+
for (index = 0; index < elf_module->e_shnum; index++)
418419
{
419420
rt_ubase_t i, nr_reloc;
420421
Elf_Sym *symtab;
421422
Elf_Rel *rel;
422423

423-
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
424+
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
424425
if (!IS_REL(shdr[index]))
425426
continue;
426-
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
427+
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
427428
if (!IS_RELA(shdr[index]))
428429
continue;
429-
#endif
430+
#endif
430431

431432

432433
/* get relocate item */
433434
rel = (Elf_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
434435

435436
/* locate .dynsym and .dynstr */
436-
symtab = (Elf_Sym *)((rt_uint8_t *)module_ptr +
437-
shdr[shdr[index].sh_link].sh_offset);
438-
strtab = (rt_uint8_t *)module_ptr +
439-
shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
440-
shstrab = (rt_uint8_t *)module_ptr +
441-
shdr[elf_module->e_shstrndx].sh_offset;
437+
symtab = (Elf_Sym *)((rt_uint8_t *)module_ptr +
438+
shdr[shdr[index].sh_link].sh_offset);
439+
strtab = (rt_uint8_t *)module_ptr +
440+
shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
441+
shstrab = (rt_uint8_t *)module_ptr +
442+
shdr[elf_module->e_shstrndx].sh_offset;
442443
nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf_Rel));
443444

444445
/* relocate every items */
445-
for (i = 0; i < nr_reloc; i ++)
446+
for (i = 0; i < nr_reloc; i++)
446447
{
447-
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
448+
#if (defined(__arm__) || defined(__i386__) || (__riscv_xlen == 32))
448449
Elf_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
449-
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
450+
#elif (defined(__aarch64__) || defined(__x86_64__) || (__riscv_xlen == 64))
450451
Elf_Sym *sym = &symtab[ELF64_R_SYM(rel->r_info)];
451-
#endif
452+
#endif
452453

453454
LOG_D("relocate symbol: %s", strtab + sym->st_name);
454455

@@ -460,14 +461,14 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
460461
(ELF_ST_TYPE(sym->st_info) == STT_OBJECT))
461462
{
462463
if (rt_strncmp((const char *)(shstrab +
463-
shdr[sym->st_shndx].sh_name), ELF_RODATA, 8) == 0)
464+
shdr[sym->st_shndx].sh_name),
465+
ELF_RODATA, 8) == 0)
464466
{
465467
/* relocate rodata section */
466468
LOG_D("rodata");
467469
addr = (Elf_Addr)(rodata_addr + sym->st_value);
468470
}
469-
else if (rt_strncmp((const char *)
470-
(shstrab + shdr[sym->st_shndx].sh_name), ELF_BSS, 5) == 0)
471+
else if (rt_strncmp((const char *)(shstrab + shdr[sym->st_shndx].sh_name), ELF_BSS, 5) == 0)
471472
{
472473
/* relocate bss section */
473474
LOG_D("bss");
@@ -481,11 +482,12 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
481482
addr = (Elf_Addr)data_addr + sym->st_value;
482483
}
483484

484-
if (addr != 0) dlmodule_relocate(module, rel, addr);
485+
if (addr != 0)
486+
dlmodule_relocate(module, rel, addr);
485487
}
486488
else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
487489
{
488-
addr = (Elf_Addr)((rt_uint8_t *) module->mem_space - module_addr + sym->st_value);
490+
addr = (Elf_Addr)((rt_uint8_t *)module->mem_space - module_addr + sym->st_value);
489491

490492
/* relocate function */
491493
dlmodule_relocate(module, rel, addr);
@@ -495,10 +497,9 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
495497
{
496498
/* relocate function */
497499
dlmodule_relocate(module, rel,
498-
(Elf_Addr)((rt_uint8_t *)
499-
module->mem_space
500-
- module_addr
501-
+ sym->st_value));
500+
(Elf_Addr)((rt_uint8_t *)
501+
module->mem_space -
502+
module_addr + sym->st_value));
502503
}
503504
else
504505
{
@@ -517,16 +518,16 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
517518
}
518519
else
519520
LOG_E("Module: can't find %s in kernel symbol table",
520-
strtab + sym->st_name);
521+
strtab + sym->st_name);
521522
}
522523
else
523524
{
524-
addr = (Elf_Addr)((rt_uint8_t *) module->mem_space - module_addr + sym->st_value);
525+
addr = (Elf_Addr)((rt_uint8_t *)module->mem_space - module_addr + sym->st_value);
525526
dlmodule_relocate(module, rel, addr);
526527
}
527528
}
528529

529-
rel ++;
530+
rel++;
530531
}
531532
}
532533

0 commit comments

Comments
 (0)