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/llext/llext_internal.h>
10 #include <zephyr/llext/loader.h>
11 #include <zephyr/logging/log.h>
12 
13 LOG_MODULE_DECLARE(llext, CONFIG_LLEXT_LOG_LEVEL);
14 
15 /*
16  * ELF relocation tables on Xtensa contain relocations of different types. They
17  * specify how the relocation should be performed. Which relocations are used
18  * depends on the type of the ELF object (e.g. shared or partially linked
19  * object), structure of the object (single or multiple source files), compiler
20  * flags used (e.g. -fPIC), etc. Also not all relocation table entries should be
21  * acted upon. Some of them describe relocations that have already been
22  * resolved by the linker. We have to distinguish them from actionable
23  * relocations and only need to handle the latter ones.
24  */
25 #define R_XTENSA_NONE           0
26 #define R_XTENSA_32             1
27 #define R_XTENSA_RTLD           2
28 #define R_XTENSA_GLOB_DAT       3
29 #define R_XTENSA_JMP_SLOT       4
30 #define R_XTENSA_RELATIVE       5
31 #define R_XTENSA_PLT            6
32 #define R_XTENSA_ASM_EXPAND	11
33 #define R_XTENSA_SLOT0_OP	20
34 
xtensa_elf_relocate(struct llext_loader * ldr,struct llext * ext,const elf_rela_t * rel,uintptr_t addr,uint8_t * loc,int type,uint32_t stb)35 static void xtensa_elf_relocate(struct llext_loader *ldr, struct llext *ext,
36 				const elf_rela_t *rel, uintptr_t addr,
37 				uint8_t *loc, int type, uint32_t stb)
38 {
39 	elf_word *got_entry = (elf_word *)loc;
40 
41 	switch (type) {
42 	case R_XTENSA_RELATIVE:
43 		;
44 		/* Relocate a local symbol: Xtensa specific. Seems to only be used with PIC */
45 		unsigned int sh_ndx;
46 
47 		for (sh_ndx = 0; sh_ndx < ext->sect_cnt; sh_ndx++) {
48 			if (ext->sect_hdrs[sh_ndx].sh_addr <= *got_entry &&
49 			    *got_entry <
50 			    ext->sect_hdrs[sh_ndx].sh_addr + ext->sect_hdrs[sh_ndx].sh_size)
51 				break;
52 		}
53 
54 		if (sh_ndx == ext->sect_cnt) {
55 			LOG_ERR("%#x not found in any of the sections", *got_entry);
56 			return;
57 		}
58 
59 		*got_entry += (uintptr_t)llext_loaded_sect_ptr(ldr, ext, sh_ndx) -
60 			ext->sect_hdrs[sh_ndx].sh_addr;
61 		break;
62 	case R_XTENSA_GLOB_DAT:
63 	case R_XTENSA_JMP_SLOT:
64 		if (stb == STB_GLOBAL) {
65 			*got_entry = addr;
66 		}
67 		break;
68 	case R_XTENSA_32:
69 		/* Used for both LOCAL and GLOBAL bindings */
70 		*got_entry += addr;
71 		break;
72 	case R_XTENSA_SLOT0_OP:
73 		/* Apparently only actionable with LOCAL bindings */
74 		;
75 		elf_sym_t rsym;
76 		int ret = llext_seek(ldr, ldr->sects[LLEXT_MEM_SYMTAB].sh_offset +
77 				     ELF_R_SYM(rel->r_info) * sizeof(elf_sym_t));
78 
79 		if (!ret) {
80 			ret = llext_read(ldr, &rsym, sizeof(elf_sym_t));
81 		}
82 		if (ret) {
83 			LOG_ERR("Failed to read a symbol table entry, LLEXT linking might fail.");
84 			return;
85 		}
86 
87 		/*
88 		 * So far in all observed use-cases
89 		 * llext_loaded_sect_ptr(ldr, ext, rsym.st_shndx) was already
90 		 * available as the "addr" argument of this function, supplied
91 		 * by arch_elf_relocate_local() from its non-STT_SECTION branch.
92 		 */
93 		uintptr_t link_addr = (uintptr_t)llext_loaded_sect_ptr(ldr, ext, rsym.st_shndx) +
94 			rsym.st_value + rel->r_addend;
95 		ssize_t value = (link_addr - (((uintptr_t)got_entry + 3) & ~3)) >> 2;
96 
97 		/* Check the opcode */
98 		if ((loc[0] & 0xf) == 1 && !loc[1] && !loc[2]) {
99 			/* L32R: low nibble is 1 */
100 			loc[1] = value & 0xff;
101 			loc[2] = (value >> 8) & 0xff;
102 		} else if ((loc[0] & 0xf) == 5 && !(loc[0] & 0xc0) && !loc[1] && !loc[2]) {
103 			/* CALLn: low nibble is 5 */
104 			loc[0] = (loc[0] & 0x3f) | ((value << 6) & 0xc0);
105 			loc[1] = (value >> 2) & 0xff;
106 			loc[2] = (value >> 10) & 0xff;
107 		} else {
108 			LOG_DBG("%p: unhandled OPC or no relocation %02x%02x%02x inf %#x offs %#x",
109 				(void *)loc, loc[2], loc[1], loc[0],
110 				rel->r_info, rel->r_offset);
111 			break;
112 		}
113 
114 		break;
115 	case R_XTENSA_ASM_EXPAND:
116 		/* Nothing to do */
117 		break;
118 	default:
119 		LOG_DBG("Unsupported relocation type %u", type);
120 
121 		return;
122 	}
123 
124 	LOG_DBG("Applied relocation to %#x type %u at %p",
125 		*(uint32_t *)((uintptr_t)got_entry & ~3), type, (void *)got_entry);
126 }
127 
128 /**
129  * @brief Architecture specific function for STB_LOCAL ELF relocations
130  */
arch_elf_relocate_local(struct llext_loader * ldr,struct llext * ext,const elf_rela_t * rel,const elf_sym_t * sym,uint8_t * rel_addr,const struct llext_load_param * ldr_parm)131 void arch_elf_relocate_local(struct llext_loader *ldr, struct llext *ext, const elf_rela_t *rel,
132 			     const elf_sym_t *sym, uint8_t *rel_addr,
133 			     const struct llext_load_param *ldr_parm)
134 {
135 	int type = ELF32_R_TYPE(rel->r_info);
136 	uintptr_t sh_addr;
137 
138 	if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) {
139 		elf_shdr_t *shdr = ext->sect_hdrs + sym->st_shndx;
140 
141 		/* shdr->sh_addr is NULL when not built for a specific address */
142 		sh_addr = shdr->sh_addr &&
143 			(!ldr_parm->section_detached || !ldr_parm->section_detached(shdr)) ?
144 			shdr->sh_addr : (uintptr_t)llext_loaded_sect_ptr(ldr, ext, sym->st_shndx);
145 	} else {
146 		sh_addr = ldr->sects[LLEXT_MEM_TEXT].sh_addr;
147 	}
148 
149 	xtensa_elf_relocate(ldr, ext, rel, sh_addr, rel_addr, type, ELF_ST_BIND(sym->st_info));
150 }
151 
152 /**
153  * @brief Architecture specific function for STB_GLOBAL ELF relocations
154  */
arch_elf_relocate_global(struct llext_loader * ldr,struct llext * ext,const elf_rela_t * rel,const elf_sym_t * sym,uint8_t * rel_addr,const void * link_addr)155 void arch_elf_relocate_global(struct llext_loader *ldr, struct llext *ext, const elf_rela_t *rel,
156 			      const elf_sym_t *sym, uint8_t *rel_addr, const void *link_addr)
157 {
158 	int type = ELF32_R_TYPE(rel->r_info);
159 
160 	/* For global relocations we expect the initial value for R_XTENSA_RELATIVE to be zero */
161 	if (type == R_XTENSA_RELATIVE && *(elf_word *)rel_addr) {
162 		LOG_WRN("global: non-zero relative value %#x", *(elf_word *)rel_addr);
163 	}
164 
165 	xtensa_elf_relocate(ldr, ext, rel, (uintptr_t)link_addr, rel_addr, type,
166 			    ELF_ST_BIND(sym->st_info));
167 }
168