Commit | Line | Data |
---|---|---|
8e2aed3f | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-or-later |
8e2aed3f | 3 | * |
c0c0989a | 4 | * Copyright (C) 2015 Antoine Busque <abusque@efficios.com> |
8e2aed3f AB |
5 | */ |
6 | ||
c0c0989a MJ |
7 | #ifndef _LTTNG_UST_ELF_H |
8 | #define _LTTNG_UST_ELF_H | |
9 | ||
8e2aed3f AB |
10 | #include <stdlib.h> |
11 | #include <stdint.h> | |
12 | #include <stdio.h> | |
8e2aed3f AB |
13 | #include <elf.h> |
14 | #include <lttng/ust-endian.h> | |
15 | ||
16 | /* | |
17 | * Determine native endianness in order to convert when reading an ELF | |
18 | * file if there is a mismatch. | |
19 | */ | |
20 | #if BYTE_ORDER == LITTLE_ENDIAN | |
21 | #define NATIVE_ELF_ENDIANNESS ELFDATA2LSB | |
22 | #else | |
23 | #define NATIVE_ELF_ENDIANNESS ELFDATA2MSB | |
24 | #endif | |
25 | ||
26 | /* | |
27 | * The size in bytes of the debug link CRC as contained in an ELF | |
28 | * section. | |
29 | */ | |
30 | #define ELF_CRC_SIZE 4 | |
31 | /* | |
32 | * ELF notes are aligned on 4 bytes. ref: ELF specification version | |
33 | * 1.1 p. 2-5. | |
34 | */ | |
35 | #define ELF_NOTE_ENTRY_ALIGN 4 | |
36 | /* | |
37 | * Within an ELF note, the `desc` field is also aligned on 4 | |
38 | * bytes. ref: ELF specification version 1.1 p. 2-5. | |
39 | */ | |
40 | #define ELF_NOTE_DESC_ALIGN 4 | |
41 | ||
42 | #define bswap(x) \ | |
43 | do { \ | |
44 | switch (sizeof(x)) { \ | |
45 | case 8: \ | |
46 | x = bswap_64(x); \ | |
47 | break; \ | |
48 | case 4: \ | |
49 | x = bswap_32(x); \ | |
50 | break; \ | |
51 | case 2: \ | |
52 | x = bswap_16(x); \ | |
53 | break; \ | |
54 | case 1: \ | |
55 | break; \ | |
56 | default: \ | |
57 | abort(); \ | |
58 | } \ | |
59 | } while (0) | |
60 | ||
61 | #define bswap_phdr(phdr) \ | |
62 | do { \ | |
63 | bswap((phdr).p_type); \ | |
64 | bswap((phdr).p_offset); \ | |
65 | bswap((phdr).p_filesz); \ | |
66 | bswap((phdr).p_memsz); \ | |
67 | bswap((phdr).p_align); \ | |
e1f0c569 | 68 | bswap((phdr).p_vaddr); \ |
8e2aed3f AB |
69 | } while (0) |
70 | ||
71 | #define bswap_shdr(shdr) \ | |
72 | do { \ | |
73 | bswap((shdr).sh_name); \ | |
74 | bswap((shdr).sh_type); \ | |
75 | bswap((shdr).sh_flags); \ | |
76 | bswap((shdr).sh_addr); \ | |
77 | bswap((shdr).sh_offset); \ | |
78 | bswap((shdr).sh_size); \ | |
79 | bswap((shdr).sh_link); \ | |
80 | bswap((shdr).sh_info); \ | |
81 | bswap((shdr).sh_addralign); \ | |
82 | bswap((shdr).sh_entsize); \ | |
83 | } while (0) | |
84 | ||
85 | #define bswap_ehdr(ehdr) \ | |
86 | do { \ | |
87 | bswap((ehdr).e_type); \ | |
88 | bswap((ehdr).e_machine); \ | |
89 | bswap((ehdr).e_version); \ | |
90 | bswap((ehdr).e_entry); \ | |
91 | bswap((ehdr).e_phoff); \ | |
92 | bswap((ehdr).e_shoff); \ | |
93 | bswap((ehdr).e_flags); \ | |
94 | bswap((ehdr).e_ehsize); \ | |
95 | bswap((ehdr).e_phentsize); \ | |
96 | bswap((ehdr).e_phnum); \ | |
97 | bswap((ehdr).e_shentsize); \ | |
98 | bswap((ehdr).e_shnum); \ | |
99 | bswap((ehdr).e_shstrndx); \ | |
100 | } while (0) | |
101 | ||
102 | #define copy_phdr(src_phdr, dst_phdr) \ | |
103 | do { \ | |
104 | (dst_phdr).p_type = (src_phdr).p_type; \ | |
105 | (dst_phdr).p_offset = (src_phdr).p_offset; \ | |
106 | (dst_phdr).p_filesz = (src_phdr).p_filesz; \ | |
107 | (dst_phdr).p_memsz = (src_phdr).p_memsz; \ | |
108 | (dst_phdr).p_align = (src_phdr).p_align; \ | |
e1f0c569 | 109 | (dst_phdr).p_vaddr = (src_phdr).p_vaddr; \ |
8e2aed3f AB |
110 | } while (0) |
111 | ||
112 | #define copy_shdr(src_shdr, dst_shdr) \ | |
113 | do { \ | |
114 | (dst_shdr).sh_name = (src_shdr).sh_name; \ | |
115 | (dst_shdr).sh_type = (src_shdr).sh_type; \ | |
116 | (dst_shdr).sh_flags = (src_shdr).sh_flags; \ | |
117 | (dst_shdr).sh_addr = (src_shdr).sh_addr; \ | |
118 | (dst_shdr).sh_offset = (src_shdr).sh_offset; \ | |
119 | (dst_shdr).sh_size = (src_shdr).sh_size; \ | |
120 | (dst_shdr).sh_link = (src_shdr).sh_link; \ | |
121 | (dst_shdr).sh_info = (src_shdr).sh_info; \ | |
122 | (dst_shdr).sh_addralign = (src_shdr).sh_addralign; \ | |
123 | (dst_shdr).sh_entsize = (src_shdr).sh_entsize; \ | |
124 | } while (0) | |
125 | ||
126 | #define copy_ehdr(src_ehdr, dst_ehdr) \ | |
127 | do { \ | |
128 | (dst_ehdr).e_type = (src_ehdr).e_type; \ | |
129 | (dst_ehdr).e_machine = (src_ehdr).e_machine; \ | |
130 | (dst_ehdr).e_version = (src_ehdr).e_version; \ | |
131 | (dst_ehdr).e_entry = (src_ehdr).e_entry; \ | |
132 | (dst_ehdr).e_phoff = (src_ehdr).e_phoff; \ | |
133 | (dst_ehdr).e_shoff = (src_ehdr).e_shoff; \ | |
134 | (dst_ehdr).e_flags = (src_ehdr).e_flags; \ | |
135 | (dst_ehdr).e_ehsize = (src_ehdr).e_ehsize; \ | |
136 | (dst_ehdr).e_phentsize = (src_ehdr).e_phentsize; \ | |
137 | (dst_ehdr).e_phnum = (src_ehdr).e_phnum; \ | |
138 | (dst_ehdr).e_shentsize = (src_ehdr).e_shentsize; \ | |
139 | (dst_ehdr).e_shnum = (src_ehdr).e_shnum; \ | |
140 | (dst_ehdr).e_shstrndx = (src_ehdr).e_shstrndx; \ | |
141 | } while (0) | |
142 | ||
143 | struct lttng_ust_elf_ehdr { | |
144 | uint16_t e_type; | |
145 | uint16_t e_machine; | |
146 | uint32_t e_version; | |
147 | uint64_t e_entry; | |
148 | uint64_t e_phoff; | |
149 | uint64_t e_shoff; | |
150 | uint32_t e_flags; | |
151 | uint16_t e_ehsize; | |
152 | uint16_t e_phentsize; | |
153 | uint16_t e_phnum; | |
154 | uint16_t e_shentsize; | |
155 | uint16_t e_shnum; | |
156 | uint16_t e_shstrndx; | |
157 | }; | |
158 | ||
159 | struct lttng_ust_elf_phdr { | |
160 | uint32_t p_type; | |
161 | uint64_t p_offset; | |
162 | uint64_t p_filesz; | |
163 | uint64_t p_memsz; | |
164 | uint64_t p_align; | |
e1f0c569 | 165 | uint64_t p_vaddr; |
8e2aed3f AB |
166 | }; |
167 | ||
168 | struct lttng_ust_elf_shdr { | |
169 | uint32_t sh_name; | |
170 | uint32_t sh_type; | |
171 | uint64_t sh_flags; | |
172 | uint64_t sh_addr; | |
173 | uint64_t sh_offset; | |
174 | uint64_t sh_size; | |
175 | uint32_t sh_link; | |
176 | uint32_t sh_info; | |
177 | uint64_t sh_addralign; | |
178 | uint64_t sh_entsize; | |
179 | }; | |
180 | ||
181 | struct lttng_ust_elf_nhdr { | |
182 | uint32_t n_namesz; | |
183 | uint32_t n_descsz; | |
184 | uint32_t n_type; | |
185 | }; | |
186 | ||
187 | struct lttng_ust_elf { | |
188 | /* Offset in bytes to start of section names string table. */ | |
f5a6717a | 189 | off_t section_names_offset; |
8e2aed3f | 190 | /* Size in bytes of section names string table. */ |
f5a6717a | 191 | size_t section_names_size; |
8e2aed3f | 192 | char *path; |
405be658 | 193 | int fd; |
8e2aed3f AB |
194 | struct lttng_ust_elf_ehdr *ehdr; |
195 | uint8_t bitness; | |
196 | uint8_t endianness; | |
197 | }; | |
198 | ||
199 | static inline | |
200 | int is_elf_32_bit(struct lttng_ust_elf *elf) | |
201 | { | |
202 | return elf->bitness == ELFCLASS32; | |
203 | } | |
204 | ||
205 | static inline | |
206 | int is_elf_native_endian(struct lttng_ust_elf *elf) | |
207 | { | |
208 | return elf->endianness == NATIVE_ELF_ENDIANNESS; | |
209 | } | |
210 | ||
211 | struct lttng_ust_elf *lttng_ust_elf_create(const char *path); | |
212 | void lttng_ust_elf_destroy(struct lttng_ust_elf *elf); | |
f5eb039d | 213 | uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf *elf); |
8e2aed3f AB |
214 | int lttng_ust_elf_get_memsz(struct lttng_ust_elf *elf, uint64_t *memsz); |
215 | int lttng_ust_elf_get_build_id(struct lttng_ust_elf *elf, uint8_t **build_id, | |
216 | size_t *length, int *found); | |
217 | int lttng_ust_elf_get_debug_link(struct lttng_ust_elf *elf, char **filename, | |
218 | uint32_t *crc, int *found); | |
219 | ||
220 | #endif /* _LTTNG_UST_ELF_H */ |