libelf_checksum.c revision 165535
1164190Sjkoshy/*- 2164190Sjkoshy * Copyright (c) 2006 Joseph Koshy 3164190Sjkoshy * All rights reserved. 4164190Sjkoshy * 5164190Sjkoshy * Redistribution and use in source and binary forms, with or without 6164190Sjkoshy * modification, are permitted provided that the following conditions 7164190Sjkoshy * are met: 8164190Sjkoshy * 1. Redistributions of source code must retain the above copyright 9164190Sjkoshy * notice, this list of conditions and the following disclaimer. 10164190Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright 11164190Sjkoshy * notice, this list of conditions and the following disclaimer in the 12164190Sjkoshy * documentation and/or other materials provided with the distribution. 13164190Sjkoshy * 14164190Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15164190Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16164190Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17164190Sjkoshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18164190Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19164190Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20164190Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21164190Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22164190Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23164190Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24164190Sjkoshy * SUCH DAMAGE. 25164190Sjkoshy */ 26164190Sjkoshy 27164190Sjkoshy#include <sys/cdefs.h> 28164190Sjkoshy__FBSDID("$FreeBSD: head/lib/libelf/libelf_checksum.c 165535 2006-12-25 02:22:22Z jkoshy $"); 29164190Sjkoshy 30164190Sjkoshy#include <gelf.h> 31164190Sjkoshy 32164190Sjkoshy#include "_libelf.h" 33164190Sjkoshy 34164190Sjkoshystatic unsigned long 35164190Sjkoshy_libelf_sum(unsigned long c, const unsigned char *s, size_t size) 36164190Sjkoshy{ 37164190Sjkoshy if (s == NULL || size == 0) 38164190Sjkoshy return (c); 39164190Sjkoshy 40164190Sjkoshy while (size--) 41164190Sjkoshy c += *s++; 42164190Sjkoshy 43164190Sjkoshy return (c); 44164190Sjkoshy} 45164190Sjkoshy 46164190Sjkoshyunsigned long 47164190Sjkoshy_libelf_checksum(Elf *e, int elfclass) 48164190Sjkoshy{ 49164190Sjkoshy size_t shn; 50164190Sjkoshy Elf_Scn *scn; 51164190Sjkoshy Elf_Data *d; 52164190Sjkoshy unsigned long checksum; 53164190Sjkoshy GElf_Ehdr eh; 54164190Sjkoshy GElf_Shdr shdr; 55164190Sjkoshy 56164190Sjkoshy if (e == NULL) { 57164190Sjkoshy LIBELF_SET_ERROR(ARGUMENT, 0); 58164190Sjkoshy return (0L); 59164190Sjkoshy } 60164190Sjkoshy 61164190Sjkoshy if (e->e_class != elfclass) { 62164190Sjkoshy LIBELF_SET_ERROR(CLASS, 0); 63164190Sjkoshy return (0L); 64164190Sjkoshy } 65164190Sjkoshy 66164190Sjkoshy if (gelf_getehdr(e, &eh) == NULL) 67164190Sjkoshy return (0); 68164190Sjkoshy 69164190Sjkoshy /* 70164190Sjkoshy * Iterate over all sections in the ELF file, computing the 71164190Sjkoshy * checksum along the way. 72164190Sjkoshy * 73164190Sjkoshy * The first section is always SHN_UNDEF and can be skipped. 74164190Sjkoshy * Non-allocatable sections are skipped, as are sections that 75164190Sjkoshy * could be affected by utilities such as strip(1). 76164190Sjkoshy */ 77164190Sjkoshy 78164190Sjkoshy checksum = 0; 79165535Sjkoshy for (shn = 1; shn < e->e_u.e_elf.e_nscn; shn++) { 80164190Sjkoshy if ((scn = elf_getscn(e, shn)) == NULL) 81164190Sjkoshy return (0); 82164190Sjkoshy if (gelf_getshdr(scn, &shdr) == NULL) 83164190Sjkoshy return (0); 84164190Sjkoshy if ((shdr.sh_flags & SHF_ALLOC) == 0 || 85164190Sjkoshy shdr.sh_type == SHT_DYNAMIC || 86164190Sjkoshy shdr.sh_type == SHT_DYNSYM) 87164190Sjkoshy continue; 88164190Sjkoshy 89164190Sjkoshy d = NULL; 90164190Sjkoshy while ((d = elf_rawdata(scn, d)) != NULL) 91164190Sjkoshy checksum = _libelf_sum(checksum, 92164190Sjkoshy (unsigned char *) d->d_buf, d->d_size); 93164190Sjkoshy } 94164190Sjkoshy 95164190Sjkoshy /* 96164190Sjkoshy * Return a 16-bit checksum compatible with Solaris. 97164190Sjkoshy */ 98164190Sjkoshy return (((checksum >> 16) & 0xFFFFUL) + (checksum & 0xFFFFUL)); 99164190Sjkoshy} 100