Commit | Line | Data |
---|---|---|
8e2aed3f AB |
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 | #include <helper.h> | |
20 | #include <string.h> | |
21 | #include <lttng/align.h> | |
22 | #include <lttng/ust-elf.h> | |
405be658 MD |
23 | #include <sys/types.h> |
24 | #include <sys/stat.h> | |
25 | #include <fcntl.h> | |
26 | #include <unistd.h> | |
27 | #include "lttng-tracer-core.h" | |
28 | ||
29 | #define BUF_LEN 4096 | |
8e2aed3f AB |
30 | |
31 | /* | |
32 | * Retrieve the nth (where n is the `index` argument) phdr (program | |
33 | * header) from the given elf instance. | |
34 | * | |
35 | * A pointer to the phdr is returned on success, NULL on failure. | |
36 | */ | |
37 | static | |
38 | struct lttng_ust_elf_phdr *lttng_ust_elf_get_phdr(struct lttng_ust_elf *elf, | |
39 | uint16_t index) | |
40 | { | |
41 | struct lttng_ust_elf_phdr *phdr = NULL; | |
42 | long offset; | |
43 | ||
44 | if (!elf) { | |
45 | goto error; | |
46 | } | |
47 | ||
48 | if (index >= elf->ehdr->e_phnum) { | |
49 | goto error; | |
50 | } | |
51 | ||
52 | phdr = zmalloc(sizeof(struct lttng_ust_elf_phdr)); | |
53 | if (!phdr) { | |
54 | goto error; | |
55 | } | |
56 | ||
57 | offset = elf->ehdr->e_phoff + 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; | |
104 | long offset; | |
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 | ||
119 | offset = elf->ehdr->e_shoff + 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 | |
163 | char *lttng_ust_elf_get_section_name(struct lttng_ust_elf *elf, uint32_t offset) | |
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; |
8e2aed3f AB |
236 | |
237 | elf = zmalloc(sizeof(struct lttng_ust_elf)); | |
238 | if (!elf) { | |
239 | goto error; | |
240 | } | |
241 | ||
242 | elf->path = strdup(path); | |
243 | if (!elf->path) { | |
244 | goto error; | |
245 | } | |
246 | ||
405be658 MD |
247 | elf->fd = open(elf->path, O_RDONLY | O_CLOEXEC); |
248 | if (elf->fd < 0) { | |
8e2aed3f AB |
249 | goto error; |
250 | } | |
251 | ||
405be658 | 252 | if (lttng_ust_read(elf->fd, e_ident, EI_NIDENT) < EI_NIDENT) { |
8e2aed3f AB |
253 | goto error; |
254 | } | |
255 | elf->bitness = e_ident[EI_CLASS]; | |
256 | elf->endianness = e_ident[EI_DATA]; | |
405be658 MD |
257 | |
258 | if (lseek(elf->fd, 0, SEEK_SET) < 0) { | |
259 | goto error; | |
260 | } | |
8e2aed3f AB |
261 | |
262 | elf->ehdr = zmalloc(sizeof(struct lttng_ust_elf_ehdr)); | |
263 | if (!elf->ehdr) { | |
264 | goto error; | |
265 | } | |
266 | ||
267 | if (is_elf_32_bit(elf)) { | |
268 | Elf32_Ehdr elf_ehdr; | |
269 | ||
405be658 MD |
270 | if (lttng_ust_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr)) |
271 | < sizeof(elf_ehdr)) { | |
8e2aed3f AB |
272 | goto error; |
273 | } | |
274 | if (!is_elf_native_endian(elf)) { | |
275 | bswap_ehdr(elf_ehdr); | |
276 | } | |
277 | copy_ehdr(elf_ehdr, *(elf->ehdr)); | |
278 | } else { | |
279 | Elf64_Ehdr elf_ehdr; | |
280 | ||
405be658 MD |
281 | if (lttng_ust_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr)) |
282 | < sizeof(elf_ehdr)) { | |
8e2aed3f AB |
283 | goto error; |
284 | } | |
285 | if (!is_elf_native_endian(elf)) { | |
286 | bswap_ehdr(elf_ehdr); | |
287 | } | |
288 | copy_ehdr(elf_ehdr, *(elf->ehdr)); | |
289 | } | |
290 | ||
291 | section_names_shdr = lttng_ust_elf_get_shdr(elf, elf->ehdr->e_shstrndx); | |
292 | if (!section_names_shdr) { | |
293 | goto error; | |
294 | } | |
295 | ||
296 | elf->section_names_offset = section_names_shdr->sh_offset; | |
297 | elf->section_names_size = section_names_shdr->sh_size; | |
298 | ||
299 | free(section_names_shdr); | |
8e2aed3f AB |
300 | return elf; |
301 | ||
302 | error: | |
303 | if (elf) { | |
304 | free(elf->ehdr); | |
405be658 MD |
305 | if (elf->fd >= 0) { |
306 | if (close(elf->fd)) { | |
307 | abort(); | |
308 | } | |
309 | } | |
8e2aed3f | 310 | free(elf->path); |
405be658 | 311 | free(elf); |
8e2aed3f | 312 | } |
8e2aed3f AB |
313 | return NULL; |
314 | } | |
315 | ||
316 | /* | |
317 | * Destroy the given lttng_ust_elf instance. | |
318 | */ | |
319 | void lttng_ust_elf_destroy(struct lttng_ust_elf *elf) | |
320 | { | |
321 | if (!elf) { | |
322 | return; | |
323 | } | |
324 | ||
325 | free(elf->ehdr); | |
405be658 MD |
326 | if (close(elf->fd)) { |
327 | abort(); | |
328 | } | |
8e2aed3f AB |
329 | free(elf->path); |
330 | free(elf); | |
331 | } | |
332 | ||
333 | /* | |
334 | * Compute the total in-memory size of the ELF file, in bytes. | |
335 | * | |
336 | * Returns 0 if successful, -1 if not. On success, the memory size is | |
337 | * returned through the out parameter `memsz`. | |
338 | */ | |
339 | int lttng_ust_elf_get_memsz(struct lttng_ust_elf *elf, uint64_t *memsz) | |
340 | { | |
341 | uint16_t i; | |
342 | uint64_t _memsz = 0; | |
343 | ||
344 | if (!elf || !memsz) { | |
345 | goto error; | |
346 | } | |
347 | ||
348 | for (i = 0; i < elf->ehdr->e_phnum; ++i) { | |
349 | struct lttng_ust_elf_phdr *phdr; | |
350 | uint64_t align; | |
351 | ||
352 | phdr = lttng_ust_elf_get_phdr(elf, i); | |
353 | if (!phdr) { | |
354 | goto error; | |
355 | } | |
356 | ||
357 | /* | |
358 | * Only PT_LOAD segments contribute to memsz. Skip | |
359 | * other segments. | |
360 | */ | |
361 | if (phdr->p_type != PT_LOAD) { | |
362 | goto next_loop; | |
363 | } | |
364 | ||
365 | /* | |
366 | * A p_align of 0 means no alignment, i.e. aligned to | |
367 | * 1 byte. | |
368 | */ | |
369 | align = phdr->p_align == 0 ? 1 : phdr->p_align; | |
370 | /* Align the start of the segment. */ | |
371 | _memsz += offset_align(_memsz, align); | |
372 | _memsz += phdr->p_memsz; | |
373 | /* | |
374 | * Add padding at the end of the segment, so it ends | |
375 | * on a multiple of the align value (which usually | |
376 | * means a page boundary). This makes the computation | |
377 | * valid even in cases where p_align would change from | |
378 | * one segment to the next. | |
379 | */ | |
380 | _memsz += offset_align(_memsz, align); | |
381 | next_loop: | |
382 | free(phdr); | |
383 | } | |
384 | ||
385 | *memsz = _memsz; | |
386 | return 0; | |
387 | error: | |
388 | return -1; | |
389 | } | |
390 | ||
391 | /* | |
392 | * Internal method used to try and get the build_id from a PT_NOTE | |
393 | * segment ranging from `offset` to `segment_end`. | |
394 | * | |
395 | * If the function returns successfully, the out parameter `found` | |
396 | * indicates whether the build id information was present in the | |
397 | * segment or not. If `found` is not 0, the out parameters `build_id` | |
398 | * and `length` will both have been set with the retrieved | |
399 | * information. | |
400 | * | |
401 | * Returns 0 on success, -1 if an error occurred. | |
402 | */ | |
403 | static | |
404 | int lttng_ust_elf_get_build_id_from_segment( | |
405 | struct lttng_ust_elf *elf, uint8_t **build_id, size_t *length, | |
406 | uint64_t offset, uint64_t segment_end, int *found) | |
407 | { | |
408 | uint8_t *_build_id; | |
409 | size_t _length; | |
410 | int _found = 0; | |
411 | ||
412 | while (offset < segment_end) { | |
413 | struct lttng_ust_elf_nhdr nhdr; | |
405be658 | 414 | size_t read_len; |
8e2aed3f AB |
415 | |
416 | /* Align start of note entry */ | |
417 | offset += offset_align(offset, ELF_NOTE_ENTRY_ALIGN); | |
418 | if (offset >= segment_end) { | |
419 | break; | |
420 | } | |
421 | /* | |
422 | * We seek manually because if the note isn't the | |
423 | * build id the data following the header will not | |
424 | * have been read. | |
425 | */ | |
405be658 | 426 | if (lseek(elf->fd, offset, SEEK_SET) < 0) { |
8e2aed3f AB |
427 | goto error; |
428 | } | |
405be658 MD |
429 | if (lttng_ust_read(elf->fd, &nhdr, sizeof(nhdr)) |
430 | < sizeof(nhdr)) { | |
8e2aed3f AB |
431 | goto error; |
432 | } | |
433 | ||
434 | if (!is_elf_native_endian(elf)) { | |
435 | nhdr.n_namesz = bswap_32(nhdr.n_namesz); | |
436 | nhdr.n_descsz = bswap_32(nhdr.n_descsz); | |
437 | nhdr.n_type = bswap_32(nhdr.n_type); | |
438 | } | |
439 | ||
440 | offset += sizeof(nhdr) + nhdr.n_namesz; | |
441 | /* Align start of desc entry */ | |
442 | offset += offset_align(offset, ELF_NOTE_DESC_ALIGN); | |
443 | ||
444 | if (nhdr.n_type != NT_GNU_BUILD_ID) { | |
445 | /* | |
446 | * Ignore non build id notes but still | |
447 | * increase the offset. | |
448 | */ | |
449 | offset += nhdr.n_descsz; | |
450 | continue; | |
451 | } | |
452 | ||
453 | _length = nhdr.n_descsz; | |
454 | _build_id = zmalloc(sizeof(uint8_t) * _length); | |
455 | if (!build_id) { | |
456 | goto error; | |
457 | } | |
458 | ||
405be658 | 459 | if (lseek(elf->fd, offset, SEEK_SET) < 0) { |
8e2aed3f AB |
460 | goto error; |
461 | } | |
405be658 MD |
462 | read_len = sizeof(*_build_id) * _length; |
463 | if (lttng_ust_read(elf->fd, _build_id, read_len) < read_len) { | |
8e2aed3f AB |
464 | goto error; |
465 | } | |
466 | ||
467 | _found = 1; | |
468 | break; | |
469 | } | |
470 | ||
471 | if (_found) { | |
472 | *build_id = _build_id; | |
473 | *length = _length; | |
474 | } | |
475 | ||
476 | *found = _found; | |
477 | return 0; | |
478 | error: | |
479 | return -1; | |
480 | } | |
481 | ||
482 | /* | |
483 | * Retrieve a build ID (an array of bytes) from the corresponding | |
484 | * section in the ELF file. The length of the build ID can be either | |
485 | * 16 or 20 bytes depending on the method used to generate it, hence | |
486 | * the length out parameter. | |
487 | * | |
488 | * If the function returns successfully, the out parameter `found` | |
489 | * indicates whether the build id information was present in the ELF | |
490 | * file or not. If `found` is not 0, the out parameters `build_id` and | |
491 | * `length` will both have been set with the retrieved information. | |
492 | * | |
493 | * Returns 0 on success, -1 if an error occurred. | |
494 | */ | |
495 | int lttng_ust_elf_get_build_id(struct lttng_ust_elf *elf, uint8_t **build_id, | |
496 | size_t *length, int *found) | |
497 | { | |
498 | uint16_t i; | |
499 | uint8_t *_build_id; | |
500 | size_t _length; | |
501 | int _found = 0; | |
502 | ||
503 | if (!elf || !build_id || !length || !found) { | |
504 | goto error; | |
505 | } | |
506 | ||
507 | for (i = 0; i < elf->ehdr->e_phnum; ++i) { | |
508 | uint64_t offset, segment_end; | |
509 | struct lttng_ust_elf_phdr *phdr; | |
5b3bbf98 | 510 | int ret = 0; |
8e2aed3f AB |
511 | |
512 | phdr = lttng_ust_elf_get_phdr(elf, i); | |
513 | if (!phdr) { | |
514 | goto error; | |
515 | } | |
516 | ||
517 | /* Build ID will be contained in a PT_NOTE segment. */ | |
518 | if (phdr->p_type != PT_NOTE) { | |
519 | goto next_loop; | |
520 | } | |
521 | ||
522 | offset = phdr->p_offset; | |
523 | segment_end = offset + phdr->p_filesz; | |
524 | ret = lttng_ust_elf_get_build_id_from_segment( | |
525 | elf, &_build_id, &_length, offset, segment_end, | |
526 | &_found); | |
527 | next_loop: | |
528 | free(phdr); | |
529 | if (ret) { | |
530 | goto error; | |
531 | } | |
532 | if (_found) { | |
533 | break; | |
534 | } | |
535 | } | |
536 | ||
537 | if (_found) { | |
538 | *build_id = _build_id; | |
539 | *length = _length; | |
540 | } | |
541 | ||
542 | *found = _found; | |
543 | return 0; | |
544 | error: | |
545 | return -1; | |
546 | } | |
547 | ||
548 | /* | |
549 | * Try to retrieve filename and CRC from given ELF section `shdr`. | |
550 | * | |
551 | * If the function returns successfully, the out parameter `found` | |
552 | * indicates whether the debug link information was present in the ELF | |
553 | * section or not. If `found` is not 0, the out parameters `filename` and | |
554 | * `crc` will both have been set with the retrieved information. | |
555 | * | |
556 | * Returns 0 on success, -1 if an error occurred. | |
557 | */ | |
8e2aed3f AB |
558 | int lttng_ust_elf_get_debug_link_from_section(struct lttng_ust_elf *elf, |
559 | char **filename, uint32_t *crc, | |
560 | int *found, | |
561 | struct lttng_ust_elf_shdr *shdr) | |
562 | { | |
563 | int _found = 0; | |
564 | char *_filename; | |
405be658 | 565 | size_t filename_len; |
8e2aed3f AB |
566 | char *section_name = NULL; |
567 | uint32_t _crc; | |
568 | ||
569 | if (!elf || !filename || !crc || !found || !shdr) { | |
570 | goto error; | |
571 | } | |
572 | ||
573 | /* | |
574 | * The .gnu_debuglink section is of type SHT_PROGBITS, | |
575 | * skip the other sections. | |
576 | */ | |
577 | if (shdr->sh_type != SHT_PROGBITS) { | |
578 | goto end; | |
579 | } | |
580 | ||
581 | section_name = lttng_ust_elf_get_section_name(elf, | |
582 | shdr->sh_name); | |
583 | if (!section_name) { | |
584 | goto end; | |
585 | } | |
586 | if (strcmp(section_name, ".gnu_debuglink")) { | |
587 | goto end; | |
588 | } | |
589 | ||
590 | /* | |
591 | * The length of the filename is the sh_size excluding the CRC | |
592 | * which comes after it in the section. | |
593 | */ | |
594 | _filename = zmalloc(sizeof(char) * (shdr->sh_size - ELF_CRC_SIZE)); | |
595 | if (!_filename) { | |
596 | goto error; | |
597 | } | |
405be658 | 598 | if (lseek(elf->fd, shdr->sh_offset, SEEK_SET) < 0) { |
8e2aed3f AB |
599 | goto error; |
600 | } | |
405be658 MD |
601 | filename_len = sizeof(*_filename) * (shdr->sh_size - ELF_CRC_SIZE); |
602 | if (lttng_ust_read(elf->fd, _filename, filename_len) < filename_len) { | |
8e2aed3f AB |
603 | goto error; |
604 | } | |
405be658 | 605 | if (lttng_ust_read(elf->fd, &_crc, sizeof(_crc)) < sizeof(_crc)) { |
8e2aed3f AB |
606 | goto error; |
607 | } | |
608 | if (!is_elf_native_endian(elf)) { | |
609 | _crc = bswap_32(_crc); | |
610 | } | |
611 | ||
612 | _found = 1; | |
613 | ||
614 | end: | |
615 | free(section_name); | |
616 | if (_found) { | |
617 | *filename = _filename; | |
618 | *crc = _crc; | |
619 | } | |
620 | *found = _found; | |
621 | ||
622 | return 0; | |
623 | ||
624 | error: | |
625 | if (section_name) { | |
626 | free(section_name); | |
627 | } | |
628 | ||
629 | return -1; | |
630 | } | |
631 | ||
632 | /* | |
633 | * Retrieve filename and CRC from ELF's .gnu_debuglink section, if any. | |
634 | * | |
635 | * If the function returns successfully, the out parameter `found` | |
636 | * indicates whether the debug link information was present in the ELF | |
637 | * file or not. If `found` is not 0, the out parameters `filename` and | |
638 | * `crc` will both have been set with the retrieved information. | |
639 | * | |
640 | * Returns 0 on success, -1 if an error occurred. | |
641 | */ | |
642 | int lttng_ust_elf_get_debug_link(struct lttng_ust_elf *elf, char **filename, | |
643 | uint32_t *crc, int *found) | |
644 | { | |
645 | int ret; | |
646 | uint16_t i; | |
647 | int _found = 0; | |
648 | char *_filename; | |
649 | uint32_t _crc; | |
650 | ||
651 | if (!elf || !filename || !crc || !found) { | |
652 | goto error; | |
653 | } | |
654 | ||
655 | for (i = 0; i < elf->ehdr->e_shnum; ++i) { | |
656 | struct lttng_ust_elf_shdr *shdr = NULL; | |
657 | ||
658 | shdr = lttng_ust_elf_get_shdr(elf, i); | |
659 | if (!shdr) { | |
660 | goto error; | |
661 | } | |
662 | ||
663 | ret = lttng_ust_elf_get_debug_link_from_section( | |
664 | elf, &_filename, &_crc, &_found, shdr); | |
665 | free(shdr); | |
666 | ||
667 | if (ret) { | |
668 | goto error; | |
669 | } | |
670 | if (_found) { | |
671 | break; | |
672 | } | |
673 | } | |
674 | ||
675 | if (_found) { | |
676 | *filename = _filename; | |
677 | *crc = _crc; | |
678 | } | |
679 | ||
680 | *found = _found; | |
681 | return 0; | |
682 | error: | |
683 | return -1; | |
684 | } |