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