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
21 #include <lttng/align.h>
22 #include <lttng/ust-elf.h>
23 #include <sys/types.h>
27 #include "lttng-tracer-core.h"
32 * Retrieve the nth (where n is the `index` argument) phdr (program
33 * header) from the given elf instance.
35 * A pointer to the phdr is returned on success, NULL on failure.
38 struct lttng_ust_elf_phdr
*lttng_ust_elf_get_phdr(struct lttng_ust_elf
*elf
,
41 struct lttng_ust_elf_phdr
*phdr
= NULL
;
48 if (index
>= elf
->ehdr
->e_phnum
) {
52 phdr
= zmalloc(sizeof(struct lttng_ust_elf_phdr
));
57 offset
= elf
->ehdr
->e_phoff
+ index
* elf
->ehdr
->e_phentsize
;
58 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
62 if (is_elf_32_bit(elf
)) {
65 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
69 if (!is_elf_native_endian(elf
)) {
72 copy_phdr(elf_phdr
, *phdr
);
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
);
94 * Retrieve the nth (where n is the `index` argument) shdr (section
95 * header) from the given elf instance.
97 * A pointer to the shdr is returned on success, NULL on failure.
100 struct lttng_ust_elf_shdr
*lttng_ust_elf_get_shdr(struct lttng_ust_elf
*elf
,
103 struct lttng_ust_elf_shdr
*shdr
= NULL
;
110 if (index
>= elf
->ehdr
->e_shnum
) {
114 shdr
= zmalloc(sizeof(struct lttng_ust_elf_shdr
));
119 offset
= elf
->ehdr
->e_shoff
+ index
* elf
->ehdr
->e_shentsize
;
120 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
124 if (is_elf_32_bit(elf
)) {
127 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
128 < sizeof(elf_shdr
)) {
131 if (!is_elf_native_endian(elf
)) {
132 bswap_shdr(elf_shdr
);
134 copy_shdr(elf_shdr
, *shdr
);
138 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
139 < sizeof(elf_shdr
)) {
142 if (!is_elf_native_endian(elf
)) {
143 bswap_shdr(elf_shdr
);
145 copy_shdr(elf_shdr
, *shdr
);
156 * Lookup a section's name from a given offset (usually from an shdr's
157 * sh_name value) in bytes relative to the beginning of the section
158 * names string table.
160 * If no name is found, NULL is returned.
163 char *lttng_ust_elf_get_section_name(struct lttng_ust_elf
*elf
, uint32_t offset
)
166 size_t len
= 0, to_read
; /* len does not include \0 */
172 if (offset
>= elf
->section_names_size
) {
176 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
180 to_read
= elf
->section_names_size
- offset
;
182 /* Find first \0 after or at current location, remember len. */
191 read_len
= lttng_ust_read(elf
->fd
, buf
,
192 min_t(size_t, BUF_LEN
, to_read
));
196 for (i
= 0; i
< read_len
; i
++) {
197 if (buf
[i
] == '\0') {
206 name
= zmalloc(sizeof(char) * (len
+ 1)); /* + 1 for \0 */
210 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
,
214 if (lttng_ust_read(elf
->fd
, name
, len
+ 1) < len
+ 1) {
226 * Create an instance of lttng_ust_elf for the ELF file located at
229 * Return a pointer to the instance on success, NULL on failure.
231 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
)
233 uint8_t e_ident
[EI_NIDENT
];
234 struct lttng_ust_elf_shdr
*section_names_shdr
;
235 struct lttng_ust_elf
*elf
= NULL
;
237 elf
= zmalloc(sizeof(struct lttng_ust_elf
));
242 elf
->path
= strdup(path
);
247 elf
->fd
= open(elf
->path
, O_RDONLY
| O_CLOEXEC
);
252 if (lttng_ust_read(elf
->fd
, e_ident
, EI_NIDENT
) < EI_NIDENT
) {
255 elf
->bitness
= e_ident
[EI_CLASS
];
256 elf
->endianness
= e_ident
[EI_DATA
];
258 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
262 elf
->ehdr
= zmalloc(sizeof(struct lttng_ust_elf_ehdr
));
267 if (is_elf_32_bit(elf
)) {
270 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
271 < sizeof(elf_ehdr
)) {
274 if (!is_elf_native_endian(elf
)) {
275 bswap_ehdr(elf_ehdr
);
277 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
281 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
282 < sizeof(elf_ehdr
)) {
285 if (!is_elf_native_endian(elf
)) {
286 bswap_ehdr(elf_ehdr
);
288 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
291 section_names_shdr
= lttng_ust_elf_get_shdr(elf
, elf
->ehdr
->e_shstrndx
);
292 if (!section_names_shdr
) {
296 elf
->section_names_offset
= section_names_shdr
->sh_offset
;
297 elf
->section_names_size
= section_names_shdr
->sh_size
;
299 free(section_names_shdr
);
306 if (close(elf
->fd
)) {
317 * Destroy the given lttng_ust_elf instance.
319 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
)
326 if (close(elf
->fd
)) {
334 * Compute the total in-memory size of the ELF file, in bytes.
336 * Returns 0 if successful, -1 if not. On success, the memory size is
337 * returned through the out parameter `memsz`.
339 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
)
344 if (!elf
|| !memsz
) {
348 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
349 struct lttng_ust_elf_phdr
*phdr
;
352 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
358 * Only PT_LOAD segments contribute to memsz. Skip
361 if (phdr
->p_type
!= PT_LOAD
) {
366 * A p_align of 0 means no alignment, i.e. aligned to
369 align
= phdr
->p_align
== 0 ? 1 : phdr
->p_align
;
370 /* Align the start of the segment. */
371 _memsz
+= offset_align(_memsz
, align
);
372 _memsz
+= phdr
->p_memsz
;
374 * Add padding at the end of the segment, so it ends
375 * on a multiple of the align value (which usually
376 * means a page boundary). This makes the computation
377 * valid even in cases where p_align would change from
378 * one segment to the next.
380 _memsz
+= offset_align(_memsz
, align
);
392 * Internal method used to try and get the build_id from a PT_NOTE
393 * segment ranging from `offset` to `segment_end`.
395 * If the function returns successfully, the out parameter `found`
396 * indicates whether the build id information was present in the
397 * segment or not. If `found` is not 0, the out parameters `build_id`
398 * and `length` will both have been set with the retrieved
401 * Returns 0 on success, -1 if an error occurred.
404 int lttng_ust_elf_get_build_id_from_segment(
405 struct lttng_ust_elf
*elf
, uint8_t **build_id
, size_t *length
,
406 uint64_t offset
, uint64_t segment_end
, int *found
)
412 while (offset
< segment_end
) {
413 struct lttng_ust_elf_nhdr nhdr
;
416 /* Align start of note entry */
417 offset
+= offset_align(offset
, ELF_NOTE_ENTRY_ALIGN
);
418 if (offset
>= segment_end
) {
422 * We seek manually because if the note isn't the
423 * build id the data following the header will not
426 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
429 if (lttng_ust_read(elf
->fd
, &nhdr
, sizeof(nhdr
))
434 if (!is_elf_native_endian(elf
)) {
435 nhdr
.n_namesz
= bswap_32(nhdr
.n_namesz
);
436 nhdr
.n_descsz
= bswap_32(nhdr
.n_descsz
);
437 nhdr
.n_type
= bswap_32(nhdr
.n_type
);
440 offset
+= sizeof(nhdr
) + nhdr
.n_namesz
;
441 /* Align start of desc entry */
442 offset
+= offset_align(offset
, ELF_NOTE_DESC_ALIGN
);
444 if (nhdr
.n_type
!= NT_GNU_BUILD_ID
) {
446 * Ignore non build id notes but still
447 * increase the offset.
449 offset
+= nhdr
.n_descsz
;
453 _length
= nhdr
.n_descsz
;
454 _build_id
= zmalloc(sizeof(uint8_t) * _length
);
459 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
462 read_len
= sizeof(*_build_id
) * _length
;
463 if (lttng_ust_read(elf
->fd
, _build_id
, read_len
) < read_len
) {
472 *build_id
= _build_id
;
483 * Retrieve a build ID (an array of bytes) from the corresponding
484 * section in the ELF file. The length of the build ID can be either
485 * 16 or 20 bytes depending on the method used to generate it, hence
486 * the length out parameter.
488 * If the function returns successfully, the out parameter `found`
489 * indicates whether the build id information was present in the ELF
490 * file or not. If `found` is not 0, the out parameters `build_id` and
491 * `length` will both have been set with the retrieved information.
493 * Returns 0 on success, -1 if an error occurred.
495 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
496 size_t *length
, int *found
)
503 if (!elf
|| !build_id
|| !length
|| !found
) {
507 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
508 uint64_t offset
, segment_end
;
509 struct lttng_ust_elf_phdr
*phdr
;
512 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
517 /* Build ID will be contained in a PT_NOTE segment. */
518 if (phdr
->p_type
!= PT_NOTE
) {
522 offset
= phdr
->p_offset
;
523 segment_end
= offset
+ phdr
->p_filesz
;
524 ret
= lttng_ust_elf_get_build_id_from_segment(
525 elf
, &_build_id
, &_length
, offset
, segment_end
,
538 *build_id
= _build_id
;
549 * Try to retrieve filename and CRC from given ELF section `shdr`.
551 * If the function returns successfully, the out parameter `found`
552 * indicates whether the debug link information was present in the ELF
553 * section or not. If `found` is not 0, the out parameters `filename` and
554 * `crc` will both have been set with the retrieved information.
556 * Returns 0 on success, -1 if an error occurred.
558 int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf
*elf
,
559 char **filename
, uint32_t *crc
,
561 struct lttng_ust_elf_shdr
*shdr
)
566 char *section_name
= NULL
;
569 if (!elf
|| !filename
|| !crc
|| !found
|| !shdr
) {
574 * The .gnu_debuglink section is of type SHT_PROGBITS,
575 * skip the other sections.
577 if (shdr
->sh_type
!= SHT_PROGBITS
) {
581 section_name
= lttng_ust_elf_get_section_name(elf
,
586 if (strcmp(section_name
, ".gnu_debuglink")) {
591 * The length of the filename is the sh_size excluding the CRC
592 * which comes after it in the section.
594 _filename
= zmalloc(sizeof(char) * (shdr
->sh_size
- ELF_CRC_SIZE
));
598 if (lseek(elf
->fd
, shdr
->sh_offset
, SEEK_SET
) < 0) {
601 filename_len
= sizeof(*_filename
) * (shdr
->sh_size
- ELF_CRC_SIZE
);
602 if (lttng_ust_read(elf
->fd
, _filename
, filename_len
) < filename_len
) {
605 if (lttng_ust_read(elf
->fd
, &_crc
, sizeof(_crc
)) < sizeof(_crc
)) {
608 if (!is_elf_native_endian(elf
)) {
609 _crc
= bswap_32(_crc
);
617 *filename
= _filename
;
633 * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any.
635 * If the function returns successfully, the out parameter `found`
636 * indicates whether the debug link information was present in the ELF
637 * file or not. If `found` is not 0, the out parameters `filename` and
638 * `crc` will both have been set with the retrieved information.
640 * Returns 0 on success, -1 if an error occurred.
642 int lttng_ust_elf_get_debug_link(struct lttng_ust_elf
*elf
, char **filename
,
643 uint32_t *crc
, int *found
)
651 if (!elf
|| !filename
|| !crc
|| !found
) {
655 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
656 struct lttng_ust_elf_shdr
*shdr
= NULL
;
658 shdr
= lttng_ust_elf_get_shdr(elf
, i
);
663 ret
= lttng_ust_elf_get_debug_link_from_section(
664 elf
, &_filename
, &_crc
, &_found
, shdr
);
676 *filename
= _filename
;
This page took 0.043391 seconds and 5 git commands to generate.