xref: /illumos-gate/usr/src/uts/intel/ia32/krtld/doreloc.c (revision 552ff457)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*552ff457Srie 
237c478bd9Sstevel@tonic-gate /*
24*552ff457Srie  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
27*552ff457Srie 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #if	defined(_KERNEL)
317c478bd9Sstevel@tonic-gate #include	<sys/types.h>
327c478bd9Sstevel@tonic-gate #include	"reloc.h"
337c478bd9Sstevel@tonic-gate #else
347c478bd9Sstevel@tonic-gate #include	<stdio.h>
357c478bd9Sstevel@tonic-gate #include	"sgs.h"
367c478bd9Sstevel@tonic-gate #include	"machdep.h"
377c478bd9Sstevel@tonic-gate #include	"libld.h"
387c478bd9Sstevel@tonic-gate #include	"reloc.h"
397c478bd9Sstevel@tonic-gate #include	"conv.h"
407c478bd9Sstevel@tonic-gate #include	"msg.h"
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
44*552ff457Srie  * This table represents the current relocations that do_reloc() is able to
45*552ff457Srie  * process.  The relocations below that are marked SPECIAL are relocations that
46*552ff457Srie  * take special processing and shouldn't actually ever be passed to do_reloc().
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate const Rel_entry	reloc_table[R_386_NUM] = {
49*552ff457Srie /* R_386_NONE */	{FLG_RE_NOTREL, 0},
507c478bd9Sstevel@tonic-gate /* R_386_32 */		{FLG_RE_NOTREL, 4},
517c478bd9Sstevel@tonic-gate /* R_386_PC32 */	{FLG_RE_PCREL, 4},
527c478bd9Sstevel@tonic-gate /* R_386_GOT32 */	{FLG_RE_GOTADD, 4},
537c478bd9Sstevel@tonic-gate /* R_386_PLT32 */	{FLG_RE_PLTREL | FLG_RE_PCREL, 4},
54*552ff457Srie /* R_386_COPY */	{FLG_RE_NOTREL, 0},			/* SPECIAL */
557c478bd9Sstevel@tonic-gate /* R_386_GLOB_DAT */	{FLG_RE_NOTREL, 4},
56*552ff457Srie /* R_386_JMP_SLOT */	{FLG_RE_NOTREL, 4},			/* SPECIAL */
577c478bd9Sstevel@tonic-gate /* R_386_RELATIVE */	{FLG_RE_NOTREL, 4},
587c478bd9Sstevel@tonic-gate /* R_386_GOTOFF */	{FLG_RE_GOTREL, 4},
597c478bd9Sstevel@tonic-gate /* R_386_GOTPC */	{FLG_RE_PCREL | FLG_RE_GOTPC | FLG_RE_LOCLBND, 4},
607c478bd9Sstevel@tonic-gate /* R_386_32PLT */	{FLG_RE_PLTREL, 4},
617c478bd9Sstevel@tonic-gate /* R_386_TLS_GD_PLT */	{FLG_RE_TLSINS | FLG_RE_PLTREL |
627c478bd9Sstevel@tonic-gate 				FLG_RE_PCREL | FLG_RE_TLSGD, 4},
637c478bd9Sstevel@tonic-gate /* R_386_TLS_LDM_PLT */	{FLG_RE_TLSINS | FLG_RE_PLTREL |
647c478bd9Sstevel@tonic-gate 				FLG_RE_PCREL | FLG_RE_TLSLD, 4},
657c478bd9Sstevel@tonic-gate /* R_386_TLS_TPOFF */	{FLG_RE_NOTREL, 4},
667c478bd9Sstevel@tonic-gate /* R_386_TLS_IE */	{FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSIE, 4},
677c478bd9Sstevel@tonic-gate /* R_386_TLS_GOTIE */	{FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSIE, 4},
68*552ff457Srie /* R_386_TLS_LE */	{FLG_RE_TLSINS | FLG_RE_TLSLE, 4},
697c478bd9Sstevel@tonic-gate /* R_386_TLS_GD */	{FLG_RE_GOTADD | FLG_RE_TLSINS | FLG_RE_TLSGD, 4},
707c478bd9Sstevel@tonic-gate /* R_386_TLS_LDM */	{FLG_RE_GOTADD | FLG_RE_TLSLD | FLG_RE_TLSINS, 4},
71*552ff457Srie /* R_386_16 */		{FLG_RE_NOTREL, 2},
72*552ff457Srie /* R_386_PC16 */	{FLG_RE_PCREL, 2},
73*552ff457Srie /* R_386_8 */		{FLG_RE_NOTREL, 1},
74*552ff457Srie /* R_386_PC8 */		{FLG_RE_PCREL, 1},
757c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN24 */	{FLG_RE_NOTSUP, 0},
767c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN25 */	{FLG_RE_NOTSUP, 0},
777c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN26 */	{FLG_RE_NOTSUP, 0},
787c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN27 */	{FLG_RE_NOTSUP, 0},
797c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN28 */	{FLG_RE_NOTSUP, 0},
807c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN29 */	{FLG_RE_NOTSUP, 0},
817c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN30 */	{FLG_RE_NOTSUP, 0},
827c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN31 */	{FLG_RE_NOTSUP, 0},
83*552ff457Srie /* R_386_TLS_LDO_32 */	{FLG_RE_TLSINS | FLG_RE_TLSLD, 4},
847c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN33 */	{FLG_RE_NOTSUP, 0},
857c478bd9Sstevel@tonic-gate /* R_386_UNKNOWN34 */	{FLG_RE_NOTSUP, 0},
867c478bd9Sstevel@tonic-gate /* R_386_TLS_DTPMOD32 */ {FLG_RE_NOTREL, 4},
877c478bd9Sstevel@tonic-gate /* R_386_TLS_DTPOFF32 */ {FLG_RE_NOTREL, 4},
887c478bd9Sstevel@tonic-gate /* R_386_UNKONWN37 */	{FLG_RE_NOTSUP, 0}
897c478bd9Sstevel@tonic-gate };
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * Write a single relocated value to its reference location.
93*552ff457Srie  * We assume we wish to add the relocation amount, value, to the
947c478bd9Sstevel@tonic-gate  * value of the address already present at the offset.
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  * NAME			VALUE	FIELD		CALCULATION
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  * R_386_NONE		 0	none		none
997c478bd9Sstevel@tonic-gate  * R_386_32		 1	word32		S + A
1007c478bd9Sstevel@tonic-gate  * R_386_PC32		 2	word32		S + A - P
1017c478bd9Sstevel@tonic-gate  * R_386_GOT32		 3	word32		G + A - P
1027c478bd9Sstevel@tonic-gate  * R_386_PLT32		 4	word32		L + A - P
1037c478bd9Sstevel@tonic-gate  * R_386_COPY		 5	none		none
1047c478bd9Sstevel@tonic-gate  * R_386_GLOB_DAT	 6	word32		S
1057c478bd9Sstevel@tonic-gate  * R_386_JMP_SLOT	 7	word32		S
1067c478bd9Sstevel@tonic-gate  * R_386_RELATIVE	 8	word32		B + A
1077c478bd9Sstevel@tonic-gate  * R_386_GOTOFF		 9	word32		S + A - GOT
1087c478bd9Sstevel@tonic-gate  * R_386_GOTPC		10	word32		GOT + A - P
1097c478bd9Sstevel@tonic-gate  * R_386_32PLT		11	word32		L + A
1107c478bd9Sstevel@tonic-gate  * R_386_TLS_GD_PLT	12	word32		@tlsgdplt
1117c478bd9Sstevel@tonic-gate  * R_386_TLS_LDM_PLT	13	word32		@tlsldmplt
1127c478bd9Sstevel@tonic-gate  * R_386_TLS_TPOFF	14	word32		@ntpoff(S)
1137c478bd9Sstevel@tonic-gate  * R_386_TLS_IE		15	word32		@indntpoff(S)
1147c478bd9Sstevel@tonic-gate  * R_386_TLS_GD		18	word32		@tlsgd(S)
1157c478bd9Sstevel@tonic-gate  * R_386_TLS_LDM	19	word32		@tlsldm(S)
116*552ff457Srie  * R_386_16		20	word16		S + A
117*552ff457Srie  * R_386_PC16		21	word16		S + A - P
118*552ff457Srie  * R_386_8		22	word8		S + A
119*552ff457Srie  * R_386_PC8		23	word8		S + A - P
1207c478bd9Sstevel@tonic-gate  * R_386_TLS_LDO_32	32	word32		@dtpoff(S)
1217c478bd9Sstevel@tonic-gate  * R_386_TLS_DTPMOD32	35	word32		@dtpmod(S)
1227c478bd9Sstevel@tonic-gate  * R_386_TLS_DTPOFF32	36	word32		@dtpoff(S)
1237c478bd9Sstevel@tonic-gate  *
124*552ff457Srie  * Relocations 0-10 are from Figure 4-4: Relocation Types from the
1257c478bd9Sstevel@tonic-gate  * intel ABI.  Relocation 11 (R_386_32PLT) is from the C++ intel abi
1267c478bd9Sstevel@tonic-gate  * and is in the process of being registered with intel ABI (1/13/94).
1277c478bd9Sstevel@tonic-gate  *
1287c478bd9Sstevel@tonic-gate  * Relocations R_386_TLS_* are added to support Thread-Local storage
1297c478bd9Sstevel@tonic-gate  *	as recorded in PSARC/2001/509
1307c478bd9Sstevel@tonic-gate  *
1317c478bd9Sstevel@tonic-gate  * Relocation calculations:
1327c478bd9Sstevel@tonic-gate  *
1337c478bd9Sstevel@tonic-gate  * CALCULATION uses the following notation:
1347c478bd9Sstevel@tonic-gate  *	A	the addend used
1357c478bd9Sstevel@tonic-gate  *	B	the base address of the shared object in memory
1367c478bd9Sstevel@tonic-gate  *	G	the offset into the global offset table
1377c478bd9Sstevel@tonic-gate  *	GOT	the address of teh global offset table
1387c478bd9Sstevel@tonic-gate  *	L	the procedure linkage entry
1397c478bd9Sstevel@tonic-gate  *	P	the place of the storage unit being relocated
1407c478bd9Sstevel@tonic-gate  *	S	the value of the symbol
1417c478bd9Sstevel@tonic-gate  *
1427c478bd9Sstevel@tonic-gate  *	@dtlndx(x): Allocate two contiguous entries in the GOT table to hold
1437c478bd9Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
1447c478bd9Sstevel@tonic-gate  *	   instructions referencing this entry will be bound to the first
1457c478bd9Sstevel@tonic-gate  *	   of the two GOT entries.
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  *	@tmndx(x): Allocate two contiguous entries in the GOT table to hold
1487c478bd9Sstevel@tonic-gate  *	   a Tls_index structure (for passing to __tls_get_addr()). The
1497c478bd9Sstevel@tonic-gate  *	   ti_offset field of the Tls_index will be set to 0 (zero) and the
1507c478bd9Sstevel@tonic-gate  *	   ti_module will be filled in at run-time. The call to
1517c478bd9Sstevel@tonic-gate  *	   __tls_get_addr() will return the starting offset of the dynamic
1527c478bd9Sstevel@tonic-gate  *	   TLS block.
1537c478bd9Sstevel@tonic-gate  *
1547c478bd9Sstevel@tonic-gate  *	@dtpoff(x): calculate the tlsoffset relative to the TLS block.
1557c478bd9Sstevel@tonic-gate  *
1567c478bd9Sstevel@tonic-gate  *	@tpoff(x): calculate the tlsoffset relative to the TLS block.
1577c478bd9Sstevel@tonic-gate  *
1587c478bd9Sstevel@tonic-gate  *	@dtpmod(x): calculate the module id of the object containing symbol x.
1597c478bd9Sstevel@tonic-gate  *
1607c478bd9Sstevel@tonic-gate  * The calculations in the CALCULATION column are assumed to have
1617c478bd9Sstevel@tonic-gate  * been performed before calling this function except for the addition of
1627c478bd9Sstevel@tonic-gate  * the addresses in the instructions.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate int
165*552ff457Srie do_reloc(uchar_t rtype, uchar_t *off, Xword *value, const char *sym,
166*552ff457Srie     const char *file)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	const Rel_entry	*rep;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	rep = &reloc_table[rtype];
171*552ff457Srie 
172*552ff457Srie 	switch (rep->re_fsize) {
173*552ff457Srie 	case 1:
174*552ff457Srie 		/* LINTED */
175*552ff457Srie 		*((uchar_t *)off) += (uchar_t)(*value);
176*552ff457Srie 		break;
177*552ff457Srie 	case 2:
178*552ff457Srie 		/* LINTED */
179*552ff457Srie 		*((Half *)off) += (Half)(*value);
180*552ff457Srie 		break;
181*552ff457Srie 	case 4:
1827c478bd9Sstevel@tonic-gate 		/* LINTED */
1837c478bd9Sstevel@tonic-gate 		*((Xword *)off) += *value;
184*552ff457Srie 		break;
185*552ff457Srie 	default:
186*552ff457Srie 		/*
187*552ff457Srie 		 * To keep chkmsg() happy: MSG_INTL(MSG_REL_UNSUPSZ)
188*552ff457Srie 		 */
189*552ff457Srie 		REL_ERR_UNSUPSZ(file, sym, rtype, rep->re_fsize);
190*552ff457Srie 		return (0);
191*552ff457Srie 	}
1927c478bd9Sstevel@tonic-gate 	return (1);
1937c478bd9Sstevel@tonic-gate }
194