Home | History | Annotate | Download | only in libelf
      1 /* Conversion functions for versioning information.
      2    Copyright (C) 1998, 1999, 2000, 2002, 2003 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4    Written by Ulrich Drepper <drepper (at) redhat.com>, 1998.
      5 
      6    Red Hat elfutils is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by the
      8    Free Software Foundation; version 2 of the License.
      9 
     10    Red Hat elfutils is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License along
     16    with Red Hat elfutils; if not, write to the Free Software Foundation,
     17    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     18 
     19    In addition, as a special exception, Red Hat, Inc. gives You the
     20    additional right to link the code of Red Hat elfutils with code licensed
     21    under any Open Source Initiative certified open source license
     22    (http://www.opensource.org/licenses/index.php) which requires the
     23    distribution of source code with any binary distribution and to
     24    distribute linked combinations of the two.  Non-GPL Code permitted under
     25    this exception must only link to the code of Red Hat elfutils through
     26    those well defined interfaces identified in the file named EXCEPTION
     27    found in the source code files (the "Approved Interfaces").  The files
     28    of Non-GPL Code may instantiate templates or use macros or inline
     29    functions from the Approved Interfaces without causing the resulting
     30    work to be covered by the GNU General Public License.  Only Red Hat,
     31    Inc. may make changes or additions to the list of Approved Interfaces.
     32    Red Hat's grant of this exception is conditioned upon your not adding
     33    any new exceptions.  If you wish to add a new Approved Interface or
     34    exception, please contact Red Hat.  You must obey the GNU General Public
     35    License in all respects for all of the Red Hat elfutils code and other
     36    code used in conjunction with Red Hat elfutils except the Non-GPL Code
     37    covered by this exception.  If you modify this file, you may extend this
     38    exception to your version of the file, but you are not obligated to do
     39    so.  If you do not wish to provide this exception without modification,
     40    you must delete this exception statement from your version and license
     41    this file solely under the GPL without exception.
     42 
     43    Red Hat elfutils is an included package of the Open Invention Network.
     44    An included package of the Open Invention Network is a package for which
     45    Open Invention Network licensees cross-license their patents.  No patent
     46    license is granted, either expressly or impliedly, by designation as an
     47    included package.  Should you wish to participate in the Open Invention
     48    Network licensing program, please visit www.openinventionnetwork.com
     49    <http://www.openinventionnetwork.com>.  */
     50 
     51 #include <assert.h>
     52 #include <gelf.h>
     53 
     54 #include "libelfP.h"
     55 
     56 
     57 static void
     58 elf_cvt_Verdef (void *dest, const void *src, size_t len, int encode)
     59 {
     60   /* We have two different record types: ElfXX_Verndef and ElfXX_Verdaux.
     61      To recognize them we have to walk the data structure and convert
     62      them one after the other.  The ENCODE parameter specifies whether
     63      we are encoding or decoding.  When we are encoding we can immediately
     64      use the data in the buffer; if not, we have to decode the data before
     65      using it.  */
     66   size_t def_offset = 0;
     67   GElf_Verdef *ddest;
     68   GElf_Verdef *dsrc;
     69 
     70   /* We rely on the types being all the same size.  */
     71   assert (sizeof (GElf_Verdef) == sizeof (Elf32_Verdef));
     72   assert (sizeof (GElf_Verdaux) == sizeof (Elf32_Verdaux));
     73   assert (sizeof (GElf_Verdef) == sizeof (Elf64_Verdef));
     74   assert (sizeof (GElf_Verdaux) == sizeof (Elf64_Verdaux));
     75 
     76   if (len == 0)
     77     return;
     78 
     79   do
     80     {
     81       size_t aux_offset;
     82       GElf_Verdaux *asrc;
     83 
     84       /* Test for correct offset.  */
     85       if (def_offset + sizeof (GElf_Verdef) > len)
     86 	return;
     87 
     88       /* Work the tree from the first record.  */
     89       ddest = (GElf_Verdef *) ((char *) dest + def_offset);
     90       dsrc = (GElf_Verdef *) ((char *) src + def_offset);
     91 
     92       /* Decode first if necessary.  */
     93       if (! encode)
     94 	{
     95 	  ddest->vd_version = bswap_16 (dsrc->vd_version);
     96 	  ddest->vd_flags = bswap_16 (dsrc->vd_flags);
     97 	  ddest->vd_ndx = bswap_16 (dsrc->vd_ndx);
     98 	  ddest->vd_cnt = bswap_16 (dsrc->vd_cnt);
     99 	  ddest->vd_hash = bswap_32 (dsrc->vd_hash);
    100 	  ddest->vd_aux = bswap_32 (dsrc->vd_aux);
    101 	  ddest->vd_next = bswap_32 (dsrc->vd_next);
    102 
    103 	  aux_offset = def_offset + ddest->vd_aux;
    104 	}
    105       else
    106 	aux_offset = def_offset + dsrc->vd_aux;
    107 
    108       /* Handle all the auxiliary records belonging to this definition.  */
    109       do
    110 	{
    111 	  GElf_Verdaux *adest;
    112 
    113 	  /* Test for correct offset.  */
    114 	  if (aux_offset + sizeof (GElf_Verdaux) > len)
    115 	    return;
    116 
    117 	  adest = (GElf_Verdaux *) ((char *) dest + aux_offset);
    118 	  asrc = (GElf_Verdaux *) ((char *) src + aux_offset);
    119 
    120 	  if (encode)
    121 	    aux_offset += asrc->vda_next;
    122 
    123 	  adest->vda_name = bswap_32 (asrc->vda_name);
    124 	  adest->vda_next = bswap_32 (asrc->vda_next);
    125 
    126 	  if (! encode)
    127 	    aux_offset += adest->vda_next;
    128 	}
    129       while (asrc->vda_next != 0);
    130 
    131       /* Encode now if necessary.  */
    132       if (encode)
    133 	{
    134 	  def_offset += dsrc->vd_next;
    135 
    136 	  ddest->vd_version = bswap_16 (dsrc->vd_version);
    137 	  ddest->vd_flags = bswap_16 (dsrc->vd_flags);
    138 	  ddest->vd_ndx = bswap_16 (dsrc->vd_ndx);
    139 	  ddest->vd_cnt = bswap_16 (dsrc->vd_cnt);
    140 	  ddest->vd_hash = bswap_32 (dsrc->vd_hash);
    141 	  ddest->vd_aux = bswap_32 (dsrc->vd_aux);
    142 	  ddest->vd_next = bswap_32 (dsrc->vd_next);
    143 	}
    144       else
    145 	def_offset += ddest->vd_next;
    146     }
    147   while (dsrc->vd_next != 0);
    148 }
    149 
    150 
    151 static void
    152 elf_cvt_Verneed (void *dest, const void *src, size_t len, int encode)
    153 {
    154   /* We have two different record types: ElfXX_Verndef and ElfXX_Verdaux.
    155      To recognize them we have to walk the data structure and convert
    156      them one after the other.  The ENCODE parameter specifies whether
    157      we are encoding or decoding.  When we are encoding we can immediately
    158      use the data in the buffer; if not, we have to decode the data before
    159      using it.  */
    160   size_t need_offset = 0;
    161   GElf_Verneed *ndest;
    162   GElf_Verneed *nsrc;
    163 
    164   /* We rely on the types being all the same size.  */
    165   assert (sizeof (GElf_Verneed) == sizeof (Elf32_Verneed));
    166   assert (sizeof (GElf_Vernaux) == sizeof (Elf32_Vernaux));
    167   assert (sizeof (GElf_Verneed) == sizeof (Elf64_Verneed));
    168   assert (sizeof (GElf_Vernaux) == sizeof (Elf64_Vernaux));
    169 
    170   if (len == 0)
    171     return;
    172 
    173   do
    174     {
    175       size_t aux_offset;
    176       GElf_Vernaux *asrc;
    177 
    178       /* Test for correct offset.  */
    179       if (need_offset + sizeof (GElf_Verneed) > len)
    180 	return;
    181 
    182       /* Work the tree from the first record.  */
    183       ndest = (GElf_Verneed *) ((char *) dest + need_offset);
    184       nsrc = (GElf_Verneed *) ((char *) src + need_offset);
    185 
    186       /* Decode first if necessary.  */
    187       if (! encode)
    188 	{
    189 	  ndest->vn_version = bswap_16 (nsrc->vn_version);
    190 	  ndest->vn_cnt = bswap_16 (nsrc->vn_cnt);
    191 	  ndest->vn_file = bswap_32 (nsrc->vn_file);
    192 	  ndest->vn_aux = bswap_32 (nsrc->vn_aux);
    193 	  ndest->vn_next = bswap_32 (nsrc->vn_next);
    194 
    195 	  aux_offset = need_offset + ndest->vn_aux;
    196 	}
    197       else
    198 	aux_offset = need_offset + nsrc->vn_aux;
    199 
    200       /* Handle all the auxiliary records belonging to this requirement.  */
    201       do
    202 	{
    203 	  GElf_Vernaux *adest;
    204 
    205 	  /* Test for correct offset.  */
    206 	  if (aux_offset + sizeof (GElf_Vernaux) > len)
    207 	    return;
    208 
    209 	  adest = (GElf_Vernaux *) ((char *) dest + aux_offset);
    210 	  asrc = (GElf_Vernaux *) ((char *) src + aux_offset);
    211 
    212 	  if (encode)
    213 	    aux_offset += asrc->vna_next;
    214 
    215 	  adest->vna_hash = bswap_32 (asrc->vna_hash);
    216 	  adest->vna_flags = bswap_16 (asrc->vna_flags);
    217 	  adest->vna_other = bswap_16 (asrc->vna_other);
    218 	  adest->vna_name = bswap_32 (asrc->vna_name);
    219 	  adest->vna_next = bswap_32 (asrc->vna_next);
    220 
    221 	  if (! encode)
    222 	    aux_offset += adest->vna_next;
    223 	}
    224       while (asrc->vna_next != 0);
    225 
    226       /* Encode now if necessary.  */
    227       if (encode)
    228 	{
    229 	  need_offset += nsrc->vn_next;
    230 
    231 	  ndest->vn_version = bswap_16 (nsrc->vn_version);
    232 	  ndest->vn_cnt = bswap_16 (nsrc->vn_cnt);
    233 	  ndest->vn_file = bswap_32 (nsrc->vn_file);
    234 	  ndest->vn_aux = bswap_32 (nsrc->vn_aux);
    235 	  ndest->vn_next = bswap_32 (nsrc->vn_next);
    236 	}
    237       else
    238 	need_offset += ndest->vn_next;
    239     }
    240   while (nsrc->vn_next != 0);
    241 }
    242