Tests: update ust-elf tests to reflect correct memsz computation
[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;
e1f0c569 357 uint64_t low_addr = UINT64_MAX, high_addr = 0;
8e2aed3f
AB
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;
8e2aed3f
AB
365
366 phdr = lttng_ust_elf_get_phdr(elf, i);
367 if (!phdr) {
368 goto error;
369 }
370
371 /*
372 * Only PT_LOAD segments contribute to memsz. Skip
373 * other segments.
374 */
375 if (phdr->p_type != PT_LOAD) {
376 goto next_loop;
377 }
378
e1f0c569
AB
379 low_addr = phdr->p_vaddr < low_addr ? phdr->p_vaddr : low_addr;
380 high_addr = phdr->p_vaddr + phdr->p_memsz > high_addr ?
381 phdr->p_vaddr + phdr->p_memsz : high_addr;
8e2aed3f
AB
382 next_loop:
383 free(phdr);
384 }
385
e1f0c569
AB
386 if (high_addr < low_addr) {
387 /* No PT_LOAD segments or corrupted data. */
388 goto error;
389 }
390
391 *memsz = high_addr - low_addr;
8e2aed3f
AB
392 return 0;
393error:
394 return -1;
395}
396
397/*
398 * Internal method used to try and get the build_id from a PT_NOTE
399 * segment ranging from `offset` to `segment_end`.
400 *
401 * If the function returns successfully, the out parameter `found`
402 * indicates whether the build id information was present in the
403 * segment or not. If `found` is not 0, the out parameters `build_id`
404 * and `length` will both have been set with the retrieved
405 * information.
406 *
407 * Returns 0 on success, -1 if an error occurred.
408 */
409static
410int lttng_ust_elf_get_build_id_from_segment(
411 struct lttng_ust_elf *elf, uint8_t **build_id, size_t *length,
82ee1ee4 412 off_t offset, off_t segment_end)
8e2aed3f 413{
814d07b1
MD
414 uint8_t *_build_id = NULL; /* Silence old gcc warning. */
415 size_t _length = 0; /* Silence old gcc warning. */
8e2aed3f
AB
416
417 while (offset < segment_end) {
418 struct lttng_ust_elf_nhdr nhdr;
405be658 419 size_t read_len;
8e2aed3f
AB
420
421 /* Align start of note entry */
422 offset += offset_align(offset, ELF_NOTE_ENTRY_ALIGN);
423 if (offset >= segment_end) {
424 break;
425 }
426 /*
427 * We seek manually because if the note isn't the
428 * build id the data following the header will not
429 * have been read.
430 */
405be658 431 if (lseek(elf->fd, offset, SEEK_SET) < 0) {
8e2aed3f
AB
432 goto error;
433 }
405be658
MD
434 if (lttng_ust_read(elf->fd, &nhdr, sizeof(nhdr))
435 < sizeof(nhdr)) {
8e2aed3f
AB
436 goto error;
437 }
438
439 if (!is_elf_native_endian(elf)) {
440 nhdr.n_namesz = bswap_32(nhdr.n_namesz);
441 nhdr.n_descsz = bswap_32(nhdr.n_descsz);
442 nhdr.n_type = bswap_32(nhdr.n_type);
443 }
444
445 offset += sizeof(nhdr) + nhdr.n_namesz;
446 /* Align start of desc entry */
447 offset += offset_align(offset, ELF_NOTE_DESC_ALIGN);
448
449 if (nhdr.n_type != NT_GNU_BUILD_ID) {
450 /*
451 * Ignore non build id notes but still
452 * increase the offset.
453 */
454 offset += nhdr.n_descsz;
455 continue;
456 }
457
458 _length = nhdr.n_descsz;
459 _build_id = zmalloc(sizeof(uint8_t) * _length);
5ada26b4 460 if (!_build_id) {
8e2aed3f
AB
461 goto error;
462 }
463
405be658 464 if (lseek(elf->fd, offset, SEEK_SET) < 0) {
8e2aed3f
AB
465 goto error;
466 }
405be658
MD
467 read_len = sizeof(*_build_id) * _length;
468 if (lttng_ust_read(elf->fd, _build_id, read_len) < read_len) {
8e2aed3f
AB
469 goto error;
470 }
471
8e2aed3f
AB
472 break;
473 }
474
82ee1ee4 475 if (_build_id) {
8e2aed3f
AB
476 *build_id = _build_id;
477 *length = _length;
478 }
479
8e2aed3f
AB
480 return 0;
481error:
83215d66 482 free(_build_id);
8e2aed3f
AB
483 return -1;
484}
485
486/*
487 * Retrieve a build ID (an array of bytes) from the corresponding
488 * section in the ELF file. The length of the build ID can be either
489 * 16 or 20 bytes depending on the method used to generate it, hence
490 * the length out parameter.
491 *
492 * If the function returns successfully, the out parameter `found`
493 * indicates whether the build id information was present in the ELF
494 * file or not. If `found` is not 0, the out parameters `build_id` and
495 * `length` will both have been set with the retrieved information.
496 *
497 * Returns 0 on success, -1 if an error occurred.
498 */
499int lttng_ust_elf_get_build_id(struct lttng_ust_elf *elf, uint8_t **build_id,
500 size_t *length, int *found)
501{
502 uint16_t i;
8c83dbda
MD
503 uint8_t *_build_id = NULL; /* Silence old gcc warning. */
504 size_t _length = 0; /* Silence old gcc warning. */
8e2aed3f
AB
505
506 if (!elf || !build_id || !length || !found) {
507 goto error;
508 }
509
510 for (i = 0; i < elf->ehdr->e_phnum; ++i) {
f5a6717a 511 off_t offset, segment_end;
8e2aed3f 512 struct lttng_ust_elf_phdr *phdr;
5b3bbf98 513 int ret = 0;
8e2aed3f
AB
514
515 phdr = lttng_ust_elf_get_phdr(elf, i);
516 if (!phdr) {
517 goto error;
518 }
519
520 /* Build ID will be contained in a PT_NOTE segment. */
521 if (phdr->p_type != PT_NOTE) {
522 goto next_loop;
523 }
524
525 offset = phdr->p_offset;
526 segment_end = offset + phdr->p_filesz;
527 ret = lttng_ust_elf_get_build_id_from_segment(
82ee1ee4 528 elf, &_build_id, &_length, offset, segment_end);
8e2aed3f
AB
529 next_loop:
530 free(phdr);
531 if (ret) {
532 goto error;
533 }
82ee1ee4 534 if (_build_id) {
8e2aed3f
AB
535 break;
536 }
537 }
538
82ee1ee4 539 if (_build_id) {
8e2aed3f
AB
540 *build_id = _build_id;
541 *length = _length;
82ee1ee4
AB
542 *found = 1;
543 } else {
544 *found = 0;
8e2aed3f
AB
545 }
546
8e2aed3f
AB
547 return 0;
548error:
99b7132d 549 free(_build_id);
8e2aed3f
AB
550 return -1;
551}
552
553/*
554 * Try to retrieve filename and CRC from given ELF section `shdr`.
555 *
556 * If the function returns successfully, the out parameter `found`
557 * indicates whether the debug link information was present in the ELF
558 * section or not. If `found` is not 0, the out parameters `filename` and
559 * `crc` will both have been set with the retrieved information.
560 *
561 * Returns 0 on success, -1 if an error occurred.
562 */
8e2aed3f
AB
563int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf *elf,
564 char **filename, uint32_t *crc,
8e2aed3f
AB
565 struct lttng_ust_elf_shdr *shdr)
566{
8c83dbda 567 char *_filename = NULL; /* Silence old gcc warning. */
405be658 568 size_t filename_len;
8e2aed3f 569 char *section_name = NULL;
8c83dbda 570 uint32_t _crc = 0; /* Silence old gcc warning. */
8e2aed3f 571
82ee1ee4 572 if (!elf || !filename || !crc || !shdr) {
8e2aed3f
AB
573 goto error;
574 }
575
576 /*
577 * The .gnu_debuglink section is of type SHT_PROGBITS,
578 * skip the other sections.
579 */
580 if (shdr->sh_type != SHT_PROGBITS) {
581 goto end;
582 }
583
584 section_name = lttng_ust_elf_get_section_name(elf,
585 shdr->sh_name);
586 if (!section_name) {
587 goto end;
588 }
589 if (strcmp(section_name, ".gnu_debuglink")) {
590 goto end;
591 }
592
593 /*
594 * The length of the filename is the sh_size excluding the CRC
595 * which comes after it in the section.
596 */
597 _filename = zmalloc(sizeof(char) * (shdr->sh_size - ELF_CRC_SIZE));
598 if (!_filename) {
599 goto error;
600 }
405be658 601 if (lseek(elf->fd, shdr->sh_offset, SEEK_SET) < 0) {
8e2aed3f
AB
602 goto error;
603 }
405be658
MD
604 filename_len = sizeof(*_filename) * (shdr->sh_size - ELF_CRC_SIZE);
605 if (lttng_ust_read(elf->fd, _filename, filename_len) < filename_len) {
8e2aed3f
AB
606 goto error;
607 }
405be658 608 if (lttng_ust_read(elf->fd, &_crc, sizeof(_crc)) < sizeof(_crc)) {
8e2aed3f
AB
609 goto error;
610 }
611 if (!is_elf_native_endian(elf)) {
612 _crc = bswap_32(_crc);
613 }
614
8e2aed3f
AB
615end:
616 free(section_name);
82ee1ee4 617 if (_filename) {
8e2aed3f
AB
618 *filename = _filename;
619 *crc = _crc;
620 }
8e2aed3f
AB
621
622 return 0;
623
624error:
83215d66
MD
625 free(_filename);
626 free(section_name);
8e2aed3f
AB
627 return -1;
628}
629
630/*
631 * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any.
632 *
633 * If the function returns successfully, the out parameter `found`
634 * indicates whether the debug link information was present in the ELF
635 * file or not. If `found` is not 0, the out parameters `filename` and
636 * `crc` will both have been set with the retrieved information.
637 *
638 * Returns 0 on success, -1 if an error occurred.
639 */
640int lttng_ust_elf_get_debug_link(struct lttng_ust_elf *elf, char **filename,
641 uint32_t *crc, int *found)
642{
643 int ret;
644 uint16_t i;
8c83dbda
MD
645 char *_filename = NULL; /* Silence old gcc warning. */
646 uint32_t _crc = 0; /* Silence old gcc warning. */
8e2aed3f
AB
647
648 if (!elf || !filename || !crc || !found) {
649 goto error;
650 }
651
652 for (i = 0; i < elf->ehdr->e_shnum; ++i) {
653 struct lttng_ust_elf_shdr *shdr = NULL;
654
655 shdr = lttng_ust_elf_get_shdr(elf, i);
656 if (!shdr) {
657 goto error;
658 }
659
660 ret = lttng_ust_elf_get_debug_link_from_section(
82ee1ee4 661 elf, &_filename, &_crc, shdr);
8e2aed3f
AB
662 free(shdr);
663
664 if (ret) {
665 goto error;
666 }
82ee1ee4 667 if (_filename) {
8e2aed3f
AB
668 break;
669 }
670 }
671
82ee1ee4 672 if (_filename) {
8e2aed3f
AB
673 *filename = _filename;
674 *crc = _crc;
82ee1ee4
AB
675 *found = 1;
676 } else {
677 *found = 0;
8e2aed3f
AB
678 }
679
8e2aed3f 680 return 0;
82ee1ee4 681
8e2aed3f 682error:
99b7132d 683 free(_filename);
8e2aed3f
AB
684 return -1;
685}
This page took 0.05499 seconds and 4 git commands to generate.