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