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