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