add some utils
[libelf-compat.git] / src / elf32_checksum.c
blob4c5985630c0ad802dfa5e053a8a27748a31c9bdc
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 elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
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
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
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
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.
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/>. */
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
34 #include <assert.h>
35 #include <endian.h>
36 #include <stdbool.h>
37 #include <stddef.h>
38 #include <string.h>
40 #include "gelf.h"
41 #include "libelfP.h"
42 #include "elf-knowledge.h"
44 #ifndef LIBELFBITS
45 # define LIBELFBITS 32
46 #endif
49 #define process_block(crc, data) \
50 __libelf_crc32 (crc, data->d_buf, data->d_size)
53 long int
54 elfw2(LIBELFBITS,checksum) (elf)
55 Elf *elf;
57 size_t shstrndx;
58 Elf_Scn *scn;
59 long int result = 0;
60 unsigned char *ident;
61 bool same_byte_order;
63 if (elf == NULL)
64 return -1l;
66 /* Find the section header string table. */
67 if (INTUSE(elf_getshdrstrndx) (elf, &shstrndx) < 0)
69 /* This can only happen if the ELF handle is not for real. */
70 __libelf_seterrno (ELF_E_INVALID_HANDLE);
71 return -1l;
74 /* Determine whether the byte order of the file and that of the host
75 is the same. */
76 ident = elf->state.ELFW(elf,LIBELFBITS).ehdr->e_ident;
77 same_byte_order = ((ident[EI_DATA] == ELFDATA2LSB
78 && __BYTE_ORDER == __LITTLE_ENDIAN)
79 || (ident[EI_DATA] == ELFDATA2MSB
80 && __BYTE_ORDER == __BIG_ENDIAN));
82 /* If we don't have native byte order, we will likely need to
83 convert the data with xlate functions. We do it upfront instead
84 of relocking mid-iteration. */
85 if (!likely (same_byte_order))
86 rwlock_wrlock (elf->lock);
87 else
88 rwlock_rdlock (elf->lock);
90 /* Iterate over all sections to find those which are not strippable. */
91 scn = NULL;
92 while ((scn = INTUSE(elf_nextscn) (elf, scn)) != NULL)
94 GElf_Shdr shdr_mem;
95 GElf_Shdr *shdr;
96 Elf_Data *data;
98 /* Get the section header. */
99 shdr = INTUSE(gelf_getshdr) (scn, &shdr_mem);
100 if (shdr == NULL)
102 __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
103 result = -1l;
104 goto out;
107 if (SECTION_STRIP_P (shdr,
108 INTUSE(elf_strptr) (elf, shstrndx, shdr->sh_name),
109 true))
110 /* The section can be stripped. Don't use it. */
111 continue;
113 /* Do not look at NOBITS sections. */
114 if (shdr->sh_type == SHT_NOBITS)
115 continue;
117 /* To compute the checksum we need to get to the data. For
118 repeatable results we must use the external format. The data
119 we get with 'elf'getdata' might be changed for endianess
120 reasons. Therefore we use 'elf_rawdata' if possible. But
121 this function can fail if the data was constructed by the
122 program. In this case we have to use 'elf_getdata' and
123 eventually convert the data to the external format. */
124 data = INTUSE(elf_rawdata) (scn, NULL);
125 if (data != NULL)
127 /* The raw data is available. */
128 result = process_block (result, data);
130 /* Maybe the user added more data. These blocks cannot be
131 read using 'elf_rawdata'. Simply proceed with looking
132 for more data block with 'elf_getdata'. */
135 /* Iterate through the list of data blocks. */
136 while ((data = INTUSE(elf_getdata) (scn, data)) != NULL)
137 /* If the file byte order is the same as the host byte order
138 process the buffer directly. If the data is just a stream
139 of bytes which the library will not convert we can use it
140 as well. */
141 if (likely (same_byte_order) || data->d_type == ELF_T_BYTE)
142 result = process_block (result, data);
143 else
145 /* Convert the data to file byte order. */
146 if (INTUSE(elfw2(LIBELFBITS,xlatetof)) (data, data, ident[EI_DATA])
147 == NULL)
149 result = -1l;
150 goto out;
153 result = process_block (result, data);
155 /* And convert it back. */
156 if (INTUSE(elfw2(LIBELFBITS,xlatetom)) (data, data, ident[EI_DATA])
157 == NULL)
159 result = -1l;
160 goto out;
165 out:
166 rwlock_unlock (elf->lock);
167 return result;
169 INTDEF(elfw2(LIBELFBITS,checksum))