Line data Source code
1 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 :
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 :
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 :
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 :
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 : /**
80 : * @brief Architecture specific function for local binding relocations
81 : *
82 : * @param[in] loader Extension loader data and context
83 : * @param[in] ext Extension to call function in
84 : * @param[in] rel Relocation data provided by elf
85 : * @param[in] sym Corresponding symbol table entry
86 : * @param[in] rel_addr Address where relocation should be performed
87 : * @param[in] ldr_parm Loader parameters
88 : * @returns 0 on success or a negative error code
89 : */
90 1 : int arch_elf_relocate_local(struct llext_loader *loader, struct llext *ext, const elf_rela_t *rel,
91 : const elf_sym_t *sym, uint8_t *rel_addr,
92 : const struct llext_load_param *ldr_parm);
93 :
94 : /**
95 : * @brief Architecture specific function for global binding relocations
96 : *
97 : * @param[in] loader Extension loader data and context
98 : * @param[in] ext Extension to call function in
99 : * @param[in] rel Relocation data provided by elf
100 : * @param[in] sym Corresponding symbol table entry
101 : * @param[in] rel_addr Address where relocation should be performed
102 : * @param[in] link_addr target address for table-based relocations
103 : * @returns 0 on success or a negative error code
104 : */
105 1 : int arch_elf_relocate_global(struct llext_loader *loader, struct llext *ext, const elf_rela_t *rel,
106 : const elf_sym_t *sym, uint8_t *rel_addr, const void *link_addr);
107 :
108 : #ifdef __cplusplus
109 : }
110 : #endif
111 :
112 : #endif /* ZEPHYR_LLEXT_INTERNAL_H */
|