elf.c 8.9 KB

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