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