Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Compute simple checksum from permanent parts of the ELF file. |
Ulrich Drepper | f189493 | 2009-06-13 15:55:42 -0700 | [diff] [blame] | 2 | Copyright (C) 2002, 2003, 2004, 2005, 2009 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame^] | 3 | This file is part of elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | Written by Ulrich Drepper <drepper@redhat.com>, 2002. |
| 5 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame^] | 6 | This file is free software; you can redistribute it and/or modify |
| 7 | it under the terms of either |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 8 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame^] | 9 | * the GNU Lesser General Public License as published by the Free |
| 10 | Software Foundation; either version 3 of the License, or (at |
| 11 | your option) any later version |
| 12 | |
| 13 | or |
| 14 | |
| 15 | * the GNU General Public License as published by the Free |
| 16 | Software Foundation; either version 2 of the License, or (at |
| 17 | your option) any later version |
| 18 | |
| 19 | or both in parallel, as here. |
| 20 | |
| 21 | elfutils is distributed in the hope that it will be useful, but |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 22 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | General Public License for more details. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 25 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame^] | 26 | You should have received copies of the GNU General Public License and |
| 27 | the GNU Lesser General Public License along with this program. If |
| 28 | not, see <http://www.gnu.org/licenses/>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | # include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <assert.h> |
| 35 | #include <endian.h> |
| 36 | #include <stdbool.h> |
| 37 | #include <stddef.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include "gelf.h" |
| 41 | #include "libelfP.h" |
| 42 | #include "elf-knowledge.h" |
| 43 | |
| 44 | #ifndef LIBELFBITS |
| 45 | # define LIBELFBITS 32 |
| 46 | #endif |
| 47 | |
| 48 | |
| 49 | /* The SECTION_STRIP_P macro wants to call into libebl which we cannot |
| 50 | do and do not have to do here. Provide a dummy replacement. */ |
| 51 | #define ebl_debugscn_p(ebl, name) true |
| 52 | |
| 53 | |
| 54 | #define process_block(crc, data) \ |
| 55 | __libelf_crc32 (crc, data->d_buf, data->d_size) |
| 56 | |
| 57 | |
| 58 | long int |
| 59 | elfw2(LIBELFBITS,checksum) (elf) |
| 60 | Elf *elf; |
| 61 | { |
| 62 | size_t shstrndx; |
| 63 | Elf_Scn *scn; |
| 64 | long int result = 0; |
| 65 | unsigned char *ident; |
| 66 | bool same_byte_order; |
| 67 | |
| 68 | if (elf == NULL) |
| 69 | return -1l; |
| 70 | |
| 71 | /* Find the section header string table. */ |
Ulrich Drepper | f189493 | 2009-06-13 15:55:42 -0700 | [diff] [blame] | 72 | if (INTUSE(elf_getshdrstrndx) (elf, &shstrndx) < 0) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 73 | { |
| 74 | /* This can only happen if the ELF handle is not for real. */ |
| 75 | __libelf_seterrno (ELF_E_INVALID_HANDLE); |
| 76 | return -1l; |
| 77 | } |
| 78 | |
| 79 | /* Determine whether the byte order of the file and that of the host |
| 80 | is the same. */ |
| 81 | ident = elf->state.ELFW(elf,LIBELFBITS).ehdr->e_ident; |
| 82 | same_byte_order = ((ident[EI_DATA] == ELFDATA2LSB |
| 83 | && __BYTE_ORDER == __LITTLE_ENDIAN) |
| 84 | || (ident[EI_DATA] == ELFDATA2MSB |
| 85 | && __BYTE_ORDER == __BIG_ENDIAN)); |
| 86 | |
Ulrich Drepper | 02f6645 | 2008-12-04 05:58:16 +0000 | [diff] [blame] | 87 | /* If we don't have native byte order, we will likely need to |
| 88 | convert the data with xlate functions. We do it upfront instead |
| 89 | of relocking mid-iteration. */ |
| 90 | if (!likely (same_byte_order)) |
| 91 | rwlock_wrlock (elf->lock); |
| 92 | else |
| 93 | rwlock_rdlock (elf->lock); |
| 94 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 95 | /* Iterate over all sections to find those which are not strippable. */ |
| 96 | scn = NULL; |
| 97 | while ((scn = INTUSE(elf_nextscn) (elf, scn)) != NULL) |
| 98 | { |
| 99 | GElf_Shdr shdr_mem; |
| 100 | GElf_Shdr *shdr; |
| 101 | Elf_Data *data; |
| 102 | |
| 103 | /* Get the section header. */ |
| 104 | shdr = INTUSE(gelf_getshdr) (scn, &shdr_mem); |
| 105 | if (shdr == NULL) |
| 106 | { |
| 107 | __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER); |
Ulrich Drepper | 02f6645 | 2008-12-04 05:58:16 +0000 | [diff] [blame] | 108 | result = -1l; |
| 109 | goto out; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | if (SECTION_STRIP_P (shdr, |
| 113 | INTUSE(elf_strptr) (elf, shstrndx, shdr->sh_name), |
| 114 | true)) |
| 115 | /* The section can be stripped. Don't use it. */ |
| 116 | continue; |
| 117 | |
| 118 | /* Do not look at NOBITS sections. */ |
| 119 | if (shdr->sh_type == SHT_NOBITS) |
| 120 | continue; |
| 121 | |
| 122 | /* To compute the checksum we need to get to the data. For |
| 123 | repeatable results we must use the external format. The data |
| 124 | we get with 'elf'getdata' might be changed for endianess |
| 125 | reasons. Therefore we use 'elf_rawdata' if possible. But |
| 126 | this function can fail if the data was constructed by the |
| 127 | program. In this case we have to use 'elf_getdata' and |
| 128 | eventually convert the data to the external format. */ |
| 129 | data = INTUSE(elf_rawdata) (scn, NULL); |
| 130 | if (data != NULL) |
| 131 | { |
| 132 | /* The raw data is available. */ |
| 133 | result = process_block (result, data); |
| 134 | |
| 135 | /* Maybe the user added more data. These blocks cannot be |
| 136 | read using 'elf_rawdata'. Simply proceed with looking |
| 137 | for more data block with 'elf_getdata'. */ |
| 138 | } |
| 139 | |
| 140 | /* Iterate through the list of data blocks. */ |
Roland McGrath | b4d6f0f | 2008-08-25 22:55:17 +0000 | [diff] [blame] | 141 | while ((data = INTUSE(elf_getdata) (scn, data)) != NULL) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 142 | /* If the file byte order is the same as the host byte order |
| 143 | process the buffer directly. If the data is just a stream |
| 144 | of bytes which the library will not convert we can use it |
| 145 | as well. */ |
| 146 | if (likely (same_byte_order) || data->d_type == ELF_T_BYTE) |
| 147 | result = process_block (result, data); |
| 148 | else |
| 149 | { |
| 150 | /* Convert the data to file byte order. */ |
| 151 | if (INTUSE(elfw2(LIBELFBITS,xlatetof)) (data, data, ident[EI_DATA]) |
| 152 | == NULL) |
Ulrich Drepper | 02f6645 | 2008-12-04 05:58:16 +0000 | [diff] [blame] | 153 | { |
| 154 | result = -1l; |
| 155 | goto out; |
| 156 | } |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 157 | |
| 158 | result = process_block (result, data); |
| 159 | |
| 160 | /* And convert it back. */ |
| 161 | if (INTUSE(elfw2(LIBELFBITS,xlatetom)) (data, data, ident[EI_DATA]) |
| 162 | == NULL) |
Ulrich Drepper | 02f6645 | 2008-12-04 05:58:16 +0000 | [diff] [blame] | 163 | { |
| 164 | result = -1l; |
| 165 | goto out; |
| 166 | } |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Ulrich Drepper | 02f6645 | 2008-12-04 05:58:16 +0000 | [diff] [blame] | 170 | out: |
| 171 | rwlock_unlock (elf->lock); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 172 | return result; |
| 173 | } |
| 174 | INTDEF(elfw2(LIBELFBITS,checksum)) |