summaryrefslogtreecommitdiff
path: root/lib/checksum.c
blob: f9e964bc17c67a59f79b59765af1bb9c44dd3fed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
checksum.c - implementation of the elf{32,64}_checksum(3) functions.
Copyright (C) 1995 - 2001 Michael Riepe

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

#include <private.h>

#ifndef lint
static const char rcsid[] = "@(#) $Id: checksum.c,v 1.7 2008/05/23 08:15:34 michael Exp $";
#endif /* lint */

/*
 * Compatibility note:
 *
 * The algorithm used in {elf32,elf64,gelf}_checksum() does not seem to
 * be documented.  I hope I got it right.  My implementation does the
 * following:
 *
 *   - skip sections that do not have the SHF_ALLOC flag set
 *   - skip sections of type SHT_NULL, SHT_NOBITS, SHT_DYNSYM and
 *     SHT_DYNAMIC
 *   - add all data bytes from the remaining sections, modulo 2**32
 *   - add upper and lower half of the result
 *   - subtract 0xffff if the result is > 0xffff
 *   - if any error occurs, return 0L
 */

static int
skip_section(Elf_Scn *scn, unsigned cls) {
    if (cls == ELFCLASS32) {
	Elf32_Shdr *shdr = &scn->s_shdr32;

	if (!(shdr->sh_flags & SHF_ALLOC)) {
	    return 1;
	}
	switch (shdr->sh_type) {
	    case SHT_NULL:
	    case SHT_NOBITS:
	    /* Solaris seems to ignore these, too */
	    case SHT_DYNSYM:
	    case SHT_DYNAMIC:
		return 1;
	}
    }
#if __LIBELF64
    else if (cls == ELFCLASS64) {
	Elf64_Shdr *shdr = &scn->s_shdr64;

	if (!(shdr->sh_flags & SHF_ALLOC)) {
	    return 1;
	}
	switch (shdr->sh_type) {
	    case SHT_NULL:
	    case SHT_NOBITS:
	    /* Solaris seems to ignore these, too */
	    case SHT_DYNSYM:
	    case SHT_DYNAMIC:
		return 1;
	}
    }
#endif /* __LIBELF64 */
    else {
	seterr(ERROR_UNIMPLEMENTED);
    }
    return 0;
}

static long
add_bytes(unsigned char *ptr, size_t len) {
    long csum = 0;

    while (len--) {
	csum += *ptr++;
    }
    return csum;
}

static long
_elf_csum(Elf *elf) {
    long csum = 0;
    Elf_Data *data;
    Elf_Scn *scn;

    if (!elf->e_ehdr && !_elf_cook(elf)) {
	/* propagate errors from _elf_cook */
	return 0L;
    }
    seterr(0);
    for (scn = elf->e_scn_1; scn; scn = scn->s_link) {
	if (scn->s_index == SHN_UNDEF || skip_section(scn, elf->e_class)) {
	    continue;
	}
	data = NULL;
	while ((data = elf_getdata(scn, data))) {
	    if (data->d_size) {
		if (data->d_buf == NULL) {
		    seterr(ERROR_NULLBUF);
		    return 0L;
		}
		csum += add_bytes(data->d_buf, data->d_size);
	    }
	}
    }
    if (_elf_errno) {
	return 0L;
    }
    csum = (csum & 0xffff) + ((csum >> 16) & 0xffff);
    if (csum > 0xffff) {
	csum -= 0xffff;
    }
    return csum;
}

long
elf32_checksum(Elf *elf) {
    if (elf) {
	if (elf->e_kind != ELF_K_ELF) {
	    seterr(ERROR_NOTELF);
	}
	else if (elf->e_class != ELFCLASS32) {
	    seterr(ERROR_CLASSMISMATCH);
	}
	else {
	    return _elf_csum(elf);
	}
    }
    return 0L;
}

#if __LIBELF64

long
elf64_checksum(Elf *elf) {
    if (elf) {
	if (elf->e_kind != ELF_K_ELF) {
	    seterr(ERROR_NOTELF);
	}
	else if (elf->e_class != ELFCLASS64) {
	    seterr(ERROR_CLASSMISMATCH);
	}
	else {
	    return _elf_csum(elf);
	}
    }
    return 0L;
}

long
gelf_checksum(Elf *elf) {
    if (elf) {
	if (elf->e_kind != ELF_K_ELF) {
	    seterr(ERROR_NOTELF);
	}
	else if (!valid_class(elf->e_class)) {
	    seterr(ERROR_UNKNOWN_CLASS);
	}
	else {
	    return _elf_csum(elf);
	}
    }
    return 0L;
}

#endif /* __LIBELF64 */