klp-diff.c 42 KB

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