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