libelf_checksum.c revision 260697
1254721Semaste/*-
2254721Semaste * Copyright (c) 2006,2008 Joseph Koshy
3254721Semaste * All rights reserved.
4254721Semaste *
5254721Semaste * Redistribution and use in source and binary forms, with or without
6254721Semaste * modification, are permitted provided that the following conditions
7254721Semaste * are met:
8254721Semaste * 1. Redistributions of source code must retain the above copyright
9254721Semaste *    notice, this list of conditions and the following disclaimer.
10254721Semaste * 2. Redistributions in binary form must reproduce the above copyright
11254721Semaste *    notice, this list of conditions and the following disclaimer in the
12254721Semaste *    documentation and/or other materials provided with the distribution.
13254721Semaste *
14254721Semaste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16254721Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17254721Semaste * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18254721Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19254721Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20254721Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21254721Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22254721Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24254721Semaste * SUCH DAMAGE.
25254721Semaste */
26254721Semaste
27254721Semaste#include <sys/cdefs.h>
28254721Semaste
29254721Semaste#include <gelf.h>
30254721Semaste
31254721Semaste#include "_libelf.h"
32254721Semaste
33254721SemasteELFTC_VCSID("$Id: libelf_checksum.c 2225 2011-11-26 18:55:54Z jkoshy $");
34254721Semaste
35254721Semastestatic unsigned long
36254721Semaste_libelf_sum(unsigned long c, const unsigned char *s, size_t size)
37254721Semaste{
38254721Semaste	if (s == NULL || size == 0)
39254721Semaste		return (c);
40254721Semaste
41254721Semaste	while (size--)
42254721Semaste		c += *s++;
43254721Semaste
44254721Semaste	return (c);
45254721Semaste}
46254721Semaste
47254721Semasteunsigned long
48254721Semaste_libelf_checksum(Elf *e, int elfclass)
49254721Semaste{
50254721Semaste	size_t shn;
51254721Semaste	Elf_Scn *scn;
52254721Semaste	Elf_Data *d;
53254721Semaste	unsigned long checksum;
54254721Semaste	GElf_Ehdr eh;
55254721Semaste	GElf_Shdr shdr;
56254721Semaste
57254721Semaste	if (e == NULL) {
58254721Semaste		LIBELF_SET_ERROR(ARGUMENT, 0);
59254721Semaste		return (0L);
60254721Semaste	}
61254721Semaste
62254721Semaste	if (e->e_class != elfclass) {
63254721Semaste		LIBELF_SET_ERROR(CLASS, 0);
64254721Semaste		return (0L);
65254721Semaste	}
66254721Semaste
67254721Semaste	if (gelf_getehdr(e, &eh) == NULL)
68254721Semaste		return (0);
69254721Semaste
70254721Semaste	/*
71254721Semaste	 * Iterate over all sections in the ELF file, computing the
72254721Semaste	 * checksum along the way.
73254721Semaste	 *
74254721Semaste	 * The first section is always SHN_UNDEF and can be skipped.
75254721Semaste	 * Non-allocatable sections are skipped, as are sections that
76254721Semaste	 * could be affected by utilities such as strip(1).
77254721Semaste	 */
78254721Semaste
79254721Semaste	checksum = 0;
80254721Semaste	for (shn = 1; shn < e->e_u.e_elf.e_nscn; shn++) {
81254721Semaste		if ((scn = elf_getscn(e, shn)) == NULL)
82254721Semaste			return (0);
83254721Semaste		if (gelf_getshdr(scn, &shdr) == NULL)
84254721Semaste			return (0);
85254721Semaste		if ((shdr.sh_flags & SHF_ALLOC) == 0 ||
86254721Semaste		    shdr.sh_type == SHT_DYNAMIC ||
87254721Semaste		    shdr.sh_type == SHT_DYNSYM)
88254721Semaste			continue;
89254721Semaste
90254721Semaste		d = NULL;
91254721Semaste		while ((d = elf_rawdata(scn, d)) != NULL)
92254721Semaste			checksum = _libelf_sum(checksum,
93254721Semaste			    (unsigned char *) d->d_buf, d->d_size);
94254721Semaste	}
95254721Semaste
96254721Semaste	/*
97254721Semaste	 * Return a 16-bit checksum compatible with Solaris.
98254721Semaste	 */
99254721Semaste	return (((checksum >> 16) & 0xFFFFUL) + (checksum & 0xFFFFUL));
100254721Semaste}
101254721Semaste