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