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