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