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>
31 #include "lttng-tracer-core.h"
35 #ifndef NT_GNU_BUILD_ID
36 # define NT_GNU_BUILD_ID 3
40 * Retrieve the nth (where n is the `index` argument) phdr (program
41 * header) from the given elf instance.
43 * A pointer to the phdr is returned on success, NULL on failure.
46 struct lttng_ust_elf_phdr
*lttng_ust_elf_get_phdr(struct lttng_ust_elf
*elf
,
49 struct lttng_ust_elf_phdr
*phdr
= NULL
;
56 if (index
>= elf
->ehdr
->e_phnum
) {
60 phdr
= zmalloc(sizeof(struct lttng_ust_elf_phdr
));
65 offset
= (off_t
) elf
->ehdr
->e_phoff
66 + (off_t
) index
* elf
->ehdr
->e_phentsize
;
67 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
71 if (is_elf_32_bit(elf
)) {
74 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
78 if (!is_elf_native_endian(elf
)) {
81 copy_phdr(elf_phdr
, *phdr
);
85 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
89 if (!is_elf_native_endian(elf
)) {
92 copy_phdr(elf_phdr
, *phdr
);
103 * Retrieve the nth (where n is the `index` argument) shdr (section
104 * header) from the given elf instance.
106 * A pointer to the shdr is returned on success, NULL on failure.
109 struct lttng_ust_elf_shdr
*lttng_ust_elf_get_shdr(struct lttng_ust_elf
*elf
,
112 struct lttng_ust_elf_shdr
*shdr
= NULL
;
119 if (index
>= elf
->ehdr
->e_shnum
) {
123 shdr
= zmalloc(sizeof(struct lttng_ust_elf_shdr
));
128 offset
= (off_t
) elf
->ehdr
->e_shoff
129 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
130 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
134 if (is_elf_32_bit(elf
)) {
137 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
138 < sizeof(elf_shdr
)) {
141 if (!is_elf_native_endian(elf
)) {
142 bswap_shdr(elf_shdr
);
144 copy_shdr(elf_shdr
, *shdr
);
148 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
149 < sizeof(elf_shdr
)) {
152 if (!is_elf_native_endian(elf
)) {
153 bswap_shdr(elf_shdr
);
155 copy_shdr(elf_shdr
, *shdr
);
166 * Lookup a section's name from a given offset (usually from an shdr's
167 * sh_name value) in bytes relative to the beginning of the section
168 * names string table.
170 * If no name is found, NULL is returned.
173 char *lttng_ust_elf_get_section_name(struct lttng_ust_elf
*elf
, off_t offset
)
176 size_t len
= 0, to_read
; /* len does not include \0 */
182 if (offset
>= elf
->section_names_size
) {
186 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
190 to_read
= elf
->section_names_size
- offset
;
192 /* Find first \0 after or at current location, remember len. */
201 read_len
= lttng_ust_read(elf
->fd
, buf
,
202 min_t(size_t, BUF_LEN
, to_read
));
206 for (i
= 0; i
< read_len
; i
++) {
207 if (buf
[i
] == '\0') {
216 name
= zmalloc(sizeof(char) * (len
+ 1)); /* + 1 for \0 */
220 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
,
224 if (lttng_ust_read(elf
->fd
, name
, len
+ 1) < len
+ 1) {
236 * Create an instance of lttng_ust_elf for the ELF file located at
239 * Return a pointer to the instance on success, NULL on failure.
241 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
)
243 uint8_t e_ident
[EI_NIDENT
];
244 struct lttng_ust_elf_shdr
*section_names_shdr
;
245 struct lttng_ust_elf
*elf
= NULL
;
248 elf
= zmalloc(sizeof(struct lttng_ust_elf
));
254 elf
->path
= strdup(path
);
259 lttng_ust_lock_fd_tracker();
260 fd
= open(elf
->path
, O_RDONLY
| O_CLOEXEC
);
262 lttng_ust_unlock_fd_tracker();
266 ret
= lttng_ust_add_fd_to_tracker(fd
);
270 PERROR("close on elf->fd");
273 lttng_ust_unlock_fd_tracker();
277 lttng_ust_unlock_fd_tracker();
279 if (lttng_ust_read(elf
->fd
, e_ident
, EI_NIDENT
) < EI_NIDENT
) {
282 elf
->bitness
= e_ident
[EI_CLASS
];
283 elf
->endianness
= e_ident
[EI_DATA
];
285 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
289 elf
->ehdr
= zmalloc(sizeof(struct lttng_ust_elf_ehdr
));
294 if (is_elf_32_bit(elf
)) {
297 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
298 < sizeof(elf_ehdr
)) {
301 if (!is_elf_native_endian(elf
)) {
302 bswap_ehdr(elf_ehdr
);
304 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
308 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
309 < sizeof(elf_ehdr
)) {
312 if (!is_elf_native_endian(elf
)) {
313 bswap_ehdr(elf_ehdr
);
315 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
318 section_names_shdr
= lttng_ust_elf_get_shdr(elf
, elf
->ehdr
->e_shstrndx
);
319 if (!section_names_shdr
) {
323 elf
->section_names_offset
= section_names_shdr
->sh_offset
;
324 elf
->section_names_size
= section_names_shdr
->sh_size
;
326 free(section_names_shdr
);
330 lttng_ust_elf_destroy(elf
);
335 * Test whether the ELF file is position independent code (PIC)
337 uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf
*elf
)
340 * PIC has and e_type value of ET_DYN, see ELF specification
341 * version 1.1 p. 1-3.
343 return elf
->ehdr
->e_type
== ET_DYN
;
347 * Destroy the given lttng_ust_elf instance.
349 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
)
358 lttng_ust_lock_fd_tracker();
359 ret
= close(elf
->fd
);
361 lttng_ust_delete_fd_from_tracker(elf
->fd
);
366 lttng_ust_unlock_fd_tracker();
375 * Compute the total in-memory size of the ELF file, in bytes.
377 * Returns 0 if successful, -1 if not. On success, the memory size is
378 * returned through the out parameter `memsz`.
380 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
)
383 uint64_t low_addr
= UINT64_MAX
, high_addr
= 0;
385 if (!elf
|| !memsz
) {
389 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
390 struct lttng_ust_elf_phdr
*phdr
;
392 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
398 * Only PT_LOAD segments contribute to memsz. Skip
401 if (phdr
->p_type
!= PT_LOAD
) {
405 low_addr
= min_t(uint64_t, low_addr
, phdr
->p_vaddr
);
406 high_addr
= max_t(uint64_t, high_addr
,
407 phdr
->p_vaddr
+ phdr
->p_memsz
);
412 if (high_addr
< low_addr
) {
413 /* No PT_LOAD segments or corrupted data. */
417 *memsz
= high_addr
- low_addr
;
424 * Internal method used to try and get the build_id from a PT_NOTE
425 * segment ranging from `offset` to `segment_end`.
427 * If the function returns successfully, the out parameter `found`
428 * indicates whether the build id information was present in the
429 * segment or not. If `found` is not 0, the out parameters `build_id`
430 * and `length` will both have been set with the retrieved
433 * Returns 0 on success, -1 if an error occurred.
436 int lttng_ust_elf_get_build_id_from_segment(
437 struct lttng_ust_elf
*elf
, uint8_t **build_id
, size_t *length
,
438 off_t offset
, off_t segment_end
)
440 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
441 size_t _length
= 0; /* Silence old gcc warning. */
443 while (offset
< segment_end
) {
444 struct lttng_ust_elf_nhdr nhdr
;
447 /* Align start of note entry */
448 offset
+= offset_align(offset
, ELF_NOTE_ENTRY_ALIGN
);
449 if (offset
>= segment_end
) {
453 * We seek manually because if the note isn't the
454 * build id the data following the header will not
457 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
460 if (lttng_ust_read(elf
->fd
, &nhdr
, sizeof(nhdr
))
465 if (!is_elf_native_endian(elf
)) {
466 nhdr
.n_namesz
= bswap_32(nhdr
.n_namesz
);
467 nhdr
.n_descsz
= bswap_32(nhdr
.n_descsz
);
468 nhdr
.n_type
= bswap_32(nhdr
.n_type
);
471 offset
+= sizeof(nhdr
) + nhdr
.n_namesz
;
472 /* Align start of desc entry */
473 offset
+= offset_align(offset
, ELF_NOTE_DESC_ALIGN
);
475 if (nhdr
.n_type
!= NT_GNU_BUILD_ID
) {
477 * Ignore non build id notes but still
478 * increase the offset.
480 offset
+= nhdr
.n_descsz
;
484 _length
= nhdr
.n_descsz
;
485 _build_id
= zmalloc(sizeof(uint8_t) * _length
);
490 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
493 read_len
= sizeof(*_build_id
) * _length
;
494 if (lttng_ust_read(elf
->fd
, _build_id
, read_len
) < read_len
) {
502 *build_id
= _build_id
;
513 * Retrieve a build ID (an array of bytes) from the corresponding
514 * section in the ELF file. The length of the build ID can be either
515 * 16 or 20 bytes depending on the method used to generate it, hence
516 * the length out parameter.
518 * If the function returns successfully, the out parameter `found`
519 * indicates whether the build id information was present in the ELF
520 * file or not. If `found` is not 0, the out parameters `build_id` and
521 * `length` will both have been set with the retrieved information.
523 * Returns 0 on success, -1 if an error occurred.
525 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
526 size_t *length
, int *found
)
529 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
530 size_t _length
= 0; /* Silence old gcc warning. */
532 if (!elf
|| !build_id
|| !length
|| !found
) {
536 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
537 off_t offset
, segment_end
;
538 struct lttng_ust_elf_phdr
*phdr
;
541 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
546 /* Build ID will be contained in a PT_NOTE segment. */
547 if (phdr
->p_type
!= PT_NOTE
) {
551 offset
= phdr
->p_offset
;
552 segment_end
= offset
+ phdr
->p_filesz
;
553 ret
= lttng_ust_elf_get_build_id_from_segment(
554 elf
, &_build_id
, &_length
, offset
, segment_end
);
566 *build_id
= _build_id
;
580 * Try to retrieve filename and CRC from given ELF section `shdr`.
582 * If the function returns successfully, the out parameter `found`
583 * indicates whether the debug link information was present in the ELF
584 * section or not. If `found` is not 0, the out parameters `filename` and
585 * `crc` will both have been set with the retrieved information.
587 * Returns 0 on success, -1 if an error occurred.
589 int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf
*elf
,
590 char **filename
, uint32_t *crc
,
591 struct lttng_ust_elf_shdr
*shdr
)
593 char *_filename
= NULL
; /* Silence old gcc warning. */
595 char *section_name
= NULL
;
596 uint32_t _crc
= 0; /* Silence old gcc warning. */
598 if (!elf
|| !filename
|| !crc
|| !shdr
) {
603 * The .gnu_debuglink section is of type SHT_PROGBITS,
604 * skip the other sections.
606 if (shdr
->sh_type
!= SHT_PROGBITS
) {
610 section_name
= lttng_ust_elf_get_section_name(elf
,
615 if (strcmp(section_name
, ".gnu_debuglink")) {
620 * The length of the filename is the sh_size excluding the CRC
621 * which comes after it in the section.
623 _filename
= zmalloc(sizeof(char) * (shdr
->sh_size
- ELF_CRC_SIZE
));
627 if (lseek(elf
->fd
, shdr
->sh_offset
, SEEK_SET
) < 0) {
630 filename_len
= sizeof(*_filename
) * (shdr
->sh_size
- ELF_CRC_SIZE
);
631 if (lttng_ust_read(elf
->fd
, _filename
, filename_len
) < filename_len
) {
634 if (lttng_ust_read(elf
->fd
, &_crc
, sizeof(_crc
)) < sizeof(_crc
)) {
637 if (!is_elf_native_endian(elf
)) {
638 _crc
= bswap_32(_crc
);
644 *filename
= _filename
;
657 * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any.
659 * If the function returns successfully, the out parameter `found`
660 * indicates whether the debug link information was present in the ELF
661 * file or not. If `found` is not 0, the out parameters `filename` and
662 * `crc` will both have been set with the retrieved information.
664 * Returns 0 on success, -1 if an error occurred.
666 int lttng_ust_elf_get_debug_link(struct lttng_ust_elf
*elf
, char **filename
,
667 uint32_t *crc
, int *found
)
671 char *_filename
= NULL
; /* Silence old gcc warning. */
672 uint32_t _crc
= 0; /* Silence old gcc warning. */
674 if (!elf
|| !filename
|| !crc
|| !found
) {
678 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
679 struct lttng_ust_elf_shdr
*shdr
= NULL
;
681 shdr
= lttng_ust_elf_get_shdr(elf
, i
);
686 ret
= lttng_ust_elf_get_debug_link_from_section(
687 elf
, &_filename
, &_crc
, shdr
);
699 *filename
= _filename
;
This page took 0.044166 seconds and 5 git commands to generate.