f5c4f1a1c4988e2e323981e491071e2572f5f527
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>
25 * Retrieve the nth (where n is the `index` argument) phdr (program
26 * header) from the given elf instance.
28 * A pointer to the phdr is returned on success, NULL on failure.
31 struct lttng_ust_elf_phdr
*lttng_ust_elf_get_phdr(struct lttng_ust_elf
*elf
,
34 struct lttng_ust_elf_phdr
*phdr
= NULL
;
41 if (index
>= elf
->ehdr
->e_phnum
) {
45 phdr
= zmalloc(sizeof(struct lttng_ust_elf_phdr
));
50 offset
= elf
->ehdr
->e_phoff
+ index
* elf
->ehdr
->e_phentsize
;
51 if (fseek(elf
->file
, offset
, SEEK_SET
)) {
55 if (is_elf_32_bit(elf
)) {
58 if (!fread(&elf_phdr
, sizeof(elf_phdr
), 1, elf
->file
)) {
61 if (!is_elf_native_endian(elf
)) {
64 copy_phdr(elf_phdr
, *phdr
);
68 if (!fread(&elf_phdr
, sizeof(elf_phdr
), 1, elf
->file
)) {
71 if (!is_elf_native_endian(elf
)) {
74 copy_phdr(elf_phdr
, *phdr
);
85 * Retrieve the nth (where n is the `index` argument) shdr (section
86 * header) from the given elf instance.
88 * A pointer to the shdr is returned on success, NULL on failure.
91 struct lttng_ust_elf_shdr
*lttng_ust_elf_get_shdr(struct lttng_ust_elf
*elf
,
94 struct lttng_ust_elf_shdr
*shdr
= NULL
;
101 if (index
>= elf
->ehdr
->e_shnum
) {
105 shdr
= zmalloc(sizeof(struct lttng_ust_elf_shdr
));
110 offset
= elf
->ehdr
->e_shoff
+ index
* elf
->ehdr
->e_shentsize
;
111 if (fseek(elf
->file
, offset
, SEEK_SET
)) {
115 if (is_elf_32_bit(elf
)) {
118 if (!fread(&elf_shdr
, sizeof(elf_shdr
), 1, elf
->file
)) {
121 if (!is_elf_native_endian(elf
)) {
122 bswap_shdr(elf_shdr
);
124 copy_shdr(elf_shdr
, *shdr
);
128 if (!fread(&elf_shdr
, sizeof(elf_shdr
), 1, elf
->file
)) {
131 if (!is_elf_native_endian(elf
)) {
132 bswap_shdr(elf_shdr
);
134 copy_shdr(elf_shdr
, *shdr
);
145 * Lookup a section's name from a given offset (usually from an shdr's
146 * sh_name value) in bytes relative to the beginning of the section
147 * names string table.
149 * If no name is found, NULL is returned.
152 char *lttng_ust_elf_get_section_name(struct lttng_ust_elf
*elf
, uint32_t offset
)
161 if (offset
>= elf
->section_names_size
) {
165 if (fseek(elf
->file
, elf
->section_names_offset
+ offset
, SEEK_SET
)) {
168 /* Note that len starts at 1, it is not an index. */
169 for (len
= 1; offset
+ len
<= elf
->section_names_size
; ++len
) {
170 switch (fgetc(elf
->file
)) {
180 /* No name was found before the end of the table. */
184 name
= zmalloc(sizeof(char) * len
);
188 if (fseek(elf
->file
, elf
->section_names_offset
+ offset
,
192 if (!fgets(name
, len
, elf
->file
)) {
204 * Create an instance of lttng_ust_elf for the ELF file located at
207 * Return a pointer to the instance on success, NULL on failure.
209 struct lttng_ust_elf
*lttng_ust_elf_create(const char *path
)
211 uint8_t e_ident
[EI_NIDENT
];
212 struct lttng_ust_elf_shdr
*section_names_shdr
;
213 struct lttng_ust_elf
*elf
;
215 elf
= zmalloc(sizeof(struct lttng_ust_elf
));
220 elf
->path
= strdup(path
);
225 elf
->file
= fopen(elf
->path
, "rb");
230 if (!fread(e_ident
, 1, EI_NIDENT
, elf
->file
)) {
233 elf
->bitness
= e_ident
[EI_CLASS
];
234 elf
->endianness
= e_ident
[EI_DATA
];
237 elf
->ehdr
= zmalloc(sizeof(struct lttng_ust_elf_ehdr
));
242 if (is_elf_32_bit(elf
)) {
245 if (!fread(&elf_ehdr
, sizeof(elf_ehdr
), 1, elf
->file
)) {
248 if (!is_elf_native_endian(elf
)) {
249 bswap_ehdr(elf_ehdr
);
251 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
255 if (!fread(&elf_ehdr
, sizeof(elf_ehdr
), 1, elf
->file
)) {
258 if (!is_elf_native_endian(elf
)) {
259 bswap_ehdr(elf_ehdr
);
261 copy_ehdr(elf_ehdr
, *(elf
->ehdr
));
264 section_names_shdr
= lttng_ust_elf_get_shdr(elf
, elf
->ehdr
->e_shstrndx
);
265 if (!section_names_shdr
) {
269 elf
->section_names_offset
= section_names_shdr
->sh_offset
;
270 elf
->section_names_size
= section_names_shdr
->sh_size
;
272 free(section_names_shdr
);
287 * Destroy the given lttng_ust_elf instance.
289 void lttng_ust_elf_destroy(struct lttng_ust_elf
*elf
)
302 * Compute the total in-memory size of the ELF file, in bytes.
304 * Returns 0 if successful, -1 if not. On success, the memory size is
305 * returned through the out parameter `memsz`.
307 int lttng_ust_elf_get_memsz(struct lttng_ust_elf
*elf
, uint64_t *memsz
)
312 if (!elf
|| !memsz
) {
316 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
317 struct lttng_ust_elf_phdr
*phdr
;
320 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
326 * Only PT_LOAD segments contribute to memsz. Skip
329 if (phdr
->p_type
!= PT_LOAD
) {
334 * A p_align of 0 means no alignment, i.e. aligned to
337 align
= phdr
->p_align
== 0 ? 1 : phdr
->p_align
;
338 /* Align the start of the segment. */
339 _memsz
+= offset_align(_memsz
, align
);
340 _memsz
+= phdr
->p_memsz
;
342 * Add padding at the end of the segment, so it ends
343 * on a multiple of the align value (which usually
344 * means a page boundary). This makes the computation
345 * valid even in cases where p_align would change from
346 * one segment to the next.
348 _memsz
+= offset_align(_memsz
, align
);
360 * Internal method used to try and get the build_id from a PT_NOTE
361 * segment ranging from `offset` to `segment_end`.
363 * If the function returns successfully, the out parameter `found`
364 * indicates whether the build id information was present in the
365 * segment or not. If `found` is not 0, the out parameters `build_id`
366 * and `length` will both have been set with the retrieved
369 * Returns 0 on success, -1 if an error occurred.
372 int lttng_ust_elf_get_build_id_from_segment(
373 struct lttng_ust_elf
*elf
, uint8_t **build_id
, size_t *length
,
374 uint64_t offset
, uint64_t segment_end
, int *found
)
380 while (offset
< segment_end
) {
381 struct lttng_ust_elf_nhdr nhdr
;
383 /* Align start of note entry */
384 offset
+= offset_align(offset
, ELF_NOTE_ENTRY_ALIGN
);
385 if (offset
>= segment_end
) {
389 * We seek manually because if the note isn't the
390 * build id the data following the header will not
393 if (fseek(elf
->file
, offset
, SEEK_SET
)) {
396 if (!fread(&nhdr
, sizeof(nhdr
), 1, elf
->file
)) {
400 if (!is_elf_native_endian(elf
)) {
401 nhdr
.n_namesz
= bswap_32(nhdr
.n_namesz
);
402 nhdr
.n_descsz
= bswap_32(nhdr
.n_descsz
);
403 nhdr
.n_type
= bswap_32(nhdr
.n_type
);
406 offset
+= sizeof(nhdr
) + nhdr
.n_namesz
;
407 /* Align start of desc entry */
408 offset
+= offset_align(offset
, ELF_NOTE_DESC_ALIGN
);
410 if (nhdr
.n_type
!= NT_GNU_BUILD_ID
) {
412 * Ignore non build id notes but still
413 * increase the offset.
415 offset
+= nhdr
.n_descsz
;
419 _length
= nhdr
.n_descsz
;
420 _build_id
= zmalloc(sizeof(uint8_t) * _length
);
425 if (fseek(elf
->file
, offset
, SEEK_SET
)) {
428 if (!fread(_build_id
, sizeof(*_build_id
), _length
, elf
->file
)) {
437 *build_id
= _build_id
;
448 * Retrieve a build ID (an array of bytes) from the corresponding
449 * section in the ELF file. The length of the build ID can be either
450 * 16 or 20 bytes depending on the method used to generate it, hence
451 * the length out parameter.
453 * If the function returns successfully, the out parameter `found`
454 * indicates whether the build id information was present in the ELF
455 * file or not. If `found` is not 0, the out parameters `build_id` and
456 * `length` will both have been set with the retrieved information.
458 * Returns 0 on success, -1 if an error occurred.
460 int lttng_ust_elf_get_build_id(struct lttng_ust_elf
*elf
, uint8_t **build_id
,
461 size_t *length
, int *found
)
468 if (!elf
|| !build_id
|| !length
|| !found
) {
472 for (i
= 0; i
< elf
->ehdr
->e_phnum
; ++i
) {
473 uint64_t offset
, segment_end
;
474 struct lttng_ust_elf_phdr
*phdr
;
477 phdr
= lttng_ust_elf_get_phdr(elf
, i
);
482 /* Build ID will be contained in a PT_NOTE segment. */
483 if (phdr
->p_type
!= PT_NOTE
) {
487 offset
= phdr
->p_offset
;
488 segment_end
= offset
+ phdr
->p_filesz
;
489 ret
= lttng_ust_elf_get_build_id_from_segment(
490 elf
, &_build_id
, &_length
, offset
, segment_end
,
503 *build_id
= _build_id
;
514 * Try to retrieve filename and CRC from given ELF section `shdr`.
516 * If the function returns successfully, the out parameter `found`
517 * indicates whether the debug link information was present in the ELF
518 * section or not. If `found` is not 0, the out parameters `filename` and
519 * `crc` will both have been set with the retrieved information.
521 * Returns 0 on success, -1 if an error occurred.
524 int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf
*elf
,
525 char **filename
, uint32_t *crc
,
527 struct lttng_ust_elf_shdr
*shdr
)
531 char *section_name
= NULL
;
534 if (!elf
|| !filename
|| !crc
|| !found
|| !shdr
) {
539 * The .gnu_debuglink section is of type SHT_PROGBITS,
540 * skip the other sections.
542 if (shdr
->sh_type
!= SHT_PROGBITS
) {
546 section_name
= lttng_ust_elf_get_section_name(elf
,
551 if (strcmp(section_name
, ".gnu_debuglink")) {
556 * The length of the filename is the sh_size excluding the CRC
557 * which comes after it in the section.
559 _filename
= zmalloc(sizeof(char) * (shdr
->sh_size
- ELF_CRC_SIZE
));
563 if (fseek(elf
->file
, shdr
->sh_offset
, SEEK_SET
)) {
566 if (!fread(_filename
, sizeof(*_filename
), shdr
->sh_size
- ELF_CRC_SIZE
,
570 if (!fread(&_crc
, sizeof(_crc
), 1, elf
->file
)) {
573 if (!is_elf_native_endian(elf
)) {
574 _crc
= bswap_32(_crc
);
582 *filename
= _filename
;
598 * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any.
600 * If the function returns successfully, the out parameter `found`
601 * indicates whether the debug link information was present in the ELF
602 * file or not. If `found` is not 0, the out parameters `filename` and
603 * `crc` will both have been set with the retrieved information.
605 * Returns 0 on success, -1 if an error occurred.
607 int lttng_ust_elf_get_debug_link(struct lttng_ust_elf
*elf
, char **filename
,
608 uint32_t *crc
, int *found
)
616 if (!elf
|| !filename
|| !crc
|| !found
) {
620 for (i
= 0; i
< elf
->ehdr
->e_shnum
; ++i
) {
621 struct lttng_ust_elf_shdr
*shdr
= NULL
;
623 shdr
= lttng_ust_elf_get_shdr(elf
, i
);
628 ret
= lttng_ust_elf_get_debug_link_from_section(
629 elf
, &_filename
, &_crc
, &_found
, shdr
);
641 *filename
= _filename
;
This page took 0.058855 seconds and 4 git commands to generate.