1 // SPDX-License-Identifier: GPL-2.0-or-later 2 #define _GNU_SOURCE /* memmem() */ 3 #include <subcmd/parse-options.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <libgen.h> 7 #include <stdio.h> 8 #include <ctype.h> 9 10 #include <objtool/objtool.h> 11 #include <objtool/warn.h> 12 #include <objtool/arch.h> 13 #include <objtool/klp.h> 14 #include <objtool/util.h> 15 #include <arch/special.h> 16 17 #include <linux/objtool_types.h> 18 #include <linux/livepatch_external.h> 19 #include <linux/stringify.h> 20 #include <linux/string.h> 21 #include <linux/jhash.h> 22 23 #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) 24 25 struct elfs { 26 struct elf *orig, *patched, *out; 27 const char *modname; 28 }; 29 30 struct export { 31 struct hlist_node hash; 32 char *mod, *sym; 33 }; 34 35 static const char * const klp_diff_usage[] = { 36 "objtool klp diff [<options>] <in1.o> <in2.o> <out.o>", 37 NULL, 38 }; 39 40 static const struct option klp_diff_options[] = { 41 OPT_GROUP("Options:"), 42 OPT_BOOLEAN('d', "debug", &debug, "enable debug output"), 43 OPT_END(), 44 }; 45 46 static DEFINE_HASHTABLE(exports, 15); 47 48 static inline u32 str_hash(const char *str) 49 { 50 return jhash(str, strlen(str), 0); 51 } 52 53 static char *escape_str(const char *orig) 54 { 55 size_t len = 0; 56 const char *a; 57 char *b, *new; 58 59 for (a = orig; *a; a++) { 60 switch (*a) { 61 case '\001': len += 5; break; 62 case '\n': 63 case '\t': len += 2; break; 64 default: len++; 65 } 66 } 67 68 new = malloc(len + 1); 69 if (!new) 70 return NULL; 71 72 for (a = orig, b = new; *a; a++) { 73 switch (*a) { 74 case '\001': memcpy(b, "<SOH>", 5); b += 5; break; 75 case '\n': *b++ = '\\'; *b++ = 'n'; break; 76 case '\t': *b++ = '\\'; *b++ = 't'; break; 77 default: *b++ = *a; 78 } 79 } 80 81 *b = '\0'; 82 return new; 83 } 84 85 static int read_exports(void) 86 { 87 const char *symvers = "Module.symvers"; 88 char line[1024], *path = NULL; 89 unsigned int line_num = 1; 90 FILE *file; 91 92 file = fopen(symvers, "r"); 93 if (!file) { 94 path = top_level_dir(symvers); 95 if (!path) { 96 ERROR("can't open '%s', \"objtool diff\" should be run from the kernel tree", symvers); 97 return -1; 98 } 99 100 file = fopen(path, "r"); 101 if (!file) { 102 ERROR_GLIBC("fopen"); 103 return -1; 104 } 105 } 106 107 while (fgets(line, 1024, file)) { 108 char *sym, *mod, *type; 109 struct export *export; 110 111 sym = strchr(line, '\t'); 112 if (!sym) { 113 ERROR("malformed Module.symvers (sym) at line %d", line_num); 114 return -1; 115 } 116 117 *sym++ = '\0'; 118 119 mod = strchr(sym, '\t'); 120 if (!mod) { 121 ERROR("malformed Module.symvers (mod) at line %d", line_num); 122 return -1; 123 } 124 125 *mod++ = '\0'; 126 127 type = strchr(mod, '\t'); 128 if (!type) { 129 ERROR("malformed Module.symvers (type) at line %d", line_num); 130 return -1; 131 } 132 133 *type++ = '\0'; 134 135 if (*sym == '\0' || *mod == '\0') { 136 ERROR("malformed Module.symvers at line %d", line_num); 137 return -1; 138 } 139 140 export = calloc(1, sizeof(*export)); 141 if (!export) { 142 ERROR_GLIBC("calloc"); 143 return -1; 144 } 145 146 export->mod = strdup(mod); 147 if (!export->mod) { 148 ERROR_GLIBC("strdup"); 149 return -1; 150 } 151 152 export->sym = strdup(sym); 153 if (!export->sym) { 154 ERROR_GLIBC("strdup"); 155 return -1; 156 } 157 158 hash_add(exports, &export->hash, str_hash(sym)); 159 } 160 161 free(path); 162 fclose(file); 163 164 return 0; 165 } 166 167 static int read_sym_checksums(struct elf *elf) 168 { 169 struct section *sec; 170 171 sec = find_section_by_name(elf, ".discard.sym_checksum"); 172 if (!sec) { 173 ERROR("'%s' missing .discard.sym_checksum section, file not processed by 'objtool --checksum'?", 174 elf->name); 175 return -1; 176 } 177 178 if (!sec->rsec) { 179 ERROR("missing reloc section for .discard.sym_checksum"); 180 return -1; 181 } 182 183 if (sec_size(sec) % sizeof(struct sym_checksum)) { 184 ERROR("struct sym_checksum size mismatch"); 185 return -1; 186 } 187 188 for (int i = 0; i < sec_size(sec) / sizeof(struct sym_checksum); i++) { 189 struct sym_checksum *sym_checksum; 190 struct reloc *reloc; 191 struct symbol *sym; 192 193 sym_checksum = (struct sym_checksum *)sec->data->d_buf + i; 194 195 reloc = find_reloc_by_dest(elf, sec, i * sizeof(*sym_checksum)); 196 if (!reloc) { 197 ERROR("can't find reloc for sym_checksum[%d]", i); 198 return -1; 199 } 200 201 sym = reloc->sym; 202 203 if (is_sec_sym(sym)) { 204 ERROR("not sure how to handle section %s", sym->name); 205 return -1; 206 } 207 208 if (is_func_sym(sym)) 209 sym->csum.checksum = sym_checksum->checksum; 210 } 211 212 return 0; 213 } 214 215 static struct symbol *first_file_symbol(struct elf *elf) 216 { 217 struct symbol *sym; 218 219 for_each_sym(elf, sym) { 220 if (is_file_sym(sym)) 221 return sym; 222 } 223 224 return NULL; 225 } 226 227 static struct symbol *next_file_symbol(struct elf *elf, struct symbol *sym) 228 { 229 for_each_sym_continue(elf, sym) { 230 if (is_file_sym(sym)) 231 return sym; 232 } 233 234 return NULL; 235 } 236 237 /* 238 * Certain static local variables should never be correlated. They will be 239 * used in place rather than referencing the originals. 240 */ 241 static bool is_uncorrelated_static_local(struct symbol *sym) 242 { 243 static const char * const vars[] = { 244 "__key.", 245 "__warned.", 246 "__already_done.", 247 "__func__.", 248 "_rs.", 249 "descriptor.", 250 "CSWTCH.", 251 }; 252 253 if (!is_object_sym(sym) || !is_local_sym(sym)) 254 return false; 255 256 if (!strcmp(sym->sec->name, ".data.once")) 257 return true; 258 259 for (int i = 0; i < ARRAY_SIZE(vars); i++) { 260 if (strstarts(sym->name, vars[i])) 261 return true; 262 } 263 264 return false; 265 } 266 267 /* 268 * Clang emits several useless .Ltmp_* code labels. 269 */ 270 static bool is_clang_tmp_label(struct symbol *sym) 271 { 272 return sym->type == STT_NOTYPE && 273 is_text_sec(sym->sec) && 274 strstarts(sym->name, ".Ltmp") && 275 isdigit(sym->name[5]); 276 } 277 278 static bool is_special_section(struct section *sec) 279 { 280 static const char * const specials[] = { 281 ".altinstructions", 282 ".smp_locks", 283 "__bug_table", 284 "__ex_table", 285 "__jump_table", 286 "__mcount_loc", 287 288 /* 289 * Extract .static_call_sites here to inherit non-module 290 * preferential treatment. The later static call processing 291 * during klp module build will be skipped when it sees this 292 * section already exists. 293 */ 294 ".static_call_sites", 295 }; 296 297 static const char * const non_special_discards[] = { 298 ".discard.addressable", 299 ".discard.sym_checksum", 300 }; 301 302 if (is_text_sec(sec)) 303 return false; 304 305 for (int i = 0; i < ARRAY_SIZE(specials); i++) { 306 if (!strcmp(sec->name, specials[i])) 307 return true; 308 } 309 310 /* Most .discard data sections are special */ 311 for (int i = 0; i < ARRAY_SIZE(non_special_discards); i++) { 312 if (!strcmp(sec->name, non_special_discards[i])) 313 return false; 314 } 315 316 return strstarts(sec->name, ".discard."); 317 } 318 319 /* 320 * These sections are referenced by special sections but aren't considered 321 * special sections themselves. 322 */ 323 static bool is_special_section_aux(struct section *sec) 324 { 325 static const char * const specials_aux[] = { 326 ".altinstr_replacement", 327 ".altinstr_aux", 328 }; 329 330 for (int i = 0; i < ARRAY_SIZE(specials_aux); i++) { 331 if (!strcmp(sec->name, specials_aux[i])) 332 return true; 333 } 334 335 return false; 336 } 337 338 /* 339 * These symbols should never be correlated, so their local patched versions 340 * are used instead of linking to the originals. 341 */ 342 static bool dont_correlate(struct symbol *sym) 343 { 344 return is_file_sym(sym) || 345 is_null_sym(sym) || 346 is_sec_sym(sym) || 347 is_prefix_func(sym) || 348 is_uncorrelated_static_local(sym) || 349 is_clang_tmp_label(sym) || 350 is_string_sec(sym->sec) || 351 is_special_section(sym->sec) || 352 is_special_section_aux(sym->sec) || 353 strstarts(sym->name, "__initcall__"); 354 } 355 356 /* 357 * For each symbol in the original kernel, find its corresponding "twin" in the 358 * patched kernel. 359 */ 360 static int correlate_symbols(struct elfs *e) 361 { 362 struct symbol *file1_sym, *file2_sym; 363 struct symbol *sym1, *sym2; 364 365 /* Correlate locals */ 366 for (file1_sym = first_file_symbol(e->orig), 367 file2_sym = first_file_symbol(e->patched); ; 368 file1_sym = next_file_symbol(e->orig, file1_sym), 369 file2_sym = next_file_symbol(e->patched, file2_sym)) { 370 371 if (!file1_sym && file2_sym) { 372 ERROR("FILE symbol mismatch: NULL != %s", file2_sym->name); 373 return -1; 374 } 375 376 if (file1_sym && !file2_sym) { 377 ERROR("FILE symbol mismatch: %s != NULL", file1_sym->name); 378 return -1; 379 } 380 381 if (!file1_sym) 382 break; 383 384 if (strcmp(file1_sym->name, file2_sym->name)) { 385 ERROR("FILE symbol mismatch: %s != %s", file1_sym->name, file2_sym->name); 386 return -1; 387 } 388 389 file1_sym->twin = file2_sym; 390 file2_sym->twin = file1_sym; 391 392 sym1 = file1_sym; 393 394 for_each_sym_continue(e->orig, sym1) { 395 if (is_file_sym(sym1) || !is_local_sym(sym1)) 396 break; 397 398 if (dont_correlate(sym1)) 399 continue; 400 401 sym2 = file2_sym; 402 for_each_sym_continue(e->patched, sym2) { 403 if (is_file_sym(sym2) || !is_local_sym(sym2)) 404 break; 405 406 if (sym2->twin || dont_correlate(sym2)) 407 continue; 408 409 if (strcmp(sym1->demangled_name, sym2->demangled_name)) 410 continue; 411 412 sym1->twin = sym2; 413 sym2->twin = sym1; 414 break; 415 } 416 } 417 } 418 419 /* Correlate globals */ 420 for_each_sym(e->orig, sym1) { 421 if (sym1->bind == STB_LOCAL) 422 continue; 423 424 sym2 = find_global_symbol_by_name(e->patched, sym1->name); 425 426 if (sym2 && !sym2->twin && !strcmp(sym1->name, sym2->name)) { 427 sym1->twin = sym2; 428 sym2->twin = sym1; 429 } 430 } 431 432 for_each_sym(e->orig, sym1) { 433 if (sym1->twin || dont_correlate(sym1)) 434 continue; 435 WARN("no correlation: %s", sym1->name); 436 } 437 438 return 0; 439 } 440 441 /* "sympos" is used by livepatch to disambiguate duplicate symbol names */ 442 static unsigned long find_sympos(struct elf *elf, struct symbol *sym) 443 { 444 bool vmlinux = str_ends_with(objname, "vmlinux.o"); 445 unsigned long sympos = 0, nr_matches = 0; 446 bool has_dup = false; 447 struct symbol *s; 448 449 if (sym->bind != STB_LOCAL) 450 return 0; 451 452 if (vmlinux && sym->type == STT_FUNC) { 453 /* 454 * HACK: Unfortunately, symbol ordering can differ between 455 * vmlinux.o and vmlinux due to the linker script emitting 456 * .text.unlikely* before .text*. Count .text.unlikely* first. 457 * 458 * TODO: Disambiguate symbols more reliably (checksums?) 459 */ 460 for_each_sym(elf, s) { 461 if (strstarts(s->sec->name, ".text.unlikely") && 462 !strcmp(s->name, sym->name)) { 463 nr_matches++; 464 if (s == sym) 465 sympos = nr_matches; 466 else 467 has_dup = true; 468 } 469 } 470 for_each_sym(elf, s) { 471 if (!strstarts(s->sec->name, ".text.unlikely") && 472 !strcmp(s->name, sym->name)) { 473 nr_matches++; 474 if (s == sym) 475 sympos = nr_matches; 476 else 477 has_dup = true; 478 } 479 } 480 } else { 481 for_each_sym(elf, s) { 482 if (!strcmp(s->name, sym->name)) { 483 nr_matches++; 484 if (s == sym) 485 sympos = nr_matches; 486 else 487 has_dup = true; 488 } 489 } 490 } 491 492 if (!sympos) { 493 ERROR("can't find sympos for %s", sym->name); 494 return ULONG_MAX; 495 } 496 497 return has_dup ? sympos : 0; 498 } 499 500 static int clone_sym_relocs(struct elfs *e, struct symbol *patched_sym); 501 502 static struct symbol *__clone_symbol(struct elf *elf, struct symbol *patched_sym, 503 bool data_too) 504 { 505 struct section *out_sec = NULL; 506 unsigned long offset = 0; 507 struct symbol *out_sym; 508 509 if (data_too && !is_undef_sym(patched_sym)) { 510 struct section *patched_sec = patched_sym->sec; 511 512 out_sec = find_section_by_name(elf, patched_sec->name); 513 if (!out_sec) { 514 out_sec = elf_create_section(elf, patched_sec->name, 0, 515 patched_sec->sh.sh_entsize, 516 patched_sec->sh.sh_type, 517 patched_sec->sh.sh_addralign, 518 patched_sec->sh.sh_flags); 519 if (!out_sec) 520 return NULL; 521 } 522 523 if (is_string_sec(patched_sym->sec)) { 524 out_sym = elf_create_section_symbol(elf, out_sec); 525 if (!out_sym) 526 return NULL; 527 528 goto sym_created; 529 } 530 531 if (!is_sec_sym(patched_sym)) 532 offset = sec_size(out_sec); 533 534 if (patched_sym->len || is_sec_sym(patched_sym)) { 535 void *data = NULL; 536 size_t size; 537 538 /* bss doesn't have data */ 539 if (patched_sym->sec->data->d_buf) 540 data = patched_sym->sec->data->d_buf + patched_sym->offset; 541 542 if (is_sec_sym(patched_sym)) 543 size = sec_size(patched_sym->sec); 544 else 545 size = patched_sym->len; 546 547 if (!elf_add_data(elf, out_sec, data, size)) 548 return NULL; 549 } 550 } 551 552 out_sym = elf_create_symbol(elf, patched_sym->name, out_sec, 553 patched_sym->bind, patched_sym->type, 554 offset, patched_sym->len); 555 if (!out_sym) 556 return NULL; 557 558 sym_created: 559 patched_sym->clone = out_sym; 560 out_sym->clone = patched_sym; 561 562 return out_sym; 563 } 564 565 static const char *sym_type(struct symbol *sym) 566 { 567 switch (sym->type) { 568 case STT_NOTYPE: return "NOTYPE"; 569 case STT_OBJECT: return "OBJECT"; 570 case STT_FUNC: return "FUNC"; 571 case STT_SECTION: return "SECTION"; 572 case STT_FILE: return "FILE"; 573 default: return "UNKNOWN"; 574 } 575 } 576 577 static const char *sym_bind(struct symbol *sym) 578 { 579 switch (sym->bind) { 580 case STB_LOCAL: return "LOCAL"; 581 case STB_GLOBAL: return "GLOBAL"; 582 case STB_WEAK: return "WEAK"; 583 default: return "UNKNOWN"; 584 } 585 } 586 587 /* 588 * Copy a symbol to the output object, optionally including its data and 589 * relocations. 590 */ 591 static struct symbol *clone_symbol(struct elfs *e, struct symbol *patched_sym, 592 bool data_too) 593 { 594 struct symbol *pfx; 595 596 if (patched_sym->clone) 597 return patched_sym->clone; 598 599 dbg_indent("%s%s", patched_sym->name, data_too ? " [+DATA]" : ""); 600 601 /* Make sure the prefix gets cloned first */ 602 if (is_func_sym(patched_sym) && data_too) { 603 pfx = get_func_prefix(patched_sym); 604 if (pfx) 605 clone_symbol(e, pfx, true); 606 } 607 608 if (!__clone_symbol(e->out, patched_sym, data_too)) 609 return NULL; 610 611 if (data_too && clone_sym_relocs(e, patched_sym)) 612 return NULL; 613 614 return patched_sym->clone; 615 } 616 617 static void mark_included_function(struct symbol *func) 618 { 619 struct symbol *pfx; 620 621 func->included = 1; 622 623 /* Include prefix function */ 624 pfx = get_func_prefix(func); 625 if (pfx) 626 pfx->included = 1; 627 628 /* Make sure .cold parent+child always stay together */ 629 if (func->cfunc && func->cfunc != func) 630 func->cfunc->included = 1; 631 if (func->pfunc && func->pfunc != func) 632 func->pfunc->included = 1; 633 } 634 635 /* 636 * Copy all changed functions (and their dependencies) from the patched object 637 * to the output object. 638 */ 639 static int mark_changed_functions(struct elfs *e) 640 { 641 struct symbol *sym_orig, *patched_sym; 642 bool changed = false; 643 644 /* Find changed functions */ 645 for_each_sym(e->orig, sym_orig) { 646 if (!is_func_sym(sym_orig) || is_prefix_func(sym_orig)) 647 continue; 648 649 patched_sym = sym_orig->twin; 650 if (!patched_sym) 651 continue; 652 653 if (sym_orig->csum.checksum != patched_sym->csum.checksum) { 654 patched_sym->changed = 1; 655 mark_included_function(patched_sym); 656 changed = true; 657 } 658 } 659 660 /* Find added functions and print them */ 661 for_each_sym(e->patched, patched_sym) { 662 if (!is_func_sym(patched_sym) || is_prefix_func(patched_sym)) 663 continue; 664 665 if (!patched_sym->twin) { 666 printf("%s: new function: %s\n", objname, patched_sym->name); 667 mark_included_function(patched_sym); 668 changed = true; 669 } 670 } 671 672 /* Print changed functions */ 673 for_each_sym(e->patched, patched_sym) { 674 if (patched_sym->changed) 675 printf("%s: changed function: %s\n", objname, patched_sym->name); 676 } 677 678 return !changed ? -1 : 0; 679 } 680 681 static int clone_included_functions(struct elfs *e) 682 { 683 struct symbol *patched_sym; 684 685 for_each_sym(e->patched, patched_sym) { 686 if (patched_sym->included) { 687 if (!clone_symbol(e, patched_sym, true)) 688 return -1; 689 } 690 } 691 692 return 0; 693 } 694 695 /* 696 * Determine whether a relocation should reference the section rather than the 697 * underlying symbol. 698 */ 699 static bool section_reference_needed(struct section *sec) 700 { 701 /* 702 * String symbols are zero-length and uncorrelated. It's easier to 703 * deal with them as section symbols. 704 */ 705 if (is_string_sec(sec)) 706 return true; 707 708 /* 709 * .rodata has mostly anonymous data so there's no way to determine the 710 * length of a needed reference. just copy the whole section if needed. 711 */ 712 if (strstarts(sec->name, ".rodata")) 713 return true; 714 715 /* UBSAN anonymous data */ 716 if (strstarts(sec->name, ".data..Lubsan") || /* GCC */ 717 strstarts(sec->name, ".data..L__unnamed_")) /* Clang */ 718 return true; 719 720 return false; 721 } 722 723 static bool is_reloc_allowed(struct reloc *reloc) 724 { 725 return section_reference_needed(reloc->sym->sec) == is_sec_sym(reloc->sym); 726 } 727 728 static struct export *find_export(struct symbol *sym) 729 { 730 struct export *export; 731 732 hash_for_each_possible(exports, export, hash, str_hash(sym->name)) { 733 if (!strcmp(export->sym, sym->name)) 734 return export; 735 } 736 737 return NULL; 738 } 739 740 static const char *__find_modname(struct elfs *e) 741 { 742 struct section *sec; 743 char *name; 744 745 sec = find_section_by_name(e->orig, ".modinfo"); 746 if (!sec) { 747 ERROR("missing .modinfo section"); 748 return NULL; 749 } 750 751 name = memmem(sec->data->d_buf, sec_size(sec), "\0name=", 6); 752 if (name) 753 return name + 6; 754 755 name = strdup(e->orig->name); 756 if (!name) { 757 ERROR_GLIBC("strdup"); 758 return NULL; 759 } 760 761 for (char *c = name; *c; c++) { 762 if (*c == '/') 763 name = c + 1; 764 else if (*c == '-') 765 *c = '_'; 766 else if (*c == '.') { 767 *c = '\0'; 768 break; 769 } 770 } 771 772 return name; 773 } 774 775 /* Get the object's module name as defined by the kernel (and klp_object) */ 776 static const char *find_modname(struct elfs *e) 777 { 778 const char *modname; 779 780 if (e->modname) 781 return e->modname; 782 783 modname = __find_modname(e); 784 e->modname = modname; 785 return modname; 786 } 787 788 /* 789 * Copying a function from its native compiled environment to a kernel module 790 * removes its natural access to local functions/variables and unexported 791 * globals. References to such symbols need to be converted to KLP relocs so 792 * the kernel arch relocation code knows to apply them and where to find the 793 * symbols. Particularly, duplicate static symbols need to be disambiguated. 794 */ 795 static bool klp_reloc_needed(struct reloc *patched_reloc) 796 { 797 struct symbol *patched_sym = patched_reloc->sym; 798 struct export *export; 799 800 /* no external symbol to reference */ 801 if (dont_correlate(patched_sym)) 802 return false; 803 804 /* For included functions, a regular reloc will do. */ 805 if (patched_sym->included) 806 return false; 807 808 /* 809 * If exported by a module, it has to be a klp reloc. Thanks to the 810 * clusterfunk that is late module patching, the patch module is 811 * allowed to be loaded before any modules it depends on. 812 * 813 * If exported by vmlinux, a normal reloc will do. 814 */ 815 export = find_export(patched_sym); 816 if (export) 817 return strcmp(export->mod, "vmlinux"); 818 819 if (!patched_sym->twin) { 820 /* 821 * Presumably the symbol and its reference were added by the 822 * patch. The symbol could be defined in this .o or in another 823 * .o in the patch module. 824 * 825 * This check needs to be *after* the export check due to the 826 * possibility of the patch adding a new UNDEF reference to an 827 * exported symbol. 828 */ 829 return false; 830 } 831 832 /* Unexported symbol which lives in the original vmlinux or module. */ 833 return true; 834 } 835 836 static int convert_reloc_sym_to_secsym(struct elf *elf, struct reloc *reloc) 837 { 838 struct symbol *sym = reloc->sym; 839 struct section *sec = sym->sec; 840 841 if (!sec->sym && !elf_create_section_symbol(elf, sec)) 842 return -1; 843 844 reloc->sym = sec->sym; 845 set_reloc_sym(elf, reloc, sym->idx); 846 set_reloc_addend(elf, reloc, sym->offset + reloc_addend(reloc)); 847 return 0; 848 } 849 850 static int convert_reloc_secsym_to_sym(struct elf *elf, struct reloc *reloc) 851 { 852 struct symbol *sym = reloc->sym; 853 struct section *sec = sym->sec; 854 855 /* If the symbol has a dedicated section, it's easy to find */ 856 sym = find_symbol_by_offset(sec, 0); 857 if (sym && sym->len == sec_size(sec)) 858 goto found_sym; 859 860 /* No dedicated section; find the symbol manually */ 861 sym = find_symbol_containing(sec, arch_adjusted_addend(reloc)); 862 if (!sym) { 863 /* 864 * This can happen for special section references to weak code 865 * whose symbol has been stripped by the linker. 866 */ 867 return -1; 868 } 869 870 found_sym: 871 reloc->sym = sym; 872 set_reloc_sym(elf, reloc, sym->idx); 873 set_reloc_addend(elf, reloc, reloc_addend(reloc) - sym->offset); 874 return 0; 875 } 876 877 /* 878 * Convert a relocation symbol reference to the needed format: either a section 879 * symbol or the underlying symbol itself. 880 */ 881 static int convert_reloc_sym(struct elf *elf, struct reloc *reloc) 882 { 883 if (is_reloc_allowed(reloc)) 884 return 0; 885 886 if (section_reference_needed(reloc->sym->sec)) 887 return convert_reloc_sym_to_secsym(elf, reloc); 888 else 889 return convert_reloc_secsym_to_sym(elf, reloc); 890 } 891 892 /* 893 * Convert a regular relocation to a klp relocation (sort of). 894 */ 895 static int clone_reloc_klp(struct elfs *e, struct reloc *patched_reloc, 896 struct section *sec, unsigned long offset, 897 struct export *export) 898 { 899 struct symbol *patched_sym = patched_reloc->sym; 900 s64 addend = reloc_addend(patched_reloc); 901 const char *sym_modname, *sym_orig_name; 902 static struct section *klp_relocs; 903 struct symbol *sym, *klp_sym; 904 unsigned long klp_reloc_off; 905 char sym_name[SYM_NAME_LEN]; 906 struct klp_reloc klp_reloc; 907 unsigned long sympos; 908 909 if (!patched_sym->twin) { 910 ERROR("unexpected klp reloc for new symbol %s", patched_sym->name); 911 return -1; 912 } 913 914 /* 915 * Keep the original reloc intact for now to avoid breaking objtool run 916 * which relies on proper relocations for many of its features. This 917 * will be disabled later by "objtool klp post-link". 918 * 919 * Convert it to UNDEF (and WEAK to avoid modpost warnings). 920 */ 921 922 sym = patched_sym->clone; 923 if (!sym) { 924 /* STB_WEAK: avoid modpost undefined symbol warnings */ 925 sym = elf_create_symbol(e->out, patched_sym->name, NULL, 926 STB_WEAK, patched_sym->type, 0, 0); 927 if (!sym) 928 return -1; 929 930 patched_sym->clone = sym; 931 sym->clone = patched_sym; 932 } 933 934 if (!elf_create_reloc(e->out, sec, offset, sym, addend, reloc_type(patched_reloc))) 935 return -1; 936 937 /* 938 * Create the KLP symbol. 939 */ 940 941 if (export) { 942 sym_modname = export->mod; 943 sym_orig_name = export->sym; 944 sympos = 0; 945 } else { 946 sym_modname = find_modname(e); 947 if (!sym_modname) 948 return -1; 949 950 sym_orig_name = patched_sym->twin->name; 951 sympos = find_sympos(e->orig, patched_sym->twin); 952 if (sympos == ULONG_MAX) 953 return -1; 954 } 955 956 /* symbol format: .klp.sym.modname.sym_name,sympos */ 957 if (snprintf_check(sym_name, SYM_NAME_LEN, KLP_SYM_PREFIX "%s.%s,%ld", 958 sym_modname, sym_orig_name, sympos)) 959 return -1; 960 961 klp_sym = find_symbol_by_name(e->out, sym_name); 962 if (!klp_sym) { 963 __dbg_indent("%s", sym_name); 964 965 /* STB_WEAK: avoid modpost undefined symbol warnings */ 966 klp_sym = elf_create_symbol(e->out, sym_name, NULL, 967 STB_WEAK, patched_sym->type, 0, 0); 968 if (!klp_sym) 969 return -1; 970 } 971 972 /* 973 * Create the __klp_relocs entry. This will be converted to an actual 974 * KLP rela by "objtool klp post-link". 975 * 976 * This intermediate step is necessary to prevent corruption by the 977 * linker, which doesn't know how to properly handle two rela sections 978 * applying to the same base section. 979 */ 980 981 if (!klp_relocs) { 982 klp_relocs = elf_create_section(e->out, KLP_RELOCS_SEC, 0, 983 0, SHT_PROGBITS, 8, SHF_ALLOC); 984 if (!klp_relocs) 985 return -1; 986 } 987 988 klp_reloc_off = sec_size(klp_relocs); 989 memset(&klp_reloc, 0, sizeof(klp_reloc)); 990 991 klp_reloc.type = reloc_type(patched_reloc); 992 if (!elf_add_data(e->out, klp_relocs, &klp_reloc, sizeof(klp_reloc))) 993 return -1; 994 995 /* klp_reloc.offset */ 996 if (!sec->sym && !elf_create_section_symbol(e->out, sec)) 997 return -1; 998 999 if (!elf_create_reloc(e->out, klp_relocs, 1000 klp_reloc_off + offsetof(struct klp_reloc, offset), 1001 sec->sym, offset, R_ABS64)) 1002 return -1; 1003 1004 /* klp_reloc.sym */ 1005 if (!elf_create_reloc(e->out, klp_relocs, 1006 klp_reloc_off + offsetof(struct klp_reloc, sym), 1007 klp_sym, addend, R_ABS64)) 1008 return -1; 1009 1010 return 0; 1011 } 1012 1013 #define dbg_clone_reloc(sec, offset, patched_sym, addend, export, klp) \ 1014 dbg_indent("%s+0x%lx: %s%s0x%lx [%s%s%s%s%s%s]", \ 1015 sec->name, offset, patched_sym->name, \ 1016 addend >= 0 ? "+" : "-", labs(addend), \ 1017 sym_type(patched_sym), \ 1018 patched_sym->type == STT_SECTION ? "" : " ", \ 1019 patched_sym->type == STT_SECTION ? "" : sym_bind(patched_sym), \ 1020 is_undef_sym(patched_sym) ? " UNDEF" : "", \ 1021 export ? " EXPORTED" : "", \ 1022 klp ? " KLP" : "") 1023 1024 /* Copy a reloc and its symbol to the output object */ 1025 static int clone_reloc(struct elfs *e, struct reloc *patched_reloc, 1026 struct section *sec, unsigned long offset) 1027 { 1028 struct symbol *patched_sym = patched_reloc->sym; 1029 struct export *export = find_export(patched_sym); 1030 long addend = reloc_addend(patched_reloc); 1031 struct symbol *out_sym; 1032 bool klp; 1033 1034 if (!is_reloc_allowed(patched_reloc)) { 1035 ERROR_FUNC(patched_reloc->sec->base, reloc_offset(patched_reloc), 1036 "missing symbol for reference to %s+%ld", 1037 patched_sym->name, addend); 1038 return -1; 1039 } 1040 1041 klp = klp_reloc_needed(patched_reloc); 1042 1043 dbg_clone_reloc(sec, offset, patched_sym, addend, export, klp); 1044 1045 if (klp) { 1046 if (clone_reloc_klp(e, patched_reloc, sec, offset, export)) 1047 return -1; 1048 1049 return 0; 1050 } 1051 1052 /* 1053 * Why !export sets 'data_too': 1054 * 1055 * Unexported non-klp symbols need to live in the patch module, 1056 * otherwise there will be unresolved symbols. Notably, this includes: 1057 * 1058 * - New functions/data 1059 * - String sections 1060 * - Special section entries 1061 * - Uncorrelated static local variables 1062 * - UBSAN sections 1063 */ 1064 out_sym = clone_symbol(e, patched_sym, patched_sym->included || !export); 1065 if (!out_sym) 1066 return -1; 1067 1068 /* 1069 * For strings, all references use section symbols, thanks to 1070 * section_reference_needed(). clone_symbol() has cloned an empty 1071 * version of the string section. Now copy the string itself. 1072 */ 1073 if (is_string_sec(patched_sym->sec)) { 1074 const char *str = patched_sym->sec->data->d_buf + addend; 1075 1076 __dbg_indent("\"%s\"", escape_str(str)); 1077 1078 addend = elf_add_string(e->out, out_sym->sec, str); 1079 if (addend == -1) 1080 return -1; 1081 } 1082 1083 if (!elf_create_reloc(e->out, sec, offset, out_sym, addend, 1084 reloc_type(patched_reloc))) 1085 return -1; 1086 1087 return 0; 1088 } 1089 1090 /* Copy all relocs needed for a symbol's contents */ 1091 static int clone_sym_relocs(struct elfs *e, struct symbol *patched_sym) 1092 { 1093 struct section *patched_rsec = patched_sym->sec->rsec; 1094 struct reloc *patched_reloc; 1095 unsigned long start, end; 1096 struct symbol *out_sym; 1097 1098 out_sym = patched_sym->clone; 1099 if (!out_sym) { 1100 ERROR("no clone for %s", patched_sym->name); 1101 return -1; 1102 } 1103 1104 if (!patched_rsec) 1105 return 0; 1106 1107 if (!is_sec_sym(patched_sym) && !patched_sym->len) 1108 return 0; 1109 1110 if (is_string_sec(patched_sym->sec)) 1111 return 0; 1112 1113 if (is_sec_sym(patched_sym)) { 1114 start = 0; 1115 end = sec_size(patched_sym->sec); 1116 } else { 1117 start = patched_sym->offset; 1118 end = start + patched_sym->len; 1119 } 1120 1121 for_each_reloc(patched_rsec, patched_reloc) { 1122 unsigned long offset; 1123 1124 if (reloc_offset(patched_reloc) < start || 1125 reloc_offset(patched_reloc) >= end) 1126 continue; 1127 1128 /* 1129 * Skip any reloc referencing .altinstr_aux. Its code is 1130 * always patched by alternatives. See ALTERNATIVE_TERNARY(). 1131 */ 1132 if (patched_reloc->sym->sec && 1133 !strcmp(patched_reloc->sym->sec->name, ".altinstr_aux")) 1134 continue; 1135 1136 if (convert_reloc_sym(e->patched, patched_reloc)) { 1137 ERROR_FUNC(patched_rsec->base, reloc_offset(patched_reloc), 1138 "failed to convert reloc sym '%s' to its proper format", 1139 patched_reloc->sym->name); 1140 return -1; 1141 } 1142 1143 offset = out_sym->offset + (reloc_offset(patched_reloc) - patched_sym->offset); 1144 1145 if (clone_reloc(e, patched_reloc, out_sym->sec, offset)) 1146 return -1; 1147 } 1148 return 0; 1149 1150 } 1151 1152 static int create_fake_symbol(struct elf *elf, struct section *sec, 1153 unsigned long offset, size_t size) 1154 { 1155 char name[SYM_NAME_LEN]; 1156 unsigned int type; 1157 static int ctr; 1158 char *c; 1159 1160 if (snprintf_check(name, SYM_NAME_LEN, "%s_%d", sec->name, ctr++)) 1161 return -1; 1162 1163 for (c = name; *c; c++) 1164 if (*c == '.') 1165 *c = '_'; 1166 1167 /* 1168 * STT_NOTYPE: Prevent objtool from validating .altinstr_replacement 1169 * while still allowing objdump to disassemble it. 1170 */ 1171 type = is_text_sec(sec) ? STT_NOTYPE : STT_OBJECT; 1172 return elf_create_symbol(elf, name, sec, STB_LOCAL, type, offset, size) ? 0 : -1; 1173 } 1174 1175 /* 1176 * Special sections (alternatives, etc) are basically arrays of structs. 1177 * For all the special sections, create a symbol for each struct entry. This 1178 * is a bit cumbersome, but it makes the extracting of the individual entries 1179 * much more straightforward. 1180 * 1181 * There are three ways to identify the entry sizes for a special section: 1182 * 1183 * 1) ELF section header sh_entsize: Ideally this would be used almost 1184 * everywhere. But unfortunately the toolchains make it difficult. The 1185 * assembler .[push]section directive syntax only takes entsize when 1186 * combined with SHF_MERGE. But Clang disallows combining SHF_MERGE with 1187 * SHF_WRITE. And some special sections do need to be writable. 1188 * 1189 * Another place this wouldn't work is .altinstr_replacement, whose entries 1190 * don't have a fixed size. 1191 * 1192 * 2) ANNOTATE_DATA_SPECIAL: This is a lightweight objtool annotation which 1193 * points to the beginning of each entry. The size of the entry is then 1194 * inferred by the location of the subsequent annotation (or end of 1195 * section). 1196 * 1197 * 3) Simple array of pointers: If the special section is just a basic array of 1198 * pointers, the entry size can be inferred by the number of relocations. 1199 * No annotations needed. 1200 * 1201 * Note I also tried to create per-entry symbols at the time of creation, in 1202 * the original [inline] asm. Unfortunately, creating uniquely named symbols 1203 * is trickier than one might think, especially with Clang inline asm. I 1204 * eventually just gave up trying to make that work, in favor of using 1205 * ANNOTATE_DATA_SPECIAL and creating the symbols here after the fact. 1206 */ 1207 static int create_fake_symbols(struct elf *elf) 1208 { 1209 struct section *sec; 1210 struct reloc *reloc; 1211 1212 /* 1213 * 1) Make symbols for all the ANNOTATE_DATA_SPECIAL entries: 1214 */ 1215 1216 sec = find_section_by_name(elf, ".discard.annotate_data"); 1217 if (!sec || !sec->rsec) 1218 return 0; 1219 1220 for_each_reloc(sec->rsec, reloc) { 1221 unsigned long offset, size; 1222 struct reloc *next_reloc; 1223 1224 if (annotype(elf, sec, reloc) != ANNOTYPE_DATA_SPECIAL) 1225 continue; 1226 1227 offset = reloc_addend(reloc); 1228 1229 size = 0; 1230 next_reloc = reloc; 1231 for_each_reloc_continue(sec->rsec, next_reloc) { 1232 if (annotype(elf, sec, next_reloc) != ANNOTYPE_DATA_SPECIAL || 1233 next_reloc->sym->sec != reloc->sym->sec) 1234 continue; 1235 1236 size = reloc_addend(next_reloc) - offset; 1237 break; 1238 } 1239 1240 if (!size) 1241 size = sec_size(reloc->sym->sec) - offset; 1242 1243 if (create_fake_symbol(elf, reloc->sym->sec, offset, size)) 1244 return -1; 1245 } 1246 1247 /* 1248 * 2) Make symbols for sh_entsize, and simple arrays of pointers: 1249 */ 1250 1251 for_each_sec(elf, sec) { 1252 unsigned int entry_size; 1253 unsigned long offset; 1254 1255 if (!is_special_section(sec) || find_symbol_by_offset(sec, 0)) 1256 continue; 1257 1258 if (!sec->rsec) { 1259 ERROR("%s: missing special section relocations", sec->name); 1260 return -1; 1261 } 1262 1263 entry_size = sec->sh.sh_entsize; 1264 if (!entry_size) { 1265 entry_size = arch_reloc_size(sec->rsec->relocs); 1266 if (sec_size(sec) != entry_size * sec_num_entries(sec->rsec)) { 1267 ERROR("%s: missing special section entsize or annotations", sec->name); 1268 return -1; 1269 } 1270 } 1271 1272 for (offset = 0; offset < sec_size(sec); offset += entry_size) { 1273 if (create_fake_symbol(elf, sec, offset, entry_size)) 1274 return -1; 1275 } 1276 } 1277 1278 return 0; 1279 } 1280 1281 /* Keep a special section entry if it references an included function */ 1282 static bool should_keep_special_sym(struct elf *elf, struct symbol *sym) 1283 { 1284 struct reloc *reloc; 1285 1286 if (is_sec_sym(sym) || !sym->sec->rsec) 1287 return false; 1288 1289 sym_for_each_reloc(elf, sym, reloc) { 1290 if (convert_reloc_sym(elf, reloc)) 1291 continue; 1292 1293 if (is_func_sym(reloc->sym) && reloc->sym->included) 1294 return true; 1295 } 1296 1297 return false; 1298 } 1299 1300 /* 1301 * Klp relocations aren't allowed for __jump_table and .static_call_sites if 1302 * the referenced symbol lives in a kernel module, because such klp relocs may 1303 * be applied after static branch/call init, resulting in code corruption. 1304 * 1305 * Validate a special section entry to avoid that. Note that an inert 1306 * tracepoint is harmless enough, in that case just skip the entry and print a 1307 * warning. Otherwise, return an error. 1308 * 1309 * This is only a temporary limitation which will be fixed when livepatch adds 1310 * support for submodules: fully self-contained modules which are embedded in 1311 * the top-level livepatch module's data and which can be loaded on demand when 1312 * their corresponding to-be-patched module gets loaded. Then klp relocs can 1313 * be retired. 1314 * 1315 * Return: 1316 * -1: error: validation failed 1317 * 1: warning: tracepoint skipped 1318 * 0: success 1319 */ 1320 static int validate_special_section_klp_reloc(struct elfs *e, struct symbol *sym) 1321 { 1322 bool static_branch = !strcmp(sym->sec->name, "__jump_table"); 1323 bool static_call = !strcmp(sym->sec->name, ".static_call_sites"); 1324 struct symbol *code_sym = NULL; 1325 unsigned long code_offset = 0; 1326 struct reloc *reloc; 1327 int ret = 0; 1328 1329 if (!static_branch && !static_call) 1330 return 0; 1331 1332 sym_for_each_reloc(e->patched, sym, reloc) { 1333 const char *sym_modname; 1334 struct export *export; 1335 1336 /* Static branch/call keys are always STT_OBJECT */ 1337 if (reloc->sym->type != STT_OBJECT) { 1338 1339 /* Save code location which can be printed below */ 1340 if (reloc->sym->type == STT_FUNC && !code_sym) { 1341 code_sym = reloc->sym; 1342 code_offset = reloc_addend(reloc); 1343 } 1344 1345 continue; 1346 } 1347 1348 if (!klp_reloc_needed(reloc)) 1349 continue; 1350 1351 export = find_export(reloc->sym); 1352 if (export) { 1353 sym_modname = export->mod; 1354 } else { 1355 sym_modname = find_modname(e); 1356 if (!sym_modname) 1357 return -1; 1358 } 1359 1360 /* vmlinux keys are ok */ 1361 if (!strcmp(sym_modname, "vmlinux")) 1362 continue; 1363 1364 if (static_branch) { 1365 if (strstarts(reloc->sym->name, "__tracepoint_")) { 1366 WARN("%s: disabling unsupported tracepoint %s", 1367 code_sym->name, reloc->sym->name + 13); 1368 ret = 1; 1369 continue; 1370 } 1371 1372 ERROR("%s+0x%lx: unsupported static branch key %s. Use static_key_enabled() instead", 1373 code_sym->name, code_offset, reloc->sym->name); 1374 return -1; 1375 } 1376 1377 /* static call */ 1378 if (strstarts(reloc->sym->name, "__SCK__tp_func_")) { 1379 ret = 1; 1380 continue; 1381 } 1382 1383 ERROR("%s()+0x%lx: unsupported static call key %s. Use KLP_STATIC_CALL() instead", 1384 code_sym->name, code_offset, reloc->sym->name); 1385 return -1; 1386 } 1387 1388 return ret; 1389 } 1390 1391 static int clone_special_section(struct elfs *e, struct section *patched_sec) 1392 { 1393 struct symbol *patched_sym; 1394 1395 /* 1396 * Extract all special section symbols (and their dependencies) which 1397 * reference included functions. 1398 */ 1399 sec_for_each_sym(patched_sec, patched_sym) { 1400 int ret; 1401 1402 if (!is_object_sym(patched_sym)) 1403 continue; 1404 1405 if (!should_keep_special_sym(e->patched, patched_sym)) 1406 continue; 1407 1408 ret = validate_special_section_klp_reloc(e, patched_sym); 1409 if (ret < 0) 1410 return -1; 1411 if (ret > 0) 1412 continue; 1413 1414 if (!clone_symbol(e, patched_sym, true)) 1415 return -1; 1416 } 1417 1418 return 0; 1419 } 1420 1421 /* Extract only the needed bits from special sections */ 1422 static int clone_special_sections(struct elfs *e) 1423 { 1424 struct section *patched_sec; 1425 1426 if (create_fake_symbols(e->patched)) 1427 return -1; 1428 1429 for_each_sec(e->patched, patched_sec) { 1430 if (is_special_section(patched_sec)) { 1431 if (clone_special_section(e, patched_sec)) 1432 return -1; 1433 } 1434 } 1435 1436 return 0; 1437 } 1438 1439 /* 1440 * Create __klp_objects and __klp_funcs sections which are intermediate 1441 * sections provided as input to the patch module's init code for building the 1442 * klp_patch, klp_object and klp_func structs for the livepatch API. 1443 */ 1444 static int create_klp_sections(struct elfs *e) 1445 { 1446 size_t obj_size = sizeof(struct klp_object_ext); 1447 size_t func_size = sizeof(struct klp_func_ext); 1448 struct section *obj_sec, *funcs_sec, *str_sec; 1449 struct symbol *funcs_sym, *str_sym, *sym; 1450 char sym_name[SYM_NAME_LEN]; 1451 unsigned int nr_funcs = 0; 1452 const char *modname; 1453 void *obj_data; 1454 s64 addend; 1455 1456 obj_sec = elf_create_section_pair(e->out, KLP_OBJECTS_SEC, obj_size, 0, 0); 1457 if (!obj_sec) 1458 return -1; 1459 1460 funcs_sec = elf_create_section_pair(e->out, KLP_FUNCS_SEC, func_size, 0, 0); 1461 if (!funcs_sec) 1462 return -1; 1463 1464 funcs_sym = elf_create_section_symbol(e->out, funcs_sec); 1465 if (!funcs_sym) 1466 return -1; 1467 1468 str_sec = elf_create_section(e->out, KLP_STRINGS_SEC, 0, 0, 1469 SHT_PROGBITS, 1, 1470 SHF_ALLOC | SHF_STRINGS | SHF_MERGE); 1471 if (!str_sec) 1472 return -1; 1473 1474 if (elf_add_string(e->out, str_sec, "") == -1) 1475 return -1; 1476 1477 str_sym = elf_create_section_symbol(e->out, str_sec); 1478 if (!str_sym) 1479 return -1; 1480 1481 /* allocate klp_object_ext */ 1482 obj_data = elf_add_data(e->out, obj_sec, NULL, obj_size); 1483 if (!obj_data) 1484 return -1; 1485 1486 modname = find_modname(e); 1487 if (!modname) 1488 return -1; 1489 1490 /* klp_object_ext.name */ 1491 if (strcmp(modname, "vmlinux")) { 1492 addend = elf_add_string(e->out, str_sec, modname); 1493 if (addend == -1) 1494 return -1; 1495 1496 if (!elf_create_reloc(e->out, obj_sec, 1497 offsetof(struct klp_object_ext, name), 1498 str_sym, addend, R_ABS64)) 1499 return -1; 1500 } 1501 1502 /* klp_object_ext.funcs */ 1503 if (!elf_create_reloc(e->out, obj_sec, offsetof(struct klp_object_ext, funcs), 1504 funcs_sym, 0, R_ABS64)) 1505 return -1; 1506 1507 for_each_sym(e->out, sym) { 1508 unsigned long offset = nr_funcs * func_size; 1509 unsigned long sympos; 1510 void *func_data; 1511 1512 if (!is_func_sym(sym) || sym->cold || !sym->clone || !sym->clone->changed) 1513 continue; 1514 1515 /* allocate klp_func_ext */ 1516 func_data = elf_add_data(e->out, funcs_sec, NULL, func_size); 1517 if (!func_data) 1518 return -1; 1519 1520 /* klp_func_ext.old_name */ 1521 addend = elf_add_string(e->out, str_sec, sym->clone->twin->name); 1522 if (addend == -1) 1523 return -1; 1524 1525 if (!elf_create_reloc(e->out, funcs_sec, 1526 offset + offsetof(struct klp_func_ext, old_name), 1527 str_sym, addend, R_ABS64)) 1528 return -1; 1529 1530 /* klp_func_ext.new_func */ 1531 if (!elf_create_reloc(e->out, funcs_sec, 1532 offset + offsetof(struct klp_func_ext, new_func), 1533 sym, 0, R_ABS64)) 1534 return -1; 1535 1536 /* klp_func_ext.sympos */ 1537 BUILD_BUG_ON(sizeof(sympos) != sizeof_field(struct klp_func_ext, sympos)); 1538 sympos = find_sympos(e->orig, sym->clone->twin); 1539 if (sympos == ULONG_MAX) 1540 return -1; 1541 memcpy(func_data + offsetof(struct klp_func_ext, sympos), &sympos, 1542 sizeof_field(struct klp_func_ext, sympos)); 1543 1544 nr_funcs++; 1545 } 1546 1547 /* klp_object_ext.nr_funcs */ 1548 BUILD_BUG_ON(sizeof(nr_funcs) != sizeof_field(struct klp_object_ext, nr_funcs)); 1549 memcpy(obj_data + offsetof(struct klp_object_ext, nr_funcs), &nr_funcs, 1550 sizeof_field(struct klp_object_ext, nr_funcs)); 1551 1552 /* 1553 * Find callback pointers created by KLP_PRE_PATCH_CALLBACK() and 1554 * friends, and add them to the klp object. 1555 */ 1556 1557 if (snprintf_check(sym_name, SYM_NAME_LEN, KLP_PRE_PATCH_PREFIX "%s", modname)) 1558 return -1; 1559 1560 sym = find_symbol_by_name(e->out, sym_name); 1561 if (sym) { 1562 struct reloc *reloc; 1563 1564 reloc = find_reloc_by_dest(e->out, sym->sec, sym->offset); 1565 1566 if (!elf_create_reloc(e->out, obj_sec, 1567 offsetof(struct klp_object_ext, callbacks) + 1568 offsetof(struct klp_callbacks, pre_patch), 1569 reloc->sym, reloc_addend(reloc), R_ABS64)) 1570 return -1; 1571 } 1572 1573 if (snprintf_check(sym_name, SYM_NAME_LEN, KLP_POST_PATCH_PREFIX "%s", modname)) 1574 return -1; 1575 1576 sym = find_symbol_by_name(e->out, sym_name); 1577 if (sym) { 1578 struct reloc *reloc; 1579 1580 reloc = find_reloc_by_dest(e->out, sym->sec, sym->offset); 1581 1582 if (!elf_create_reloc(e->out, obj_sec, 1583 offsetof(struct klp_object_ext, callbacks) + 1584 offsetof(struct klp_callbacks, post_patch), 1585 reloc->sym, reloc_addend(reloc), R_ABS64)) 1586 return -1; 1587 } 1588 1589 if (snprintf_check(sym_name, SYM_NAME_LEN, KLP_PRE_UNPATCH_PREFIX "%s", modname)) 1590 return -1; 1591 1592 sym = find_symbol_by_name(e->out, sym_name); 1593 if (sym) { 1594 struct reloc *reloc; 1595 1596 reloc = find_reloc_by_dest(e->out, sym->sec, sym->offset); 1597 1598 if (!elf_create_reloc(e->out, obj_sec, 1599 offsetof(struct klp_object_ext, callbacks) + 1600 offsetof(struct klp_callbacks, pre_unpatch), 1601 reloc->sym, reloc_addend(reloc), R_ABS64)) 1602 return -1; 1603 } 1604 1605 if (snprintf_check(sym_name, SYM_NAME_LEN, KLP_POST_UNPATCH_PREFIX "%s", modname)) 1606 return -1; 1607 1608 sym = find_symbol_by_name(e->out, sym_name); 1609 if (sym) { 1610 struct reloc *reloc; 1611 1612 reloc = find_reloc_by_dest(e->out, sym->sec, sym->offset); 1613 1614 if (!elf_create_reloc(e->out, obj_sec, 1615 offsetof(struct klp_object_ext, callbacks) + 1616 offsetof(struct klp_callbacks, post_unpatch), 1617 reloc->sym, reloc_addend(reloc), R_ABS64)) 1618 return -1; 1619 } 1620 1621 return 0; 1622 } 1623 1624 /* 1625 * Copy all .modinfo import_ns= tags to ensure all namespaced exported symbols 1626 * can be accessed via normal relocs. 1627 */ 1628 static int copy_import_ns(struct elfs *e) 1629 { 1630 struct section *patched_sec, *out_sec = NULL; 1631 char *import_ns, *data_end; 1632 1633 patched_sec = find_section_by_name(e->patched, ".modinfo"); 1634 if (!patched_sec) 1635 return 0; 1636 1637 import_ns = patched_sec->data->d_buf; 1638 if (!import_ns) 1639 return 0; 1640 1641 for (data_end = import_ns + sec_size(patched_sec); 1642 import_ns < data_end; 1643 import_ns += strlen(import_ns) + 1) { 1644 1645 import_ns = memmem(import_ns, data_end - import_ns, "import_ns=", 10); 1646 if (!import_ns) 1647 return 0; 1648 1649 if (!out_sec) { 1650 out_sec = find_section_by_name(e->out, ".modinfo"); 1651 if (!out_sec) { 1652 out_sec = elf_create_section(e->out, ".modinfo", 0, 1653 patched_sec->sh.sh_entsize, 1654 patched_sec->sh.sh_type, 1655 patched_sec->sh.sh_addralign, 1656 patched_sec->sh.sh_flags); 1657 if (!out_sec) 1658 return -1; 1659 } 1660 } 1661 1662 if (!elf_add_data(e->out, out_sec, import_ns, strlen(import_ns) + 1)) 1663 return -1; 1664 } 1665 1666 return 0; 1667 } 1668 1669 int cmd_klp_diff(int argc, const char **argv) 1670 { 1671 struct elfs e = {0}; 1672 1673 argc = parse_options(argc, argv, klp_diff_options, klp_diff_usage, 0); 1674 if (argc != 3) 1675 usage_with_options(klp_diff_usage, klp_diff_options); 1676 1677 objname = argv[0]; 1678 1679 e.orig = elf_open_read(argv[0], O_RDONLY); 1680 e.patched = elf_open_read(argv[1], O_RDONLY); 1681 e.out = NULL; 1682 1683 if (!e.orig || !e.patched) 1684 return -1; 1685 1686 if (read_exports()) 1687 return -1; 1688 1689 if (read_sym_checksums(e.orig)) 1690 return -1; 1691 1692 if (read_sym_checksums(e.patched)) 1693 return -1; 1694 1695 if (correlate_symbols(&e)) 1696 return -1; 1697 1698 if (mark_changed_functions(&e)) 1699 return 0; 1700 1701 e.out = elf_create_file(&e.orig->ehdr, argv[2]); 1702 if (!e.out) 1703 return -1; 1704 1705 if (clone_included_functions(&e)) 1706 return -1; 1707 1708 if (clone_special_sections(&e)) 1709 return -1; 1710 1711 if (create_klp_sections(&e)) 1712 return -1; 1713 1714 if (copy_import_ns(&e)) 1715 return -1; 1716 1717 if (elf_write(e.out)) 1718 return -1; 1719 1720 return elf_close(e.out); 1721 } 1722