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.h>
11 #include <common/error.h>
12 #include <common/lttng-elf.h>
13 #include <common/macros.h>
14 #include <common/readwrite.h>
21 #include <sys/types.h>
27 #define TEXT_SECTION_NAME ".text"
28 #define SYMBOL_TAB_SECTION_NAME ".symtab"
29 #define STRING_TAB_SECTION_NAME ".strtab"
30 #define DYNAMIC_SYMBOL_TAB_SECTION_NAME ".dynsym"
31 #define DYNAMIC_STRING_TAB_SECTION_NAME ".dynstr"
32 #define NOTE_STAPSDT_SECTION_NAME ".note.stapsdt"
33 #define NOTE_STAPSDT_NAME "stapsdt"
34 #define NOTE_STAPSDT_TYPE 3
35 #define MAX_SECTION_DATA_SIZE 512 * 1024 * 1024
37 #if BYTE_ORDER == LITTLE_ENDIAN
38 #define NATIVE_ELF_ENDIANNESS ELFDATA2LSB
40 #define NATIVE_ELF_ENDIANNESS ELFDATA2MSB
43 #define next_4bytes_boundary(x) (typeof(x)) ((((uint64_t)x) + 3) & ~0x03)
47 switch (sizeof(x)) { \
49 x = be64toh((uint64_t)x); \
52 x = be32toh((uint32_t)x); \
55 x = be16toh((uint16_t)x); \
64 #define bswap_shdr(shdr) \
66 bswap((shdr).sh_name); \
67 bswap((shdr).sh_type); \
68 bswap((shdr).sh_flags); \
69 bswap((shdr).sh_addr); \
70 bswap((shdr).sh_offset); \
71 bswap((shdr).sh_size); \
72 bswap((shdr).sh_link); \
73 bswap((shdr).sh_info); \
74 bswap((shdr).sh_addralign); \
75 bswap((shdr).sh_entsize); \
78 #define bswap_ehdr(ehdr) \
80 bswap((ehdr).e_type); \
81 bswap((ehdr).e_machine); \
82 bswap((ehdr).e_version); \
83 bswap((ehdr).e_entry); \
84 bswap((ehdr).e_phoff); \
85 bswap((ehdr).e_shoff); \
86 bswap((ehdr).e_flags); \
87 bswap((ehdr).e_ehsize); \
88 bswap((ehdr).e_phentsize); \
89 bswap((ehdr).e_phnum); \
90 bswap((ehdr).e_shentsize); \
91 bswap((ehdr).e_shnum); \
92 bswap((ehdr).e_shstrndx); \
95 #define copy_shdr(src_shdr, dst_shdr) \
97 (dst_shdr).sh_name = (src_shdr).sh_name; \
98 (dst_shdr).sh_type = (src_shdr).sh_type; \
99 (dst_shdr).sh_flags = (src_shdr).sh_flags; \
100 (dst_shdr).sh_addr = (src_shdr).sh_addr; \
101 (dst_shdr).sh_offset = (src_shdr).sh_offset; \
102 (dst_shdr).sh_size = (src_shdr).sh_size; \
103 (dst_shdr).sh_link = (src_shdr).sh_link; \
104 (dst_shdr).sh_info = (src_shdr).sh_info; \
105 (dst_shdr).sh_addralign = (src_shdr).sh_addralign; \
106 (dst_shdr).sh_entsize = (src_shdr).sh_entsize; \
109 #define copy_ehdr(src_ehdr, dst_ehdr) \
111 (dst_ehdr).e_type = (src_ehdr).e_type; \
112 (dst_ehdr).e_machine = (src_ehdr).e_machine; \
113 (dst_ehdr).e_version = (src_ehdr).e_version; \
114 (dst_ehdr).e_entry = (src_ehdr).e_entry; \
115 (dst_ehdr).e_phoff = (src_ehdr).e_phoff; \
116 (dst_ehdr).e_shoff = (src_ehdr).e_shoff; \
117 (dst_ehdr).e_flags = (src_ehdr).e_flags; \
118 (dst_ehdr).e_ehsize = (src_ehdr).e_ehsize; \
119 (dst_ehdr).e_phentsize = (src_ehdr).e_phentsize; \
120 (dst_ehdr).e_phnum = (src_ehdr).e_phnum; \
121 (dst_ehdr).e_shentsize = (src_ehdr).e_shentsize; \
122 (dst_ehdr).e_shnum = (src_ehdr).e_shnum; \
123 (dst_ehdr).e_shstrndx = (src_ehdr).e_shstrndx; \
126 #define copy_sym(src_sym, dst_sym) \
128 dst_sym.st_name = src_sym.st_name; \
129 dst_sym.st_info = src_sym.st_info; \
130 dst_sym.st_other = src_sym.st_other; \
131 dst_sym.st_shndx = src_sym.st_shndx; \
132 dst_sym.st_value = src_sym.st_value; \
133 dst_sym.st_size = src_sym.st_size; \
136 /* Both 32bit and 64bit use the same 1 byte field for type. (See elf.h) */
137 #define ELF_ST_TYPE(val) ELF32_ST_TYPE(val)
139 struct lttng_elf_ehdr
{
148 uint16_t e_phentsize
;
150 uint16_t e_shentsize
;
155 struct lttng_elf_shdr
{
164 uint64_t sh_addralign
;
169 * This struct can hold both 32bit and 64bit symbol description. It's used with
170 * the copy_sym() macro. Using this abstraction, we can use the same code for
173 struct lttng_elf_sym
{
187 /* Offset in bytes to start of section names string table. */
188 off_t section_names_offset
;
189 /* Size in bytes of section names string table. */
190 size_t section_names_size
;
191 struct lttng_elf_ehdr
*ehdr
;
195 int is_elf_32_bit(struct lttng_elf
*elf
)
197 return elf
->bitness
== ELFCLASS32
;
201 int is_elf_native_endian(struct lttng_elf
*elf
)
203 return elf
->endianness
== NATIVE_ELF_ENDIANNESS
;
207 int populate_section_header(struct lttng_elf
* elf
, struct lttng_elf_shdr
*shdr
,
213 /* Compute the offset of the section in the file */
214 offset
= (off_t
) elf
->ehdr
->e_shoff
215 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
217 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
218 PERROR("Error seeking to the beginning of ELF section header");
223 if (is_elf_32_bit(elf
)) {
226 if (lttng_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
)) < sizeof(elf_shdr
)) {
227 PERROR("Error reading ELF section header");
231 if (!is_elf_native_endian(elf
)) {
232 bswap_shdr(elf_shdr
);
234 copy_shdr(elf_shdr
, *shdr
);
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
);
254 int populate_elf_header(struct lttng_elf
*elf
)
259 * Move the read pointer back to the beginning to read the full header
260 * and copy it in our structure.
262 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
263 PERROR("Error seeking to the beginning of the file");
269 * Use macros to set fields in the ELF header struct for both 32bit and
272 if (is_elf_32_bit(elf
)) {
275 if (lttng_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
)) < sizeof(elf_ehdr
)) {
279 if (!is_elf_native_endian(elf
)) {
280 bswap_ehdr(elf_ehdr
);
282 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
286 if (lttng_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
)) < sizeof(elf_ehdr
)) {
290 if (!is_elf_native_endian(elf
)) {
291 bswap_ehdr(elf_ehdr
);
293 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
300 * Retrieve the nth (where n is the `index` argument) shdr (section
301 * header) from the given elf instance.
303 * 0 is returned on succes, -1 on failure.
306 int lttng_elf_get_section_hdr(struct lttng_elf
*elf
,
307 uint16_t index
, struct lttng_elf_shdr
*out_header
)
316 if (index
>= elf
->ehdr
->e_shnum
) {
321 ret
= populate_section_header(elf
, out_header
, index
);
323 DBG("Error populating section header.");
332 * Lookup a section's name from a given offset (usually from an shdr's
333 * sh_name value) in bytes relative to the beginning of the section
334 * names string table.
336 * If no name is found, NULL is returned.
339 char *lttng_elf_get_section_name(struct lttng_elf
*elf
, off_t offset
)
342 size_t name_length
= 0, to_read
; /* name_length does not include \0 */
348 if (offset
>= elf
->section_names_size
) {
352 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
353 PERROR("Error seeking to the beginning of ELF string table section");
357 to_read
= elf
->section_names_size
- offset
;
359 /* Find first \0 after or at current location, remember name_length. */
368 read_len
= lttng_read(elf
->fd
, buf
, min_t(size_t, BUF_LEN
, to_read
));
370 PERROR("Error reading ELF string table section");
373 for (i
= 0; i
< read_len
; i
++) {
374 if (buf
[i
] == '\0') {
379 name_length
+= read_len
;
384 * We found the length of the section name, now seek back to the
385 * beginning of the name and copy it in the newly allocated buffer.
387 name
= zmalloc(sizeof(char) * (name_length
+ 1)); /* + 1 for \0 */
389 PERROR("Error allocating ELF section name buffer");
392 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
393 PERROR("Error seeking to the offset of the ELF section name");
396 if (lttng_read(elf
->fd
, name
, name_length
+ 1) < name_length
+ 1) {
397 PERROR("Error reading the ELF section name");
409 int lttng_elf_validate_and_populate(struct lttng_elf
*elf
)
412 uint8_t e_ident
[EI_NIDENT
];
413 uint8_t *magic_number
= NULL
;
418 ret
= LTTNG_ERR_ELF_PARSING
;
423 * First read the magic number, endianness and version to later populate
424 * the ELF header with the correct endianness and bitness.
428 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
429 PERROR("Error seeking the beginning of ELF file");
430 ret
= LTTNG_ERR_ELF_PARSING
;
433 ret
= lttng_read(elf
->fd
, e_ident
, EI_NIDENT
);
434 if (ret
< EI_NIDENT
) {
435 DBG("Error reading the ELF identification fields");
437 PERROR("Error reading the ELF identification fields");
439 ret
= LTTNG_ERR_ELF_PARSING
;
444 * Copy fields used to check that the target file is in fact a valid ELF
447 elf
->bitness
= e_ident
[EI_CLASS
];
448 elf
->endianness
= e_ident
[EI_DATA
];
449 version
= e_ident
[EI_VERSION
];
450 magic_number
= &e_ident
[EI_MAG0
];
453 * Check the magic number.
455 if (memcmp(magic_number
, ELFMAG
, SELFMAG
) != 0) {
456 DBG("Error check ELF magic number.");
457 ret
= LTTNG_ERR_ELF_PARSING
;
462 * Check the bitness is either ELFCLASS32 or ELFCLASS64.
464 if (elf
->bitness
<= ELFCLASSNONE
|| elf
->bitness
>= ELFCLASSNUM
) {
465 DBG("ELF class error.");
466 ret
= LTTNG_ERR_ELF_PARSING
;
471 * Check the endianness is either ELFDATA2LSB or ELFDATA2MSB.
473 if (elf
->endianness
<= ELFDATANONE
|| elf
->endianness
>= ELFDATANUM
) {
474 DBG("ELF endianness error.");
475 ret
= LTTNG_ERR_ELF_PARSING
;
480 * Check the version is ELF_CURRENT.
482 if (version
<= EV_NONE
|| version
>= EV_NUM
) {
483 DBG("Wrong ELF version.");
484 ret
= LTTNG_ERR_ELF_PARSING
;
488 elf
->ehdr
= zmalloc(sizeof(struct lttng_elf_ehdr
));
490 PERROR("Error allocation buffer for ELF header");
491 ret
= LTTNG_ERR_NOMEM
;
496 * Copy the content of the elf header.
498 ret
= populate_elf_header(elf
);
500 DBG("Error reading ELF header,");
514 * Create an instance of lttng_elf for the ELF file located at
517 * Return a pointer to the instance on success, NULL on failure.
520 struct lttng_elf
*lttng_elf_create(int fd
)
522 struct lttng_elf_shdr section_names_shdr
;
523 struct lttng_elf
*elf
= NULL
;
525 struct stat stat_buf
;
531 ret
= fstat(fd
, &stat_buf
);
533 PERROR("Failed to determine size of elf file");
536 if (!S_ISREG(stat_buf
.st_mode
)) {
537 ERR("Refusing to initialize lttng_elf from non-regular file");
541 elf
= zmalloc(sizeof(struct lttng_elf
));
543 PERROR("Error allocating struct lttng_elf");
546 elf
->file_size
= (size_t) stat_buf
.st_size
;
550 PERROR("Error duplicating file descriptor to binary");
554 ret
= lttng_elf_validate_and_populate(elf
);
559 ret
= lttng_elf_get_section_hdr(
560 elf
, elf
->ehdr
->e_shstrndx
, §ion_names_shdr
);
565 elf
->section_names_offset
= section_names_shdr
.sh_offset
;
566 elf
->section_names_size
= section_names_shdr
.sh_size
;
575 if (close(elf
->fd
)) {
576 PERROR("Error closing file descriptor in error path");
586 * Destroy the given lttng_elf instance.
589 void lttng_elf_destroy(struct lttng_elf
*elf
)
596 if (close(elf
->fd
)) {
597 PERROR("Error closing file description in error path");
604 int lttng_elf_get_section_hdr_by_name(struct lttng_elf
*elf
,
605 const char *section_name
, struct lttng_elf_shdr
*section_hdr
)
608 char *curr_section_name
;
610 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
612 int ret
= lttng_elf_get_section_hdr(elf
, i
, section_hdr
);
617 curr_section_name
= lttng_elf_get_section_name(elf
,
618 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
;
632 char *lttng_elf_get_section_data(struct lttng_elf
*elf
,
633 struct lttng_elf_shdr
*shdr
)
636 off_t section_offset
;
638 size_t max_alloc_size
;
644 max_alloc_size
= min_t(size_t, MAX_SECTION_DATA_SIZE
, elf
->file_size
);
646 section_offset
= shdr
->sh_offset
;
647 if (lseek(elf
->fd
, section_offset
, SEEK_SET
) < 0) {
648 PERROR("Error seeking to section offset");
652 if (shdr
->sh_size
> max_alloc_size
) {
653 ERR("ELF section size exceeds maximal allowed size of %zu bytes",
657 data
= zmalloc(shdr
->sh_size
);
659 PERROR("Error allocating buffer for ELF section data");
662 ret
= lttng_read(elf
->fd
, data
, shdr
->sh_size
);
664 PERROR("Error reading ELF section data");
677 * Convert the virtual address in a binary's mapping to the offset of
678 * the corresponding instruction in the binary file.
679 * This function assumes the address is in the text section.
681 * Returns the offset on success or non-zero in case of failure.
684 int lttng_elf_convert_addr_in_text_to_offset(struct lttng_elf
*elf_handle
,
685 size_t addr
, uint64_t *offset
)
688 off_t text_section_offset
;
689 off_t text_section_addr_beg
;
690 off_t text_section_addr_end
;
691 off_t offset_in_section
;
692 struct lttng_elf_shdr text_section_hdr
;
695 DBG("Invalid ELF handle.");
696 ret
= LTTNG_ERR_ELF_PARSING
;
700 /* Get a pointer to the .text section header. */
701 ret
= lttng_elf_get_section_hdr_by_name(elf_handle
,
702 TEXT_SECTION_NAME
, &text_section_hdr
);
704 DBG("Text section not found in binary.");
705 ret
= LTTNG_ERR_ELF_PARSING
;
709 text_section_offset
= text_section_hdr
.sh_offset
;
710 text_section_addr_beg
= text_section_hdr
.sh_addr
;
711 text_section_addr_end
=
712 text_section_addr_beg
+ text_section_hdr
.sh_size
;
715 * Verify that the address is within the .text section boundaries.
717 if (addr
< text_section_addr_beg
|| addr
> text_section_addr_end
) {
718 DBG("Address found is outside of the .text section addr=0x%zx, "
719 ".text section=[0x%jd - 0x%jd].", addr
, (intmax_t)text_section_addr_beg
,
720 (intmax_t)text_section_addr_end
);
721 ret
= LTTNG_ERR_ELF_PARSING
;
725 offset_in_section
= addr
- text_section_addr_beg
;
728 * Add the target offset in the text section to the offset of this text
729 * section from the beginning of the binary file.
731 *offset
= text_section_offset
+ offset_in_section
;
738 * Compute the offset of a symbol from the begining of the ELF binary.
740 * On success, returns 0 offset parameter is set to the computed value
741 * On failure, returns -1.
743 int lttng_elf_get_symbol_offset(int fd
, char *symbol
, uint64_t *offset
)
750 char *curr_sym_str
= NULL
;
751 char *symbol_table_data
= NULL
;
752 char *string_table_data
= NULL
;
753 const char *string_table_name
= NULL
;
754 struct lttng_elf_shdr symtab_hdr
;
755 struct lttng_elf_shdr strtab_hdr
;
756 struct lttng_elf
*elf
= NULL
;
758 if (!symbol
|| !offset
) {
759 ret
= LTTNG_ERR_ELF_PARSING
;
763 elf
= lttng_elf_create(fd
);
765 ret
= LTTNG_ERR_ELF_PARSING
;
770 * The .symtab section might not exist on stripped binaries.
771 * Try to get the symbol table section header first. If it's absent,
772 * try to get the dynamic symbol table. All symbols in the dynamic
773 * symbol tab are in the (normal) symbol table if it exists.
775 ret
= lttng_elf_get_section_hdr_by_name(elf
, SYMBOL_TAB_SECTION_NAME
,
778 DBG("Cannot get ELF Symbol Table section. Trying to get ELF Dynamic Symbol Table section.");
779 /* Get the dynamic symbol table section header. */
780 ret
= lttng_elf_get_section_hdr_by_name(elf
, DYNAMIC_SYMBOL_TAB_SECTION_NAME
,
783 DBG("Cannot get ELF Symbol Table nor Dynamic Symbol Table sections.");
784 ret
= LTTNG_ERR_ELF_PARSING
;
787 string_table_name
= DYNAMIC_STRING_TAB_SECTION_NAME
;
789 string_table_name
= STRING_TAB_SECTION_NAME
;
792 /* Get the data associated with the symbol table section. */
793 symbol_table_data
= lttng_elf_get_section_data(elf
, &symtab_hdr
);
794 if (symbol_table_data
== NULL
) {
795 DBG("Cannot get ELF Symbol Table data.");
796 ret
= LTTNG_ERR_ELF_PARSING
;
800 /* Get the string table section header. */
801 ret
= lttng_elf_get_section_hdr_by_name(elf
, string_table_name
,
804 DBG("Cannot get ELF string table section.");
805 goto free_symbol_table_data
;
808 /* Get the data associated with the string table section. */
809 string_table_data
= lttng_elf_get_section_data(elf
, &strtab_hdr
);
810 if (string_table_data
== NULL
) {
811 DBG("Cannot get ELF string table section data.");
812 ret
= LTTNG_ERR_ELF_PARSING
;
813 goto free_symbol_table_data
;
816 /* Get the number of symbol in the table for the iteration. */
817 sym_count
= symtab_hdr
.sh_size
/ symtab_hdr
.sh_entsize
;
819 /* Loop over all symbol. */
820 for (sym_idx
= 0; sym_idx
< sym_count
; sym_idx
++) {
821 struct lttng_elf_sym curr_sym
;
823 /* Get the symbol at the current index. */
824 if (is_elf_32_bit(elf
)) {
825 Elf32_Sym tmp
= ((Elf32_Sym
*) symbol_table_data
)[sym_idx
];
826 copy_sym(tmp
, curr_sym
);
828 Elf64_Sym tmp
= ((Elf64_Sym
*) symbol_table_data
)[sym_idx
];
829 copy_sym(tmp
, curr_sym
);
833 * If the st_name field is zero, there is no string name for
834 * this symbol; skip to the next symbol.
836 if (curr_sym
.st_name
== 0) {
841 * Use the st_name field in the lttng_elf_sym struct to get offset of
842 * the symbol's name from the beginning of the string table.
844 curr_sym_str
= string_table_data
+ curr_sym
.st_name
;
847 * If the current symbol is not a function; skip to the next symbol.
849 if (ELF_ST_TYPE(curr_sym
.st_info
) != STT_FUNC
) {
854 * Compare with the search symbol. If there is a match set the address
855 * output parameter and return success.
857 if (strcmp(symbol
, curr_sym_str
) == 0 ) {
859 addr
= curr_sym
.st_value
;
865 DBG("Symbol not found.");
866 ret
= LTTNG_ERR_ELF_PARSING
;
867 goto free_string_table_data
;
871 * Use the virtual address of the symbol to compute the offset of this
872 * symbol from the beginning of the executable file.
874 ret
= lttng_elf_convert_addr_in_text_to_offset(elf
, addr
, offset
);
876 DBG("Cannot convert addr to offset.");
877 goto free_string_table_data
;
881 free_string_table_data
:
882 free(string_table_data
);
883 free_symbol_table_data
:
884 free(symbol_table_data
);
886 lttng_elf_destroy(elf
);
892 * Compute the offsets of SDT probes from the begining of the ELF binary.
894 * On success, returns 0 and the nb_probes parameter is set to the number of
895 * offsets found and the offsets parameter points to an array of offsets where
896 * the SDT probes are.
897 * On failure, returns -1.
899 int lttng_elf_get_sdt_probe_offsets(int fd
, const char *provider_name
,
900 const char *probe_name
, uint64_t **offsets
, uint32_t *nb_probes
)
902 int ret
= 0, nb_match
= 0;
903 struct lttng_elf_shdr stap_note_section_hdr
;
904 struct lttng_elf
*elf
= NULL
;
905 char *stap_note_section_data
= NULL
;
906 char *curr_note_section_begin
, *curr_data_ptr
, *curr_probe
, *curr_provider
;
908 uint32_t name_size
, desc_size
, note_type
;
909 uint64_t curr_probe_location
, curr_probe_offset
, curr_semaphore_location
;
910 uint64_t *probe_locs
= NULL
, *new_probe_locs
= NULL
;
912 if (!provider_name
|| !probe_name
|| !nb_probes
|| !offsets
) {
913 DBG("Invalid arguments.");
914 ret
= LTTNG_ERR_ELF_PARSING
;
918 elf
= lttng_elf_create(fd
);
920 DBG("Error allocation ELF.");
921 ret
= LTTNG_ERR_ELF_PARSING
;
925 /* Get the stap note section header. */
926 ret
= lttng_elf_get_section_hdr_by_name(elf
, NOTE_STAPSDT_SECTION_NAME
,
927 &stap_note_section_hdr
);
929 DBG("Cannot get ELF stap note section.");
930 goto destroy_elf_error
;
933 /* Get the data associated with the stap note section. */
934 stap_note_section_data
=
935 lttng_elf_get_section_data(elf
, &stap_note_section_hdr
);
936 if (stap_note_section_data
== NULL
) {
937 DBG("Cannot get ELF stap note section data.");
938 ret
= LTTNG_ERR_ELF_PARSING
;
939 goto destroy_elf_error
;
942 next_note_ptr
= stap_note_section_data
;
943 curr_note_section_begin
= stap_note_section_data
;
947 curr_data_ptr
= next_note_ptr
;
948 /* Check if we have reached the end of the note section. */
950 curr_note_section_begin
+
951 stap_note_section_hdr
.sh_size
) {
952 *nb_probes
= nb_match
;
953 *offsets
= probe_locs
;
957 /* Get name size field. */
958 name_size
= next_4bytes_boundary(*(uint32_t*) curr_data_ptr
);
959 curr_data_ptr
+= sizeof(uint32_t);
961 /* Sanity check; a zero name_size is reserved. */
962 if (name_size
== 0) {
963 DBG("Invalid name size field in SDT probe descriptions"
969 /* Get description size field. */
970 desc_size
= next_4bytes_boundary(*(uint32_t*) curr_data_ptr
);
971 curr_data_ptr
+= sizeof(uint32_t);
973 /* Get type field. */
974 note_type
= *(uint32_t *) curr_data_ptr
;
975 curr_data_ptr
+= sizeof(uint32_t);
978 * Move the pointer to the next note to be ready for the next
979 * iteration. The current note is made of 3 unsigned 32bit
980 * integers (name size, descriptor size and note type), the
981 * name and the descriptor. To move to the next note, we move
982 * the pointer according to those values.
984 next_note_ptr
= next_note_ptr
+
985 (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
= 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(elf
,
1051 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
);