1 /*
2  * Copyright (c) 2024 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_LLEXT_INTERNAL_H
8 #define ZEPHYR_LLEXT_INTERNAL_H
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #include <zephyr/llext/llext.h>
15 
16 /**
17  * @file
18  * @brief Private header for linkable loadable extensions
19  */
20 
21 /** @cond ignore */
22 
23 
24 struct llext_elf_sect_map {
25 	enum llext_mem mem_idx;
26 	size_t offset;
27 };
28 
29 const void *llext_loaded_sect_ptr(struct llext_loader *ldr, struct llext *ext, unsigned int sh_ndx);
30 
31 
llext_string(const struct llext_loader * ldr,const struct llext * ext,enum llext_mem mem_idx,unsigned int idx)32 static inline const char *llext_string(const struct llext_loader *ldr, const struct llext *ext,
33 	enum llext_mem mem_idx, unsigned int idx)
34 {
35 	return (const char *)ext->mem[mem_idx] + idx;
36 }
37 
llext_get_reloc_instruction_location(struct llext_loader * ldr,struct llext * ext,int shndx,const elf_rela_t * rela)38 static inline uintptr_t llext_get_reloc_instruction_location(struct llext_loader *ldr,
39 							     struct llext *ext,
40 							     int shndx,
41 							     const elf_rela_t *rela)
42 {
43 	return (uintptr_t) llext_loaded_sect_ptr(ldr, ext, shndx) + rela->r_offset;
44 }
45 
llext_section_name(const struct llext_loader * ldr,const struct llext * ext,const elf_shdr_t * shdr)46 static inline const char *llext_section_name(const struct llext_loader *ldr,
47 					     const struct llext *ext,
48 					     const elf_shdr_t *shdr)
49 {
50 	return llext_string(ldr, ext, LLEXT_MEM_SHSTRTAB, shdr->sh_name);
51 }
52 
llext_symbol_name(const struct llext_loader * ldr,const struct llext * ext,const elf_sym_t * sym)53 static inline const char *llext_symbol_name(const struct llext_loader *ldr,
54 					    const struct llext *ext,
55 					    const elf_sym_t *sym)
56 {
57 	if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) {
58 		return llext_section_name(ldr, ext, ext->sect_hdrs + sym->st_shndx);
59 	} else {
60 		return llext_string(ldr, ext, LLEXT_MEM_STRTAB, sym->st_name);
61 	}
62 }
63 
64 /*
65  * Determine address of a symbol.
66  */
67 int llext_lookup_symbol(struct llext_loader *ldr, struct llext *ext, uintptr_t *link_addr,
68 			const elf_rela_t *rel, const elf_sym_t *sym, const char *name,
69 			const elf_shdr_t *shdr);
70 
71 /*
72  * Read the symbol entry corresponding to a relocation from the binary.
73  */
74 int llext_read_symbol(struct llext_loader *ldr, struct llext *ext, const elf_rela_t *rel,
75 		      elf_sym_t *sym);
76 
77 /** @endcond */
78 
79 #ifdef __cplusplus
80 }
81 #endif
82 
83 #endif /* ZEPHYR_LLEXT_INTERNAL_H */
84