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>
6 * SPDX-License-Identifier: LGPL-2.1-or-later
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>
24 #include <sys/types.h>
28 #define TEXT_SECTION_NAME ".text"
29 #define SYMBOL_TAB_SECTION_NAME ".symtab"
30 #define STRING_TAB_SECTION_NAME ".strtab"
31 #define DYNAMIC_SYMBOL_TAB_SECTION_NAME ".dynsym"
32 #define DYNAMIC_STRING_TAB_SECTION_NAME ".dynstr"
33 #define NOTE_STAPSDT_SECTION_NAME ".note.stapsdt"
34 #define NOTE_STAPSDT_NAME "stapsdt"
35 #define NOTE_STAPSDT_TYPE 3
36 #define MAX_SECTION_DATA_SIZE 512 * 1024 * 1024
38 #if BYTE_ORDER == LITTLE_ENDIAN
39 #define NATIVE_ELF_ENDIANNESS ELFDATA2LSB
41 #define NATIVE_ELF_ENDIANNESS ELFDATA2MSB
44 #define next_4bytes_boundary(x) (typeof(x)) ((((uint64_t) x) + 3) & ~0x03)
48 switch (sizeof(x)) { \
50 x = be64toh((uint64_t) x); \
53 x = be32toh((uint32_t) x); \
56 x = be16toh((uint16_t) x); \
65 #define bswap_shdr(shdr) \
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); \
79 #define bswap_ehdr(ehdr) \
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); \
96 #define copy_shdr(src_shdr, dst_shdr) \
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; \
110 #define copy_ehdr(src_ehdr, dst_ehdr) \
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; \
127 #define copy_sym(src_sym, dst_sym) \
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; \
138 #define ELFCLASSNUM 3
150 struct lttng_elf_ehdr
{
159 uint16_t e_phentsize
;
161 uint16_t e_shentsize
;
166 struct lttng_elf_shdr
{
175 uint64_t sh_addralign
;
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
184 struct lttng_elf_sym
{
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
;
206 static inline int is_elf_32_bit(struct lttng_elf
*elf
)
208 return elf
->bitness
== ELFCLASS32
;
211 static inline int is_elf_native_endian(struct lttng_elf
*elf
)
213 return elf
->endianness
== NATIVE_ELF_ENDIANNESS
;
217 populate_section_header(struct lttng_elf
*elf
, struct lttng_elf_shdr
*shdr
, uint32_t index
)
222 /* Compute the offset of the section in the file */
223 offset
= (off_t
) elf
->ehdr
->e_shoff
+ (off_t
) index
* elf
->ehdr
->e_shentsize
;
225 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
226 PERROR("Error seeking to the beginning of ELF section header");
231 if (is_elf_32_bit(elf
)) {
234 if (lttng_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
)) < sizeof(elf_shdr
)) {
235 PERROR("Error reading ELF section header");
239 if (!is_elf_native_endian(elf
)) {
240 bswap_shdr(elf_shdr
);
242 copy_shdr(elf_shdr
, *shdr
);
246 if (lttng_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
)) < sizeof(elf_shdr
)) {
247 PERROR("Error reading ELF section header");
251 if (!is_elf_native_endian(elf
)) {
252 bswap_shdr(elf_shdr
);
254 copy_shdr(elf_shdr
, *shdr
);
261 static int populate_elf_header(struct lttng_elf
*elf
)
266 * Move the read pointer back to the beginning to read the full header
267 * and copy it in our structure.
269 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
270 PERROR("Error seeking to the beginning of the file");
276 * Use macros to set fields in the ELF header struct for both 32bit and
279 if (is_elf_32_bit(elf
)) {
282 if (lttng_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
)) < sizeof(elf_ehdr
)) {
286 if (!is_elf_native_endian(elf
)) {
287 bswap_ehdr(elf_ehdr
);
289 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
293 if (lttng_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
)) < sizeof(elf_ehdr
)) {
297 if (!is_elf_native_endian(elf
)) {
298 bswap_ehdr(elf_ehdr
);
300 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
307 * Retrieve the nth (where n is the `index` argument) shdr (section
308 * header) from the given elf instance.
310 * 0 is returned on succes, -1 on failure.
313 lttng_elf_get_section_hdr(struct lttng_elf
*elf
, uint16_t index
, struct lttng_elf_shdr
*out_header
)
322 if (index
>= elf
->ehdr
->e_shnum
) {
327 ret
= populate_section_header(elf
, out_header
, index
);
329 DBG("Error populating section header.");
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.
342 * If no name is found, NULL is returned.
344 static char *lttng_elf_get_section_name(struct lttng_elf
*elf
, off_t offset
)
347 size_t name_length
= 0, to_read
; /* name_length does not include \0 */
353 if (offset
>= elf
->section_names_size
) {
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");
362 to_read
= elf
->section_names_size
- offset
;
364 /* Find first \0 after or at current location, remember name_length. */
373 read_len
= lttng_read(elf
->fd
, buf
, std::min
<size_t>(BUF_LEN
, to_read
));
375 PERROR("Error reading ELF string table section");
378 for (i
= 0; i
< read_len
; i
++) {
379 if (buf
[i
] == '\0') {
384 name_length
+= read_len
;
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.
392 name
= calloc
<char>((name_length
+ 1)); /* + 1 for \0 */
394 PERROR("Error allocating ELF section name buffer");
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");
401 if (lttng_read(elf
->fd
, name
, name_length
+ 1) < name_length
+ 1) {
402 PERROR("Error reading the ELF section name");
413 static int lttng_elf_validate_and_populate(struct lttng_elf
*elf
)
416 uint8_t e_ident
[EI_NIDENT
];
417 uint8_t *magic_number
= NULL
;
422 ret
= LTTNG_ERR_ELF_PARSING
;
427 * First read the magic number, endianness and version to later populate
428 * the ELF header with the correct endianness and bitness.
432 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
433 PERROR("Error seeking the beginning of ELF file");
434 ret
= LTTNG_ERR_ELF_PARSING
;
437 ret
= lttng_read(elf
->fd
, e_ident
, EI_NIDENT
);
438 if (ret
< EI_NIDENT
) {
439 DBG("Error reading the ELF identification fields");
441 PERROR("Error reading the ELF identification fields");
443 ret
= LTTNG_ERR_ELF_PARSING
;
448 * Copy fields used to check that the target file is in fact a valid ELF
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
];
457 * Check the magic number.
459 if (memcmp(magic_number
, ELFMAG
, SELFMAG
) != 0) {
460 DBG("Error check ELF magic number.");
461 ret
= LTTNG_ERR_ELF_PARSING
;
466 * Check the bitness is either ELFCLASS32 or ELFCLASS64.
468 if (elf
->bitness
<= ELFCLASSNONE
|| elf
->bitness
>= ELFCLASSNUM
) {
469 DBG("ELF class error.");
470 ret
= LTTNG_ERR_ELF_PARSING
;
475 * Check the endianness is either ELFDATA2LSB or ELFDATA2MSB.
477 if (elf
->endianness
<= ELFDATANONE
|| elf
->endianness
>= ELFDATANUM
) {
478 DBG("ELF endianness error.");
479 ret
= LTTNG_ERR_ELF_PARSING
;
484 * Check the version is ELF_CURRENT.
486 if (version
<= EV_NONE
|| version
>= EV_NUM
) {
487 DBG("Wrong ELF version.");
488 ret
= LTTNG_ERR_ELF_PARSING
;
492 elf
->ehdr
= zmalloc
<lttng_elf_ehdr
>();
494 PERROR("Error allocation buffer for ELF header");
495 ret
= LTTNG_ERR_NOMEM
;
500 * Copy the content of the elf header.
502 ret
= populate_elf_header(elf
);
504 DBG("Error reading ELF header,");
518 * Create an instance of lttng_elf for the ELF file located at
521 * Return a pointer to the instance on success, NULL on failure.
523 static struct lttng_elf
*lttng_elf_create(int fd
)
525 struct lttng_elf_shdr section_names_shdr
;
526 struct lttng_elf
*elf
= NULL
;
528 struct stat stat_buf
;
534 ret
= fstat(fd
, &stat_buf
);
536 PERROR("Failed to determine size of elf file");
539 if (!S_ISREG(stat_buf
.st_mode
)) {
540 ERR("Refusing to initialize lttng_elf from non-regular file");
544 elf
= zmalloc
<lttng_elf
>();
546 PERROR("Error allocating struct lttng_elf");
549 elf
->file_size
= (size_t) stat_buf
.st_size
;
553 PERROR("Error duplicating file descriptor to binary");
557 ret
= lttng_elf_validate_and_populate(elf
);
562 ret
= lttng_elf_get_section_hdr(elf
, elf
->ehdr
->e_shstrndx
, §ion_names_shdr
);
567 elf
->section_names_offset
= section_names_shdr
.sh_offset
;
568 elf
->section_names_size
= section_names_shdr
.sh_size
;
577 if (close(elf
->fd
)) {
578 PERROR("Error closing file descriptor in error path");
588 * Destroy the given lttng_elf instance.
590 static void lttng_elf_destroy(struct lttng_elf
*elf
)
597 if (close(elf
->fd
)) {
598 PERROR("Error closing file description in error path");
604 static int lttng_elf_get_section_hdr_by_name(struct lttng_elf
*elf
,
605 const char *section_name
,
606 struct lttng_elf_shdr
*section_hdr
)
609 char *curr_section_name
;
611 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
613 int ret
= lttng_elf_get_section_hdr(elf
, i
, section_hdr
);
618 curr_section_name
= lttng_elf_get_section_name(elf
, section_hdr
->sh_name
);
619 if (!curr_section_name
) {
622 name_equal
= strcmp(curr_section_name
, section_name
) == 0;
623 free(curr_section_name
);
628 return LTTNG_ERR_ELF_PARSING
;
631 static char *lttng_elf_get_section_data(struct lttng_elf
*elf
, struct lttng_elf_shdr
*shdr
)
634 off_t section_offset
;
636 size_t max_alloc_size
;
642 max_alloc_size
= std::min
<size_t>(MAX_SECTION_DATA_SIZE
, elf
->file_size
);
644 section_offset
= shdr
->sh_offset
;
645 if (lseek(elf
->fd
, section_offset
, SEEK_SET
) < 0) {
646 PERROR("Error seeking to section offset");
650 if (shdr
->sh_size
> max_alloc_size
) {
651 ERR("ELF section size exceeds maximal allowed size of %zu bytes", max_alloc_size
);
654 data
= calloc
<char>(shdr
->sh_size
);
656 PERROR("Error allocating buffer for ELF section data");
659 ret
= lttng_read(elf
->fd
, data
, shdr
->sh_size
);
661 PERROR("Error reading ELF section data");
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.
678 * Returns the offset on success or non-zero in case of failure.
680 static int lttng_elf_convert_addr_in_text_to_offset(struct lttng_elf
*elf_handle
,
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
;
689 struct lttng_elf_shdr text_section_hdr
;
692 DBG("Invalid ELF handle.");
693 ret
= LTTNG_ERR_ELF_PARSING
;
697 /* Get a pointer to the .text section header. */
698 ret
= lttng_elf_get_section_hdr_by_name(elf_handle
, TEXT_SECTION_NAME
, &text_section_hdr
);
700 DBG("Text section not found in binary.");
701 ret
= LTTNG_ERR_ELF_PARSING
;
705 text_section_offset
= text_section_hdr
.sh_offset
;
706 text_section_addr_beg
= text_section_hdr
.sh_addr
;
707 text_section_addr_end
= text_section_addr_beg
+ text_section_hdr
.sh_size
;
710 * Verify that the address is within the .text section boundaries.
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, "
714 ".text section=[0x%jd - 0x%jd].",
716 (intmax_t) text_section_addr_beg
,
717 (intmax_t) text_section_addr_end
);
718 ret
= LTTNG_ERR_ELF_PARSING
;
722 offset_in_section
= addr
- text_section_addr_beg
;
725 * Add the target offset in the text section to the offset of this text
726 * section from the beginning of the binary file.
728 *offset
= text_section_offset
+ offset_in_section
;
735 * Compute the offset of a symbol from the begining of the ELF binary.
737 * On success, returns 0 offset parameter is set to the computed value
738 * On failure, returns -1.
740 int lttng_elf_get_symbol_offset(int fd
, char *symbol
, uint64_t *offset
)
747 char *curr_sym_str
= NULL
;
748 char *symbol_table_data
= NULL
;
749 char *string_table_data
= NULL
;
750 const char *string_table_name
= NULL
;
751 struct lttng_elf_shdr symtab_hdr
;
752 struct lttng_elf_shdr strtab_hdr
;
753 struct lttng_elf
*elf
= NULL
;
755 if (!symbol
|| !offset
) {
756 ret
= LTTNG_ERR_ELF_PARSING
;
760 elf
= lttng_elf_create(fd
);
762 ret
= LTTNG_ERR_ELF_PARSING
;
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.
772 ret
= lttng_elf_get_section_hdr_by_name(elf
, SYMBOL_TAB_SECTION_NAME
, &symtab_hdr
);
774 DBG("Cannot get ELF Symbol Table section. Trying to get ELF Dynamic Symbol Table section.");
775 /* Get the dynamic symbol table section header. */
776 ret
= lttng_elf_get_section_hdr_by_name(
777 elf
, DYNAMIC_SYMBOL_TAB_SECTION_NAME
, &symtab_hdr
);
779 DBG("Cannot get ELF Symbol Table nor Dynamic Symbol Table sections.");
780 ret
= LTTNG_ERR_ELF_PARSING
;
783 string_table_name
= DYNAMIC_STRING_TAB_SECTION_NAME
;
785 string_table_name
= STRING_TAB_SECTION_NAME
;
788 /* Get the data associated with the symbol table section. */
789 symbol_table_data
= lttng_elf_get_section_data(elf
, &symtab_hdr
);
790 if (symbol_table_data
== NULL
) {
791 DBG("Cannot get ELF Symbol Table data.");
792 ret
= LTTNG_ERR_ELF_PARSING
;
796 /* Get the string table section header. */
797 ret
= lttng_elf_get_section_hdr_by_name(elf
, string_table_name
, &strtab_hdr
);
799 DBG("Cannot get ELF string table section.");
800 goto free_symbol_table_data
;
803 /* Get the data associated with the string table section. */
804 string_table_data
= lttng_elf_get_section_data(elf
, &strtab_hdr
);
805 if (string_table_data
== NULL
) {
806 DBG("Cannot get ELF string table section data.");
807 ret
= LTTNG_ERR_ELF_PARSING
;
808 goto free_symbol_table_data
;
811 /* Get the number of symbol in the table for the iteration. */
812 if (symtab_hdr
.sh_entsize
== 0) {
813 DBG("Invalid ELF string table entry size.");
814 ret
= LTTNG_ERR_ELF_PARSING
;
815 goto free_string_table_data
;
818 sym_count
= symtab_hdr
.sh_size
/ symtab_hdr
.sh_entsize
;
820 /* Loop over all symbol. */
821 for (sym_idx
= 0; sym_idx
< sym_count
; sym_idx
++) {
822 struct lttng_elf_sym curr_sym
;
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
);
829 Elf64_Sym tmp
= ((Elf64_Sym
*) symbol_table_data
)[sym_idx
];
830 copy_sym(tmp
, curr_sym
);
834 * If the st_name field is zero, there is no string name for
835 * this symbol; skip to the next symbol.
837 if (curr_sym
.st_name
== 0) {
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.
845 curr_sym_str
= string_table_data
+ curr_sym
.st_name
;
848 * If the current symbol is not a function; skip to the next symbol.
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
) {
856 * Compare with the search symbol. If there is a match set the address
857 * output parameter and return success.
859 if (strcmp(symbol
, curr_sym_str
) == 0) {
861 addr
= curr_sym
.st_value
;
867 DBG("Symbol not found.");
868 ret
= LTTNG_ERR_ELF_PARSING
;
869 goto free_string_table_data
;
873 * Use the virtual address of the symbol to compute the offset of this
874 * symbol from the beginning of the executable file.
876 ret
= lttng_elf_convert_addr_in_text_to_offset(elf
, addr
, offset
);
878 DBG("Cannot convert addr to offset.");
879 goto free_string_table_data
;
882 free_string_table_data
:
883 free(string_table_data
);
884 free_symbol_table_data
:
885 free(symbol_table_data
);
887 lttng_elf_destroy(elf
);
893 * Compute the offsets of SDT probes from the begining of the ELF binary.
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.
900 int lttng_elf_get_sdt_probe_offsets(int fd
,
901 const char *provider_name
,
902 const char *probe_name
,
906 int ret
= 0, nb_match
= 0;
907 struct lttng_elf_shdr stap_note_section_hdr
;
908 struct lttng_elf
*elf
= NULL
;
909 char *stap_note_section_data
= NULL
;
910 char *curr_note_section_begin
, *curr_data_ptr
, *curr_probe
, *curr_provider
;
912 uint32_t name_size
, desc_size
, note_type
;
913 uint64_t curr_probe_location
, curr_probe_offset
, curr_semaphore_location
;
914 uint64_t *probe_locs
= NULL
, *new_probe_locs
= NULL
;
916 if (!provider_name
|| !probe_name
|| !nb_probes
|| !offsets
) {
917 DBG("Invalid arguments.");
918 ret
= LTTNG_ERR_ELF_PARSING
;
922 elf
= lttng_elf_create(fd
);
924 DBG("Error allocation ELF.");
925 ret
= LTTNG_ERR_ELF_PARSING
;
929 /* Get the stap note section header. */
930 ret
= lttng_elf_get_section_hdr_by_name(
931 elf
, NOTE_STAPSDT_SECTION_NAME
, &stap_note_section_hdr
);
933 DBG("Cannot get ELF stap note section.");
934 goto destroy_elf_error
;
937 /* Get the data associated with the stap note section. */
938 stap_note_section_data
= lttng_elf_get_section_data(elf
, &stap_note_section_hdr
);
939 if (stap_note_section_data
== NULL
) {
940 DBG("Cannot get ELF stap note section data.");
941 ret
= LTTNG_ERR_ELF_PARSING
;
942 goto destroy_elf_error
;
945 next_note_ptr
= stap_note_section_data
;
946 curr_note_section_begin
= stap_note_section_data
;
950 curr_data_ptr
= next_note_ptr
;
951 /* Check if we have reached the end of the note section. */
952 if (curr_data_ptr
>= curr_note_section_begin
+ stap_note_section_hdr
.sh_size
) {
953 *nb_probes
= nb_match
;
954 *offsets
= probe_locs
;
958 /* Get name size field. */
959 name_size
= next_4bytes_boundary(*(uint32_t *) curr_data_ptr
);
960 curr_data_ptr
+= sizeof(uint32_t);
962 /* Sanity check; a zero name_size is reserved. */
963 if (name_size
== 0) {
964 DBG("Invalid name size field in SDT probe descriptions"
970 /* Get description size field. */
971 desc_size
= next_4bytes_boundary(*(uint32_t *) curr_data_ptr
);
972 curr_data_ptr
+= sizeof(uint32_t);
974 /* Get type field. */
975 note_type
= *(uint32_t *) curr_data_ptr
;
976 curr_data_ptr
+= sizeof(uint32_t);
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.
985 next_note_ptr
= next_note_ptr
+ (3 * sizeof(uint32_t)) + desc_size
+ name_size
;
988 * Move ptr to the end of the name string (we don't need it)
989 * and go to the next 4 byte alignement.
991 if (note_type
!= NOTE_STAPSDT_TYPE
||
992 strncmp(curr_data_ptr
, NOTE_STAPSDT_NAME
, name_size
) != 0) {
996 curr_data_ptr
+= name_size
;
998 /* Get probe location. */
999 curr_probe_location
= *(uint64_t *) curr_data_ptr
;
1000 curr_data_ptr
+= sizeof(uint64_t);
1002 /* Pass over the base. Not needed. */
1003 curr_data_ptr
+= sizeof(uint64_t);
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;
1012 /* Get probe name. */
1013 curr_probe
= curr_data_ptr
;
1015 /* Check if the provider and probe name match */
1016 if (strcmp(provider_name
, curr_provider
) == 0 &&
1017 strcmp(probe_name
, curr_probe
) == 0) {
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
1025 if (curr_semaphore_location
!= 0) {
1026 ret
= LTTNG_ERR_SDT_PROBE_SEMAPHORE
;
1030 new_size
= (++nb_match
) * sizeof(uint64_t);
1033 * Found a match with not semaphore, we need to copy the
1034 * probe_location to the output parameter.
1036 new_probe_locs
= (uint64_t *) realloc(probe_locs
, new_size
);
1037 if (!new_probe_locs
) {
1038 /* Error allocating a larger buffer */
1039 DBG("Allocation error in SDT.");
1040 ret
= LTTNG_ERR_NOMEM
;
1043 probe_locs
= new_probe_locs
;
1044 new_probe_locs
= NULL
;
1047 * Use the virtual address of the probe to compute the offset of
1048 * this probe from the beginning of the executable file.
1050 ret
= lttng_elf_convert_addr_in_text_to_offset(
1051 elf
, curr_probe_location
, &curr_probe_offset
);
1053 DBG("Conversion error in SDT.");
1057 probe_locs
[nb_match
- 1] = curr_probe_offset
;
1062 free(stap_note_section_data
);
1064 lttng_elf_destroy(elf
);