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