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