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