Subversion Repositories Kolibri OS

Rev

Rev 5197 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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