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 <fcntl.h> 13#include <stdio.h> 14#include <stdlib.h> 15#include <string.h> 16#include <unistd.h> 17#include <errno.h> 18#include "builtin.h" 19 20#include "elf.h" 21#include "warn.h" 22 23#define MAX_NAME_LEN 128 24 25static inline u32 str_hash(const char *str) 26{ 27 return jhash(str, strlen(str), 0); 28} 29 30static inline int elf_hash_bits(void) 31{ 32 return vmlinux ? ELF_HASH_BITS : 16; 33} 34 35#define elf_hash_add(hashtable, node, key) \ 36 hlist_add_head(node, &hashtable[hash_min(key, elf_hash_bits())]) 37 38static void elf_hash_init(struct hlist_head *table) 39{ 40 __hash_init(table, 1U << elf_hash_bits()); 41} 42 43#define elf_hash_for_each_possible(name, obj, member, key) \ 44 hlist_for_each_entry(obj, &name[hash_min(key, elf_hash_bits())], member) 45 46static void rb_add(struct rb_root *tree, struct rb_node *node, 47 int (*cmp)(struct rb_node *, const struct rb_node *)) 48{ 49 struct rb_node **link = &tree->rb_node; 50 struct rb_node *parent = NULL; 51 52 while (*link) { 53 parent = *link; 54 if (cmp(node, parent) < 0) 55 link = &parent->rb_left; 56 else 57 link = &parent->rb_right; 58 } 59 60 rb_link_node(node, parent, link); 61 rb_insert_color(node, tree); 62} 63 64static struct rb_node *rb_find_first(const struct rb_root *tree, const void *key, 65 int (*cmp)(const void *key, const struct rb_node *)) 66{ 67 struct rb_node *node = tree->rb_node; 68 struct rb_node *match = NULL; 69 70 while (node) { 71 int c = cmp(key, node); 72 if (c <= 0) { 73 if (!c) 74 match = node; 75 node = node->rb_left; 76 } else if (c > 0) { 77 node = node->rb_right; 78 } 79 } 80 81 return match; 82} 83 84static struct rb_node *rb_next_match(struct rb_node *node, const void *key, 85 int (*cmp)(const void *key, const struct rb_node *)) 86{ 87 node = rb_next(node); 88 if (node && cmp(key, node)) 89 node = NULL; 90 return node; 91} 92 93#define rb_for_each(tree, node, key, cmp) \ 94 for ((node) = rb_find_first((tree), (key), (cmp)); \ 95 (node); (node) = rb_next_match((node), (key), (cmp))) 96 97static int symbol_to_offset(struct rb_node *a, const struct rb_node *b) 98{ 99 struct symbol *sa = rb_entry(a, struct symbol, node); 100 struct symbol *sb = rb_entry(b, struct symbol, node); 101 102 if (sa->offset < sb->offset) 103 return -1; 104 if (sa->offset > sb->offset) 105 return 1; 106 107 if (sa->len < sb->len) 108 return -1; 109 if (sa->len > sb->len) 110 return 1; 111 112 sa->alias = sb; 113 114 return 0; 115} 116 117static int symbol_by_offset(const void *key, const struct rb_node *node) 118{ 119 const struct symbol *s = rb_entry(node, struct symbol, node); 120 const unsigned long *o = key; 121 122 if (*o < s->offset) 123 return -1; 124 if (*o >= s->offset + s->len) 125 return 1; 126 127 return 0; 128} 129 130struct section *find_section_by_name(const struct elf *elf, const char *name) 131{ 132 struct section *sec; 133 134 elf_hash_for_each_possible(elf->section_name_hash, sec, name_hash, str_hash(name)) 135 if (!strcmp(sec->name, name)) 136 return sec; 137 138 return NULL; 139} 140 141static struct section *find_section_by_index(struct elf *elf, 142 unsigned int idx) 143{ 144 struct section *sec; 145 146 elf_hash_for_each_possible(elf->section_hash, sec, hash, idx) 147 if (sec->idx == idx) 148 return sec; 149 150 return NULL; 151} 152 153static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx) 154{ 155 struct symbol *sym; 156 157 elf_hash_for_each_possible(elf->symbol_hash, sym, hash, idx) 158 if (sym->idx == idx) 159 return sym; 160 161 return NULL; 162} 163 164struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset) 165{ 166 struct rb_node *node; 167 168 rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) { 169 struct symbol *s = rb_entry(node, struct symbol, node); 170 171 if (s->offset == offset && s->type != STT_SECTION) 172 return s; 173 } 174 175 return NULL; 176} 177 178struct symbol *find_func_by_offset(struct section *sec, unsigned long offset) 179{ 180 struct rb_node *node; 181 182 rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) { 183 struct symbol *s = rb_entry(node, struct symbol, node); 184 185 if (s->offset == offset && s->type == STT_FUNC) 186 return s; 187 } 188 189 return NULL; 190} 191 192struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset) 193{ 194 struct rb_node *node; 195 196 rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) { 197 struct symbol *s = rb_entry(node, struct symbol, node); 198 199 if (s->type != STT_SECTION) 200 return s; 201 } 202 203 return NULL; 204} 205 206struct symbol *find_func_containing(struct section *sec, unsigned long offset) 207{ 208 struct rb_node *node; 209 210 rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) { 211 struct symbol *s = rb_entry(node, struct symbol, node); 212 213 if (s->type == STT_FUNC) 214 return s; 215 } 216 217 return NULL; 218} 219 220struct symbol *find_symbol_by_name(const struct elf *elf, const char *name) 221{ 222 struct symbol *sym; 223 224 elf_hash_for_each_possible(elf->symbol_name_hash, sym, name_hash, str_hash(name)) 225 if (!strcmp(sym->name, name)) 226 return sym; 227 228 return NULL; 229} 230 231struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec, 232 unsigned long offset, unsigned int len) 233{ 234 struct reloc *reloc, *r = NULL; 235 unsigned long o; 236 237 if (!sec->reloc) 238 return NULL; 239 240 sec = sec->reloc; 241 242 for_offset_range(o, offset, offset + len) { 243 elf_hash_for_each_possible(elf->reloc_hash, reloc, hash, 244 sec_offset_hash(sec, o)) { 245 if (reloc->sec != sec) 246 continue; 247 248 if (reloc->offset >= offset && reloc->offset < offset + len) { 249 if (!r || reloc->offset < r->offset) 250 r = reloc; 251 } 252 } 253 if (r) 254 return r; 255 } 256 257 return NULL; 258} 259 260struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset) 261{ 262 return find_reloc_by_dest_range(elf, sec, offset, 1); 263} 264 265static int read_sections(struct elf *elf) 266{ 267 Elf_Scn *s = NULL; 268 struct section *sec; 269 size_t shstrndx, sections_nr; 270 int i; 271 272 if (elf_getshdrnum(elf->elf, §ions_nr)) { 273 WARN_ELF("elf_getshdrnum"); 274 return -1; 275 } 276 277 if (elf_getshdrstrndx(elf->elf, &shstrndx)) { 278 WARN_ELF("elf_getshdrstrndx"); 279 return -1; 280 } 281 282 for (i = 0; i < sections_nr; i++) { 283 sec = malloc(sizeof(*sec)); 284 if (!sec) { 285 perror("malloc"); 286 return -1; 287 } 288 memset(sec, 0, sizeof(*sec)); 289 290 INIT_LIST_HEAD(&sec->symbol_list); 291 INIT_LIST_HEAD(&sec->reloc_list); 292 293 s = elf_getscn(elf->elf, i); 294 if (!s) { 295 WARN_ELF("elf_getscn"); 296 return -1; 297 } 298 299 sec->idx = elf_ndxscn(s); 300 301 if (!gelf_getshdr(s, &sec->sh)) { 302 WARN_ELF("gelf_getshdr"); 303 return -1; 304 } 305 306 sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name); 307 if (!sec->name) { 308 WARN_ELF("elf_strptr"); 309 return -1; 310 } 311 312 if (sec->sh.sh_size != 0) { 313 sec->data = elf_getdata(s, NULL); 314 if (!sec->data) { 315 WARN_ELF("elf_getdata"); 316 return -1; 317 } 318 if (sec->data->d_off != 0 || 319 sec->data->d_size != sec->sh.sh_size) { 320 WARN("unexpected data attributes for %s", 321 sec->name); 322 return -1; 323 } 324 } 325 sec->len = sec->sh.sh_size; 326 327 list_add_tail(&sec->list, &elf->sections); 328 elf_hash_add(elf->section_hash, &sec->hash, sec->idx); 329 elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name)); 330 } 331 332 if (stats) 333 printf("nr_sections: %lu\n", (unsigned long)sections_nr); 334 335 /* sanity check, one more call to elf_nextscn() should return NULL */ 336 if (elf_nextscn(elf->elf, s)) { 337 WARN("section entry mismatch"); 338 return -1; 339 } 340 341 return 0; 342} 343 344static void elf_add_symbol(struct elf *elf, struct symbol *sym) 345{ 346 struct list_head *entry; 347 struct rb_node *pnode; 348 349 sym->alias = sym; 350 351 sym->type = GELF_ST_TYPE(sym->sym.st_info); 352 sym->bind = GELF_ST_BIND(sym->sym.st_info); 353 354 sym->offset = sym->sym.st_value; 355 sym->len = sym->sym.st_size; 356 357 rb_add(&sym->sec->symbol_tree, &sym->node, symbol_to_offset); 358 pnode = rb_prev(&sym->node); 359 if (pnode) 360 entry = &rb_entry(pnode, struct symbol, node)->list; 361 else 362 entry = &sym->sec->symbol_list; 363 list_add(&sym->list, entry); 364 elf_hash_add(elf->symbol_hash, &sym->hash, sym->idx); 365 elf_hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name)); 366 367 /* 368 * Don't store empty STT_NOTYPE symbols in the rbtree. They 369 * can exist within a function, confusing the sorting. 370 */ 371 if (!sym->len) 372 rb_erase(&sym->node, &sym->sec->symbol_tree); 373} 374 375static int read_symbols(struct elf *elf) 376{ 377 struct section *symtab, *symtab_shndx, *sec; 378 struct symbol *sym, *pfunc; 379 int symbols_nr, i; 380 char *coldstr; 381 Elf_Data *shndx_data = NULL; 382 Elf32_Word shndx; 383 384 symtab = find_section_by_name(elf, ".symtab"); 385 if (!symtab) { 386 /* 387 * A missing symbol table is actually possible if it's an empty 388 * .o file. This can happen for thunk_64.o. 389 */ 390 return 0; 391 } 392 393 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); 394 if (symtab_shndx) 395 shndx_data = symtab_shndx->data; 396 397 symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize; 398 399 for (i = 0; i < symbols_nr; i++) { 400 sym = malloc(sizeof(*sym)); 401 if (!sym) { 402 perror("malloc"); 403 return -1; 404 } 405 memset(sym, 0, sizeof(*sym)); 406 407 sym->idx = i; 408 409 if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym, 410 &shndx)) { 411 WARN_ELF("gelf_getsymshndx"); 412 goto err; 413 } 414 415 sym->name = elf_strptr(elf->elf, symtab->sh.sh_link, 416 sym->sym.st_name); 417 if (!sym->name) { 418 WARN_ELF("elf_strptr"); 419 goto err; 420 } 421 422 if ((sym->sym.st_shndx > SHN_UNDEF && 423 sym->sym.st_shndx < SHN_LORESERVE) || 424 (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) { 425 if (sym->sym.st_shndx != SHN_XINDEX) 426 shndx = sym->sym.st_shndx; 427 428 sym->sec = find_section_by_index(elf, shndx); 429 if (!sym->sec) { 430 WARN("couldn't find section for symbol %s", 431 sym->name); 432 goto err; 433 } 434 if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) { 435 sym->name = sym->sec->name; 436 sym->sec->sym = sym; 437 } 438 } else 439 sym->sec = find_section_by_index(elf, 0); 440 441 elf_add_symbol(elf, sym); 442 } 443 444 if (stats) 445 printf("nr_symbols: %lu\n", (unsigned long)symbols_nr); 446 447 /* Create parent/child links for any cold subfunctions */ 448 list_for_each_entry(sec, &elf->sections, list) { 449 list_for_each_entry(sym, &sec->symbol_list, list) { 450 char pname[MAX_NAME_LEN + 1]; 451 size_t pnamelen; 452 if (sym->type != STT_FUNC) 453 continue; 454 455 if (sym->pfunc == NULL) 456 sym->pfunc = sym; 457 458 if (sym->cfunc == NULL) 459 sym->cfunc = sym; 460 461 coldstr = strstr(sym->name, ".cold"); 462 if (!coldstr) 463 continue; 464 465 pnamelen = coldstr - sym->name; 466 if (pnamelen > MAX_NAME_LEN) { 467 WARN("%s(): parent function name exceeds maximum length of %d characters", 468 sym->name, MAX_NAME_LEN); 469 return -1; 470 } 471 472 strncpy(pname, sym->name, pnamelen); 473 pname[pnamelen] = '\0'; 474 pfunc = find_symbol_by_name(elf, pname); 475 476 if (!pfunc) { 477 WARN("%s(): can't find parent function", 478 sym->name); 479 return -1; 480 } 481 482 sym->pfunc = pfunc; 483 pfunc->cfunc = sym; 484 485 /* 486 * Unfortunately, -fnoreorder-functions puts the child 487 * inside the parent. Remove the overlap so we can 488 * have sane assumptions. 489 * 490 * Note that pfunc->len now no longer matches 491 * pfunc->sym.st_size. 492 */ 493 if (sym->sec == pfunc->sec && 494 sym->offset >= pfunc->offset && 495 sym->offset + sym->len == pfunc->offset + pfunc->len) { 496 pfunc->len -= sym->len; 497 } 498 } 499 } 500 501 return 0; 502 503err: 504 free(sym); 505 return -1; 506} 507 508static struct section *elf_create_reloc_section(struct elf *elf, 509 struct section *base, 510 int reltype); 511 512int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset, 513 unsigned int type, struct symbol *sym, s64 addend) 514{ 515 struct reloc *reloc; 516 517 if (!sec->reloc && !elf_create_reloc_section(elf, sec, SHT_RELA)) 518 return -1; 519 520 reloc = malloc(sizeof(*reloc)); 521 if (!reloc) { 522 perror("malloc"); 523 return -1; 524 } 525 memset(reloc, 0, sizeof(*reloc)); 526 527 reloc->sec = sec->reloc; 528 reloc->offset = offset; 529 reloc->type = type; 530 reloc->sym = sym; 531 reloc->addend = addend; 532 533 list_add_tail(&reloc->list, &sec->reloc->reloc_list); 534 elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc)); 535 536 sec->reloc->changed = true; 537 538 return 0; 539} 540 541/* 542 * Ensure that any reloc section containing references to @sym is marked 543 * changed such that it will get re-generated in elf_rebuild_reloc_sections() 544 * with the new symbol index. 545 */ 546static void elf_dirty_reloc_sym(struct elf *elf, struct symbol *sym) 547{ 548 struct section *sec; 549 550 list_for_each_entry(sec, &elf->sections, list) { 551 struct reloc *reloc; 552 553 if (sec->changed) 554 continue; 555 556 list_for_each_entry(reloc, &sec->reloc_list, list) { 557 if (reloc->sym == sym) { 558 sec->changed = true; 559 break; 560 } 561 } 562 } 563} 564 565/* 566 * The libelf API is terrible; gelf_update_sym*() takes a data block relative 567 * index value, *NOT* the symbol index. As such, iterate the data blocks and 568 * adjust index until it fits. 569 * 570 * If no data block is found, allow adding a new data block provided the index 571 * is only one past the end. 572 */ 573static int elf_update_symbol(struct elf *elf, struct section *symtab, 574 struct section *symtab_shndx, struct symbol *sym) 575{ 576 Elf32_Word shndx = sym->sec ? sym->sec->idx : SHN_UNDEF; 577 Elf_Data *symtab_data = NULL, *shndx_data = NULL; 578 Elf64_Xword entsize = symtab->sh.sh_entsize; 579 int max_idx, idx = sym->idx; 580 Elf_Scn *s, *t = NULL; 581 bool is_special_shndx = sym->sym.st_shndx >= SHN_LORESERVE && 582 sym->sym.st_shndx != SHN_XINDEX; 583 584 if (is_special_shndx) 585 shndx = sym->sym.st_shndx; 586 587 s = elf_getscn(elf->elf, symtab->idx); 588 if (!s) { 589 WARN_ELF("elf_getscn"); 590 return -1; 591 } 592 593 if (symtab_shndx) { 594 t = elf_getscn(elf->elf, symtab_shndx->idx); 595 if (!t) { 596 WARN_ELF("elf_getscn"); 597 return -1; 598 } 599 } 600 601 for (;;) { 602 /* get next data descriptor for the relevant sections */ 603 symtab_data = elf_getdata(s, symtab_data); 604 if (t) 605 shndx_data = elf_getdata(t, shndx_data); 606 607 /* end-of-list */ 608 if (!symtab_data) { 609 void *buf; 610 611 if (idx) { 612 /* we don't do holes in symbol tables */ 613 WARN("index out of range"); 614 return -1; 615 } 616 617 /* if @idx == 0, it's the next contiguous entry, create it */ 618 symtab_data = elf_newdata(s); 619 if (t) 620 shndx_data = elf_newdata(t); 621 622 buf = calloc(1, entsize); 623 if (!buf) { 624 WARN("malloc"); 625 return -1; 626 } 627 628 symtab_data->d_buf = buf; 629 symtab_data->d_size = entsize; 630 symtab_data->d_align = 1; 631 symtab_data->d_type = ELF_T_SYM; 632 633 symtab->sh.sh_size += entsize; 634 symtab->changed = true; 635 636 if (t) { 637 shndx_data->d_buf = &sym->sec->idx; 638 shndx_data->d_size = sizeof(Elf32_Word); 639 shndx_data->d_align = sizeof(Elf32_Word); 640 shndx_data->d_type = ELF_T_WORD; 641 642 symtab_shndx->sh.sh_size += sizeof(Elf32_Word); 643 symtab_shndx->changed = true; 644 } 645 646 break; 647 } 648 649 /* empty blocks should not happen */ 650 if (!symtab_data->d_size) { 651 WARN("zero size data"); 652 return -1; 653 } 654 655 /* is this the right block? */ 656 max_idx = symtab_data->d_size / entsize; 657 if (idx < max_idx) 658 break; 659 660 /* adjust index and try again */ 661 idx -= max_idx; 662 } 663 664 /* something went side-ways */ 665 if (idx < 0) { 666 WARN("negative index"); 667 return -1; 668 } 669 670 /* setup extended section index magic and write the symbol */ 671 if ((shndx >= SHN_UNDEF && shndx < SHN_LORESERVE) || is_special_shndx) { 672 sym->sym.st_shndx = shndx; 673 if (!shndx_data) 674 shndx = 0; 675 } else { 676 sym->sym.st_shndx = SHN_XINDEX; 677 if (!shndx_data) { 678 WARN("no .symtab_shndx"); 679 return -1; 680 } 681 } 682 683 if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) { 684 WARN_ELF("gelf_update_symshndx"); 685 return -1; 686 } 687 688 return 0; 689} 690 691static struct symbol * 692elf_create_section_symbol(struct elf *elf, struct section *sec) 693{ 694 struct section *symtab, *symtab_shndx; 695 Elf32_Word first_non_local, new_idx; 696 struct symbol *sym, *old; 697 698 symtab = find_section_by_name(elf, ".symtab"); 699 if (symtab) { 700 symtab_shndx = find_section_by_name(elf, ".symtab_shndx"); 701 } else { 702 WARN("no .symtab"); 703 return NULL; 704 } 705 706 sym = calloc(1, sizeof(*sym)); 707 if (!sym) { 708 perror("malloc"); 709 return NULL; 710 } 711 712 sym->name = sec->name; 713 sym->sec = sec; 714 715 // st_name 0 716 sym->sym.st_info = GELF_ST_INFO(STB_LOCAL, STT_SECTION); 717 // st_other 0 718 // st_value 0 719 // st_size 0 720 721 /* 722 * Move the first global symbol, as per sh_info, into a new, higher 723 * symbol index. This fees up a spot for a new local symbol. 724 */ 725 first_non_local = symtab->sh.sh_info; 726 new_idx = symtab->sh.sh_size / symtab->sh.sh_entsize; 727 old = find_symbol_by_index(elf, first_non_local); 728 if (old) { 729 old->idx = new_idx; 730 731 hlist_del(&old->hash); 732 elf_hash_add(elf->symbol_hash, &old->hash, old->idx); 733 734 elf_dirty_reloc_sym(elf, old); 735 736 if (elf_update_symbol(elf, symtab, symtab_shndx, old)) { 737 WARN("elf_update_symbol move"); 738 return NULL; 739 } 740 741 new_idx = first_non_local; 742 } 743 744 sym->idx = new_idx; 745 if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) { 746 WARN("elf_update_symbol"); 747 return NULL; 748 } 749 750 /* 751 * Either way, we added a LOCAL symbol. 752 */ 753 symtab->sh.sh_info += 1; 754 755 elf_add_symbol(elf, sym); 756 757 return sym; 758} 759 760int elf_add_reloc_to_insn(struct elf *elf, struct section *sec, 761 unsigned long offset, unsigned int type, 762 struct section *insn_sec, unsigned long insn_off) 763{ 764 struct symbol *sym = insn_sec->sym; 765 int addend = insn_off; 766 767 if (!sym) { 768 /* 769 * Due to how weak functions work, we must use section based 770 * relocations. Symbol based relocations would result in the 771 * weak and non-weak function annotations being overlaid on the 772 * non-weak function after linking. 773 */ 774 sym = elf_create_section_symbol(elf, insn_sec); 775 if (!sym) 776 return -1; 777 778 insn_sec->sym = sym; 779 } 780 781 return elf_add_reloc(elf, sec, offset, type, sym, addend); 782} 783 784static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx) 785{ 786 if (!gelf_getrel(sec->data, i, &reloc->rel)) { 787 WARN_ELF("gelf_getrel"); 788 return -1; 789 } 790 reloc->type = GELF_R_TYPE(reloc->rel.r_info); 791 reloc->addend = 0; 792 reloc->offset = reloc->rel.r_offset; 793 *symndx = GELF_R_SYM(reloc->rel.r_info); 794 return 0; 795} 796 797static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx) 798{ 799 if (!gelf_getrela(sec->data, i, &reloc->rela)) { 800 WARN_ELF("gelf_getrela"); 801 return -1; 802 } 803 reloc->type = GELF_R_TYPE(reloc->rela.r_info); 804 reloc->addend = reloc->rela.r_addend; 805 reloc->offset = reloc->rela.r_offset; 806 *symndx = GELF_R_SYM(reloc->rela.r_info); 807 return 0; 808} 809 810static int read_relocs(struct elf *elf) 811{ 812 struct section *sec; 813 struct reloc *reloc; 814 int i; 815 unsigned int symndx; 816 unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0; 817 818 list_for_each_entry(sec, &elf->sections, list) { 819 if ((sec->sh.sh_type != SHT_RELA) && 820 (sec->sh.sh_type != SHT_REL)) 821 continue; 822 823 sec->base = find_section_by_index(elf, sec->sh.sh_info); 824 if (!sec->base) { 825 WARN("can't find base section for reloc section %s", 826 sec->name); 827 return -1; 828 } 829 830 sec->base->reloc = sec; 831 832 nr_reloc = 0; 833 for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) { 834 reloc = malloc(sizeof(*reloc)); 835 if (!reloc) { 836 perror("malloc"); 837 return -1; 838 } 839 memset(reloc, 0, sizeof(*reloc)); 840 switch (sec->sh.sh_type) { 841 case SHT_REL: 842 if (read_rel_reloc(sec, i, reloc, &symndx)) 843 return -1; 844 break; 845 case SHT_RELA: 846 if (read_rela_reloc(sec, i, reloc, &symndx)) 847 return -1; 848 break; 849 default: return -1; 850 } 851 852 reloc->sec = sec; 853 reloc->idx = i; 854 reloc->sym = find_symbol_by_index(elf, symndx); 855 if (!reloc->sym) { 856 WARN("can't find reloc entry symbol %d for %s", 857 symndx, sec->name); 858 return -1; 859 } 860 861 list_add_tail(&reloc->list, &sec->reloc_list); 862 elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc)); 863 864 nr_reloc++; 865 } 866 max_reloc = max(max_reloc, nr_reloc); 867 tot_reloc += nr_reloc; 868 } 869 870 if (stats) { 871 printf("max_reloc: %lu\n", max_reloc); 872 printf("tot_reloc: %lu\n", tot_reloc); 873 } 874 875 return 0; 876} 877 878struct elf *elf_open_read(const char *name, int flags) 879{ 880 struct elf *elf; 881 Elf_Cmd cmd; 882 883 elf_version(EV_CURRENT); 884 885 elf = malloc(sizeof(*elf)); 886 if (!elf) { 887 perror("malloc"); 888 return NULL; 889 } 890 memset(elf, 0, offsetof(struct elf, sections)); 891 892 INIT_LIST_HEAD(&elf->sections); 893 894 elf_hash_init(elf->symbol_hash); 895 elf_hash_init(elf->symbol_name_hash); 896 elf_hash_init(elf->section_hash); 897 elf_hash_init(elf->section_name_hash); 898 elf_hash_init(elf->reloc_hash); 899 900 elf->fd = open(name, flags); 901 if (elf->fd == -1) { 902 fprintf(stderr, "objtool: Can't open '%s': %s\n", 903 name, strerror(errno)); 904 goto err; 905 } 906 907 if ((flags & O_ACCMODE) == O_RDONLY) 908 cmd = ELF_C_READ_MMAP; 909 else if ((flags & O_ACCMODE) == O_RDWR) 910 cmd = ELF_C_RDWR; 911 else /* O_WRONLY */ 912 cmd = ELF_C_WRITE; 913 914 elf->elf = elf_begin(elf->fd, cmd, NULL); 915 if (!elf->elf) { 916 WARN_ELF("elf_begin"); 917 goto err; 918 } 919 920 if (!gelf_getehdr(elf->elf, &elf->ehdr)) { 921 WARN_ELF("gelf_getehdr"); 922 goto err; 923 } 924 925 if (read_sections(elf)) 926 goto err; 927 928 if (read_symbols(elf)) 929 goto err; 930 931 if (read_relocs(elf)) 932 goto err; 933 934 return elf; 935 936err: 937 elf_close(elf); 938 return NULL; 939} 940 941static int elf_add_string(struct elf *elf, struct section *strtab, char *str) 942{ 943 Elf_Data *data; 944 Elf_Scn *s; 945 int len; 946 947 if (!strtab) 948 strtab = find_section_by_name(elf, ".strtab"); 949 if (!strtab) { 950 WARN("can't find .strtab section"); 951 return -1; 952 } 953 954 s = elf_getscn(elf->elf, strtab->idx); 955 if (!s) { 956 WARN_ELF("elf_getscn"); 957 return -1; 958 } 959 960 data = elf_newdata(s); 961 if (!data) { 962 WARN_ELF("elf_newdata"); 963 return -1; 964 } 965 966 data->d_buf = str; 967 data->d_size = strlen(str) + 1; 968 data->d_align = 1; 969 data->d_type = ELF_T_SYM; 970 971 len = strtab->len; 972 strtab->len += data->d_size; 973 strtab->changed = true; 974 975 return len; 976} 977 978struct section *elf_create_section(struct elf *elf, const char *name, 979 unsigned int sh_flags, size_t entsize, int nr) 980{ 981 struct section *sec, *shstrtab; 982 size_t size = entsize * nr; 983 Elf_Scn *s; 984 985 sec = malloc(sizeof(*sec)); 986 if (!sec) { 987 perror("malloc"); 988 return NULL; 989 } 990 memset(sec, 0, sizeof(*sec)); 991 992 INIT_LIST_HEAD(&sec->symbol_list); 993 INIT_LIST_HEAD(&sec->reloc_list); 994 995 s = elf_newscn(elf->elf); 996 if (!s) { 997 WARN_ELF("elf_newscn"); 998 return NULL; 999 } 1000 1001 sec->name = strdup(name); 1002 if (!sec->name) { 1003 perror("strdup"); 1004 return NULL; 1005 } 1006 1007 sec->idx = elf_ndxscn(s); 1008 sec->len = size; 1009 sec->changed = true; 1010 1011 sec->data = elf_newdata(s); 1012 if (!sec->data) { 1013 WARN_ELF("elf_newdata"); 1014 return NULL; 1015 } 1016 1017 sec->data->d_size = size; 1018 sec->data->d_align = 1; 1019 1020 if (size) { 1021 sec->data->d_buf = malloc(size); 1022 if (!sec->data->d_buf) { 1023 perror("malloc"); 1024 return NULL; 1025 } 1026 memset(sec->data->d_buf, 0, size); 1027 } 1028 1029 if (!gelf_getshdr(s, &sec->sh)) { 1030 WARN_ELF("gelf_getshdr"); 1031 return NULL; 1032 } 1033 1034 sec->sh.sh_size = size; 1035 sec->sh.sh_entsize = entsize; 1036 sec->sh.sh_type = SHT_PROGBITS; 1037 sec->sh.sh_addralign = 1; 1038 sec->sh.sh_flags = SHF_ALLOC | sh_flags; 1039 1040 /* Add section name to .shstrtab (or .strtab for Clang) */ 1041 shstrtab = find_section_by_name(elf, ".shstrtab"); 1042 if (!shstrtab) 1043 shstrtab = find_section_by_name(elf, ".strtab"); 1044 if (!shstrtab) { 1045 WARN("can't find .shstrtab or .strtab section"); 1046 return NULL; 1047 } 1048 sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name); 1049 if (sec->sh.sh_name == -1) 1050 return NULL; 1051 1052 list_add_tail(&sec->list, &elf->sections); 1053 elf_hash_add(elf->section_hash, &sec->hash, sec->idx); 1054 elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name)); 1055 1056 elf->changed = true; 1057 1058 return sec; 1059} 1060 1061static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base) 1062{ 1063 char *relocname; 1064 struct section *sec; 1065 1066 relocname = malloc(strlen(base->name) + strlen(".rel") + 1); 1067 if (!relocname) { 1068 perror("malloc"); 1069 return NULL; 1070 } 1071 strcpy(relocname, ".rel"); 1072 strcat(relocname, base->name); 1073 1074 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0); 1075 free(relocname); 1076 if (!sec) 1077 return NULL; 1078 1079 base->reloc = sec; 1080 sec->base = base; 1081 1082 sec->sh.sh_type = SHT_REL; 1083 sec->sh.sh_addralign = 8; 1084 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; 1085 sec->sh.sh_info = base->idx; 1086 sec->sh.sh_flags = SHF_INFO_LINK; 1087 1088 return sec; 1089} 1090 1091static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base) 1092{ 1093 char *relocname; 1094 struct section *sec; 1095 1096 relocname = malloc(strlen(base->name) + strlen(".rela") + 1); 1097 if (!relocname) { 1098 perror("malloc"); 1099 return NULL; 1100 } 1101 strcpy(relocname, ".rela"); 1102 strcat(relocname, base->name); 1103 1104 sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0); 1105 free(relocname); 1106 if (!sec) 1107 return NULL; 1108 1109 base->reloc = sec; 1110 sec->base = base; 1111 1112 sec->sh.sh_type = SHT_RELA; 1113 sec->sh.sh_addralign = 8; 1114 sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx; 1115 sec->sh.sh_info = base->idx; 1116 sec->sh.sh_flags = SHF_INFO_LINK; 1117 1118 return sec; 1119} 1120 1121static struct section *elf_create_reloc_section(struct elf *elf, 1122 struct section *base, 1123 int reltype) 1124{ 1125 switch (reltype) { 1126 case SHT_REL: return elf_create_rel_reloc_section(elf, base); 1127 case SHT_RELA: return elf_create_rela_reloc_section(elf, base); 1128 default: return NULL; 1129 } 1130} 1131 1132static int elf_rebuild_rel_reloc_section(struct section *sec, int nr) 1133{ 1134 struct reloc *reloc; 1135 int idx = 0, size; 1136 GElf_Rel *relocs; 1137 1138 /* Allocate a buffer for relocations */ 1139 size = nr * sizeof(*relocs); 1140 relocs = malloc(size); 1141 if (!relocs) { 1142 perror("malloc"); 1143 return -1; 1144 } 1145 1146 sec->data->d_buf = relocs; 1147 sec->data->d_size = size; 1148 1149 sec->sh.sh_size = size; 1150 1151 idx = 0; 1152 list_for_each_entry(reloc, &sec->reloc_list, list) { 1153 relocs[idx].r_offset = reloc->offset; 1154 relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); 1155 idx++; 1156 } 1157 1158 return 0; 1159} 1160 1161static int elf_rebuild_rela_reloc_section(struct section *sec, int nr) 1162{ 1163 struct reloc *reloc; 1164 int idx = 0, size; 1165 GElf_Rela *relocs; 1166 1167 /* Allocate a buffer for relocations with addends */ 1168 size = nr * sizeof(*relocs); 1169 relocs = malloc(size); 1170 if (!relocs) { 1171 perror("malloc"); 1172 return -1; 1173 } 1174 1175 sec->data->d_buf = relocs; 1176 sec->data->d_size = size; 1177 1178 sec->sh.sh_size = size; 1179 1180 idx = 0; 1181 list_for_each_entry(reloc, &sec->reloc_list, list) { 1182 relocs[idx].r_offset = reloc->offset; 1183 relocs[idx].r_addend = reloc->addend; 1184 relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); 1185 idx++; 1186 } 1187 1188 return 0; 1189} 1190 1191static int elf_rebuild_reloc_section(struct elf *elf, struct section *sec) 1192{ 1193 struct reloc *reloc; 1194 int nr; 1195 1196 nr = 0; 1197 list_for_each_entry(reloc, &sec->reloc_list, list) 1198 nr++; 1199 1200 switch (sec->sh.sh_type) { 1201 case SHT_REL: return elf_rebuild_rel_reloc_section(sec, nr); 1202 case SHT_RELA: return elf_rebuild_rela_reloc_section(sec, nr); 1203 default: return -1; 1204 } 1205} 1206 1207int elf_write_insn(struct elf *elf, struct section *sec, 1208 unsigned long offset, unsigned int len, 1209 const char *insn) 1210{ 1211 Elf_Data *data = sec->data; 1212 1213 if (data->d_type != ELF_T_BYTE || data->d_off) { 1214 WARN("write to unexpected data for section: %s", sec->name); 1215 return -1; 1216 } 1217 1218 memcpy(data->d_buf + offset, insn, len); 1219 elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY); 1220 1221 elf->changed = true; 1222 1223 return 0; 1224} 1225 1226int elf_write_reloc(struct elf *elf, struct reloc *reloc) 1227{ 1228 struct section *sec = reloc->sec; 1229 1230 if (sec->sh.sh_type == SHT_REL) { 1231 reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); 1232 reloc->rel.r_offset = reloc->offset; 1233 1234 if (!gelf_update_rel(sec->data, reloc->idx, &reloc->rel)) { 1235 WARN_ELF("gelf_update_rel"); 1236 return -1; 1237 } 1238 } else { 1239 reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type); 1240 reloc->rela.r_addend = reloc->addend; 1241 reloc->rela.r_offset = reloc->offset; 1242 1243 if (!gelf_update_rela(sec->data, reloc->idx, &reloc->rela)) { 1244 WARN_ELF("gelf_update_rela"); 1245 return -1; 1246 } 1247 } 1248 1249 elf->changed = true; 1250 1251 return 0; 1252} 1253 1254int elf_write(struct elf *elf) 1255{ 1256 struct section *sec; 1257 Elf_Scn *s; 1258 1259 /* Update changed relocation sections and section headers: */ 1260 list_for_each_entry(sec, &elf->sections, list) { 1261 if (sec->changed) { 1262 if (sec->base && 1263 elf_rebuild_reloc_section(elf, sec)) { 1264 WARN("elf_rebuild_reloc_section"); 1265 return -1; 1266 } 1267 1268 s = elf_getscn(elf->elf, sec->idx); 1269 if (!s) { 1270 WARN_ELF("elf_getscn"); 1271 return -1; 1272 } 1273 if (!gelf_update_shdr(s, &sec->sh)) { 1274 WARN_ELF("gelf_update_shdr"); 1275 return -1; 1276 } 1277 1278 sec->changed = false; 1279 elf->changed = true; 1280 } 1281 } 1282 1283 /* Make sure the new section header entries get updated properly. */ 1284 elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY); 1285 1286 /* Write all changes to the file. */ 1287 if (elf_update(elf->elf, ELF_C_WRITE) < 0) { 1288 WARN_ELF("elf_update"); 1289 return -1; 1290 } 1291 1292 elf->changed = false; 1293 1294 return 0; 1295} 1296 1297void elf_close(struct elf *elf) 1298{ 1299 struct section *sec, *tmpsec; 1300 struct symbol *sym, *tmpsym; 1301 struct reloc *reloc, *tmpreloc; 1302 1303 if (elf->elf) 1304 elf_end(elf->elf); 1305 1306 if (elf->fd > 0) 1307 close(elf->fd); 1308 1309 list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) { 1310 list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) { 1311 list_del(&sym->list); 1312 hash_del(&sym->hash); 1313 free(sym); 1314 } 1315 list_for_each_entry_safe(reloc, tmpreloc, &sec->reloc_list, list) { 1316 list_del(&reloc->list); 1317 hash_del(&reloc->hash); 1318 free(reloc); 1319 } 1320 list_del(&sec->list); 1321 free(sec); 1322 } 1323 1324 free(elf); 1325} 1326