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