1 /*
2  * Copyright (c) 2023 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/llext/elf.h>
8 #include <zephyr/llext/llext.h>
9 #include <zephyr/logging/log.h>
10 
11 LOG_MODULE_REGISTER(elf, CONFIG_LLEXT_LOG_LEVEL);
12 
13 /**
14  * @brief Architecture specific function for relocating partially linked (static) elf
15  *
16  * Elf files contain a series of relocations described in a section. These relocation
17  * instructions are architecture specific and each architecture supporting extensions
18  * must implement this.
19  *
20  * The relocation codes for arm are well documented
21  * https://github.com/ARM-software/abi-aa/blob/main/aaelf32/aaelf32.rst#relocation
22  */
arch_elf_relocate(elf_rel_t * rel,uintptr_t opaddr,uintptr_t opval)23 void arch_elf_relocate(elf_rel_t *rel, uintptr_t opaddr, uintptr_t opval)
24 {
25 	elf_word reloc_type = ELF32_R_TYPE(rel->r_info);
26 
27 	switch (reloc_type) {
28 	case R_ARM_ABS32:
29 		/* Update the absolute address of a load/store instruction */
30 		*((uint32_t *)opaddr) = (uint32_t)opval;
31 		break;
32 	default:
33 		LOG_DBG("Unsupported ARM elf relocation type %d at address %lx",
34 			reloc_type, opaddr);
35 		break;
36 	}
37 }
38