/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2012, Joyent, Inc. All rights reserved.
*/
/*
* SPARC V9 machine dependent and ELF file class dependent functions.
* Contains routines for performing function binding and symbol relocations.
*/
#include <stdio.h>
#include <sys/elf_SPARC.h>
#include <dlfcn.h>
#include <synch.h>
#include <string.h>
#include <debug.h>
#include <reloc.h>
#include <conv.h>
#include "_rtld.h"
#include "_audit.h"
#include "_elf.h"
#include "_inline_gen.h"
#include "_inline_reloc.h"
#include "msg.h"
int
{
/*
* Check machine type and flags.
*/
/*
* Check vendor-specific extensions.
*/
return (0);
}
return (0);
}
/*
* Generic check.
* All of our 64-bit SPARC's support the US1 (UltraSPARC 1)
* instructions so that bit isn't worth checking for explicitly.
*/
return (0);
}
return (0);
}
return (1);
}
void
{
/*
* There is no need to analyze ld.so because we don't map in any of
* its dependencies. However we may map these dependencies in later
* (as if ld.so had dlopened them), so initialize the plt and the
* permission information.
*/
/*
* Install the lm pointer in .PLT2 as per the ABI.
*/
/*
* The V9 ABI states that the first 32k PLT entries
* use .PLT1, with .PLT0 used by the "latter" entries.
* We don't currently implement the extendend format,
* so install an error handler in .PLT0 to catch anyone
* trying to use it.
*/
/*
* Initialize .PLT1
*/
}
}
/*
* elf_plt_write() will test to see how far away our destination
* address lies. If it is close enough that a branch can
* be used instead of a jmpl - we will fill the plt in with
* single branch. The branches are much quicker then
* a jmpl instruction - see bug#4356879 for further
* details.
*
* NOTE: we pass in both a 'pltaddr' and a 'vpltaddr' since
* address is not the same as the 'virtual' runtime
* address.
*/
{
long disp;
return (PLT_T_FAR);
}
/*
* Test if the destination address is close enough to use
* a ba,a... instruction to reach it.
*/
/*
* The
*
* ba,a,pt %icc, <dest>
*
* is the most efficient of the PLT's. If we
* are within +-20 bits - use that branch.
*/
/* LINTED */
} else {
/*
* Otherwise - we fall back to the good old
*
* ba,a <dest>
*
* Which still beats a jmpl instruction.
*/
/* LINTED */
}
return (rc);
}
if ((nsym >> 32) == 0) {
return (PLT_T_U32);
}
if ((nsym >> 44) == 0) {
return (PLT_T_U44);
}
/*
* The PLT destination is not in reach of
* a branch instruction - so we fall back
* to a 'jmpl' sequence.
*/
DBG_CALL(pltcntfull++);
return (PLT_T_FULL);
}
/*
* Once relocated, the following 6 instruction sequence moves
* a 64-bit immediate value into register %g1
*/
#define VAL64_TO_G1 \
/* 0x00 */ 0x0b, 0x00, 0x00, 0x00, /* sethi %hh(value), %g5 */ \
/* 0x04 */ 0x8a, 0x11, 0x60, 0x00, /* or %g5, %hm(value), %g5 */ \
/* 0x08 */ 0x8b, 0x29, 0x70, 0x20, /* sllx %g5, 32, %g5 */ \
/* 0x0c */ 0x03, 0x00, 0x00, 0x00, /* sethi %lm(value), %g1 */ \
/* 0x10 */ 0x82, 0x10, 0x60, 0x00, /* or %g1, %lo(value), %g1 */ \
/* 0x14 */ 0x82, 0x10, 0x40, 0x05 /* or %g1, %g5, %g1 */
/*
* Local storage space created on the stack created for this glue
* code includes space for:
* 0x8 pointer to dyn_data
* 0x8 size prev stack frame
*/
/* 0x0 */ 0x2a, 0xcf, 0x80, 0x03, /* brnz,a,pt %fp, 0xc */
/* 0x4 */ 0x82, 0x27, 0x80, 0x0e, /* sub %fp, %sp, %g1 */
/* 0x8 */ 0x82, 0x10, 0x20, 0xb0, /* mov 176, %g1 */
/* 0xc */ 0x9d, 0xe3, 0xbf, 0x40, /* save %sp, -192, %sp */
/* 0x10 */ 0xc2, 0x77, 0xa7, 0xef, /* stx %g1, [%fp + 2031] */
/* store prev stack size */
/* 0x2c */ 0xc2, 0x77, 0xa7, 0xf7, /* stx %g1, [%fp + 2039] */
/* Call to elf_plt_trace() via g1 */
/* 0x48 */ 0x9f, 0xc0, 0x60, 0x00, /* jmpl ! link r[15] to addr in g1 */
/* 0x4c */ 0x01, 0x00, 0x00, 0x00 /* nop ! for jmpl delay slot *AND* */
/* to get 8-byte alignment */
};
sizeof (Addr) + /* reflmp */
sizeof (Addr) + /* deflmp */
sizeof (Word) + /* symndx */
sizeof (Word) + /* sb_flags */
sizeof (Sym); /* symdef */
/*
* the dynamic plt entry is:
*
* brnz,a,pt %fp, 1f
* sub %sp, %fp, %g1
* mov SA(MINFRAME), %g1
* 1:
* save %sp, -(SA(MINFRAME) + (2 * CLONGSIZE)), %sp
*
* ! store prev stack size
* stx %g1, [%fp + STACK_BIAS - (2 * CLONGSIZE)]
*
* 2:
* ! move dyn_data to %g1
* sethi %hh(dyn_data), %g5
* or %g5, %hm(dyn_data), %g5
* sllx %g5, 32, %g5
* sethi %lm(dyn_data), %g1
* or %g1, %lo(dyn_data), %g1
* or %g1, %g5, %g1
*
* ! store dyn_data ptr on frame (from %g1)
* stx %g1, [%fp + STACK_BIAS - CLONGSIZE]
*
* ! Move address of elf_plt_trace() into %g1
* [Uses same 6 instructions as shown at label 2: above. Not shown.]
*
* ! Use JMPL to make call. CALL instruction is limited to 30-bits.
* ! of displacement.
* jmp1 %g1, %o7
*
* ! JMPL has a delay slot that must be filled. And, the sequence
* ! of instructions needs to have 8-byte alignment. This NOP does both.
* ! The alignment is needed for the data we put following the
* ! instruction.
* nop
*
* dyn data:
* Addr reflmp
* Addr deflmp
* Word symndx
* Word sb_flags
* Sym symdef (Elf64_Sym = 24-bytes)
*/
/*
* Relocate the instructions given by the VAL64_TO_G1 macro above.
* The arguments parallel those of do_reloc_rtld().
*
* entry:
* off - Address of 1st instruction in sequence.
* value - Value being relocated (addend)
* sym - Name of value being relocated.
* lml - link map list
*
* exit:
* Returns TRUE for success, FALSE for failure.
*/
static int
{
/*
* relocating:
* sethi %hh(value), %g5
*/
return (0);
}
/*
* relocating:
* or %g5, %hm(value), %g5
*/
return (0);
}
/*
* relocating:
* sethi %lm(value), %g1
*/
return (0);
}
/*
* relocating:
* or %g1, %lo(value), %g1
*/
return (0);
}
return (1);
}
static caddr_t
int *fail)
{
extern ulong_t elf_plt_trace();
/*
* If both pltenter & pltexit have been disabled there
* there is no reason to even create the glue code.
*/
return (to);
}
/*
* We only need to add the glue code if there is an auditing
* library that is interested in this binding.
*/
(pltndx * dyn_plt_ent_size));
/*
* Have we initialized this dynamic plt entry yet? If we haven't do it
* now. Otherwise this function has been called before, but from a
* different plt (ie. from another shared object). In that case
* we just set the plt to point to the new dyn_plt.
*/
if (*dyn_plt == 0) {
sizeof (dyn_plt_template));
sizeof (dyn_plt_template));
/*
* relocating:
* VAL64_TO_G1(dyndata)
* VAL64_TO_G1(&elf_plt_trace)
*/
*fail = 1;
return (0);
}
/*
* symndx in the high word, sb_flags in the low.
*/
dyndata++;
}
}
/*
* Function binding routine - invoked on the first call to a function through
* the procedure linkage table;
* passes first through an assembly language interface.
*
* Takes the address of the PLT entry where the call originated,
* the offset into the relocation table of the associated
* relocation entry and the address of the link map (rt_private_map struct)
* for the entry.
*
* Returns the address of the function referenced after re-writing the PLT
* entry to invoke the function directly.
*
* On error, causes process to terminate with a signal.
*/
{
char *name;
/*
* For compatibility with libthread (TI_VERSION 1) we track the entry
* value. A zero value indicates we have recursed into ld.so.1 to
* further process a locking request. Under this recursion we disable
* tsort and cleanup activities.
*/
}
/*
* Must calculate true plt relocation address from reloc.
* Take offset, subtract number of reserved PLT entries, and divide
* by PLT entry size, which should give the index of the plt
* entry (and relocation entry since they have been defined to be
* in the same order). Then we must multiply by the size of
* a relocation entry, which will give us the offset of the
* plt relocation entry from the start of them given by JMPREL(lm).
*/
} else {
farplt = 1;
}
/*
* Perform some basic sanity checks. If we didn't get a load map
* or the plt offset is invalid then its possible someone has walked
* over the plt entries or jumped to plt[01] out of the blue.
*/
}
/*
* Use relocation entry to get symbol table entry and symbol name.
*/
/*
* Determine the last link-map of this list, this'll be the starting
* point for any tsort() processing.
*/
/*
* Find definition for symbol. Initialize the symbol lookup, and symbol
* result, data structures.
*/
}
/*
* Record that this new link map is now bound to the caller.
*/
}
/* LINTED */
&sb_flags);
}
vaddr = 0;
else
pbtype = PLT_T_NONE;
if (!(rtld_flags & RT_FL_NOBIND)) {
int fail = 0;
/* LINTED */
if (fail)
} else {
/*
* Write standard PLT entry to jump directly
* to newly bound function.
*/
}
}
/*
* Print binding information and rebuild PLT entry.
*/
/*
* Complete any processing for newly loaded objects. Note we don't
* know exactly where any new objects are loaded (we know the object
* that supplied the symbol, but others may have been loaded lazily as
* we searched for the symbol), so sorting starts from the last
* link-map know on entry to this routine.
*/
if (entry)
/*
* Some operations like dldump() or dlopen()'ing a relocatable object
* result in objects being loaded on rtld's link-map, make sure these
* objects are initialized also.
*/
/*
* Make sure the object to which we've bound has had it's .init fired.
* Cleanup before return to user code.
*/
if (entry) {
}
if (lmflags & LML_FLG_RTLDLM)
return (symval);
}
static int
{
void *plt;
int i;
sname));
return (1);
}
break;
}
/*
* Just fail in usual relocation way
*/
return (1);
}
/*
* elf_plt_write assumes the plt was previously filled
* with NOP's, so fill it in now.
*/
for (i = 0; i < (M_PLT_ENTSIZE / sizeof (uint_t)); i++) {
}
return (0);
return (1);
}
/*
* Read and process the relocations for one link object, we assume all
* relocation sections for loadable segments are stored contiguously in
* the file.
*/
int
{
long reladd;
/*
* If an object has any DT_REGISTER entries associated with
* it, they are processed now.
*/
if (elf_regsyms(lmp) == 0)
return (0);
}
/*
* Although only necessary for lazy binding, initialize the first
* procedure linkage table entry to go to elf_rtbndr(). dbx(1) seems
* to find this useful.
*/
/*
* Make sure the segment is writable.
*/
if ((((mpp =
return (0);
/*
* Install the lm pointer in .PLT2 as per the ABI.
*/
/*
* The V9 ABI states that the first 32k PLT entries
* use .PLT1, with .PLT0 used by the "latter" entries.
* We don't currently implement the extendend format,
* so install an error handler in .PLT0 to catch anyone
* trying to use it.
*/
/*
* Initialize .PLT1
*/
}
/*
* Initialize the plt start and end addresses.
*/
/*
* If we've been called upon to promote an RTLD_LAZY object to an
* RTLD_NOW then we're only interested in scaning the .plt table.
*/
if (plt) {
} else {
/*
* The relocation sections appear to the run-time linker as a
* single table. Determine the address of the beginning and end
* of this table. There are two different interpretations of
* the ABI at this point:
*
* o The REL table and its associated RELSZ indicate the
* concatenation of *all* relocation sections (this is the
* model our link-editor constructs).
*
* o The REL table and its associated RELSZ indicate the
* concatenation of all *but* the .plt relocations. These
* relocations are specified individually by the JMPREL and
* PLTRELSZ entries.
*
* Determine from our knowledege of the relocation range and
* .plt range, the range of the total relocation table. Note
* that one other ABI assumption seems to be that the .plt
* relocations always follow any other relocations, the
* following range checking drops that assumption.
*/
if (pltbgn) {
}
}
return (1);
}
/*
* If we're processing in lazy mode there is no need to scan the
* .rela.plt table.
*/
noplt = 1;
/*
* Loop through relocations.
*/
/*
* If this is a RELATIVE relocation in a shared object
* (the common case), and if we are not debugging, then
* jump into a tighter relocaiton loop (elf_reloc_relacount)
* Only make the jump if we've been given a hint on the
* number of relocations.
*/
if ((rtype == R_SPARC_RELATIVE) &&
if (relacount) {
textrel, 0);
relacount = 0;
} else {
}
break;
}
/*
* Optimizations.
*/
if (rtype == R_SPARC_NONE)
continue;
continue;
}
if (rtype != R_SPARC_REGISTER) {
/*
* If this is a shared object, add the base address
* to offset.
*/
/*
* If this relocation is not against part of the image
* mapped into memory we skip it.
*/
rsymndx);
continue;
}
}
/*
* If we're promoting plts, determine if this one has already
* been written. An uninitialized plts' second instruction is a
* branch.
*/
if (plt) {
/* LINTED */
continue;
}
binfo = 0;
pbtype = PLT_T_NONE;
/*
* If a symbol index is specified then get the symbol table
* entry, locate the symbol definition, and determine its
* address.
*/
if (rsymndx) {
/*
* If a Syminfo section is provided, determine if this
* symbol is deferred, and if so, skip this relocation.
*/
continue;
/*
* Get the local symbol table entry.
*/
/*
* If this is a local symbol, just use the base address.
* (we should have no local relocations in the
* executable).
*/
/*
* Special case TLS relocations.
*/
if ((rtype == R_SPARC_TLS_DTPMOD32) ||
(rtype == R_SPARC_TLS_DTPMOD64)) {
/*
* Use the TLS modid.
*/
} else if ((rtype == R_SPARC_TLS_TPOFF32) ||
(rtype == R_SPARC_TLS_TPOFF64)) {
ret = 0;
break;
}
}
} else {
/*
* If the symbol index is equal to the previous
* symbol index relocation we processed then
* reuse the previous values. (Note that there
* have been cases where a relocation exists
* against a copy relocation symbol, our ld(1)
* should optimize this away, but make sure we
* don't use the same symbol information should
* this case exist).
*/
(rtype != R_SPARC_COPY)) {
/* LINTED */
if (psymdef == 0) {
continue;
}
/* LINTED */
/* LINTED */
/* LINTED */
/* LINTED */
/* LINTED */
/* LINTED */
&sb_flags);
}
} else {
/*
* Lookup the symbol definition.
* Initialize the symbol lookup, and
* symbol result, data structures.
*/
in_nfavl)) {
}
/*
* If the symbol is not found and the
* reference was not to a weak symbol,
* report an error. Weak references
* may be unresolved.
*/
/* BEGIN CSTYLED */
if (symdef == 0) {
continue;
ret = 0;
break;
} else {
psymdef = 0;
continue;
}
}
/* END CSTYLED */
/*
* If symbol was found in an object
* other than the referencing object
* then record the binding.
*/
FL1_RT_NOINIFIN) == 0)) {
AL_CNT_RELBIND) == 0) {
ret = 0;
break;
}
}
/*
* Calculate the location of definition;
* symbol value plus base address of
* containing shared object.
*/
else
STT_TLS))
/*
* Retain this symbol index and the
* value in case it can be used for the
* subsequent relocations.
*/
if (rtype != R_SPARC_COPY) {
}
/* LINTED */
&sb_flags);
}
}
/*
* If relocation is PC-relative, subtract
* offset address.
*/
if (IS_PC_RELATIVE(rtype))
/*
* Special case TLS relocations.
*/
if ((rtype == R_SPARC_TLS_DTPMOD32) ||
(rtype == R_SPARC_TLS_DTPMOD64)) {
/*
* Relocation value is the TLS modid.
*/
} else if ((rtype == R_SPARC_TLS_TPOFF64) ||
(rtype == R_SPARC_TLS_TPOFF32)) {
value)) == 0) {
ret = 0;
break;
}
}
}
} else {
/*
* Special cases.
*/
if (rtype == R_SPARC_REGISTER) {
/*
* A register symbol associated with symbol
* index 0 is initialized (i.e. relocated) to
* a constant in the r_addend field rather than
* to a symbol value.
*/
value = 0;
} else if ((rtype == R_SPARC_TLS_DTPMOD32) ||
(rtype == R_SPARC_TLS_DTPMOD64)) {
/*
* TLS relocation value is the TLS modid.
*/
} else
}
/*
* Make sure the segment is writable.
*/
if ((rtype != R_SPARC_REGISTER) &&
ret = 0;
break;
}
/*
* Call relocation routine to perform required relocation.
*/
switch (rtype) {
case R_SPARC_REGISTER:
/*
* The v9 ABI 4.2.4 says that system objects may,
* but are not required to, use register symbols
* to inidcate how they use global registers. Thus
* at least %g6, %g7 must be allowed in addition
* to %g2 and %g3.
*/
if (roffset == STO_SPARC_REGISTER_G1) {
} else if (roffset == STO_SPARC_REGISTER_G2) {
} else if (roffset == STO_SPARC_REGISTER_G3) {
} else if (roffset == STO_SPARC_REGISTER_G4) {
} else if (roffset == STO_SPARC_REGISTER_G5) {
} else if (roffset == STO_SPARC_REGISTER_G6) {
} else if (roffset == STO_SPARC_REGISTER_G7) {
} else {
ret = 0;
break;
}
break;
case R_SPARC_COPY:
ret = 0;
break;
case R_SPARC_JMP_SLOT:
vaddr = 0;
else
int fail = 0;
/* LINTED */
if (fail)
ret = 0;
} else {
/*
* Write standard PLT entry to jump directly
* to newly bound function.
*/
pltndx);
}
break;
case R_SPARC_WDISP30:
void * plt = 0;
ret = 0;
break;
}
}
/* FALLTHROUGH */
default:
if (IS_EXTOFFSET(rtype))
/*
* Write the relocation out. If this relocation is a
* common basic write, skip the doreloc() engine.
*/
if ((rtype == R_SPARC_GLOB_DAT) ||
(rtype == R_SPARC_64)) {
if (roffset & 0x7) {
0, &inv_buf),
ret = 0;
} else
} else {
ret = 0;
}
/*
* The value now contains the 'bit-shifted' value that
* was or'ed into memory (this was set by
* do_reloc_rtld()).
*/
/*
* If this relocation is against a text segment, make
* sure that the instruction cache is flushed.
*/
if (textrel)
}
if ((ret == 0) &&
break;
if (binfo) {
}
}
/*
* Free up any items on the pltpadlist if it was allocated
*/
if (pltpadlist)
}
/*
* Provide a machine specific interface to the conversion routine. By calling
* the machine specific version, rather than the generic version, we insure that
* ld.so.1.
*/
const char *
{
}