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