elf.c 8.6 KB

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