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 <jpoimboe@redhat.com> 7 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com> 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 <ctype.h> 20 #include <linux/align.h> 21 #include <linux/kernel.h> 22 #include <linux/interval_tree_generic.h> 23 #include <linux/log2.h> 24 #include <objtool/builtin.h> 25 #include <objtool/elf.h> 26 #include <objtool/warn.h> 27 28 static ssize_t demangled_name_len(const char *name); 29 30 u32 str_hash_demangled(const char *str) 31 { 32 return jhash(str, demangled_name_len(str), 0); 33 } 34 35 #define elf_hash_add(name, node, key) \ 36 ({ \ 37 struct elf_hash_node *__node = node; \ 38 __node->next = __elf_table_entry(elf, name, key); \ 39 __elf_table_entry(elf, name, key) = __node; \ 40 }) 41 42 static inline void __elf_hash_del(struct elf_hash_node *node, 43 struct elf_hash_node **head) 44 { 45 struct elf_hash_node *cur, *prev; 46 47 if (node == *head) { 48 *head = node->next; 49 return; 50 } 51 52 for (prev = NULL, cur = *head; cur; prev = cur, cur = cur->next) { 53 if (cur == node) { 54 prev->next = cur->next; 55 break; 56 } 57 } 58 } 59 60 #define elf_hash_del(name, node, key) \ 61 __elf_hash_del(node, &__elf_table_entry(elf, name, key)) 62 63 #define elf_alloc_hash(name, size) \ 64 ({ \ 65 __elf_bits(elf, name) = max(10, ilog2(size)); \ 66 __elf_table(elf, name) = mmap(NULL, \ 67 sizeof(struct elf_hash_node *) << __elf_bits(elf, name), \ 68 PROT_READ|PROT_WRITE, \ 69 MAP_PRIVATE|MAP_ANON, -1, 0); \ 70 if (__elf_table(elf, name) == (void *)-1L) { \ 71 ERROR_GLIBC("mmap fail " #name); \ 72 __elf_table(elf, name) = NULL; \ 73 } \ 74 __elf_table(elf, name); \ 75 }) 76 77 static inline unsigned long __sym_start(struct symbol *s) 78 { 79 return s->offset; 80 } 81 82 static inline unsigned long __sym_last(struct symbol *s) 83 { 84 return s->offset + (s->len ? s->len - 1 : 0); 85 } 86 87 INTERVAL_TREE_DEFINE(struct symbol, node, unsigned long, __subtree_last, 88 __sym_start, __sym_last, static inline __maybe_unused, 89 __sym) 90 91 #define __sym_for_each(_iter, _tree, _start, _end) \ 92 for (_iter = __sym_iter_first((_tree), (_start), (_end)); \ 93 _iter; _iter = __sym_iter_next(_iter, (_start), (_end))) 94 95 struct symbol_hole { 96 unsigned long key; 97 const struct symbol *sym; 98 }; 99 100 /* 101 * Find the last symbol before @offset. 102 */ 103 static int symbol_hole_by_offset(const void *key, const struct rb_node *node) 104 { 105 const struct symbol *s = rb_entry(node, struct symbol, node); 106 struct symbol_hole *sh = (void *)key; 107 108 if (sh->key < s->offset) 109 return -1; 110 111 if (sh->key >= s->offset + s->len) { 112 sh->sym = s; 113 return 1; 114 } 115 116 return 0; 117 } 118 119 struct section *find_section_by_name(const struct elf *elf, const char *name) 120 { 121 struct section *sec; 122 123 elf_hash_for_each_possible(elf, section_name, sec, name_hash, str_hash(name)) { 124 if (!strcmp(sec->name, name)) 125 return sec; 126 } 127 128 return NULL; 129 } 130 131 static struct section *find_section_by_index(struct elf *elf, 132 unsigned int idx) 133 { 134 struct section *sec; 135 136 elf_hash_for_each_possible(elf, section, sec, hash, idx) { 137 if (sec->idx == idx) 138 return sec; 139 } 140 141 return NULL; 142 } 143 144 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx) 145 { 146 struct symbol *sym; 147 148 elf_hash_for_each_possible(elf, symbol, sym, hash, idx) { 149 if (sym->idx == idx) 150 return sym; 151 } 152 153 return NULL; 154 } 155 156 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset) 157 { 158 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree; 159 struct symbol *sym; 160 161 __sym_for_each(sym, tree, offset, offset) { 162 if (sym->offset == offset && !is_sec_sym(sym)) 163 return sym->alias; 164 } 165 166 return NULL; 167 } 168 169 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset) 170 { 171 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree; 172 struct symbol *func; 173 174 __sym_for_each(func, tree, offset, offset) { 175 if (func->offset == offset && is_func_sym(func)) 176 return func->alias; 177 } 178 179 return NULL; 180 } 181 182 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset) 183 { 184 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree; 185 struct symbol *sym = NULL, *tmp; 186 187 __sym_for_each(tmp, tree, offset, offset) { 188 if (tmp->len) { 189 if (!sym) { 190 sym = tmp; 191 continue; 192 } 193 194 if (sym->offset != tmp->offset || sym->len != tmp->len) { 195 /* 196 * In the rare case of overlapping symbols, 197 * pick the smaller one. 198 * 199 * TODO: outlaw overlapping symbols 200 */ 201 if (tmp->len < sym->len) 202 sym = tmp; 203 } 204 } 205 } 206 207 return sym ? sym->alias : NULL; 208 } 209 210 /* 211 * Also match the symbol end address which can be used for a bounds comparison. 212 */ 213 struct symbol *find_symbol_containing_inclusive(const struct section *sec, 214 unsigned long offset) 215 { 216 struct symbol *sym = find_symbol_containing(sec, offset); 217 218 if (!sym && offset) 219 sym = find_symbol_containing(sec, offset - 1); 220 221 return sym; 222 } 223 224 /* 225 * Returns size of hole starting at @offset. 226 */ 227 int find_symbol_hole_containing(const struct section *sec, unsigned long offset) 228 { 229 struct symbol_hole hole = { 230 .key = offset, 231 .sym = NULL, 232 }; 233 struct rb_node *n; 234 struct symbol *s; 235 236 /* 237 * Find the rightmost symbol for which @offset is after it. 238 */ 239 n = rb_find(&hole, &sec->symbol_tree.rb_root, symbol_hole_by_offset); 240 241 /* found a symbol that contains @offset */ 242 if (n) 243 return 0; /* not a hole */ 244 245 /* 246 * @offset >= sym->offset + sym->len, find symbol after it. 247 * When hole.sym is empty, use the first node to compute the hole. 248 * If there is no symbol in the section, the first node will be NULL, 249 * in which case, -1 is returned to skip the whole section. 250 */ 251 if (hole.sym) 252 n = rb_next(&hole.sym->node); 253 else 254 n = rb_first_cached(&sec->symbol_tree); 255 256 if (!n) 257 return -1; /* until end of address space */ 258 259 /* hole until start of next symbol */ 260 s = rb_entry(n, struct symbol, node); 261 return s->offset - offset; 262 } 263 264 struct symbol *find_func_containing(struct section *sec, unsigned long offset) 265 { 266 struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree; 267 struct symbol *func; 268 269 __sym_for_each(func, tree, offset, offset) { 270 if (is_func_sym(func)) 271 return func->alias; 272 } 273 274 return NULL; 275 } 276 277 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name) 278 { 279 struct symbol *sym; 280 281 elf_hash_for_each_possible(elf, symbol_name, sym, name_hash, str_hash(name)) { 282 if (!strcmp(sym->name, name)) 283 return sym; 284 } 285 286 return NULL; 287 } 288 289 /* Find local symbol with matching STT_FILE */ 290 static struct symbol *find_local_symbol_by_file_and_name(const struct elf *elf, 291 struct symbol *file, 292 const char *name) 293 { 294 struct symbol *sym; 295 296 elf_hash_for_each_possible(elf, symbol_name, sym, name_hash, str_hash_demangled(name)) { 297 if (sym->bind == STB_LOCAL && sym->file == file && 298 !strcmp(sym->name, name)) { 299 return sym; 300 } 301 } 302 303 return NULL; 304 } 305 306 struct symbol *find_global_symbol_by_name(const struct elf *elf, const char *name) 307 { 308 struct symbol *sym; 309 310 elf_hash_for_each_possible(elf, symbol_name, sym, name_hash, str_hash_demangled(name)) { 311 if (!strcmp(sym->name, name) && !is_local_sym(sym)) 312 return sym; 313 } 314 315 return NULL; 316 } 317 318 /* If there are multiple matches, return the first one in the range */ 319 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec, 320 unsigned long offset, unsigned int len) 321 { 322 struct reloc *reloc, *r = NULL; 323 struct section *rsec; 324 unsigned long o; 325 326 rsec = sec->rsec; 327 if (!rsec) 328 return NULL; 329 330 for_offset_range(o, offset, offset + len) { 331 elf_hash_for_each_possible(elf, reloc, reloc, hash, 332 sec_offset_hash(rsec, o)) { 333 if (reloc->sec != rsec) 334 continue; 335 336 if (reloc_offset(reloc) >= offset && 337 reloc_offset(reloc) < offset + len) { 338 if (!r || reloc_offset(reloc) < reloc_offset(r)) 339 r = reloc; 340 } 341 } 342 if (r && (reloc_offset(r) & OFFSET_STRIDE_MASK) == o) 343 return r; 344 } 345 346 return r; 347 } 348 349 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset) 350 { 351 return find_reloc_by_dest_range(elf, sec, offset, 1); 352 } 353 354 static bool is_dwarf_section(struct section *sec) 355 { 356 return !strncmp(sec->name, ".debug_", 7); 357 } 358 359 static int read_sections(struct elf *elf) 360 { 361 Elf_Scn *s = NULL; 362 struct section *sec; 363 size_t shstrndx, sections_nr; 364 int i; 365 366 if (elf_getshdrnum(elf->elf, §ions_nr)) { 367 ERROR_ELF("elf_getshdrnum"); 368 return -1; 369 } 370 371 if (elf_getshdrstrndx(elf->elf, &shstrndx)) { 372 ERROR_ELF("elf_getshdrstrndx"); 373 return -1; 374 } 375 376 if (!elf_alloc_hash(section, sections_nr) || 377 !elf_alloc_hash(section_name, sections_nr)) 378 return -1; 379 380 elf->section_data = calloc(sections_nr, sizeof(*sec)); 381 if (!elf->section_data) { 382 ERROR_GLIBC("calloc"); 383 return -1; 384 } 385 for (i = 0; i < sections_nr; i++) { 386 sec = &elf->section_data[i]; 387 388 INIT_LIST_HEAD(&sec->symbol_list); 389 390 s = elf_getscn(elf->elf, i); 391 if (!s) { 392 ERROR_ELF("elf_getscn"); 393 return -1; 394 } 395 396 sec->idx = elf_ndxscn(s); 397 398 if (!gelf_getshdr(s, &sec->sh)) { 399 ERROR_ELF("gelf_getshdr"); 400 return -1; 401 } 402 403 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name); 404 if (!sec->name) { 405 ERROR_ELF("elf_strptr"); 406 return -1; 407 } 408 409 if (sec_size(sec) != 0 && !is_dwarf_section(sec)) { 410 sec->data = elf_getdata(s, NULL); 411 if (!sec->data) { 412 ERROR_ELF("elf_getdata"); 413 return -1; 414 } 415 if (sec->data->d_off != 0 || 416 sec->data->d_size != sec_size(sec)) { 417 ERROR("unexpected data attributes for %s", sec->name); 418 return -1; 419 } 420 } 421 422 list_add_tail(&sec->list, &elf->sections); 423 elf_hash_add(section, &sec->hash, sec->idx); 424 elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name)); 425 426 if (is_reloc_sec(sec)) 427 elf->num_relocs += sec_num_entries(sec); 428 } 429 430 if (opts.stats) { 431 printf("nr_sections: %lu\n", (unsigned long)sections_nr); 432 printf("section_bits: %d\n", elf->section_bits); 433 } 434 435 /* sanity check, one more call to elf_nextscn() should return NULL */ 436 if (elf_nextscn(elf->elf, s)) { 437 ERROR("section entry mismatch"); 438 return -1; 439 } 440 441 return 0; 442 } 443 444 /* 445 * Returns desired length of the demangled name. 446 * If name doesn't need demangling, return strlen(name). 447 */ 448 static ssize_t demangled_name_len(const char *name) 449 { 450 ssize_t idx; 451 const char *p; 452 453 p = strstr(name, ".llvm."); 454 if (p) 455 return p - name; 456 457 if (!strstarts(name, "__UNIQUE_ID_") && !strchr(name, '.')) 458 return strlen(name); 459 460 for (idx = strlen(name) - 1; idx >= 0; idx--) { 461 char c = name[idx]; 462 463 if (!isdigit(c) && c != '.' && c != '_') 464 break; 465 } 466 if (idx <= 0) 467 return strlen(name); 468 return idx + 1; 469 } 470 471 /* 472 * Remove number suffix of a symbol. 473 * 474 * Specifically, remove trailing numbers for "__UNIQUE_ID_" symbols and 475 * symbols with '.'. 476 * 477 * With CONFIG_LTO_CLANG_THIN, it is possible to have nested __UNIQUE_ID_, 478 * such as 479 * 480 * __UNIQUE_ID_addressable___UNIQUE_ID_pci_invalid_bar_694_695 481 * 482 * to remove both trailing numbers, also remove trailing '_'. 483 * 484 * For symbols with llvm suffix, i.e., foo.llvm.<hash>, remove the 485 * .llvm.<hash> part. 486 */ 487 static const char *demangle_name(struct symbol *sym) 488 { 489 char *str; 490 ssize_t len; 491 492 if (!is_func_sym(sym) && !is_object_sym(sym)) 493 return sym->name; 494 495 len = demangled_name_len(sym->name); 496 if (len == strlen(sym->name)) 497 return sym->name; 498 499 str = strndup(sym->name, len); 500 if (!str) { 501 ERROR_GLIBC("strdup"); 502 return NULL; 503 } 504 505 return str; 506 } 507 508 static int elf_add_symbol(struct elf *elf, struct symbol *sym) 509 { 510 struct list_head *entry; 511 struct rb_node *pnode; 512 struct symbol *iter; 513 514 INIT_LIST_HEAD(&sym->pv_target); 515 sym->alias = sym; 516 517 sym->type = GELF_ST_TYPE(sym->sym.st_info); 518 sym->bind = GELF_ST_BIND(sym->sym.st_info); 519 520 if (is_file_sym(sym)) 521 elf->num_files++; 522 523 sym->offset = sym->sym.st_value; 524 sym->len = sym->sym.st_size; 525 526 __sym_for_each(iter, &sym->sec->symbol_tree, sym->offset, sym->offset) { 527 if (!is_undef_sym(iter) && iter->offset == sym->offset && 528 iter->type == sym->type && iter->len == sym->len) 529 iter->alias = sym; 530 } 531 532 __sym_insert(sym, &sym->sec->symbol_tree); 533 pnode = rb_prev(&sym->node); 534 if (pnode) 535 entry = &rb_entry(pnode, struct symbol, node)->list; 536 else 537 entry = &sym->sec->symbol_list; 538 list_add(&sym->list, entry); 539 540 sym->demangled_name = demangle_name(sym); 541 if (!sym->demangled_name) 542 return -1; 543 544 list_add_tail(&sym->global_list, &elf->symbols); 545 elf_hash_add(symbol, &sym->hash, sym->idx); 546 elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->demangled_name)); 547 548 if (is_func_sym(sym) && 549 (strstarts(sym->name, "__pfx_") || 550 strstarts(sym->name, "__cfi_") || 551 strstarts(sym->name, "__pi___pfx_") || 552 strstarts(sym->name, "__pi___cfi_"))) 553 sym->prefix = 1; 554 555 if (strstarts(sym->name, ".klp.sym")) 556 sym->klp = 1; 557 558 if (!sym->klp && !is_sec_sym(sym) && strstr(sym->name, ".cold")) { 559 sym->cold = 1; 560 561 /* 562 * Clang doesn't mark cold subfunctions as STT_FUNC, which 563 * breaks several objtool assumptions. Fake it. 564 */ 565 sym->type = STT_FUNC; 566 } 567 568 sym->pfunc = sym->cfunc = sym; 569 570 return 0; 571 } 572 573 static int read_symbols(struct elf *elf) 574 { 575 struct section *symtab, *symtab_shndx, *sec; 576 struct symbol *sym, *pfunc, *file = NULL; 577 int symbols_nr, i; 578 char *coldstr; 579 Elf_Data *shndx_data = NULL; 580 Elf32_Word shndx; 581 582 symtab = find_section_by_name(elf, ".symtab"); 583 if (symtab) { 584 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); 585 if (symtab_shndx) 586 shndx_data = symtab_shndx->data; 587 588 symbols_nr = sec_num_entries(symtab); 589 } else { 590 /* 591 * A missing symbol table is actually possible if it's an empty 592 * .o file. This can happen for thunk_64.o. Make sure to at 593 * least allocate the symbol hash tables so we can do symbol 594 * lookups without crashing. 595 */ 596 symbols_nr = 0; 597 } 598 599 if (!elf_alloc_hash(symbol, symbols_nr) || 600 !elf_alloc_hash(symbol_name, symbols_nr)) 601 return -1; 602 603 elf->symbol_data = calloc(symbols_nr, sizeof(*sym)); 604 if (!elf->symbol_data) { 605 ERROR_GLIBC("calloc"); 606 return -1; 607 } 608 609 INIT_LIST_HEAD(&elf->symbols); 610 611 for (i = 0; i < symbols_nr; i++) { 612 sym = &elf->symbol_data[i]; 613 614 sym->idx = i; 615 616 if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym, 617 &shndx)) { 618 ERROR_ELF("gelf_getsymshndx"); 619 return -1; 620 } 621 622 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link, 623 sym->sym.st_name); 624 if (!sym->name) { 625 ERROR_ELF("elf_strptr"); 626 return -1; 627 } 628 629 if ((sym->sym.st_shndx > SHN_UNDEF && 630 sym->sym.st_shndx < SHN_LORESERVE) || 631 (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) { 632 if (sym->sym.st_shndx != SHN_XINDEX) 633 shndx = sym->sym.st_shndx; 634 635 sym->sec = find_section_by_index(elf, shndx); 636 if (!sym->sec) { 637 ERROR("couldn't find section for symbol %s", sym->name); 638 return -1; 639 } 640 if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) { 641 sym->name = sym->sec->name; 642 sym->sec->sym = sym; 643 } 644 } else 645 sym->sec = find_section_by_index(elf, 0); 646 647 if (elf_add_symbol(elf, sym)) 648 return -1; 649 650 if (is_file_sym(sym)) 651 file = sym; 652 else if (sym->bind == STB_LOCAL && !is_sec_sym(sym)) 653 sym->file = file; 654 } 655 656 if (opts.stats) { 657 printf("nr_symbols: %lu\n", (unsigned long)symbols_nr); 658 printf("symbol_bits: %d\n", elf->symbol_bits); 659 } 660 661 /* Create parent/child links for any cold subfunctions */ 662 list_for_each_entry(sec, &elf->sections, list) { 663 sec_for_each_sym(sec, sym) { 664 char *pname; 665 size_t pnamelen; 666 667 if (!sym->cold) 668 continue; 669 670 coldstr = strstr(sym->name, ".cold"); 671 if (!coldstr) { 672 ERROR("%s(): cold subfunction without \".cold\"?", sym->name); 673 return -1; 674 } 675 676 pnamelen = coldstr - sym->name; 677 pname = strndup(sym->name, pnamelen); 678 if (!pname) { 679 ERROR("%s(): failed to allocate memory", sym->name); 680 return -1; 681 } 682 683 pfunc = find_local_symbol_by_file_and_name(elf, sym->file, pname); 684 if (!pfunc) 685 pfunc = find_global_symbol_by_name(elf, pname); 686 free(pname); 687 688 if (!pfunc) { 689 ERROR("%s(): can't find parent function", sym->name); 690 return -1; 691 } 692 693 sym->pfunc = pfunc->alias; 694 pfunc->cfunc = sym; 695 pfunc->alias->cfunc = sym; 696 697 /* 698 * Unfortunately, -fnoreorder-functions puts the child 699 * inside the parent. Remove the overlap so we can 700 * have sane assumptions. 701 * 702 * Note that pfunc->len now no longer matches 703 * pfunc->sym.st_size. 704 */ 705 if (sym->sec == pfunc->sec && 706 sym->offset >= pfunc->offset && 707 sym->offset + sym->len == pfunc->offset + pfunc->len) { 708 pfunc->len -= sym->len; 709 } 710 } 711 } 712 713 return 0; 714 } 715 716 static int mark_group_syms(struct elf *elf) 717 { 718 struct section *symtab, *sec; 719 struct symbol *sym; 720 721 symtab = find_section_by_name(elf, ".symtab"); 722 if (!symtab) { 723 ERROR("no .symtab"); 724 return -1; 725 } 726 727 for_each_sec(elf, sec) { 728 if (sec->sh.sh_type == SHT_GROUP && 729 sec->sh.sh_link == symtab->idx) { 730 sym = find_symbol_by_index(elf, sec->sh.sh_info); 731 if (!sym) { 732 ERROR("%s: can't find SHT_GROUP signature symbol", 733 sec->name); 734 return -1; 735 } 736 737 sym->group_sec = sec; 738 } 739 } 740 741 return 0; 742 } 743 744 /* 745 * @sym's idx has changed. Update the relocs which reference it. 746 */ 747 static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym) 748 { 749 struct reloc *reloc; 750 751 for (reloc = sym->relocs; reloc; reloc = sym_next_reloc(reloc)) 752 set_reloc_sym(elf, reloc, reloc->sym->idx); 753 754 return 0; 755 } 756 757 /* 758 * The libelf API is terrible; gelf_update_sym*() takes a data block relative 759 * index value, *NOT* the symbol index. As such, iterate the data blocks and 760 * adjust index until it fits. 761 * 762 * If no data block is found, allow adding a new data block provided the index 763 * is only one past the end. 764 */ 765 static int elf_update_symbol(struct elf *elf, struct section *symtab, 766 struct section *symtab_shndx, struct symbol *sym) 767 { 768 Elf32_Word shndx; 769 Elf_Data *symtab_data = NULL, *shndx_data = NULL; 770 Elf64_Xword entsize = symtab->sh.sh_entsize; 771 int max_idx, idx = sym->idx; 772 Elf_Scn *s, *t = NULL; 773 bool is_special_shndx = sym->sym.st_shndx >= SHN_LORESERVE && 774 sym->sym.st_shndx != SHN_XINDEX; 775 776 shndx = is_special_shndx ? sym->sym.st_shndx : sym->sec->idx; 777 778 s = elf_getscn(elf->elf, symtab->idx); 779 if (!s) { 780 ERROR_ELF("elf_getscn"); 781 return -1; 782 } 783 784 if (symtab_shndx) { 785 t = elf_getscn(elf->elf, symtab_shndx->idx); 786 if (!t) { 787 ERROR_ELF("elf_getscn"); 788 return -1; 789 } 790 } 791 792 for (;;) { 793 /* get next data descriptor for the relevant sections */ 794 symtab_data = elf_getdata(s, symtab_data); 795 if (t) 796 shndx_data = elf_getdata(t, shndx_data); 797 798 /* end-of-list */ 799 if (!symtab_data) { 800 /* 801 * Over-allocate to avoid O(n^2) symbol creation 802 * behaviour. The down side is that libelf doesn't 803 * like this; see elf_truncate_section() for the fixup. 804 */ 805 int num = max(1U, sym->idx/3); 806 void *buf; 807 808 if (idx) { 809 /* we don't do holes in symbol tables */ 810 ERROR("index out of range"); 811 return -1; 812 } 813 814 /* if @idx == 0, it's the next contiguous entry, create it */ 815 symtab_data = elf_newdata(s); 816 if (t) 817 shndx_data = elf_newdata(t); 818 819 buf = calloc(num, entsize); 820 if (!buf) { 821 ERROR_GLIBC("calloc"); 822 return -1; 823 } 824 825 symtab_data->d_buf = buf; 826 symtab_data->d_size = num * entsize; 827 symtab_data->d_align = 1; 828 symtab_data->d_type = ELF_T_SYM; 829 830 mark_sec_changed(elf, symtab, true); 831 symtab->truncate = true; 832 833 if (t) { 834 buf = calloc(num, sizeof(Elf32_Word)); 835 if (!buf) { 836 ERROR_GLIBC("calloc"); 837 return -1; 838 } 839 840 shndx_data->d_buf = buf; 841 shndx_data->d_size = num * sizeof(Elf32_Word); 842 shndx_data->d_align = sizeof(Elf32_Word); 843 shndx_data->d_type = ELF_T_WORD; 844 845 mark_sec_changed(elf, symtab_shndx, true); 846 symtab_shndx->truncate = true; 847 } 848 849 break; 850 } 851 852 /* empty blocks should not happen */ 853 if (!symtab_data->d_size) { 854 ERROR("zero size data"); 855 return -1; 856 } 857 858 /* is this the right block? */ 859 max_idx = symtab_data->d_size / entsize; 860 if (idx < max_idx) 861 break; 862 863 /* adjust index and try again */ 864 idx -= max_idx; 865 } 866 867 /* something went side-ways */ 868 if (idx < 0) { 869 ERROR("negative index"); 870 return -1; 871 } 872 873 /* setup extended section index magic and write the symbol */ 874 if (shndx < SHN_LORESERVE || is_special_shndx) { 875 sym->sym.st_shndx = shndx; 876 if (!shndx_data) 877 shndx = 0; 878 } else { 879 sym->sym.st_shndx = SHN_XINDEX; 880 if (!shndx_data) { 881 ERROR("no .symtab_shndx"); 882 return -1; 883 } 884 } 885 886 if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) { 887 ERROR_ELF("gelf_update_symshndx"); 888 return -1; 889 } 890 891 return 0; 892 } 893 894 struct symbol *elf_create_symbol(struct elf *elf, const char *name, 895 struct section *sec, unsigned int bind, 896 unsigned int type, unsigned long offset, 897 size_t size) 898 { 899 struct section *symtab, *symtab_shndx; 900 Elf32_Word first_non_local, new_idx; 901 struct symbol *old, *sym; 902 903 sym = calloc(1, sizeof(*sym)); 904 if (!sym) { 905 ERROR_GLIBC("calloc"); 906 return NULL; 907 } 908 909 sym->name = strdup(name); 910 if (!sym->name) { 911 ERROR_GLIBC("strdup"); 912 return NULL; 913 } 914 915 if (type != STT_SECTION) { 916 sym->sym.st_name = elf_add_string(elf, NULL, sym->name); 917 if (sym->sym.st_name == -1) 918 return NULL; 919 } 920 921 if (sec) { 922 sym->sec = sec; 923 } else { 924 sym->sec = find_section_by_index(elf, 0); 925 if (!sym->sec) { 926 ERROR("no NULL section"); 927 return NULL; 928 } 929 } 930 931 sym->sym.st_info = GELF_ST_INFO(bind, type); 932 sym->sym.st_value = offset; 933 sym->sym.st_size = size; 934 935 symtab = find_section_by_name(elf, ".symtab"); 936 if (!symtab) { 937 ERROR("no .symtab"); 938 return NULL; 939 } 940 941 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); 942 943 new_idx = sec_num_entries(symtab); 944 945 if (bind != STB_LOCAL) 946 goto non_local; 947 948 /* 949 * Move the first global symbol, as per sh_info, into a new, higher 950 * symbol index. This frees up a spot for a new local symbol. 951 */ 952 first_non_local = symtab->sh.sh_info; 953 old = find_symbol_by_index(elf, first_non_local); 954 if (old) { 955 956 elf_hash_del(symbol, &old->hash, old->idx); 957 elf_hash_add(symbol, &old->hash, new_idx); 958 old->idx = new_idx; 959 960 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) { 961 ERROR("elf_update_symbol move"); 962 return NULL; 963 } 964 965 if (elf_update_sym_relocs(elf, old)) 966 return NULL; 967 968 if (old->group_sec) { 969 old->group_sec->sh.sh_info = new_idx; 970 mark_sec_changed(elf, old->group_sec, true); 971 } 972 973 new_idx = first_non_local; 974 } 975 976 /* 977 * Either way, we will add a LOCAL symbol. 978 */ 979 symtab->sh.sh_info += 1; 980 981 non_local: 982 sym->idx = new_idx; 983 if (sym->idx && elf_update_symbol(elf, symtab, symtab_shndx, sym)) 984 return NULL; 985 986 symtab->sh.sh_size += symtab->sh.sh_entsize; 987 mark_sec_changed(elf, symtab, true); 988 989 if (symtab_shndx) { 990 symtab_shndx->sh.sh_size += sizeof(Elf32_Word); 991 mark_sec_changed(elf, symtab_shndx, true); 992 } 993 994 if (elf_add_symbol(elf, sym)) 995 return NULL; 996 997 return sym; 998 } 999 1000 struct symbol *elf_create_section_symbol(struct elf *elf, struct section *sec) 1001 { 1002 struct symbol *sym = calloc(1, sizeof(*sym)); 1003 1004 sym = elf_create_symbol(elf, sec->name, sec, STB_LOCAL, STT_SECTION, 0, 0); 1005 if (!sym) 1006 return NULL; 1007 1008 sec->sym = sym; 1009 1010 return sym; 1011 } 1012 1013 struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec, 1014 unsigned int reloc_idx, unsigned long offset, 1015 struct symbol *sym, s64 addend, unsigned int type) 1016 { 1017 struct reloc *reloc, empty = { 0 }; 1018 1019 if (reloc_idx >= sec_num_entries(rsec)) { 1020 ERROR("%s: bad reloc_idx %u for %s with %d relocs", 1021 __func__, reloc_idx, rsec->name, sec_num_entries(rsec)); 1022 return NULL; 1023 } 1024 1025 reloc = &rsec->relocs[reloc_idx]; 1026 1027 if (memcmp(reloc, &empty, sizeof(empty))) { 1028 ERROR("%s: %s: reloc %d already initialized!", 1029 __func__, rsec->name, reloc_idx); 1030 return NULL; 1031 } 1032 1033 reloc->sec = rsec; 1034 reloc->sym = sym; 1035 1036 set_reloc_offset(elf, reloc, offset); 1037 set_reloc_sym(elf, reloc, sym->idx); 1038 set_reloc_type(elf, reloc, type); 1039 set_reloc_addend(elf, reloc, addend); 1040 1041 elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); 1042 set_sym_next_reloc(reloc, sym->relocs); 1043 sym->relocs = reloc; 1044 1045 return reloc; 1046 } 1047 1048 struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec, 1049 unsigned long offset, 1050 unsigned int reloc_idx, 1051 struct section *insn_sec, 1052 unsigned long insn_off) 1053 { 1054 struct symbol *sym = insn_sec->sym; 1055 s64 addend = insn_off; 1056 1057 if (!is_text_sec(insn_sec)) { 1058 ERROR("bad call to %s() for data symbol %s", __func__, sym->name); 1059 return NULL; 1060 } 1061 1062 if (!sym) { 1063 /* 1064 * Due to how weak functions work, we must use section based 1065 * relocations. Symbol based relocations would result in the 1066 * weak and non-weak function annotations being overlaid on the 1067 * non-weak function after linking. 1068 */ 1069 sym = elf_create_section_symbol(elf, insn_sec); 1070 if (!sym) 1071 return NULL; 1072 } 1073 1074 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend, 1075 elf_text_rela_type(elf)); 1076 } 1077 1078 struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec, 1079 unsigned long offset, 1080 unsigned int reloc_idx, 1081 struct symbol *sym, 1082 s64 addend) 1083 { 1084 if (is_text_sec(sec)) { 1085 ERROR("bad call to %s() for text symbol %s", __func__, sym->name); 1086 return NULL; 1087 } 1088 1089 return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend, 1090 elf_data_rela_type(elf)); 1091 } 1092 1093 static int read_relocs(struct elf *elf) 1094 { 1095 unsigned long nr_reloc, max_reloc = 0; 1096 struct section *rsec; 1097 struct reloc *reloc; 1098 unsigned int symndx; 1099 struct symbol *sym; 1100 int i; 1101 1102 if (!elf_alloc_hash(reloc, elf->num_relocs)) 1103 return -1; 1104 1105 list_for_each_entry(rsec, &elf->sections, list) { 1106 if (!is_reloc_sec(rsec)) 1107 continue; 1108 1109 rsec->base = find_section_by_index(elf, rsec->sh.sh_info); 1110 if (!rsec->base) { 1111 ERROR("can't find base section for reloc section %s", rsec->name); 1112 return -1; 1113 } 1114 1115 rsec->base->rsec = rsec; 1116 1117 /* nr_alloc_relocs=0: libelf owns d_buf */ 1118 rsec->nr_alloc_relocs = 0; 1119 1120 rsec->relocs = calloc(sec_num_entries(rsec), sizeof(*reloc)); 1121 if (!rsec->relocs) { 1122 ERROR_GLIBC("calloc"); 1123 return -1; 1124 } 1125 1126 nr_reloc = 0; 1127 for (i = 0; i < sec_num_entries(rsec); i++) { 1128 reloc = &rsec->relocs[i]; 1129 1130 reloc->sec = rsec; 1131 symndx = reloc_sym(reloc); 1132 reloc->sym = sym = find_symbol_by_index(elf, symndx); 1133 if (!reloc->sym) { 1134 ERROR("can't find reloc entry symbol %d for %s", symndx, rsec->name); 1135 return -1; 1136 } 1137 1138 elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc)); 1139 set_sym_next_reloc(reloc, sym->relocs); 1140 sym->relocs = reloc; 1141 1142 nr_reloc++; 1143 } 1144 max_reloc = max(max_reloc, nr_reloc); 1145 } 1146 1147 if (opts.stats) { 1148 printf("max_reloc: %lu\n", max_reloc); 1149 printf("num_relocs: %lu\n", elf->num_relocs); 1150 printf("reloc_bits: %d\n", elf->reloc_bits); 1151 } 1152 1153 return 0; 1154 } 1155 1156 static void mark_rodata(struct elf *elf) 1157 { 1158 struct section *sec; 1159 1160 for_each_sec(elf, sec) { 1161 if ((strstarts(sec->name, ".rodata") && !strstr(sec->name, ".str1.")) || 1162 strstarts(sec->name, ".data.rel.ro")) 1163 sec->rodata = true; 1164 } 1165 } 1166 1167 struct elf *elf_open_read(const char *name, int flags) 1168 { 1169 struct elf *elf; 1170 Elf_Cmd cmd; 1171 1172 elf_version(EV_CURRENT); 1173 1174 elf = malloc(sizeof(*elf)); 1175 if (!elf) { 1176 ERROR_GLIBC("malloc"); 1177 return NULL; 1178 } 1179 memset(elf, 0, sizeof(*elf)); 1180 1181 INIT_LIST_HEAD(&elf->sections); 1182 1183 elf->fd = open(name, flags); 1184 if (elf->fd == -1) { 1185 fprintf(stderr, "objtool: Can't open '%s': %s\n", 1186 name, strerror(errno)); 1187 goto err; 1188 } 1189 1190 elf->name = strdup(name); 1191 if (!elf->name) { 1192 ERROR_GLIBC("strdup"); 1193 return NULL; 1194 } 1195 1196 if ((flags & O_ACCMODE) == O_RDONLY) 1197 cmd = ELF_C_READ_MMAP; 1198 else if ((flags & O_ACCMODE) == O_RDWR) 1199 cmd = ELF_C_RDWR; 1200 else /* O_WRONLY */ 1201 cmd = ELF_C_WRITE; 1202 1203 elf->elf = elf_begin(elf->fd, cmd, NULL); 1204 if (!elf->elf) { 1205 ERROR_ELF("elf_begin"); 1206 goto err; 1207 } 1208 1209 if (!gelf_getehdr(elf->elf, &elf->ehdr)) { 1210 ERROR_ELF("gelf_getehdr"); 1211 goto err; 1212 } 1213 1214 if (read_sections(elf)) 1215 goto err; 1216 1217 mark_rodata(elf); 1218 1219 if (read_symbols(elf)) 1220 goto err; 1221 1222 if (mark_group_syms(elf)) 1223 goto err; 1224 1225 if (read_relocs(elf)) 1226 goto err; 1227 1228 return elf; 1229 1230 err: 1231 elf_close(elf); 1232 return NULL; 1233 } 1234 1235 struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) 1236 { 1237 struct section *null, *symtab, *strtab, *shstrtab; 1238 char *tmp_name; 1239 struct symbol *sym; 1240 struct elf *elf; 1241 1242 elf_version(EV_CURRENT); 1243 1244 elf = calloc(1, sizeof(*elf)); 1245 if (!elf) { 1246 ERROR_GLIBC("calloc"); 1247 return NULL; 1248 } 1249 1250 INIT_LIST_HEAD(&elf->sections); 1251 1252 tmp_name = malloc(strlen(name) + 8); 1253 if (!tmp_name) { 1254 ERROR_GLIBC("malloc"); 1255 return NULL; 1256 } 1257 1258 sprintf(tmp_name, "%s.XXXXXX", name); 1259 1260 elf->fd = mkstemp(tmp_name); 1261 if (elf->fd == -1) { 1262 ERROR_GLIBC("can't create tmp file"); 1263 exit(1); 1264 } 1265 1266 elf->tmp_name = tmp_name; 1267 1268 elf->name = strdup(name); 1269 if (!elf->name) { 1270 ERROR_GLIBC("strdup"); 1271 return NULL; 1272 } 1273 1274 elf->elf = elf_begin(elf->fd, ELF_C_WRITE, NULL); 1275 if (!elf->elf) { 1276 ERROR_ELF("elf_begin"); 1277 return NULL; 1278 } 1279 1280 if (!gelf_newehdr(elf->elf, ELFCLASS64)) { 1281 ERROR_ELF("gelf_newehdr"); 1282 return NULL; 1283 } 1284 1285 memcpy(&elf->ehdr, ehdr, sizeof(elf->ehdr)); 1286 1287 if (!gelf_update_ehdr(elf->elf, &elf->ehdr)) { 1288 ERROR_ELF("gelf_update_ehdr"); 1289 return NULL; 1290 } 1291 1292 INIT_LIST_HEAD(&elf->symbols); 1293 1294 if (!elf_alloc_hash(section, 1000) || 1295 !elf_alloc_hash(section_name, 1000) || 1296 !elf_alloc_hash(symbol, 10000) || 1297 !elf_alloc_hash(symbol_name, 10000) || 1298 !elf_alloc_hash(reloc, 100000)) 1299 return NULL; 1300 1301 null = elf_create_section(elf, NULL, 0, 0, SHT_NULL, 0, 0); 1302 shstrtab = elf_create_section(elf, NULL, 0, 0, SHT_STRTAB, 1, 0); 1303 strtab = elf_create_section(elf, NULL, 0, 0, SHT_STRTAB, 1, 0); 1304 1305 if (!null || !shstrtab || !strtab) 1306 return NULL; 1307 1308 null->name = ""; 1309 shstrtab->name = ".shstrtab"; 1310 strtab->name = ".strtab"; 1311 1312 null->sh.sh_name = elf_add_string(elf, shstrtab, null->name); 1313 shstrtab->sh.sh_name = elf_add_string(elf, shstrtab, shstrtab->name); 1314 strtab->sh.sh_name = elf_add_string(elf, shstrtab, strtab->name); 1315 1316 if (null->sh.sh_name == -1 || shstrtab->sh.sh_name == -1 || strtab->sh.sh_name == -1) 1317 return NULL; 1318 1319 elf_hash_add(section_name, &null->name_hash, str_hash(null->name)); 1320 elf_hash_add(section_name, &strtab->name_hash, str_hash(strtab->name)); 1321 elf_hash_add(section_name, &shstrtab->name_hash, str_hash(shstrtab->name)); 1322 1323 if (elf_add_string(elf, strtab, "") == -1) 1324 return NULL; 1325 1326 symtab = elf_create_section(elf, ".symtab", 0x18, 0x18, SHT_SYMTAB, 0x8, 0); 1327 if (!symtab) 1328 return NULL; 1329 1330 symtab->sh.sh_link = strtab->idx; 1331 symtab->sh.sh_info = 1; 1332 1333 elf->ehdr.e_shstrndx = shstrtab->idx; 1334 if (!gelf_update_ehdr(elf->elf, &elf->ehdr)) { 1335 ERROR_ELF("gelf_update_ehdr"); 1336 return NULL; 1337 } 1338 1339 sym = calloc(1, sizeof(*sym)); 1340 if (!sym) { 1341 ERROR_GLIBC("calloc"); 1342 return NULL; 1343 } 1344 1345 sym->name = ""; 1346 sym->sec = null; 1347 elf_add_symbol(elf, sym); 1348 1349 return elf; 1350 } 1351 1352 unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str) 1353 { 1354 unsigned int offset; 1355 1356 if (!strtab) 1357 strtab = find_section_by_name(elf, ".strtab"); 1358 if (!strtab) { 1359 ERROR("can't find .strtab section"); 1360 return -1; 1361 } 1362 1363 if (!strtab->sh.sh_addralign) { 1364 ERROR("'%s': invalid sh_addralign", strtab->name); 1365 return -1; 1366 } 1367 1368 offset = ALIGN(sec_size(strtab), strtab->sh.sh_addralign); 1369 1370 if (!elf_add_data(elf, strtab, str, strlen(str) + 1)) 1371 return -1; 1372 1373 return offset; 1374 } 1375 1376 void *elf_add_data(struct elf *elf, struct section *sec, const void *data, size_t size) 1377 { 1378 unsigned long offset; 1379 Elf_Scn *s; 1380 1381 if (!sec->sh.sh_addralign) { 1382 ERROR("'%s': invalid sh_addralign", sec->name); 1383 return NULL; 1384 } 1385 1386 s = elf_getscn(elf->elf, sec->idx); 1387 if (!s) { 1388 ERROR_ELF("elf_getscn"); 1389 return NULL; 1390 } 1391 1392 sec->data = elf_newdata(s); 1393 if (!sec->data) { 1394 ERROR_ELF("elf_newdata"); 1395 return NULL; 1396 } 1397 1398 sec->data->d_buf = calloc(1, size); 1399 if (!sec->data->d_buf) { 1400 ERROR_GLIBC("calloc"); 1401 return NULL; 1402 } 1403 1404 if (data) 1405 memcpy(sec->data->d_buf, data, size); 1406 1407 sec->data->d_size = size; 1408 sec->data->d_align = sec->sh.sh_addralign; 1409 1410 offset = ALIGN(sec_size(sec), sec->sh.sh_addralign); 1411 sec->sh.sh_size = offset + size; 1412 1413 mark_sec_changed(elf, sec, true); 1414 1415 return sec->data->d_buf; 1416 } 1417 1418 struct section *elf_create_section(struct elf *elf, const char *name, 1419 size_t size, size_t entsize, 1420 unsigned int type, unsigned int align, 1421 unsigned int flags) 1422 { 1423 struct section *sec, *shstrtab; 1424 Elf_Scn *s; 1425 1426 if (name && find_section_by_name(elf, name)) { 1427 ERROR("section '%s' already exists", name); 1428 return NULL; 1429 } 1430 1431 sec = calloc(1, sizeof(*sec)); 1432 if (!sec) { 1433 ERROR_GLIBC("calloc"); 1434 return NULL; 1435 } 1436 1437 INIT_LIST_HEAD(&sec->symbol_list); 1438 1439 /* don't actually create the section, just the data structures */ 1440 if (type == SHT_NULL) 1441 goto add; 1442 1443 s = elf_newscn(elf->elf); 1444 if (!s) { 1445 ERROR_ELF("elf_newscn"); 1446 return NULL; 1447 } 1448 1449 sec->idx = elf_ndxscn(s); 1450 1451 if (size) { 1452 sec->data = elf_newdata(s); 1453 if (!sec->data) { 1454 ERROR_ELF("elf_newdata"); 1455 return NULL; 1456 } 1457 1458 sec->data->d_size = size; 1459 sec->data->d_align = 1; 1460 1461 sec->data->d_buf = calloc(1, size); 1462 if (!sec->data->d_buf) { 1463 ERROR_GLIBC("calloc"); 1464 return NULL; 1465 } 1466 } 1467 1468 if (!gelf_getshdr(s, &sec->sh)) { 1469 ERROR_ELF("gelf_getshdr"); 1470 return NULL; 1471 } 1472 1473 sec->sh.sh_size = size; 1474 sec->sh.sh_entsize = entsize; 1475 sec->sh.sh_type = type; 1476 sec->sh.sh_addralign = align; 1477 sec->sh.sh_flags = flags; 1478 1479 if (name) { 1480 sec->name = strdup(name); 1481 if (!sec->name) { 1482 ERROR("strdup"); 1483 return NULL; 1484 } 1485 1486 /* Add section name to .shstrtab (or .strtab for Clang) */ 1487 shstrtab = find_section_by_name(elf, ".shstrtab"); 1488 if (!shstrtab) { 1489 shstrtab = find_section_by_name(elf, ".strtab"); 1490 if (!shstrtab) { 1491 ERROR("can't find .shstrtab or .strtab"); 1492 return NULL; 1493 } 1494 } 1495 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name); 1496 if (sec->sh.sh_name == -1) 1497 return NULL; 1498 1499 elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name)); 1500 } 1501 1502 add: 1503 list_add_tail(&sec->list, &elf->sections); 1504 elf_hash_add(section, &sec->hash, sec->idx); 1505 1506 mark_sec_changed(elf, sec, true); 1507 1508 return sec; 1509 } 1510 1511 static int elf_alloc_reloc(struct elf *elf, struct section *rsec) 1512 { 1513 struct reloc *old_relocs, *old_relocs_end, *new_relocs; 1514 unsigned int nr_relocs_old = sec_num_entries(rsec); 1515 unsigned int nr_relocs_new = nr_relocs_old + 1; 1516 unsigned long nr_alloc; 1517 struct symbol *sym; 1518 1519 if (!rsec->data) { 1520 rsec->data = elf_newdata(elf_getscn(elf->elf, rsec->idx)); 1521 if (!rsec->data) { 1522 ERROR_ELF("elf_newdata"); 1523 return -1; 1524 } 1525 1526 rsec->data->d_align = 1; 1527 rsec->data->d_type = ELF_T_RELA; 1528 rsec->data->d_buf = NULL; 1529 } 1530 1531 rsec->data->d_size = nr_relocs_new * elf_rela_size(elf); 1532 rsec->sh.sh_size = rsec->data->d_size; 1533 1534 nr_alloc = max(64UL, roundup_pow_of_two(nr_relocs_new)); 1535 if (nr_alloc <= rsec->nr_alloc_relocs) 1536 return 0; 1537 1538 if (rsec->data->d_buf && !rsec->nr_alloc_relocs) { 1539 void *orig_buf = rsec->data->d_buf; 1540 1541 /* 1542 * The original d_buf is owned by libelf so it can't be 1543 * realloced. 1544 */ 1545 rsec->data->d_buf = malloc(nr_alloc * elf_rela_size(elf)); 1546 if (!rsec->data->d_buf) { 1547 ERROR_GLIBC("malloc"); 1548 return -1; 1549 } 1550 memcpy(rsec->data->d_buf, orig_buf, 1551 nr_relocs_old * elf_rela_size(elf)); 1552 } else { 1553 rsec->data->d_buf = realloc(rsec->data->d_buf, 1554 nr_alloc * elf_rela_size(elf)); 1555 if (!rsec->data->d_buf) { 1556 ERROR_GLIBC("realloc"); 1557 return -1; 1558 } 1559 } 1560 1561 rsec->nr_alloc_relocs = nr_alloc; 1562 1563 old_relocs = rsec->relocs; 1564 new_relocs = calloc(nr_alloc, sizeof(struct reloc)); 1565 if (!new_relocs) { 1566 ERROR_GLIBC("calloc"); 1567 return -1; 1568 } 1569 1570 if (!old_relocs) 1571 goto done; 1572 1573 /* 1574 * The struct reloc's address has changed. Update all the symbols and 1575 * relocs which reference it. 1576 */ 1577 1578 old_relocs_end = &old_relocs[nr_relocs_old]; 1579 for_each_sym(elf, sym) { 1580 struct reloc *reloc; 1581 1582 reloc = sym->relocs; 1583 if (!reloc) 1584 continue; 1585 1586 if (reloc >= old_relocs && reloc < old_relocs_end) 1587 sym->relocs = &new_relocs[reloc - old_relocs]; 1588 1589 while (1) { 1590 struct reloc *next_reloc = sym_next_reloc(reloc); 1591 1592 if (!next_reloc) 1593 break; 1594 1595 if (next_reloc >= old_relocs && next_reloc < old_relocs_end) 1596 set_sym_next_reloc(reloc, &new_relocs[next_reloc - old_relocs]); 1597 1598 reloc = next_reloc; 1599 } 1600 } 1601 1602 memcpy(new_relocs, old_relocs, nr_relocs_old * sizeof(struct reloc)); 1603 1604 for (int i = 0; i < nr_relocs_old; i++) { 1605 struct reloc *old = &old_relocs[i]; 1606 struct reloc *new = &new_relocs[i]; 1607 u32 key = reloc_hash(old); 1608 1609 elf_hash_del(reloc, &old->hash, key); 1610 elf_hash_add(reloc, &new->hash, key); 1611 } 1612 1613 free(old_relocs); 1614 done: 1615 rsec->relocs = new_relocs; 1616 return 0; 1617 } 1618 1619 struct section *elf_create_rela_section(struct elf *elf, struct section *sec, 1620 unsigned int nr_relocs) 1621 { 1622 struct section *rsec; 1623 char *rsec_name; 1624 1625 rsec_name = malloc(strlen(sec->name) + strlen(".rela") + 1); 1626 if (!rsec_name) { 1627 ERROR_GLIBC("malloc"); 1628 return NULL; 1629 } 1630 strcpy(rsec_name, ".rela"); 1631 strcat(rsec_name, sec->name); 1632 1633 rsec = elf_create_section(elf, rsec_name, nr_relocs * elf_rela_size(elf), 1634 elf_rela_size(elf), SHT_RELA, elf_addr_size(elf), 1635 SHF_INFO_LINK); 1636 free(rsec_name); 1637 if (!rsec) 1638 return NULL; 1639 1640 if (nr_relocs) { 1641 rsec->data->d_type = ELF_T_RELA; 1642 1643 rsec->nr_alloc_relocs = nr_relocs; 1644 rsec->relocs = calloc(nr_relocs, sizeof(struct reloc)); 1645 if (!rsec->relocs) { 1646 ERROR_GLIBC("calloc"); 1647 return NULL; 1648 } 1649 } 1650 1651 rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; 1652 rsec->sh.sh_info = sec->idx; 1653 1654 sec->rsec = rsec; 1655 rsec->base = sec; 1656 1657 return rsec; 1658 } 1659 1660 struct reloc *elf_create_reloc(struct elf *elf, struct section *sec, 1661 unsigned long offset, 1662 struct symbol *sym, s64 addend, 1663 unsigned int type) 1664 { 1665 struct section *rsec = sec->rsec; 1666 1667 if (!rsec) { 1668 rsec = elf_create_rela_section(elf, sec, 0); 1669 if (!rsec) 1670 return NULL; 1671 } 1672 1673 if (find_reloc_by_dest(elf, sec, offset)) { 1674 ERROR_FUNC(sec, offset, "duplicate reloc"); 1675 return NULL; 1676 } 1677 1678 if (elf_alloc_reloc(elf, rsec)) 1679 return NULL; 1680 1681 mark_sec_changed(elf, rsec, true); 1682 1683 return elf_init_reloc(elf, rsec, sec_num_entries(rsec) - 1, offset, sym, 1684 addend, type); 1685 } 1686 1687 struct section *elf_create_section_pair(struct elf *elf, const char *name, 1688 size_t entsize, unsigned int nr, 1689 unsigned int nr_relocs) 1690 { 1691 struct section *sec; 1692 1693 sec = elf_create_section(elf, name, nr * entsize, entsize, 1694 SHT_PROGBITS, 1, SHF_ALLOC); 1695 if (!sec) 1696 return NULL; 1697 1698 if (!elf_create_rela_section(elf, sec, nr_relocs)) 1699 return NULL; 1700 1701 return sec; 1702 } 1703 1704 int elf_write_insn(struct elf *elf, struct section *sec, 1705 unsigned long offset, unsigned int len, 1706 const char *insn) 1707 { 1708 Elf_Data *data = sec->data; 1709 1710 if (data->d_type != ELF_T_BYTE || data->d_off) { 1711 ERROR("write to unexpected data for section: %s", sec->name); 1712 return -1; 1713 } 1714 1715 memcpy(data->d_buf + offset, insn, len); 1716 1717 mark_sec_changed(elf, sec, true); 1718 1719 return 0; 1720 } 1721 1722 /* 1723 * When Elf_Scn::sh_size is smaller than the combined Elf_Data::d_size 1724 * do you: 1725 * 1726 * A) adhere to the section header and truncate the data, or 1727 * B) ignore the section header and write out all the data you've got? 1728 * 1729 * Yes, libelf sucks and we need to manually truncate if we over-allocate data. 1730 */ 1731 static int elf_truncate_section(struct elf *elf, struct section *sec) 1732 { 1733 u64 size = sec_size(sec); 1734 bool truncated = false; 1735 Elf_Data *data = NULL; 1736 Elf_Scn *s; 1737 1738 s = elf_getscn(elf->elf, sec->idx); 1739 if (!s) { 1740 ERROR_ELF("elf_getscn"); 1741 return -1; 1742 } 1743 1744 for (;;) { 1745 /* get next data descriptor for the relevant section */ 1746 data = elf_getdata(s, data); 1747 if (!data) { 1748 if (size) { 1749 ERROR("end of section data but non-zero size left\n"); 1750 return -1; 1751 } 1752 return 0; 1753 } 1754 1755 if (truncated) { 1756 /* when we remove symbols */ 1757 ERROR("truncated; but more data\n"); 1758 return -1; 1759 } 1760 1761 if (!data->d_size) { 1762 ERROR("zero size data"); 1763 return -1; 1764 } 1765 1766 if (data->d_size > size) { 1767 truncated = true; 1768 data->d_size = size; 1769 } 1770 1771 size -= data->d_size; 1772 } 1773 } 1774 1775 int elf_write(struct elf *elf) 1776 { 1777 struct section *sec; 1778 Elf_Scn *s; 1779 1780 /* Update changed relocation sections and section headers: */ 1781 list_for_each_entry(sec, &elf->sections, list) { 1782 if (sec->truncate && elf_truncate_section(elf, sec)) 1783 return -1; 1784 1785 if (sec_changed(sec)) { 1786 s = elf_getscn(elf->elf, sec->idx); 1787 if (!s) { 1788 ERROR_ELF("elf_getscn"); 1789 return -1; 1790 } 1791 1792 /* Note this also flags the section dirty */ 1793 if (!gelf_update_shdr(s, &sec->sh)) { 1794 ERROR_ELF("gelf_update_shdr"); 1795 return -1; 1796 } 1797 1798 mark_sec_changed(elf, sec, false); 1799 } 1800 } 1801 1802 /* Make sure the new section header entries get updated properly. */ 1803 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY); 1804 1805 /* Write all changes to the file. */ 1806 if (elf_update(elf->elf, ELF_C_WRITE) < 0) { 1807 ERROR_ELF("elf_update"); 1808 return -1; 1809 } 1810 1811 elf->changed = false; 1812 1813 return 0; 1814 } 1815 1816 int elf_close(struct elf *elf) 1817 { 1818 if (elf->elf) 1819 elf_end(elf->elf); 1820 1821 if (elf->fd > 0) 1822 close(elf->fd); 1823 1824 if (elf->tmp_name && rename(elf->tmp_name, elf->name)) 1825 return -1; 1826 1827 /* 1828 * NOTE: All remaining allocations are leaked on purpose. Objtool is 1829 * about to exit anyway. 1830 */ 1831 return 0; 1832 } 1833