32.xlatetof.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * 32.xlatetof.c - implementation of the elf32_xlateto[fm](3) functions.
  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. #include <ext_types.h>
  21. #include <byteswap.h>
  22. #ifndef lint
  23. static const char rcsid[] = "@(#) $Id: 32.xlatetof.c,v 1.27 2008/05/23 08:15:34 michael Exp $";
  24. #endif /* lint */
  25. /*
  26. * Ugly, ugly
  27. */
  28. #ifdef _WIN32
  29. # define Cat2(a,b)a##b
  30. # define Cat3(a,b,c)a##b##c
  31. # define Ex1(m1,m2,a,b)m1##m2(a##b)
  32. # define Ex2(m1,m2,a,b,c)m1##m2(a,b##c)
  33. #else /* _WIN32 */
  34. # define x
  35. # if defined/**/x
  36. # define Cat2(a,b)a##b
  37. # define Cat3(a,b,c)a##b##c
  38. # define Ex1(m1,m2,a,b)m1##m2(a##b)
  39. # define Ex2(m1,m2,a,b,c)m1##m2(a,b##c)
  40. # else
  41. # define Cat2(a,b)a/**/b
  42. # define Cat3(a,b,c)a/**/b/**/c
  43. # define Ex1(m1,m2,a,b)m1/**/m2(a/**/b)
  44. # define Ex2(m1,m2,a,b,c)m1/**/m2(a,b/**/c)
  45. # endif
  46. # undef x
  47. #endif /* _WIN32 */
  48. /*
  49. * auxiliary macros for execution order reversal
  50. */
  51. #define seq_forw(a,b) a b
  52. #define seq_back(a,b) b a
  53. /*
  54. * function instantiator
  55. */
  56. #define copy_type_e_io(name,e,io,tfrom,tto,copy) \
  57. static size_t \
  58. Cat3(name,_,io)(unsigned char *dst, const unsigned char *src, size_t n) { \
  59. n /= sizeof(tfrom); \
  60. if (n && dst) { \
  61. const tfrom *from = (const tfrom*)src; \
  62. tto *to = (tto*)dst; \
  63. size_t i; \
  64. \
  65. if (sizeof(tfrom) < sizeof(tto)) { \
  66. from += n; \
  67. to += n; \
  68. for (i = 0; i < n; i++) { \
  69. --from; \
  70. --to; \
  71. copy(e,io,seq_back) \
  72. } \
  73. } \
  74. else { \
  75. for (i = 0; i < n; i++) { \
  76. copy(e,io,seq_forw) \
  77. from++; \
  78. to++; \
  79. } \
  80. } \
  81. } \
  82. return n * sizeof(tto); \
  83. }
  84. #define copy_type_e(name,e,type,copy) \
  85. copy_type_e_io(name,e,tom,Cat2(__ext_,type),type,copy) \
  86. copy_type_e_io(name,e,tof,type,Cat2(__ext_,type),copy)
  87. /*
  88. * master function instantiator
  89. */
  90. #define copy_type(name,version,type,copy) \
  91. copy_type_e(Cat3(name,L,version),L,type,copy) \
  92. copy_type_e(Cat3(name,M,version),M,type,copy)
  93. /*
  94. * scalar copying
  95. */
  96. #define copy_scalar_tom(type) *to = Cat2(__load_,type)(*from);
  97. #define copy_scalar_tof(type) Cat2(__store_,type)(*to, *from);
  98. /*
  99. * structure member copying
  100. */
  101. #define copy_tom(mb,type) to->mb = Cat2(__load_,type)(from->mb);
  102. #define copy_tof(mb,type) Cat2(__store_,type)(to->mb, from->mb);
  103. /*
  104. * structure member copying (direction independent)
  105. */
  106. #define copy_byte(e,io,mb) to->mb = from->mb;
  107. #define copy_addr(e,io,mb) Ex2(copy_,io,mb,u32,e)
  108. #define copy_half(e,io,mb) Ex2(copy_,io,mb,u16,e)
  109. #define copy_off(e,io,mb) Ex2(copy_,io,mb,u32,e)
  110. #define copy_sword(e,io,mb) Ex2(copy_,io,mb,i32,e)
  111. #define copy_word(e,io,mb) Ex2(copy_,io,mb,u32,e)
  112. #define copy_arr(e,io,mb) \
  113. array_copy(to->mb, sizeof(to->mb), from->mb, sizeof(from->mb));
  114. /*
  115. * scalar copying (direction independent)
  116. * these macros are used as `copy' arguments to copy_type()
  117. */
  118. #define copy_addr_11(e,io,seq) Ex1(copy_scalar_,io,u32,e)
  119. #define copy_half_11(e,io,seq) Ex1(copy_scalar_,io,u16,e)
  120. #define copy_off_11(e,io,seq) Ex1(copy_scalar_,io,u32,e)
  121. #define copy_sword_11(e,io,seq) Ex1(copy_scalar_,io,i32,e)
  122. #define copy_word_11(e,io,seq) Ex1(copy_scalar_,io,u32,e)
  123. /*
  124. * structure copying (direction independent)
  125. * these macros are used as `copy' arguments to copy_type()
  126. */
  127. #define copy_dyn_11(e,io,seq) \
  128. seq(copy_sword(e,io,d_tag), \
  129. seq(copy_addr(e,io,d_un.d_ptr), \
  130. nullcopy))
  131. #define copy_ehdr_11(e,io,seq) \
  132. seq(copy_arr(e,io,e_ident), \
  133. seq(copy_half(e,io,e_type), \
  134. seq(copy_half(e,io,e_machine), \
  135. seq(copy_word(e,io,e_version), \
  136. seq(copy_addr(e,io,e_entry), \
  137. seq(copy_off(e,io,e_phoff), \
  138. seq(copy_off(e,io,e_shoff), \
  139. seq(copy_word(e,io,e_flags), \
  140. seq(copy_half(e,io,e_ehsize), \
  141. seq(copy_half(e,io,e_phentsize), \
  142. seq(copy_half(e,io,e_phnum), \
  143. seq(copy_half(e,io,e_shentsize), \
  144. seq(copy_half(e,io,e_shnum), \
  145. seq(copy_half(e,io,e_shstrndx), \
  146. nullcopy))))))))))))))
  147. #define copy_phdr_11(e,io,seq) \
  148. seq(copy_word(e,io,p_type), \
  149. seq(copy_off(e,io,p_offset), \
  150. seq(copy_addr(e,io,p_vaddr), \
  151. seq(copy_addr(e,io,p_paddr), \
  152. seq(copy_word(e,io,p_filesz), \
  153. seq(copy_word(e,io,p_memsz), \
  154. seq(copy_word(e,io,p_flags), \
  155. seq(copy_word(e,io,p_align), \
  156. nullcopy))))))))
  157. #define copy_rela_11(e,io,seq) \
  158. seq(copy_addr(e,io,r_offset), \
  159. seq(copy_word(e,io,r_info), \
  160. seq(copy_sword(e,io,r_addend), \
  161. nullcopy)))
  162. #define copy_rel_11(e,io,seq) \
  163. seq(copy_addr(e,io,r_offset), \
  164. seq(copy_word(e,io,r_info), \
  165. nullcopy))
  166. #define copy_shdr_11(e,io,seq) \
  167. seq(copy_word(e,io,sh_name), \
  168. seq(copy_word(e,io,sh_type), \
  169. seq(copy_word(e,io,sh_flags), \
  170. seq(copy_addr(e,io,sh_addr), \
  171. seq(copy_off(e,io,sh_offset), \
  172. seq(copy_word(e,io,sh_size), \
  173. seq(copy_word(e,io,sh_link), \
  174. seq(copy_word(e,io,sh_info), \
  175. seq(copy_word(e,io,sh_addralign), \
  176. seq(copy_word(e,io,sh_entsize), \
  177. nullcopy))))))))))
  178. #define copy_sym_11(e,io,seq) \
  179. seq(copy_word(e,io,st_name), \
  180. seq(copy_addr(e,io,st_value), \
  181. seq(copy_word(e,io,st_size), \
  182. seq(copy_byte(e,io,st_info), \
  183. seq(copy_byte(e,io,st_other), \
  184. seq(copy_half(e,io,st_shndx), \
  185. nullcopy))))))
  186. #define nullcopy /**/
  187. static size_t
  188. byte_copy(unsigned char *dst, const unsigned char *src, size_t n) {
  189. if (n && dst && dst != src) {
  190. #if HAVE_BROKEN_MEMMOVE
  191. size_t i;
  192. if (dst >= src + n || dst + n <= src) {
  193. memcpy(dst, src, n);
  194. }
  195. else if (dst < src) {
  196. for (i = 0; i < n; i++) {
  197. dst[i] = src[i];
  198. }
  199. }
  200. else {
  201. for (i = n; --i; ) {
  202. dst[i] = src[i];
  203. }
  204. }
  205. #else /* HAVE_BROKEN_MEMMOVE */
  206. memmove(dst, src, n);
  207. #endif /* HAVE_BROKEN_MEMMOVE */
  208. }
  209. return n;
  210. }
  211. static void
  212. array_copy(unsigned char *dst, size_t dlen, const unsigned char *src, size_t slen) {
  213. byte_copy(dst, src, dlen < slen ? dlen : slen);
  214. if (dlen > slen) {
  215. memset(dst + slen, 0, dlen - slen);
  216. }
  217. }
  218. /*
  219. * instantiate copy functions
  220. */
  221. copy_type(addr_32,_,Elf32_Addr,copy_addr_11)
  222. copy_type(half_32,_,Elf32_Half,copy_half_11)
  223. copy_type(off_32,_,Elf32_Off,copy_off_11)
  224. copy_type(sword_32,_,Elf32_Sword,copy_sword_11)
  225. copy_type(word_32,_,Elf32_Word,copy_word_11)
  226. copy_type(dyn_32,11,Elf32_Dyn,copy_dyn_11)
  227. copy_type(ehdr_32,11,Elf32_Ehdr,copy_ehdr_11)
  228. copy_type(phdr_32,11,Elf32_Phdr,copy_phdr_11)
  229. copy_type(rela_32,11,Elf32_Rela,copy_rela_11)
  230. copy_type(rel_32,11,Elf32_Rel,copy_rel_11)
  231. copy_type(shdr_32,11,Elf32_Shdr,copy_shdr_11)
  232. copy_type(sym_32,11,Elf32_Sym,copy_sym_11)
  233. typedef size_t (*xlator)(unsigned char*, const unsigned char*, size_t);
  234. typedef xlator xltab[ELF_T_NUM][2];
  235. /*
  236. * translation table (32-bit, version 1 -> version 1)
  237. */
  238. #if PIC
  239. static xltab
  240. #else /* PIC */
  241. static const xltab
  242. #endif /* PIC */
  243. xlate32_11[/*encoding*/] = {
  244. {
  245. { byte_copy, byte_copy },
  246. { addr_32L__tom, addr_32L__tof },
  247. { dyn_32L11_tom, dyn_32L11_tof },
  248. { ehdr_32L11_tom, ehdr_32L11_tof },
  249. { half_32L__tom, half_32L__tof },
  250. { off_32L__tom, off_32L__tof },
  251. { phdr_32L11_tom, phdr_32L11_tof },
  252. { rela_32L11_tom, rela_32L11_tof },
  253. { rel_32L11_tom, rel_32L11_tof },
  254. { shdr_32L11_tom, shdr_32L11_tof },
  255. { sword_32L__tom, sword_32L__tof },
  256. { sym_32L11_tom, sym_32L11_tof },
  257. { word_32L__tom, word_32L__tof },
  258. { 0, 0 }, /* there is no Sxword */
  259. { 0, 0 }, /* there is no Xword */
  260. #if __LIBELF_SYMBOL_VERSIONS
  261. { _elf_verdef_32L11_tom, _elf_verdef_32L11_tof },
  262. { _elf_verneed_32L11_tom, _elf_verneed_32L11_tof },
  263. #else /* __LIBELF_SYMBOL_VERSIONS */
  264. { 0, 0 },
  265. { 0, 0 },
  266. #endif /* __LIBELF_SYMBOL_VERSIONS */
  267. },
  268. {
  269. { byte_copy, byte_copy },
  270. { addr_32M__tom, addr_32M__tof },
  271. { dyn_32M11_tom, dyn_32M11_tof },
  272. { ehdr_32M11_tom, ehdr_32M11_tof },
  273. { half_32M__tom, half_32M__tof },
  274. { off_32M__tom, off_32M__tof },
  275. { phdr_32M11_tom, phdr_32M11_tof },
  276. { rela_32M11_tom, rela_32M11_tof },
  277. { rel_32M11_tom, rel_32M11_tof },
  278. { shdr_32M11_tom, shdr_32M11_tof },
  279. { sword_32M__tom, sword_32M__tof },
  280. { sym_32M11_tom, sym_32M11_tof },
  281. { word_32M__tom, word_32M__tof },
  282. { 0, 0 }, /* there is no Sxword */
  283. { 0, 0 }, /* there is no Xword */
  284. #if __LIBELF_SYMBOL_VERSIONS
  285. { _elf_verdef_32M11_tom, _elf_verdef_32M11_tof },
  286. { _elf_verneed_32M11_tom, _elf_verneed_32M11_tof },
  287. #else /* __LIBELF_SYMBOL_VERSIONS */
  288. { 0, 0 },
  289. { 0, 0 },
  290. #endif /* __LIBELF_SYMBOL_VERSIONS */
  291. },
  292. };
  293. /*
  294. * main translation table (32-bit)
  295. */
  296. #if PIC
  297. static xltab*
  298. #else /* PIC */
  299. static const xltab *const
  300. #endif /* PIC */
  301. xlate32[EV_CURRENT - EV_NONE][EV_CURRENT - EV_NONE] = {
  302. { xlate32_11, },
  303. };
  304. #define translator(sv,dv,enc,type,d) \
  305. (xlate32[(sv) - EV_NONE - 1] \
  306. [(dv) - EV_NONE - 1] \
  307. [(enc) - ELFDATA2LSB] \
  308. [(type) - ELF_T_BYTE] \
  309. [d])
  310. /*
  311. * destination buffer size
  312. */
  313. size_t
  314. _elf32_xltsize(const Elf_Data *src, unsigned dv, unsigned encode, int tof) {
  315. Elf_Type type = src->d_type;
  316. unsigned sv = src->d_version;
  317. xlator op;
  318. if (!valid_version(sv) || !valid_version(dv)) {
  319. seterr(ERROR_UNKNOWN_VERSION);
  320. return (size_t)-1;
  321. }
  322. if (tof) {
  323. /*
  324. * Encoding doesn't really matter (the translator only looks at
  325. * the source, which resides in memory), but we need a proper
  326. * encoding to select a translator...
  327. */
  328. encode = ELFDATA2LSB;
  329. }
  330. else if (!valid_encoding(encode)) {
  331. seterr(ERROR_UNKNOWN_ENCODING);
  332. return (size_t)-1;
  333. }
  334. if (!valid_type(type)) {
  335. seterr(ERROR_UNKNOWN_TYPE);
  336. return (size_t)-1;
  337. }
  338. if (!(op = translator(sv, dv, encode, type, tof))) {
  339. seterr(ERROR_UNKNOWN_TYPE);
  340. return (size_t)-1;
  341. }
  342. return (*op)(NULL, src->d_buf, src->d_size);
  343. }
  344. /*
  345. * direction-independent translation
  346. */
  347. static Elf_Data*
  348. elf32_xlate(Elf_Data *dst, const Elf_Data *src, unsigned encode, int tof) {
  349. Elf_Type type;
  350. int dv;
  351. int sv;
  352. size_t dsize;
  353. size_t tmp;
  354. xlator op;
  355. if (!src || !dst) {
  356. return NULL;
  357. }
  358. if (!src->d_buf || !dst->d_buf) {
  359. seterr(ERROR_NULLBUF);
  360. return NULL;
  361. }
  362. if (!valid_encoding(encode)) {
  363. seterr(ERROR_UNKNOWN_ENCODING);
  364. return NULL;
  365. }
  366. sv = src->d_version;
  367. dv = dst->d_version;
  368. if (!valid_version(sv) || !valid_version(dv)) {
  369. seterr(ERROR_UNKNOWN_VERSION);
  370. return NULL;
  371. }
  372. type = src->d_type;
  373. if (!valid_type(type)) {
  374. seterr(ERROR_UNKNOWN_TYPE);
  375. return NULL;
  376. }
  377. op = translator(sv, dv, encode, type, tof);
  378. if (!op) {
  379. seterr(ERROR_UNKNOWN_TYPE);
  380. return NULL;
  381. }
  382. dsize = (*op)(NULL, src->d_buf, src->d_size);
  383. if (dsize == (size_t)-1) {
  384. return NULL;
  385. }
  386. if (dst->d_size < dsize) {
  387. seterr(ERROR_DST2SMALL);
  388. return NULL;
  389. }
  390. if (dsize) {
  391. tmp = (*op)(dst->d_buf, src->d_buf, src->d_size);
  392. if (tmp == (size_t)-1) {
  393. return NULL;
  394. }
  395. elf_assert(tmp == dsize);
  396. }
  397. dst->d_size = dsize;
  398. dst->d_type = type;
  399. return dst;
  400. }
  401. /*
  402. * finally, the "official" translation functions
  403. */
  404. Elf_Data*
  405. elf32_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned encode) {
  406. return elf32_xlate(dst, src, encode, 0);
  407. }
  408. Elf_Data*
  409. elf32_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned encode) {
  410. return elf32_xlate(dst, src, encode, 1);
  411. }