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