1240616Sjimharris/*-
2240616Sjimharris * Copyright (c) 2006,2008 Joseph Koshy
3240616Sjimharris * All rights reserved.
4240616Sjimharris *
5240616Sjimharris * Redistribution and use in source and binary forms, with or without
6240616Sjimharris * modification, are permitted provided that the following conditions
7240616Sjimharris * are met:
8240616Sjimharris * 1. Redistributions of source code must retain the above copyright
9240616Sjimharris *    notice, this list of conditions and the following disclaimer.
10240616Sjimharris * 2. Redistributions in binary form must reproduce the above copyright
11240616Sjimharris *    notice, this list of conditions and the following disclaimer in the
12240616Sjimharris *    documentation and/or other materials provided with the distribution.
13240616Sjimharris *
14240616Sjimharris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15240616Sjimharris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16240616Sjimharris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17240616Sjimharris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18240616Sjimharris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19240616Sjimharris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20240616Sjimharris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21240616Sjimharris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22240616Sjimharris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23240616Sjimharris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24240616Sjimharris * SUCH DAMAGE.
25240616Sjimharris */
26240616Sjimharris
27240616Sjimharris#include <gelf.h>
28240616Sjimharris
29240616Sjimharris#include "_libelf.h"
30240616Sjimharris
31240616SjimharrisELFTC_VCSID("$Id: libelf_checksum.c 3174 2015-03-27 17:13:41Z emaste $");
32240616Sjimharris
33240616Sjimharrisstatic unsigned long
34240616Sjimharris_libelf_sum(unsigned long c, const unsigned char *s, size_t size)
35240616Sjimharris{
36240616Sjimharris	if (s == NULL || size == 0)
37240616Sjimharris		return (c);
38240616Sjimharris
39240616Sjimharris	while (size--)
40240616Sjimharris		c += *s++;
41240616Sjimharris
42240616Sjimharris	return (c);
43240616Sjimharris}
44240616Sjimharris
45240616Sjimharrislong
46240616Sjimharris_libelf_checksum(Elf *e, int elfclass)
47240616Sjimharris{
48240616Sjimharris	size_t shn;
49240616Sjimharris	Elf_Scn *scn;
50240616Sjimharris	Elf_Data *d;
51240616Sjimharris	unsigned long checksum;
52240616Sjimharris	GElf_Ehdr eh;
53240616Sjimharris	GElf_Shdr shdr;
54240616Sjimharris
55240616Sjimharris	if (e == NULL) {
56240616Sjimharris		LIBELF_SET_ERROR(ARGUMENT, 0);
57240616Sjimharris		return (0L);
58240616Sjimharris	}
59240616Sjimharris
60240616Sjimharris	if (e->e_class != elfclass) {
61240616Sjimharris		LIBELF_SET_ERROR(CLASS, 0);
62240616Sjimharris		return (0L);
63240616Sjimharris	}
64240616Sjimharris
65240616Sjimharris	if (gelf_getehdr(e, &eh) == NULL)
66240616Sjimharris		return (0);
67240616Sjimharris
68240616Sjimharris	/*
69240616Sjimharris	 * Iterate over all sections in the ELF file, computing the
70240616Sjimharris	 * checksum along the way.
71240616Sjimharris	 *
72240616Sjimharris	 * The first section is always SHN_UNDEF and can be skipped.
73240616Sjimharris	 * Non-allocatable sections are skipped, as are sections that
74240616Sjimharris	 * could be affected by utilities such as strip(1).
75240616Sjimharris	 */
76240616Sjimharris
77240616Sjimharris	checksum = 0;
78240616Sjimharris	for (shn = 1; shn < e->e_u.e_elf.e_nscn; shn++) {
79240616Sjimharris		if ((scn = elf_getscn(e, shn)) == NULL)
80240616Sjimharris			return (0);
81240616Sjimharris		if (gelf_getshdr(scn, &shdr) == NULL)
82240616Sjimharris			return (0);
83240616Sjimharris		if ((shdr.sh_flags & SHF_ALLOC) == 0 ||
84240616Sjimharris		    shdr.sh_type == SHT_DYNAMIC ||
85240616Sjimharris		    shdr.sh_type == SHT_DYNSYM)
86240616Sjimharris			continue;
87240616Sjimharris
88240616Sjimharris		d = NULL;
89240616Sjimharris		while ((d = elf_rawdata(scn, d)) != NULL)
90240616Sjimharris			checksum = _libelf_sum(checksum,
91240616Sjimharris			    (unsigned char *) d->d_buf, (size_t) d->d_size);
92240616Sjimharris	}
93240616Sjimharris
94240616Sjimharris	/*
95240616Sjimharris	 * Return a 16-bit checksum compatible with Solaris.
96240616Sjimharris	 */
97240616Sjimharris	return (long) (((checksum >> 16) & 0xFFFFUL) + (checksum & 0xFFFFUL));
98240616Sjimharris}
99240616Sjimharris