verneed.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * verneed.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 verneed_h_rcsid[] = "@(#) $Id: verneed.h,v 1.13 2008/05/23 08:15:35 michael Exp $";
  21. #endif /* lint */
  22. #if VER_NEED_CURRENT != 1
  23. #error libelf currently does not support VER_NEED_CURRENT != 1
  24. #endif /* VER_NEED_CURRENT != 1 */
  25. #if TOFILE
  26. static void
  27. __store_vernaux(vernaux_ftype *dst, const vernaux_mtype *src, unsigned enc) {
  28. if (enc == ELFDATA2LSB) {
  29. __store_u32L(dst->vna_hash, src->vna_hash);
  30. __store_u16L(dst->vna_flags, src->vna_flags);
  31. __store_u16L(dst->vna_other, src->vna_other);
  32. __store_u32L(dst->vna_name, src->vna_name);
  33. __store_u32L(dst->vna_next, src->vna_next);
  34. }
  35. else {
  36. __store_u32M(dst->vna_hash, src->vna_hash);
  37. __store_u16M(dst->vna_flags, src->vna_flags);
  38. __store_u16M(dst->vna_other, src->vna_other);
  39. __store_u32M(dst->vna_name, src->vna_name);
  40. __store_u32M(dst->vna_next, src->vna_next);
  41. }
  42. }
  43. static void
  44. __store_verneed(verneed_ftype *dst, const verneed_mtype *src, unsigned enc) {
  45. if (enc == ELFDATA2LSB) {
  46. __store_u16L(dst->vn_version, src->vn_version);
  47. __store_u16L(dst->vn_cnt, src->vn_cnt);
  48. __store_u32L(dst->vn_file, src->vn_file);
  49. __store_u32L(dst->vn_aux, src->vn_aux);
  50. __store_u32L(dst->vn_next, src->vn_next);
  51. }
  52. else {
  53. __store_u16M(dst->vn_version, src->vn_version);
  54. __store_u16M(dst->vn_cnt, src->vn_cnt);
  55. __store_u32M(dst->vn_file, src->vn_file);
  56. __store_u32M(dst->vn_aux, src->vn_aux);
  57. __store_u32M(dst->vn_next, src->vn_next);
  58. }
  59. }
  60. typedef vernaux_mtype vernaux_stype;
  61. typedef vernaux_ftype vernaux_dtype;
  62. typedef verneed_mtype verneed_stype;
  63. typedef verneed_ftype verneed_dtype;
  64. typedef align_mtype verneed_atype;
  65. #define copy_vernaux_srctotmp(d, s, e) (*(d) = *(s))
  66. #define copy_vernaux_tmptodst(d, s, e) __store_vernaux((d), (s), (e))
  67. #define copy_verneed_srctotmp(d, s, e) (*(d) = *(s))
  68. #define copy_verneed_tmptodst(d, s, e) __store_verneed((d), (s), (e))
  69. #define translator_suffix _tof
  70. #else /* TOFILE */
  71. static void
  72. __load_vernaux(vernaux_mtype *dst, const vernaux_ftype *src, unsigned enc) {
  73. if (enc == ELFDATA2LSB) {
  74. dst->vna_hash = __load_u32L(src->vna_hash);
  75. dst->vna_flags = __load_u16L(src->vna_flags);
  76. dst->vna_other = __load_u16L(src->vna_other);
  77. dst->vna_name = __load_u32L(src->vna_name);
  78. dst->vna_next = __load_u32L(src->vna_next);
  79. }
  80. else {
  81. dst->vna_hash = __load_u32M(src->vna_hash);
  82. dst->vna_flags = __load_u16M(src->vna_flags);
  83. dst->vna_other = __load_u16M(src->vna_other);
  84. dst->vna_name = __load_u32M(src->vna_name);
  85. dst->vna_next = __load_u32M(src->vna_next);
  86. }
  87. }
  88. static void
  89. __load_verneed(verneed_mtype *dst, const verneed_ftype *src, unsigned enc) {
  90. if (enc == ELFDATA2LSB) {
  91. dst->vn_version = __load_u16L(src->vn_version);
  92. dst->vn_cnt = __load_u16L(src->vn_cnt);
  93. dst->vn_file = __load_u32L(src->vn_file);
  94. dst->vn_aux = __load_u32L(src->vn_aux);
  95. dst->vn_next = __load_u32L(src->vn_next);
  96. }
  97. else {
  98. dst->vn_version = __load_u16M(src->vn_version);
  99. dst->vn_cnt = __load_u16M(src->vn_cnt);
  100. dst->vn_file = __load_u32M(src->vn_file);
  101. dst->vn_aux = __load_u32M(src->vn_aux);
  102. dst->vn_next = __load_u32M(src->vn_next);
  103. }
  104. }
  105. typedef vernaux_ftype vernaux_stype;
  106. typedef vernaux_mtype vernaux_dtype;
  107. typedef verneed_ftype verneed_stype;
  108. typedef verneed_mtype verneed_dtype;
  109. typedef align_ftype verneed_atype;
  110. #define copy_vernaux_srctotmp(d, s, e) __load_vernaux((d), (s), (e))
  111. #define copy_vernaux_tmptodst(d, s, e) (*(d) = *(s))
  112. #define copy_verneed_srctotmp(d, s, e) __load_verneed((d), (s), (e))
  113. #define copy_verneed_tmptodst(d, s, e) (*(d) = *(s))
  114. #define translator_suffix _tom
  115. #endif /* TOFILE */
  116. #define cat3(a,b,c) a##b##c
  117. #define xlt3(p,e,s) cat3(p,e,s)
  118. #define xltprefix(x) xlt3(x,_,class_suffix)
  119. #define translator(x,e) xlt3(xltprefix(_elf_##x),e,translator_suffix)
  120. static size_t
  121. xlt_verneed(unsigned char *dst, const unsigned char *src, size_t n, unsigned enc) {
  122. size_t off;
  123. if (sizeof(verneed_stype) != sizeof(verneed_dtype)
  124. || sizeof(vernaux_stype) != sizeof(vernaux_dtype)) {
  125. /* never happens for ELF v1 and Verneed v1 */
  126. seterr(ERROR_UNIMPLEMENTED);
  127. return (size_t)-1;
  128. }
  129. /* size translation shortcut */
  130. if (dst == NULL) {
  131. return n;
  132. }
  133. if (src == NULL) {
  134. seterr(ERROR_NULLBUF);
  135. return (size_t)-1;
  136. }
  137. off = 0;
  138. while (off + sizeof(verneed_stype) <= n) {
  139. const verneed_stype *svn;
  140. verneed_dtype *dvn;
  141. verneed_mtype vn;
  142. size_t acount;
  143. size_t aoff;
  144. /*
  145. * check for proper alignment
  146. */
  147. if (off % sizeof(verneed_atype)) {
  148. seterr(ERROR_VERNEED_FORMAT);
  149. return (size_t)-1;
  150. }
  151. /*
  152. * copy and check src
  153. */
  154. svn = (verneed_stype*)(src + off);
  155. dvn = (verneed_dtype*)(dst + off);
  156. copy_verneed_srctotmp(&vn, svn, enc);
  157. if (vn.vn_version < 1
  158. || vn.vn_version > VER_NEED_CURRENT) {
  159. seterr(ERROR_VERNEED_VERSION);
  160. return (size_t)-1;
  161. }
  162. if (vn.vn_cnt < 1
  163. || vn.vn_aux == 0) {
  164. seterr(ERROR_VERNEED_FORMAT);
  165. return (size_t)-1;
  166. }
  167. copy_verneed_tmptodst(dvn, &vn, enc);
  168. /*
  169. * copy aux array
  170. */
  171. aoff = off + vn.vn_aux;
  172. for (acount = 0; acount < vn.vn_cnt; acount++) {
  173. const vernaux_stype *svna;
  174. vernaux_dtype *dvna;
  175. vernaux_mtype vna;
  176. /*
  177. * are we still inside the buffer limits?
  178. */
  179. if (aoff + sizeof(vernaux_stype) > n) {
  180. break;
  181. }
  182. /*
  183. * check for proper alignment
  184. */
  185. if (aoff % sizeof(verneed_atype)) {
  186. seterr(ERROR_VERNEED_FORMAT);
  187. return (size_t)-1;
  188. }
  189. /*
  190. * copy and check src
  191. */
  192. svna = (vernaux_stype*)(src + aoff);
  193. dvna = (vernaux_dtype*)(dst + aoff);
  194. copy_vernaux_srctotmp(&vna, svna, enc);
  195. copy_vernaux_tmptodst(dvna, &vna, enc);
  196. /*
  197. * advance to next vernaux
  198. */
  199. if (vna.vna_next == 0) {
  200. /* end of list */
  201. break;
  202. }
  203. aoff += vna.vna_next;
  204. }
  205. /*
  206. * advance to next verneed
  207. */
  208. if (vn.vn_next == 0) {
  209. /* end of list */
  210. break;
  211. }
  212. off += vn.vn_next;
  213. }
  214. return n;
  215. }
  216. size_t
  217. translator(verneed,L11)(unsigned char *dst, const unsigned char *src, size_t n) {
  218. return xlt_verneed(dst, src, n, ELFDATA2LSB);
  219. }
  220. size_t
  221. translator(verneed,M11)(unsigned char *dst, const unsigned char *src, size_t n) {
  222. return xlt_verneed(dst, src, n, ELFDATA2MSB);
  223. }