elf.c 29 KB

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