xref: /aosp_15_r20/external/elfutils/libelf/gnuhash_xlate.h (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1*7304104dSAndroid Build Coastguard Worker /* Conversion functions for versioning information.
2*7304104dSAndroid Build Coastguard Worker    Copyright (C) 2006, 2007 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker    Copyright (C) 2023, Mark J. Wielaard <[email protected]>
4*7304104dSAndroid Build Coastguard Worker    This file is part of elfutils.
5*7304104dSAndroid Build Coastguard Worker    Written by Ulrich Drepper <[email protected]>, 2006.
6*7304104dSAndroid Build Coastguard Worker 
7*7304104dSAndroid Build Coastguard Worker    This file is free software; you can redistribute it and/or modify
8*7304104dSAndroid Build Coastguard Worker    it under the terms of either
9*7304104dSAndroid Build Coastguard Worker 
10*7304104dSAndroid Build Coastguard Worker      * the GNU Lesser General Public License as published by the Free
11*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 3 of the License, or (at
12*7304104dSAndroid Build Coastguard Worker        your option) any later version
13*7304104dSAndroid Build Coastguard Worker 
14*7304104dSAndroid Build Coastguard Worker    or
15*7304104dSAndroid Build Coastguard Worker 
16*7304104dSAndroid Build Coastguard Worker      * the GNU General Public License as published by the Free
17*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 2 of the License, or (at
18*7304104dSAndroid Build Coastguard Worker        your option) any later version
19*7304104dSAndroid Build Coastguard Worker 
20*7304104dSAndroid Build Coastguard Worker    or both in parallel, as here.
21*7304104dSAndroid Build Coastguard Worker 
22*7304104dSAndroid Build Coastguard Worker    elfutils is distributed in the hope that it will be useful, but
23*7304104dSAndroid Build Coastguard Worker    WITHOUT ANY WARRANTY; without even the implied warranty of
24*7304104dSAndroid Build Coastguard Worker    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25*7304104dSAndroid Build Coastguard Worker    General Public License for more details.
26*7304104dSAndroid Build Coastguard Worker 
27*7304104dSAndroid Build Coastguard Worker    You should have received copies of the GNU General Public License and
28*7304104dSAndroid Build Coastguard Worker    the GNU Lesser General Public License along with this program.  If
29*7304104dSAndroid Build Coastguard Worker    not, see <http://www.gnu.org/licenses/>.  */
30*7304104dSAndroid Build Coastguard Worker 
31*7304104dSAndroid Build Coastguard Worker #include <assert.h>
32*7304104dSAndroid Build Coastguard Worker #include <gelf.h>
33*7304104dSAndroid Build Coastguard Worker 
34*7304104dSAndroid Build Coastguard Worker #include "libelfP.h"
35*7304104dSAndroid Build Coastguard Worker 
36*7304104dSAndroid Build Coastguard Worker 
37*7304104dSAndroid Build Coastguard Worker static void
elf_cvt_gnuhash(void * dest,const void * src,size_t len,int encode)38*7304104dSAndroid Build Coastguard Worker elf_cvt_gnuhash (void *dest, const void *src, size_t len, int encode)
39*7304104dSAndroid Build Coastguard Worker {
40*7304104dSAndroid Build Coastguard Worker   size_t size = len;
41*7304104dSAndroid Build Coastguard Worker   /* The GNU hash table format on 64 bit machines mixes 32 bit and 64 bit
42*7304104dSAndroid Build Coastguard Worker      words.  We must detangle them here.   */
43*7304104dSAndroid Build Coastguard Worker   Elf32_Word *dest32 = dest;
44*7304104dSAndroid Build Coastguard Worker   const Elf32_Word *src32 = src;
45*7304104dSAndroid Build Coastguard Worker 
46*7304104dSAndroid Build Coastguard Worker   /* First four control words, 32 bits.  */
47*7304104dSAndroid Build Coastguard Worker   for (unsigned int cnt = 0; cnt < 4; ++cnt)
48*7304104dSAndroid Build Coastguard Worker     {
49*7304104dSAndroid Build Coastguard Worker       if (len < 4)
50*7304104dSAndroid Build Coastguard Worker 	goto done;
51*7304104dSAndroid Build Coastguard Worker       dest32[cnt] = bswap_32 (src32[cnt]);
52*7304104dSAndroid Build Coastguard Worker       len -= 4;
53*7304104dSAndroid Build Coastguard Worker     }
54*7304104dSAndroid Build Coastguard Worker 
55*7304104dSAndroid Build Coastguard Worker   Elf32_Word bitmask_words = encode ? src32[2] : dest32[2];
56*7304104dSAndroid Build Coastguard Worker 
57*7304104dSAndroid Build Coastguard Worker   /* Now the 64 bit words.  */
58*7304104dSAndroid Build Coastguard Worker   Elf64_Xword *dest64 = (Elf64_Xword *) &dest32[4];
59*7304104dSAndroid Build Coastguard Worker   const Elf64_Xword *src64 = (const Elf64_Xword *) &src32[4];
60*7304104dSAndroid Build Coastguard Worker   for (unsigned int cnt = 0; cnt < bitmask_words; ++cnt)
61*7304104dSAndroid Build Coastguard Worker     {
62*7304104dSAndroid Build Coastguard Worker       if (len < 8)
63*7304104dSAndroid Build Coastguard Worker 	goto done;
64*7304104dSAndroid Build Coastguard Worker       dest64[cnt] = bswap_64 (src64[cnt]);
65*7304104dSAndroid Build Coastguard Worker       len -= 8;
66*7304104dSAndroid Build Coastguard Worker     }
67*7304104dSAndroid Build Coastguard Worker 
68*7304104dSAndroid Build Coastguard Worker   /* The rest are 32 bit words again.  */
69*7304104dSAndroid Build Coastguard Worker   src32 = (const Elf32_Word *) &src64[bitmask_words];
70*7304104dSAndroid Build Coastguard Worker   dest32 = (Elf32_Word *) &dest64[bitmask_words];
71*7304104dSAndroid Build Coastguard Worker   while (len >= 4)
72*7304104dSAndroid Build Coastguard Worker     {
73*7304104dSAndroid Build Coastguard Worker       *dest32++ = bswap_32 (*src32++);
74*7304104dSAndroid Build Coastguard Worker       len -= 4;
75*7304104dSAndroid Build Coastguard Worker     }
76*7304104dSAndroid Build Coastguard Worker 
77*7304104dSAndroid Build Coastguard Worker  done:
78*7304104dSAndroid Build Coastguard Worker   /* If there are any bytes left, we weren't able to convert the
79*7304104dSAndroid Build Coastguard Worker      partial structures, just copy them over. */
80*7304104dSAndroid Build Coastguard Worker   if (len > 0)
81*7304104dSAndroid Build Coastguard Worker     memmove (dest + size - len, src + size - len, len);
82*7304104dSAndroid Build Coastguard Worker }
83