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