elf.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * elf.c - ELF access library
  4. *
  5. * Adapted from kpatch (https://github.com/dynup/kpatch):
  6. * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com>
  7. * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
  8. */
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/mman.h>
  12. #include <fcntl.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #include <linux/interval_tree_generic.h>
  19. #include <objtool/builtin.h>
  20. #include <objtool/elf.h>
  21. #include <objtool/warn.h>
  22. static inline u32 str_hash(const char *str)
  23. {
  24. return jhash(str, strlen(str), 0);
  25. }
  26. #define __elf_table(name) (elf->name##_hash)
  27. #define __elf_bits(name) (elf->name##_bits)
  28. #define __elf_table_entry(name, key) \
  29. __elf_table(name)[hash_min(key, __elf_bits(name))]
  30. #define elf_hash_add(name, node, key) \
  31. ({ \
  32. struct elf_hash_node *__node = node; \
  33. __node->next = __elf_table_entry(name, key); \
  34. __elf_table_entry(name, key) = __node; \
  35. })
  36. static inline void __elf_hash_del(struct elf_hash_node *node,
  37. struct elf_hash_node **head)
  38. {
  39. struct elf_hash_node *cur, *prev;
  40. if (node == *head) {
  41. *head = node->next;
  42. return;
  43. }
  44. for (prev = NULL, cur = *head; cur; prev = cur, cur = cur->next) {
  45. if (cur == node) {
  46. prev->next = cur->next;
  47. break;
  48. }
  49. }
  50. }
  51. #define elf_hash_del(name, node, key) \
  52. __elf_hash_del(node, &__elf_table_entry(name, key))
  53. #define elf_list_entry(ptr, type, member) \
  54. ({ \
  55. typeof(ptr) __ptr = (ptr); \
  56. __ptr ? container_of(__ptr, type, member) : NULL; \
  57. })
  58. #define elf_hash_for_each_possible(name, obj, member, key) \
  59. for (obj = elf_list_entry(__elf_table_entry(name, key), typeof(*obj), member); \
  60. obj; \
  61. obj = elf_list_entry(obj->member.next, typeof(*(obj)), member))
  62. #define elf_alloc_hash(name, size) \
  63. ({ \
  64. __elf_bits(name) = max(10, ilog2(size)); \
  65. __elf_table(name) = mmap(NULL, sizeof(struct elf_hash_node *) << __elf_bits(name), \
  66. PROT_READ|PROT_WRITE, \
  67. MAP_PRIVATE|MAP_ANON, -1, 0); \
  68. if (__elf_table(name) == (void *)-1L) { \
  69. WARN("mmap fail " #name); \
  70. __elf_table(name) = NULL; \
  71. } \
  72. __elf_table(name); \
  73. })
  74. static inline unsigned long __sym_start(struct symbol *s)
  75. {
  76. return s->offset;
  77. }
  78. static inline unsigned long __sym_last(struct symbol *s)
  79. {
  80. return s->offset + s->len - 1;
  81. }
  82. INTERVAL_TREE_DEFINE(struct symbol, node, unsigned long, __subtree_last,
  83. __sym_start, __sym_last, static, __sym)
  84. #define __sym_for_each(_iter, _tree, _start, _end) \
  85. for (_iter = __sym_iter_first((_tree), (_start), (_end)); \
  86. _iter; _iter = __sym_iter_next(_iter, (_start), (_end)))
  87. struct symbol_hole {
  88. unsigned long key;
  89. const struct symbol *sym;
  90. };
  91. /*
  92. * Find !section symbol where @offset is after it.
  93. */
  94. static int symbol_hole_by_offset(const void *key, const struct rb_node *node)
  95. {
  96. const struct symbol *s = rb_entry(node, struct symbol, node);
  97. struct symbol_hole *sh = (void *)key;
  98. if (sh->key < s->offset)
  99. return -1;
  100. if (sh->key >= s->offset + s->len) {
  101. if (s->type != STT_SECTION)
  102. sh->sym = s;
  103. return 1;
  104. }
  105. return 0;
  106. }
  107. struct section *find_section_by_name(const struct elf *elf, const char *name)
  108. {
  109. struct section *sec;
  110. elf_hash_for_each_possible(section_name, sec, name_hash, str_hash(name)) {
  111. if (!strcmp(sec->name, name))
  112. return sec;
  113. }
  114. return NULL;
  115. }
  116. static struct section *find_section_by_index(struct elf *elf,
  117. unsigned int idx)
  118. {
  119. struct section *sec;
  120. elf_hash_for_each_possible(section, sec, hash, idx) {
  121. if (sec->idx == idx)
  122. return sec;
  123. }
  124. return NULL;
  125. }
  126. static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
  127. {
  128. struct symbol *sym;
  129. elf_hash_for_each_possible(symbol, sym, hash, idx) {
  130. if (sym->idx == idx)
  131. return sym;
  132. }
  133. return NULL;
  134. }
  135. struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
  136. {
  137. struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
  138. struct symbol *iter;
  139. __sym_for_each(iter, tree, offset, offset) {
  140. if (iter->offset == offset && iter->type != STT_SECTION)
  141. return iter;
  142. }
  143. return NULL;
  144. }
  145. struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
  146. {
  147. struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
  148. struct symbol *iter;
  149. __sym_for_each(iter, tree, offset, offset) {
  150. if (iter->offset == offset && iter->type == STT_FUNC)
  151. return iter;
  152. }
  153. return NULL;
  154. }
  155. struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
  156. {
  157. struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
  158. struct symbol *iter;
  159. __sym_for_each(iter, tree, offset, offset) {
  160. if (iter->type != STT_SECTION)
  161. return iter;
  162. }
  163. return NULL;
  164. }
  165. /*
  166. * Returns size of hole starting at @offset.
  167. */
  168. int find_symbol_hole_containing(const struct section *sec, unsigned long offset)
  169. {
  170. struct symbol_hole hole = {
  171. .key = offset,
  172. .sym = NULL,
  173. };
  174. struct rb_node *n;
  175. struct symbol *s;
  176. /*
  177. * Find the rightmost symbol for which @offset is after it.
  178. */
  179. n = rb_find(&hole, &sec->symbol_tree.rb_root, symbol_hole_by_offset);
  180. /* found a symbol that contains @offset */
  181. if (n)
  182. return 0; /* not a hole */
  183. /* didn't find a symbol for which @offset is after it */
  184. if (!hole.sym)
  185. return 0; /* not a hole */
  186. /* @offset >= sym->offset + sym->len, find symbol after it */
  187. n = rb_next(&hole.sym->node);
  188. if (!n)
  189. return -1; /* until end of address space */
  190. /* hole until start of next symbol */
  191. s = rb_entry(n, struct symbol, node);
  192. return s->offset - offset;
  193. }
  194. struct symbol *find_func_containing(struct section *sec, unsigned long offset)
  195. {
  196. struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
  197. struct symbol *iter;
  198. __sym_for_each(iter, tree, offset, offset) {
  199. if (iter->type == STT_FUNC)
  200. return iter;
  201. }
  202. return NULL;
  203. }
  204. struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
  205. {
  206. struct symbol *sym;
  207. elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
  208. if (!strcmp(sym->name, name))
  209. return sym;
  210. }
  211. return NULL;
  212. }
  213. struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
  214. unsigned long offset, unsigned int len)
  215. {
  216. struct reloc *reloc, *r = NULL;
  217. struct section *rsec;
  218. unsigned long o;
  219. rsec = sec->rsec;
  220. if (!rsec)
  221. return NULL;
  222. for_offset_range(o, offset, offset + len) {
  223. elf_hash_for_each_possible(reloc, reloc, hash,
  224. sec_offset_hash(rsec, o)) {
  225. if (reloc->sec != rsec)
  226. continue;
  227. if (reloc_offset(reloc) >= offset &&
  228. reloc_offset(reloc) < offset + len) {
  229. if (!r || reloc_offset(reloc) < reloc_offset(r))
  230. r = reloc;
  231. }
  232. }
  233. if (r)
  234. return r;
  235. }
  236. return NULL;
  237. }
  238. struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
  239. {
  240. return find_reloc_by_dest_range(elf, sec, offset, 1);
  241. }
  242. static bool is_dwarf_section(struct section *sec)
  243. {
  244. return !strncmp(sec->name, ".debug_", 7);
  245. }
  246. static int read_sections(struct elf *elf)
  247. {
  248. Elf_Scn *s = NULL;
  249. struct section *sec;
  250. size_t shstrndx, sections_nr;
  251. int i;
  252. if (elf_getshdrnum(elf->elf, &sections_nr)) {
  253. WARN_ELF("elf_getshdrnum");
  254. return -1;
  255. }
  256. if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
  257. WARN_ELF("elf_getshdrstrndx");
  258. return -1;
  259. }
  260. if (!elf_alloc_hash(section, sections_nr) ||
  261. !elf_alloc_hash(section_name, sections_nr))
  262. return -1;
  263. elf->section_data = calloc(sections_nr, sizeof(*sec));
  264. if (!elf->section_data) {
  265. perror("calloc");
  266. return -1;
  267. }
  268. for (i = 0; i < sections_nr; i++) {
  269. sec = &elf->section_data[i];
  270. INIT_LIST_HEAD(&sec->symbol_list);
  271. s = elf_getscn(elf->elf, i);
  272. if (!s) {
  273. WARN_ELF("elf_getscn");
  274. return -1;
  275. }
  276. sec->idx = elf_ndxscn(s);
  277. if (!gelf_getshdr(s, &sec->sh)) {
  278. WARN_ELF("gelf_getshdr");
  279. return -1;
  280. }
  281. sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
  282. if (!sec->name) {
  283. WARN_ELF("elf_strptr");
  284. return -1;
  285. }
  286. if (sec->sh.sh_size != 0 && !is_dwarf_section(sec)) {
  287. sec->data = elf_getdata(s, NULL);
  288. if (!sec->data) {
  289. WARN_ELF("elf_getdata");
  290. return -1;
  291. }
  292. if (sec->data->d_off != 0 ||
  293. sec->data->d_size != sec->sh.sh_size) {
  294. WARN("unexpected data attributes for %s",
  295. sec->name);
  296. return -1;
  297. }
  298. }
  299. list_add_tail(&sec->list, &elf->sections);
  300. elf_hash_add(section, &sec->hash, sec->idx);
  301. elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
  302. if (is_reloc_sec(sec))
  303. elf->num_relocs += sec_num_entries(sec);
  304. }
  305. if (opts.stats) {
  306. printf("nr_sections: %lu\n", (unsigned long)sections_nr);
  307. printf("section_bits: %d\n", elf->section_bits);
  308. }
  309. /* sanity check, one more call to elf_nextscn() should return NULL */
  310. if (elf_nextscn(elf->elf, s)) {
  311. WARN("section entry mismatch");
  312. return -1;
  313. }
  314. return 0;
  315. }
  316. static void elf_add_symbol(struct elf *elf, struct symbol *sym)
  317. {
  318. struct list_head *entry;
  319. struct rb_node *pnode;
  320. struct symbol *iter;
  321. INIT_LIST_HEAD(&sym->pv_target);
  322. sym->alias = sym;
  323. sym->type = GELF_ST_TYPE(sym->sym.st_info);
  324. sym->bind = GELF_ST_BIND(sym->sym.st_info);
  325. if (sym->type == STT_FILE)
  326. elf->num_files++;
  327. sym->offset = sym->sym.st_value;
  328. sym->len = sym->sym.st_size;
  329. __sym_for_each(iter, &sym->sec->symbol_tree, sym->offset, sym->offset) {
  330. if (iter->offset == sym->offset && iter->type == sym->type)
  331. iter->alias = sym;
  332. }
  333. __sym_insert(sym, &sym->sec->symbol_tree);
  334. pnode = rb_prev(&sym->node);
  335. if (pnode)
  336. entry = &rb_entry(pnode, struct symbol, node)->list;
  337. else
  338. entry = &sym->sec->symbol_list;
  339. list_add(&sym->list, entry);
  340. elf_hash_add(symbol, &sym->hash, sym->idx);
  341. elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
  342. /*
  343. * Don't store empty STT_NOTYPE symbols in the rbtree. They
  344. * can exist within a function, confusing the sorting.
  345. */
  346. if (!sym->len)
  347. __sym_remove(sym, &sym->sec->symbol_tree);
  348. }
  349. static int read_symbols(struct elf *elf)
  350. {
  351. struct section *symtab, *symtab_shndx, *sec;
  352. struct symbol *sym, *pfunc;
  353. int symbols_nr, i;
  354. char *coldstr;
  355. Elf_Data *shndx_data = NULL;
  356. Elf32_Word shndx;
  357. symtab = find_section_by_name(elf, ".symtab");
  358. if (symtab) {
  359. symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
  360. if (symtab_shndx)
  361. shndx_data = symtab_shndx->data;
  362. symbols_nr = sec_num_entries(symtab);
  363. } else {
  364. /*
  365. * A missing symbol table is actually possible if it's an empty
  366. * .o file. This can happen for thunk_64.o. Make sure to at
  367. * least allocate the symbol hash tables so we can do symbol
  368. * lookups without crashing.
  369. */
  370. symbols_nr = 0;
  371. }
  372. if (!elf_alloc_hash(symbol, symbols_nr) ||
  373. !elf_alloc_hash(symbol_name, symbols_nr))
  374. return -1;
  375. elf->symbol_data = calloc(symbols_nr, sizeof(*sym));
  376. if (!elf->symbol_data) {
  377. perror("calloc");
  378. return -1;
  379. }
  380. for (i = 0; i < symbols_nr; i++) {
  381. sym = &elf->symbol_data[i];
  382. sym->idx = i;
  383. if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
  384. &shndx)) {
  385. WARN_ELF("gelf_getsymshndx");
  386. goto err;
  387. }
  388. sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
  389. sym->sym.st_name);
  390. if (!sym->name) {
  391. WARN_ELF("elf_strptr");
  392. goto err;
  393. }
  394. if ((sym->sym.st_shndx > SHN_UNDEF &&
  395. sym->sym.st_shndx < SHN_LORESERVE) ||
  396. (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
  397. if (sym->sym.st_shndx != SHN_XINDEX)
  398. shndx = sym->sym.st_shndx;
  399. sym->sec = find_section_by_index(elf, shndx);
  400. if (!sym->sec) {
  401. WARN("couldn't find section for symbol %s",
  402. sym->name);
  403. goto err;
  404. }
  405. if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {
  406. sym->name = sym->sec->name;
  407. sym->sec->sym = sym;
  408. }
  409. } else
  410. sym->sec = find_section_by_index(elf, 0);
  411. elf_add_symbol(elf, sym);
  412. }
  413. if (opts.stats) {
  414. printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
  415. printf("symbol_bits: %d\n", elf->symbol_bits);
  416. }
  417. /* Create parent/child links for any cold subfunctions */
  418. list_for_each_entry(sec, &elf->sections, list) {
  419. sec_for_each_sym(sec, sym) {
  420. char *pname;
  421. size_t pnamelen;
  422. if (sym->type != STT_FUNC)
  423. continue;
  424. if (sym->pfunc == NULL)
  425. sym->pfunc = sym;
  426. if (sym->cfunc == NULL)
  427. sym->cfunc = sym;
  428. coldstr = strstr(sym->name, ".cold");
  429. if (!coldstr)
  430. continue;
  431. pnamelen = coldstr - sym->name;
  432. pname = strndup(sym->name, pnamelen);
  433. if (!pname) {
  434. WARN("%s(): failed to allocate memory",
  435. sym->name);
  436. return -1;
  437. }
  438. pfunc = find_symbol_by_name(elf, pname);
  439. free(pname);
  440. if (!pfunc) {
  441. WARN("%s(): can't find parent function",
  442. sym->name);
  443. return -1;
  444. }
  445. sym->pfunc = pfunc;
  446. pfunc->cfunc = sym;
  447. /*
  448. * Unfortunately, -fnoreorder-functions puts the child
  449. * inside the parent. Remove the overlap so we can
  450. * have sane assumptions.
  451. *
  452. * Note that pfunc->len now no longer matches
  453. * pfunc->sym.st_size.
  454. */
  455. if (sym->sec == pfunc->sec &&
  456. sym->offset >= pfunc->offset &&
  457. sym->offset + sym->len == pfunc->offset + pfunc->len) {
  458. pfunc->len -= sym->len;
  459. }
  460. }
  461. }
  462. return 0;
  463. err:
  464. free(sym);
  465. return -1;
  466. }
  467. /*
  468. * @sym's idx has changed. Update the relocs which reference it.
  469. */
  470. static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym)
  471. {
  472. struct reloc *reloc;
  473. for (reloc = sym->relocs; reloc; reloc = reloc->sym_next_reloc)
  474. set_reloc_sym(elf, reloc, reloc->sym->idx);
  475. return 0;
  476. }
  477. /*
  478. * The libelf API is terrible; gelf_update_sym*() takes a data block relative
  479. * index value, *NOT* the symbol index. As such, iterate the data blocks and
  480. * adjust index until it fits.
  481. *
  482. * If no data block is found, allow adding a new data block provided the index
  483. * is only one past the end.
  484. */
  485. static int elf_update_symbol(struct elf *elf, struct section *symtab,
  486. struct section *symtab_shndx, struct symbol *sym)
  487. {
  488. Elf32_Word shndx = sym->sec ? sym->sec->idx : SHN_UNDEF;
  489. Elf_Data *symtab_data = NULL, *shndx_data = NULL;
  490. Elf64_Xword entsize = symtab->sh.sh_entsize;
  491. int max_idx, idx = sym->idx;
  492. Elf_Scn *s, *t = NULL;
  493. bool is_special_shndx = sym->sym.st_shndx >= SHN_LORESERVE &&
  494. sym->sym.st_shndx != SHN_XINDEX;
  495. if (is_special_shndx)
  496. shndx = sym->sym.st_shndx;
  497. s = elf_getscn(elf->elf, symtab->idx);
  498. if (!s) {
  499. WARN_ELF("elf_getscn");
  500. return -1;
  501. }
  502. if (symtab_shndx) {
  503. t = elf_getscn(elf->elf, symtab_shndx->idx);
  504. if (!t) {
  505. WARN_ELF("elf_getscn");
  506. return -1;
  507. }
  508. }
  509. for (;;) {
  510. /* get next data descriptor for the relevant sections */
  511. symtab_data = elf_getdata(s, symtab_data);
  512. if (t)
  513. shndx_data = elf_getdata(t, shndx_data);
  514. /* end-of-list */
  515. if (!symtab_data) {
  516. /*
  517. * Over-allocate to avoid O(n^2) symbol creation
  518. * behaviour. The down side is that libelf doesn't
  519. * like this; see elf_truncate_section() for the fixup.
  520. */
  521. int num = max(1U, sym->idx/3);
  522. void *buf;
  523. if (idx) {
  524. /* we don't do holes in symbol tables */
  525. WARN("index out of range");
  526. return -1;
  527. }
  528. /* if @idx == 0, it's the next contiguous entry, create it */
  529. symtab_data = elf_newdata(s);
  530. if (t)
  531. shndx_data = elf_newdata(t);
  532. buf = calloc(num, entsize);
  533. if (!buf) {
  534. WARN("malloc");
  535. return -1;
  536. }
  537. symtab_data->d_buf = buf;
  538. symtab_data->d_size = num * entsize;
  539. symtab_data->d_align = 1;
  540. symtab_data->d_type = ELF_T_SYM;
  541. mark_sec_changed(elf, symtab, true);
  542. symtab->truncate = true;
  543. if (t) {
  544. buf = calloc(num, sizeof(Elf32_Word));
  545. if (!buf) {
  546. WARN("malloc");
  547. return -1;
  548. }
  549. shndx_data->d_buf = buf;
  550. shndx_data->d_size = num * sizeof(Elf32_Word);
  551. shndx_data->d_align = sizeof(Elf32_Word);
  552. shndx_data->d_type = ELF_T_WORD;
  553. mark_sec_changed(elf, symtab_shndx, true);
  554. symtab_shndx->truncate = true;
  555. }
  556. break;
  557. }
  558. /* empty blocks should not happen */
  559. if (!symtab_data->d_size) {
  560. WARN("zero size data");
  561. return -1;
  562. }
  563. /* is this the right block? */
  564. max_idx = symtab_data->d_size / entsize;
  565. if (idx < max_idx)
  566. break;
  567. /* adjust index and try again */
  568. idx -= max_idx;
  569. }
  570. /* something went side-ways */
  571. if (idx < 0) {
  572. WARN("negative index");
  573. return -1;
  574. }
  575. /* setup extended section index magic and write the symbol */
  576. if ((shndx >= SHN_UNDEF && shndx < SHN_LORESERVE) || is_special_shndx) {
  577. sym->sym.st_shndx = shndx;
  578. if (!shndx_data)
  579. shndx = 0;
  580. } else {
  581. sym->sym.st_shndx = SHN_XINDEX;
  582. if (!shndx_data) {
  583. WARN("no .symtab_shndx");
  584. return -1;
  585. }
  586. }
  587. if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) {
  588. WARN_ELF("gelf_update_symshndx");
  589. return -1;
  590. }
  591. return 0;
  592. }
  593. static struct symbol *
  594. __elf_create_symbol(struct elf *elf, struct symbol *sym)
  595. {
  596. struct section *symtab, *symtab_shndx;
  597. Elf32_Word first_non_local, new_idx;
  598. struct symbol *old;
  599. symtab = find_section_by_name(elf, ".symtab");
  600. if (symtab) {
  601. symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
  602. } else {
  603. WARN("no .symtab");
  604. return NULL;
  605. }
  606. new_idx = sec_num_entries(symtab);
  607. if (GELF_ST_BIND(sym->sym.st_info) != STB_LOCAL)
  608. goto non_local;
  609. /*
  610. * Move the first global symbol, as per sh_info, into a new, higher
  611. * symbol index. This fees up a spot for a new local symbol.
  612. */
  613. first_non_local = symtab->sh.sh_info;
  614. old = find_symbol_by_index(elf, first_non_local);
  615. if (old) {
  616. elf_hash_del(symbol, &old->hash, old->idx);
  617. elf_hash_add(symbol, &old->hash, new_idx);
  618. old->idx = new_idx;
  619. if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
  620. WARN("elf_update_symbol move");
  621. return NULL;
  622. }
  623. if (elf_update_sym_relocs(elf, old))
  624. return NULL;
  625. new_idx = first_non_local;
  626. }
  627. /*
  628. * Either way, we will add a LOCAL symbol.
  629. */
  630. symtab->sh.sh_info += 1;
  631. non_local:
  632. sym->idx = new_idx;
  633. if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) {
  634. WARN("elf_update_symbol");
  635. return NULL;
  636. }
  637. symtab->sh.sh_size += symtab->sh.sh_entsize;
  638. mark_sec_changed(elf, symtab, true);
  639. if (symtab_shndx) {
  640. symtab_shndx->sh.sh_size += sizeof(Elf32_Word);
  641. mark_sec_changed(elf, symtab_shndx, true);
  642. }
  643. return sym;
  644. }
  645. static struct symbol *
  646. elf_create_section_symbol(struct elf *elf, struct section *sec)
  647. {
  648. struct symbol *sym = calloc(1, sizeof(*sym));
  649. if (!sym) {
  650. perror("malloc");
  651. return NULL;
  652. }
  653. sym->name = sec->name;
  654. sym->sec = sec;
  655. // st_name 0
  656. sym->sym.st_info = GELF_ST_INFO(STB_LOCAL, STT_SECTION);
  657. // st_other 0
  658. // st_value 0
  659. // st_size 0
  660. sym = __elf_create_symbol(elf, sym);
  661. if (sym)
  662. elf_add_symbol(elf, sym);
  663. return sym;
  664. }
  665. static int elf_add_string(struct elf *elf, struct section *strtab, char *str);
  666. struct symbol *
  667. elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size)
  668. {
  669. struct symbol *sym = calloc(1, sizeof(*sym));
  670. size_t namelen = strlen(orig->name) + sizeof("__pfx_");
  671. char *name = malloc(namelen);
  672. if (!sym || !name) {
  673. perror("malloc");
  674. return NULL;
  675. }
  676. snprintf(name, namelen, "__pfx_%s", orig->name);
  677. sym->name = name;
  678. sym->sec = orig->sec;
  679. sym->sym.st_name = elf_add_string(elf, NULL, name);
  680. sym->sym.st_info = orig->sym.st_info;
  681. sym->sym.st_value = orig->sym.st_value - size;
  682. sym->sym.st_size = size;
  683. sym = __elf_create_symbol(elf, sym);
  684. if (sym)
  685. elf_add_symbol(elf, sym);
  686. return sym;
  687. }
  688. static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
  689. unsigned int reloc_idx,
  690. unsigned long offset, struct symbol *sym,
  691. s64 addend, unsigned int type)
  692. {
  693. struct reloc *reloc, empty = { 0 };
  694. if (reloc_idx >= sec_num_entries(rsec)) {
  695. WARN("%s: bad reloc_idx %u for %s with %d relocs",
  696. __func__, reloc_idx, rsec->name, sec_num_entries(rsec));
  697. return NULL;
  698. }
  699. reloc = &rsec->relocs[reloc_idx];
  700. if (memcmp(reloc, &empty, sizeof(empty))) {
  701. WARN("%s: %s: reloc %d already initialized!",
  702. __func__, rsec->name, reloc_idx);
  703. return NULL;
  704. }
  705. reloc->sec = rsec;
  706. reloc->sym = sym;
  707. set_reloc_offset(elf, reloc, offset);
  708. set_reloc_sym(elf, reloc, sym->idx);
  709. set_reloc_type(elf, reloc, type);
  710. set_reloc_addend(elf, reloc, addend);
  711. elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
  712. reloc->sym_next_reloc = sym->relocs;
  713. sym->relocs = reloc;
  714. return reloc;
  715. }
  716. struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec,
  717. unsigned long offset,
  718. unsigned int reloc_idx,
  719. struct section *insn_sec,
  720. unsigned long insn_off)
  721. {
  722. struct symbol *sym = insn_sec->sym;
  723. int addend = insn_off;
  724. if (!(insn_sec->sh.sh_flags & SHF_EXECINSTR)) {
  725. WARN("bad call to %s() for data symbol %s",
  726. __func__, sym->name);
  727. return NULL;
  728. }
  729. if (!sym) {
  730. /*
  731. * Due to how weak functions work, we must use section based
  732. * relocations. Symbol based relocations would result in the
  733. * weak and non-weak function annotations being overlaid on the
  734. * non-weak function after linking.
  735. */
  736. sym = elf_create_section_symbol(elf, insn_sec);
  737. if (!sym)
  738. return NULL;
  739. insn_sec->sym = sym;
  740. }
  741. return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
  742. elf_text_rela_type(elf));
  743. }
  744. struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec,
  745. unsigned long offset,
  746. unsigned int reloc_idx,
  747. struct symbol *sym,
  748. s64 addend)
  749. {
  750. if (sym->sec && (sec->sh.sh_flags & SHF_EXECINSTR)) {
  751. WARN("bad call to %s() for text symbol %s",
  752. __func__, sym->name);
  753. return NULL;
  754. }
  755. return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
  756. elf_data_rela_type(elf));
  757. }
  758. static int read_relocs(struct elf *elf)
  759. {
  760. unsigned long nr_reloc, max_reloc = 0;
  761. struct section *rsec;
  762. struct reloc *reloc;
  763. unsigned int symndx;
  764. struct symbol *sym;
  765. int i;
  766. if (!elf_alloc_hash(reloc, elf->num_relocs))
  767. return -1;
  768. list_for_each_entry(rsec, &elf->sections, list) {
  769. if (!is_reloc_sec(rsec))
  770. continue;
  771. rsec->base = find_section_by_index(elf, rsec->sh.sh_info);
  772. if (!rsec->base) {
  773. WARN("can't find base section for reloc section %s",
  774. rsec->name);
  775. return -1;
  776. }
  777. rsec->base->rsec = rsec;
  778. nr_reloc = 0;
  779. rsec->relocs = calloc(sec_num_entries(rsec), sizeof(*reloc));
  780. if (!rsec->relocs) {
  781. perror("calloc");
  782. return -1;
  783. }
  784. for (i = 0; i < sec_num_entries(rsec); i++) {
  785. reloc = &rsec->relocs[i];
  786. reloc->sec = rsec;
  787. symndx = reloc_sym(reloc);
  788. reloc->sym = sym = find_symbol_by_index(elf, symndx);
  789. if (!reloc->sym) {
  790. WARN("can't find reloc entry symbol %d for %s",
  791. symndx, rsec->name);
  792. return -1;
  793. }
  794. elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
  795. reloc->sym_next_reloc = sym->relocs;
  796. sym->relocs = reloc;
  797. nr_reloc++;
  798. }
  799. max_reloc = max(max_reloc, nr_reloc);
  800. }
  801. if (opts.stats) {
  802. printf("max_reloc: %lu\n", max_reloc);
  803. printf("num_relocs: %lu\n", elf->num_relocs);
  804. printf("reloc_bits: %d\n", elf->reloc_bits);
  805. }
  806. return 0;
  807. }
  808. struct elf *elf_open_read(const char *name, int flags)
  809. {
  810. struct elf *elf;
  811. Elf_Cmd cmd;
  812. elf_version(EV_CURRENT);
  813. elf = malloc(sizeof(*elf));
  814. if (!elf) {
  815. perror("malloc");
  816. return NULL;
  817. }
  818. memset(elf, 0, sizeof(*elf));
  819. INIT_LIST_HEAD(&elf->sections);
  820. elf->fd = open(name, flags);
  821. if (elf->fd == -1) {
  822. fprintf(stderr, "objtool: Can't open '%s': %s\n",
  823. name, strerror(errno));
  824. goto err;
  825. }
  826. if ((flags & O_ACCMODE) == O_RDONLY)
  827. cmd = ELF_C_READ_MMAP;
  828. else if ((flags & O_ACCMODE) == O_RDWR)
  829. cmd = ELF_C_RDWR;
  830. else /* O_WRONLY */
  831. cmd = ELF_C_WRITE;
  832. elf->elf = elf_begin(elf->fd, cmd, NULL);
  833. if (!elf->elf) {
  834. WARN_ELF("elf_begin");
  835. goto err;
  836. }
  837. if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
  838. WARN_ELF("gelf_getehdr");
  839. goto err;
  840. }
  841. if (read_sections(elf))
  842. goto err;
  843. if (read_symbols(elf))
  844. goto err;
  845. if (read_relocs(elf))
  846. goto err;
  847. return elf;
  848. err:
  849. elf_close(elf);
  850. return NULL;
  851. }
  852. static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
  853. {
  854. Elf_Data *data;
  855. Elf_Scn *s;
  856. int len;
  857. if (!strtab)
  858. strtab = find_section_by_name(elf, ".strtab");
  859. if (!strtab) {
  860. WARN("can't find .strtab section");
  861. return -1;
  862. }
  863. s = elf_getscn(elf->elf, strtab->idx);
  864. if (!s) {
  865. WARN_ELF("elf_getscn");
  866. return -1;
  867. }
  868. data = elf_newdata(s);
  869. if (!data) {
  870. WARN_ELF("elf_newdata");
  871. return -1;
  872. }
  873. data->d_buf = str;
  874. data->d_size = strlen(str) + 1;
  875. data->d_align = 1;
  876. len = strtab->sh.sh_size;
  877. strtab->sh.sh_size += data->d_size;
  878. mark_sec_changed(elf, strtab, true);
  879. return len;
  880. }
  881. struct section *elf_create_section(struct elf *elf, const char *name,
  882. size_t entsize, unsigned int nr)
  883. {
  884. struct section *sec, *shstrtab;
  885. size_t size = entsize * nr;
  886. Elf_Scn *s;
  887. sec = malloc(sizeof(*sec));
  888. if (!sec) {
  889. perror("malloc");
  890. return NULL;
  891. }
  892. memset(sec, 0, sizeof(*sec));
  893. INIT_LIST_HEAD(&sec->symbol_list);
  894. s = elf_newscn(elf->elf);
  895. if (!s) {
  896. WARN_ELF("elf_newscn");
  897. return NULL;
  898. }
  899. sec->name = strdup(name);
  900. if (!sec->name) {
  901. perror("strdup");
  902. return NULL;
  903. }
  904. sec->idx = elf_ndxscn(s);
  905. sec->data = elf_newdata(s);
  906. if (!sec->data) {
  907. WARN_ELF("elf_newdata");
  908. return NULL;
  909. }
  910. sec->data->d_size = size;
  911. sec->data->d_align = 1;
  912. if (size) {
  913. sec->data->d_buf = malloc(size);
  914. if (!sec->data->d_buf) {
  915. perror("malloc");
  916. return NULL;
  917. }
  918. memset(sec->data->d_buf, 0, size);
  919. }
  920. if (!gelf_getshdr(s, &sec->sh)) {
  921. WARN_ELF("gelf_getshdr");
  922. return NULL;
  923. }
  924. sec->sh.sh_size = size;
  925. sec->sh.sh_entsize = entsize;
  926. sec->sh.sh_type = SHT_PROGBITS;
  927. sec->sh.sh_addralign = 1;
  928. sec->sh.sh_flags = SHF_ALLOC;
  929. /* Add section name to .shstrtab (or .strtab for Clang) */
  930. shstrtab = find_section_by_name(elf, ".shstrtab");
  931. if (!shstrtab)
  932. shstrtab = find_section_by_name(elf, ".strtab");
  933. if (!shstrtab) {
  934. WARN("can't find .shstrtab or .strtab section");
  935. return NULL;
  936. }
  937. sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
  938. if (sec->sh.sh_name == -1)
  939. return NULL;
  940. list_add_tail(&sec->list, &elf->sections);
  941. elf_hash_add(section, &sec->hash, sec->idx);
  942. elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
  943. mark_sec_changed(elf, sec, true);
  944. return sec;
  945. }
  946. static struct section *elf_create_rela_section(struct elf *elf,
  947. struct section *sec,
  948. unsigned int reloc_nr)
  949. {
  950. struct section *rsec;
  951. char *rsec_name;
  952. rsec_name = malloc(strlen(sec->name) + strlen(".rela") + 1);
  953. if (!rsec_name) {
  954. perror("malloc");
  955. return NULL;
  956. }
  957. strcpy(rsec_name, ".rela");
  958. strcat(rsec_name, sec->name);
  959. rsec = elf_create_section(elf, rsec_name, elf_rela_size(elf), reloc_nr);
  960. free(rsec_name);
  961. if (!rsec)
  962. return NULL;
  963. rsec->data->d_type = ELF_T_RELA;
  964. rsec->sh.sh_type = SHT_RELA;
  965. rsec->sh.sh_addralign = elf_addr_size(elf);
  966. rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
  967. rsec->sh.sh_info = sec->idx;
  968. rsec->sh.sh_flags = SHF_INFO_LINK;
  969. rsec->relocs = calloc(sec_num_entries(rsec), sizeof(struct reloc));
  970. if (!rsec->relocs) {
  971. perror("calloc");
  972. return NULL;
  973. }
  974. sec->rsec = rsec;
  975. rsec->base = sec;
  976. return rsec;
  977. }
  978. struct section *elf_create_section_pair(struct elf *elf, const char *name,
  979. size_t entsize, unsigned int nr,
  980. unsigned int reloc_nr)
  981. {
  982. struct section *sec;
  983. sec = elf_create_section(elf, name, entsize, nr);
  984. if (!sec)
  985. return NULL;
  986. if (!elf_create_rela_section(elf, sec, reloc_nr))
  987. return NULL;
  988. return sec;
  989. }
  990. int elf_write_insn(struct elf *elf, struct section *sec,
  991. unsigned long offset, unsigned int len,
  992. const char *insn)
  993. {
  994. Elf_Data *data = sec->data;
  995. if (data->d_type != ELF_T_BYTE || data->d_off) {
  996. WARN("write to unexpected data for section: %s", sec->name);
  997. return -1;
  998. }
  999. memcpy(data->d_buf + offset, insn, len);
  1000. mark_sec_changed(elf, sec, true);
  1001. return 0;
  1002. }
  1003. /*
  1004. * When Elf_Scn::sh_size is smaller than the combined Elf_Data::d_size
  1005. * do you:
  1006. *
  1007. * A) adhere to the section header and truncate the data, or
  1008. * B) ignore the section header and write out all the data you've got?
  1009. *
  1010. * Yes, libelf sucks and we need to manually truncate if we over-allocate data.
  1011. */
  1012. static int elf_truncate_section(struct elf *elf, struct section *sec)
  1013. {
  1014. u64 size = sec->sh.sh_size;
  1015. bool truncated = false;
  1016. Elf_Data *data = NULL;
  1017. Elf_Scn *s;
  1018. s = elf_getscn(elf->elf, sec->idx);
  1019. if (!s) {
  1020. WARN_ELF("elf_getscn");
  1021. return -1;
  1022. }
  1023. for (;;) {
  1024. /* get next data descriptor for the relevant section */
  1025. data = elf_getdata(s, data);
  1026. if (!data) {
  1027. if (size) {
  1028. WARN("end of section data but non-zero size left\n");
  1029. return -1;
  1030. }
  1031. return 0;
  1032. }
  1033. if (truncated) {
  1034. /* when we remove symbols */
  1035. WARN("truncated; but more data\n");
  1036. return -1;
  1037. }
  1038. if (!data->d_size) {
  1039. WARN("zero size data");
  1040. return -1;
  1041. }
  1042. if (data->d_size > size) {
  1043. truncated = true;
  1044. data->d_size = size;
  1045. }
  1046. size -= data->d_size;
  1047. }
  1048. }
  1049. int elf_write(struct elf *elf)
  1050. {
  1051. struct section *sec;
  1052. Elf_Scn *s;
  1053. if (opts.dryrun)
  1054. return 0;
  1055. /* Update changed relocation sections and section headers: */
  1056. list_for_each_entry(sec, &elf->sections, list) {
  1057. if (sec->truncate)
  1058. elf_truncate_section(elf, sec);
  1059. if (sec_changed(sec)) {
  1060. s = elf_getscn(elf->elf, sec->idx);
  1061. if (!s) {
  1062. WARN_ELF("elf_getscn");
  1063. return -1;
  1064. }
  1065. /* Note this also flags the section dirty */
  1066. if (!gelf_update_shdr(s, &sec->sh)) {
  1067. WARN_ELF("gelf_update_shdr");
  1068. return -1;
  1069. }
  1070. mark_sec_changed(elf, sec, false);
  1071. }
  1072. }
  1073. /* Make sure the new section header entries get updated properly. */
  1074. elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
  1075. /* Write all changes to the file. */
  1076. if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
  1077. WARN_ELF("elf_update");
  1078. return -1;
  1079. }
  1080. elf->changed = false;
  1081. return 0;
  1082. }
  1083. void elf_close(struct elf *elf)
  1084. {
  1085. if (elf->elf)
  1086. elf_end(elf->elf);
  1087. if (elf->fd > 0)
  1088. close(elf->fd);
  1089. /*
  1090. * NOTE: All remaining allocations are leaked on purpose. Objtool is
  1091. * about to exit anyway.
  1092. */
  1093. }