machrel.c revision 54d82594cac34899a52710db0b8235a171e83e31
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2005 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 <strings.h>
#include <sys/elf_amd64.h>
#include <debug.h>
#include <reloc.h>
#include <msg.h>
#include <_libld.h>
{
/* LINTED */
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
{
if (ofl->ofl_pltcnt) {
(*dyn)++;
}
}
}
{
return (value);
}
/*
* Build a single plt entry - code is:
* JMP *name1@GOTPCREL(%rip)
* PUSHL $index
* JMP .PLT0
*/
/* 0x00 jmpq *name1@GOTPCREL(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
/* 0x06 pushq $index */ 0x68, 0x00, 0x00, 0x00, 0x00,
/* 0x0b jmpq .plt0(%rip) */ 0xe9, 0x00, 0x00, 0x00, 0x00
/* 0x10 */
};
static uintptr_t
{
/*
* Fill in the got entry with the address of the next instruction.
*/
/* LINTED */
/*
* patchup:
* jmpq *name1@gotpcrel(%rip)
*
* NOTE: 0x06 represents next instruction.
*/
/*
* If '-z noreloc' is specified - skip the do_reloc
* stage.
*/
if ((flags & FLG_OF_RELOBJ) ||
!(dtflags1 & DF_1_NORELOC)) {
MSG_ORIG(MSG_SPECFIL_PLTENT)) == 0) {
return (S_ERROR);
}
}
/*
* patchup:
* pushq $pltndx
*/
/*
* If '-z noreloc' is specified - skip the do_reloc
* stage.
*/
if ((flags & FLG_OF_RELOBJ) ||
!(dtflags1 & DF_1_NORELOC)) {
MSG_ORIG(MSG_SPECFIL_PLTENT)) == 0) {
return (S_ERROR);
}
}
/*
* patchup:
* jmpq .plt0(%rip)
* NOTE: 0x10 represents next instruction. The rather complex series
* of casts is necessary to sign extend an offset into a 64-bit value
* while satisfying various compiler error checks. Handle with care.
*/
/*
* If '-z noreloc' is specified - skip the do_reloc
* stage.
*/
if ((flags & FLG_OF_RELOBJ) ||
!(dtflags1 & DF_1_NORELOC)) {
MSG_ORIG(MSG_SPECFIL_PLTENT)) == 0) {
return (S_ERROR);
}
}
return (1);
}
{
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 then we need to adjust the
* raddend field to compensate for the new position of the input section
* within the new output section.
*/
/* LINTED */
sectmoved = 1;
else
/* LINTED */
raddend +=
} else {
/* LINTED */
raddend +=
}
}
/*
* Note: for GOT relative relocations on amd64
* we discard the addend. It was relevant
* to the reference - not to the data item
* being referenced (ie: that -4 thing).
*/
raddend = 0;
/*
* Note that relocations for PLT's actually
* cause a relocation againt the GOT.
*/
raddend = 0;
return (S_ERROR);
/*
* This must be a R_AMD64_COPY. For these set the roffset to
* point to the new symbols location.
*/
/*
* The raddend doesn't mean anything in a R_SPARC_COPY
* relocation. Null it out because it can confuse people.
*/
raddend = 0;
} 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
/*
* Add the symbols 'value' to the addend field.
*/
/*
* addend field for R_AMD64_DTPMOD64 means nothing. The addend
* is propogated in the corresponding R_AMD64_DTPOFF64
* relocation.
*/
raddend = 0;
/*
* 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_AMD64_JUMP_SLOT) actually
* result in modifications to the .got.
*/
return (1);
}
/*
* amd64 Instructions for TLS processing
*/
static uchar_t tlsinstr_gd_ie[] = {
/*
* 0x00 movq %fs:0, %rax
*/
0x64, 0x48, 0x8b, 0x04, 0x25,
0x00, 0x00, 0x00, 0x00,
/*
* 0x09 addq x@gottpoff(%rip), %rax
*/
0x48, 0x03, 0x05, 0x00, 0x00,
0x00, 0x00
};
static uchar_t tlsinstr_gd_le[] = {
/*
* 0x00 movq %fs:0, %rax
*/
0x64, 0x48, 0x8b, 0x04, 0x25,
0x00, 0x00, 0x00, 0x00,
/*
* 0x09 leaq x@gottpoff(%rip), %rax
*/
0x48, 0x8d, 0x80, 0x00, 0x00,
0x00, 0x00
};
static uchar_t tlsinstr_ld_le[] = {
/*
* .byte 0x66
*/
0x66,
/*
* .byte 0x66
*/
0x66,
/*
* .byte 0x66
*/
0x66,
/*
* movq %fs:0, %rax
*/
0x64, 0x48, 0x8b, 0x04, 0x25,
0x00, 0x00, 0x00, 0x00
};
{
/*
* IE reference model
*/
switch (rtype) {
case R_AMD64_TLSGD:
/*
* GD -> IE
*
* Transition:
* 0x00 .byte 0x66
* 0x01 leaq x@tlsgd(%rip), %rdi
* 0x08 .word 0x6666
* 0x0a rex64
* 0x0b call __tls_get_addr@plt
* 0x10
* To:
* 0x00 movq %fs:0, %rax
* 0x09 addq x@gottpoff(%rip), %rax
* 0x10
*/
/*
* Addjust 'offset' to beginning of instruction
* sequence.
*/
offset -= 4;
sizeof (tlsinstr_gd_ie));
return (FIX_RELOC);
case R_AMD64_PLT32:
/*
* Fixup done via the TLS_GD relocation
*/
return (FIX_DONE);
}
}
/*
* LE reference model
*/
switch (rtype) {
case R_AMD64_TLSGD:
/*
* GD -> LE
*
* Transition:
* 0x00 .byte 0x66
* 0x01 leaq x@tlsgd(%rip), %rdi
* 0x08 .word 0x6666
* 0x0a rex64
* 0x0b call __tls_get_addr@plt
* 0x10
* To:
* 0x00 movq %fs:0, %rax
* 0x09 leaq x@tpoff(%rax), %rax
* 0x10
*/
arsp->rel_raddend = 0;
/*
* Addjust 'offset' to beginning of instruction
* sequence.
*/
offset -= 4;
sizeof (tlsinstr_gd_le));
return (FIX_RELOC);
case R_AMD64_GOTTPOFF:
/*
* IE -> LE
*
* Transition:
* 0x00 movq %fs:0, %rax
* 0x09 addq x@gottopoff(%rip), %rax
* 0x10
* To:
* 0x00 movq %fs:0, %rax
* 0x09 leaq x@tpoff(%rax), %rax
* 0x10
*/
arsp->rel_raddend = 0;
/*
* Addjust 'offset' to beginning of instruction
* sequence.
*/
offset -= 12;
/*
* Same code sequence used in the GD -> LE
* transition.
*/
sizeof (tlsinstr_gd_le));
return (FIX_RELOC);
case R_AMD64_TLSLD:
/*
* LD -> LE
*
* Transition
* 0x00 leaq x1@tlsgd(%rip), %rdi
* 0x07 call __tls_get_addr@plt
* 0x0c
* To:
* 0x00 .byte 0x66
* 0x01 .byte 0x66
* 0x02 .byte 0x66
* 0x03 movq %fs:0, %rax
*/
offset -= 3;
sizeof (tlsinstr_ld_le));
return (FIX_DONE);
case R_AMD64_DTPOFF32:
/*
* LD->LE
*
* Transition:
* 0x00 leaq x1@dtpoff(%rax), %rcx
* To:
* 0x00 leaq x1@tpoff(%rax), %rcx
*/
arsp->rel_raddend = 0;
return (FIX_RELOC);
}
return (FIX_RELOC);
}
{
/*
* process active relocs
*/
/* 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 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))
/*
* 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).
*/
}
/*
* Add relocations addend to value. Add extra
* relocation addend if needed.
*
* Note: for GOT relative relocations on amd64
* we discard the addend. It was relevant
* to the reference - not to the data item
* being referenced (ie: that -4 thing).
*/
/*
* 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.
*/
else
/*
* Add the GOTs data's offset.
*/
/*
* And do it.
*/
continue;
/*
* Calculation:
* G + GOT + A - P
*/
/*
* This is the LE TLS reference model. Static
* offset is hard-coded.
*/
/*
* Since this code is fixedup
* 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));
/* LINTED */
arsp->rel_osdesc));
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)) {
ifl_name) == 0)
}
}
}
return (return_code);
}
{
/*
* 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++;
/*
* Identify and possibly warn of a displacement relocation.
*/
}
return (1);
}
{
/*
* If no relocation cache structures are available allocate a
* new one and link it into the bucket list.
*/
/*
* Typically, when generating an executable or shared object
* there will be a active relocation for every input relocation.
*/
if (nextsize == 0) {
if (size > REL_HAIDESCNO)
else
} else
} else
return (S_ERROR);
/* LINTED */
/* LINTED */
}
ofl->ofl_actrelscnt++;
/*
* If this is a displacement relocation relocation, warn.
*/
}
return (1);
}
/*
* Stub routine since register symbols are not supported on amd64.
*/
/* 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_AMD64_RELATIVE
* fi
*/
/*
* R_AMD64_RELATIVE updates a 64bit address, if this
* relocation isn't a 64bit binding then we can not
* simplify it to a RELATIVE relocation.
*/
}
return (S_ERROR);
return (1);
}
/*
* 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 amd64.
*/
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);
}
return (S_ERROR);
if (local)
rflags |= FLG_REL_SCNNDX;
return (S_ERROR);
return (S_ERROR);
if (local)
rflags |= FLG_REL_SCNNDX;
return (S_ERROR);
return (S_ERROR);
} else {
return (S_ERROR);
}
}
}
{
/*
* Try to determine if we can do any relocations at
* this point. We can if:
*
* (local_symbol) and (non_GOT_relocation) and
* (IS_PC_RELATIVE()) and
* (relocation to symbol in same section)
*/
}
/*
* If '-zredlocsym' is in effect make all local sym relocations
* against the 'section symbols', since they are the only symbols
* which will be added to the .symtab.
*/
/*
* But if this is a PIC code, don't allow it for now.
*/
return (S_ERROR);
}
}
}
/* ARGSUSED3 */
Gotndx *
{
return (ofl->ofl_tlsldgotndx);
return (gnp);
}
}
return ((Gotndx *)0);
}
{
else
gotndx++;
}
/* ARGSUSED5 */
{
return (1);
gotents = 2;
else
gotents = 1;
plnp = 0;
break;
}
/*
* Allocate a new entry.
*/
return (S_ERROR);
if (gref == GOT_REF_TLSLD) {
return (1);
}
if (plnp == 0) {
/*
* Insert at head of list
*/
return (S_ERROR);
/*
* Insert in middle of lest
*/
return (S_ERROR);
} else {
/*
* Append to tail of list
*/
return (S_ERROR);
}
return (1);
}
void
{
}
/* 0x00 PUSHQ GOT+8(%rip) */ 0xff, 0x35, 0x00, 0x00, 0x00, 0x00,
/* 0x06 JMP *GOT+16(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00,
/* 0x0c NOP */ 0x90,
/* 0x0d NOP */ 0x90,
/* 0x0e NOP */ 0x90,
/* 0x0f NOP */ 0x90
};
/*
* 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:
* 0x00 PUSHQ GOT+8(%rip) # GOT[1]
* 0x06 JMP *GOT+16(%rip) # GOT[2]
* 0x0c NOP
* 0x0d NOP
* 0x0e NOP
* 0x0f NOP
*/
/*
* filin:
* PUSHQ GOT + 8(%rip)
*
* Note: 0x06 below represents the offset to the
* next instruction - which is what %rip will
* be pointing at.
*/
(M_GOT_XLINKMAP * M_GOT_ENTSIZE) -
/*
* If '-z noreloc' is specified - skip the do_reloc
* stage.
*/
if ((flags & FLG_OF_RELOBJ) ||
!(dtflags1 & DF_1_NORELOC)) {
MSG_ORIG(MSG_SPECFIL_PLTENT)) == 0) {
return (S_ERROR);
}
}
/*
* filin:
* JMP *GOT+16(%rip)
*/
(M_GOT_XRTLD * M_GOT_ENTSIZE) -
/*
* If '-z noreloc' is specified - skip the do_reloc
* stage.
*/
if ((flags & FLG_OF_RELOBJ) ||
!(dtflags1 & DF_1_NORELOC)) {
MSG_ORIG(MSG_SPECFIL_PLTENT)) == 0) {
return (S_ERROR);
}
}
}
return (1);
}
/*
* Return got[0].
*/
{
else
return (0);
}