11#include <kernel.h>
22
3- /* .dynsym/.dynstr in the image */
4- extern const elf_sym __dynsym_start [];
5- extern const elf_sym __dynsym_stop [];
6- extern const char __dynstr_start [];
7-
83const void * kernel_dlsym (const char * name ) {
9- const elf_sym * s ;
10-
11- if (name == NULL ) {
4+ if (!name ) {
125 return NULL ;
136 }
14-
15- for (s = __dynsym_start ; s < __dynsym_stop ; s ++ ) {
16- unsigned bind ;
17-
18- if (s -> st_name == 0 ) {
19- continue ;
20- }
21-
22- bind = ELF64_ST_BIND (s -> st_info );
23- if (bind != STB_GLOBAL && bind != STB_WEAK ) {
24- continue ;
25- }
26-
27- if (s -> st_shndx == SHN_UNDEF || s -> st_shndx >= SHN_LORESERVE ) {
28- continue ;
29- }
30-
31- if (strcmp (__dynstr_start + s -> st_name , name ) == 0 ) {
32- return (const void * ) (uintptr_t ) s -> st_value ; /* higher-half VMA */
33- }
34- }
35-
36- return NULL ;
7+ uint64_t a = findsymbol_addr (name );
8+ return a ? (const void * ) (uintptr_t ) a : NULL ;
379}
3810
3911bool parse_elf_rel_headers (const uint8_t * file , size_t len , const elf_ehdr * * eh , const elf_shdr * * sh , size_t * shnum , const char * * shstr ) {
@@ -236,15 +208,21 @@ bool module_apply_relocations(const uint8_t *file, const elf_shdr *sh, size_t sh
236208 }
237209
238210 uint16_t tgt_index = (uint16_t ) sh [i ].sh_info ;
239- uint8_t * tgt_base = module_section_base (m , tgt_index );
240- const elf_rela * rela = (const elf_rela * ) (file + sh [i ].sh_offset );
241- size_t count = (size_t ) (sh [i ].sh_size / sizeof (elf_rela ));
242211
212+ /* NEW: ignore relocations for non-ALLOC target sections (e.g. .debug*) */
213+ if (tgt_index >= shnum || !is_alloc_section (& sh [tgt_index ])) {
214+ continue ;
215+ }
216+
217+ uint8_t * tgt_base = module_section_base (m , tgt_index );
243218 if (tgt_base == NULL ) {
244- dprintf ("module_apply_relocations: target section base not found\n" );
219+ dprintf ("module_apply_relocations: target section base not found (idx=%u) \n" , tgt_index );
245220 return false;
246221 }
247222
223+ const elf_rela * rela = (const elf_rela * ) (file + sh [i ].sh_offset );
224+ size_t count = (size_t ) (sh [i ].sh_size / sizeof (elf_rela ));
225+
248226 for (size_t j = 0 ; j < count ; j ++ ) {
249227 uint32_t type = (uint32_t ) ELF64_R_TYPE (rela [j ].r_info );
250228 uint32_t si = (uint32_t ) ELF64_R_SYM (rela [j ].r_info );
@@ -263,10 +241,10 @@ bool module_apply_relocations(const uint8_t *file, const elf_shdr *sh, size_t sh
263241 }
264242
265243 switch (type ) {
266- case R_X86_64_64 : {
244+ case R_X86_64_64 :
267245 * (uint64_t * ) P = (uint64_t ) ((uint64_t ) S + (uint64_t ) A );
268246 break ;
269- }
247+
270248 case R_X86_64_PC32 : {
271249 int64_t val = (int64_t ) S + A - (int64_t ) P ;
272250 if (val < INT_MIN || val > INT_MAX ) {
@@ -276,6 +254,18 @@ bool module_apply_relocations(const uint8_t *file, const elf_shdr *sh, size_t sh
276254 * (int32_t * ) P = (int32_t ) val ;
277255 break ;
278256 }
257+
258+ /* Compilers often emit PLT32 for extern calls */
259+ case 4 /* R_X86_64_PLT32 */ : {
260+ int64_t val = (int64_t ) S + A - (int64_t ) P ;
261+ if (val < INT_MIN || val > INT_MAX ) {
262+ dprintf ("module_apply_relocations: PLT32 overflow\n" );
263+ return false;
264+ }
265+ * (int32_t * ) P = (int32_t ) val ;
266+ break ;
267+ }
268+
279269 case R_X86_64_32 : {
280270 uint64_t val = (uint64_t ) S + (uint64_t ) A ;
281271 if (val > UINT_MAX ) {
@@ -285,6 +275,7 @@ bool module_apply_relocations(const uint8_t *file, const elf_shdr *sh, size_t sh
285275 * (uint32_t * ) P = (uint32_t ) val ;
286276 break ;
287277 }
278+
288279 case R_X86_64_32S : {
289280 int64_t val = (int64_t ) S + A ;
290281 if (val < INT_MIN || val > INT_MAX ) {
@@ -294,14 +285,13 @@ bool module_apply_relocations(const uint8_t *file, const elf_shdr *sh, size_t sh
294285 * (int32_t * ) P = (int32_t ) val ;
295286 break ;
296287 }
297- default : {
288+
289+ default :
298290 dprintf ("module_apply_relocations: unsupported reloc type: %u\n" , type );
299291 return false;
300- }
301292 }
302293 }
303294 }
304-
305295 return true;
306296}
307297
0 commit comments