Back to index

cell-binutils  2.17cvs20070401
elf32-i386.c
Go to the documentation of this file.
00001 /* Intel 80386/80486-specific support for 32-bit ELF
00002    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
00003    2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
00004 
00005    This file is part of BFD, the Binary File Descriptor library.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00020 
00021 #include "bfd.h"
00022 #include "sysdep.h"
00023 #include "bfdlink.h"
00024 #include "libbfd.h"
00025 #include "elf-bfd.h"
00026 #include "elf-vxworks.h"
00027 
00028 /* 386 uses REL relocations instead of RELA.  */
00029 #define USE_REL      1
00030 
00031 #include "elf/i386.h"
00032 
00033 static reloc_howto_type elf_howto_table[]=
00034 {
00035   HOWTO(R_386_NONE, 0, 0, 0, FALSE, 0, complain_overflow_bitfield,
00036        bfd_elf_generic_reloc, "R_386_NONE",
00037        TRUE, 0x00000000, 0x00000000, FALSE),
00038   HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00039        bfd_elf_generic_reloc, "R_386_32",
00040        TRUE, 0xffffffff, 0xffffffff, FALSE),
00041   HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
00042        bfd_elf_generic_reloc, "R_386_PC32",
00043        TRUE, 0xffffffff, 0xffffffff, TRUE),
00044   HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00045        bfd_elf_generic_reloc, "R_386_GOT32",
00046        TRUE, 0xffffffff, 0xffffffff, FALSE),
00047   HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
00048        bfd_elf_generic_reloc, "R_386_PLT32",
00049        TRUE, 0xffffffff, 0xffffffff, TRUE),
00050   HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00051        bfd_elf_generic_reloc, "R_386_COPY",
00052        TRUE, 0xffffffff, 0xffffffff, FALSE),
00053   HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00054        bfd_elf_generic_reloc, "R_386_GLOB_DAT",
00055        TRUE, 0xffffffff, 0xffffffff, FALSE),
00056   HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00057        bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
00058        TRUE, 0xffffffff, 0xffffffff, FALSE),
00059   HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00060        bfd_elf_generic_reloc, "R_386_RELATIVE",
00061        TRUE, 0xffffffff, 0xffffffff, FALSE),
00062   HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00063        bfd_elf_generic_reloc, "R_386_GOTOFF",
00064        TRUE, 0xffffffff, 0xffffffff, FALSE),
00065   HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
00066        bfd_elf_generic_reloc, "R_386_GOTPC",
00067        TRUE, 0xffffffff, 0xffffffff, TRUE),
00068 
00069   /* We have a gap in the reloc numbers here.
00070      R_386_standard counts the number up to this point, and
00071      R_386_ext_offset is the value to subtract from a reloc type of
00072      R_386_16 thru R_386_PC8 to form an index into this table.  */
00073 #define R_386_standard (R_386_GOTPC + 1)
00074 #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
00075 
00076   /* These relocs are a GNU extension.  */
00077   HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00078        bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
00079        TRUE, 0xffffffff, 0xffffffff, FALSE),
00080   HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00081        bfd_elf_generic_reloc, "R_386_TLS_IE",
00082        TRUE, 0xffffffff, 0xffffffff, FALSE),
00083   HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00084        bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
00085        TRUE, 0xffffffff, 0xffffffff, FALSE),
00086   HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00087        bfd_elf_generic_reloc, "R_386_TLS_LE",
00088        TRUE, 0xffffffff, 0xffffffff, FALSE),
00089   HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00090        bfd_elf_generic_reloc, "R_386_TLS_GD",
00091        TRUE, 0xffffffff, 0xffffffff, FALSE),
00092   HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00093        bfd_elf_generic_reloc, "R_386_TLS_LDM",
00094        TRUE, 0xffffffff, 0xffffffff, FALSE),
00095   HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
00096        bfd_elf_generic_reloc, "R_386_16",
00097        TRUE, 0xffff, 0xffff, FALSE),
00098   HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
00099        bfd_elf_generic_reloc, "R_386_PC16",
00100        TRUE, 0xffff, 0xffff, TRUE),
00101   HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
00102        bfd_elf_generic_reloc, "R_386_8",
00103        TRUE, 0xff, 0xff, FALSE),
00104   HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
00105        bfd_elf_generic_reloc, "R_386_PC8",
00106        TRUE, 0xff, 0xff, TRUE),
00107 
00108 #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
00109 #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
00110   /* These are common with Solaris TLS implementation.  */
00111   HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00112        bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
00113        TRUE, 0xffffffff, 0xffffffff, FALSE),
00114   HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00115        bfd_elf_generic_reloc, "R_386_TLS_IE_32",
00116        TRUE, 0xffffffff, 0xffffffff, FALSE),
00117   HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00118        bfd_elf_generic_reloc, "R_386_TLS_LE_32",
00119        TRUE, 0xffffffff, 0xffffffff, FALSE),
00120   HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00121        bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
00122        TRUE, 0xffffffff, 0xffffffff, FALSE),
00123   HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00124        bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
00125        TRUE, 0xffffffff, 0xffffffff, FALSE),
00126   HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00127        bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
00128        TRUE, 0xffffffff, 0xffffffff, FALSE),
00129   EMPTY_HOWTO (38),
00130   HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00131        bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
00132        TRUE, 0xffffffff, 0xffffffff, FALSE),
00133   HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
00134        bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
00135        FALSE, 0, 0, FALSE),
00136   HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00137        bfd_elf_generic_reloc, "R_386_TLS_DESC",
00138        TRUE, 0xffffffff, 0xffffffff, FALSE),
00139 
00140   /* Another gap.  */
00141 #define R_386_tls (R_386_TLS_DESC + 1 - R_386_tls_offset)
00142 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_tls)
00143 
00144 /* GNU extension to record C++ vtable hierarchy.  */
00145   HOWTO (R_386_GNU_VTINHERIT,      /* type */
00146         0,                  /* rightshift */
00147         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00148         0,                  /* bitsize */
00149         FALSE,                     /* pc_relative */
00150         0,                  /* bitpos */
00151         complain_overflow_dont, /* complain_on_overflow */
00152         NULL,               /* special_function */
00153         "R_386_GNU_VTINHERIT",     /* name */
00154         FALSE,                     /* partial_inplace */
00155         0,                  /* src_mask */
00156         0,                  /* dst_mask */
00157         FALSE),             /* pcrel_offset */
00158 
00159 /* GNU extension to record C++ vtable member usage.  */
00160   HOWTO (R_386_GNU_VTENTRY, /* type */
00161         0,                  /* rightshift */
00162         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00163         0,                  /* bitsize */
00164         FALSE,                     /* pc_relative */
00165         0,                  /* bitpos */
00166         complain_overflow_dont, /* complain_on_overflow */
00167         _bfd_elf_rel_vtable_reloc_fn, /* special_function */
00168         "R_386_GNU_VTENTRY",       /* name */
00169         FALSE,                     /* partial_inplace */
00170         0,                  /* src_mask */
00171         0,                  /* dst_mask */
00172         FALSE)                     /* pcrel_offset */
00173 
00174 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
00175 
00176 };
00177 
00178 #ifdef DEBUG_GEN_RELOC
00179 #define TRACE(str) \
00180   fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
00181 #else
00182 #define TRACE(str)
00183 #endif
00184 
00185 static reloc_howto_type *
00186 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00187                          bfd_reloc_code_real_type code)
00188 {
00189   switch (code)
00190     {
00191     case BFD_RELOC_NONE:
00192       TRACE ("BFD_RELOC_NONE");
00193       return &elf_howto_table[R_386_NONE];
00194 
00195     case BFD_RELOC_32:
00196       TRACE ("BFD_RELOC_32");
00197       return &elf_howto_table[R_386_32];
00198 
00199     case BFD_RELOC_CTOR:
00200       TRACE ("BFD_RELOC_CTOR");
00201       return &elf_howto_table[R_386_32];
00202 
00203     case BFD_RELOC_32_PCREL:
00204       TRACE ("BFD_RELOC_PC32");
00205       return &elf_howto_table[R_386_PC32];
00206 
00207     case BFD_RELOC_386_GOT32:
00208       TRACE ("BFD_RELOC_386_GOT32");
00209       return &elf_howto_table[R_386_GOT32];
00210 
00211     case BFD_RELOC_386_PLT32:
00212       TRACE ("BFD_RELOC_386_PLT32");
00213       return &elf_howto_table[R_386_PLT32];
00214 
00215     case BFD_RELOC_386_COPY:
00216       TRACE ("BFD_RELOC_386_COPY");
00217       return &elf_howto_table[R_386_COPY];
00218 
00219     case BFD_RELOC_386_GLOB_DAT:
00220       TRACE ("BFD_RELOC_386_GLOB_DAT");
00221       return &elf_howto_table[R_386_GLOB_DAT];
00222 
00223     case BFD_RELOC_386_JUMP_SLOT:
00224       TRACE ("BFD_RELOC_386_JUMP_SLOT");
00225       return &elf_howto_table[R_386_JUMP_SLOT];
00226 
00227     case BFD_RELOC_386_RELATIVE:
00228       TRACE ("BFD_RELOC_386_RELATIVE");
00229       return &elf_howto_table[R_386_RELATIVE];
00230 
00231     case BFD_RELOC_386_GOTOFF:
00232       TRACE ("BFD_RELOC_386_GOTOFF");
00233       return &elf_howto_table[R_386_GOTOFF];
00234 
00235     case BFD_RELOC_386_GOTPC:
00236       TRACE ("BFD_RELOC_386_GOTPC");
00237       return &elf_howto_table[R_386_GOTPC];
00238 
00239       /* These relocs are a GNU extension.  */
00240     case BFD_RELOC_386_TLS_TPOFF:
00241       TRACE ("BFD_RELOC_386_TLS_TPOFF");
00242       return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
00243 
00244     case BFD_RELOC_386_TLS_IE:
00245       TRACE ("BFD_RELOC_386_TLS_IE");
00246       return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
00247 
00248     case BFD_RELOC_386_TLS_GOTIE:
00249       TRACE ("BFD_RELOC_386_TLS_GOTIE");
00250       return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
00251 
00252     case BFD_RELOC_386_TLS_LE:
00253       TRACE ("BFD_RELOC_386_TLS_LE");
00254       return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
00255 
00256     case BFD_RELOC_386_TLS_GD:
00257       TRACE ("BFD_RELOC_386_TLS_GD");
00258       return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
00259 
00260     case BFD_RELOC_386_TLS_LDM:
00261       TRACE ("BFD_RELOC_386_TLS_LDM");
00262       return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
00263 
00264     case BFD_RELOC_16:
00265       TRACE ("BFD_RELOC_16");
00266       return &elf_howto_table[R_386_16 - R_386_ext_offset];
00267 
00268     case BFD_RELOC_16_PCREL:
00269       TRACE ("BFD_RELOC_16_PCREL");
00270       return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
00271 
00272     case BFD_RELOC_8:
00273       TRACE ("BFD_RELOC_8");
00274       return &elf_howto_table[R_386_8 - R_386_ext_offset];
00275 
00276     case BFD_RELOC_8_PCREL:
00277       TRACE ("BFD_RELOC_8_PCREL");
00278       return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
00279 
00280     /* Common with Sun TLS implementation.  */
00281     case BFD_RELOC_386_TLS_LDO_32:
00282       TRACE ("BFD_RELOC_386_TLS_LDO_32");
00283       return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
00284 
00285     case BFD_RELOC_386_TLS_IE_32:
00286       TRACE ("BFD_RELOC_386_TLS_IE_32");
00287       return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
00288 
00289     case BFD_RELOC_386_TLS_LE_32:
00290       TRACE ("BFD_RELOC_386_TLS_LE_32");
00291       return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
00292 
00293     case BFD_RELOC_386_TLS_DTPMOD32:
00294       TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
00295       return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
00296 
00297     case BFD_RELOC_386_TLS_DTPOFF32:
00298       TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
00299       return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
00300 
00301     case BFD_RELOC_386_TLS_TPOFF32:
00302       TRACE ("BFD_RELOC_386_TLS_TPOFF32");
00303       return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
00304 
00305     case BFD_RELOC_386_TLS_GOTDESC:
00306       TRACE ("BFD_RELOC_386_TLS_GOTDESC");
00307       return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
00308 
00309     case BFD_RELOC_386_TLS_DESC_CALL:
00310       TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
00311       return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
00312 
00313     case BFD_RELOC_386_TLS_DESC:
00314       TRACE ("BFD_RELOC_386_TLS_DESC");
00315       return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
00316 
00317     case BFD_RELOC_VTABLE_INHERIT:
00318       TRACE ("BFD_RELOC_VTABLE_INHERIT");
00319       return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
00320 
00321     case BFD_RELOC_VTABLE_ENTRY:
00322       TRACE ("BFD_RELOC_VTABLE_ENTRY");
00323       return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
00324 
00325     default:
00326       break;
00327     }
00328 
00329   TRACE ("Unknown");
00330   return 0;
00331 }
00332 
00333 static reloc_howto_type *
00334 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00335                          const char *r_name)
00336 {
00337   unsigned int i;
00338 
00339   for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
00340     if (elf_howto_table[i].name != NULL
00341        && strcasecmp (elf_howto_table[i].name, r_name) == 0)
00342       return &elf_howto_table[i];
00343 
00344   return NULL;
00345 }
00346 
00347 static void
00348 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
00349                          arelent *cache_ptr,
00350                          Elf_Internal_Rela *dst)
00351 {
00352   unsigned int r_type = ELF32_R_TYPE (dst->r_info);
00353   unsigned int indx;
00354 
00355   if ((indx = r_type) >= R_386_standard
00356       && ((indx = r_type - R_386_ext_offset) - R_386_standard
00357          >= R_386_ext - R_386_standard)
00358       && ((indx = r_type - R_386_tls_offset) - R_386_ext
00359          >= R_386_tls - R_386_ext)
00360       && ((indx = r_type - R_386_vt_offset) - R_386_tls
00361          >= R_386_vt - R_386_tls))
00362     {
00363       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
00364                           abfd, (int) r_type);
00365       indx = R_386_NONE;
00366     }
00367   cache_ptr->howto = &elf_howto_table[indx];
00368 }
00369 
00370 /* Return whether a symbol name implies a local label.  The UnixWare
00371    2.1 cc generates temporary symbols that start with .X, so we
00372    recognize them here.  FIXME: do other SVR4 compilers also use .X?.
00373    If so, we should move the .X recognition into
00374    _bfd_elf_is_local_label_name.  */
00375 
00376 static bfd_boolean
00377 elf_i386_is_local_label_name (bfd *abfd, const char *name)
00378 {
00379   if (name[0] == '.' && name[1] == 'X')
00380     return TRUE;
00381 
00382   return _bfd_elf_is_local_label_name (abfd, name);
00383 }
00384 
00385 /* Support for core dump NOTE sections.  */
00386 
00387 static bfd_boolean
00388 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
00389 {
00390   int offset;
00391   size_t size;
00392 
00393   if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
00394     {
00395       int pr_version = bfd_get_32 (abfd, note->descdata);
00396 
00397       if (pr_version != 1)
00398        return FALSE;
00399 
00400       /* pr_cursig */
00401       elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 20);
00402 
00403       /* pr_pid */
00404       elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
00405 
00406       /* pr_reg */
00407       offset = 28;
00408       size = bfd_get_32 (abfd, note->descdata + 8);
00409     }
00410   else
00411     {
00412       switch (note->descsz)
00413        {
00414        default:
00415          return FALSE;
00416 
00417        case 144:            /* Linux/i386 */
00418          /* pr_cursig */
00419          elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
00420 
00421          /* pr_pid */
00422          elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
00423 
00424          /* pr_reg */
00425          offset = 72;
00426          size = 68;
00427 
00428          break;
00429        }
00430     }
00431 
00432   /* Make a ".reg/999" section.  */
00433   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
00434                                      size, note->descpos + offset);
00435 }
00436 
00437 static bfd_boolean
00438 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
00439 {
00440   if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
00441     {
00442       int pr_version = bfd_get_32 (abfd, note->descdata);
00443 
00444       if (pr_version != 1)
00445        return FALSE;
00446 
00447       elf_tdata (abfd)->core_program
00448        = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
00449       elf_tdata (abfd)->core_command
00450        = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
00451     }
00452   else
00453     {
00454       switch (note->descsz)
00455        {
00456        default:
00457          return FALSE;
00458 
00459        case 124:            /* Linux/i386 elf_prpsinfo.  */
00460          elf_tdata (abfd)->core_program
00461            = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
00462          elf_tdata (abfd)->core_command
00463            = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
00464        }
00465     }
00466 
00467   /* Note that for some reason, a spurious space is tacked
00468      onto the end of the args in some (at least one anyway)
00469      implementations, so strip it off if it exists.  */
00470   {
00471     char *command = elf_tdata (abfd)->core_command;
00472     int n = strlen (command);
00473 
00474     if (0 < n && command[n - 1] == ' ')
00475       command[n - 1] = '\0';
00476   }
00477 
00478   return TRUE;
00479 }
00480 
00481 /* Functions for the i386 ELF linker.
00482 
00483    In order to gain some understanding of code in this file without
00484    knowing all the intricate details of the linker, note the
00485    following:
00486 
00487    Functions named elf_i386_* are called by external routines, other
00488    functions are only called locally.  elf_i386_* functions appear
00489    in this file more or less in the order in which they are called
00490    from external routines.  eg. elf_i386_check_relocs is called
00491    early in the link process, elf_i386_finish_dynamic_sections is
00492    one of the last functions.  */
00493 
00494 
00495 /* The name of the dynamic interpreter.  This is put in the .interp
00496    section.  */
00497 
00498 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
00499 
00500 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
00501    copying dynamic variables from a shared lib into an app's dynbss
00502    section, and instead use a dynamic relocation to point into the
00503    shared lib.  */
00504 #define ELIMINATE_COPY_RELOCS 1
00505 
00506 /* The size in bytes of an entry in the procedure linkage table.  */
00507 
00508 #define PLT_ENTRY_SIZE 16
00509 
00510 /* The first entry in an absolute procedure linkage table looks like
00511    this.  See the SVR4 ABI i386 supplement to see how this works.
00512    Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte.  */
00513 
00514 static const bfd_byte elf_i386_plt0_entry[12] =
00515 {
00516   0xff, 0x35, /* pushl contents of address */
00517   0, 0, 0, 0, /* replaced with address of .got + 4.  */
00518   0xff, 0x25, /* jmp indirect */
00519   0, 0, 0, 0  /* replaced with address of .got + 8.  */
00520 };
00521 
00522 /* Subsequent entries in an absolute procedure linkage table look like
00523    this.  */
00524 
00525 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
00526 {
00527   0xff, 0x25, /* jmp indirect */
00528   0, 0, 0, 0, /* replaced with address of this symbol in .got.  */
00529   0x68,              /* pushl immediate */
00530   0, 0, 0, 0, /* replaced with offset into relocation table.  */
00531   0xe9,              /* jmp relative */
00532   0, 0, 0, 0  /* replaced with offset to start of .plt.  */
00533 };
00534 
00535 /* The first entry in a PIC procedure linkage table look like this.
00536    Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte.  */
00537 
00538 static const bfd_byte elf_i386_pic_plt0_entry[12] =
00539 {
00540   0xff, 0xb3, 4, 0, 0, 0,   /* pushl 4(%ebx) */
00541   0xff, 0xa3, 8, 0, 0, 0    /* jmp *8(%ebx) */
00542 };
00543 
00544 /* Subsequent entries in a PIC procedure linkage table look like this.  */
00545 
00546 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
00547 {
00548   0xff, 0xa3, /* jmp *offset(%ebx) */
00549   0, 0, 0, 0, /* replaced with offset of this symbol in .got.  */
00550   0x68,              /* pushl immediate */
00551   0, 0, 0, 0, /* replaced with offset into relocation table.  */
00552   0xe9,              /* jmp relative */
00553   0, 0, 0, 0  /* replaced with offset to start of .plt.  */
00554 };
00555 
00556 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
00557    for the PLTResolve stub and then for each PLT entry.  */
00558 #define PLTRESOLVE_RELOCS_SHLIB 0
00559 #define PLTRESOLVE_RELOCS 2
00560 #define PLT_NON_JUMP_SLOT_RELOCS 2
00561 
00562 /* The i386 linker needs to keep track of the number of relocs that it
00563    decides to copy as dynamic relocs in check_relocs for each symbol.
00564    This is so that it can later discard them if they are found to be
00565    unnecessary.  We store the information in a field extending the
00566    regular ELF linker hash table.  */
00567 
00568 struct elf_i386_dyn_relocs
00569 {
00570   struct elf_i386_dyn_relocs *next;
00571 
00572   /* The input section of the reloc.  */
00573   asection *sec;
00574 
00575   /* Total number of relocs copied for the input section.  */
00576   bfd_size_type count;
00577 
00578   /* Number of pc-relative relocs copied for the input section.  */
00579   bfd_size_type pc_count;
00580 };
00581 
00582 /* i386 ELF linker hash entry.  */
00583 
00584 struct elf_i386_link_hash_entry
00585 {
00586   struct elf_link_hash_entry elf;
00587 
00588   /* Track dynamic relocs copied for this symbol.  */
00589   struct elf_i386_dyn_relocs *dyn_relocs;
00590 
00591 #define GOT_UNKNOWN  0
00592 #define GOT_NORMAL   1
00593 #define GOT_TLS_GD   2
00594 #define GOT_TLS_IE   4
00595 #define GOT_TLS_IE_POS      5
00596 #define GOT_TLS_IE_NEG      6
00597 #define GOT_TLS_IE_BOTH 7
00598 #define GOT_TLS_GDESC       8
00599 #define GOT_TLS_GD_BOTH_P(type)                                       \
00600   ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
00601 #define GOT_TLS_GD_P(type)                                     \
00602   ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
00603 #define GOT_TLS_GDESC_P(type)                                         \
00604   ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
00605 #define GOT_TLS_GD_ANY_P(type)                                        \
00606   (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
00607   unsigned char tls_type;
00608 
00609   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
00610      starting at the end of the jump table.  */
00611   bfd_vma tlsdesc_got;
00612 };
00613 
00614 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
00615 
00616 struct elf_i386_obj_tdata
00617 {
00618   struct elf_obj_tdata root;
00619 
00620   /* tls_type for each local got entry.  */
00621   char *local_got_tls_type;
00622 
00623   /* GOTPLT entries for TLS descriptors.  */
00624   bfd_vma *local_tlsdesc_gotent;
00625 };
00626 
00627 #define elf_i386_tdata(abfd) \
00628   ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
00629 
00630 #define elf_i386_local_got_tls_type(abfd) \
00631   (elf_i386_tdata (abfd)->local_got_tls_type)
00632 
00633 #define elf_i386_local_tlsdesc_gotent(abfd) \
00634   (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
00635 
00636 static bfd_boolean
00637 elf_i386_mkobject (bfd *abfd)
00638 {
00639   if (abfd->tdata.any == NULL)
00640     {
00641       bfd_size_type amt = sizeof (struct elf_i386_obj_tdata);
00642       abfd->tdata.any = bfd_zalloc (abfd, amt);
00643       if (abfd->tdata.any == NULL)
00644        return FALSE;
00645     }
00646   return bfd_elf_mkobject (abfd);
00647 }
00648 
00649 /* i386 ELF linker hash table.  */
00650 
00651 struct elf_i386_link_hash_table
00652 {
00653   struct elf_link_hash_table elf;
00654 
00655   /* Short-cuts to get to dynamic linker sections.  */
00656   asection *sgot;
00657   asection *sgotplt;
00658   asection *srelgot;
00659   asection *splt;
00660   asection *srelplt;
00661   asection *sdynbss;
00662   asection *srelbss;
00663 
00664   /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.  */
00665   asection *srelplt2;
00666 
00667   /* True if the target system is VxWorks.  */
00668   int is_vxworks;
00669 
00670   /* Value used to fill the last word of the first plt entry.  */
00671   bfd_byte plt0_pad_byte;
00672 
00673   /* The index of the next unused R_386_TLS_DESC slot in .rel.plt.  */
00674   bfd_vma next_tls_desc_index;
00675 
00676   union {
00677     bfd_signed_vma refcount;
00678     bfd_vma offset;
00679   } tls_ldm_got;
00680 
00681   /* The amount of space used by the reserved portion of the sgotplt
00682      section, plus whatever space is used by the jump slots.  */
00683   bfd_vma sgotplt_jump_table_size;
00684 
00685   /* Small local sym to section mapping cache.  */
00686   struct sym_sec_cache sym_sec;
00687 };
00688 
00689 /* Get the i386 ELF linker hash table from a link_info structure.  */
00690 
00691 #define elf_i386_hash_table(p) \
00692   ((struct elf_i386_link_hash_table *) ((p)->hash))
00693 
00694 #define elf_i386_compute_jump_table_size(htab) \
00695   ((htab)->next_tls_desc_index * 4)
00696 
00697 /* Create an entry in an i386 ELF linker hash table.  */
00698 
00699 static struct bfd_hash_entry *
00700 link_hash_newfunc (struct bfd_hash_entry *entry,
00701                  struct bfd_hash_table *table,
00702                  const char *string)
00703 {
00704   /* Allocate the structure if it has not already been allocated by a
00705      subclass.  */
00706   if (entry == NULL)
00707     {
00708       entry = bfd_hash_allocate (table,
00709                              sizeof (struct elf_i386_link_hash_entry));
00710       if (entry == NULL)
00711        return entry;
00712     }
00713 
00714   /* Call the allocation method of the superclass.  */
00715   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
00716   if (entry != NULL)
00717     {
00718       struct elf_i386_link_hash_entry *eh;
00719 
00720       eh = (struct elf_i386_link_hash_entry *) entry;
00721       eh->dyn_relocs = NULL;
00722       eh->tls_type = GOT_UNKNOWN;
00723       eh->tlsdesc_got = (bfd_vma) -1;
00724     }
00725 
00726   return entry;
00727 }
00728 
00729 /* Create an i386 ELF linker hash table.  */
00730 
00731 static struct bfd_link_hash_table *
00732 elf_i386_link_hash_table_create (bfd *abfd)
00733 {
00734   struct elf_i386_link_hash_table *ret;
00735   bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
00736 
00737   ret = bfd_malloc (amt);
00738   if (ret == NULL)
00739     return NULL;
00740 
00741   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
00742                                   sizeof (struct elf_i386_link_hash_entry)))
00743     {
00744       free (ret);
00745       return NULL;
00746     }
00747 
00748   ret->sgot = NULL;
00749   ret->sgotplt = NULL;
00750   ret->srelgot = NULL;
00751   ret->splt = NULL;
00752   ret->srelplt = NULL;
00753   ret->sdynbss = NULL;
00754   ret->srelbss = NULL;
00755   ret->tls_ldm_got.refcount = 0;
00756   ret->next_tls_desc_index = 0;
00757   ret->sgotplt_jump_table_size = 0;
00758   ret->sym_sec.abfd = NULL;
00759   ret->is_vxworks = 0;
00760   ret->srelplt2 = NULL;
00761   ret->plt0_pad_byte = 0;
00762 
00763   return &ret->elf.root;
00764 }
00765 
00766 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
00767    shortcuts to them in our hash table.  */
00768 
00769 static bfd_boolean
00770 create_got_section (bfd *dynobj, struct bfd_link_info *info)
00771 {
00772   struct elf_i386_link_hash_table *htab;
00773 
00774   if (! _bfd_elf_create_got_section (dynobj, info))
00775     return FALSE;
00776 
00777   htab = elf_i386_hash_table (info);
00778   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
00779   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
00780   if (!htab->sgot || !htab->sgotplt)
00781     abort ();
00782 
00783   htab->srelgot = bfd_make_section_with_flags (dynobj, ".rel.got",
00784                                           (SEC_ALLOC | SEC_LOAD
00785                                           | SEC_HAS_CONTENTS
00786                                           | SEC_IN_MEMORY
00787                                           | SEC_LINKER_CREATED
00788                                           | SEC_READONLY));
00789   if (htab->srelgot == NULL
00790       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
00791     return FALSE;
00792   return TRUE;
00793 }
00794 
00795 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
00796    .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
00797    hash table.  */
00798 
00799 static bfd_boolean
00800 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
00801 {
00802   struct elf_i386_link_hash_table *htab;
00803 
00804   htab = elf_i386_hash_table (info);
00805   if (!htab->sgot && !create_got_section (dynobj, info))
00806     return FALSE;
00807 
00808   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
00809     return FALSE;
00810 
00811   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
00812   htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
00813   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
00814   if (!info->shared)
00815     htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
00816 
00817   if (!htab->splt || !htab->srelplt || !htab->sdynbss
00818       || (!info->shared && !htab->srelbss))
00819     abort ();
00820 
00821   if (htab->is_vxworks
00822       && !elf_vxworks_create_dynamic_sections (dynobj, info, &htab->srelplt2))
00823     return FALSE;
00824 
00825   return TRUE;
00826 }
00827 
00828 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
00829 
00830 static void
00831 elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
00832                             struct elf_link_hash_entry *dir,
00833                             struct elf_link_hash_entry *ind)
00834 {
00835   struct elf_i386_link_hash_entry *edir, *eind;
00836 
00837   edir = (struct elf_i386_link_hash_entry *) dir;
00838   eind = (struct elf_i386_link_hash_entry *) ind;
00839 
00840   if (eind->dyn_relocs != NULL)
00841     {
00842       if (edir->dyn_relocs != NULL)
00843        {
00844          struct elf_i386_dyn_relocs **pp;
00845          struct elf_i386_dyn_relocs *p;
00846 
00847          /* Add reloc counts against the indirect sym to the direct sym
00848             list.  Merge any entries against the same section.  */
00849          for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
00850            {
00851              struct elf_i386_dyn_relocs *q;
00852 
00853              for (q = edir->dyn_relocs; q != NULL; q = q->next)
00854               if (q->sec == p->sec)
00855                 {
00856                   q->pc_count += p->pc_count;
00857                   q->count += p->count;
00858                   *pp = p->next;
00859                   break;
00860                 }
00861              if (q == NULL)
00862               pp = &p->next;
00863            }
00864          *pp = edir->dyn_relocs;
00865        }
00866 
00867       edir->dyn_relocs = eind->dyn_relocs;
00868       eind->dyn_relocs = NULL;
00869     }
00870 
00871   if (ind->root.type == bfd_link_hash_indirect
00872       && dir->got.refcount <= 0)
00873     {
00874       edir->tls_type = eind->tls_type;
00875       eind->tls_type = GOT_UNKNOWN;
00876     }
00877 
00878   if (ELIMINATE_COPY_RELOCS
00879       && ind->root.type != bfd_link_hash_indirect
00880       && dir->dynamic_adjusted)
00881     {
00882       /* If called to transfer flags for a weakdef during processing
00883         of elf_adjust_dynamic_symbol, don't copy non_got_ref.
00884         We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
00885       dir->ref_dynamic |= ind->ref_dynamic;
00886       dir->ref_regular |= ind->ref_regular;
00887       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
00888       dir->needs_plt |= ind->needs_plt;
00889       dir->pointer_equality_needed |= ind->pointer_equality_needed;
00890     }
00891   else
00892     _bfd_elf_link_hash_copy_indirect (info, dir, ind);
00893 }
00894 
00895 static int
00896 elf_i386_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
00897 {
00898   if (info->shared)
00899     return r_type;
00900 
00901   switch (r_type)
00902     {
00903     case R_386_TLS_GD:
00904     case R_386_TLS_GOTDESC:
00905     case R_386_TLS_DESC_CALL:
00906     case R_386_TLS_IE_32:
00907       if (is_local)
00908        return R_386_TLS_LE_32;
00909       return R_386_TLS_IE_32;
00910     case R_386_TLS_IE:
00911     case R_386_TLS_GOTIE:
00912       if (is_local)
00913        return R_386_TLS_LE_32;
00914       return r_type;
00915     case R_386_TLS_LDM:
00916       return R_386_TLS_LE_32;
00917     }
00918 
00919   return r_type;
00920 }
00921 
00922 /* Look through the relocs for a section during the first phase, and
00923    calculate needed space in the global offset table, procedure linkage
00924    table, and dynamic reloc sections.  */
00925 
00926 static bfd_boolean
00927 elf_i386_check_relocs (bfd *abfd,
00928                      struct bfd_link_info *info,
00929                      asection *sec,
00930                      const Elf_Internal_Rela *relocs)
00931 {
00932   struct elf_i386_link_hash_table *htab;
00933   Elf_Internal_Shdr *symtab_hdr;
00934   struct elf_link_hash_entry **sym_hashes;
00935   const Elf_Internal_Rela *rel;
00936   const Elf_Internal_Rela *rel_end;
00937   asection *sreloc;
00938 
00939   if (info->relocatable)
00940     return TRUE;
00941 
00942   htab = elf_i386_hash_table (info);
00943   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
00944   sym_hashes = elf_sym_hashes (abfd);
00945 
00946   sreloc = NULL;
00947 
00948   rel_end = relocs + sec->reloc_count;
00949   for (rel = relocs; rel < rel_end; rel++)
00950     {
00951       unsigned int r_type;
00952       unsigned long r_symndx;
00953       struct elf_link_hash_entry *h;
00954 
00955       r_symndx = ELF32_R_SYM (rel->r_info);
00956       r_type = ELF32_R_TYPE (rel->r_info);
00957 
00958       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
00959        {
00960          (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
00961                              abfd,
00962                              r_symndx);
00963          return FALSE;
00964        }
00965 
00966       if (r_symndx < symtab_hdr->sh_info)
00967        h = NULL;
00968       else
00969        {
00970          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
00971          while (h->root.type == bfd_link_hash_indirect
00972                || h->root.type == bfd_link_hash_warning)
00973            h = (struct elf_link_hash_entry *) h->root.u.i.link;
00974        }
00975 
00976       r_type = elf_i386_tls_transition (info, r_type, h == NULL);
00977 
00978       switch (r_type)
00979        {
00980        case R_386_TLS_LDM:
00981          htab->tls_ldm_got.refcount += 1;
00982          goto create_got;
00983 
00984        case R_386_PLT32:
00985          /* This symbol requires a procedure linkage table entry.  We
00986             actually build the entry in adjust_dynamic_symbol,
00987             because this might be a case of linking PIC code which is
00988             never referenced by a dynamic object, in which case we
00989             don't need to generate a procedure linkage table entry
00990             after all.  */
00991 
00992          /* If this is a local symbol, we resolve it directly without
00993             creating a procedure linkage table entry.  */
00994          if (h == NULL)
00995            continue;
00996 
00997          h->needs_plt = 1;
00998          h->plt.refcount += 1;
00999          break;
01000 
01001        case R_386_TLS_IE_32:
01002        case R_386_TLS_IE:
01003        case R_386_TLS_GOTIE:
01004          if (info->shared)
01005            info->flags |= DF_STATIC_TLS;
01006          /* Fall through */
01007 
01008        case R_386_GOT32:
01009        case R_386_TLS_GD:
01010        case R_386_TLS_GOTDESC:
01011        case R_386_TLS_DESC_CALL:
01012          /* This symbol requires a global offset table entry.  */
01013          {
01014            int tls_type, old_tls_type;
01015 
01016            switch (r_type)
01017              {
01018              default:
01019              case R_386_GOT32: tls_type = GOT_NORMAL; break;
01020              case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
01021              case R_386_TLS_GOTDESC:
01022              case R_386_TLS_DESC_CALL:
01023               tls_type = GOT_TLS_GDESC; break;
01024              case R_386_TLS_IE_32:
01025               if (ELF32_R_TYPE (rel->r_info) == r_type)
01026                 tls_type = GOT_TLS_IE_NEG;
01027               else
01028                 /* If this is a GD->IE transition, we may use either of
01029                    R_386_TLS_TPOFF and R_386_TLS_TPOFF32.  */
01030                 tls_type = GOT_TLS_IE;
01031               break;
01032              case R_386_TLS_IE:
01033              case R_386_TLS_GOTIE:
01034               tls_type = GOT_TLS_IE_POS; break;
01035              }
01036 
01037            if (h != NULL)
01038              {
01039               h->got.refcount += 1;
01040               old_tls_type = elf_i386_hash_entry(h)->tls_type;
01041              }
01042            else
01043              {
01044               bfd_signed_vma *local_got_refcounts;
01045 
01046               /* This is a global offset table entry for a local symbol.  */
01047               local_got_refcounts = elf_local_got_refcounts (abfd);
01048               if (local_got_refcounts == NULL)
01049                 {
01050                   bfd_size_type size;
01051 
01052                   size = symtab_hdr->sh_info;
01053                   size *= (sizeof (bfd_signed_vma)
01054                           + sizeof (bfd_vma) + sizeof(char));
01055                   local_got_refcounts = bfd_zalloc (abfd, size);
01056                   if (local_got_refcounts == NULL)
01057                     return FALSE;
01058                   elf_local_got_refcounts (abfd) = local_got_refcounts;
01059                   elf_i386_local_tlsdesc_gotent (abfd)
01060                     = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
01061                   elf_i386_local_got_tls_type (abfd)
01062                     = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
01063                 }
01064               local_got_refcounts[r_symndx] += 1;
01065               old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
01066              }
01067 
01068            if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
01069              tls_type |= old_tls_type;
01070            /* If a TLS symbol is accessed using IE at least once,
01071               there is no point to use dynamic model for it.  */
01072            else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
01073                    && (! GOT_TLS_GD_ANY_P (old_tls_type)
01074                       || (tls_type & GOT_TLS_IE) == 0))
01075              {
01076               if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
01077                 tls_type = old_tls_type;
01078               else if (GOT_TLS_GD_ANY_P (old_tls_type)
01079                       && GOT_TLS_GD_ANY_P (tls_type))
01080                 tls_type |= old_tls_type;
01081               else
01082                 {
01083                   (*_bfd_error_handler)
01084                     (_("%B: `%s' accessed both as normal and "
01085                       "thread local symbol"),
01086                      abfd,
01087                      h ? h->root.root.string : "<local>");
01088                   return FALSE;
01089                 }
01090              }
01091 
01092            if (old_tls_type != tls_type)
01093              {
01094               if (h != NULL)
01095                 elf_i386_hash_entry (h)->tls_type = tls_type;
01096               else
01097                 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
01098              }
01099          }
01100          /* Fall through */
01101 
01102        case R_386_GOTOFF:
01103        case R_386_GOTPC:
01104        create_got:
01105          if (htab->sgot == NULL)
01106            {
01107              if (htab->elf.dynobj == NULL)
01108               htab->elf.dynobj = abfd;
01109              if (!create_got_section (htab->elf.dynobj, info))
01110               return FALSE;
01111            }
01112          if (r_type != R_386_TLS_IE)
01113            break;
01114          /* Fall through */
01115 
01116        case R_386_TLS_LE_32:
01117        case R_386_TLS_LE:
01118          if (!info->shared)
01119            break;
01120          info->flags |= DF_STATIC_TLS;
01121          /* Fall through */
01122 
01123        case R_386_32:
01124        case R_386_PC32:
01125          if (h != NULL && !info->shared)
01126            {
01127              /* If this reloc is in a read-only section, we might
01128                need a copy reloc.  We can't check reliably at this
01129                stage whether the section is read-only, as input
01130                sections have not yet been mapped to output sections.
01131                Tentatively set the flag for now, and correct in
01132                adjust_dynamic_symbol.  */
01133              h->non_got_ref = 1;
01134 
01135              /* We may need a .plt entry if the function this reloc
01136                refers to is in a shared lib.  */
01137              h->plt.refcount += 1;
01138              if (r_type != R_386_PC32)
01139               h->pointer_equality_needed = 1;
01140            }
01141 
01142          /* If we are creating a shared library, and this is a reloc
01143             against a global symbol, or a non PC relative reloc
01144             against a local symbol, then we need to copy the reloc
01145             into the shared library.  However, if we are linking with
01146             -Bsymbolic, we do not need to copy a reloc against a
01147             global symbol which is defined in an object we are
01148             including in the link (i.e., DEF_REGULAR is set).  At
01149             this point we have not seen all the input files, so it is
01150             possible that DEF_REGULAR is not set now but will be set
01151             later (it is never cleared).  In case of a weak definition,
01152             DEF_REGULAR may be cleared later by a strong definition in
01153             a shared library.  We account for that possibility below by
01154             storing information in the relocs_copied field of the hash
01155             table entry.  A similar situation occurs when creating
01156             shared libraries and symbol visibility changes render the
01157             symbol local.
01158 
01159             If on the other hand, we are creating an executable, we
01160             may need to keep relocations for symbols satisfied by a
01161             dynamic library if we manage to avoid copy relocs for the
01162             symbol.  */
01163          if ((info->shared
01164               && (sec->flags & SEC_ALLOC) != 0
01165               && (r_type != R_386_PC32
01166                  || (h != NULL
01167                      && (! SYMBOLIC_BIND (info, h)
01168                         || h->root.type == bfd_link_hash_defweak
01169                         || !h->def_regular))))
01170              || (ELIMINATE_COPY_RELOCS
01171                 && !info->shared
01172                 && (sec->flags & SEC_ALLOC) != 0
01173                 && h != NULL
01174                 && (h->root.type == bfd_link_hash_defweak
01175                     || !h->def_regular)))
01176            {
01177              struct elf_i386_dyn_relocs *p;
01178              struct elf_i386_dyn_relocs **head;
01179 
01180              /* We must copy these reloc types into the output file.
01181                Create a reloc section in dynobj and make room for
01182                this reloc.  */
01183              if (sreloc == NULL)
01184               {
01185                 const char *name;
01186                 bfd *dynobj;
01187                 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
01188                 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
01189 
01190                 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
01191                 if (name == NULL)
01192                   return FALSE;
01193 
01194                 if (! CONST_STRNEQ (name, ".rel")
01195                     || strcmp (bfd_get_section_name (abfd, sec),
01196                              name + 4) != 0)
01197                   {
01198                     (*_bfd_error_handler)
01199                      (_("%B: bad relocation section name `%s\'"),
01200                       abfd, name);
01201                   }
01202 
01203                 if (htab->elf.dynobj == NULL)
01204                   htab->elf.dynobj = abfd;
01205 
01206                 dynobj = htab->elf.dynobj;
01207                 sreloc = bfd_get_section_by_name (dynobj, name);
01208                 if (sreloc == NULL)
01209                   {
01210                     flagword flags;
01211 
01212                     flags = (SEC_HAS_CONTENTS | SEC_READONLY
01213                             | SEC_IN_MEMORY | SEC_LINKER_CREATED);
01214                     if ((sec->flags & SEC_ALLOC) != 0)
01215                      flags |= SEC_ALLOC | SEC_LOAD;
01216                     sreloc = bfd_make_section_with_flags (dynobj,
01217                                                      name,
01218                                                      flags);
01219                     if (sreloc == NULL
01220                        || ! bfd_set_section_alignment (dynobj, sreloc, 2))
01221                      return FALSE;
01222                   }
01223                 elf_section_data (sec)->sreloc = sreloc;
01224               }
01225 
01226              /* If this is a global symbol, we count the number of
01227                relocations we need for this symbol.  */
01228              if (h != NULL)
01229               {
01230                 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
01231               }
01232              else
01233               {
01234                 void **vpp;
01235                 /* Track dynamic relocs needed for local syms too.
01236                    We really need local syms available to do this
01237                    easily.  Oh well.  */
01238 
01239                 asection *s;
01240                 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
01241                                            sec, r_symndx);
01242                 if (s == NULL)
01243                   return FALSE;
01244 
01245                 vpp = &elf_section_data (s)->local_dynrel;
01246                 head = (struct elf_i386_dyn_relocs **)vpp;
01247               }
01248 
01249              p = *head;
01250              if (p == NULL || p->sec != sec)
01251               {
01252                 bfd_size_type amt = sizeof *p;
01253                 p = bfd_alloc (htab->elf.dynobj, amt);
01254                 if (p == NULL)
01255                   return FALSE;
01256                 p->next = *head;
01257                 *head = p;
01258                 p->sec = sec;
01259                 p->count = 0;
01260                 p->pc_count = 0;
01261               }
01262 
01263              p->count += 1;
01264              if (r_type == R_386_PC32)
01265               p->pc_count += 1;
01266            }
01267          break;
01268 
01269          /* This relocation describes the C++ object vtable hierarchy.
01270             Reconstruct it for later use during GC.  */
01271        case R_386_GNU_VTINHERIT:
01272          if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
01273            return FALSE;
01274          break;
01275 
01276          /* This relocation describes which C++ vtable entries are actually
01277             used.  Record for later use during GC.  */
01278        case R_386_GNU_VTENTRY:
01279          if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
01280            return FALSE;
01281          break;
01282 
01283        default:
01284          break;
01285        }
01286     }
01287 
01288   return TRUE;
01289 }
01290 
01291 /* Return the section that should be marked against GC for a given
01292    relocation.  */
01293 
01294 static asection *
01295 elf_i386_gc_mark_hook (asection *sec,
01296                      struct bfd_link_info *info,
01297                      Elf_Internal_Rela *rel,
01298                      struct elf_link_hash_entry *h,
01299                      Elf_Internal_Sym *sym)
01300 {
01301   if (h != NULL)
01302     switch (ELF32_R_TYPE (rel->r_info))
01303       {
01304       case R_386_GNU_VTINHERIT:
01305       case R_386_GNU_VTENTRY:
01306        return NULL;
01307       }
01308 
01309   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
01310 }
01311 
01312 /* Update the got entry reference counts for the section being removed.  */
01313 
01314 static bfd_boolean
01315 elf_i386_gc_sweep_hook (bfd *abfd,
01316                      struct bfd_link_info *info,
01317                      asection *sec,
01318                      const Elf_Internal_Rela *relocs)
01319 {
01320   Elf_Internal_Shdr *symtab_hdr;
01321   struct elf_link_hash_entry **sym_hashes;
01322   bfd_signed_vma *local_got_refcounts;
01323   const Elf_Internal_Rela *rel, *relend;
01324 
01325   elf_section_data (sec)->local_dynrel = NULL;
01326 
01327   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
01328   sym_hashes = elf_sym_hashes (abfd);
01329   local_got_refcounts = elf_local_got_refcounts (abfd);
01330 
01331   relend = relocs + sec->reloc_count;
01332   for (rel = relocs; rel < relend; rel++)
01333     {
01334       unsigned long r_symndx;
01335       unsigned int r_type;
01336       struct elf_link_hash_entry *h = NULL;
01337 
01338       r_symndx = ELF32_R_SYM (rel->r_info);
01339       if (r_symndx >= symtab_hdr->sh_info)
01340        {
01341          struct elf_i386_link_hash_entry *eh;
01342          struct elf_i386_dyn_relocs **pp;
01343          struct elf_i386_dyn_relocs *p;
01344 
01345          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
01346          while (h->root.type == bfd_link_hash_indirect
01347                || h->root.type == bfd_link_hash_warning)
01348            h = (struct elf_link_hash_entry *) h->root.u.i.link;
01349          eh = (struct elf_i386_link_hash_entry *) h;
01350 
01351          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
01352            if (p->sec == sec)
01353              {
01354               /* Everything must go for SEC.  */
01355               *pp = p->next;
01356               break;
01357              }
01358        }
01359 
01360       r_type = ELF32_R_TYPE (rel->r_info);
01361       r_type = elf_i386_tls_transition (info, r_type, h != NULL);
01362       switch (r_type)
01363        {
01364        case R_386_TLS_LDM:
01365          if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
01366            elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
01367          break;
01368 
01369        case R_386_TLS_GD:
01370        case R_386_TLS_GOTDESC:
01371        case R_386_TLS_DESC_CALL:
01372        case R_386_TLS_IE_32:
01373        case R_386_TLS_IE:
01374        case R_386_TLS_GOTIE:
01375        case R_386_GOT32:
01376          if (h != NULL)
01377            {
01378              if (h->got.refcount > 0)
01379               h->got.refcount -= 1;
01380            }
01381          else if (local_got_refcounts != NULL)
01382            {
01383              if (local_got_refcounts[r_symndx] > 0)
01384               local_got_refcounts[r_symndx] -= 1;
01385            }
01386          break;
01387 
01388        case R_386_32:
01389        case R_386_PC32:
01390          if (info->shared)
01391            break;
01392          /* Fall through */
01393 
01394        case R_386_PLT32:
01395          if (h != NULL)
01396            {
01397              if (h->plt.refcount > 0)
01398               h->plt.refcount -= 1;
01399            }
01400          break;
01401 
01402        default:
01403          break;
01404        }
01405     }
01406 
01407   return TRUE;
01408 }
01409 
01410 /* Adjust a symbol defined by a dynamic object and referenced by a
01411    regular object.  The current definition is in some section of the
01412    dynamic object, but we're not including those sections.  We have to
01413    change the definition to something the rest of the link can
01414    understand.  */
01415 
01416 static bfd_boolean
01417 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
01418                             struct elf_link_hash_entry *h)
01419 {
01420   struct elf_i386_link_hash_table *htab;
01421   asection *s;
01422   unsigned int power_of_two;
01423 
01424   /* If this is a function, put it in the procedure linkage table.  We
01425      will fill in the contents of the procedure linkage table later,
01426      when we know the address of the .got section.  */
01427   if (h->type == STT_FUNC
01428       || h->needs_plt)
01429     {
01430       if (h->plt.refcount <= 0
01431          || SYMBOL_CALLS_LOCAL (info, h)
01432          || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
01433              && h->root.type == bfd_link_hash_undefweak))
01434        {
01435          /* This case can occur if we saw a PLT32 reloc in an input
01436             file, but the symbol was never referred to by a dynamic
01437             object, or if all references were garbage collected.  In
01438             such a case, we don't actually need to build a procedure
01439             linkage table, and we can just do a PC32 reloc instead.  */
01440          h->plt.offset = (bfd_vma) -1;
01441          h->needs_plt = 0;
01442        }
01443 
01444       return TRUE;
01445     }
01446   else
01447     /* It's possible that we incorrectly decided a .plt reloc was
01448        needed for an R_386_PC32 reloc to a non-function sym in
01449        check_relocs.  We can't decide accurately between function and
01450        non-function syms in check-relocs;  Objects loaded later in
01451        the link may change h->type.  So fix it now.  */
01452     h->plt.offset = (bfd_vma) -1;
01453 
01454   /* If this is a weak symbol, and there is a real definition, the
01455      processor independent code will have arranged for us to see the
01456      real definition first, and we can just use the same value.  */
01457   if (h->u.weakdef != NULL)
01458     {
01459       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
01460                 || h->u.weakdef->root.type == bfd_link_hash_defweak);
01461       h->root.u.def.section = h->u.weakdef->root.u.def.section;
01462       h->root.u.def.value = h->u.weakdef->root.u.def.value;
01463       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
01464        h->non_got_ref = h->u.weakdef->non_got_ref;
01465       return TRUE;
01466     }
01467 
01468   /* This is a reference to a symbol defined by a dynamic object which
01469      is not a function.  */
01470 
01471   /* If we are creating a shared library, we must presume that the
01472      only references to the symbol are via the global offset table.
01473      For such cases we need not do anything here; the relocations will
01474      be handled correctly by relocate_section.  */
01475   if (info->shared)
01476     return TRUE;
01477 
01478   /* If there are no references to this symbol that do not use the
01479      GOT, we don't need to generate a copy reloc.  */
01480   if (!h->non_got_ref)
01481     return TRUE;
01482 
01483   /* If -z nocopyreloc was given, we won't generate them either.  */
01484   if (info->nocopyreloc)
01485     {
01486       h->non_got_ref = 0;
01487       return TRUE;
01488     }
01489 
01490   htab = elf_i386_hash_table (info);
01491 
01492   /* If there aren't any dynamic relocs in read-only sections, then
01493      we can keep the dynamic relocs and avoid the copy reloc.  This
01494      doesn't work on VxWorks, where we can not have dynamic relocations
01495      (other than copy and jump slot relocations) in an executable.  */
01496   if (ELIMINATE_COPY_RELOCS && !htab->is_vxworks)
01497     {
01498       struct elf_i386_link_hash_entry * eh;
01499       struct elf_i386_dyn_relocs *p;
01500 
01501       eh = (struct elf_i386_link_hash_entry *) h;
01502       for (p = eh->dyn_relocs; p != NULL; p = p->next)
01503        {
01504          s = p->sec->output_section;
01505          if (s != NULL && (s->flags & SEC_READONLY) != 0)
01506            break;
01507        }
01508 
01509       if (p == NULL)
01510        {
01511          h->non_got_ref = 0;
01512          return TRUE;
01513        }
01514     }
01515 
01516   if (h->size == 0)
01517     {
01518       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
01519                           h->root.root.string);
01520       return TRUE;
01521     }
01522 
01523   /* We must allocate the symbol in our .dynbss section, which will
01524      become part of the .bss section of the executable.  There will be
01525      an entry for this symbol in the .dynsym section.  The dynamic
01526      object will contain position independent code, so all references
01527      from the dynamic object to this symbol will go through the global
01528      offset table.  The dynamic linker will use the .dynsym entry to
01529      determine the address it must put in the global offset table, so
01530      both the dynamic object and the regular object will refer to the
01531      same memory location for the variable.  */
01532 
01533   /* We must generate a R_386_COPY reloc to tell the dynamic linker to
01534      copy the initial value out of the dynamic object and into the
01535      runtime process image.  */
01536   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
01537     {
01538       htab->srelbss->size += sizeof (Elf32_External_Rel);
01539       h->needs_copy = 1;
01540     }
01541 
01542   /* We need to figure out the alignment required for this symbol.  I
01543      have no idea how ELF linkers handle this.  */
01544   power_of_two = bfd_log2 (h->size);
01545   if (power_of_two > 3)
01546     power_of_two = 3;
01547 
01548   /* Apply the required alignment.  */
01549   s = htab->sdynbss;
01550   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
01551   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
01552     {
01553       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
01554        return FALSE;
01555     }
01556 
01557   /* Define the symbol as being at this point in the section.  */
01558   h->root.u.def.section = s;
01559   h->root.u.def.value = s->size;
01560 
01561   /* Increment the section size to make room for the symbol.  */
01562   s->size += h->size;
01563 
01564   return TRUE;
01565 }
01566 
01567 /* Allocate space in .plt, .got and associated reloc sections for
01568    dynamic relocs.  */
01569 
01570 static bfd_boolean
01571 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
01572 {
01573   struct bfd_link_info *info;
01574   struct elf_i386_link_hash_table *htab;
01575   struct elf_i386_link_hash_entry *eh;
01576   struct elf_i386_dyn_relocs *p;
01577 
01578   if (h->root.type == bfd_link_hash_indirect)
01579     return TRUE;
01580 
01581   if (h->root.type == bfd_link_hash_warning)
01582     /* When warning symbols are created, they **replace** the "real"
01583        entry in the hash table, thus we never get to see the real
01584        symbol in a hash traversal.  So look at it now.  */
01585     h = (struct elf_link_hash_entry *) h->root.u.i.link;
01586 
01587   info = (struct bfd_link_info *) inf;
01588   htab = elf_i386_hash_table (info);
01589 
01590   if (htab->elf.dynamic_sections_created
01591       && h->plt.refcount > 0)
01592     {
01593       /* Make sure this symbol is output as a dynamic symbol.
01594         Undefined weak syms won't yet be marked as dynamic.  */
01595       if (h->dynindx == -1
01596          && !h->forced_local)
01597        {
01598          if (! bfd_elf_link_record_dynamic_symbol (info, h))
01599            return FALSE;
01600        }
01601 
01602       if (info->shared
01603          || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
01604        {
01605          asection *s = htab->splt;
01606 
01607          /* If this is the first .plt entry, make room for the special
01608             first entry.  */
01609          if (s->size == 0)
01610            s->size += PLT_ENTRY_SIZE;
01611 
01612          h->plt.offset = s->size;
01613 
01614          /* If this symbol is not defined in a regular file, and we are
01615             not generating a shared library, then set the symbol to this
01616             location in the .plt.  This is required to make function
01617             pointers compare as equal between the normal executable and
01618             the shared library.  */
01619          if (! info->shared
01620              && !h->def_regular)
01621            {
01622              h->root.u.def.section = s;
01623              h->root.u.def.value = h->plt.offset;
01624            }
01625 
01626          /* Make room for this entry.  */
01627          s->size += PLT_ENTRY_SIZE;
01628 
01629          /* We also need to make an entry in the .got.plt section, which
01630             will be placed in the .got section by the linker script.  */
01631          htab->sgotplt->size += 4;
01632 
01633          /* We also need to make an entry in the .rel.plt section.  */
01634          htab->srelplt->size += sizeof (Elf32_External_Rel);
01635          htab->next_tls_desc_index++;
01636 
01637          if (htab->is_vxworks && !info->shared)
01638            {
01639              /* VxWorks has a second set of relocations for each PLT entry
01640                in executables.  They go in a separate relocation section,
01641                which is processed by the kernel loader.  */
01642 
01643              /* There are two relocations for the initial PLT entry: an
01644                R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
01645                R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
01646 
01647              if (h->plt.offset == PLT_ENTRY_SIZE)
01648               htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
01649 
01650              /* There are two extra relocations for each subsequent PLT entry:
01651                an R_386_32 relocation for the GOT entry, and an R_386_32
01652                relocation for the PLT entry.  */
01653 
01654              htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
01655            }
01656        }
01657       else
01658        {
01659          h->plt.offset = (bfd_vma) -1;
01660          h->needs_plt = 0;
01661        }
01662     }
01663   else
01664     {
01665       h->plt.offset = (bfd_vma) -1;
01666       h->needs_plt = 0;
01667     }
01668 
01669   eh = (struct elf_i386_link_hash_entry *) h;
01670   eh->tlsdesc_got = (bfd_vma) -1;
01671 
01672   /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
01673      make it a R_386_TLS_LE_32 requiring no TLS entry.  */
01674   if (h->got.refcount > 0
01675       && !info->shared
01676       && h->dynindx == -1
01677       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
01678     h->got.offset = (bfd_vma) -1;
01679   else if (h->got.refcount > 0)
01680     {
01681       asection *s;
01682       bfd_boolean dyn;
01683       int tls_type = elf_i386_hash_entry(h)->tls_type;
01684 
01685       /* Make sure this symbol is output as a dynamic symbol.
01686         Undefined weak syms won't yet be marked as dynamic.  */
01687       if (h->dynindx == -1
01688          && !h->forced_local)
01689        {
01690          if (! bfd_elf_link_record_dynamic_symbol (info, h))
01691            return FALSE;
01692        }
01693 
01694       s = htab->sgot;
01695       if (GOT_TLS_GDESC_P (tls_type))
01696        {
01697          eh->tlsdesc_got = htab->sgotplt->size
01698            - elf_i386_compute_jump_table_size (htab);
01699          htab->sgotplt->size += 8;
01700          h->got.offset = (bfd_vma) -2;
01701        }
01702       if (! GOT_TLS_GDESC_P (tls_type)
01703          || GOT_TLS_GD_P (tls_type))
01704        {
01705          h->got.offset = s->size;
01706          s->size += 4;
01707          /* R_386_TLS_GD needs 2 consecutive GOT slots.  */
01708          if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
01709            s->size += 4;
01710        }
01711       dyn = htab->elf.dynamic_sections_created;
01712       /* R_386_TLS_IE_32 needs one dynamic relocation,
01713         R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
01714         (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
01715         need two), R_386_TLS_GD needs one if local symbol and two if
01716         global.  */
01717       if (tls_type == GOT_TLS_IE_BOTH)
01718        htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
01719       else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
01720               || (tls_type & GOT_TLS_IE))
01721        htab->srelgot->size += sizeof (Elf32_External_Rel);
01722       else if (GOT_TLS_GD_P (tls_type))
01723        htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
01724       else if (! GOT_TLS_GDESC_P (tls_type)
01725               && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
01726                  || h->root.type != bfd_link_hash_undefweak)
01727               && (info->shared
01728                  || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
01729        htab->srelgot->size += sizeof (Elf32_External_Rel);
01730       if (GOT_TLS_GDESC_P (tls_type))
01731        htab->srelplt->size += sizeof (Elf32_External_Rel);
01732     }
01733   else
01734     h->got.offset = (bfd_vma) -1;
01735 
01736   if (eh->dyn_relocs == NULL)
01737     return TRUE;
01738 
01739   /* In the shared -Bsymbolic case, discard space allocated for
01740      dynamic pc-relative relocs against symbols which turn out to be
01741      defined in regular objects.  For the normal shared case, discard
01742      space for pc-relative relocs that have become local due to symbol
01743      visibility changes.  */
01744 
01745   if (info->shared)
01746     {
01747       /* The only reloc that uses pc_count is R_386_PC32, which will
01748         appear on a call or on something like ".long foo - .".  We
01749         want calls to protected symbols to resolve directly to the
01750         function rather than going via the plt.  If people want
01751         function pointer comparisons to work as expected then they
01752         should avoid writing assembly like ".long foo - .".  */
01753       if (SYMBOL_CALLS_LOCAL (info, h))
01754        {
01755          struct elf_i386_dyn_relocs **pp;
01756 
01757          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
01758            {
01759              p->count -= p->pc_count;
01760              p->pc_count = 0;
01761              if (p->count == 0)
01762               *pp = p->next;
01763              else
01764               pp = &p->next;
01765            }
01766        }
01767 
01768       /* Also discard relocs on undefined weak syms with non-default
01769         visibility.  */
01770       if (eh->dyn_relocs != NULL
01771          && h->root.type == bfd_link_hash_undefweak)
01772        {
01773          if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
01774            eh->dyn_relocs = NULL;
01775 
01776          /* Make sure undefined weak symbols are output as a dynamic
01777             symbol in PIEs.  */
01778          else if (h->dynindx == -1
01779                  && !h->forced_local)
01780            {
01781              if (! bfd_elf_link_record_dynamic_symbol (info, h))
01782               return FALSE;
01783            }
01784        }
01785     }
01786   else if (ELIMINATE_COPY_RELOCS)
01787     {
01788       /* For the non-shared case, discard space for relocs against
01789         symbols which turn out to need copy relocs or are not
01790         dynamic.  */
01791 
01792       if (!h->non_got_ref
01793          && ((h->def_dynamic
01794               && !h->def_regular)
01795              || (htab->elf.dynamic_sections_created
01796                 && (h->root.type == bfd_link_hash_undefweak
01797                     || h->root.type == bfd_link_hash_undefined))))
01798        {
01799          /* Make sure this symbol is output as a dynamic symbol.
01800             Undefined weak syms won't yet be marked as dynamic.  */
01801          if (h->dynindx == -1
01802              && !h->forced_local)
01803            {
01804              if (! bfd_elf_link_record_dynamic_symbol (info, h))
01805               return FALSE;
01806            }
01807 
01808          /* If that succeeded, we know we'll be keeping all the
01809             relocs.  */
01810          if (h->dynindx != -1)
01811            goto keep;
01812        }
01813 
01814       eh->dyn_relocs = NULL;
01815 
01816     keep: ;
01817     }
01818 
01819   /* Finally, allocate space.  */
01820   for (p = eh->dyn_relocs; p != NULL; p = p->next)
01821     {
01822       asection *sreloc = elf_section_data (p->sec)->sreloc;
01823       sreloc->size += p->count * sizeof (Elf32_External_Rel);
01824     }
01825 
01826   return TRUE;
01827 }
01828 
01829 /* Find any dynamic relocs that apply to read-only sections.  */
01830 
01831 static bfd_boolean
01832 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
01833 {
01834   struct elf_i386_link_hash_entry *eh;
01835   struct elf_i386_dyn_relocs *p;
01836 
01837   if (h->root.type == bfd_link_hash_warning)
01838     h = (struct elf_link_hash_entry *) h->root.u.i.link;
01839 
01840   eh = (struct elf_i386_link_hash_entry *) h;
01841   for (p = eh->dyn_relocs; p != NULL; p = p->next)
01842     {
01843       asection *s = p->sec->output_section;
01844 
01845       if (s != NULL && (s->flags & SEC_READONLY) != 0)
01846        {
01847          struct bfd_link_info *info = (struct bfd_link_info *) inf;
01848 
01849          info->flags |= DF_TEXTREL;
01850 
01851          /* Not an error, just cut short the traversal.  */
01852          return FALSE;
01853        }
01854     }
01855   return TRUE;
01856 }
01857 
01858 /* Set the sizes of the dynamic sections.  */
01859 
01860 static bfd_boolean
01861 elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
01862                             struct bfd_link_info *info)
01863 {
01864   struct elf_i386_link_hash_table *htab;
01865   bfd *dynobj;
01866   asection *s;
01867   bfd_boolean relocs;
01868   bfd *ibfd;
01869 
01870   htab = elf_i386_hash_table (info);
01871   dynobj = htab->elf.dynobj;
01872   if (dynobj == NULL)
01873     abort ();
01874 
01875   if (htab->elf.dynamic_sections_created)
01876     {
01877       /* Set the contents of the .interp section to the interpreter.  */
01878       if (info->executable)
01879        {
01880          s = bfd_get_section_by_name (dynobj, ".interp");
01881          if (s == NULL)
01882            abort ();
01883          s->size = sizeof ELF_DYNAMIC_INTERPRETER;
01884          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
01885        }
01886     }
01887 
01888   /* Set up .got offsets for local syms, and space for local dynamic
01889      relocs.  */
01890   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
01891     {
01892       bfd_signed_vma *local_got;
01893       bfd_signed_vma *end_local_got;
01894       char *local_tls_type;
01895       bfd_vma *local_tlsdesc_gotent;
01896       bfd_size_type locsymcount;
01897       Elf_Internal_Shdr *symtab_hdr;
01898       asection *srel;
01899 
01900       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
01901        continue;
01902 
01903       for (s = ibfd->sections; s != NULL; s = s->next)
01904        {
01905          struct elf_i386_dyn_relocs *p;
01906 
01907          for (p = ((struct elf_i386_dyn_relocs *)
01908                    elf_section_data (s)->local_dynrel);
01909               p != NULL;
01910               p = p->next)
01911            {
01912              if (!bfd_is_abs_section (p->sec)
01913                 && bfd_is_abs_section (p->sec->output_section))
01914               {
01915                 /* Input section has been discarded, either because
01916                    it is a copy of a linkonce section or due to
01917                    linker script /DISCARD/, so we'll be discarding
01918                    the relocs too.  */
01919               }
01920              else if (p->count != 0)
01921               {
01922                 srel = elf_section_data (p->sec)->sreloc;
01923                 srel->size += p->count * sizeof (Elf32_External_Rel);
01924                 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
01925                   info->flags |= DF_TEXTREL;
01926               }
01927            }
01928        }
01929 
01930       local_got = elf_local_got_refcounts (ibfd);
01931       if (!local_got)
01932        continue;
01933 
01934       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
01935       locsymcount = symtab_hdr->sh_info;
01936       end_local_got = local_got + locsymcount;
01937       local_tls_type = elf_i386_local_got_tls_type (ibfd);
01938       local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
01939       s = htab->sgot;
01940       srel = htab->srelgot;
01941       for (; local_got < end_local_got;
01942           ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
01943        {
01944          *local_tlsdesc_gotent = (bfd_vma) -1;
01945          if (*local_got > 0)
01946            {
01947              if (GOT_TLS_GDESC_P (*local_tls_type))
01948               {
01949                 *local_tlsdesc_gotent = htab->sgotplt->size
01950                   - elf_i386_compute_jump_table_size (htab);
01951                 htab->sgotplt->size += 8;
01952                 *local_got = (bfd_vma) -2;
01953               }
01954              if (! GOT_TLS_GDESC_P (*local_tls_type)
01955                 || GOT_TLS_GD_P (*local_tls_type))
01956               {
01957                 *local_got = s->size;
01958                 s->size += 4;
01959                 if (GOT_TLS_GD_P (*local_tls_type)
01960                     || *local_tls_type == GOT_TLS_IE_BOTH)
01961                   s->size += 4;
01962               }
01963              if (info->shared
01964                 || GOT_TLS_GD_ANY_P (*local_tls_type)
01965                 || (*local_tls_type & GOT_TLS_IE))
01966               {
01967                 if (*local_tls_type == GOT_TLS_IE_BOTH)
01968                   srel->size += 2 * sizeof (Elf32_External_Rel);
01969                 else if (GOT_TLS_GD_P (*local_tls_type)
01970                         || ! GOT_TLS_GDESC_P (*local_tls_type))
01971                   srel->size += sizeof (Elf32_External_Rel);
01972                 if (GOT_TLS_GDESC_P (*local_tls_type))
01973                   htab->srelplt->size += sizeof (Elf32_External_Rel);
01974               }
01975            }
01976          else
01977            *local_got = (bfd_vma) -1;
01978        }
01979     }
01980 
01981   if (htab->tls_ldm_got.refcount > 0)
01982     {
01983       /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
01984         relocs.  */
01985       htab->tls_ldm_got.offset = htab->sgot->size;
01986       htab->sgot->size += 8;
01987       htab->srelgot->size += sizeof (Elf32_External_Rel);
01988     }
01989   else
01990     htab->tls_ldm_got.offset = -1;
01991 
01992   /* Allocate global sym .plt and .got entries, and space for global
01993      sym dynamic relocs.  */
01994   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
01995 
01996   /* For every jump slot reserved in the sgotplt, reloc_count is
01997      incremented.  However, when we reserve space for TLS descriptors,
01998      it's not incremented, so in order to compute the space reserved
01999      for them, it suffices to multiply the reloc count by the jump
02000      slot size.  */
02001   if (htab->srelplt)
02002     htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
02003 
02004   /* We now have determined the sizes of the various dynamic sections.
02005      Allocate memory for them.  */
02006   relocs = FALSE;
02007   for (s = dynobj->sections; s != NULL; s = s->next)
02008     {
02009       bfd_boolean strip_section = TRUE;
02010 
02011       if ((s->flags & SEC_LINKER_CREATED) == 0)
02012        continue;
02013 
02014       if (s == htab->splt
02015          || s == htab->sgot
02016          || s == htab->sgotplt
02017          || s == htab->sdynbss)
02018        {
02019          /* Strip this section if we don't need it; see the
02020             comment below.  */
02021          /* We'd like to strip these sections if they aren't needed, but if
02022             we've exported dynamic symbols from them we must leave them.
02023             It's too late to tell BFD to get rid of the symbols.  */
02024 
02025          if (htab->elf.hplt != NULL)
02026            strip_section = FALSE;
02027        }
02028       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
02029        {
02030          if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
02031            relocs = TRUE;
02032 
02033          /* We use the reloc_count field as a counter if we need
02034             to copy relocs into the output file.  */
02035          s->reloc_count = 0;
02036        }
02037       else
02038        {
02039          /* It's not one of our sections, so don't allocate space.  */
02040          continue;
02041        }
02042 
02043       if (s->size == 0)
02044        {
02045          /* If we don't need this section, strip it from the
02046             output file.  This is mostly to handle .rel.bss and
02047             .rel.plt.  We must create both sections in
02048             create_dynamic_sections, because they must be created
02049             before the linker maps input sections to output
02050             sections.  The linker does that before
02051             adjust_dynamic_symbol is called, and it is that
02052             function which decides whether anything needs to go
02053             into these sections.  */
02054          if (strip_section)
02055            s->flags |= SEC_EXCLUDE;
02056          continue;
02057        }
02058 
02059       if ((s->flags & SEC_HAS_CONTENTS) == 0)
02060        continue;
02061 
02062       /* Allocate memory for the section contents.  We use bfd_zalloc
02063         here in case unused entries are not reclaimed before the
02064         section's contents are written out.  This should not happen,
02065         but this way if it does, we get a R_386_NONE reloc instead
02066         of garbage.  */
02067       s->contents = bfd_zalloc (dynobj, s->size);
02068       if (s->contents == NULL)
02069        return FALSE;
02070     }
02071 
02072   if (htab->elf.dynamic_sections_created)
02073     {
02074       /* Add some entries to the .dynamic section.  We fill in the
02075         values later, in elf_i386_finish_dynamic_sections, but we
02076         must add the entries now so that we get the correct size for
02077         the .dynamic section.  The DT_DEBUG entry is filled in by the
02078         dynamic linker and used by the debugger.  */
02079 #define add_dynamic_entry(TAG, VAL) \
02080   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
02081 
02082       if (info->executable)
02083        {
02084          if (!add_dynamic_entry (DT_DEBUG, 0))
02085            return FALSE;
02086        }
02087 
02088       if (htab->splt->size != 0)
02089        {
02090          if (!add_dynamic_entry (DT_PLTGOT, 0)
02091              || !add_dynamic_entry (DT_PLTRELSZ, 0)
02092              || !add_dynamic_entry (DT_PLTREL, DT_REL)
02093              || !add_dynamic_entry (DT_JMPREL, 0))
02094            return FALSE;
02095        }
02096 
02097       if (relocs)
02098        {
02099          if (!add_dynamic_entry (DT_REL, 0)
02100              || !add_dynamic_entry (DT_RELSZ, 0)
02101              || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
02102            return FALSE;
02103 
02104          /* If any dynamic relocs apply to a read-only section,
02105             then we need a DT_TEXTREL entry.  */
02106          if ((info->flags & DF_TEXTREL) == 0)
02107            elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
02108                                 (PTR) info);
02109 
02110          if ((info->flags & DF_TEXTREL) != 0)
02111            {
02112              if (!add_dynamic_entry (DT_TEXTREL, 0))
02113               return FALSE;
02114            }
02115        }
02116     }
02117 #undef add_dynamic_entry
02118 
02119   return TRUE;
02120 }
02121 
02122 static bfd_boolean
02123 elf_i386_always_size_sections (bfd *output_bfd,
02124                             struct bfd_link_info *info)
02125 {
02126   asection *tls_sec = elf_hash_table (info)->tls_sec;
02127 
02128   if (tls_sec)
02129     {
02130       struct elf_link_hash_entry *tlsbase;
02131 
02132       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
02133                                   "_TLS_MODULE_BASE_",
02134                                   FALSE, FALSE, FALSE);
02135 
02136       if (tlsbase && tlsbase->type == STT_TLS)
02137        {
02138          struct bfd_link_hash_entry *bh = NULL;
02139          const struct elf_backend_data *bed
02140            = get_elf_backend_data (output_bfd);
02141 
02142          if (!(_bfd_generic_link_add_one_symbol
02143               (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
02144                tls_sec, 0, NULL, FALSE,
02145                bed->collect, &bh)))
02146            return FALSE;
02147          tlsbase = (struct elf_link_hash_entry *)bh;
02148          tlsbase->def_regular = 1;
02149          tlsbase->other = STV_HIDDEN;
02150          (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
02151        }
02152     }
02153 
02154   return TRUE;
02155 }
02156 
02157 /* Set the correct type for an x86 ELF section.  We do this by the
02158    section name, which is a hack, but ought to work.  */
02159 
02160 static bfd_boolean
02161 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
02162                      Elf_Internal_Shdr *hdr,
02163                      asection *sec)
02164 {
02165   register const char *name;
02166 
02167   name = bfd_get_section_name (abfd, sec);
02168 
02169   /* This is an ugly, but unfortunately necessary hack that is
02170      needed when producing EFI binaries on x86. It tells
02171      elf.c:elf_fake_sections() not to consider ".reloc" as a section
02172      containing ELF relocation info.  We need this hack in order to
02173      be able to generate ELF binaries that can be translated into
02174      EFI applications (which are essentially COFF objects).  Those
02175      files contain a COFF ".reloc" section inside an ELFNN object,
02176      which would normally cause BFD to segfault because it would
02177      attempt to interpret this section as containing relocation
02178      entries for section "oc".  With this hack enabled, ".reloc"
02179      will be treated as a normal data section, which will avoid the
02180      segfault.  However, you won't be able to create an ELFNN binary
02181      with a section named "oc" that needs relocations, but that's
02182      the kind of ugly side-effects you get when detecting section
02183      types based on their names...  In practice, this limitation is
02184      unlikely to bite.  */
02185   if (strcmp (name, ".reloc") == 0)
02186     hdr->sh_type = SHT_PROGBITS;
02187 
02188   return TRUE;
02189 }
02190 
02191 /* Return the base VMA address which should be subtracted from real addresses
02192    when resolving @dtpoff relocation.
02193    This is PT_TLS segment p_vaddr.  */
02194 
02195 static bfd_vma
02196 dtpoff_base (struct bfd_link_info *info)
02197 {
02198   /* If tls_sec is NULL, we should have signalled an error already.  */
02199   if (elf_hash_table (info)->tls_sec == NULL)
02200     return 0;
02201   return elf_hash_table (info)->tls_sec->vma;
02202 }
02203 
02204 /* Return the relocation value for @tpoff relocation
02205    if STT_TLS virtual address is ADDRESS.  */
02206 
02207 static bfd_vma
02208 tpoff (struct bfd_link_info *info, bfd_vma address)
02209 {
02210   struct elf_link_hash_table *htab = elf_hash_table (info);
02211 
02212   /* If tls_sec is NULL, we should have signalled an error already.  */
02213   if (htab->tls_sec == NULL)
02214     return 0;
02215   return htab->tls_size + htab->tls_sec->vma - address;
02216 }
02217 
02218 /* Relocate an i386 ELF section.  */
02219 
02220 static bfd_boolean
02221 elf_i386_relocate_section (bfd *output_bfd,
02222                         struct bfd_link_info *info,
02223                         bfd *input_bfd,
02224                         asection *input_section,
02225                         bfd_byte *contents,
02226                         Elf_Internal_Rela *relocs,
02227                         Elf_Internal_Sym *local_syms,
02228                         asection **local_sections)
02229 {
02230   struct elf_i386_link_hash_table *htab;
02231   Elf_Internal_Shdr *symtab_hdr;
02232   struct elf_link_hash_entry **sym_hashes;
02233   bfd_vma *local_got_offsets;
02234   bfd_vma *local_tlsdesc_gotents;
02235   Elf_Internal_Rela *rel;
02236   Elf_Internal_Rela *relend;
02237 
02238   htab = elf_i386_hash_table (info);
02239   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
02240   sym_hashes = elf_sym_hashes (input_bfd);
02241   local_got_offsets = elf_local_got_offsets (input_bfd);
02242   local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
02243 
02244   rel = relocs;
02245   relend = relocs + input_section->reloc_count;
02246   for (; rel < relend; rel++)
02247     {
02248       unsigned int r_type;
02249       reloc_howto_type *howto;
02250       unsigned long r_symndx;
02251       struct elf_link_hash_entry *h;
02252       Elf_Internal_Sym *sym;
02253       asection *sec;
02254       bfd_vma off, offplt;
02255       bfd_vma relocation;
02256       bfd_boolean unresolved_reloc;
02257       bfd_reloc_status_type r;
02258       unsigned int indx;
02259       int tls_type;
02260 
02261       r_type = ELF32_R_TYPE (rel->r_info);
02262       if (r_type == R_386_GNU_VTINHERIT
02263          || r_type == R_386_GNU_VTENTRY)
02264        continue;
02265 
02266       if ((indx = r_type) >= R_386_standard
02267          && ((indx = r_type - R_386_ext_offset) - R_386_standard
02268              >= R_386_ext - R_386_standard)
02269          && ((indx = r_type - R_386_tls_offset) - R_386_ext
02270              >= R_386_tls - R_386_ext))
02271        {
02272          (*_bfd_error_handler)
02273            (_("%B: unrecognized relocation (0x%x) in section `%A'"),
02274             input_bfd, input_section, r_type);
02275          bfd_set_error (bfd_error_bad_value);
02276          return FALSE;
02277        }
02278       howto = elf_howto_table + indx;
02279 
02280       r_symndx = ELF32_R_SYM (rel->r_info);
02281       h = NULL;
02282       sym = NULL;
02283       sec = NULL;
02284       unresolved_reloc = FALSE;
02285       if (r_symndx < symtab_hdr->sh_info)
02286        {
02287          sym = local_syms + r_symndx;
02288          sec = local_sections[r_symndx];
02289          relocation = (sec->output_section->vma
02290                      + sec->output_offset
02291                      + sym->st_value);
02292 
02293          if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
02294              && ((sec->flags & SEC_MERGE) != 0
02295                 || (info->relocatable
02296                     && sec->output_offset != 0)))
02297            {
02298              bfd_vma addend;
02299              bfd_byte *where = contents + rel->r_offset;
02300 
02301              switch (howto->size)
02302               {
02303               case 0:
02304                 addend = bfd_get_8 (input_bfd, where);
02305                 if (howto->pc_relative)
02306                   {
02307                     addend = (addend ^ 0x80) - 0x80;
02308                     addend += 1;
02309                   }
02310                 break;
02311               case 1:
02312                 addend = bfd_get_16 (input_bfd, where);
02313                 if (howto->pc_relative)
02314                   {
02315                     addend = (addend ^ 0x8000) - 0x8000;
02316                     addend += 2;
02317                   }
02318                 break;
02319               case 2:
02320                 addend = bfd_get_32 (input_bfd, where);
02321                 if (howto->pc_relative)
02322                   {
02323                     addend = (addend ^ 0x80000000) - 0x80000000;
02324                     addend += 4;
02325                   }
02326                 break;
02327               default:
02328                 abort ();
02329               }
02330 
02331              if (info->relocatable)
02332               addend += sec->output_offset;
02333              else
02334               {
02335                 asection *msec = sec;
02336                 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
02337                                              addend);
02338                 addend -= relocation;
02339                 addend += msec->output_section->vma + msec->output_offset;
02340               }
02341 
02342              switch (howto->size)
02343               {
02344               case 0:
02345                 /* FIXME: overflow checks.  */
02346                 if (howto->pc_relative)
02347                   addend -= 1;
02348                 bfd_put_8 (input_bfd, addend, where);
02349                 break;
02350               case 1:
02351                 if (howto->pc_relative)
02352                   addend -= 2;
02353                 bfd_put_16 (input_bfd, addend, where);
02354                 break;
02355               case 2:
02356                 if (howto->pc_relative)
02357                   addend -= 4;
02358                 bfd_put_32 (input_bfd, addend, where);
02359                 break;
02360               }
02361            }
02362        }
02363       else
02364        {
02365          bfd_boolean warned;
02366 
02367          RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
02368                                r_symndx, symtab_hdr, sym_hashes,
02369                                h, sec, relocation,
02370                                unresolved_reloc, warned);
02371        }
02372 
02373       if (sec != NULL && elf_discarded_section (sec))
02374        {
02375          /* For relocs against symbols from removed linkonce sections,
02376             or sections discarded by a linker script, we just want the
02377             section contents zeroed.  Avoid any special processing.  */
02378          _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
02379          rel->r_info = 0;
02380          rel->r_addend = 0;
02381          continue;
02382        }
02383 
02384       if (info->relocatable)
02385        continue;
02386 
02387       switch (r_type)
02388        {
02389        case R_386_GOT32:
02390          /* Relocation is to the entry for this symbol in the global
02391             offset table.  */
02392          if (htab->sgot == NULL)
02393            abort ();
02394 
02395          if (h != NULL)
02396            {
02397              bfd_boolean dyn;
02398 
02399              off = h->got.offset;
02400              dyn = htab->elf.dynamic_sections_created;
02401              if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
02402                 || (info->shared
02403                     && SYMBOL_REFERENCES_LOCAL (info, h))
02404                 || (ELF_ST_VISIBILITY (h->other)
02405                     && h->root.type == bfd_link_hash_undefweak))
02406               {
02407                 /* This is actually a static link, or it is a
02408                    -Bsymbolic link and the symbol is defined
02409                    locally, or the symbol was forced to be local
02410                    because of a version file.  We must initialize
02411                    this entry in the global offset table.  Since the
02412                    offset must always be a multiple of 4, we use the
02413                    least significant bit to record whether we have
02414                    initialized it already.
02415 
02416                    When doing a dynamic link, we create a .rel.got
02417                    relocation entry to initialize the value.  This
02418                    is done in the finish_dynamic_symbol routine.  */
02419                 if ((off & 1) != 0)
02420                   off &= ~1;
02421                 else
02422                   {
02423                     bfd_put_32 (output_bfd, relocation,
02424                               htab->sgot->contents + off);
02425                     h->got.offset |= 1;
02426                   }
02427               }
02428              else
02429               unresolved_reloc = FALSE;
02430            }
02431          else
02432            {
02433              if (local_got_offsets == NULL)
02434               abort ();
02435 
02436              off = local_got_offsets[r_symndx];
02437 
02438              /* The offset must always be a multiple of 4.  We use
02439                the least significant bit to record whether we have
02440                already generated the necessary reloc.  */
02441              if ((off & 1) != 0)
02442               off &= ~1;
02443              else
02444               {
02445                 bfd_put_32 (output_bfd, relocation,
02446                            htab->sgot->contents + off);
02447 
02448                 if (info->shared)
02449                   {
02450                     asection *s;
02451                     Elf_Internal_Rela outrel;
02452                     bfd_byte *loc;
02453 
02454                     s = htab->srelgot;
02455                     if (s == NULL)
02456                      abort ();
02457 
02458                     outrel.r_offset = (htab->sgot->output_section->vma
02459                                     + htab->sgot->output_offset
02460                                     + off);
02461                     outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
02462                     loc = s->contents;
02463                     loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
02464                     bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
02465                   }
02466 
02467                 local_got_offsets[r_symndx] |= 1;
02468               }
02469            }
02470 
02471          if (off >= (bfd_vma) -2)
02472            abort ();
02473 
02474          relocation = htab->sgot->output_section->vma
02475                      + htab->sgot->output_offset + off
02476                      - htab->sgotplt->output_section->vma
02477                      - htab->sgotplt->output_offset;
02478          break;
02479 
02480        case R_386_GOTOFF:
02481          /* Relocation is relative to the start of the global offset
02482             table.  */
02483 
02484          /* Check to make sure it isn't a protected function symbol
02485             for shared library since it may not be local when used
02486             as function address.  */
02487          if (info->shared
02488              && !info->executable
02489              && h
02490              && h->def_regular
02491              && h->type == STT_FUNC
02492              && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
02493            {
02494              (*_bfd_error_handler)
02495               (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
02496                input_bfd, h->root.root.string);
02497              bfd_set_error (bfd_error_bad_value);
02498              return FALSE;
02499            }
02500 
02501          /* Note that sgot is not involved in this
02502             calculation.  We always want the start of .got.plt.  If we
02503             defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
02504             permitted by the ABI, we might have to change this
02505             calculation.  */
02506          relocation -= htab->sgotplt->output_section->vma
02507                      + htab->sgotplt->output_offset;
02508          break;
02509 
02510        case R_386_GOTPC:
02511          /* Use global offset table as symbol value.  */
02512          relocation = htab->sgotplt->output_section->vma
02513                      + htab->sgotplt->output_offset;
02514          unresolved_reloc = FALSE;
02515          break;
02516 
02517        case R_386_PLT32:
02518          /* Relocation is to the entry for this symbol in the
02519             procedure linkage table.  */
02520 
02521          /* Resolve a PLT32 reloc against a local symbol directly,
02522             without using the procedure linkage table.  */
02523          if (h == NULL)
02524            break;
02525 
02526          if (h->plt.offset == (bfd_vma) -1
02527              || htab->splt == NULL)
02528            {
02529              /* We didn't make a PLT entry for this symbol.  This
02530                happens when statically linking PIC code, or when
02531                using -Bsymbolic.  */
02532              break;
02533            }
02534 
02535          relocation = (htab->splt->output_section->vma
02536                      + htab->splt->output_offset
02537                      + h->plt.offset);
02538          unresolved_reloc = FALSE;
02539          break;
02540 
02541        case R_386_32:
02542        case R_386_PC32:
02543          if ((input_section->flags & SEC_ALLOC) == 0)
02544            break;
02545 
02546          if ((info->shared
02547               && (h == NULL
02548                  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
02549                  || h->root.type != bfd_link_hash_undefweak)
02550               && (r_type != R_386_PC32
02551                  || !SYMBOL_CALLS_LOCAL (info, h)))
02552              || (ELIMINATE_COPY_RELOCS
02553                 && !info->shared
02554                 && h != NULL
02555                 && h->dynindx != -1
02556                 && !h->non_got_ref
02557                 && ((h->def_dynamic
02558                      && !h->def_regular)
02559                     || h->root.type == bfd_link_hash_undefweak
02560                     || h->root.type == bfd_link_hash_undefined)))
02561            {
02562              Elf_Internal_Rela outrel;
02563              bfd_byte *loc;
02564              bfd_boolean skip, relocate;
02565              asection *sreloc;
02566 
02567              /* When generating a shared object, these relocations
02568                are copied into the output file to be resolved at run
02569                time.  */
02570 
02571              skip = FALSE;
02572              relocate = FALSE;
02573 
02574              outrel.r_offset =
02575               _bfd_elf_section_offset (output_bfd, info, input_section,
02576                                     rel->r_offset);
02577              if (outrel.r_offset == (bfd_vma) -1)
02578               skip = TRUE;
02579              else if (outrel.r_offset == (bfd_vma) -2)
02580               skip = TRUE, relocate = TRUE;
02581              outrel.r_offset += (input_section->output_section->vma
02582                               + input_section->output_offset);
02583 
02584              if (skip)
02585               memset (&outrel, 0, sizeof outrel);
02586              else if (h != NULL
02587                      && h->dynindx != -1
02588                      && (r_type == R_386_PC32
02589                         || !info->shared
02590                         || !SYMBOLIC_BIND (info, h)
02591                         || !h->def_regular))
02592               outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
02593              else
02594               {
02595                 /* This symbol is local, or marked to become local.  */
02596                 relocate = TRUE;
02597                 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
02598               }
02599 
02600              sreloc = elf_section_data (input_section)->sreloc;
02601              if (sreloc == NULL)
02602               abort ();
02603 
02604              loc = sreloc->contents;
02605              loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
02606              bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
02607 
02608              /* If this reloc is against an external symbol, we do
02609                not want to fiddle with the addend.  Otherwise, we
02610                need to include the symbol value so that it becomes
02611                an addend for the dynamic reloc.  */
02612              if (! relocate)
02613               continue;
02614            }
02615          break;
02616 
02617        case R_386_TLS_IE:
02618          if (info->shared)
02619            {
02620              Elf_Internal_Rela outrel;
02621              bfd_byte *loc;
02622              asection *sreloc;
02623 
02624              outrel.r_offset = rel->r_offset
02625                             + input_section->output_section->vma
02626                             + input_section->output_offset;
02627              outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
02628              sreloc = elf_section_data (input_section)->sreloc;
02629              if (sreloc == NULL)
02630               abort ();
02631              loc = sreloc->contents;
02632              loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
02633              bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
02634            }
02635          /* Fall through */
02636 
02637        case R_386_TLS_GD:
02638        case R_386_TLS_GOTDESC:
02639        case R_386_TLS_DESC_CALL:
02640        case R_386_TLS_IE_32:
02641        case R_386_TLS_GOTIE:
02642          r_type = elf_i386_tls_transition (info, r_type, h == NULL);
02643          tls_type = GOT_UNKNOWN;
02644          if (h == NULL && local_got_offsets)
02645            tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
02646          else if (h != NULL)
02647            {
02648              tls_type = elf_i386_hash_entry(h)->tls_type;
02649              if (!info->shared && h->dynindx == -1 && (tls_type & GOT_TLS_IE))
02650               r_type = R_386_TLS_LE_32;
02651            }
02652          if (tls_type == GOT_TLS_IE)
02653            tls_type = GOT_TLS_IE_NEG;
02654          if (r_type == R_386_TLS_GD
02655              || r_type == R_386_TLS_GOTDESC
02656              || r_type == R_386_TLS_DESC_CALL)
02657            {
02658              if (tls_type == GOT_TLS_IE_POS)
02659               r_type = R_386_TLS_GOTIE;
02660              else if (tls_type & GOT_TLS_IE)
02661               r_type = R_386_TLS_IE_32;
02662            }
02663 
02664          if (r_type == R_386_TLS_LE_32)
02665            {
02666              BFD_ASSERT (! unresolved_reloc);
02667              if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
02668               {
02669                 unsigned int val, type;
02670                 bfd_vma roff;
02671 
02672                 /* GD->LE transition.  */
02673                 BFD_ASSERT (rel->r_offset >= 2);
02674                 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
02675                 BFD_ASSERT (type == 0x8d || type == 0x04);
02676                 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
02677                 BFD_ASSERT (bfd_get_8 (input_bfd,
02678                                     contents + rel->r_offset + 4)
02679                            == 0xe8);
02680                 BFD_ASSERT (rel + 1 < relend);
02681                 BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
02682                 roff = rel->r_offset + 5;
02683                 val = bfd_get_8 (input_bfd,
02684                                contents + rel->r_offset - 1);
02685                 if (type == 0x04)
02686                   {
02687                     /* leal foo(,%reg,1), %eax; call ___tls_get_addr
02688                       Change it into:
02689                       movl %gs:0, %eax; subl $foo@tpoff, %eax
02690                       (6 byte form of subl).  */
02691                     BFD_ASSERT (rel->r_offset >= 3);
02692                     BFD_ASSERT (bfd_get_8 (input_bfd,
02693                                         contents + rel->r_offset - 3)
02694                               == 0x8d);
02695                     BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
02696                     memcpy (contents + rel->r_offset - 3,
02697                            "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
02698                   }
02699                 else
02700                   {
02701                     BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
02702                     if (rel->r_offset + 10 <= input_section->size
02703                        && bfd_get_8 (input_bfd,
02704                                    contents + rel->r_offset + 9) == 0x90)
02705                      {
02706                        /* leal foo(%reg), %eax; call ___tls_get_addr; nop
02707                           Change it into:
02708                           movl %gs:0, %eax; subl $foo@tpoff, %eax
02709                           (6 byte form of subl).  */
02710                        memcpy (contents + rel->r_offset - 2,
02711                               "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
02712                        roff = rel->r_offset + 6;
02713                      }
02714                     else
02715                      {
02716                        /* leal foo(%reg), %eax; call ___tls_get_addr
02717                           Change it into:
02718                           movl %gs:0, %eax; subl $foo@tpoff, %eax
02719                           (5 byte form of subl).  */
02720                        memcpy (contents + rel->r_offset - 2,
02721                               "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
02722                      }
02723                   }
02724                 bfd_put_32 (output_bfd, tpoff (info, relocation),
02725                            contents + roff);
02726                 /* Skip R_386_PLT32.  */
02727                 rel++;
02728                 continue;
02729               }
02730              else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
02731               {
02732                 /* GDesc -> LE transition.
02733                    It's originally something like:
02734                    leal x@tlsdesc(%ebx), %eax
02735 
02736                    leal x@ntpoff, %eax
02737 
02738                    Registers other than %eax may be set up here.  */
02739 
02740                 unsigned int val, type;
02741                 bfd_vma roff;
02742 
02743                 /* First, make sure it's a leal adding ebx to a
02744                    32-bit offset into any register, although it's
02745                    probably almost always going to be eax.  */
02746                 roff = rel->r_offset;
02747                 BFD_ASSERT (roff >= 2);
02748                 type = bfd_get_8 (input_bfd, contents + roff - 2);
02749                 BFD_ASSERT (type == 0x8d);
02750                 val = bfd_get_8 (input_bfd, contents + roff - 1);
02751                 BFD_ASSERT ((val & 0xc7) == 0x83);
02752                 BFD_ASSERT (roff + 4 <= input_section->size);
02753 
02754                 /* Now modify the instruction as appropriate.  */
02755                 /* aoliva FIXME: remove the above and xor the byte
02756                    below with 0x86.  */
02757                 bfd_put_8 (output_bfd, val ^ 0x86,
02758                           contents + roff - 1);
02759                 bfd_put_32 (output_bfd, -tpoff (info, relocation),
02760                            contents + roff);
02761                 continue;
02762               }
02763              else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
02764               {
02765                 /* GDesc -> LE transition.
02766                    It's originally:
02767                    call *(%eax)
02768                    Turn it into:
02769                    nop; nop  */
02770 
02771                 unsigned int val, type;
02772                 bfd_vma roff;
02773 
02774                 /* First, make sure it's a call *(%eax).  */
02775                 roff = rel->r_offset;
02776                 BFD_ASSERT (roff + 2 <= input_section->size);
02777                 type = bfd_get_8 (input_bfd, contents + roff);
02778                 BFD_ASSERT (type == 0xff);
02779                 val = bfd_get_8 (input_bfd, contents + roff + 1);
02780                 BFD_ASSERT (val == 0x10);
02781 
02782                 /* Now modify the instruction as appropriate.  Use
02783                    xchg %ax,%ax instead of 2 nops.  */
02784                 bfd_put_8 (output_bfd, 0x66, contents + roff);
02785                 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
02786                 continue;
02787               }
02788              else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
02789               {
02790                 unsigned int val, type;
02791 
02792                 /* IE->LE transition:
02793                    Originally it can be one of:
02794                    movl foo, %eax
02795                    movl foo, %reg
02796                    addl foo, %reg
02797                    We change it into:
02798                    movl $foo, %eax
02799                    movl $foo, %reg
02800                    addl $foo, %reg.  */
02801                 BFD_ASSERT (rel->r_offset >= 1);
02802                 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
02803                 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
02804                 if (val == 0xa1)
02805                   {
02806                     /* movl foo, %eax.  */
02807                     bfd_put_8 (output_bfd, 0xb8,
02808                              contents + rel->r_offset - 1);
02809                   }
02810                 else
02811                   {
02812                     BFD_ASSERT (rel->r_offset >= 2);
02813                     type = bfd_get_8 (input_bfd,
02814                                    contents + rel->r_offset - 2);
02815                     switch (type)
02816                      {
02817                      case 0x8b:
02818                        /* movl */
02819                        BFD_ASSERT ((val & 0xc7) == 0x05);
02820                        bfd_put_8 (output_bfd, 0xc7,
02821                                  contents + rel->r_offset - 2);
02822                        bfd_put_8 (output_bfd,
02823                                  0xc0 | ((val >> 3) & 7),
02824                                  contents + rel->r_offset - 1);
02825                        break;
02826                      case 0x03:
02827                        /* addl */
02828                        BFD_ASSERT ((val & 0xc7) == 0x05);
02829                        bfd_put_8 (output_bfd, 0x81,
02830                                  contents + rel->r_offset - 2);
02831                        bfd_put_8 (output_bfd,
02832                                  0xc0 | ((val >> 3) & 7),
02833                                  contents + rel->r_offset - 1);
02834                        break;
02835                      default:
02836                        BFD_FAIL ();
02837                        break;
02838                      }
02839                   }
02840                 bfd_put_32 (output_bfd, -tpoff (info, relocation),
02841                            contents + rel->r_offset);
02842                 continue;
02843               }
02844              else
02845               {
02846                 unsigned int val, type;
02847 
02848                 /* {IE_32,GOTIE}->LE transition:
02849                    Originally it can be one of:
02850                    subl foo(%reg1), %reg2
02851                    movl foo(%reg1), %reg2
02852                    addl foo(%reg1), %reg2
02853                    We change it into:
02854                    subl $foo, %reg2
02855                    movl $foo, %reg2 (6 byte form)
02856                    addl $foo, %reg2.  */
02857                 BFD_ASSERT (rel->r_offset >= 2);
02858                 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
02859                 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
02860                 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
02861                 BFD_ASSERT ((val & 0xc0) == 0x80 && (val & 7) != 4);
02862                 if (type == 0x8b)
02863                   {
02864                     /* movl */
02865                     bfd_put_8 (output_bfd, 0xc7,
02866                              contents + rel->r_offset - 2);
02867                     bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
02868                              contents + rel->r_offset - 1);
02869                   }
02870                 else if (type == 0x2b)
02871                   {
02872                     /* subl */
02873                     bfd_put_8 (output_bfd, 0x81,
02874                              contents + rel->r_offset - 2);
02875                     bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
02876                              contents + rel->r_offset - 1);
02877                   }
02878                 else if (type == 0x03)
02879                   {
02880                     /* addl */
02881                     bfd_put_8 (output_bfd, 0x81,
02882                              contents + rel->r_offset - 2);
02883                     bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
02884                              contents + rel->r_offset - 1);
02885                   }
02886                 else
02887                   BFD_FAIL ();
02888                 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
02889                   bfd_put_32 (output_bfd, -tpoff (info, relocation),
02890                             contents + rel->r_offset);
02891                 else
02892                   bfd_put_32 (output_bfd, tpoff (info, relocation),
02893                             contents + rel->r_offset);
02894                 continue;
02895               }
02896            }
02897 
02898          if (htab->sgot == NULL)
02899            abort ();
02900 
02901          if (h != NULL)
02902            {
02903              off = h->got.offset;
02904              offplt = elf_i386_hash_entry (h)->tlsdesc_got;
02905            }
02906          else
02907            {
02908              if (local_got_offsets == NULL)
02909               abort ();
02910 
02911              off = local_got_offsets[r_symndx];
02912              offplt = local_tlsdesc_gotents[r_symndx];
02913            }
02914 
02915          if ((off & 1) != 0)
02916            off &= ~1;
02917          else
02918            {
02919              Elf_Internal_Rela outrel;
02920              bfd_byte *loc;
02921              int dr_type, indx;
02922              asection *sreloc;
02923 
02924              if (htab->srelgot == NULL)
02925               abort ();
02926 
02927              indx = h && h->dynindx != -1 ? h->dynindx : 0;
02928 
02929              if (GOT_TLS_GDESC_P (tls_type))
02930               {
02931                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
02932                 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
02933                            <= htab->sgotplt->size);
02934                 outrel.r_offset = (htab->sgotplt->output_section->vma
02935                                  + htab->sgotplt->output_offset
02936                                  + offplt
02937                                  + htab->sgotplt_jump_table_size);
02938                 sreloc = htab->srelplt;
02939                 loc = sreloc->contents;
02940                 loc += (htab->next_tls_desc_index++
02941                        * sizeof (Elf32_External_Rel));
02942                 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
02943                            <= sreloc->contents + sreloc->size);
02944                 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
02945                 if (indx == 0)
02946                   {
02947                     BFD_ASSERT (! unresolved_reloc);
02948                     bfd_put_32 (output_bfd,
02949                               relocation - dtpoff_base (info),
02950                               htab->sgotplt->contents + offplt
02951                               + htab->sgotplt_jump_table_size + 4);
02952                   }
02953                 else
02954                   {
02955                     bfd_put_32 (output_bfd, 0,
02956                               htab->sgotplt->contents + offplt
02957                               + htab->sgotplt_jump_table_size + 4);
02958                   }
02959               }
02960 
02961              sreloc = htab->srelgot;
02962 
02963              outrel.r_offset = (htab->sgot->output_section->vma
02964                              + htab->sgot->output_offset + off);
02965 
02966              if (GOT_TLS_GD_P (tls_type))
02967               dr_type = R_386_TLS_DTPMOD32;
02968              else if (GOT_TLS_GDESC_P (tls_type))
02969               goto dr_done;
02970              else if (tls_type == GOT_TLS_IE_POS)
02971               dr_type = R_386_TLS_TPOFF;
02972              else
02973               dr_type = R_386_TLS_TPOFF32;
02974 
02975              if (dr_type == R_386_TLS_TPOFF && indx == 0)
02976               bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
02977                          htab->sgot->contents + off);
02978              else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
02979               bfd_put_32 (output_bfd, dtpoff_base (info) - relocation,
02980                          htab->sgot->contents + off);
02981              else if (dr_type != R_386_TLS_DESC)
02982               bfd_put_32 (output_bfd, 0,
02983                          htab->sgot->contents + off);
02984              outrel.r_info = ELF32_R_INFO (indx, dr_type);
02985 
02986              loc = sreloc->contents;
02987              loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
02988              BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
02989                        <= sreloc->contents + sreloc->size);
02990              bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
02991 
02992              if (GOT_TLS_GD_P (tls_type))
02993               {
02994                 if (indx == 0)
02995                   {
02996                     BFD_ASSERT (! unresolved_reloc);
02997                     bfd_put_32 (output_bfd,
02998                               relocation - dtpoff_base (info),
02999                               htab->sgot->contents + off + 4);
03000                   }
03001                 else
03002                   {
03003                     bfd_put_32 (output_bfd, 0,
03004                               htab->sgot->contents + off + 4);
03005                     outrel.r_info = ELF32_R_INFO (indx,
03006                                               R_386_TLS_DTPOFF32);
03007                     outrel.r_offset += 4;
03008                     sreloc->reloc_count++;
03009                     loc += sizeof (Elf32_External_Rel);
03010                     BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
03011                               <= sreloc->contents + sreloc->size);
03012                     bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
03013                   }
03014               }
03015              else if (tls_type == GOT_TLS_IE_BOTH)
03016               {
03017                 bfd_put_32 (output_bfd,
03018                            indx == 0 ? relocation - dtpoff_base (info) : 0,
03019                            htab->sgot->contents + off + 4);
03020                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
03021                 outrel.r_offset += 4;
03022                 sreloc->reloc_count++;
03023                 loc += sizeof (Elf32_External_Rel);
03024                 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
03025               }
03026 
03027            dr_done:
03028              if (h != NULL)
03029               h->got.offset |= 1;
03030              else
03031               local_got_offsets[r_symndx] |= 1;
03032            }
03033 
03034          if (off >= (bfd_vma) -2
03035              && ! GOT_TLS_GDESC_P (tls_type))
03036            abort ();
03037          if (r_type == R_386_TLS_GOTDESC
03038              || r_type == R_386_TLS_DESC_CALL)
03039            {
03040              relocation = htab->sgotplt_jump_table_size + offplt;
03041              unresolved_reloc = FALSE;
03042            }
03043          else if (r_type == ELF32_R_TYPE (rel->r_info))
03044            {
03045              bfd_vma g_o_t = htab->sgotplt->output_section->vma
03046                            + htab->sgotplt->output_offset;
03047              relocation = htab->sgot->output_section->vma
03048               + htab->sgot->output_offset + off - g_o_t;
03049              if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
03050                 && tls_type == GOT_TLS_IE_BOTH)
03051               relocation += 4;
03052              if (r_type == R_386_TLS_IE)
03053               relocation += g_o_t;
03054              unresolved_reloc = FALSE;
03055            }
03056          else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
03057            {
03058              unsigned int val, type;
03059              bfd_vma roff;
03060 
03061              /* GD->IE transition.  */
03062              BFD_ASSERT (rel->r_offset >= 2);
03063              type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
03064              BFD_ASSERT (type == 0x8d || type == 0x04);
03065              BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
03066              BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
03067                        == 0xe8);
03068              BFD_ASSERT (rel + 1 < relend);
03069              BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
03070              roff = rel->r_offset - 3;
03071              val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
03072              if (type == 0x04)
03073               {
03074                 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
03075                    Change it into:
03076                    movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax.  */
03077                 BFD_ASSERT (rel->r_offset >= 3);
03078                 BFD_ASSERT (bfd_get_8 (input_bfd,
03079                                     contents + rel->r_offset - 3)
03080                            == 0x8d);
03081                 BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
03082                 val >>= 3;
03083               }
03084              else
03085               {
03086                 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
03087                    Change it into:
03088                    movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax.  */
03089                 BFD_ASSERT (rel->r_offset + 10 <= input_section->size);
03090                 BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
03091                 BFD_ASSERT (bfd_get_8 (input_bfd,
03092                                     contents + rel->r_offset + 9)
03093                            == 0x90);
03094                 roff = rel->r_offset - 2;
03095               }
03096              memcpy (contents + roff,
03097                     "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
03098              contents[roff + 7] = 0x80 | (val & 7);
03099              /* If foo is used only with foo@gotntpoff(%reg) and
03100                foo@indntpoff, but not with foo@gottpoff(%reg), change
03101                subl $foo@gottpoff(%reg), %eax
03102                into:
03103                addl $foo@gotntpoff(%reg), %eax.  */
03104              if (tls_type == GOT_TLS_IE_POS)
03105               contents[roff + 6] = 0x03;
03106              bfd_put_32 (output_bfd,
03107                        htab->sgot->output_section->vma
03108                        + htab->sgot->output_offset + off
03109                        - htab->sgotplt->output_section->vma
03110                        - htab->sgotplt->output_offset,
03111                        contents + roff + 8);
03112              /* Skip R_386_PLT32.  */
03113              rel++;
03114              continue;
03115            }
03116          else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
03117            {
03118              /* GDesc -> IE transition.
03119                It's originally something like:
03120                leal x@tlsdesc(%ebx), %eax
03121 
03122                Change it to:
03123                movl x@gotntpoff(%ebx), %eax # before nop; nop
03124                or:
03125                movl x@gottpoff(%ebx), %eax # before negl %eax
03126 
03127                Registers other than %eax may be set up here.  */
03128 
03129              unsigned int val, type;
03130              bfd_vma roff;
03131 
03132              /* First, make sure it's a leal adding ebx to a 32-bit
03133                offset into any register, although it's probably
03134                almost always going to be eax.  */
03135              roff = rel->r_offset;
03136              BFD_ASSERT (roff >= 2);
03137              type = bfd_get_8 (input_bfd, contents + roff - 2);
03138              BFD_ASSERT (type == 0x8d);
03139              val = bfd_get_8 (input_bfd, contents + roff - 1);
03140              BFD_ASSERT ((val & 0xc7) == 0x83);
03141              BFD_ASSERT (roff + 4 <= input_section->size);
03142 
03143              /* Now modify the instruction as appropriate.  */
03144              /* To turn a leal into a movl in the form we use it, it
03145                suffices to change the first byte from 0x8d to 0x8b.
03146                aoliva FIXME: should we decide to keep the leal, all
03147                we have to do is remove the statement below, and
03148                adjust the relaxation of R_386_TLS_DESC_CALL.  */
03149              bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
03150 
03151              if (tls_type == GOT_TLS_IE_BOTH)
03152               off += 4;
03153 
03154              bfd_put_32 (output_bfd,
03155                        htab->sgot->output_section->vma
03156                        + htab->sgot->output_offset + off
03157                        - htab->sgotplt->output_section->vma
03158                        - htab->sgotplt->output_offset,
03159                        contents + roff);
03160              continue;
03161            }
03162          else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
03163            {
03164              /* GDesc -> IE transition.
03165                It's originally:
03166                call *(%eax)
03167 
03168                Change it to:
03169                nop; nop
03170                or
03171                negl %eax
03172                depending on how we transformed the TLS_GOTDESC above.
03173              */
03174 
03175              unsigned int val, type;
03176              bfd_vma roff;
03177 
03178              /* First, make sure it's a call *(%eax).  */
03179              roff = rel->r_offset;
03180              BFD_ASSERT (roff + 2 <= input_section->size);
03181              type = bfd_get_8 (input_bfd, contents + roff);
03182              BFD_ASSERT (type == 0xff);
03183              val = bfd_get_8 (input_bfd, contents + roff + 1);
03184              BFD_ASSERT (val == 0x10);
03185 
03186              /* Now modify the instruction as appropriate.  */
03187              if (tls_type != GOT_TLS_IE_NEG)
03188               {
03189                 /* xchg %ax,%ax */
03190                 bfd_put_8 (output_bfd, 0x66, contents + roff);
03191                 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
03192               }
03193              else
03194               {
03195                 /* negl %eax */
03196                 bfd_put_8 (output_bfd, 0xf7, contents + roff);
03197                 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
03198               }
03199 
03200              continue;
03201            }
03202          else
03203            BFD_ASSERT (FALSE);
03204          break;
03205 
03206        case R_386_TLS_LDM:
03207          if (! info->shared)
03208            {
03209              unsigned int val;
03210 
03211              /* LD->LE transition:
03212                Ensure it is:
03213                leal foo(%reg), %eax; call ___tls_get_addr.
03214                We change it into:
03215                movl %gs:0, %eax; nop; leal 0(%esi,1), %esi.  */
03216              BFD_ASSERT (rel->r_offset >= 2);
03217              BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
03218                        == 0x8d);
03219              val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
03220              BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
03221              BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
03222              BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
03223                        == 0xe8);
03224              BFD_ASSERT (rel + 1 < relend);
03225              BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
03226              memcpy (contents + rel->r_offset - 2,
03227                     "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
03228              /* Skip R_386_PLT32.  */
03229              rel++;
03230              continue;
03231            }
03232 
03233          if (htab->sgot == NULL)
03234            abort ();
03235 
03236          off = htab->tls_ldm_got.offset;
03237          if (off & 1)
03238            off &= ~1;
03239          else
03240            {
03241              Elf_Internal_Rela outrel;
03242              bfd_byte *loc;
03243 
03244              if (htab->srelgot == NULL)
03245               abort ();
03246 
03247              outrel.r_offset = (htab->sgot->output_section->vma
03248                              + htab->sgot->output_offset + off);
03249 
03250              bfd_put_32 (output_bfd, 0,
03251                        htab->sgot->contents + off);
03252              bfd_put_32 (output_bfd, 0,
03253                        htab->sgot->contents + off + 4);
03254              outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
03255              loc = htab->srelgot->contents;
03256              loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
03257              bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
03258              htab->tls_ldm_got.offset |= 1;
03259            }
03260          relocation = htab->sgot->output_section->vma
03261                      + htab->sgot->output_offset + off
03262                      - htab->sgotplt->output_section->vma
03263                      - htab->sgotplt->output_offset;
03264          unresolved_reloc = FALSE;
03265          break;
03266 
03267        case R_386_TLS_LDO_32:
03268          if (info->shared || (input_section->flags & SEC_CODE) == 0)
03269            relocation -= dtpoff_base (info);
03270          else
03271            /* When converting LDO to LE, we must negate.  */
03272            relocation = -tpoff (info, relocation);
03273          break;
03274 
03275        case R_386_TLS_LE_32:
03276        case R_386_TLS_LE:
03277          if (info->shared)
03278            {
03279              Elf_Internal_Rela outrel;
03280              asection *sreloc;
03281              bfd_byte *loc;
03282              int indx;
03283 
03284              outrel.r_offset = rel->r_offset
03285                             + input_section->output_section->vma
03286                             + input_section->output_offset;
03287              if (h != NULL && h->dynindx != -1)
03288               indx = h->dynindx;
03289              else
03290               indx = 0;
03291              if (r_type == R_386_TLS_LE_32)
03292               outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
03293              else
03294               outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
03295              sreloc = elf_section_data (input_section)->sreloc;
03296              if (sreloc == NULL)
03297               abort ();
03298              loc = sreloc->contents;
03299              loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
03300              bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
03301              if (indx)
03302               continue;
03303              else if (r_type == R_386_TLS_LE_32)
03304               relocation = dtpoff_base (info) - relocation;
03305              else
03306               relocation -= dtpoff_base (info);
03307            }
03308          else if (r_type == R_386_TLS_LE_32)
03309            relocation = tpoff (info, relocation);
03310          else
03311            relocation = -tpoff (info, relocation);
03312          break;
03313 
03314        default:
03315          break;
03316        }
03317 
03318       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
03319         because such sections are not SEC_ALLOC and thus ld.so will
03320         not process them.  */
03321       if (unresolved_reloc
03322          && !((input_section->flags & SEC_DEBUGGING) != 0
03323               && h->def_dynamic))
03324        {
03325          (*_bfd_error_handler)
03326            (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
03327             input_bfd,
03328             input_section,
03329             (long) rel->r_offset,
03330             howto->name,
03331             h->root.root.string);
03332          return FALSE;
03333        }
03334 
03335       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
03336                                 contents, rel->r_offset,
03337                                 relocation, 0);
03338 
03339       if (r != bfd_reloc_ok)
03340        {
03341          const char *name;
03342 
03343          if (h != NULL)
03344            name = h->root.root.string;
03345          else
03346            {
03347              name = bfd_elf_string_from_elf_section (input_bfd,
03348                                                 symtab_hdr->sh_link,
03349                                                 sym->st_name);
03350              if (name == NULL)
03351               return FALSE;
03352              if (*name == '\0')
03353               name = bfd_section_name (input_bfd, sec);
03354            }
03355 
03356          if (r == bfd_reloc_overflow)
03357            {
03358              if (! ((*info->callbacks->reloc_overflow)
03359                    (info, (h ? &h->root : NULL), name, howto->name,
03360                     (bfd_vma) 0, input_bfd, input_section,
03361                     rel->r_offset)))
03362               return FALSE;
03363            }
03364          else
03365            {
03366              (*_bfd_error_handler)
03367               (_("%B(%A+0x%lx): reloc against `%s': error %d"),
03368                input_bfd, input_section,
03369                (long) rel->r_offset, name, (int) r);
03370              return FALSE;
03371            }
03372        }
03373     }
03374 
03375   return TRUE;
03376 }
03377 
03378 /* Finish up dynamic symbol handling.  We set the contents of various
03379    dynamic sections here.  */
03380 
03381 static bfd_boolean
03382 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
03383                             struct bfd_link_info *info,
03384                             struct elf_link_hash_entry *h,
03385                             Elf_Internal_Sym *sym)
03386 {
03387   struct elf_i386_link_hash_table *htab;
03388 
03389   htab = elf_i386_hash_table (info);
03390 
03391   if (h->plt.offset != (bfd_vma) -1)
03392     {
03393       bfd_vma plt_index;
03394       bfd_vma got_offset;
03395       Elf_Internal_Rela rel;
03396       bfd_byte *loc;
03397 
03398       /* This symbol has an entry in the procedure linkage table.  Set
03399         it up.  */
03400 
03401       if (h->dynindx == -1
03402          || htab->splt == NULL
03403          || htab->sgotplt == NULL
03404          || htab->srelplt == NULL)
03405        abort ();
03406 
03407       /* Get the index in the procedure linkage table which
03408         corresponds to this symbol.  This is the index of this symbol
03409         in all the symbols for which we are making plt entries.  The
03410         first entry in the procedure linkage table is reserved.  */
03411       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
03412 
03413       /* Get the offset into the .got table of the entry that
03414         corresponds to this function.  Each .got entry is 4 bytes.
03415         The first three are reserved.  */
03416       got_offset = (plt_index + 3) * 4;
03417 
03418       /* Fill in the entry in the procedure linkage table.  */
03419       if (! info->shared)
03420        {
03421          memcpy (htab->splt->contents + h->plt.offset, elf_i386_plt_entry,
03422                 PLT_ENTRY_SIZE);
03423          bfd_put_32 (output_bfd,
03424                     (htab->sgotplt->output_section->vma
03425                      + htab->sgotplt->output_offset
03426                      + got_offset),
03427                     htab->splt->contents + h->plt.offset + 2);
03428 
03429          if (htab->is_vxworks)
03430            {
03431              int s, k, reloc_index;
03432 
03433              /* Create the R_386_32 relocation referencing the GOT
03434                for this PLT entry.  */
03435 
03436              /* S: Current slot number (zero-based).  */
03437              s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
03438              /* K: Number of relocations for PLTResolve. */
03439              if (info->shared)
03440               k = PLTRESOLVE_RELOCS_SHLIB;
03441              else
03442               k = PLTRESOLVE_RELOCS;
03443              /* Skip the PLTresolve relocations, and the relocations for
03444                the other PLT slots. */
03445              reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
03446              loc = (htab->srelplt2->contents + reloc_index
03447                    * sizeof (Elf32_External_Rel));
03448 
03449              rel.r_offset = (htab->splt->output_section->vma
03450                            + htab->splt->output_offset
03451                            + h->plt.offset + 2),
03452              rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
03453              bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
03454 
03455              /* Create the R_386_32 relocation referencing the beginning of
03456                the PLT for this GOT entry.  */
03457              rel.r_offset = (htab->sgotplt->output_section->vma
03458                            + htab->sgotplt->output_offset
03459                            + got_offset);
03460              rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
03461              bfd_elf32_swap_reloc_out (output_bfd, &rel,
03462              loc + sizeof (Elf32_External_Rel));
03463            }
03464        }
03465       else
03466        {
03467          memcpy (htab->splt->contents + h->plt.offset, elf_i386_pic_plt_entry,
03468                 PLT_ENTRY_SIZE);
03469          bfd_put_32 (output_bfd, got_offset,
03470                     htab->splt->contents + h->plt.offset + 2);
03471        }
03472 
03473       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
03474                 htab->splt->contents + h->plt.offset + 7);
03475       bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
03476                 htab->splt->contents + h->plt.offset + 12);
03477 
03478       /* Fill in the entry in the global offset table.  */
03479       bfd_put_32 (output_bfd,
03480                 (htab->splt->output_section->vma
03481                  + htab->splt->output_offset
03482                  + h->plt.offset
03483                  + 6),
03484                 htab->sgotplt->contents + got_offset);
03485 
03486       /* Fill in the entry in the .rel.plt section.  */
03487       rel.r_offset = (htab->sgotplt->output_section->vma
03488                     + htab->sgotplt->output_offset
03489                     + got_offset);
03490       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
03491       loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rel);
03492       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
03493 
03494       if (!h->def_regular)
03495        {
03496          /* Mark the symbol as undefined, rather than as defined in
03497             the .plt section.  Leave the value if there were any
03498             relocations where pointer equality matters (this is a clue
03499             for the dynamic linker, to make function pointer
03500             comparisons work between an application and shared
03501             library), otherwise set it to zero.  If a function is only
03502             called from a binary, there is no need to slow down
03503             shared libraries because of that.  */
03504          sym->st_shndx = SHN_UNDEF;
03505          if (!h->pointer_equality_needed)
03506            sym->st_value = 0;
03507        }
03508     }
03509 
03510   if (h->got.offset != (bfd_vma) -1
03511       && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
03512       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
03513     {
03514       Elf_Internal_Rela rel;
03515       bfd_byte *loc;
03516 
03517       /* This symbol has an entry in the global offset table.  Set it
03518         up.  */
03519 
03520       if (htab->sgot == NULL || htab->srelgot == NULL)
03521        abort ();
03522 
03523       rel.r_offset = (htab->sgot->output_section->vma
03524                     + htab->sgot->output_offset
03525                     + (h->got.offset & ~(bfd_vma) 1));
03526 
03527       /* If this is a static link, or it is a -Bsymbolic link and the
03528         symbol is defined locally or was forced to be local because
03529         of a version file, we just want to emit a RELATIVE reloc.
03530         The entry in the global offset table will already have been
03531         initialized in the relocate_section function.  */
03532       if (info->shared
03533          && SYMBOL_REFERENCES_LOCAL (info, h))
03534        {
03535          BFD_ASSERT((h->got.offset & 1) != 0);
03536          rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
03537        }
03538       else
03539        {
03540          BFD_ASSERT((h->got.offset & 1) == 0);
03541          bfd_put_32 (output_bfd, (bfd_vma) 0,
03542                     htab->sgot->contents + h->got.offset);
03543          rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
03544        }
03545 
03546       loc = htab->srelgot->contents;
03547       loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
03548       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
03549     }
03550 
03551   if (h->needs_copy)
03552     {
03553       Elf_Internal_Rela rel;
03554       bfd_byte *loc;
03555 
03556       /* This symbol needs a copy reloc.  Set it up.  */
03557 
03558       if (h->dynindx == -1
03559          || (h->root.type != bfd_link_hash_defined
03560              && h->root.type != bfd_link_hash_defweak)
03561          || htab->srelbss == NULL)
03562        abort ();
03563 
03564       rel.r_offset = (h->root.u.def.value
03565                     + h->root.u.def.section->output_section->vma
03566                     + h->root.u.def.section->output_offset);
03567       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
03568       loc = htab->srelbss->contents;
03569       loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
03570       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
03571     }
03572 
03573   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.
03574      On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
03575      is relative to the ".got" section.  */
03576   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
03577       || (!htab->is_vxworks && h == htab->elf.hgot))
03578     sym->st_shndx = SHN_ABS;
03579 
03580   return TRUE;
03581 }
03582 
03583 /* Used to decide how to sort relocs in an optimal manner for the
03584    dynamic linker, before writing them out.  */
03585 
03586 static enum elf_reloc_type_class
03587 elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
03588 {
03589   switch (ELF32_R_TYPE (rela->r_info))
03590     {
03591     case R_386_RELATIVE:
03592       return reloc_class_relative;
03593     case R_386_JUMP_SLOT:
03594       return reloc_class_plt;
03595     case R_386_COPY:
03596       return reloc_class_copy;
03597     default:
03598       return reloc_class_normal;
03599     }
03600 }
03601 
03602 /* Finish up the dynamic sections.  */
03603 
03604 static bfd_boolean
03605 elf_i386_finish_dynamic_sections (bfd *output_bfd,
03606                               struct bfd_link_info *info)
03607 {
03608   struct elf_i386_link_hash_table *htab;
03609   bfd *dynobj;
03610   asection *sdyn;
03611 
03612   htab = elf_i386_hash_table (info);
03613   dynobj = htab->elf.dynobj;
03614   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
03615 
03616   if (htab->elf.dynamic_sections_created)
03617     {
03618       Elf32_External_Dyn *dyncon, *dynconend;
03619 
03620       if (sdyn == NULL || htab->sgot == NULL)
03621        abort ();
03622 
03623       dyncon = (Elf32_External_Dyn *) sdyn->contents;
03624       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
03625       for (; dyncon < dynconend; dyncon++)
03626        {
03627          Elf_Internal_Dyn dyn;
03628          asection *s;
03629 
03630          bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
03631 
03632          switch (dyn.d_tag)
03633            {
03634            default:
03635              continue;
03636 
03637            case DT_PLTGOT:
03638              s = htab->sgotplt;
03639              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
03640              break;
03641 
03642            case DT_JMPREL:
03643              s = htab->srelplt;
03644              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
03645              break;
03646 
03647            case DT_PLTRELSZ:
03648              s = htab->srelplt;
03649              dyn.d_un.d_val = s->size;
03650              break;
03651 
03652            case DT_RELSZ:
03653              /* My reading of the SVR4 ABI indicates that the
03654                procedure linkage table relocs (DT_JMPREL) should be
03655                included in the overall relocs (DT_REL).  This is
03656                what Solaris does.  However, UnixWare can not handle
03657                that case.  Therefore, we override the DT_RELSZ entry
03658                here to make it not include the JMPREL relocs.  */
03659              s = htab->srelplt;
03660              if (s == NULL)
03661               continue;
03662              dyn.d_un.d_val -= s->size;
03663              break;
03664 
03665            case DT_REL:
03666              /* We may not be using the standard ELF linker script.
03667                If .rel.plt is the first .rel section, we adjust
03668                DT_REL to not include it.  */
03669              s = htab->srelplt;
03670              if (s == NULL)
03671               continue;
03672              if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
03673               continue;
03674              dyn.d_un.d_ptr += s->size;
03675              break;
03676            }
03677 
03678          bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
03679        }
03680 
03681       /* Fill in the first entry in the procedure linkage table.  */
03682       if (htab->splt && htab->splt->size > 0)
03683        {
03684          if (info->shared)
03685            {
03686              memcpy (htab->splt->contents, elf_i386_pic_plt0_entry,
03687                     sizeof (elf_i386_pic_plt0_entry));
03688              memset (htab->splt->contents + sizeof (elf_i386_pic_plt0_entry),
03689                     htab->plt0_pad_byte,
03690                     PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
03691            }
03692          else
03693            {
03694              memcpy (htab->splt->contents, elf_i386_plt0_entry,
03695                     sizeof(elf_i386_plt0_entry));
03696              memset (htab->splt->contents + sizeof (elf_i386_plt0_entry),
03697                     htab->plt0_pad_byte,
03698                     PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
03699              bfd_put_32 (output_bfd,
03700                        (htab->sgotplt->output_section->vma
03701                         + htab->sgotplt->output_offset
03702                         + 4),
03703                        htab->splt->contents + 2);
03704              bfd_put_32 (output_bfd,
03705                        (htab->sgotplt->output_section->vma
03706                         + htab->sgotplt->output_offset
03707                         + 8),
03708                        htab->splt->contents + 8);
03709 
03710              if (htab->is_vxworks)
03711               {
03712                 Elf_Internal_Rela rel;
03713 
03714                 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
03715                    On IA32 we use REL relocations so the addend goes in
03716                    the PLT directly.  */
03717                 rel.r_offset = (htab->splt->output_section->vma
03718                               + htab->splt->output_offset
03719                               + 2);
03720                 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
03721                 bfd_elf32_swap_reloc_out (output_bfd, &rel,
03722                                        htab->srelplt2->contents);
03723                 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
03724                 rel.r_offset = (htab->splt->output_section->vma
03725                               + htab->splt->output_offset
03726                               + 8);
03727                 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
03728                 bfd_elf32_swap_reloc_out (output_bfd, &rel,
03729                                        htab->srelplt2->contents +
03730                                        sizeof (Elf32_External_Rel));
03731               }
03732            }
03733 
03734          /* UnixWare sets the entsize of .plt to 4, although that doesn't
03735             really seem like the right value.  */
03736          elf_section_data (htab->splt->output_section)
03737            ->this_hdr.sh_entsize = 4;
03738 
03739          /* Correct the .rel.plt.unloaded relocations.  */
03740          if (htab->is_vxworks && !info->shared)
03741            {
03742              int num_plts = (htab->splt->size / PLT_ENTRY_SIZE) - 1;
03743              unsigned char *p;
03744 
03745              p = htab->srelplt2->contents;
03746              if (info->shared)
03747               p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
03748              else
03749               p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
03750 
03751              for (; num_plts; num_plts--)
03752               {
03753                 Elf_Internal_Rela rel;
03754                 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
03755                 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
03756                 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
03757                 p += sizeof (Elf32_External_Rel);
03758 
03759                 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
03760                 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
03761                 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
03762                 p += sizeof (Elf32_External_Rel);
03763               }
03764            }
03765        }
03766     }
03767 
03768   if (htab->sgotplt)
03769     {
03770       /* Fill in the first three entries in the global offset table.  */
03771       if (htab->sgotplt->size > 0)
03772        {
03773          bfd_put_32 (output_bfd,
03774                     (sdyn == NULL ? 0
03775                      : sdyn->output_section->vma + sdyn->output_offset),
03776                     htab->sgotplt->contents);
03777          bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 4);
03778          bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 8);
03779        }
03780 
03781       elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
03782     }
03783 
03784   if (htab->sgot && htab->sgot->size > 0)
03785     elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
03786 
03787   return TRUE;
03788 }
03789 
03790 /* Return address for Ith PLT stub in section PLT, for relocation REL
03791    or (bfd_vma) -1 if it should not be included.  */
03792 
03793 static bfd_vma
03794 elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
03795                     const arelent *rel ATTRIBUTE_UNUSED)
03796 {
03797   return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
03798 }
03799 
03800 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
03801 
03802 static bfd_boolean
03803 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
03804 {
03805   if (h->plt.offset != (bfd_vma) -1
03806       && !h->def_regular
03807       && !h->pointer_equality_needed)
03808     return FALSE;
03809 
03810   return _bfd_elf_hash_symbol (h);
03811 }
03812 
03813 #define TARGET_LITTLE_SYM          bfd_elf32_i386_vec
03814 #define TARGET_LITTLE_NAME         "elf32-i386"
03815 #define ELF_ARCH                   bfd_arch_i386
03816 #define ELF_MACHINE_CODE           EM_386
03817 #define ELF_MAXPAGESIZE                   0x1000
03818 
03819 #define elf_backend_can_gc_sections       1
03820 #define elf_backend_can_refcount   1
03821 #define elf_backend_want_got_plt   1
03822 #define elf_backend_plt_readonly   1
03823 #define elf_backend_want_plt_sym   0
03824 #define elf_backend_got_header_size       12
03825 
03826 /* Support RELA for objdump of prelink objects.  */
03827 #define elf_info_to_howto                elf_i386_info_to_howto_rel
03828 #define elf_info_to_howto_rel                   elf_i386_info_to_howto_rel
03829 
03830 #define bfd_elf32_mkobject               elf_i386_mkobject
03831 
03832 #define bfd_elf32_bfd_is_local_label_name     elf_i386_is_local_label_name
03833 #define bfd_elf32_bfd_link_hash_table_create  elf_i386_link_hash_table_create
03834 #define bfd_elf32_bfd_reloc_type_lookup         elf_i386_reloc_type_lookup
03835 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
03836 
03837 #define elf_backend_adjust_dynamic_symbol     elf_i386_adjust_dynamic_symbol
03838 #define elf_backend_check_relocs         elf_i386_check_relocs
03839 #define elf_backend_copy_indirect_symbol      elf_i386_copy_indirect_symbol
03840 #define elf_backend_create_dynamic_sections   elf_i386_create_dynamic_sections
03841 #define elf_backend_fake_sections        elf_i386_fake_sections
03842 #define elf_backend_finish_dynamic_sections   elf_i386_finish_dynamic_sections
03843 #define elf_backend_finish_dynamic_symbol     elf_i386_finish_dynamic_symbol
03844 #define elf_backend_gc_mark_hook         elf_i386_gc_mark_hook
03845 #define elf_backend_gc_sweep_hook        elf_i386_gc_sweep_hook
03846 #define elf_backend_grok_prstatus        elf_i386_grok_prstatus
03847 #define elf_backend_grok_psinfo                 elf_i386_grok_psinfo
03848 #define elf_backend_reloc_type_class            elf_i386_reloc_type_class
03849 #define elf_backend_relocate_section            elf_i386_relocate_section
03850 #define elf_backend_size_dynamic_sections     elf_i386_size_dynamic_sections
03851 #define elf_backend_always_size_sections      elf_i386_always_size_sections
03852 #define elf_backend_omit_section_dynsym \
03853   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
03854 #define elf_backend_plt_sym_val                 elf_i386_plt_sym_val
03855 #define elf_backend_hash_symbol                 elf_i386_hash_symbol
03856 
03857 #include "elf32-target.h"
03858 
03859 /* FreeBSD support.  */
03860 
03861 #undef TARGET_LITTLE_SYM
03862 #define       TARGET_LITTLE_SYM           bfd_elf32_i386_freebsd_vec
03863 #undef TARGET_LITTLE_NAME
03864 #define       TARGET_LITTLE_NAME          "elf32-i386-freebsd"
03865 #undef ELF_OSABI
03866 #define       ELF_OSABI                   ELFOSABI_FREEBSD
03867 
03868 /* The kernel recognizes executables as valid only if they carry a
03869    "FreeBSD" label in the ELF header.  So we put this label on all
03870    executables and (for simplicity) also all other object files.  */
03871 
03872 static void
03873 elf_i386_post_process_headers (bfd *abfd,
03874                             struct bfd_link_info *info ATTRIBUTE_UNUSED)
03875 {
03876   Elf_Internal_Ehdr *i_ehdrp;
03877 
03878   i_ehdrp = elf_elfheader (abfd);
03879 
03880   /* Put an ABI label supported by FreeBSD >= 4.1.  */
03881   i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
03882 #ifdef OLD_FREEBSD_ABI_LABEL
03883   /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard.  */
03884   memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
03885 #endif
03886 }
03887 
03888 #undef elf_backend_post_process_headers
03889 #define       elf_backend_post_process_headers   elf_i386_post_process_headers
03890 #undef elf32_bed
03891 #define       elf32_bed                          elf32_i386_fbsd_bed
03892 
03893 #include "elf32-target.h"
03894 
03895 /* VxWorks support.  */
03896 
03897 #undef TARGET_LITTLE_SYM
03898 #define TARGET_LITTLE_SYM          bfd_elf32_i386_vxworks_vec
03899 #undef TARGET_LITTLE_NAME
03900 #define TARGET_LITTLE_NAME         "elf32-i386-vxworks"
03901 #undef ELF_OSABI
03902 
03903 /* Like elf_i386_link_hash_table_create but with tweaks for VxWorks.  */
03904 
03905 static struct bfd_link_hash_table *
03906 elf_i386_vxworks_link_hash_table_create (bfd *abfd)
03907 {
03908   struct bfd_link_hash_table *ret;
03909   struct elf_i386_link_hash_table *htab;
03910 
03911   ret = elf_i386_link_hash_table_create (abfd);
03912   if (ret)
03913     {
03914       htab = (struct elf_i386_link_hash_table *) ret;
03915       htab->is_vxworks = 1;
03916       htab->plt0_pad_byte = 0x90;
03917     }
03918 
03919   return ret;
03920 }
03921 
03922 
03923 #undef elf_backend_post_process_headers
03924 #undef bfd_elf32_bfd_link_hash_table_create
03925 #define bfd_elf32_bfd_link_hash_table_create \
03926   elf_i386_vxworks_link_hash_table_create
03927 #undef elf_backend_add_symbol_hook
03928 #define elf_backend_add_symbol_hook \
03929   elf_vxworks_add_symbol_hook
03930 #undef elf_backend_link_output_symbol_hook
03931 #define elf_backend_link_output_symbol_hook \
03932   elf_vxworks_link_output_symbol_hook
03933 #undef elf_backend_emit_relocs
03934 #define elf_backend_emit_relocs                  elf_vxworks_emit_relocs
03935 #undef elf_backend_final_write_processing
03936 #define elf_backend_final_write_processing \
03937   elf_vxworks_final_write_processing
03938 
03939 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
03940    define it.  */
03941 #undef elf_backend_want_plt_sym
03942 #define elf_backend_want_plt_sym   1
03943 
03944 #undef elf32_bed
03945 #define elf32_bed                         elf32_i386_vxworks_bed
03946 
03947 #include "elf32-target.h"