xref: /minix3/external/bsd/elftoolchain/dist/libelf/libelf_checksum.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: libelf_checksum.c,v 1.2 2014/03/09 16:58:04 christos Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2006,2008 Joseph Koshy
5*0a6a1f1dSLionel Sambuc  * All rights reserved.
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc  * are met:
10*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc  *
16*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*0a6a1f1dSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*0a6a1f1dSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*0a6a1f1dSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*0a6a1f1dSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*0a6a1f1dSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*0a6a1f1dSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*0a6a1f1dSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*0a6a1f1dSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0a6a1f1dSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0a6a1f1dSLionel Sambuc  * SUCH DAMAGE.
27*0a6a1f1dSLionel Sambuc  */
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
30*0a6a1f1dSLionel Sambuc 
31*0a6a1f1dSLionel Sambuc #include <gelf.h>
32*0a6a1f1dSLionel Sambuc 
33*0a6a1f1dSLionel Sambuc #include "_libelf.h"
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: libelf_checksum.c,v 1.2 2014/03/09 16:58:04 christos Exp $");
36*0a6a1f1dSLionel Sambuc ELFTC_VCSID("Id: libelf_checksum.c 2225 2011-11-26 18:55:54Z jkoshy ");
37*0a6a1f1dSLionel Sambuc 
38*0a6a1f1dSLionel Sambuc static unsigned long
_libelf_sum(unsigned long c,const unsigned char * s,size_t size)39*0a6a1f1dSLionel Sambuc _libelf_sum(unsigned long c, const unsigned char *s, size_t size)
40*0a6a1f1dSLionel Sambuc {
41*0a6a1f1dSLionel Sambuc 	if (s == NULL || size == 0)
42*0a6a1f1dSLionel Sambuc 		return (c);
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc 	while (size--)
45*0a6a1f1dSLionel Sambuc 		c += *s++;
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc 	return (c);
48*0a6a1f1dSLionel Sambuc }
49*0a6a1f1dSLionel Sambuc 
50*0a6a1f1dSLionel Sambuc unsigned long
_libelf_checksum(Elf * e,int elfclass)51*0a6a1f1dSLionel Sambuc _libelf_checksum(Elf *e, int elfclass)
52*0a6a1f1dSLionel Sambuc {
53*0a6a1f1dSLionel Sambuc 	size_t shn;
54*0a6a1f1dSLionel Sambuc 	Elf_Scn *scn;
55*0a6a1f1dSLionel Sambuc 	Elf_Data *d;
56*0a6a1f1dSLionel Sambuc 	unsigned long checksum;
57*0a6a1f1dSLionel Sambuc 	GElf_Ehdr eh;
58*0a6a1f1dSLionel Sambuc 	GElf_Shdr shdr;
59*0a6a1f1dSLionel Sambuc 
60*0a6a1f1dSLionel Sambuc 	if (e == NULL) {
61*0a6a1f1dSLionel Sambuc 		LIBELF_SET_ERROR(ARGUMENT, 0);
62*0a6a1f1dSLionel Sambuc 		return (0L);
63*0a6a1f1dSLionel Sambuc 	}
64*0a6a1f1dSLionel Sambuc 
65*0a6a1f1dSLionel Sambuc 	if (e->e_class != elfclass) {
66*0a6a1f1dSLionel Sambuc 		LIBELF_SET_ERROR(CLASS, 0);
67*0a6a1f1dSLionel Sambuc 		return (0L);
68*0a6a1f1dSLionel Sambuc 	}
69*0a6a1f1dSLionel Sambuc 
70*0a6a1f1dSLionel Sambuc 	if (gelf_getehdr(e, &eh) == NULL)
71*0a6a1f1dSLionel Sambuc 		return (0);
72*0a6a1f1dSLionel Sambuc 
73*0a6a1f1dSLionel Sambuc 	/*
74*0a6a1f1dSLionel Sambuc 	 * Iterate over all sections in the ELF file, computing the
75*0a6a1f1dSLionel Sambuc 	 * checksum along the way.
76*0a6a1f1dSLionel Sambuc 	 *
77*0a6a1f1dSLionel Sambuc 	 * The first section is always SHN_UNDEF and can be skipped.
78*0a6a1f1dSLionel Sambuc 	 * Non-allocatable sections are skipped, as are sections that
79*0a6a1f1dSLionel Sambuc 	 * could be affected by utilities such as strip(1).
80*0a6a1f1dSLionel Sambuc 	 */
81*0a6a1f1dSLionel Sambuc 
82*0a6a1f1dSLionel Sambuc 	checksum = 0;
83*0a6a1f1dSLionel Sambuc 	for (shn = 1; shn < e->e_u.e_elf.e_nscn; shn++) {
84*0a6a1f1dSLionel Sambuc 		if ((scn = elf_getscn(e, shn)) == NULL)
85*0a6a1f1dSLionel Sambuc 			return (0);
86*0a6a1f1dSLionel Sambuc 		if (gelf_getshdr(scn, &shdr) == NULL)
87*0a6a1f1dSLionel Sambuc 			return (0);
88*0a6a1f1dSLionel Sambuc 		if ((shdr.sh_flags & SHF_ALLOC) == 0 ||
89*0a6a1f1dSLionel Sambuc 		    shdr.sh_type == SHT_DYNAMIC ||
90*0a6a1f1dSLionel Sambuc 		    shdr.sh_type == SHT_DYNSYM)
91*0a6a1f1dSLionel Sambuc 			continue;
92*0a6a1f1dSLionel Sambuc 
93*0a6a1f1dSLionel Sambuc 		d = NULL;
94*0a6a1f1dSLionel Sambuc 		while ((d = elf_rawdata(scn, d)) != NULL)
95*0a6a1f1dSLionel Sambuc 			checksum = _libelf_sum(checksum,
96*0a6a1f1dSLionel Sambuc 			    (unsigned char *) d->d_buf, d->d_size);
97*0a6a1f1dSLionel Sambuc 	}
98*0a6a1f1dSLionel Sambuc 
99*0a6a1f1dSLionel Sambuc 	/*
100*0a6a1f1dSLionel Sambuc 	 * Return a 16-bit checksum compatible with Solaris.
101*0a6a1f1dSLionel Sambuc 	 */
102*0a6a1f1dSLionel Sambuc 	return (((checksum >> 16) & 0xFFFFUL) + (checksum & 0xFFFFUL));
103*0a6a1f1dSLionel Sambuc }
104