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>
25 #include <sys/types.h>
30 #include "lttng-tracer-core.h"
34 #ifndef NT_GNU_BUILD_ID
35 # define NT_GNU_BUILD_ID 3
39 * Retrieve the nth (where n is the `index` argument) phdr (program
40 * header) from the given elf instance.
42 * A pointer to the phdr is returned on success, NULL on failure.
45 struct lttng_ust_elf_phdr
*lttng_ust_elf_get_phdr(struct lttng_ust_elf
*elf
,
48 struct lttng_ust_elf_phdr
*phdr
= NULL
;
55 if (index
>= elf
->ehdr
->e_phnum
) {
59 phdr
= zmalloc(sizeof(struct lttng_ust_elf_phdr
));
64 offset
= (off_t
) elf
->ehdr
->e_phoff
65 + (off_t
) index
* elf
->ehdr
->e_phentsize
;
66 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
70 if (is_elf_32_bit(elf
)) {
73 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
77 if (!is_elf_native_endian(elf
)) {
80 copy_phdr(elf_phdr
, *phdr
);
84 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
88 if (!is_elf_native_endian(elf
)) {
91 copy_phdr(elf_phdr
, *phdr
);
102 * Retrieve the nth (where n is the `index` argument) shdr (section
103 * header) from the given elf instance.
105 * A pointer to the shdr is returned on success, NULL on failure.
108 struct lttng_ust_elf_shdr
*lttng_ust_elf_get_shdr(struct lttng_ust_elf
*elf
,
111 struct lttng_ust_elf_shdr
*shdr
= NULL
;
118 if (index
>= elf
->ehdr
->e_shnum
) {
122 shdr
= zmalloc(sizeof(struct lttng_ust_elf_shdr
));
127 offset
= (off_t
) elf
->ehdr
->e_shoff
128 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
129 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
133 if (is_elf_32_bit(elf
)) {
136 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
137 < sizeof(elf_shdr
)) {
140 if (!is_elf_native_endian(elf
)) {
141 bswap_shdr(elf_shdr
);
143 copy_shdr(elf_shdr
, *shdr
);
147 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
148 < sizeof(elf_shdr
)) {
151 if (!is_elf_native_endian(elf
)) {
152 bswap_shdr(elf_shdr
);
154 copy_shdr(elf_shdr
, *shdr
);
165 * Lookup a section's name from a given offset (usually from an shdr's
166 * sh_name value) in bytes relative to the beginning of the section
167 * names string table.
169 * If no name is found, NULL is returned.
172 char *lttng_ust_elf_get_section_name(struct lttng_ust_elf
*elf
, off_t offset
)
175 size_t len
= 0, to_read
; /* len does not include \0 */
181 if (offset
>= elf
->section_names_size
) {
185 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
189 to_read
= elf
->section_names_size
- offset
;
191 /* Find first \0 after or at current location, remember len. */
200 read_len
= lttng_ust_read(elf
->fd
, buf
,
201 min_t(size_t, BUF_LEN
, to_read
));
205 for (i
= 0; i
< read_len
; i
++) {
206 if (buf
[i
] == '\0') {
215 name
= zmalloc(sizeof(char) * (len
+ 1)); /* + 1 for \0 */
219 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
,
223 if (lttng_ust_read(elf
->fd
, name
, len
+ 1) < len
+ 1) {
235 * Create an instance of lttng_ust_elf for the ELF file located at
238 * Return a pointer to the instance on success, NULL on failure.
240 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
)
242 uint8_t e_ident
[EI_NIDENT
];
243 struct lttng_ust_elf_shdr
*section_names_shdr
;
244 struct lttng_ust_elf
*elf
= NULL
;
246 elf
= zmalloc(sizeof(struct lttng_ust_elf
));
251 elf
->path
= strdup(path
);
256 elf
->fd
= open(elf
->path
, O_RDONLY
| O_CLOEXEC
);
261 if (lttng_ust_read(elf
->fd
, e_ident
, EI_NIDENT
) < EI_NIDENT
) {
264 elf
->bitness
= e_ident
[EI_CLASS
];
265 elf
->endianness
= e_ident
[EI_DATA
];
267 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
271 elf
->ehdr
= zmalloc(sizeof(struct lttng_ust_elf_ehdr
));
276 if (is_elf_32_bit(elf
)) {
279 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
280 < sizeof(elf_ehdr
)) {
283 if (!is_elf_native_endian(elf
)) {
284 bswap_ehdr(elf_ehdr
);
286 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
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
));
300 section_names_shdr
= lttng_ust_elf_get_shdr(elf
, elf
->ehdr
->e_shstrndx
);
301 if (!section_names_shdr
) {
305 elf
->section_names_offset
= section_names_shdr
->sh_offset
;
306 elf
->section_names_size
= section_names_shdr
->sh_size
;
308 free(section_names_shdr
);
315 if (close(elf
->fd
)) {
326 * Test whether the ELF file is position independent code (PIC)
328 uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf
*elf
)
331 * PIC has and e_type value of ET_DYN, see ELF specification
332 * version 1.1 p. 1-3.
334 return elf
->ehdr
->e_type
== ET_DYN
;
338 * Destroy the given lttng_ust_elf instance.
340 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
)
347 if (close(elf
->fd
)) {
355 * Compute the total in-memory size of the ELF file, in bytes.
357 * Returns 0 if successful, -1 if not. On success, the memory size is
358 * returned through the out parameter `memsz`.
360 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
)
363 uint64_t low_addr
= UINT64_MAX
, high_addr
= 0;
365 if (!elf
|| !memsz
) {
369 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
370 struct lttng_ust_elf_phdr
*phdr
;
372 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
378 * Only PT_LOAD segments contribute to memsz. Skip
381 if (phdr
->p_type
!= PT_LOAD
) {
385 low_addr
= min_t(uint64_t, low_addr
, phdr
->p_vaddr
);
386 high_addr
= max_t(uint64_t, high_addr
,
387 phdr
->p_vaddr
+ phdr
->p_memsz
);
392 if (high_addr
< low_addr
) {
393 /* No PT_LOAD segments or corrupted data. */
397 *memsz
= high_addr
- low_addr
;
404 * Internal method used to try and get the build_id from a PT_NOTE
405 * segment ranging from `offset` to `segment_end`.
407 * If the function returns successfully, the out parameter `found`
408 * indicates whether the build id information was present in the
409 * segment or not. If `found` is not 0, the out parameters `build_id`
410 * and `length` will both have been set with the retrieved
413 * Returns 0 on success, -1 if an error occurred.
416 int lttng_ust_elf_get_build_id_from_segment(
417 struct lttng_ust_elf
*elf
, uint8_t **build_id
, size_t *length
,
418 off_t offset
, off_t segment_end
)
420 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
421 size_t _length
= 0; /* Silence old gcc warning. */
423 while (offset
< segment_end
) {
424 struct lttng_ust_elf_nhdr nhdr
;
427 /* Align start of note entry */
428 offset
+= offset_align(offset
, ELF_NOTE_ENTRY_ALIGN
);
429 if (offset
>= segment_end
) {
433 * We seek manually because if the note isn't the
434 * build id the data following the header will not
437 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
440 if (lttng_ust_read(elf
->fd
, &nhdr
, sizeof(nhdr
))
445 if (!is_elf_native_endian(elf
)) {
446 nhdr
.n_namesz
= bswap_32(nhdr
.n_namesz
);
447 nhdr
.n_descsz
= bswap_32(nhdr
.n_descsz
);
448 nhdr
.n_type
= bswap_32(nhdr
.n_type
);
451 offset
+= sizeof(nhdr
) + nhdr
.n_namesz
;
452 /* Align start of desc entry */
453 offset
+= offset_align(offset
, ELF_NOTE_DESC_ALIGN
);
455 if (nhdr
.n_type
!= NT_GNU_BUILD_ID
) {
457 * Ignore non build id notes but still
458 * increase the offset.
460 offset
+= nhdr
.n_descsz
;
464 _length
= nhdr
.n_descsz
;
465 _build_id
= zmalloc(sizeof(uint8_t) * _length
);
470 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
473 read_len
= sizeof(*_build_id
) * _length
;
474 if (lttng_ust_read(elf
->fd
, _build_id
, read_len
) < read_len
) {
482 *build_id
= _build_id
;
493 * Retrieve a build ID (an array of bytes) from the corresponding
494 * section in the ELF file. The length of the build ID can be either
495 * 16 or 20 bytes depending on the method used to generate it, hence
496 * the length out parameter.
498 * If the function returns successfully, the out parameter `found`
499 * indicates whether the build id information was present in the ELF
500 * file or not. If `found` is not 0, the out parameters `build_id` and
501 * `length` will both have been set with the retrieved information.
503 * Returns 0 on success, -1 if an error occurred.
505 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
506 size_t *length
, int *found
)
509 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
510 size_t _length
= 0; /* Silence old gcc warning. */
512 if (!elf
|| !build_id
|| !length
|| !found
) {
516 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
517 off_t offset
, segment_end
;
518 struct lttng_ust_elf_phdr
*phdr
;
521 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
526 /* Build ID will be contained in a PT_NOTE segment. */
527 if (phdr
->p_type
!= PT_NOTE
) {
531 offset
= phdr
->p_offset
;
532 segment_end
= offset
+ phdr
->p_filesz
;
533 ret
= lttng_ust_elf_get_build_id_from_segment(
534 elf
, &_build_id
, &_length
, offset
, segment_end
);
546 *build_id
= _build_id
;
560 * Try to retrieve filename and CRC from given ELF section `shdr`.
562 * If the function returns successfully, the out parameter `found`
563 * indicates whether the debug link information was present in the ELF
564 * section or not. If `found` is not 0, the out parameters `filename` and
565 * `crc` will both have been set with the retrieved information.
567 * Returns 0 on success, -1 if an error occurred.
569 int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf
*elf
,
570 char **filename
, uint32_t *crc
,
571 struct lttng_ust_elf_shdr
*shdr
)
573 char *_filename
= NULL
; /* Silence old gcc warning. */
575 char *section_name
= NULL
;
576 uint32_t _crc
= 0; /* Silence old gcc warning. */
578 if (!elf
|| !filename
|| !crc
|| !shdr
) {
583 * The .gnu_debuglink section is of type SHT_PROGBITS,
584 * skip the other sections.
586 if (shdr
->sh_type
!= SHT_PROGBITS
) {
590 section_name
= lttng_ust_elf_get_section_name(elf
,
595 if (strcmp(section_name
, ".gnu_debuglink")) {
600 * The length of the filename is the sh_size excluding the CRC
601 * which comes after it in the section.
603 _filename
= zmalloc(sizeof(char) * (shdr
->sh_size
- ELF_CRC_SIZE
));
607 if (lseek(elf
->fd
, shdr
->sh_offset
, SEEK_SET
) < 0) {
610 filename_len
= sizeof(*_filename
) * (shdr
->sh_size
- ELF_CRC_SIZE
);
611 if (lttng_ust_read(elf
->fd
, _filename
, filename_len
) < filename_len
) {
614 if (lttng_ust_read(elf
->fd
, &_crc
, sizeof(_crc
)) < sizeof(_crc
)) {
617 if (!is_elf_native_endian(elf
)) {
618 _crc
= bswap_32(_crc
);
624 *filename
= _filename
;
637 * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any.
639 * If the function returns successfully, the out parameter `found`
640 * indicates whether the debug link information was present in the ELF
641 * file or not. If `found` is not 0, the out parameters `filename` and
642 * `crc` will both have been set with the retrieved information.
644 * Returns 0 on success, -1 if an error occurred.
646 int lttng_ust_elf_get_debug_link(struct lttng_ust_elf
*elf
, char **filename
,
647 uint32_t *crc
, int *found
)
651 char *_filename
= NULL
; /* Silence old gcc warning. */
652 uint32_t _crc
= 0; /* Silence old gcc warning. */
654 if (!elf
|| !filename
|| !crc
|| !found
) {
658 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
659 struct lttng_ust_elf_shdr
*shdr
= NULL
;
661 shdr
= lttng_ust_elf_get_shdr(elf
, i
);
666 ret
= lttng_ust_elf_get_debug_link_from_section(
667 elf
, &_filename
, &_crc
, shdr
);
679 *filename
= _filename
;
This page took 0.043712 seconds and 4 git commands to generate.