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>
24 #include <sys/types.h>
29 #include "lttng-tracer-core.h"
34 * Retrieve the nth (where n is the `index` argument) phdr (program
35 * header) from the given elf instance.
37 * A pointer to the phdr is returned on success, NULL on failure.
40 struct lttng_ust_elf_phdr
*lttng_ust_elf_get_phdr(struct lttng_ust_elf
*elf
,
43 struct lttng_ust_elf_phdr
*phdr
= NULL
;
50 if (index
>= elf
->ehdr
->e_phnum
) {
54 phdr
= zmalloc(sizeof(struct lttng_ust_elf_phdr
));
59 offset
= (off_t
) elf
->ehdr
->e_phoff
60 + (off_t
) index
* elf
->ehdr
->e_phentsize
;
61 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
65 if (is_elf_32_bit(elf
)) {
68 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
72 if (!is_elf_native_endian(elf
)) {
75 copy_phdr(elf_phdr
, *phdr
);
79 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
83 if (!is_elf_native_endian(elf
)) {
86 copy_phdr(elf_phdr
, *phdr
);
97 * Retrieve the nth (where n is the `index` argument) shdr (section
98 * header) from the given elf instance.
100 * A pointer to the shdr is returned on success, NULL on failure.
103 struct lttng_ust_elf_shdr
*lttng_ust_elf_get_shdr(struct lttng_ust_elf
*elf
,
106 struct lttng_ust_elf_shdr
*shdr
= NULL
;
113 if (index
>= elf
->ehdr
->e_shnum
) {
117 shdr
= zmalloc(sizeof(struct lttng_ust_elf_shdr
));
122 offset
= (off_t
) elf
->ehdr
->e_shoff
123 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
124 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
128 if (is_elf_32_bit(elf
)) {
131 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
132 < sizeof(elf_shdr
)) {
135 if (!is_elf_native_endian(elf
)) {
136 bswap_shdr(elf_shdr
);
138 copy_shdr(elf_shdr
, *shdr
);
142 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
143 < sizeof(elf_shdr
)) {
146 if (!is_elf_native_endian(elf
)) {
147 bswap_shdr(elf_shdr
);
149 copy_shdr(elf_shdr
, *shdr
);
160 * Lookup a section's name from a given offset (usually from an shdr's
161 * sh_name value) in bytes relative to the beginning of the section
162 * names string table.
164 * If no name is found, NULL is returned.
167 char *lttng_ust_elf_get_section_name(struct lttng_ust_elf
*elf
, off_t offset
)
170 size_t len
= 0, to_read
; /* len does not include \0 */
176 if (offset
>= elf
->section_names_size
) {
180 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
184 to_read
= elf
->section_names_size
- offset
;
186 /* Find first \0 after or at current location, remember len. */
195 read_len
= lttng_ust_read(elf
->fd
, buf
,
196 min_t(size_t, BUF_LEN
, to_read
));
200 for (i
= 0; i
< read_len
; i
++) {
201 if (buf
[i
] == '\0') {
210 name
= zmalloc(sizeof(char) * (len
+ 1)); /* + 1 for \0 */
214 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
,
218 if (lttng_ust_read(elf
->fd
, name
, len
+ 1) < len
+ 1) {
230 * Create an instance of lttng_ust_elf for the ELF file located at
233 * Return a pointer to the instance on success, NULL on failure.
235 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
)
237 uint8_t e_ident
[EI_NIDENT
];
238 struct lttng_ust_elf_shdr
*section_names_shdr
;
239 struct lttng_ust_elf
*elf
= NULL
;
241 elf
= zmalloc(sizeof(struct lttng_ust_elf
));
246 elf
->path
= strdup(path
);
251 elf
->fd
= open(elf
->path
, O_RDONLY
| O_CLOEXEC
);
256 if (lttng_ust_read(elf
->fd
, e_ident
, EI_NIDENT
) < EI_NIDENT
) {
259 elf
->bitness
= e_ident
[EI_CLASS
];
260 elf
->endianness
= e_ident
[EI_DATA
];
262 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
266 elf
->ehdr
= zmalloc(sizeof(struct lttng_ust_elf_ehdr
));
271 if (is_elf_32_bit(elf
)) {
274 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
275 < sizeof(elf_ehdr
)) {
278 if (!is_elf_native_endian(elf
)) {
279 bswap_ehdr(elf_ehdr
);
281 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
285 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
286 < sizeof(elf_ehdr
)) {
289 if (!is_elf_native_endian(elf
)) {
290 bswap_ehdr(elf_ehdr
);
292 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
295 section_names_shdr
= lttng_ust_elf_get_shdr(elf
, elf
->ehdr
->e_shstrndx
);
296 if (!section_names_shdr
) {
300 elf
->section_names_offset
= section_names_shdr
->sh_offset
;
301 elf
->section_names_size
= section_names_shdr
->sh_size
;
303 free(section_names_shdr
);
310 if (close(elf
->fd
)) {
321 * Test whether the ELF file is position independent code (PIC)
323 uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf
*elf
)
326 * PIC has and e_type value of ET_DYN, see ELF specification
327 * version 1.1 p. 1-3.
329 return elf
->ehdr
->e_type
== ET_DYN
;
333 * Destroy the given lttng_ust_elf instance.
335 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
)
342 if (close(elf
->fd
)) {
350 * Compute the total in-memory size of the ELF file, in bytes.
352 * Returns 0 if successful, -1 if not. On success, the memory size is
353 * returned through the out parameter `memsz`.
355 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
)
358 uint64_t low_addr
= UINT64_MAX
, high_addr
= 0;
360 if (!elf
|| !memsz
) {
364 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
365 struct lttng_ust_elf_phdr
*phdr
;
367 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
373 * Only PT_LOAD segments contribute to memsz. Skip
376 if (phdr
->p_type
!= PT_LOAD
) {
380 low_addr
= min_t(uint64_t, low_addr
, phdr
->p_vaddr
);
381 high_addr
= max_t(uint64_t, high_addr
,
382 phdr
->p_vaddr
+ phdr
->p_memsz
);
387 if (high_addr
< low_addr
) {
388 /* No PT_LOAD segments or corrupted data. */
392 *memsz
= high_addr
- low_addr
;
399 * Internal method used to try and get the build_id from a PT_NOTE
400 * segment ranging from `offset` to `segment_end`.
402 * If the function returns successfully, the out parameter `found`
403 * indicates whether the build id information was present in the
404 * segment or not. If `found` is not 0, the out parameters `build_id`
405 * and `length` will both have been set with the retrieved
408 * Returns 0 on success, -1 if an error occurred.
411 int lttng_ust_elf_get_build_id_from_segment(
412 struct lttng_ust_elf
*elf
, uint8_t **build_id
, size_t *length
,
413 off_t offset
, off_t segment_end
)
415 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
416 size_t _length
= 0; /* Silence old gcc warning. */
418 while (offset
< segment_end
) {
419 struct lttng_ust_elf_nhdr nhdr
;
422 /* Align start of note entry */
423 offset
+= offset_align(offset
, ELF_NOTE_ENTRY_ALIGN
);
424 if (offset
>= segment_end
) {
428 * We seek manually because if the note isn't the
429 * build id the data following the header will not
432 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
435 if (lttng_ust_read(elf
->fd
, &nhdr
, sizeof(nhdr
))
440 if (!is_elf_native_endian(elf
)) {
441 nhdr
.n_namesz
= bswap_32(nhdr
.n_namesz
);
442 nhdr
.n_descsz
= bswap_32(nhdr
.n_descsz
);
443 nhdr
.n_type
= bswap_32(nhdr
.n_type
);
446 offset
+= sizeof(nhdr
) + nhdr
.n_namesz
;
447 /* Align start of desc entry */
448 offset
+= offset_align(offset
, ELF_NOTE_DESC_ALIGN
);
450 if (nhdr
.n_type
!= NT_GNU_BUILD_ID
) {
452 * Ignore non build id notes but still
453 * increase the offset.
455 offset
+= nhdr
.n_descsz
;
459 _length
= nhdr
.n_descsz
;
460 _build_id
= zmalloc(sizeof(uint8_t) * _length
);
465 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
468 read_len
= sizeof(*_build_id
) * _length
;
469 if (lttng_ust_read(elf
->fd
, _build_id
, read_len
) < read_len
) {
477 *build_id
= _build_id
;
488 * Retrieve a build ID (an array of bytes) from the corresponding
489 * section in the ELF file. The length of the build ID can be either
490 * 16 or 20 bytes depending on the method used to generate it, hence
491 * the length out parameter.
493 * If the function returns successfully, the out parameter `found`
494 * indicates whether the build id information was present in the ELF
495 * file or not. If `found` is not 0, the out parameters `build_id` and
496 * `length` will both have been set with the retrieved information.
498 * Returns 0 on success, -1 if an error occurred.
500 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
501 size_t *length
, int *found
)
504 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
505 size_t _length
= 0; /* Silence old gcc warning. */
507 if (!elf
|| !build_id
|| !length
|| !found
) {
511 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
512 off_t offset
, segment_end
;
513 struct lttng_ust_elf_phdr
*phdr
;
516 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
521 /* Build ID will be contained in a PT_NOTE segment. */
522 if (phdr
->p_type
!= PT_NOTE
) {
526 offset
= phdr
->p_offset
;
527 segment_end
= offset
+ phdr
->p_filesz
;
528 ret
= lttng_ust_elf_get_build_id_from_segment(
529 elf
, &_build_id
, &_length
, offset
, segment_end
);
541 *build_id
= _build_id
;
555 * Try to retrieve filename and CRC from given ELF section `shdr`.
557 * If the function returns successfully, the out parameter `found`
558 * indicates whether the debug link information was present in the ELF
559 * section or not. If `found` is not 0, the out parameters `filename` and
560 * `crc` will both have been set with the retrieved information.
562 * Returns 0 on success, -1 if an error occurred.
564 int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf
*elf
,
565 char **filename
, uint32_t *crc
,
566 struct lttng_ust_elf_shdr
*shdr
)
568 char *_filename
= NULL
; /* Silence old gcc warning. */
570 char *section_name
= NULL
;
571 uint32_t _crc
= 0; /* Silence old gcc warning. */
573 if (!elf
|| !filename
|| !crc
|| !shdr
) {
578 * The .gnu_debuglink section is of type SHT_PROGBITS,
579 * skip the other sections.
581 if (shdr
->sh_type
!= SHT_PROGBITS
) {
585 section_name
= lttng_ust_elf_get_section_name(elf
,
590 if (strcmp(section_name
, ".gnu_debuglink")) {
595 * The length of the filename is the sh_size excluding the CRC
596 * which comes after it in the section.
598 _filename
= zmalloc(sizeof(char) * (shdr
->sh_size
- ELF_CRC_SIZE
));
602 if (lseek(elf
->fd
, shdr
->sh_offset
, SEEK_SET
) < 0) {
605 filename_len
= sizeof(*_filename
) * (shdr
->sh_size
- ELF_CRC_SIZE
);
606 if (lttng_ust_read(elf
->fd
, _filename
, filename_len
) < filename_len
) {
609 if (lttng_ust_read(elf
->fd
, &_crc
, sizeof(_crc
)) < sizeof(_crc
)) {
612 if (!is_elf_native_endian(elf
)) {
613 _crc
= bswap_32(_crc
);
619 *filename
= _filename
;
632 * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any.
634 * If the function returns successfully, the out parameter `found`
635 * indicates whether the debug link information was present in the ELF
636 * file or not. If `found` is not 0, the out parameters `filename` and
637 * `crc` will both have been set with the retrieved information.
639 * Returns 0 on success, -1 if an error occurred.
641 int lttng_ust_elf_get_debug_link(struct lttng_ust_elf
*elf
, char **filename
,
642 uint32_t *crc
, int *found
)
646 char *_filename
= NULL
; /* Silence old gcc warning. */
647 uint32_t _crc
= 0; /* Silence old gcc warning. */
649 if (!elf
|| !filename
|| !crc
|| !found
) {
653 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
654 struct lttng_ust_elf_shdr
*shdr
= NULL
;
656 shdr
= lttng_ust_elf_get_shdr(elf
, i
);
661 ret
= lttng_ust_elf_get_debug_link_from_section(
662 elf
, &_filename
, &_crc
, shdr
);
674 *filename
= _filename
;
This page took 0.043764 seconds and 4 git commands to generate.