1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * elf.c - ELF access library 4 * 5 * Adapted from kpatch (https://github.com/dynup/kpatch): 6 * Copyright (C) 2013-2015 Josh Poimboeuf <[email protected]> 7 * Copyright (C) 2014 Seth Jennings <[email protected]> 8 */ 9 10 #include <sys/types.h> 11 #include <sys/stat.h> 12 #include <sys/mman.h> 13 #include <fcntl.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <string.h> 17 #include <unistd.h> 18 #include <errno.h> 19 #include <linux/interval_tree_generic.h> 20 #include <objtool/builtin.h> 21 22 #include <objtool/elf.h> 23 #include <objtool/warn.h> 24 25 #define MAX_NAME_LEN 128 26 27 static inline u32 str_hash(const char *str) 28 { 29 return jhash(str, strlen(str), 0); 30 } 31 32 #define __elf_table(name) (elf->name##_hash) 33 #define __elf_bits(name) (elf->name##_bits) 34 35 #define elf_hash_add(name, node, key) \ 36 hlist_add_head(node, &__elf_table(name)[hash_min(key, __elf_bits(name))]) 37 38 #define elf_hash_for_each_possible(name, obj, member, key) \ 39 hlist_for_each_entry(obj, &__elf_table(name)[hash_min(key, __elf_bits(name))], member) 40 41 #define elf_alloc_hash(name, size) \ 42 ({ \ 43 __elf_bits(name) = max(10, ilog2(size)); \ 44 __elf_table(name) = mmap(NULL, sizeof(struct hlist_head) << __elf_bits(name), \ 45 PROT_READ|PROT_WRITE, \ 46 MAP_PRIVATE|MAP_ANON, -1, 0); \ 47 if (__elf_table(name) == (void *)-1L) { \ 48 WARN("mmap fail " #name); \ 49 __elf_table(name) = NULL; \ 50 } \ 51 __elf_table(name); \ 52 }) 53 54 static inline unsigned long __sym_start(struct symbol *s) 55 { 56 return s->offset; 57 } 58 59 static inline unsigned long __sym_last(struct symbol *s) 60 { 61 return s->offset + s->len - 1; 62 } 63 64 INTERVAL_TREE_DEFINE(struct symbol, node, unsigned long, __subtree_last, 65 __sym_start, __sym_last, static, __sym) 66 67 #define __sym_for_each(_iter, _tree, _start, _end) \ 68 for (_iter = __sym_iter_first((_tree), (_start), (_end)); \ 69 _iter; _iter = __sym_iter_next(_iter, (_start), (_end))) 70 71 struct symbol_hole { 72 unsigned long key; 73 const struct symbol *sym; 74 }; 75 76 /* 77 * Find !section symbol where @offset is after it. 78 */ 79 static int symbol_hole_by_offset(const void *key, const struct rb_node *node) 80 { 81 const struct symbol *s = rb_entry(node, struct symbol, node); 82 struct symbol_hole *sh = (void *)key; 83 84 if (sh->key < s->offset) 85 return -1; 86 87 if (sh->key >= s->offset + s->len) { 88 if (s->type != STT_SECTION) 89 sh->sym = s; 90 return 1; 91 } 92 93 return 0; 94 } 95 96 struct section *find_section_by_name(const struct elf *elf, const char *name) 97 { 98 struct section *sec; 99 100 elf_hash_for_each_possible(section_name, sec, name_hash, str_hash(name)) { 101 if (!strcmp(sec->name, name)) 102 return sec; 103 } 104 105 return NULL; 106 } 107 108 static struct section *find_section_by_index(struct elf *elf, 109 unsigned int idx) 110 { 111 struct section *sec; 112 113 elf_hash_for_each_possible(section, sec, hash, idx) { 114 if (sec->idx == idx) 115 return sec; 116 } 117 118 return NULL; 119 } 120 121 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx) 122 { 123 struct symbol *sym; 124 125 elf_hash_for_each_possible(symbol, sym, hash, idx) { 126 if (sym->idx == idx) 127 return sym; 128 } 129 130 return NULL; 131 } 132 133 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset) 134 { 135 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree; 136 struct symbol *iter; 137 138 __sym_for_each(iter, tree, offset, offset) { 139 if (iter->offset == offset && iter->type != STT_SECTION) 140 return iter; 141 } 142 143 return NULL; 144 } 145 146 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset) 147 { 148 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree; 149 struct symbol *iter; 150 151 __sym_for_each(iter, tree, offset, offset) { 152 if (iter->offset == offset && iter->type == STT_FUNC) 153 return iter; 154 } 155 156 return NULL; 157 } 158 159 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset) 160 { 161 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree; 162 struct symbol *iter; 163 164 __sym_for_each(iter, tree, offset, offset) { 165 if (iter->type != STT_SECTION) 166 return iter; 167 } 168 169 return NULL; 170 } 171 172 /* 173 * Returns size of hole starting at @offset. 174 */ 175 int find_symbol_hole_containing(const struct section *sec, unsigned long offset) 176 { 177 struct symbol_hole hole = { 178 .key = offset, 179 .sym = NULL, 180 }; 181 struct rb_node *n; 182 struct symbol *s; 183 184 /* 185 * Find the rightmost symbol for which @offset is after it. 186 */ 187 n = rb_find(&hole, &sec->symbol_tree.rb_root, symbol_hole_by_offset); 188 189 /* found a symbol that contains @offset */ 190 if (n) 191 return 0; /* not a hole */ 192 193 /* didn't find a symbol for which @offset is after it */ 194 if (!hole.sym) 195 return 0; /* not a hole */ 196 197 /* @offset >= sym->offset + sym->len, find symbol after it */ 198 n = rb_next(&hole.sym->node); 199 if (!n) 200 return -1; /* until end of address space */ 201 202 /* hole until start of next symbol */ 203 s = rb_entry(n, struct symbol, node); 204 return s->offset - offset; 205 } 206 207 struct symbol *find_func_containing(struct section *sec, unsigned long offset) 208 { 209 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree; 210 struct symbol *iter; 211 212 __sym_for_each(iter, tree, offset, offset) { 213 if (iter->type == STT_FUNC) 214 return iter; 215 } 216 217 return NULL; 218 } 219 220 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name) 221 { 222 struct symbol *sym; 223 224 elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) { 225 if (!strcmp(sym->name, name)) 226 return sym; 227 } 228 229 return NULL; 230 } 231 232 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec, 233 unsigned long offset, unsigned int len) 234 { 235 struct reloc *reloc, *r = NULL; 236 struct section *rsec; 237 unsigned long o; 238 239 rsec = sec->rsec; 240 if (!rsec) 241 return NULL; 242 243 for_offset_range(o, offset, offset + len) { 244 elf_hash_for_each_possible(reloc, reloc, hash, 245 sec_offset_hash(rsec, o)) { 246 if (reloc->sec != rsec) 247 continue; 248 249 if (reloc_offset(reloc) >= offset && 250 reloc_offset(reloc) < offset + len) { 251 if (!r || reloc_offset(reloc) < reloc_offset(r)) 252 r = reloc; 253 } 254 } 255 if (r) 256 return r; 257 } 258 259 return NULL; 260 } 261 262 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset) 263 { 264 return find_reloc_by_dest_range(elf, sec, offset, 1); 265 } 266 267 static int read_sections(struct elf *elf) 268 { 269 Elf_Scn *s = NULL; 270 struct section *sec; 271 size_t shstrndx, sections_nr; 272 int i; 273 274 if (elf_getshdrnum(elf->elf, §ions_nr)) { 275 WARN_ELF("elf_getshdrnum"); 276 return -1; 277 } 278 279 if (elf_getshdrstrndx(elf->elf, &shstrndx)) { 280 WARN_ELF("elf_getshdrstrndx"); 281 return -1; 282 } 283 284 if (!elf_alloc_hash(section, sections_nr) || 285 !elf_alloc_hash(section_name, sections_nr)) 286 return -1; 287 288 elf->section_data = calloc(sections_nr, sizeof(*sec)); 289 if (!elf->section_data) { 290 perror("calloc"); 291 return -1; 292 } 293 for (i = 0; i < sections_nr; i++) { 294 sec = &elf->section_data[i]; 295 296 INIT_LIST_HEAD(&sec->symbol_list); 297 298 s = elf_getscn(elf->elf, i); 299 if (!s) { 300 WARN_ELF("elf_getscn"); 301 return -1; 302 } 303 304 sec->idx = elf_ndxscn(s); 305 306 if (!gelf_getshdr(s, &sec->sh)) { 307 WARN_ELF("gelf_getshdr"); 308 return -1; 309 } 310 311 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name); 312 if (!sec->name) { 313 WARN_ELF("elf_strptr"); 314 return -1; 315 } 316 317 if (sec->sh.sh_size != 0) { 318 sec->data = elf_getdata(s, NULL); 319 if (!sec->data) { 320 WARN_ELF("elf_getdata"); 321 return -1; 322 } 323 if (sec->data->d_off != 0 || 324 sec->data->d_size != sec->sh.sh_size) { 325 WARN("unexpected data attributes for %s", 326 sec->name); 327 return -1; 328 } 329 } 330 331 list_add_tail(&sec->list, &elf->sections); 332 elf_hash_add(section, &sec->hash, sec->idx); 333 elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name)); 334 335 if (is_reloc_sec(sec)) 336 elf->num_relocs += sec_num_entries(sec); 337 } 338 339 if (opts.stats) { 340 printf("nr_sections: %lu\n", (unsigned long)sections_nr); 341 printf("section_bits: %d\n", elf->section_bits); 342 } 343 344 /* sanity check, one more call to elf_nextscn() should return NULL */ 345 if (elf_nextscn(elf->elf, s)) { 346 WARN("section entry mismatch"); 347 return -1; 348 } 349 350 return 0; 351 } 352 353 static void elf_add_symbol(struct elf *elf, struct symbol *sym) 354 { 355 struct list_head *entry; 356 struct rb_node *pnode; 357 struct symbol *iter; 358 359 INIT_LIST_HEAD(&sym->reloc_list); 360 INIT_LIST_HEAD(&sym->pv_target); 361 sym->alias = sym; 362 363 sym->type = GELF_ST_TYPE(sym->sym.st_info); 364 sym->bind = GELF_ST_BIND(sym->sym.st_info); 365 366 if (sym->type == STT_FILE) 367 elf->num_files++; 368 369 sym->offset = sym->sym.st_value; 370 sym->len = sym->sym.st_size; 371 372 __sym_for_each(iter, &sym->sec->symbol_tree, sym->offset, sym->offset) { 373 if (iter->offset == sym->offset && iter->type == sym->type) 374 iter->alias = sym; 375 } 376 377 __sym_insert(sym, &sym->sec->symbol_tree); 378 pnode = rb_prev(&sym->node); 379 if (pnode) 380 entry = &rb_entry(pnode, struct symbol, node)->list; 381 else 382 entry = &sym->sec->symbol_list; 383 list_add(&sym->list, entry); 384 elf_hash_add(symbol, &sym->hash, sym->idx); 385 elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name)); 386 387 /* 388 * Don't store empty STT_NOTYPE symbols in the rbtree. They 389 * can exist within a function, confusing the sorting. 390 */ 391 if (!sym->len) 392 __sym_remove(sym, &sym->sec->symbol_tree); 393 } 394 395 static int read_symbols(struct elf *elf) 396 { 397 struct section *symtab, *symtab_shndx, *sec; 398 struct symbol *sym, *pfunc; 399 int symbols_nr, i; 400 char *coldstr; 401 Elf_Data *shndx_data = NULL; 402 Elf32_Word shndx; 403 404 symtab = find_section_by_name(elf, ".symtab"); 405 if (symtab) { 406 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); 407 if (symtab_shndx) 408 shndx_data = symtab_shndx->data; 409 410 symbols_nr = sec_num_entries(symtab); 411 } else { 412 /* 413 * A missing symbol table is actually possible if it's an empty 414 * .o file. This can happen for thunk_64.o. Make sure to at 415 * least allocate the symbol hash tables so we can do symbol 416 * lookups without crashing. 417 */ 418 symbols_nr = 0; 419 } 420 421 if (!elf_alloc_hash(symbol, symbols_nr) || 422 !elf_alloc_hash(symbol_name, symbols_nr)) 423 return -1; 424 425 elf->symbol_data = calloc(symbols_nr, sizeof(*sym)); 426 if (!elf->symbol_data) { 427 perror("calloc"); 428 return -1; 429 } 430 for (i = 0; i < symbols_nr; i++) { 431 sym = &elf->symbol_data[i]; 432 433 sym->idx = i; 434 435 if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym, 436 &shndx)) { 437 WARN_ELF("gelf_getsymshndx"); 438 goto err; 439 } 440 441 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link, 442 sym->sym.st_name); 443 if (!sym->name) { 444 WARN_ELF("elf_strptr"); 445 goto err; 446 } 447 448 if ((sym->sym.st_shndx > SHN_UNDEF && 449 sym->sym.st_shndx < SHN_LORESERVE) || 450 (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) { 451 if (sym->sym.st_shndx != SHN_XINDEX) 452 shndx = sym->sym.st_shndx; 453 454 sym->sec = find_section_by_index(elf, shndx); 455 if (!sym->sec) { 456 WARN("couldn't find section for symbol %s", 457 sym->name); 458 goto err; 459 } 460 if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) { 461 sym->name = sym->sec->name; 462 sym->sec->sym = sym; 463 } 464 } else 465 sym->sec = find_section_by_index(elf, 0); 466 467 elf_add_symbol(elf, sym); 468 } 469 470 if (opts.stats) { 471 printf("nr_symbols: %lu\n", (unsigned long)symbols_nr); 472 printf("symbol_bits: %d\n", elf->symbol_bits); 473 } 474 475 /* Create parent/child links for any cold subfunctions */ 476 list_for_each_entry(sec, &elf->sections, list) { 477 sec_for_each_sym(sec, sym) { 478 char pname[MAX_NAME_LEN + 1]; 479 size_t pnamelen; 480 if (sym->type != STT_FUNC) 481 continue; 482 483 if (sym->pfunc == NULL) 484 sym->pfunc = sym; 485 486 if (sym->cfunc == NULL) 487 sym->cfunc = sym; 488 489 coldstr = strstr(sym->name, ".cold"); 490 if (!coldstr) 491 continue; 492 493 pnamelen = coldstr - sym->name; 494 if (pnamelen > MAX_NAME_LEN) { 495 WARN("%s(): parent function name exceeds maximum length of %d characters", 496 sym->name, MAX_NAME_LEN); 497 return -1; 498 } 499 500 strncpy(pname, sym->name, pnamelen); 501 pname[pnamelen] = '\0'; 502 pfunc = find_symbol_by_name(elf, pname); 503 504 if (!pfunc) { 505 WARN("%s(): can't find parent function", 506 sym->name); 507 return -1; 508 } 509 510 sym->pfunc = pfunc; 511 pfunc->cfunc = sym; 512 513 /* 514 * Unfortunately, -fnoreorder-functions puts the child 515 * inside the parent. Remove the overlap so we can 516 * have sane assumptions. 517 * 518 * Note that pfunc->len now no longer matches 519 * pfunc->sym.st_size. 520 */ 521 if (sym->sec == pfunc->sec && 522 sym->offset >= pfunc->offset && 523 sym->offset + sym->len == pfunc->offset + pfunc->len) { 524 pfunc->len -= sym->len; 525 } 526 } 527 } 528 529 return 0; 530 531 err: 532 free(sym); 533 return -1; 534 } 535 536 /* 537 * @sym's idx has changed. Update the relocs which reference it. 538 */ 539 static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym) 540 { 541 struct reloc *reloc; 542 543 list_for_each_entry(reloc, &sym->reloc_list, sym_reloc_entry) { 544 reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc_type(reloc)); 545 if (elf_write_reloc(elf, reloc)) 546 return -1; 547 } 548 549 return 0; 550 } 551 552 /* 553 * The libelf API is terrible; gelf_update_sym*() takes a data block relative 554 * index value, *NOT* the symbol index. As such, iterate the data blocks and 555 * adjust index until it fits. 556 * 557 * If no data block is found, allow adding a new data block provided the index 558 * is only one past the end. 559 */ 560 static int elf_update_symbol(struct elf *elf, struct section *symtab, 561 struct section *symtab_shndx, struct symbol *sym) 562 { 563 Elf32_Word shndx = sym->sec ? sym->sec->idx : SHN_UNDEF; 564 Elf_Data *symtab_data = NULL, *shndx_data = NULL; 565 Elf64_Xword entsize = symtab->sh.sh_entsize; 566 int max_idx, idx = sym->idx; 567 Elf_Scn *s, *t = NULL; 568 bool is_special_shndx = sym->sym.st_shndx >= SHN_LORESERVE && 569 sym->sym.st_shndx != SHN_XINDEX; 570 571 if (is_special_shndx) 572 shndx = sym->sym.st_shndx; 573 574 s = elf_getscn(elf->elf, symtab->idx); 575 if (!s) { 576 WARN_ELF("elf_getscn"); 577 return -1; 578 } 579 580 if (symtab_shndx) { 581 t = elf_getscn(elf->elf, symtab_shndx->idx); 582 if (!t) { 583 WARN_ELF("elf_getscn"); 584 return -1; 585 } 586 } 587 588 for (;;) { 589 /* get next data descriptor for the relevant sections */ 590 symtab_data = elf_getdata(s, symtab_data); 591 if (t) 592 shndx_data = elf_getdata(t, shndx_data); 593 594 /* end-of-list */ 595 if (!symtab_data) { 596 /* 597 * Over-allocate to avoid O(n^2) symbol creation 598 * behaviour. The down side is that libelf doesn't 599 * like this; see elf_truncate_section() for the fixup. 600 */ 601 int num = max(1U, sym->idx/3); 602 void *buf; 603 604 if (idx) { 605 /* we don't do holes in symbol tables */ 606 WARN("index out of range"); 607 return -1; 608 } 609 610 /* if @idx == 0, it's the next contiguous entry, create it */ 611 symtab_data = elf_newdata(s); 612 if (t) 613 shndx_data = elf_newdata(t); 614 615 buf = calloc(num, entsize); 616 if (!buf) { 617 WARN("malloc"); 618 return -1; 619 } 620 621 symtab_data->d_buf = buf; 622 symtab_data->d_size = num * entsize; 623 symtab_data->d_align = 1; 624 symtab_data->d_type = ELF_T_SYM; 625 626 mark_sec_changed(elf, symtab, true); 627 symtab->truncate = true; 628 629 if (t) { 630 buf = calloc(num, sizeof(Elf32_Word)); 631 if (!buf) { 632 WARN("malloc"); 633 return -1; 634 } 635 636 shndx_data->d_buf = buf; 637 shndx_data->d_size = num * sizeof(Elf32_Word); 638 shndx_data->d_align = sizeof(Elf32_Word); 639 shndx_data->d_type = ELF_T_WORD; 640 641 mark_sec_changed(elf, symtab_shndx, true); 642 symtab_shndx->truncate = true; 643 } 644 645 break; 646 } 647 648 /* empty blocks should not happen */ 649 if (!symtab_data->d_size) { 650 WARN("zero size data"); 651 return -1; 652 } 653 654 /* is this the right block? */ 655 max_idx = symtab_data->d_size / entsize; 656 if (idx < max_idx) 657 break; 658 659 /* adjust index and try again */ 660 idx -= max_idx; 661 } 662 663 /* something went side-ways */ 664 if (idx < 0) { 665 WARN("negative index"); 666 return -1; 667 } 668 669 /* setup extended section index magic and write the symbol */ 670 if ((shndx >= SHN_UNDEF && shndx < SHN_LORESERVE) || is_special_shndx) { 671 sym->sym.st_shndx = shndx; 672 if (!shndx_data) 673 shndx = 0; 674 } else { 675 sym->sym.st_shndx = SHN_XINDEX; 676 if (!shndx_data) { 677 WARN("no .symtab_shndx"); 678 return -1; 679 } 680 } 681 682 if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) { 683 WARN_ELF("gelf_update_symshndx"); 684 return -1; 685 } 686 687 return 0; 688 } 689 690 static struct symbol * 691 __elf_create_symbol(struct elf *elf, struct symbol *sym) 692 { 693 struct section *symtab, *symtab_shndx; 694 Elf32_Word first_non_local, new_idx; 695 struct symbol *old; 696 697 symtab = find_section_by_name(elf, ".symtab"); 698 if (symtab) { 699 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); 700 } else { 701 WARN("no .symtab"); 702 return NULL; 703 } 704 705 new_idx = sec_num_entries(symtab); 706 707 if (GELF_ST_BIND(sym->sym.st_info) != STB_LOCAL) 708 goto non_local; 709 710 /* 711 * Move the first global symbol, as per sh_info, into a new, higher 712 * symbol index. This fees up a spot for a new local symbol. 713 */ 714 first_non_local = symtab->sh.sh_info; 715 old = find_symbol_by_index(elf, first_non_local); 716 if (old) { 717 old->idx = new_idx; 718 719 hlist_del(&old->hash); 720 elf_hash_add(symbol, &old->hash, old->idx); 721 722 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) { 723 WARN("elf_update_symbol move"); 724 return NULL; 725 } 726 727 if (elf_update_sym_relocs(elf, old)) 728 return NULL; 729 730 new_idx = first_non_local; 731 } 732 733 /* 734 * Either way, we will add a LOCAL symbol. 735 */ 736 symtab->sh.sh_info += 1; 737 738 non_local: 739 sym->idx = new_idx; 740 if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) { 741 WARN("elf_update_symbol"); 742 return NULL; 743 } 744 745 symtab->sh.sh_size += symtab->sh.sh_entsize; 746 mark_sec_changed(elf, symtab, true); 747 748 if (symtab_shndx) { 749 symtab_shndx->sh.sh_size += sizeof(Elf32_Word); 750 mark_sec_changed(elf, symtab_shndx, true); 751 } 752 753 return sym; 754 } 755 756 static struct symbol * 757 elf_create_section_symbol(struct elf *elf, struct section *sec) 758 { 759 struct symbol *sym = calloc(1, sizeof(*sym)); 760 761 if (!sym) { 762 perror("malloc"); 763 return NULL; 764 } 765 766 sym->name = sec->name; 767 sym->sec = sec; 768 769 // st_name 0 770 sym->sym.st_info = GELF_ST_INFO(STB_LOCAL, STT_SECTION); 771 // st_other 0 772 // st_value 0 773 // st_size 0 774 775 sym = __elf_create_symbol(elf, sym); 776 if (sym) 777 elf_add_symbol(elf, sym); 778 779 return sym; 780 } 781 782 static int elf_add_string(struct elf *elf, struct section *strtab, char *str); 783 784 struct symbol * 785 elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size) 786 { 787 struct symbol *sym = calloc(1, sizeof(*sym)); 788 size_t namelen = strlen(orig->name) + sizeof("__pfx_"); 789 char *name = malloc(namelen); 790 791 if (!sym || !name) { 792 perror("malloc"); 793 return NULL; 794 } 795 796 snprintf(name, namelen, "__pfx_%s", orig->name); 797 798 sym->name = name; 799 sym->sec = orig->sec; 800 801 sym->sym.st_name = elf_add_string(elf, NULL, name); 802 sym->sym.st_info = orig->sym.st_info; 803 sym->sym.st_value = orig->sym.st_value - size; 804 sym->sym.st_size = size; 805 806 sym = __elf_create_symbol(elf, sym); 807 if (sym) 808 elf_add_symbol(elf, sym); 809 810 return sym; 811 } 812 813 static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec, 814 unsigned int reloc_idx, 815 unsigned long offset, struct symbol *sym, 816 s64 addend, unsigned int type) 817 { 818 struct reloc *reloc, empty = { 0 }; 819 820 if (reloc_idx >= sec_num_entries(rsec)) { 821 WARN("%s: bad reloc_idx %u for %s with %d relocs", 822 __func__, reloc_idx, rsec->name, sec_num_entries(rsec)); 823 return NULL; 824 } 825 826 reloc = &rsec->relocs[reloc_idx]; 827 828 if (memcmp(reloc, &empty, sizeof(empty))) { 829 WARN("%s: %s: reloc %d already initialized!", 830 __func__, rsec->name, reloc_idx); 831 return NULL; 832 } 833 834 reloc->sec = rsec; 835 reloc->sym = sym; 836 reloc->addend = addend; 837 838 reloc->rel.r_offset = offset; 839 reloc->rel.r_info = GELF_R_INFO(sym->idx, type); 840 841 if (elf_write_reloc(elf, reloc)) 842 return NULL; 843 844 list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); 845 elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); 846 847 return reloc; 848 } 849 850 struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec, 851 unsigned long offset, 852 unsigned int reloc_idx, 853 struct section *insn_sec, 854 unsigned long insn_off) 855 { 856 struct symbol *sym = insn_sec->sym; 857 int addend = insn_off; 858 859 if (!(insn_sec->sh.sh_flags & SHF_EXECINSTR)) { 860 WARN("bad call to %s() for data symbol %s", 861 __func__, sym->name); 862 return NULL; 863 } 864 865 if (!sym) { 866 /* 867 * Due to how weak functions work, we must use section based 868 * relocations. Symbol based relocations would result in the 869 * weak and non-weak function annotations being overlaid on the 870 * non-weak function after linking. 871 */ 872 sym = elf_create_section_symbol(elf, insn_sec); 873 if (!sym) 874 return NULL; 875 876 insn_sec->sym = sym; 877 } 878 879 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend, 880 elf_text_rela_type(elf)); 881 } 882 883 struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec, 884 unsigned long offset, 885 unsigned int reloc_idx, 886 struct symbol *sym, 887 s64 addend) 888 { 889 if (sym->sec && (sec->sh.sh_flags & SHF_EXECINSTR)) { 890 WARN("bad call to %s() for text symbol %s", 891 __func__, sym->name); 892 return NULL; 893 } 894 895 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend, 896 elf_data_rela_type(elf)); 897 } 898 899 static int read_reloc(struct section *rsec, int i, struct reloc *reloc) 900 { 901 bool rela = rsec->sh.sh_type == SHT_RELA; 902 void *retp; 903 904 if (rela) 905 retp = gelf_getrela(rsec->data, i, &reloc->rela); 906 else 907 retp = gelf_getrel(rsec->data, i, &reloc->rel); 908 909 if (!retp) { 910 WARN_ELF("gelf_getrela"); 911 return -1; 912 } 913 914 reloc->addend = rela ? reloc->rela.r_addend : 0; 915 916 return 0; 917 } 918 919 static int read_relocs(struct elf *elf) 920 { 921 unsigned long nr_reloc, max_reloc = 0; 922 struct section *rsec; 923 struct reloc *reloc; 924 unsigned int symndx; 925 struct symbol *sym; 926 int i; 927 928 if (!elf_alloc_hash(reloc, elf->num_relocs)) 929 return -1; 930 931 list_for_each_entry(rsec, &elf->sections, list) { 932 if (!is_reloc_sec(rsec)) 933 continue; 934 935 rsec->base = find_section_by_index(elf, rsec->sh.sh_info); 936 if (!rsec->base) { 937 WARN("can't find base section for reloc section %s", 938 rsec->name); 939 return -1; 940 } 941 942 rsec->base->rsec = rsec; 943 944 nr_reloc = 0; 945 rsec->relocs = calloc(sec_num_entries(rsec), sizeof(*reloc)); 946 if (!rsec->relocs) { 947 perror("calloc"); 948 return -1; 949 } 950 for (i = 0; i < sec_num_entries(rsec); i++) { 951 reloc = &rsec->relocs[i]; 952 953 if (read_reloc(rsec, i, reloc)) 954 return -1; 955 956 reloc->sec = rsec; 957 symndx = GELF_R_SYM(reloc->rel.r_info); 958 reloc->sym = sym = find_symbol_by_index(elf, symndx); 959 if (!reloc->sym) { 960 WARN("can't find reloc entry symbol %d for %s", 961 symndx, rsec->name); 962 return -1; 963 } 964 965 list_add_tail(&reloc->sym_reloc_entry, &sym->reloc_list); 966 elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); 967 968 nr_reloc++; 969 } 970 max_reloc = max(max_reloc, nr_reloc); 971 } 972 973 if (opts.stats) { 974 printf("max_reloc: %lu\n", max_reloc); 975 printf("num_relocs: %lu\n", elf->num_relocs); 976 printf("reloc_bits: %d\n", elf->reloc_bits); 977 } 978 979 return 0; 980 } 981 982 struct elf *elf_open_read(const char *name, int flags) 983 { 984 struct elf *elf; 985 Elf_Cmd cmd; 986 987 elf_version(EV_CURRENT); 988 989 elf = malloc(sizeof(*elf)); 990 if (!elf) { 991 perror("malloc"); 992 return NULL; 993 } 994 memset(elf, 0, offsetof(struct elf, sections)); 995 996 INIT_LIST_HEAD(&elf->sections); 997 998 elf->fd = open(name, flags); 999 if (elf->fd == -1) { 1000 fprintf(stderr, "objtool: Can't open '%s': %s\n", 1001 name, strerror(errno)); 1002 goto err; 1003 } 1004 1005 if ((flags & O_ACCMODE) == O_RDONLY) 1006 cmd = ELF_C_READ_MMAP; 1007 else if ((flags & O_ACCMODE) == O_RDWR) 1008 cmd = ELF_C_RDWR; 1009 else /* O_WRONLY */ 1010 cmd = ELF_C_WRITE; 1011 1012 elf->elf = elf_begin(elf->fd, cmd, NULL); 1013 if (!elf->elf) { 1014 WARN_ELF("elf_begin"); 1015 goto err; 1016 } 1017 1018 if (!gelf_getehdr(elf->elf, &elf->ehdr)) { 1019 WARN_ELF("gelf_getehdr"); 1020 goto err; 1021 } 1022 1023 if (read_sections(elf)) 1024 goto err; 1025 1026 if (read_symbols(elf)) 1027 goto err; 1028 1029 if (read_relocs(elf)) 1030 goto err; 1031 1032 return elf; 1033 1034 err: 1035 elf_close(elf); 1036 return NULL; 1037 } 1038 1039 static int elf_add_string(struct elf *elf, struct section *strtab, char *str) 1040 { 1041 Elf_Data *data; 1042 Elf_Scn *s; 1043 int len; 1044 1045 if (!strtab) 1046 strtab = find_section_by_name(elf, ".strtab"); 1047 if (!strtab) { 1048 WARN("can't find .strtab section"); 1049 return -1; 1050 } 1051 1052 s = elf_getscn(elf->elf, strtab->idx); 1053 if (!s) { 1054 WARN_ELF("elf_getscn"); 1055 return -1; 1056 } 1057 1058 data = elf_newdata(s); 1059 if (!data) { 1060 WARN_ELF("elf_newdata"); 1061 return -1; 1062 } 1063 1064 data->d_buf = str; 1065 data->d_size = strlen(str) + 1; 1066 data->d_align = 1; 1067 1068 len = strtab->sh.sh_size; 1069 strtab->sh.sh_size += data->d_size; 1070 1071 mark_sec_changed(elf, strtab, true); 1072 1073 return len; 1074 } 1075 1076 struct section *elf_create_section(struct elf *elf, const char *name, 1077 size_t entsize, unsigned int nr) 1078 { 1079 struct section *sec, *shstrtab; 1080 size_t size = entsize * nr; 1081 Elf_Scn *s; 1082 1083 sec = malloc(sizeof(*sec)); 1084 if (!sec) { 1085 perror("malloc"); 1086 return NULL; 1087 } 1088 memset(sec, 0, sizeof(*sec)); 1089 1090 INIT_LIST_HEAD(&sec->symbol_list); 1091 1092 s = elf_newscn(elf->elf); 1093 if (!s) { 1094 WARN_ELF("elf_newscn"); 1095 return NULL; 1096 } 1097 1098 sec->name = strdup(name); 1099 if (!sec->name) { 1100 perror("strdup"); 1101 return NULL; 1102 } 1103 1104 sec->idx = elf_ndxscn(s); 1105 1106 sec->data = elf_newdata(s); 1107 if (!sec->data) { 1108 WARN_ELF("elf_newdata"); 1109 return NULL; 1110 } 1111 1112 sec->data->d_size = size; 1113 sec->data->d_align = 1; 1114 1115 if (size) { 1116 sec->data->d_buf = malloc(size); 1117 if (!sec->data->d_buf) { 1118 perror("malloc"); 1119 return NULL; 1120 } 1121 memset(sec->data->d_buf, 0, size); 1122 } 1123 1124 if (!gelf_getshdr(s, &sec->sh)) { 1125 WARN_ELF("gelf_getshdr"); 1126 return NULL; 1127 } 1128 1129 sec->sh.sh_size = size; 1130 sec->sh.sh_entsize = entsize; 1131 sec->sh.sh_type = SHT_PROGBITS; 1132 sec->sh.sh_addralign = 1; 1133 sec->sh.sh_flags = SHF_ALLOC; 1134 1135 /* Add section name to .shstrtab (or .strtab for Clang) */ 1136 shstrtab = find_section_by_name(elf, ".shstrtab"); 1137 if (!shstrtab) 1138 shstrtab = find_section_by_name(elf, ".strtab"); 1139 if (!shstrtab) { 1140 WARN("can't find .shstrtab or .strtab section"); 1141 return NULL; 1142 } 1143 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name); 1144 if (sec->sh.sh_name == -1) 1145 return NULL; 1146 1147 list_add_tail(&sec->list, &elf->sections); 1148 elf_hash_add(section, &sec->hash, sec->idx); 1149 elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name)); 1150 1151 mark_sec_changed(elf, sec, true); 1152 1153 return sec; 1154 } 1155 1156 static struct section *elf_create_rela_section(struct elf *elf, 1157 struct section *sec, 1158 unsigned int reloc_nr) 1159 { 1160 struct section *rsec; 1161 char *rsec_name; 1162 1163 rsec_name = malloc(strlen(sec->name) + strlen(".rela") + 1); 1164 if (!rsec_name) { 1165 perror("malloc"); 1166 return NULL; 1167 } 1168 strcpy(rsec_name, ".rela"); 1169 strcat(rsec_name, sec->name); 1170 1171 rsec = elf_create_section(elf, rsec_name, elf_rela_size(elf), reloc_nr); 1172 free(rsec_name); 1173 if (!rsec) 1174 return NULL; 1175 1176 rsec->data->d_type = ELF_T_RELA; 1177 rsec->sh.sh_type = SHT_RELA; 1178 rsec->sh.sh_addralign = elf_addr_size(elf); 1179 rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; 1180 rsec->sh.sh_info = sec->idx; 1181 rsec->sh.sh_flags = SHF_INFO_LINK; 1182 1183 rsec->relocs = calloc(sec_num_entries(rsec), sizeof(struct reloc)); 1184 if (!rsec->relocs) { 1185 perror("calloc"); 1186 return NULL; 1187 } 1188 1189 sec->rsec = rsec; 1190 rsec->base = sec; 1191 1192 return rsec; 1193 } 1194 1195 struct section *elf_create_section_pair(struct elf *elf, const char *name, 1196 size_t entsize, unsigned int nr, 1197 unsigned int reloc_nr) 1198 { 1199 struct section *sec; 1200 1201 sec = elf_create_section(elf, name, entsize, nr); 1202 if (!sec) 1203 return NULL; 1204 1205 if (!elf_create_rela_section(elf, sec, reloc_nr)) 1206 return NULL; 1207 1208 return sec; 1209 } 1210 1211 int elf_write_insn(struct elf *elf, struct section *sec, 1212 unsigned long offset, unsigned int len, 1213 const char *insn) 1214 { 1215 Elf_Data *data = sec->data; 1216 1217 if (data->d_type != ELF_T_BYTE || data->d_off) { 1218 WARN("write to unexpected data for section: %s", sec->name); 1219 return -1; 1220 } 1221 1222 memcpy(data->d_buf + offset, insn, len); 1223 1224 mark_sec_changed(elf, sec, true); 1225 1226 return 0; 1227 } 1228 1229 int elf_write_reloc(struct elf *elf, struct reloc *reloc) 1230 { 1231 struct section *rsec = reloc->sec; 1232 int ret; 1233 1234 if (rsec->sh.sh_type == SHT_RELA) { 1235 reloc->rela.r_addend = reloc->addend; 1236 ret = gelf_update_rela(rsec->data, reloc_idx(reloc), &reloc->rela); 1237 } else { 1238 ret = gelf_update_rel(rsec->data, reloc_idx(reloc), &reloc->rel); 1239 } 1240 1241 if (!ret) { 1242 WARN_ELF("gelf_update_rela"); 1243 return -1; 1244 } 1245 1246 mark_sec_changed(elf, rsec, true); 1247 1248 return 0; 1249 } 1250 1251 /* 1252 * When Elf_Scn::sh_size is smaller than the combined Elf_Data::d_size 1253 * do you: 1254 * 1255 * A) adhere to the section header and truncate the data, or 1256 * B) ignore the section header and write out all the data you've got? 1257 * 1258 * Yes, libelf sucks and we need to manually truncate if we over-allocate data. 1259 */ 1260 static int elf_truncate_section(struct elf *elf, struct section *sec) 1261 { 1262 u64 size = sec->sh.sh_size; 1263 bool truncated = false; 1264 Elf_Data *data = NULL; 1265 Elf_Scn *s; 1266 1267 s = elf_getscn(elf->elf, sec->idx); 1268 if (!s) { 1269 WARN_ELF("elf_getscn"); 1270 return -1; 1271 } 1272 1273 for (;;) { 1274 /* get next data descriptor for the relevant section */ 1275 data = elf_getdata(s, data); 1276 1277 if (!data) { 1278 if (size) { 1279 WARN("end of section data but non-zero size left\n"); 1280 return -1; 1281 } 1282 return 0; 1283 } 1284 1285 if (truncated) { 1286 /* when we remove symbols */ 1287 WARN("truncated; but more data\n"); 1288 return -1; 1289 } 1290 1291 if (!data->d_size) { 1292 WARN("zero size data"); 1293 return -1; 1294 } 1295 1296 if (data->d_size > size) { 1297 truncated = true; 1298 data->d_size = size; 1299 } 1300 1301 size -= data->d_size; 1302 } 1303 } 1304 1305 int elf_write(struct elf *elf) 1306 { 1307 struct section *sec; 1308 Elf_Scn *s; 1309 1310 if (opts.dryrun) 1311 return 0; 1312 1313 /* Update changed relocation sections and section headers: */ 1314 list_for_each_entry(sec, &elf->sections, list) { 1315 if (sec->truncate) 1316 elf_truncate_section(elf, sec); 1317 1318 if (sec_changed(sec)) { 1319 s = elf_getscn(elf->elf, sec->idx); 1320 if (!s) { 1321 WARN_ELF("elf_getscn"); 1322 return -1; 1323 } 1324 1325 /* Note this also flags the section dirty */ 1326 if (!gelf_update_shdr(s, &sec->sh)) { 1327 WARN_ELF("gelf_update_shdr"); 1328 return -1; 1329 } 1330 1331 mark_sec_changed(elf, sec, false); 1332 } 1333 } 1334 1335 /* Make sure the new section header entries get updated properly. */ 1336 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY); 1337 1338 /* Write all changes to the file. */ 1339 if (elf_update(elf->elf, ELF_C_WRITE) < 0) { 1340 WARN_ELF("elf_update"); 1341 return -1; 1342 } 1343 1344 elf->changed = false; 1345 1346 return 0; 1347 } 1348 1349 void elf_close(struct elf *elf) 1350 { 1351 if (elf->elf) 1352 elf_end(elf->elf); 1353 1354 if (elf->fd > 0) 1355 close(elf->fd); 1356 1357 /* 1358 * NOTE: All remaining allocations are leaked on purpose. Objtool is 1359 * about to exit anyway. 1360 */ 1361 } 1362