| 1 | /* |
| 2 | * Copyright (C) 2015 Antoine Busque <abusque@efficios.com> |
| 3 | * Copyright (C) 2017 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 4 | * Copyright (C) 2017 Erica Bugden <erica.bugden@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <common/compat/endian.h> |
| 11 | #include <common/error.h> |
| 12 | #include <common/lttng-elf.h> |
| 13 | #include <common/macros.h> |
| 14 | #include <common/readwrite.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdbool.h> |
| 17 | #include <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include <elf.h> |
| 25 | |
| 26 | #define BUF_LEN 4096 |
| 27 | #define TEXT_SECTION_NAME ".text" |
| 28 | #define SYMBOL_TAB_SECTION_NAME ".symtab" |
| 29 | #define STRING_TAB_SECTION_NAME ".strtab" |
| 30 | #define DYNAMIC_SYMBOL_TAB_SECTION_NAME ".dynsym" |
| 31 | #define DYNAMIC_STRING_TAB_SECTION_NAME ".dynstr" |
| 32 | #define NOTE_STAPSDT_SECTION_NAME ".note.stapsdt" |
| 33 | #define NOTE_STAPSDT_NAME "stapsdt" |
| 34 | #define NOTE_STAPSDT_TYPE 3 |
| 35 | #define MAX_SECTION_DATA_SIZE 512 * 1024 * 1024 |
| 36 | |
| 37 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 38 | #define NATIVE_ELF_ENDIANNESS ELFDATA2LSB |
| 39 | #else |
| 40 | #define NATIVE_ELF_ENDIANNESS ELFDATA2MSB |
| 41 | #endif |
| 42 | |
| 43 | #define next_4bytes_boundary(x) (typeof(x)) ((((uint64_t)x) + 3) & ~0x03) |
| 44 | |
| 45 | #define bswap(x) \ |
| 46 | do { \ |
| 47 | switch (sizeof(x)) { \ |
| 48 | case 8: \ |
| 49 | x = be64toh((uint64_t)x); \ |
| 50 | break; \ |
| 51 | case 4: \ |
| 52 | x = be32toh((uint32_t)x); \ |
| 53 | break; \ |
| 54 | case 2: \ |
| 55 | x = be16toh((uint16_t)x); \ |
| 56 | break; \ |
| 57 | case 1: \ |
| 58 | break; \ |
| 59 | default: \ |
| 60 | abort(); \ |
| 61 | } \ |
| 62 | } while (0) |
| 63 | |
| 64 | #define bswap_shdr(shdr) \ |
| 65 | do { \ |
| 66 | bswap((shdr).sh_name); \ |
| 67 | bswap((shdr).sh_type); \ |
| 68 | bswap((shdr).sh_flags); \ |
| 69 | bswap((shdr).sh_addr); \ |
| 70 | bswap((shdr).sh_offset); \ |
| 71 | bswap((shdr).sh_size); \ |
| 72 | bswap((shdr).sh_link); \ |
| 73 | bswap((shdr).sh_info); \ |
| 74 | bswap((shdr).sh_addralign); \ |
| 75 | bswap((shdr).sh_entsize); \ |
| 76 | } while (0) |
| 77 | |
| 78 | #define bswap_ehdr(ehdr) \ |
| 79 | do { \ |
| 80 | bswap((ehdr).e_type); \ |
| 81 | bswap((ehdr).e_machine); \ |
| 82 | bswap((ehdr).e_version); \ |
| 83 | bswap((ehdr).e_entry); \ |
| 84 | bswap((ehdr).e_phoff); \ |
| 85 | bswap((ehdr).e_shoff); \ |
| 86 | bswap((ehdr).e_flags); \ |
| 87 | bswap((ehdr).e_ehsize); \ |
| 88 | bswap((ehdr).e_phentsize); \ |
| 89 | bswap((ehdr).e_phnum); \ |
| 90 | bswap((ehdr).e_shentsize); \ |
| 91 | bswap((ehdr).e_shnum); \ |
| 92 | bswap((ehdr).e_shstrndx); \ |
| 93 | } while (0) |
| 94 | |
| 95 | #define copy_shdr(src_shdr, dst_shdr) \ |
| 96 | do { \ |
| 97 | (dst_shdr).sh_name = (src_shdr).sh_name; \ |
| 98 | (dst_shdr).sh_type = (src_shdr).sh_type; \ |
| 99 | (dst_shdr).sh_flags = (src_shdr).sh_flags; \ |
| 100 | (dst_shdr).sh_addr = (src_shdr).sh_addr; \ |
| 101 | (dst_shdr).sh_offset = (src_shdr).sh_offset; \ |
| 102 | (dst_shdr).sh_size = (src_shdr).sh_size; \ |
| 103 | (dst_shdr).sh_link = (src_shdr).sh_link; \ |
| 104 | (dst_shdr).sh_info = (src_shdr).sh_info; \ |
| 105 | (dst_shdr).sh_addralign = (src_shdr).sh_addralign; \ |
| 106 | (dst_shdr).sh_entsize = (src_shdr).sh_entsize; \ |
| 107 | } while (0) |
| 108 | |
| 109 | #define copy_ehdr(src_ehdr, dst_ehdr) \ |
| 110 | do { \ |
| 111 | (dst_ehdr).e_type = (src_ehdr).e_type; \ |
| 112 | (dst_ehdr).e_machine = (src_ehdr).e_machine; \ |
| 113 | (dst_ehdr).e_version = (src_ehdr).e_version; \ |
| 114 | (dst_ehdr).e_entry = (src_ehdr).e_entry; \ |
| 115 | (dst_ehdr).e_phoff = (src_ehdr).e_phoff; \ |
| 116 | (dst_ehdr).e_shoff = (src_ehdr).e_shoff; \ |
| 117 | (dst_ehdr).e_flags = (src_ehdr).e_flags; \ |
| 118 | (dst_ehdr).e_ehsize = (src_ehdr).e_ehsize; \ |
| 119 | (dst_ehdr).e_phentsize = (src_ehdr).e_phentsize; \ |
| 120 | (dst_ehdr).e_phnum = (src_ehdr).e_phnum; \ |
| 121 | (dst_ehdr).e_shentsize = (src_ehdr).e_shentsize; \ |
| 122 | (dst_ehdr).e_shnum = (src_ehdr).e_shnum; \ |
| 123 | (dst_ehdr).e_shstrndx = (src_ehdr).e_shstrndx; \ |
| 124 | } while (0) |
| 125 | |
| 126 | #define copy_sym(src_sym, dst_sym) \ |
| 127 | do { \ |
| 128 | dst_sym.st_name = src_sym.st_name; \ |
| 129 | dst_sym.st_info = src_sym.st_info; \ |
| 130 | dst_sym.st_other = src_sym.st_other; \ |
| 131 | dst_sym.st_shndx = src_sym.st_shndx; \ |
| 132 | dst_sym.st_value = src_sym.st_value; \ |
| 133 | dst_sym.st_size = src_sym.st_size; \ |
| 134 | } while (0) |
| 135 | |
| 136 | /* Both 32bit and 64bit use the same 1 byte field for type. (See elf.h) */ |
| 137 | #define ELF_ST_TYPE(val) ELF32_ST_TYPE(val) |
| 138 | |
| 139 | struct lttng_elf_ehdr { |
| 140 | uint16_t e_type; |
| 141 | uint16_t e_machine; |
| 142 | uint32_t e_version; |
| 143 | uint64_t e_entry; |
| 144 | uint64_t e_phoff; |
| 145 | uint64_t e_shoff; |
| 146 | uint32_t e_flags; |
| 147 | uint16_t e_ehsize; |
| 148 | uint16_t e_phentsize; |
| 149 | uint16_t e_phnum; |
| 150 | uint16_t e_shentsize; |
| 151 | uint16_t e_shnum; |
| 152 | uint16_t e_shstrndx; |
| 153 | }; |
| 154 | |
| 155 | struct lttng_elf_shdr { |
| 156 | uint32_t sh_name; |
| 157 | uint32_t sh_type; |
| 158 | uint64_t sh_flags; |
| 159 | uint64_t sh_addr; |
| 160 | uint64_t sh_offset; |
| 161 | uint64_t sh_size; |
| 162 | uint32_t sh_link; |
| 163 | uint32_t sh_info; |
| 164 | uint64_t sh_addralign; |
| 165 | uint64_t sh_entsize; |
| 166 | }; |
| 167 | |
| 168 | /* |
| 169 | * This struct can hold both 32bit and 64bit symbol description. It's used with |
| 170 | * the copy_sym() macro. Using this abstraction, we can use the same code for |
| 171 | * both bitness. |
| 172 | */ |
| 173 | struct lttng_elf_sym { |
| 174 | uint32_t st_name; |
| 175 | uint8_t st_info; |
| 176 | uint8_t st_other; |
| 177 | uint16_t st_shndx; |
| 178 | uint64_t st_value; |
| 179 | uint64_t st_size; |
| 180 | }; |
| 181 | |
| 182 | struct lttng_elf { |
| 183 | int fd; |
| 184 | size_t file_size; |
| 185 | uint8_t bitness; |
| 186 | uint8_t endianness; |
| 187 | /* Offset in bytes to start of section names string table. */ |
| 188 | off_t section_names_offset; |
| 189 | /* Size in bytes of section names string table. */ |
| 190 | size_t section_names_size; |
| 191 | struct lttng_elf_ehdr *ehdr; |
| 192 | }; |
| 193 | |
| 194 | static inline |
| 195 | int is_elf_32_bit(struct lttng_elf *elf) |
| 196 | { |
| 197 | return elf->bitness == ELFCLASS32; |
| 198 | } |
| 199 | |
| 200 | static inline |
| 201 | int is_elf_native_endian(struct lttng_elf *elf) |
| 202 | { |
| 203 | return elf->endianness == NATIVE_ELF_ENDIANNESS; |
| 204 | } |
| 205 | |
| 206 | static |
| 207 | int populate_section_header(struct lttng_elf * elf, struct lttng_elf_shdr *shdr, |
| 208 | uint32_t index) |
| 209 | { |
| 210 | int ret = 0; |
| 211 | off_t offset; |
| 212 | |
| 213 | /* Compute the offset of the section in the file */ |
| 214 | offset = (off_t) elf->ehdr->e_shoff |
| 215 | + (off_t) index * elf->ehdr->e_shentsize; |
| 216 | |
| 217 | if (lseek(elf->fd, offset, SEEK_SET) < 0) { |
| 218 | PERROR("Error seeking to the beginning of ELF section header"); |
| 219 | ret = -1; |
| 220 | goto error; |
| 221 | } |
| 222 | |
| 223 | if (is_elf_32_bit(elf)) { |
| 224 | Elf32_Shdr elf_shdr; |
| 225 | |
| 226 | if (lttng_read(elf->fd, &elf_shdr, sizeof(elf_shdr)) < sizeof(elf_shdr)) { |
| 227 | PERROR("Error reading ELF section header"); |
| 228 | ret = -1; |
| 229 | goto error; |
| 230 | } |
| 231 | if (!is_elf_native_endian(elf)) { |
| 232 | bswap_shdr(elf_shdr); |
| 233 | } |
| 234 | copy_shdr(elf_shdr, *shdr); |
| 235 | } else { |
| 236 | Elf64_Shdr elf_shdr; |
| 237 | |
| 238 | if (lttng_read(elf->fd, &elf_shdr, sizeof(elf_shdr)) < sizeof(elf_shdr)) { |
| 239 | PERROR("Error reading ELF section header"); |
| 240 | ret = -1; |
| 241 | goto error; |
| 242 | } |
| 243 | if (!is_elf_native_endian(elf)) { |
| 244 | bswap_shdr(elf_shdr); |
| 245 | } |
| 246 | copy_shdr(elf_shdr, *shdr); |
| 247 | } |
| 248 | |
| 249 | error: |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | static |
| 254 | int populate_elf_header(struct lttng_elf *elf) |
| 255 | { |
| 256 | int ret = 0; |
| 257 | |
| 258 | /* |
| 259 | * Move the read pointer back to the beginning to read the full header |
| 260 | * and copy it in our structure. |
| 261 | */ |
| 262 | if (lseek(elf->fd, 0, SEEK_SET) < 0) { |
| 263 | PERROR("Error seeking to the beginning of the file"); |
| 264 | ret = -1; |
| 265 | goto error; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Use macros to set fields in the ELF header struct for both 32bit and |
| 270 | * 64bit. |
| 271 | */ |
| 272 | if (is_elf_32_bit(elf)) { |
| 273 | Elf32_Ehdr elf_ehdr; |
| 274 | |
| 275 | if (lttng_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr)) < sizeof(elf_ehdr)) { |
| 276 | ret = -1; |
| 277 | goto error; |
| 278 | } |
| 279 | if (!is_elf_native_endian(elf)) { |
| 280 | bswap_ehdr(elf_ehdr); |
| 281 | } |
| 282 | copy_ehdr(elf_ehdr, *(elf->ehdr)); |
| 283 | } else { |
| 284 | Elf64_Ehdr elf_ehdr; |
| 285 | |
| 286 | if (lttng_read(elf->fd, &elf_ehdr, sizeof(elf_ehdr)) < sizeof(elf_ehdr)) { |
| 287 | ret = -1; |
| 288 | goto error; |
| 289 | } |
| 290 | if (!is_elf_native_endian(elf)) { |
| 291 | bswap_ehdr(elf_ehdr); |
| 292 | } |
| 293 | copy_ehdr(elf_ehdr, *(elf->ehdr)); |
| 294 | } |
| 295 | error: |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Retrieve the nth (where n is the `index` argument) shdr (section |
| 301 | * header) from the given elf instance. |
| 302 | * |
| 303 | * 0 is returned on succes, -1 on failure. |
| 304 | */ |
| 305 | static |
| 306 | int lttng_elf_get_section_hdr(struct lttng_elf *elf, |
| 307 | uint16_t index, struct lttng_elf_shdr *out_header) |
| 308 | { |
| 309 | int ret = 0; |
| 310 | |
| 311 | if (!elf) { |
| 312 | ret = -1; |
| 313 | goto error; |
| 314 | } |
| 315 | |
| 316 | if (index >= elf->ehdr->e_shnum) { |
| 317 | ret = -1; |
| 318 | goto error; |
| 319 | } |
| 320 | |
| 321 | ret = populate_section_header(elf, out_header, index); |
| 322 | if (ret) { |
| 323 | DBG("Error populating section header."); |
| 324 | goto error; |
| 325 | } |
| 326 | |
| 327 | error: |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Lookup a section's name from a given offset (usually from an shdr's |
| 333 | * sh_name value) in bytes relative to the beginning of the section |
| 334 | * names string table. |
| 335 | * |
| 336 | * If no name is found, NULL is returned. |
| 337 | */ |
| 338 | static |
| 339 | char *lttng_elf_get_section_name(struct lttng_elf *elf, off_t offset) |
| 340 | { |
| 341 | char *name = NULL; |
| 342 | size_t name_length = 0, to_read; /* name_length does not include \0 */ |
| 343 | |
| 344 | if (!elf) { |
| 345 | goto error; |
| 346 | } |
| 347 | |
| 348 | if (offset >= elf->section_names_size) { |
| 349 | goto error; |
| 350 | } |
| 351 | |
| 352 | if (lseek(elf->fd, elf->section_names_offset + offset, SEEK_SET) < 0) { |
| 353 | PERROR("Error seeking to the beginning of ELF string table section"); |
| 354 | goto error; |
| 355 | } |
| 356 | |
| 357 | to_read = elf->section_names_size - offset; |
| 358 | |
| 359 | /* Find first \0 after or at current location, remember name_length. */ |
| 360 | for (;;) { |
| 361 | char buf[BUF_LEN]; |
| 362 | ssize_t read_len; |
| 363 | size_t i; |
| 364 | |
| 365 | if (!to_read) { |
| 366 | goto error; |
| 367 | } |
| 368 | read_len = lttng_read(elf->fd, buf, min_t(size_t, BUF_LEN, to_read)); |
| 369 | if (read_len <= 0) { |
| 370 | PERROR("Error reading ELF string table section"); |
| 371 | goto error; |
| 372 | } |
| 373 | for (i = 0; i < read_len; i++) { |
| 374 | if (buf[i] == '\0') { |
| 375 | name_length += i; |
| 376 | goto end; |
| 377 | } |
| 378 | } |
| 379 | name_length += read_len; |
| 380 | to_read -= read_len; |
| 381 | } |
| 382 | end: |
| 383 | /* |
| 384 | * We found the length of the section name, now seek back to the |
| 385 | * beginning of the name and copy it in the newly allocated buffer. |
| 386 | */ |
| 387 | name = zmalloc(sizeof(char) * (name_length + 1)); /* + 1 for \0 */ |
| 388 | if (!name) { |
| 389 | PERROR("Error allocating ELF section name buffer"); |
| 390 | goto error; |
| 391 | } |
| 392 | if (lseek(elf->fd, elf->section_names_offset + offset, SEEK_SET) < 0) { |
| 393 | PERROR("Error seeking to the offset of the ELF section name"); |
| 394 | goto error; |
| 395 | } |
| 396 | if (lttng_read(elf->fd, name, name_length + 1) < name_length + 1) { |
| 397 | PERROR("Error reading the ELF section name"); |
| 398 | goto error; |
| 399 | } |
| 400 | |
| 401 | return name; |
| 402 | |
| 403 | error: |
| 404 | free(name); |
| 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | static |
| 409 | int lttng_elf_validate_and_populate(struct lttng_elf *elf) |
| 410 | { |
| 411 | uint8_t version; |
| 412 | uint8_t e_ident[EI_NIDENT]; |
| 413 | uint8_t *magic_number = NULL; |
| 414 | int ret = 0; |
| 415 | |
| 416 | if (elf->fd == -1) { |
| 417 | DBG("fd error"); |
| 418 | ret = LTTNG_ERR_ELF_PARSING; |
| 419 | goto end; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * First read the magic number, endianness and version to later populate |
| 424 | * the ELF header with the correct endianness and bitness. |
| 425 | * (see elf.h) |
| 426 | */ |
| 427 | |
| 428 | if (lseek(elf->fd, 0, SEEK_SET) < 0) { |
| 429 | PERROR("Error seeking the beginning of ELF file"); |
| 430 | ret = LTTNG_ERR_ELF_PARSING; |
| 431 | goto end; |
| 432 | } |
| 433 | ret = lttng_read(elf->fd, e_ident, EI_NIDENT); |
| 434 | if (ret < EI_NIDENT) { |
| 435 | DBG("Error reading the ELF identification fields"); |
| 436 | if (ret == -1) { |
| 437 | PERROR("Error reading the ELF identification fields"); |
| 438 | } |
| 439 | ret = LTTNG_ERR_ELF_PARSING; |
| 440 | goto end; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Copy fields used to check that the target file is in fact a valid ELF |
| 445 | * file. |
| 446 | */ |
| 447 | elf->bitness = e_ident[EI_CLASS]; |
| 448 | elf->endianness = e_ident[EI_DATA]; |
| 449 | version = e_ident[EI_VERSION]; |
| 450 | magic_number = &e_ident[EI_MAG0]; |
| 451 | |
| 452 | /* |
| 453 | * Check the magic number. |
| 454 | */ |
| 455 | if (memcmp(magic_number, ELFMAG, SELFMAG) != 0) { |
| 456 | DBG("Error check ELF magic number."); |
| 457 | ret = LTTNG_ERR_ELF_PARSING; |
| 458 | goto end; |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Check the bitness is either ELFCLASS32 or ELFCLASS64. |
| 463 | */ |
| 464 | if (elf->bitness <= ELFCLASSNONE || elf->bitness >= ELFCLASSNUM) { |
| 465 | DBG("ELF class error."); |
| 466 | ret = LTTNG_ERR_ELF_PARSING; |
| 467 | goto end; |
| 468 | } |
| 469 | |
| 470 | /* |
| 471 | * Check the endianness is either ELFDATA2LSB or ELFDATA2MSB. |
| 472 | */ |
| 473 | if (elf->endianness <= ELFDATANONE || elf->endianness >= ELFDATANUM) { |
| 474 | DBG("ELF endianness error."); |
| 475 | ret = LTTNG_ERR_ELF_PARSING; |
| 476 | goto end; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Check the version is ELF_CURRENT. |
| 481 | */ |
| 482 | if (version <= EV_NONE || version >= EV_NUM) { |
| 483 | DBG("Wrong ELF version."); |
| 484 | ret = LTTNG_ERR_ELF_PARSING; |
| 485 | goto end; |
| 486 | } |
| 487 | |
| 488 | elf->ehdr = zmalloc(sizeof(struct lttng_elf_ehdr)); |
| 489 | if (!elf->ehdr) { |
| 490 | PERROR("Error allocation buffer for ELF header"); |
| 491 | ret = LTTNG_ERR_NOMEM; |
| 492 | goto end; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Copy the content of the elf header. |
| 497 | */ |
| 498 | ret = populate_elf_header(elf); |
| 499 | if (ret) { |
| 500 | DBG("Error reading ELF header,"); |
| 501 | goto free_elf_error; |
| 502 | } |
| 503 | |
| 504 | goto end; |
| 505 | |
| 506 | free_elf_error: |
| 507 | free(elf->ehdr); |
| 508 | elf->ehdr = NULL; |
| 509 | end: |
| 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Create an instance of lttng_elf for the ELF file located at |
| 515 | * `path`. |
| 516 | * |
| 517 | * Return a pointer to the instance on success, NULL on failure. |
| 518 | */ |
| 519 | static |
| 520 | struct lttng_elf *lttng_elf_create(int fd) |
| 521 | { |
| 522 | struct lttng_elf_shdr section_names_shdr; |
| 523 | struct lttng_elf *elf = NULL; |
| 524 | int ret; |
| 525 | struct stat stat_buf; |
| 526 | |
| 527 | if (fd < 0) { |
| 528 | goto error; |
| 529 | } |
| 530 | |
| 531 | ret = fstat(fd, &stat_buf); |
| 532 | if (ret) { |
| 533 | PERROR("Failed to determine size of elf file"); |
| 534 | goto error; |
| 535 | } |
| 536 | if (!S_ISREG(stat_buf.st_mode)) { |
| 537 | ERR("Refusing to initialize lttng_elf from non-regular file"); |
| 538 | goto error; |
| 539 | } |
| 540 | |
| 541 | elf = zmalloc(sizeof(struct lttng_elf)); |
| 542 | if (!elf) { |
| 543 | PERROR("Error allocating struct lttng_elf"); |
| 544 | goto error; |
| 545 | } |
| 546 | elf->file_size = (size_t) stat_buf.st_size; |
| 547 | |
| 548 | elf->fd = dup(fd); |
| 549 | if (elf->fd < 0) { |
| 550 | PERROR("Error duplicating file descriptor to binary"); |
| 551 | goto error; |
| 552 | } |
| 553 | |
| 554 | ret = lttng_elf_validate_and_populate(elf); |
| 555 | if (ret) { |
| 556 | goto error; |
| 557 | } |
| 558 | |
| 559 | ret = lttng_elf_get_section_hdr( |
| 560 | elf, elf->ehdr->e_shstrndx, §ion_names_shdr); |
| 561 | if (ret) { |
| 562 | goto error; |
| 563 | } |
| 564 | |
| 565 | elf->section_names_offset = section_names_shdr.sh_offset; |
| 566 | elf->section_names_size = section_names_shdr.sh_size; |
| 567 | return elf; |
| 568 | |
| 569 | error: |
| 570 | if (elf) { |
| 571 | if (elf->ehdr) { |
| 572 | free(elf->ehdr); |
| 573 | } |
| 574 | if (elf->fd >= 0) { |
| 575 | if (close(elf->fd)) { |
| 576 | PERROR("Error closing file descriptor in error path"); |
| 577 | abort(); |
| 578 | } |
| 579 | } |
| 580 | free(elf); |
| 581 | } |
| 582 | return NULL; |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * Destroy the given lttng_elf instance. |
| 587 | */ |
| 588 | static |
| 589 | void lttng_elf_destroy(struct lttng_elf *elf) |
| 590 | { |
| 591 | if (!elf) { |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | free(elf->ehdr); |
| 596 | if (close(elf->fd)) { |
| 597 | PERROR("Error closing file description in error path"); |
| 598 | abort(); |
| 599 | } |
| 600 | free(elf); |
| 601 | } |
| 602 | |
| 603 | static |
| 604 | int lttng_elf_get_section_hdr_by_name(struct lttng_elf *elf, |
| 605 | const char *section_name, struct lttng_elf_shdr *section_hdr) |
| 606 | { |
| 607 | int i; |
| 608 | char *curr_section_name; |
| 609 | |
| 610 | for (i = 0; i < elf->ehdr->e_shnum; ++i) { |
| 611 | bool name_equal; |
| 612 | int ret = lttng_elf_get_section_hdr(elf, i, section_hdr); |
| 613 | |
| 614 | if (ret) { |
| 615 | break; |
| 616 | } |
| 617 | curr_section_name = lttng_elf_get_section_name(elf, |
| 618 | section_hdr->sh_name); |
| 619 | if (!curr_section_name) { |
| 620 | continue; |
| 621 | } |
| 622 | name_equal = strcmp(curr_section_name, section_name) == 0; |
| 623 | free(curr_section_name); |
| 624 | if (name_equal) { |
| 625 | return 0; |
| 626 | } |
| 627 | } |
| 628 | return LTTNG_ERR_ELF_PARSING; |
| 629 | } |
| 630 | |
| 631 | static |
| 632 | char *lttng_elf_get_section_data(struct lttng_elf *elf, |
| 633 | struct lttng_elf_shdr *shdr) |
| 634 | { |
| 635 | int ret; |
| 636 | off_t section_offset; |
| 637 | char *data; |
| 638 | size_t max_alloc_size; |
| 639 | |
| 640 | if (!elf || !shdr) { |
| 641 | goto error; |
| 642 | } |
| 643 | |
| 644 | max_alloc_size = min_t(size_t, MAX_SECTION_DATA_SIZE, elf->file_size); |
| 645 | |
| 646 | section_offset = shdr->sh_offset; |
| 647 | if (lseek(elf->fd, section_offset, SEEK_SET) < 0) { |
| 648 | PERROR("Error seeking to section offset"); |
| 649 | goto error; |
| 650 | } |
| 651 | |
| 652 | if (shdr->sh_size > max_alloc_size) { |
| 653 | ERR("ELF section size exceeds maximal allowed size of %zu bytes", |
| 654 | max_alloc_size); |
| 655 | goto error; |
| 656 | } |
| 657 | data = zmalloc(shdr->sh_size); |
| 658 | if (!data) { |
| 659 | PERROR("Error allocating buffer for ELF section data"); |
| 660 | goto error; |
| 661 | } |
| 662 | ret = lttng_read(elf->fd, data, shdr->sh_size); |
| 663 | if (ret == -1) { |
| 664 | PERROR("Error reading ELF section data"); |
| 665 | goto free_error; |
| 666 | } |
| 667 | |
| 668 | return data; |
| 669 | |
| 670 | free_error: |
| 671 | free(data); |
| 672 | error: |
| 673 | return NULL; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * Convert the virtual address in a binary's mapping to the offset of |
| 678 | * the corresponding instruction in the binary file. |
| 679 | * This function assumes the address is in the text section. |
| 680 | * |
| 681 | * Returns the offset on success or non-zero in case of failure. |
| 682 | */ |
| 683 | static |
| 684 | int lttng_elf_convert_addr_in_text_to_offset(struct lttng_elf *elf_handle, |
| 685 | size_t addr, uint64_t *offset) |
| 686 | { |
| 687 | int ret = 0; |
| 688 | off_t text_section_offset; |
| 689 | off_t text_section_addr_beg; |
| 690 | off_t text_section_addr_end; |
| 691 | off_t offset_in_section; |
| 692 | struct lttng_elf_shdr text_section_hdr; |
| 693 | |
| 694 | if (!elf_handle) { |
| 695 | DBG("Invalid ELF handle."); |
| 696 | ret = LTTNG_ERR_ELF_PARSING; |
| 697 | goto error; |
| 698 | } |
| 699 | |
| 700 | /* Get a pointer to the .text section header. */ |
| 701 | ret = lttng_elf_get_section_hdr_by_name(elf_handle, |
| 702 | TEXT_SECTION_NAME, &text_section_hdr); |
| 703 | if (ret) { |
| 704 | DBG("Text section not found in binary."); |
| 705 | ret = LTTNG_ERR_ELF_PARSING; |
| 706 | goto error; |
| 707 | } |
| 708 | |
| 709 | text_section_offset = text_section_hdr.sh_offset; |
| 710 | text_section_addr_beg = text_section_hdr.sh_addr; |
| 711 | text_section_addr_end = |
| 712 | text_section_addr_beg + text_section_hdr.sh_size; |
| 713 | |
| 714 | /* |
| 715 | * Verify that the address is within the .text section boundaries. |
| 716 | */ |
| 717 | if (addr < text_section_addr_beg || addr > text_section_addr_end) { |
| 718 | DBG("Address found is outside of the .text section addr=0x%zx, " |
| 719 | ".text section=[0x%jd - 0x%jd].", addr, (intmax_t)text_section_addr_beg, |
| 720 | (intmax_t)text_section_addr_end); |
| 721 | ret = LTTNG_ERR_ELF_PARSING; |
| 722 | goto error; |
| 723 | } |
| 724 | |
| 725 | offset_in_section = addr - text_section_addr_beg; |
| 726 | |
| 727 | /* |
| 728 | * Add the target offset in the text section to the offset of this text |
| 729 | * section from the beginning of the binary file. |
| 730 | */ |
| 731 | *offset = text_section_offset + offset_in_section; |
| 732 | |
| 733 | error: |
| 734 | return ret; |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | * Compute the offset of a symbol from the begining of the ELF binary. |
| 739 | * |
| 740 | * On success, returns 0 offset parameter is set to the computed value |
| 741 | * On failure, returns -1. |
| 742 | */ |
| 743 | int lttng_elf_get_symbol_offset(int fd, char *symbol, uint64_t *offset) |
| 744 | { |
| 745 | int ret = 0; |
| 746 | int sym_found = 0; |
| 747 | int sym_count = 0; |
| 748 | int sym_idx = 0; |
| 749 | uint64_t addr = 0; |
| 750 | char *curr_sym_str = NULL; |
| 751 | char *symbol_table_data = NULL; |
| 752 | char *string_table_data = NULL; |
| 753 | const char *string_table_name = NULL; |
| 754 | struct lttng_elf_shdr symtab_hdr; |
| 755 | struct lttng_elf_shdr strtab_hdr; |
| 756 | struct lttng_elf *elf = NULL; |
| 757 | |
| 758 | if (!symbol || !offset ) { |
| 759 | ret = LTTNG_ERR_ELF_PARSING; |
| 760 | goto end; |
| 761 | } |
| 762 | |
| 763 | elf = lttng_elf_create(fd); |
| 764 | if (!elf) { |
| 765 | ret = LTTNG_ERR_ELF_PARSING; |
| 766 | goto end; |
| 767 | } |
| 768 | |
| 769 | /* |
| 770 | * The .symtab section might not exist on stripped binaries. |
| 771 | * Try to get the symbol table section header first. If it's absent, |
| 772 | * try to get the dynamic symbol table. All symbols in the dynamic |
| 773 | * symbol tab are in the (normal) symbol table if it exists. |
| 774 | */ |
| 775 | ret = lttng_elf_get_section_hdr_by_name(elf, SYMBOL_TAB_SECTION_NAME, |
| 776 | &symtab_hdr); |
| 777 | if (ret) { |
| 778 | DBG("Cannot get ELF Symbol Table section. Trying to get ELF Dynamic Symbol Table section."); |
| 779 | /* Get the dynamic symbol table section header. */ |
| 780 | ret = lttng_elf_get_section_hdr_by_name(elf, DYNAMIC_SYMBOL_TAB_SECTION_NAME, |
| 781 | &symtab_hdr); |
| 782 | if (ret) { |
| 783 | DBG("Cannot get ELF Symbol Table nor Dynamic Symbol Table sections."); |
| 784 | ret = LTTNG_ERR_ELF_PARSING; |
| 785 | goto destroy_elf; |
| 786 | } |
| 787 | string_table_name = DYNAMIC_STRING_TAB_SECTION_NAME; |
| 788 | } else { |
| 789 | string_table_name = STRING_TAB_SECTION_NAME; |
| 790 | } |
| 791 | |
| 792 | /* Get the data associated with the symbol table section. */ |
| 793 | symbol_table_data = lttng_elf_get_section_data(elf, &symtab_hdr); |
| 794 | if (symbol_table_data == NULL) { |
| 795 | DBG("Cannot get ELF Symbol Table data."); |
| 796 | ret = LTTNG_ERR_ELF_PARSING; |
| 797 | goto destroy_elf; |
| 798 | } |
| 799 | |
| 800 | /* Get the string table section header. */ |
| 801 | ret = lttng_elf_get_section_hdr_by_name(elf, string_table_name, |
| 802 | &strtab_hdr); |
| 803 | if (ret) { |
| 804 | DBG("Cannot get ELF string table section."); |
| 805 | goto free_symbol_table_data; |
| 806 | } |
| 807 | |
| 808 | /* Get the data associated with the string table section. */ |
| 809 | string_table_data = lttng_elf_get_section_data(elf, &strtab_hdr); |
| 810 | if (string_table_data == NULL) { |
| 811 | DBG("Cannot get ELF string table section data."); |
| 812 | ret = LTTNG_ERR_ELF_PARSING; |
| 813 | goto free_symbol_table_data; |
| 814 | } |
| 815 | |
| 816 | /* Get the number of symbol in the table for the iteration. */ |
| 817 | if (symtab_hdr.sh_entsize == 0) { |
| 818 | DBG("Invalid ELF string table entry size."); |
| 819 | ret = LTTNG_ERR_ELF_PARSING; |
| 820 | goto free_string_table_data; |
| 821 | } |
| 822 | |
| 823 | sym_count = symtab_hdr.sh_size / symtab_hdr.sh_entsize; |
| 824 | |
| 825 | /* Loop over all symbol. */ |
| 826 | for (sym_idx = 0; sym_idx < sym_count; sym_idx++) { |
| 827 | struct lttng_elf_sym curr_sym; |
| 828 | |
| 829 | /* Get the symbol at the current index. */ |
| 830 | if (is_elf_32_bit(elf)) { |
| 831 | Elf32_Sym tmp = ((Elf32_Sym *) symbol_table_data)[sym_idx]; |
| 832 | copy_sym(tmp, curr_sym); |
| 833 | } else { |
| 834 | Elf64_Sym tmp = ((Elf64_Sym *) symbol_table_data)[sym_idx]; |
| 835 | copy_sym(tmp, curr_sym); |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | * If the st_name field is zero, there is no string name for |
| 840 | * this symbol; skip to the next symbol. |
| 841 | */ |
| 842 | if (curr_sym.st_name == 0) { |
| 843 | continue; |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * Use the st_name field in the lttng_elf_sym struct to get offset of |
| 848 | * the symbol's name from the beginning of the string table. |
| 849 | */ |
| 850 | curr_sym_str = string_table_data + curr_sym.st_name; |
| 851 | |
| 852 | /* |
| 853 | * If the current symbol is not a function; skip to the next symbol. |
| 854 | */ |
| 855 | if (ELF_ST_TYPE(curr_sym.st_info) != STT_FUNC) { |
| 856 | continue; |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * Compare with the search symbol. If there is a match set the address |
| 861 | * output parameter and return success. |
| 862 | */ |
| 863 | if (strcmp(symbol, curr_sym_str) == 0 ) { |
| 864 | sym_found = 1; |
| 865 | addr = curr_sym.st_value; |
| 866 | break; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | if (!sym_found) { |
| 871 | DBG("Symbol not found."); |
| 872 | ret = LTTNG_ERR_ELF_PARSING; |
| 873 | goto free_string_table_data; |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * Use the virtual address of the symbol to compute the offset of this |
| 878 | * symbol from the beginning of the executable file. |
| 879 | */ |
| 880 | ret = lttng_elf_convert_addr_in_text_to_offset(elf, addr, offset); |
| 881 | if (ret) { |
| 882 | DBG("Cannot convert addr to offset."); |
| 883 | goto free_string_table_data; |
| 884 | } |
| 885 | |
| 886 | |
| 887 | free_string_table_data: |
| 888 | free(string_table_data); |
| 889 | free_symbol_table_data: |
| 890 | free(symbol_table_data); |
| 891 | destroy_elf: |
| 892 | lttng_elf_destroy(elf); |
| 893 | end: |
| 894 | return ret; |
| 895 | } |
| 896 | |
| 897 | /* |
| 898 | * Compute the offsets of SDT probes from the begining of the ELF binary. |
| 899 | * |
| 900 | * On success, returns 0 and the nb_probes parameter is set to the number of |
| 901 | * offsets found and the offsets parameter points to an array of offsets where |
| 902 | * the SDT probes are. |
| 903 | * On failure, returns -1. |
| 904 | */ |
| 905 | int lttng_elf_get_sdt_probe_offsets(int fd, const char *provider_name, |
| 906 | const char *probe_name, uint64_t **offsets, uint32_t *nb_probes) |
| 907 | { |
| 908 | int ret = 0, nb_match = 0; |
| 909 | struct lttng_elf_shdr stap_note_section_hdr; |
| 910 | struct lttng_elf *elf = NULL; |
| 911 | char *stap_note_section_data = NULL; |
| 912 | char *curr_note_section_begin, *curr_data_ptr, *curr_probe, *curr_provider; |
| 913 | char *next_note_ptr; |
| 914 | uint32_t name_size, desc_size, note_type; |
| 915 | uint64_t curr_probe_location, curr_probe_offset, curr_semaphore_location; |
| 916 | uint64_t *probe_locs = NULL, *new_probe_locs = NULL; |
| 917 | |
| 918 | if (!provider_name || !probe_name || !nb_probes || !offsets) { |
| 919 | DBG("Invalid arguments."); |
| 920 | ret = LTTNG_ERR_ELF_PARSING; |
| 921 | goto error; |
| 922 | } |
| 923 | |
| 924 | elf = lttng_elf_create(fd); |
| 925 | if (!elf) { |
| 926 | DBG("Error allocation ELF."); |
| 927 | ret = LTTNG_ERR_ELF_PARSING; |
| 928 | goto error; |
| 929 | } |
| 930 | |
| 931 | /* Get the stap note section header. */ |
| 932 | ret = lttng_elf_get_section_hdr_by_name(elf, NOTE_STAPSDT_SECTION_NAME, |
| 933 | &stap_note_section_hdr); |
| 934 | if (ret) { |
| 935 | DBG("Cannot get ELF stap note section."); |
| 936 | goto destroy_elf_error; |
| 937 | } |
| 938 | |
| 939 | /* Get the data associated with the stap note section. */ |
| 940 | stap_note_section_data = |
| 941 | lttng_elf_get_section_data(elf, &stap_note_section_hdr); |
| 942 | if (stap_note_section_data == NULL) { |
| 943 | DBG("Cannot get ELF stap note section data."); |
| 944 | ret = LTTNG_ERR_ELF_PARSING; |
| 945 | goto destroy_elf_error; |
| 946 | } |
| 947 | |
| 948 | next_note_ptr = stap_note_section_data; |
| 949 | curr_note_section_begin = stap_note_section_data; |
| 950 | |
| 951 | *offsets = NULL; |
| 952 | while (1) { |
| 953 | curr_data_ptr = next_note_ptr; |
| 954 | /* Check if we have reached the end of the note section. */ |
| 955 | if (curr_data_ptr >= |
| 956 | curr_note_section_begin + |
| 957 | stap_note_section_hdr.sh_size) { |
| 958 | *nb_probes = nb_match; |
| 959 | *offsets = probe_locs; |
| 960 | ret = 0; |
| 961 | break; |
| 962 | } |
| 963 | /* Get name size field. */ |
| 964 | name_size = next_4bytes_boundary(*(uint32_t*) curr_data_ptr); |
| 965 | curr_data_ptr += sizeof(uint32_t); |
| 966 | |
| 967 | /* Sanity check; a zero name_size is reserved. */ |
| 968 | if (name_size == 0) { |
| 969 | DBG("Invalid name size field in SDT probe descriptions" |
| 970 | "section."); |
| 971 | ret = -1; |
| 972 | goto realloc_error; |
| 973 | } |
| 974 | |
| 975 | /* Get description size field. */ |
| 976 | desc_size = next_4bytes_boundary(*(uint32_t*) curr_data_ptr); |
| 977 | curr_data_ptr += sizeof(uint32_t); |
| 978 | |
| 979 | /* Get type field. */ |
| 980 | note_type = *(uint32_t *) curr_data_ptr; |
| 981 | curr_data_ptr += sizeof(uint32_t); |
| 982 | |
| 983 | /* |
| 984 | * Move the pointer to the next note to be ready for the next |
| 985 | * iteration. The current note is made of 3 unsigned 32bit |
| 986 | * integers (name size, descriptor size and note type), the |
| 987 | * name and the descriptor. To move to the next note, we move |
| 988 | * the pointer according to those values. |
| 989 | */ |
| 990 | next_note_ptr = next_note_ptr + |
| 991 | (3 * sizeof(uint32_t)) + desc_size + name_size; |
| 992 | |
| 993 | /* |
| 994 | * Move ptr to the end of the name string (we don't need it) |
| 995 | * and go to the next 4 byte alignement. |
| 996 | */ |
| 997 | if (note_type != NOTE_STAPSDT_TYPE || |
| 998 | strncmp(curr_data_ptr, NOTE_STAPSDT_NAME, name_size) != 0) { |
| 999 | continue; |
| 1000 | } |
| 1001 | |
| 1002 | curr_data_ptr += name_size; |
| 1003 | |
| 1004 | /* Get probe location. */ |
| 1005 | curr_probe_location = *(uint64_t *) curr_data_ptr; |
| 1006 | curr_data_ptr += sizeof(uint64_t); |
| 1007 | |
| 1008 | /* Pass over the base. Not needed. */ |
| 1009 | curr_data_ptr += sizeof(uint64_t); |
| 1010 | |
| 1011 | /* Get semaphore location. */ |
| 1012 | curr_semaphore_location = *(uint64_t *) curr_data_ptr; |
| 1013 | curr_data_ptr += sizeof(uint64_t); |
| 1014 | /* Get provider name. */ |
| 1015 | curr_provider = curr_data_ptr; |
| 1016 | curr_data_ptr += strlen(curr_provider) + 1; |
| 1017 | |
| 1018 | /* Get probe name. */ |
| 1019 | curr_probe = curr_data_ptr; |
| 1020 | |
| 1021 | /* Check if the provider and probe name match */ |
| 1022 | if (strcmp(provider_name, curr_provider) == 0 && |
| 1023 | strcmp(probe_name, curr_probe) == 0) { |
| 1024 | int new_size; |
| 1025 | |
| 1026 | /* |
| 1027 | * We currently don't support SDT probes with semaphores. Return |
| 1028 | * success as we found a matching probe but it's guarded by a |
| 1029 | * semaphore. |
| 1030 | */ |
| 1031 | if (curr_semaphore_location != 0) { |
| 1032 | ret = LTTNG_ERR_SDT_PROBE_SEMAPHORE; |
| 1033 | goto realloc_error; |
| 1034 | } |
| 1035 | |
| 1036 | new_size = (++nb_match) * sizeof(uint64_t); |
| 1037 | |
| 1038 | /* |
| 1039 | * Found a match with not semaphore, we need to copy the |
| 1040 | * probe_location to the output parameter. |
| 1041 | */ |
| 1042 | new_probe_locs = realloc(probe_locs, new_size); |
| 1043 | if (!new_probe_locs) { |
| 1044 | /* Error allocating a larger buffer */ |
| 1045 | DBG("Allocation error in SDT."); |
| 1046 | ret = LTTNG_ERR_NOMEM; |
| 1047 | goto realloc_error; |
| 1048 | } |
| 1049 | probe_locs = new_probe_locs; |
| 1050 | new_probe_locs = NULL; |
| 1051 | |
| 1052 | /* |
| 1053 | * Use the virtual address of the probe to compute the offset of |
| 1054 | * this probe from the beginning of the executable file. |
| 1055 | */ |
| 1056 | ret = lttng_elf_convert_addr_in_text_to_offset(elf, |
| 1057 | curr_probe_location, &curr_probe_offset); |
| 1058 | if (ret) { |
| 1059 | DBG("Conversion error in SDT."); |
| 1060 | goto realloc_error; |
| 1061 | } |
| 1062 | |
| 1063 | probe_locs[nb_match - 1] = curr_probe_offset; |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | end: |
| 1068 | free(stap_note_section_data); |
| 1069 | destroy_elf_error: |
| 1070 | lttng_elf_destroy(elf); |
| 1071 | error: |
| 1072 | return ret; |
| 1073 | realloc_error: |
| 1074 | free(probe_locs); |
| 1075 | goto end; |
| 1076 | } |