@@ -131,132 +131,6 @@ static int elf_load_tls_template_from_vfs(const char* path, elf_image_info_t* in
131131 return 0 ;
132132}
133133
134- typedef uint64_t (* elf_ifunc_resolver_t )(void );
135-
136- static int elf_apply_relocation_entries (const Elf64_Rela * relocs , uint64_t reloc_count )
137- {
138- if (!relocs || reloc_count == 0 )
139- return 0 ;
140-
141- for (uint64_t i = 0 ; i < reloc_count ; ++ i ) {
142- const Elf64_Rela * reloc = & relocs [i ];
143- uint32_t reloc_type = ELF64_R_TYPE (reloc -> r_info );
144- uint64_t * target = (uint64_t * )reloc -> r_offset ;
145-
146- if (!target ) {
147- eprintf ("elf: invalid relocation target" );
148- return -1 ;
149- }
150-
151- switch (reloc_type ) {
152- case R_X86_64_RELATIVE :
153- * target = (uint64_t )reloc -> r_addend ;
154- break ;
155-
156- case R_X86_64_IRELATIVE : {
157- elf_ifunc_resolver_t resolver = (elf_ifunc_resolver_t )(uint64_t )reloc -> r_addend ;
158- * target = resolver ? resolver () : 0 ;
159- break ;
160- }
161-
162- default :
163- break ;
164- }
165- }
166-
167- return 0 ;
168- }
169-
170- static int elf_apply_relocations_from_memory (void * file_base_address , uint64_t file_size , const Elf64_Ehdr * header )
171- {
172- if (!file_base_address || !header || header -> e_shoff == 0 || header -> e_shnum == 0 )
173- return 0 ;
174-
175- uint64_t shdr_bytes = (uint64_t )header -> e_shnum * header -> e_shentsize ;
176- if (header -> e_shentsize != sizeof (Elf64_Shdr ) || header -> e_shoff + shdr_bytes > file_size ) {
177- eprintf ("elf: invalid section header table" );
178- return -1 ;
179- }
180-
181- Elf64_Shdr * shdrs = (Elf64_Shdr * )((uint8_t * )file_base_address + header -> e_shoff );
182- for (uint16_t i = 0 ; i < header -> e_shnum ; ++ i ) {
183- Elf64_Shdr * sh = & shdrs [i ];
184- if (sh -> sh_type != SHT_RELA || sh -> sh_size == 0 )
185- continue ;
186-
187- if (sh -> sh_offset + sh -> sh_size > file_size || (sh -> sh_size % sizeof (Elf64_Rela )) != 0 ) {
188- eprintf ("elf: invalid SHT_RELA bounds" );
189- return -1 ;
190- }
191-
192- if (elf_apply_relocation_entries ((Elf64_Rela * )((uint8_t * )file_base_address + sh -> sh_offset ),
193- sh -> sh_size / sizeof (Elf64_Rela )) != 0 )
194- return -1 ;
195- }
196-
197- return 0 ;
198- }
199-
200- static int elf_apply_relocations_from_vfs (const char * path , uint32_t file_size , const Elf64_Ehdr * header )
201- {
202- if (!path || !header || header -> e_shoff == 0 || header -> e_shnum == 0 )
203- return 0 ;
204-
205- uint64_t shdr_bytes = (uint64_t )header -> e_shnum * header -> e_shentsize ;
206- if (header -> e_shentsize != sizeof (Elf64_Shdr ) || header -> e_shoff + shdr_bytes > file_size ) {
207- eprintf ("elf: invalid section header table" );
208- return -1 ;
209- }
210-
211- Elf64_Shdr * shdrs = kmalloc (shdr_bytes );
212- if (!shdrs ) {
213- eprintf ("elf: failed to allocate section headers" );
214- return -1 ;
215- }
216-
217- if (elf_vfs_read_exact_path (path , (uint32_t )header -> e_shoff , shdrs , (uint32_t )shdr_bytes ) != 0 ) {
218- eprintf ("elf: failed to read section headers" );
219- kfree (shdrs );
220- return -1 ;
221- }
222-
223- for (uint16_t i = 0 ; i < header -> e_shnum ; ++ i ) {
224- Elf64_Shdr * sh = & shdrs [i ];
225- if (sh -> sh_type != SHT_RELA || sh -> sh_size == 0 )
226- continue ;
227-
228- if (sh -> sh_offset + sh -> sh_size > file_size || (sh -> sh_size % sizeof (Elf64_Rela )) != 0 ) {
229- eprintf ("elf: invalid SHT_RELA bounds" );
230- kfree (shdrs );
231- return -1 ;
232- }
233-
234- Elf64_Rela * relocs = kmalloc (sh -> sh_size );
235- if (!relocs ) {
236- eprintf ("elf: failed to allocate relocations" );
237- kfree (shdrs );
238- return -1 ;
239- }
240-
241- if (elf_vfs_read_exact_path (path , (uint32_t )sh -> sh_offset , relocs , (uint32_t )sh -> sh_size ) != 0 ) {
242- eprintf ("elf: failed to read relocations" );
243- kfree (relocs );
244- kfree (shdrs );
245- return -1 ;
246- }
247-
248- int rc = elf_apply_relocation_entries (relocs , sh -> sh_size / sizeof (Elf64_Rela ));
249- kfree (relocs );
250- if (rc != 0 ) {
251- kfree (shdrs );
252- return -1 ;
253- }
254- }
255-
256- kfree (shdrs );
257- return 0 ;
258- }
259-
260134static uint64_t elf_runtime_addr_for_offset (Elf64_Phdr * headers , uint16_t phnum , uint64_t file_offset )
261135{
262136 for (uint16_t i = 0 ; i < phnum ; ++ i ) {
@@ -491,9 +365,6 @@ void* elf_load_from_memory_ex(void* file_base_address, uint64_t file_size, elf_i
491365 return NULL ;
492366 }
493367
494- if (elf_apply_relocations_from_memory (file_base_address , file_size , & header ) != 0 )
495- return NULL ;
496-
497368 return (void * )header .e_entry ;
498369}
499370
@@ -596,11 +467,6 @@ void* elf_load_from_vfs_ex(const char* path, elf_image_info_t* info)
596467 }
597468 }
598469
599- if (elf_apply_relocations_from_vfs (path , size , & header ) != 0 ) {
600- kfree (program_headers );
601- return NULL ;
602- }
603-
604470 kfree (program_headers );
605471 void * entry = (void * )header .e_entry ;
606472 return entry ;
0 commit comments