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.hpp>
12 #include <common/error.hpp>
13 #include <common/lttng-elf.hpp>
14 #include <common/macros.hpp>
15 #include <common/readwrite.hpp>
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
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
;
207 int is_elf_32_bit(struct lttng_elf
*elf
)
209 return elf
->bitness
== ELFCLASS32
;
213 int is_elf_native_endian(struct lttng_elf
*elf
)
215 return elf
->endianness
== NATIVE_ELF_ENDIANNESS
;
219 int populate_section_header(struct lttng_elf
* elf
, struct lttng_elf_shdr
*shdr
,
225 /* Compute the offset of the section in the file */
226 offset
= (off_t
) elf
->ehdr
->e_shoff
227 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
229 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
230 PERROR("Error seeking to the beginning of ELF section header");
235 if (is_elf_32_bit(elf
)) {
238 if (lttng_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
)) < sizeof(elf_shdr
)) {
239 PERROR("Error reading ELF section header");
243 if (!is_elf_native_endian(elf
)) {
244 bswap_shdr(elf_shdr
);
246 copy_shdr(elf_shdr
, *shdr
);
250 if (lttng_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
)) < sizeof(elf_shdr
)) {
251 PERROR("Error reading ELF section header");
255 if (!is_elf_native_endian(elf
)) {
256 bswap_shdr(elf_shdr
);
258 copy_shdr(elf_shdr
, *shdr
);
266 int populate_elf_header(struct lttng_elf
*elf
)
271 * Move the read pointer back to the beginning to read the full header
272 * and copy it in our structure.
274 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
275 PERROR("Error seeking to the beginning of the file");
281 * Use macros to set fields in the ELF header struct for both 32bit and
284 if (is_elf_32_bit(elf
)) {
287 if (lttng_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
)) < sizeof(elf_ehdr
)) {
291 if (!is_elf_native_endian(elf
)) {
292 bswap_ehdr(elf_ehdr
);
294 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
298 if (lttng_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
)) < sizeof(elf_ehdr
)) {
302 if (!is_elf_native_endian(elf
)) {
303 bswap_ehdr(elf_ehdr
);
305 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
312 * Retrieve the nth (where n is the `index` argument) shdr (section
313 * header) from the given elf instance.
315 * 0 is returned on succes, -1 on failure.
318 int lttng_elf_get_section_hdr(struct lttng_elf
*elf
,
319 uint16_t index
, struct lttng_elf_shdr
*out_header
)
328 if (index
>= elf
->ehdr
->e_shnum
) {
333 ret
= populate_section_header(elf
, out_header
, index
);
335 DBG("Error populating section header.");
344 * Lookup a section's name from a given offset (usually from an shdr's
345 * sh_name value) in bytes relative to the beginning of the section
346 * names string table.
348 * If no name is found, NULL is returned.
351 char *lttng_elf_get_section_name(struct lttng_elf
*elf
, off_t offset
)
354 size_t name_length
= 0, to_read
; /* name_length does not include \0 */
360 if (offset
>= elf
->section_names_size
) {
364 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
365 PERROR("Error seeking to the beginning of ELF string table section");
369 to_read
= elf
->section_names_size
- offset
;
371 /* Find first \0 after or at current location, remember name_length. */
380 read_len
= lttng_read(elf
->fd
, buf
, std::min
<size_t>(BUF_LEN
, to_read
));
382 PERROR("Error reading ELF string table section");
385 for (i
= 0; i
< read_len
; i
++) {
386 if (buf
[i
] == '\0') {
391 name_length
+= read_len
;
396 * We found the length of the section name, now seek back to the
397 * beginning of the name and copy it in the newly allocated buffer.
399 name
= calloc
<char>((name_length
+ 1)); /* + 1 for \0 */
401 PERROR("Error allocating ELF section name buffer");
404 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
405 PERROR("Error seeking to the offset of the ELF section name");
408 if (lttng_read(elf
->fd
, name
, name_length
+ 1) < name_length
+ 1) {
409 PERROR("Error reading the ELF section name");
421 int lttng_elf_validate_and_populate(struct lttng_elf
*elf
)
424 uint8_t e_ident
[EI_NIDENT
];
425 uint8_t *magic_number
= NULL
;
430 ret
= LTTNG_ERR_ELF_PARSING
;
435 * First read the magic number, endianness and version to later populate
436 * the ELF header with the correct endianness and bitness.
440 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
441 PERROR("Error seeking the beginning of ELF file");
442 ret
= LTTNG_ERR_ELF_PARSING
;
445 ret
= lttng_read(elf
->fd
, e_ident
, EI_NIDENT
);
446 if (ret
< EI_NIDENT
) {
447 DBG("Error reading the ELF identification fields");
449 PERROR("Error reading the ELF identification fields");
451 ret
= LTTNG_ERR_ELF_PARSING
;
456 * Copy fields used to check that the target file is in fact a valid ELF
459 elf
->bitness
= e_ident
[EI_CLASS
];
460 elf
->endianness
= e_ident
[EI_DATA
];
461 version
= e_ident
[EI_VERSION
];
462 magic_number
= &e_ident
[EI_MAG0
];
465 * Check the magic number.
467 if (memcmp(magic_number
, ELFMAG
, SELFMAG
) != 0) {
468 DBG("Error check ELF magic number.");
469 ret
= LTTNG_ERR_ELF_PARSING
;
474 * Check the bitness is either ELFCLASS32 or ELFCLASS64.
476 if (elf
->bitness
<= ELFCLASSNONE
|| elf
->bitness
>= ELFCLASSNUM
) {
477 DBG("ELF class error.");
478 ret
= LTTNG_ERR_ELF_PARSING
;
483 * Check the endianness is either ELFDATA2LSB or ELFDATA2MSB.
485 if (elf
->endianness
<= ELFDATANONE
|| elf
->endianness
>= ELFDATANUM
) {
486 DBG("ELF endianness error.");
487 ret
= LTTNG_ERR_ELF_PARSING
;
492 * Check the version is ELF_CURRENT.
494 if (version
<= EV_NONE
|| version
>= EV_NUM
) {
495 DBG("Wrong ELF version.");
496 ret
= LTTNG_ERR_ELF_PARSING
;
500 elf
->ehdr
= zmalloc
<lttng_elf_ehdr
>();
502 PERROR("Error allocation buffer for ELF header");
503 ret
= LTTNG_ERR_NOMEM
;
508 * Copy the content of the elf header.
510 ret
= populate_elf_header(elf
);
512 DBG("Error reading ELF header,");
526 * Create an instance of lttng_elf for the ELF file located at
529 * Return a pointer to the instance on success, NULL on failure.
532 struct lttng_elf
*lttng_elf_create(int fd
)
534 struct lttng_elf_shdr section_names_shdr
;
535 struct lttng_elf
*elf
= NULL
;
537 struct stat stat_buf
;
543 ret
= fstat(fd
, &stat_buf
);
545 PERROR("Failed to determine size of elf file");
548 if (!S_ISREG(stat_buf
.st_mode
)) {
549 ERR("Refusing to initialize lttng_elf from non-regular file");
553 elf
= zmalloc
<lttng_elf
>();
555 PERROR("Error allocating struct lttng_elf");
558 elf
->file_size
= (size_t) stat_buf
.st_size
;
562 PERROR("Error duplicating file descriptor to binary");
566 ret
= lttng_elf_validate_and_populate(elf
);
571 ret
= lttng_elf_get_section_hdr(
572 elf
, elf
->ehdr
->e_shstrndx
, §ion_names_shdr
);
577 elf
->section_names_offset
= section_names_shdr
.sh_offset
;
578 elf
->section_names_size
= section_names_shdr
.sh_size
;
587 if (close(elf
->fd
)) {
588 PERROR("Error closing file descriptor in error path");
598 * Destroy the given lttng_elf instance.
601 void lttng_elf_destroy(struct lttng_elf
*elf
)
608 if (close(elf
->fd
)) {
609 PERROR("Error closing file description in error path");
616 int lttng_elf_get_section_hdr_by_name(struct lttng_elf
*elf
,
617 const char *section_name
, struct lttng_elf_shdr
*section_hdr
)
620 char *curr_section_name
;
622 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
624 int ret
= lttng_elf_get_section_hdr(elf
, i
, section_hdr
);
629 curr_section_name
= lttng_elf_get_section_name(elf
,
630 section_hdr
->sh_name
);
631 if (!curr_section_name
) {
634 name_equal
= strcmp(curr_section_name
, section_name
) == 0;
635 free(curr_section_name
);
640 return LTTNG_ERR_ELF_PARSING
;
644 char *lttng_elf_get_section_data(struct lttng_elf
*elf
,
645 struct lttng_elf_shdr
*shdr
)
648 off_t section_offset
;
650 size_t max_alloc_size
;
656 max_alloc_size
= std::min
<size_t>(MAX_SECTION_DATA_SIZE
, elf
->file_size
);
658 section_offset
= shdr
->sh_offset
;
659 if (lseek(elf
->fd
, section_offset
, SEEK_SET
) < 0) {
660 PERROR("Error seeking to section offset");
664 if (shdr
->sh_size
> max_alloc_size
) {
665 ERR("ELF section size exceeds maximal allowed size of %zu bytes",
669 data
= calloc
<char>(shdr
->sh_size
);
671 PERROR("Error allocating buffer for ELF section data");
674 ret
= lttng_read(elf
->fd
, data
, shdr
->sh_size
);
676 PERROR("Error reading ELF section data");
689 * Convert the virtual address in a binary's mapping to the offset of
690 * the corresponding instruction in the binary file.
691 * This function assumes the address is in the text section.
693 * Returns the offset on success or non-zero in case of failure.
696 int lttng_elf_convert_addr_in_text_to_offset(struct lttng_elf
*elf_handle
,
697 size_t addr
, uint64_t *offset
)
700 off_t text_section_offset
;
701 off_t text_section_addr_beg
;
702 off_t text_section_addr_end
;
703 off_t offset_in_section
;
704 struct lttng_elf_shdr text_section_hdr
;
707 DBG("Invalid ELF handle.");
708 ret
= LTTNG_ERR_ELF_PARSING
;
712 /* Get a pointer to the .text section header. */
713 ret
= lttng_elf_get_section_hdr_by_name(elf_handle
,
714 TEXT_SECTION_NAME
, &text_section_hdr
);
716 DBG("Text section not found in binary.");
717 ret
= LTTNG_ERR_ELF_PARSING
;
721 text_section_offset
= text_section_hdr
.sh_offset
;
722 text_section_addr_beg
= text_section_hdr
.sh_addr
;
723 text_section_addr_end
=
724 text_section_addr_beg
+ text_section_hdr
.sh_size
;
727 * Verify that the address is within the .text section boundaries.
729 if (addr
< text_section_addr_beg
|| addr
> text_section_addr_end
) {
730 DBG("Address found is outside of the .text section addr=0x%zx, "
731 ".text section=[0x%jd - 0x%jd].", addr
, (intmax_t)text_section_addr_beg
,
732 (intmax_t)text_section_addr_end
);
733 ret
= LTTNG_ERR_ELF_PARSING
;
737 offset_in_section
= addr
- text_section_addr_beg
;
740 * Add the target offset in the text section to the offset of this text
741 * section from the beginning of the binary file.
743 *offset
= text_section_offset
+ offset_in_section
;
750 * Compute the offset of a symbol from the begining of the ELF binary.
752 * On success, returns 0 offset parameter is set to the computed value
753 * On failure, returns -1.
755 int lttng_elf_get_symbol_offset(int fd
, char *symbol
, uint64_t *offset
)
762 char *curr_sym_str
= NULL
;
763 char *symbol_table_data
= NULL
;
764 char *string_table_data
= NULL
;
765 const char *string_table_name
= NULL
;
766 struct lttng_elf_shdr symtab_hdr
;
767 struct lttng_elf_shdr strtab_hdr
;
768 struct lttng_elf
*elf
= NULL
;
770 if (!symbol
|| !offset
) {
771 ret
= LTTNG_ERR_ELF_PARSING
;
775 elf
= lttng_elf_create(fd
);
777 ret
= LTTNG_ERR_ELF_PARSING
;
782 * The .symtab section might not exist on stripped binaries.
783 * Try to get the symbol table section header first. If it's absent,
784 * try to get the dynamic symbol table. All symbols in the dynamic
785 * symbol tab are in the (normal) symbol table if it exists.
787 ret
= lttng_elf_get_section_hdr_by_name(elf
, SYMBOL_TAB_SECTION_NAME
,
790 DBG("Cannot get ELF Symbol Table section. Trying to get ELF Dynamic Symbol Table section.");
791 /* Get the dynamic symbol table section header. */
792 ret
= lttng_elf_get_section_hdr_by_name(elf
, DYNAMIC_SYMBOL_TAB_SECTION_NAME
,
795 DBG("Cannot get ELF Symbol Table nor Dynamic Symbol Table sections.");
796 ret
= LTTNG_ERR_ELF_PARSING
;
799 string_table_name
= DYNAMIC_STRING_TAB_SECTION_NAME
;
801 string_table_name
= STRING_TAB_SECTION_NAME
;
804 /* Get the data associated with the symbol table section. */
805 symbol_table_data
= lttng_elf_get_section_data(elf
, &symtab_hdr
);
806 if (symbol_table_data
== NULL
) {
807 DBG("Cannot get ELF Symbol Table data.");
808 ret
= LTTNG_ERR_ELF_PARSING
;
812 /* Get the string table section header. */
813 ret
= lttng_elf_get_section_hdr_by_name(elf
, string_table_name
,
816 DBG("Cannot get ELF string table section.");
817 goto free_symbol_table_data
;
820 /* Get the data associated with the string table section. */
821 string_table_data
= lttng_elf_get_section_data(elf
, &strtab_hdr
);
822 if (string_table_data
== NULL
) {
823 DBG("Cannot get ELF string table section data.");
824 ret
= LTTNG_ERR_ELF_PARSING
;
825 goto free_symbol_table_data
;
828 /* Get the number of symbol in the table for the iteration. */
829 if (symtab_hdr
.sh_entsize
== 0) {
830 DBG("Invalid ELF string table entry size.");
831 ret
= LTTNG_ERR_ELF_PARSING
;
832 goto free_string_table_data
;
835 sym_count
= symtab_hdr
.sh_size
/ symtab_hdr
.sh_entsize
;
837 /* Loop over all symbol. */
838 for (sym_idx
= 0; sym_idx
< sym_count
; sym_idx
++) {
839 struct lttng_elf_sym curr_sym
;
841 /* Get the symbol at the current index. */
842 if (is_elf_32_bit(elf
)) {
843 Elf32_Sym tmp
= ((Elf32_Sym
*) symbol_table_data
)[sym_idx
];
844 copy_sym(tmp
, curr_sym
);
846 Elf64_Sym tmp
= ((Elf64_Sym
*) symbol_table_data
)[sym_idx
];
847 copy_sym(tmp
, curr_sym
);
851 * If the st_name field is zero, there is no string name for
852 * this symbol; skip to the next symbol.
854 if (curr_sym
.st_name
== 0) {
859 * Use the st_name field in the lttng_elf_sym struct to get offset of
860 * the symbol's name from the beginning of the string table.
862 curr_sym_str
= string_table_data
+ curr_sym
.st_name
;
865 * If the current symbol is not a function; skip to the next symbol.
867 /* Both 32bit and 64bit use the same 1 byte field for type. (See elf.h) */
868 if (ELF32_ST_TYPE(curr_sym
.st_info
) != STT_FUNC
) {
873 * Compare with the search symbol. If there is a match set the address
874 * output parameter and return success.
876 if (strcmp(symbol
, curr_sym_str
) == 0 ) {
878 addr
= curr_sym
.st_value
;
884 DBG("Symbol not found.");
885 ret
= LTTNG_ERR_ELF_PARSING
;
886 goto free_string_table_data
;
890 * Use the virtual address of the symbol to compute the offset of this
891 * symbol from the beginning of the executable file.
893 ret
= lttng_elf_convert_addr_in_text_to_offset(elf
, addr
, offset
);
895 DBG("Cannot convert addr to offset.");
896 goto free_string_table_data
;
900 free_string_table_data
:
901 free(string_table_data
);
902 free_symbol_table_data
:
903 free(symbol_table_data
);
905 lttng_elf_destroy(elf
);
911 * Compute the offsets of SDT probes from the begining of the ELF binary.
913 * On success, returns 0 and the nb_probes parameter is set to the number of
914 * offsets found and the offsets parameter points to an array of offsets where
915 * the SDT probes are.
916 * On failure, returns -1.
918 int lttng_elf_get_sdt_probe_offsets(int fd
, const char *provider_name
,
919 const char *probe_name
, uint64_t **offsets
, uint32_t *nb_probes
)
921 int ret
= 0, nb_match
= 0;
922 struct lttng_elf_shdr stap_note_section_hdr
;
923 struct lttng_elf
*elf
= NULL
;
924 char *stap_note_section_data
= NULL
;
925 char *curr_note_section_begin
, *curr_data_ptr
, *curr_probe
, *curr_provider
;
927 uint32_t name_size
, desc_size
, note_type
;
928 uint64_t curr_probe_location
, curr_probe_offset
, curr_semaphore_location
;
929 uint64_t *probe_locs
= NULL
, *new_probe_locs
= NULL
;
931 if (!provider_name
|| !probe_name
|| !nb_probes
|| !offsets
) {
932 DBG("Invalid arguments.");
933 ret
= LTTNG_ERR_ELF_PARSING
;
937 elf
= lttng_elf_create(fd
);
939 DBG("Error allocation ELF.");
940 ret
= LTTNG_ERR_ELF_PARSING
;
944 /* Get the stap note section header. */
945 ret
= lttng_elf_get_section_hdr_by_name(elf
, NOTE_STAPSDT_SECTION_NAME
,
946 &stap_note_section_hdr
);
948 DBG("Cannot get ELF stap note section.");
949 goto destroy_elf_error
;
952 /* Get the data associated with the stap note section. */
953 stap_note_section_data
=
954 lttng_elf_get_section_data(elf
, &stap_note_section_hdr
);
955 if (stap_note_section_data
== NULL
) {
956 DBG("Cannot get ELF stap note section data.");
957 ret
= LTTNG_ERR_ELF_PARSING
;
958 goto destroy_elf_error
;
961 next_note_ptr
= stap_note_section_data
;
962 curr_note_section_begin
= stap_note_section_data
;
966 curr_data_ptr
= next_note_ptr
;
967 /* Check if we have reached the end of the note section. */
969 curr_note_section_begin
+
970 stap_note_section_hdr
.sh_size
) {
971 *nb_probes
= nb_match
;
972 *offsets
= probe_locs
;
976 /* Get name size field. */
977 name_size
= next_4bytes_boundary(*(uint32_t*) curr_data_ptr
);
978 curr_data_ptr
+= sizeof(uint32_t);
980 /* Sanity check; a zero name_size is reserved. */
981 if (name_size
== 0) {
982 DBG("Invalid name size field in SDT probe descriptions"
988 /* Get description size field. */
989 desc_size
= next_4bytes_boundary(*(uint32_t*) curr_data_ptr
);
990 curr_data_ptr
+= sizeof(uint32_t);
992 /* Get type field. */
993 note_type
= *(uint32_t *) curr_data_ptr
;
994 curr_data_ptr
+= sizeof(uint32_t);
997 * Move the pointer to the next note to be ready for the next
998 * iteration. The current note is made of 3 unsigned 32bit
999 * integers (name size, descriptor size and note type), the
1000 * name and the descriptor. To move to the next note, we move
1001 * the pointer according to those values.
1003 next_note_ptr
= next_note_ptr
+
1004 (3 * sizeof(uint32_t)) + desc_size
+ name_size
;
1007 * Move ptr to the end of the name string (we don't need it)
1008 * and go to the next 4 byte alignement.
1010 if (note_type
!= NOTE_STAPSDT_TYPE
||
1011 strncmp(curr_data_ptr
, NOTE_STAPSDT_NAME
, name_size
) != 0) {
1015 curr_data_ptr
+= name_size
;
1017 /* Get probe location. */
1018 curr_probe_location
= *(uint64_t *) curr_data_ptr
;
1019 curr_data_ptr
+= sizeof(uint64_t);
1021 /* Pass over the base. Not needed. */
1022 curr_data_ptr
+= sizeof(uint64_t);
1024 /* Get semaphore location. */
1025 curr_semaphore_location
= *(uint64_t *) curr_data_ptr
;
1026 curr_data_ptr
+= sizeof(uint64_t);
1027 /* Get provider name. */
1028 curr_provider
= curr_data_ptr
;
1029 curr_data_ptr
+= strlen(curr_provider
) + 1;
1031 /* Get probe name. */
1032 curr_probe
= curr_data_ptr
;
1034 /* Check if the provider and probe name match */
1035 if (strcmp(provider_name
, curr_provider
) == 0 &&
1036 strcmp(probe_name
, curr_probe
) == 0) {
1040 * We currently don't support SDT probes with semaphores. Return
1041 * success as we found a matching probe but it's guarded by a
1044 if (curr_semaphore_location
!= 0) {
1045 ret
= LTTNG_ERR_SDT_PROBE_SEMAPHORE
;
1049 new_size
= (++nb_match
) * sizeof(uint64_t);
1052 * Found a match with not semaphore, we need to copy the
1053 * probe_location to the output parameter.
1055 new_probe_locs
= (uint64_t *) realloc(probe_locs
, new_size
);
1056 if (!new_probe_locs
) {
1057 /* Error allocating a larger buffer */
1058 DBG("Allocation error in SDT.");
1059 ret
= LTTNG_ERR_NOMEM
;
1062 probe_locs
= new_probe_locs
;
1063 new_probe_locs
= NULL
;
1066 * Use the virtual address of the probe to compute the offset of
1067 * this probe from the beginning of the executable file.
1069 ret
= lttng_elf_convert_addr_in_text_to_offset(elf
,
1070 curr_probe_location
, &curr_probe_offset
);
1072 DBG("Conversion error in SDT.");
1076 probe_locs
[nb_match
- 1] = curr_probe_offset
;
1081 free(stap_note_section_data
);
1083 lttng_elf_destroy(elf
);