Blame sysdeps/sparc/sparc64/dl-plt.h

Packit 6c4009
/* PLT fixups.  Sparc 64-bit version.
Packit 6c4009
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _DL_PLT_H
Packit 6c4009
#define _DL_PLT_H
Packit 6c4009
Packit 6c4009
/* We have 4 cases to handle.  And we code different code sequences
Packit 6c4009
   for each one.  I love V9 code models...  */
Packit 6c4009
static inline void __attribute__ ((always_inline))
Packit 6c4009
sparc64_fixup_plt (struct link_map *map, const Elf64_Rela *reloc,
Packit 6c4009
		   Elf64_Addr *reloc_addr, Elf64_Addr value,
Packit 6c4009
		   Elf64_Addr high, int t)
Packit 6c4009
{
Packit 6c4009
  unsigned int *insns = (unsigned int *) reloc_addr;
Packit 6c4009
  Elf64_Addr plt_vaddr = (Elf64_Addr) reloc_addr;
Packit 6c4009
  Elf64_Sxword disp = value - plt_vaddr;
Packit 6c4009
Packit 6c4009
  /* 't' is '0' if we are resolving this PLT entry for RTLD bootstrap,
Packit 6c4009
     in which case we'll be resolving all PLT entries and thus can
Packit 6c4009
     optimize by overwriting instructions starting at the first PLT entry
Packit 6c4009
     instruction and we need not be mindful of thread safety.
Packit 6c4009
Packit 6c4009
     Otherwise, 't' is '1'.
Packit 6c4009
Packit 6c4009
     Now move plt_vaddr up to the call instruction.  */
Packit 6c4009
  plt_vaddr += ((t + 1) * 4);
Packit 6c4009
Packit 6c4009
  /* PLT entries .PLT32768 and above look always the same.  */
Packit 6c4009
  if (__builtin_expect (high, 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      *reloc_addr = value - map->l_addr;
Packit 6c4009
    }
Packit 6c4009
  /* Near destination.  */
Packit 6c4009
  else if (disp >= -0x800000 && disp < 0x800000)
Packit 6c4009
    {
Packit 6c4009
      unsigned int insn;
Packit 6c4009
Packit 6c4009
      /* ba,a */
Packit 6c4009
      insn = 0x30800000 | ((disp >> 2) & 0x3fffff);
Packit 6c4009
Packit 6c4009
      if (disp >= -0x100000 && disp < 0x100000)
Packit 6c4009
	{
Packit 6c4009
	  /* ba,a,pt %icc */
Packit 6c4009
	  insn = 0x30480000  | ((disp >> 2) & 0x07ffff);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* As this is just one instruction, it is thread safe and so we
Packit 6c4009
	 can avoid the unnecessary sethi FOO, %g1.  Each 64-bit PLT
Packit 6c4009
	 entry is 8 instructions long, so we can't run into the 'jmp'
Packit 6c4009
	 delay slot problems 32-bit PLTs can.  */
Packit 6c4009
      insns[0] = insn;
Packit 6c4009
      __asm __volatile ("flush %0" : : "r" (insns));
Packit 6c4009
    }
Packit 6c4009
  /* 32-bit Sparc style, the target is in the lower 32-bits of
Packit 6c4009
     address space.  */
Packit 6c4009
  else if (insns += t, (value >> 32) == 0)
Packit 6c4009
    {
Packit 6c4009
      /* sethi	%hi(target), %g1
Packit 6c4009
	 jmpl	%g1 + %lo(target), %g0  */
Packit 6c4009
Packit 6c4009
      insns[1] = 0x81c06000 | (value & 0x3ff);
Packit 6c4009
      __asm __volatile ("flush %0 + 4" : : "r" (insns));
Packit 6c4009
Packit 6c4009
      insns[0] = 0x03000000 | ((unsigned int)(value >> 10));
Packit 6c4009
      __asm __volatile ("flush %0" : : "r" (insns));
Packit 6c4009
    }
Packit 6c4009
  /* We can also get somewhat simple sequences if the distance between
Packit 6c4009
     the target and the PLT entry is within +/- 2GB.  */
Packit 6c4009
  else if ((plt_vaddr > value
Packit 6c4009
	    && ((plt_vaddr - value) >> 31) == 0)
Packit 6c4009
	   || (value > plt_vaddr
Packit 6c4009
	       && ((value - plt_vaddr) >> 31) == 0))
Packit 6c4009
    {
Packit 6c4009
      unsigned int displacement;
Packit 6c4009
Packit 6c4009
      if (plt_vaddr > value)
Packit 6c4009
	displacement = (0 - (plt_vaddr - value));
Packit 6c4009
      else
Packit 6c4009
	displacement = value - plt_vaddr;
Packit 6c4009
Packit 6c4009
      /* mov	%o7, %g1
Packit 6c4009
	 call	displacement
Packit 6c4009
	  mov	%g1, %o7  */
Packit 6c4009
Packit 6c4009
      insns[2] = 0x9e100001;
Packit 6c4009
      __asm __volatile ("flush %0 + 8" : : "r" (insns));
Packit 6c4009
Packit 6c4009
      insns[1] = 0x40000000 | (displacement >> 2);
Packit 6c4009
      __asm __volatile ("flush %0 + 4" : : "r" (insns));
Packit 6c4009
Packit 6c4009
      insns[0] = 0x8210000f;
Packit 6c4009
      __asm __volatile ("flush %0" : : "r" (insns));
Packit 6c4009
    }
Packit 6c4009
  /* Worst case, ho hum...  */
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      unsigned int high32 = (value >> 32);
Packit 6c4009
      unsigned int low32 = (unsigned int) value;
Packit 6c4009
Packit 6c4009
      /* ??? Some tricks can be stolen from the sparc64 egcs backend
Packit 6c4009
	     constant formation code I wrote.  -DaveM  */
Packit 6c4009
Packit 6c4009
      if (__glibc_unlikely (high32 & 0x3ff))
Packit 6c4009
	{
Packit 6c4009
	  /* sethi	%hh(value), %g1
Packit 6c4009
	     sethi	%lm(value), %g5
Packit 6c4009
	     or		%g1, %hm(value), %g1
Packit 6c4009
	     or		%g5, %lo(value), %g5
Packit 6c4009
	     sllx	%g1, 32, %g1
Packit 6c4009
	     jmpl	%g1 + %g5, %g0
Packit 6c4009
	      nop  */
Packit 6c4009
Packit 6c4009
	  insns[5] = 0x81c04005;
Packit 6c4009
	  __asm __volatile ("flush %0 + 20" : : "r" (insns));
Packit 6c4009
Packit 6c4009
	  insns[4] = 0x83287020;
Packit 6c4009
	  __asm __volatile ("flush %0 + 16" : : "r" (insns));
Packit 6c4009
Packit 6c4009
	  insns[3] = 0x8a116000 | (low32 & 0x3ff);
Packit 6c4009
	  __asm __volatile ("flush %0 + 12" : : "r" (insns));
Packit 6c4009
Packit 6c4009
	  insns[2] = 0x82106000 | (high32 & 0x3ff);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* sethi	%hh(value), %g1
Packit 6c4009
	     sethi	%lm(value), %g5
Packit 6c4009
	     sllx	%g1, 32, %g1
Packit 6c4009
	     or		%g5, %lo(value), %g5
Packit 6c4009
	     jmpl	%g1 + %g5, %g0
Packit 6c4009
	      nop  */
Packit 6c4009
Packit 6c4009
	  insns[4] = 0x81c04005;
Packit 6c4009
	  __asm __volatile ("flush %0 + 16" : : "r" (insns));
Packit 6c4009
Packit 6c4009
	  insns[3] = 0x8a116000 | (low32 & 0x3ff);
Packit 6c4009
	  __asm __volatile ("flush %0 + 12" : : "r" (insns));
Packit 6c4009
Packit 6c4009
	  insns[2] = 0x83287020;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      __asm __volatile ("flush %0 + 8" : : "r" (insns));
Packit 6c4009
Packit 6c4009
      insns[1] = 0x0b000000 | (low32 >> 10);
Packit 6c4009
      __asm __volatile ("flush %0 + 4" : : "r" (insns));
Packit 6c4009
Packit 6c4009
      insns[0] = 0x03000000 | (high32 >> 10);
Packit 6c4009
      __asm __volatile ("flush %0" : : "r" (insns));
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif /* dl-plt.h */