Commit | Line | Data |
---|---|---|
d0927b41 FD |
1 | /* |
2 | * Copyright (C) 2015 Antoine Busque <abusque@efficios.com> | |
3 | * Copyright (C) 2017 Francis Deslauriers <francis.deslauriers@efficios.com> | |
4 | * Copyright (C) 2017 Erica Bugden <erica.bugden@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 <common/compat/endian.h> | |
22 | #include <common/error.h> | |
23 | #include <common/lttng-elf.h> | |
24 | #include <common/macros.h> | |
25 | #include <common/readwrite.h> | |
26 | #include <fcntl.h> | |
27 | #include <stdbool.h> | |
28 | #include <stdint.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <sys/stat.h> | |
32 | #include <sys/types.h> | |
33 | #include <unistd.h> | |
34 | ||
35 | #include <elf.h> | |
36 | ||
37 | #define BUF_LEN 4096 | |
38 | #define TEXT_SECTION_NAME ".text" | |
39 | #define SYMBOL_TAB_SECTION_NAME ".symtab" | |
40 | #define STRING_TAB_SECTION_NAME ".strtab" | |
ef3dfe5d FD |
41 | #define DYNAMIC_SYMBOL_TAB_SECTION_NAME ".dynsym" |
42 | #define DYNAMIC_STRING_TAB_SECTION_NAME ".dynstr" | |
d0927b41 FD |
43 | #define NOTE_STAPSDT_SECTION_NAME ".note.stapsdt" |
44 | #define NOTE_STAPSDT_NAME "stapsdt" | |
45 | #define NOTE_STAPSDT_TYPE 3 | |
46 | ||
47 | #if BYTE_ORDER == LITTLE_ENDIAN | |
48 | #define NATIVE_ELF_ENDIANNESS ELFDATA2LSB | |
49 | #else | |
50 | #define NATIVE_ELF_ENDIANNESS ELFDATA2MSB | |
51 | #endif | |
52 | ||
8bd52288 FD |
53 | #define next_4bytes_boundary(x) (typeof(x)) ((((uint64_t)x) + 3) & ~0x03) |
54 | ||
d0927b41 FD |
55 | #define bswap(x) \ |
56 | do { \ | |
57 | switch (sizeof(x)) { \ | |
58 | case 8: \ | |
b1b34226 | 59 | x = be64toh((uint64_t)x); \ |
d0927b41 FD |
60 | break; \ |
61 | case 4: \ | |
b1b34226 | 62 | x = be32toh((uint32_t)x); \ |
d0927b41 FD |
63 | break; \ |
64 | case 2: \ | |
b1b34226 | 65 | x = be16toh((uint16_t)x); \ |
d0927b41 FD |
66 | break; \ |
67 | case 1: \ | |
68 | break; \ | |
69 | default: \ | |
70 | abort(); \ | |
71 | } \ | |
72 | } while (0) | |
73 | ||
74 | #define bswap_shdr(shdr) \ | |
75 | do { \ | |
76 | bswap((shdr).sh_name); \ | |
77 | bswap((shdr).sh_type); \ | |
78 | bswap((shdr).sh_flags); \ | |
79 | bswap((shdr).sh_addr); \ | |
80 | bswap((shdr).sh_offset); \ | |
81 | bswap((shdr).sh_size); \ | |
82 | bswap((shdr).sh_link); \ | |
83 | bswap((shdr).sh_info); \ | |
84 | bswap((shdr).sh_addralign); \ | |
85 | bswap((shdr).sh_entsize); \ | |
86 | } while (0) | |
87 | ||
88 | #define bswap_ehdr(ehdr) \ | |
89 | do { \ | |
90 | bswap((ehdr).e_type); \ | |
91 | bswap((ehdr).e_machine); \ | |
92 | bswap((ehdr).e_version); \ | |
93 | bswap((ehdr).e_entry); \ | |
94 | bswap((ehdr).e_phoff); \ | |
95 | bswap((ehdr).e_shoff); \ | |
96 | bswap((ehdr).e_flags); \ | |
97 | bswap((ehdr).e_ehsize); \ | |
98 | bswap((ehdr).e_phentsize); \ | |
99 | bswap((ehdr).e_phnum); \ | |
100 | bswap((ehdr).e_shentsize); \ | |
101 | bswap((ehdr).e_shnum); \ | |
102 | bswap((ehdr).e_shstrndx); \ | |
103 | } while (0) | |
104 | ||
105 | #define copy_shdr(src_shdr, dst_shdr) \ | |
106 | do { \ | |
107 | (dst_shdr).sh_name = (src_shdr).sh_name; \ | |
108 | (dst_shdr).sh_type = (src_shdr).sh_type; \ | |
109 | (dst_shdr).sh_flags = (src_shdr).sh_flags; \ | |
110 | (dst_shdr).sh_addr = (src_shdr).sh_addr; \ | |
111 | (dst_shdr).sh_offset = (src_shdr).sh_offset; \ | |
112 | (dst_shdr).sh_size = (src_shdr).sh_size; \ | |
113 | (dst_shdr).sh_link = (src_shdr).sh_link; \ | |
114 | (dst_shdr).sh_info = (src_shdr).sh_info; \ | |
115 | (dst_shdr).sh_addralign = (src_shdr).sh_addralign; \ | |
116 | (dst_shdr).sh_entsize = (src_shdr).sh_entsize; \ | |
117 | } while (0) | |
118 | ||
119 | #define copy_ehdr(src_ehdr, dst_ehdr) \ | |
120 | do { \ | |
121 | (dst_ehdr).e_type = (src_ehdr).e_type; \ | |
122 | (dst_ehdr).e_machine = (src_ehdr).e_machine; \ | |
123 | (dst_ehdr).e_version = (src_ehdr).e_version; \ | |
124 | (dst_ehdr).e_entry = (src_ehdr).e_entry; \ | |
125 | (dst_ehdr).e_phoff = (src_ehdr).e_phoff; \ | |
126 | (dst_ehdr).e_shoff = (src_ehdr).e_shoff; \ | |
127 | (dst_ehdr).e_flags = (src_ehdr).e_flags; \ | |
128 | (dst_ehdr).e_ehsize = (src_ehdr).e_ehsize; \ | |
129 | (dst_ehdr).e_phentsize = (src_ehdr).e_phentsize; \ | |
130 | (dst_ehdr).e_phnum = (src_ehdr).e_phnum; \ | |
131 | (dst_ehdr).e_shentsize = (src_ehdr).e_shentsize; \ | |
132 | (dst_ehdr).e_shnum = (src_ehdr).e_shnum; \ | |
133 | (dst_ehdr).e_shstrndx = (src_ehdr).e_shstrndx; \ | |
134 | } while (0) | |
135 | ||
136 | #define copy_sym(src_sym, dst_sym) \ | |
137 | do { \ | |
138 | dst_sym.st_name = src_sym.st_name; \ | |
139 | dst_sym.st_info = src_sym.st_info; \ | |
140 | dst_sym.st_other = src_sym.st_other; \ | |
141 | dst_sym.st_shndx = src_sym.st_shndx; \ | |
142 | dst_sym.st_value = src_sym.st_value; \ | |
143 | dst_sym.st_size = src_sym.st_size; \ | |
144 | } while (0) | |
145 | ||
146 | /* Both 32bit and 64bit use the same 1 byte field for type. (See elf.h) */ | |
147 | #define ELF_ST_TYPE(val) ELF32_ST_TYPE(val) | |
148 | ||
149 | struct lttng_elf_ehdr { | |
150 | uint16_t e_type; | |
151 | uint16_t e_machine; | |
152 | uint32_t e_version; | |
153 | uint64_t e_entry; | |
154 | uint64_t e_phoff; | |
155 | uint64_t e_shoff; | |
156 | uint32_t e_flags; | |
157 | uint16_t e_ehsize; | |
158 | uint16_t e_phentsize; | |
159 | uint16_t e_phnum; | |
160 | uint16_t e_shentsize; | |
161 | uint16_t e_shnum; | |
162 | uint16_t e_shstrndx; | |
163 | }; | |
164 | ||
165 | struct lttng_elf_shdr { | |
166 | uint32_t sh_name; | |
167 | uint32_t sh_type; | |
168 | uint64_t sh_flags; | |
169 | uint64_t sh_addr; | |
170 | uint64_t sh_offset; | |
171 | uint64_t sh_size; | |
172 | uint32_t sh_link; | |
173 | uint32_t sh_info; | |
174 | uint64_t sh_addralign; | |
175 | uint64_t sh_entsize; | |
176 | }; | |
177 | ||
178 | /* | |
179 | * This struct can hold both 32bit and 64bit symbol description. It's used with | |
180 | * the copy_sym() macro. Using this abstraction, we can use the same code for | |
181 | * both bitness. | |
182 | */ | |
183 | struct lttng_elf_sym { | |
184 | uint32_t st_name; | |
185 | uint8_t st_info; | |
186 | uint8_t st_other; | |
187 | uint16_t st_shndx; | |
188 | uint64_t st_value; | |
189 | uint64_t st_size; | |
190 | }; | |
191 | ||
192 | struct lttng_elf { | |
193 | int fd; | |
194 | uint8_t bitness; | |
195 | uint8_t endianness; | |
196 | /* Offset in bytes to start of section names string table. */ | |
197 | off_t section_names_offset; | |
198 | /* Size in bytes of section names string table. */ | |
199 | size_t section_names_size; | |
200 | struct lttng_elf_ehdr *ehdr; | |
201 | }; | |
202 | ||
203 | static inline | |
204 | int is_elf_32_bit(struct lttng_elf *elf) | |
205 | { | |
206 | return elf->bitness == ELFCLASS32; | |
207 | } | |
208 | ||
209 | static inline | |
210 | int is_elf_native_endian(struct lttng_elf *elf) | |
211 | { | |
212 | return elf->endianness == NATIVE_ELF_ENDIANNESS; | |
213 | } | |
214 | ||
215 | static | |
216 | int populate_section_header(struct lttng_elf * elf, struct lttng_elf_shdr *shdr, | |
217 | uint32_t index) | |
218 | { | |
219 | int ret = 0; | |
220 | off_t offset; | |
221 | ||
222 | /* Compute the offset of the section in the file */ | |
223 | offset = (off_t) elf->ehdr->e_shoff | |
224 | + (off_t) index * elf->ehdr->e_shentsize; | |
225 | ||
226 | if (lseek(elf->fd, offset, SEEK_SET) < 0) { | |
227 | PERROR("Error seeking to the beginning of ELF section header"); | |
228 | ret = -1; | |
229 | goto error; | |
230 | } | |
231 | ||
232 | if (is_elf_32_bit(elf)) { | |
233 | Elf32_Shdr elf_shdr; | |
234 | ||
235 | if (lttng_read(elf->fd, &elf_shdr, sizeof(elf_shdr)) < sizeof(elf_shdr)) { | |
236 | PERROR("Error reading ELF section header"); | |
237 | ret = -1; | |
238 | goto error; | |
239 | } | |
240 | if (!is_elf_native_endian(elf)) { | |
241 | bswap_shdr(elf_shdr); | |
242 | } | |
243 | copy_shdr(elf_shdr, *shdr); | |
244 | } else { | |
245 | Elf64_Shdr elf_shdr; | |
246 | ||
247 | if (lttng_read(elf->fd, &elf_shdr, sizeof(elf_shdr)) < sizeof(elf_shdr)) { | |
248 | PERROR("Error reading ELF section header"); | |
249 | ret = -1; | |
250 | goto error; | |
251 | } | |
252 | if (!is_elf_native_endian(elf)) { | |
253 | bswap_shdr(elf_shdr); | |
254 | } | |
255 | copy_shdr(elf_shdr, *shdr); | |
256 | } | |
257 | ||
258 | error: | |
259 | return ret; | |
260 | } | |
261 | ||
262 | static | |
263 | int populate_elf_header(struct lttng_elf *elf) | |
264 | { | |
265 | int ret = 0; | |
266 | ||
267 | /* | |
268 | * Move the read pointer back to the beginning to read the full header | |
269 | * and copy it in our structure. | |
270 | */ | |
271 | if (lseek(elf->fd, 0, SEEK_SET) < 0) { | |
272 | PERROR("Error seeking to the beginning of the file"); | |
273 | ret = -1; | |
274 | goto error; | |
275 | } | |
276 | ||
277 | /* | |
278 | * Use macros to set fields in the ELF header struct for both 32bit and | |
279 | * 64bit. | |
280 | */ | |
281 | if (is_elf_32_bit(elf)) { | |
282 | Elf32_Ehdr elf_ehdr; | |
283 | ||
284 | if (lttng_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr)) < sizeof(elf_ehdr)) { | |
285 | ret = -1; | |
286 | goto error; | |
287 | } | |
288 | if (!is_elf_native_endian(elf)) { | |
289 | bswap_ehdr(elf_ehdr); | |
290 | } | |
291 | copy_ehdr(elf_ehdr, *(elf->ehdr)); | |
292 | } else { | |
293 | Elf64_Ehdr elf_ehdr; | |
294 | ||
295 | if (lttng_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr)) < sizeof(elf_ehdr)) { | |
296 | ret = -1; | |
297 | goto error; | |
298 | } | |
299 | if (!is_elf_native_endian(elf)) { | |
300 | bswap_ehdr(elf_ehdr); | |
301 | } | |
302 | copy_ehdr(elf_ehdr, *(elf->ehdr)); | |
303 | } | |
304 | error: | |
305 | return ret; | |
306 | } | |
307 | ||
308 | /* | |
309 | * Retrieve the nth (where n is the `index` argument) shdr (section | |
310 | * header) from the given elf instance. | |
311 | * | |
312 | * A pointer to the shdr is returned on success, NULL on failure. | |
313 | */ | |
314 | static | |
315 | struct lttng_elf_shdr *lttng_elf_get_section_hdr(struct lttng_elf *elf, | |
316 | uint16_t index) | |
317 | { | |
318 | struct lttng_elf_shdr *section_header = NULL; | |
319 | int ret = 0; | |
320 | ||
321 | if (!elf) { | |
322 | goto error; | |
323 | } | |
324 | ||
325 | if (index >= elf->ehdr->e_shnum) { | |
326 | goto error; | |
327 | } | |
328 | ||
329 | section_header = zmalloc(sizeof(struct lttng_elf_shdr)); | |
330 | if (!section_header) { | |
331 | goto error; | |
332 | } | |
333 | ||
334 | ret = populate_section_header(elf, section_header, index); | |
335 | if (ret) { | |
d0927b41 FD |
336 | DBG("Error populating section header."); |
337 | goto error; | |
338 | } | |
339 | return section_header; | |
340 | ||
341 | error: | |
342 | free(section_header); | |
343 | return NULL; | |
344 | } | |
345 | ||
346 | /* | |
347 | * Lookup a section's name from a given offset (usually from an shdr's | |
348 | * sh_name value) in bytes relative to the beginning of the section | |
349 | * names string table. | |
350 | * | |
351 | * If no name is found, NULL is returned. | |
352 | */ | |
353 | static | |
354 | char *lttng_elf_get_section_name(struct lttng_elf *elf, off_t offset) | |
355 | { | |
356 | char *name = NULL; | |
357 | size_t name_length = 0, to_read; /* name_length does not include \0 */ | |
358 | ||
359 | if (!elf) { | |
360 | goto error; | |
361 | } | |
362 | ||
363 | if (offset >= elf->section_names_size) { | |
364 | goto error; | |
365 | } | |
366 | ||
367 | if (lseek(elf->fd, elf->section_names_offset + offset, SEEK_SET) < 0) { | |
368 | PERROR("Error seeking to the beginning of ELF string table section"); | |
369 | goto error; | |
370 | } | |
371 | ||
372 | to_read = elf->section_names_size - offset; | |
373 | ||
374 | /* Find first \0 after or at current location, remember name_length. */ | |
375 | for (;;) { | |
376 | char buf[BUF_LEN]; | |
377 | ssize_t read_len; | |
378 | size_t i; | |
379 | ||
380 | if (!to_read) { | |
381 | goto error; | |
382 | } | |
383 | read_len = lttng_read(elf->fd, buf, min_t(size_t, BUF_LEN, to_read)); | |
384 | if (read_len <= 0) { | |
385 | PERROR("Error reading ELF string table section"); | |
386 | goto error; | |
387 | } | |
388 | for (i = 0; i < read_len; i++) { | |
389 | if (buf[i] == '\0') { | |
390 | name_length += i; | |
391 | goto end; | |
392 | } | |
393 | } | |
394 | name_length += read_len; | |
395 | to_read -= read_len; | |
396 | } | |
397 | end: | |
398 | /* | |
399 | * We found the length of the section name, now seek back to the | |
400 | * beginning of the name and copy it in the newly allocated buffer. | |
401 | */ | |
402 | name = zmalloc(sizeof(char) * (name_length + 1)); /* + 1 for \0 */ | |
403 | if (!name) { | |
404 | PERROR("Error allocating ELF section name buffer"); | |
405 | goto error; | |
406 | } | |
407 | if (lseek(elf->fd, elf->section_names_offset + offset, SEEK_SET) < 0) { | |
408 | PERROR("Error seeking to the offset of the ELF section name"); | |
409 | goto error; | |
410 | } | |
411 | if (lttng_read(elf->fd, name, name_length + 1) < name_length + 1) { | |
412 | PERROR("Error reading the ELF section name"); | |
413 | goto error; | |
414 | } | |
415 | ||
416 | return name; | |
417 | ||
418 | error: | |
419 | free(name); | |
420 | return NULL; | |
421 | } | |
422 | ||
423 | static | |
424 | int lttng_elf_validate_and_populate(struct lttng_elf *elf) | |
425 | { | |
426 | uint8_t version; | |
427 | uint8_t e_ident[EI_NIDENT]; | |
428 | uint8_t *magic_number = NULL; | |
429 | int ret = 0; | |
430 | ||
431 | if (elf->fd == -1) { | |
432 | DBG("fd error"); | |
433 | ret = LTTNG_ERR_ELF_PARSING; | |
434 | goto end; | |
435 | } | |
436 | ||
437 | /* | |
438 | * First read the magic number, endianness and version to later populate | |
439 | * the ELF header with the correct endianness and bitness. | |
440 | * (see elf.h) | |
441 | */ | |
442 | ||
443 | if (lseek(elf->fd, 0, SEEK_SET) < 0) { | |
444 | PERROR("Error seeking the beginning of ELF file"); | |
445 | ret = LTTNG_ERR_ELF_PARSING; | |
446 | goto end; | |
447 | } | |
448 | ret = lttng_read(elf->fd, e_ident, EI_NIDENT); | |
449 | if (ret < EI_NIDENT) { | |
450 | DBG("Error reading the ELF identification fields"); | |
451 | if (ret == -1) { | |
452 | PERROR("Error reading the ELF identification fields"); | |
453 | } | |
454 | ret = LTTNG_ERR_ELF_PARSING; | |
455 | goto end; | |
456 | } | |
457 | ||
458 | /* | |
459 | * Copy fields used to check that the target file is in fact a valid ELF | |
460 | * file. | |
461 | */ | |
462 | elf->bitness = e_ident[EI_CLASS]; | |
463 | elf->endianness = e_ident[EI_DATA]; | |
464 | version = e_ident[EI_VERSION]; | |
465 | magic_number = &e_ident[EI_MAG0]; | |
466 | ||
467 | /* | |
468 | * Check the magic number. | |
469 | */ | |
470 | if (memcmp(magic_number, ELFMAG, SELFMAG) != 0) { | |
471 | DBG("Error check ELF magic number."); | |
472 | ret = LTTNG_ERR_ELF_PARSING; | |
473 | goto end; | |
474 | } | |
475 | ||
476 | /* | |
477 | * Check the bitness is either ELFCLASS32 or ELFCLASS64. | |
478 | */ | |
479 | if (elf->bitness <= ELFCLASSNONE || elf->bitness >= ELFCLASSNUM) { | |
480 | DBG("ELF class error."); | |
481 | ret = LTTNG_ERR_ELF_PARSING; | |
482 | goto end; | |
483 | } | |
484 | ||
485 | /* | |
486 | * Check the endianness is either ELFDATA2LSB or ELFDATA2MSB. | |
487 | */ | |
488 | if (elf->endianness <= ELFDATANONE || elf->endianness >= ELFDATANUM) { | |
489 | DBG("ELF endianness error."); | |
490 | ret = LTTNG_ERR_ELF_PARSING; | |
491 | goto end; | |
492 | } | |
493 | ||
494 | /* | |
495 | * Check the version is ELF_CURRENT. | |
496 | */ | |
497 | if (version <= EV_NONE || version >= EV_NUM) { | |
498 | DBG("Wrong ELF version."); | |
499 | ret = LTTNG_ERR_ELF_PARSING; | |
500 | goto end; | |
501 | } | |
502 | ||
503 | elf->ehdr = zmalloc(sizeof(struct lttng_elf_ehdr)); | |
504 | if (!elf->ehdr) { | |
505 | PERROR("Error allocation buffer for ELF header"); | |
506 | ret = LTTNG_ERR_NOMEM; | |
507 | goto end; | |
508 | } | |
509 | ||
510 | /* | |
511 | * Copy the content of the elf header. | |
512 | */ | |
513 | ret = populate_elf_header(elf); | |
514 | if (ret) { | |
515 | DBG("Error reading ELF header,"); | |
516 | goto free_elf_error; | |
517 | } | |
518 | ||
519 | goto end; | |
520 | ||
521 | free_elf_error: | |
522 | free(elf->ehdr); | |
523 | elf->ehdr = NULL; | |
524 | end: | |
525 | return ret; | |
526 | } | |
527 | ||
528 | /* | |
529 | * Create an instance of lttng_elf for the ELF file located at | |
530 | * `path`. | |
531 | * | |
532 | * Return a pointer to the instance on success, NULL on failure. | |
533 | */ | |
534 | static | |
535 | struct lttng_elf *lttng_elf_create(int fd) | |
536 | { | |
537 | struct lttng_elf_shdr *section_names_shdr; | |
538 | struct lttng_elf *elf = NULL; | |
539 | int ret; | |
540 | ||
541 | if (fd < 0) { | |
542 | goto error; | |
543 | } | |
544 | ||
545 | elf = zmalloc(sizeof(struct lttng_elf)); | |
546 | if (!elf) { | |
547 | PERROR("Error allocating struct lttng_elf"); | |
548 | goto error; | |
549 | } | |
550 | ||
551 | elf->fd = dup(fd); | |
552 | if (elf->fd < 0) { | |
553 | PERROR("Error duplicating file descriptor to binary"); | |
554 | goto error; | |
555 | } | |
556 | ||
557 | ret = lttng_elf_validate_and_populate(elf); | |
558 | if (ret) { | |
559 | goto error; | |
560 | } | |
561 | ||
562 | section_names_shdr = lttng_elf_get_section_hdr(elf, elf->ehdr->e_shstrndx); | |
563 | if (!section_names_shdr) { | |
564 | goto error; | |
565 | } | |
566 | ||
567 | elf->section_names_offset = section_names_shdr->sh_offset; | |
568 | elf->section_names_size = section_names_shdr->sh_size; | |
569 | ||
570 | free(section_names_shdr); | |
571 | return elf; | |
572 | ||
573 | error: | |
574 | if (elf) { | |
575 | if (elf->ehdr) { | |
576 | free(elf->ehdr); | |
577 | } | |
578 | if (elf->fd >= 0) { | |
579 | if (close(elf->fd)) { | |
580 | PERROR("Error closing file descriptor in error path"); | |
581 | abort(); | |
582 | } | |
583 | } | |
584 | free(elf); | |
585 | } | |
586 | return NULL; | |
587 | } | |
588 | ||
589 | /* | |
590 | * Destroy the given lttng_elf instance. | |
591 | */ | |
592 | static | |
593 | void lttng_elf_destroy(struct lttng_elf *elf) | |
594 | { | |
595 | if (!elf) { | |
596 | return; | |
597 | } | |
598 | ||
599 | free(elf->ehdr); | |
600 | if (close(elf->fd)) { | |
601 | PERROR("Error closing file description in error path"); | |
602 | abort(); | |
603 | } | |
604 | free(elf); | |
605 | } | |
606 | ||
607 | static | |
608 | int lttng_elf_get_section_hdr_by_name(struct lttng_elf *elf, | |
609 | const char *section_name, struct lttng_elf_shdr **section_hdr) | |
610 | { | |
611 | int i; | |
612 | char *curr_section_name; | |
613 | for (i = 0; i < elf->ehdr->e_shnum; ++i) { | |
614 | *section_hdr = lttng_elf_get_section_hdr(elf, i); | |
615 | curr_section_name = lttng_elf_get_section_name(elf, | |
616 | (*section_hdr)->sh_name); | |
617 | ||
618 | if (!curr_section_name) { | |
619 | continue; | |
620 | } | |
621 | if (strcmp(curr_section_name, section_name) == 0) { | |
622 | return 0; | |
623 | } | |
624 | } | |
625 | return LTTNG_ERR_ELF_PARSING; | |
626 | } | |
627 | ||
628 | static | |
629 | char *lttng_elf_get_section_data(struct lttng_elf *elf, | |
630 | struct lttng_elf_shdr *shdr) | |
631 | { | |
632 | int ret; | |
633 | off_t section_offset; | |
634 | char *data; | |
635 | ||
636 | if (!elf || !shdr) { | |
637 | goto error; | |
638 | } | |
639 | ||
640 | section_offset = shdr->sh_offset; | |
641 | if (lseek(elf->fd, section_offset, SEEK_SET) < 0) { | |
642 | PERROR("Error seeking to section offset"); | |
643 | goto error; | |
644 | } | |
645 | ||
646 | data = zmalloc(shdr->sh_size); | |
647 | if (!data) { | |
648 | PERROR("Error allocating buffer for ELF section data"); | |
649 | goto error; | |
650 | } | |
651 | ret = lttng_read(elf->fd, data, shdr->sh_size); | |
652 | if (ret == -1) { | |
653 | PERROR("Error reading ELF section data"); | |
654 | goto free_error; | |
655 | } | |
656 | ||
657 | return data; | |
658 | ||
659 | free_error: | |
660 | free(data); | |
661 | error: | |
662 | return NULL; | |
663 | } | |
664 | ||
665 | /* | |
666 | * Convert the virtual address in a binary's mapping to the offset of | |
667 | * the corresponding instruction in the binary file. | |
668 | * This function assumes the address is in the text section. | |
669 | * | |
670 | * Returns the offset on success or non-zero in case of failure. | |
671 | */ | |
672 | static | |
673 | int lttng_elf_convert_addr_in_text_to_offset(struct lttng_elf *elf_handle, | |
674 | size_t addr, uint64_t *offset) | |
675 | { | |
676 | int ret = 0; | |
677 | off_t text_section_offset; | |
678 | off_t text_section_addr_beg; | |
679 | off_t text_section_addr_end; | |
680 | off_t offset_in_section; | |
681 | struct lttng_elf_shdr *text_section_hdr = NULL; | |
682 | ||
683 | if (!elf_handle) { | |
684 | DBG("Invalid ELF handle."); | |
685 | ret = LTTNG_ERR_ELF_PARSING; | |
686 | goto error; | |
687 | } | |
688 | ||
689 | /* Get a pointer to the .text section header. */ | |
690 | ret = lttng_elf_get_section_hdr_by_name(elf_handle, | |
691 | TEXT_SECTION_NAME, &text_section_hdr); | |
692 | if (ret) { | |
693 | DBG("Text section not found in binary."); | |
694 | ret = LTTNG_ERR_ELF_PARSING; | |
695 | goto error; | |
696 | } | |
697 | ||
698 | text_section_offset = text_section_hdr->sh_offset; | |
699 | text_section_addr_beg = text_section_hdr->sh_addr; | |
700 | text_section_addr_end = text_section_addr_beg + text_section_hdr->sh_size; | |
701 | ||
702 | /* | |
703 | * Verify that the address is within the .text section boundaries. | |
704 | */ | |
705 | if (addr < text_section_addr_beg || addr > text_section_addr_end) { | |
706 | DBG("Address found is outside of the .text section addr=0x%zx, " | |
707 | ".text section=[0x%jd - 0x%jd].", addr, (intmax_t)text_section_addr_beg, | |
708 | (intmax_t)text_section_addr_end); | |
709 | ret = LTTNG_ERR_ELF_PARSING; | |
710 | goto error; | |
711 | } | |
712 | ||
713 | offset_in_section = addr - text_section_addr_beg; | |
714 | ||
715 | /* | |
716 | * Add the target offset in the text section to the offset of this text | |
717 | * section from the beginning of the binary file. | |
718 | */ | |
719 | *offset = text_section_offset + offset_in_section; | |
720 | ||
721 | error: | |
722 | return ret; | |
723 | } | |
724 | ||
725 | /* | |
726 | * Compute the offset of a symbol from the begining of the ELF binary. | |
727 | * | |
728 | * On success, returns 0 offset parameter is set to the computed value | |
729 | * On failure, returns -1. | |
730 | */ | |
731 | int lttng_elf_get_symbol_offset(int fd, char *symbol, uint64_t *offset) | |
732 | { | |
733 | int ret = 0; | |
734 | int sym_found = 0; | |
735 | int sym_count = 0; | |
736 | int sym_idx = 0; | |
737 | uint64_t addr = 0; | |
738 | char *curr_sym_str = NULL; | |
739 | char *symbol_table_data = NULL; | |
740 | char *string_table_data = NULL; | |
ef3dfe5d | 741 | char *string_table_name = NULL; |
d0927b41 FD |
742 | struct lttng_elf_shdr *symtab_hdr = NULL; |
743 | struct lttng_elf_shdr *strtab_hdr = NULL; | |
744 | struct lttng_elf *elf = NULL; | |
745 | ||
746 | if (!symbol || !offset ) { | |
747 | ret = LTTNG_ERR_ELF_PARSING; | |
748 | goto end; | |
749 | } | |
750 | ||
751 | elf = lttng_elf_create(fd); | |
752 | if (!elf) { | |
753 | ret = LTTNG_ERR_ELF_PARSING; | |
754 | goto end; | |
755 | } | |
756 | ||
ef3dfe5d FD |
757 | /* |
758 | * The .symtab section might not exist on stripped binaries. | |
759 | * Try to get the symbol table section header first. If it's absent, | |
760 | * try to get the dynamic symbol table. All symbols in the dynamic | |
761 | * symbol tab are in the (normal) symbol table if it exists. | |
762 | */ | |
d0927b41 FD |
763 | ret = lttng_elf_get_section_hdr_by_name(elf, SYMBOL_TAB_SECTION_NAME, |
764 | &symtab_hdr); | |
765 | if (ret) { | |
ef3dfe5d FD |
766 | DBG("Cannot get ELF Symbol Table section. Trying to get ELF Dynamic Symbol Table section."); |
767 | /* Get the dynamic symbol table section header. */ | |
768 | ret = lttng_elf_get_section_hdr_by_name(elf, DYNAMIC_SYMBOL_TAB_SECTION_NAME, | |
769 | &symtab_hdr); | |
770 | if (ret) { | |
771 | DBG("Cannot get ELF Symbol Table nor Dynamic Symbol Table sections."); | |
772 | ret = LTTNG_ERR_ELF_PARSING; | |
773 | goto destroy_elf; | |
774 | } | |
775 | string_table_name = DYNAMIC_STRING_TAB_SECTION_NAME; | |
776 | } else { | |
777 | string_table_name = STRING_TAB_SECTION_NAME; | |
d0927b41 | 778 | } |
ef3dfe5d | 779 | |
d0927b41 FD |
780 | /* Get the data associated with the symbol table section. */ |
781 | symbol_table_data = lttng_elf_get_section_data(elf, symtab_hdr); | |
782 | if (symbol_table_data == NULL) { | |
783 | DBG("Cannot get ELF Symbol Table data."); | |
784 | ret = LTTNG_ERR_ELF_PARSING; | |
785 | goto destroy_elf; | |
786 | } | |
787 | ||
788 | /* Get the string table section header. */ | |
ef3dfe5d | 789 | ret = lttng_elf_get_section_hdr_by_name(elf, string_table_name, |
d0927b41 FD |
790 | &strtab_hdr); |
791 | if (ret) { | |
792 | DBG("Cannot get ELF string table section."); | |
793 | goto free_symbol_table_data; | |
794 | } | |
795 | ||
796 | /* Get the data associated with the string table section. */ | |
797 | string_table_data = lttng_elf_get_section_data(elf, strtab_hdr); | |
798 | if (string_table_data == NULL) { | |
799 | DBG("Cannot get ELF string table section data."); | |
800 | ret = LTTNG_ERR_ELF_PARSING; | |
801 | goto free_symbol_table_data; | |
802 | } | |
803 | ||
804 | /* Get the number of symbol in the table for the iteration. */ | |
805 | sym_count = symtab_hdr->sh_size / symtab_hdr->sh_entsize; | |
806 | ||
807 | /* Loop over all symbol. */ | |
808 | for (sym_idx = 0; sym_idx < sym_count; sym_idx++) { | |
809 | struct lttng_elf_sym curr_sym; | |
810 | ||
811 | /* Get the symbol at the current index. */ | |
812 | if (is_elf_32_bit(elf)) { | |
813 | Elf32_Sym tmp = ((Elf32_Sym *) symbol_table_data)[sym_idx]; | |
814 | copy_sym(tmp, curr_sym); | |
815 | } else { | |
816 | Elf64_Sym tmp = ((Elf64_Sym *) symbol_table_data)[sym_idx]; | |
817 | copy_sym(tmp, curr_sym); | |
818 | } | |
819 | ||
820 | /* | |
821 | * If the st_name field is zero, there is no string name for | |
822 | * this symbol; skip to the next symbol. | |
823 | */ | |
824 | if (curr_sym.st_name == 0) { | |
825 | continue; | |
826 | } | |
827 | ||
828 | /* | |
829 | * Use the st_name field in the lttng_elf_sym struct to get offset of | |
830 | * the symbol's name from the beginning of the string table. | |
831 | */ | |
832 | curr_sym_str = string_table_data + curr_sym.st_name; | |
833 | ||
834 | /* | |
835 | * If the current symbol is not a function; skip to the next symbol. | |
836 | */ | |
837 | if (ELF_ST_TYPE(curr_sym.st_info) != STT_FUNC) { | |
838 | continue; | |
839 | } | |
840 | ||
841 | /* | |
842 | * Compare with the search symbol. If there is a match set the address | |
843 | * output parameter and return success. | |
844 | */ | |
845 | if (strcmp(symbol, curr_sym_str) == 0 ) { | |
846 | sym_found = 1; | |
847 | addr = curr_sym.st_value; | |
848 | break; | |
849 | } | |
850 | } | |
851 | ||
852 | if (!sym_found) { | |
853 | DBG("Symbol not found."); | |
854 | ret = LTTNG_ERR_ELF_PARSING; | |
855 | goto free_string_table_data; | |
856 | } | |
857 | ||
858 | /* | |
859 | * Use the virtual address of the symbol to compute the offset of this | |
860 | * symbol from the beginning of the executable file. | |
861 | */ | |
862 | ret = lttng_elf_convert_addr_in_text_to_offset(elf, addr, offset); | |
863 | if (ret) { | |
512df046 | 864 | DBG("Cannot convert addr to offset."); |
d0927b41 FD |
865 | goto free_string_table_data; |
866 | } | |
867 | ||
868 | ||
869 | free_string_table_data: | |
870 | free(string_table_data); | |
871 | free_symbol_table_data: | |
872 | free(symbol_table_data); | |
873 | destroy_elf: | |
874 | lttng_elf_destroy(elf); | |
875 | end: | |
876 | return ret; | |
877 | } | |
8bd52288 FD |
878 | |
879 | /* | |
880 | * Compute the offsets of SDT probes from the begining of the ELF binary. | |
881 | * | |
882 | * On success, returns 0 and the nb_probes parameter is set to the number of | |
883 | * offsets found and the offsets parameter points to an array of offsets where | |
884 | * the SDT probes are. | |
885 | * On failure, returns -1. | |
886 | */ | |
887 | int lttng_elf_get_sdt_probe_offsets(int fd, const char *provider_name, | |
888 | const char *probe_name, uint64_t **offsets, uint32_t *nb_probes) | |
889 | { | |
890 | int ret = 0, nb_match = 0; | |
891 | struct lttng_elf_shdr *stap_note_section_hdr = NULL; | |
892 | struct lttng_elf *elf = NULL; | |
893 | char *stap_note_section_data = NULL; | |
894 | char *curr_note_section_begin, *curr_data_ptr, *curr_probe, *curr_provider; | |
d3be5495 | 895 | char *next_note_ptr; |
8bd52288 FD |
896 | uint32_t name_size, desc_size, note_type; |
897 | uint64_t curr_probe_location, curr_probe_offset, curr_semaphore_location; | |
898 | uint64_t *probe_locs = NULL, *new_probe_locs = NULL; | |
899 | ||
900 | if (!provider_name || !probe_name || !nb_probes || !offsets) { | |
901 | DBG("Invalid arguments."); | |
902 | ret = LTTNG_ERR_ELF_PARSING; | |
903 | goto error; | |
904 | } | |
905 | ||
906 | elf = lttng_elf_create(fd); | |
907 | if (!elf) { | |
908 | DBG("Error allocation ELF."); | |
909 | ret = LTTNG_ERR_ELF_PARSING; | |
910 | goto error; | |
911 | } | |
912 | ||
913 | /* Get the stap note section header. */ | |
914 | ret = lttng_elf_get_section_hdr_by_name(elf, NOTE_STAPSDT_SECTION_NAME, | |
915 | &stap_note_section_hdr); | |
916 | if (ret) { | |
917 | DBG("Cannot get ELF stap note section."); | |
918 | goto destroy_elf_error; | |
919 | } | |
920 | ||
921 | /* Get the data associated with the stap note section. */ | |
922 | stap_note_section_data = lttng_elf_get_section_data(elf, stap_note_section_hdr); | |
923 | if (stap_note_section_data == NULL) { | |
924 | DBG("Cannot get ELF stap note section data."); | |
925 | ret = LTTNG_ERR_ELF_PARSING; | |
926 | goto destroy_elf_error; | |
927 | } | |
928 | ||
8bd52288 FD |
929 | next_note_ptr = stap_note_section_data; |
930 | curr_note_section_begin = stap_note_section_data; | |
931 | ||
932 | *offsets = NULL; | |
933 | while (1) { | |
934 | curr_data_ptr = next_note_ptr; | |
935 | /* Check if we have reached the end of the note section. */ | |
936 | if (curr_data_ptr >= | |
937 | curr_note_section_begin + stap_note_section_hdr->sh_size) { | |
938 | *nb_probes = nb_match; | |
939 | *offsets = probe_locs; | |
940 | ret = 0; | |
941 | break; | |
942 | } | |
943 | /* Get name size field. */ | |
944 | name_size = next_4bytes_boundary(*(uint32_t*) curr_data_ptr); | |
945 | curr_data_ptr += sizeof(uint32_t); | |
946 | ||
947 | /* Sanity check; a zero name_size is reserved. */ | |
948 | if (name_size == 0) { | |
949 | DBG("Invalid name size field in SDT probe descriptions" | |
950 | "section."); | |
951 | ret = -1; | |
952 | goto realloc_error; | |
953 | } | |
954 | ||
955 | /* Get description size field. */ | |
956 | desc_size = next_4bytes_boundary(*(uint32_t*) curr_data_ptr); | |
957 | curr_data_ptr += sizeof(uint32_t); | |
958 | ||
959 | /* Get type field. */ | |
960 | note_type = *(uint32_t *) curr_data_ptr; | |
961 | curr_data_ptr += sizeof(uint32_t); | |
962 | ||
963 | /* | |
964 | * Move the pointer to the next note to be ready for the next | |
965 | * iteration. The current note is made of 3 unsigned 32bit | |
966 | * integers (name size, descriptor size and note type), the | |
967 | * name and the descriptor. To move to the next note, we move | |
968 | * the pointer according to those values. | |
969 | */ | |
970 | next_note_ptr = next_note_ptr + | |
971 | (3 * sizeof(uint32_t)) + desc_size + name_size; | |
972 | ||
973 | /* | |
974 | * Move ptr to the end of the name string (we don't need it) | |
975 | * and go to the next 4 byte alignement. | |
976 | */ | |
977 | if (note_type != NOTE_STAPSDT_TYPE || | |
978 | strncmp(curr_data_ptr, NOTE_STAPSDT_NAME, name_size) != 0) { | |
979 | continue; | |
980 | } | |
981 | ||
982 | curr_data_ptr += name_size; | |
983 | ||
8bd52288 FD |
984 | /* Get probe location. */ |
985 | curr_probe_location = *(uint64_t *) curr_data_ptr; | |
986 | curr_data_ptr += sizeof(uint64_t); | |
987 | ||
988 | /* Pass over the base. Not needed. */ | |
989 | curr_data_ptr += sizeof(uint64_t); | |
990 | ||
991 | /* Get semaphore location. */ | |
992 | curr_semaphore_location = *(uint64_t *) curr_data_ptr; | |
993 | curr_data_ptr += sizeof(uint64_t); | |
994 | /* Get provider name. */ | |
995 | curr_provider = curr_data_ptr; | |
996 | curr_data_ptr += strlen(curr_provider) + 1; | |
997 | ||
998 | /* Get probe name. */ | |
999 | curr_probe = curr_data_ptr; | |
8bd52288 FD |
1000 | |
1001 | /* Check if the provider and probe name match */ | |
1002 | if (strcmp(provider_name, curr_provider) == 0 && | |
1003 | strcmp(probe_name, curr_probe) == 0) { | |
1004 | int new_size; | |
1005 | ||
1006 | /* | |
1007 | * We currently don't support SDT probes with semaphores. Return | |
1008 | * success as we found a matching probe but it's guarded by a | |
1009 | * semaphore. | |
1010 | */ | |
1011 | if (curr_semaphore_location != 0) { | |
1012 | ret = LTTNG_ERR_SDT_PROBE_SEMAPHORE; | |
09f3038c | 1013 | goto realloc_error; |
8bd52288 FD |
1014 | } |
1015 | ||
1016 | new_size = (++nb_match) * sizeof(uint64_t); | |
1017 | ||
1018 | /* | |
1019 | * Found a match with not semaphore, we need to copy the | |
1020 | * probe_location to the output parameter. | |
1021 | */ | |
1022 | new_probe_locs = realloc(probe_locs, new_size); | |
1023 | if (!new_probe_locs) { | |
1024 | /* Error allocating a larger buffer */ | |
1025 | DBG("Allocation error in SDT."); | |
1026 | ret = LTTNG_ERR_NOMEM; | |
1027 | goto realloc_error; | |
1028 | } | |
1029 | probe_locs = new_probe_locs; | |
1030 | new_probe_locs = NULL; | |
1031 | ||
1032 | /* | |
1033 | * Use the virtual address of the probe to compute the offset of | |
1034 | * this probe from the beginning of the executable file. | |
1035 | */ | |
1036 | ret = lttng_elf_convert_addr_in_text_to_offset(elf, | |
1037 | curr_probe_location, &curr_probe_offset); | |
1038 | if (ret) { | |
1039 | DBG("Conversion error in SDT."); | |
1040 | goto realloc_error; | |
1041 | } | |
1042 | ||
1043 | probe_locs[nb_match - 1] = curr_probe_offset; | |
1044 | } | |
1045 | } | |
1046 | ||
1047 | end: | |
1048 | free(stap_note_section_data); | |
1049 | destroy_elf_error: | |
1050 | lttng_elf_destroy(elf); | |
1051 | error: | |
1052 | return ret; | |
1053 | realloc_error: | |
1054 | free(probe_locs); | |
1055 | goto end; | |
1056 | } |