elf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. coldstr[0] = '\0';
  251. pfunc = find_symbol_by_name(elf, sym->name);
  252. coldstr[0] = '.';
  253. if (!pfunc) {
  254. WARN("%s(): can't find parent function",
  255. sym->name);
  256. goto err;
  257. }
  258. sym->pfunc = pfunc;
  259. pfunc->cfunc = sym;
  260. }
  261. }
  262. }
  263. return 0;
  264. err:
  265. free(sym);
  266. return -1;
  267. }
  268. static int read_relas(struct elf *elf)
  269. {
  270. struct section *sec;
  271. struct rela *rela;
  272. int i;
  273. unsigned int symndx;
  274. list_for_each_entry(sec, &elf->sections, list) {
  275. if (sec->sh.sh_type != SHT_RELA)
  276. continue;
  277. sec->base = find_section_by_name(elf, sec->name + 5);
  278. if (!sec->base) {
  279. WARN("can't find base section for rela section %s",
  280. sec->name);
  281. return -1;
  282. }
  283. sec->base->rela = sec;
  284. for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
  285. rela = malloc(sizeof(*rela));
  286. if (!rela) {
  287. perror("malloc");
  288. return -1;
  289. }
  290. memset(rela, 0, sizeof(*rela));
  291. if (!gelf_getrela(sec->data, i, &rela->rela)) {
  292. WARN_ELF("gelf_getrela");
  293. return -1;
  294. }
  295. rela->type = GELF_R_TYPE(rela->rela.r_info);
  296. rela->addend = rela->rela.r_addend;
  297. rela->offset = rela->rela.r_offset;
  298. symndx = GELF_R_SYM(rela->rela.r_info);
  299. rela->sym = find_symbol_by_index(elf, symndx);
  300. if (!rela->sym) {
  301. WARN("can't find rela entry symbol %d for %s",
  302. symndx, sec->name);
  303. return -1;
  304. }
  305. list_add_tail(&rela->list, &sec->rela_list);
  306. hash_add(sec->rela_hash, &rela->hash, rela->offset);
  307. }
  308. }
  309. return 0;
  310. }
  311. struct elf *elf_open(const char *name, int flags)
  312. {
  313. struct elf *elf;
  314. Elf_Cmd cmd;
  315. elf_version(EV_CURRENT);
  316. elf = malloc(sizeof(*elf));
  317. if (!elf) {
  318. perror("malloc");
  319. return NULL;
  320. }
  321. memset(elf, 0, sizeof(*elf));
  322. INIT_LIST_HEAD(&elf->sections);
  323. elf->fd = open(name, flags);
  324. if (elf->fd == -1) {
  325. fprintf(stderr, "objtool: Can't open '%s': %s\n",
  326. name, strerror(errno));
  327. goto err;
  328. }
  329. if ((flags & O_ACCMODE) == O_RDONLY)
  330. cmd = ELF_C_READ_MMAP;
  331. else if ((flags & O_ACCMODE) == O_RDWR)
  332. cmd = ELF_C_RDWR;
  333. else /* O_WRONLY */
  334. cmd = ELF_C_WRITE;
  335. elf->elf = elf_begin(elf->fd, cmd, NULL);
  336. if (!elf->elf) {
  337. WARN_ELF("elf_begin");
  338. goto err;
  339. }
  340. if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
  341. WARN_ELF("gelf_getehdr");
  342. goto err;
  343. }
  344. if (read_sections(elf))
  345. goto err;
  346. if (read_symbols(elf))
  347. goto err;
  348. if (read_relas(elf))
  349. goto err;
  350. return elf;
  351. err:
  352. elf_close(elf);
  353. return NULL;
  354. }
  355. struct section *elf_create_section(struct elf *elf, const char *name,
  356. size_t entsize, int nr)
  357. {
  358. struct section *sec, *shstrtab;
  359. size_t size = entsize * nr;
  360. struct Elf_Scn *s;
  361. Elf_Data *data;
  362. sec = malloc(sizeof(*sec));
  363. if (!sec) {
  364. perror("malloc");
  365. return NULL;
  366. }
  367. memset(sec, 0, sizeof(*sec));
  368. INIT_LIST_HEAD(&sec->symbol_list);
  369. INIT_LIST_HEAD(&sec->rela_list);
  370. hash_init(sec->rela_hash);
  371. hash_init(sec->symbol_hash);
  372. list_add_tail(&sec->list, &elf->sections);
  373. s = elf_newscn(elf->elf);
  374. if (!s) {
  375. WARN_ELF("elf_newscn");
  376. return NULL;
  377. }
  378. sec->name = strdup(name);
  379. if (!sec->name) {
  380. perror("strdup");
  381. return NULL;
  382. }
  383. sec->idx = elf_ndxscn(s);
  384. sec->len = size;
  385. sec->changed = true;
  386. sec->data = elf_newdata(s);
  387. if (!sec->data) {
  388. WARN_ELF("elf_newdata");
  389. return NULL;
  390. }
  391. sec->data->d_size = size;
  392. sec->data->d_align = 1;
  393. if (size) {
  394. sec->data->d_buf = malloc(size);
  395. if (!sec->data->d_buf) {
  396. perror("malloc");
  397. return NULL;
  398. }
  399. memset(sec->data->d_buf, 0, size);
  400. }
  401. if (!gelf_getshdr(s, &sec->sh)) {
  402. WARN_ELF("gelf_getshdr");
  403. return NULL;
  404. }
  405. sec->sh.sh_size = size;
  406. sec->sh.sh_entsize = entsize;
  407. sec->sh.sh_type = SHT_PROGBITS;
  408. sec->sh.sh_addralign = 1;
  409. sec->sh.sh_flags = SHF_ALLOC;
  410. /* Add section name to .shstrtab */
  411. shstrtab = find_section_by_name(elf, ".shstrtab");
  412. if (!shstrtab) {
  413. WARN("can't find .shstrtab section");
  414. return NULL;
  415. }
  416. s = elf_getscn(elf->elf, shstrtab->idx);
  417. if (!s) {
  418. WARN_ELF("elf_getscn");
  419. return NULL;
  420. }
  421. data = elf_newdata(s);
  422. if (!data) {
  423. WARN_ELF("elf_newdata");
  424. return NULL;
  425. }
  426. data->d_buf = sec->name;
  427. data->d_size = strlen(name) + 1;
  428. data->d_align = 1;
  429. sec->sh.sh_name = shstrtab->len;
  430. shstrtab->len += strlen(name) + 1;
  431. shstrtab->changed = true;
  432. return sec;
  433. }
  434. struct section *elf_create_rela_section(struct elf *elf, struct section *base)
  435. {
  436. char *relaname;
  437. struct section *sec;
  438. relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
  439. if (!relaname) {
  440. perror("malloc");
  441. return NULL;
  442. }
  443. strcpy(relaname, ".rela");
  444. strcat(relaname, base->name);
  445. sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
  446. free(relaname);
  447. if (!sec)
  448. return NULL;
  449. base->rela = sec;
  450. sec->base = base;
  451. sec->sh.sh_type = SHT_RELA;
  452. sec->sh.sh_addralign = 8;
  453. sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
  454. sec->sh.sh_info = base->idx;
  455. sec->sh.sh_flags = SHF_INFO_LINK;
  456. return sec;
  457. }
  458. int elf_rebuild_rela_section(struct section *sec)
  459. {
  460. struct rela *rela;
  461. int nr, idx = 0, size;
  462. GElf_Rela *relas;
  463. nr = 0;
  464. list_for_each_entry(rela, &sec->rela_list, list)
  465. nr++;
  466. size = nr * sizeof(*relas);
  467. relas = malloc(size);
  468. if (!relas) {
  469. perror("malloc");
  470. return -1;
  471. }
  472. sec->data->d_buf = relas;
  473. sec->data->d_size = size;
  474. sec->sh.sh_size = size;
  475. idx = 0;
  476. list_for_each_entry(rela, &sec->rela_list, list) {
  477. relas[idx].r_offset = rela->offset;
  478. relas[idx].r_addend = rela->addend;
  479. relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
  480. idx++;
  481. }
  482. return 0;
  483. }
  484. int elf_write(struct elf *elf)
  485. {
  486. struct section *sec;
  487. Elf_Scn *s;
  488. /* Update section headers for changed sections: */
  489. list_for_each_entry(sec, &elf->sections, list) {
  490. if (sec->changed) {
  491. s = elf_getscn(elf->elf, sec->idx);
  492. if (!s) {
  493. WARN_ELF("elf_getscn");
  494. return -1;
  495. }
  496. if (!gelf_update_shdr(s, &sec->sh)) {
  497. WARN_ELF("gelf_update_shdr");
  498. return -1;
  499. }
  500. }
  501. }
  502. /* Make sure the new section header entries get updated properly. */
  503. elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
  504. /* Write all changes to the file. */
  505. if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
  506. WARN_ELF("elf_update");
  507. return -1;
  508. }
  509. return 0;
  510. }
  511. void elf_close(struct elf *elf)
  512. {
  513. struct section *sec, *tmpsec;
  514. struct symbol *sym, *tmpsym;
  515. struct rela *rela, *tmprela;
  516. if (elf->elf)
  517. elf_end(elf->elf);
  518. if (elf->fd > 0)
  519. close(elf->fd);
  520. list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
  521. list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
  522. list_del(&sym->list);
  523. hash_del(&sym->hash);
  524. free(sym);
  525. }
  526. list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
  527. list_del(&rela->list);
  528. hash_del(&rela->hash);
  529. free(rela);
  530. }
  531. list_del(&sec->list);
  532. free(sec);
  533. }
  534. free(elf);
  535. }