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