Skip to content

Commit 4940bb1

Browse files
KennethLiu61mysterywolf
authored andcommitted
components: libc: fix pointer-to-integer cast warnings and address truncation
[Problem Description] 1. When enabling RT_USING_MODULE=y, compilation warnings occur: dlelf.c:386:27: warning: cast from pointer to integer of different size dlelf.c:398:25: warning: cast from pointer to integer of different size dlelf.c:408:24: warning: cast from pointer to integer of different size 2. On RV64 architectures (e.g. Sophgo SG2042 with RISC-V Sv39 and 40-bit physical addressing), dlsym may fail when accessing addresses beyond 32-bit range. [Root Cause] In dlmodule_load_relocated_object() and dlmodule_symbol_find(), pointer is cast to rt_uint32_t which truncates: | rt_ubase_t rodata_addr = (rt_uint32_t)ptr; This causes: - Warnings on 64-bit systems (pointer width > 32-bit) - Actual address truncation on RV64 when physical address exceeds 32-bit [Solution] Replace rt_uint32_t with architecture-adaptive rt_ubase_t: | rt_ubase_t rodata_addr = (rt_ubase_t)ptr; The rt_ubase_t is defined in include/rttypes.h as: | #ifdef ARCH_CPU_64BIT | typedef rt_uint64_t rt_ubase_t; | #else | typedef rt_uint32_t rt_ubase_t; | #endif This ensures correct width casting for both 32/64-bit architectures. Signed-off-by: Liu Gui <[email protected]>
1 parent aa3328f commit 4940bb1

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

components/libc/posix/libdl/dlelf.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,9 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
383383
rt_memcpy(ptr,
384384
(rt_uint8_t *)elf_module + shdr[index].sh_offset,
385385
shdr[index].sh_size);
386-
rodata_addr = (rt_uint32_t)ptr;
386+
rodata_addr = (rt_ubase_t)ptr;
387387
LOG_D("load rodata 0x%x, size %d, rodata 0x%x", ptr,
388-
shdr[index].sh_size, *(rt_uint32_t *)data_addr);
388+
shdr[index].sh_size, *(rt_ubase_t *)rodata_addr);
389389
ptr += shdr[index].sh_size;
390390
}
391391

@@ -395,17 +395,17 @@ rt_err_t dlmodule_load_relocated_object(struct rt_dlmodule* module, void *module
395395
rt_memcpy(ptr,
396396
(rt_uint8_t *)elf_module + shdr[index].sh_offset,
397397
shdr[index].sh_size);
398-
data_addr = (rt_uint32_t)ptr;
398+
data_addr = (rt_ubase_t)ptr;
399399
LOG_D("load data 0x%x, size %d, data 0x%x", ptr,
400-
shdr[index].sh_size, *(rt_uint32_t *)data_addr);
400+
shdr[index].sh_size, *(rt_ubase_t *)data_addr);
401401
ptr += shdr[index].sh_size;
402402
}
403403

404404
/* load bss section */
405405
if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index]))
406406
{
407407
rt_memset(ptr, 0, shdr[index].sh_size);
408-
bss_addr = (rt_uint32_t)ptr;
408+
bss_addr = (rt_ubase_t)ptr;
409409
LOG_D("load bss 0x%x, size %d", ptr, shdr[index].sh_size);
410410
}
411411
}

components/libc/posix/libdl/dlmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,18 +837,18 @@ void dlmodule_exit(int ret_code)
837837
* @brief search for a symbol by its name in the kernel symbol table.
838838
*
839839
* @param sym_str the symbol name string.
840-
* @return rt_uint32_t On success, it returns the address of the symbol.
840+
* @return rt_ubase_t On success, it returns the address of the symbol.
841841
* Otherwise, it returns 0 (indicating the symbol was not found).
842842
*/
843-
rt_uint32_t dlmodule_symbol_find(const char *sym_str)
843+
rt_ubase_t dlmodule_symbol_find(const char *sym_str)
844844
{
845845
/* find in kernel symbol table */
846846
struct rt_module_symtab *index;
847847

848848
for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++)
849849
{
850850
if (rt_strcmp(index->name, sym_str) == 0)
851-
return (rt_uint32_t)index->addr;
851+
return (rt_ubase_t)index->addr;
852852
}
853853

854854
return 0;

components/libc/posix/libdl/dlmodule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ void dlmodule_exit(int ret_code);
8282

8383
struct rt_dlmodule *dlmodule_find(const char *name);
8484

85-
rt_uint32_t dlmodule_symbol_find(const char *sym_str);
85+
rt_ubase_t dlmodule_symbol_find(const char *sym_str);
8686

8787
#endif

0 commit comments

Comments
 (0)