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>
18 #include "common/elf.h"
19 #include "common/logging.h"
20 #include "common/macros.h"
21 #include "common/ust-fd.h"
22 #include "common/utils.h"
26 #ifndef NT_GNU_BUILD_ID
27 # define NT_GNU_BUILD_ID 3
31 * Retrieve the nth (where n is the `index` argument) phdr (program
32 * header) from the given elf instance.
34 * A pointer to the phdr is returned on success, NULL on failure.
37 struct lttng_ust_elf_phdr
*lttng_ust_elf_get_phdr(struct lttng_ust_elf
*elf
,
40 struct lttng_ust_elf_phdr
*phdr
= NULL
;
47 if (index
>= elf
->ehdr
->e_phnum
) {
51 phdr
= zmalloc(sizeof(struct lttng_ust_elf_phdr
));
56 offset
= (off_t
) elf
->ehdr
->e_phoff
57 + (off_t
) index
* elf
->ehdr
->e_phentsize
;
58 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
62 if (is_elf_32_bit(elf
)) {
65 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
69 if (!is_elf_native_endian(elf
)) {
72 copy_phdr(elf_phdr
, *phdr
);
76 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
80 if (!is_elf_native_endian(elf
)) {
83 copy_phdr(elf_phdr
, *phdr
);
94 * Retrieve the nth (where n is the `index` argument) shdr (section
95 * header) from the given elf instance.
97 * A pointer to the shdr is returned on success, NULL on failure.
100 struct lttng_ust_elf_shdr
*lttng_ust_elf_get_shdr(struct lttng_ust_elf
*elf
,
103 struct lttng_ust_elf_shdr
*shdr
= NULL
;
110 if (index
>= elf
->ehdr
->e_shnum
) {
114 shdr
= zmalloc(sizeof(struct lttng_ust_elf_shdr
));
119 offset
= (off_t
) elf
->ehdr
->e_shoff
120 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
121 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
125 if (is_elf_32_bit(elf
)) {
128 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
129 < sizeof(elf_shdr
)) {
132 if (!is_elf_native_endian(elf
)) {
133 bswap_shdr(elf_shdr
);
135 copy_shdr(elf_shdr
, *shdr
);
139 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
140 < sizeof(elf_shdr
)) {
143 if (!is_elf_native_endian(elf
)) {
144 bswap_shdr(elf_shdr
);
146 copy_shdr(elf_shdr
, *shdr
);
157 * Lookup a section's name from a given offset (usually from an shdr's
158 * sh_name value) in bytes relative to the beginning of the section
159 * names string table.
161 * If no name is found, NULL is returned.
164 char *lttng_ust_elf_get_section_name(struct lttng_ust_elf
*elf
, off_t offset
)
167 size_t len
= 0, to_read
; /* len does not include \0 */
173 if (offset
>= elf
->section_names_size
) {
177 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
181 to_read
= elf
->section_names_size
- offset
;
183 /* Find first \0 after or at current location, remember len. */
192 read_len
= lttng_ust_read(elf
->fd
, buf
,
193 min_t(size_t, BUF_LEN
, to_read
));
197 for (i
= 0; i
< read_len
; i
++) {
198 if (buf
[i
] == '\0') {
207 name
= zmalloc(sizeof(char) * (len
+ 1)); /* + 1 for \0 */
211 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
,
215 if (lttng_ust_read(elf
->fd
, name
, len
+ 1) < len
+ 1) {
227 * Create an instance of lttng_ust_elf for the ELF file located at
230 * Return a pointer to the instance on success, NULL on failure.
232 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
)
234 uint8_t e_ident
[EI_NIDENT
];
235 struct lttng_ust_elf_shdr
*section_names_shdr
;
236 struct lttng_ust_elf
*elf
= NULL
;
239 elf
= zmalloc(sizeof(struct lttng_ust_elf
));
244 /* Initialize fd field to -1. 0 is a valid fd number */
247 elf
->path
= strdup(path
);
252 lttng_ust_lock_fd_tracker();
253 fd
= open(elf
->path
, O_RDONLY
| O_CLOEXEC
);
255 lttng_ust_unlock_fd_tracker();
259 ret
= lttng_ust_add_fd_to_tracker(fd
);
263 PERROR("close on elf->fd");
266 lttng_ust_unlock_fd_tracker();
270 lttng_ust_unlock_fd_tracker();
272 if (lttng_ust_read(elf
->fd
, e_ident
, EI_NIDENT
) < EI_NIDENT
) {
275 elf
->bitness
= e_ident
[EI_CLASS
];
276 elf
->endianness
= e_ident
[EI_DATA
];
278 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
282 elf
->ehdr
= zmalloc(sizeof(struct lttng_ust_elf_ehdr
));
287 if (is_elf_32_bit(elf
)) {
290 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
291 < sizeof(elf_ehdr
)) {
294 if (!is_elf_native_endian(elf
)) {
295 bswap_ehdr(elf_ehdr
);
297 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
301 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
302 < sizeof(elf_ehdr
)) {
305 if (!is_elf_native_endian(elf
)) {
306 bswap_ehdr(elf_ehdr
);
308 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
311 section_names_shdr
= lttng_ust_elf_get_shdr(elf
, elf
->ehdr
->e_shstrndx
);
312 if (!section_names_shdr
) {
316 elf
->section_names_offset
= section_names_shdr
->sh_offset
;
317 elf
->section_names_size
= section_names_shdr
->sh_size
;
319 free(section_names_shdr
);
323 lttng_ust_elf_destroy(elf
);
328 * Test whether the ELF file is position independent code (PIC)
330 uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf
*elf
)
333 * PIC has and e_type value of ET_DYN, see ELF specification
334 * version 1.1 p. 1-3.
336 return elf
->ehdr
->e_type
== ET_DYN
;
340 * Destroy the given lttng_ust_elf instance.
342 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
)
351 lttng_ust_lock_fd_tracker();
352 ret
= close(elf
->fd
);
354 lttng_ust_delete_fd_from_tracker(elf
->fd
);
359 lttng_ust_unlock_fd_tracker();
368 * Compute the total in-memory size of the ELF file, in bytes.
370 * Returns 0 if successful, -1 if not. On success, the memory size is
371 * returned through the out parameter `memsz`.
373 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
)
376 uint64_t low_addr
= UINT64_MAX
, high_addr
= 0;
378 if (!elf
|| !memsz
) {
382 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
383 struct lttng_ust_elf_phdr
*phdr
;
385 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
391 * Only PT_LOAD segments contribute to memsz. Skip
394 if (phdr
->p_type
!= PT_LOAD
) {
398 low_addr
= min_t(uint64_t, low_addr
, phdr
->p_vaddr
);
399 high_addr
= max_t(uint64_t, high_addr
,
400 phdr
->p_vaddr
+ phdr
->p_memsz
);
405 if (high_addr
< low_addr
) {
406 /* No PT_LOAD segments or corrupted data. */
410 *memsz
= high_addr
- low_addr
;
417 * Internal method used to try and get the build_id from a PT_NOTE
418 * segment ranging from `offset` to `segment_end`.
420 * If the function returns successfully, the out parameter `found`
421 * indicates whether the build id information was present in the
422 * segment or not. If `found` is not 0, the out parameters `build_id`
423 * and `length` will both have been set with the retrieved
426 * Returns 0 on success, -1 if an error occurred.
429 int lttng_ust_elf_get_build_id_from_segment(
430 struct lttng_ust_elf
*elf
, uint8_t **build_id
, size_t *length
,
431 off_t offset
, off_t segment_end
)
433 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
434 size_t _length
= 0; /* Silence old gcc warning. */
436 while (offset
< segment_end
) {
437 struct lttng_ust_elf_nhdr nhdr
;
440 /* Align start of note entry */
441 offset
+= lttng_ust_offset_align(offset
, ELF_NOTE_ENTRY_ALIGN
);
442 if (offset
>= segment_end
) {
446 * We seek manually because if the note isn't the
447 * build id the data following the header will not
450 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
453 if (lttng_ust_read(elf
->fd
, &nhdr
, sizeof(nhdr
))
458 if (!is_elf_native_endian(elf
)) {
459 nhdr
.n_namesz
= bswap_32(nhdr
.n_namesz
);
460 nhdr
.n_descsz
= bswap_32(nhdr
.n_descsz
);
461 nhdr
.n_type
= bswap_32(nhdr
.n_type
);
464 offset
+= sizeof(nhdr
) + nhdr
.n_namesz
;
465 /* Align start of desc entry */
466 offset
+= lttng_ust_offset_align(offset
, ELF_NOTE_DESC_ALIGN
);
468 if (nhdr
.n_type
!= NT_GNU_BUILD_ID
) {
470 * Ignore non build id notes but still
471 * increase the offset.
473 offset
+= nhdr
.n_descsz
;
477 _length
= nhdr
.n_descsz
;
478 _build_id
= zmalloc(sizeof(uint8_t) * _length
);
483 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
486 read_len
= sizeof(*_build_id
) * _length
;
487 if (lttng_ust_read(elf
->fd
, _build_id
, read_len
) < read_len
) {
495 *build_id
= _build_id
;
506 * Retrieve a build ID (an array of bytes) from the corresponding
507 * section in the ELF file. The length of the build ID can be either
508 * 16 or 20 bytes depending on the method used to generate it, hence
509 * the length out parameter.
511 * If the function returns successfully, the out parameter `found`
512 * indicates whether the build id information was present in the ELF
513 * file or not. If `found` is not 0, the out parameters `build_id` and
514 * `length` will both have been set with the retrieved information.
516 * Returns 0 on success, -1 if an error occurred.
518 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
519 size_t *length
, int *found
)
522 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
523 size_t _length
= 0; /* Silence old gcc warning. */
525 if (!elf
|| !build_id
|| !length
|| !found
) {
529 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
530 off_t offset
, segment_end
;
531 struct lttng_ust_elf_phdr
*phdr
;
534 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
539 /* Build ID will be contained in a PT_NOTE segment. */
540 if (phdr
->p_type
!= PT_NOTE
) {
544 offset
= phdr
->p_offset
;
545 segment_end
= offset
+ phdr
->p_filesz
;
546 ret
= lttng_ust_elf_get_build_id_from_segment(
547 elf
, &_build_id
, &_length
, offset
, segment_end
);
559 *build_id
= _build_id
;
573 * Try to retrieve filename and CRC from given ELF section `shdr`.
575 * If the function returns successfully, the out parameter `found`
576 * indicates whether the debug link information was present in the ELF
577 * section or not. If `found` is not 0, the out parameters `filename` and
578 * `crc` will both have been set with the retrieved information.
580 * 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.043535 seconds and 4 git commands to generate.