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