elf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. * elf.c - ELF access library
  3. *
  4. * Adapted from kpatch (https://github.com/dynup/kpatch):
  5. * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com>
  6. * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #include "elf.h"
  30. #include "warn.h"
  31. struct section *find_section_by_name(struct elf *elf, const char *name)
  32. {
  33. struct section *sec;
  34. list_for_each_entry(sec, &elf->sections, list)
  35. if (!strcmp(sec->name, name))
  36. return sec;
  37. return NULL;
  38. }
  39. static struct section *find_section_by_index(struct elf *elf,
  40. unsigned int idx)
  41. {
  42. struct section *sec;
  43. list_for_each_entry(sec, &elf->sections, list)
  44. if (sec->idx == idx)
  45. return sec;
  46. return NULL;
  47. }
  48. static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
  49. {
  50. struct section *sec;
  51. struct symbol *sym;
  52. list_for_each_entry(sec, &elf->sections, list)
  53. hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
  54. if (sym->idx == idx)
  55. return sym;
  56. return NULL;
  57. }
  58. struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
  59. {
  60. struct symbol *sym;
  61. list_for_each_entry(sym, &sec->symbol_list, list)
  62. if (sym->type != STT_SECTION &&
  63. sym->offset == offset)
  64. return sym;
  65. return NULL;
  66. }
  67. struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
  68. {
  69. struct section *sec;
  70. struct symbol *sym;
  71. list_for_each_entry(sec, &elf->sections, list)
  72. list_for_each_entry(sym, &sec->symbol_list, list)
  73. if (!strcmp(sym->name, name))
  74. return sym;
  75. return NULL;
  76. }
  77. struct symbol *find_symbol_containing(struct section *sec, unsigned long offset)
  78. {
  79. struct symbol *sym;
  80. list_for_each_entry(sym, &sec->symbol_list, list)
  81. if (sym->type != STT_SECTION &&
  82. offset >= sym->offset && offset < sym->offset + sym->len)
  83. return sym;
  84. return NULL;
  85. }
  86. struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
  87. unsigned int len)
  88. {
  89. struct rela *rela;
  90. unsigned long o;
  91. if (!sec->rela)
  92. return NULL;
  93. for (o = offset; o < offset + len; o++)
  94. hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
  95. if (rela->offset == o)
  96. return rela;
  97. return NULL;
  98. }
  99. struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
  100. {
  101. return find_rela_by_dest_range(sec, offset, 1);
  102. }
  103. struct symbol *find_containing_func(struct section *sec, unsigned long offset)
  104. {
  105. struct symbol *func;
  106. list_for_each_entry(func, &sec->symbol_list, list)
  107. if (func->type == STT_FUNC && offset >= func->offset &&
  108. offset < func->offset + func->len)
  109. return func;
  110. return NULL;
  111. }
  112. static int read_sections(struct elf *elf)
  113. {
  114. Elf_Scn *s = NULL;
  115. struct section *sec;
  116. size_t shstrndx, sections_nr;
  117. int i;
  118. if (elf_getshdrnum(elf->elf, &sections_nr)) {
  119. WARN_ELF("elf_getshdrnum");
  120. return -1;
  121. }
  122. if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
  123. WARN_ELF("elf_getshdrstrndx");
  124. return -1;
  125. }
  126. for (i = 0; i < sections_nr; i++) {
  127. sec = malloc(sizeof(*sec));
  128. if (!sec) {
  129. perror("malloc");
  130. return -1;
  131. }
  132. memset(sec, 0, sizeof(*sec));
  133. INIT_LIST_HEAD(&sec->symbol_list);
  134. INIT_LIST_HEAD(&sec->rela_list);
  135. hash_init(sec->rela_hash);
  136. hash_init(sec->symbol_hash);
  137. list_add_tail(&sec->list, &elf->sections);
  138. s = elf_getscn(elf->elf, i);
  139. if (!s) {
  140. WARN_ELF("elf_getscn");
  141. return -1;
  142. }
  143. sec->idx = elf_ndxscn(s);
  144. if (!gelf_getshdr(s, &sec->sh)) {
  145. WARN_ELF("gelf_getshdr");
  146. return -1;
  147. }
  148. sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
  149. if (!sec->name) {
  150. WARN_ELF("elf_strptr");
  151. return -1;
  152. }
  153. if (sec->sh.sh_size != 0) {
  154. sec->data = elf_getdata(s, NULL);
  155. if (!sec->data) {
  156. WARN_ELF("elf_getdata");
  157. return -1;
  158. }
  159. if (sec->data->d_off != 0 ||
  160. sec->data->d_size != sec->sh.sh_size) {
  161. WARN("unexpected data attributes for %s",
  162. sec->name);
  163. return -1;
  164. }
  165. }
  166. sec->len = sec->sh.sh_size;
  167. }
  168. /* sanity check, one more call to elf_nextscn() should return NULL */
  169. if (elf_nextscn(elf->elf, s)) {
  170. WARN("section entry mismatch");
  171. return -1;
  172. }
  173. return 0;
  174. }
  175. static int read_symbols(struct elf *elf)
  176. {
  177. struct section *symtab, *sec;
  178. struct symbol *sym, *pfunc;
  179. struct list_head *entry, *tmp;
  180. int symbols_nr, i;
  181. char *coldstr;
  182. symtab = find_section_by_name(elf, ".symtab");
  183. if (!symtab) {
  184. WARN("missing symbol table");
  185. return -1;
  186. }
  187. symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
  188. for (i = 0; i < symbols_nr; i++) {
  189. sym = malloc(sizeof(*sym));
  190. if (!sym) {
  191. perror("malloc");
  192. return -1;
  193. }
  194. memset(sym, 0, sizeof(*sym));
  195. sym->idx = i;
  196. if (!gelf_getsym(symtab->data, i, &sym->sym)) {
  197. WARN_ELF("gelf_getsym");
  198. goto err;
  199. }
  200. sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
  201. sym->sym.st_name);
  202. if (!sym->name) {
  203. WARN_ELF("elf_strptr");
  204. goto err;
  205. }
  206. sym->type = GELF_ST_TYPE(sym->sym.st_info);
  207. sym->bind = GELF_ST_BIND(sym->sym.st_info);
  208. if (sym->sym.st_shndx > SHN_UNDEF &&
  209. sym->sym.st_shndx < SHN_LORESERVE) {
  210. sym->sec = find_section_by_index(elf,
  211. sym->sym.st_shndx);
  212. if (!sym->sec) {
  213. WARN("couldn't find section for symbol %s",
  214. sym->name);
  215. goto err;
  216. }
  217. if (sym->type == STT_SECTION) {
  218. sym->name = sym->sec->name;
  219. sym->sec->sym = sym;
  220. }
  221. } else
  222. sym->sec = find_section_by_index(elf, 0);
  223. sym->offset = sym->sym.st_value;
  224. sym->len = sym->sym.st_size;
  225. /* sorted insert into a per-section list */
  226. entry = &sym->sec->symbol_list;
  227. list_for_each_prev(tmp, &sym->sec->symbol_list) {
  228. struct symbol *s;
  229. s = list_entry(tmp, struct symbol, list);
  230. if (sym->offset > s->offset) {
  231. entry = tmp;
  232. break;
  233. }
  234. if (sym->offset == s->offset && sym->len >= s->len) {
  235. entry = tmp;
  236. break;
  237. }
  238. }
  239. list_add(&sym->list, entry);
  240. hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
  241. }
  242. /* Create parent/child links for any cold subfunctions */
  243. list_for_each_entry(sec, &elf->sections, list) {
  244. list_for_each_entry(sym, &sec->symbol_list, list) {
  245. if (sym->type != STT_FUNC)
  246. continue;
  247. sym->pfunc = sym->cfunc = sym;
  248. coldstr = strstr(sym->name, ".cold.");
  249. if (!coldstr)
  250. continue;
  251. coldstr[0] = '\0';
  252. pfunc = find_symbol_by_name(elf, sym->name);
  253. coldstr[0] = '.';
  254. if (!pfunc) {
  255. WARN("%s(): can't find parent function",
  256. sym->name);
  257. goto err;
  258. }
  259. sym->pfunc = pfunc;
  260. pfunc->cfunc = sym;
  261. /*
  262. * Unfortunately, -fnoreorder-functions puts the child
  263. * inside the parent. Remove the overlap so we can
  264. * have sane assumptions.
  265. *
  266. * Note that pfunc->len now no longer matches
  267. * pfunc->sym.st_size.
  268. */
  269. if (sym->sec == pfunc->sec &&
  270. sym->offset >= pfunc->offset &&
  271. sym->offset + sym->len == pfunc->offset + pfunc->len) {
  272. pfunc->len -= sym->len;
  273. }
  274. }
  275. }
  276. return 0;
  277. err:
  278. free(sym);
  279. return -1;
  280. }
  281. static int read_relas(struct elf *elf)
  282. {
  283. struct section *sec;
  284. struct rela *rela;
  285. int i;
  286. unsigned int symndx;
  287. list_for_each_entry(sec, &elf->sections, list) {
  288. if (sec->sh.sh_type != SHT_RELA)
  289. continue;
  290. sec->base = find_section_by_name(elf, sec->name + 5);
  291. if (!sec->base) {
  292. WARN("can't find base section for rela section %s",
  293. sec->name);
  294. return -1;
  295. }
  296. sec->base->rela = sec;
  297. for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
  298. rela = malloc(sizeof(*rela));
  299. if (!rela) {
  300. perror("malloc");
  301. return -1;
  302. }
  303. memset(rela, 0, sizeof(*rela));
  304. if (!gelf_getrela(sec->data, i, &rela->rela)) {
  305. WARN_ELF("gelf_getrela");
  306. return -1;
  307. }
  308. rela->type = GELF_R_TYPE(rela->rela.r_info);
  309. rela->addend = rela->rela.r_addend;
  310. rela->offset = rela->rela.r_offset;
  311. symndx = GELF_R_SYM(rela->rela.r_info);
  312. rela->sym = find_symbol_by_index(elf, symndx);
  313. if (!rela->sym) {
  314. WARN("can't find rela entry symbol %d for %s",
  315. symndx, sec->name);
  316. return -1;
  317. }
  318. list_add_tail(&rela->list, &sec->rela_list);
  319. hash_add(sec->rela_hash, &rela->hash, rela->offset);
  320. }
  321. }
  322. return 0;
  323. }
  324. struct elf *elf_open(const char *name, int flags)
  325. {
  326. struct elf *elf;
  327. Elf_Cmd cmd;
  328. elf_version(EV_CURRENT);
  329. elf = malloc(sizeof(*elf));
  330. if (!elf) {
  331. perror("malloc");
  332. return NULL;
  333. }
  334. memset(elf, 0, sizeof(*elf));
  335. INIT_LIST_HEAD(&elf->sections);
  336. elf->fd = open(name, flags);
  337. if (elf->fd == -1) {
  338. fprintf(stderr, "objtool: Can't open '%s': %s\n",
  339. name, strerror(errno));
  340. goto err;
  341. }
  342. if ((flags & O_ACCMODE) == O_RDONLY)
  343. cmd = ELF_C_READ_MMAP;
  344. else if ((flags & O_ACCMODE) == O_RDWR)
  345. cmd = ELF_C_RDWR;
  346. else /* O_WRONLY */
  347. cmd = ELF_C_WRITE;
  348. elf->elf = elf_begin(elf->fd, cmd, NULL);
  349. if (!elf->elf) {
  350. WARN_ELF("elf_begin");
  351. goto err;
  352. }
  353. if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
  354. WARN_ELF("gelf_getehdr");
  355. goto err;
  356. }
  357. if (read_sections(elf))
  358. goto err;
  359. if (read_symbols(elf))
  360. goto err;
  361. if (read_relas(elf))
  362. goto err;
  363. return elf;
  364. err:
  365. elf_close(elf);
  366. return NULL;
  367. }
  368. struct section *elf_create_section(struct elf *elf, const char *name,
  369. size_t entsize, int nr)
  370. {
  371. struct section *sec, *shstrtab;
  372. size_t size = entsize * nr;
  373. struct Elf_Scn *s;
  374. Elf_Data *data;
  375. sec = malloc(sizeof(*sec));
  376. if (!sec) {
  377. perror("malloc");
  378. return NULL;
  379. }
  380. memset(sec, 0, sizeof(*sec));
  381. INIT_LIST_HEAD(&sec->symbol_list);
  382. INIT_LIST_HEAD(&sec->rela_list);
  383. hash_init(sec->rela_hash);
  384. hash_init(sec->symbol_hash);
  385. list_add_tail(&sec->list, &elf->sections);
  386. s = elf_newscn(elf->elf);
  387. if (!s) {
  388. WARN_ELF("elf_newscn");
  389. return NULL;
  390. }
  391. sec->name = strdup(name);
  392. if (!sec->name) {
  393. perror("strdup");
  394. return NULL;
  395. }
  396. sec->idx = elf_ndxscn(s);
  397. sec->len = size;
  398. sec->changed = true;
  399. sec->data = elf_newdata(s);
  400. if (!sec->data) {
  401. WARN_ELF("elf_newdata");
  402. return NULL;
  403. }
  404. sec->data->d_size = size;
  405. sec->data->d_align = 1;
  406. if (size) {
  407. sec->data->d_buf = malloc(size);
  408. if (!sec->data->d_buf) {
  409. perror("malloc");
  410. return NULL;
  411. }
  412. memset(sec->data->d_buf, 0, size);
  413. }
  414. if (!gelf_getshdr(s, &sec->sh)) {
  415. WARN_ELF("gelf_getshdr");
  416. return NULL;
  417. }
  418. sec->sh.sh_size = size;
  419. sec->sh.sh_entsize = entsize;
  420. sec->sh.sh_type = SHT_PROGBITS;
  421. sec->sh.sh_addralign = 1;
  422. sec->sh.sh_flags = SHF_ALLOC;
  423. /* Add section name to .shstrtab (or .strtab for Clang) */
  424. shstrtab = find_section_by_name(elf, ".shstrtab");
  425. if (!shstrtab)
  426. shstrtab = find_section_by_name(elf, ".strtab");
  427. if (!shstrtab) {
  428. WARN("can't find .shstrtab or .strtab section");
  429. return NULL;
  430. }
  431. s = elf_getscn(elf->elf, shstrtab->idx);
  432. if (!s) {
  433. WARN_ELF("elf_getscn");
  434. return NULL;
  435. }
  436. data = elf_newdata(s);
  437. if (!data) {
  438. WARN_ELF("elf_newdata");
  439. return NULL;
  440. }
  441. data->d_buf = sec->name;
  442. data->d_size = strlen(name) + 1;
  443. data->d_align = 1;
  444. sec->sh.sh_name = shstrtab->len;
  445. shstrtab->len += strlen(name) + 1;
  446. shstrtab->changed = true;
  447. return sec;
  448. }
  449. struct section *elf_create_rela_section(struct elf *elf, struct section *base)
  450. {
  451. char *relaname;
  452. struct section *sec;
  453. relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
  454. if (!relaname) {
  455. perror("malloc");
  456. return NULL;
  457. }
  458. strcpy(relaname, ".rela");
  459. strcat(relaname, base->name);
  460. sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
  461. free(relaname);
  462. if (!sec)
  463. return NULL;
  464. base->rela = sec;
  465. sec->base = base;
  466. sec->sh.sh_type = SHT_RELA;
  467. sec->sh.sh_addralign = 8;
  468. sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
  469. sec->sh.sh_info = base->idx;
  470. sec->sh.sh_flags = SHF_INFO_LINK;
  471. return sec;
  472. }
  473. int elf_rebuild_rela_section(struct section *sec)
  474. {
  475. struct rela *rela;
  476. int nr, idx = 0, size;
  477. GElf_Rela *relas;
  478. nr = 0;
  479. list_for_each_entry(rela, &sec->rela_list, list)
  480. nr++;
  481. size = nr * sizeof(*relas);
  482. relas = malloc(size);
  483. if (!relas) {
  484. perror("malloc");
  485. return -1;
  486. }
  487. sec->data->d_buf = relas;
  488. sec->data->d_size = size;
  489. sec->sh.sh_size = size;
  490. idx = 0;
  491. list_for_each_entry(rela, &sec->rela_list, list) {
  492. relas[idx].r_offset = rela->offset;
  493. relas[idx].r_addend = rela->addend;
  494. relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
  495. idx++;
  496. }
  497. return 0;
  498. }
  499. int elf_write(struct elf *elf)
  500. {
  501. struct section *sec;
  502. Elf_Scn *s;
  503. /* Update section headers for changed sections: */
  504. list_for_each_entry(sec, &elf->sections, list) {
  505. if (sec->changed) {
  506. s = elf_getscn(elf->elf, sec->idx);
  507. if (!s) {
  508. WARN_ELF("elf_getscn");
  509. return -1;
  510. }
  511. if (!gelf_update_shdr(s, &sec->sh)) {
  512. WARN_ELF("gelf_update_shdr");
  513. return -1;
  514. }
  515. }
  516. }
  517. /* Make sure the new section header entries get updated properly. */
  518. elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
  519. /* Write all changes to the file. */
  520. if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
  521. WARN_ELF("elf_update");
  522. return -1;
  523. }
  524. return 0;
  525. }
  526. void elf_close(struct elf *elf)
  527. {
  528. struct section *sec, *tmpsec;
  529. struct symbol *sym, *tmpsym;
  530. struct rela *rela, *tmprela;
  531. if (elf->elf)
  532. elf_end(elf->elf);
  533. if (elf->fd > 0)
  534. close(elf->fd);
  535. list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
  536. list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
  537. list_del(&sym->list);
  538. hash_del(&sym->hash);
  539. free(sym);
  540. }
  541. list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
  542. list_del(&rela->list);
  543. hash_del(&rela->hash);
  544. free(rela);
  545. }
  546. list_del(&sec->list);
  547. free(sec);
  548. }
  549. free(elf);
  550. }