Home | History | Annotate | Download | only in libelf
      1 /* Compute simple checksum from permanent parts of the ELF file.
      2    Copyright (C) 2002, 2003, 2004 Red Hat, Inc.
      3    Written by Ulrich Drepper <drepper (at) redhat.com>, 2002.
      4 
      5    This program is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation, version 2.
      8 
      9    This program is distributed in the hope that it will be useful,
     10    but WITHOUT ANY WARRANTY; without even the implied warranty of
     11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12    GNU General Public License for more details.
     13 
     14    You should have received a copy of the GNU General Public License
     15    along with this program; if not, write to the Free Software Foundation,
     16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
     17 
     18 #ifdef HAVE_CONFIG_H
     19 # include <config.h>
     20 #endif
     21 
     22 #include <assert.h>
     23 #include <stdbool.h>
     24 #include <stddef.h>
     25 #include <string.h>
     26 
     27 #include "gelf.h"
     28 #include "libelfP.h"
     29 #include "elf-knowledge.h"
     30 
     31 #ifndef LIBELFBITS
     32 # define LIBELFBITS 32
     33 #endif
     34 
     35 
     36 /* The SECTION_STRIP_P macro wants to call into libebl which we cannot
     37    do and do not have to do here.  Provide a dummy replacement.  */
     38 #define ebl_debugscn_p(ebl, name) true
     39 
     40 
     41 #define process_block(crc, data) \
     42   __libelf_crc32 (crc, data->d_buf, data->d_size)
     43 
     44 
     45 long int
     46 elfw2(LIBELFBITS,checksum) (elf)
     47      Elf *elf;
     48 {
     49   size_t shstrndx;
     50   Elf_Scn *scn;
     51   long int result = 0;
     52   unsigned char *ident;
     53   bool same_byte_order;
     54 
     55   if (elf == NULL)
     56     return -1l;
     57 
     58   /* Find the section header string table.  */
     59   if  (INTUSE(elf_getshstrndx) (elf, &shstrndx) < 0)
     60     {
     61       /* This can only happen if the ELF handle is not for real.  */
     62       __libelf_seterrno (ELF_E_INVALID_HANDLE);
     63       return -1l;
     64     }
     65 
     66   /* Determine whether the byte order of the file and that of the host
     67      is the same.  */
     68   ident = elf->state.ELFW(elf,LIBELFBITS).ehdr->e_ident;
     69   same_byte_order = ((ident[EI_DATA] == ELFDATA2LSB
     70 		      && __BYTE_ORDER == __LITTLE_ENDIAN)
     71 		     || (ident[EI_DATA] == ELFDATA2MSB
     72 			 && __BYTE_ORDER == __BIG_ENDIAN));
     73 
     74   /* Iterate over all sections to find those which are not strippable.  */
     75   scn = NULL;
     76   while ((scn = INTUSE(elf_nextscn) (elf, scn)) != NULL)
     77     {
     78       GElf_Shdr shdr_mem;
     79       GElf_Shdr *shdr;
     80       Elf_Data *data;
     81 
     82       /* Get the section header.  */
     83       shdr = INTUSE(gelf_getshdr) (scn, &shdr_mem);
     84       if (shdr == NULL)
     85 	{
     86 	  __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
     87 	  return -1l;
     88 	}
     89 
     90       if (SECTION_STRIP_P (NULL, NULL, NULL, shdr,
     91 			   elf_strptr (elf, shstrndx, shdr->sh_name),
     92 			   true, false))
     93 	/* The section can be stripped.  Don't use it.  */
     94 	continue;
     95 
     96       /* To compute the checksum we need to get to the data.  For
     97 	 repeatable results we must use the external format.  The data
     98 	 we get with 'elf'getdata' might be changed for endianess
     99 	 reasons.  Therefore we use 'elf_rawdata' if possible.  But
    100 	 this function can fail if the data was constructed by the
    101 	 program.  In this case we have to use 'elf_getdata' and
    102 	 eventually convert the data to the external format.  */
    103       data = INTUSE(elf_rawdata) (scn, NULL);
    104       if (data != NULL)
    105 	{
    106 	  /* The raw data is available.  */
    107 	  result = process_block (result, data);
    108 
    109 	  /* Maybe the user added more data.  These blocks cannot be
    110 	     read using 'elf_rawdata'.  Simply proceed with looking
    111 	     for more data block with 'elf_getdata'.  */
    112 	}
    113 
    114       /* Iterate through the list of data blocks.  */
    115       while ((data = INTUSE(elf_getdata) (scn, data)) != NULL)
    116 	/* If the file byte order is the same as the host byte order
    117 	   process the buffer directly.  If the data is just a stream
    118 	   of bytes which the library will not convert we can use it
    119 	   as well.  */
    120 	if (likely (same_byte_order) || data->d_type == ELF_T_BYTE)
    121 	  result = process_block (result, data);
    122 	else
    123 	  {
    124 	    /* Convert the data to file byte order.  */
    125 	    if (INTUSE(elfw2(LIBELFBITS,xlatetof)) (data, data, ident[EI_DATA])
    126 		== NULL)
    127 	      return -1l;
    128 
    129 	    result = process_block (result, data);
    130 
    131 	    /* And convert it back.  */
    132 	    if (INTUSE(elfw2(LIBELFBITS,xlatetom)) (data, data, ident[EI_DATA])
    133 		== NULL)
    134 	      return -1l;
    135 	  }
    136     }
    137 
    138   return result;
    139 }
    140 INTDEF(elfw2(LIBELFBITS,checksum))
    141