Fix: sessiond: unbounded elf section data size allocation
[lttng-tools.git] / src / common / lttng-elf.c
CommitLineData
d0927b41
FD
1/*
2 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
3 * Copyright (C) 2017 Francis Deslauriers <francis.deslauriers@efficios.com>
4 * Copyright (C) 2017 Erica Bugden <erica.bugden@efficios.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <common/compat/endian.h>
22#include <common/error.h>
23#include <common/lttng-elf.h>
24#include <common/macros.h>
25#include <common/readwrite.h>
26#include <fcntl.h>
27#include <stdbool.h>
28#include <stdint.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <unistd.h>
34
35#include <elf.h>
36
37#define BUF_LEN 4096
38#define TEXT_SECTION_NAME ".text"
39#define SYMBOL_TAB_SECTION_NAME ".symtab"
40#define STRING_TAB_SECTION_NAME ".strtab"
ef3dfe5d
FD
41#define DYNAMIC_SYMBOL_TAB_SECTION_NAME ".dynsym"
42#define DYNAMIC_STRING_TAB_SECTION_NAME ".dynstr"
d0927b41
FD
43#define NOTE_STAPSDT_SECTION_NAME ".note.stapsdt"
44#define NOTE_STAPSDT_NAME "stapsdt"
45#define NOTE_STAPSDT_TYPE 3
b7e59a88 46#define MAX_SECTION_DATA_SIZE 512 * 1024 * 1024
d0927b41
FD
47
48#if BYTE_ORDER == LITTLE_ENDIAN
49#define NATIVE_ELF_ENDIANNESS ELFDATA2LSB
50#else
51#define NATIVE_ELF_ENDIANNESS ELFDATA2MSB
52#endif
53
8bd52288
FD
54#define next_4bytes_boundary(x) (typeof(x)) ((((uint64_t)x) + 3) & ~0x03)
55
d0927b41
FD
56#define bswap(x) \
57 do { \
58 switch (sizeof(x)) { \
59 case 8: \
b1b34226 60 x = be64toh((uint64_t)x); \
d0927b41
FD
61 break; \
62 case 4: \
b1b34226 63 x = be32toh((uint32_t)x); \
d0927b41
FD
64 break; \
65 case 2: \
b1b34226 66 x = be16toh((uint16_t)x); \
d0927b41
FD
67 break; \
68 case 1: \
69 break; \
70 default: \
71 abort(); \
72 } \
73 } while (0)
74
75#define bswap_shdr(shdr) \
76 do { \
77 bswap((shdr).sh_name); \
78 bswap((shdr).sh_type); \
79 bswap((shdr).sh_flags); \
80 bswap((shdr).sh_addr); \
81 bswap((shdr).sh_offset); \
82 bswap((shdr).sh_size); \
83 bswap((shdr).sh_link); \
84 bswap((shdr).sh_info); \
85 bswap((shdr).sh_addralign); \
86 bswap((shdr).sh_entsize); \
87 } while (0)
88
89#define bswap_ehdr(ehdr) \
90 do { \
91 bswap((ehdr).e_type); \
92 bswap((ehdr).e_machine); \
93 bswap((ehdr).e_version); \
94 bswap((ehdr).e_entry); \
95 bswap((ehdr).e_phoff); \
96 bswap((ehdr).e_shoff); \
97 bswap((ehdr).e_flags); \
98 bswap((ehdr).e_ehsize); \
99 bswap((ehdr).e_phentsize); \
100 bswap((ehdr).e_phnum); \
101 bswap((ehdr).e_shentsize); \
102 bswap((ehdr).e_shnum); \
103 bswap((ehdr).e_shstrndx); \
104 } while (0)
105
106#define copy_shdr(src_shdr, dst_shdr) \
107 do { \
108 (dst_shdr).sh_name = (src_shdr).sh_name; \
109 (dst_shdr).sh_type = (src_shdr).sh_type; \
110 (dst_shdr).sh_flags = (src_shdr).sh_flags; \
111 (dst_shdr).sh_addr = (src_shdr).sh_addr; \
112 (dst_shdr).sh_offset = (src_shdr).sh_offset; \
113 (dst_shdr).sh_size = (src_shdr).sh_size; \
114 (dst_shdr).sh_link = (src_shdr).sh_link; \
115 (dst_shdr).sh_info = (src_shdr).sh_info; \
116 (dst_shdr).sh_addralign = (src_shdr).sh_addralign; \
117 (dst_shdr).sh_entsize = (src_shdr).sh_entsize; \
118 } while (0)
119
120#define copy_ehdr(src_ehdr, dst_ehdr) \
121 do { \
122 (dst_ehdr).e_type = (src_ehdr).e_type; \
123 (dst_ehdr).e_machine = (src_ehdr).e_machine; \
124 (dst_ehdr).e_version = (src_ehdr).e_version; \
125 (dst_ehdr).e_entry = (src_ehdr).e_entry; \
126 (dst_ehdr).e_phoff = (src_ehdr).e_phoff; \
127 (dst_ehdr).e_shoff = (src_ehdr).e_shoff; \
128 (dst_ehdr).e_flags = (src_ehdr).e_flags; \
129 (dst_ehdr).e_ehsize = (src_ehdr).e_ehsize; \
130 (dst_ehdr).e_phentsize = (src_ehdr).e_phentsize; \
131 (dst_ehdr).e_phnum = (src_ehdr).e_phnum; \
132 (dst_ehdr).e_shentsize = (src_ehdr).e_shentsize; \
133 (dst_ehdr).e_shnum = (src_ehdr).e_shnum; \
134 (dst_ehdr).e_shstrndx = (src_ehdr).e_shstrndx; \
135 } while (0)
136
137#define copy_sym(src_sym, dst_sym) \
138 do { \
139 dst_sym.st_name = src_sym.st_name; \
140 dst_sym.st_info = src_sym.st_info; \
141 dst_sym.st_other = src_sym.st_other; \
142 dst_sym.st_shndx = src_sym.st_shndx; \
143 dst_sym.st_value = src_sym.st_value; \
144 dst_sym.st_size = src_sym.st_size; \
145 } while (0)
146
147/* Both 32bit and 64bit use the same 1 byte field for type. (See elf.h) */
148#define ELF_ST_TYPE(val) ELF32_ST_TYPE(val)
149
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};
192
193struct lttng_elf {
194 int fd;
b7e59a88 195 size_t file_size;
d0927b41
FD
196 uint8_t bitness;
197 uint8_t endianness;
198 /* Offset in bytes to start of section names string table. */
199 off_t section_names_offset;
200 /* Size in bytes of section names string table. */
201 size_t section_names_size;
202 struct lttng_elf_ehdr *ehdr;
203};
204
205static inline
206int is_elf_32_bit(struct lttng_elf *elf)
207{
208 return elf->bitness == ELFCLASS32;
209}
210
211static inline
212int is_elf_native_endian(struct lttng_elf *elf)
213{
214 return elf->endianness == NATIVE_ELF_ENDIANNESS;
215}
216
217static
218int populate_section_header(struct lttng_elf * elf, struct lttng_elf_shdr *shdr,
219 uint32_t index)
220{
221 int ret = 0;
222 off_t offset;
223
224 /* Compute the offset of the section in the file */
225 offset = (off_t) elf->ehdr->e_shoff
226 + (off_t) index * elf->ehdr->e_shentsize;
227
228 if (lseek(elf->fd, offset, SEEK_SET) < 0) {
229 PERROR("Error seeking to the beginning of ELF section header");
230 ret = -1;
231 goto error;
232 }
233
234 if (is_elf_32_bit(elf)) {
235 Elf32_Shdr elf_shdr;
236
237 if (lttng_read(elf->fd, &elf_shdr, sizeof(elf_shdr)) < sizeof(elf_shdr)) {
238 PERROR("Error reading ELF section header");
239 ret = -1;
240 goto error;
241 }
242 if (!is_elf_native_endian(elf)) {
243 bswap_shdr(elf_shdr);
244 }
245 copy_shdr(elf_shdr, *shdr);
246 } else {
247 Elf64_Shdr elf_shdr;
248
249 if (lttng_read(elf->fd, &elf_shdr, sizeof(elf_shdr)) < sizeof(elf_shdr)) {
250 PERROR("Error reading ELF section header");
251 ret = -1;
252 goto error;
253 }
254 if (!is_elf_native_endian(elf)) {
255 bswap_shdr(elf_shdr);
256 }
257 copy_shdr(elf_shdr, *shdr);
258 }
259
260error:
261 return ret;
262}
263
264static
265int populate_elf_header(struct lttng_elf *elf)
266{
267 int ret = 0;
268
269 /*
270 * Move the read pointer back to the beginning to read the full header
271 * and copy it in our structure.
272 */
273 if (lseek(elf->fd, 0, SEEK_SET) < 0) {
274 PERROR("Error seeking to the beginning of the file");
275 ret = -1;
276 goto error;
277 }
278
279 /*
280 * Use macros to set fields in the ELF header struct for both 32bit and
281 * 64bit.
282 */
283 if (is_elf_32_bit(elf)) {
284 Elf32_Ehdr elf_ehdr;
285
286 if (lttng_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr)) < sizeof(elf_ehdr)) {
287 ret = -1;
288 goto error;
289 }
290 if (!is_elf_native_endian(elf)) {
291 bswap_ehdr(elf_ehdr);
292 }
293 copy_ehdr(elf_ehdr, *(elf->ehdr));
294 } else {
295 Elf64_Ehdr elf_ehdr;
296
297 if (lttng_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr)) < sizeof(elf_ehdr)) {
298 ret = -1;
299 goto error;
300 }
301 if (!is_elf_native_endian(elf)) {
302 bswap_ehdr(elf_ehdr);
303 }
304 copy_ehdr(elf_ehdr, *(elf->ehdr));
305 }
306error:
307 return ret;
308}
309
310/*
311 * Retrieve the nth (where n is the `index` argument) shdr (section
312 * header) from the given elf instance.
313 *
22fae25a 314 * 0 is returned on succes, -1 on failure.
d0927b41
FD
315 */
316static
22fae25a
JG
317int lttng_elf_get_section_hdr(struct lttng_elf *elf,
318 uint16_t index, struct lttng_elf_shdr *out_header)
d0927b41 319{
d0927b41
FD
320 int ret = 0;
321
322 if (!elf) {
22fae25a 323 ret = -1;
d0927b41
FD
324 goto error;
325 }
326
327 if (index >= elf->ehdr->e_shnum) {
22fae25a 328 ret = -1;
d0927b41
FD
329 goto error;
330 }
331
22fae25a 332 ret = populate_section_header(elf, out_header, index);
d0927b41 333 if (ret) {
d0927b41
FD
334 DBG("Error populating section header.");
335 goto error;
336 }
d0927b41
FD
337
338error:
22fae25a 339 return ret;
d0927b41
FD
340}
341
342/*
343 * Lookup a section's name from a given offset (usually from an shdr's
344 * sh_name value) in bytes relative to the beginning of the section
345 * names string table.
346 *
347 * If no name is found, NULL is returned.
348 */
349static
350char *lttng_elf_get_section_name(struct lttng_elf *elf, off_t offset)
351{
352 char *name = NULL;
353 size_t name_length = 0, to_read; /* name_length does not include \0 */
354
355 if (!elf) {
356 goto error;
357 }
358
359 if (offset >= elf->section_names_size) {
360 goto error;
361 }
362
363 if (lseek(elf->fd, elf->section_names_offset + offset, SEEK_SET) < 0) {
364 PERROR("Error seeking to the beginning of ELF string table section");
365 goto error;
366 }
367
368 to_read = elf->section_names_size - offset;
369
370 /* Find first \0 after or at current location, remember name_length. */
371 for (;;) {
372 char buf[BUF_LEN];
373 ssize_t read_len;
374 size_t i;
375
376 if (!to_read) {
377 goto error;
378 }
379 read_len = lttng_read(elf->fd, buf, min_t(size_t, BUF_LEN, to_read));
380 if (read_len <= 0) {
381 PERROR("Error reading ELF string table section");
382 goto error;
383 }
384 for (i = 0; i < read_len; i++) {
385 if (buf[i] == '\0') {
386 name_length += i;
387 goto end;
388 }
389 }
390 name_length += read_len;
391 to_read -= read_len;
392 }
393end:
394 /*
395 * We found the length of the section name, now seek back to the
396 * beginning of the name and copy it in the newly allocated buffer.
397 */
398 name = zmalloc(sizeof(char) * (name_length + 1)); /* + 1 for \0 */
399 if (!name) {
400 PERROR("Error allocating ELF section name buffer");
401 goto error;
402 }
403 if (lseek(elf->fd, elf->section_names_offset + offset, SEEK_SET) < 0) {
404 PERROR("Error seeking to the offset of the ELF section name");
405 goto error;
406 }
407 if (lttng_read(elf->fd, name, name_length + 1) < name_length + 1) {
408 PERROR("Error reading the ELF section name");
409 goto error;
410 }
411
412 return name;
413
414error:
415 free(name);
416 return NULL;
417}
418
419static
420int lttng_elf_validate_and_populate(struct lttng_elf *elf)
421{
422 uint8_t version;
423 uint8_t e_ident[EI_NIDENT];
424 uint8_t *magic_number = NULL;
425 int ret = 0;
426
427 if (elf->fd == -1) {
428 DBG("fd error");
429 ret = LTTNG_ERR_ELF_PARSING;
430 goto end;
431 }
432
433 /*
434 * First read the magic number, endianness and version to later populate
435 * the ELF header with the correct endianness and bitness.
436 * (see elf.h)
437 */
438
439 if (lseek(elf->fd, 0, SEEK_SET) < 0) {
440 PERROR("Error seeking the beginning of ELF file");
441 ret = LTTNG_ERR_ELF_PARSING;
442 goto end;
443 }
444 ret = lttng_read(elf->fd, e_ident, EI_NIDENT);
445 if (ret < EI_NIDENT) {
446 DBG("Error reading the ELF identification fields");
447 if (ret == -1) {
448 PERROR("Error reading the ELF identification fields");
449 }
450 ret = LTTNG_ERR_ELF_PARSING;
451 goto end;
452 }
453
454 /*
455 * Copy fields used to check that the target file is in fact a valid ELF
456 * file.
457 */
458 elf->bitness = e_ident[EI_CLASS];
459 elf->endianness = e_ident[EI_DATA];
460 version = e_ident[EI_VERSION];
461 magic_number = &e_ident[EI_MAG0];
462
463 /*
464 * Check the magic number.
465 */
466 if (memcmp(magic_number, ELFMAG, SELFMAG) != 0) {
467 DBG("Error check ELF magic number.");
468 ret = LTTNG_ERR_ELF_PARSING;
469 goto end;
470 }
471
472 /*
473 * Check the bitness is either ELFCLASS32 or ELFCLASS64.
474 */
475 if (elf->bitness <= ELFCLASSNONE || elf->bitness >= ELFCLASSNUM) {
476 DBG("ELF class error.");
477 ret = LTTNG_ERR_ELF_PARSING;
478 goto end;
479 }
480
481 /*
482 * Check the endianness is either ELFDATA2LSB or ELFDATA2MSB.
483 */
484 if (elf->endianness <= ELFDATANONE || elf->endianness >= ELFDATANUM) {
485 DBG("ELF endianness error.");
486 ret = LTTNG_ERR_ELF_PARSING;
487 goto end;
488 }
489
490 /*
491 * Check the version is ELF_CURRENT.
492 */
493 if (version <= EV_NONE || version >= EV_NUM) {
494 DBG("Wrong ELF version.");
495 ret = LTTNG_ERR_ELF_PARSING;
496 goto end;
497 }
498
499 elf->ehdr = zmalloc(sizeof(struct lttng_elf_ehdr));
500 if (!elf->ehdr) {
501 PERROR("Error allocation buffer for ELF header");
502 ret = LTTNG_ERR_NOMEM;
503 goto end;
504 }
505
506 /*
507 * Copy the content of the elf header.
508 */
509 ret = populate_elf_header(elf);
510 if (ret) {
511 DBG("Error reading ELF header,");
512 goto free_elf_error;
513 }
514
515 goto end;
516
517free_elf_error:
518 free(elf->ehdr);
519 elf->ehdr = NULL;
520end:
521 return ret;
522}
523
524/*
525 * Create an instance of lttng_elf for the ELF file located at
526 * `path`.
527 *
528 * Return a pointer to the instance on success, NULL on failure.
529 */
530static
531struct lttng_elf *lttng_elf_create(int fd)
532{
22fae25a 533 struct lttng_elf_shdr section_names_shdr;
d0927b41
FD
534 struct lttng_elf *elf = NULL;
535 int ret;
b7e59a88 536 struct stat stat_buf;
d0927b41
FD
537
538 if (fd < 0) {
539 goto error;
540 }
541
b7e59a88
JG
542 ret = fstat(fd, &stat_buf);
543 if (ret) {
544 PERROR("Failed to determine size of elf file");
545 goto error;
546 }
547 if (!S_ISREG(stat_buf.st_mode)) {
548 ERR("Refusing to initialize lttng_elf from non-regular file");
549 goto error;
550 }
551
d0927b41
FD
552 elf = zmalloc(sizeof(struct lttng_elf));
553 if (!elf) {
554 PERROR("Error allocating struct lttng_elf");
555 goto error;
556 }
b7e59a88 557 elf->file_size = (size_t) stat_buf.st_size;
d0927b41
FD
558
559 elf->fd = dup(fd);
560 if (elf->fd < 0) {
561 PERROR("Error duplicating file descriptor to binary");
562 goto error;
563 }
564
565 ret = lttng_elf_validate_and_populate(elf);
566 if (ret) {
567 goto error;
568 }
569
22fae25a
JG
570 ret = lttng_elf_get_section_hdr(
571 elf, elf->ehdr->e_shstrndx, &section_names_shdr);
572 if (ret) {
d0927b41
FD
573 goto error;
574 }
575
22fae25a
JG
576 elf->section_names_offset = section_names_shdr.sh_offset;
577 elf->section_names_size = section_names_shdr.sh_size;
d0927b41
FD
578 return elf;
579
580error:
581 if (elf) {
582 if (elf->ehdr) {
583 free(elf->ehdr);
584 }
585 if (elf->fd >= 0) {
586 if (close(elf->fd)) {
587 PERROR("Error closing file descriptor in error path");
588 abort();
589 }
590 }
591 free(elf);
592 }
593 return NULL;
594}
595
596/*
597 * Destroy the given lttng_elf instance.
598 */
599static
600void lttng_elf_destroy(struct lttng_elf *elf)
601{
602 if (!elf) {
603 return;
604 }
605
606 free(elf->ehdr);
607 if (close(elf->fd)) {
608 PERROR("Error closing file description in error path");
609 abort();
610 }
611 free(elf);
612}
613
614static
615int lttng_elf_get_section_hdr_by_name(struct lttng_elf *elf,
22fae25a 616 const char *section_name, struct lttng_elf_shdr *section_hdr)
d0927b41
FD
617{
618 int i;
619 char *curr_section_name;
22fae25a 620
d0927b41 621 for (i = 0; i < elf->ehdr->e_shnum; ++i) {
4e0b99ca 622 bool name_equal;
22fae25a 623 int ret = lttng_elf_get_section_hdr(elf, i, section_hdr);
d0927b41 624
22fae25a
JG
625 if (ret) {
626 break;
627 }
628 curr_section_name = lttng_elf_get_section_name(elf,
629 section_hdr->sh_name);
d0927b41
FD
630 if (!curr_section_name) {
631 continue;
632 }
4e0b99ca
JG
633 name_equal = strcmp(curr_section_name, section_name) == 0;
634 free(curr_section_name);
635 if (name_equal) {
d0927b41
FD
636 return 0;
637 }
638 }
639 return LTTNG_ERR_ELF_PARSING;
640}
641
642static
643char *lttng_elf_get_section_data(struct lttng_elf *elf,
644 struct lttng_elf_shdr *shdr)
645{
646 int ret;
647 off_t section_offset;
648 char *data;
b7e59a88
JG
649 const size_t max_alloc_size = min_t(size_t, MAX_SECTION_DATA_SIZE,
650 elf->file_size);
d0927b41
FD
651
652 if (!elf || !shdr) {
653 goto error;
654 }
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
b7e59a88
JG
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 }
d0927b41
FD
667 data = 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
680free_error:
681 free(data);
682error:
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 */
693static
694int 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;
22fae25a 702 struct lttng_elf_shdr text_section_hdr;
d0927b41
FD
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
22fae25a
JG
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;
d0927b41
FD
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
743error:
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 */
753int 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;
ef3dfe5d 763 char *string_table_name = NULL;
22fae25a
JG
764 struct lttng_elf_shdr symtab_hdr;
765 struct lttng_elf_shdr strtab_hdr;
d0927b41
FD
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
ef3dfe5d
FD
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 */
d0927b41
FD
785 ret = lttng_elf_get_section_hdr_by_name(elf, SYMBOL_TAB_SECTION_NAME,
786 &symtab_hdr);
787 if (ret) {
ef3dfe5d
FD
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;
d0927b41 800 }
ef3dfe5d 801
d0927b41 802 /* Get the data associated with the symbol table section. */
22fae25a 803 symbol_table_data = lttng_elf_get_section_data(elf, &symtab_hdr);
d0927b41
FD
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. */
ef3dfe5d 811 ret = lttng_elf_get_section_hdr_by_name(elf, string_table_name,
d0927b41
FD
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. */
22fae25a 819 string_table_data = lttng_elf_get_section_data(elf, &strtab_hdr);
d0927b41
FD
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. */
22fae25a 827 sym_count = symtab_hdr.sh_size / symtab_hdr.sh_entsize;
d0927b41
FD
828
829 /* Loop over all symbol. */
830 for (sym_idx = 0; sym_idx < sym_count; sym_idx++) {
831 struct lttng_elf_sym curr_sym;
832
833 /* Get the symbol at the current index. */
834 if (is_elf_32_bit(elf)) {
835 Elf32_Sym tmp = ((Elf32_Sym *) symbol_table_data)[sym_idx];
836 copy_sym(tmp, curr_sym);
837 } else {
838 Elf64_Sym tmp = ((Elf64_Sym *) symbol_table_data)[sym_idx];
839 copy_sym(tmp, curr_sym);
840 }
841
842 /*
843 * If the st_name field is zero, there is no string name for
844 * this symbol; skip to the next symbol.
845 */
846 if (curr_sym.st_name == 0) {
847 continue;
848 }
849
850 /*
851 * Use the st_name field in the lttng_elf_sym struct to get offset of
852 * the symbol's name from the beginning of the string table.
853 */
854 curr_sym_str = string_table_data + curr_sym.st_name;
855
856 /*
857 * If the current symbol is not a function; skip to the next symbol.
858 */
859 if (ELF_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) {
512df046 886 DBG("Cannot convert addr to offset.");
d0927b41
FD
887 goto free_string_table_data;
888 }
889
890
891free_string_table_data:
892 free(string_table_data);
893free_symbol_table_data:
894 free(symbol_table_data);
895destroy_elf:
896 lttng_elf_destroy(elf);
897end:
898 return ret;
899}
8bd52288
FD
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 */
909int 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;
22fae25a 913 struct lttng_elf_shdr stap_note_section_hdr;
8bd52288
FD
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;
d3be5495 917 char *next_note_ptr;
8bd52288
FD
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. */
22fae25a
JG
944 stap_note_section_data =
945 lttng_elf_get_section_data(elf, &stap_note_section_hdr);
8bd52288
FD
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
8bd52288
FD
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 >=
22fae25a
JG
960 curr_note_section_begin +
961 stap_note_section_hdr.sh_size) {
8bd52288
FD
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
8bd52288
FD
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;
8bd52288
FD
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;
09f3038c 1037 goto realloc_error;
8bd52288
FD
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
1071end:
1072 free(stap_note_section_data);
1073destroy_elf_error:
1074 lttng_elf_destroy(elf);
1075error:
1076 return ret;
1077realloc_error:
1078 free(probe_locs);
1079 goto end;
1080}
This page took 0.067555 seconds and 4 git commands to generate.