xref: /haiku/src/system/boot/loader/elf.cpp (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
1 /*
2  * Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Copyright 2012, Alex Smith, alex@alex-smith.me.uk.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "elf.h"
9 
10 #include <boot/arch.h>
11 #include <boot/platform.h>
12 #include <boot/stage2.h>
13 #include <driver_settings.h>
14 #include <elf_private.h>
15 #include <kernel.h>
16 #include <SupportDefs.h>
17 
18 #include <errno.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <stdlib.h>
22 
23 //#define TRACE_ELF
24 #ifdef TRACE_ELF
25 #	define TRACE(x) dprintf x
26 #else
27 #	define TRACE(x) ;
28 #endif
29 
30 
31 static bool sLoadElfSymbols = true;
32 
33 
34 // #pragma mark - Generic ELF loader
35 
36 
37 template<typename Class>
38 class ELFLoader {
39 private:
40 	typedef typename Class::ImageType	ImageType;
41 	typedef typename Class::RegionType	RegionType;
42 	typedef typename Class::AddrType	AddrType;
43 	typedef typename Class::EhdrType	EhdrType;
44 	typedef typename Class::PhdrType	PhdrType;
45 	typedef typename Class::ShdrType	ShdrType;
46 	typedef typename Class::DynType		DynType;
47 	typedef typename Class::SymType		SymType;
48 	typedef typename Class::RelType		RelType;
49 	typedef typename Class::RelaType	RelaType;
50 
51 public:
52 	static	status_t	Create(int fd, preloaded_image** _image);
53 	static	status_t	Load(int fd, preloaded_image* image);
54 	static	status_t	Relocate(preloaded_image* image);
55 	static	status_t	Resolve(ImageType* image, SymType* symbol,
56 							AddrType* symbolAddress);
57 
58 private:
59 	static	status_t	_LoadSymbolTable(int fd, ImageType* image);
60 	static	status_t	_ParseDynamicSection(ImageType* image);
61 };
62 
63 
64 #ifdef BOOT_SUPPORT_ELF32
65 struct ELF32Class {
66 	static const uint8 kIdentClass = ELFCLASS32;
67 
68 	typedef preloaded_elf32_image	ImageType;
69 	typedef elf32_region			RegionType;
70 	typedef Elf32_Addr				AddrType;
71 	typedef Elf32_Ehdr				EhdrType;
72 	typedef Elf32_Phdr				PhdrType;
73 	typedef Elf32_Shdr				ShdrType;
74 	typedef Elf32_Dyn				DynType;
75 	typedef Elf32_Sym				SymType;
76 	typedef Elf32_Rel				RelType;
77 	typedef Elf32_Rela				RelaType;
78 
79 	static inline status_t
80 	AllocateRegion(AddrType* _address, AddrType size, uint8 protection,
81 		void** _mappedAddress)
82 	{
83 		status_t status = platform_allocate_region((void**)_address, size,
84 			protection, false);
85 		if (status != B_OK)
86 			return status;
87 
88 		*_mappedAddress = (void*)*_address;
89 
90 		addr_t res;
91 		platform_bootloader_address_to_kernel_address((void*)*_address, &res);
92 
93 		*_address = res;
94 
95 		return B_OK;
96 	}
97 
98 	static inline void*
99 	Map(AddrType address)
100 	{
101 		void *result = NULL;
102 		if (platform_kernel_address_to_bootloader_address(address, &result) != B_OK) {
103 			panic("Couldn't convert address 0x%08x", (uint32_t)address);
104 		}
105 
106 		return result;
107 	}
108 };
109 
110 typedef ELFLoader<ELF32Class> ELF32Loader;
111 #endif
112 
113 
114 #ifdef BOOT_SUPPORT_ELF64
115 struct ELF64Class {
116 	static const uint8 kIdentClass = ELFCLASS64;
117 
118 	typedef preloaded_elf64_image	ImageType;
119 	typedef elf64_region			RegionType;
120 	typedef Elf64_Addr				AddrType;
121 	typedef Elf64_Ehdr				EhdrType;
122 	typedef Elf64_Phdr				PhdrType;
123 	typedef Elf64_Shdr				ShdrType;
124 	typedef Elf64_Dyn				DynType;
125 	typedef Elf64_Sym				SymType;
126 	typedef Elf64_Rel				RelType;
127 	typedef Elf64_Rela				RelaType;
128 
129 	static inline status_t
130 	AllocateRegion(AddrType* _address, AddrType size, uint8 protection,
131 		void **_mappedAddress)
132 	{
133 #if defined(_BOOT_PLATFORM_BIOS)
134 		// Assume the real 64-bit base address is KERNEL_LOAD_BASE_64_BIT and
135 		// the mappings in the loader address space are at KERNEL_LOAD_BASE.
136 
137 		void* address = (void*)(addr_t)(*_address & 0xffffffff);
138 #else
139 		void* address = (void*)*_address;
140 #endif
141 
142 		status_t status = platform_allocate_region(&address, size, protection,
143 			false);
144 		if (status != B_OK)
145 			return status;
146 
147 		*_mappedAddress = address;
148 #if defined(_BOOT_PLATFORM_BIOS)
149 		*_address = (AddrType)(addr_t)address + KERNEL_FIXUP_FOR_LONG_MODE;
150 #else
151 		platform_bootloader_address_to_kernel_address(address, _address);
152 #endif
153 		return B_OK;
154 	}
155 
156 	static inline void*
157 	Map(AddrType address)
158 	{
159 #ifdef _BOOT_PLATFORM_BIOS
160 		return (void*)(addr_t)(address - KERNEL_FIXUP_FOR_LONG_MODE);
161 #else
162 		void *result;
163 		if (platform_kernel_address_to_bootloader_address(address, &result) != B_OK) {
164 			panic("Couldn't convert address %#" PRIx64, address);
165 		}
166 		return result;
167 #endif
168 	}
169 };
170 
171 typedef ELFLoader<ELF64Class> ELF64Loader;
172 #endif
173 
174 
175 template<typename Class>
176 /*static*/ status_t
177 ELFLoader<Class>::Create(int fd, preloaded_image** _image)
178 {
179 	ImageType* image = (ImageType*)kernel_args_malloc(sizeof(ImageType));
180 	if (image == NULL)
181 		return B_NO_MEMORY;
182 
183 	ssize_t length = read_pos(fd, 0, &image->elf_header, sizeof(EhdrType));
184 	if (length < (ssize_t)sizeof(EhdrType)) {
185 		kernel_args_free(image);
186 		return B_BAD_TYPE;
187 	}
188 
189 	const EhdrType& elfHeader = image->elf_header;
190 
191 	if (memcmp(elfHeader.e_ident, ELFMAG, 4) != 0
192 		|| elfHeader.e_ident[4] != Class::kIdentClass
193 		|| elfHeader.e_phoff == 0
194 		|| !elfHeader.IsHostEndian()
195 		|| elfHeader.e_phentsize != sizeof(PhdrType)) {
196 		kernel_args_free(image);
197 		return B_BAD_TYPE;
198 	}
199 
200 	image->elf_class = elfHeader.e_ident[EI_CLASS];
201 
202 	*_image = image;
203 	return B_OK;
204 }
205 
206 
207 template<typename Class>
208 /*static*/ status_t
209 ELFLoader<Class>::Load(int fd, preloaded_image* _image)
210 {
211 	size_t totalSize;
212 	ssize_t length;
213 	status_t status;
214 	void* mappedRegion = NULL;
215 
216 	ImageType* image = static_cast<ImageType*>(_image);
217 	const EhdrType& elfHeader = image->elf_header;
218 
219 	ssize_t size = elfHeader.e_phnum * elfHeader.e_phentsize;
220 	PhdrType* programHeaders = (PhdrType*)malloc(size);
221 	if (programHeaders == NULL) {
222 		dprintf("error allocating space for program headers\n");
223 		status = B_NO_MEMORY;
224 		goto error1;
225 	}
226 
227 	length = read_pos(fd, elfHeader.e_phoff, programHeaders, size);
228 	if (length < size) {
229 		TRACE(("error reading in program headers\n"));
230 		status = B_ERROR;
231 		goto error1;
232 	}
233 
234 	// create an area large enough to hold the image
235 
236 	image->data_region.size = 0;
237 	image->text_region.size = 0;
238 
239 	for (int32 i = 0; i < elfHeader.e_phnum; i++) {
240 		PhdrType& header = programHeaders[i];
241 
242 		switch (header.p_type) {
243 			case PT_LOAD:
244 				break;
245 			case PT_DYNAMIC:
246 				image->dynamic_section.start = header.p_vaddr;
247 				image->dynamic_section.size = header.p_memsz;
248 				continue;
249 			case PT_INTERP:
250 			case PT_PHDR:
251 			case PT_ARM_UNWIND:
252 				// known but unused type
253 				continue;
254 			default:
255 				dprintf("unhandled pheader type 0x%" B_PRIx32 "\n", header.p_type);
256 				continue;
257 		}
258 
259 		RegionType* region;
260 		if (header.IsReadWrite()) {
261 			if (image->data_region.size != 0) {
262 				dprintf("elf: rw already handled!\n");
263 				continue;
264 			}
265 			region = &image->data_region;
266 		} else if (header.IsExecutable()) {
267 			if (image->text_region.size != 0) {
268 				dprintf("elf: ro already handled!\n");
269 				continue;
270 			}
271 			region = &image->text_region;
272 		} else
273 			continue;
274 
275 		region->start = ROUNDDOWN(header.p_vaddr, B_PAGE_SIZE);
276 		region->size = ROUNDUP(header.p_memsz + (header.p_vaddr % B_PAGE_SIZE),
277 			B_PAGE_SIZE);
278 		region->delta = -region->start;
279 
280 		TRACE(("segment %" B_PRId32 ": start = 0x%" B_PRIx64 ", size = %"
281 			B_PRIu64 ", delta = %" B_PRIx64 "\n", i, (uint64)region->start,
282 			(uint64)region->size, (int64)(AddrType)region->delta));
283 	}
284 
285 
286 	// found both, text and data?
287 	if (image->data_region.size == 0 || image->text_region.size == 0) {
288 		dprintf("Couldn't find both text and data segment!\n");
289 		status = B_BAD_DATA;
290 		goto error1;
291 	}
292 
293 	// get the segment order
294 	RegionType* firstRegion;
295 	RegionType* secondRegion;
296 	if (image->text_region.start < image->data_region.start) {
297 		firstRegion = &image->text_region;
298 		secondRegion = &image->data_region;
299 	} else {
300 		firstRegion = &image->data_region;
301 		secondRegion = &image->text_region;
302 	}
303 
304 	// The kernel and the modules are relocatable, thus AllocateRegion()
305 	// can automatically allocate an address, but shall prefer the specified
306 	// base address.
307 	totalSize = secondRegion->start + secondRegion->size - firstRegion->start;
308 	{
309 		AddrType address = firstRegion->start;
310 		if (Class::AllocateRegion(&address, totalSize,
311 				B_READ_AREA | B_WRITE_AREA | B_EXECUTE_AREA, &mappedRegion)
312 				!= B_OK) {
313 			status = B_NO_MEMORY;
314 			goto error1;
315 		}
316 		firstRegion->start = address;
317 	}
318 
319 	// initialize the region pointers to the allocated region
320 	secondRegion->start += firstRegion->start + firstRegion->delta;
321 
322 	image->data_region.delta += image->data_region.start;
323 	image->text_region.delta += image->text_region.start;
324 
325 	TRACE(("text: start 0x%" B_PRIx64 ", size 0x%" B_PRIx64 ", delta 0x%"
326 		B_PRIx64 "\n", (uint64)image->text_region.start,
327 		(uint64)image->text_region.size,
328 		(int64)(AddrType)image->text_region.delta));
329 	TRACE(("data: start 0x%" B_PRIx64 ", size 0x%" B_PRIx64 ", delta 0x%"
330 		B_PRIx64 "\n", (uint64)image->data_region.start,
331 		(uint64)image->data_region.size,
332 		(int64)(AddrType)image->data_region.delta));
333 
334 	// load program data
335 
336 	for (int32 i = 0; i < elfHeader.e_phnum; i++) {
337 		PhdrType& header = programHeaders[i];
338 
339 		if (header.p_type != PT_LOAD)
340 			continue;
341 
342 		RegionType* region;
343 		if (header.IsReadWrite())
344 			region = &image->data_region;
345 		else if (header.IsExecutable())
346 			region = &image->text_region;
347 		else
348 			continue;
349 
350 		TRACE(("load segment %" PRId32 " (%" PRIu64 " bytes) mapped at %p...\n",
351 			i, (uint64)header.p_filesz, Class::Map(region->start)));
352 
353 		length = read_pos(fd, header.p_offset,
354 			Class::Map(region->start + (header.p_vaddr % B_PAGE_SIZE)),
355 			header.p_filesz);
356 		if (length < (ssize_t)header.p_filesz) {
357 			status = B_BAD_DATA;
358 			dprintf("error reading in seg %" B_PRId32 "\n", i);
359 			goto error2;
360 		}
361 
362 		// Clear anything above the file size (that may also contain the BSS
363 		// area)
364 
365 		uint32 offset = (header.p_vaddr % B_PAGE_SIZE) + header.p_filesz;
366 		if (offset < region->size)
367 			memset(Class::Map(region->start + offset), 0, region->size - offset);
368 	}
369 
370 	// offset dynamic section, and program entry addresses by the delta of the
371 	// regions
372 	image->dynamic_section.start += image->text_region.delta;
373 	image->elf_header.e_entry += image->text_region.delta;
374 
375 	image->num_debug_symbols = 0;
376 	image->debug_symbols = NULL;
377 	image->debug_string_table = NULL;
378 
379 	if (sLoadElfSymbols)
380 		_LoadSymbolTable(fd, image);
381 
382 	free(programHeaders);
383 
384 	return B_OK;
385 
386 error2:
387 	if (mappedRegion != NULL)
388 		platform_free_region(mappedRegion, totalSize);
389 error1:
390 	free(programHeaders);
391 	kernel_args_free(image);
392 
393 	return status;
394 }
395 
396 
397 template<typename Class>
398 /*static*/ status_t
399 ELFLoader<Class>::Relocate(preloaded_image* _image)
400 {
401 	ImageType* image = static_cast<ImageType*>(_image);
402 
403 	status_t status = _ParseDynamicSection(image);
404 	if (status != B_OK)
405 		return status;
406 
407 	// deal with the rels first
408 	if (image->rel) {
409 		TRACE(("total %i relocs\n",
410 			(int)image->rel_len / (int)sizeof(RelType)));
411 
412 		status = boot_arch_elf_relocate_rel(image, image->rel, image->rel_len);
413 		if (status != B_OK)
414 			return status;
415 	}
416 
417 	if (image->pltrel) {
418 		RelType* pltrel = image->pltrel;
419 		if (image->pltrel_type == DT_REL) {
420 			TRACE(("total %i plt-relocs\n",
421 				(int)image->pltrel_len / (int)sizeof(RelType)));
422 
423 			status = boot_arch_elf_relocate_rel(image, pltrel,
424 				image->pltrel_len);
425 		} else {
426 			TRACE(("total %i plt-relocs\n",
427 				(int)image->pltrel_len / (int)sizeof(RelaType)));
428 
429 			status = boot_arch_elf_relocate_rela(image, (RelaType*)pltrel,
430 				image->pltrel_len);
431 		}
432 		if (status != B_OK)
433 			return status;
434 	}
435 
436 	if (image->rela) {
437 		TRACE(("total %i rela relocs\n",
438 			(int)image->rela_len / (int)sizeof(RelaType)));
439 		status = boot_arch_elf_relocate_rela(image, image->rela,
440 			image->rela_len);
441 		if (status != B_OK)
442 			return status;
443 	}
444 
445 	return B_OK;
446 }
447 
448 template<typename Class>
449 /*static*/ status_t
450 ELFLoader<Class>::Resolve(ImageType* image, SymType* symbol,
451 	AddrType* symbolAddress)
452 {
453 	switch (symbol->st_shndx) {
454 		case SHN_UNDEF:
455 			// Since we do that only for the kernel, there shouldn't be
456 			// undefined symbols.
457 			TRACE(("elf_resolve_symbol: undefined symbol\n"));
458 			return B_MISSING_SYMBOL;
459 		case SHN_ABS:
460 			*symbolAddress = symbol->st_value;
461 			return B_NO_ERROR;
462 		case SHN_COMMON:
463 			// ToDo: finish this
464 			TRACE(("elf_resolve_symbol: COMMON symbol, finish me!\n"));
465 			return B_ERROR;
466 		default:
467 			// standard symbol
468 			*symbolAddress = symbol->st_value + image->text_region.delta;
469 			return B_OK;
470 	}
471 }
472 
473 
474 template<typename Class>
475 /*static*/ status_t
476 ELFLoader<Class>::_LoadSymbolTable(int fd, ImageType* image)
477 {
478 	const EhdrType& elfHeader = image->elf_header;
479 	SymType* symbolTable = NULL;
480 	ShdrType* stringHeader = NULL;
481 	uint32 numSymbols = 0;
482 	char* stringTable;
483 	status_t status;
484 
485 	// get section headers
486 
487 	ssize_t size = elfHeader.e_shnum * elfHeader.e_shentsize;
488 	ShdrType* sectionHeaders = (ShdrType*)malloc(size);
489 	if (sectionHeaders == NULL) {
490 		dprintf("error allocating space for section headers\n");
491 		return B_NO_MEMORY;
492 	}
493 
494 	ssize_t length = read_pos(fd, elfHeader.e_shoff, sectionHeaders, size);
495 	if (length < size) {
496 		TRACE(("error reading in program headers\n"));
497 		status = B_ERROR;
498 		goto error1;
499 	}
500 
501 	// find symbol table in section headers
502 
503 	for (int32 i = 0; i < elfHeader.e_shnum; i++) {
504 		if (sectionHeaders[i].sh_type == SHT_SYMTAB) {
505 			stringHeader = &sectionHeaders[sectionHeaders[i].sh_link];
506 
507 			if (stringHeader->sh_type != SHT_STRTAB) {
508 				TRACE(("doesn't link to string table\n"));
509 				status = B_BAD_DATA;
510 				goto error1;
511 			}
512 
513 			// read in symbol table
514 			size = sectionHeaders[i].sh_size;
515 			symbolTable = (SymType*)kernel_args_malloc(size);
516 			if (symbolTable == NULL) {
517 				status = B_NO_MEMORY;
518 				goto error1;
519 			}
520 
521 			length = read_pos(fd, sectionHeaders[i].sh_offset, symbolTable,
522 				size);
523 			if (length < size) {
524 				TRACE(("error reading in symbol table\n"));
525 				status = B_ERROR;
526 				goto error1;
527 			}
528 
529 			numSymbols = size / sizeof(SymType);
530 			break;
531 		}
532 	}
533 
534 	if (symbolTable == NULL) {
535 		TRACE(("no symbol table\n"));
536 		status = B_BAD_VALUE;
537 		goto error1;
538 	}
539 
540 	// read in string table
541 
542 	size = stringHeader->sh_size;
543 	stringTable = (char*)kernel_args_malloc(size);
544 	if (stringTable == NULL) {
545 		status = B_NO_MEMORY;
546 		goto error2;
547 	}
548 
549 	length = read_pos(fd, stringHeader->sh_offset, stringTable, size);
550 	if (length < size) {
551 		TRACE(("error reading in string table\n"));
552 		status = B_ERROR;
553 		goto error3;
554 	}
555 
556 	TRACE(("loaded %" B_PRIu32 " debug symbols\n", numSymbols));
557 
558 	// insert tables into image
559 	image->debug_symbols = symbolTable;
560 	image->num_debug_symbols = numSymbols;
561 	image->debug_string_table = stringTable;
562 	image->debug_string_table_size = size;
563 
564 	free(sectionHeaders);
565 	return B_OK;
566 
567 error3:
568 	kernel_args_free(stringTable);
569 error2:
570 	kernel_args_free(symbolTable);
571 error1:
572 	free(sectionHeaders);
573 
574 	return status;
575 }
576 
577 
578 template<typename Class>
579 /*static*/ status_t
580 ELFLoader<Class>::_ParseDynamicSection(ImageType* image)
581 {
582 	image->syms = 0;
583 	image->rel = 0;
584 	image->rel_len = 0;
585 	image->rela = 0;
586 	image->rela_len = 0;
587 	image->pltrel = 0;
588 	image->pltrel_len = 0;
589 	image->pltrel_type = 0;
590 
591 	if(image->dynamic_section.start == 0)
592 		return B_ERROR;
593 
594 	DynType* d = (DynType*)Class::Map(image->dynamic_section.start);
595 
596 	for (int i = 0; d[i].d_tag != DT_NULL; i++) {
597 		switch (d[i].d_tag) {
598 			case DT_HASH:
599 			case DT_STRTAB:
600 				break;
601 			case DT_SYMTAB:
602 				image->syms = (SymType*)Class::Map(d[i].d_un.d_ptr
603 					+ image->text_region.delta);
604 				break;
605 			case DT_REL:
606 				image->rel = (RelType*)Class::Map(d[i].d_un.d_ptr
607 					+ image->text_region.delta);
608 				break;
609 			case DT_RELSZ:
610 				image->rel_len = d[i].d_un.d_val;
611 				break;
612 			case DT_RELA:
613 				image->rela = (RelaType*)Class::Map(d[i].d_un.d_ptr
614 					+ image->text_region.delta);
615 				break;
616 			case DT_RELASZ:
617 				image->rela_len = d[i].d_un.d_val;
618 				break;
619 			case DT_JMPREL:
620 				image->pltrel = (RelType*)Class::Map(d[i].d_un.d_ptr
621 					+ image->text_region.delta);
622 				break;
623 			case DT_PLTRELSZ:
624 				image->pltrel_len = d[i].d_un.d_val;
625 				break;
626 			case DT_PLTREL:
627 				image->pltrel_type = d[i].d_un.d_val;
628 				break;
629 
630 			default:
631 				continue;
632 		}
633 	}
634 
635 	// lets make sure we found all the required sections
636 	if (image->syms == NULL)
637 		return B_ERROR;
638 
639 	return B_OK;
640 }
641 
642 
643 // #pragma mark -
644 
645 
646 void
647 elf_init()
648 {
649 	void* settings = load_driver_settings("kernel");
650 	if (settings == NULL)
651 		return;
652 
653 	sLoadElfSymbols = get_driver_boolean_parameter(settings, "load_symbols",
654 		false, false);
655 
656 	unload_driver_settings(settings);
657 }
658 
659 
660 status_t
661 elf_load_image(int fd, preloaded_image** _image)
662 {
663 	status_t status = B_ERROR;
664 
665 	TRACE(("elf_load_image(fd = %d, _image = %p)\n", fd, _image));
666 
667 #if BOOT_SUPPORT_ELF64
668 	if (gKernelArgs.kernel_image == NULL
669 		|| gKernelArgs.kernel_image->elf_class == ELFCLASS64) {
670 		status = ELF64Loader::Create(fd, _image);
671 		if (status == B_OK)
672 			return ELF64Loader::Load(fd, *_image);
673 		else if (status != B_BAD_TYPE)
674 			return status;
675 	}
676 #endif
677 #if BOOT_SUPPORT_ELF32
678 	if (gKernelArgs.kernel_image == NULL
679 		|| gKernelArgs.kernel_image->elf_class == ELFCLASS32) {
680 		status = ELF32Loader::Create(fd, _image);
681 		if (status == B_OK)
682 			return ELF32Loader::Load(fd, *_image);
683 	}
684 #endif
685 
686 	return status;
687 }
688 
689 
690 status_t
691 elf_load_image(Directory* directory, const char* path)
692 {
693 	preloaded_image* image;
694 
695 	TRACE(("elf_load_image(directory = %p, \"%s\")\n", directory, path));
696 
697 	int fd = open_from(directory, path, O_RDONLY);
698 	if (fd < 0)
699 		return fd;
700 
701 	// check if this file has already been loaded
702 
703 	struct stat stat;
704 	if (fstat(fd, &stat) < 0)
705 		return errno;
706 
707 	image = gKernelArgs.preloaded_images;
708 	for (; image != NULL; image = image->next) {
709 		if (image->inode == stat.st_ino) {
710 			// file has already been loaded, no need to load it twice!
711 			close(fd);
712 			return B_OK;
713 		}
714 	}
715 
716 	// we still need to load it, so do it
717 
718 	status_t status = elf_load_image(fd, &image);
719 	if (status == B_OK) {
720 		image->name = kernel_args_strdup(path);
721 		image->inode = stat.st_ino;
722 
723 		// insert to kernel args
724 		image->next = gKernelArgs.preloaded_images;
725 		gKernelArgs.preloaded_images = image;
726 	} else
727 		kernel_args_free(image);
728 
729 	close(fd);
730 	return status;
731 }
732 
733 
734 status_t
735 elf_relocate_image(preloaded_image* image)
736 {
737 #ifdef BOOT_SUPPORT_ELF64
738 	if (image->elf_class == ELFCLASS64)
739 		return ELF64Loader::Relocate(image);
740 #endif
741 #ifdef BOOT_SUPPORT_ELF32
742 	if (image->elf_class == ELFCLASS32)
743 		return ELF32Loader::Relocate(image);
744 #endif
745 	return B_ERROR;
746 }
747 
748 
749 #ifdef BOOT_SUPPORT_ELF32
750 status_t
751 boot_elf_resolve_symbol(preloaded_elf32_image* image, Elf32_Sym* symbol,
752 	Elf32_Addr* symbolAddress)
753 {
754 	return ELF32Loader::Resolve(image, symbol, symbolAddress);
755 }
756 
757 Elf32_Addr
758 boot_elf32_get_relocation(Elf32_Addr resolveAddress)
759 {
760 	Elf32_Addr* src = (Elf32_Addr*)ELF32Class::Map(resolveAddress);
761 	return *src;
762 }
763 
764 void
765 boot_elf32_set_relocation(Elf32_Addr resolveAddress, Elf32_Addr finalAddress)
766 {
767 	Elf32_Addr* dest = (Elf32_Addr*)ELF32Class::Map(resolveAddress);
768 	*dest = finalAddress;
769 }
770 #endif
771 
772 
773 #ifdef BOOT_SUPPORT_ELF64
774 status_t
775 boot_elf_resolve_symbol(preloaded_elf64_image* image, Elf64_Sym* symbol,
776 	Elf64_Addr* symbolAddress)
777 {
778 	return ELF64Loader::Resolve(image, symbol, symbolAddress);
779 }
780 
781 void
782 boot_elf64_set_relocation(Elf64_Addr resolveAddress, Elf64_Addr finalAddress)
783 {
784 	Elf64_Addr* dest = (Elf64_Addr*)ELF64Class::Map(resolveAddress);
785 	*dest = finalAddress;
786 }
787 #endif
788