elf.c 14 KB

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