74b3f3ca661aac9fe3b9e4e43f7dbe6f8c8388fd
2 * SPDX-License-Identifier: LGPL-2.1-or-later
4 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
13 #include <sys/types.h>
16 #include <lttng/ust-utils.h>
21 #include "lttng-tracer-core.h"
22 #include "lttng-ust-elf.h"
23 #include "ust-helper.h"
27 #ifndef NT_GNU_BUILD_ID
28 # define NT_GNU_BUILD_ID 3
32 * Retrieve the nth (where n is the `index` argument) phdr (program
33 * header) from the given elf instance.
35 * A pointer to the phdr is returned on success, NULL on failure.
38 struct lttng_ust_elf_phdr
*lttng_ust_elf_get_phdr(struct lttng_ust_elf
*elf
,
41 struct lttng_ust_elf_phdr
*phdr
= NULL
;
48 if (index
>= elf
->ehdr
->e_phnum
) {
52 phdr
= zmalloc(sizeof(struct lttng_ust_elf_phdr
));
57 offset
= (off_t
) elf
->ehdr
->e_phoff
58 + (off_t
) index
* elf
->ehdr
->e_phentsize
;
59 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
63 if (is_elf_32_bit(elf
)) {
66 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
70 if (!is_elf_native_endian(elf
)) {
73 copy_phdr(elf_phdr
, *phdr
);
77 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
81 if (!is_elf_native_endian(elf
)) {
84 copy_phdr(elf_phdr
, *phdr
);
95 * Retrieve the nth (where n is the `index` argument) shdr (section
96 * header) from the given elf instance.
98 * A pointer to the shdr is returned on success, NULL on failure.
101 struct lttng_ust_elf_shdr
*lttng_ust_elf_get_shdr(struct lttng_ust_elf
*elf
,
104 struct lttng_ust_elf_shdr
*shdr
= NULL
;
111 if (index
>= elf
->ehdr
->e_shnum
) {
115 shdr
= zmalloc(sizeof(struct lttng_ust_elf_shdr
));
120 offset
= (off_t
) elf
->ehdr
->e_shoff
121 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
122 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
126 if (is_elf_32_bit(elf
)) {
129 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
130 < sizeof(elf_shdr
)) {
133 if (!is_elf_native_endian(elf
)) {
134 bswap_shdr(elf_shdr
);
136 copy_shdr(elf_shdr
, *shdr
);
140 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
141 < sizeof(elf_shdr
)) {
144 if (!is_elf_native_endian(elf
)) {
145 bswap_shdr(elf_shdr
);
147 copy_shdr(elf_shdr
, *shdr
);
158 * Lookup a section's name from a given offset (usually from an shdr's
159 * sh_name value) in bytes relative to the beginning of the section
160 * names string table.
162 * If no name is found, NULL is returned.
165 char *lttng_ust_elf_get_section_name(struct lttng_ust_elf
*elf
, off_t offset
)
168 size_t len
= 0, to_read
; /* len does not include \0 */
174 if (offset
>= elf
->section_names_size
) {
178 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
182 to_read
= elf
->section_names_size
- offset
;
184 /* Find first \0 after or at current location, remember len. */
193 read_len
= lttng_ust_read(elf
->fd
, buf
,
194 min_t(size_t, BUF_LEN
, to_read
));
198 for (i
= 0; i
< read_len
; i
++) {
199 if (buf
[i
] == '\0') {
208 name
= zmalloc(sizeof(char) * (len
+ 1)); /* + 1 for \0 */
212 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
,
216 if (lttng_ust_read(elf
->fd
, name
, len
+ 1) < len
+ 1) {
228 * Create an instance of lttng_ust_elf for the ELF file located at
231 * Return a pointer to the instance on success, NULL on failure.
233 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
)
235 uint8_t e_ident
[EI_NIDENT
];
236 struct lttng_ust_elf_shdr
*section_names_shdr
;
237 struct lttng_ust_elf
*elf
= NULL
;
240 elf
= zmalloc(sizeof(struct lttng_ust_elf
));
245 /* Initialize fd field to -1. 0 is a valid fd number */
248 elf
->path
= strdup(path
);
253 lttng_ust_lock_fd_tracker();
254 fd
= open(elf
->path
, O_RDONLY
| O_CLOEXEC
);
256 lttng_ust_unlock_fd_tracker();
260 ret
= lttng_ust_add_fd_to_tracker(fd
);
264 PERROR("close on elf->fd");
267 lttng_ust_unlock_fd_tracker();
271 lttng_ust_unlock_fd_tracker();
273 if (lttng_ust_read(elf
->fd
, e_ident
, EI_NIDENT
) < EI_NIDENT
) {
276 elf
->bitness
= e_ident
[EI_CLASS
];
277 elf
->endianness
= e_ident
[EI_DATA
];
279 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
283 elf
->ehdr
= zmalloc(sizeof(struct lttng_ust_elf_ehdr
));
288 if (is_elf_32_bit(elf
)) {
291 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
292 < sizeof(elf_ehdr
)) {
295 if (!is_elf_native_endian(elf
)) {
296 bswap_ehdr(elf_ehdr
);
298 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
302 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
303 < sizeof(elf_ehdr
)) {
306 if (!is_elf_native_endian(elf
)) {
307 bswap_ehdr(elf_ehdr
);
309 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
312 section_names_shdr
= lttng_ust_elf_get_shdr(elf
, elf
->ehdr
->e_shstrndx
);
313 if (!section_names_shdr
) {
317 elf
->section_names_offset
= section_names_shdr
->sh_offset
;
318 elf
->section_names_size
= section_names_shdr
->sh_size
;
320 free(section_names_shdr
);
324 lttng_ust_elf_destroy(elf
);
329 * Test whether the ELF file is position independent code (PIC)
331 uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf
*elf
)
334 * PIC has and e_type value of ET_DYN, see ELF specification
335 * version 1.1 p. 1-3.
337 return elf
->ehdr
->e_type
== ET_DYN
;
341 * Destroy the given lttng_ust_elf instance.
343 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
)
352 lttng_ust_lock_fd_tracker();
353 ret
= close(elf
->fd
);
355 lttng_ust_delete_fd_from_tracker(elf
->fd
);
360 lttng_ust_unlock_fd_tracker();
369 * Compute the total in-memory size of the ELF file, in bytes.
371 * Returns 0 if successful, -1 if not. On success, the memory size is
372 * returned through the out parameter `memsz`.
374 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
)
377 uint64_t low_addr
= UINT64_MAX
, high_addr
= 0;
379 if (!elf
|| !memsz
) {
383 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
384 struct lttng_ust_elf_phdr
*phdr
;
386 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
392 * Only PT_LOAD segments contribute to memsz. Skip
395 if (phdr
->p_type
!= PT_LOAD
) {
399 low_addr
= min_t(uint64_t, low_addr
, phdr
->p_vaddr
);
400 high_addr
= max_t(uint64_t, high_addr
,
401 phdr
->p_vaddr
+ phdr
->p_memsz
);
406 if (high_addr
< low_addr
) {
407 /* No PT_LOAD segments or corrupted data. */
411 *memsz
= high_addr
- low_addr
;
418 * Internal method used to try and get the build_id from a PT_NOTE
419 * segment ranging from `offset` to `segment_end`.
421 * If the function returns successfully, the out parameter `found`
422 * indicates whether the build id information was present in the
423 * segment or not. If `found` is not 0, the out parameters `build_id`
424 * and `length` will both have been set with the retrieved
427 * Returns 0 on success, -1 if an error occurred.
430 int lttng_ust_elf_get_build_id_from_segment(
431 struct lttng_ust_elf
*elf
, uint8_t **build_id
, size_t *length
,
432 off_t offset
, off_t segment_end
)
434 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
435 size_t _length
= 0; /* Silence old gcc warning. */
437 while (offset
< segment_end
) {
438 struct lttng_ust_elf_nhdr nhdr
;
441 /* Align start of note entry */
442 offset
+= lttng_ust_offset_align(offset
, ELF_NOTE_ENTRY_ALIGN
);
443 if (offset
>= segment_end
) {
447 * We seek manually because if the note isn't the
448 * build id the data following the header will not
451 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
454 if (lttng_ust_read(elf
->fd
, &nhdr
, sizeof(nhdr
))
459 if (!is_elf_native_endian(elf
)) {
460 nhdr
.n_namesz
= bswap_32(nhdr
.n_namesz
);
461 nhdr
.n_descsz
= bswap_32(nhdr
.n_descsz
);
462 nhdr
.n_type
= bswap_32(nhdr
.n_type
);
465 offset
+= sizeof(nhdr
) + nhdr
.n_namesz
;
466 /* Align start of desc entry */
467 offset
+= lttng_ust_offset_align(offset
, ELF_NOTE_DESC_ALIGN
);
469 if (nhdr
.n_type
!= NT_GNU_BUILD_ID
) {
471 * Ignore non build id notes but still
472 * increase the offset.
474 offset
+= nhdr
.n_descsz
;
478 _length
= nhdr
.n_descsz
;
479 _build_id
= zmalloc(sizeof(uint8_t) * _length
);
484 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
487 read_len
= sizeof(*_build_id
) * _length
;
488 if (lttng_ust_read(elf
->fd
, _build_id
, read_len
) < read_len
) {
496 *build_id
= _build_id
;
507 * Retrieve a build ID (an array of bytes) from the corresponding
508 * section in the ELF file. The length of the build ID can be either
509 * 16 or 20 bytes depending on the method used to generate it, hence
510 * the length out parameter.
512 * If the function returns successfully, the out parameter `found`
513 * indicates whether the build id information was present in the ELF
514 * file or not. If `found` is not 0, the out parameters `build_id` and
515 * `length` will both have been set with the retrieved information.
517 * Returns 0 on success, -1 if an error occurred.
519 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
520 size_t *length
, int *found
)
523 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
524 size_t _length
= 0; /* Silence old gcc warning. */
526 if (!elf
|| !build_id
|| !length
|| !found
) {
530 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
531 off_t offset
, segment_end
;
532 struct lttng_ust_elf_phdr
*phdr
;
535 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
540 /* Build ID will be contained in a PT_NOTE segment. */
541 if (phdr
->p_type
!= PT_NOTE
) {
545 offset
= phdr
->p_offset
;
546 segment_end
= offset
+ phdr
->p_filesz
;
547 ret
= lttng_ust_elf_get_build_id_from_segment(
548 elf
, &_build_id
, &_length
, offset
, segment_end
);
560 *build_id
= _build_id
;
574 * Try to retrieve filename and CRC from given ELF section `shdr`.
576 * If the function returns successfully, the out parameter `found`
577 * indicates whether the debug link information was present in the ELF
578 * section or not. If `found` is not 0, the out parameters `filename` and
579 * `crc` will both have been set with the retrieved information.
581 * Returns 0 on success, -1 if an error occurred.
583 int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf
*elf
,
584 char **filename
, uint32_t *crc
,
585 struct lttng_ust_elf_shdr
*shdr
)
587 char *_filename
= NULL
; /* Silence old gcc warning. */
589 char *section_name
= NULL
;
590 uint32_t _crc
= 0; /* Silence old gcc warning. */
592 if (!elf
|| !filename
|| !crc
|| !shdr
) {
597 * The .gnu_debuglink section is of type SHT_PROGBITS,
598 * skip the other sections.
600 if (shdr
->sh_type
!= SHT_PROGBITS
) {
604 section_name
= lttng_ust_elf_get_section_name(elf
,
609 if (strcmp(section_name
, ".gnu_debuglink")) {
614 * The length of the filename is the sh_size excluding the CRC
615 * which comes after it in the section.
617 _filename
= zmalloc(sizeof(char) * (shdr
->sh_size
- ELF_CRC_SIZE
));
621 if (lseek(elf
->fd
, shdr
->sh_offset
, SEEK_SET
) < 0) {
624 filename_len
= sizeof(*_filename
) * (shdr
->sh_size
- ELF_CRC_SIZE
);
625 if (lttng_ust_read(elf
->fd
, _filename
, filename_len
) < filename_len
) {
628 if (lttng_ust_read(elf
->fd
, &_crc
, sizeof(_crc
)) < sizeof(_crc
)) {
631 if (!is_elf_native_endian(elf
)) {
632 _crc
= bswap_32(_crc
);
638 *filename
= _filename
;
651 * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any.
653 * If the function returns successfully, the out parameter `found`
654 * indicates whether the debug link information was present in the ELF
655 * file or not. If `found` is not 0, the out parameters `filename` and
656 * `crc` will both have been set with the retrieved information.
658 * Returns 0 on success, -1 if an error occurred.
660 int lttng_ust_elf_get_debug_link(struct lttng_ust_elf
*elf
, char **filename
,
661 uint32_t *crc
, int *found
)
665 char *_filename
= NULL
; /* Silence old gcc warning. */
666 uint32_t _crc
= 0; /* Silence old gcc warning. */
668 if (!elf
|| !filename
|| !crc
|| !found
) {
672 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
673 struct lttng_ust_elf_shdr
*shdr
= NULL
;
675 shdr
= lttng_ust_elf_get_shdr(elf
, i
);
680 ret
= lttng_ust_elf_get_debug_link_from_section(
681 elf
, &_filename
, &_crc
, shdr
);
693 *filename
= _filename
;
This page took 0.081942 seconds and 3 git commands to generate.