663699b7ce91a5d9febf0d9903b64d042a580782
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"
35 * Retrieve the nth (where n is the `index` argument) phdr (program
36 * header) from the given elf instance.
38 * A pointer to the phdr is returned on success, NULL on failure.
41 struct lttng_ust_elf_phdr
*lttng_ust_elf_get_phdr(struct lttng_ust_elf
*elf
,
44 struct lttng_ust_elf_phdr
*phdr
= NULL
;
51 if (index
>= elf
->ehdr
->e_phnum
) {
55 phdr
= zmalloc(sizeof(struct lttng_ust_elf_phdr
));
60 offset
= (off_t
) elf
->ehdr
->e_phoff
61 + (off_t
) index
* elf
->ehdr
->e_phentsize
;
62 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
66 if (is_elf_32_bit(elf
)) {
69 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
73 if (!is_elf_native_endian(elf
)) {
76 copy_phdr(elf_phdr
, *phdr
);
80 if (lttng_ust_read(elf
->fd
, &elf_phdr
, sizeof(elf_phdr
))
84 if (!is_elf_native_endian(elf
)) {
87 copy_phdr(elf_phdr
, *phdr
);
98 * Retrieve the nth (where n is the `index` argument) shdr (section
99 * header) from the given elf instance.
101 * A pointer to the shdr is returned on success, NULL on failure.
104 struct lttng_ust_elf_shdr
*lttng_ust_elf_get_shdr(struct lttng_ust_elf
*elf
,
107 struct lttng_ust_elf_shdr
*shdr
= NULL
;
114 if (index
>= elf
->ehdr
->e_shnum
) {
118 shdr
= zmalloc(sizeof(struct lttng_ust_elf_shdr
));
123 offset
= (off_t
) elf
->ehdr
->e_shoff
124 + (off_t
) index
* elf
->ehdr
->e_shentsize
;
125 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
129 if (is_elf_32_bit(elf
)) {
132 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
133 < sizeof(elf_shdr
)) {
136 if (!is_elf_native_endian(elf
)) {
137 bswap_shdr(elf_shdr
);
139 copy_shdr(elf_shdr
, *shdr
);
143 if (lttng_ust_read(elf
->fd
, &elf_shdr
, sizeof(elf_shdr
))
144 < sizeof(elf_shdr
)) {
147 if (!is_elf_native_endian(elf
)) {
148 bswap_shdr(elf_shdr
);
150 copy_shdr(elf_shdr
, *shdr
);
161 * Lookup a section's name from a given offset (usually from an shdr's
162 * sh_name value) in bytes relative to the beginning of the section
163 * names string table.
165 * If no name is found, NULL is returned.
168 char *lttng_ust_elf_get_section_name(struct lttng_ust_elf
*elf
, off_t offset
)
171 size_t len
= 0, to_read
; /* len does not include \0 */
177 if (offset
>= elf
->section_names_size
) {
181 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
, SEEK_SET
) < 0) {
185 to_read
= elf
->section_names_size
- offset
;
187 /* Find first \0 after or at current location, remember len. */
196 read_len
= lttng_ust_read(elf
->fd
, buf
,
197 min_t(size_t, BUF_LEN
, to_read
));
201 for (i
= 0; i
< read_len
; i
++) {
202 if (buf
[i
] == '\0') {
211 name
= zmalloc(sizeof(char) * (len
+ 1)); /* + 1 for \0 */
215 if (lseek(elf
->fd
, elf
->section_names_offset
+ offset
,
219 if (lttng_ust_read(elf
->fd
, name
, len
+ 1) < len
+ 1) {
231 * Create an instance of lttng_ust_elf for the ELF file located at
234 * Return a pointer to the instance on success, NULL on failure.
236 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
)
238 uint8_t e_ident
[EI_NIDENT
];
239 struct lttng_ust_elf_shdr
*section_names_shdr
;
240 struct lttng_ust_elf
*elf
= NULL
;
242 elf
= zmalloc(sizeof(struct lttng_ust_elf
));
247 elf
->path
= strdup(path
);
252 elf
->fd
= open(elf
->path
, O_RDONLY
| O_CLOEXEC
);
257 if (lttng_ust_read(elf
->fd
, e_ident
, EI_NIDENT
) < EI_NIDENT
) {
260 elf
->bitness
= e_ident
[EI_CLASS
];
261 elf
->endianness
= e_ident
[EI_DATA
];
263 if (lseek(elf
->fd
, 0, SEEK_SET
) < 0) {
267 elf
->ehdr
= zmalloc(sizeof(struct lttng_ust_elf_ehdr
));
272 if (is_elf_32_bit(elf
)) {
275 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
276 < sizeof(elf_ehdr
)) {
279 if (!is_elf_native_endian(elf
)) {
280 bswap_ehdr(elf_ehdr
);
282 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
286 if (lttng_ust_read(elf
->fd
, &elf_ehdr
, sizeof(elf_ehdr
))
287 < sizeof(elf_ehdr
)) {
290 if (!is_elf_native_endian(elf
)) {
291 bswap_ehdr(elf_ehdr
);
293 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
296 section_names_shdr
= lttng_ust_elf_get_shdr(elf
, elf
->ehdr
->e_shstrndx
);
297 if (!section_names_shdr
) {
301 elf
->section_names_offset
= section_names_shdr
->sh_offset
;
302 elf
->section_names_size
= section_names_shdr
->sh_size
;
304 free(section_names_shdr
);
311 if (close(elf
->fd
)) {
322 * Test whether the ELF file is position independent code (PIC)
324 uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf
*elf
)
327 * PIC has and e_type value of ET_DYN, see ELF specification
328 * version 1.1 p. 1-3.
330 return elf
->ehdr
->e_type
== ET_DYN
;
334 * Destroy the given lttng_ust_elf instance.
336 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
)
343 if (close(elf
->fd
)) {
351 * Compute the total in-memory size of the ELF file, in bytes.
353 * Returns 0 if successful, -1 if not. On success, the memory size is
354 * returned through the out parameter `memsz`.
356 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
)
359 uint64_t low_addr
= UINT64_MAX
, high_addr
= 0;
361 if (!elf
|| !memsz
) {
365 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
366 struct lttng_ust_elf_phdr
*phdr
;
368 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
374 * Only PT_LOAD segments contribute to memsz. Skip
377 if (phdr
->p_type
!= PT_LOAD
) {
381 low_addr
= min_t(uint64_t, low_addr
, phdr
->p_vaddr
);
382 high_addr
= max_t(uint64_t, high_addr
,
383 phdr
->p_vaddr
+ phdr
->p_memsz
);
388 if (high_addr
< low_addr
) {
389 /* No PT_LOAD segments or corrupted data. */
393 *memsz
= high_addr
- low_addr
;
400 * Internal method used to try and get the build_id from a PT_NOTE
401 * segment ranging from `offset` to `segment_end`.
403 * If the function returns successfully, the out parameter `found`
404 * indicates whether the build id information was present in the
405 * segment or not. If `found` is not 0, the out parameters `build_id`
406 * and `length` will both have been set with the retrieved
409 * Returns 0 on success, -1 if an error occurred.
412 int lttng_ust_elf_get_build_id_from_segment(
413 struct lttng_ust_elf
*elf
, uint8_t **build_id
, size_t *length
,
414 off_t offset
, off_t segment_end
)
416 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
417 size_t _length
= 0; /* Silence old gcc warning. */
419 while (offset
< segment_end
) {
420 struct lttng_ust_elf_nhdr nhdr
;
423 /* Align start of note entry */
424 offset
+= offset_align(offset
, ELF_NOTE_ENTRY_ALIGN
);
425 if (offset
>= segment_end
) {
429 * We seek manually because if the note isn't the
430 * build id the data following the header will not
433 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
436 if (lttng_ust_read(elf
->fd
, &nhdr
, sizeof(nhdr
))
441 if (!is_elf_native_endian(elf
)) {
442 nhdr
.n_namesz
= bswap_32(nhdr
.n_namesz
);
443 nhdr
.n_descsz
= bswap_32(nhdr
.n_descsz
);
444 nhdr
.n_type
= bswap_32(nhdr
.n_type
);
447 offset
+= sizeof(nhdr
) + nhdr
.n_namesz
;
448 /* Align start of desc entry */
449 offset
+= offset_align(offset
, ELF_NOTE_DESC_ALIGN
);
451 if (nhdr
.n_type
!= NT_GNU_BUILD_ID
) {
453 * Ignore non build id notes but still
454 * increase the offset.
456 offset
+= nhdr
.n_descsz
;
460 _length
= nhdr
.n_descsz
;
461 _build_id
= zmalloc(sizeof(uint8_t) * _length
);
466 if (lseek(elf
->fd
, offset
, SEEK_SET
) < 0) {
469 read_len
= sizeof(*_build_id
) * _length
;
470 if (lttng_ust_read(elf
->fd
, _build_id
, read_len
) < read_len
) {
478 *build_id
= _build_id
;
489 * Retrieve a build ID (an array of bytes) from the corresponding
490 * section in the ELF file. The length of the build ID can be either
491 * 16 or 20 bytes depending on the method used to generate it, hence
492 * the length out parameter.
494 * If the function returns successfully, the out parameter `found`
495 * indicates whether the build id information was present in the ELF
496 * file or not. If `found` is not 0, the out parameters `build_id` and
497 * `length` will both have been set with the retrieved information.
499 * Returns 0 on success, -1 if an error occurred.
501 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
502 size_t *length
, int *found
)
505 uint8_t *_build_id
= NULL
; /* Silence old gcc warning. */
506 size_t _length
= 0; /* Silence old gcc warning. */
508 if (!elf
|| !build_id
|| !length
|| !found
) {
512 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
513 off_t offset
, segment_end
;
514 struct lttng_ust_elf_phdr
*phdr
;
517 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
522 /* Build ID will be contained in a PT_NOTE segment. */
523 if (phdr
->p_type
!= PT_NOTE
) {
527 offset
= phdr
->p_offset
;
528 segment_end
= offset
+ phdr
->p_filesz
;
529 ret
= lttng_ust_elf_get_build_id_from_segment(
530 elf
, &_build_id
, &_length
, offset
, segment_end
);
542 *build_id
= _build_id
;
556 * Try to retrieve filename and CRC from given ELF section `shdr`.
558 * If the function returns successfully, the out parameter `found`
559 * indicates whether the debug link information was present in the ELF
560 * section or not. If `found` is not 0, the out parameters `filename` and
561 * `crc` will both have been set with the retrieved information.
563 * Returns 0 on success, -1 if an error occurred.
565 int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf
*elf
,
566 char **filename
, uint32_t *crc
,
567 struct lttng_ust_elf_shdr
*shdr
)
569 char *_filename
= NULL
; /* Silence old gcc warning. */
571 char *section_name
= NULL
;
572 uint32_t _crc
= 0; /* Silence old gcc warning. */
574 if (!elf
|| !filename
|| !crc
|| !shdr
) {
579 * The .gnu_debuglink section is of type SHT_PROGBITS,
580 * skip the other sections.
582 if (shdr
->sh_type
!= SHT_PROGBITS
) {
586 section_name
= lttng_ust_elf_get_section_name(elf
,
591 if (strcmp(section_name
, ".gnu_debuglink")) {
596 * The length of the filename is the sh_size excluding the CRC
597 * which comes after it in the section.
599 _filename
= zmalloc(sizeof(char) * (shdr
->sh_size
- ELF_CRC_SIZE
));
603 if (lseek(elf
->fd
, shdr
->sh_offset
, SEEK_SET
) < 0) {
606 filename_len
= sizeof(*_filename
) * (shdr
->sh_size
- ELF_CRC_SIZE
);
607 if (lttng_ust_read(elf
->fd
, _filename
, filename_len
) < filename_len
) {
610 if (lttng_ust_read(elf
->fd
, &_crc
, sizeof(_crc
)) < sizeof(_crc
)) {
613 if (!is_elf_native_endian(elf
)) {
614 _crc
= bswap_32(_crc
);
620 *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
)
647 char *_filename
= NULL
; /* Silence old gcc warning. */
648 uint32_t _crc
= 0; /* Silence old gcc warning. */
650 if (!elf
|| !filename
|| !crc
|| !found
) {
654 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
655 struct lttng_ust_elf_shdr
*shdr
= NULL
;
657 shdr
= lttng_ust_elf_get_shdr(elf
, i
);
662 ret
= lttng_ust_elf_get_debug_link_from_section(
663 elf
, &_filename
, &_crc
, shdr
);
675 *filename
= _filename
;
This page took 0.057985 seconds and 3 git commands to generate.