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