2 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <lttng/align.h>
23 #include <lttng/ust-elf.h>
28 #include <sys/types.h>
33 #include "lttng-tracer-core.h"
37 #ifndef NT_GNU_BUILD_ID
38 # define NT_GNU_BUILD_ID 3
42 * Retrieve the nth (where n is the `index` argument) phdr (program
43 * header) from the given elf instance.
45 * A pointer to the phdr is returned on success, NULL on failure.
48 struct lttng_ust_elf_phdr
*lttng_ust_elf_get_phdr(struct lttng_ust_elf
*elf
,
51 struct lttng_ust_elf_phdr
*phdr
= NULL
;
58 if (index
>= elf
->ehdr
->e_phnum
) {
62 phdr
= zmalloc(sizeof(struct lttng_ust_elf_phdr
));
67 offset
= (off_t
) elf
->ehdr
->e_phoff
68 + (off_t
) index
* elf
->ehdr
->e_phentsize
;
69 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
73 if (is_elf_32_bit(elf
)) {
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
);
87 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
91 if (!is_elf_native_endian(elf
)) {
94 copy_phdr(elf_phdr
, *phdr
);
105 * Retrieve the nth (where n is the `index` argument) shdr (section
106 * header) from the given elf instance.
108 * A pointer to the shdr is returned on success, NULL on failure.
111 struct lttng_ust_elf_shdr
*lttng_ust_elf_get_shdr(struct lttng_ust_elf
*elf
,
114 struct lttng_ust_elf_shdr
*shdr
= NULL
;
121 if (index
>= elf
->ehdr
->e_shnum
) {
125 shdr
= zmalloc(sizeof(struct lttng_ust_elf_shdr
));
130 offset
= (off_t
) elf
->ehdr
->e_shoff
131 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
132 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
136 if (is_elf_32_bit(elf
)) {
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
);
150 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
151 < sizeof(elf_shdr
)) {
154 if (!is_elf_native_endian(elf
)) {
155 bswap_shdr(elf_shdr
);
157 copy_shdr(elf_shdr
, *shdr
);
168 * Lookup a section's name from a given offset (usually from an shdr's
169 * sh_name value) in bytes relative to the beginning of the section
170 * names string table.
172 * If no name is found, NULL is returned.
175 char *lttng_ust_elf_get_section_name(struct lttng_ust_elf
*elf
, off_t offset
)
178 size_t len
= 0, to_read
; /* len does not include \0 */
184 if (offset
>= elf
->section_names_size
) {
188 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
192 to_read
= elf
->section_names_size
- offset
;
194 /* Find first \0 after or at current location, remember len. */
203 read_len
= lttng_ust_read(elf
->fd
, buf
,
204 min_t(size_t, BUF_LEN
, to_read
));
208 for (i
= 0; i
< read_len
; i
++) {
209 if (buf
[i
] == '\0') {
218 name
= zmalloc(sizeof(char) * (len
+ 1)); /* + 1 for \0 */
222 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
,
226 if (lttng_ust_read(elf
->fd
, name
, len
+ 1) < len
+ 1) {
238 * Create an instance of lttng_ust_elf for the ELF file located at
241 * Return a pointer to the instance on success, NULL on failure.
243 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
)
245 uint8_t e_ident
[EI_NIDENT
];
246 struct lttng_ust_elf_shdr
*section_names_shdr
;
247 struct lttng_ust_elf
*elf
= NULL
;
250 elf
= zmalloc(sizeof(struct lttng_ust_elf
));
255 /* Initialize fd field to -1. 0 is a valid fd number */
258 elf
->path
= strdup(path
);
263 lttng_ust_lock_fd_tracker();
264 fd
= open(elf
->path
, O_RDONLY
| O_CLOEXEC
);
266 lttng_ust_unlock_fd_tracker();
270 ret
= lttng_ust_add_fd_to_tracker(fd
);
274 PERROR("close on elf->fd");
277 lttng_ust_unlock_fd_tracker();
281 lttng_ust_unlock_fd_tracker();
283 if (lttng_ust_read(elf
->fd
, e_ident
, EI_NIDENT
) < EI_NIDENT
) {
286 elf
->bitness
= e_ident
[EI_CLASS
];
287 elf
->endianness
= e_ident
[EI_DATA
];
289 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
293 elf
->ehdr
= zmalloc(sizeof(struct lttng_ust_elf_ehdr
));
298 if (is_elf_32_bit(elf
)) {
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
));
312 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
313 < sizeof(elf_ehdr
)) {
316 if (!is_elf_native_endian(elf
)) {
317 bswap_ehdr(elf_ehdr
);
319 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
322 section_names_shdr
= lttng_ust_elf_get_shdr(elf
, elf
->ehdr
->e_shstrndx
);
323 if (!section_names_shdr
) {
327 elf
->section_names_offset
= section_names_shdr
->sh_offset
;
328 elf
->section_names_size
= section_names_shdr
->sh_size
;
330 free(section_names_shdr
);
334 lttng_ust_elf_destroy(elf
);
339 * Test whether the ELF file is position independent code (PIC)
341 uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf
*elf
)
344 * PIC has and e_type value of ET_DYN, see ELF specification
345 * version 1.1 p. 1-3.
347 return elf
->ehdr
->e_type
== ET_DYN
;
351 * Destroy the given lttng_ust_elf instance.
353 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
)
362 lttng_ust_lock_fd_tracker();
363 ret
= close(elf
->fd
);
365 lttng_ust_delete_fd_from_tracker(elf
->fd
);
370 lttng_ust_unlock_fd_tracker();
379 * Compute the total in-memory size of the ELF file, in bytes.
381 * Returns 0 if successful, -1 if not. On success, the memory size is
382 * returned through the out parameter `memsz`.
384 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
)
387 uint64_t low_addr
= UINT64_MAX
, high_addr
= 0;
389 if (!elf
|| !memsz
) {
393 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
394 struct lttng_ust_elf_phdr
*phdr
;
396 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
402 * Only PT_LOAD segments contribute to memsz. Skip
405 if (phdr
->p_type
!= PT_LOAD
) {
409 low_addr
= min_t(uint64_t, low_addr
, phdr
->p_vaddr
);
410 high_addr
= max_t(uint64_t, high_addr
,
411 phdr
->p_vaddr
+ phdr
->p_memsz
);
416 if (high_addr
< low_addr
) {
417 /* No PT_LOAD segments or corrupted data. */
421 *memsz
= high_addr
- low_addr
;
428 * Internal method used to try and get the build_id from a PT_NOTE
429 * segment ranging from `offset` to `segment_end`.
431 * If the function returns successfully, the out parameter `found`
432 * indicates whether the build id information was present in the
433 * segment or not. If `found` is not 0, the out parameters `build_id`
434 * and `length` will both have been set with the retrieved
437 * Returns 0 on success, -1 if an error occurred.
440 int lttng_ust_elf_get_build_id_from_segment(
441 struct lttng_ust_elf
*elf
, uint8_t **build_id
, size_t *length
,
442 off_t offset
, off_t segment_end
)
444 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
445 size_t _length
= 0; /* Silence old gcc warning. */
447 while (offset
< segment_end
) {
448 struct lttng_ust_elf_nhdr nhdr
;
451 /* Align start of note entry */
452 offset
+= offset_align(offset
, ELF_NOTE_ENTRY_ALIGN
);
453 if (offset
>= segment_end
) {
457 * We seek manually because if the note isn't the
458 * build id the data following the header will not
461 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
464 if (lttng_ust_read(elf
->fd
, &nhdr
, sizeof(nhdr
))
469 if (!is_elf_native_endian(elf
)) {
470 nhdr
.n_namesz
= bswap_32(nhdr
.n_namesz
);
471 nhdr
.n_descsz
= bswap_32(nhdr
.n_descsz
);
472 nhdr
.n_type
= bswap_32(nhdr
.n_type
);
475 offset
+= sizeof(nhdr
) + nhdr
.n_namesz
;
476 /* Align start of desc entry */
477 offset
+= offset_align(offset
, ELF_NOTE_DESC_ALIGN
);
479 if (nhdr
.n_type
!= NT_GNU_BUILD_ID
) {
481 * Ignore non build id notes but still
482 * increase the offset.
484 offset
+= nhdr
.n_descsz
;
488 _length
= nhdr
.n_descsz
;
489 _build_id
= zmalloc(sizeof(uint8_t) * _length
);
494 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
497 read_len
= sizeof(*_build_id
) * _length
;
498 if (lttng_ust_read(elf
->fd
, _build_id
, read_len
) < read_len
) {
506 *build_id
= _build_id
;
517 * Retrieve a build ID (an array of bytes) from the corresponding
518 * section in the ELF file. The length of the build ID can be either
519 * 16 or 20 bytes depending on the method used to generate it, hence
520 * the length out parameter.
522 * If the function returns successfully, the out parameter `found`
523 * indicates whether the build id information was present in the ELF
524 * file or not. If `found` is not 0, the out parameters `build_id` and
525 * `length` will both have been set with the retrieved information.
527 * Returns 0 on success, -1 if an error occurred.
529 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
530 size_t *length
, int *found
)
533 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
534 size_t _length
= 0; /* Silence old gcc warning. */
536 if (!elf
|| !build_id
|| !length
|| !found
) {
540 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
541 off_t offset
, segment_end
;
542 struct lttng_ust_elf_phdr
*phdr
;
545 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
550 /* Build ID will be contained in a PT_NOTE segment. */
551 if (phdr
->p_type
!= PT_NOTE
) {
555 offset
= phdr
->p_offset
;
556 segment_end
= offset
+ phdr
->p_filesz
;
557 ret
= lttng_ust_elf_get_build_id_from_segment(
558 elf
, &_build_id
, &_length
, offset
, segment_end
);
570 *build_id
= _build_id
;
584 * Try to retrieve filename and CRC from given ELF section `shdr`.
586 * If the function returns successfully, the out parameter `found`
587 * indicates whether the debug link information was present in the ELF
588 * section or not. If `found` is not 0, the out parameters `filename` and
589 * `crc` will both have been set with the retrieved information.
591 * Returns 0 on success, -1 if an error occurred.
593 int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf
*elf
,
594 char **filename
, uint32_t *crc
,
595 struct lttng_ust_elf_shdr
*shdr
)
597 char *_filename
= NULL
; /* Silence old gcc warning. */
599 char *section_name
= NULL
;
600 uint32_t _crc
= 0; /* Silence old gcc warning. */
602 if (!elf
|| !filename
|| !crc
|| !shdr
) {
607 * The .gnu_debuglink section is of type SHT_PROGBITS,
608 * skip the other sections.
610 if (shdr
->sh_type
!= SHT_PROGBITS
) {
614 section_name
= lttng_ust_elf_get_section_name(elf
,
619 if (strcmp(section_name
, ".gnu_debuglink")) {
624 * The length of the filename is the sh_size excluding the CRC
625 * which comes after it in the section.
627 _filename
= zmalloc(sizeof(char) * (shdr
->sh_size
- ELF_CRC_SIZE
));
631 if (lseek(elf
->fd
, shdr
->sh_offset
, SEEK_SET
) < 0) {
634 filename_len
= sizeof(*_filename
) * (shdr
->sh_size
- ELF_CRC_SIZE
);
635 if (lttng_ust_read(elf
->fd
, _filename
, filename_len
) < filename_len
) {
638 if (lttng_ust_read(elf
->fd
, &_crc
, sizeof(_crc
)) < sizeof(_crc
)) {
641 if (!is_elf_native_endian(elf
)) {
642 _crc
= bswap_32(_crc
);
648 *filename
= _filename
;
661 * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any.
663 * If the function returns successfully, the out parameter `found`
664 * indicates whether the debug link information was present in the ELF
665 * file or not. If `found` is not 0, the out parameters `filename` and
666 * `crc` will both have been set with the retrieved information.
668 * Returns 0 on success, -1 if an error occurred.
670 int lttng_ust_elf_get_debug_link(struct lttng_ust_elf
*elf
, char **filename
,
671 uint32_t *crc
, int *found
)
675 char *_filename
= NULL
; /* Silence old gcc warning. */
676 uint32_t _crc
= 0; /* Silence old gcc warning. */
678 if (!elf
|| !filename
|| !crc
|| !found
) {
682 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
683 struct lttng_ust_elf_shdr
*shdr
= NULL
;
685 shdr
= lttng_ust_elf_get_shdr(elf
, i
);
690 ret
= lttng_ust_elf_get_debug_link_from_section(
691 elf
, &_filename
, &_crc
, shdr
);
703 *filename
= _filename
;
This page took 0.045442 seconds and 5 git commands to generate.