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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright (c) 1988 AT&T 240Sstevel@tonic-gate * All Rights Reserved 250Sstevel@tonic-gate * 260Sstevel@tonic-gate * 270Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 280Sstevel@tonic-gate * Use is subject to license terms. 290Sstevel@tonic-gate */ 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 310Sstevel@tonic-gate 320Sstevel@tonic-gate /* 330Sstevel@tonic-gate * x86 machine dependent and ELF file class dependent functions. 340Sstevel@tonic-gate * Contains routines for performing function binding and symbol relocations. 350Sstevel@tonic-gate */ 360Sstevel@tonic-gate #include "_synonyms.h" 370Sstevel@tonic-gate 380Sstevel@tonic-gate #include <stdio.h> 390Sstevel@tonic-gate #include <sys/elf.h> 400Sstevel@tonic-gate #include <sys/elf_386.h> 410Sstevel@tonic-gate #include <sys/mman.h> 420Sstevel@tonic-gate #include <dlfcn.h> 430Sstevel@tonic-gate #include <synch.h> 440Sstevel@tonic-gate #include <string.h> 450Sstevel@tonic-gate #include "_rtld.h" 460Sstevel@tonic-gate #include "_audit.h" 470Sstevel@tonic-gate #include "_elf.h" 480Sstevel@tonic-gate #include "msg.h" 490Sstevel@tonic-gate #include "debug.h" 500Sstevel@tonic-gate #include "reloc.h" 510Sstevel@tonic-gate #include "conv.h" 520Sstevel@tonic-gate 530Sstevel@tonic-gate 540Sstevel@tonic-gate extern void elf_rtbndr(Rt_map *, ulong_t, caddr_t); 550Sstevel@tonic-gate 560Sstevel@tonic-gate int 570Sstevel@tonic-gate elf_mach_flags_check(Rej_desc *rej, Ehdr *ehdr) 580Sstevel@tonic-gate { 590Sstevel@tonic-gate /* 600Sstevel@tonic-gate * Check machine type and flags. 610Sstevel@tonic-gate */ 620Sstevel@tonic-gate if (ehdr->e_flags != 0) { 630Sstevel@tonic-gate rej->rej_type = SGS_REJ_BADFLAG; 640Sstevel@tonic-gate rej->rej_info = (uint_t)ehdr->e_flags; 650Sstevel@tonic-gate return (0); 660Sstevel@tonic-gate } 670Sstevel@tonic-gate return (1); 680Sstevel@tonic-gate } 690Sstevel@tonic-gate 700Sstevel@tonic-gate void 710Sstevel@tonic-gate ldso_plt_init(Rt_map * lmp) 720Sstevel@tonic-gate { 730Sstevel@tonic-gate /* 740Sstevel@tonic-gate * There is no need to analyze ld.so because we don't map in any of 750Sstevel@tonic-gate * its dependencies. However we may map these dependencies in later 760Sstevel@tonic-gate * (as if ld.so had dlopened them), so initialize the plt and the 770Sstevel@tonic-gate * permission information. 780Sstevel@tonic-gate */ 790Sstevel@tonic-gate if (PLTGOT(lmp)) 800Sstevel@tonic-gate elf_plt_init((PLTGOT(lmp)), (caddr_t)lmp); 810Sstevel@tonic-gate } 820Sstevel@tonic-gate 830Sstevel@tonic-gate static const uchar_t dyn_plt_template[] = { 840Sstevel@tonic-gate /* 0x00 */ 0x55, /* pushl %ebp */ 850Sstevel@tonic-gate /* 0x01 */ 0x8b, 0xec, /* movl %esp, %ebp */ 860Sstevel@tonic-gate /* 0x03 */ 0x68, 0x00, 0x00, 0x00, 0x00, /* pushl trace_fields */ 870Sstevel@tonic-gate /* 0x08 */ 0xe9, 0xfc, 0xff, 0xff, 0xff, 0xff /* jmp elf_plt_trace */ 880Sstevel@tonic-gate }; 890Sstevel@tonic-gate int dyn_plt_ent_size = sizeof (dyn_plt_template); 900Sstevel@tonic-gate 910Sstevel@tonic-gate /* 920Sstevel@tonic-gate * the dynamic plt entry is: 930Sstevel@tonic-gate * 940Sstevel@tonic-gate * pushl %ebp 950Sstevel@tonic-gate * movl %esp, %ebp 960Sstevel@tonic-gate * pushl tfp 970Sstevel@tonic-gate * jmp elf_plt_trace 980Sstevel@tonic-gate * dyn_data: 990Sstevel@tonic-gate * .align 4 1000Sstevel@tonic-gate * uintptr_t reflmp 1010Sstevel@tonic-gate * uintptr_t deflmp 1020Sstevel@tonic-gate * uint_t symndx 1030Sstevel@tonic-gate * uint_t sb_flags 1040Sstevel@tonic-gate * Sym symdef 1050Sstevel@tonic-gate */ 1060Sstevel@tonic-gate static caddr_t 1070Sstevel@tonic-gate elf_plt_trace_write(uint_t roffset, Rt_map *rlmp, Rt_map *dlmp, Sym *sym, 1080Sstevel@tonic-gate uint_t symndx, uint_t pltndx, caddr_t to, uint_t sb_flags, int *fail) 1090Sstevel@tonic-gate { 1100Sstevel@tonic-gate extern int elf_plt_trace(); 1110Sstevel@tonic-gate ulong_t got_entry; 1120Sstevel@tonic-gate uchar_t *dyn_plt; 1130Sstevel@tonic-gate uintptr_t *dyndata; 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate /* 1160Sstevel@tonic-gate * We only need to add the glue code if there is an auditing 1170Sstevel@tonic-gate * library that is interested in this binding. 1180Sstevel@tonic-gate */ 1190Sstevel@tonic-gate dyn_plt = (uchar_t *)((uintptr_t)AUDINFO(rlmp)->ai_dynplts + 1200Sstevel@tonic-gate (pltndx * dyn_plt_ent_size)); 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate /* 1230Sstevel@tonic-gate * Have we initialized this dynamic plt entry yet? If we haven't do it 1240Sstevel@tonic-gate * now. Otherwise this function has been called before, but from a 1250Sstevel@tonic-gate * different plt (ie. from another shared object). In that case 1260Sstevel@tonic-gate * we just set the plt to point to the new dyn_plt. 1270Sstevel@tonic-gate */ 1280Sstevel@tonic-gate if (*dyn_plt == 0) { 1290Sstevel@tonic-gate Sym * symp; 1300Sstevel@tonic-gate Word symvalue; 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate (void) memcpy((void *)dyn_plt, dyn_plt_template, 1330Sstevel@tonic-gate sizeof (dyn_plt_template)); 1340Sstevel@tonic-gate dyndata = (uintptr_t *)((uintptr_t)dyn_plt + 1350Sstevel@tonic-gate ROUND(sizeof (dyn_plt_template), M_WORD_ALIGN)); 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate /* 1380Sstevel@tonic-gate * relocate: 1390Sstevel@tonic-gate * pushl dyn_data 1400Sstevel@tonic-gate */ 1410Sstevel@tonic-gate symvalue = (Word)dyndata; 1420Sstevel@tonic-gate if (do_reloc(R_386_32, &dyn_plt[4], &symvalue, 1430Sstevel@tonic-gate MSG_ORIG(MSG_SYM_LADYNDATA), 1440Sstevel@tonic-gate MSG_ORIG(MSG_SPECFIL_DYNPLT)) == 0) { 1450Sstevel@tonic-gate *fail = 1; 1460Sstevel@tonic-gate return (0); 1470Sstevel@tonic-gate } 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate /* 1500Sstevel@tonic-gate * jmps are relative, so I need to figure out the relative 1510Sstevel@tonic-gate * address to elf_plt_trace. 1520Sstevel@tonic-gate * 1530Sstevel@tonic-gate * relocating: 1540Sstevel@tonic-gate * jmp elf_plt_trace 1550Sstevel@tonic-gate */ 1560Sstevel@tonic-gate symvalue = (ulong_t)(elf_plt_trace) - (ulong_t)(dyn_plt + 9); 1570Sstevel@tonic-gate if (do_reloc(R_386_PC32, &dyn_plt[9], &symvalue, 1580Sstevel@tonic-gate MSG_ORIG(MSG_SYM_ELFPLTTRACE), 1590Sstevel@tonic-gate MSG_ORIG(MSG_SPECFIL_DYNPLT)) == 0) { 1600Sstevel@tonic-gate *fail = 1; 1610Sstevel@tonic-gate return (0); 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate *dyndata++ = (uintptr_t)rlmp; 1650Sstevel@tonic-gate *dyndata++ = (uintptr_t)dlmp; 1660Sstevel@tonic-gate *dyndata++ = (uint_t)symndx; 1670Sstevel@tonic-gate *dyndata++ = (uint_t)sb_flags; 1680Sstevel@tonic-gate symp = (Sym *)dyndata; 1690Sstevel@tonic-gate *symp = *sym; 1700Sstevel@tonic-gate symp->st_name += (Word)STRTAB(dlmp); 1710Sstevel@tonic-gate symp->st_value = (Addr)to; 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate got_entry = (ulong_t)roffset; 1750Sstevel@tonic-gate *(ulong_t *)got_entry = (ulong_t)dyn_plt; 1760Sstevel@tonic-gate return ((caddr_t)dyn_plt); 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* 1810Sstevel@tonic-gate * Function binding routine - invoked on the first call to a function through 1820Sstevel@tonic-gate * the procedure linkage table; 1830Sstevel@tonic-gate * passes first through an assembly language interface. 1840Sstevel@tonic-gate * 1850Sstevel@tonic-gate * Takes the offset into the relocation table of the associated 1860Sstevel@tonic-gate * relocation entry and the address of the link map (rt_private_map struct) 1870Sstevel@tonic-gate * for the entry. 1880Sstevel@tonic-gate * 1890Sstevel@tonic-gate * Returns the address of the function referenced after re-writing the PLT 1900Sstevel@tonic-gate * entry to invoke the function directly. 1910Sstevel@tonic-gate * 1920Sstevel@tonic-gate * On error, causes process to terminate with a signal. 1930Sstevel@tonic-gate */ 1940Sstevel@tonic-gate ulong_t 1950Sstevel@tonic-gate elf_bndr(Rt_map *lmp, ulong_t reloff, caddr_t from) 1960Sstevel@tonic-gate { 1970Sstevel@tonic-gate Rt_map *nlmp, * llmp; 1980Sstevel@tonic-gate ulong_t addr, symval, rsymndx; 1990Sstevel@tonic-gate char *name; 2000Sstevel@tonic-gate Rel *rptr; 2010Sstevel@tonic-gate Sym *sym, *nsym; 2020Sstevel@tonic-gate uint_t binfo, sb_flags = 0; 2030Sstevel@tonic-gate Slookup sl; 2040Sstevel@tonic-gate int entry, dbg_save, lmflags; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate /* 2070Sstevel@tonic-gate * For compatibility with libthread (TI_VERSION 1) we track the entry 2080Sstevel@tonic-gate * value. A zero value indicates we have recursed into ld.so.1 to 2090Sstevel@tonic-gate * further process a locking request. Under this recursion we disable 2100Sstevel@tonic-gate * tsort and cleanup activities. 2110Sstevel@tonic-gate */ 2120Sstevel@tonic-gate entry = enter(); 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate if ((lmflags = LIST(lmp)->lm_flags) & LML_FLG_RTLDLM) { 2150Sstevel@tonic-gate dbg_save = dbg_mask; 2160Sstevel@tonic-gate dbg_mask = 0; 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* 2200Sstevel@tonic-gate * Perform some basic sanity checks. If we didn't get a load map or 2210Sstevel@tonic-gate * the relocation offset is invalid then its possible someone has walked 2220Sstevel@tonic-gate * over the .got entries or jumped to plt0 out of the blue. 2230Sstevel@tonic-gate */ 2240Sstevel@tonic-gate if (!lmp || ((reloff % sizeof (Rel)) != 0)) { 2250Sstevel@tonic-gate eprintf(ERR_FATAL, MSG_INTL(MSG_REL_PLTREF), 2260Sstevel@tonic-gate conv_reloc_386_type_str(R_386_JMP_SLOT), 227238Sseizo EC_ADDR(lmp), EC_XWORD(reloff), EC_ADDR(from)); 2280Sstevel@tonic-gate rtldexit(LIST(lmp), 1); 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* 2320Sstevel@tonic-gate * Use relocation entry to get symbol table entry and symbol name. 2330Sstevel@tonic-gate */ 2340Sstevel@tonic-gate addr = (ulong_t)JMPREL(lmp); 2350Sstevel@tonic-gate rptr = (Rel *)(addr + reloff); 2360Sstevel@tonic-gate rsymndx = ELF_R_SYM(rptr->r_info); 2370Sstevel@tonic-gate sym = (Sym *)((ulong_t)SYMTAB(lmp) + (rsymndx * SYMENT(lmp))); 2380Sstevel@tonic-gate name = (char *)(STRTAB(lmp) + sym->st_name); 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate /* 2410Sstevel@tonic-gate * Determine the last link-map of this list, this'll be the starting 2420Sstevel@tonic-gate * point for any tsort() processing. 2430Sstevel@tonic-gate */ 2440Sstevel@tonic-gate llmp = LIST(lmp)->lm_tail; 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate /* 2470Sstevel@tonic-gate * Find definition for symbol. 2480Sstevel@tonic-gate */ 2490Sstevel@tonic-gate sl.sl_name = name; 2500Sstevel@tonic-gate sl.sl_cmap = lmp; 2510Sstevel@tonic-gate sl.sl_imap = LIST(lmp)->lm_head; 2520Sstevel@tonic-gate sl.sl_hash = 0; 2530Sstevel@tonic-gate sl.sl_rsymndx = rsymndx; 2540Sstevel@tonic-gate sl.sl_flags = LKUP_DEFT; 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate if ((nsym = lookup_sym(&sl, &nlmp, &binfo)) == 0) { 2570Sstevel@tonic-gate eprintf(ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp), 2580Sstevel@tonic-gate demangle(name)); 2590Sstevel@tonic-gate rtldexit(LIST(lmp), 1); 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate symval = nsym->st_value; 2630Sstevel@tonic-gate if (!(FLAGS(nlmp) & FLG_RT_FIXED) && 2640Sstevel@tonic-gate (nsym->st_shndx != SHN_ABS)) 2650Sstevel@tonic-gate symval += ADDR(nlmp); 2660Sstevel@tonic-gate if ((lmp != nlmp) && ((FLAGS1(nlmp) & FL1_RT_NOINIFIN) == 0)) { 2670Sstevel@tonic-gate /* 2680Sstevel@tonic-gate * Record that this new link map is now bound to the caller. 2690Sstevel@tonic-gate */ 2700Sstevel@tonic-gate if (bind_one(lmp, nlmp, BND_REFER) == 0) 2710Sstevel@tonic-gate rtldexit(LIST(lmp), 1); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate if ((LIST(lmp)->lm_tflags | FLAGS1(lmp)) & LML_TFLG_AUD_SYMBIND) { 2750Sstevel@tonic-gate uint_t symndx = (((uintptr_t)nsym - 2760Sstevel@tonic-gate (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp)); 2770Sstevel@tonic-gate symval = audit_symbind(lmp, nlmp, nsym, symndx, symval, 2780Sstevel@tonic-gate &sb_flags); 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate if (!(rtld_flags & RT_FL_NOBIND)) { 2820Sstevel@tonic-gate addr = rptr->r_offset; 2830Sstevel@tonic-gate if (!(FLAGS(lmp) & FLG_RT_FIXED)) 2840Sstevel@tonic-gate addr += ADDR(lmp); 2850Sstevel@tonic-gate if (((LIST(lmp)->lm_tflags | FLAGS1(lmp)) & 2860Sstevel@tonic-gate (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) && 2870Sstevel@tonic-gate AUDINFO(lmp)->ai_dynplts) { 2880Sstevel@tonic-gate int fail = 0; 2890Sstevel@tonic-gate uint_t pltndx = reloff / sizeof (Rel); 2900Sstevel@tonic-gate uint_t symndx = (((uintptr_t)nsym - 2910Sstevel@tonic-gate (uintptr_t)SYMTAB(nlmp)) / 2920Sstevel@tonic-gate SYMENT(nlmp)); 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate symval = (ulong_t)elf_plt_trace_write(addr, lmp, nlmp, 2950Sstevel@tonic-gate nsym, symndx, pltndx, (caddr_t)symval, sb_flags, 2960Sstevel@tonic-gate &fail); 2970Sstevel@tonic-gate if (fail) 2980Sstevel@tonic-gate rtldexit(LIST(lmp), 1); 2990Sstevel@tonic-gate } else { 3000Sstevel@tonic-gate /* 3010Sstevel@tonic-gate * Write standard PLT entry to jump directly 3020Sstevel@tonic-gate * to newly bound function. 3030Sstevel@tonic-gate */ 3040Sstevel@tonic-gate *(ulong_t *)addr = symval; 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate /* 3090Sstevel@tonic-gate * Print binding information and rebuild PLT entry. 3100Sstevel@tonic-gate */ 3110Sstevel@tonic-gate DBG_CALL(Dbg_bind_global(NAME(lmp), from, from - ADDR(lmp), 3120Sstevel@tonic-gate (Xword)(reloff / sizeof (Rel)), PLT_T_FULL, NAME(nlmp), 3130Sstevel@tonic-gate (caddr_t)symval, (caddr_t)nsym->st_value, name, binfo)); 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate /* 3160Sstevel@tonic-gate * Complete any processing for newly loaded objects. Note we don't 3170Sstevel@tonic-gate * know exactly where any new objects are loaded (we know the object 3180Sstevel@tonic-gate * that supplied the symbol, but others may have been loaded lazily as 3190Sstevel@tonic-gate * we searched for the symbol), so sorting starts from the last 3200Sstevel@tonic-gate * link-map know on entry to this routine. 3210Sstevel@tonic-gate */ 3220Sstevel@tonic-gate if (entry) 3230Sstevel@tonic-gate load_completion(llmp, lmp); 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate /* 3260Sstevel@tonic-gate * Some operations like dldump() or dlopen()'ing a relocatable object 3270Sstevel@tonic-gate * result in objects being loaded on rtld's link-map, make sure these 3280Sstevel@tonic-gate * objects are initialized also. 3290Sstevel@tonic-gate */ 3300Sstevel@tonic-gate if ((LIST(nlmp)->lm_flags & LML_FLG_RTLDLM) && LIST(nlmp)->lm_init) 3310Sstevel@tonic-gate load_completion(nlmp, 0); 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate /* 3340Sstevel@tonic-gate * If the object we've bound to is in the process of being initialized 3350Sstevel@tonic-gate * by another thread, determine whether we should block. 3360Sstevel@tonic-gate */ 3370Sstevel@tonic-gate is_dep_ready(nlmp, lmp, DBG_WAIT_SYMBOL); 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate /* 3400Sstevel@tonic-gate * Make sure the object to which we've bound has had it's .init fired. 3410Sstevel@tonic-gate * Cleanup before return to user code. 3420Sstevel@tonic-gate */ 3430Sstevel@tonic-gate if (entry) { 3440Sstevel@tonic-gate is_dep_init(nlmp, lmp); 3450Sstevel@tonic-gate leave(LIST(lmp)); 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate if (lmflags & LML_FLG_RTLDLM) 3490Sstevel@tonic-gate dbg_mask = dbg_save; 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate return (symval); 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate /* 3560Sstevel@tonic-gate * When the relocation loop realizes that it's dealing with relative 3570Sstevel@tonic-gate * relocations in a shared object, it breaks into this tighter loop 3580Sstevel@tonic-gate * as an optimization. 3590Sstevel@tonic-gate */ 3600Sstevel@tonic-gate ulong_t 3610Sstevel@tonic-gate elf_reloc_relative(ulong_t relbgn, ulong_t relend, ulong_t relsiz, 3620Sstevel@tonic-gate ulong_t basebgn, ulong_t etext, ulong_t emap) 3630Sstevel@tonic-gate { 3640Sstevel@tonic-gate ulong_t roffset = ((Rel *)relbgn)->r_offset; 3650Sstevel@tonic-gate char rtype; 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate do { 3680Sstevel@tonic-gate roffset += basebgn; 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate /* 3710Sstevel@tonic-gate * If this relocation is against an address not mapped in, 3720Sstevel@tonic-gate * then break out of the relative relocation loop, falling 3730Sstevel@tonic-gate * back on the main relocation loop. 3740Sstevel@tonic-gate */ 3750Sstevel@tonic-gate if (roffset < etext || roffset > emap) 3760Sstevel@tonic-gate break; 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate /* 3790Sstevel@tonic-gate * Perform the actual relocation. 3800Sstevel@tonic-gate */ 3810Sstevel@tonic-gate *((ulong_t *)roffset) += basebgn; 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate relbgn += relsiz; 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate if (relbgn >= relend) 3860Sstevel@tonic-gate break; 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info); 3890Sstevel@tonic-gate roffset = ((Rel *)relbgn)->r_offset; 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate } while (rtype == R_386_RELATIVE); 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate return (relbgn); 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate /* 3970Sstevel@tonic-gate * This is the tightest loop for RELATIVE relocations for those 3980Sstevel@tonic-gate * objects built with the DT_RELACOUNT .dynamic entry. 3990Sstevel@tonic-gate */ 4000Sstevel@tonic-gate ulong_t 4010Sstevel@tonic-gate elf_reloc_relacount(ulong_t relbgn, ulong_t relacount, ulong_t relsiz, 4020Sstevel@tonic-gate ulong_t basebgn) 4030Sstevel@tonic-gate { 4040Sstevel@tonic-gate ulong_t roffset = ((Rel *) relbgn)->r_offset; 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate for (; relacount; relacount--) { 4070Sstevel@tonic-gate roffset += basebgn; 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate /* 4100Sstevel@tonic-gate * Perform the actual relocation. 4110Sstevel@tonic-gate */ 4120Sstevel@tonic-gate *((ulong_t *)roffset) += basebgn; 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate relbgn += relsiz; 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate roffset = ((Rel *)relbgn)->r_offset; 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate return (relbgn); 4210Sstevel@tonic-gate } 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate /* 4240Sstevel@tonic-gate * Read and process the relocations for one link object, we assume all 4250Sstevel@tonic-gate * relocation sections for loadable segments are stored contiguously in 4260Sstevel@tonic-gate * the file. 4270Sstevel@tonic-gate */ 4280Sstevel@tonic-gate int 4290Sstevel@tonic-gate elf_reloc(Rt_map *lmp, uint_t plt) 4300Sstevel@tonic-gate { 4310Sstevel@tonic-gate ulong_t relbgn, relend, relsiz, basebgn; 4320Sstevel@tonic-gate ulong_t pltbgn, pltend, _pltbgn, _pltend; 4330Sstevel@tonic-gate ulong_t roffset, rsymndx, psymndx = 0, etext = ETEXT(lmp); 4340Sstevel@tonic-gate ulong_t emap, dsymndx; 4350Sstevel@tonic-gate uchar_t rtype; 4360Sstevel@tonic-gate long value, pvalue; 4370Sstevel@tonic-gate Sym *symref, *psymref, *symdef, *psymdef; 4380Sstevel@tonic-gate char *name, *pname; 4390Sstevel@tonic-gate Rt_map *_lmp, *plmp; 4400Sstevel@tonic-gate int textrel = 0, ret = 1, noplt = 0; 4410Sstevel@tonic-gate int relacount = RELACOUNT(lmp), plthint = 0; 4420Sstevel@tonic-gate Rel *rel; 4430Sstevel@tonic-gate uint_t binfo, pbinfo; 4440Sstevel@tonic-gate Alist *bound = 0; 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate /* 4470Sstevel@tonic-gate * Although only necessary for lazy binding, initialize the first 4480Sstevel@tonic-gate * global offset entry to go to elf_rtbndr(). dbx(1) seems 4490Sstevel@tonic-gate * to find this useful. 4500Sstevel@tonic-gate */ 4510Sstevel@tonic-gate if ((plt == 0) && PLTGOT(lmp)) { 4520Sstevel@tonic-gate if ((ulong_t)PLTGOT(lmp) < etext) { 4530Sstevel@tonic-gate if (elf_set_prot(lmp, PROT_WRITE) == 0) 4540Sstevel@tonic-gate return (0); 4550Sstevel@tonic-gate textrel = 1; 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate elf_plt_init(PLTGOT(lmp), (caddr_t)lmp); 4580Sstevel@tonic-gate } 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate /* 4610Sstevel@tonic-gate * Initialize the plt start and end addresses. 4620Sstevel@tonic-gate */ 4630Sstevel@tonic-gate if ((pltbgn = (ulong_t)JMPREL(lmp)) != 0) 4640Sstevel@tonic-gate pltend = pltbgn + (ulong_t)(PLTRELSZ(lmp)); 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate relsiz = (ulong_t)(RELENT(lmp)); 4680Sstevel@tonic-gate basebgn = ADDR(lmp); 4690Sstevel@tonic-gate emap = ADDR(lmp) + MSIZE(lmp); 4700Sstevel@tonic-gate 4710Sstevel@tonic-gate if (PLTRELSZ(lmp)) 4720Sstevel@tonic-gate plthint = PLTRELSZ(lmp) / relsiz; 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate /* 4750Sstevel@tonic-gate * If we've been called upon to promote an RTLD_LAZY object to an 4760Sstevel@tonic-gate * RTLD_NOW then we're only interested in scaning the .plt table. 4770Sstevel@tonic-gate * An uninitialized .plt is the case where the associated got entry 4780Sstevel@tonic-gate * points back to the plt itself. Determine the range of the real .plt 4790Sstevel@tonic-gate * entries using the _PROCEDURE_LINKAGE_TABLE_ symbol. 4800Sstevel@tonic-gate */ 4810Sstevel@tonic-gate if (plt) { 4820Sstevel@tonic-gate Slookup sl; 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate relbgn = pltbgn; 4850Sstevel@tonic-gate relend = pltend; 4860Sstevel@tonic-gate if (!relbgn || (relbgn == relend)) 4870Sstevel@tonic-gate return (1); 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate sl.sl_name = MSG_ORIG(MSG_SYM_PLT); 4900Sstevel@tonic-gate sl.sl_cmap = lmp; 4910Sstevel@tonic-gate sl.sl_imap = lmp; 492*546Srie sl.sl_hash = elf_hash(MSG_ORIG(MSG_SYM_PLT)); 4930Sstevel@tonic-gate sl.sl_rsymndx = 0; 4940Sstevel@tonic-gate sl.sl_flags = LKUP_DEFT; 4950Sstevel@tonic-gate 4960Sstevel@tonic-gate if ((symdef = elf_find_sym(&sl, &_lmp, &binfo)) == 0) 4970Sstevel@tonic-gate return (1); 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate _pltbgn = symdef->st_value; 5000Sstevel@tonic-gate if (!(FLAGS(lmp) & FLG_RT_FIXED) && 5010Sstevel@tonic-gate (symdef->st_shndx != SHN_ABS)) 5020Sstevel@tonic-gate _pltbgn += basebgn; 5030Sstevel@tonic-gate _pltend = _pltbgn + (((PLTRELSZ(lmp) / relsiz)) * 5040Sstevel@tonic-gate M_PLT_ENTSIZE) + M_PLT_RESERVSZ; 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate } else { 5070Sstevel@tonic-gate /* 5080Sstevel@tonic-gate * The relocation sections appear to the run-time linker as a 5090Sstevel@tonic-gate * single table. Determine the address of the beginning and end 5100Sstevel@tonic-gate * of this table. There are two different interpretations of 5110Sstevel@tonic-gate * the ABI at this point: 5120Sstevel@tonic-gate * 5130Sstevel@tonic-gate * o The REL table and its associated RELSZ indicate the 5140Sstevel@tonic-gate * concatenation of *all* relocation sections (this is the 5150Sstevel@tonic-gate * model our link-editor constructs). 5160Sstevel@tonic-gate * 5170Sstevel@tonic-gate * o The REL table and its associated RELSZ indicate the 5180Sstevel@tonic-gate * concatenation of all *but* the .plt relocations. These 5190Sstevel@tonic-gate * relocations are specified individually by the JMPREL and 5200Sstevel@tonic-gate * PLTRELSZ entries. 5210Sstevel@tonic-gate * 5220Sstevel@tonic-gate * Determine from our knowledege of the relocation range and 5230Sstevel@tonic-gate * .plt range, the range of the total relocation table. Note 5240Sstevel@tonic-gate * that one other ABI assumption seems to be that the .plt 5250Sstevel@tonic-gate * relocations always follow any other relocations, the 5260Sstevel@tonic-gate * following range checking drops that assumption. 5270Sstevel@tonic-gate */ 5280Sstevel@tonic-gate relbgn = (ulong_t)(REL(lmp)); 5290Sstevel@tonic-gate relend = relbgn + (ulong_t)(RELSZ(lmp)); 5300Sstevel@tonic-gate if (pltbgn) { 5310Sstevel@tonic-gate if (!relbgn || (relbgn > pltbgn)) 5320Sstevel@tonic-gate relbgn = pltbgn; 5330Sstevel@tonic-gate if (!relbgn || (relend < pltend)) 5340Sstevel@tonic-gate relend = pltend; 5350Sstevel@tonic-gate } 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate if (!relbgn || (relbgn == relend)) { 5380Sstevel@tonic-gate DBG_CALL(Dbg_reloc_run(NAME(lmp), 0, plt, DBG_REL_NONE)); 5390Sstevel@tonic-gate return (1); 5400Sstevel@tonic-gate } 5410Sstevel@tonic-gate DBG_CALL(Dbg_reloc_run(NAME(lmp), M_REL_SHT_TYPE, plt, DBG_REL_START)); 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate /* 5440Sstevel@tonic-gate * If we're processing a dynamic executable in lazy mode there is no 5450Sstevel@tonic-gate * need to scan the .rel.plt table, however if we're processing a shared 5460Sstevel@tonic-gate * object in lazy mode the .got addresses associated to each .plt must 5470Sstevel@tonic-gate * be relocated to reflect the location of the shared object. 5480Sstevel@tonic-gate */ 5490Sstevel@tonic-gate if (pltbgn && ((MODE(lmp) & RTLD_NOW) == 0) && 5500Sstevel@tonic-gate (FLAGS(lmp) & FLG_RT_FIXED)) 5510Sstevel@tonic-gate noplt = 1; 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate /* 5540Sstevel@tonic-gate * Loop through relocations. 5550Sstevel@tonic-gate */ 5560Sstevel@tonic-gate while (relbgn < relend) { 5570Sstevel@tonic-gate uint_t sb_flags = 0; 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info); 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate /* 5620Sstevel@tonic-gate * If this is a RELATIVE relocation in a shared object (the 5630Sstevel@tonic-gate * common case), and if we are not debugging, then jump into a 5640Sstevel@tonic-gate * tighter relocation loop (elf_reloc_relative). Only make the 5650Sstevel@tonic-gate * jump if we've been given a hint on the number of relocations. 5660Sstevel@tonic-gate */ 5670Sstevel@tonic-gate if ((rtype == R_386_RELATIVE) && 5680Sstevel@tonic-gate !(FLAGS(lmp) & FLG_RT_FIXED) && !dbg_mask) { 5690Sstevel@tonic-gate /* 5700Sstevel@tonic-gate * It's possible that the relative relocation block 5710Sstevel@tonic-gate * has relocations against the text segment as well 5720Sstevel@tonic-gate * as the data segment. Since our optimized relocation 5730Sstevel@tonic-gate * engine does not check which segment the relocation 5740Sstevel@tonic-gate * is against - just mprotect it now if it's been 5750Sstevel@tonic-gate * marked as containing TEXTREL's. 5760Sstevel@tonic-gate */ 5770Sstevel@tonic-gate if ((textrel == 0) && (FLAGS1(lmp) & FL1_RT_TEXTREL)) { 5780Sstevel@tonic-gate if (elf_set_prot(lmp, PROT_WRITE) == 0) { 5790Sstevel@tonic-gate ret = 0; 5800Sstevel@tonic-gate break; 5810Sstevel@tonic-gate } 5820Sstevel@tonic-gate textrel = 1; 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate if (relacount) { 5850Sstevel@tonic-gate relbgn = elf_reloc_relacount(relbgn, relacount, 5860Sstevel@tonic-gate relsiz, basebgn); 5870Sstevel@tonic-gate relacount = 0; 5880Sstevel@tonic-gate } else { 5890Sstevel@tonic-gate relbgn = elf_reloc_relative(relbgn, relend, 5900Sstevel@tonic-gate relsiz, basebgn, etext, emap); 5910Sstevel@tonic-gate } 5920Sstevel@tonic-gate if (relbgn >= relend) 5930Sstevel@tonic-gate break; 5940Sstevel@tonic-gate rtype = ELF_R_TYPE(((Rel *)relbgn)->r_info); 5950Sstevel@tonic-gate } 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate roffset = ((Rel *)relbgn)->r_offset; 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate /* 6000Sstevel@tonic-gate * If this is a shared object, add the base address to offset. 6010Sstevel@tonic-gate */ 6020Sstevel@tonic-gate if (!(FLAGS(lmp) & FLG_RT_FIXED)) { 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate /* 6050Sstevel@tonic-gate * If we're processing lazy bindings, we have to step 6060Sstevel@tonic-gate * through the plt entries and add the base address 6070Sstevel@tonic-gate * to the corresponding got entry. 6080Sstevel@tonic-gate */ 6090Sstevel@tonic-gate if (plthint && (plt == 0) && 6100Sstevel@tonic-gate (rtype == R_386_JMP_SLOT) && 6110Sstevel@tonic-gate ((MODE(lmp) & RTLD_NOW) == 0)) { 6120Sstevel@tonic-gate relbgn = elf_reloc_relacount(relbgn, 6130Sstevel@tonic-gate plthint, relsiz, basebgn); 6140Sstevel@tonic-gate plthint = 0; 6150Sstevel@tonic-gate continue; 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate roffset += basebgn; 6180Sstevel@tonic-gate } 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate rsymndx = ELF_R_SYM(((Rel *)relbgn)->r_info); 6210Sstevel@tonic-gate rel = (Rel *)relbgn; 6220Sstevel@tonic-gate relbgn += relsiz; 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate /* 6250Sstevel@tonic-gate * Optimizations. 6260Sstevel@tonic-gate */ 6270Sstevel@tonic-gate if (rtype == R_386_NONE) 6280Sstevel@tonic-gate continue; 6290Sstevel@tonic-gate if (noplt && ((ulong_t)rel >= pltbgn) && 6300Sstevel@tonic-gate ((ulong_t)rel < pltend)) { 6310Sstevel@tonic-gate relbgn = pltend; 6320Sstevel@tonic-gate continue; 6330Sstevel@tonic-gate } 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate /* 6360Sstevel@tonic-gate * If we're promoting plts determine if this one has already 6370Sstevel@tonic-gate * been written. 6380Sstevel@tonic-gate */ 6390Sstevel@tonic-gate if (plt) { 6400Sstevel@tonic-gate if ((*(ulong_t *)roffset < _pltbgn) || 6410Sstevel@tonic-gate (*(ulong_t *)roffset > _pltend)) 6420Sstevel@tonic-gate continue; 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate /* 6460Sstevel@tonic-gate * If this relocation is not against part of the image 6470Sstevel@tonic-gate * mapped into memory we skip it. 6480Sstevel@tonic-gate */ 6490Sstevel@tonic-gate if ((roffset < ADDR(lmp)) || (roffset > (ADDR(lmp) + 6500Sstevel@tonic-gate MSIZE(lmp)))) { 6510Sstevel@tonic-gate elf_reloc_bad(lmp, (void *)rel, 6520Sstevel@tonic-gate rtype, roffset, rsymndx); 6530Sstevel@tonic-gate continue; 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate binfo = 0; 6570Sstevel@tonic-gate /* 6580Sstevel@tonic-gate * If a symbol index is specified then get the symbol table 6590Sstevel@tonic-gate * entry, locate the symbol definition, and determine its 6600Sstevel@tonic-gate * address. 6610Sstevel@tonic-gate */ 6620Sstevel@tonic-gate if (rsymndx) { 6630Sstevel@tonic-gate /* 6640Sstevel@tonic-gate * Get the local symbol table entry. 6650Sstevel@tonic-gate */ 6660Sstevel@tonic-gate symref = (Sym *)((ulong_t)SYMTAB(lmp) + 6670Sstevel@tonic-gate (rsymndx * SYMENT(lmp))); 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate /* 6700Sstevel@tonic-gate * If this is a local symbol, just use the base address. 6710Sstevel@tonic-gate * (we should have no local relocations in the 6720Sstevel@tonic-gate * executable). 6730Sstevel@tonic-gate */ 6740Sstevel@tonic-gate if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) { 6750Sstevel@tonic-gate value = basebgn; 6760Sstevel@tonic-gate name = (char *)0; 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate /* 6790Sstevel@tonic-gate * TLS relocation - value for DTPMOD32 6800Sstevel@tonic-gate * relocation is the TLS modid. 6810Sstevel@tonic-gate */ 6820Sstevel@tonic-gate if (rtype == R_386_TLS_DTPMOD32) 6830Sstevel@tonic-gate value = TLSMODID(lmp); 6840Sstevel@tonic-gate } else { 6850Sstevel@tonic-gate /* 6860Sstevel@tonic-gate * If the symbol index is equal to the previous 6870Sstevel@tonic-gate * symbol index relocation we processed then 6880Sstevel@tonic-gate * reuse the previous values. (Note that there 6890Sstevel@tonic-gate * have been cases where a relocation exists 6900Sstevel@tonic-gate * against a copy relocation symbol, our ld(1) 6910Sstevel@tonic-gate * should optimize this away, but make sure we 6920Sstevel@tonic-gate * don't use the same symbol information should 6930Sstevel@tonic-gate * this case exist). 6940Sstevel@tonic-gate */ 6950Sstevel@tonic-gate if ((rsymndx == psymndx) && 6960Sstevel@tonic-gate (rtype != R_386_COPY)) { 6970Sstevel@tonic-gate /* LINTED */ 6980Sstevel@tonic-gate if (psymdef == 0) { 6990Sstevel@tonic-gate DBG_CALL(Dbg_bind_weak( 7000Sstevel@tonic-gate NAME(lmp), (caddr_t)roffset, 7010Sstevel@tonic-gate (caddr_t) 7020Sstevel@tonic-gate (roffset - basebgn), name)); 7030Sstevel@tonic-gate continue; 7040Sstevel@tonic-gate } 7050Sstevel@tonic-gate /* LINTED */ 7060Sstevel@tonic-gate value = pvalue; 7070Sstevel@tonic-gate /* LINTED */ 7080Sstevel@tonic-gate name = pname; 7090Sstevel@tonic-gate /* LINTED */ 7100Sstevel@tonic-gate symdef = psymdef; 7110Sstevel@tonic-gate /* LINTED */ 7120Sstevel@tonic-gate symref = psymref; 7130Sstevel@tonic-gate /* LINTED */ 7140Sstevel@tonic-gate _lmp = plmp; 7150Sstevel@tonic-gate /* LINTED */ 7160Sstevel@tonic-gate binfo = pbinfo; 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate if ((LIST(_lmp)->lm_tflags | 7190Sstevel@tonic-gate FLAGS1(_lmp)) & 7200Sstevel@tonic-gate LML_TFLG_AUD_SYMBIND) { 7210Sstevel@tonic-gate value = audit_symbind(lmp, _lmp, 7220Sstevel@tonic-gate /* LINTED */ 7230Sstevel@tonic-gate symdef, dsymndx, value, 7240Sstevel@tonic-gate &sb_flags); 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate } else { 7270Sstevel@tonic-gate Slookup sl; 7280Sstevel@tonic-gate uchar_t bind; 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate /* 7310Sstevel@tonic-gate * Lookup the symbol definition. 7320Sstevel@tonic-gate */ 7330Sstevel@tonic-gate name = (char *)(STRTAB(lmp) + 7340Sstevel@tonic-gate symref->st_name); 7350Sstevel@tonic-gate 7360Sstevel@tonic-gate sl.sl_name = name; 7370Sstevel@tonic-gate sl.sl_cmap = lmp; 7380Sstevel@tonic-gate sl.sl_imap = 0; 7390Sstevel@tonic-gate sl.sl_hash = 0; 7400Sstevel@tonic-gate sl.sl_rsymndx = rsymndx; 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate if (rtype == R_386_COPY) 7430Sstevel@tonic-gate sl.sl_flags = LKUP_COPY; 7440Sstevel@tonic-gate else 7450Sstevel@tonic-gate sl.sl_flags = LKUP_DEFT; 7460Sstevel@tonic-gate 7470Sstevel@tonic-gate sl.sl_flags |= LKUP_ALLCNTLIST; 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate if (rtype != R_386_JMP_SLOT) 7500Sstevel@tonic-gate sl.sl_flags |= LKUP_SPEC; 7510Sstevel@tonic-gate 7520Sstevel@tonic-gate bind = ELF_ST_BIND(symref->st_info); 7530Sstevel@tonic-gate if (bind == STB_WEAK) 7540Sstevel@tonic-gate sl.sl_flags |= LKUP_WEAK; 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate symdef = lookup_sym(&sl, &_lmp, &binfo); 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate /* 7590Sstevel@tonic-gate * If the symbol is not found and the 7600Sstevel@tonic-gate * reference was not to a weak symbol, 7610Sstevel@tonic-gate * report an error. Weak references 7620Sstevel@tonic-gate * may be unresolved. 7630Sstevel@tonic-gate * chkmsg: MSG_INTL(MSG_LDD_SYM_NFOUND) 7640Sstevel@tonic-gate */ 7650Sstevel@tonic-gate if (symdef == 0) { 7660Sstevel@tonic-gate if (bind != STB_WEAK) { 7670Sstevel@tonic-gate if (LIST(lmp)->lm_flags & 7680Sstevel@tonic-gate LML_FLG_IGNRELERR) { 7690Sstevel@tonic-gate continue; 7700Sstevel@tonic-gate } else if (LIST(lmp)->lm_flags & 7710Sstevel@tonic-gate LML_FLG_TRC_WARN) { 7720Sstevel@tonic-gate (void) printf(MSG_INTL( 7730Sstevel@tonic-gate MSG_LDD_SYM_NFOUND), 7740Sstevel@tonic-gate demangle(name), 7750Sstevel@tonic-gate NAME(lmp)); 7760Sstevel@tonic-gate continue; 7770Sstevel@tonic-gate } else { 7780Sstevel@tonic-gate eprintf(ERR_FATAL, 7790Sstevel@tonic-gate MSG_INTL(MSG_REL_NOSYM), 7800Sstevel@tonic-gate NAME(lmp), 7810Sstevel@tonic-gate demangle(name)); 7820Sstevel@tonic-gate ret = 0; 7830Sstevel@tonic-gate break; 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate } else { 7860Sstevel@tonic-gate psymndx = rsymndx; 7870Sstevel@tonic-gate psymdef = 0; 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate DBG_CALL(Dbg_bind_weak( 7900Sstevel@tonic-gate NAME(lmp), (caddr_t)roffset, 7910Sstevel@tonic-gate (caddr_t) 7920Sstevel@tonic-gate (roffset - basebgn), name)); 7930Sstevel@tonic-gate continue; 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate } 7960Sstevel@tonic-gate 7970Sstevel@tonic-gate /* 7980Sstevel@tonic-gate * If symbol was found in an object 7990Sstevel@tonic-gate * other than the referencing object 8000Sstevel@tonic-gate * then record the binding. 8010Sstevel@tonic-gate */ 8020Sstevel@tonic-gate if ((lmp != _lmp) && ((FLAGS1(_lmp) & 8030Sstevel@tonic-gate FL1_RT_NOINIFIN) == 0)) { 8040Sstevel@tonic-gate if (alist_test(&bound, _lmp, 8050Sstevel@tonic-gate sizeof (Rt_map *), 8060Sstevel@tonic-gate AL_CNT_RELBIND) == 0) { 8070Sstevel@tonic-gate ret = 0; 8080Sstevel@tonic-gate break; 8090Sstevel@tonic-gate } 8100Sstevel@tonic-gate } 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate /* 8130Sstevel@tonic-gate * Calculate the location of definition; 8140Sstevel@tonic-gate * symbol value plus base address of 8150Sstevel@tonic-gate * containing shared object. 8160Sstevel@tonic-gate */ 8170Sstevel@tonic-gate value = symdef->st_value; 8180Sstevel@tonic-gate if (!(FLAGS(_lmp) & FLG_RT_FIXED) && 8190Sstevel@tonic-gate (symdef->st_shndx != SHN_ABS) && 8200Sstevel@tonic-gate (ELF_ST_TYPE(symdef->st_info) != 8210Sstevel@tonic-gate STT_TLS)) 8220Sstevel@tonic-gate value += ADDR(_lmp); 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate /* 8250Sstevel@tonic-gate * Retain this symbol index and the 8260Sstevel@tonic-gate * value in case it can be used for the 8270Sstevel@tonic-gate * subsequent relocations. 8280Sstevel@tonic-gate */ 8290Sstevel@tonic-gate if (rtype != R_386_COPY) { 8300Sstevel@tonic-gate psymndx = rsymndx; 8310Sstevel@tonic-gate pvalue = value; 8320Sstevel@tonic-gate pname = name; 8330Sstevel@tonic-gate psymdef = symdef; 8340Sstevel@tonic-gate psymref = symref; 8350Sstevel@tonic-gate plmp = _lmp; 8360Sstevel@tonic-gate pbinfo = binfo; 8370Sstevel@tonic-gate } 8380Sstevel@tonic-gate if ((LIST(_lmp)->lm_tflags | 8390Sstevel@tonic-gate FLAGS1(_lmp)) & 8400Sstevel@tonic-gate LML_TFLG_AUD_SYMBIND) { 8410Sstevel@tonic-gate dsymndx = (((uintptr_t)symdef - 8420Sstevel@tonic-gate (uintptr_t)SYMTAB(_lmp)) / 8430Sstevel@tonic-gate SYMENT(_lmp)); 8440Sstevel@tonic-gate value = audit_symbind(lmp, _lmp, 8450Sstevel@tonic-gate symdef, dsymndx, value, 8460Sstevel@tonic-gate &sb_flags); 8470Sstevel@tonic-gate } 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate /* 8510Sstevel@tonic-gate * If relocation is PC-relative, subtract 8520Sstevel@tonic-gate * offset address. 8530Sstevel@tonic-gate */ 8540Sstevel@tonic-gate if (IS_PC_RELATIVE(rtype)) 8550Sstevel@tonic-gate value -= roffset; 8560Sstevel@tonic-gate 8570Sstevel@tonic-gate /* 8580Sstevel@tonic-gate * TLS relocation - value for DTPMOD32 8590Sstevel@tonic-gate * relocation is the TLS modid. 8600Sstevel@tonic-gate */ 8610Sstevel@tonic-gate if (rtype == R_386_TLS_DTPMOD32) 8620Sstevel@tonic-gate value = TLSMODID(_lmp); 8630Sstevel@tonic-gate else if (rtype == R_386_TLS_TPOFF) 8640Sstevel@tonic-gate value = -(TLSSTATOFF(_lmp) - value); 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate } else { 8670Sstevel@tonic-gate /* 8680Sstevel@tonic-gate * Special case: 8690Sstevel@tonic-gate * 8700Sstevel@tonic-gate * A DTPMOD32 relocation is a local binding to a TLS 8710Sstevel@tonic-gate * symbol. Fill in the TLSMODID for the current object. 8720Sstevel@tonic-gate */ 8730Sstevel@tonic-gate if (rtype == R_386_TLS_DTPMOD32) 8740Sstevel@tonic-gate value = TLSMODID(lmp); 8750Sstevel@tonic-gate else 8760Sstevel@tonic-gate value = basebgn; 8770Sstevel@tonic-gate name = (char *)0; 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate 8800Sstevel@tonic-gate /* 8810Sstevel@tonic-gate * If this object has relocations in the text segment, turn 8820Sstevel@tonic-gate * off the write protect. 8830Sstevel@tonic-gate */ 8840Sstevel@tonic-gate if ((roffset < etext) && (textrel == 0)) { 8850Sstevel@tonic-gate if (elf_set_prot(lmp, PROT_WRITE) == 0) { 8860Sstevel@tonic-gate ret = 0; 8870Sstevel@tonic-gate break; 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate textrel = 1; 8900Sstevel@tonic-gate } 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate /* 8930Sstevel@tonic-gate * Call relocation routine to perform required relocation. 8940Sstevel@tonic-gate */ 8950Sstevel@tonic-gate DBG_CALL(Dbg_reloc_in(M_MACH, M_REL_SHT_TYPE, rel, name, NULL)); 8960Sstevel@tonic-gate 8970Sstevel@tonic-gate switch (rtype) { 8980Sstevel@tonic-gate case R_386_COPY: 8990Sstevel@tonic-gate if (elf_copy_reloc(name, symref, lmp, (void *)roffset, 9000Sstevel@tonic-gate symdef, _lmp, (const void *)value) == 0) 9010Sstevel@tonic-gate ret = 0; 9020Sstevel@tonic-gate break; 9030Sstevel@tonic-gate case R_386_JMP_SLOT: 9040Sstevel@tonic-gate if (((LIST(lmp)->lm_tflags | FLAGS1(lmp)) & 9050Sstevel@tonic-gate (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) && 9060Sstevel@tonic-gate AUDINFO(lmp)->ai_dynplts) { 9070Sstevel@tonic-gate int fail = 0; 9080Sstevel@tonic-gate int pltndx = (((ulong_t)rel - 9090Sstevel@tonic-gate (uintptr_t)JMPREL(lmp)) / relsiz); 9100Sstevel@tonic-gate int symndx = (((uintptr_t)symdef - 9110Sstevel@tonic-gate (uintptr_t)SYMTAB(_lmp)) / 9120Sstevel@tonic-gate SYMENT(_lmp)); 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate (void) elf_plt_trace_write(roffset, lmp, _lmp, 9150Sstevel@tonic-gate symdef, symndx, pltndx, (caddr_t)value, 9160Sstevel@tonic-gate sb_flags, &fail); 9170Sstevel@tonic-gate if (fail) 9180Sstevel@tonic-gate ret = 0; 9190Sstevel@tonic-gate } else { 9200Sstevel@tonic-gate /* 9210Sstevel@tonic-gate * Write standard PLT entry to jump directly 9220Sstevel@tonic-gate * to newly bound function. 9230Sstevel@tonic-gate */ 9240Sstevel@tonic-gate DBG_CALL(Dbg_reloc_apply((Xword)roffset, 9250Sstevel@tonic-gate (Xword)value)); 9260Sstevel@tonic-gate *(ulong_t *)roffset = value; 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate break; 9290Sstevel@tonic-gate default: 9300Sstevel@tonic-gate /* 9310Sstevel@tonic-gate * Write the relocation out. 9320Sstevel@tonic-gate */ 9330Sstevel@tonic-gate if (do_reloc(rtype, (uchar_t *)roffset, 9340Sstevel@tonic-gate (Word *)&value, name, NAME(lmp)) == 0) 9350Sstevel@tonic-gate ret = 0; 9360Sstevel@tonic-gate 9370Sstevel@tonic-gate DBG_CALL(Dbg_reloc_apply((Xword)roffset, 9380Sstevel@tonic-gate (Xword)value)); 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate if ((ret == 0) && 9420Sstevel@tonic-gate ((LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) == 0)) 9430Sstevel@tonic-gate break; 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate if (binfo) { 9460Sstevel@tonic-gate DBG_CALL(Dbg_bind_global(NAME(lmp), (caddr_t)roffset, 9470Sstevel@tonic-gate (caddr_t)(roffset - basebgn), (Xword)(-1), 9480Sstevel@tonic-gate PLT_T_FULL, NAME(_lmp), (caddr_t)value, 9490Sstevel@tonic-gate (caddr_t)symdef->st_value, name, binfo)); 9500Sstevel@tonic-gate } 9510Sstevel@tonic-gate } 9520Sstevel@tonic-gate 9530Sstevel@tonic-gate return (relocate_finish(lmp, bound, textrel, ret)); 9540Sstevel@tonic-gate } 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate /* 9570Sstevel@tonic-gate * Initialize the first few got entries so that function calls go to 9580Sstevel@tonic-gate * elf_rtbndr: 9590Sstevel@tonic-gate * 9600Sstevel@tonic-gate * GOT[GOT_XLINKMAP] = the address of the link map 9610Sstevel@tonic-gate * GOT[GOT_XRTLD] = the address of rtbinder 9620Sstevel@tonic-gate */ 9630Sstevel@tonic-gate void 9640Sstevel@tonic-gate elf_plt_init(void *got, caddr_t l) 9650Sstevel@tonic-gate { 9660Sstevel@tonic-gate uint_t *_got; 9670Sstevel@tonic-gate /* LINTED */ 9680Sstevel@tonic-gate Rt_map *lmp = (Rt_map *)l; 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate _got = (uint_t *)got + M_GOT_XLINKMAP; 9710Sstevel@tonic-gate *_got = (uint_t)lmp; 9720Sstevel@tonic-gate _got = (uint_t *)got + M_GOT_XRTLD; 9730Sstevel@tonic-gate *_got = (uint_t)elf_rtbndr; 9740Sstevel@tonic-gate } 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate /* 9770Sstevel@tonic-gate * For SVR4 Intel compatability. USL uses /usr/lib/libc.so.1 as the run-time 9780Sstevel@tonic-gate * linker, so the interpreter's address will differ from /usr/lib/ld.so.1. 9790Sstevel@tonic-gate * Further, USL has special _iob[] and _ctype[] processing that makes up for the 9800Sstevel@tonic-gate * fact that these arrays do not have associated copy relocations. So we try 9810Sstevel@tonic-gate * and make up for that here. Any relocations found will be added to the global 9820Sstevel@tonic-gate * copy relocation list and will be processed in setup(). 9830Sstevel@tonic-gate */ 9840Sstevel@tonic-gate static int 9850Sstevel@tonic-gate _elf_copy_reloc(const char *name, Rt_map *rlmp, Rt_map *dlmp) 9860Sstevel@tonic-gate { 9870Sstevel@tonic-gate Sym *symref, *symdef; 9880Sstevel@tonic-gate caddr_t ref, def; 9890Sstevel@tonic-gate Rt_map *_lmp; 9900Sstevel@tonic-gate Rel rel; 9910Sstevel@tonic-gate Slookup sl; 9920Sstevel@tonic-gate uint_t binfo; 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate /* 9950Sstevel@tonic-gate * Determine if the special symbol exists as a reference in the dynamic 9960Sstevel@tonic-gate * executable, and that an associated definition exists in libc.so.1. 9970Sstevel@tonic-gate */ 9980Sstevel@tonic-gate sl.sl_name = name; 9990Sstevel@tonic-gate sl.sl_cmap = rlmp; 10000Sstevel@tonic-gate sl.sl_imap = rlmp; 10010Sstevel@tonic-gate sl.sl_hash = 0; 10020Sstevel@tonic-gate sl.sl_rsymndx = 0; 10030Sstevel@tonic-gate sl.sl_flags = LKUP_FIRST; 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate if ((symref = lookup_sym(&sl, &_lmp, &binfo)) == 0) 10060Sstevel@tonic-gate return (1); 10070Sstevel@tonic-gate 10080Sstevel@tonic-gate sl.sl_imap = dlmp; 10090Sstevel@tonic-gate sl.sl_flags = LKUP_DEFT; 10100Sstevel@tonic-gate 10110Sstevel@tonic-gate if ((symdef = lookup_sym(&sl, &_lmp, &binfo)) == 0) 10120Sstevel@tonic-gate return (1); 10130Sstevel@tonic-gate if (strcmp(NAME(_lmp), MSG_ORIG(MSG_PTH_LIBC))) 10140Sstevel@tonic-gate return (1); 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate /* 10170Sstevel@tonic-gate * Determine the reference and definition addresses. 10180Sstevel@tonic-gate */ 10190Sstevel@tonic-gate ref = (void *)(symref->st_value); 10200Sstevel@tonic-gate if (!(FLAGS(rlmp) & FLG_RT_FIXED)) 10210Sstevel@tonic-gate ref += ADDR(rlmp); 10220Sstevel@tonic-gate def = (void *)(symdef->st_value); 10230Sstevel@tonic-gate if (!(FLAGS(_lmp) & FLG_RT_FIXED)) 10240Sstevel@tonic-gate def += ADDR(_lmp); 10250Sstevel@tonic-gate 10260Sstevel@tonic-gate /* 10270Sstevel@tonic-gate * Set up a relocation entry for debugging and call the generic copy 10280Sstevel@tonic-gate * relocation function to provide symbol size error checking and to 10290Sstevel@tonic-gate * record the copy relocation that must be performed. 10300Sstevel@tonic-gate */ 10310Sstevel@tonic-gate rel.r_offset = (Addr)ref; 10320Sstevel@tonic-gate rel.r_info = (Word)R_386_COPY; 10330Sstevel@tonic-gate DBG_CALL(Dbg_reloc_in(M_MACH, M_REL_SHT_TYPE, &rel, name, 0)); 10340Sstevel@tonic-gate 10350Sstevel@tonic-gate return (elf_copy_reloc((char *)name, symref, rlmp, (void *)ref, symdef, 10360Sstevel@tonic-gate _lmp, (void *)def)); 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate int 10400Sstevel@tonic-gate elf_copy_gen(Rt_map *lmp) 10410Sstevel@tonic-gate { 10420Sstevel@tonic-gate if (interp && ((ulong_t)interp->i_faddr != 10430Sstevel@tonic-gate r_debug.rtd_rdebug.r_ldbase) && 10440Sstevel@tonic-gate !(strcmp(interp->i_name, MSG_ORIG(MSG_PTH_LIBC)))) { 10450Sstevel@tonic-gate 10466Srie DBG_CALL(Dbg_reloc_run(NAME(lmp), M_REL_SHT_TYPE, 0, 10470Sstevel@tonic-gate DBG_REL_START)); 10480Sstevel@tonic-gate 10490Sstevel@tonic-gate if (_elf_copy_reloc(MSG_ORIG(MSG_SYM_CTYPE), lmp, 10500Sstevel@tonic-gate (Rt_map *)NEXT(lmp)) == 0) 10510Sstevel@tonic-gate return (0); 10520Sstevel@tonic-gate if (_elf_copy_reloc(MSG_ORIG(MSG_SYM_IOB), lmp, 10530Sstevel@tonic-gate (Rt_map *)NEXT(lmp)) == 0) 10540Sstevel@tonic-gate return (0); 10550Sstevel@tonic-gate } 10560Sstevel@tonic-gate return (1); 10570Sstevel@tonic-gate } 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate /* 10600Sstevel@tonic-gate * Plt writing interface to allow debugging initialization to be generic. 10610Sstevel@tonic-gate */ 10620Sstevel@tonic-gate Pltbindtype 10630Sstevel@tonic-gate /* ARGSUSED1 */ 10640Sstevel@tonic-gate elf_plt_write(uintptr_t addr, uintptr_t vaddr, void *rptr, uintptr_t symval, 10650Sstevel@tonic-gate Xword pltndx) 10660Sstevel@tonic-gate { 10670Sstevel@tonic-gate Rel *rel = (Rel*)rptr; 10680Sstevel@tonic-gate uintptr_t pltaddr; 10690Sstevel@tonic-gate 10700Sstevel@tonic-gate pltaddr = addr + rel->r_offset; 10710Sstevel@tonic-gate *(ulong_t *)pltaddr = (ulong_t)symval; 10720Sstevel@tonic-gate DBG_CALL(pltcntfull++); 10730Sstevel@tonic-gate return (PLT_T_FULL); 10740Sstevel@tonic-gate } 10750Sstevel@tonic-gate 10760Sstevel@tonic-gate /* 10770Sstevel@tonic-gate * Provide a machine specific interface to the conversion routine. By calling 10780Sstevel@tonic-gate * the machine specific version, rather than the generic version, we insure that 10790Sstevel@tonic-gate * the data tables/strings for all known machine versions aren't dragged into 10800Sstevel@tonic-gate * ld.so.1. 10810Sstevel@tonic-gate */ 10820Sstevel@tonic-gate const char * 10830Sstevel@tonic-gate _conv_reloc_type_str(uint_t rel) 10840Sstevel@tonic-gate { 10850Sstevel@tonic-gate return (conv_reloc_386_type_str(rel)); 10860Sstevel@tonic-gate } 1087