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