machrel.intel.c revision 0bc07c75e71baa4cc26f90611864f7e60dcea093
/*
* 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) 1990, 1991 UNIX System Laboratories, Inc.
* Copyright (c) 1988 AT&T
* All Rights Reserved
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <string.h>
#include <stdio.h>
#include <debug.h>
#include <reloc.h>
#include "msg.h"
#include "_libld.h"
{
/* LINTED */
reld->rel_raddend = 0;
reld->rel_typedata = 0;
}
void
{
}
void
{
/*
* Create this entry if we are going to create a PLT table.
*/
if (ofl->ofl_pltcnt)
(*cnt)++; /* DT_PLTGOT */
}
}
void
{
else
(*dyn)++;
}
}
{
return (value);
}
/*
* Build a single plt entry - code is:
* if (building a.out)
* JMP *got_off
* else
* JMP *got_off@GOT(%ebx)
* PUSHL &rel_off
* JMP -n(%pc) # -n is pcrel offset to first plt entry
*
* The got_off@GOT entry gets filled with the address of the PUSHL,
* so the first pass through the plt jumps back here, jumping
* in turn to the first plt entry, which jumps to the dynamic
* linker. The dynamic linker then patches the GOT, rerouting
* future plt calls to the proper destination.
*/
static void
{
/*
* Fill in the got entry with the address of the next instruction.
*/
/* LINTED */
pltent[0] = M_SPECIAL_INST;
pltent += 2;
/* LINTED */
got_off);
} else {
pltent[0] = M_SPECIAL_INST;
pltent += 2;
/* LINTED */
}
pltent += 4;
pltent[0] = M_INST_PUSHL;
pltent++;
/* LINTED */
pltent += 4;
pltent[0] = M_INST_JMP;
pltent++;
/* LINTED */
}
{
char *relbits;
int sectmoved = 0;
/*
* If the section this relocation is against has been discarded
* (-zignore), then also discard (skip) the relocation itself.
*/
return (1);
}
/*
* If this is a relocation against a move table, or expanded move
* table, adjust the relocation entries.
*/
/*
* If this is a relocation against a section using a partial initialized
* symbol, adjust the embedded symbol info.
*
* The second argument of the am_I_partial() is the value stored at the
* target address relocation is going to be applied.
*/
/* LINTED */
orsp->rel_roffset)))) {
sectmoved = 1;
}
}
/*
* Note that relocations for PLT's actually
* cause a relocation againt the GOT.
*/
/*
* This must be a R_386_COPY. For these set the roffset to
* point to the new symbols location.
*/
} else {
/*
* Calculate virtual offset of reference point; equals offset
* into section + vaddr of section for loadable sections, or
* offset plus section displacement for nonloadable sections.
*/
}
/*
* Assign the symbols index for the output relocation. If the
* relocation refers to a SECTION symbol then it's index is based upon
* the output sections symbols index. Otherwise the index can be
* derived from the symbols index itself.
*/
if (sectmoved == 0) {
/*
* Check for a null input section. This can
* occur if this relocation references a symbol
* generated by sym_add_sym().
*/
else
} else
} else
/*
* Assert we haven't walked off the end of our relocation table.
*/
/*
* Determine if this relocation is against a non-writable, allocatable
* section. If so we may need to provide a text relocation diagnostic.
* Note that relocations against the .plt (R_386_JMP_SLOT) actually
* result in modifications to the .got.
*/
return (1);
}
/*
* i386 Instructions for TLS processing
*/
static uchar_t tlsinstr_gd_ie[] = {
/*
* 0x00 movl %gs:0x0, %eax
*/
0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
/*
* 0x06 addl x(%eax), %eax
* 0x0c ...
*/
0x03, 0x80, 0x00, 0x00, 0x00, 0x00
};
static uchar_t tlsinstr_gd_le[] = {
/*
* 0x00 movl %gs:0x0, %eax
*/
0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
/*
* 0x06 addl $0x0, %eax
*/
0x05, 0x00, 0x00, 0x00, 0x00,
/*
* 0x0b nop
* 0x0c
*/
0x90
};
static uchar_t tlsinstr_gd_ie_movgs[] = {
/*
* movl %gs:0x0,%eax
*/
0x65, 0xa1, 0x00, 0x00, 0x00, 00
};
#define MODRM_MSK_MOD 0xc0
#define MODRM_MSK_RO 0x38
#define MODRM_MSK_RM 0x07
#define SIB_MSK_SS 0xc0
#define SIB_MSK_IND 0x38
#define SIB_MSK_BS 0x07
static Fixupret
{
/*
* IE reference model
*/
switch (rtype) {
case R_386_TLS_GD:
/*
* Transition:
* 0x0 leal x@tlsgd(,r1,1), %eax
* 0x7 call ___tls_get_addr
* 0xc
* To:
* 0x0 movl %gs:0, %eax
* 0x6 addl x@gotntpoff(r1), %eax
*/
/*
* Addjust 'offset' to beginning of instruction
* sequence.
*/
offset -= 3;
sizeof (tlsinstr_gd_ie));
/*
* set register %r1 into the addl
* instruction.
*/
return (FIX_RELOC);
case R_386_TLS_GD_PLT:
/*
* Fixup done via the TLS_GD relocation
*/
return (FIX_DONE);
}
}
/*
* LE reference model
*/
switch (rtype) {
case R_386_TLS_GD:
/*
* Transition:
* 0x0 leal x@tlsgd(,r1,1), %eax
* 0x7 call ___tls_get_addr
* 0xc
* To:
* 0x0 movl %gs:0, %eax
* 0x6 addl $x@ntpoff, %eax
* 0xb nop
* 0xc
*/
/*
* Addjust 'offset' to beginning of instruction
* sequence.
*/
offset -= 3;
sizeof (tlsinstr_gd_le));
return (FIX_RELOC);
case R_386_TLS_GD_PLT:
case R_386_PLT32:
/*
* Fixup done via the TLS_GD relocation
*/
return (FIX_DONE);
case R_386_TLS_LDM_PLT:
/*
* Transition:
* call __tls_get_addr()
* to:
* nop
* nop
* nop
* nop
* nop
*/
return (FIX_DONE);
case R_386_TLS_LDM:
/*
* Transition:
*
* 0x00 leal x1@tlsldm(%ebx), %eax
* 0x06 call ___tls_get_addr
*
* to:
*
* 0x00 movl %gs:0, %eax
*/
sizeof (tlsinstr_gd_ie_movgs));
return (FIX_DONE);
case R_386_TLS_LDO_32:
/*
* Instructions:
*
* 0x10 leal x1@dtpoff(%eax), %edx R_386_TLS_LDO_32
* to
* 0x10 leal x1@ntpoff(%eax), %edx R_386_TLS_LE
*
*/
offset -= 2;
return (FIX_RELOC);
case R_386_TLS_GOTIE:
/*
* These transitions are a little different than the
* others, in that we could have multiple instructions
* pointed to by a single relocation. Depending upon the
* instruction, we perform a different code transition.
*
* Here's the known transitions:
*
* 1) movl foo@gotntpoff(%reg1), %reg2
* 0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
*
* 2) addl foo@gotntpoff(%reg1), %reg2
* 0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
*
* Transitions IE -> LE
*
* 1) movl $foo@ntpoff, %reg2
* 0xc7, 0xc0 | reg2, foo@ntpoff
*
* 2) addl $foo@ntpoff, %reg2
* 0x81, 0xc0 | reg2, foo@ntpoff
*
* Note: reg1 != 4 (%esp)
*/
offset -= 2;
if (offset[0] == 0x8b) {
/* case 1 above */
return (FIX_RELOC);
}
if (offset[0] == 0x03) {
/* case 2 above */
return (FIX_RELOC);
}
/*
* Unexpected instruction sequence - fatal error.
*/
return (FIX_ERROR);
case R_386_TLS_IE:
/*
* These transitions are a little different than the
* others, in that we could have multiple instructions
* pointed to by a single relocation. Depending upon the
* instruction, we perform a different code transition.
*
* Here's the known transitions:
* 1) movl foo@indntpoff, %eax
* 0xa1, foo@indntpoff
*
* 2) movl foo@indntpoff, %eax
* 0x8b, 0x05 | (reg << 3), foo@gotntpoff
*
* 3) addl foo@indntpoff, %eax
* 0x03, 0x05 | (reg << 3), foo@gotntpoff
*
* Transitions IE -> LE
*
* 1) movl $foo@ntpoff, %eax
* 0xb8, foo@ntpoff
*
* 2) movl $foo@ntpoff, %reg
* 0xc7, 0xc0 | reg, foo@ntpoff
*
* 3) addl $foo@ntpoff, %reg
* 0x81, 0xc0 | reg, foo@ntpoff
*/
offset--;
if (offset[0] == 0xa1) {
/* case 1 above */
return (FIX_RELOC);
}
offset--;
if (offset[0] == 0x8b) {
/* case 2 above */
return (FIX_RELOC);
}
if (offset[0] == 0x03) {
/* case 3 above */
return (FIX_RELOC);
}
/*
* Unexpected instruction sequence - fatal error.
*/
return (FIX_ERROR);
}
return (FIX_RELOC);
}
{
/*
* Process active relocations.
*/
/* LINTED */
const char *ifl_name;
int moved = 0;
/*
* If the section this relocation is against has been
* discarded (-zignore), then discard (skip) the
* relocation itself.
*/
(FLG_REL_GOT | FLG_REL_BSS |
FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
continue;
}
/*
* We deteremine what the 'got reference'
* model (if required) is at this point. This
* needs to be done before tls_fixup() since
* it may 'transition' our instructions.
*
* The got table entries have already been assigned,
* and we bind to those initial entries.
*/
else
/*
* Perform any required TLS fixups.
*/
return (S_ERROR);
continue;
}
/*
* If this is a relocation against a move table, or
* expanded move table, adjust the relocation entries.
*/
value = 0;
STT_SECTION) {
/*
* The value for a symbol pointing to a SECTION
* is based off of that sections position.
*
* The second argument of the ld_am_I_partial()
* is the value stored at the target address
* relocation is going to be applied.
*/
/* LINTED */
((uchar_t *)
arsp->rel_roffset)))) {
/*
* If the symbol is moved,
* adjust the value
*/
moved = 1;
} else {
}
} else {
/*
* else the value is the symbols value
*/
}
/*
* Relocation against the GLOBAL_OFFSET_TABLE.
*/
/*
* If loadable and not producing a relocatable object
* add the sections virtual address to the reference
* address.
*/
((flags & FLG_OF_RELOBJ) == 0))
/*
* If this entry has a PLT assigned to it, it's
* value is actually the address of the PLT (and
* not the address of the function).
*/
}
/*
* Determine whether the value needs further adjustment.
* Filter through the attributes of the relocation to
* determine what adjustment is required. Note, many
* of the following cases are only applicable when a
* .got is present. As a .got is not generated when a
* relocatable object is being built, any adjustments
* that require a .got need to be skipped.
*/
((flags & FLG_OF_RELOBJ) == 0)) {
/*
* Perform relocation against GOT table. Since
* this doesn't fit exactly into a relocation
* we place the appropriate byte in the GOT
* directly
*
* Calculate offset into GOT at which to apply
* the relocation.
*/
ofl, 0);
else
/*
* Add the GOTs data's offset.
*/
/*
* And do it.
*/
continue;
((flags & FLG_OF_RELOBJ) == 0)) {
((flags & FLG_OF_RELOBJ) == 0)) {
value =
(((flags & FLG_OF_RELOBJ) == 0) ||
((flags & FLG_OF_RELOBJ) == 0)) {
ofl, 0);
value +=
}
((flags & FLG_OF_RELOBJ) == 0)) {
GOT_REF_GENERIC, ofl, 0);
((flags & FLG_OF_RELOBJ) == 0)) {
/*
* This is the LE TLS reference model. Static
* offset is hard-coded.
*/
/*
* Since this code is fixed up, it assumes a
* negative offset that can be added to the
* thread pointer.
*/
}
else
/*
* Make sure we have data to relocate. Compiler and
* assembler developers have been known to generate
* relocations against invalid sections (normally .bss),
* so for their benefit give them sufficient information
* to help analyze the problem. End users should never
* see this.
*/
return (S_ERROR);
}
/*
* Get the address of the data item we need to modify.
*/
is_indata));
int class;
else
class = ERR_WARNING;
continue;
}
}
/*
* The relocation is additive. Ignore the previous
* symbol value if this local partial symbol is
* expanded.
*/
if (moved)
/*
* If '-z noreloc' is specified - skip the do_reloc
* stage.
*/
if ((flags & FLG_OF_RELOBJ) ||
!(dtflags1 & DF_1_NORELOC)) {
}
}
}
return (return_code);
}
/*
* Add an output relocation record.
*/
{
/*
* Static executables *do not* want any relocations against them.
* Since our engine still creates relocations against a WEAK UNDEFINED
* symbol in a static executable, it's best to disable them here
* instead of through out the relocation code.
*/
(FLG_OF_STATIC | FLG_OF_EXEC))
return (1);
/*
* If no relocation cache structures are available allocate
* a new one and link it into the cache list.
*/
/*
* Output relocation numbers can vary considerably between
* building executables or shared objects (pic vs. non-pic),
* etc. But, they typically aren't very large, so for these
* objects use a standard bucket size. For building relocatable
* objects, typically there will be an output relocation for
* every input relocation.
*/
if (nextsize == 0) {
if (size > REL_HOIDESCNO)
else
} else
} else
return (S_ERROR);
/* LINTED */
/* LINTED */
}
/*
* If we are adding a output relocation against a section
* symbol (non-RELATIVE) then mark that section. These sections
* will be added to the .dynsym symbol table.
*/
((flags & FLG_REL_SCNNDX) ||
/*
* If this is a COMMON symbol - no output section
* exists yet - (it's created as part of sym_validate()).
* So - we mark here that when it's created it should
* be tagged with the FLG_OS_OUTREL flag.
*/
else
} else {
ofl->ofl_dynshdrcnt++;
}
}
}
ofl->ofl_outrelscnt++;
if (flags & FLG_REL_GOT)
else if (flags & FLG_REL_PLT)
else if (flags & FLG_REL_BSS)
else if (flags & FLG_REL_NOINFO)
else
ofl->ofl_relocrelcnt++;
/*
* We don't perform sorting on PLT relocations because
* they have already been assigned a PLT index and if we
* were to sort them we would have to re-assign the plt indexes.
*/
if (!(flags & FLG_REL_PLT))
ofl->ofl_reloccnt++;
/*
* Insure a GLOBAL_OFFSET_TABLE is generated if required.
*/
/*
* Identify and possibly warn of a displacement relocation.
*/
}
return (1);
}
/*
* Stub routine since register symbols are not supported on i386.
*/
/* ARGSUSED */
{
return (S_ERROR);
}
/*
* process relocation for a LOCAL symbol
*/
{
/*
* if ((shared object) and (not pc relative relocation) and
* (not against ABS symbol))
* then
* build R_386_RELATIVE
* fi
*/
return (S_ERROR);
}
/*
* If the relocation is against a 'non-allocatable' section
* and we can not resolve it now - then give a warning
* message.
*
* We can not resolve the symbol if either:
* a) it's undefined
* b) it's defined in a shared library and a
* COPY relocation hasn't moved it to the executable
*
* Note: because we process all of the relocations against the
* text segment before any others - we know whether
* or not a copy relocation will be generated before
* we get here (see reloc_init()->reloc_segments()).
*/
/*
* If the relocation is against a SHT_SUNW_ANNOTATE
* section - then silently ignore that the relocation
* can not be resolved.
*/
if (rsp->rel_osdesc &&
return (0);
return (1);
}
/*
* Perform relocation.
*/
}
/* ARGSUSED */
{
/*
* Stub routine for common code compatibility, we shouldn't
* actually get here on x86.
*/
assert(0);
return (S_ERROR);
}
{
/*
* all TLS relocations are illegal in a static executable.
*/
(FLG_OF_STATIC | FLG_OF_EXEC)) {
return (S_ERROR);
}
/*
* Any TLS relocation must be against a STT_TLS symbol, all others
* are illegal.
*/
return (S_ERROR);
}
/*
* We're a executable - use either the IE or LE
* access model.
*/
if (flags & FLG_OF_EXEC) {
/*
* If we are using either IE or LE reference
* model set the DF_STATIC_TLS flag.
*/
if (!local) {
/*
* IE access model
*/
/*
* It's not possible for LD or LE reference
* models to reference a symbol external to
* the current object.
*/
return (S_ERROR);
}
/*
* Assign a GOT entry for static TLS references
*/
return (S_ERROR);
return (S_ERROR);
}
/*
* If (GD or LD) reference models - fixups
* are required.
*/
}
/*
* LE access model
*/
}
/*
* Building a shared object
*/
/*
* Building a shared object - only GD & LD access models
* will work here.
*/
return (S_ERROR);
}
/*
* LD access mode can only bind to local symbols.
*/
return (S_ERROR);
}
GOT_REF_TLSLD, ofl, 0)) == 0)) {
return (S_ERROR);
if (local)
rflags |= FLG_REL_SCNNDX;
return (S_ERROR);
ofl, 0)) == 0)) {
return (S_ERROR);
if (local)
rflags |= FLG_REL_SCNNDX;
return (S_ERROR);
return (S_ERROR);
} else {
return (S_ERROR);
}
}
/*
* cause a call to __tls_get_addr(). Let's convert this
* relocation to that symbol now, and prepare for the PLT magic.
*/
return (S_ERROR);
return (S_ERROR);
return (1);
}
}
/* ARGSUSED3 */
Gotndx *
{
return (ofl->ofl_tlsldgotndx);
return (gnp);
}
return ((Gotndx *)0);
}
{
else
gotndx++;
}
/* ARGSUSED4 */
{
if (pgnp)
return (1);
gotents = 2;
else
gotents = 1;
return (S_ERROR);
if (gref == GOT_REF_TLSLD) {
return (1);
}
return (S_ERROR);
return (1);
}
void
{
}
/*
* Initializes .got[0] with the _DYNAMIC symbol value.
*/
{
(M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
/* LINTED */
}
}
/*
* Fill in the reserved slot in the procedure linkage table the first
* entry is:
* if (building a.out) {
* PUSHL got[1] # the address of the link map entry
* JMP * got[2] # the address of rtbinder
* } else {
* PUSHL got[1]@GOT(%ebx) # the address of the link map entry
* JMP * got[2]@GOT(%ebx) # the address of rtbinder
* }
*/
pltent[0] = M_SPECIAL_INST;
pltent += 2;
/* LINTED */
pltent += 4;
pltent[0] = M_SPECIAL_INST;
pltent += 2;
/* LINTED */
} else {
pltent[0] = M_SPECIAL_INST;
pltent += 2;
/* LINTED */
pltent += 4;
pltent[0] = M_SPECIAL_INST;
pltent += 2;
/* LINTED */
}
}
return (1);
}