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
51618Srie * Common Development and Distribution License (the "License").
61618Srie * 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 */
211618Srie
220Sstevel@tonic-gate /*
23*13074SAli.Bahrami@Oracle.COM * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
251618Srie
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <dlfcn.h>
280Sstevel@tonic-gate #include <libelf.h>
290Sstevel@tonic-gate #include <link.h>
301618Srie #include <debug.h>
310Sstevel@tonic-gate #include "msg.h"
320Sstevel@tonic-gate #include "_libld.h"
330Sstevel@tonic-gate
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate * Table which defines the default functions to be called by the library
360Sstevel@tonic-gate * SUPPORT (-S <libname>). These functions can be redefined by the
370Sstevel@tonic-gate * ld_support_loadso() routine.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate static Support_list support[LDS_NUM] = {
409131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_VERSION), NULL}, /* LDS_VERSION */
419131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_INPUT_DONE), NULL}, /* LDS_INPUT_DONE */
421618Srie #if defined(_ELF64)
439131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_START_64), NULL}, /* LDS_START */
449131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_ATEXIT_64), NULL}, /* LDS_ATEXIT */
459131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_OPEN_64), NULL}, /* LDS_OPEN */
469131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_FILE_64), NULL}, /* LDS_FILE */
479131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_INSEC_64), NULL}, /* LDS_INSEC */
489131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_SEC_64), NULL} /* LDS_SEC */
491618Srie #else /* Elf32 */
509131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_START), NULL}, /* LDS_START */
519131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_ATEXIT), NULL}, /* LDS_ATEXIT */
529131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_OPEN), NULL}, /* LDS_OPEN */
539131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_FILE), NULL}, /* LDS_FILE */
549131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_INSEC), NULL}, /* LDS_INSEC */
559131SRod.Evans@Sun.COM {MSG_ORIG(MSG_SUP_SEC), NULL} /* LDS_SEC */
560Sstevel@tonic-gate #endif
570Sstevel@tonic-gate };
580Sstevel@tonic-gate
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate * Loads in a support shared object specified using the SGS_SUPPORT environment
610Sstevel@tonic-gate * variable or the -S ld option, and determines which interface functions are
620Sstevel@tonic-gate * provided by that object.
630Sstevel@tonic-gate */
640Sstevel@tonic-gate uintptr_t
ld_sup_loadso(Ofl_desc * ofl,const char * obj)651618Srie ld_sup_loadso(Ofl_desc *ofl, const char *obj)
660Sstevel@tonic-gate {
679131SRod.Evans@Sun.COM void *handle, (*fptr)();
689131SRod.Evans@Sun.COM uint_t interface, version = LD_SUP_VERSION1;
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate * Load the required support library. If we are unable to load it fail
720Sstevel@tonic-gate * with a fatal error.
730Sstevel@tonic-gate */
742850Srie if ((handle = dlopen(obj, (RTLD_LAZY | RTLD_FIRST))) == NULL) {
75*13074SAli.Bahrami@Oracle.COM ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_SUP_NOLOAD),
761618Srie obj, dlerror());
770Sstevel@tonic-gate return (S_ERROR);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate
802850Srie for (interface = 0; interface < LDS_NUM; interface++) {
819131SRod.Evans@Sun.COM Func_list fl;
829131SRod.Evans@Sun.COM
832850Srie if ((fptr = (void (*)())dlsym(handle,
842850Srie support[interface].sup_name)) == NULL)
852850Srie continue;
860Sstevel@tonic-gate
872850Srie DBG_CALL(Dbg_support_load(ofl->ofl_lml, obj,
882850Srie support[interface].sup_name));
891618Srie
902850Srie if (interface == LDS_VERSION) {
918324SAli.Bahrami@Sun.COM DBG_CALL(Dbg_support_action(ofl->ofl_lml, obj,
922850Srie support[LDS_VERSION].sup_name, LDS_VERSION, 0));
930Sstevel@tonic-gate
948324SAli.Bahrami@Sun.COM version = ((uint_t(*)())fptr)(LD_SUP_VCURRENT);
958324SAli.Bahrami@Sun.COM
968324SAli.Bahrami@Sun.COM /*
978324SAli.Bahrami@Sun.COM * If version is LD_SUP_VNONE, unload the support
988324SAli.Bahrami@Sun.COM * library and act as if it was never requested.
998324SAli.Bahrami@Sun.COM * This provides the support library with a mechanism
1008324SAli.Bahrami@Sun.COM * for opting out of the process.
1018324SAli.Bahrami@Sun.COM *
1028324SAli.Bahrami@Sun.COM * Note that this depends on LDS_VERSION being the
1038324SAli.Bahrami@Sun.COM * first item in support[]. If this were not true,
1048324SAli.Bahrami@Sun.COM * then other functions from the library might be
1058324SAli.Bahrami@Sun.COM * entered into the function lists, and unloading
1068324SAli.Bahrami@Sun.COM * the object would cause corruption.
1078324SAli.Bahrami@Sun.COM */
1088324SAli.Bahrami@Sun.COM assert(LDS_VERSION == 0);
1098324SAli.Bahrami@Sun.COM if (version == LD_SUP_VNONE) {
1108324SAli.Bahrami@Sun.COM DBG_CALL(Dbg_support_vnone(ofl->ofl_lml,
1118324SAli.Bahrami@Sun.COM obj));
1128324SAli.Bahrami@Sun.COM (void) dlclose(handle);
1138324SAli.Bahrami@Sun.COM return (1);
1148324SAli.Bahrami@Sun.COM }
1158324SAli.Bahrami@Sun.COM
1168324SAli.Bahrami@Sun.COM /*
1178324SAli.Bahrami@Sun.COM * If the support library has a version higher
1188324SAli.Bahrami@Sun.COM * than we support, we are unable to accept it.
1198324SAli.Bahrami@Sun.COM */
1208324SAli.Bahrami@Sun.COM if (version > LD_SUP_VCURRENT) {
121*13074SAli.Bahrami@Oracle.COM ld_eprintf(ofl, ERR_FATAL,
1228368SAli.Bahrami@Sun.COM MSG_INTL(MSG_SUP_BADVERSION), obj,
1232850Srie LD_SUP_VCURRENT, version);
1242850Srie (void) dlclose(handle);
1252850Srie return (S_ERROR);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate }
1288324SAli.Bahrami@Sun.COM
1299131SRod.Evans@Sun.COM fl.fl_obj = obj;
1309131SRod.Evans@Sun.COM fl.fl_fptr = fptr;
1319131SRod.Evans@Sun.COM fl.fl_version = version;
1329131SRod.Evans@Sun.COM if (alist_append(&support[interface].sup_funcs, &fl,
1339131SRod.Evans@Sun.COM sizeof (Func_list), AL_CNT_SUPPORT) == NULL)
1342850Srie return (S_ERROR);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate return (1);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate * Wrapper routines for the ld support library calls.
1410Sstevel@tonic-gate */
1420Sstevel@tonic-gate void
ld_sup_start(Ofl_desc * ofl,const Half etype,const char * caller)1431618Srie ld_sup_start(Ofl_desc *ofl, const Half etype, const char *caller)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate Func_list *flp;
1469131SRod.Evans@Sun.COM Aliste idx;
1470Sstevel@tonic-gate
1489131SRod.Evans@Sun.COM for (ALIST_TRAVERSE(support[LDS_START].sup_funcs, idx, flp)) {
1491618Srie DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
1501618Srie support[LDS_START].sup_name, LDS_START, ofl->ofl_name));
1511618Srie (*flp->fl_fptr)(ofl->ofl_name, etype, caller);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate void
ld_sup_atexit(Ofl_desc * ofl,int ecode)1561618Srie ld_sup_atexit(Ofl_desc *ofl, int ecode)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate Func_list *flp;
1599131SRod.Evans@Sun.COM Aliste idx;
1600Sstevel@tonic-gate
1619131SRod.Evans@Sun.COM for (ALIST_TRAVERSE(support[LDS_ATEXIT].sup_funcs, idx, flp)) {
1621618Srie DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
1630Sstevel@tonic-gate support[LDS_ATEXIT].sup_name, LDS_ATEXIT, 0));
1641618Srie (*flp->fl_fptr)(ecode);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate void
ld_sup_open(Ofl_desc * ofl,const char ** opath,const char ** ofile,int * ofd,int flags,Elf ** oelf,Elf * ref,size_t off,const Elf_Kind ekind)1692978Srie ld_sup_open(Ofl_desc *ofl, const char **opath, const char **ofile, int *ofd,
1702978Srie int flags, Elf **oelf, Elf *ref, size_t off, const Elf_Kind ekind)
1712978Srie {
1722978Srie Func_list *flp;
1739131SRod.Evans@Sun.COM Aliste idx;
1742978Srie
1759131SRod.Evans@Sun.COM for (ALIST_TRAVERSE(support[LDS_OPEN].sup_funcs, idx, flp)) {
1769131SRod.Evans@Sun.COM const char *npath = *opath;
1779131SRod.Evans@Sun.COM const char *nfile = *ofile;
1789131SRod.Evans@Sun.COM Elf *nelf = *oelf;
1799131SRod.Evans@Sun.COM int nfd = *ofd;
1809131SRod.Evans@Sun.COM int _flags = 0;
1812978Srie
1822978Srie /*
1832978Srie * This interface was introduced in VERSION3. Only call this
1842978Srie * function for libraries reporting support for version 3 or
1852978Srie * above.
1862978Srie */
1872978Srie if (flp->fl_version < LD_SUP_VERSION3)
1882978Srie continue;
1892978Srie
1902978Srie if (!(flags & FLG_IF_CMDLINE))
1912978Srie _flags |= LD_SUP_DERIVED;
1922978Srie if (!(flags & FLG_IF_NEEDED))
1932978Srie _flags |= LD_SUP_INHERITED;
1942978Srie if (flags & FLG_IF_EXTRACT)
1952978Srie _flags |= LD_SUP_EXTRACTED;
1962978Srie
1972978Srie /*
1982978Srie * If the present object is an extracted archive member, make
1992978Srie * sure the archive offset is reset so that the caller can
2002978Srie * obtain an ELF descriptor to the same member (an elf_begin()
2012978Srie * moves the offset to the next member).
2022978Srie */
2032978Srie if (flags & FLG_IF_EXTRACT)
2042978Srie (void) elf_rand(ref, off);
2052978Srie
2062978Srie DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
2072978Srie support[LDS_OPEN].sup_name, LDS_OPEN, *opath));
2082978Srie (*flp->fl_fptr)(&npath, &nfile, &nfd, _flags, &nelf, ref, off,
2092978Srie ekind);
2102978Srie
2119131SRod.Evans@Sun.COM /*
2129131SRod.Evans@Sun.COM * If the file descriptor, ELF descriptor, or file names have
2139131SRod.Evans@Sun.COM * been modified, then diagnose the differences and return the
2149131SRod.Evans@Sun.COM * new data. As a basic test, make sure the support library
2159131SRod.Evans@Sun.COM * hasn't nulled out data ld(1) will try and dereference.
2169131SRod.Evans@Sun.COM */
2179131SRod.Evans@Sun.COM if ((npath != *opath) || (nfd != *ofd) || (nelf != *oelf)) {
21811827SRod.Evans@Sun.COM DBG_CALL(Dbg_file_modified(ofl->ofl_lml, flp->fl_obj,
21911827SRod.Evans@Sun.COM *opath, npath, *ofd, nfd, *oelf, nelf));
2209131SRod.Evans@Sun.COM if (npath)
2219131SRod.Evans@Sun.COM *opath = npath;
2229131SRod.Evans@Sun.COM if (nfile)
2239131SRod.Evans@Sun.COM *ofile = nfile;
2249131SRod.Evans@Sun.COM *ofd = nfd;
2259131SRod.Evans@Sun.COM *oelf = nelf;
2269131SRod.Evans@Sun.COM }
2272978Srie }
2282978Srie }
2292978Srie
2302978Srie void
ld_sup_file(Ofl_desc * ofl,const char * ifile,const Elf_Kind ekind,int flags,Elf * elf)2311618Srie ld_sup_file(Ofl_desc *ofl, const char *ifile, const Elf_Kind ekind, int flags,
2321618Srie Elf *elf)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate Func_list *flp;
2359131SRod.Evans@Sun.COM Aliste idx;
2360Sstevel@tonic-gate
2379131SRod.Evans@Sun.COM for (ALIST_TRAVERSE(support[LDS_FILE].sup_funcs, idx, flp)) {
2380Sstevel@tonic-gate int _flags = 0;
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate if (!(flags & FLG_IF_CMDLINE))
2410Sstevel@tonic-gate _flags |= LD_SUP_DERIVED;
2420Sstevel@tonic-gate if (!(flags & FLG_IF_NEEDED))
2430Sstevel@tonic-gate _flags |= LD_SUP_INHERITED;
2440Sstevel@tonic-gate if (flags & FLG_IF_EXTRACT)
2450Sstevel@tonic-gate _flags |= LD_SUP_EXTRACTED;
2460Sstevel@tonic-gate
2471618Srie DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
2480Sstevel@tonic-gate support[LDS_FILE].sup_name, LDS_FILE, ifile));
2490Sstevel@tonic-gate (*flp->fl_fptr)(ifile, ekind, _flags, elf);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate uintptr_t
ld_sup_input_section(Ofl_desc * ofl,Ifl_desc * ifl,const char * sname,Shdr ** oshdr,Word ndx,Elf_Scn * scn,Elf * elf)2542647Srie ld_sup_input_section(Ofl_desc *ofl, Ifl_desc *ifl, const char *sname,
2552647Srie Shdr **oshdr, Word ndx, Elf_Scn *scn, Elf *elf)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate Func_list *flp;
2589131SRod.Evans@Sun.COM Aliste idx;
2590Sstevel@tonic-gate uint_t flags = 0;
2600Sstevel@tonic-gate Elf_Data *data = NULL;
2610Sstevel@tonic-gate
2629131SRod.Evans@Sun.COM for (ALIST_TRAVERSE(support[LDS_INSEC].sup_funcs, idx, flp)) {
2639131SRod.Evans@Sun.COM Shdr *nshdr = *oshdr;
2649131SRod.Evans@Sun.COM
2650Sstevel@tonic-gate /*
2662850Srie * This interface was introduced in VERSION2. Only call this
2672850Srie * function for libraries reporting support for version 2 or
2682850Srie * above.
2690Sstevel@tonic-gate */
2700Sstevel@tonic-gate if (flp->fl_version < LD_SUP_VERSION2)
2710Sstevel@tonic-gate continue;
2722850Srie
2730Sstevel@tonic-gate if ((data == NULL) &&
2740Sstevel@tonic-gate ((data = elf_getdata(scn, NULL)) == NULL)) {
275*13074SAli.Bahrami@Oracle.COM ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA),
276*13074SAli.Bahrami@Oracle.COM ifl->ifl_name);
2770Sstevel@tonic-gate return (S_ERROR);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate
2801618Srie DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
2812850Srie support[LDS_INSEC].sup_name, LDS_INSEC, sname));
2822647Srie (*flp->fl_fptr)(sname, &nshdr, ndx, data, elf, &flags);
2832647Srie
2849131SRod.Evans@Sun.COM /*
2859131SRod.Evans@Sun.COM * If the section header has been re-allocated (known to occur
2869131SRod.Evans@Sun.COM * with libCCexcept.so), then diagnose the section header
2879131SRod.Evans@Sun.COM * difference and return the new section header.
2889131SRod.Evans@Sun.COM */
2899131SRod.Evans@Sun.COM if (nshdr != *oshdr) {
29011827SRod.Evans@Sun.COM Ehdr *ehdr = ifl->ifl_ehdr;
29111827SRod.Evans@Sun.COM
29211827SRod.Evans@Sun.COM DBG_CALL(Dbg_shdr_modified(ofl->ofl_lml, flp->fl_obj,
29311827SRod.Evans@Sun.COM ehdr->e_ident[EI_OSABI], ehdr->e_machine, ndx,
29411827SRod.Evans@Sun.COM *oshdr, nshdr, sname));
2959131SRod.Evans@Sun.COM *oshdr = nshdr;
2969131SRod.Evans@Sun.COM }
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate return (0);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate void
ld_sup_section(Ofl_desc * ofl,const char * scn,Shdr * shdr,Word ndx,Elf_Data * data,Elf * elf)3021618Srie ld_sup_section(Ofl_desc *ofl, const char *scn, Shdr *shdr, Word ndx,
3030Sstevel@tonic-gate Elf_Data *data, Elf *elf)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate Func_list *flp;
3069131SRod.Evans@Sun.COM Aliste idx;
3070Sstevel@tonic-gate
3089131SRod.Evans@Sun.COM for (ALIST_TRAVERSE(support[LDS_SEC].sup_funcs, idx, flp)) {
3091618Srie DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
3102850Srie support[LDS_SEC].sup_name, LDS_SEC, scn));
3110Sstevel@tonic-gate (*flp->fl_fptr)(scn, shdr, ndx, data, elf);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate void
ld_sup_input_done(Ofl_desc * ofl)3161618Srie ld_sup_input_done(Ofl_desc *ofl)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate Func_list *flp;
3199131SRod.Evans@Sun.COM Aliste idx;
3200Sstevel@tonic-gate uint_t flags = 0;
3210Sstevel@tonic-gate
3229131SRod.Evans@Sun.COM for (ALIST_TRAVERSE(support[LDS_INPUT_DONE].sup_funcs, idx, flp)) {
3230Sstevel@tonic-gate /*
3242850Srie * This interface was introduced in VERSION2. Only call this
3252850Srie * function for libraries reporting support for version 2 or
3262850Srie * above.
3270Sstevel@tonic-gate */
3280Sstevel@tonic-gate if (flp->fl_version < LD_SUP_VERSION2)
3290Sstevel@tonic-gate continue;
3302850Srie
3311618Srie DBG_CALL(Dbg_support_action(ofl->ofl_lml, flp->fl_obj,
3320Sstevel@tonic-gate support[LDS_INPUT_DONE].sup_name, LDS_INPUT_DONE, 0));
3330Sstevel@tonic-gate (*flp->fl_fptr)(&flags);
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate }
336