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