Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* Intel 80386/80486-specific support for 32-bit ELF
  2.    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
  3.    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
  4.    Free Software Foundation, Inc.
  5.  
  6.    This file is part of BFD, the Binary File Descriptor library.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 3 of the License, or
  11.    (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  21.    MA 02110-1301, USA.  */
  22.  
  23. #include "sysdep.h"
  24. #include "bfd.h"
  25. #include "bfdlink.h"
  26. #include "libbfd.h"
  27. #include "elf-bfd.h"
  28. #include "elf-nacl.h"
  29. #include "elf-vxworks.h"
  30. #include "bfd_stdint.h"
  31. #include "objalloc.h"
  32. #include "hashtab.h"
  33. #include "dwarf2.h"
  34.  
  35. /* 386 uses REL relocations instead of RELA.  */
  36. #define USE_REL 1
  37.  
  38. #include "elf/i386.h"
  39.  
  40. static reloc_howto_type elf_howto_table[]=
  41. {
  42.   HOWTO(R_386_NONE, 0, 0, 0, FALSE, 0, complain_overflow_bitfield,
  43.         bfd_elf_generic_reloc, "R_386_NONE",
  44.         TRUE, 0x00000000, 0x00000000, FALSE),
  45.   HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  46.         bfd_elf_generic_reloc, "R_386_32",
  47.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  48.   HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
  49.         bfd_elf_generic_reloc, "R_386_PC32",
  50.         TRUE, 0xffffffff, 0xffffffff, TRUE),
  51.   HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  52.         bfd_elf_generic_reloc, "R_386_GOT32",
  53.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  54.   HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
  55.         bfd_elf_generic_reloc, "R_386_PLT32",
  56.         TRUE, 0xffffffff, 0xffffffff, TRUE),
  57.   HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  58.         bfd_elf_generic_reloc, "R_386_COPY",
  59.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  60.   HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  61.         bfd_elf_generic_reloc, "R_386_GLOB_DAT",
  62.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  63.   HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  64.         bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
  65.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  66.   HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  67.         bfd_elf_generic_reloc, "R_386_RELATIVE",
  68.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  69.   HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  70.         bfd_elf_generic_reloc, "R_386_GOTOFF",
  71.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  72.   HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
  73.         bfd_elf_generic_reloc, "R_386_GOTPC",
  74.         TRUE, 0xffffffff, 0xffffffff, TRUE),
  75.  
  76.   /* We have a gap in the reloc numbers here.
  77.      R_386_standard counts the number up to this point, and
  78.      R_386_ext_offset is the value to subtract from a reloc type of
  79.      R_386_16 thru R_386_PC8 to form an index into this table.  */
  80. #define R_386_standard (R_386_GOTPC + 1)
  81. #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
  82.  
  83.   /* These relocs are a GNU extension.  */
  84.   HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  85.         bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
  86.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  87.   HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  88.         bfd_elf_generic_reloc, "R_386_TLS_IE",
  89.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  90.   HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  91.         bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
  92.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  93.   HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  94.         bfd_elf_generic_reloc, "R_386_TLS_LE",
  95.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  96.   HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  97.         bfd_elf_generic_reloc, "R_386_TLS_GD",
  98.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  99.   HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  100.         bfd_elf_generic_reloc, "R_386_TLS_LDM",
  101.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  102.   HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
  103.         bfd_elf_generic_reloc, "R_386_16",
  104.         TRUE, 0xffff, 0xffff, FALSE),
  105.   HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
  106.         bfd_elf_generic_reloc, "R_386_PC16",
  107.         TRUE, 0xffff, 0xffff, TRUE),
  108.   HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
  109.         bfd_elf_generic_reloc, "R_386_8",
  110.         TRUE, 0xff, 0xff, FALSE),
  111.   HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
  112.         bfd_elf_generic_reloc, "R_386_PC8",
  113.         TRUE, 0xff, 0xff, TRUE),
  114.  
  115. #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
  116. #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
  117.   /* These are common with Solaris TLS implementation.  */
  118.   HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  119.         bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
  120.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  121.   HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  122.         bfd_elf_generic_reloc, "R_386_TLS_IE_32",
  123.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  124.   HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  125.         bfd_elf_generic_reloc, "R_386_TLS_LE_32",
  126.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  127.   HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  128.         bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
  129.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  130.   HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  131.         bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
  132.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  133.   HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  134.         bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
  135.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  136.   HOWTO(R_386_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
  137.         bfd_elf_generic_reloc, "R_386_SIZE32",
  138.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  139.   HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  140.         bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
  141.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  142.   HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
  143.         bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
  144.         FALSE, 0, 0, FALSE),
  145.   HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  146.         bfd_elf_generic_reloc, "R_386_TLS_DESC",
  147.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  148.   HOWTO(R_386_IRELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
  149.         bfd_elf_generic_reloc, "R_386_IRELATIVE",
  150.         TRUE, 0xffffffff, 0xffffffff, FALSE),
  151.  
  152.   /* Another gap.  */
  153. #define R_386_irelative (R_386_IRELATIVE + 1 - R_386_tls_offset)
  154. #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_irelative)
  155.  
  156. /* GNU extension to record C++ vtable hierarchy.  */
  157.   HOWTO (R_386_GNU_VTINHERIT,   /* type */
  158.          0,                     /* rightshift */
  159.          2,                     /* size (0 = byte, 1 = short, 2 = long) */
  160.          0,                     /* bitsize */
  161.          FALSE,                 /* pc_relative */
  162.          0,                     /* bitpos */
  163.          complain_overflow_dont, /* complain_on_overflow */
  164.          NULL,                  /* special_function */
  165.          "R_386_GNU_VTINHERIT", /* name */
  166.          FALSE,                 /* partial_inplace */
  167.          0,                     /* src_mask */
  168.          0,                     /* dst_mask */
  169.          FALSE),                /* pcrel_offset */
  170.  
  171. /* GNU extension to record C++ vtable member usage.  */
  172.   HOWTO (R_386_GNU_VTENTRY,     /* type */
  173.          0,                     /* rightshift */
  174.          2,                     /* size (0 = byte, 1 = short, 2 = long) */
  175.          0,                     /* bitsize */
  176.          FALSE,                 /* pc_relative */
  177.          0,                     /* bitpos */
  178.          complain_overflow_dont, /* complain_on_overflow */
  179.          _bfd_elf_rel_vtable_reloc_fn, /* special_function */
  180.          "R_386_GNU_VTENTRY",   /* name */
  181.          FALSE,                 /* partial_inplace */
  182.          0,                     /* src_mask */
  183.          0,                     /* dst_mask */
  184.          FALSE)                 /* pcrel_offset */
  185.  
  186. #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
  187.  
  188. };
  189.  
  190. #ifdef DEBUG_GEN_RELOC
  191. #define TRACE(str) \
  192.   fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
  193. #else
  194. #define TRACE(str)
  195. #endif
  196.  
  197. static reloc_howto_type *
  198. elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  199.                             bfd_reloc_code_real_type code)
  200. {
  201.   switch (code)
  202.     {
  203.     case BFD_RELOC_NONE:
  204.       TRACE ("BFD_RELOC_NONE");
  205.       return &elf_howto_table[R_386_NONE];
  206.  
  207.     case BFD_RELOC_32:
  208.       TRACE ("BFD_RELOC_32");
  209.       return &elf_howto_table[R_386_32];
  210.  
  211.     case BFD_RELOC_CTOR:
  212.       TRACE ("BFD_RELOC_CTOR");
  213.       return &elf_howto_table[R_386_32];
  214.  
  215.     case BFD_RELOC_32_PCREL:
  216.       TRACE ("BFD_RELOC_PC32");
  217.       return &elf_howto_table[R_386_PC32];
  218.  
  219.     case BFD_RELOC_386_GOT32:
  220.       TRACE ("BFD_RELOC_386_GOT32");
  221.       return &elf_howto_table[R_386_GOT32];
  222.  
  223.     case BFD_RELOC_386_PLT32:
  224.       TRACE ("BFD_RELOC_386_PLT32");
  225.       return &elf_howto_table[R_386_PLT32];
  226.  
  227.     case BFD_RELOC_386_COPY:
  228.       TRACE ("BFD_RELOC_386_COPY");
  229.       return &elf_howto_table[R_386_COPY];
  230.  
  231.     case BFD_RELOC_386_GLOB_DAT:
  232.       TRACE ("BFD_RELOC_386_GLOB_DAT");
  233.       return &elf_howto_table[R_386_GLOB_DAT];
  234.  
  235.     case BFD_RELOC_386_JUMP_SLOT:
  236.       TRACE ("BFD_RELOC_386_JUMP_SLOT");
  237.       return &elf_howto_table[R_386_JUMP_SLOT];
  238.  
  239.     case BFD_RELOC_386_RELATIVE:
  240.       TRACE ("BFD_RELOC_386_RELATIVE");
  241.       return &elf_howto_table[R_386_RELATIVE];
  242.  
  243.     case BFD_RELOC_386_GOTOFF:
  244.       TRACE ("BFD_RELOC_386_GOTOFF");
  245.       return &elf_howto_table[R_386_GOTOFF];
  246.  
  247.     case BFD_RELOC_386_GOTPC:
  248.       TRACE ("BFD_RELOC_386_GOTPC");
  249.       return &elf_howto_table[R_386_GOTPC];
  250.  
  251.       /* These relocs are a GNU extension.  */
  252.     case BFD_RELOC_386_TLS_TPOFF:
  253.       TRACE ("BFD_RELOC_386_TLS_TPOFF");
  254.       return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
  255.  
  256.     case BFD_RELOC_386_TLS_IE:
  257.       TRACE ("BFD_RELOC_386_TLS_IE");
  258.       return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
  259.  
  260.     case BFD_RELOC_386_TLS_GOTIE:
  261.       TRACE ("BFD_RELOC_386_TLS_GOTIE");
  262.       return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
  263.  
  264.     case BFD_RELOC_386_TLS_LE:
  265.       TRACE ("BFD_RELOC_386_TLS_LE");
  266.       return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
  267.  
  268.     case BFD_RELOC_386_TLS_GD:
  269.       TRACE ("BFD_RELOC_386_TLS_GD");
  270.       return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
  271.  
  272.     case BFD_RELOC_386_TLS_LDM:
  273.       TRACE ("BFD_RELOC_386_TLS_LDM");
  274.       return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
  275.  
  276.     case BFD_RELOC_16:
  277.       TRACE ("BFD_RELOC_16");
  278.       return &elf_howto_table[R_386_16 - R_386_ext_offset];
  279.  
  280.     case BFD_RELOC_16_PCREL:
  281.       TRACE ("BFD_RELOC_16_PCREL");
  282.       return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
  283.  
  284.     case BFD_RELOC_8:
  285.       TRACE ("BFD_RELOC_8");
  286.       return &elf_howto_table[R_386_8 - R_386_ext_offset];
  287.  
  288.     case BFD_RELOC_8_PCREL:
  289.       TRACE ("BFD_RELOC_8_PCREL");
  290.       return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
  291.  
  292.     /* Common with Sun TLS implementation.  */
  293.     case BFD_RELOC_386_TLS_LDO_32:
  294.       TRACE ("BFD_RELOC_386_TLS_LDO_32");
  295.       return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
  296.  
  297.     case BFD_RELOC_386_TLS_IE_32:
  298.       TRACE ("BFD_RELOC_386_TLS_IE_32");
  299.       return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
  300.  
  301.     case BFD_RELOC_386_TLS_LE_32:
  302.       TRACE ("BFD_RELOC_386_TLS_LE_32");
  303.       return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
  304.  
  305.     case BFD_RELOC_386_TLS_DTPMOD32:
  306.       TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
  307.       return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
  308.  
  309.     case BFD_RELOC_386_TLS_DTPOFF32:
  310.       TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
  311.       return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
  312.  
  313.     case BFD_RELOC_386_TLS_TPOFF32:
  314.       TRACE ("BFD_RELOC_386_TLS_TPOFF32");
  315.       return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
  316.  
  317.     case BFD_RELOC_SIZE32:
  318.       TRACE ("BFD_RELOC_SIZE32");
  319.       return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
  320.  
  321.     case BFD_RELOC_386_TLS_GOTDESC:
  322.       TRACE ("BFD_RELOC_386_TLS_GOTDESC");
  323.       return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
  324.  
  325.     case BFD_RELOC_386_TLS_DESC_CALL:
  326.       TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
  327.       return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
  328.  
  329.     case BFD_RELOC_386_TLS_DESC:
  330.       TRACE ("BFD_RELOC_386_TLS_DESC");
  331.       return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
  332.  
  333.     case BFD_RELOC_386_IRELATIVE:
  334.       TRACE ("BFD_RELOC_386_IRELATIVE");
  335.       return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
  336.  
  337.     case BFD_RELOC_VTABLE_INHERIT:
  338.       TRACE ("BFD_RELOC_VTABLE_INHERIT");
  339.       return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
  340.  
  341.     case BFD_RELOC_VTABLE_ENTRY:
  342.       TRACE ("BFD_RELOC_VTABLE_ENTRY");
  343.       return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
  344.  
  345.     default:
  346.       break;
  347.     }
  348.  
  349.   TRACE ("Unknown");
  350.   return 0;
  351. }
  352.  
  353. static reloc_howto_type *
  354. elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  355.                             const char *r_name)
  356. {
  357.   unsigned int i;
  358.  
  359.   for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
  360.     if (elf_howto_table[i].name != NULL
  361.         && strcasecmp (elf_howto_table[i].name, r_name) == 0)
  362.       return &elf_howto_table[i];
  363.  
  364.   return NULL;
  365. }
  366.  
  367. static reloc_howto_type *
  368. elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
  369. {
  370.   unsigned int indx;
  371.  
  372.   if ((indx = r_type) >= R_386_standard
  373.       && ((indx = r_type - R_386_ext_offset) - R_386_standard
  374.           >= R_386_ext - R_386_standard)
  375.       && ((indx = r_type - R_386_tls_offset) - R_386_ext
  376.           >= R_386_irelative - R_386_ext)
  377.       && ((indx = r_type - R_386_vt_offset) - R_386_irelative
  378.           >= R_386_vt - R_386_irelative))
  379.     {
  380.       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
  381.                              abfd, (int) r_type);
  382.       indx = R_386_NONE;
  383.     }
  384.   BFD_ASSERT (elf_howto_table [indx].type == r_type);
  385.   return &elf_howto_table[indx];
  386. }
  387.  
  388. static void
  389. elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
  390.                             arelent *cache_ptr,
  391.                             Elf_Internal_Rela *dst)
  392. {
  393.   unsigned int r_type = ELF32_R_TYPE (dst->r_info);
  394.   cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
  395. }
  396.  
  397. /* Return whether a symbol name implies a local label.  The UnixWare
  398.    2.1 cc generates temporary symbols that start with .X, so we
  399.    recognize them here.  FIXME: do other SVR4 compilers also use .X?.
  400.    If so, we should move the .X recognition into
  401.    _bfd_elf_is_local_label_name.  */
  402.  
  403. static bfd_boolean
  404. elf_i386_is_local_label_name (bfd *abfd, const char *name)
  405. {
  406.   if (name[0] == '.' && name[1] == 'X')
  407.     return TRUE;
  408.  
  409.   return _bfd_elf_is_local_label_name (abfd, name);
  410. }
  411. /* Support for core dump NOTE sections.  */
  412.  
  413. static bfd_boolean
  414. elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  415. {
  416.   int offset;
  417.   size_t size;
  418.  
  419.   if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
  420.     {
  421.       int pr_version = bfd_get_32 (abfd, note->descdata);
  422.  
  423.       if (pr_version != 1)
  424.         return FALSE;
  425.  
  426.       /* pr_cursig */
  427.       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
  428.  
  429.       /* pr_pid */
  430.       elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
  431.  
  432.       /* pr_reg */
  433.       offset = 28;
  434.       size = bfd_get_32 (abfd, note->descdata + 8);
  435.     }
  436.   else
  437.     {
  438.       switch (note->descsz)
  439.         {
  440.         default:
  441.           return FALSE;
  442.  
  443.         case 144:               /* Linux/i386 */
  444.           /* pr_cursig */
  445.           elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  446.  
  447.           /* pr_pid */
  448.           elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
  449.  
  450.           /* pr_reg */
  451.           offset = 72;
  452.           size = 68;
  453.  
  454.           break;
  455.         }
  456.     }
  457.  
  458.   /* Make a ".reg/999" section.  */
  459.   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  460.                                           size, note->descpos + offset);
  461. }
  462.  
  463. static bfd_boolean
  464. elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  465. {
  466.   if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
  467.     {
  468.       int pr_version = bfd_get_32 (abfd, note->descdata);
  469.  
  470.       if (pr_version != 1)
  471.         return FALSE;
  472.  
  473.       elf_tdata (abfd)->core->program
  474.         = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
  475.       elf_tdata (abfd)->core->command
  476.         = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
  477.     }
  478.   else
  479.     {
  480.       switch (note->descsz)
  481.         {
  482.         default:
  483.           return FALSE;
  484.  
  485.         case 124:               /* Linux/i386 elf_prpsinfo.  */
  486.           elf_tdata (abfd)->core->pid
  487.             = bfd_get_32 (abfd, note->descdata + 12);
  488.           elf_tdata (abfd)->core->program
  489.             = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
  490.           elf_tdata (abfd)->core->command
  491.             = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
  492.         }
  493.     }
  494.  
  495.   /* Note that for some reason, a spurious space is tacked
  496.      onto the end of the args in some (at least one anyway)
  497.      implementations, so strip it off if it exists.  */
  498.   {
  499.     char *command = elf_tdata (abfd)->core->command;
  500.     int n = strlen (command);
  501.  
  502.     if (0 < n && command[n - 1] == ' ')
  503.       command[n - 1] = '\0';
  504.   }
  505.  
  506.   return TRUE;
  507. }
  508. /* Functions for the i386 ELF linker.
  509.  
  510.    In order to gain some understanding of code in this file without
  511.    knowing all the intricate details of the linker, note the
  512.    following:
  513.  
  514.    Functions named elf_i386_* are called by external routines, other
  515.    functions are only called locally.  elf_i386_* functions appear
  516.    in this file more or less in the order in which they are called
  517.    from external routines.  eg. elf_i386_check_relocs is called
  518.    early in the link process, elf_i386_finish_dynamic_sections is
  519.    one of the last functions.  */
  520.  
  521.  
  522. /* The name of the dynamic interpreter.  This is put in the .interp
  523.    section.  */
  524.  
  525. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
  526.  
  527. /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
  528.    copying dynamic variables from a shared lib into an app's dynbss
  529.    section, and instead use a dynamic relocation to point into the
  530.    shared lib.  */
  531. #define ELIMINATE_COPY_RELOCS 1
  532.  
  533. /* The size in bytes of an entry in the procedure linkage table.  */
  534.  
  535. #define PLT_ENTRY_SIZE 16
  536.  
  537. /* The first entry in an absolute procedure linkage table looks like
  538.    this.  See the SVR4 ABI i386 supplement to see how this works.
  539.    Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte.  */
  540.  
  541. static const bfd_byte elf_i386_plt0_entry[12] =
  542. {
  543.   0xff, 0x35,   /* pushl contents of address */
  544.   0, 0, 0, 0,   /* replaced with address of .got + 4.  */
  545.   0xff, 0x25,   /* jmp indirect */
  546.   0, 0, 0, 0    /* replaced with address of .got + 8.  */
  547. };
  548.  
  549. /* Subsequent entries in an absolute procedure linkage table look like
  550.    this.  */
  551.  
  552. static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
  553. {
  554.   0xff, 0x25,   /* jmp indirect */
  555.   0, 0, 0, 0,   /* replaced with address of this symbol in .got.  */
  556.   0x68,         /* pushl immediate */
  557.   0, 0, 0, 0,   /* replaced with offset into relocation table.  */
  558.   0xe9,         /* jmp relative */
  559.   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
  560. };
  561.  
  562. /* The first entry in a PIC procedure linkage table look like this.
  563.    Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte.  */
  564.  
  565. static const bfd_byte elf_i386_pic_plt0_entry[12] =
  566. {
  567.   0xff, 0xb3, 4, 0, 0, 0,       /* pushl 4(%ebx) */
  568.   0xff, 0xa3, 8, 0, 0, 0        /* jmp *8(%ebx) */
  569. };
  570.  
  571. /* Subsequent entries in a PIC procedure linkage table look like this.  */
  572.  
  573. static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
  574. {
  575.   0xff, 0xa3,   /* jmp *offset(%ebx) */
  576.   0, 0, 0, 0,   /* replaced with offset of this symbol in .got.  */
  577.   0x68,         /* pushl immediate */
  578.   0, 0, 0, 0,   /* replaced with offset into relocation table.  */
  579.   0xe9,         /* jmp relative */
  580.   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
  581. };
  582.  
  583. /* .eh_frame covering the .plt section.  */
  584.  
  585. static const bfd_byte elf_i386_eh_frame_plt[] =
  586. {
  587. #define PLT_CIE_LENGTH          20
  588. #define PLT_FDE_LENGTH          36
  589. #define PLT_FDE_START_OFFSET    4 + PLT_CIE_LENGTH + 8
  590. #define PLT_FDE_LEN_OFFSET      4 + PLT_CIE_LENGTH + 12
  591.   PLT_CIE_LENGTH, 0, 0, 0,      /* CIE length */
  592.   0, 0, 0, 0,                   /* CIE ID */
  593.   1,                            /* CIE version */
  594.   'z', 'R', 0,                  /* Augmentation string */
  595.   1,                            /* Code alignment factor */
  596.   0x7c,                         /* Data alignment factor */
  597.   8,                            /* Return address column */
  598.   1,                            /* Augmentation size */
  599.   DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
  600.   DW_CFA_def_cfa, 4, 4,         /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
  601.   DW_CFA_offset + 8, 1,         /* DW_CFA_offset: r8 (eip) at cfa-4 */
  602.   DW_CFA_nop, DW_CFA_nop,
  603.  
  604.   PLT_FDE_LENGTH, 0, 0, 0,      /* FDE length */
  605.   PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
  606.   0, 0, 0, 0,                   /* R_386_PC32 .plt goes here */
  607.   0, 0, 0, 0,                   /* .plt size goes here */
  608.   0,                            /* Augmentation size */
  609.   DW_CFA_def_cfa_offset, 8,     /* DW_CFA_def_cfa_offset: 8 */
  610.   DW_CFA_advance_loc + 6,       /* DW_CFA_advance_loc: 6 to __PLT__+6 */
  611.   DW_CFA_def_cfa_offset, 12,    /* DW_CFA_def_cfa_offset: 12 */
  612.   DW_CFA_advance_loc + 10,      /* DW_CFA_advance_loc: 10 to __PLT__+16 */
  613.   DW_CFA_def_cfa_expression,    /* DW_CFA_def_cfa_expression */
  614.   11,                           /* Block length */
  615.   DW_OP_breg4, 4,               /* DW_OP_breg4 (esp): 4 */
  616.   DW_OP_breg8, 0,               /* DW_OP_breg8 (eip): 0 */
  617.   DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
  618.   DW_OP_lit2, DW_OP_shl, DW_OP_plus,
  619.   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
  620. };
  621.  
  622. struct elf_i386_plt_layout
  623. {
  624.   /* The first entry in an absolute procedure linkage table looks like this.  */
  625.   const bfd_byte *plt0_entry;
  626.   unsigned int plt0_entry_size;
  627.  
  628.   /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2].  */
  629.   unsigned int plt0_got1_offset;
  630.   unsigned int plt0_got2_offset;
  631.  
  632.   /* Later entries in an absolute procedure linkage table look like this.  */
  633.   const bfd_byte *plt_entry;
  634.   unsigned int plt_entry_size;
  635.  
  636.   /* Offsets into plt_entry that are to be replaced with...  */
  637.   unsigned int plt_got_offset;    /* ... address of this symbol in .got. */
  638.   unsigned int plt_reloc_offset;  /* ... offset into relocation table. */
  639.   unsigned int plt_plt_offset;    /* ... offset to start of .plt. */
  640.  
  641.   /* Offset into plt_entry where the initial value of the GOT entry points.  */
  642.   unsigned int plt_lazy_offset;
  643.  
  644.   /* The first entry in a PIC procedure linkage table looks like this.  */
  645.   const bfd_byte *pic_plt0_entry;
  646.  
  647.   /* Subsequent entries in a PIC procedure linkage table look like this.  */
  648.   const bfd_byte *pic_plt_entry;
  649.  
  650.   /* .eh_frame covering the .plt section.  */
  651.   const bfd_byte *eh_frame_plt;
  652.   unsigned int eh_frame_plt_size;
  653. };
  654.  
  655. #define GET_PLT_ENTRY_SIZE(abfd) \
  656.   get_elf_i386_backend_data (abfd)->plt->plt_entry_size
  657.  
  658. /* These are the standard parameters.  */
  659. static const struct elf_i386_plt_layout elf_i386_plt =
  660.   {
  661.     elf_i386_plt0_entry,                /* plt0_entry */
  662.     sizeof (elf_i386_plt0_entry),       /* plt0_entry_size */
  663.     2,                                  /* plt0_got1_offset */
  664.     8,                                  /* plt0_got2_offset */
  665.     elf_i386_plt_entry,                 /* plt_entry */
  666.     PLT_ENTRY_SIZE,                     /* plt_entry_size */
  667.     2,                                  /* plt_got_offset */
  668.     7,                                  /* plt_reloc_offset */
  669.     12,                                 /* plt_plt_offset */
  670.     6,                                  /* plt_lazy_offset */
  671.     elf_i386_pic_plt0_entry,            /* pic_plt0_entry */
  672.     elf_i386_pic_plt_entry,             /* pic_plt_entry */
  673.     elf_i386_eh_frame_plt,              /* eh_frame_plt */
  674.     sizeof (elf_i386_eh_frame_plt),     /* eh_frame_plt_size */
  675.   };
  676.  
  677. /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
  678.    for the PLTResolve stub and then for each PLT entry.  */
  679. #define PLTRESOLVE_RELOCS_SHLIB 0
  680. #define PLTRESOLVE_RELOCS 2
  681. #define PLT_NON_JUMP_SLOT_RELOCS 2
  682.  
  683. /* Architecture-specific backend data for i386.  */
  684.  
  685. struct elf_i386_backend_data
  686. {
  687.   /* Parameters describing PLT generation.  */
  688.   const struct elf_i386_plt_layout *plt;
  689.  
  690.   /* Value used to fill the unused bytes of the first PLT entry.  */
  691.   bfd_byte plt0_pad_byte;
  692.  
  693.   /* True if the target system is VxWorks.  */
  694.   int is_vxworks;
  695. };
  696.  
  697. #define get_elf_i386_backend_data(abfd) \
  698.   ((const struct elf_i386_backend_data *) \
  699.    get_elf_backend_data (abfd)->arch_data)
  700.  
  701. /* These are the standard parameters.  */
  702. static const struct elf_i386_backend_data elf_i386_arch_bed =
  703.   {
  704.     &elf_i386_plt,                      /* plt */
  705.     0,                                  /* plt0_pad_byte */
  706.     0,                                  /* is_vxworks */
  707.   };
  708.  
  709. #define elf_backend_arch_data   &elf_i386_arch_bed
  710.  
  711. /* i386 ELF linker hash entry.  */
  712.  
  713. struct elf_i386_link_hash_entry
  714. {
  715.   struct elf_link_hash_entry elf;
  716.  
  717.   /* Track dynamic relocs copied for this symbol.  */
  718.   struct elf_dyn_relocs *dyn_relocs;
  719.  
  720. #define GOT_UNKNOWN     0
  721. #define GOT_NORMAL      1
  722. #define GOT_TLS_GD      2
  723. #define GOT_TLS_IE      4
  724. #define GOT_TLS_IE_POS  5
  725. #define GOT_TLS_IE_NEG  6
  726. #define GOT_TLS_IE_BOTH 7
  727. #define GOT_TLS_GDESC   8
  728. #define GOT_TLS_GD_BOTH_P(type)                                         \
  729.   ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
  730. #define GOT_TLS_GD_P(type)                                              \
  731.   ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
  732. #define GOT_TLS_GDESC_P(type)                                           \
  733.   ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
  734. #define GOT_TLS_GD_ANY_P(type)                                          \
  735.   (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
  736.   unsigned char tls_type;
  737.  
  738.   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
  739.      starting at the end of the jump table.  */
  740.   bfd_vma tlsdesc_got;
  741. };
  742.  
  743. #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
  744.  
  745. struct elf_i386_obj_tdata
  746. {
  747.   struct elf_obj_tdata root;
  748.  
  749.   /* tls_type for each local got entry.  */
  750.   char *local_got_tls_type;
  751.  
  752.   /* GOTPLT entries for TLS descriptors.  */
  753.   bfd_vma *local_tlsdesc_gotent;
  754. };
  755.  
  756. #define elf_i386_tdata(abfd) \
  757.   ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
  758.  
  759. #define elf_i386_local_got_tls_type(abfd) \
  760.   (elf_i386_tdata (abfd)->local_got_tls_type)
  761.  
  762. #define elf_i386_local_tlsdesc_gotent(abfd) \
  763.   (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
  764.  
  765. #define is_i386_elf(bfd)                                \
  766.   (bfd_get_flavour (bfd) == bfd_target_elf_flavour      \
  767.    && elf_tdata (bfd) != NULL                           \
  768.    && elf_object_id (bfd) == I386_ELF_DATA)
  769.  
  770. static bfd_boolean
  771. elf_i386_mkobject (bfd *abfd)
  772. {
  773.   return bfd_elf_allocate_object (abfd, sizeof (struct elf_i386_obj_tdata),
  774.                                   I386_ELF_DATA);
  775. }
  776.  
  777. /* i386 ELF linker hash table.  */
  778.  
  779. struct elf_i386_link_hash_table
  780. {
  781.   struct elf_link_hash_table elf;
  782.  
  783.   /* Short-cuts to get to dynamic linker sections.  */
  784.   asection *sdynbss;
  785.   asection *srelbss;
  786.   asection *plt_eh_frame;
  787.  
  788.   union
  789.   {
  790.     bfd_signed_vma refcount;
  791.     bfd_vma offset;
  792.   } tls_ldm_got;
  793.  
  794.   /* The amount of space used by the reserved portion of the sgotplt
  795.      section, plus whatever space is used by the jump slots.  */
  796.   bfd_vma sgotplt_jump_table_size;
  797.  
  798.   /* Small local sym cache.  */
  799.   struct sym_cache sym_cache;
  800.  
  801.   /* _TLS_MODULE_BASE_ symbol.  */
  802.   struct bfd_link_hash_entry *tls_module_base;
  803.  
  804.   /* Used by local STT_GNU_IFUNC symbols.  */
  805.   htab_t loc_hash_table;
  806.   void * loc_hash_memory;
  807.  
  808.   /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.  */
  809.   asection *srelplt2;
  810.  
  811.   /* The index of the next unused R_386_TLS_DESC slot in .rel.plt.  */
  812.   bfd_vma next_tls_desc_index;
  813.  
  814.   /* The index of the next unused R_386_JUMP_SLOT slot in .rel.plt.  */
  815.   bfd_vma next_jump_slot_index;
  816.  
  817.   /* The index of the next unused R_386_IRELATIVE slot in .rel.plt.  */
  818.   bfd_vma next_irelative_index;
  819. };
  820.  
  821. /* Get the i386 ELF linker hash table from a link_info structure.  */
  822.  
  823. #define elf_i386_hash_table(p) \
  824.   (elf_hash_table_id  ((struct elf_link_hash_table *) ((p)->hash)) \
  825.   == I386_ELF_DATA ? ((struct elf_i386_link_hash_table *) ((p)->hash)) : NULL)
  826.  
  827. #define elf_i386_compute_jump_table_size(htab) \
  828.   ((htab)->next_tls_desc_index * 4)
  829.  
  830. /* Create an entry in an i386 ELF linker hash table.  */
  831.  
  832. static struct bfd_hash_entry *
  833. elf_i386_link_hash_newfunc (struct bfd_hash_entry *entry,
  834.                             struct bfd_hash_table *table,
  835.                             const char *string)
  836. {
  837.   /* Allocate the structure if it has not already been allocated by a
  838.      subclass.  */
  839.   if (entry == NULL)
  840.     {
  841.       entry = (struct bfd_hash_entry *)
  842.           bfd_hash_allocate (table, sizeof (struct elf_i386_link_hash_entry));
  843.       if (entry == NULL)
  844.         return entry;
  845.     }
  846.  
  847.   /* Call the allocation method of the superclass.  */
  848.   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
  849.   if (entry != NULL)
  850.     {
  851.       struct elf_i386_link_hash_entry *eh;
  852.  
  853.       eh = (struct elf_i386_link_hash_entry *) entry;
  854.       eh->dyn_relocs = NULL;
  855.       eh->tls_type = GOT_UNKNOWN;
  856.       eh->tlsdesc_got = (bfd_vma) -1;
  857.     }
  858.  
  859.   return entry;
  860. }
  861.  
  862. /* Compute a hash of a local hash entry.  We use elf_link_hash_entry
  863.   for local symbol so that we can handle local STT_GNU_IFUNC symbols
  864.   as global symbol.  We reuse indx and dynstr_index for local symbol
  865.   hash since they aren't used by global symbols in this backend.  */
  866.  
  867. static hashval_t
  868. elf_i386_local_htab_hash (const void *ptr)
  869. {
  870.   struct elf_link_hash_entry *h
  871.     = (struct elf_link_hash_entry *) ptr;
  872.   return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
  873. }
  874.  
  875. /* Compare local hash entries.  */
  876.  
  877. static int
  878. elf_i386_local_htab_eq (const void *ptr1, const void *ptr2)
  879. {
  880.   struct elf_link_hash_entry *h1
  881.      = (struct elf_link_hash_entry *) ptr1;
  882.   struct elf_link_hash_entry *h2
  883.     = (struct elf_link_hash_entry *) ptr2;
  884.  
  885.   return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
  886. }
  887.  
  888. /* Find and/or create a hash entry for local symbol.  */
  889.  
  890. static struct elf_link_hash_entry *
  891. elf_i386_get_local_sym_hash (struct elf_i386_link_hash_table *htab,
  892.                              bfd *abfd, const Elf_Internal_Rela *rel,
  893.                              bfd_boolean create)
  894. {
  895.   struct elf_i386_link_hash_entry e, *ret;
  896.   asection *sec = abfd->sections;
  897.   hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
  898.                                        ELF32_R_SYM (rel->r_info));
  899.   void **slot;
  900.  
  901.   e.elf.indx = sec->id;
  902.   e.elf.dynstr_index = ELF32_R_SYM (rel->r_info);
  903.   slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
  904.                                    create ? INSERT : NO_INSERT);
  905.  
  906.   if (!slot)
  907.     return NULL;
  908.  
  909.   if (*slot)
  910.     {
  911.       ret = (struct elf_i386_link_hash_entry *) *slot;
  912.       return &ret->elf;
  913.     }
  914.  
  915.   ret = (struct elf_i386_link_hash_entry *)
  916.         objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
  917.                         sizeof (struct elf_i386_link_hash_entry));
  918.   if (ret)
  919.     {
  920.       memset (ret, 0, sizeof (*ret));
  921.       ret->elf.indx = sec->id;
  922.       ret->elf.dynstr_index = ELF32_R_SYM (rel->r_info);
  923.       ret->elf.dynindx = -1;
  924.       *slot = ret;
  925.     }
  926.   return &ret->elf;
  927. }
  928.  
  929. /* Create an i386 ELF linker hash table.  */
  930.  
  931. static struct bfd_link_hash_table *
  932. elf_i386_link_hash_table_create (bfd *abfd)
  933. {
  934.   struct elf_i386_link_hash_table *ret;
  935.   bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
  936.  
  937.   ret = (struct elf_i386_link_hash_table *) bfd_zmalloc (amt);
  938.   if (ret == NULL)
  939.     return NULL;
  940.  
  941.   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
  942.                                       elf_i386_link_hash_newfunc,
  943.                                       sizeof (struct elf_i386_link_hash_entry),
  944.                                       I386_ELF_DATA))
  945.     {
  946.       free (ret);
  947.       return NULL;
  948.     }
  949.  
  950.   ret->loc_hash_table = htab_try_create (1024,
  951.                                          elf_i386_local_htab_hash,
  952.                                          elf_i386_local_htab_eq,
  953.                                          NULL);
  954.   ret->loc_hash_memory = objalloc_create ();
  955.   if (!ret->loc_hash_table || !ret->loc_hash_memory)
  956.     {
  957.       free (ret);
  958.       return NULL;
  959.     }
  960.  
  961.   return &ret->elf.root;
  962. }
  963.  
  964. /* Destroy an i386 ELF linker hash table.  */
  965.  
  966. static void
  967. elf_i386_link_hash_table_free (struct bfd_link_hash_table *hash)
  968. {
  969.   struct elf_i386_link_hash_table *htab
  970.     = (struct elf_i386_link_hash_table *) hash;
  971.  
  972.   if (htab->loc_hash_table)
  973.     htab_delete (htab->loc_hash_table);
  974.   if (htab->loc_hash_memory)
  975.     objalloc_free ((struct objalloc *) htab->loc_hash_memory);
  976.   _bfd_elf_link_hash_table_free (hash);
  977. }
  978.  
  979. /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
  980.    .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
  981.    hash table.  */
  982.  
  983. static bfd_boolean
  984. elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
  985. {
  986.   struct elf_i386_link_hash_table *htab;
  987.  
  988.   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
  989.     return FALSE;
  990.  
  991.   htab = elf_i386_hash_table (info);
  992.   if (htab == NULL)
  993.     return FALSE;
  994.  
  995.   htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
  996.   if (!info->shared)
  997.     htab->srelbss = bfd_get_linker_section (dynobj, ".rel.bss");
  998.  
  999.   if (!htab->sdynbss
  1000.       || (!info->shared && !htab->srelbss))
  1001.     abort ();
  1002.  
  1003.   if (get_elf_i386_backend_data (dynobj)->is_vxworks
  1004.       && !elf_vxworks_create_dynamic_sections (dynobj, info,
  1005.                                                &htab->srelplt2))
  1006.     return FALSE;
  1007.  
  1008.   if (!info->no_ld_generated_unwind_info
  1009.       && htab->plt_eh_frame == NULL
  1010.       && htab->elf.splt != NULL)
  1011.     {
  1012.       flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
  1013.                         | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  1014.                         | SEC_LINKER_CREATED);
  1015.       htab->plt_eh_frame
  1016.         = bfd_make_section_anyway_with_flags (dynobj, ".eh_frame", flags);
  1017.       if (htab->plt_eh_frame == NULL
  1018.           || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame, 2))
  1019.         return FALSE;
  1020.     }
  1021.  
  1022.   return TRUE;
  1023. }
  1024.  
  1025. /* Copy the extra info we tack onto an elf_link_hash_entry.  */
  1026.  
  1027. static void
  1028. elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
  1029.                                struct elf_link_hash_entry *dir,
  1030.                                struct elf_link_hash_entry *ind)
  1031. {
  1032.   struct elf_i386_link_hash_entry *edir, *eind;
  1033.  
  1034.   edir = (struct elf_i386_link_hash_entry *) dir;
  1035.   eind = (struct elf_i386_link_hash_entry *) ind;
  1036.  
  1037.   if (eind->dyn_relocs != NULL)
  1038.     {
  1039.       if (edir->dyn_relocs != NULL)
  1040.         {
  1041.           struct elf_dyn_relocs **pp;
  1042.           struct elf_dyn_relocs *p;
  1043.  
  1044.           /* Add reloc counts against the indirect sym to the direct sym
  1045.              list.  Merge any entries against the same section.  */
  1046.           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
  1047.             {
  1048.               struct elf_dyn_relocs *q;
  1049.  
  1050.               for (q = edir->dyn_relocs; q != NULL; q = q->next)
  1051.                 if (q->sec == p->sec)
  1052.                   {
  1053.                     q->pc_count += p->pc_count;
  1054.                     q->count += p->count;
  1055.                     *pp = p->next;
  1056.                     break;
  1057.                   }
  1058.               if (q == NULL)
  1059.                 pp = &p->next;
  1060.             }
  1061.           *pp = edir->dyn_relocs;
  1062.         }
  1063.  
  1064.       edir->dyn_relocs = eind->dyn_relocs;
  1065.       eind->dyn_relocs = NULL;
  1066.     }
  1067.  
  1068.   if (ind->root.type == bfd_link_hash_indirect
  1069.       && dir->got.refcount <= 0)
  1070.     {
  1071.       edir->tls_type = eind->tls_type;
  1072.       eind->tls_type = GOT_UNKNOWN;
  1073.     }
  1074.  
  1075.   if (ELIMINATE_COPY_RELOCS
  1076.       && ind->root.type != bfd_link_hash_indirect
  1077.       && dir->dynamic_adjusted)
  1078.     {
  1079.       /* If called to transfer flags for a weakdef during processing
  1080.          of elf_adjust_dynamic_symbol, don't copy non_got_ref.
  1081.          We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
  1082.       dir->ref_dynamic |= ind->ref_dynamic;
  1083.       dir->ref_regular |= ind->ref_regular;
  1084.       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
  1085.       dir->needs_plt |= ind->needs_plt;
  1086.       dir->pointer_equality_needed |= ind->pointer_equality_needed;
  1087.     }
  1088.   else
  1089.     _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  1090. }
  1091.  
  1092. /* Return TRUE if the TLS access code sequence support transition
  1093.    from R_TYPE.  */
  1094.  
  1095. static bfd_boolean
  1096. elf_i386_check_tls_transition (bfd *abfd, asection *sec,
  1097.                                bfd_byte *contents,
  1098.                                Elf_Internal_Shdr *symtab_hdr,
  1099.                                struct elf_link_hash_entry **sym_hashes,
  1100.                                unsigned int r_type,
  1101.                                const Elf_Internal_Rela *rel,
  1102.                                const Elf_Internal_Rela *relend)
  1103. {
  1104.   unsigned int val, type;
  1105.   unsigned long r_symndx;
  1106.   struct elf_link_hash_entry *h;
  1107.   bfd_vma offset;
  1108.  
  1109.   /* Get the section contents.  */
  1110.   if (contents == NULL)
  1111.     {
  1112.       if (elf_section_data (sec)->this_hdr.contents != NULL)
  1113.         contents = elf_section_data (sec)->this_hdr.contents;
  1114.       else
  1115.         {
  1116.           /* FIXME: How to better handle error condition?  */
  1117.           if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  1118.             return FALSE;
  1119.  
  1120.           /* Cache the section contents for elf_link_input_bfd.  */
  1121.           elf_section_data (sec)->this_hdr.contents = contents;
  1122.         }
  1123.     }
  1124.  
  1125.   offset = rel->r_offset;
  1126.   switch (r_type)
  1127.     {
  1128.     case R_386_TLS_GD:
  1129.     case R_386_TLS_LDM:
  1130.       if (offset < 2 || (rel + 1) >= relend)
  1131.         return FALSE;
  1132.  
  1133.       type = bfd_get_8 (abfd, contents + offset - 2);
  1134.       if (r_type == R_386_TLS_GD)
  1135.         {
  1136.           /* Check transition from GD access model.  Only
  1137.                 leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr
  1138.                 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop
  1139.              can transit to different access model.  */
  1140.           if ((offset + 10) > sec->size ||
  1141.               (type != 0x8d && type != 0x04))
  1142.             return FALSE;
  1143.  
  1144.           val = bfd_get_8 (abfd, contents + offset - 1);
  1145.           if (type == 0x04)
  1146.             {
  1147.               /* leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr */
  1148.               if (offset < 3)
  1149.                 return FALSE;
  1150.  
  1151.               if (bfd_get_8 (abfd, contents + offset - 3) != 0x8d)
  1152.                 return FALSE;
  1153.  
  1154.               if ((val & 0xc7) != 0x05 || val == (4 << 3))
  1155.                 return FALSE;
  1156.             }
  1157.           else
  1158.             {
  1159.               /* leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop  */
  1160.               if ((val & 0xf8) != 0x80 || (val & 7) == 4)
  1161.                 return FALSE;
  1162.  
  1163.               if (bfd_get_8 (abfd, contents + offset + 9) != 0x90)
  1164.                 return FALSE;
  1165.             }
  1166.         }
  1167.       else
  1168.         {
  1169.           /* Check transition from LD access model.  Only
  1170.                 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr
  1171.              can transit to different access model.  */
  1172.           if (type != 0x8d || (offset + 9) > sec->size)
  1173.             return FALSE;
  1174.  
  1175.           val = bfd_get_8 (abfd, contents + offset - 1);
  1176.           if ((val & 0xf8) != 0x80 || (val & 7) == 4)
  1177.             return FALSE;
  1178.         }
  1179.  
  1180.       if (bfd_get_8 (abfd, contents + offset + 4) != 0xe8)
  1181.         return FALSE;
  1182.  
  1183.       r_symndx = ELF32_R_SYM (rel[1].r_info);
  1184.       if (r_symndx < symtab_hdr->sh_info)
  1185.         return FALSE;
  1186.  
  1187.       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1188.       /* Use strncmp to check ___tls_get_addr since ___tls_get_addr
  1189.          may be versioned.  */
  1190.       return (h != NULL
  1191.               && h->root.root.string != NULL
  1192.               && (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
  1193.                   || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
  1194.               && (strncmp (h->root.root.string, "___tls_get_addr",
  1195.                            15) == 0));
  1196.  
  1197.     case R_386_TLS_IE:
  1198.       /* Check transition from IE access model:
  1199.                 movl foo@indntpoff(%rip), %eax
  1200.                 movl foo@indntpoff(%rip), %reg
  1201.                 addl foo@indntpoff(%rip), %reg
  1202.        */
  1203.  
  1204.       if (offset < 1 || (offset + 4) > sec->size)
  1205.         return FALSE;
  1206.  
  1207.       /* Check "movl foo@tpoff(%rip), %eax" first.  */
  1208.       val = bfd_get_8 (abfd, contents + offset - 1);
  1209.       if (val == 0xa1)
  1210.         return TRUE;
  1211.  
  1212.       if (offset < 2)
  1213.         return FALSE;
  1214.  
  1215.       /* Check movl|addl foo@tpoff(%rip), %reg.   */
  1216.       type = bfd_get_8 (abfd, contents + offset - 2);
  1217.       return ((type == 0x8b || type == 0x03)
  1218.               && (val & 0xc7) == 0x05);
  1219.  
  1220.     case R_386_TLS_GOTIE:
  1221.     case R_386_TLS_IE_32:
  1222.       /* Check transition from {IE_32,GOTIE} access model:
  1223.                 subl foo@{tpoff,gontoff}(%reg1), %reg2
  1224.                 movl foo@{tpoff,gontoff}(%reg1), %reg2
  1225.                 addl foo@{tpoff,gontoff}(%reg1), %reg2
  1226.        */
  1227.  
  1228.       if (offset < 2 || (offset + 4) > sec->size)
  1229.         return FALSE;
  1230.  
  1231.       val = bfd_get_8 (abfd, contents + offset - 1);
  1232.       if ((val & 0xc0) != 0x80 || (val & 7) == 4)
  1233.         return FALSE;
  1234.  
  1235.       type = bfd_get_8 (abfd, contents + offset - 2);
  1236.       return type == 0x8b || type == 0x2b || type == 0x03;
  1237.  
  1238.     case R_386_TLS_GOTDESC:
  1239.       /* Check transition from GDesc access model:
  1240.                 leal x@tlsdesc(%ebx), %eax
  1241.  
  1242.          Make sure it's a leal adding ebx to a 32-bit offset
  1243.          into any register, although it's probably almost always
  1244.          going to be eax.  */
  1245.  
  1246.       if (offset < 2 || (offset + 4) > sec->size)
  1247.         return FALSE;
  1248.  
  1249.       if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
  1250.         return FALSE;
  1251.  
  1252.       val = bfd_get_8 (abfd, contents + offset - 1);
  1253.       return (val & 0xc7) == 0x83;
  1254.  
  1255.     case R_386_TLS_DESC_CALL:
  1256.       /* Check transition from GDesc access model:
  1257.                 call *x@tlsdesc(%rax)
  1258.        */
  1259.       if (offset + 2 <= sec->size)
  1260.         {
  1261.           /* Make sure that it's a call *x@tlsdesc(%rax).  */
  1262.           static const unsigned char call[] = { 0xff, 0x10 };
  1263.           return memcmp (contents + offset, call, 2) == 0;
  1264.         }
  1265.  
  1266.       return FALSE;
  1267.  
  1268.     default:
  1269.       abort ();
  1270.     }
  1271. }
  1272.  
  1273. /* Return TRUE if the TLS access transition is OK or no transition
  1274.    will be performed.  Update R_TYPE if there is a transition.  */
  1275.  
  1276. static bfd_boolean
  1277. elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
  1278.                          asection *sec, bfd_byte *contents,
  1279.                          Elf_Internal_Shdr *symtab_hdr,
  1280.                          struct elf_link_hash_entry **sym_hashes,
  1281.                          unsigned int *r_type, int tls_type,
  1282.                          const Elf_Internal_Rela *rel,
  1283.                          const Elf_Internal_Rela *relend,
  1284.                          struct elf_link_hash_entry *h,
  1285.                          unsigned long r_symndx)
  1286. {
  1287.   unsigned int from_type = *r_type;
  1288.   unsigned int to_type = from_type;
  1289.   bfd_boolean check = TRUE;
  1290.  
  1291.   /* Skip TLS transition for functions.  */
  1292.   if (h != NULL
  1293.       && (h->type == STT_FUNC
  1294.           || h->type == STT_GNU_IFUNC))
  1295.     return TRUE;
  1296.  
  1297.   switch (from_type)
  1298.     {
  1299.     case R_386_TLS_GD:
  1300.     case R_386_TLS_GOTDESC:
  1301.     case R_386_TLS_DESC_CALL:
  1302.     case R_386_TLS_IE_32:
  1303.     case R_386_TLS_IE:
  1304.     case R_386_TLS_GOTIE:
  1305.       if (info->executable)
  1306.         {
  1307.           if (h == NULL)
  1308.             to_type = R_386_TLS_LE_32;
  1309.           else if (from_type != R_386_TLS_IE
  1310.                    && from_type != R_386_TLS_GOTIE)
  1311.             to_type = R_386_TLS_IE_32;
  1312.         }
  1313.  
  1314.       /* When we are called from elf_i386_relocate_section, CONTENTS
  1315.          isn't NULL and there may be additional transitions based on
  1316.          TLS_TYPE.  */
  1317.       if (contents != NULL)
  1318.         {
  1319.           unsigned int new_to_type = to_type;
  1320.  
  1321.           if (info->executable
  1322.               && h != NULL
  1323.               && h->dynindx == -1
  1324.               && (tls_type & GOT_TLS_IE))
  1325.             new_to_type = R_386_TLS_LE_32;
  1326.  
  1327.           if (to_type == R_386_TLS_GD
  1328.               || to_type == R_386_TLS_GOTDESC
  1329.               || to_type == R_386_TLS_DESC_CALL)
  1330.             {
  1331.               if (tls_type == GOT_TLS_IE_POS)
  1332.                 new_to_type = R_386_TLS_GOTIE;
  1333.               else if (tls_type & GOT_TLS_IE)
  1334.                 new_to_type = R_386_TLS_IE_32;
  1335.             }
  1336.  
  1337.           /* We checked the transition before when we were called from
  1338.              elf_i386_check_relocs.  We only want to check the new
  1339.              transition which hasn't been checked before.  */
  1340.           check = new_to_type != to_type && from_type == to_type;
  1341.           to_type = new_to_type;
  1342.         }
  1343.  
  1344.       break;
  1345.  
  1346.     case R_386_TLS_LDM:
  1347.       if (info->executable)
  1348.         to_type = R_386_TLS_LE_32;
  1349.       break;
  1350.  
  1351.     default:
  1352.       return TRUE;
  1353.     }
  1354.  
  1355.   /* Return TRUE if there is no transition.  */
  1356.   if (from_type == to_type)
  1357.     return TRUE;
  1358.  
  1359.   /* Check if the transition can be performed.  */
  1360.   if (check
  1361.       && ! elf_i386_check_tls_transition (abfd, sec, contents,
  1362.                                           symtab_hdr, sym_hashes,
  1363.                                           from_type, rel, relend))
  1364.     {
  1365.       reloc_howto_type *from, *to;
  1366.       const char *name;
  1367.  
  1368.       from = elf_i386_rtype_to_howto (abfd, from_type);
  1369.       to = elf_i386_rtype_to_howto (abfd, to_type);
  1370.  
  1371.       if (h)
  1372.         name = h->root.root.string;
  1373.       else
  1374.         {
  1375.           struct elf_i386_link_hash_table *htab;
  1376.  
  1377.           htab = elf_i386_hash_table (info);
  1378.           if (htab == NULL)
  1379.             name = "*unknown*";
  1380.           else
  1381.             {
  1382.               Elf_Internal_Sym *isym;
  1383.  
  1384.               isym = bfd_sym_from_r_symndx (&htab->sym_cache,
  1385.                                             abfd, r_symndx);
  1386.               name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
  1387.             }
  1388.         }
  1389.  
  1390.       (*_bfd_error_handler)
  1391.         (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
  1392.            "in section `%A' failed"),
  1393.          abfd, sec, from->name, to->name, name,
  1394.          (unsigned long) rel->r_offset);
  1395.       bfd_set_error (bfd_error_bad_value);
  1396.       return FALSE;
  1397.     }
  1398.  
  1399.   *r_type = to_type;
  1400.   return TRUE;
  1401. }
  1402.  
  1403. /* Look through the relocs for a section during the first phase, and
  1404.    calculate needed space in the global offset table, procedure linkage
  1405.    table, and dynamic reloc sections.  */
  1406.  
  1407. static bfd_boolean
  1408. elf_i386_check_relocs (bfd *abfd,
  1409.                        struct bfd_link_info *info,
  1410.                        asection *sec,
  1411.                        const Elf_Internal_Rela *relocs)
  1412. {
  1413.   struct elf_i386_link_hash_table *htab;
  1414.   Elf_Internal_Shdr *symtab_hdr;
  1415.   struct elf_link_hash_entry **sym_hashes;
  1416.   const Elf_Internal_Rela *rel;
  1417.   const Elf_Internal_Rela *rel_end;
  1418.   asection *sreloc;
  1419.  
  1420.   if (info->relocatable)
  1421.     return TRUE;
  1422.  
  1423.   BFD_ASSERT (is_i386_elf (abfd));
  1424.  
  1425.   htab = elf_i386_hash_table (info);
  1426.   if (htab == NULL)
  1427.     return FALSE;
  1428.  
  1429.   symtab_hdr = &elf_symtab_hdr (abfd);
  1430.   sym_hashes = elf_sym_hashes (abfd);
  1431.  
  1432.   sreloc = NULL;
  1433.  
  1434.   rel_end = relocs + sec->reloc_count;
  1435.   for (rel = relocs; rel < rel_end; rel++)
  1436.     {
  1437.       unsigned int r_type;
  1438.       unsigned long r_symndx;
  1439.       struct elf_link_hash_entry *h;
  1440.       Elf_Internal_Sym *isym;
  1441.       const char *name;
  1442.       bfd_boolean size_reloc;
  1443.  
  1444.       r_symndx = ELF32_R_SYM (rel->r_info);
  1445.       r_type = ELF32_R_TYPE (rel->r_info);
  1446.  
  1447.       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
  1448.         {
  1449.           (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
  1450.                                  abfd,
  1451.                                  r_symndx);
  1452.           return FALSE;
  1453.         }
  1454.  
  1455.       if (r_symndx < symtab_hdr->sh_info)
  1456.         {
  1457.           /* A local symbol.  */
  1458.           isym = bfd_sym_from_r_symndx (&htab->sym_cache,
  1459.                                         abfd, r_symndx);
  1460.           if (isym == NULL)
  1461.             return FALSE;
  1462.  
  1463.           /* Check relocation against local STT_GNU_IFUNC symbol.  */
  1464.           if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
  1465.             {
  1466.               h = elf_i386_get_local_sym_hash (htab, abfd, rel, TRUE);
  1467.               if (h == NULL)
  1468.                 return FALSE;
  1469.  
  1470.               /* Fake a STT_GNU_IFUNC symbol.  */
  1471.               h->type = STT_GNU_IFUNC;
  1472.               h->def_regular = 1;
  1473.               h->ref_regular = 1;
  1474.               h->forced_local = 1;
  1475.               h->root.type = bfd_link_hash_defined;
  1476.             }
  1477.           else
  1478.             h = NULL;
  1479.         }
  1480.       else
  1481.         {
  1482.           isym = NULL;
  1483.           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1484.           while (h->root.type == bfd_link_hash_indirect
  1485.                  || h->root.type == bfd_link_hash_warning)
  1486.             h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1487.         }
  1488.  
  1489.       if (h != NULL)
  1490.         {
  1491.           /* Create the ifunc sections for static executables.  If we
  1492.              never see an indirect function symbol nor we are building
  1493.              a static executable, those sections will be empty and
  1494.              won't appear in output.  */
  1495.           switch (r_type)
  1496.             {
  1497.             default:
  1498.               break;
  1499.  
  1500.             case R_386_32:
  1501.             case R_386_PC32:
  1502.             case R_386_PLT32:
  1503.             case R_386_GOT32:
  1504.             case R_386_GOTOFF:
  1505.               if (htab->elf.dynobj == NULL)
  1506.                 htab->elf.dynobj = abfd;
  1507.               if (!_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
  1508.                 return FALSE;
  1509.               break;
  1510.             }
  1511.  
  1512.           /* It is referenced by a non-shared object. */
  1513.           h->ref_regular = 1;
  1514.           h->root.non_ir_ref = 1;
  1515.         }
  1516.  
  1517.       if (! elf_i386_tls_transition (info, abfd, sec, NULL,
  1518.                                      symtab_hdr, sym_hashes,
  1519.                                      &r_type, GOT_UNKNOWN,
  1520.                                      rel, rel_end, h, r_symndx))
  1521.         return FALSE;
  1522.  
  1523.       switch (r_type)
  1524.         {
  1525.         case R_386_TLS_LDM:
  1526.           htab->tls_ldm_got.refcount += 1;
  1527.           goto create_got;
  1528.  
  1529.         case R_386_PLT32:
  1530.           /* This symbol requires a procedure linkage table entry.  We
  1531.              actually build the entry in adjust_dynamic_symbol,
  1532.              because this might be a case of linking PIC code which is
  1533.              never referenced by a dynamic object, in which case we
  1534.              don't need to generate a procedure linkage table entry
  1535.              after all.  */
  1536.  
  1537.           /* If this is a local symbol, we resolve it directly without
  1538.              creating a procedure linkage table entry.  */
  1539.           if (h == NULL)
  1540.             continue;
  1541.  
  1542.           h->needs_plt = 1;
  1543.           h->plt.refcount += 1;
  1544.           break;
  1545.  
  1546.         case R_386_SIZE32:
  1547.           size_reloc = TRUE;
  1548.           goto do_size;
  1549.  
  1550.         case R_386_TLS_IE_32:
  1551.         case R_386_TLS_IE:
  1552.         case R_386_TLS_GOTIE:
  1553.           if (!info->executable)
  1554.             info->flags |= DF_STATIC_TLS;
  1555.           /* Fall through */
  1556.  
  1557.         case R_386_GOT32:
  1558.         case R_386_TLS_GD:
  1559.         case R_386_TLS_GOTDESC:
  1560.         case R_386_TLS_DESC_CALL:
  1561.           /* This symbol requires a global offset table entry.  */
  1562.           {
  1563.             int tls_type, old_tls_type;
  1564.  
  1565.             switch (r_type)
  1566.               {
  1567.               default:
  1568.               case R_386_GOT32: tls_type = GOT_NORMAL; break;
  1569.               case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
  1570.               case R_386_TLS_GOTDESC:
  1571.               case R_386_TLS_DESC_CALL:
  1572.                 tls_type = GOT_TLS_GDESC; break;
  1573.               case R_386_TLS_IE_32:
  1574.                 if (ELF32_R_TYPE (rel->r_info) == r_type)
  1575.                   tls_type = GOT_TLS_IE_NEG;
  1576.                 else
  1577.                   /* If this is a GD->IE transition, we may use either of
  1578.                      R_386_TLS_TPOFF and R_386_TLS_TPOFF32.  */
  1579.                   tls_type = GOT_TLS_IE;
  1580.                 break;
  1581.               case R_386_TLS_IE:
  1582.               case R_386_TLS_GOTIE:
  1583.                 tls_type = GOT_TLS_IE_POS; break;
  1584.               }
  1585.  
  1586.             if (h != NULL)
  1587.               {
  1588.                 h->got.refcount += 1;
  1589.                 old_tls_type = elf_i386_hash_entry(h)->tls_type;
  1590.               }
  1591.             else
  1592.               {
  1593.                 bfd_signed_vma *local_got_refcounts;
  1594.  
  1595.                 /* This is a global offset table entry for a local symbol.  */
  1596.                 local_got_refcounts = elf_local_got_refcounts (abfd);
  1597.                 if (local_got_refcounts == NULL)
  1598.                   {
  1599.                     bfd_size_type size;
  1600.  
  1601.                     size = symtab_hdr->sh_info;
  1602.                     size *= (sizeof (bfd_signed_vma)
  1603.                              + sizeof (bfd_vma) + sizeof(char));
  1604.                     local_got_refcounts = (bfd_signed_vma *)
  1605.                         bfd_zalloc (abfd, size);
  1606.                     if (local_got_refcounts == NULL)
  1607.                       return FALSE;
  1608.                     elf_local_got_refcounts (abfd) = local_got_refcounts;
  1609.                     elf_i386_local_tlsdesc_gotent (abfd)
  1610.                       = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
  1611.                     elf_i386_local_got_tls_type (abfd)
  1612.                       = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
  1613.                   }
  1614.                 local_got_refcounts[r_symndx] += 1;
  1615.                 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
  1616.               }
  1617.  
  1618.             if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
  1619.               tls_type |= old_tls_type;
  1620.             /* If a TLS symbol is accessed using IE at least once,
  1621.                there is no point to use dynamic model for it.  */
  1622.             else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
  1623.                      && (! GOT_TLS_GD_ANY_P (old_tls_type)
  1624.                          || (tls_type & GOT_TLS_IE) == 0))
  1625.               {
  1626.                 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
  1627.                   tls_type = old_tls_type;
  1628.                 else if (GOT_TLS_GD_ANY_P (old_tls_type)
  1629.                          && GOT_TLS_GD_ANY_P (tls_type))
  1630.                   tls_type |= old_tls_type;
  1631.                 else
  1632.                   {
  1633.                     if (h)
  1634.                       name = h->root.root.string;
  1635.                     else
  1636.                       name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
  1637.                                              NULL);
  1638.                     (*_bfd_error_handler)
  1639.                       (_("%B: `%s' accessed both as normal and "
  1640.                          "thread local symbol"),
  1641.                        abfd, name);
  1642.                     bfd_set_error (bfd_error_bad_value);
  1643.                     return FALSE;
  1644.                   }
  1645.               }
  1646.  
  1647.             if (old_tls_type != tls_type)
  1648.               {
  1649.                 if (h != NULL)
  1650.                   elf_i386_hash_entry (h)->tls_type = tls_type;
  1651.                 else
  1652.                   elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
  1653.               }
  1654.           }
  1655.           /* Fall through */
  1656.  
  1657.         case R_386_GOTOFF:
  1658.         case R_386_GOTPC:
  1659.         create_got:
  1660.           if (htab->elf.sgot == NULL)
  1661.             {
  1662.               if (htab->elf.dynobj == NULL)
  1663.                 htab->elf.dynobj = abfd;
  1664.               if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
  1665.                 return FALSE;
  1666.             }
  1667.           if (r_type != R_386_TLS_IE)
  1668.             break;
  1669.           /* Fall through */
  1670.  
  1671.         case R_386_TLS_LE_32:
  1672.         case R_386_TLS_LE:
  1673.           if (info->executable)
  1674.             break;
  1675.           info->flags |= DF_STATIC_TLS;
  1676.           /* Fall through */
  1677.  
  1678.         case R_386_32:
  1679.         case R_386_PC32:
  1680.           if (h != NULL && info->executable)
  1681.             {
  1682.               /* If this reloc is in a read-only section, we might
  1683.                  need a copy reloc.  We can't check reliably at this
  1684.                  stage whether the section is read-only, as input
  1685.                  sections have not yet been mapped to output sections.
  1686.                  Tentatively set the flag for now, and correct in
  1687.                  adjust_dynamic_symbol.  */
  1688.               h->non_got_ref = 1;
  1689.  
  1690.               /* We may need a .plt entry if the function this reloc
  1691.                  refers to is in a shared lib.  */
  1692.               h->plt.refcount += 1;
  1693.               if (r_type != R_386_PC32)
  1694.                 h->pointer_equality_needed = 1;
  1695.             }
  1696.  
  1697.           size_reloc = FALSE;
  1698. do_size:
  1699.           /* If we are creating a shared library, and this is a reloc
  1700.              against a global symbol, or a non PC relative reloc
  1701.              against a local symbol, then we need to copy the reloc
  1702.              into the shared library.  However, if we are linking with
  1703.              -Bsymbolic, we do not need to copy a reloc against a
  1704.              global symbol which is defined in an object we are
  1705.              including in the link (i.e., DEF_REGULAR is set).  At
  1706.              this point we have not seen all the input files, so it is
  1707.              possible that DEF_REGULAR is not set now but will be set
  1708.              later (it is never cleared).  In case of a weak definition,
  1709.              DEF_REGULAR may be cleared later by a strong definition in
  1710.              a shared library.  We account for that possibility below by
  1711.              storing information in the relocs_copied field of the hash
  1712.              table entry.  A similar situation occurs when creating
  1713.              shared libraries and symbol visibility changes render the
  1714.              symbol local.
  1715.  
  1716.              If on the other hand, we are creating an executable, we
  1717.              may need to keep relocations for symbols satisfied by a
  1718.              dynamic library if we manage to avoid copy relocs for the
  1719.              symbol.  */
  1720.           if ((info->shared
  1721.                && (sec->flags & SEC_ALLOC) != 0
  1722.                && (r_type != R_386_PC32
  1723.                    || (h != NULL
  1724.                        && (! SYMBOLIC_BIND (info, h)
  1725.                            || h->root.type == bfd_link_hash_defweak
  1726.                            || !h->def_regular))))
  1727.               || (ELIMINATE_COPY_RELOCS
  1728.                   && !info->shared
  1729.                   && (sec->flags & SEC_ALLOC) != 0
  1730.                   && h != NULL
  1731.                   && (h->root.type == bfd_link_hash_defweak
  1732.                       || !h->def_regular)))
  1733.             {
  1734.               struct elf_dyn_relocs *p;
  1735.               struct elf_dyn_relocs **head;
  1736.  
  1737.               /* We must copy these reloc types into the output file.
  1738.                  Create a reloc section in dynobj and make room for
  1739.                  this reloc.  */
  1740.               if (sreloc == NULL)
  1741.                 {
  1742.                   if (htab->elf.dynobj == NULL)
  1743.                     htab->elf.dynobj = abfd;
  1744.  
  1745.                   sreloc = _bfd_elf_make_dynamic_reloc_section
  1746.                     (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
  1747.  
  1748.                   if (sreloc == NULL)
  1749.                     return FALSE;
  1750.                 }
  1751.  
  1752.               /* If this is a global symbol, we count the number of
  1753.                  relocations we need for this symbol.  */
  1754.               if (h != NULL)
  1755.                 {
  1756.                   head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
  1757.                 }
  1758.               else
  1759.                 {
  1760.                   /* Track dynamic relocs needed for local syms too.
  1761.                      We really need local syms available to do this
  1762.                      easily.  Oh well.  */
  1763.                   void **vpp;
  1764.                   asection *s;
  1765.  
  1766.                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
  1767.                                                 abfd, r_symndx);
  1768.                   if (isym == NULL)
  1769.                     return FALSE;
  1770.  
  1771.                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
  1772.                   if (s == NULL)
  1773.                     s = sec;
  1774.  
  1775.                   vpp = &elf_section_data (s)->local_dynrel;
  1776.                   head = (struct elf_dyn_relocs **)vpp;
  1777.                 }
  1778.  
  1779.               p = *head;
  1780.               if (p == NULL || p->sec != sec)
  1781.                 {
  1782.                   bfd_size_type amt = sizeof *p;
  1783.                   p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
  1784.                                                            amt);
  1785.                   if (p == NULL)
  1786.                     return FALSE;
  1787.                   p->next = *head;
  1788.                   *head = p;
  1789.                   p->sec = sec;
  1790.                   p->count = 0;
  1791.                   p->pc_count = 0;
  1792.                 }
  1793.  
  1794.               p->count += 1;
  1795.               /* Count size relocation as PC-relative relocation.  */
  1796.               if (r_type == R_386_PC32 || size_reloc)
  1797.                 p->pc_count += 1;
  1798.             }
  1799.           break;
  1800.  
  1801.           /* This relocation describes the C++ object vtable hierarchy.
  1802.              Reconstruct it for later use during GC.  */
  1803.         case R_386_GNU_VTINHERIT:
  1804.           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  1805.             return FALSE;
  1806.           break;
  1807.  
  1808.           /* This relocation describes which C++ vtable entries are actually
  1809.              used.  Record for later use during GC.  */
  1810.         case R_386_GNU_VTENTRY:
  1811.           BFD_ASSERT (h != NULL);
  1812.           if (h != NULL
  1813.               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
  1814.             return FALSE;
  1815.           break;
  1816.  
  1817.         default:
  1818.           break;
  1819.         }
  1820.     }
  1821.  
  1822.   return TRUE;
  1823. }
  1824.  
  1825. /* Return the section that should be marked against GC for a given
  1826.    relocation.  */
  1827.  
  1828. static asection *
  1829. elf_i386_gc_mark_hook (asection *sec,
  1830.                        struct bfd_link_info *info,
  1831.                        Elf_Internal_Rela *rel,
  1832.                        struct elf_link_hash_entry *h,
  1833.                        Elf_Internal_Sym *sym)
  1834. {
  1835.   if (h != NULL)
  1836.     switch (ELF32_R_TYPE (rel->r_info))
  1837.       {
  1838.       case R_386_GNU_VTINHERIT:
  1839.       case R_386_GNU_VTENTRY:
  1840.         return NULL;
  1841.       }
  1842.  
  1843.   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  1844. }
  1845.  
  1846. /* Update the got entry reference counts for the section being removed.  */
  1847.  
  1848. static bfd_boolean
  1849. elf_i386_gc_sweep_hook (bfd *abfd,
  1850.                         struct bfd_link_info *info,
  1851.                         asection *sec,
  1852.                         const Elf_Internal_Rela *relocs)
  1853. {
  1854.   struct elf_i386_link_hash_table *htab;
  1855.   Elf_Internal_Shdr *symtab_hdr;
  1856.   struct elf_link_hash_entry **sym_hashes;
  1857.   bfd_signed_vma *local_got_refcounts;
  1858.   const Elf_Internal_Rela *rel, *relend;
  1859.  
  1860.   if (info->relocatable)
  1861.     return TRUE;
  1862.  
  1863.   htab = elf_i386_hash_table (info);
  1864.   if (htab == NULL)
  1865.     return FALSE;
  1866.  
  1867.   elf_section_data (sec)->local_dynrel = NULL;
  1868.  
  1869.   symtab_hdr = &elf_symtab_hdr (abfd);
  1870.   sym_hashes = elf_sym_hashes (abfd);
  1871.   local_got_refcounts = elf_local_got_refcounts (abfd);
  1872.  
  1873.   relend = relocs + sec->reloc_count;
  1874.   for (rel = relocs; rel < relend; rel++)
  1875.     {
  1876.       unsigned long r_symndx;
  1877.       unsigned int r_type;
  1878.       struct elf_link_hash_entry *h = NULL;
  1879.  
  1880.       r_symndx = ELF32_R_SYM (rel->r_info);
  1881.       if (r_symndx >= symtab_hdr->sh_info)
  1882.         {
  1883.           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1884.           while (h->root.type == bfd_link_hash_indirect
  1885.                  || h->root.type == bfd_link_hash_warning)
  1886.             h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1887.         }
  1888.       else
  1889.         {
  1890.           /* A local symbol.  */
  1891.           Elf_Internal_Sym *isym;
  1892.  
  1893.           isym = bfd_sym_from_r_symndx (&htab->sym_cache,
  1894.                                         abfd, r_symndx);
  1895.  
  1896.           /* Check relocation against local STT_GNU_IFUNC symbol.  */
  1897.           if (isym != NULL
  1898.               && ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
  1899.             {
  1900.               h = elf_i386_get_local_sym_hash (htab, abfd, rel, FALSE);
  1901.               if (h == NULL)
  1902.                 abort ();
  1903.             }
  1904.         }
  1905.  
  1906.       if (h)
  1907.         {
  1908.           struct elf_i386_link_hash_entry *eh;
  1909.           struct elf_dyn_relocs **pp;
  1910.           struct elf_dyn_relocs *p;
  1911.  
  1912.           eh = (struct elf_i386_link_hash_entry *) h;
  1913.           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
  1914.             if (p->sec == sec)
  1915.               {
  1916.                 /* Everything must go for SEC.  */
  1917.                 *pp = p->next;
  1918.                 break;
  1919.               }
  1920.         }
  1921.  
  1922.       r_type = ELF32_R_TYPE (rel->r_info);
  1923.       if (! elf_i386_tls_transition (info, abfd, sec, NULL,
  1924.                                      symtab_hdr, sym_hashes,
  1925.                                      &r_type, GOT_UNKNOWN,
  1926.                                      rel, relend, h, r_symndx))
  1927.         return FALSE;
  1928.  
  1929.       switch (r_type)
  1930.         {
  1931.         case R_386_TLS_LDM:
  1932.           if (htab->tls_ldm_got.refcount > 0)
  1933.             htab->tls_ldm_got.refcount -= 1;
  1934.           break;
  1935.  
  1936.         case R_386_TLS_GD:
  1937.         case R_386_TLS_GOTDESC:
  1938.         case R_386_TLS_DESC_CALL:
  1939.         case R_386_TLS_IE_32:
  1940.         case R_386_TLS_IE:
  1941.         case R_386_TLS_GOTIE:
  1942.         case R_386_GOT32:
  1943.           if (h != NULL)
  1944.             {
  1945.               if (h->got.refcount > 0)
  1946.                 h->got.refcount -= 1;
  1947.               if (h->type == STT_GNU_IFUNC)
  1948.                 {
  1949.                   if (h->plt.refcount > 0)
  1950.                     h->plt.refcount -= 1;
  1951.                 }
  1952.             }
  1953.           else if (local_got_refcounts != NULL)
  1954.             {
  1955.               if (local_got_refcounts[r_symndx] > 0)
  1956.                 local_got_refcounts[r_symndx] -= 1;
  1957.             }
  1958.           break;
  1959.  
  1960.         case R_386_32:
  1961.         case R_386_PC32:
  1962.         case R_386_SIZE32:
  1963.           if (info->shared
  1964.               && (h == NULL || h->type != STT_GNU_IFUNC))
  1965.             break;
  1966.           /* Fall through */
  1967.  
  1968.         case R_386_PLT32:
  1969.           if (h != NULL)
  1970.             {
  1971.               if (h->plt.refcount > 0)
  1972.                 h->plt.refcount -= 1;
  1973.             }
  1974.           break;
  1975.  
  1976.         case R_386_GOTOFF:
  1977.           if (h != NULL && h->type == STT_GNU_IFUNC)
  1978.             {
  1979.               if (h->got.refcount > 0)
  1980.                 h->got.refcount -= 1;
  1981.               if (h->plt.refcount > 0)
  1982.                 h->plt.refcount -= 1;
  1983.             }
  1984.           break;
  1985.  
  1986.         default:
  1987.           break;
  1988.         }
  1989.     }
  1990.  
  1991.   return TRUE;
  1992. }
  1993.  
  1994. /* Adjust a symbol defined by a dynamic object and referenced by a
  1995.    regular object.  The current definition is in some section of the
  1996.    dynamic object, but we're not including those sections.  We have to
  1997.    change the definition to something the rest of the link can
  1998.    understand.  */
  1999.  
  2000. static bfd_boolean
  2001. elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
  2002.                                 struct elf_link_hash_entry *h)
  2003. {
  2004.   struct elf_i386_link_hash_table *htab;
  2005.   asection *s;
  2006.   struct elf_i386_link_hash_entry *eh;
  2007.   struct elf_dyn_relocs *p;
  2008.  
  2009.   /* STT_GNU_IFUNC symbol must go through PLT. */
  2010.   if (h->type == STT_GNU_IFUNC)
  2011.     {
  2012.       /* All local STT_GNU_IFUNC references must be treate as local
  2013.          calls via local PLT.  */
  2014.       if (h->ref_regular
  2015.           && SYMBOL_CALLS_LOCAL (info, h))
  2016.         {
  2017.           bfd_size_type pc_count = 0, count = 0;
  2018.           struct elf_dyn_relocs **pp;
  2019.  
  2020.           eh = (struct elf_i386_link_hash_entry *) h;
  2021.           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
  2022.             {
  2023.               pc_count += p->pc_count;
  2024.               p->count -= p->pc_count;
  2025.               p->pc_count = 0;
  2026.               count += p->count;
  2027.               if (p->count == 0)
  2028.                 *pp = p->next;
  2029.               else
  2030.                 pp = &p->next;
  2031.             }
  2032.  
  2033.           if (pc_count || count)
  2034.             {
  2035.               h->needs_plt = 1;
  2036.               h->non_got_ref = 1;
  2037.               if (h->plt.refcount <= 0)
  2038.                 h->plt.refcount = 1;
  2039.               else
  2040.                 h->plt.refcount += 1;
  2041.             }
  2042.         }
  2043.  
  2044.       if (h->plt.refcount <= 0)
  2045.         {
  2046.           h->plt.offset = (bfd_vma) -1;
  2047.           h->needs_plt = 0;
  2048.         }
  2049.       return TRUE;
  2050.     }
  2051.  
  2052.   /* If this is a function, put it in the procedure linkage table.  We
  2053.      will fill in the contents of the procedure linkage table later,
  2054.      when we know the address of the .got section.  */
  2055.   if (h->type == STT_FUNC
  2056.       || h->needs_plt)
  2057.     {
  2058.       if (h->plt.refcount <= 0
  2059.           || SYMBOL_CALLS_LOCAL (info, h)
  2060.           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  2061.               && h->root.type == bfd_link_hash_undefweak))
  2062.         {
  2063.           /* This case can occur if we saw a PLT32 reloc in an input
  2064.              file, but the symbol was never referred to by a dynamic
  2065.              object, or if all references were garbage collected.  In
  2066.              such a case, we don't actually need to build a procedure
  2067.              linkage table, and we can just do a PC32 reloc instead.  */
  2068.           h->plt.offset = (bfd_vma) -1;
  2069.           h->needs_plt = 0;
  2070.         }
  2071.  
  2072.       return TRUE;
  2073.     }
  2074.   else
  2075.     /* It's possible that we incorrectly decided a .plt reloc was
  2076.        needed for an R_386_PC32 reloc to a non-function sym in
  2077.        check_relocs.  We can't decide accurately between function and
  2078.        non-function syms in check-relocs;  Objects loaded later in
  2079.        the link may change h->type.  So fix it now.  */
  2080.     h->plt.offset = (bfd_vma) -1;
  2081.  
  2082.   /* If this is a weak symbol, and there is a real definition, the
  2083.      processor independent code will have arranged for us to see the
  2084.      real definition first, and we can just use the same value.  */
  2085.   if (h->u.weakdef != NULL)
  2086.     {
  2087.       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
  2088.                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
  2089.       h->root.u.def.section = h->u.weakdef->root.u.def.section;
  2090.       h->root.u.def.value = h->u.weakdef->root.u.def.value;
  2091.       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
  2092.         h->non_got_ref = h->u.weakdef->non_got_ref;
  2093.       return TRUE;
  2094.     }
  2095.  
  2096.   /* This is a reference to a symbol defined by a dynamic object which
  2097.      is not a function.  */
  2098.  
  2099.   /* If we are creating a shared library, we must presume that the
  2100.      only references to the symbol are via the global offset table.
  2101.      For such cases we need not do anything here; the relocations will
  2102.      be handled correctly by relocate_section.  */
  2103.   if (info->shared)
  2104.     return TRUE;
  2105.  
  2106.   /* If there are no references to this symbol that do not use the
  2107.      GOT, we don't need to generate a copy reloc.  */
  2108.   if (!h->non_got_ref)
  2109.     return TRUE;
  2110.  
  2111.   /* If -z nocopyreloc was given, we won't generate them either.  */
  2112.   if (info->nocopyreloc)
  2113.     {
  2114.       h->non_got_ref = 0;
  2115.       return TRUE;
  2116.     }
  2117.  
  2118.   htab = elf_i386_hash_table (info);
  2119.   if (htab == NULL)
  2120.     return FALSE;
  2121.  
  2122.   /* If there aren't any dynamic relocs in read-only sections, then
  2123.      we can keep the dynamic relocs and avoid the copy reloc.  This
  2124.      doesn't work on VxWorks, where we can not have dynamic relocations
  2125.      (other than copy and jump slot relocations) in an executable.  */
  2126.   if (ELIMINATE_COPY_RELOCS
  2127.       && !get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
  2128.     {
  2129.       eh = (struct elf_i386_link_hash_entry *) h;
  2130.       for (p = eh->dyn_relocs; p != NULL; p = p->next)
  2131.         {
  2132.           s = p->sec->output_section;
  2133.           if (s != NULL && (s->flags & SEC_READONLY) != 0)
  2134.             break;
  2135.         }
  2136.  
  2137.       if (p == NULL)
  2138.         {
  2139.           h->non_got_ref = 0;
  2140.           return TRUE;
  2141.         }
  2142.     }
  2143.  
  2144.   /* We must allocate the symbol in our .dynbss section, which will
  2145.      become part of the .bss section of the executable.  There will be
  2146.      an entry for this symbol in the .dynsym section.  The dynamic
  2147.      object will contain position independent code, so all references
  2148.      from the dynamic object to this symbol will go through the global
  2149.      offset table.  The dynamic linker will use the .dynsym entry to
  2150.      determine the address it must put in the global offset table, so
  2151.      both the dynamic object and the regular object will refer to the
  2152.      same memory location for the variable.  */
  2153.  
  2154.   /* We must generate a R_386_COPY reloc to tell the dynamic linker to
  2155.      copy the initial value out of the dynamic object and into the
  2156.      runtime process image.  */
  2157.   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  2158.     {
  2159.       htab->srelbss->size += sizeof (Elf32_External_Rel);
  2160.       h->needs_copy = 1;
  2161.     }
  2162.  
  2163.   s = htab->sdynbss;
  2164.  
  2165.   return _bfd_elf_adjust_dynamic_copy (h, s);
  2166. }
  2167.  
  2168. /* Allocate space in .plt, .got and associated reloc sections for
  2169.    dynamic relocs.  */
  2170.  
  2171. static bfd_boolean
  2172. elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
  2173. {
  2174.   struct bfd_link_info *info;
  2175.   struct elf_i386_link_hash_table *htab;
  2176.   struct elf_i386_link_hash_entry *eh;
  2177.   struct elf_dyn_relocs *p;
  2178.   unsigned plt_entry_size;
  2179.  
  2180.   if (h->root.type == bfd_link_hash_indirect)
  2181.     return TRUE;
  2182.  
  2183.   eh = (struct elf_i386_link_hash_entry *) h;
  2184.  
  2185.   info = (struct bfd_link_info *) inf;
  2186.   htab = elf_i386_hash_table (info);
  2187.   if (htab == NULL)
  2188.     return FALSE;
  2189.  
  2190.   plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
  2191.  
  2192.   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
  2193.      here if it is defined and referenced in a non-shared object.  */
  2194.   if (h->type == STT_GNU_IFUNC
  2195.       && h->def_regular)
  2196.     return _bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs,
  2197.                                                plt_entry_size,
  2198.                                                plt_entry_size, 4);
  2199.   else if (htab->elf.dynamic_sections_created
  2200.            && h->plt.refcount > 0)
  2201.     {
  2202.       /* Make sure this symbol is output as a dynamic symbol.
  2203.          Undefined weak syms won't yet be marked as dynamic.  */
  2204.       if (h->dynindx == -1
  2205.           && !h->forced_local)
  2206.         {
  2207.           if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2208.             return FALSE;
  2209.         }
  2210.  
  2211.       if (info->shared
  2212.           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
  2213.         {
  2214.           asection *s = htab->elf.splt;
  2215.  
  2216.           /* If this is the first .plt entry, make room for the special
  2217.              first entry.  */
  2218.           if (s->size == 0)
  2219.             s->size += plt_entry_size;
  2220.  
  2221.           h->plt.offset = s->size;
  2222.  
  2223.           /* If this symbol is not defined in a regular file, and we are
  2224.              not generating a shared library, then set the symbol to this
  2225.              location in the .plt.  This is required to make function
  2226.              pointers compare as equal between the normal executable and
  2227.              the shared library.  */
  2228.           if (! info->shared
  2229.               && !h->def_regular)
  2230.             {
  2231.               h->root.u.def.section = s;
  2232.               h->root.u.def.value = h->plt.offset;
  2233.             }
  2234.  
  2235.           /* Make room for this entry.  */
  2236.           s->size += plt_entry_size;
  2237.  
  2238.           /* We also need to make an entry in the .got.plt section, which
  2239.              will be placed in the .got section by the linker script.  */
  2240.           htab->elf.sgotplt->size += 4;
  2241.  
  2242.           /* We also need to make an entry in the .rel.plt section.  */
  2243.           htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
  2244.           htab->elf.srelplt->reloc_count++;
  2245.  
  2246.           if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks
  2247.               && !info->shared)
  2248.             {
  2249.               /* VxWorks has a second set of relocations for each PLT entry
  2250.                  in executables.  They go in a separate relocation section,
  2251.                  which is processed by the kernel loader.  */
  2252.  
  2253.               /* There are two relocations for the initial PLT entry: an
  2254.                  R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
  2255.                  R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
  2256.  
  2257.               if (h->plt.offset == plt_entry_size)
  2258.                 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
  2259.  
  2260.               /* There are two extra relocations for each subsequent PLT entry:
  2261.                  an R_386_32 relocation for the GOT entry, and an R_386_32
  2262.                  relocation for the PLT entry.  */
  2263.  
  2264.               htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
  2265.             }
  2266.         }
  2267.       else
  2268.         {
  2269.           h->plt.offset = (bfd_vma) -1;
  2270.           h->needs_plt = 0;
  2271.         }
  2272.     }
  2273.   else
  2274.     {
  2275.       h->plt.offset = (bfd_vma) -1;
  2276.       h->needs_plt = 0;
  2277.     }
  2278.  
  2279.   eh->tlsdesc_got = (bfd_vma) -1;
  2280.  
  2281.   /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
  2282.      make it a R_386_TLS_LE_32 requiring no TLS entry.  */
  2283.   if (h->got.refcount > 0
  2284.       && info->executable
  2285.       && h->dynindx == -1
  2286.       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
  2287.     h->got.offset = (bfd_vma) -1;
  2288.   else if (h->got.refcount > 0)
  2289.     {
  2290.       asection *s;
  2291.       bfd_boolean dyn;
  2292.       int tls_type = elf_i386_hash_entry(h)->tls_type;
  2293.  
  2294.       /* Make sure this symbol is output as a dynamic symbol.
  2295.          Undefined weak syms won't yet be marked as dynamic.  */
  2296.       if (h->dynindx == -1
  2297.           && !h->forced_local)
  2298.         {
  2299.           if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2300.             return FALSE;
  2301.         }
  2302.  
  2303.       s = htab->elf.sgot;
  2304.       if (GOT_TLS_GDESC_P (tls_type))
  2305.         {
  2306.           eh->tlsdesc_got = htab->elf.sgotplt->size
  2307.             - elf_i386_compute_jump_table_size (htab);
  2308.           htab->elf.sgotplt->size += 8;
  2309.           h->got.offset = (bfd_vma) -2;
  2310.         }
  2311.       if (! GOT_TLS_GDESC_P (tls_type)
  2312.           || GOT_TLS_GD_P (tls_type))
  2313.         {
  2314.           h->got.offset = s->size;
  2315.           s->size += 4;
  2316.           /* R_386_TLS_GD needs 2 consecutive GOT slots.  */
  2317.           if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
  2318.             s->size += 4;
  2319.         }
  2320.       dyn = htab->elf.dynamic_sections_created;
  2321.       /* R_386_TLS_IE_32 needs one dynamic relocation,
  2322.          R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
  2323.          (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
  2324.          need two), R_386_TLS_GD needs one if local symbol and two if
  2325.          global.  */
  2326.       if (tls_type == GOT_TLS_IE_BOTH)
  2327.         htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
  2328.       else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
  2329.                || (tls_type & GOT_TLS_IE))
  2330.         htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
  2331.       else if (GOT_TLS_GD_P (tls_type))
  2332.         htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
  2333.       else if (! GOT_TLS_GDESC_P (tls_type)
  2334.                && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  2335.                    || h->root.type != bfd_link_hash_undefweak)
  2336.                && (info->shared
  2337.                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
  2338.         htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
  2339.       if (GOT_TLS_GDESC_P (tls_type))
  2340.         htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
  2341.     }
  2342.   else
  2343.     h->got.offset = (bfd_vma) -1;
  2344.  
  2345.   if (eh->dyn_relocs == NULL)
  2346.     return TRUE;
  2347.  
  2348.   /* In the shared -Bsymbolic case, discard space allocated for
  2349.      dynamic pc-relative relocs against symbols which turn out to be
  2350.      defined in regular objects.  For the normal shared case, discard
  2351.      space for pc-relative relocs that have become local due to symbol
  2352.      visibility changes.  */
  2353.  
  2354.   if (info->shared)
  2355.     {
  2356.       /* The only reloc that uses pc_count is R_386_PC32, which will
  2357.          appear on a call or on something like ".long foo - .".  We
  2358.          want calls to protected symbols to resolve directly to the
  2359.          function rather than going via the plt.  If people want
  2360.          function pointer comparisons to work as expected then they
  2361.          should avoid writing assembly like ".long foo - .".  */
  2362.       if (SYMBOL_CALLS_LOCAL (info, h))
  2363.         {
  2364.           struct elf_dyn_relocs **pp;
  2365.  
  2366.           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
  2367.             {
  2368.               p->count -= p->pc_count;
  2369.               p->pc_count = 0;
  2370.               if (p->count == 0)
  2371.                 *pp = p->next;
  2372.               else
  2373.                 pp = &p->next;
  2374.             }
  2375.         }
  2376.  
  2377.       if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks)
  2378.         {
  2379.           struct elf_dyn_relocs **pp;
  2380.           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
  2381.             {
  2382.               if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
  2383.                 *pp = p->next;
  2384.               else
  2385.                 pp = &p->next;
  2386.             }
  2387.         }
  2388.  
  2389.       /* Also discard relocs on undefined weak syms with non-default
  2390.          visibility.  */
  2391.       if (eh->dyn_relocs != NULL
  2392.           && h->root.type == bfd_link_hash_undefweak)
  2393.         {
  2394.           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  2395.             eh->dyn_relocs = NULL;
  2396.  
  2397.           /* Make sure undefined weak symbols are output as a dynamic
  2398.              symbol in PIEs.  */
  2399.           else if (h->dynindx == -1
  2400.                    && !h->forced_local)
  2401.             {
  2402.               if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2403.                 return FALSE;
  2404.             }
  2405.         }
  2406.     }
  2407.   else if (ELIMINATE_COPY_RELOCS)
  2408.     {
  2409.       /* For the non-shared case, discard space for relocs against
  2410.          symbols which turn out to need copy relocs or are not
  2411.          dynamic.  */
  2412.  
  2413.       if (!h->non_got_ref
  2414.           && ((h->def_dynamic
  2415.                && !h->def_regular)
  2416.               || (htab->elf.dynamic_sections_created
  2417.                   && (h->root.type == bfd_link_hash_undefweak
  2418.                       || h->root.type == bfd_link_hash_undefined))))
  2419.         {
  2420.           /* Make sure this symbol is output as a dynamic symbol.
  2421.              Undefined weak syms won't yet be marked as dynamic.  */
  2422.           if (h->dynindx == -1
  2423.               && !h->forced_local)
  2424.             {
  2425.               if (! bfd_elf_link_record_dynamic_symbol (info, h))
  2426.                 return FALSE;
  2427.             }
  2428.  
  2429.           /* If that succeeded, we know we'll be keeping all the
  2430.              relocs.  */
  2431.           if (h->dynindx != -1)
  2432.             goto keep;
  2433.         }
  2434.  
  2435.       eh->dyn_relocs = NULL;
  2436.  
  2437.     keep: ;
  2438.     }
  2439.  
  2440.   /* Finally, allocate space.  */
  2441.   for (p = eh->dyn_relocs; p != NULL; p = p->next)
  2442.     {
  2443.       asection *sreloc;
  2444.  
  2445.       sreloc = elf_section_data (p->sec)->sreloc;
  2446.  
  2447.       BFD_ASSERT (sreloc != NULL);
  2448.       sreloc->size += p->count * sizeof (Elf32_External_Rel);
  2449.     }
  2450.  
  2451.   return TRUE;
  2452. }
  2453.  
  2454. /* Allocate space in .plt, .got and associated reloc sections for
  2455.    local dynamic relocs.  */
  2456.  
  2457. static bfd_boolean
  2458. elf_i386_allocate_local_dynrelocs (void **slot, void *inf)
  2459. {
  2460.   struct elf_link_hash_entry *h
  2461.     = (struct elf_link_hash_entry *) *slot;
  2462.  
  2463.   if (h->type != STT_GNU_IFUNC
  2464.       || !h->def_regular
  2465.       || !h->ref_regular
  2466.       || !h->forced_local
  2467.       || h->root.type != bfd_link_hash_defined)
  2468.     abort ();
  2469.  
  2470.   return elf_i386_allocate_dynrelocs (h, inf);
  2471. }
  2472.  
  2473. /* Find any dynamic relocs that apply to read-only sections.  */
  2474.  
  2475. static bfd_boolean
  2476. elf_i386_readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
  2477. {
  2478.   struct elf_i386_link_hash_entry *eh;
  2479.   struct elf_dyn_relocs *p;
  2480.  
  2481.   /* Skip local IFUNC symbols. */
  2482.   if (h->forced_local && h->type == STT_GNU_IFUNC)
  2483.     return TRUE;
  2484.  
  2485.   eh = (struct elf_i386_link_hash_entry *) h;
  2486.   for (p = eh->dyn_relocs; p != NULL; p = p->next)
  2487.     {
  2488.       asection *s = p->sec->output_section;
  2489.  
  2490.       if (s != NULL && (s->flags & SEC_READONLY) != 0)
  2491.         {
  2492.           struct bfd_link_info *info = (struct bfd_link_info *) inf;
  2493.  
  2494.           info->flags |= DF_TEXTREL;
  2495.  
  2496.           if (info->warn_shared_textrel && info->shared)
  2497.             info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'.\n"),
  2498.                                     p->sec->owner, h->root.root.string,
  2499.                                     p->sec);
  2500.  
  2501.           /* Not an error, just cut short the traversal.  */
  2502.           return FALSE;
  2503.         }
  2504.     }
  2505.   return TRUE;
  2506. }
  2507.  
  2508. /* Convert
  2509.    mov foo@GOT(%reg), %reg
  2510.    to
  2511.    lea foo@GOTOFF(%reg), %reg
  2512.    with the local symbol, foo.  */
  2513.  
  2514. static bfd_boolean
  2515. elf_i386_convert_mov_to_lea (bfd *abfd, asection *sec,
  2516.                              struct bfd_link_info *link_info)
  2517. {
  2518.   Elf_Internal_Shdr *symtab_hdr;
  2519.   Elf_Internal_Rela *internal_relocs;
  2520.   Elf_Internal_Rela *irel, *irelend;
  2521.   bfd_byte *contents;
  2522.   struct elf_i386_link_hash_table *htab;
  2523.   bfd_boolean changed_contents;
  2524.   bfd_boolean changed_relocs;
  2525.   bfd_signed_vma *local_got_refcounts;
  2526.  
  2527.   /* Don't even try to convert non-ELF outputs.  */
  2528.   if (!is_elf_hash_table (link_info->hash))
  2529.     return FALSE;
  2530.  
  2531.   /* Nothing to do if there are no codes, no relocations or no output.  */
  2532.   if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
  2533.       || sec->reloc_count == 0
  2534.       || discarded_section (sec))
  2535.     return TRUE;
  2536.  
  2537.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2538.  
  2539.   /* Load the relocations for this section.  */
  2540.   internal_relocs = (_bfd_elf_link_read_relocs
  2541.                      (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
  2542.                       link_info->keep_memory));
  2543.   if (internal_relocs == NULL)
  2544.     return FALSE;
  2545.  
  2546.   htab = elf_i386_hash_table (link_info);
  2547.   changed_contents = FALSE;
  2548.   changed_relocs = FALSE;
  2549.   local_got_refcounts = elf_local_got_refcounts (abfd);
  2550.  
  2551.   /* Get the section contents.  */
  2552.   if (elf_section_data (sec)->this_hdr.contents != NULL)
  2553.     contents = elf_section_data (sec)->this_hdr.contents;
  2554.   else
  2555.     {
  2556.       if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  2557.         goto error_return;
  2558.     }
  2559.  
  2560.   irelend = internal_relocs + sec->reloc_count;
  2561.   for (irel = internal_relocs; irel < irelend; irel++)
  2562.     {
  2563.       unsigned int r_type = ELF32_R_TYPE (irel->r_info);
  2564.       unsigned int r_symndx = ELF32_R_SYM (irel->r_info);
  2565.       unsigned int indx;
  2566.       struct elf_link_hash_entry *h;
  2567.  
  2568.       if (r_type != R_386_GOT32)
  2569.         continue;
  2570.  
  2571.       /* Get the symbol referred to by the reloc.  */
  2572.       if (r_symndx < symtab_hdr->sh_info)
  2573.         {
  2574.           Elf_Internal_Sym *isym;
  2575.  
  2576.           isym = bfd_sym_from_r_symndx (&htab->sym_cache,
  2577.                                         abfd, r_symndx);
  2578.  
  2579.           /* STT_GNU_IFUNC must keep R_386_GOT32 relocation.  */
  2580.           if (ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC
  2581.               && bfd_get_8 (input_bfd,
  2582.                             contents + irel->r_offset - 2) == 0x8b)
  2583.             {
  2584.               bfd_put_8 (output_bfd, 0x8d,
  2585.                          contents + irel->r_offset - 2);
  2586.               irel->r_info = ELF32_R_INFO (r_symndx, R_386_GOTOFF);
  2587.               if (local_got_refcounts != NULL
  2588.                   && local_got_refcounts[r_symndx] > 0)
  2589.                 local_got_refcounts[r_symndx] -= 1;
  2590.               changed_contents = TRUE;
  2591.               changed_relocs = TRUE;
  2592.             }
  2593.           continue;
  2594.         }
  2595.  
  2596.       indx = r_symndx - symtab_hdr->sh_info;
  2597.       h = elf_sym_hashes (abfd)[indx];
  2598.       BFD_ASSERT (h != NULL);
  2599.  
  2600.       while (h->root.type == bfd_link_hash_indirect
  2601.              || h->root.type == bfd_link_hash_warning)
  2602.         h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2603.  
  2604.       /* STT_GNU_IFUNC must keep R_386_GOT32 relocation.  We also avoid
  2605.          optimizing _DYNAMIC since ld.so may use its link-time address.  */
  2606.       if (h->def_regular
  2607.           && h->type != STT_GNU_IFUNC
  2608.           && h != htab->elf.hdynamic
  2609.           && SYMBOL_REFERENCES_LOCAL (link_info, h)
  2610.           && bfd_get_8 (input_bfd,
  2611.                         contents + irel->r_offset - 2) == 0x8b)
  2612.         {
  2613.           bfd_put_8 (output_bfd, 0x8d,
  2614.                      contents + irel->r_offset - 2);
  2615.           irel->r_info = ELF32_R_INFO (r_symndx, R_386_GOTOFF);
  2616.           if (h->got.refcount > 0)
  2617.             h->got.refcount -= 1;
  2618.           changed_contents = TRUE;
  2619.           changed_relocs = TRUE;
  2620.         }
  2621.     }
  2622.  
  2623.   if (contents != NULL
  2624.       && elf_section_data (sec)->this_hdr.contents != contents)
  2625.     {
  2626.       if (!changed_contents && !link_info->keep_memory)
  2627.         free (contents);
  2628.       else
  2629.         {
  2630.           /* Cache the section contents for elf_link_input_bfd.  */
  2631.           elf_section_data (sec)->this_hdr.contents = contents;
  2632.         }
  2633.     }
  2634.  
  2635.   if (elf_section_data (sec)->relocs != internal_relocs)
  2636.     {
  2637.       if (!changed_relocs)
  2638.         free (internal_relocs);
  2639.       else
  2640.         elf_section_data (sec)->relocs = internal_relocs;
  2641.     }
  2642.  
  2643.   return TRUE;
  2644.  
  2645.  error_return:
  2646.   if (contents != NULL
  2647.       && elf_section_data (sec)->this_hdr.contents != contents)
  2648.     free (contents);
  2649.   if (internal_relocs != NULL
  2650.       && elf_section_data (sec)->relocs != internal_relocs)
  2651.     free (internal_relocs);
  2652.   return FALSE;
  2653. }
  2654.  
  2655. /* Set the sizes of the dynamic sections.  */
  2656.  
  2657. static bfd_boolean
  2658. elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
  2659. {
  2660.   struct elf_i386_link_hash_table *htab;
  2661.   bfd *dynobj;
  2662.   asection *s;
  2663.   bfd_boolean relocs;
  2664.   bfd *ibfd;
  2665.  
  2666.   htab = elf_i386_hash_table (info);
  2667.   if (htab == NULL)
  2668.     return FALSE;
  2669.   dynobj = htab->elf.dynobj;
  2670.   if (dynobj == NULL)
  2671.     abort ();
  2672.  
  2673.   if (htab->elf.dynamic_sections_created)
  2674.     {
  2675.       /* Set the contents of the .interp section to the interpreter.  */
  2676.       if (info->executable)
  2677.         {
  2678.           s = bfd_get_linker_section (dynobj, ".interp");
  2679.           if (s == NULL)
  2680.             abort ();
  2681.           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  2682.           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  2683.         }
  2684.     }
  2685.  
  2686.   /* Set up .got offsets for local syms, and space for local dynamic
  2687.      relocs.  */
  2688.   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
  2689.     {
  2690.       bfd_signed_vma *local_got;
  2691.       bfd_signed_vma *end_local_got;
  2692.       char *local_tls_type;
  2693.       bfd_vma *local_tlsdesc_gotent;
  2694.       bfd_size_type locsymcount;
  2695.       Elf_Internal_Shdr *symtab_hdr;
  2696.       asection *srel;
  2697.  
  2698.       if (! is_i386_elf (ibfd))
  2699.         continue;
  2700.  
  2701.       for (s = ibfd->sections; s != NULL; s = s->next)
  2702.         {
  2703.           struct elf_dyn_relocs *p;
  2704.  
  2705.           if (!elf_i386_convert_mov_to_lea (ibfd, s, info))
  2706.             return FALSE;
  2707.  
  2708.           for (p = ((struct elf_dyn_relocs *)
  2709.                      elf_section_data (s)->local_dynrel);
  2710.                p != NULL;
  2711.                p = p->next)
  2712.             {
  2713.               if (!bfd_is_abs_section (p->sec)
  2714.                   && bfd_is_abs_section (p->sec->output_section))
  2715.                 {
  2716.                   /* Input section has been discarded, either because
  2717.                      it is a copy of a linkonce section or due to
  2718.                      linker script /DISCARD/, so we'll be discarding
  2719.                      the relocs too.  */
  2720.                 }
  2721.               else if (get_elf_i386_backend_data (output_bfd)->is_vxworks
  2722.                        && strcmp (p->sec->output_section->name,
  2723.                                   ".tls_vars") == 0)
  2724.                 {
  2725.                   /* Relocations in vxworks .tls_vars sections are
  2726.                      handled specially by the loader.  */
  2727.                 }
  2728.               else if (p->count != 0)
  2729.                 {
  2730.                   srel = elf_section_data (p->sec)->sreloc;
  2731.                   srel->size += p->count * sizeof (Elf32_External_Rel);
  2732.                   if ((p->sec->output_section->flags & SEC_READONLY) != 0
  2733.                       && (info->flags & DF_TEXTREL) == 0)
  2734.                     {
  2735.                       info->flags |= DF_TEXTREL;
  2736.                       if (info->warn_shared_textrel && info->shared)
  2737.                         info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'.\n"),
  2738.                                                 p->sec->owner, p->sec);
  2739.                     }
  2740.                 }
  2741.             }
  2742.         }
  2743.  
  2744.       local_got = elf_local_got_refcounts (ibfd);
  2745.       if (!local_got)
  2746.         continue;
  2747.  
  2748.       symtab_hdr = &elf_symtab_hdr (ibfd);
  2749.       locsymcount = symtab_hdr->sh_info;
  2750.       end_local_got = local_got + locsymcount;
  2751.       local_tls_type = elf_i386_local_got_tls_type (ibfd);
  2752.       local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
  2753.       s = htab->elf.sgot;
  2754.       srel = htab->elf.srelgot;
  2755.       for (; local_got < end_local_got;
  2756.            ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
  2757.         {
  2758.           *local_tlsdesc_gotent = (bfd_vma) -1;
  2759.           if (*local_got > 0)
  2760.             {
  2761.               if (GOT_TLS_GDESC_P (*local_tls_type))
  2762.                 {
  2763.                   *local_tlsdesc_gotent = htab->elf.sgotplt->size
  2764.                     - elf_i386_compute_jump_table_size (htab);
  2765.                   htab->elf.sgotplt->size += 8;
  2766.                   *local_got = (bfd_vma) -2;
  2767.                 }
  2768.               if (! GOT_TLS_GDESC_P (*local_tls_type)
  2769.                   || GOT_TLS_GD_P (*local_tls_type))
  2770.                 {
  2771.                   *local_got = s->size;
  2772.                   s->size += 4;
  2773.                   if (GOT_TLS_GD_P (*local_tls_type)
  2774.                       || *local_tls_type == GOT_TLS_IE_BOTH)
  2775.                     s->size += 4;
  2776.                 }
  2777.               if (info->shared
  2778.                   || GOT_TLS_GD_ANY_P (*local_tls_type)
  2779.                   || (*local_tls_type & GOT_TLS_IE))
  2780.                 {
  2781.                   if (*local_tls_type == GOT_TLS_IE_BOTH)
  2782.                     srel->size += 2 * sizeof (Elf32_External_Rel);
  2783.                   else if (GOT_TLS_GD_P (*local_tls_type)
  2784.                            || ! GOT_TLS_GDESC_P (*local_tls_type))
  2785.                     srel->size += sizeof (Elf32_External_Rel);
  2786.                   if (GOT_TLS_GDESC_P (*local_tls_type))
  2787.                     htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
  2788.                 }
  2789.             }
  2790.           else
  2791.             *local_got = (bfd_vma) -1;
  2792.         }
  2793.     }
  2794.  
  2795.   if (htab->tls_ldm_got.refcount > 0)
  2796.     {
  2797.       /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
  2798.          relocs.  */
  2799.       htab->tls_ldm_got.offset = htab->elf.sgot->size;
  2800.       htab->elf.sgot->size += 8;
  2801.       htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
  2802.     }
  2803.   else
  2804.     htab->tls_ldm_got.offset = -1;
  2805.  
  2806.   /* Allocate global sym .plt and .got entries, and space for global
  2807.      sym dynamic relocs.  */
  2808.   elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info);
  2809.  
  2810.   /* Allocate .plt and .got entries, and space for local symbols.  */
  2811.   htab_traverse (htab->loc_hash_table,
  2812.                  elf_i386_allocate_local_dynrelocs,
  2813.                  info);
  2814.  
  2815.   /* For every jump slot reserved in the sgotplt, reloc_count is
  2816.      incremented.  However, when we reserve space for TLS descriptors,
  2817.      it's not incremented, so in order to compute the space reserved
  2818.      for them, it suffices to multiply the reloc count by the jump
  2819.      slot size.
  2820.  
  2821.      PR ld/13302: We start next_irelative_index at the end of .rela.plt
  2822.      so that R_386_IRELATIVE entries come last.  */
  2823.   if (htab->elf.srelplt)
  2824.     {
  2825.       htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
  2826.       htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
  2827.       htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
  2828.     }
  2829.   else if (htab->elf.irelplt)
  2830.     htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
  2831.  
  2832.  
  2833.   if (htab->elf.sgotplt)
  2834.     {
  2835.       /* Don't allocate .got.plt section if there are no GOT nor PLT
  2836.          entries and there is no reference to _GLOBAL_OFFSET_TABLE_.  */
  2837.       if ((htab->elf.hgot == NULL
  2838.            || !htab->elf.hgot->ref_regular_nonweak)
  2839.           && (htab->elf.sgotplt->size
  2840.               == get_elf_backend_data (output_bfd)->got_header_size)
  2841.           && (htab->elf.splt == NULL
  2842.               || htab->elf.splt->size == 0)
  2843.           && (htab->elf.sgot == NULL
  2844.               || htab->elf.sgot->size == 0)
  2845.           && (htab->elf.iplt == NULL
  2846.               || htab->elf.iplt->size == 0)
  2847.           && (htab->elf.igotplt == NULL
  2848.               || htab->elf.igotplt->size == 0))
  2849.         htab->elf.sgotplt->size = 0;
  2850.     }
  2851.  
  2852.  
  2853.   if (htab->plt_eh_frame != NULL
  2854.       && htab->elf.splt != NULL
  2855.       && htab->elf.splt->size != 0
  2856.       && !bfd_is_abs_section (htab->elf.splt->output_section)
  2857.       && _bfd_elf_eh_frame_present (info))
  2858.     htab->plt_eh_frame->size = sizeof (elf_i386_eh_frame_plt);
  2859.  
  2860.   /* We now have determined the sizes of the various dynamic sections.
  2861.      Allocate memory for them.  */
  2862.   relocs = FALSE;
  2863.   for (s = dynobj->sections; s != NULL; s = s->next)
  2864.     {
  2865.       bfd_boolean strip_section = TRUE;
  2866.  
  2867.       if ((s->flags & SEC_LINKER_CREATED) == 0)
  2868.         continue;
  2869.  
  2870.       if (s == htab->elf.splt
  2871.           || s == htab->elf.sgot)
  2872.         {
  2873.           /* Strip this section if we don't need it; see the
  2874.              comment below.  */
  2875.           /* We'd like to strip these sections if they aren't needed, but if
  2876.              we've exported dynamic symbols from them we must leave them.
  2877.              It's too late to tell BFD to get rid of the symbols.  */
  2878.  
  2879.           if (htab->elf.hplt != NULL)
  2880.             strip_section = FALSE;
  2881.         }
  2882.       else if (s == htab->elf.sgotplt
  2883.                || s == htab->elf.iplt
  2884.                || s == htab->elf.igotplt
  2885.                || s == htab->plt_eh_frame
  2886.                || s == htab->sdynbss)
  2887.         {
  2888.           /* Strip these too.  */
  2889.         }
  2890.       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
  2891.         {
  2892.           if (s->size != 0
  2893.               && s != htab->elf.srelplt
  2894.               && s != htab->srelplt2)
  2895.             relocs = TRUE;
  2896.  
  2897.           /* We use the reloc_count field as a counter if we need
  2898.              to copy relocs into the output file.  */
  2899.           s->reloc_count = 0;
  2900.         }
  2901.       else
  2902.         {
  2903.           /* It's not one of our sections, so don't allocate space.  */
  2904.           continue;
  2905.         }
  2906.  
  2907.       if (s->size == 0)
  2908.         {
  2909.           /* If we don't need this section, strip it from the
  2910.              output file.  This is mostly to handle .rel.bss and
  2911.              .rel.plt.  We must create both sections in
  2912.              create_dynamic_sections, because they must be created
  2913.              before the linker maps input sections to output
  2914.              sections.  The linker does that before
  2915.              adjust_dynamic_symbol is called, and it is that
  2916.              function which decides whether anything needs to go
  2917.              into these sections.  */
  2918.           if (strip_section)
  2919.             s->flags |= SEC_EXCLUDE;
  2920.           continue;
  2921.         }
  2922.  
  2923.       if ((s->flags & SEC_HAS_CONTENTS) == 0)
  2924.         continue;
  2925.  
  2926.       /* Allocate memory for the section contents.  We use bfd_zalloc
  2927.          here in case unused entries are not reclaimed before the
  2928.          section's contents are written out.  This should not happen,
  2929.          but this way if it does, we get a R_386_NONE reloc instead
  2930.          of garbage.  */
  2931.       s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
  2932.       if (s->contents == NULL)
  2933.         return FALSE;
  2934.     }
  2935.  
  2936.   if (htab->plt_eh_frame != NULL
  2937.       && htab->plt_eh_frame->contents != NULL)
  2938.     {
  2939.       memcpy (htab->plt_eh_frame->contents, elf_i386_eh_frame_plt,
  2940.               sizeof (elf_i386_eh_frame_plt));
  2941.       bfd_put_32 (dynobj, htab->elf.splt->size,
  2942.                   htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
  2943.     }
  2944.  
  2945.   if (htab->elf.dynamic_sections_created)
  2946.     {
  2947.       /* Add some entries to the .dynamic section.  We fill in the
  2948.          values later, in elf_i386_finish_dynamic_sections, but we
  2949.          must add the entries now so that we get the correct size for
  2950.          the .dynamic section.  The DT_DEBUG entry is filled in by the
  2951.          dynamic linker and used by the debugger.  */
  2952. #define add_dynamic_entry(TAG, VAL) \
  2953.   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  2954.  
  2955.       if (info->executable)
  2956.         {
  2957.           if (!add_dynamic_entry (DT_DEBUG, 0))
  2958.             return FALSE;
  2959.         }
  2960.  
  2961.       if (htab->elf.splt->size != 0)
  2962.         {
  2963.           if (!add_dynamic_entry (DT_PLTGOT, 0)
  2964.               || !add_dynamic_entry (DT_PLTRELSZ, 0)
  2965.               || !add_dynamic_entry (DT_PLTREL, DT_REL)
  2966.               || !add_dynamic_entry (DT_JMPREL, 0))
  2967.             return FALSE;
  2968.         }
  2969.  
  2970.       if (relocs)
  2971.         {
  2972.           if (!add_dynamic_entry (DT_REL, 0)
  2973.               || !add_dynamic_entry (DT_RELSZ, 0)
  2974.               || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
  2975.             return FALSE;
  2976.  
  2977.           /* If any dynamic relocs apply to a read-only section,
  2978.              then we need a DT_TEXTREL entry.  */
  2979.           if ((info->flags & DF_TEXTREL) == 0)
  2980.             elf_link_hash_traverse (&htab->elf,
  2981.                                     elf_i386_readonly_dynrelocs, info);
  2982.  
  2983.           if ((info->flags & DF_TEXTREL) != 0)
  2984.             {
  2985.               if (!add_dynamic_entry (DT_TEXTREL, 0))
  2986.                 return FALSE;
  2987.             }
  2988.         }
  2989.       if (get_elf_i386_backend_data (output_bfd)->is_vxworks
  2990.           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
  2991.         return FALSE;
  2992.     }
  2993. #undef add_dynamic_entry
  2994.  
  2995.   return TRUE;
  2996. }
  2997.  
  2998. static bfd_boolean
  2999. elf_i386_always_size_sections (bfd *output_bfd,
  3000.                                struct bfd_link_info *info)
  3001. {
  3002.   asection *tls_sec = elf_hash_table (info)->tls_sec;
  3003.  
  3004.   if (tls_sec)
  3005.     {
  3006.       struct elf_link_hash_entry *tlsbase;
  3007.  
  3008.       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
  3009.                                       "_TLS_MODULE_BASE_",
  3010.                                       FALSE, FALSE, FALSE);
  3011.  
  3012.       if (tlsbase && tlsbase->type == STT_TLS)
  3013.         {
  3014.           struct elf_i386_link_hash_table *htab;
  3015.           struct bfd_link_hash_entry *bh = NULL;
  3016.           const struct elf_backend_data *bed
  3017.             = get_elf_backend_data (output_bfd);
  3018.  
  3019.           htab = elf_i386_hash_table (info);
  3020.           if (htab == NULL)
  3021.             return FALSE;
  3022.  
  3023.           if (!(_bfd_generic_link_add_one_symbol
  3024.                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
  3025.                  tls_sec, 0, NULL, FALSE,
  3026.                  bed->collect, &bh)))
  3027.             return FALSE;
  3028.  
  3029.           htab->tls_module_base = bh;
  3030.  
  3031.           tlsbase = (struct elf_link_hash_entry *)bh;
  3032.           tlsbase->def_regular = 1;
  3033.           tlsbase->other = STV_HIDDEN;
  3034.           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
  3035.         }
  3036.     }
  3037.  
  3038.   return TRUE;
  3039. }
  3040.  
  3041. /* Set the correct type for an x86 ELF section.  We do this by the
  3042.    section name, which is a hack, but ought to work.  */
  3043.  
  3044. static bfd_boolean
  3045. elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
  3046.                         Elf_Internal_Shdr *hdr,
  3047.                         asection *sec)
  3048. {
  3049.   const char *name;
  3050.  
  3051.   name = bfd_get_section_name (abfd, sec);
  3052.  
  3053.   /* This is an ugly, but unfortunately necessary hack that is
  3054.      needed when producing EFI binaries on x86. It tells
  3055.      elf.c:elf_fake_sections() not to consider ".reloc" as a section
  3056.      containing ELF relocation info.  We need this hack in order to
  3057.      be able to generate ELF binaries that can be translated into
  3058.      EFI applications (which are essentially COFF objects).  Those
  3059.      files contain a COFF ".reloc" section inside an ELFNN object,
  3060.      which would normally cause BFD to segfault because it would
  3061.      attempt to interpret this section as containing relocation
  3062.      entries for section "oc".  With this hack enabled, ".reloc"
  3063.      will be treated as a normal data section, which will avoid the
  3064.      segfault.  However, you won't be able to create an ELFNN binary
  3065.      with a section named "oc" that needs relocations, but that's
  3066.      the kind of ugly side-effects you get when detecting section
  3067.      types based on their names...  In practice, this limitation is
  3068.      unlikely to bite.  */
  3069.   if (strcmp (name, ".reloc") == 0)
  3070.     hdr->sh_type = SHT_PROGBITS;
  3071.  
  3072.   return TRUE;
  3073. }
  3074.  
  3075. /* _TLS_MODULE_BASE_ needs to be treated especially when linking
  3076.    executables.  Rather than setting it to the beginning of the TLS
  3077.    section, we have to set it to the end.    This function may be called
  3078.    multiple times, it is idempotent.  */
  3079.  
  3080. static void
  3081. elf_i386_set_tls_module_base (struct bfd_link_info *info)
  3082. {
  3083.   struct elf_i386_link_hash_table *htab;
  3084.   struct bfd_link_hash_entry *base;
  3085.  
  3086.   if (!info->executable)
  3087.     return;
  3088.  
  3089.   htab = elf_i386_hash_table (info);
  3090.   if (htab == NULL)
  3091.     return;
  3092.  
  3093.   base = htab->tls_module_base;
  3094.   if (base == NULL)
  3095.     return;
  3096.  
  3097.   base->u.def.value = htab->elf.tls_size;
  3098. }
  3099.  
  3100. /* Return the base VMA address which should be subtracted from real addresses
  3101.    when resolving @dtpoff relocation.
  3102.    This is PT_TLS segment p_vaddr.  */
  3103.  
  3104. static bfd_vma
  3105. elf_i386_dtpoff_base (struct bfd_link_info *info)
  3106. {
  3107.   /* If tls_sec is NULL, we should have signalled an error already.  */
  3108.   if (elf_hash_table (info)->tls_sec == NULL)
  3109.     return 0;
  3110.   return elf_hash_table (info)->tls_sec->vma;
  3111. }
  3112.  
  3113. /* Return the relocation value for @tpoff relocation
  3114.    if STT_TLS virtual address is ADDRESS.  */
  3115.  
  3116. static bfd_vma
  3117. elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
  3118. {
  3119.   struct elf_link_hash_table *htab = elf_hash_table (info);
  3120.   const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
  3121.   bfd_vma static_tls_size;
  3122.  
  3123.   /* If tls_sec is NULL, we should have signalled an error already.  */
  3124.   if (htab->tls_sec == NULL)
  3125.     return 0;
  3126.  
  3127.   /* Consider special static TLS alignment requirements.  */
  3128.   static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
  3129.   return static_tls_size + htab->tls_sec->vma - address;
  3130. }
  3131.  
  3132. /* Relocate an i386 ELF section.  */
  3133.  
  3134. static bfd_boolean
  3135. elf_i386_relocate_section (bfd *output_bfd,
  3136.                            struct bfd_link_info *info,
  3137.                            bfd *input_bfd,
  3138.                            asection *input_section,
  3139.                            bfd_byte *contents,
  3140.                            Elf_Internal_Rela *relocs,
  3141.                            Elf_Internal_Sym *local_syms,
  3142.                            asection **local_sections)
  3143. {
  3144.   struct elf_i386_link_hash_table *htab;
  3145.   Elf_Internal_Shdr *symtab_hdr;
  3146.   struct elf_link_hash_entry **sym_hashes;
  3147.   bfd_vma *local_got_offsets;
  3148.   bfd_vma *local_tlsdesc_gotents;
  3149.   Elf_Internal_Rela *rel;
  3150.   Elf_Internal_Rela *relend;
  3151.   bfd_boolean is_vxworks_tls;
  3152.   unsigned plt_entry_size;
  3153.  
  3154.   BFD_ASSERT (is_i386_elf (input_bfd));
  3155.  
  3156.   htab = elf_i386_hash_table (info);
  3157.   if (htab == NULL)
  3158.     return FALSE;
  3159.   symtab_hdr = &elf_symtab_hdr (input_bfd);
  3160.   sym_hashes = elf_sym_hashes (input_bfd);
  3161.   local_got_offsets = elf_local_got_offsets (input_bfd);
  3162.   local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
  3163.   /* We have to handle relocations in vxworks .tls_vars sections
  3164.      specially, because the dynamic loader is 'weird'.  */
  3165.   is_vxworks_tls = (get_elf_i386_backend_data (output_bfd)->is_vxworks
  3166.                     && info->shared
  3167.                     && !strcmp (input_section->output_section->name,
  3168.                                 ".tls_vars"));
  3169.  
  3170.   elf_i386_set_tls_module_base (info);
  3171.  
  3172.   plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
  3173.  
  3174.   rel = relocs;
  3175.   relend = relocs + input_section->reloc_count;
  3176.   for (; rel < relend; rel++)
  3177.     {
  3178.       unsigned int r_type;
  3179.       reloc_howto_type *howto;
  3180.       unsigned long r_symndx;
  3181.       struct elf_link_hash_entry *h;
  3182.       Elf_Internal_Sym *sym;
  3183.       asection *sec;
  3184.       bfd_vma off, offplt;
  3185.       bfd_vma relocation;
  3186.       bfd_boolean unresolved_reloc;
  3187.       bfd_reloc_status_type r;
  3188.       unsigned int indx;
  3189.       int tls_type;
  3190.       bfd_vma st_size;
  3191.  
  3192.       r_type = ELF32_R_TYPE (rel->r_info);
  3193.       if (r_type == R_386_GNU_VTINHERIT
  3194.           || r_type == R_386_GNU_VTENTRY)
  3195.         continue;
  3196.  
  3197.       if ((indx = r_type) >= R_386_standard
  3198.           && ((indx = r_type - R_386_ext_offset) - R_386_standard
  3199.               >= R_386_ext - R_386_standard)
  3200.           && ((indx = r_type - R_386_tls_offset) - R_386_ext
  3201.               >= R_386_irelative - R_386_ext))
  3202.         {
  3203.           (*_bfd_error_handler)
  3204.             (_("%B: unrecognized relocation (0x%x) in section `%A'"),
  3205.              input_bfd, input_section, r_type);
  3206.           bfd_set_error (bfd_error_bad_value);
  3207.           return FALSE;
  3208.         }
  3209.       howto = elf_howto_table + indx;
  3210.  
  3211.       r_symndx = ELF32_R_SYM (rel->r_info);
  3212.       h = NULL;
  3213.       sym = NULL;
  3214.       sec = NULL;
  3215.       unresolved_reloc = FALSE;
  3216.       if (r_symndx < symtab_hdr->sh_info)
  3217.         {
  3218.           sym = local_syms + r_symndx;
  3219.           sec = local_sections[r_symndx];
  3220.           relocation = (sec->output_section->vma
  3221.                         + sec->output_offset
  3222.                         + sym->st_value);
  3223.           st_size = sym->st_size;
  3224.  
  3225.           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
  3226.               && ((sec->flags & SEC_MERGE) != 0
  3227.                   || (info->relocatable
  3228.                       && sec->output_offset != 0)))
  3229.             {
  3230.               bfd_vma addend;
  3231.               bfd_byte *where = contents + rel->r_offset;
  3232.  
  3233.               switch (howto->size)
  3234.                 {
  3235.                 case 0:
  3236.                   addend = bfd_get_8 (input_bfd, where);
  3237.                   if (howto->pc_relative)
  3238.                     {
  3239.                       addend = (addend ^ 0x80) - 0x80;
  3240.                       addend += 1;
  3241.                     }
  3242.                   break;
  3243.                 case 1:
  3244.                   addend = bfd_get_16 (input_bfd, where);
  3245.                   if (howto->pc_relative)
  3246.                     {
  3247.                       addend = (addend ^ 0x8000) - 0x8000;
  3248.                       addend += 2;
  3249.                     }
  3250.                   break;
  3251.                 case 2:
  3252.                   addend = bfd_get_32 (input_bfd, where);
  3253.                   if (howto->pc_relative)
  3254.                     {
  3255.                       addend = (addend ^ 0x80000000) - 0x80000000;
  3256.                       addend += 4;
  3257.                     }
  3258.                   break;
  3259.                 default:
  3260.                   abort ();
  3261.                 }
  3262.  
  3263.               if (info->relocatable)
  3264.                 addend += sec->output_offset;
  3265.               else
  3266.                 {
  3267.                   asection *msec = sec;
  3268.                   addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
  3269.                                                    addend);
  3270.                   addend -= relocation;
  3271.                   addend += msec->output_section->vma + msec->output_offset;
  3272.                 }
  3273.  
  3274.               switch (howto->size)
  3275.                 {
  3276.                 case 0:
  3277.                   /* FIXME: overflow checks.  */
  3278.                   if (howto->pc_relative)
  3279.                     addend -= 1;
  3280.                   bfd_put_8 (input_bfd, addend, where);
  3281.                   break;
  3282.                 case 1:
  3283.                   if (howto->pc_relative)
  3284.                     addend -= 2;
  3285.                   bfd_put_16 (input_bfd, addend, where);
  3286.                   break;
  3287.                 case 2:
  3288.                   if (howto->pc_relative)
  3289.                     addend -= 4;
  3290.                   bfd_put_32 (input_bfd, addend, where);
  3291.                   break;
  3292.                 }
  3293.             }
  3294.           else if (!info->relocatable
  3295.                    && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
  3296.             {
  3297.               /* Relocate against local STT_GNU_IFUNC symbol.  */
  3298.               h = elf_i386_get_local_sym_hash (htab, input_bfd, rel,
  3299.                                                FALSE);
  3300.               if (h == NULL)
  3301.                 abort ();
  3302.  
  3303.               /* Set STT_GNU_IFUNC symbol value.  */
  3304.               h->root.u.def.value = sym->st_value;
  3305.               h->root.u.def.section = sec;
  3306.             }
  3307.         }
  3308.       else
  3309.         {
  3310.           bfd_boolean warned ATTRIBUTE_UNUSED;
  3311.  
  3312.           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  3313.                                    r_symndx, symtab_hdr, sym_hashes,
  3314.                                    h, sec, relocation,
  3315.                                    unresolved_reloc, warned);
  3316.           st_size = h->size;
  3317.         }
  3318.  
  3319.       if (sec != NULL && discarded_section (sec))
  3320.         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  3321.                                          rel, 1, relend, howto, 0, contents);
  3322.  
  3323.       if (info->relocatable)
  3324.         continue;
  3325.  
  3326.       /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
  3327.          it here if it is defined in a non-shared object.  */
  3328.       if (h != NULL
  3329.           && h->type == STT_GNU_IFUNC
  3330.           && h->def_regular)
  3331.         {
  3332.           asection *plt, *gotplt, *base_got;
  3333.           bfd_vma plt_index;
  3334.           const char *name;
  3335.  
  3336.           if ((input_section->flags & SEC_ALLOC) == 0
  3337.               || h->plt.offset == (bfd_vma) -1)
  3338.             abort ();
  3339.  
  3340.           /* STT_GNU_IFUNC symbol must go through PLT.  */
  3341.           if (htab->elf.splt != NULL)
  3342.             {
  3343.               plt = htab->elf.splt;
  3344.               gotplt = htab->elf.sgotplt;
  3345.             }
  3346.           else
  3347.             {
  3348.               plt = htab->elf.iplt;
  3349.               gotplt = htab->elf.igotplt;
  3350.             }
  3351.  
  3352.           relocation = (plt->output_section->vma
  3353.                         + plt->output_offset + h->plt.offset);
  3354.  
  3355.           switch (r_type)
  3356.             {
  3357.             default:
  3358.               if (h->root.root.string)
  3359.                 name = h->root.root.string;
  3360.               else
  3361.                 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
  3362.                                          NULL);
  3363.               (*_bfd_error_handler)
  3364.                 (_("%B: relocation %s against STT_GNU_IFUNC "
  3365.                    "symbol `%s' isn't handled by %s"), input_bfd,
  3366.                  elf_howto_table[r_type].name,
  3367.                  name, __FUNCTION__);
  3368.               bfd_set_error (bfd_error_bad_value);
  3369.               return FALSE;
  3370.  
  3371.             case R_386_32:
  3372.               /* Generate dynamic relcoation only when there is a
  3373.                  non-GOT reference in a shared object.  */
  3374.               if (info->shared && h->non_got_ref)
  3375.                 {
  3376.                   Elf_Internal_Rela outrel;
  3377.                   asection *sreloc;
  3378.                   bfd_vma offset;
  3379.  
  3380.                   /* Need a dynamic relocation to get the real function
  3381.                      adddress.  */
  3382.                   offset = _bfd_elf_section_offset (output_bfd,
  3383.                                                     info,
  3384.                                                     input_section,
  3385.                                                     rel->r_offset);
  3386.                   if (offset == (bfd_vma) -1
  3387.                       || offset == (bfd_vma) -2)
  3388.                     abort ();
  3389.  
  3390.                   outrel.r_offset = (input_section->output_section->vma
  3391.                                      + input_section->output_offset
  3392.                                      + offset);
  3393.  
  3394.                   if (h->dynindx == -1
  3395.                       || h->forced_local
  3396.                       || info->executable)
  3397.                     {
  3398.                       /* This symbol is resolved locally.  */
  3399.                       outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
  3400.                       bfd_put_32 (output_bfd,
  3401.                                   (h->root.u.def.value
  3402.                                    + h->root.u.def.section->output_section->vma
  3403.                                    + h->root.u.def.section->output_offset),
  3404.                                   contents + offset);
  3405.                     }
  3406.                   else
  3407.                     outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  3408.  
  3409.                   sreloc = htab->elf.irelifunc;
  3410.                   elf_append_rel (output_bfd, sreloc, &outrel);
  3411.  
  3412.                   /* If this reloc is against an external symbol, we
  3413.                      do not want to fiddle with the addend.  Otherwise,
  3414.                      we need to include the symbol value so that it
  3415.                      becomes an addend for the dynamic reloc.  For an
  3416.                      internal symbol, we have updated addend.  */
  3417.                   continue;
  3418.                 }
  3419.               /* FALLTHROUGH */
  3420.             case R_386_PC32:
  3421.             case R_386_PLT32:
  3422.               goto do_relocation;
  3423.  
  3424.             case R_386_GOT32:
  3425.               base_got = htab->elf.sgot;
  3426.               off = h->got.offset;
  3427.  
  3428.               if (base_got == NULL)
  3429.                 abort ();
  3430.  
  3431.               if (off == (bfd_vma) -1)
  3432.                 {
  3433.                   /* We can't use h->got.offset here to save state, or
  3434.                      even just remember the offset, as finish_dynamic_symbol
  3435.                      would use that as offset into .got.  */
  3436.  
  3437.                   if (htab->elf.splt != NULL)
  3438.                     {
  3439.                       plt_index = h->plt.offset / plt_entry_size - 1;
  3440.                       off = (plt_index + 3) * 4;
  3441.                       base_got = htab->elf.sgotplt;
  3442.                     }
  3443.                   else
  3444.                     {
  3445.                       plt_index = h->plt.offset / plt_entry_size;
  3446.                       off = plt_index * 4;
  3447.                       base_got = htab->elf.igotplt;
  3448.                     }
  3449.  
  3450.                   if (h->dynindx == -1
  3451.                       || h->forced_local
  3452.                       || info->symbolic)
  3453.                     {
  3454.                       /* This references the local defitionion.  We must
  3455.                          initialize this entry in the global offset table.
  3456.                          Since the offset must always be a multiple of 8,
  3457.                          we use the least significant bit to record
  3458.                          whether we have initialized it already.
  3459.  
  3460.                          When doing a dynamic link, we create a .rela.got
  3461.                          relocation entry to initialize the value.  This
  3462.                          is done in the finish_dynamic_symbol routine.   */
  3463.                       if ((off & 1) != 0)
  3464.                         off &= ~1;
  3465.                       else
  3466.                         {
  3467.                           bfd_put_32 (output_bfd, relocation,
  3468.                                       base_got->contents + off);
  3469.                           h->got.offset |= 1;
  3470.                         }
  3471.                     }
  3472.  
  3473.                   relocation = off;
  3474.  
  3475.                   /* Adjust for static executables.  */
  3476.                   if (htab->elf.splt == NULL)
  3477.                     relocation += gotplt->output_offset;
  3478.                 }
  3479.               else
  3480.                 {
  3481.                   relocation = (base_got->output_section->vma
  3482.                                 + base_got->output_offset + off
  3483.                                 - gotplt->output_section->vma
  3484.                                 - gotplt->output_offset);
  3485.                   /* Adjust for static executables.  */
  3486.                   if (htab->elf.splt == NULL)
  3487.                     relocation += gotplt->output_offset;
  3488.                 }
  3489.  
  3490.               goto do_relocation;
  3491.  
  3492.             case R_386_GOTOFF:
  3493.               relocation -= (gotplt->output_section->vma
  3494.                              + gotplt->output_offset);
  3495.               goto do_relocation;
  3496.             }
  3497.         }
  3498.  
  3499.       switch (r_type)
  3500.         {
  3501.         case R_386_GOT32:
  3502.           /* Relocation is to the entry for this symbol in the global
  3503.              offset table.  */
  3504.           if (htab->elf.sgot == NULL)
  3505.             abort ();
  3506.  
  3507.           if (h != NULL)
  3508.             {
  3509.               bfd_boolean dyn;
  3510.  
  3511.               off = h->got.offset;
  3512.               dyn = htab->elf.dynamic_sections_created;
  3513.               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
  3514.                   || (info->shared
  3515.                       && SYMBOL_REFERENCES_LOCAL (info, h))
  3516.                   || (ELF_ST_VISIBILITY (h->other)
  3517.                       && h->root.type == bfd_link_hash_undefweak))
  3518.                 {
  3519.                   /* This is actually a static link, or it is a
  3520.                      -Bsymbolic link and the symbol is defined
  3521.                      locally, or the symbol was forced to be local
  3522.                      because of a version file.  We must initialize
  3523.                      this entry in the global offset table.  Since the
  3524.                      offset must always be a multiple of 4, we use the
  3525.                      least significant bit to record whether we have
  3526.                      initialized it already.
  3527.  
  3528.                      When doing a dynamic link, we create a .rel.got
  3529.                      relocation entry to initialize the value.  This
  3530.                      is done in the finish_dynamic_symbol routine.  */
  3531.                   if ((off & 1) != 0)
  3532.                     off &= ~1;
  3533.                   else
  3534.                     {
  3535.                       bfd_put_32 (output_bfd, relocation,
  3536.                                   htab->elf.sgot->contents + off);
  3537.                       h->got.offset |= 1;
  3538.                     }
  3539.                 }
  3540.               else
  3541.                 unresolved_reloc = FALSE;
  3542.             }
  3543.           else
  3544.             {
  3545.               if (local_got_offsets == NULL)
  3546.                 abort ();
  3547.  
  3548.               off = local_got_offsets[r_symndx];
  3549.  
  3550.               /* The offset must always be a multiple of 4.  We use
  3551.                  the least significant bit to record whether we have
  3552.                  already generated the necessary reloc.  */
  3553.               if ((off & 1) != 0)
  3554.                 off &= ~1;
  3555.               else
  3556.                 {
  3557.                   bfd_put_32 (output_bfd, relocation,
  3558.                               htab->elf.sgot->contents + off);
  3559.  
  3560.                   if (info->shared)
  3561.                     {
  3562.                       asection *s;
  3563.                       Elf_Internal_Rela outrel;
  3564.  
  3565.                       s = htab->elf.srelgot;
  3566.                       if (s == NULL)
  3567.                         abort ();
  3568.  
  3569.                       outrel.r_offset = (htab->elf.sgot->output_section->vma
  3570.                                          + htab->elf.sgot->output_offset
  3571.                                          + off);
  3572.                       outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
  3573.                       elf_append_rel (output_bfd, s, &outrel);
  3574.                     }
  3575.  
  3576.                   local_got_offsets[r_symndx] |= 1;
  3577.                 }
  3578.             }
  3579.  
  3580.           if (off >= (bfd_vma) -2)
  3581.             abort ();
  3582.  
  3583.           relocation = htab->elf.sgot->output_section->vma
  3584.                        + htab->elf.sgot->output_offset + off
  3585.                        - htab->elf.sgotplt->output_section->vma
  3586.                        - htab->elf.sgotplt->output_offset;
  3587.           break;
  3588.  
  3589.         case R_386_GOTOFF:
  3590.           /* Relocation is relative to the start of the global offset
  3591.              table.  */
  3592.  
  3593.           /* Check to make sure it isn't a protected function symbol
  3594.              for shared library since it may not be local when used
  3595.              as function address.  We also need to make sure that a
  3596.              symbol is defined locally.  */
  3597.           if (info->shared && h)
  3598.             {
  3599.               if (!h->def_regular)
  3600.                 {
  3601.                   const char *v;
  3602.  
  3603.                   switch (ELF_ST_VISIBILITY (h->other))
  3604.                     {
  3605.                     case STV_HIDDEN:
  3606.                       v = _("hidden symbol");
  3607.                       break;
  3608.                     case STV_INTERNAL:
  3609.                       v = _("internal symbol");
  3610.                       break;
  3611.                     case STV_PROTECTED:
  3612.                       v = _("protected symbol");
  3613.                       break;
  3614.                     default:
  3615.                       v = _("symbol");
  3616.                       break;
  3617.                     }
  3618.  
  3619.                   (*_bfd_error_handler)
  3620.                     (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"),
  3621.                      input_bfd, v, h->root.root.string);
  3622.                   bfd_set_error (bfd_error_bad_value);
  3623.                   return FALSE;
  3624.                 }
  3625.               else if (!info->executable
  3626.                        && !SYMBOLIC_BIND (info, h)
  3627.                        && h->type == STT_FUNC
  3628.                        && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
  3629.                 {
  3630.                   (*_bfd_error_handler)
  3631.                     (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
  3632.                      input_bfd, h->root.root.string);
  3633.                   bfd_set_error (bfd_error_bad_value);
  3634.                   return FALSE;
  3635.                 }
  3636.             }
  3637.  
  3638.           /* Note that sgot is not involved in this
  3639.              calculation.  We always want the start of .got.plt.  If we
  3640.              defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
  3641.              permitted by the ABI, we might have to change this
  3642.              calculation.  */
  3643.           relocation -= htab->elf.sgotplt->output_section->vma
  3644.                         + htab->elf.sgotplt->output_offset;
  3645.           break;
  3646.  
  3647.         case R_386_GOTPC:
  3648.           /* Use global offset table as symbol value.  */
  3649.           relocation = htab->elf.sgotplt->output_section->vma
  3650.                        + htab->elf.sgotplt->output_offset;
  3651.           unresolved_reloc = FALSE;
  3652.           break;
  3653.  
  3654.         case R_386_PLT32:
  3655.           /* Relocation is to the entry for this symbol in the
  3656.              procedure linkage table.  */
  3657.  
  3658.           /* Resolve a PLT32 reloc against a local symbol directly,
  3659.              without using the procedure linkage table.  */
  3660.           if (h == NULL)
  3661.             break;
  3662.  
  3663.           if (h->plt.offset == (bfd_vma) -1
  3664.               || htab->elf.splt == NULL)
  3665.             {
  3666.               /* We didn't make a PLT entry for this symbol.  This
  3667.                  happens when statically linking PIC code, or when
  3668.                  using -Bsymbolic.  */
  3669.               break;
  3670.             }
  3671.  
  3672.           relocation = (htab->elf.splt->output_section->vma
  3673.                         + htab->elf.splt->output_offset
  3674.                         + h->plt.offset);
  3675.           unresolved_reloc = FALSE;
  3676.           break;
  3677.  
  3678.         case R_386_SIZE32:
  3679.           /* Set to symbol size.  */
  3680.           relocation = st_size;
  3681.           /* Fall through.  */
  3682.  
  3683.         case R_386_32:
  3684.         case R_386_PC32:
  3685.           if ((input_section->flags & SEC_ALLOC) == 0
  3686.               || is_vxworks_tls)
  3687.             break;
  3688.  
  3689.           if ((info->shared
  3690.                && (h == NULL
  3691.                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  3692.                    || h->root.type != bfd_link_hash_undefweak)
  3693.                && ((r_type != R_386_PC32 && r_type != R_386_SIZE32)
  3694.                    || !SYMBOL_CALLS_LOCAL (info, h)))
  3695.               || (ELIMINATE_COPY_RELOCS
  3696.                   && !info->shared
  3697.                   && h != NULL
  3698.                   && h->dynindx != -1
  3699.                   && !h->non_got_ref
  3700.                   && ((h->def_dynamic
  3701.                        && !h->def_regular)
  3702.                       || h->root.type == bfd_link_hash_undefweak
  3703.                       || h->root.type == bfd_link_hash_undefined)))
  3704.             {
  3705.               Elf_Internal_Rela outrel;
  3706.               bfd_boolean skip, relocate;
  3707.               asection *sreloc;
  3708.  
  3709.               /* When generating a shared object, these relocations
  3710.                  are copied into the output file to be resolved at run
  3711.                  time.  */
  3712.  
  3713.               skip = FALSE;
  3714.               relocate = FALSE;
  3715.  
  3716.               outrel.r_offset =
  3717.                 _bfd_elf_section_offset (output_bfd, info, input_section,
  3718.                                          rel->r_offset);
  3719.               if (outrel.r_offset == (bfd_vma) -1)
  3720.                 skip = TRUE;
  3721.               else if (outrel.r_offset == (bfd_vma) -2)
  3722.                 skip = TRUE, relocate = TRUE;
  3723.               outrel.r_offset += (input_section->output_section->vma
  3724.                                   + input_section->output_offset);
  3725.  
  3726.               if (skip)
  3727.                 memset (&outrel, 0, sizeof outrel);
  3728.               else if (h != NULL
  3729.                        && h->dynindx != -1
  3730.                        && (r_type == R_386_PC32
  3731.                            || !info->shared
  3732.                            || !SYMBOLIC_BIND (info, h)
  3733.                            || !h->def_regular))
  3734.                 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  3735.               else
  3736.                 {
  3737.                   /* This symbol is local, or marked to become local.  */
  3738.                   relocate = TRUE;
  3739.                   outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
  3740.                 }
  3741.  
  3742.               sreloc = elf_section_data (input_section)->sreloc;
  3743.  
  3744.               if (sreloc == NULL || sreloc->contents == NULL)
  3745.                 {
  3746.                   r = bfd_reloc_notsupported;
  3747.                   goto check_relocation_error;
  3748.                 }
  3749.  
  3750.               elf_append_rel (output_bfd, sreloc, &outrel);
  3751.  
  3752.               /* If this reloc is against an external symbol, we do
  3753.                  not want to fiddle with the addend.  Otherwise, we
  3754.                  need to include the symbol value so that it becomes
  3755.                  an addend for the dynamic reloc.  */
  3756.               if (! relocate)
  3757.                 continue;
  3758.             }
  3759.           break;
  3760.  
  3761.         case R_386_TLS_IE:
  3762.           if (!info->executable)
  3763.             {
  3764.               Elf_Internal_Rela outrel;
  3765.               asection *sreloc;
  3766.  
  3767.               outrel.r_offset = rel->r_offset
  3768.                                 + input_section->output_section->vma
  3769.                                 + input_section->output_offset;
  3770.               outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
  3771.               sreloc = elf_section_data (input_section)->sreloc;
  3772.               if (sreloc == NULL)
  3773.                 abort ();
  3774.               elf_append_rel (output_bfd, sreloc, &outrel);
  3775.             }
  3776.           /* Fall through */
  3777.  
  3778.         case R_386_TLS_GD:
  3779.         case R_386_TLS_GOTDESC:
  3780.         case R_386_TLS_DESC_CALL:
  3781.         case R_386_TLS_IE_32:
  3782.         case R_386_TLS_GOTIE:
  3783.           tls_type = GOT_UNKNOWN;
  3784.           if (h == NULL && local_got_offsets)
  3785.             tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
  3786.           else if (h != NULL)
  3787.             tls_type = elf_i386_hash_entry(h)->tls_type;
  3788.           if (tls_type == GOT_TLS_IE)
  3789.             tls_type = GOT_TLS_IE_NEG;
  3790.  
  3791.           if (! elf_i386_tls_transition (info, input_bfd,
  3792.                                          input_section, contents,
  3793.                                          symtab_hdr, sym_hashes,
  3794.                                          &r_type, tls_type, rel,
  3795.                                          relend, h, r_symndx))
  3796.             return FALSE;
  3797.  
  3798.           if (r_type == R_386_TLS_LE_32)
  3799.             {
  3800.               BFD_ASSERT (! unresolved_reloc);
  3801.               if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
  3802.                 {
  3803.                   unsigned int type;
  3804.                   bfd_vma roff;
  3805.  
  3806.                   /* GD->LE transition.  */
  3807.                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
  3808.                   if (type == 0x04)
  3809.                     {
  3810.                       /* leal foo(,%reg,1), %eax; call ___tls_get_addr
  3811.                          Change it into:
  3812.                          movl %gs:0, %eax; subl $foo@tpoff, %eax
  3813.                          (6 byte form of subl).  */
  3814.                       memcpy (contents + rel->r_offset - 3,
  3815.                               "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
  3816.                       roff = rel->r_offset + 5;
  3817.                     }
  3818.                   else
  3819.                     {
  3820.                       /* leal foo(%reg), %eax; call ___tls_get_addr; nop
  3821.                          Change it into:
  3822.                          movl %gs:0, %eax; subl $foo@tpoff, %eax
  3823.                          (6 byte form of subl).  */
  3824.                       memcpy (contents + rel->r_offset - 2,
  3825.                               "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
  3826.                       roff = rel->r_offset + 6;
  3827.                     }
  3828.                   bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
  3829.                               contents + roff);
  3830.                   /* Skip R_386_PC32/R_386_PLT32.  */
  3831.                   rel++;
  3832.                   continue;
  3833.                 }
  3834.               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
  3835.                 {
  3836.                   /* GDesc -> LE transition.
  3837.                      It's originally something like:
  3838.                      leal x@tlsdesc(%ebx), %eax
  3839.  
  3840.                      leal x@ntpoff, %eax
  3841.  
  3842.                      Registers other than %eax may be set up here.  */
  3843.  
  3844.                   unsigned int val;
  3845.                   bfd_vma roff;
  3846.  
  3847.                   roff = rel->r_offset;
  3848.                   val = bfd_get_8 (input_bfd, contents + roff - 1);
  3849.  
  3850.                   /* Now modify the instruction as appropriate.  */
  3851.                   /* aoliva FIXME: remove the above and xor the byte
  3852.                      below with 0x86.  */
  3853.                   bfd_put_8 (output_bfd, val ^ 0x86,
  3854.                              contents + roff - 1);
  3855.                   bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
  3856.                               contents + roff);
  3857.                   continue;
  3858.                 }
  3859.               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
  3860.                 {
  3861.                   /* GDesc -> LE transition.
  3862.                      It's originally:
  3863.                      call *(%eax)
  3864.                      Turn it into:
  3865.                      xchg %ax,%ax  */
  3866.  
  3867.                   bfd_vma roff;
  3868.  
  3869.                   roff = rel->r_offset;
  3870.                   bfd_put_8 (output_bfd, 0x66, contents + roff);
  3871.                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
  3872.                   continue;
  3873.                 }
  3874.               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
  3875.                 {
  3876.                   unsigned int val;
  3877.  
  3878.                   /* IE->LE transition:
  3879.                      Originally it can be one of:
  3880.                      movl foo, %eax
  3881.                      movl foo, %reg
  3882.                      addl foo, %reg
  3883.                      We change it into:
  3884.                      movl $foo, %eax
  3885.                      movl $foo, %reg
  3886.                      addl $foo, %reg.  */
  3887.                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
  3888.                   if (val == 0xa1)
  3889.                     {
  3890.                       /* movl foo, %eax.  */
  3891.                       bfd_put_8 (output_bfd, 0xb8,
  3892.                                  contents + rel->r_offset - 1);
  3893.                     }
  3894.                   else
  3895.                     {
  3896.                       unsigned int type;
  3897.  
  3898.                       type = bfd_get_8 (input_bfd,
  3899.                                         contents + rel->r_offset - 2);
  3900.                       switch (type)
  3901.                         {
  3902.                         case 0x8b:
  3903.                           /* movl */
  3904.                           bfd_put_8 (output_bfd, 0xc7,
  3905.                                      contents + rel->r_offset - 2);
  3906.                           bfd_put_8 (output_bfd,
  3907.                                      0xc0 | ((val >> 3) & 7),
  3908.                                      contents + rel->r_offset - 1);
  3909.                           break;
  3910.                         case 0x03:
  3911.                           /* addl */
  3912.                           bfd_put_8 (output_bfd, 0x81,
  3913.                                      contents + rel->r_offset - 2);
  3914.                           bfd_put_8 (output_bfd,
  3915.                                      0xc0 | ((val >> 3) & 7),
  3916.                                      contents + rel->r_offset - 1);
  3917.                           break;
  3918.                         default:
  3919.                           BFD_FAIL ();
  3920.                           break;
  3921.                         }
  3922.                     }
  3923.                   bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
  3924.                               contents + rel->r_offset);
  3925.                   continue;
  3926.                 }
  3927.               else
  3928.                 {
  3929.                   unsigned int val, type;
  3930.  
  3931.                   /* {IE_32,GOTIE}->LE transition:
  3932.                      Originally it can be one of:
  3933.                      subl foo(%reg1), %reg2
  3934.                      movl foo(%reg1), %reg2
  3935.                      addl foo(%reg1), %reg2
  3936.                      We change it into:
  3937.                      subl $foo, %reg2
  3938.                      movl $foo, %reg2 (6 byte form)
  3939.                      addl $foo, %reg2.  */
  3940.                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
  3941.                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
  3942.                   if (type == 0x8b)
  3943.                     {
  3944.                       /* movl */
  3945.                       bfd_put_8 (output_bfd, 0xc7,
  3946.                                  contents + rel->r_offset - 2);
  3947.                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
  3948.                                  contents + rel->r_offset - 1);
  3949.                     }
  3950.                   else if (type == 0x2b)
  3951.                     {
  3952.                       /* subl */
  3953.                       bfd_put_8 (output_bfd, 0x81,
  3954.                                  contents + rel->r_offset - 2);
  3955.                       bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
  3956.                                  contents + rel->r_offset - 1);
  3957.                     }
  3958.                   else if (type == 0x03)
  3959.                     {
  3960.                       /* addl */
  3961.                       bfd_put_8 (output_bfd, 0x81,
  3962.                                  contents + rel->r_offset - 2);
  3963.                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
  3964.                                  contents + rel->r_offset - 1);
  3965.                     }
  3966.                   else
  3967.                     BFD_FAIL ();
  3968.                   if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
  3969.                     bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
  3970.                                 contents + rel->r_offset);
  3971.                   else
  3972.                     bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
  3973.                                 contents + rel->r_offset);
  3974.                   continue;
  3975.                 }
  3976.             }
  3977.  
  3978.           if (htab->elf.sgot == NULL)
  3979.             abort ();
  3980.  
  3981.           if (h != NULL)
  3982.             {
  3983.               off = h->got.offset;
  3984.               offplt = elf_i386_hash_entry (h)->tlsdesc_got;
  3985.             }
  3986.           else
  3987.             {
  3988.               if (local_got_offsets == NULL)
  3989.                 abort ();
  3990.  
  3991.               off = local_got_offsets[r_symndx];
  3992.               offplt = local_tlsdesc_gotents[r_symndx];
  3993.             }
  3994.  
  3995.           if ((off & 1) != 0)
  3996.             off &= ~1;
  3997.           else
  3998.             {
  3999.               Elf_Internal_Rela outrel;
  4000.               int dr_type;
  4001.               asection *sreloc;
  4002.  
  4003.               if (htab->elf.srelgot == NULL)
  4004.                 abort ();
  4005.  
  4006.               indx = h && h->dynindx != -1 ? h->dynindx : 0;
  4007.  
  4008.               if (GOT_TLS_GDESC_P (tls_type))
  4009.                 {
  4010.                   bfd_byte *loc;
  4011.                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
  4012.                   BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
  4013.                               <= htab->elf.sgotplt->size);
  4014.                   outrel.r_offset = (htab->elf.sgotplt->output_section->vma
  4015.                                      + htab->elf.sgotplt->output_offset
  4016.                                      + offplt
  4017.                                      + htab->sgotplt_jump_table_size);
  4018.                   sreloc = htab->elf.srelplt;
  4019.                   loc = sreloc->contents;
  4020.                   loc += (htab->next_tls_desc_index++
  4021.                           * sizeof (Elf32_External_Rel));
  4022.                   BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
  4023.                               <= sreloc->contents + sreloc->size);
  4024.                   bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
  4025.                   if (indx == 0)
  4026.                     {
  4027.                       BFD_ASSERT (! unresolved_reloc);
  4028.                       bfd_put_32 (output_bfd,
  4029.                                   relocation - elf_i386_dtpoff_base (info),
  4030.                                   htab->elf.sgotplt->contents + offplt
  4031.                                   + htab->sgotplt_jump_table_size + 4);
  4032.                     }
  4033.                   else
  4034.                     {
  4035.                       bfd_put_32 (output_bfd, 0,
  4036.                                   htab->elf.sgotplt->contents + offplt
  4037.                                   + htab->sgotplt_jump_table_size + 4);
  4038.                     }
  4039.                 }
  4040.  
  4041.               sreloc = htab->elf.srelgot;
  4042.  
  4043.               outrel.r_offset = (htab->elf.sgot->output_section->vma
  4044.                                  + htab->elf.sgot->output_offset + off);
  4045.  
  4046.               if (GOT_TLS_GD_P (tls_type))
  4047.                 dr_type = R_386_TLS_DTPMOD32;
  4048.               else if (GOT_TLS_GDESC_P (tls_type))
  4049.                 goto dr_done;
  4050.               else if (tls_type == GOT_TLS_IE_POS)
  4051.                 dr_type = R_386_TLS_TPOFF;
  4052.               else
  4053.                 dr_type = R_386_TLS_TPOFF32;
  4054.  
  4055.               if (dr_type == R_386_TLS_TPOFF && indx == 0)
  4056.                 bfd_put_32 (output_bfd,
  4057.                             relocation - elf_i386_dtpoff_base (info),
  4058.                             htab->elf.sgot->contents + off);
  4059.               else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
  4060.                 bfd_put_32 (output_bfd,
  4061.                             elf_i386_dtpoff_base (info) - relocation,
  4062.                             htab->elf.sgot->contents + off);
  4063.               else if (dr_type != R_386_TLS_DESC)
  4064.                 bfd_put_32 (output_bfd, 0,
  4065.                             htab->elf.sgot->contents + off);
  4066.               outrel.r_info = ELF32_R_INFO (indx, dr_type);
  4067.  
  4068.               elf_append_rel (output_bfd, sreloc, &outrel);
  4069.  
  4070.               if (GOT_TLS_GD_P (tls_type))
  4071.                 {
  4072.                   if (indx == 0)
  4073.                     {
  4074.                       BFD_ASSERT (! unresolved_reloc);
  4075.                       bfd_put_32 (output_bfd,
  4076.                                   relocation - elf_i386_dtpoff_base (info),
  4077.                                   htab->elf.sgot->contents + off + 4);
  4078.                     }
  4079.                   else
  4080.                     {
  4081.                       bfd_put_32 (output_bfd, 0,
  4082.                                   htab->elf.sgot->contents + off + 4);
  4083.                       outrel.r_info = ELF32_R_INFO (indx,
  4084.                                                     R_386_TLS_DTPOFF32);
  4085.                       outrel.r_offset += 4;
  4086.                       elf_append_rel (output_bfd, sreloc, &outrel);
  4087.                     }
  4088.                 }
  4089.               else if (tls_type == GOT_TLS_IE_BOTH)
  4090.                 {
  4091.                   bfd_put_32 (output_bfd,
  4092.                               (indx == 0
  4093.                                ? relocation - elf_i386_dtpoff_base (info)
  4094.                                : 0),
  4095.                               htab->elf.sgot->contents + off + 4);
  4096.                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
  4097.                   outrel.r_offset += 4;
  4098.                   elf_append_rel (output_bfd, sreloc, &outrel);
  4099.                 }
  4100.  
  4101.             dr_done:
  4102.               if (h != NULL)
  4103.                 h->got.offset |= 1;
  4104.               else
  4105.                 local_got_offsets[r_symndx] |= 1;
  4106.             }
  4107.  
  4108.           if (off >= (bfd_vma) -2
  4109.               && ! GOT_TLS_GDESC_P (tls_type))
  4110.             abort ();
  4111.           if (r_type == R_386_TLS_GOTDESC
  4112.               || r_type == R_386_TLS_DESC_CALL)
  4113.             {
  4114.               relocation = htab->sgotplt_jump_table_size + offplt;
  4115.               unresolved_reloc = FALSE;
  4116.             }
  4117.           else if (r_type == ELF32_R_TYPE (rel->r_info))
  4118.             {
  4119.               bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
  4120.                               + htab->elf.sgotplt->output_offset;
  4121.               relocation = htab->elf.sgot->output_section->vma
  4122.                 + htab->elf.sgot->output_offset + off - g_o_t;
  4123.               if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
  4124.                   && tls_type == GOT_TLS_IE_BOTH)
  4125.                 relocation += 4;
  4126.               if (r_type == R_386_TLS_IE)
  4127.                 relocation += g_o_t;
  4128.               unresolved_reloc = FALSE;
  4129.             }
  4130.           else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
  4131.             {
  4132.               unsigned int val, type;
  4133.               bfd_vma roff;
  4134.  
  4135.               /* GD->IE transition.  */
  4136.               type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
  4137.               val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
  4138.               if (type == 0x04)
  4139.                 {
  4140.                   /* leal foo(,%reg,1), %eax; call ___tls_get_addr
  4141.                      Change it into:
  4142.                      movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax.  */
  4143.                   val >>= 3;
  4144.                   roff = rel->r_offset - 3;
  4145.                 }
  4146.               else
  4147.                 {
  4148.                   /* leal foo(%reg), %eax; call ___tls_get_addr; nop
  4149.                      Change it into:
  4150.                      movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax.  */
  4151.                   roff = rel->r_offset - 2;
  4152.                 }
  4153.               memcpy (contents + roff,
  4154.                       "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
  4155.               contents[roff + 7] = 0x80 | (val & 7);
  4156.               /* If foo is used only with foo@gotntpoff(%reg) and
  4157.                  foo@indntpoff, but not with foo@gottpoff(%reg), change
  4158.                  subl $foo@gottpoff(%reg), %eax
  4159.                  into:
  4160.                  addl $foo@gotntpoff(%reg), %eax.  */
  4161.               if (tls_type == GOT_TLS_IE_POS)
  4162.                 contents[roff + 6] = 0x03;
  4163.               bfd_put_32 (output_bfd,
  4164.                           htab->elf.sgot->output_section->vma
  4165.                           + htab->elf.sgot->output_offset + off
  4166.                           - htab->elf.sgotplt->output_section->vma
  4167.                           - htab->elf.sgotplt->output_offset,
  4168.                           contents + roff + 8);
  4169.               /* Skip R_386_PLT32.  */
  4170.               rel++;
  4171.               continue;
  4172.             }
  4173.           else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
  4174.             {
  4175.               /* GDesc -> IE transition.
  4176.                  It's originally something like:
  4177.                  leal x@tlsdesc(%ebx), %eax
  4178.  
  4179.                  Change it to:
  4180.                  movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
  4181.                  or:
  4182.                  movl x@gottpoff(%ebx), %eax # before negl %eax
  4183.  
  4184.                  Registers other than %eax may be set up here.  */
  4185.  
  4186.               bfd_vma roff;
  4187.  
  4188.               /* First, make sure it's a leal adding ebx to a 32-bit
  4189.                  offset into any register, although it's probably
  4190.                  almost always going to be eax.  */
  4191.               roff = rel->r_offset;
  4192.  
  4193.               /* Now modify the instruction as appropriate.  */
  4194.               /* To turn a leal into a movl in the form we use it, it
  4195.                  suffices to change the first byte from 0x8d to 0x8b.
  4196.                  aoliva FIXME: should we decide to keep the leal, all
  4197.                  we have to do is remove the statement below, and
  4198.                  adjust the relaxation of R_386_TLS_DESC_CALL.  */
  4199.               bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
  4200.  
  4201.               if (tls_type == GOT_TLS_IE_BOTH)
  4202.                 off += 4;
  4203.  
  4204.               bfd_put_32 (output_bfd,
  4205.                           htab->elf.sgot->output_section->vma
  4206.                           + htab->elf.sgot->output_offset + off
  4207.                           - htab->elf.sgotplt->output_section->vma
  4208.                           - htab->elf.sgotplt->output_offset,
  4209.                           contents + roff);
  4210.               continue;
  4211.             }
  4212.           else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
  4213.             {
  4214.               /* GDesc -> IE transition.
  4215.                  It's originally:
  4216.                  call *(%eax)
  4217.  
  4218.                  Change it to:
  4219.                  xchg %ax,%ax
  4220.                  or
  4221.                  negl %eax
  4222.                  depending on how we transformed the TLS_GOTDESC above.
  4223.               */
  4224.  
  4225.               bfd_vma roff;
  4226.  
  4227.               roff = rel->r_offset;
  4228.  
  4229.               /* Now modify the instruction as appropriate.  */
  4230.               if (tls_type != GOT_TLS_IE_NEG)
  4231.                 {
  4232.                   /* xchg %ax,%ax */
  4233.                   bfd_put_8 (output_bfd, 0x66, contents + roff);
  4234.                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
  4235.                 }
  4236.               else
  4237.                 {
  4238.                   /* negl %eax */
  4239.                   bfd_put_8 (output_bfd, 0xf7, contents + roff);
  4240.                   bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
  4241.                 }
  4242.  
  4243.               continue;
  4244.             }
  4245.           else
  4246.             BFD_ASSERT (FALSE);
  4247.           break;
  4248.  
  4249.         case R_386_TLS_LDM:
  4250.           if (! elf_i386_tls_transition (info, input_bfd,
  4251.                                          input_section, contents,
  4252.                                          symtab_hdr, sym_hashes,
  4253.                                          &r_type, GOT_UNKNOWN, rel,
  4254.                                          relend, h, r_symndx))
  4255.             return FALSE;
  4256.  
  4257.           if (r_type != R_386_TLS_LDM)
  4258.             {
  4259.               /* LD->LE transition:
  4260.                  leal foo(%reg), %eax; call ___tls_get_addr.
  4261.                  We change it into:
  4262.                  movl %gs:0, %eax; nop; leal 0(%esi,1), %esi.  */
  4263.               BFD_ASSERT (r_type == R_386_TLS_LE_32);
  4264.               memcpy (contents + rel->r_offset - 2,
  4265.                       "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
  4266.               /* Skip R_386_PC32/R_386_PLT32.  */
  4267.               rel++;
  4268.               continue;
  4269.             }
  4270.  
  4271.           if (htab->elf.sgot == NULL)
  4272.             abort ();
  4273.  
  4274.           off = htab->tls_ldm_got.offset;
  4275.           if (off & 1)
  4276.             off &= ~1;
  4277.           else
  4278.             {
  4279.               Elf_Internal_Rela outrel;
  4280.  
  4281.               if (htab->elf.srelgot == NULL)
  4282.                 abort ();
  4283.  
  4284.               outrel.r_offset = (htab->elf.sgot->output_section->vma
  4285.                                  + htab->elf.sgot->output_offset + off);
  4286.  
  4287.               bfd_put_32 (output_bfd, 0,
  4288.                           htab->elf.sgot->contents + off);
  4289.               bfd_put_32 (output_bfd, 0,
  4290.                           htab->elf.sgot->contents + off + 4);
  4291.               outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
  4292.               elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
  4293.               htab->tls_ldm_got.offset |= 1;
  4294.             }
  4295.           relocation = htab->elf.sgot->output_section->vma
  4296.                        + htab->elf.sgot->output_offset + off
  4297.                        - htab->elf.sgotplt->output_section->vma
  4298.                        - htab->elf.sgotplt->output_offset;
  4299.           unresolved_reloc = FALSE;
  4300.           break;
  4301.  
  4302.         case R_386_TLS_LDO_32:
  4303.           if (!info->executable || (input_section->flags & SEC_CODE) == 0)
  4304.             relocation -= elf_i386_dtpoff_base (info);
  4305.           else
  4306.             /* When converting LDO to LE, we must negate.  */
  4307.             relocation = -elf_i386_tpoff (info, relocation);
  4308.           break;
  4309.  
  4310.         case R_386_TLS_LE_32:
  4311.         case R_386_TLS_LE:
  4312.           if (!info->executable)
  4313.             {
  4314.               Elf_Internal_Rela outrel;
  4315.               asection *sreloc;
  4316.  
  4317.               outrel.r_offset = rel->r_offset
  4318.                                 + input_section->output_section->vma
  4319.                                 + input_section->output_offset;
  4320.               if (h != NULL && h->dynindx != -1)
  4321.                 indx = h->dynindx;
  4322.               else
  4323.                 indx = 0;
  4324.               if (r_type == R_386_TLS_LE_32)
  4325.                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
  4326.               else
  4327.                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
  4328.               sreloc = elf_section_data (input_section)->sreloc;
  4329.               if (sreloc == NULL)
  4330.                 abort ();
  4331.               elf_append_rel (output_bfd, sreloc, &outrel);
  4332.               if (indx)
  4333.                 continue;
  4334.               else if (r_type == R_386_TLS_LE_32)
  4335.                 relocation = elf_i386_dtpoff_base (info) - relocation;
  4336.               else
  4337.                 relocation -= elf_i386_dtpoff_base (info);
  4338.             }
  4339.           else if (r_type == R_386_TLS_LE_32)
  4340.             relocation = elf_i386_tpoff (info, relocation);
  4341.           else
  4342.             relocation = -elf_i386_tpoff (info, relocation);
  4343.           break;
  4344.  
  4345.         default:
  4346.           break;
  4347.         }
  4348.  
  4349.       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
  4350.          because such sections are not SEC_ALLOC and thus ld.so will
  4351.          not process them.  */
  4352.       if (unresolved_reloc
  4353.           && !((input_section->flags & SEC_DEBUGGING) != 0
  4354.                && h->def_dynamic)
  4355.           && _bfd_elf_section_offset (output_bfd, info, input_section,
  4356.                                       rel->r_offset) != (bfd_vma) -1)
  4357.         {
  4358.           (*_bfd_error_handler)
  4359.             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
  4360.              input_bfd,
  4361.              input_section,
  4362.              (long) rel->r_offset,
  4363.              howto->name,
  4364.              h->root.root.string);
  4365.           return FALSE;
  4366.         }
  4367.  
  4368. do_relocation:
  4369.       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  4370.                                     contents, rel->r_offset,
  4371.                                     relocation, 0);
  4372.  
  4373. check_relocation_error:
  4374.       if (r != bfd_reloc_ok)
  4375.         {
  4376.           const char *name;
  4377.  
  4378.           if (h != NULL)
  4379.             name = h->root.root.string;
  4380.           else
  4381.             {
  4382.               name = bfd_elf_string_from_elf_section (input_bfd,
  4383.                                                       symtab_hdr->sh_link,
  4384.                                                       sym->st_name);
  4385.               if (name == NULL)
  4386.                 return FALSE;
  4387.               if (*name == '\0')
  4388.                 name = bfd_section_name (input_bfd, sec);
  4389.             }
  4390.  
  4391.           if (r == bfd_reloc_overflow)
  4392.             {
  4393.               if (! ((*info->callbacks->reloc_overflow)
  4394.                      (info, (h ? &h->root : NULL), name, howto->name,
  4395.                       (bfd_vma) 0, input_bfd, input_section,
  4396.                       rel->r_offset)))
  4397.                 return FALSE;
  4398.             }
  4399.           else
  4400.             {
  4401.               (*_bfd_error_handler)
  4402.                 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
  4403.                  input_bfd, input_section,
  4404.                  (long) rel->r_offset, name, (int) r);
  4405.               return FALSE;
  4406.             }
  4407.         }
  4408.     }
  4409.  
  4410.   return TRUE;
  4411. }
  4412.  
  4413. /* Finish up dynamic symbol handling.  We set the contents of various
  4414.    dynamic sections here.  */
  4415.  
  4416. static bfd_boolean
  4417. elf_i386_finish_dynamic_symbol (bfd *output_bfd,
  4418.                                 struct bfd_link_info *info,
  4419.                                 struct elf_link_hash_entry *h,
  4420.                                 Elf_Internal_Sym *sym)
  4421. {
  4422.   struct elf_i386_link_hash_table *htab;
  4423.   unsigned plt_entry_size;
  4424.   const struct elf_i386_backend_data *abed;
  4425.  
  4426.   htab = elf_i386_hash_table (info);
  4427.   if (htab == NULL)
  4428.     return FALSE;
  4429.  
  4430.   abed = get_elf_i386_backend_data (output_bfd);
  4431.   plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd);
  4432.  
  4433.   if (h->plt.offset != (bfd_vma) -1)
  4434.     {
  4435.       bfd_vma plt_index;
  4436.       bfd_vma got_offset;
  4437.       Elf_Internal_Rela rel;
  4438.       bfd_byte *loc;
  4439.       asection *plt, *gotplt, *relplt;
  4440.  
  4441.       /* When building a static executable, use .iplt, .igot.plt and
  4442.          .rel.iplt sections for STT_GNU_IFUNC symbols.  */
  4443.       if (htab->elf.splt != NULL)
  4444.         {
  4445.           plt = htab->elf.splt;
  4446.           gotplt = htab->elf.sgotplt;
  4447.           relplt = htab->elf.srelplt;
  4448.         }
  4449.       else
  4450.         {
  4451.           plt = htab->elf.iplt;
  4452.           gotplt = htab->elf.igotplt;
  4453.           relplt = htab->elf.irelplt;
  4454.         }
  4455.  
  4456.       /* This symbol has an entry in the procedure linkage table.  Set
  4457.          it up.  */
  4458.  
  4459.       if ((h->dynindx == -1
  4460.            && !((h->forced_local || info->executable)
  4461.                 && h->def_regular
  4462.                 && h->type == STT_GNU_IFUNC))
  4463.           || plt == NULL
  4464.           || gotplt == NULL
  4465.           || relplt == NULL)
  4466.         abort ();
  4467.  
  4468.       /* Get the index in the procedure linkage table which
  4469.          corresponds to this symbol.  This is the index of this symbol
  4470.          in all the symbols for which we are making plt entries.  The
  4471.          first entry in the procedure linkage table is reserved.
  4472.  
  4473.          Get the offset into the .got table of the entry that
  4474.          corresponds to this function.  Each .got entry is 4 bytes.
  4475.          The first three are reserved.
  4476.  
  4477.          For static executables, we don't reserve anything.  */
  4478.  
  4479.       if (plt == htab->elf.splt)
  4480.         {
  4481.           got_offset = h->plt.offset / plt_entry_size - 1;
  4482.           got_offset = (got_offset + 3) * 4;
  4483.         }
  4484.       else
  4485.         {
  4486.           got_offset = h->plt.offset / plt_entry_size;
  4487.           got_offset = got_offset * 4;
  4488.         }
  4489.  
  4490.       /* Fill in the entry in the procedure linkage table.  */
  4491.       if (! info->shared)
  4492.         {
  4493.           memcpy (plt->contents + h->plt.offset, abed->plt->plt_entry,
  4494.                   abed->plt->plt_entry_size);
  4495.           bfd_put_32 (output_bfd,
  4496.                       (gotplt->output_section->vma
  4497.                        + gotplt->output_offset
  4498.                        + got_offset),
  4499.                       plt->contents + h->plt.offset
  4500.                       + abed->plt->plt_got_offset);
  4501.  
  4502.           if (abed->is_vxworks)
  4503.             {
  4504.               int s, k, reloc_index;
  4505.  
  4506.               /* Create the R_386_32 relocation referencing the GOT
  4507.                  for this PLT entry.  */
  4508.  
  4509.               /* S: Current slot number (zero-based).  */
  4510.               s = ((h->plt.offset - abed->plt->plt_entry_size)
  4511.                    / abed->plt->plt_entry_size);
  4512.               /* K: Number of relocations for PLTResolve. */
  4513.               if (info->shared)
  4514.                 k = PLTRESOLVE_RELOCS_SHLIB;
  4515.               else
  4516.                 k = PLTRESOLVE_RELOCS;
  4517.               /* Skip the PLTresolve relocations, and the relocations for
  4518.                  the other PLT slots. */
  4519.               reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
  4520.               loc = (htab->srelplt2->contents + reloc_index
  4521.                      * sizeof (Elf32_External_Rel));
  4522.  
  4523.               rel.r_offset = (htab->elf.splt->output_section->vma
  4524.                               + htab->elf.splt->output_offset
  4525.                               + h->plt.offset + 2),
  4526.               rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
  4527.               bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
  4528.  
  4529.               /* Create the R_386_32 relocation referencing the beginning of
  4530.                  the PLT for this GOT entry.  */
  4531.               rel.r_offset = (htab->elf.sgotplt->output_section->vma
  4532.                               + htab->elf.sgotplt->output_offset
  4533.                               + got_offset);
  4534.               rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
  4535.               bfd_elf32_swap_reloc_out (output_bfd, &rel,
  4536.                                         loc + sizeof (Elf32_External_Rel));
  4537.             }
  4538.         }
  4539.       else
  4540.         {
  4541.           memcpy (plt->contents + h->plt.offset, abed->plt->pic_plt_entry,
  4542.                   abed->plt->plt_entry_size);
  4543.           bfd_put_32 (output_bfd, got_offset,
  4544.                       plt->contents + h->plt.offset
  4545.                       + abed->plt->plt_got_offset);
  4546.         }
  4547.  
  4548.       /* Fill in the entry in the global offset table.  */
  4549.       bfd_put_32 (output_bfd,
  4550.                   (plt->output_section->vma
  4551.                    + plt->output_offset
  4552.                    + h->plt.offset
  4553.                    + abed->plt->plt_lazy_offset),
  4554.                   gotplt->contents + got_offset);
  4555.  
  4556.       /* Fill in the entry in the .rel.plt section.  */
  4557.       rel.r_offset = (gotplt->output_section->vma
  4558.                       + gotplt->output_offset
  4559.                       + got_offset);
  4560.       if (h->dynindx == -1
  4561.           || ((info->executable
  4562.                || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  4563.               && h->def_regular
  4564.                && h->type == STT_GNU_IFUNC))
  4565.         {
  4566.           /* If an STT_GNU_IFUNC symbol is locally defined, generate
  4567.              R_386_IRELATIVE instead of R_386_JUMP_SLOT.  Store addend
  4568.              in the .got.plt section.  */
  4569.           bfd_put_32 (output_bfd,
  4570.                       (h->root.u.def.value
  4571.                        + h->root.u.def.section->output_section->vma
  4572.                        + h->root.u.def.section->output_offset),
  4573.                       gotplt->contents + got_offset);
  4574.           rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
  4575.           /* R_386_IRELATIVE comes last.  */
  4576.           plt_index = htab->next_irelative_index--;
  4577.         }
  4578.       else
  4579.         {
  4580.           rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
  4581.           plt_index = htab->next_jump_slot_index++;
  4582.         }
  4583.       loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
  4584.       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
  4585.  
  4586.       /* Don't fill PLT entry for static executables.  */
  4587.       if (plt == htab->elf.splt)
  4588.         {
  4589.           bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
  4590.                       plt->contents + h->plt.offset
  4591.                       + abed->plt->plt_reloc_offset);
  4592.           bfd_put_32 (output_bfd, - (h->plt.offset
  4593.                                      + abed->plt->plt_plt_offset + 4),
  4594.                       plt->contents + h->plt.offset
  4595.                       + abed->plt->plt_plt_offset);
  4596.         }
  4597.  
  4598.       if (!h->def_regular)
  4599.         {
  4600.           /* Mark the symbol as undefined, rather than as defined in
  4601.              the .plt section.  Leave the value if there were any
  4602.              relocations where pointer equality matters (this is a clue
  4603.              for the dynamic linker, to make function pointer
  4604.              comparisons work between an application and shared
  4605.              library), otherwise set it to zero.  If a function is only
  4606.              called from a binary, there is no need to slow down
  4607.              shared libraries because of that.  */
  4608.           sym->st_shndx = SHN_UNDEF;
  4609.           if (!h->pointer_equality_needed)
  4610.             sym->st_value = 0;
  4611.         }
  4612.     }
  4613.  
  4614.   if (h->got.offset != (bfd_vma) -1
  4615.       && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
  4616.       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
  4617.     {
  4618.       Elf_Internal_Rela rel;
  4619.  
  4620.       /* This symbol has an entry in the global offset table.  Set it
  4621.          up.  */
  4622.  
  4623.       if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
  4624.         abort ();
  4625.  
  4626.       rel.r_offset = (htab->elf.sgot->output_section->vma
  4627.                       + htab->elf.sgot->output_offset
  4628.                       + (h->got.offset & ~(bfd_vma) 1));
  4629.  
  4630.       /* If this is a static link, or it is a -Bsymbolic link and the
  4631.          symbol is defined locally or was forced to be local because
  4632.          of a version file, we just want to emit a RELATIVE reloc.
  4633.          The entry in the global offset table will already have been
  4634.          initialized in the relocate_section function.  */
  4635.       if (h->def_regular
  4636.           && h->type == STT_GNU_IFUNC)
  4637.         {
  4638.           if (info->shared)
  4639.             {
  4640.               /* Generate R_386_GLOB_DAT.  */
  4641.               goto do_glob_dat;
  4642.             }
  4643.           else
  4644.             {
  4645.               asection *plt;
  4646.  
  4647.               if (!h->pointer_equality_needed)
  4648.                 abort ();
  4649.  
  4650.               /* For non-shared object, we can't use .got.plt, which
  4651.                  contains the real function addres if we need pointer
  4652.                  equality.  We load the GOT entry with the PLT entry.  */
  4653.               plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
  4654.               bfd_put_32 (output_bfd,
  4655.                           (plt->output_section->vma
  4656.                            + plt->output_offset + h->plt.offset),
  4657.                           htab->elf.sgot->contents + h->got.offset);
  4658.               return TRUE;
  4659.             }
  4660.         }
  4661.       else if (info->shared
  4662.                && SYMBOL_REFERENCES_LOCAL (info, h))
  4663.         {
  4664.           BFD_ASSERT((h->got.offset & 1) != 0);
  4665.           rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
  4666.         }
  4667.       else
  4668.         {
  4669.           BFD_ASSERT((h->got.offset & 1) == 0);
  4670. do_glob_dat:
  4671.           bfd_put_32 (output_bfd, (bfd_vma) 0,
  4672.                       htab->elf.sgot->contents + h->got.offset);
  4673.           rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
  4674.         }
  4675.  
  4676.       elf_append_rel (output_bfd, htab->elf.srelgot, &rel);
  4677.     }
  4678.  
  4679.   if (h->needs_copy)
  4680.     {
  4681.       Elf_Internal_Rela rel;
  4682.  
  4683.       /* This symbol needs a copy reloc.  Set it up.  */
  4684.  
  4685.       if (h->dynindx == -1
  4686.           || (h->root.type != bfd_link_hash_defined
  4687.               && h->root.type != bfd_link_hash_defweak)
  4688.           || htab->srelbss == NULL)
  4689.         abort ();
  4690.  
  4691.       rel.r_offset = (h->root.u.def.value
  4692.                       + h->root.u.def.section->output_section->vma
  4693.                       + h->root.u.def.section->output_offset);
  4694.       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
  4695.       elf_append_rel (output_bfd, htab->srelbss, &rel);
  4696.     }
  4697.  
  4698.   return TRUE;
  4699. }
  4700.  
  4701. /* Finish up local dynamic symbol handling.  We set the contents of
  4702.    various dynamic sections here.  */
  4703.  
  4704. static bfd_boolean
  4705. elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
  4706. {
  4707.   struct elf_link_hash_entry *h
  4708.     = (struct elf_link_hash_entry *) *slot;
  4709.   struct bfd_link_info *info
  4710.     = (struct bfd_link_info *) inf;
  4711.  
  4712.   return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
  4713.                                          h, NULL);
  4714. }
  4715.  
  4716. /* Used to decide how to sort relocs in an optimal manner for the
  4717.    dynamic linker, before writing them out.  */
  4718.  
  4719. static enum elf_reloc_type_class
  4720. elf_i386_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  4721.                            const asection *rel_sec ATTRIBUTE_UNUSED,
  4722.                            const Elf_Internal_Rela *rela)
  4723. {
  4724.   switch (ELF32_R_TYPE (rela->r_info))
  4725.     {
  4726.     case R_386_RELATIVE:
  4727.       return reloc_class_relative;
  4728.     case R_386_JUMP_SLOT:
  4729.       return reloc_class_plt;
  4730.     case R_386_COPY:
  4731.       return reloc_class_copy;
  4732.     default:
  4733.       return reloc_class_normal;
  4734.     }
  4735. }
  4736.  
  4737. /* Finish up the dynamic sections.  */
  4738.  
  4739. static bfd_boolean
  4740. elf_i386_finish_dynamic_sections (bfd *output_bfd,
  4741.                                   struct bfd_link_info *info)
  4742. {
  4743.   struct elf_i386_link_hash_table *htab;
  4744.   bfd *dynobj;
  4745.   asection *sdyn;
  4746.   const struct elf_i386_backend_data *abed;
  4747.  
  4748.   htab = elf_i386_hash_table (info);
  4749.   if (htab == NULL)
  4750.     return FALSE;
  4751.  
  4752.   dynobj = htab->elf.dynobj;
  4753.   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  4754.   abed = get_elf_i386_backend_data (output_bfd);
  4755.  
  4756.   if (htab->elf.dynamic_sections_created)
  4757.     {
  4758.       Elf32_External_Dyn *dyncon, *dynconend;
  4759.  
  4760.       if (sdyn == NULL || htab->elf.sgot == NULL)
  4761.         abort ();
  4762.  
  4763.       dyncon = (Elf32_External_Dyn *) sdyn->contents;
  4764.       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  4765.       for (; dyncon < dynconend; dyncon++)
  4766.         {
  4767.           Elf_Internal_Dyn dyn;
  4768.           asection *s;
  4769.  
  4770.           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  4771.  
  4772.           switch (dyn.d_tag)
  4773.             {
  4774.             default:
  4775.               if (abed->is_vxworks
  4776.                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
  4777.                 break;
  4778.               continue;
  4779.  
  4780.             case DT_PLTGOT:
  4781.               s = htab->elf.sgotplt;
  4782.               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  4783.               break;
  4784.  
  4785.             case DT_JMPREL:
  4786.               s = htab->elf.srelplt;
  4787.               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  4788.               break;
  4789.  
  4790.             case DT_PLTRELSZ:
  4791.               s = htab->elf.srelplt;
  4792.               dyn.d_un.d_val = s->size;
  4793.               break;
  4794.  
  4795.             case DT_RELSZ:
  4796.               /* My reading of the SVR4 ABI indicates that the
  4797.                  procedure linkage table relocs (DT_JMPREL) should be
  4798.                  included in the overall relocs (DT_REL).  This is
  4799.                  what Solaris does.  However, UnixWare can not handle
  4800.                  that case.  Therefore, we override the DT_RELSZ entry
  4801.                  here to make it not include the JMPREL relocs.  */
  4802.               s = htab->elf.srelplt;
  4803.               if (s == NULL)
  4804.                 continue;
  4805.               dyn.d_un.d_val -= s->size;
  4806.               break;
  4807.  
  4808.             case DT_REL:
  4809.               /* We may not be using the standard ELF linker script.
  4810.                  If .rel.plt is the first .rel section, we adjust
  4811.                  DT_REL to not include it.  */
  4812.               s = htab->elf.srelplt;
  4813.               if (s == NULL)
  4814.                 continue;
  4815.               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
  4816.                 continue;
  4817.               dyn.d_un.d_ptr += s->size;
  4818.               break;
  4819.             }
  4820.  
  4821.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  4822.         }
  4823.  
  4824.       /* Fill in the first entry in the procedure linkage table.  */
  4825.       if (htab->elf.splt && htab->elf.splt->size > 0)
  4826.         {
  4827.           if (info->shared)
  4828.             {
  4829.               memcpy (htab->elf.splt->contents, abed->plt->pic_plt0_entry,
  4830.                       abed->plt->plt0_entry_size);
  4831.               memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
  4832.                       abed->plt0_pad_byte,
  4833.                       abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
  4834.             }
  4835.           else
  4836.             {
  4837.               memcpy (htab->elf.splt->contents, abed->plt->plt0_entry,
  4838.                       abed->plt->plt0_entry_size);
  4839.               memset (htab->elf.splt->contents + abed->plt->plt0_entry_size,
  4840.                       abed->plt0_pad_byte,
  4841.                       abed->plt->plt_entry_size - abed->plt->plt0_entry_size);
  4842.               bfd_put_32 (output_bfd,
  4843.                           (htab->elf.sgotplt->output_section->vma
  4844.                            + htab->elf.sgotplt->output_offset
  4845.                            + 4),
  4846.                           htab->elf.splt->contents
  4847.                           + abed->plt->plt0_got1_offset);
  4848.               bfd_put_32 (output_bfd,
  4849.                           (htab->elf.sgotplt->output_section->vma
  4850.                            + htab->elf.sgotplt->output_offset
  4851.                            + 8),
  4852.                           htab->elf.splt->contents
  4853.                           + abed->plt->plt0_got2_offset);
  4854.  
  4855.               if (abed->is_vxworks)
  4856.                 {
  4857.                   Elf_Internal_Rela rel;
  4858.  
  4859.                   /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
  4860.                      On IA32 we use REL relocations so the addend goes in
  4861.                      the PLT directly.  */
  4862.                   rel.r_offset = (htab->elf.splt->output_section->vma
  4863.                                   + htab->elf.splt->output_offset
  4864.                                   + abed->plt->plt0_got1_offset);
  4865.                   rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
  4866.                   bfd_elf32_swap_reloc_out (output_bfd, &rel,
  4867.                                             htab->srelplt2->contents);
  4868.                   /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
  4869.                   rel.r_offset = (htab->elf.splt->output_section->vma
  4870.                                   + htab->elf.splt->output_offset
  4871.                                   + abed->plt->plt0_got2_offset);
  4872.                   rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
  4873.                   bfd_elf32_swap_reloc_out (output_bfd, &rel,
  4874.                                             htab->srelplt2->contents +
  4875.                                             sizeof (Elf32_External_Rel));
  4876.                 }
  4877.             }
  4878.  
  4879.           /* UnixWare sets the entsize of .plt to 4, although that doesn't
  4880.              really seem like the right value.  */
  4881.           elf_section_data (htab->elf.splt->output_section)
  4882.             ->this_hdr.sh_entsize = 4;
  4883.  
  4884.           /* Correct the .rel.plt.unloaded relocations.  */
  4885.           if (abed->is_vxworks && !info->shared)
  4886.             {
  4887.               int num_plts = (htab->elf.splt->size
  4888.                               / abed->plt->plt_entry_size) - 1;
  4889.               unsigned char *p;
  4890.  
  4891.               p = htab->srelplt2->contents;
  4892.               if (info->shared)
  4893.                 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
  4894.               else
  4895.                 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
  4896.  
  4897.               for (; num_plts; num_plts--)
  4898.                 {
  4899.                   Elf_Internal_Rela rel;
  4900.                   bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
  4901.                   rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
  4902.                   bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
  4903.                   p += sizeof (Elf32_External_Rel);
  4904.  
  4905.                   bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
  4906.                   rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
  4907.                   bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
  4908.                   p += sizeof (Elf32_External_Rel);
  4909.                 }
  4910.             }
  4911.         }
  4912.     }
  4913.  
  4914.   if (htab->elf.sgotplt)
  4915.     {
  4916.       if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
  4917.         {
  4918.           (*_bfd_error_handler)
  4919.             (_("discarded output section: `%A'"), htab->elf.sgotplt);
  4920.           return FALSE;
  4921.         }
  4922.  
  4923.       /* Fill in the first three entries in the global offset table.  */
  4924.       if (htab->elf.sgotplt->size > 0)
  4925.         {
  4926.           bfd_put_32 (output_bfd,
  4927.                       (sdyn == NULL ? 0
  4928.                        : sdyn->output_section->vma + sdyn->output_offset),
  4929.                       htab->elf.sgotplt->contents);
  4930.           bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
  4931.           bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
  4932.         }
  4933.  
  4934.       elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
  4935.     }
  4936.  
  4937.   /* Adjust .eh_frame for .plt section.  */
  4938.   if (htab->plt_eh_frame != NULL
  4939.       && htab->plt_eh_frame->contents != NULL)
  4940.     {
  4941.       if (htab->elf.splt != NULL
  4942.           && htab->elf.splt->size != 0
  4943.           && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
  4944.           && htab->elf.splt->output_section != NULL
  4945.           && htab->plt_eh_frame->output_section != NULL)
  4946.         {
  4947.           bfd_vma plt_start = htab->elf.splt->output_section->vma;
  4948.           bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
  4949.                                    + htab->plt_eh_frame->output_offset
  4950.                                    + PLT_FDE_START_OFFSET;
  4951.           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
  4952.                              htab->plt_eh_frame->contents
  4953.                              + PLT_FDE_START_OFFSET);
  4954.         }
  4955.       if (htab->plt_eh_frame->sec_info_type
  4956.           == SEC_INFO_TYPE_EH_FRAME)
  4957.         {
  4958.           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
  4959.                                                  htab->plt_eh_frame,
  4960.                                                  htab->plt_eh_frame->contents))
  4961.             return FALSE;
  4962.         }
  4963.     }
  4964.  
  4965.   if (htab->elf.sgot && htab->elf.sgot->size > 0)
  4966.     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
  4967.  
  4968.   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
  4969.   htab_traverse (htab->loc_hash_table,
  4970.                  elf_i386_finish_local_dynamic_symbol,
  4971.                  info);
  4972.  
  4973.   return TRUE;
  4974. }
  4975.  
  4976. /* Return address for Ith PLT stub in section PLT, for relocation REL
  4977.    or (bfd_vma) -1 if it should not be included.  */
  4978.  
  4979. static bfd_vma
  4980. elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
  4981.                       const arelent *rel ATTRIBUTE_UNUSED)
  4982. {
  4983.   return plt->vma + (i + 1) * GET_PLT_ENTRY_SIZE (plt->owner);
  4984. }
  4985.  
  4986. /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
  4987.  
  4988. static bfd_boolean
  4989. elf_i386_hash_symbol (struct elf_link_hash_entry *h)
  4990. {
  4991.   if (h->plt.offset != (bfd_vma) -1
  4992.       && !h->def_regular
  4993.       && !h->pointer_equality_needed)
  4994.     return FALSE;
  4995.  
  4996.   return _bfd_elf_hash_symbol (h);
  4997. }
  4998.  
  4999. /* Hook called by the linker routine which adds symbols from an object
  5000.    file.  */
  5001.  
  5002. static bfd_boolean
  5003. elf_i386_add_symbol_hook (bfd * abfd,
  5004.                           struct bfd_link_info * info ATTRIBUTE_UNUSED,
  5005.                           Elf_Internal_Sym * sym,
  5006.                           const char ** namep ATTRIBUTE_UNUSED,
  5007.                           flagword * flagsp ATTRIBUTE_UNUSED,
  5008.                           asection ** secp ATTRIBUTE_UNUSED,
  5009.                           bfd_vma * valp ATTRIBUTE_UNUSED)
  5010. {
  5011.   if ((abfd->flags & DYNAMIC) == 0
  5012.       && (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
  5013.           || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE))
  5014.     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
  5015.  
  5016.   return TRUE;
  5017. }
  5018.  
  5019. #define TARGET_LITTLE_SYM               bfd_elf32_i386_vec
  5020. #define TARGET_LITTLE_NAME              "elf32-i386"
  5021. #define ELF_ARCH                        bfd_arch_i386
  5022. #define ELF_TARGET_ID                   I386_ELF_DATA
  5023. #define ELF_MACHINE_CODE                EM_386
  5024. #define ELF_MAXPAGESIZE                 0x1000
  5025.  
  5026. #define elf_backend_can_gc_sections     1
  5027. #define elf_backend_can_refcount        1
  5028. #define elf_backend_want_got_plt        1
  5029. #define elf_backend_plt_readonly        1
  5030. #define elf_backend_want_plt_sym        0
  5031. #define elf_backend_got_header_size     12
  5032. #define elf_backend_plt_alignment       4
  5033.  
  5034. /* Support RELA for objdump of prelink objects.  */
  5035. #define elf_info_to_howto                     elf_i386_info_to_howto_rel
  5036. #define elf_info_to_howto_rel                 elf_i386_info_to_howto_rel
  5037.  
  5038. #define bfd_elf32_mkobject                    elf_i386_mkobject
  5039.  
  5040. #define bfd_elf32_bfd_is_local_label_name     elf_i386_is_local_label_name
  5041. #define bfd_elf32_bfd_link_hash_table_create  elf_i386_link_hash_table_create
  5042. #define bfd_elf32_bfd_link_hash_table_free    elf_i386_link_hash_table_free
  5043. #define bfd_elf32_bfd_reloc_type_lookup       elf_i386_reloc_type_lookup
  5044. #define bfd_elf32_bfd_reloc_name_lookup       elf_i386_reloc_name_lookup
  5045.  
  5046. #define elf_backend_adjust_dynamic_symbol     elf_i386_adjust_dynamic_symbol
  5047. #define elf_backend_relocs_compatible         _bfd_elf_relocs_compatible
  5048. #define elf_backend_check_relocs              elf_i386_check_relocs
  5049. #define elf_backend_copy_indirect_symbol      elf_i386_copy_indirect_symbol
  5050. #define elf_backend_create_dynamic_sections   elf_i386_create_dynamic_sections
  5051. #define elf_backend_fake_sections             elf_i386_fake_sections
  5052. #define elf_backend_finish_dynamic_sections   elf_i386_finish_dynamic_sections
  5053. #define elf_backend_finish_dynamic_symbol     elf_i386_finish_dynamic_symbol
  5054. #define elf_backend_gc_mark_hook              elf_i386_gc_mark_hook
  5055. #define elf_backend_gc_sweep_hook             elf_i386_gc_sweep_hook
  5056. #define elf_backend_grok_prstatus             elf_i386_grok_prstatus
  5057. #define elf_backend_grok_psinfo               elf_i386_grok_psinfo
  5058. #define elf_backend_reloc_type_class          elf_i386_reloc_type_class
  5059. #define elf_backend_relocate_section          elf_i386_relocate_section
  5060. #define elf_backend_size_dynamic_sections     elf_i386_size_dynamic_sections
  5061. #define elf_backend_always_size_sections      elf_i386_always_size_sections
  5062. #define elf_backend_omit_section_dynsym \
  5063.   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
  5064. #define elf_backend_plt_sym_val               elf_i386_plt_sym_val
  5065. #define elf_backend_hash_symbol               elf_i386_hash_symbol
  5066. #define elf_backend_add_symbol_hook           elf_i386_add_symbol_hook
  5067. #undef  elf_backend_post_process_headers
  5068. #define elf_backend_post_process_headers        _bfd_elf_set_osabi
  5069.  
  5070. #include "elf32-target.h"
  5071.  
  5072. /* FreeBSD support.  */
  5073.  
  5074. #undef  TARGET_LITTLE_SYM
  5075. #define TARGET_LITTLE_SYM               bfd_elf32_i386_freebsd_vec
  5076. #undef  TARGET_LITTLE_NAME
  5077. #define TARGET_LITTLE_NAME              "elf32-i386-freebsd"
  5078. #undef  ELF_OSABI
  5079. #define ELF_OSABI                       ELFOSABI_FREEBSD
  5080.  
  5081. /* The kernel recognizes executables as valid only if they carry a
  5082.    "FreeBSD" label in the ELF header.  So we put this label on all
  5083.    executables and (for simplicity) also all other object files.  */
  5084.  
  5085. static void
  5086. elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
  5087. {
  5088.   _bfd_elf_set_osabi (abfd, info);
  5089.  
  5090. #ifdef OLD_FREEBSD_ABI_LABEL
  5091.   /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard.  */
  5092.   memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
  5093. #endif
  5094. }
  5095.  
  5096. #undef  elf_backend_post_process_headers
  5097. #define elf_backend_post_process_headers        elf_i386_fbsd_post_process_headers
  5098. #undef  elf32_bed
  5099. #define elf32_bed                               elf32_i386_fbsd_bed
  5100.  
  5101. #undef elf_backend_add_symbol_hook
  5102.  
  5103. #include "elf32-target.h"
  5104.  
  5105. /* Solaris 2.  */
  5106.  
  5107. #undef  TARGET_LITTLE_SYM
  5108. #define TARGET_LITTLE_SYM               bfd_elf32_i386_sol2_vec
  5109. #undef  TARGET_LITTLE_NAME
  5110. #define TARGET_LITTLE_NAME              "elf32-i386-sol2"
  5111.  
  5112. /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
  5113.    objects won't be recognized.  */
  5114. #undef ELF_OSABI
  5115.  
  5116. #undef  elf32_bed
  5117. #define elf32_bed                       elf32_i386_sol2_bed
  5118.  
  5119. /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
  5120.    boundary.  */
  5121. #undef elf_backend_static_tls_alignment
  5122. #define elf_backend_static_tls_alignment 8
  5123.  
  5124. /* The Solaris 2 ABI requires a plt symbol on all platforms.
  5125.  
  5126.    Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
  5127.    File, p.63.  */
  5128. #undef elf_backend_want_plt_sym
  5129. #define elf_backend_want_plt_sym        1
  5130.  
  5131. #include "elf32-target.h"
  5132.  
  5133. /* Native Client support.  */
  5134.  
  5135. #undef  TARGET_LITTLE_SYM
  5136. #define TARGET_LITTLE_SYM               bfd_elf32_i386_nacl_vec
  5137. #undef  TARGET_LITTLE_NAME
  5138. #define TARGET_LITTLE_NAME              "elf32-i386-nacl"
  5139. #undef  elf32_bed
  5140. #define elf32_bed                       elf32_i386_nacl_bed
  5141.  
  5142. #undef  ELF_MAXPAGESIZE
  5143. #define ELF_MAXPAGESIZE                 0x10000
  5144.  
  5145. /* Restore defaults.  */
  5146. #undef  ELF_OSABI
  5147. #undef  elf_backend_want_plt_sym
  5148. #define elf_backend_want_plt_sym        0
  5149. #undef  elf_backend_post_process_headers
  5150. #define elf_backend_post_process_headers        _bfd_elf_set_osabi
  5151. #undef  elf_backend_static_tls_alignment
  5152.  
  5153. /* NaCl uses substantially different PLT entries for the same effects.  */
  5154.  
  5155. #undef  elf_backend_plt_alignment
  5156. #define elf_backend_plt_alignment       5
  5157. #define NACL_PLT_ENTRY_SIZE             64
  5158. #define NACLMASK                        0xe0 /* 32-byte alignment mask.  */
  5159.  
  5160. static const bfd_byte elf_i386_nacl_plt0_entry[] =
  5161.   {
  5162.     0xff, 0x35,                   /* pushl contents of address */
  5163.     0, 0, 0, 0,                   /* replaced with address of .got + 4.  */
  5164.     0x8b, 0x0d,                   /* movl contents of address, %ecx */
  5165.     0, 0, 0, 0,                   /* replaced with address of .got + 8.  */
  5166.     0x83, 0xe1, NACLMASK,         /* andl $NACLMASK, %ecx */
  5167.     0xff, 0xe1                    /* jmp *%ecx */
  5168.   };
  5169.  
  5170. static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
  5171.   {
  5172.     0x8b, 0x0d,                         /* movl contents of address, %ecx */
  5173.     0, 0, 0, 0,                         /* replaced with GOT slot address.  */
  5174.     0x83, 0xe1, NACLMASK,               /* andl $NACLMASK, %ecx */
  5175.     0xff, 0xe1,                         /* jmp *%ecx */
  5176.  
  5177.     /* Pad to the next 32-byte boundary with nop instructions.  */
  5178.     0x90,
  5179.     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  5180.     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  5181.  
  5182.     /* Lazy GOT entries point here (32-byte aligned).  */
  5183.     0x68,                              /* pushl immediate */
  5184.     0, 0, 0, 0,                        /* replaced with reloc offset.  */
  5185.     0xe9,                              /* jmp relative */
  5186.     0, 0, 0, 0,                        /* replaced with offset to .plt.  */
  5187.  
  5188.     /* Pad to the next 32-byte boundary with nop instructions.  */
  5189.     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  5190.     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  5191.     0x90, 0x90
  5192.   };
  5193.  
  5194. static const bfd_byte
  5195. elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
  5196.   {
  5197.     0xff, 0x73, 0x04,           /* pushl 4(%ebx) */
  5198.     0x8b, 0x4b, 0x08,           /* mov 0x8(%ebx), %ecx */
  5199.     0x83, 0xe1, 0xe0,           /* and $NACLMASK, %ecx */
  5200.     0xff, 0xe1,                 /* jmp *%ecx */
  5201.  
  5202.     /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
  5203.        so pad to that size with nop instructions.  */
  5204.     0x90, 0x90, 0x90, 0x90, 0x90, 0x90
  5205.   };
  5206.  
  5207. static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
  5208.   {
  5209.     0x8b, 0x8b,          /* movl offset(%ebx), %ecx */
  5210.     0, 0, 0, 0,          /* replaced with offset of this symbol in .got.  */
  5211.     0x83, 0xe1, 0xe0,    /* andl $NACLMASK, %ecx */
  5212.     0xff, 0xe1,          /* jmp *%ecx */
  5213.  
  5214.     /* Pad to the next 32-byte boundary with nop instructions.  */
  5215.     0x90,
  5216.     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  5217.     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  5218.  
  5219.     /* Lazy GOT entries point here (32-byte aligned).  */
  5220.     0x68,                /* pushl immediate */
  5221.     0, 0, 0, 0,          /* replaced with offset into relocation table.  */
  5222.     0xe9,                /* jmp relative */
  5223.     0, 0, 0, 0,          /* replaced with offset to start of .plt.  */
  5224.  
  5225.     /* Pad to the next 32-byte boundary with nop instructions.  */
  5226.     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  5227.     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  5228.     0x90, 0x90
  5229.   };
  5230.  
  5231. static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
  5232.   {
  5233. #if (PLT_CIE_LENGTH != 20                               \
  5234.      || PLT_FDE_LENGTH != 36                            \
  5235.      || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8  \
  5236.      || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
  5237. # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
  5238. #endif
  5239.     PLT_CIE_LENGTH, 0, 0, 0,            /* CIE length */
  5240.     0, 0, 0, 0,                         /* CIE ID */
  5241.     1,                                  /* CIE version */
  5242.     'z', 'R', 0,                        /* Augmentation string */
  5243.     1,                                  /* Code alignment factor */
  5244.     0x7c,                               /* Data alignment factor: -4 */
  5245.     8,                                  /* Return address column */
  5246.     1,                                  /* Augmentation size */
  5247.     DW_EH_PE_pcrel | DW_EH_PE_sdata4,   /* FDE encoding */
  5248.     DW_CFA_def_cfa, 4, 4,               /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
  5249.     DW_CFA_offset + 8, 1,               /* DW_CFA_offset: r8 (eip) at cfa-4 */
  5250.     DW_CFA_nop, DW_CFA_nop,
  5251.  
  5252.     PLT_FDE_LENGTH, 0, 0, 0,     /* FDE length */
  5253.     PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
  5254.     0, 0, 0, 0,                  /* R_386_PC32 .plt goes here */
  5255.     0, 0, 0, 0,                  /* .plt size goes here */
  5256.     0,                           /* Augmentation size */
  5257.     DW_CFA_def_cfa_offset, 8,    /* DW_CFA_def_cfa_offset: 8 */
  5258.     DW_CFA_advance_loc + 6,      /* DW_CFA_advance_loc: 6 to __PLT__+6 */
  5259.     DW_CFA_def_cfa_offset, 12,   /* DW_CFA_def_cfa_offset: 12 */
  5260.     DW_CFA_advance_loc + 58,     /* DW_CFA_advance_loc: 58 to __PLT__+64 */
  5261.     DW_CFA_def_cfa_expression,   /* DW_CFA_def_cfa_expression */
  5262.     13,                          /* Block length */
  5263.     DW_OP_breg4, 4,              /* DW_OP_breg4 (esp): 4 */
  5264.     DW_OP_breg8, 0,              /* DW_OP_breg8 (eip): 0 */
  5265.     DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
  5266.     DW_OP_lit2, DW_OP_shl, DW_OP_plus,
  5267.     DW_CFA_nop, DW_CFA_nop
  5268.   };
  5269.  
  5270. static const struct elf_i386_plt_layout elf_i386_nacl_plt =
  5271.   {
  5272.     elf_i386_nacl_plt0_entry,           /* plt0_entry */
  5273.     sizeof (elf_i386_nacl_plt0_entry),  /* plt0_entry_size */
  5274.     2,                                  /* plt0_got1_offset */
  5275.     8,                                  /* plt0_got2_offset */
  5276.     elf_i386_nacl_plt_entry,            /* plt_entry */
  5277.     NACL_PLT_ENTRY_SIZE,                /* plt_entry_size */
  5278.     2,                                  /* plt_got_offset */
  5279.     33,                                 /* plt_reloc_offset */
  5280.     38,                                 /* plt_plt_offset */
  5281.     32,                                 /* plt_lazy_offset */
  5282.     elf_i386_nacl_pic_plt0_entry,       /* pic_plt0_entry */
  5283.     elf_i386_nacl_pic_plt_entry,        /* pic_plt_entry */
  5284.     elf_i386_nacl_eh_frame_plt,         /* eh_frame_plt */
  5285.     sizeof (elf_i386_nacl_eh_frame_plt),/* eh_frame_plt_size */
  5286.   };
  5287.  
  5288. static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
  5289.   {
  5290.     &elf_i386_nacl_plt,                      /* plt */
  5291.     0x90,                               /* plt0_pad_byte: nop insn */
  5292.     0,                                  /* is_vxworks */
  5293.   };
  5294.  
  5295. static bfd_boolean
  5296. elf32_i386_nacl_elf_object_p (bfd *abfd)
  5297. {
  5298.   /* Set the right machine number for a NaCl i386 ELF32 file.  */
  5299.   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
  5300.   return TRUE;
  5301. }
  5302.  
  5303. #undef  elf_backend_arch_data
  5304. #define elf_backend_arch_data   &elf_i386_nacl_arch_bed
  5305.  
  5306. #undef  elf_backend_object_p
  5307. #define elf_backend_object_p                    elf32_i386_nacl_elf_object_p
  5308. #undef  elf_backend_modify_segment_map
  5309. #define elf_backend_modify_segment_map          nacl_modify_segment_map
  5310. #undef  elf_backend_modify_program_headers
  5311. #define elf_backend_modify_program_headers      nacl_modify_program_headers
  5312. #undef  elf_backend_final_write_processing
  5313. #define elf_backend_final_write_processing      nacl_final_write_processing
  5314.  
  5315. #include "elf32-target.h"
  5316.  
  5317. /* Restore defaults.  */
  5318. #undef  elf_backend_object_p
  5319. #undef  elf_backend_modify_segment_map
  5320. #undef  elf_backend_modify_program_headers
  5321. #undef  elf_backend_final_write_processing
  5322.  
  5323. /* VxWorks support.  */
  5324.  
  5325. #undef  TARGET_LITTLE_SYM
  5326. #define TARGET_LITTLE_SYM               bfd_elf32_i386_vxworks_vec
  5327. #undef  TARGET_LITTLE_NAME
  5328. #define TARGET_LITTLE_NAME              "elf32-i386-vxworks"
  5329. #undef  ELF_OSABI
  5330. #undef  elf_backend_plt_alignment
  5331. #define elf_backend_plt_alignment       4
  5332.  
  5333. static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
  5334.   {
  5335.     &elf_i386_plt,                      /* plt */
  5336.     0x90,                               /* plt0_pad_byte */
  5337.     1,                                  /* is_vxworks */
  5338.   };
  5339.  
  5340. #undef  elf_backend_arch_data
  5341. #define elf_backend_arch_data   &elf_i386_vxworks_arch_bed
  5342.  
  5343. #undef elf_backend_relocs_compatible
  5344. #undef elf_backend_post_process_headers
  5345. #undef elf_backend_add_symbol_hook
  5346. #define elf_backend_add_symbol_hook \
  5347.   elf_vxworks_add_symbol_hook
  5348. #undef elf_backend_link_output_symbol_hook
  5349. #define elf_backend_link_output_symbol_hook \
  5350.   elf_vxworks_link_output_symbol_hook
  5351. #undef elf_backend_emit_relocs
  5352. #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
  5353. #undef elf_backend_final_write_processing
  5354. #define elf_backend_final_write_processing \
  5355.   elf_vxworks_final_write_processing
  5356. #undef elf_backend_static_tls_alignment
  5357.  
  5358. /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
  5359.    define it.  */
  5360. #undef elf_backend_want_plt_sym
  5361. #define elf_backend_want_plt_sym        1
  5362.  
  5363. #undef  elf32_bed
  5364. #define elf32_bed                               elf32_i386_vxworks_bed
  5365.  
  5366. #include "elf32-target.h"
  5367.