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 * 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.
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.
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
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>
32 #include <sys/types.h>
38 #define TEXT_SECTION_NAME ".text"
39 #define SYMBOL_TAB_SECTION_NAME ".symtab"
40 #define STRING_TAB_SECTION_NAME ".strtab"
41 #define DYNAMIC_SYMBOL_TAB_SECTION_NAME ".dynsym"
42 #define DYNAMIC_STRING_TAB_SECTION_NAME ".dynstr"
43 #define NOTE_STAPSDT_SECTION_NAME ".note.stapsdt"
44 #define NOTE_STAPSDT_NAME "stapsdt"
45 #define NOTE_STAPSDT_TYPE 3
46 #define MAX_SECTION_DATA_SIZE 512 * 1024 * 1024
48 #if BYTE_ORDER == LITTLE_ENDIAN
49 #define NATIVE_ELF_ENDIANNESS ELFDATA2LSB
51 #define NATIVE_ELF_ENDIANNESS ELFDATA2MSB
54 #define next_4bytes_boundary(x) (typeof(x)) ((((uint64_t)x) + 3) & ~0x03)
58 switch (sizeof(x)) { \
60 x = be64toh((uint64_t)x); \
63 x = be32toh((uint32_t)x); \
66 x = be16toh((uint16_t)x); \
75 #define bswap_shdr(shdr) \
77 bswap((shdr).sh_name); \
78 bswap((shdr).sh_type); \
79 bswap((shdr).sh_flags); \
80 bswap((shdr).sh_addr); \
81 bswap((shdr).sh_offset); \
82 bswap((shdr).sh_size); \
83 bswap((shdr).sh_link); \
84 bswap((shdr).sh_info); \
85 bswap((shdr).sh_addralign); \
86 bswap((shdr).sh_entsize); \
89 #define bswap_ehdr(ehdr) \
91 bswap((ehdr).e_type); \
92 bswap((ehdr).e_machine); \
93 bswap((ehdr).e_version); \
94 bswap((ehdr).e_entry); \
95 bswap((ehdr).e_phoff); \
96 bswap((ehdr).e_shoff); \
97 bswap((ehdr).e_flags); \
98 bswap((ehdr).e_ehsize); \
99 bswap((ehdr).e_phentsize); \
100 bswap((ehdr).e_phnum); \
101 bswap((ehdr).e_shentsize); \
102 bswap((ehdr).e_shnum); \
103 bswap((ehdr).e_shstrndx); \
106 #define copy_shdr(src_shdr, dst_shdr) \
108 (dst_shdr).sh_name = (src_shdr).sh_name; \
109 (dst_shdr).sh_type = (src_shdr).sh_type; \
110 (dst_shdr).sh_flags = (src_shdr).sh_flags; \
111 (dst_shdr).sh_addr = (src_shdr).sh_addr; \
112 (dst_shdr).sh_offset = (src_shdr).sh_offset; \
113 (dst_shdr).sh_size = (src_shdr).sh_size; \
114 (dst_shdr).sh_link = (src_shdr).sh_link; \
115 (dst_shdr).sh_info = (src_shdr).sh_info; \
116 (dst_shdr).sh_addralign = (src_shdr).sh_addralign; \
117 (dst_shdr).sh_entsize = (src_shdr).sh_entsize; \
120 #define copy_ehdr(src_ehdr, dst_ehdr) \
122 (dst_ehdr).e_type = (src_ehdr).e_type; \
123 (dst_ehdr).e_machine = (src_ehdr).e_machine; \
124 (dst_ehdr).e_version = (src_ehdr).e_version; \
125 (dst_ehdr).e_entry = (src_ehdr).e_entry; \
126 (dst_ehdr).e_phoff = (src_ehdr).e_phoff; \
127 (dst_ehdr).e_shoff = (src_ehdr).e_shoff; \
128 (dst_ehdr).e_flags = (src_ehdr).e_flags; \
129 (dst_ehdr).e_ehsize = (src_ehdr).e_ehsize; \
130 (dst_ehdr).e_phentsize = (src_ehdr).e_phentsize; \
131 (dst_ehdr).e_phnum = (src_ehdr).e_phnum; \
132 (dst_ehdr).e_shentsize = (src_ehdr).e_shentsize; \
133 (dst_ehdr).e_shnum = (src_ehdr).e_shnum; \
134 (dst_ehdr).e_shstrndx = (src_ehdr).e_shstrndx; \
137 #define copy_sym(src_sym, dst_sym) \
139 dst_sym.st_name = src_sym.st_name; \
140 dst_sym.st_info = src_sym.st_info; \
141 dst_sym.st_other = src_sym.st_other; \
142 dst_sym.st_shndx = src_sym.st_shndx; \
143 dst_sym.st_value = src_sym.st_value; \
144 dst_sym.st_size = src_sym.st_size; \
147 /* Both 32bit and 64bit use the same 1 byte field for type. (See elf.h) */
148 #define ELF_ST_TYPE(val) ELF32_ST_TYPE(val)
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
{
198 /* Offset in bytes to start of section names string table. */
199 off_t section_names_offset
;
200 /* Size in bytes of section names string table. */
201 size_t section_names_size
;
202 struct lttng_elf_ehdr
*ehdr
;
206 int is_elf_32_bit(struct lttng_elf
*elf
)
208 return elf
->bitness
== ELFCLASS32
;
212 int is_elf_native_endian(struct lttng_elf
*elf
)
214 return elf
->endianness
== NATIVE_ELF_ENDIANNESS
;
218 int populate_section_header(struct lttng_elf
* elf
, struct lttng_elf_shdr
*shdr
,
224 /* Compute the offset of the section in the file */
225 offset
= (off_t
) elf
->ehdr
->e_shoff
226 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
228 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
229 PERROR("Error seeking to the beginning of ELF section header");
234 if (is_elf_32_bit(elf
)) {
237 if (lttng_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
)) < sizeof(elf_shdr
)) {
238 PERROR("Error reading ELF section header");
242 if (!is_elf_native_endian(elf
)) {
243 bswap_shdr(elf_shdr
);
245 copy_shdr(elf_shdr
, *shdr
);
249 if (lttng_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
)) < sizeof(elf_shdr
)) {
250 PERROR("Error reading ELF section header");
254 if (!is_elf_native_endian(elf
)) {
255 bswap_shdr(elf_shdr
);
257 copy_shdr(elf_shdr
, *shdr
);
265 int populate_elf_header(struct lttng_elf
*elf
)
270 * Move the read pointer back to the beginning to read the full header
271 * and copy it in our structure.
273 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
274 PERROR("Error seeking to the beginning of the file");
280 * Use macros to set fields in the ELF header struct for both 32bit and
283 if (is_elf_32_bit(elf
)) {
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
));
297 if (lttng_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
)) < sizeof(elf_ehdr
)) {
301 if (!is_elf_native_endian(elf
)) {
302 bswap_ehdr(elf_ehdr
);
304 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
311 * Retrieve the nth (where n is the `index` argument) shdr (section
312 * header) from the given elf instance.
314 * 0 is returned on succes, -1 on failure.
317 int lttng_elf_get_section_hdr(struct lttng_elf
*elf
,
318 uint16_t index
, struct lttng_elf_shdr
*out_header
)
327 if (index
>= elf
->ehdr
->e_shnum
) {
332 ret
= populate_section_header(elf
, out_header
, index
);
334 DBG("Error populating section header.");
343 * Lookup a section's name from a given offset (usually from an shdr's
344 * sh_name value) in bytes relative to the beginning of the section
345 * names string table.
347 * If no name is found, NULL is returned.
350 char *lttng_elf_get_section_name(struct lttng_elf
*elf
, off_t offset
)
353 size_t name_length
= 0, to_read
; /* name_length does not include \0 */
359 if (offset
>= elf
->section_names_size
) {
363 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
364 PERROR("Error seeking to the beginning of ELF string table section");
368 to_read
= elf
->section_names_size
- offset
;
370 /* Find first \0 after or at current location, remember name_length. */
379 read_len
= lttng_read(elf
->fd
, buf
, min_t(size_t, BUF_LEN
, to_read
));
381 PERROR("Error reading ELF string table section");
384 for (i
= 0; i
< read_len
; i
++) {
385 if (buf
[i
] == '\0') {
390 name_length
+= read_len
;
395 * We found the length of the section name, now seek back to the
396 * beginning of the name and copy it in the newly allocated buffer.
398 name
= zmalloc(sizeof(char) * (name_length
+ 1)); /* + 1 for \0 */
400 PERROR("Error allocating ELF section name buffer");
403 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
404 PERROR("Error seeking to the offset of the ELF section name");
407 if (lttng_read(elf
->fd
, name
, name_length
+ 1) < name_length
+ 1) {
408 PERROR("Error reading the ELF section name");
420 int lttng_elf_validate_and_populate(struct lttng_elf
*elf
)
423 uint8_t e_ident
[EI_NIDENT
];
424 uint8_t *magic_number
= NULL
;
429 ret
= LTTNG_ERR_ELF_PARSING
;
434 * First read the magic number, endianness and version to later populate
435 * the ELF header with the correct endianness and bitness.
439 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
440 PERROR("Error seeking the beginning of ELF file");
441 ret
= LTTNG_ERR_ELF_PARSING
;
444 ret
= lttng_read(elf
->fd
, e_ident
, EI_NIDENT
);
445 if (ret
< EI_NIDENT
) {
446 DBG("Error reading the ELF identification fields");
448 PERROR("Error reading the ELF identification fields");
450 ret
= LTTNG_ERR_ELF_PARSING
;
455 * Copy fields used to check that the target file is in fact a valid ELF
458 elf
->bitness
= e_ident
[EI_CLASS
];
459 elf
->endianness
= e_ident
[EI_DATA
];
460 version
= e_ident
[EI_VERSION
];
461 magic_number
= &e_ident
[EI_MAG0
];
464 * Check the magic number.
466 if (memcmp(magic_number
, ELFMAG
, SELFMAG
) != 0) {
467 DBG("Error check ELF magic number.");
468 ret
= LTTNG_ERR_ELF_PARSING
;
473 * Check the bitness is either ELFCLASS32 or ELFCLASS64.
475 if (elf
->bitness
<= ELFCLASSNONE
|| elf
->bitness
>= ELFCLASSNUM
) {
476 DBG("ELF class error.");
477 ret
= LTTNG_ERR_ELF_PARSING
;
482 * Check the endianness is either ELFDATA2LSB or ELFDATA2MSB.
484 if (elf
->endianness
<= ELFDATANONE
|| elf
->endianness
>= ELFDATANUM
) {
485 DBG("ELF endianness error.");
486 ret
= LTTNG_ERR_ELF_PARSING
;
491 * Check the version is ELF_CURRENT.
493 if (version
<= EV_NONE
|| version
>= EV_NUM
) {
494 DBG("Wrong ELF version.");
495 ret
= LTTNG_ERR_ELF_PARSING
;
499 elf
->ehdr
= zmalloc(sizeof(struct lttng_elf_ehdr
));
501 PERROR("Error allocation buffer for ELF header");
502 ret
= LTTNG_ERR_NOMEM
;
507 * Copy the content of the elf header.
509 ret
= populate_elf_header(elf
);
511 DBG("Error reading ELF header,");
525 * Create an instance of lttng_elf for the ELF file located at
528 * Return a pointer to the instance on success, NULL on failure.
531 struct lttng_elf
*lttng_elf_create(int fd
)
533 struct lttng_elf_shdr section_names_shdr
;
534 struct lttng_elf
*elf
= NULL
;
536 struct stat stat_buf
;
542 ret
= fstat(fd
, &stat_buf
);
544 PERROR("Failed to determine size of elf file");
547 if (!S_ISREG(stat_buf
.st_mode
)) {
548 ERR("Refusing to initialize lttng_elf from non-regular file");
552 elf
= zmalloc(sizeof(struct lttng_elf
));
554 PERROR("Error allocating struct lttng_elf");
557 elf
->file_size
= (size_t) stat_buf
.st_size
;
561 PERROR("Error duplicating file descriptor to binary");
565 ret
= lttng_elf_validate_and_populate(elf
);
570 ret
= lttng_elf_get_section_hdr(
571 elf
, elf
->ehdr
->e_shstrndx
, §ion_names_shdr
);
576 elf
->section_names_offset
= section_names_shdr
.sh_offset
;
577 elf
->section_names_size
= section_names_shdr
.sh_size
;
586 if (close(elf
->fd
)) {
587 PERROR("Error closing file descriptor in error path");
597 * Destroy the given lttng_elf instance.
600 void lttng_elf_destroy(struct lttng_elf
*elf
)
607 if (close(elf
->fd
)) {
608 PERROR("Error closing file description in error path");
615 int lttng_elf_get_section_hdr_by_name(struct lttng_elf
*elf
,
616 const char *section_name
, struct lttng_elf_shdr
*section_hdr
)
619 char *curr_section_name
;
621 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
623 int ret
= lttng_elf_get_section_hdr(elf
, i
, section_hdr
);
628 curr_section_name
= lttng_elf_get_section_name(elf
,
629 section_hdr
->sh_name
);
630 if (!curr_section_name
) {
633 name_equal
= strcmp(curr_section_name
, section_name
) == 0;
634 free(curr_section_name
);
639 return LTTNG_ERR_ELF_PARSING
;
643 char *lttng_elf_get_section_data(struct lttng_elf
*elf
,
644 struct lttng_elf_shdr
*shdr
)
647 off_t section_offset
;
649 size_t max_alloc_size
;
655 max_alloc_size
= min_t(size_t, MAX_SECTION_DATA_SIZE
, elf
->file_size
);
657 section_offset
= shdr
->sh_offset
;
658 if (lseek(elf
->fd
, section_offset
, SEEK_SET
) < 0) {
659 PERROR("Error seeking to section offset");
663 if (shdr
->sh_size
> max_alloc_size
) {
664 ERR("ELF section size exceeds maximal allowed size of %zu bytes",
668 data
= zmalloc(shdr
->sh_size
);
670 PERROR("Error allocating buffer for ELF section data");
673 ret
= lttng_read(elf
->fd
, data
, shdr
->sh_size
);
675 PERROR("Error reading ELF section data");
688 * Convert the virtual address in a binary's mapping to the offset of
689 * the corresponding instruction in the binary file.
690 * This function assumes the address is in the text section.
692 * Returns the offset on success or non-zero in case of failure.
695 int lttng_elf_convert_addr_in_text_to_offset(struct lttng_elf
*elf_handle
,
696 size_t addr
, uint64_t *offset
)
699 off_t text_section_offset
;
700 off_t text_section_addr_beg
;
701 off_t text_section_addr_end
;
702 off_t offset_in_section
;
703 struct lttng_elf_shdr text_section_hdr
;
706 DBG("Invalid ELF handle.");
707 ret
= LTTNG_ERR_ELF_PARSING
;
711 /* Get a pointer to the .text section header. */
712 ret
= lttng_elf_get_section_hdr_by_name(elf_handle
,
713 TEXT_SECTION_NAME
, &text_section_hdr
);
715 DBG("Text section not found in binary.");
716 ret
= LTTNG_ERR_ELF_PARSING
;
720 text_section_offset
= text_section_hdr
.sh_offset
;
721 text_section_addr_beg
= text_section_hdr
.sh_addr
;
722 text_section_addr_end
=
723 text_section_addr_beg
+ text_section_hdr
.sh_size
;
726 * Verify that the address is within the .text section boundaries.
728 if (addr
< text_section_addr_beg
|| addr
> text_section_addr_end
) {
729 DBG("Address found is outside of the .text section addr=0x%zx, "
730 ".text section=[0x%jd - 0x%jd].", addr
, (intmax_t)text_section_addr_beg
,
731 (intmax_t)text_section_addr_end
);
732 ret
= LTTNG_ERR_ELF_PARSING
;
736 offset_in_section
= addr
- text_section_addr_beg
;
739 * Add the target offset in the text section to the offset of this text
740 * section from the beginning of the binary file.
742 *offset
= text_section_offset
+ offset_in_section
;
749 * Compute the offset of a symbol from the begining of the ELF binary.
751 * On success, returns 0 offset parameter is set to the computed value
752 * On failure, returns -1.
754 int lttng_elf_get_symbol_offset(int fd
, char *symbol
, uint64_t *offset
)
761 char *curr_sym_str
= NULL
;
762 char *symbol_table_data
= NULL
;
763 char *string_table_data
= NULL
;
764 char *string_table_name
= NULL
;
765 struct lttng_elf_shdr symtab_hdr
;
766 struct lttng_elf_shdr strtab_hdr
;
767 struct lttng_elf
*elf
= NULL
;
769 if (!symbol
|| !offset
) {
770 ret
= LTTNG_ERR_ELF_PARSING
;
774 elf
= lttng_elf_create(fd
);
776 ret
= LTTNG_ERR_ELF_PARSING
;
781 * The .symtab section might not exist on stripped binaries.
782 * Try to get the symbol table section header first. If it's absent,
783 * try to get the dynamic symbol table. All symbols in the dynamic
784 * symbol tab are in the (normal) symbol table if it exists.
786 ret
= lttng_elf_get_section_hdr_by_name(elf
, SYMBOL_TAB_SECTION_NAME
,
789 DBG("Cannot get ELF Symbol Table section. Trying to get ELF Dynamic Symbol Table section.");
790 /* Get the dynamic symbol table section header. */
791 ret
= lttng_elf_get_section_hdr_by_name(elf
, DYNAMIC_SYMBOL_TAB_SECTION_NAME
,
794 DBG("Cannot get ELF Symbol Table nor Dynamic Symbol Table sections.");
795 ret
= LTTNG_ERR_ELF_PARSING
;
798 string_table_name
= DYNAMIC_STRING_TAB_SECTION_NAME
;
800 string_table_name
= STRING_TAB_SECTION_NAME
;
803 /* Get the data associated with the symbol table section. */
804 symbol_table_data
= lttng_elf_get_section_data(elf
, &symtab_hdr
);
805 if (symbol_table_data
== NULL
) {
806 DBG("Cannot get ELF Symbol Table data.");
807 ret
= LTTNG_ERR_ELF_PARSING
;
811 /* Get the string table section header. */
812 ret
= lttng_elf_get_section_hdr_by_name(elf
, string_table_name
,
815 DBG("Cannot get ELF string table section.");
816 goto free_symbol_table_data
;
819 /* Get the data associated with the string table section. */
820 string_table_data
= lttng_elf_get_section_data(elf
, &strtab_hdr
);
821 if (string_table_data
== NULL
) {
822 DBG("Cannot get ELF string table section data.");
823 ret
= LTTNG_ERR_ELF_PARSING
;
824 goto free_symbol_table_data
;
827 /* Get the number of symbol in the table for the iteration. */
828 sym_count
= symtab_hdr
.sh_size
/ symtab_hdr
.sh_entsize
;
830 /* Loop over all symbol. */
831 for (sym_idx
= 0; sym_idx
< sym_count
; sym_idx
++) {
832 struct lttng_elf_sym curr_sym
;
834 /* Get the symbol at the current index. */
835 if (is_elf_32_bit(elf
)) {
836 Elf32_Sym tmp
= ((Elf32_Sym
*) symbol_table_data
)[sym_idx
];
837 copy_sym(tmp
, curr_sym
);
839 Elf64_Sym tmp
= ((Elf64_Sym
*) symbol_table_data
)[sym_idx
];
840 copy_sym(tmp
, curr_sym
);
844 * If the st_name field is zero, there is no string name for
845 * this symbol; skip to the next symbol.
847 if (curr_sym
.st_name
== 0) {
852 * Use the st_name field in the lttng_elf_sym struct to get offset of
853 * the symbol's name from the beginning of the string table.
855 curr_sym_str
= string_table_data
+ curr_sym
.st_name
;
858 * If the current symbol is not a function; skip to the next symbol.
860 if (ELF_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
= 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
);