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
23 #include <lttng/align.h>
24 #include <lttng/ust-elf.h>
29 #include <sys/types.h>
34 #include "lttng-tracer-core.h"
38 #ifndef NT_GNU_BUILD_ID
39 # define NT_GNU_BUILD_ID 3
43 * Retrieve the nth (where n is the `index` argument) phdr (program
44 * header) from the given elf instance.
46 * A pointer to the phdr is returned on success, NULL on failure.
49 struct lttng_ust_elf_phdr
*lttng_ust_elf_get_phdr(struct lttng_ust_elf
*elf
,
52 struct lttng_ust_elf_phdr
*phdr
= NULL
;
59 if (index
>= elf
->ehdr
->e_phnum
) {
63 phdr
= zmalloc(sizeof(struct lttng_ust_elf_phdr
));
68 offset
= (off_t
) elf
->ehdr
->e_phoff
69 + (off_t
) index
* elf
->ehdr
->e_phentsize
;
70 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
74 if (is_elf_32_bit(elf
)) {
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
);
88 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
92 if (!is_elf_native_endian(elf
)) {
95 copy_phdr(elf_phdr
, *phdr
);
106 * Retrieve the nth (where n is the `index` argument) shdr (section
107 * header) from the given elf instance.
109 * A pointer to the shdr is returned on success, NULL on failure.
112 struct lttng_ust_elf_shdr
*lttng_ust_elf_get_shdr(struct lttng_ust_elf
*elf
,
115 struct lttng_ust_elf_shdr
*shdr
= NULL
;
122 if (index
>= elf
->ehdr
->e_shnum
) {
126 shdr
= zmalloc(sizeof(struct lttng_ust_elf_shdr
));
131 offset
= (off_t
) elf
->ehdr
->e_shoff
132 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
133 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
137 if (is_elf_32_bit(elf
)) {
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
);
151 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
152 < sizeof(elf_shdr
)) {
155 if (!is_elf_native_endian(elf
)) {
156 bswap_shdr(elf_shdr
);
158 copy_shdr(elf_shdr
, *shdr
);
169 * Lookup a section's name from a given offset (usually from an shdr's
170 * sh_name value) in bytes relative to the beginning of the section
171 * names string table.
173 * If no name is found, NULL is returned.
176 char *lttng_ust_elf_get_section_name(struct lttng_ust_elf
*elf
, off_t offset
)
179 size_t len
= 0, to_read
; /* len does not include \0 */
185 if (offset
>= elf
->section_names_size
) {
189 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
193 to_read
= elf
->section_names_size
- offset
;
195 /* Find first \0 after or at current location, remember len. */
204 read_len
= lttng_ust_read(elf
->fd
, buf
,
205 min_t(size_t, BUF_LEN
, to_read
));
209 for (i
= 0; i
< read_len
; i
++) {
210 if (buf
[i
] == '\0') {
219 name
= zmalloc(sizeof(char) * (len
+ 1)); /* + 1 for \0 */
223 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
,
227 if (lttng_ust_read(elf
->fd
, name
, len
+ 1) < len
+ 1) {
239 * Create an instance of lttng_ust_elf for the ELF file located at
242 * Return a pointer to the instance on success, NULL on failure.
244 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
)
246 uint8_t e_ident
[EI_NIDENT
];
247 struct lttng_ust_elf_shdr
*section_names_shdr
;
248 struct lttng_ust_elf
*elf
= NULL
;
251 elf
= zmalloc(sizeof(struct lttng_ust_elf
));
256 /* Initialize fd field to -1. 0 is a valid fd number */
259 elf
->path
= strdup(path
);
264 lttng_ust_lock_fd_tracker();
265 fd
= open(elf
->path
, O_RDONLY
| O_CLOEXEC
);
267 lttng_ust_unlock_fd_tracker();
271 ret
= lttng_ust_add_fd_to_tracker(fd
);
275 PERROR("close on elf->fd");
278 lttng_ust_unlock_fd_tracker();
282 lttng_ust_unlock_fd_tracker();
284 if (lttng_ust_read(elf
->fd
, e_ident
, EI_NIDENT
) < EI_NIDENT
) {
287 elf
->bitness
= e_ident
[EI_CLASS
];
288 elf
->endianness
= e_ident
[EI_DATA
];
290 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
294 elf
->ehdr
= zmalloc(sizeof(struct lttng_ust_elf_ehdr
));
299 if (is_elf_32_bit(elf
)) {
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
));
313 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
314 < sizeof(elf_ehdr
)) {
317 if (!is_elf_native_endian(elf
)) {
318 bswap_ehdr(elf_ehdr
);
320 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
323 section_names_shdr
= lttng_ust_elf_get_shdr(elf
, elf
->ehdr
->e_shstrndx
);
324 if (!section_names_shdr
) {
328 elf
->section_names_offset
= section_names_shdr
->sh_offset
;
329 elf
->section_names_size
= section_names_shdr
->sh_size
;
331 free(section_names_shdr
);
335 lttng_ust_elf_destroy(elf
);
340 * Test whether the ELF file is position independent code (PIC)
342 uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf
*elf
)
345 * PIC has and e_type value of ET_DYN, see ELF specification
346 * version 1.1 p. 1-3.
348 return elf
->ehdr
->e_type
== ET_DYN
;
352 * Destroy the given lttng_ust_elf instance.
354 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
)
363 lttng_ust_lock_fd_tracker();
364 ret
= close(elf
->fd
);
366 lttng_ust_delete_fd_from_tracker(elf
->fd
);
371 lttng_ust_unlock_fd_tracker();
380 * Compute the total in-memory size of the ELF file, in bytes.
382 * Returns 0 if successful, -1 if not. On success, the memory size is
383 * returned through the out parameter `memsz`.
385 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
)
388 uint64_t low_addr
= UINT64_MAX
, high_addr
= 0;
390 if (!elf
|| !memsz
) {
394 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
395 struct lttng_ust_elf_phdr
*phdr
;
397 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
403 * Only PT_LOAD segments contribute to memsz. Skip
406 if (phdr
->p_type
!= PT_LOAD
) {
410 low_addr
= min_t(uint64_t, low_addr
, phdr
->p_vaddr
);
411 high_addr
= max_t(uint64_t, high_addr
,
412 phdr
->p_vaddr
+ phdr
->p_memsz
);
417 if (high_addr
< low_addr
) {
418 /* No PT_LOAD segments or corrupted data. */
422 *memsz
= high_addr
- low_addr
;
429 * Internal method used to try and get the build_id from a PT_NOTE
430 * segment ranging from `offset` to `segment_end`.
432 * If the function returns successfully, the out parameter `found`
433 * indicates whether the build id information was present in the
434 * segment or not. If `found` is not 0, the out parameters `build_id`
435 * and `length` will both have been set with the retrieved
438 * Returns 0 on success, -1 if an error occurred.
441 int lttng_ust_elf_get_build_id_from_segment(
442 struct lttng_ust_elf
*elf
, uint8_t **build_id
, size_t *length
,
443 off_t offset
, off_t segment_end
)
445 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
446 size_t _length
= 0; /* Silence old gcc warning. */
448 while (offset
< segment_end
) {
449 struct lttng_ust_elf_nhdr nhdr
;
452 /* Align start of note entry */
453 offset
+= offset_align(offset
, ELF_NOTE_ENTRY_ALIGN
);
454 if (offset
>= segment_end
) {
458 * We seek manually because if the note isn't the
459 * build id the data following the header will not
462 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
465 if (lttng_ust_read(elf
->fd
, &nhdr
, sizeof(nhdr
))
470 if (!is_elf_native_endian(elf
)) {
471 nhdr
.n_namesz
= bswap_32(nhdr
.n_namesz
);
472 nhdr
.n_descsz
= bswap_32(nhdr
.n_descsz
);
473 nhdr
.n_type
= bswap_32(nhdr
.n_type
);
476 offset
+= sizeof(nhdr
) + nhdr
.n_namesz
;
477 /* Align start of desc entry */
478 offset
+= offset_align(offset
, ELF_NOTE_DESC_ALIGN
);
480 if (nhdr
.n_type
!= NT_GNU_BUILD_ID
) {
482 * Ignore non build id notes but still
483 * increase the offset.
485 offset
+= nhdr
.n_descsz
;
489 _length
= nhdr
.n_descsz
;
490 _build_id
= zmalloc(sizeof(uint8_t) * _length
);
495 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
498 read_len
= sizeof(*_build_id
) * _length
;
499 if (lttng_ust_read(elf
->fd
, _build_id
, read_len
) < read_len
) {
507 *build_id
= _build_id
;
518 * Retrieve a build ID (an array of bytes) from the corresponding
519 * section in the ELF file. The length of the build ID can be either
520 * 16 or 20 bytes depending on the method used to generate it, hence
521 * the length out parameter.
523 * If the function returns successfully, the out parameter `found`
524 * indicates whether the build id information was present in the ELF
525 * file or not. If `found` is not 0, the out parameters `build_id` and
526 * `length` will both have been set with the retrieved information.
528 * Returns 0 on success, -1 if an error occurred.
530 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
531 size_t *length
, int *found
)
534 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
535 size_t _length
= 0; /* Silence old gcc warning. */
537 if (!elf
|| !build_id
|| !length
|| !found
) {
541 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
542 off_t offset
, segment_end
;
543 struct lttng_ust_elf_phdr
*phdr
;
546 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
551 /* Build ID will be contained in a PT_NOTE segment. */
552 if (phdr
->p_type
!= PT_NOTE
) {
556 offset
= phdr
->p_offset
;
557 segment_end
= offset
+ phdr
->p_filesz
;
558 ret
= lttng_ust_elf_get_build_id_from_segment(
559 elf
, &_build_id
, &_length
, offset
, segment_end
);
571 *build_id
= _build_id
;
585 * Try to retrieve filename and CRC from given ELF section `shdr`.
587 * If the function returns successfully, the out parameter `found`
588 * indicates whether the debug link information was present in the ELF
589 * section or not. If `found` is not 0, the out parameters `filename` and
590 * `crc` will both have been set with the retrieved information.
592 * Returns 0 on success, -1 if an error occurred.
594 int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf
*elf
,
595 char **filename
, uint32_t *crc
,
596 struct lttng_ust_elf_shdr
*shdr
)
598 char *_filename
= NULL
; /* Silence old gcc warning. */
600 char *section_name
= NULL
;
601 uint32_t _crc
= 0; /* Silence old gcc warning. */
603 if (!elf
|| !filename
|| !crc
|| !shdr
) {
608 * The .gnu_debuglink section is of type SHT_PROGBITS,
609 * skip the other sections.
611 if (shdr
->sh_type
!= SHT_PROGBITS
) {
615 section_name
= lttng_ust_elf_get_section_name(elf
,
620 if (strcmp(section_name
, ".gnu_debuglink")) {
625 * The length of the filename is the sh_size excluding the CRC
626 * which comes after it in the section.
628 _filename
= zmalloc(sizeof(char) * (shdr
->sh_size
- ELF_CRC_SIZE
));
632 if (lseek(elf
->fd
, shdr
->sh_offset
, SEEK_SET
) < 0) {
635 filename_len
= sizeof(*_filename
) * (shdr
->sh_size
- ELF_CRC_SIZE
);
636 if (lttng_ust_read(elf
->fd
, _filename
, filename_len
) < filename_len
) {
639 if (lttng_ust_read(elf
->fd
, &_crc
, sizeof(_crc
)) < sizeof(_crc
)) {
642 if (!is_elf_native_endian(elf
)) {
643 _crc
= bswap_32(_crc
);
649 *filename
= _filename
;
662 * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any.
664 * If the function returns successfully, the out parameter `found`
665 * indicates whether the debug link information was present in the ELF
666 * file or not. If `found` is not 0, the out parameters `filename` and
667 * `crc` will both have been set with the retrieved information.
669 * Returns 0 on success, -1 if an error occurred.
671 int lttng_ust_elf_get_debug_link(struct lttng_ust_elf
*elf
, char **filename
,
672 uint32_t *crc
, int *found
)
676 char *_filename
= NULL
; /* Silence old gcc warning. */
677 uint32_t _crc
= 0; /* Silence old gcc warning. */
679 if (!elf
|| !filename
|| !crc
|| !found
) {
683 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
684 struct lttng_ust_elf_shdr
*shdr
= NULL
;
686 shdr
= lttng_ust_elf_get_shdr(elf
, i
);
691 ret
= lttng_ust_elf_get_debug_link_from_section(
692 elf
, &_filename
, &_crc
, shdr
);
704 *filename
= _filename
;
This page took 0.061672 seconds and 5 git commands to generate.