1 /* Compute simple checksum from permanent parts of the ELF file.
2 Copyright (C) 2002, 2003, 2004, 2005, 2009 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
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.
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.
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>. */
63 #include "elf-knowledge.h"
66 # define LIBELFBITS 32
70 /* The SECTION_STRIP_P macro wants to call into libebl which we cannot
71 do and do not have to do here. Provide a dummy replacement. */
72 #define ebl_debugscn_p(ebl, name) true
75 #define process_block(crc, data) \
76 __libelf_crc32 (crc, data->d_buf, data->d_size)
80 elfw2(LIBELFBITS,checksum) (elf)
92 /* Find the section header string table. */
93 if (INTUSE(elf_getshdrstrndx) (elf, &shstrndx) < 0)
95 /* This can only happen if the ELF handle is not for real. */
96 __libelf_seterrno (ELF_E_INVALID_HANDLE);
100 /* Determine whether the byte order of the file and that of the host
102 ident = elf->state.ELFW(elf,LIBELFBITS).ehdr->e_ident;
103 same_byte_order = ((ident[EI_DATA] == ELFDATA2LSB
104 && __BYTE_ORDER == __LITTLE_ENDIAN)
105 || (ident[EI_DATA] == ELFDATA2MSB
106 && __BYTE_ORDER == __BIG_ENDIAN));
108 /* If we don't have native byte order, we will likely need to
109 convert the data with xlate functions. We do it upfront instead
110 of relocking mid-iteration. */
111 if (!likely (same_byte_order))
112 rwlock_wrlock (elf->lock);
114 rwlock_rdlock (elf->lock);
116 /* Iterate over all sections to find those which are not strippable. */
118 while ((scn = INTUSE(elf_nextscn) (elf, scn)) != NULL)
124 /* Get the section header. */
125 shdr = INTUSE(gelf_getshdr) (scn, &shdr_mem);
128 __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
133 if (SECTION_STRIP_P (shdr,
134 INTUSE(elf_strptr) (elf, shstrndx, shdr->sh_name),
136 /* The section can be stripped. Don't use it. */
139 /* Do not look at NOBITS sections. */
140 if (shdr->sh_type == SHT_NOBITS)
143 /* To compute the checksum we need to get to the data. For
144 repeatable results we must use the external format. The data
145 we get with 'elf'getdata' might be changed for endianess
146 reasons. Therefore we use 'elf_rawdata' if possible. But
147 this function can fail if the data was constructed by the
148 program. In this case we have to use 'elf_getdata' and
149 eventually convert the data to the external format. */
150 data = INTUSE(elf_rawdata) (scn, NULL);
153 /* The raw data is available. */
154 result = process_block (result, data);
156 /* Maybe the user added more data. These blocks cannot be
157 read using 'elf_rawdata'. Simply proceed with looking
158 for more data block with 'elf_getdata'. */
161 /* Iterate through the list of data blocks. */
162 while ((data = INTUSE(elf_getdata) (scn, data)) != NULL)
163 /* If the file byte order is the same as the host byte order
164 process the buffer directly. If the data is just a stream
165 of bytes which the library will not convert we can use it
167 if (likely (same_byte_order) || data->d_type == ELF_T_BYTE)
168 result = process_block (result, data);
171 /* Convert the data to file byte order. */
172 if (INTUSE(elfw2(LIBELFBITS,xlatetof)) (data, data, ident[EI_DATA])
179 result = process_block (result, data);
181 /* And convert it back. */
182 if (INTUSE(elfw2(LIBELFBITS,xlatetom)) (data, data, ident[EI_DATA])
192 rwlock_unlock (elf->lock);
195 INTDEF(elfw2(LIBELFBITS,checksum))