Move all sources to 'src/'
[lttng-ust.git] / src / liblttng-ust / lttng-ust-elf.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-or-later
3 *
4 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
5 */
6
7 #define _LGPL_SOURCE
8 #include <fcntl.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15
16 #include <lttng/ust-utils.h>
17
18 #include <ust-elf.h>
19 #include <ust-fd.h>
20
21 #include "lttng-tracer-core.h"
22 #include "lttng-ust-elf.h"
23 #include "ust-helper.h"
24
25 #define BUF_LEN 4096
26
27 #ifndef NT_GNU_BUILD_ID
28 # define NT_GNU_BUILD_ID 3
29 #endif
30
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 */
37 static
38 struct 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;
42 off_t offset;
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
57 offset = (off_t) elf->ehdr->e_phoff
58 + (off_t) index * elf->ehdr->e_phentsize;
59 if (lseek(elf->fd, offset, SEEK_SET) < 0) {
60 goto error;
61 }
62
63 if (is_elf_32_bit(elf)) {
64 Elf32_Phdr elf_phdr;
65
66 if (lttng_ust_read(elf->fd, &elf_phdr, sizeof(elf_phdr))
67 < sizeof(elf_phdr)) {
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
77 if (lttng_ust_read(elf->fd, &elf_phdr, sizeof(elf_phdr))
78 < sizeof(elf_phdr)) {
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
89 error:
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 */
100 static
101 struct 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;
105 off_t offset;
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
120 offset = (off_t) elf->ehdr->e_shoff
121 + (off_t) index * elf->ehdr->e_shentsize;
122 if (lseek(elf->fd, offset, SEEK_SET) < 0) {
123 goto error;
124 }
125
126 if (is_elf_32_bit(elf)) {
127 Elf32_Shdr elf_shdr;
128
129 if (lttng_ust_read(elf->fd, &elf_shdr, sizeof(elf_shdr))
130 < sizeof(elf_shdr)) {
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
140 if (lttng_ust_read(elf->fd, &elf_shdr, sizeof(elf_shdr))
141 < sizeof(elf_shdr)) {
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
152 error:
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 */
164 static
165 char *lttng_ust_elf_get_section_name(struct lttng_ust_elf *elf, off_t offset)
166 {
167 char *name = NULL;
168 size_t len = 0, to_read; /* len does not include \0 */
169
170 if (!elf) {
171 goto error;
172 }
173
174 if (offset >= elf->section_names_size) {
175 goto error;
176 }
177
178 if (lseek(elf->fd, elf->section_names_offset + offset, SEEK_SET) < 0) {
179 goto error;
180 }
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) {
191 goto error;
192 }
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;
206 }
207 end:
208 name = zmalloc(sizeof(char) * (len + 1)); /* + 1 for \0 */
209 if (!name) {
210 goto error;
211 }
212 if (lseek(elf->fd, elf->section_names_offset + offset,
213 SEEK_SET) < 0) {
214 goto error;
215 }
216 if (lttng_ust_read(elf->fd, name, len + 1) < len + 1) {
217 goto error;
218 }
219
220 return name;
221
222 error:
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 */
233 struct 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;
237 struct lttng_ust_elf *elf = NULL;
238 int ret, fd;
239
240 elf = zmalloc(sizeof(struct lttng_ust_elf));
241 if (!elf) {
242 goto error;
243 }
244
245 /* Initialize fd field to -1. 0 is a valid fd number */
246 elf->fd = -1;
247
248 elf->path = strdup(path);
249 if (!elf->path) {
250 goto error;
251 }
252
253 lttng_ust_lock_fd_tracker();
254 fd = open(elf->path, O_RDONLY | O_CLOEXEC);
255 if (fd < 0) {
256 lttng_ust_unlock_fd_tracker();
257 goto error;
258 }
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;
271 lttng_ust_unlock_fd_tracker();
272
273 if (lttng_ust_read(elf->fd, e_ident, EI_NIDENT) < EI_NIDENT) {
274 goto error;
275 }
276 elf->bitness = e_ident[EI_CLASS];
277 elf->endianness = e_ident[EI_DATA];
278
279 if (lseek(elf->fd, 0, SEEK_SET) < 0) {
280 goto error;
281 }
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
291 if (lttng_ust_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr))
292 < sizeof(elf_ehdr)) {
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
302 if (lttng_ust_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr))
303 < sizeof(elf_ehdr)) {
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);
321 return elf;
322
323 error:
324 lttng_ust_elf_destroy(elf);
325 return NULL;
326 }
327
328 /*
329 * Test whether the ELF file is position independent code (PIC)
330 */
331 uint8_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
340 /*
341 * Destroy the given lttng_ust_elf instance.
342 */
343 void lttng_ust_elf_destroy(struct lttng_ust_elf *elf)
344 {
345 int ret;
346
347 if (!elf) {
348 return;
349 }
350
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();
361 }
362
363 free(elf->ehdr);
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 */
374 int lttng_ust_elf_get_memsz(struct lttng_ust_elf *elf, uint64_t *memsz)
375 {
376 uint16_t i;
377 uint64_t low_addr = UINT64_MAX, high_addr = 0;
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;
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
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);
402 next_loop:
403 free(phdr);
404 }
405
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;
412 return 0;
413 error:
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 */
429 static
430 int lttng_ust_elf_get_build_id_from_segment(
431 struct lttng_ust_elf *elf, uint8_t **build_id, size_t *length,
432 off_t offset, off_t segment_end)
433 {
434 uint8_t *_build_id = NULL; /* Silence old gcc warning. */
435 size_t _length = 0; /* Silence old gcc warning. */
436
437 while (offset < segment_end) {
438 struct lttng_ust_elf_nhdr nhdr;
439 size_t read_len;
440
441 /* Align start of note entry */
442 offset += lttng_ust_offset_align(offset, ELF_NOTE_ENTRY_ALIGN);
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 */
451 if (lseek(elf->fd, offset, SEEK_SET) < 0) {
452 goto error;
453 }
454 if (lttng_ust_read(elf->fd, &nhdr, sizeof(nhdr))
455 < sizeof(nhdr)) {
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 */
467 offset += lttng_ust_offset_align(offset, ELF_NOTE_DESC_ALIGN);
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);
480 if (!_build_id) {
481 goto error;
482 }
483
484 if (lseek(elf->fd, offset, SEEK_SET) < 0) {
485 goto error;
486 }
487 read_len = sizeof(*_build_id) * _length;
488 if (lttng_ust_read(elf->fd, _build_id, read_len) < read_len) {
489 goto error;
490 }
491
492 break;
493 }
494
495 if (_build_id) {
496 *build_id = _build_id;
497 *length = _length;
498 }
499
500 return 0;
501 error:
502 free(_build_id);
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 */
519 int 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;
523 uint8_t *_build_id = NULL; /* Silence old gcc warning. */
524 size_t _length = 0; /* Silence old gcc warning. */
525
526 if (!elf || !build_id || !length || !found) {
527 goto error;
528 }
529
530 for (i = 0; i < elf->ehdr->e_phnum; ++i) {
531 off_t offset, segment_end;
532 struct lttng_ust_elf_phdr *phdr;
533 int ret = 0;
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(
548 elf, &_build_id, &_length, offset, segment_end);
549 next_loop:
550 free(phdr);
551 if (ret) {
552 goto error;
553 }
554 if (_build_id) {
555 break;
556 }
557 }
558
559 if (_build_id) {
560 *build_id = _build_id;
561 *length = _length;
562 *found = 1;
563 } else {
564 *found = 0;
565 }
566
567 return 0;
568 error:
569 free(_build_id);
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 */
583 static
584 int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf *elf,
585 char **filename, uint32_t *crc,
586 struct lttng_ust_elf_shdr *shdr)
587 {
588 char *_filename = NULL; /* Silence old gcc warning. */
589 size_t filename_len;
590 char *section_name = NULL;
591 uint32_t _crc = 0; /* Silence old gcc warning. */
592
593 if (!elf || !filename || !crc || !shdr) {
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 }
622 if (lseek(elf->fd, shdr->sh_offset, SEEK_SET) < 0) {
623 goto error;
624 }
625 filename_len = sizeof(*_filename) * (shdr->sh_size - ELF_CRC_SIZE);
626 if (lttng_ust_read(elf->fd, _filename, filename_len) < filename_len) {
627 goto error;
628 }
629 if (lttng_ust_read(elf->fd, &_crc, sizeof(_crc)) < sizeof(_crc)) {
630 goto error;
631 }
632 if (!is_elf_native_endian(elf)) {
633 _crc = bswap_32(_crc);
634 }
635
636 end:
637 free(section_name);
638 if (_filename) {
639 *filename = _filename;
640 *crc = _crc;
641 }
642
643 return 0;
644
645 error:
646 free(_filename);
647 free(section_name);
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 */
661 int 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;
666 char *_filename = NULL; /* Silence old gcc warning. */
667 uint32_t _crc = 0; /* Silence old gcc warning. */
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(
682 elf, &_filename, &_crc, shdr);
683 free(shdr);
684
685 if (ret) {
686 goto error;
687 }
688 if (_filename) {
689 break;
690 }
691 }
692
693 if (_filename) {
694 *filename = _filename;
695 *crc = _crc;
696 *found = 1;
697 } else {
698 *found = 0;
699 }
700
701 return 0;
702
703 error:
704 free(_filename);
705 return -1;
706 }
This page took 0.044459 seconds and 4 git commands to generate.