elf.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 "elf.h"
  29. #include "warn.h"
  30. struct section *find_section_by_name(struct elf *elf, const char *name)
  31. {
  32. struct section *sec;
  33. list_for_each_entry(sec, &elf->sections, list)
  34. if (!strcmp(sec->name, name))
  35. return sec;
  36. return NULL;
  37. }
  38. static struct section *find_section_by_index(struct elf *elf,
  39. unsigned int idx)
  40. {
  41. struct section *sec;
  42. list_for_each_entry(sec, &elf->sections, list)
  43. if (sec->idx == idx)
  44. return sec;
  45. return NULL;
  46. }
  47. static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
  48. {
  49. struct section *sec;
  50. struct symbol *sym;
  51. list_for_each_entry(sec, &elf->sections, list)
  52. hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
  53. if (sym->idx == idx)
  54. return sym;
  55. return NULL;
  56. }
  57. struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
  58. {
  59. struct symbol *sym;
  60. list_for_each_entry(sym, &sec->symbol_list, list)
  61. if (sym->type != STT_SECTION &&
  62. sym->offset == offset)
  63. return sym;
  64. return NULL;
  65. }
  66. struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
  67. unsigned int len)
  68. {
  69. struct rela *rela;
  70. unsigned long o;
  71. if (!sec->rela)
  72. return NULL;
  73. for (o = offset; o < offset + len; o++)
  74. hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
  75. if (rela->offset == o)
  76. return rela;
  77. return NULL;
  78. }
  79. struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
  80. {
  81. return find_rela_by_dest_range(sec, offset, 1);
  82. }
  83. struct symbol *find_containing_func(struct section *sec, unsigned long offset)
  84. {
  85. struct symbol *func;
  86. list_for_each_entry(func, &sec->symbol_list, list)
  87. if (func->type == STT_FUNC && offset >= func->offset &&
  88. offset < func->offset + func->len)
  89. return func;
  90. return NULL;
  91. }
  92. static int read_sections(struct elf *elf)
  93. {
  94. Elf_Scn *s = NULL;
  95. struct section *sec;
  96. size_t shstrndx, sections_nr;
  97. int i;
  98. if (elf_getshdrnum(elf->elf, &sections_nr)) {
  99. perror("elf_getshdrnum");
  100. return -1;
  101. }
  102. if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
  103. perror("elf_getshdrstrndx");
  104. return -1;
  105. }
  106. for (i = 0; i < sections_nr; i++) {
  107. sec = malloc(sizeof(*sec));
  108. if (!sec) {
  109. perror("malloc");
  110. return -1;
  111. }
  112. memset(sec, 0, sizeof(*sec));
  113. INIT_LIST_HEAD(&sec->symbol_list);
  114. INIT_LIST_HEAD(&sec->rela_list);
  115. hash_init(sec->rela_hash);
  116. hash_init(sec->symbol_hash);
  117. list_add_tail(&sec->list, &elf->sections);
  118. s = elf_getscn(elf->elf, i);
  119. if (!s) {
  120. perror("elf_getscn");
  121. return -1;
  122. }
  123. sec->idx = elf_ndxscn(s);
  124. if (!gelf_getshdr(s, &sec->sh)) {
  125. perror("gelf_getshdr");
  126. return -1;
  127. }
  128. sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
  129. if (!sec->name) {
  130. perror("elf_strptr");
  131. return -1;
  132. }
  133. sec->elf_data = elf_getdata(s, NULL);
  134. if (!sec->elf_data) {
  135. perror("elf_getdata");
  136. return -1;
  137. }
  138. if (sec->elf_data->d_off != 0 ||
  139. sec->elf_data->d_size != sec->sh.sh_size) {
  140. WARN("unexpected data attributes for %s", sec->name);
  141. return -1;
  142. }
  143. sec->data = (unsigned long)sec->elf_data->d_buf;
  144. sec->len = sec->elf_data->d_size;
  145. }
  146. /* sanity check, one more call to elf_nextscn() should return NULL */
  147. if (elf_nextscn(elf->elf, s)) {
  148. WARN("section entry mismatch");
  149. return -1;
  150. }
  151. return 0;
  152. }
  153. static int read_symbols(struct elf *elf)
  154. {
  155. struct section *symtab;
  156. struct symbol *sym;
  157. struct list_head *entry, *tmp;
  158. int symbols_nr, i;
  159. symtab = find_section_by_name(elf, ".symtab");
  160. if (!symtab) {
  161. WARN("missing symbol table");
  162. return -1;
  163. }
  164. symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
  165. for (i = 0; i < symbols_nr; i++) {
  166. sym = malloc(sizeof(*sym));
  167. if (!sym) {
  168. perror("malloc");
  169. return -1;
  170. }
  171. memset(sym, 0, sizeof(*sym));
  172. sym->idx = i;
  173. if (!gelf_getsym(symtab->elf_data, i, &sym->sym)) {
  174. perror("gelf_getsym");
  175. goto err;
  176. }
  177. sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
  178. sym->sym.st_name);
  179. if (!sym->name) {
  180. perror("elf_strptr");
  181. goto err;
  182. }
  183. sym->type = GELF_ST_TYPE(sym->sym.st_info);
  184. sym->bind = GELF_ST_BIND(sym->sym.st_info);
  185. if (sym->sym.st_shndx > SHN_UNDEF &&
  186. sym->sym.st_shndx < SHN_LORESERVE) {
  187. sym->sec = find_section_by_index(elf,
  188. sym->sym.st_shndx);
  189. if (!sym->sec) {
  190. WARN("couldn't find section for symbol %s",
  191. sym->name);
  192. goto err;
  193. }
  194. if (sym->type == STT_SECTION) {
  195. sym->name = sym->sec->name;
  196. sym->sec->sym = sym;
  197. }
  198. } else
  199. sym->sec = find_section_by_index(elf, 0);
  200. sym->offset = sym->sym.st_value;
  201. sym->len = sym->sym.st_size;
  202. /* sorted insert into a per-section list */
  203. entry = &sym->sec->symbol_list;
  204. list_for_each_prev(tmp, &sym->sec->symbol_list) {
  205. struct symbol *s;
  206. s = list_entry(tmp, struct symbol, list);
  207. if (sym->offset > s->offset) {
  208. entry = tmp;
  209. break;
  210. }
  211. if (sym->offset == s->offset && sym->len >= s->len) {
  212. entry = tmp;
  213. break;
  214. }
  215. }
  216. list_add(&sym->list, entry);
  217. hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
  218. }
  219. return 0;
  220. err:
  221. free(sym);
  222. return -1;
  223. }
  224. static int read_relas(struct elf *elf)
  225. {
  226. struct section *sec;
  227. struct rela *rela;
  228. int i;
  229. unsigned int symndx;
  230. list_for_each_entry(sec, &elf->sections, list) {
  231. if (sec->sh.sh_type != SHT_RELA)
  232. continue;
  233. sec->base = find_section_by_name(elf, sec->name + 5);
  234. if (!sec->base) {
  235. WARN("can't find base section for rela section %s",
  236. sec->name);
  237. return -1;
  238. }
  239. sec->base->rela = sec;
  240. for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
  241. rela = malloc(sizeof(*rela));
  242. if (!rela) {
  243. perror("malloc");
  244. return -1;
  245. }
  246. memset(rela, 0, sizeof(*rela));
  247. if (!gelf_getrela(sec->elf_data, i, &rela->rela)) {
  248. perror("gelf_getrela");
  249. return -1;
  250. }
  251. rela->type = GELF_R_TYPE(rela->rela.r_info);
  252. rela->addend = rela->rela.r_addend;
  253. rela->offset = rela->rela.r_offset;
  254. symndx = GELF_R_SYM(rela->rela.r_info);
  255. rela->sym = find_symbol_by_index(elf, symndx);
  256. if (!rela->sym) {
  257. WARN("can't find rela entry symbol %d for %s",
  258. symndx, sec->name);
  259. return -1;
  260. }
  261. list_add_tail(&rela->list, &sec->rela_list);
  262. hash_add(sec->rela_hash, &rela->hash, rela->offset);
  263. }
  264. }
  265. return 0;
  266. }
  267. struct elf *elf_open(const char *name)
  268. {
  269. struct elf *elf;
  270. elf_version(EV_CURRENT);
  271. elf = malloc(sizeof(*elf));
  272. if (!elf) {
  273. perror("malloc");
  274. return NULL;
  275. }
  276. memset(elf, 0, sizeof(*elf));
  277. INIT_LIST_HEAD(&elf->sections);
  278. elf->name = strdup(name);
  279. if (!elf->name) {
  280. perror("strdup");
  281. goto err;
  282. }
  283. elf->fd = open(name, O_RDONLY);
  284. if (elf->fd == -1) {
  285. perror("open");
  286. goto err;
  287. }
  288. elf->elf = elf_begin(elf->fd, ELF_C_READ_MMAP, NULL);
  289. if (!elf->elf) {
  290. perror("elf_begin");
  291. goto err;
  292. }
  293. if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
  294. perror("gelf_getehdr");
  295. goto err;
  296. }
  297. if (read_sections(elf))
  298. goto err;
  299. if (read_symbols(elf))
  300. goto err;
  301. if (read_relas(elf))
  302. goto err;
  303. return elf;
  304. err:
  305. elf_close(elf);
  306. return NULL;
  307. }
  308. void elf_close(struct elf *elf)
  309. {
  310. struct section *sec, *tmpsec;
  311. struct symbol *sym, *tmpsym;
  312. struct rela *rela, *tmprela;
  313. list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
  314. list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
  315. list_del(&sym->list);
  316. hash_del(&sym->hash);
  317. free(sym);
  318. }
  319. list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
  320. list_del(&rela->list);
  321. hash_del(&rela->hash);
  322. free(rela);
  323. }
  324. list_del(&sec->list);
  325. free(sec);
  326. }
  327. if (elf->name)
  328. free(elf->name);
  329. if (elf->fd > 0)
  330. close(elf->fd);
  331. if (elf->elf)
  332. elf_end(elf->elf);
  333. free(elf);
  334. }