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