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>
28 #include "lttng-tracer-core.h"
33 * Retrieve the nth (where n is the `index` argument) phdr (program
34 * header) from the given elf instance.
36 * A pointer to the phdr is returned on success, NULL on failure.
39 struct lttng_ust_elf_phdr
*lttng_ust_elf_get_phdr(struct lttng_ust_elf
*elf
,
42 struct lttng_ust_elf_phdr
*phdr
= NULL
;
49 if (index
>= elf
->ehdr
->e_phnum
) {
53 phdr
= zmalloc(sizeof(struct lttng_ust_elf_phdr
));
58 offset
= (off_t
) elf
->ehdr
->e_phoff
59 + (off_t
) index
* elf
->ehdr
->e_phentsize
;
60 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
64 if (is_elf_32_bit(elf
)) {
67 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
71 if (!is_elf_native_endian(elf
)) {
74 copy_phdr(elf_phdr
, *phdr
);
78 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
82 if (!is_elf_native_endian(elf
)) {
85 copy_phdr(elf_phdr
, *phdr
);
96 * Retrieve the nth (where n is the `index` argument) shdr (section
97 * header) from the given elf instance.
99 * A pointer to the shdr is returned on success, NULL on failure.
102 struct lttng_ust_elf_shdr
*lttng_ust_elf_get_shdr(struct lttng_ust_elf
*elf
,
105 struct lttng_ust_elf_shdr
*shdr
= NULL
;
112 if (index
>= elf
->ehdr
->e_shnum
) {
116 shdr
= zmalloc(sizeof(struct lttng_ust_elf_shdr
));
121 offset
= (off_t
) elf
->ehdr
->e_shoff
122 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
123 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
127 if (is_elf_32_bit(elf
)) {
130 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
131 < sizeof(elf_shdr
)) {
134 if (!is_elf_native_endian(elf
)) {
135 bswap_shdr(elf_shdr
);
137 copy_shdr(elf_shdr
, *shdr
);
141 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
142 < sizeof(elf_shdr
)) {
145 if (!is_elf_native_endian(elf
)) {
146 bswap_shdr(elf_shdr
);
148 copy_shdr(elf_shdr
, *shdr
);
159 * Lookup a section's name from a given offset (usually from an shdr's
160 * sh_name value) in bytes relative to the beginning of the section
161 * names string table.
163 * If no name is found, NULL is returned.
166 char *lttng_ust_elf_get_section_name(struct lttng_ust_elf
*elf
, off_t offset
)
169 size_t len
= 0, to_read
; /* len does not include \0 */
175 if (offset
>= elf
->section_names_size
) {
179 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
183 to_read
= elf
->section_names_size
- offset
;
185 /* Find first \0 after or at current location, remember len. */
194 read_len
= lttng_ust_read(elf
->fd
, buf
,
195 min_t(size_t, BUF_LEN
, to_read
));
199 for (i
= 0; i
< read_len
; i
++) {
200 if (buf
[i
] == '\0') {
209 name
= zmalloc(sizeof(char) * (len
+ 1)); /* + 1 for \0 */
213 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
,
217 if (lttng_ust_read(elf
->fd
, name
, len
+ 1) < len
+ 1) {
229 * Create an instance of lttng_ust_elf for the ELF file located at
232 * Return a pointer to the instance on success, NULL on failure.
234 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
)
236 uint8_t e_ident
[EI_NIDENT
];
237 struct lttng_ust_elf_shdr
*section_names_shdr
;
238 struct lttng_ust_elf
*elf
= NULL
;
240 elf
= zmalloc(sizeof(struct lttng_ust_elf
));
245 elf
->path
= strdup(path
);
250 elf
->fd
= open(elf
->path
, O_RDONLY
| O_CLOEXEC
);
255 if (lttng_ust_read(elf
->fd
, e_ident
, EI_NIDENT
) < EI_NIDENT
) {
258 elf
->bitness
= e_ident
[EI_CLASS
];
259 elf
->endianness
= e_ident
[EI_DATA
];
261 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
265 elf
->ehdr
= zmalloc(sizeof(struct lttng_ust_elf_ehdr
));
270 if (is_elf_32_bit(elf
)) {
273 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
274 < sizeof(elf_ehdr
)) {
277 if (!is_elf_native_endian(elf
)) {
278 bswap_ehdr(elf_ehdr
);
280 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
284 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
285 < sizeof(elf_ehdr
)) {
288 if (!is_elf_native_endian(elf
)) {
289 bswap_ehdr(elf_ehdr
);
291 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
294 section_names_shdr
= lttng_ust_elf_get_shdr(elf
, elf
->ehdr
->e_shstrndx
);
295 if (!section_names_shdr
) {
299 elf
->section_names_offset
= section_names_shdr
->sh_offset
;
300 elf
->section_names_size
= section_names_shdr
->sh_size
;
302 free(section_names_shdr
);
309 if (close(elf
->fd
)) {
320 * Test whether the ELF file is position independent code (PIC)
322 uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf
*elf
)
325 * PIC has and e_type value of ET_DYN, see ELF specification
326 * version 1.1 p. 1-3.
328 return elf
->ehdr
->e_type
== ET_DYN
;
332 * Destroy the given lttng_ust_elf instance.
334 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
)
341 if (close(elf
->fd
)) {
349 * Compute the total in-memory size of the ELF file, in bytes.
351 * Returns 0 if successful, -1 if not. On success, the memory size is
352 * returned through the out parameter `memsz`.
354 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
)
357 uint64_t low_addr
= UINT64_MAX
, high_addr
= 0;
359 if (!elf
|| !memsz
) {
363 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
364 struct lttng_ust_elf_phdr
*phdr
;
366 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
372 * Only PT_LOAD segments contribute to memsz. Skip
375 if (phdr
->p_type
!= PT_LOAD
) {
379 low_addr
= min_t(uint64_t, low_addr
, phdr
->p_vaddr
);
380 high_addr
= max_t(uint64_t, high_addr
,
381 phdr
->p_vaddr
+ phdr
->p_memsz
);
386 if (high_addr
< low_addr
) {
387 /* No PT_LOAD segments or corrupted data. */
391 *memsz
= high_addr
- low_addr
;
398 * Internal method used to try and get the build_id from a PT_NOTE
399 * segment ranging from `offset` to `segment_end`.
401 * If the function returns successfully, the out parameter `found`
402 * indicates whether the build id information was present in the
403 * segment or not. If `found` is not 0, the out parameters `build_id`
404 * and `length` will both have been set with the retrieved
407 * Returns 0 on success, -1 if an error occurred.
410 int lttng_ust_elf_get_build_id_from_segment(
411 struct lttng_ust_elf
*elf
, uint8_t **build_id
, size_t *length
,
412 off_t offset
, off_t segment_end
)
414 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
415 size_t _length
= 0; /* Silence old gcc warning. */
417 while (offset
< segment_end
) {
418 struct lttng_ust_elf_nhdr nhdr
;
421 /* Align start of note entry */
422 offset
+= offset_align(offset
, ELF_NOTE_ENTRY_ALIGN
);
423 if (offset
>= segment_end
) {
427 * We seek manually because if the note isn't the
428 * build id the data following the header will not
431 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
434 if (lttng_ust_read(elf
->fd
, &nhdr
, sizeof(nhdr
))
439 if (!is_elf_native_endian(elf
)) {
440 nhdr
.n_namesz
= bswap_32(nhdr
.n_namesz
);
441 nhdr
.n_descsz
= bswap_32(nhdr
.n_descsz
);
442 nhdr
.n_type
= bswap_32(nhdr
.n_type
);
445 offset
+= sizeof(nhdr
) + nhdr
.n_namesz
;
446 /* Align start of desc entry */
447 offset
+= offset_align(offset
, ELF_NOTE_DESC_ALIGN
);
449 if (nhdr
.n_type
!= NT_GNU_BUILD_ID
) {
451 * Ignore non build id notes but still
452 * increase the offset.
454 offset
+= nhdr
.n_descsz
;
458 _length
= nhdr
.n_descsz
;
459 _build_id
= zmalloc(sizeof(uint8_t) * _length
);
464 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
467 read_len
= sizeof(*_build_id
) * _length
;
468 if (lttng_ust_read(elf
->fd
, _build_id
, read_len
) < read_len
) {
476 *build_id
= _build_id
;
487 * Retrieve a build ID (an array of bytes) from the corresponding
488 * section in the ELF file. The length of the build ID can be either
489 * 16 or 20 bytes depending on the method used to generate it, hence
490 * the length out parameter.
492 * If the function returns successfully, the out parameter `found`
493 * indicates whether the build id information was present in the ELF
494 * file or not. If `found` is not 0, the out parameters `build_id` and
495 * `length` will both have been set with the retrieved information.
497 * Returns 0 on success, -1 if an error occurred.
499 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
500 size_t *length
, int *found
)
503 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
504 size_t _length
= 0; /* Silence old gcc warning. */
506 if (!elf
|| !build_id
|| !length
|| !found
) {
510 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
511 off_t offset
, segment_end
;
512 struct lttng_ust_elf_phdr
*phdr
;
515 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
520 /* Build ID will be contained in a PT_NOTE segment. */
521 if (phdr
->p_type
!= PT_NOTE
) {
525 offset
= phdr
->p_offset
;
526 segment_end
= offset
+ phdr
->p_filesz
;
527 ret
= lttng_ust_elf_get_build_id_from_segment(
528 elf
, &_build_id
, &_length
, offset
, segment_end
);
540 *build_id
= _build_id
;
554 * Try to retrieve filename and CRC from given ELF section `shdr`.
556 * If the function returns successfully, the out parameter `found`
557 * indicates whether the debug link information was present in the ELF
558 * section or not. If `found` is not 0, the out parameters `filename` and
559 * `crc` will both have been set with the retrieved information.
561 * Returns 0 on success, -1 if an error occurred.
563 int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf
*elf
,
564 char **filename
, uint32_t *crc
,
565 struct lttng_ust_elf_shdr
*shdr
)
567 char *_filename
= NULL
; /* Silence old gcc warning. */
569 char *section_name
= NULL
;
570 uint32_t _crc
= 0; /* Silence old gcc warning. */
572 if (!elf
|| !filename
|| !crc
|| !shdr
) {
577 * The .gnu_debuglink section is of type SHT_PROGBITS,
578 * skip the other sections.
580 if (shdr
->sh_type
!= SHT_PROGBITS
) {
584 section_name
= lttng_ust_elf_get_section_name(elf
,
589 if (strcmp(section_name
, ".gnu_debuglink")) {
594 * The length of the filename is the sh_size excluding the CRC
595 * which comes after it in the section.
597 _filename
= zmalloc(sizeof(char) * (shdr
->sh_size
- ELF_CRC_SIZE
));
601 if (lseek(elf
->fd
, shdr
->sh_offset
, SEEK_SET
) < 0) {
604 filename_len
= sizeof(*_filename
) * (shdr
->sh_size
- ELF_CRC_SIZE
);
605 if (lttng_ust_read(elf
->fd
, _filename
, filename_len
) < filename_len
) {
608 if (lttng_ust_read(elf
->fd
, &_crc
, sizeof(_crc
)) < sizeof(_crc
)) {
611 if (!is_elf_native_endian(elf
)) {
612 _crc
= bswap_32(_crc
);
618 *filename
= _filename
;
631 * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any.
633 * If the function returns successfully, the out parameter `found`
634 * indicates whether the debug link information was present in the ELF
635 * file or not. If `found` is not 0, the out parameters `filename` and
636 * `crc` will both have been set with the retrieved information.
638 * Returns 0 on success, -1 if an error occurred.
640 int lttng_ust_elf_get_debug_link(struct lttng_ust_elf
*elf
, char **filename
,
641 uint32_t *crc
, int *found
)
645 char *_filename
= NULL
; /* Silence old gcc warning. */
646 uint32_t _crc
= 0; /* Silence old gcc warning. */
648 if (!elf
|| !filename
|| !crc
|| !found
) {
652 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
653 struct lttng_ust_elf_shdr
*shdr
= NULL
;
655 shdr
= lttng_ust_elf_get_shdr(elf
, i
);
660 ret
= lttng_ust_elf_get_debug_link_from_section(
661 elf
, &_filename
, &_crc
, shdr
);
673 *filename
= _filename
;
This page took 0.073049 seconds and 5 git commands to generate.