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
11 #include <common/compat/endian.h>
12 #include <common/error.h>
13 #include <common/lttng-elf.h>
14 #include <common/macros.h>
15 #include <common/readwrite.h>
22 #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
149 struct lttng_elf_ehdr
{
158 uint16_t e_phentsize
;
160 uint16_t e_shentsize
;
165 struct lttng_elf_shdr
{
174 uint64_t sh_addralign
;
179 * This struct can hold both 32bit and 64bit symbol description. It's used with
180 * the copy_sym() macro. Using this abstraction, we can use the same code for
183 struct lttng_elf_sym
{
197 /* Offset in bytes to start of section names string table. */
198 off_t section_names_offset
;
199 /* Size in bytes of section names string table. */
200 size_t section_names_size
;
201 struct lttng_elf_ehdr
*ehdr
;
205 int is_elf_32_bit(struct lttng_elf
*elf
)
207 return elf
->bitness
== ELFCLASS32
;
211 int is_elf_native_endian(struct lttng_elf
*elf
)
213 return elf
->endianness
== NATIVE_ELF_ENDIANNESS
;
217 int populate_section_header(struct lttng_elf
* elf
, struct lttng_elf_shdr
*shdr
,
223 /* Compute the offset of the section in the file */
224 offset
= (off_t
) elf
->ehdr
->e_shoff
225 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
227 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
228 PERROR("Error seeking to the beginning of ELF section header");
233 if (is_elf_32_bit(elf
)) {
236 if (lttng_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
)) < sizeof(elf_shdr
)) {
237 PERROR("Error reading ELF section header");
241 if (!is_elf_native_endian(elf
)) {
242 bswap_shdr(elf_shdr
);
244 copy_shdr(elf_shdr
, *shdr
);
248 if (lttng_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
)) < sizeof(elf_shdr
)) {
249 PERROR("Error reading ELF section header");
253 if (!is_elf_native_endian(elf
)) {
254 bswap_shdr(elf_shdr
);
256 copy_shdr(elf_shdr
, *shdr
);
264 int populate_elf_header(struct lttng_elf
*elf
)
269 * Move the read pointer back to the beginning to read the full header
270 * and copy it in our structure.
272 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
273 PERROR("Error seeking to the beginning of the file");
279 * Use macros to set fields in the ELF header struct for both 32bit and
282 if (is_elf_32_bit(elf
)) {
285 if (lttng_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
)) < sizeof(elf_ehdr
)) {
289 if (!is_elf_native_endian(elf
)) {
290 bswap_ehdr(elf_ehdr
);
292 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
296 if (lttng_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
)) < sizeof(elf_ehdr
)) {
300 if (!is_elf_native_endian(elf
)) {
301 bswap_ehdr(elf_ehdr
);
303 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
310 * Retrieve the nth (where n is the `index` argument) shdr (section
311 * header) from the given elf instance.
313 * 0 is returned on succes, -1 on failure.
316 int lttng_elf_get_section_hdr(struct lttng_elf
*elf
,
317 uint16_t index
, struct lttng_elf_shdr
*out_header
)
326 if (index
>= elf
->ehdr
->e_shnum
) {
331 ret
= populate_section_header(elf
, out_header
, index
);
333 DBG("Error populating section header.");
342 * Lookup a section's name from a given offset (usually from an shdr's
343 * sh_name value) in bytes relative to the beginning of the section
344 * names string table.
346 * If no name is found, NULL is returned.
349 char *lttng_elf_get_section_name(struct lttng_elf
*elf
, off_t offset
)
352 size_t name_length
= 0, to_read
; /* name_length does not include \0 */
358 if (offset
>= elf
->section_names_size
) {
362 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
363 PERROR("Error seeking to the beginning of ELF string table section");
367 to_read
= elf
->section_names_size
- offset
;
369 /* Find first \0 after or at current location, remember name_length. */
378 read_len
= lttng_read(elf
->fd
, buf
, std::min
<size_t>(BUF_LEN
, to_read
));
380 PERROR("Error reading ELF string table section");
383 for (i
= 0; i
< read_len
; i
++) {
384 if (buf
[i
] == '\0') {
389 name_length
+= read_len
;
394 * We found the length of the section name, now seek back to the
395 * beginning of the name and copy it in the newly allocated buffer.
397 name
= (char *)zmalloc(sizeof(char) * (name_length
+ 1)); /* + 1 for \0 */
399 PERROR("Error allocating ELF section name buffer");
402 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
403 PERROR("Error seeking to the offset of the ELF section name");
406 if (lttng_read(elf
->fd
, name
, name_length
+ 1) < name_length
+ 1) {
407 PERROR("Error reading the ELF section name");
419 int lttng_elf_validate_and_populate(struct lttng_elf
*elf
)
422 uint8_t e_ident
[EI_NIDENT
];
423 uint8_t *magic_number
= NULL
;
428 ret
= LTTNG_ERR_ELF_PARSING
;
433 * First read the magic number, endianness and version to later populate
434 * the ELF header with the correct endianness and bitness.
438 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
439 PERROR("Error seeking the beginning of ELF file");
440 ret
= LTTNG_ERR_ELF_PARSING
;
443 ret
= lttng_read(elf
->fd
, e_ident
, EI_NIDENT
);
444 if (ret
< EI_NIDENT
) {
445 DBG("Error reading the ELF identification fields");
447 PERROR("Error reading the ELF identification fields");
449 ret
= LTTNG_ERR_ELF_PARSING
;
454 * Copy fields used to check that the target file is in fact a valid ELF
457 elf
->bitness
= e_ident
[EI_CLASS
];
458 elf
->endianness
= e_ident
[EI_DATA
];
459 version
= e_ident
[EI_VERSION
];
460 magic_number
= &e_ident
[EI_MAG0
];
463 * Check the magic number.
465 if (memcmp(magic_number
, ELFMAG
, SELFMAG
) != 0) {
466 DBG("Error check ELF magic number.");
467 ret
= LTTNG_ERR_ELF_PARSING
;
472 * Check the bitness is either ELFCLASS32 or ELFCLASS64.
474 if (elf
->bitness
<= ELFCLASSNONE
|| elf
->bitness
>= ELFCLASSNUM
) {
475 DBG("ELF class error.");
476 ret
= LTTNG_ERR_ELF_PARSING
;
481 * Check the endianness is either ELFDATA2LSB or ELFDATA2MSB.
483 if (elf
->endianness
<= ELFDATANONE
|| elf
->endianness
>= ELFDATANUM
) {
484 DBG("ELF endianness error.");
485 ret
= LTTNG_ERR_ELF_PARSING
;
490 * Check the version is ELF_CURRENT.
492 if (version
<= EV_NONE
|| version
>= EV_NUM
) {
493 DBG("Wrong ELF version.");
494 ret
= LTTNG_ERR_ELF_PARSING
;
498 elf
->ehdr
= (lttng_elf_ehdr
*) zmalloc(sizeof(struct lttng_elf_ehdr
));
500 PERROR("Error allocation buffer for ELF header");
501 ret
= LTTNG_ERR_NOMEM
;
506 * Copy the content of the elf header.
508 ret
= populate_elf_header(elf
);
510 DBG("Error reading ELF header,");
524 * Create an instance of lttng_elf for the ELF file located at
527 * Return a pointer to the instance on success, NULL on failure.
530 struct lttng_elf
*lttng_elf_create(int fd
)
532 struct lttng_elf_shdr section_names_shdr
;
533 struct lttng_elf
*elf
= NULL
;
535 struct stat stat_buf
;
541 ret
= fstat(fd
, &stat_buf
);
543 PERROR("Failed to determine size of elf file");
546 if (!S_ISREG(stat_buf
.st_mode
)) {
547 ERR("Refusing to initialize lttng_elf from non-regular file");
551 elf
= (lttng_elf
*) zmalloc(sizeof(struct lttng_elf
));
553 PERROR("Error allocating struct lttng_elf");
556 elf
->file_size
= (size_t) stat_buf
.st_size
;
560 PERROR("Error duplicating file descriptor to binary");
564 ret
= lttng_elf_validate_and_populate(elf
);
569 ret
= lttng_elf_get_section_hdr(
570 elf
, elf
->ehdr
->e_shstrndx
, §ion_names_shdr
);
575 elf
->section_names_offset
= section_names_shdr
.sh_offset
;
576 elf
->section_names_size
= section_names_shdr
.sh_size
;
585 if (close(elf
->fd
)) {
586 PERROR("Error closing file descriptor in error path");
596 * Destroy the given lttng_elf instance.
599 void lttng_elf_destroy(struct lttng_elf
*elf
)
606 if (close(elf
->fd
)) {
607 PERROR("Error closing file description in error path");
614 int lttng_elf_get_section_hdr_by_name(struct lttng_elf
*elf
,
615 const char *section_name
, struct lttng_elf_shdr
*section_hdr
)
618 char *curr_section_name
;
620 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
622 int ret
= lttng_elf_get_section_hdr(elf
, i
, section_hdr
);
627 curr_section_name
= lttng_elf_get_section_name(elf
,
628 section_hdr
->sh_name
);
629 if (!curr_section_name
) {
632 name_equal
= strcmp(curr_section_name
, section_name
) == 0;
633 free(curr_section_name
);
638 return LTTNG_ERR_ELF_PARSING
;
642 char *lttng_elf_get_section_data(struct lttng_elf
*elf
,
643 struct lttng_elf_shdr
*shdr
)
646 off_t section_offset
;
648 size_t max_alloc_size
;
654 max_alloc_size
= std::min
<size_t>(MAX_SECTION_DATA_SIZE
, elf
->file_size
);
656 section_offset
= shdr
->sh_offset
;
657 if (lseek(elf
->fd
, section_offset
, SEEK_SET
) < 0) {
658 PERROR("Error seeking to section offset");
662 if (shdr
->sh_size
> max_alloc_size
) {
663 ERR("ELF section size exceeds maximal allowed size of %zu bytes",
667 data
= (char *) zmalloc(shdr
->sh_size
);
669 PERROR("Error allocating buffer for ELF section data");
672 ret
= lttng_read(elf
->fd
, data
, shdr
->sh_size
);
674 PERROR("Error reading ELF section data");
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.
691 * Returns the offset on success or non-zero in case of failure.
694 int lttng_elf_convert_addr_in_text_to_offset(struct lttng_elf
*elf_handle
,
695 size_t addr
, uint64_t *offset
)
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
;
702 struct lttng_elf_shdr text_section_hdr
;
705 DBG("Invalid ELF handle.");
706 ret
= LTTNG_ERR_ELF_PARSING
;
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
);
714 DBG("Text section not found in binary.");
715 ret
= LTTNG_ERR_ELF_PARSING
;
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
;
725 * Verify that the address is within the .text section boundaries.
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
;
735 offset_in_section
= addr
- text_section_addr_beg
;
738 * Add the target offset in the text section to the offset of this text
739 * section from the beginning of the binary file.
741 *offset
= text_section_offset
+ offset_in_section
;
748 * Compute the offset of a symbol from the begining of the ELF binary.
750 * On success, returns 0 offset parameter is set to the computed value
751 * On failure, returns -1.
753 int lttng_elf_get_symbol_offset(int fd
, char *symbol
, uint64_t *offset
)
760 char *curr_sym_str
= NULL
;
761 char *symbol_table_data
= NULL
;
762 char *string_table_data
= NULL
;
763 const char *string_table_name
= NULL
;
764 struct lttng_elf_shdr symtab_hdr
;
765 struct lttng_elf_shdr strtab_hdr
;
766 struct lttng_elf
*elf
= NULL
;
768 if (!symbol
|| !offset
) {
769 ret
= LTTNG_ERR_ELF_PARSING
;
773 elf
= lttng_elf_create(fd
);
775 ret
= LTTNG_ERR_ELF_PARSING
;
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.
785 ret
= lttng_elf_get_section_hdr_by_name(elf
, SYMBOL_TAB_SECTION_NAME
,
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
,
793 DBG("Cannot get ELF Symbol Table nor Dynamic Symbol Table sections.");
794 ret
= LTTNG_ERR_ELF_PARSING
;
797 string_table_name
= DYNAMIC_STRING_TAB_SECTION_NAME
;
799 string_table_name
= STRING_TAB_SECTION_NAME
;
802 /* Get the data associated with the symbol table section. */
803 symbol_table_data
= lttng_elf_get_section_data(elf
, &symtab_hdr
);
804 if (symbol_table_data
== NULL
) {
805 DBG("Cannot get ELF Symbol Table data.");
806 ret
= LTTNG_ERR_ELF_PARSING
;
810 /* Get the string table section header. */
811 ret
= lttng_elf_get_section_hdr_by_name(elf
, string_table_name
,
814 DBG("Cannot get ELF string table section.");
815 goto free_symbol_table_data
;
818 /* Get the data associated with the string table section. */
819 string_table_data
= lttng_elf_get_section_data(elf
, &strtab_hdr
);
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
;
826 /* Get the number of symbol in the table for the iteration. */
827 sym_count
= symtab_hdr
.sh_size
/ symtab_hdr
.sh_entsize
;
829 /* Loop over all symbol. */
830 for (sym_idx
= 0; sym_idx
< sym_count
; sym_idx
++) {
831 struct lttng_elf_sym curr_sym
;
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
);
838 Elf64_Sym tmp
= ((Elf64_Sym
*) symbol_table_data
)[sym_idx
];
839 copy_sym(tmp
, curr_sym
);
843 * If the st_name field is zero, there is no string name for
844 * this symbol; skip to the next symbol.
846 if (curr_sym
.st_name
== 0) {
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.
854 curr_sym_str
= string_table_data
+ curr_sym
.st_name
;
857 * If the current symbol is not a function; skip to the next symbol.
859 /* Both 32bit and 64bit use the same 1 byte field for type. (See elf.h) */
860 if (ELF32_ST_TYPE(curr_sym
.st_info
) != STT_FUNC
) {
865 * Compare with the search symbol. If there is a match set the address
866 * output parameter and return success.
868 if (strcmp(symbol
, curr_sym_str
) == 0 ) {
870 addr
= curr_sym
.st_value
;
876 DBG("Symbol not found.");
877 ret
= LTTNG_ERR_ELF_PARSING
;
878 goto free_string_table_data
;
882 * Use the virtual address of the symbol to compute the offset of this
883 * symbol from the beginning of the executable file.
885 ret
= lttng_elf_convert_addr_in_text_to_offset(elf
, addr
, offset
);
887 DBG("Cannot convert addr to offset.");
888 goto free_string_table_data
;
892 free_string_table_data
:
893 free(string_table_data
);
894 free_symbol_table_data
:
895 free(symbol_table_data
);
897 lttng_elf_destroy(elf
);
903 * Compute the offsets of SDT probes from the begining of the ELF binary.
905 * On success, returns 0 and the nb_probes parameter is set to the number of
906 * offsets found and the offsets parameter points to an array of offsets where
907 * the SDT probes are.
908 * On failure, returns -1.
910 int lttng_elf_get_sdt_probe_offsets(int fd
, const char *provider_name
,
911 const char *probe_name
, uint64_t **offsets
, uint32_t *nb_probes
)
913 int ret
= 0, nb_match
= 0;
914 struct lttng_elf_shdr stap_note_section_hdr
;
915 struct lttng_elf
*elf
= NULL
;
916 char *stap_note_section_data
= NULL
;
917 char *curr_note_section_begin
, *curr_data_ptr
, *curr_probe
, *curr_provider
;
919 uint32_t name_size
, desc_size
, note_type
;
920 uint64_t curr_probe_location
, curr_probe_offset
, curr_semaphore_location
;
921 uint64_t *probe_locs
= NULL
, *new_probe_locs
= NULL
;
923 if (!provider_name
|| !probe_name
|| !nb_probes
|| !offsets
) {
924 DBG("Invalid arguments.");
925 ret
= LTTNG_ERR_ELF_PARSING
;
929 elf
= lttng_elf_create(fd
);
931 DBG("Error allocation ELF.");
932 ret
= LTTNG_ERR_ELF_PARSING
;
936 /* Get the stap note section header. */
937 ret
= lttng_elf_get_section_hdr_by_name(elf
, NOTE_STAPSDT_SECTION_NAME
,
938 &stap_note_section_hdr
);
940 DBG("Cannot get ELF stap note section.");
941 goto destroy_elf_error
;
944 /* Get the data associated with the stap note section. */
945 stap_note_section_data
=
946 lttng_elf_get_section_data(elf
, &stap_note_section_hdr
);
947 if (stap_note_section_data
== NULL
) {
948 DBG("Cannot get ELF stap note section data.");
949 ret
= LTTNG_ERR_ELF_PARSING
;
950 goto destroy_elf_error
;
953 next_note_ptr
= stap_note_section_data
;
954 curr_note_section_begin
= stap_note_section_data
;
958 curr_data_ptr
= next_note_ptr
;
959 /* Check if we have reached the end of the note section. */
961 curr_note_section_begin
+
962 stap_note_section_hdr
.sh_size
) {
963 *nb_probes
= nb_match
;
964 *offsets
= probe_locs
;
968 /* Get name size field. */
969 name_size
= next_4bytes_boundary(*(uint32_t*) curr_data_ptr
);
970 curr_data_ptr
+= sizeof(uint32_t);
972 /* Sanity check; a zero name_size is reserved. */
973 if (name_size
== 0) {
974 DBG("Invalid name size field in SDT probe descriptions"
980 /* Get description size field. */
981 desc_size
= next_4bytes_boundary(*(uint32_t*) curr_data_ptr
);
982 curr_data_ptr
+= sizeof(uint32_t);
984 /* Get type field. */
985 note_type
= *(uint32_t *) curr_data_ptr
;
986 curr_data_ptr
+= sizeof(uint32_t);
989 * Move the pointer to the next note to be ready for the next
990 * iteration. The current note is made of 3 unsigned 32bit
991 * integers (name size, descriptor size and note type), the
992 * name and the descriptor. To move to the next note, we move
993 * the pointer according to those values.
995 next_note_ptr
= next_note_ptr
+
996 (3 * sizeof(uint32_t)) + desc_size
+ name_size
;
999 * Move ptr to the end of the name string (we don't need it)
1000 * and go to the next 4 byte alignement.
1002 if (note_type
!= NOTE_STAPSDT_TYPE
||
1003 strncmp(curr_data_ptr
, NOTE_STAPSDT_NAME
, name_size
) != 0) {
1007 curr_data_ptr
+= name_size
;
1009 /* Get probe location. */
1010 curr_probe_location
= *(uint64_t *) curr_data_ptr
;
1011 curr_data_ptr
+= sizeof(uint64_t);
1013 /* Pass over the base. Not needed. */
1014 curr_data_ptr
+= sizeof(uint64_t);
1016 /* Get semaphore location. */
1017 curr_semaphore_location
= *(uint64_t *) curr_data_ptr
;
1018 curr_data_ptr
+= sizeof(uint64_t);
1019 /* Get provider name. */
1020 curr_provider
= curr_data_ptr
;
1021 curr_data_ptr
+= strlen(curr_provider
) + 1;
1023 /* Get probe name. */
1024 curr_probe
= curr_data_ptr
;
1026 /* Check if the provider and probe name match */
1027 if (strcmp(provider_name
, curr_provider
) == 0 &&
1028 strcmp(probe_name
, curr_probe
) == 0) {
1032 * We currently don't support SDT probes with semaphores. Return
1033 * success as we found a matching probe but it's guarded by a
1036 if (curr_semaphore_location
!= 0) {
1037 ret
= LTTNG_ERR_SDT_PROBE_SEMAPHORE
;
1041 new_size
= (++nb_match
) * sizeof(uint64_t);
1044 * Found a match with not semaphore, we need to copy the
1045 * probe_location to the output parameter.
1047 new_probe_locs
= (uint64_t *) realloc(probe_locs
, new_size
);
1048 if (!new_probe_locs
) {
1049 /* Error allocating a larger buffer */
1050 DBG("Allocation error in SDT.");
1051 ret
= LTTNG_ERR_NOMEM
;
1054 probe_locs
= new_probe_locs
;
1055 new_probe_locs
= NULL
;
1058 * Use the virtual address of the probe to compute the offset of
1059 * this probe from the beginning of the executable file.
1061 ret
= lttng_elf_convert_addr_in_text_to_offset(elf
,
1062 curr_probe_location
, &curr_probe_offset
);
1064 DBG("Conversion error in SDT.");
1068 probe_locs
[nb_match
- 1] = curr_probe_offset
;
1073 free(stap_note_section_data
);
1075 lttng_elf_destroy(elf
);