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