2 * SPDX-License-Identifier: LGPL-2.1-or-later
4 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
9 #include <ust-helper.h>
10 #include <lttng/align.h>
11 #include <lttng/ust-elf.h>
16 #include <sys/types.h>
21 #include "lttng-tracer-core.h"
25 #ifndef NT_GNU_BUILD_ID
26 # define NT_GNU_BUILD_ID 3
30 * Retrieve the nth (where n is the `index` argument) phdr (program
31 * header) from the given elf instance.
33 * A pointer to the phdr is returned on success, NULL on failure.
36 struct lttng_ust_elf_phdr
*lttng_ust_elf_get_phdr(struct lttng_ust_elf
*elf
,
39 struct lttng_ust_elf_phdr
*phdr
= NULL
;
46 if (index
>= elf
->ehdr
->e_phnum
) {
50 phdr
= zmalloc(sizeof(struct lttng_ust_elf_phdr
));
55 offset
= (off_t
) elf
->ehdr
->e_phoff
56 + (off_t
) index
* elf
->ehdr
->e_phentsize
;
57 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
61 if (is_elf_32_bit(elf
)) {
64 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
68 if (!is_elf_native_endian(elf
)) {
71 copy_phdr(elf_phdr
, *phdr
);
75 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
79 if (!is_elf_native_endian(elf
)) {
82 copy_phdr(elf_phdr
, *phdr
);
93 * Retrieve the nth (where n is the `index` argument) shdr (section
94 * header) from the given elf instance.
96 * A pointer to the shdr is returned on success, NULL on failure.
99 struct lttng_ust_elf_shdr
*lttng_ust_elf_get_shdr(struct lttng_ust_elf
*elf
,
102 struct lttng_ust_elf_shdr
*shdr
= NULL
;
109 if (index
>= elf
->ehdr
->e_shnum
) {
113 shdr
= zmalloc(sizeof(struct lttng_ust_elf_shdr
));
118 offset
= (off_t
) elf
->ehdr
->e_shoff
119 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
120 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
124 if (is_elf_32_bit(elf
)) {
127 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
128 < sizeof(elf_shdr
)) {
131 if (!is_elf_native_endian(elf
)) {
132 bswap_shdr(elf_shdr
);
134 copy_shdr(elf_shdr
, *shdr
);
138 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
139 < sizeof(elf_shdr
)) {
142 if (!is_elf_native_endian(elf
)) {
143 bswap_shdr(elf_shdr
);
145 copy_shdr(elf_shdr
, *shdr
);
156 * Lookup a section's name from a given offset (usually from an shdr's
157 * sh_name value) in bytes relative to the beginning of the section
158 * names string table.
160 * If no name is found, NULL is returned.
163 char *lttng_ust_elf_get_section_name(struct lttng_ust_elf
*elf
, off_t offset
)
166 size_t len
= 0, to_read
; /* len does not include \0 */
172 if (offset
>= elf
->section_names_size
) {
176 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
180 to_read
= elf
->section_names_size
- offset
;
182 /* Find first \0 after or at current location, remember len. */
191 read_len
= lttng_ust_read(elf
->fd
, buf
,
192 min_t(size_t, BUF_LEN
, to_read
));
196 for (i
= 0; i
< read_len
; i
++) {
197 if (buf
[i
] == '\0') {
206 name
= zmalloc(sizeof(char) * (len
+ 1)); /* + 1 for \0 */
210 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
,
214 if (lttng_ust_read(elf
->fd
, name
, len
+ 1) < len
+ 1) {
226 * Create an instance of lttng_ust_elf for the ELF file located at
229 * Return a pointer to the instance on success, NULL on failure.
231 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
)
233 uint8_t e_ident
[EI_NIDENT
];
234 struct lttng_ust_elf_shdr
*section_names_shdr
;
235 struct lttng_ust_elf
*elf
= NULL
;
238 elf
= zmalloc(sizeof(struct lttng_ust_elf
));
243 /* Initialize fd field to -1. 0 is a valid fd number */
246 elf
->path
= strdup(path
);
251 lttng_ust_lock_fd_tracker();
252 fd
= open(elf
->path
, O_RDONLY
| O_CLOEXEC
);
254 lttng_ust_unlock_fd_tracker();
258 ret
= lttng_ust_add_fd_to_tracker(fd
);
262 PERROR("close on elf->fd");
265 lttng_ust_unlock_fd_tracker();
269 lttng_ust_unlock_fd_tracker();
271 if (lttng_ust_read(elf
->fd
, e_ident
, EI_NIDENT
) < EI_NIDENT
) {
274 elf
->bitness
= e_ident
[EI_CLASS
];
275 elf
->endianness
= e_ident
[EI_DATA
];
277 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
281 elf
->ehdr
= zmalloc(sizeof(struct lttng_ust_elf_ehdr
));
286 if (is_elf_32_bit(elf
)) {
289 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
290 < sizeof(elf_ehdr
)) {
293 if (!is_elf_native_endian(elf
)) {
294 bswap_ehdr(elf_ehdr
);
296 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
300 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
301 < sizeof(elf_ehdr
)) {
304 if (!is_elf_native_endian(elf
)) {
305 bswap_ehdr(elf_ehdr
);
307 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
310 section_names_shdr
= lttng_ust_elf_get_shdr(elf
, elf
->ehdr
->e_shstrndx
);
311 if (!section_names_shdr
) {
315 elf
->section_names_offset
= section_names_shdr
->sh_offset
;
316 elf
->section_names_size
= section_names_shdr
->sh_size
;
318 free(section_names_shdr
);
322 lttng_ust_elf_destroy(elf
);
327 * Test whether the ELF file is position independent code (PIC)
329 uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf
*elf
)
332 * PIC has and e_type value of ET_DYN, see ELF specification
333 * version 1.1 p. 1-3.
335 return elf
->ehdr
->e_type
== ET_DYN
;
339 * Destroy the given lttng_ust_elf instance.
341 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
)
350 lttng_ust_lock_fd_tracker();
351 ret
= close(elf
->fd
);
353 lttng_ust_delete_fd_from_tracker(elf
->fd
);
358 lttng_ust_unlock_fd_tracker();
367 * Compute the total in-memory size of the ELF file, in bytes.
369 * Returns 0 if successful, -1 if not. On success, the memory size is
370 * returned through the out parameter `memsz`.
372 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
)
375 uint64_t low_addr
= UINT64_MAX
, high_addr
= 0;
377 if (!elf
|| !memsz
) {
381 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
382 struct lttng_ust_elf_phdr
*phdr
;
384 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
390 * Only PT_LOAD segments contribute to memsz. Skip
393 if (phdr
->p_type
!= PT_LOAD
) {
397 low_addr
= min_t(uint64_t, low_addr
, phdr
->p_vaddr
);
398 high_addr
= max_t(uint64_t, high_addr
,
399 phdr
->p_vaddr
+ phdr
->p_memsz
);
404 if (high_addr
< low_addr
) {
405 /* No PT_LOAD segments or corrupted data. */
409 *memsz
= high_addr
- low_addr
;
416 * Internal method used to try and get the build_id from a PT_NOTE
417 * segment ranging from `offset` to `segment_end`.
419 * If the function returns successfully, the out parameter `found`
420 * indicates whether the build id information was present in the
421 * segment or not. If `found` is not 0, the out parameters `build_id`
422 * and `length` will both have been set with the retrieved
425 * Returns 0 on success, -1 if an error occurred.
428 int lttng_ust_elf_get_build_id_from_segment(
429 struct lttng_ust_elf
*elf
, uint8_t **build_id
, size_t *length
,
430 off_t offset
, off_t segment_end
)
432 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
433 size_t _length
= 0; /* Silence old gcc warning. */
435 while (offset
< segment_end
) {
436 struct lttng_ust_elf_nhdr nhdr
;
439 /* Align start of note entry */
440 offset
+= lttng_ust_offset_align(offset
, ELF_NOTE_ENTRY_ALIGN
);
441 if (offset
>= segment_end
) {
445 * We seek manually because if the note isn't the
446 * build id the data following the header will not
449 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
452 if (lttng_ust_read(elf
->fd
, &nhdr
, sizeof(nhdr
))
457 if (!is_elf_native_endian(elf
)) {
458 nhdr
.n_namesz
= bswap_32(nhdr
.n_namesz
);
459 nhdr
.n_descsz
= bswap_32(nhdr
.n_descsz
);
460 nhdr
.n_type
= bswap_32(nhdr
.n_type
);
463 offset
+= sizeof(nhdr
) + nhdr
.n_namesz
;
464 /* Align start of desc entry */
465 offset
+= lttng_ust_offset_align(offset
, ELF_NOTE_DESC_ALIGN
);
467 if (nhdr
.n_type
!= NT_GNU_BUILD_ID
) {
469 * Ignore non build id notes but still
470 * increase the offset.
472 offset
+= nhdr
.n_descsz
;
476 _length
= nhdr
.n_descsz
;
477 _build_id
= zmalloc(sizeof(uint8_t) * _length
);
482 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
485 read_len
= sizeof(*_build_id
) * _length
;
486 if (lttng_ust_read(elf
->fd
, _build_id
, read_len
) < read_len
) {
494 *build_id
= _build_id
;
505 * Retrieve a build ID (an array of bytes) from the corresponding
506 * section in the ELF file. The length of the build ID can be either
507 * 16 or 20 bytes depending on the method used to generate it, hence
508 * the length out parameter.
510 * If the function returns successfully, the out parameter `found`
511 * indicates whether the build id information was present in the ELF
512 * file or not. If `found` is not 0, the out parameters `build_id` and
513 * `length` will both have been set with the retrieved information.
515 * Returns 0 on success, -1 if an error occurred.
517 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
518 size_t *length
, int *found
)
521 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
522 size_t _length
= 0; /* Silence old gcc warning. */
524 if (!elf
|| !build_id
|| !length
|| !found
) {
528 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
529 off_t offset
, segment_end
;
530 struct lttng_ust_elf_phdr
*phdr
;
533 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
538 /* Build ID will be contained in a PT_NOTE segment. */
539 if (phdr
->p_type
!= PT_NOTE
) {
543 offset
= phdr
->p_offset
;
544 segment_end
= offset
+ phdr
->p_filesz
;
545 ret
= lttng_ust_elf_get_build_id_from_segment(
546 elf
, &_build_id
, &_length
, offset
, segment_end
);
558 *build_id
= _build_id
;
572 * Try to retrieve filename and CRC from given ELF section `shdr`.
574 * If the function returns successfully, the out parameter `found`
575 * indicates whether the debug link information was present in the ELF
576 * section or not. If `found` is not 0, the out parameters `filename` and
577 * `crc` will both have been set with the retrieved information.
579 * Returns 0 on success, -1 if an error occurred.
581 int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf
*elf
,
582 char **filename
, uint32_t *crc
,
583 struct lttng_ust_elf_shdr
*shdr
)
585 char *_filename
= NULL
; /* Silence old gcc warning. */
587 char *section_name
= NULL
;
588 uint32_t _crc
= 0; /* Silence old gcc warning. */
590 if (!elf
|| !filename
|| !crc
|| !shdr
) {
595 * The .gnu_debuglink section is of type SHT_PROGBITS,
596 * skip the other sections.
598 if (shdr
->sh_type
!= SHT_PROGBITS
) {
602 section_name
= lttng_ust_elf_get_section_name(elf
,
607 if (strcmp(section_name
, ".gnu_debuglink")) {
612 * The length of the filename is the sh_size excluding the CRC
613 * which comes after it in the section.
615 _filename
= zmalloc(sizeof(char) * (shdr
->sh_size
- ELF_CRC_SIZE
));
619 if (lseek(elf
->fd
, shdr
->sh_offset
, SEEK_SET
) < 0) {
622 filename_len
= sizeof(*_filename
) * (shdr
->sh_size
- ELF_CRC_SIZE
);
623 if (lttng_ust_read(elf
->fd
, _filename
, filename_len
) < filename_len
) {
626 if (lttng_ust_read(elf
->fd
, &_crc
, sizeof(_crc
)) < sizeof(_crc
)) {
629 if (!is_elf_native_endian(elf
)) {
630 _crc
= bswap_32(_crc
);
636 *filename
= _filename
;
649 * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any.
651 * If the function returns successfully, the out parameter `found`
652 * indicates whether the debug link information was present in the ELF
653 * file or not. If `found` is not 0, the out parameters `filename` and
654 * `crc` will both have been set with the retrieved information.
656 * Returns 0 on success, -1 if an error occurred.
658 int lttng_ust_elf_get_debug_link(struct lttng_ust_elf
*elf
, char **filename
,
659 uint32_t *crc
, int *found
)
663 char *_filename
= NULL
; /* Silence old gcc warning. */
664 uint32_t _crc
= 0; /* Silence old gcc warning. */
666 if (!elf
|| !filename
|| !crc
|| !found
) {
670 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
671 struct lttng_ust_elf_shdr
*shdr
= NULL
;
673 shdr
= lttng_ust_elf_get_shdr(elf
, i
);
678 ret
= lttng_ust_elf_get_debug_link_from_section(
679 elf
, &_filename
, &_crc
, shdr
);
691 *filename
= _filename
;
This page took 0.053087 seconds and 5 git commands to generate.