cook.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * cook.c - read and translate ELF files.
  3. * Copyright (C) 1995 - 2006 Michael Riepe
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. */
  19. #include <private.h>
  20. #ifndef lint
  21. static const char rcsid[] = "@(#) $Id: cook.c,v 1.29 2008/05/23 08:15:34 michael Exp $";
  22. #endif /* lint */
  23. const Elf_Scn _elf_scn_init = INIT_SCN;
  24. const Scn_Data _elf_data_init = INIT_DATA;
  25. Elf_Type
  26. _elf_scn_type(unsigned t) {
  27. switch (t) {
  28. case SHT_DYNAMIC: return ELF_T_DYN;
  29. case SHT_DYNSYM: return ELF_T_SYM;
  30. case SHT_HASH: return ELF_T_WORD;
  31. case SHT_REL: return ELF_T_REL;
  32. case SHT_RELA: return ELF_T_RELA;
  33. case SHT_SYMTAB: return ELF_T_SYM;
  34. case SHT_SYMTAB_SHNDX: return ELF_T_WORD; /* XXX: really? */
  35. #if __LIBELF_SYMBOL_VERSIONS
  36. #if __LIBELF_SUN_SYMBOL_VERSIONS
  37. case SHT_SUNW_verdef: return ELF_T_VDEF;
  38. case SHT_SUNW_verneed: return ELF_T_VNEED;
  39. case SHT_SUNW_versym: return ELF_T_HALF;
  40. #else /* __LIBELF_SUN_SYMBOL_VERSIONS */
  41. case SHT_GNU_verdef: return ELF_T_VDEF;
  42. case SHT_GNU_verneed: return ELF_T_VNEED;
  43. case SHT_GNU_versym: return ELF_T_HALF;
  44. #endif /* __LIBELF_SUN_SYMBOL_VERSIONS */
  45. #endif /* __LIBELF_SYMBOL_VERSIONS */
  46. }
  47. return ELF_T_BYTE;
  48. }
  49. /*
  50. * Check for overflow on 32-bit systems
  51. */
  52. #define overflow(a,b,t) (sizeof(a) < sizeof(t) && (t)(a) != (b))
  53. #define truncerr(t) ((t)==ELF_T_EHDR?ERROR_TRUNC_EHDR: \
  54. ((t)==ELF_T_PHDR?ERROR_TRUNC_PHDR: \
  55. ERROR_INTERNAL))
  56. #define memerr(t) ((t)==ELF_T_EHDR?ERROR_MEM_EHDR: \
  57. ((t)==ELF_T_PHDR?ERROR_MEM_PHDR: \
  58. ERROR_INTERNAL))
  59. Elf_Data*
  60. _elf_xlatetom(const Elf *elf, Elf_Data *dst, const Elf_Data *src) {
  61. if (elf->e_class == ELFCLASS32) {
  62. return elf32_xlatetom(dst, src, elf->e_encoding);
  63. }
  64. #if __LIBELF64
  65. else if (elf->e_class == ELFCLASS64) {
  66. return elf64_xlatetom(dst, src, elf->e_encoding);
  67. }
  68. #endif /* __LIBELF64 */
  69. seterr(ERROR_UNIMPLEMENTED);
  70. return NULL;
  71. }
  72. static char*
  73. _elf_item(void *buf, Elf *elf, Elf_Type type, size_t off) {
  74. Elf_Data src, dst;
  75. elf_assert(valid_type(type));
  76. if (off < 0 || off > elf->e_size) {
  77. seterr(ERROR_OUTSIDE);
  78. return NULL;
  79. }
  80. src.d_type = type;
  81. src.d_version = elf->e_version;
  82. src.d_size = _fsize(elf->e_class, src.d_version, type);
  83. elf_assert(src.d_size);
  84. if ((elf->e_size - off) < src.d_size) {
  85. seterr(truncerr(type));
  86. return NULL;
  87. }
  88. dst.d_version = _elf_version;
  89. dst.d_size = _msize(elf->e_class, dst.d_version, type);
  90. elf_assert(dst.d_size);
  91. if (!(dst.d_buf = buf) && !(dst.d_buf = malloc(dst.d_size))) {
  92. seterr(memerr(type));
  93. return NULL;
  94. }
  95. elf_assert(elf->e_data);
  96. if (elf->e_rawdata) {
  97. src.d_buf = elf->e_rawdata + off;
  98. }
  99. else {
  100. src.d_buf = elf->e_data + off;
  101. }
  102. if (_elf_xlatetom(elf, &dst, &src)) {
  103. return (char*)dst.d_buf;
  104. }
  105. if (dst.d_buf != buf) {
  106. free(dst.d_buf);
  107. }
  108. return NULL;
  109. }
  110. static int
  111. _elf_cook_phdr(Elf *elf) {
  112. size_t num, off, entsz;
  113. if (elf->e_class == ELFCLASS32) {
  114. num = ((Elf32_Ehdr*)elf->e_ehdr)->e_phnum;
  115. off = ((Elf32_Ehdr*)elf->e_ehdr)->e_phoff;
  116. entsz = ((Elf32_Ehdr*)elf->e_ehdr)->e_phentsize;
  117. }
  118. #if __LIBELF64
  119. else if (elf->e_class == ELFCLASS64) {
  120. num = ((Elf64_Ehdr*)elf->e_ehdr)->e_phnum;
  121. off = ((Elf64_Ehdr*)elf->e_ehdr)->e_phoff;
  122. entsz = ((Elf64_Ehdr*)elf->e_ehdr)->e_phentsize;
  123. /*
  124. * Check for overflow on 32-bit systems
  125. */
  126. if (overflow(off, ((Elf64_Ehdr*)elf->e_ehdr)->e_phoff, Elf64_Off)) {
  127. seterr(ERROR_OUTSIDE);
  128. return 0;
  129. }
  130. }
  131. #endif /* __LIBELF64 */
  132. else {
  133. seterr(ERROR_UNIMPLEMENTED);
  134. return 0;
  135. }
  136. if (off) {
  137. Elf_Scn *scn;
  138. size_t size;
  139. unsigned i;
  140. char *p;
  141. if (num == PN_XNUM) {
  142. /*
  143. * Overflow in ehdr->e_phnum.
  144. * Get real value from first SHDR.
  145. */
  146. if (!(scn = elf->e_scn_1)) {
  147. seterr(ERROR_NOSUCHSCN);
  148. return 0;
  149. }
  150. if (elf->e_class == ELFCLASS32) {
  151. num = scn->s_shdr32.sh_info;
  152. }
  153. #if __LIBELF64
  154. else if (elf->e_class == ELFCLASS64) {
  155. num = scn->s_shdr64.sh_info;
  156. }
  157. #endif /* __LIBELF64 */
  158. /* we already had this
  159. else {
  160. seterr(ERROR_UNIMPLEMENTED);
  161. return 0;
  162. }
  163. */
  164. }
  165. size = _fsize(elf->e_class, elf->e_version, ELF_T_PHDR);
  166. elf_assert(size);
  167. #if ENABLE_EXTENDED_FORMAT
  168. if (entsz < size) {
  169. #else /* ENABLE_EXTENDED_FORMAT */
  170. if (entsz != size) {
  171. #endif /* ENABLE_EXTENDED_FORMAT */
  172. seterr(ERROR_EHDR_PHENTSIZE);
  173. return 0;
  174. }
  175. size = _msize(elf->e_class, _elf_version, ELF_T_PHDR);
  176. elf_assert(size);
  177. if (!(p = malloc(num * size))) {
  178. seterr(memerr(ELF_T_PHDR));
  179. return 0;
  180. }
  181. for (i = 0; i < num; i++) {
  182. if (!_elf_item(p + i * size, elf, ELF_T_PHDR, off + i * entsz)) {
  183. free(p);
  184. return 0;
  185. }
  186. }
  187. elf->e_phdr = p;
  188. elf->e_phnum = num;
  189. }
  190. return 1;
  191. }
  192. static int
  193. _elf_cook_shdr(Elf *elf) {
  194. size_t num, off, entsz;
  195. if (elf->e_class == ELFCLASS32) {
  196. num = ((Elf32_Ehdr*)elf->e_ehdr)->e_shnum;
  197. off = ((Elf32_Ehdr*)elf->e_ehdr)->e_shoff;
  198. entsz = ((Elf32_Ehdr*)elf->e_ehdr)->e_shentsize;
  199. }
  200. #if __LIBELF64
  201. else if (elf->e_class == ELFCLASS64) {
  202. num = ((Elf64_Ehdr*)elf->e_ehdr)->e_shnum;
  203. off = ((Elf64_Ehdr*)elf->e_ehdr)->e_shoff;
  204. entsz = ((Elf64_Ehdr*)elf->e_ehdr)->e_shentsize;
  205. /*
  206. * Check for overflow on 32-bit systems
  207. */
  208. if (overflow(off, ((Elf64_Ehdr*)elf->e_ehdr)->e_shoff, Elf64_Off)) {
  209. seterr(ERROR_OUTSIDE);
  210. return 0;
  211. }
  212. }
  213. #endif /* __LIBELF64 */
  214. else {
  215. seterr(ERROR_UNIMPLEMENTED);
  216. return 0;
  217. }
  218. if (off) {
  219. struct tmp {
  220. Elf_Scn scn;
  221. Scn_Data data;
  222. } *head;
  223. Elf_Data src, dst;
  224. Elf_Scn *scn;
  225. Scn_Data *sd;
  226. unsigned i;
  227. if (off < 0 || off > elf->e_size) {
  228. seterr(ERROR_OUTSIDE);
  229. return 0;
  230. }
  231. src.d_type = ELF_T_SHDR;
  232. src.d_version = elf->e_version;
  233. src.d_size = _fsize(elf->e_class, src.d_version, ELF_T_SHDR);
  234. elf_assert(src.d_size);
  235. #if ENABLE_EXTENDED_FORMAT
  236. if (entsz < src.d_size) {
  237. #else /* ENABLE_EXTENDED_FORMAT */
  238. if (entsz != src.d_size) {
  239. #endif /* ENABLE_EXTENDED_FORMAT */
  240. seterr(ERROR_EHDR_SHENTSIZE);
  241. return 0;
  242. }
  243. dst.d_version = EV_CURRENT;
  244. if (num == 0) {
  245. union {
  246. Elf32_Shdr sh32;
  247. #if __LIBELF64
  248. Elf64_Shdr sh64;
  249. #endif /* __LIBELF64 */
  250. } u;
  251. /*
  252. * Overflow in ehdr->e_shnum.
  253. * Get real value from first SHDR.
  254. */
  255. if (elf->e_size - off < entsz) {
  256. seterr(ERROR_TRUNC_SHDR);
  257. return 0;
  258. }
  259. if (elf->e_rawdata) {
  260. src.d_buf = elf->e_rawdata + off;
  261. }
  262. else {
  263. src.d_buf = elf->e_data + off;
  264. }
  265. dst.d_buf = &u;
  266. dst.d_size = sizeof(u);
  267. if (!_elf_xlatetom(elf, &dst, &src)) {
  268. return 0;
  269. }
  270. elf_assert(dst.d_size == _msize(elf->e_class, EV_CURRENT, ELF_T_SHDR));
  271. elf_assert(dst.d_type == ELF_T_SHDR);
  272. if (elf->e_class == ELFCLASS32) {
  273. num = u.sh32.sh_size;
  274. }
  275. #if __LIBELF64
  276. else if (elf->e_class == ELFCLASS64) {
  277. num = u.sh64.sh_size;
  278. /*
  279. * Check for overflow on 32-bit systems
  280. */
  281. if (overflow(num, u.sh64.sh_size, Elf64_Xword)) {
  282. seterr(ERROR_OUTSIDE);
  283. return 0;
  284. }
  285. }
  286. #endif /* __LIBELF64 */
  287. }
  288. if ((elf->e_size - off) / entsz < num) {
  289. seterr(ERROR_TRUNC_SHDR);
  290. return 0;
  291. }
  292. if (!(head = (struct tmp*)malloc(num * sizeof(struct tmp)))) {
  293. seterr(ERROR_MEM_SCN);
  294. return 0;
  295. }
  296. for (scn = NULL, i = num; i-- > 0; ) {
  297. head[i].scn = _elf_scn_init;
  298. head[i].data = _elf_data_init;
  299. head[i].scn.s_link = scn;
  300. if (!scn) {
  301. elf->e_scn_n = &head[i].scn;
  302. }
  303. scn = &head[i].scn;
  304. sd = &head[i].data;
  305. if (elf->e_rawdata) {
  306. src.d_buf = elf->e_rawdata + off + i * entsz;
  307. }
  308. else {
  309. src.d_buf = elf->e_data + off + i * entsz;
  310. }
  311. dst.d_buf = &scn->s_uhdr;
  312. dst.d_size = sizeof(scn->s_uhdr);
  313. if (!_elf_xlatetom(elf, &dst, &src)) {
  314. elf->e_scn_n = NULL;
  315. free(head);
  316. return 0;
  317. }
  318. elf_assert(dst.d_size == _msize(elf->e_class, EV_CURRENT, ELF_T_SHDR));
  319. elf_assert(dst.d_type == ELF_T_SHDR);
  320. scn->s_elf = elf;
  321. scn->s_index = i;
  322. scn->s_data_1 = sd;
  323. scn->s_data_n = sd;
  324. sd->sd_scn = scn;
  325. if (elf->e_class == ELFCLASS32) {
  326. Elf32_Shdr *shdr = &scn->s_shdr32;
  327. scn->s_type = shdr->sh_type;
  328. scn->s_size = shdr->sh_size;
  329. scn->s_offset = shdr->sh_offset;
  330. sd->sd_data.d_align = shdr->sh_addralign;
  331. sd->sd_data.d_type = _elf_scn_type(scn->s_type);
  332. }
  333. #if __LIBELF64
  334. else if (elf->e_class == ELFCLASS64) {
  335. Elf64_Shdr *shdr = &scn->s_shdr64;
  336. scn->s_type = shdr->sh_type;
  337. scn->s_size = shdr->sh_size;
  338. scn->s_offset = shdr->sh_offset;
  339. sd->sd_data.d_align = shdr->sh_addralign;
  340. /*
  341. * Check for overflow on 32-bit systems
  342. */
  343. if (overflow(scn->s_size, shdr->sh_size, Elf64_Xword)
  344. || overflow(scn->s_offset, shdr->sh_offset, Elf64_Off)
  345. || overflow(sd->sd_data.d_align, shdr->sh_addralign, Elf64_Xword)) {
  346. seterr(ERROR_OUTSIDE);
  347. return 0;
  348. }
  349. sd->sd_data.d_type = _elf_scn_type(scn->s_type);
  350. /*
  351. * QUIRKS MODE:
  352. *
  353. * Some 64-bit architectures use 64-bit entries in the
  354. * .hash section. This violates the ELF standard, and
  355. * should be fixed. It's mostly harmless as long as the
  356. * binary and the machine running your program have the
  357. * same byte order, but you're in trouble if they don't,
  358. * and if the entry size is wrong.
  359. *
  360. * As a workaround, I let libelf guess the right size
  361. * for the binary. This relies pretty much on the fact
  362. * that the binary provides correct data in the section
  363. * headers. If it doesn't, it's probably broken anyway.
  364. * Therefore, libelf uses a standard conforming value
  365. * when it's not absolutely sure.
  366. */
  367. if (scn->s_type == SHT_HASH) {
  368. int override = 0;
  369. /*
  370. * sh_entsize must reflect the entry size
  371. */
  372. if (shdr->sh_entsize == ELF64_FSZ_ADDR) {
  373. override++;
  374. }
  375. /*
  376. * sh_size must be a multiple of sh_entsize
  377. */
  378. if (shdr->sh_size % ELF64_FSZ_ADDR == 0) {
  379. override++;
  380. }
  381. /*
  382. * There must be room for at least 2 entries
  383. */
  384. if (shdr->sh_size >= 2 * ELF64_FSZ_ADDR) {
  385. override++;
  386. }
  387. /*
  388. * sh_addralign must be correctly set
  389. */
  390. if (shdr->sh_addralign == ELF64_FSZ_ADDR) {
  391. override++;
  392. }
  393. /*
  394. * The section must be properly aligned
  395. */
  396. if (shdr->sh_offset % ELF64_FSZ_ADDR == 0) {
  397. override++;
  398. }
  399. /* XXX: also look at the data? */
  400. /*
  401. * Make a conservative decision...
  402. */
  403. if (override >= 5) {
  404. sd->sd_data.d_type = ELF_T_ADDR;
  405. }
  406. }
  407. /*
  408. * END QUIRKS MODE.
  409. */
  410. }
  411. #endif /* __LIBELF64 */
  412. /* we already had this
  413. else {
  414. seterr(ERROR_UNIMPLEMENTED);
  415. return 0;
  416. }
  417. */
  418. sd->sd_data.d_size = scn->s_size;
  419. sd->sd_data.d_version = _elf_version;
  420. }
  421. elf_assert(scn == &head[0].scn);
  422. elf->e_scn_1 = &head[0].scn;
  423. head[0].scn.s_freeme = 1;
  424. }
  425. return 1;
  426. }
  427. static int
  428. _elf_cook_file(Elf *elf) {
  429. elf->e_ehdr = _elf_item(NULL, elf, ELF_T_EHDR, 0);
  430. if (!elf->e_ehdr) {
  431. return 0;
  432. }
  433. /*
  434. * Note: _elf_cook_phdr may require the first section header!
  435. */
  436. if (!_elf_cook_shdr(elf)) {
  437. return 0;
  438. }
  439. if (!_elf_cook_phdr(elf)) {
  440. return 0;
  441. }
  442. return 1;
  443. }
  444. int
  445. _elf_cook(Elf *elf) {
  446. elf_assert(_elf_scn_init.s_magic == SCN_MAGIC);
  447. elf_assert(_elf_data_init.sd_magic == DATA_MAGIC);
  448. elf_assert(elf);
  449. elf_assert(elf->e_magic == ELF_MAGIC);
  450. elf_assert(elf->e_kind == ELF_K_ELF);
  451. elf_assert(!elf->e_ehdr);
  452. if (!valid_version(elf->e_version)) {
  453. seterr(ERROR_UNKNOWN_VERSION);
  454. }
  455. else if (!valid_encoding(elf->e_encoding)) {
  456. seterr(ERROR_UNKNOWN_ENCODING);
  457. }
  458. else if (valid_class(elf->e_class)) {
  459. return _elf_cook_file(elf);
  460. }
  461. else {
  462. seterr(ERROR_UNKNOWN_CLASS);
  463. }
  464. return 0;
  465. }