xref: /optee_os/ldelf/ta_elf.h (revision c88ba1258463bfa946499d2caa9341115b2fa849)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2019, Linaro Limited
4  */
5 
6 #ifndef TA_ELF_H
7 #define TA_ELF_H
8 
9 #include <ldelf.h>
10 #include <stdarg.h>
11 #include <sys/queue.h>
12 #include <tee_api_types.h>
13 #include <types_ext.h>
14 
15 struct segment {
16 	size_t offset;
17 	size_t vaddr;
18 	size_t filesz;
19 	size_t memsz;
20 	size_t flags;
21 	size_t align;
22 	bool remapped_writeable;
23 	TAILQ_ENTRY(segment) link;
24 };
25 
26 TAILQ_HEAD(segment_head, segment);
27 
28 struct ta_elf {
29 	bool is_main;
30 	bool is_32bit;	/* Initialized from Elf32_Ehdr/Elf64_Ehdr */
31 	bool is_legacy;
32 
33 	vaddr_t load_addr;
34 	vaddr_t max_addr;
35 	vaddr_t max_offs;
36 
37 	vaddr_t ehdr_addr;
38 
39 	/* Initialized from Elf32_Ehdr/Elf64_Ehdr */
40 	vaddr_t e_entry;
41 	vaddr_t e_phoff;
42 	vaddr_t e_shoff;
43 	unsigned int e_phnum;
44 	unsigned int e_shnum;
45 	unsigned int e_phentsize;
46 	unsigned int e_shentsize;
47 
48 	void *phdr;
49 	void *shdr;
50 	/*
51 	 * dynsymtab and dynstr are used for external symbols, they may hold
52 	 * other symbols too.
53 	 */
54 	void *dynsymtab;
55 	size_t num_dynsyms;
56 	const char *dynstr;
57 	size_t dynstr_size;
58 
59 	/* DT_HASH hash table for faster resolution of external symbols */
60 	void *hashtab;
61 
62 	struct segment_head segs;
63 
64 	vaddr_t exidx_start;
65 	size_t exidx_size;
66 
67 	/* Thread Local Storage */
68 	size_t tls_mod_id;
69 
70 	uint32_t handle;
71 
72 	struct ta_head *head;
73 
74 	TEE_UUID uuid;
75 	TAILQ_ENTRY(ta_elf) link;
76 };
77 
78 TAILQ_HEAD(ta_elf_queue, ta_elf);
79 
80 typedef void (*print_func_t)(void *pctx, const char *fmt, va_list ap)
81 	__printf(2, 0);
82 
83 extern struct ta_elf_queue main_elf_queue;
84 struct ta_elf *ta_elf_find_elf(const TEE_UUID *uuid);
85 
86 void ta_elf_load_main(const TEE_UUID *uuid, uint32_t *is_32bit, uint64_t *sp,
87 		      uint32_t *ta_flags);
88 void ta_elf_finalize_load_main(uint64_t *entry);
89 void ta_elf_load_dependency(struct ta_elf *elf, bool is_32bit);
90 void ta_elf_relocate(struct ta_elf *elf);
91 void ta_elf_finalize_mappings(struct ta_elf *elf);
92 
93 void ta_elf_print_mappings(void *pctx, print_func_t print_func,
94 			   struct ta_elf_queue *elf_queue, size_t num_maps,
95 			   struct dump_map *maps, vaddr_t mpool_base);
96 
97 #ifdef CFG_UNWIND
98 void ta_elf_stack_trace_a32(uint32_t regs[16]);
99 void ta_elf_stack_trace_a64(uint64_t fp, uint64_t sp, uint64_t pc);
100 #else
101 static inline void ta_elf_stack_trace_a32(uint32_t regs[16] __unused) { }
102 static inline void ta_elf_stack_trace_a64(uint64_t fp __unused,
103 					  uint64_t sp __unused,
104 					  uint64_t pc __unused) { }
105 #endif /*CFG_UNWIND*/
106 
107 TEE_Result ta_elf_resolve_sym(const char *name, vaddr_t *val,
108 			      struct ta_elf **found_elf, struct ta_elf *elf);
109 TEE_Result ta_elf_add_library(const TEE_UUID *uuid);
110 TEE_Result ta_elf_set_init_fini_info(bool is_32bit);
111 
112 #endif /*TA_ELF_H*/
113