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