Fix: pass private data to context callbacks
[lttng-ust.git] / liblttng-ust / lttng-ust-elf.c
CommitLineData
8e2aed3f 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-or-later
8e2aed3f 3 *
c0c0989a 4 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
8e2aed3f
AB
5 */
6
3fbec7dc 7#define _LGPL_SOURCE
fb31eb73 8#include <fcntl.h>
fb31eb73
FD
9#include <stdbool.h>
10#include <stdint.h>
11#include <string.h>
405be658 12#include <sys/stat.h>
fb31eb73 13#include <sys/types.h>
405be658 14#include <unistd.h>
fb31eb73 15
eae3c729 16#include <lttng/ust-utils.h>
3d3a2bb8
MJ
17
18#include <ust-elf.h>
ba8dcc8c 19#include <ust-fd.h>
fb31eb73 20
405be658 21#include "lttng-tracer-core.h"
2a8db464 22#include "lttng-ust-elf.h"
ddabe860 23#include "ust-helper.h"
405be658
MD
24
25#define BUF_LEN 4096
8e2aed3f 26
2df586a6
PP
27#ifndef NT_GNU_BUILD_ID
28# define NT_GNU_BUILD_ID 3
29#endif
30
8e2aed3f
AB
31/*
32 * Retrieve the nth (where n is the `index` argument) phdr (program
33 * header) from the given elf instance.
34 *
35 * A pointer to the phdr is returned on success, NULL on failure.
36 */
37static
38struct lttng_ust_elf_phdr *lttng_ust_elf_get_phdr(struct lttng_ust_elf *elf,
39 uint16_t index)
40{
41 struct lttng_ust_elf_phdr *phdr = NULL;
f5a6717a 42 off_t offset;
8e2aed3f
AB
43
44 if (!elf) {
45 goto error;
46 }
47
48 if (index >= elf->ehdr->e_phnum) {
49 goto error;
50 }
51
52 phdr = zmalloc(sizeof(struct lttng_ust_elf_phdr));
53 if (!phdr) {
54 goto error;
55 }
56
f5a6717a
MD
57 offset = (off_t) elf->ehdr->e_phoff
58 + (off_t) index * elf->ehdr->e_phentsize;
405be658 59 if (lseek(elf->fd, offset, SEEK_SET) < 0) {
8e2aed3f
AB
60 goto error;
61 }
62
63 if (is_elf_32_bit(elf)) {
64 Elf32_Phdr elf_phdr;
65
405be658
MD
66 if (lttng_ust_read(elf->fd, &elf_phdr, sizeof(elf_phdr))
67 < sizeof(elf_phdr)) {
8e2aed3f
AB
68 goto error;
69 }
70 if (!is_elf_native_endian(elf)) {
71 bswap_phdr(elf_phdr);
72 }
73 copy_phdr(elf_phdr, *phdr);
74 } else {
75 Elf64_Phdr elf_phdr;
76
405be658
MD
77 if (lttng_ust_read(elf->fd, &elf_phdr, sizeof(elf_phdr))
78 < sizeof(elf_phdr)) {
8e2aed3f
AB
79 goto error;
80 }
81 if (!is_elf_native_endian(elf)) {
82 bswap_phdr(elf_phdr);
83 }
84 copy_phdr(elf_phdr, *phdr);
85 }
86
87 return phdr;
88
89error:
90 free(phdr);
91 return NULL;
92}
93
94/*
95 * Retrieve the nth (where n is the `index` argument) shdr (section
96 * header) from the given elf instance.
97 *
98 * A pointer to the shdr is returned on success, NULL on failure.
99 */
100static
101struct lttng_ust_elf_shdr *lttng_ust_elf_get_shdr(struct lttng_ust_elf *elf,
102 uint16_t index)
103{
104 struct lttng_ust_elf_shdr *shdr = NULL;
4c5dac0d 105 off_t offset;
8e2aed3f
AB
106
107 if (!elf) {
108 goto error;
109 }
110
111 if (index >= elf->ehdr->e_shnum) {
112 goto error;
113 }
114
115 shdr = zmalloc(sizeof(struct lttng_ust_elf_shdr));
116 if (!shdr) {
117 goto error;
118 }
119
4c5dac0d 120 offset = (off_t) elf->ehdr->e_shoff
5a26ac54 121 + (off_t) index * elf->ehdr->e_shentsize;
405be658 122 if (lseek(elf->fd, offset, SEEK_SET) < 0) {
8e2aed3f
AB
123 goto error;
124 }
125
126 if (is_elf_32_bit(elf)) {
127 Elf32_Shdr elf_shdr;
128
405be658
MD
129 if (lttng_ust_read(elf->fd, &elf_shdr, sizeof(elf_shdr))
130 < sizeof(elf_shdr)) {
8e2aed3f
AB
131 goto error;
132 }
133 if (!is_elf_native_endian(elf)) {
134 bswap_shdr(elf_shdr);
135 }
136 copy_shdr(elf_shdr, *shdr);
137 } else {
138 Elf64_Shdr elf_shdr;
139
405be658
MD
140 if (lttng_ust_read(elf->fd, &elf_shdr, sizeof(elf_shdr))
141 < sizeof(elf_shdr)) {
8e2aed3f
AB
142 goto error;
143 }
144 if (!is_elf_native_endian(elf)) {
145 bswap_shdr(elf_shdr);
146 }
147 copy_shdr(elf_shdr, *shdr);
148 }
149
150 return shdr;
151
152error:
153 free(shdr);
154 return NULL;
155}
156
157/*
158 * Lookup a section's name from a given offset (usually from an shdr's
159 * sh_name value) in bytes relative to the beginning of the section
160 * names string table.
161 *
162 * If no name is found, NULL is returned.
163 */
164static
f5a6717a 165char *lttng_ust_elf_get_section_name(struct lttng_ust_elf *elf, off_t offset)
8e2aed3f
AB
166{
167 char *name = NULL;
405be658 168 size_t len = 0, to_read; /* len does not include \0 */
8e2aed3f
AB
169
170 if (!elf) {
171 goto error;
172 }
173
174 if (offset >= elf->section_names_size) {
175 goto error;
176 }
177
405be658 178 if (lseek(elf->fd, elf->section_names_offset + offset, SEEK_SET) < 0) {
8e2aed3f
AB
179 goto error;
180 }
405be658
MD
181
182 to_read = elf->section_names_size - offset;
183
184 /* Find first \0 after or at current location, remember len. */
185 for (;;) {
186 char buf[BUF_LEN];
187 ssize_t read_len;
188 size_t i;
189
190 if (!to_read) {
8e2aed3f 191 goto error;
8e2aed3f 192 }
405be658
MD
193 read_len = lttng_ust_read(elf->fd, buf,
194 min_t(size_t, BUF_LEN, to_read));
195 if (read_len <= 0) {
196 goto error;
197 }
198 for (i = 0; i < read_len; i++) {
199 if (buf[i] == '\0') {
200 len += i;
201 goto end;
202 }
203 }
204 len += read_len;
205 to_read -= read_len;
8e2aed3f 206 }
8e2aed3f 207end:
405be658 208 name = zmalloc(sizeof(char) * (len + 1)); /* + 1 for \0 */
8e2aed3f
AB
209 if (!name) {
210 goto error;
211 }
405be658
MD
212 if (lseek(elf->fd, elf->section_names_offset + offset,
213 SEEK_SET) < 0) {
8e2aed3f
AB
214 goto error;
215 }
405be658 216 if (lttng_ust_read(elf->fd, name, len + 1) < len + 1) {
8e2aed3f
AB
217 goto error;
218 }
219
220 return name;
221
222error:
223 free(name);
224 return NULL;
225}
226
227/*
228 * Create an instance of lttng_ust_elf for the ELF file located at
229 * `path`.
230 *
231 * Return a pointer to the instance on success, NULL on failure.
232 */
233struct lttng_ust_elf *lttng_ust_elf_create(const char *path)
234{
235 uint8_t e_ident[EI_NIDENT];
236 struct lttng_ust_elf_shdr *section_names_shdr;
405be658 237 struct lttng_ust_elf *elf = NULL;
f5c453e9 238 int ret, fd;
8e2aed3f
AB
239
240 elf = zmalloc(sizeof(struct lttng_ust_elf));
241 if (!elf) {
242 goto error;
243 }
244
1ac2e79c
JR
245 /* Initialize fd field to -1. 0 is a valid fd number */
246 elf->fd = -1;
ba8dcc8c 247
8e2aed3f
AB
248 elf->path = strdup(path);
249 if (!elf->path) {
250 goto error;
251 }
252
ba8dcc8c 253 lttng_ust_lock_fd_tracker();
f5c453e9
JR
254 fd = open(elf->path, O_RDONLY | O_CLOEXEC);
255 if (fd < 0) {
ba8dcc8c 256 lttng_ust_unlock_fd_tracker();
8e2aed3f
AB
257 goto error;
258 }
f5c453e9
JR
259
260 ret = lttng_ust_add_fd_to_tracker(fd);
261 if (ret < 0) {
262 ret = close(fd);
263 if (ret) {
264 PERROR("close on elf->fd");
265 }
266 ret = -1;
267 lttng_ust_unlock_fd_tracker();
268 goto error;
269 }
270 elf->fd = ret;
ba8dcc8c 271 lttng_ust_unlock_fd_tracker();
8e2aed3f 272
405be658 273 if (lttng_ust_read(elf->fd, e_ident, EI_NIDENT) < EI_NIDENT) {
8e2aed3f
AB
274 goto error;
275 }
276 elf->bitness = e_ident[EI_CLASS];
277 elf->endianness = e_ident[EI_DATA];
405be658
MD
278
279 if (lseek(elf->fd, 0, SEEK_SET) < 0) {
280 goto error;
281 }
8e2aed3f
AB
282
283 elf->ehdr = zmalloc(sizeof(struct lttng_ust_elf_ehdr));
284 if (!elf->ehdr) {
285 goto error;
286 }
287
288 if (is_elf_32_bit(elf)) {
289 Elf32_Ehdr elf_ehdr;
290
405be658
MD
291 if (lttng_ust_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr))
292 < sizeof(elf_ehdr)) {
8e2aed3f
AB
293 goto error;
294 }
295 if (!is_elf_native_endian(elf)) {
296 bswap_ehdr(elf_ehdr);
297 }
298 copy_ehdr(elf_ehdr, *(elf->ehdr));
299 } else {
300 Elf64_Ehdr elf_ehdr;
301
405be658
MD
302 if (lttng_ust_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr))
303 < sizeof(elf_ehdr)) {
8e2aed3f
AB
304 goto error;
305 }
306 if (!is_elf_native_endian(elf)) {
307 bswap_ehdr(elf_ehdr);
308 }
309 copy_ehdr(elf_ehdr, *(elf->ehdr));
310 }
311
312 section_names_shdr = lttng_ust_elf_get_shdr(elf, elf->ehdr->e_shstrndx);
313 if (!section_names_shdr) {
314 goto error;
315 }
316
317 elf->section_names_offset = section_names_shdr->sh_offset;
318 elf->section_names_size = section_names_shdr->sh_size;
319
320 free(section_names_shdr);
8e2aed3f
AB
321 return elf;
322
323error:
ba8dcc8c 324 lttng_ust_elf_destroy(elf);
8e2aed3f
AB
325 return NULL;
326}
327
f5eb039d
AB
328/*
329 * Test whether the ELF file is position independent code (PIC)
330 */
331uint8_t lttng_ust_elf_is_pic(struct lttng_ust_elf *elf)
332{
333 /*
334 * PIC has and e_type value of ET_DYN, see ELF specification
335 * version 1.1 p. 1-3.
336 */
337 return elf->ehdr->e_type == ET_DYN;
338}
339
8e2aed3f
AB
340/*
341 * Destroy the given lttng_ust_elf instance.
342 */
343void lttng_ust_elf_destroy(struct lttng_ust_elf *elf)
344{
ba8dcc8c
JR
345 int ret;
346
8e2aed3f
AB
347 if (!elf) {
348 return;
349 }
350
ba8dcc8c
JR
351 if (elf->fd >= 0) {
352 lttng_ust_lock_fd_tracker();
353 ret = close(elf->fd);
354 if (!ret) {
355 lttng_ust_delete_fd_from_tracker(elf->fd);
356 } else {
357 PERROR("close");
358 abort();
359 }
360 lttng_ust_unlock_fd_tracker();
405be658 361 }
ba8dcc8c
JR
362
363 free(elf->ehdr);
8e2aed3f
AB
364 free(elf->path);
365 free(elf);
366}
367
368/*
369 * Compute the total in-memory size of the ELF file, in bytes.
370 *
371 * Returns 0 if successful, -1 if not. On success, the memory size is
372 * returned through the out parameter `memsz`.
373 */
374int lttng_ust_elf_get_memsz(struct lttng_ust_elf *elf, uint64_t *memsz)
375{
376 uint16_t i;
e1f0c569 377 uint64_t low_addr = UINT64_MAX, high_addr = 0;
8e2aed3f
AB
378
379 if (!elf || !memsz) {
380 goto error;
381 }
382
383 for (i = 0; i < elf->ehdr->e_phnum; ++i) {
384 struct lttng_ust_elf_phdr *phdr;
8e2aed3f
AB
385
386 phdr = lttng_ust_elf_get_phdr(elf, i);
387 if (!phdr) {
388 goto error;
389 }
390
391 /*
392 * Only PT_LOAD segments contribute to memsz. Skip
393 * other segments.
394 */
395 if (phdr->p_type != PT_LOAD) {
396 goto next_loop;
397 }
398
8886accb
MD
399 low_addr = min_t(uint64_t, low_addr, phdr->p_vaddr);
400 high_addr = max_t(uint64_t, high_addr,
401 phdr->p_vaddr + phdr->p_memsz);
8e2aed3f
AB
402 next_loop:
403 free(phdr);
404 }
405
e1f0c569
AB
406 if (high_addr < low_addr) {
407 /* No PT_LOAD segments or corrupted data. */
408 goto error;
409 }
410
411 *memsz = high_addr - low_addr;
8e2aed3f
AB
412 return 0;
413error:
414 return -1;
415}
416
417/*
418 * Internal method used to try and get the build_id from a PT_NOTE
419 * segment ranging from `offset` to `segment_end`.
420 *
421 * If the function returns successfully, the out parameter `found`
422 * indicates whether the build id information was present in the
423 * segment or not. If `found` is not 0, the out parameters `build_id`
424 * and `length` will both have been set with the retrieved
425 * information.
426 *
427 * Returns 0 on success, -1 if an error occurred.
428 */
429static
430int lttng_ust_elf_get_build_id_from_segment(
431 struct lttng_ust_elf *elf, uint8_t **build_id, size_t *length,
82ee1ee4 432 off_t offset, off_t segment_end)
8e2aed3f 433{
814d07b1
MD
434 uint8_t *_build_id = NULL; /* Silence old gcc warning. */
435 size_t _length = 0; /* Silence old gcc warning. */
8e2aed3f
AB
436
437 while (offset < segment_end) {
438 struct lttng_ust_elf_nhdr nhdr;
405be658 439 size_t read_len;
8e2aed3f
AB
440
441 /* Align start of note entry */
b72687b8 442 offset += lttng_ust_offset_align(offset, ELF_NOTE_ENTRY_ALIGN);
8e2aed3f
AB
443 if (offset >= segment_end) {
444 break;
445 }
446 /*
447 * We seek manually because if the note isn't the
448 * build id the data following the header will not
449 * have been read.
450 */
405be658 451 if (lseek(elf->fd, offset, SEEK_SET) < 0) {
8e2aed3f
AB
452 goto error;
453 }
405be658
MD
454 if (lttng_ust_read(elf->fd, &nhdr, sizeof(nhdr))
455 < sizeof(nhdr)) {
8e2aed3f
AB
456 goto error;
457 }
458
459 if (!is_elf_native_endian(elf)) {
460 nhdr.n_namesz = bswap_32(nhdr.n_namesz);
461 nhdr.n_descsz = bswap_32(nhdr.n_descsz);
462 nhdr.n_type = bswap_32(nhdr.n_type);
463 }
464
465 offset += sizeof(nhdr) + nhdr.n_namesz;
466 /* Align start of desc entry */
b72687b8 467 offset += lttng_ust_offset_align(offset, ELF_NOTE_DESC_ALIGN);
8e2aed3f
AB
468
469 if (nhdr.n_type != NT_GNU_BUILD_ID) {
470 /*
471 * Ignore non build id notes but still
472 * increase the offset.
473 */
474 offset += nhdr.n_descsz;
475 continue;
476 }
477
478 _length = nhdr.n_descsz;
479 _build_id = zmalloc(sizeof(uint8_t) * _length);
5ada26b4 480 if (!_build_id) {
8e2aed3f
AB
481 goto error;
482 }
483
405be658 484 if (lseek(elf->fd, offset, SEEK_SET) < 0) {
8e2aed3f
AB
485 goto error;
486 }
405be658
MD
487 read_len = sizeof(*_build_id) * _length;
488 if (lttng_ust_read(elf->fd, _build_id, read_len) < read_len) {
8e2aed3f
AB
489 goto error;
490 }
491
8e2aed3f
AB
492 break;
493 }
494
82ee1ee4 495 if (_build_id) {
8e2aed3f
AB
496 *build_id = _build_id;
497 *length = _length;
498 }
499
8e2aed3f
AB
500 return 0;
501error:
83215d66 502 free(_build_id);
8e2aed3f
AB
503 return -1;
504}
505
506/*
507 * Retrieve a build ID (an array of bytes) from the corresponding
508 * section in the ELF file. The length of the build ID can be either
509 * 16 or 20 bytes depending on the method used to generate it, hence
510 * the length out parameter.
511 *
512 * If the function returns successfully, the out parameter `found`
513 * indicates whether the build id information was present in the ELF
514 * file or not. If `found` is not 0, the out parameters `build_id` and
515 * `length` will both have been set with the retrieved information.
516 *
517 * Returns 0 on success, -1 if an error occurred.
518 */
519int lttng_ust_elf_get_build_id(struct lttng_ust_elf *elf, uint8_t **build_id,
520 size_t *length, int *found)
521{
522 uint16_t i;
8c83dbda
MD
523 uint8_t *_build_id = NULL; /* Silence old gcc warning. */
524 size_t _length = 0; /* Silence old gcc warning. */
8e2aed3f
AB
525
526 if (!elf || !build_id || !length || !found) {
527 goto error;
528 }
529
530 for (i = 0; i < elf->ehdr->e_phnum; ++i) {
f5a6717a 531 off_t offset, segment_end;
8e2aed3f 532 struct lttng_ust_elf_phdr *phdr;
5b3bbf98 533 int ret = 0;
8e2aed3f
AB
534
535 phdr = lttng_ust_elf_get_phdr(elf, i);
536 if (!phdr) {
537 goto error;
538 }
539
540 /* Build ID will be contained in a PT_NOTE segment. */
541 if (phdr->p_type != PT_NOTE) {
542 goto next_loop;
543 }
544
545 offset = phdr->p_offset;
546 segment_end = offset + phdr->p_filesz;
547 ret = lttng_ust_elf_get_build_id_from_segment(
82ee1ee4 548 elf, &_build_id, &_length, offset, segment_end);
8e2aed3f
AB
549 next_loop:
550 free(phdr);
551 if (ret) {
552 goto error;
553 }
82ee1ee4 554 if (_build_id) {
8e2aed3f
AB
555 break;
556 }
557 }
558
82ee1ee4 559 if (_build_id) {
8e2aed3f
AB
560 *build_id = _build_id;
561 *length = _length;
82ee1ee4
AB
562 *found = 1;
563 } else {
564 *found = 0;
8e2aed3f
AB
565 }
566
8e2aed3f
AB
567 return 0;
568error:
99b7132d 569 free(_build_id);
8e2aed3f
AB
570 return -1;
571}
572
573/*
574 * Try to retrieve filename and CRC from given ELF section `shdr`.
575 *
576 * If the function returns successfully, the out parameter `found`
577 * indicates whether the debug link information was present in the ELF
578 * section or not. If `found` is not 0, the out parameters `filename` and
579 * `crc` will both have been set with the retrieved information.
580 *
581 * Returns 0 on success, -1 if an error occurred.
582 */
4b4a1337 583static
8e2aed3f
AB
584int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf *elf,
585 char **filename, uint32_t *crc,
8e2aed3f
AB
586 struct lttng_ust_elf_shdr *shdr)
587{
8c83dbda 588 char *_filename = NULL; /* Silence old gcc warning. */
405be658 589 size_t filename_len;
8e2aed3f 590 char *section_name = NULL;
8c83dbda 591 uint32_t _crc = 0; /* Silence old gcc warning. */
8e2aed3f 592
82ee1ee4 593 if (!elf || !filename || !crc || !shdr) {
8e2aed3f
AB
594 goto error;
595 }
596
597 /*
598 * The .gnu_debuglink section is of type SHT_PROGBITS,
599 * skip the other sections.
600 */
601 if (shdr->sh_type != SHT_PROGBITS) {
602 goto end;
603 }
604
605 section_name = lttng_ust_elf_get_section_name(elf,
606 shdr->sh_name);
607 if (!section_name) {
608 goto end;
609 }
610 if (strcmp(section_name, ".gnu_debuglink")) {
611 goto end;
612 }
613
614 /*
615 * The length of the filename is the sh_size excluding the CRC
616 * which comes after it in the section.
617 */
618 _filename = zmalloc(sizeof(char) * (shdr->sh_size - ELF_CRC_SIZE));
619 if (!_filename) {
620 goto error;
621 }
405be658 622 if (lseek(elf->fd, shdr->sh_offset, SEEK_SET) < 0) {
8e2aed3f
AB
623 goto error;
624 }
405be658
MD
625 filename_len = sizeof(*_filename) * (shdr->sh_size - ELF_CRC_SIZE);
626 if (lttng_ust_read(elf->fd, _filename, filename_len) < filename_len) {
8e2aed3f
AB
627 goto error;
628 }
405be658 629 if (lttng_ust_read(elf->fd, &_crc, sizeof(_crc)) < sizeof(_crc)) {
8e2aed3f
AB
630 goto error;
631 }
632 if (!is_elf_native_endian(elf)) {
633 _crc = bswap_32(_crc);
634 }
635
8e2aed3f
AB
636end:
637 free(section_name);
82ee1ee4 638 if (_filename) {
8e2aed3f
AB
639 *filename = _filename;
640 *crc = _crc;
641 }
8e2aed3f
AB
642
643 return 0;
644
645error:
83215d66
MD
646 free(_filename);
647 free(section_name);
8e2aed3f
AB
648 return -1;
649}
650
651/*
652 * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any.
653 *
654 * If the function returns successfully, the out parameter `found`
655 * indicates whether the debug link information was present in the ELF
656 * file or not. If `found` is not 0, the out parameters `filename` and
657 * `crc` will both have been set with the retrieved information.
658 *
659 * Returns 0 on success, -1 if an error occurred.
660 */
661int lttng_ust_elf_get_debug_link(struct lttng_ust_elf *elf, char **filename,
662 uint32_t *crc, int *found)
663{
664 int ret;
665 uint16_t i;
8c83dbda
MD
666 char *_filename = NULL; /* Silence old gcc warning. */
667 uint32_t _crc = 0; /* Silence old gcc warning. */
8e2aed3f
AB
668
669 if (!elf || !filename || !crc || !found) {
670 goto error;
671 }
672
673 for (i = 0; i < elf->ehdr->e_shnum; ++i) {
674 struct lttng_ust_elf_shdr *shdr = NULL;
675
676 shdr = lttng_ust_elf_get_shdr(elf, i);
677 if (!shdr) {
678 goto error;
679 }
680
681 ret = lttng_ust_elf_get_debug_link_from_section(
82ee1ee4 682 elf, &_filename, &_crc, shdr);
8e2aed3f
AB
683 free(shdr);
684
685 if (ret) {
686 goto error;
687 }
82ee1ee4 688 if (_filename) {
8e2aed3f
AB
689 break;
690 }
691 }
692
82ee1ee4 693 if (_filename) {
8e2aed3f
AB
694 *filename = _filename;
695 *crc = _crc;
82ee1ee4
AB
696 *found = 1;
697 } else {
698 *found = 0;
8e2aed3f
AB
699 }
700
8e2aed3f 701 return 0;
82ee1ee4 702
8e2aed3f 703error:
99b7132d 704 free(_filename);
8e2aed3f
AB
705 return -1;
706}
This page took 0.060052 seconds and 4 git commands to generate.