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