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