verdef.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * verdef.h - copy versioning information.
  3. * Copyright (C) 2001 - 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. #ifndef lint
  20. static const char verdef_h_rcsid[] = "@(#) $Id: verdef.h,v 1.13 2008/05/23 08:15:35 michael Exp $";
  21. #endif /* lint */
  22. #if VER_DEF_CURRENT != 1
  23. #error libelf currently does not support VER_DEF_CURRENT != 1
  24. #endif /* VER_DEF_CURRENT != 1 */
  25. #if TOFILE
  26. static void
  27. __store_verdaux(verdaux_ftype *dst, const verdaux_mtype *src, unsigned enc) {
  28. if (enc == ELFDATA2LSB) {
  29. __store_u32L(dst->vda_name, src->vda_name);
  30. __store_u32L(dst->vda_next, src->vda_next);
  31. }
  32. else {
  33. __store_u32M(dst->vda_name, src->vda_name);
  34. __store_u32M(dst->vda_next, src->vda_next);
  35. }
  36. }
  37. static void
  38. __store_verdef(verdef_ftype *dst, const verdef_mtype *src, unsigned enc) {
  39. if (enc == ELFDATA2LSB) {
  40. __store_u16L(dst->vd_version, src->vd_version);
  41. __store_u16L(dst->vd_flags, src->vd_flags);
  42. __store_u16L(dst->vd_ndx, src->vd_ndx);
  43. __store_u16L(dst->vd_cnt, src->vd_cnt);
  44. __store_u32L(dst->vd_hash, src->vd_hash);
  45. __store_u32L(dst->vd_aux, src->vd_aux);
  46. __store_u32L(dst->vd_next, src->vd_next);
  47. }
  48. else {
  49. __store_u16M(dst->vd_version, src->vd_version);
  50. __store_u16M(dst->vd_flags, src->vd_flags);
  51. __store_u16M(dst->vd_ndx, src->vd_ndx);
  52. __store_u16M(dst->vd_cnt, src->vd_cnt);
  53. __store_u32M(dst->vd_hash, src->vd_hash);
  54. __store_u32M(dst->vd_aux, src->vd_aux);
  55. __store_u32M(dst->vd_next, src->vd_next);
  56. }
  57. }
  58. typedef verdaux_mtype verdaux_stype;
  59. typedef verdaux_ftype verdaux_dtype;
  60. typedef verdef_mtype verdef_stype;
  61. typedef verdef_ftype verdef_dtype;
  62. typedef align_mtype verdef_atype;
  63. #define copy_verdaux_srctotmp(d, s, e) (*(d) = *(s))
  64. #define copy_verdaux_tmptodst(d, s, e) __store_verdaux((d), (s), (e))
  65. #define copy_verdef_srctotmp(d, s, e) (*(d) = *(s))
  66. #define copy_verdef_tmptodst(d, s, e) __store_verdef((d), (s), (e))
  67. #define translator_suffix _tof
  68. #else /* TOFILE */
  69. static void
  70. __load_verdaux(verdaux_mtype *dst, const verdaux_ftype *src, unsigned enc) {
  71. if (enc == ELFDATA2LSB) {
  72. dst->vda_name = __load_u32L(src->vda_name);
  73. dst->vda_next = __load_u32L(src->vda_next);
  74. }
  75. else {
  76. dst->vda_name = __load_u32M(src->vda_name);
  77. dst->vda_next = __load_u32M(src->vda_next);
  78. }
  79. }
  80. static void
  81. __load_verdef(verdef_mtype *dst, const verdef_ftype *src, unsigned enc) {
  82. if (enc == ELFDATA2LSB) {
  83. dst->vd_version = __load_u16L(src->vd_version);
  84. dst->vd_flags = __load_u16L(src->vd_flags);
  85. dst->vd_ndx = __load_u16L(src->vd_ndx);
  86. dst->vd_cnt = __load_u16L(src->vd_cnt);
  87. dst->vd_hash = __load_u32L(src->vd_hash);
  88. dst->vd_aux = __load_u32L(src->vd_aux);
  89. dst->vd_next = __load_u32L(src->vd_next);
  90. }
  91. else {
  92. dst->vd_version = __load_u16M(src->vd_version);
  93. dst->vd_flags = __load_u16M(src->vd_flags);
  94. dst->vd_ndx = __load_u16M(src->vd_ndx);
  95. dst->vd_cnt = __load_u16M(src->vd_cnt);
  96. dst->vd_hash = __load_u32M(src->vd_hash);
  97. dst->vd_aux = __load_u32M(src->vd_aux);
  98. dst->vd_next = __load_u32M(src->vd_next);
  99. }
  100. }
  101. typedef verdaux_ftype verdaux_stype;
  102. typedef verdaux_mtype verdaux_dtype;
  103. typedef verdef_ftype verdef_stype;
  104. typedef verdef_mtype verdef_dtype;
  105. typedef align_ftype verdef_atype;
  106. #define copy_verdaux_srctotmp(d, s, e) __load_verdaux((d), (s), (e))
  107. #define copy_verdaux_tmptodst(d, s, e) (*(d) = *(s))
  108. #define copy_verdef_srctotmp(d, s, e) __load_verdef((d), (s), (e))
  109. #define copy_verdef_tmptodst(d, s, e) (*(d) = *(s))
  110. #define translator_suffix _tom
  111. #endif /* TOFILE */
  112. #define cat3(a,b,c) a##b##c
  113. #define xlt3(p,e,s) cat3(p,e,s)
  114. #define xltprefix(x) xlt3(x,_,class_suffix)
  115. #define translator(x,e) xlt3(xltprefix(_elf_##x),e,translator_suffix)
  116. static size_t
  117. xlt_verdef(unsigned char *dst, const unsigned char *src, size_t n, unsigned enc) {
  118. size_t off;
  119. if (sizeof(verdef_stype) != sizeof(verdef_dtype)
  120. || sizeof(verdaux_stype) != sizeof(verdaux_dtype)) {
  121. /* never happens for ELF v1 and Verneed v1 */
  122. seterr(ERROR_UNIMPLEMENTED);
  123. return (size_t)-1;
  124. }
  125. /* size translation shortcut */
  126. if (dst == NULL) {
  127. return n;
  128. }
  129. if (src == NULL) {
  130. seterr(ERROR_NULLBUF);
  131. return (size_t)-1;
  132. }
  133. off = 0;
  134. while (off + sizeof(verdef_stype) <= n) {
  135. const verdef_stype *svd;
  136. verdef_dtype *dvd;
  137. verdef_mtype vd;
  138. size_t acount;
  139. size_t aoff;
  140. /*
  141. * check for proper alignment
  142. */
  143. if (off % sizeof(verdef_atype)) {
  144. seterr(ERROR_VERDEF_FORMAT);
  145. return (size_t)-1;
  146. }
  147. /*
  148. * copy and check src
  149. */
  150. svd = (verdef_stype*)(src + off);
  151. dvd = (verdef_dtype*)(dst + off);
  152. copy_verdef_srctotmp(&vd, svd, enc);
  153. if (vd.vd_version < 1
  154. || vd.vd_version > VER_DEF_CURRENT) {
  155. seterr(ERROR_VERDEF_VERSION);
  156. return (size_t)-1;
  157. }
  158. if (vd.vd_cnt < 1
  159. || vd.vd_aux == 0) {
  160. seterr(ERROR_VERDEF_FORMAT);
  161. return (size_t)-1;
  162. }
  163. copy_verdef_tmptodst(dvd, &vd, enc);
  164. /*
  165. * copy aux array
  166. */
  167. aoff = off + vd.vd_aux;
  168. for (acount = 0; acount < vd.vd_cnt; acount++) {
  169. const verdaux_stype *svda;
  170. verdaux_dtype *dvda;
  171. verdaux_mtype vda;
  172. /*
  173. * are we still inside the buffer limits?
  174. */
  175. if (aoff + sizeof(verdaux_stype) > n) {
  176. break;
  177. }
  178. /*
  179. * check for proper alignment
  180. */
  181. if (aoff % sizeof(verdef_atype)) {
  182. seterr(ERROR_VERDEF_FORMAT);
  183. return (size_t)-1;
  184. }
  185. /*
  186. * copy and check src
  187. */
  188. svda = (verdaux_stype*)(src + aoff);
  189. dvda = (verdaux_dtype*)(dst + aoff);
  190. copy_verdaux_srctotmp(&vda, svda, enc);
  191. copy_verdaux_tmptodst(dvda, &vda, enc);
  192. /*
  193. * advance to next verdaux
  194. */
  195. if (vda.vda_next == 0) {
  196. /* end of list */
  197. break;
  198. }
  199. aoff += vda.vda_next;
  200. }
  201. /*
  202. * advance to next verdef
  203. */
  204. if (vd.vd_next == 0) {
  205. /* end of list */
  206. break;
  207. }
  208. off += vd.vd_next;
  209. }
  210. return n;
  211. }
  212. size_t
  213. translator(verdef,L11)(unsigned char *dst, const unsigned char *src, size_t n) {
  214. return xlt_verdef(dst, src, n, ELFDATA2LSB);
  215. }
  216. size_t
  217. translator(verdef,M11)(unsigned char *dst, const unsigned char *src, size_t n) {
  218. return xlt_verdef(dst, src, n, ELFDATA2MSB);
  219. }