10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53621Sab196087 * Common Development and Distribution License (the "License").
63621Sab196087 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216812Sraf
220Sstevel@tonic-gate /*
23*11776SPeter.Dennis@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */
310Sstevel@tonic-gate /* All Rights Reserved */
320Sstevel@tonic-gate
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <libelf.h>
360Sstevel@tonic-gate #include "decl.h"
370Sstevel@tonic-gate #include "msg.h"
380Sstevel@tonic-gate
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate * Routines for generating a checksum for an elf image. Typically used to create
410Sstevel@tonic-gate * a DT_CHECKSUM entry. This checksum is intended to remain constant after
420Sstevel@tonic-gate * operations such as strip(1)/mcs(1), thus only allocatable sections are
430Sstevel@tonic-gate * processed, and of those, any that might be modified by these external
440Sstevel@tonic-gate * commands are skipped.
450Sstevel@tonic-gate */
460Sstevel@tonic-gate #define MSW(l) (((l) >> 16) & 0x0000ffffL)
470Sstevel@tonic-gate #define LSW(l) ((l) & 0x0000ffffL)
480Sstevel@tonic-gate
490Sstevel@tonic-gate
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate * update and epilogue sum functions (stolen from libcmd)
520Sstevel@tonic-gate */
530Sstevel@tonic-gate static long
sumupd(long sum,char * cp,unsigned long cnt)540Sstevel@tonic-gate sumupd(long sum, char *cp, unsigned long cnt)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate if ((cp == 0) || (cnt == 0))
570Sstevel@tonic-gate return (sum);
580Sstevel@tonic-gate
590Sstevel@tonic-gate while (cnt--)
600Sstevel@tonic-gate sum += *cp++ & 0x00ff;
610Sstevel@tonic-gate
620Sstevel@tonic-gate return (sum);
630Sstevel@tonic-gate }
640Sstevel@tonic-gate
650Sstevel@tonic-gate static long
sumepi(long sum)660Sstevel@tonic-gate sumepi(long sum)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate long _sum;
690Sstevel@tonic-gate
700Sstevel@tonic-gate _sum = LSW(sum) + MSW(sum);
710Sstevel@tonic-gate return ((ushort_t)(LSW(_sum) + MSW(_sum)));
720Sstevel@tonic-gate }
730Sstevel@tonic-gate
74*11776SPeter.Dennis@Sun.COM /*
75*11776SPeter.Dennis@Sun.COM * This module is compiled twice, the second time having
76*11776SPeter.Dennis@Sun.COM * -D_ELF64 defined. The following set of macros represent
77*11776SPeter.Dennis@Sun.COM * the differences between the two compilations. Be
78*11776SPeter.Dennis@Sun.COM * careful *not* to add any class dependent code (anything
79*11776SPeter.Dennis@Sun.COM * that has elf32 or elf64 in the name) to this code
80*11776SPeter.Dennis@Sun.COM * without hiding it behind a switchable macro like these.
81*11776SPeter.Dennis@Sun.COM */
82*11776SPeter.Dennis@Sun.COM #if defined(_ELF64)
83*11776SPeter.Dennis@Sun.COM
84*11776SPeter.Dennis@Sun.COM #define elf_checksum elf64_checksum
85*11776SPeter.Dennis@Sun.COM #define Elf_Ehdr Elf64_Ehdr
86*11776SPeter.Dennis@Sun.COM #define Elf_Shdr Elf64_Shdr
87*11776SPeter.Dennis@Sun.COM #define getehdr elf64_getehdr
88*11776SPeter.Dennis@Sun.COM #define getshdr elf64_getshdr
89*11776SPeter.Dennis@Sun.COM
90*11776SPeter.Dennis@Sun.COM #else /* else ELF32 */
91*11776SPeter.Dennis@Sun.COM
92*11776SPeter.Dennis@Sun.COM #define elf_checksum elf32_checksum
93*11776SPeter.Dennis@Sun.COM #define Elf_Ehdr Elf32_Ehdr
94*11776SPeter.Dennis@Sun.COM #define Elf_Shdr Elf32_Shdr
95*11776SPeter.Dennis@Sun.COM #define getehdr elf32_getehdr
96*11776SPeter.Dennis@Sun.COM #define getshdr elf32_getshdr
97*11776SPeter.Dennis@Sun.COM
98*11776SPeter.Dennis@Sun.COM #endif /* ELF64 */
99*11776SPeter.Dennis@Sun.COM
1000Sstevel@tonic-gate long
elf_checksum(Elf * elf)101*11776SPeter.Dennis@Sun.COM elf_checksum(Elf * elf)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate long sum = 0;
104*11776SPeter.Dennis@Sun.COM Elf_Ehdr * ehdr;
105*11776SPeter.Dennis@Sun.COM Elf_Shdr * shdr;
1060Sstevel@tonic-gate Elf_Scn * scn;
1070Sstevel@tonic-gate Elf_Data * data, * (* getdata)(Elf_Scn *, Elf_Data *);
1080Sstevel@tonic-gate size_t shnum;
1090Sstevel@tonic-gate
110*11776SPeter.Dennis@Sun.COM if ((ehdr = getehdr(elf)) == 0)
1110Sstevel@tonic-gate return (0);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate * Determine the data information to retrieve. When called from ld()
1150Sstevel@tonic-gate * we're processing an ELF_C_IMAGE (memory) image and thus need to use
1160Sstevel@tonic-gate * elf_getdata(), as there is not yet a file image (or raw data) backing
1170Sstevel@tonic-gate * this. When called from utilities like elfdump(1) we're processing a
1180Sstevel@tonic-gate * file image and thus using the elf_rawdata() allows the same byte
1190Sstevel@tonic-gate * stream to be processed from different architectures - presently this
1200Sstevel@tonic-gate * is irrelevant, as the checksum simply sums the data bytes, their
1210Sstevel@tonic-gate * order doesn't matter. But being uncooked is slightly less overhead.
1225088Sab196087 *
1235088Sab196087 * If the file is writable, the raw data will not reflect any
1245088Sab196087 * changes made in the process, so the uncooked version is only
1255088Sab196087 * for readonly files.
1260Sstevel@tonic-gate */
1275088Sab196087 if ((elf->ed_myflags & (EDF_MEMORY | EDF_WRITE)) != 0)
1280Sstevel@tonic-gate getdata = elf_getdata;
1290Sstevel@tonic-gate else
1300Sstevel@tonic-gate getdata = elf_rawdata;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate for (shnum = 1; shnum < ehdr->e_shnum; shnum++) {
1330Sstevel@tonic-gate if ((scn = elf_getscn(elf, shnum)) == 0)
1340Sstevel@tonic-gate return (0);
135*11776SPeter.Dennis@Sun.COM if ((shdr = getshdr(scn)) == 0)
1360Sstevel@tonic-gate return (0);
1370Sstevel@tonic-gate
1383621Sab196087 /* Exclude strippable sections */
1390Sstevel@tonic-gate if (!(shdr->sh_flags & SHF_ALLOC))
1400Sstevel@tonic-gate continue;
1410Sstevel@tonic-gate
1423621Sab196087 /*
1433621Sab196087 * Exclude allocable sections that can change:
1443621Sab196087 * - The .dynsym section can contain section symbols
1453621Sab196087 * that strip might remove.
1463621Sab196087 * - The .dynamic section is modified by the setting of
1473621Sab196087 * this checksum value.
148*11776SPeter.Dennis@Sun.COM * - The .SUNW_dof section uses ftok(3C), which returns
149*11776SPeter.Dennis@Sun.COM * different values, to define a key for the
150*11776SPeter.Dennis@Sun.COM * objects in that section.
1513621Sab196087 */
1520Sstevel@tonic-gate if ((shdr->sh_type == SHT_DYNSYM) ||
153*11776SPeter.Dennis@Sun.COM (shdr->sh_type == SHT_DYNAMIC) ||
154*11776SPeter.Dennis@Sun.COM (shdr->sh_type == SHT_SUNW_dof))
1550Sstevel@tonic-gate continue;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate data = 0;
158*11776SPeter.Dennis@Sun.COM while ((data = (*getdata)(scn, data)) != 0)
1590Sstevel@tonic-gate sum = sumupd(sum, data->d_buf, data->d_size);
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate return (sumepi(sum));
1630Sstevel@tonic-gate }
164