home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / elf32-i386.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  46KB  |  1,547 lines

  1. /* Intel 80386/80486-specific support for 32-bit ELF
  2.    Copyright 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
  3.  
  4. This file is part of BFD, the Binary File Descriptor library.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  19.  
  20. #include "bfd.h"
  21. #include "sysdep.h"
  22. #include "bfdlink.h"
  23. #include "libbfd.h"
  24. #include "elf-bfd.h"
  25.  
  26. static reloc_howto_type *elf_i386_reloc_type_lookup
  27.   PARAMS ((bfd *, bfd_reloc_code_real_type));
  28. static void elf_i386_info_to_howto
  29.   PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
  30. static void elf_i386_info_to_howto_rel
  31.   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
  32. static boolean elf_i386_check_relocs
  33.   PARAMS ((bfd *, struct bfd_link_info *, asection *,
  34.        const Elf_Internal_Rela *));
  35. static boolean elf_i386_adjust_dynamic_symbol
  36.   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
  37. static boolean elf_i386_size_dynamic_sections
  38.   PARAMS ((bfd *, struct bfd_link_info *));
  39. static boolean elf_i386_relocate_section
  40.   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  41.        Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
  42. static boolean elf_i386_finish_dynamic_symbol
  43.   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
  44.        Elf_Internal_Sym *));
  45. static boolean elf_i386_finish_dynamic_sections
  46.   PARAMS ((bfd *, struct bfd_link_info *));
  47.  
  48. #define USE_REL    1        /* 386 uses REL relocations instead of RELA */
  49.  
  50. enum reloc_type
  51.   {
  52.     R_386_NONE = 0,
  53.     R_386_32,
  54.     R_386_PC32,
  55.     R_386_GOT32,
  56.     R_386_PLT32,
  57.     R_386_COPY,
  58.     R_386_GLOB_DAT,
  59.     R_386_JUMP_SLOT,
  60.     R_386_RELATIVE,
  61.     R_386_GOTOFF,
  62.     R_386_GOTPC,
  63.     R_386_max
  64.   };
  65.  
  66. #if 0
  67. static CONST char *CONST reloc_type_names[] =
  68. {
  69.   "R_386_NONE",
  70.   "R_386_32",
  71.   "R_386_PC32",
  72.   "R_386_GOT32",
  73.   "R_386_PLT32",
  74.   "R_386_COPY",
  75.   "R_386_GLOB_DAT",
  76.   "R_386_JUMP_SLOT",
  77.   "R_386_RELATIVE",
  78.   "R_386_GOTOFF",
  79.   "R_386_GOTPC",
  80. };
  81. #endif
  82.  
  83. static reloc_howto_type elf_howto_table[]=
  84. {
  85.   HOWTO(R_386_NONE,     0,0, 0,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_NONE",        true,0x00000000,0x00000000,false),
  86.   HOWTO(R_386_32,     0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_32",        true,0xffffffff,0xffffffff,false),
  87.   HOWTO(R_386_PC32,     0,2,32,true, 0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PC32",        true,0xffffffff,0xffffffff,true),
  88.   HOWTO(R_386_GOT32,     0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOT32",    true,0xffffffff,0xffffffff,false),
  89.   HOWTO(R_386_PLT32,     0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PLT32",    true,0xffffffff,0xffffffff,true),
  90.   HOWTO(R_386_COPY,      0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_COPY",        true,0xffffffff,0xffffffff,false),
  91.   HOWTO(R_386_GLOB_DAT,  0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GLOB_DAT", true,0xffffffff,0xffffffff,false),
  92.   HOWTO(R_386_JUMP_SLOT, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_JUMP_SLOT",true,0xffffffff,0xffffffff,false),
  93.   HOWTO(R_386_RELATIVE,  0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_RELATIVE", true,0xffffffff,0xffffffff,false),
  94.   HOWTO(R_386_GOTOFF,    0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTOFF",   true,0xffffffff,0xffffffff,false),
  95.   HOWTO(R_386_GOTPC,     0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTPC",    true,0xffffffff,0xffffffff,true),
  96. };
  97.  
  98. #ifdef DEBUG_GEN_RELOC
  99. #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
  100. #else
  101. #define TRACE(str)
  102. #endif
  103.  
  104. static reloc_howto_type *
  105. elf_i386_reloc_type_lookup (abfd, code)
  106.      bfd *abfd;
  107.      bfd_reloc_code_real_type code;
  108. {
  109.   switch (code)
  110.     {
  111.     case BFD_RELOC_NONE:
  112.       TRACE ("BFD_RELOC_NONE");
  113.       return &elf_howto_table[ (int)R_386_NONE ];
  114.  
  115.     case BFD_RELOC_32:
  116.       TRACE ("BFD_RELOC_32");
  117.       return &elf_howto_table[ (int)R_386_32 ];
  118.  
  119.     case BFD_RELOC_32_PCREL:
  120.       TRACE ("BFD_RELOC_PC32");
  121.       return &elf_howto_table[ (int)R_386_PC32 ];
  122.  
  123.     case BFD_RELOC_386_GOT32:
  124.       TRACE ("BFD_RELOC_386_GOT32");
  125.       return &elf_howto_table[ (int)R_386_GOT32 ];
  126.  
  127.     case BFD_RELOC_386_PLT32:
  128.       TRACE ("BFD_RELOC_386_PLT32");
  129.       return &elf_howto_table[ (int)R_386_PLT32 ];
  130.  
  131.     case BFD_RELOC_386_COPY:
  132.       TRACE ("BFD_RELOC_386_COPY");
  133.       return &elf_howto_table[ (int)R_386_COPY ];
  134.  
  135.     case BFD_RELOC_386_GLOB_DAT:
  136.       TRACE ("BFD_RELOC_386_GLOB_DAT");
  137.       return &elf_howto_table[ (int)R_386_GLOB_DAT ];
  138.  
  139.     case BFD_RELOC_386_JUMP_SLOT:
  140.       TRACE ("BFD_RELOC_386_JUMP_SLOT");
  141.       return &elf_howto_table[ (int)R_386_JUMP_SLOT ];
  142.  
  143.     case BFD_RELOC_386_RELATIVE:
  144.       TRACE ("BFD_RELOC_386_RELATIVE");
  145.       return &elf_howto_table[ (int)R_386_RELATIVE ];
  146.  
  147.     case BFD_RELOC_386_GOTOFF:
  148.       TRACE ("BFD_RELOC_386_GOTOFF");
  149.       return &elf_howto_table[ (int)R_386_GOTOFF ];
  150.  
  151.     case BFD_RELOC_386_GOTPC:
  152.       TRACE ("BFD_RELOC_386_GOTPC");
  153.       return &elf_howto_table[ (int)R_386_GOTPC ];
  154.  
  155.     default:
  156.       break;
  157.     }
  158.  
  159.   TRACE ("Unknown");
  160.   return 0;
  161. }
  162.  
  163. static void
  164. elf_i386_info_to_howto (abfd, cache_ptr, dst)
  165.      bfd        *abfd;
  166.      arelent        *cache_ptr;
  167.      Elf32_Internal_Rela *dst;
  168. {
  169.   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max);
  170.  
  171.   cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
  172. }
  173.  
  174. static void
  175. elf_i386_info_to_howto_rel (abfd, cache_ptr, dst)
  176.      bfd        *abfd;
  177.      arelent        *cache_ptr;
  178.      Elf32_Internal_Rel *dst;
  179. {
  180.   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max);
  181.  
  182.   cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
  183. }
  184.  
  185. /* Functions for the i386 ELF linker.  */
  186.  
  187. /* The name of the dynamic interpreter.  This is put in the .interp
  188.    section.  */
  189.  
  190. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
  191.  
  192. /* The size in bytes of an entry in the procedure linkage table.  */
  193.  
  194. #define PLT_ENTRY_SIZE 16
  195.  
  196. /* The first entry in an absolute procedure linkage table looks like
  197.    this.  See the SVR4 ABI i386 supplement to see how this works.  */
  198.  
  199. static const bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] =
  200. {
  201.   0xff, 0x35,    /* pushl contents of address */
  202.   0, 0, 0, 0,    /* replaced with address of .got + 4.  */
  203.   0xff, 0x25,    /* jmp indirect */
  204.   0, 0, 0, 0,    /* replaced with address of .got + 8.  */
  205.   0, 0, 0, 0    /* pad out to 16 bytes.  */
  206. };
  207.  
  208. /* Subsequent entries in an absolute procedure linkage table look like
  209.    this.  */
  210.  
  211. static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
  212. {
  213.   0xff, 0x25,    /* jmp indirect */
  214.   0, 0, 0, 0,    /* replaced with address of this symbol in .got.  */
  215.   0x68,        /* pushl immediate */
  216.   0, 0, 0, 0,    /* replaced with offset into relocation table.  */
  217.   0xe9,        /* jmp relative */
  218.   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
  219. };
  220.  
  221. /* The first entry in a PIC procedure linkage table look like this.  */
  222.  
  223. static const bfd_byte elf_i386_pic_plt0_entry[PLT_ENTRY_SIZE] =
  224. {
  225.   0xff, 0xb3, 4, 0, 0, 0,    /* pushl 4(%ebx) */    
  226.   0xff, 0xa3, 8, 0, 0, 0,    /* jmp *8(%ebx) */    
  227.   0, 0, 0, 0            /* pad out to 16 bytes.  */
  228. };
  229.  
  230. /* Subsequent entries in a PIC procedure linkage table look like this.  */
  231.  
  232. static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
  233. {
  234.   0xff, 0xa3,    /* jmp *offset(%ebx) */
  235.   0, 0, 0, 0,    /* replaced with offset of this symbol in .got.  */
  236.   0x68,        /* pushl immediate */
  237.   0, 0, 0, 0,    /* replaced with offset into relocation table.  */
  238.   0xe9,        /* jmp relative */
  239.   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
  240. };
  241.  
  242. /* Look through the relocs for a section during the first phase, and
  243.    allocate space in the global offset table or procedure linkage
  244.    table.  */
  245.  
  246. static boolean
  247. elf_i386_check_relocs (abfd, info, sec, relocs)
  248.      bfd *abfd;
  249.      struct bfd_link_info *info;
  250.      asection *sec;
  251.      const Elf_Internal_Rela *relocs;
  252. {
  253.   bfd *dynobj;
  254.   Elf_Internal_Shdr *symtab_hdr;
  255.   struct elf_link_hash_entry **sym_hashes;
  256.   bfd_vma *local_got_offsets;
  257.   const Elf_Internal_Rela *rel;
  258.   const Elf_Internal_Rela *rel_end;
  259.   asection *sgot;
  260.   asection *srelgot;
  261.   asection *sreloc;
  262.  
  263.   if (info->relocateable)
  264.     return true;
  265.  
  266.   dynobj = elf_hash_table (info)->dynobj;
  267.   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  268.   sym_hashes = elf_sym_hashes (abfd);
  269.   local_got_offsets = elf_local_got_offsets (abfd);
  270.  
  271.   sgot = NULL;
  272.   srelgot = NULL;
  273.   sreloc = NULL;
  274.  
  275.   rel_end = relocs + sec->reloc_count;
  276.   for (rel = relocs; rel < rel_end; rel++)
  277.     {
  278.       unsigned long r_symndx;
  279.       struct elf_link_hash_entry *h;
  280.  
  281.       r_symndx = ELF32_R_SYM (rel->r_info);
  282.  
  283.       if (r_symndx < symtab_hdr->sh_info)
  284.     h = NULL;
  285.       else
  286.     h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  287.  
  288.       /* Some relocs require a global offset table.  */
  289.       if (dynobj == NULL)
  290.     {
  291.       switch (ELF32_R_TYPE (rel->r_info))
  292.         {
  293.         case R_386_GOT32:
  294.         case R_386_GOTOFF:
  295.         case R_386_GOTPC:
  296.           elf_hash_table (info)->dynobj = dynobj = abfd;
  297.           if (! _bfd_elf_create_got_section (dynobj, info))
  298.         return false;
  299.           break;
  300.  
  301.         default:
  302.           break;
  303.         }
  304.     }
  305.  
  306.       switch (ELF32_R_TYPE (rel->r_info))
  307.     {
  308.     case R_386_GOT32:
  309.       /* This symbol requires a global offset table entry.  */
  310.      
  311.       if (sgot == NULL)
  312.         {
  313.           sgot = bfd_get_section_by_name (dynobj, ".got");
  314.           BFD_ASSERT (sgot != NULL);
  315.         }
  316.  
  317.       if (srelgot == NULL
  318.           && (h != NULL || info->shared))
  319.         {
  320.           srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
  321.           if (srelgot == NULL)
  322.         {
  323.           srelgot = bfd_make_section (dynobj, ".rel.got");
  324.           if (srelgot == NULL
  325.               || ! bfd_set_section_flags (dynobj, srelgot,
  326.                           (SEC_ALLOC
  327.                            | SEC_LOAD
  328.                            | SEC_HAS_CONTENTS
  329.                            | SEC_IN_MEMORY
  330.                            | SEC_READONLY))
  331.               || ! bfd_set_section_alignment (dynobj, srelgot, 2))
  332.             return false;
  333.         }
  334.         }
  335.  
  336.       if (h != NULL)
  337.         {
  338.           if (h->got_offset != (bfd_vma) -1)
  339.         {
  340.           /* We have already allocated space in the .got.  */
  341.           break;
  342.         }
  343.           h->got_offset = sgot->_raw_size;
  344.  
  345.           /* Make sure this symbol is output as a dynamic symbol.  */
  346.           if (h->dynindx == -1)
  347.         {
  348.           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
  349.             return false;
  350.         }
  351.  
  352.           srelgot->_raw_size += sizeof (Elf32_External_Rel);
  353.         }
  354.       else
  355.         {
  356.                /* This is a global offset table entry for a local
  357.                  symbol.  */
  358.           if (local_got_offsets == NULL)
  359.         {
  360.           size_t size;
  361.           register unsigned int i;
  362.  
  363.           size = symtab_hdr->sh_info * sizeof (bfd_vma);
  364.           local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
  365.           if (local_got_offsets == NULL)
  366.             return false;
  367.           elf_local_got_offsets (abfd) = local_got_offsets;
  368.           for (i = 0; i < symtab_hdr->sh_info; i++)
  369.             local_got_offsets[i] = (bfd_vma) -1;
  370.         }
  371.           if (local_got_offsets[r_symndx] != (bfd_vma) -1)
  372.         {
  373.           /* We have already allocated space in the .got.  */
  374.           break;
  375.         }
  376.           local_got_offsets[r_symndx] = sgot->_raw_size;
  377.  
  378.           if (info->shared)
  379.         {
  380.           /* If we are generating a shared object, we need to
  381.                      output a R_386_RELATIVE reloc so that the dynamic
  382.                      linker can adjust this GOT entry.  */
  383.           srelgot->_raw_size += sizeof (Elf32_External_Rel);
  384.         }
  385.         }
  386.  
  387.       sgot->_raw_size += 4;
  388.  
  389.       break;
  390.  
  391.     case R_386_PLT32:
  392.       /* This symbol requires a procedure linkage table entry.  We
  393.              actually build the entry in adjust_dynamic_symbol,
  394.              because this might be a case of linking PIC code which is
  395.              never referenced by a dynamic object, in which case we
  396.              don't need to generate a procedure linkage table entry
  397.              after all.  */
  398.  
  399.       /* If this is a local symbol, we resolve it directly without
  400.              creating a procedure linkage table entry.  */
  401.       if (h == NULL)
  402.         continue;
  403.  
  404.       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
  405.  
  406.       break;
  407.  
  408.     case R_386_32:
  409.     case R_386_PC32:
  410.       if (info->shared
  411.           && (sec->flags & SEC_ALLOC) != 0
  412.           && (ELF32_R_TYPE (rel->r_info) != R_386_PC32 || h != NULL))
  413.         {
  414.           /* When creating a shared object, we must copy these
  415.                  reloc types into the output file.  We create a reloc
  416.                  section in dynobj and make room for this reloc.  */
  417.           if (sreloc == NULL)
  418.         {
  419.           const char *name;
  420.  
  421.           name = (bfd_elf_string_from_elf_section
  422.               (abfd,
  423.                elf_elfheader (abfd)->e_shstrndx,
  424.                elf_section_data (sec)->rel_hdr.sh_name));
  425.           if (name == NULL)
  426.             return false;
  427.  
  428.           BFD_ASSERT (strncmp (name, ".rel", 4) == 0
  429.                   && strcmp (bfd_get_section_name (abfd, sec),
  430.                      name + 4) == 0);
  431.  
  432.           sreloc = bfd_get_section_by_name (dynobj, name);
  433.           if (sreloc == NULL)
  434.             {
  435.               sreloc = bfd_make_section (dynobj, name);
  436.               if (sreloc == NULL
  437.               || ! bfd_set_section_flags (dynobj, sreloc,
  438.                               (SEC_ALLOC
  439.                                | SEC_LOAD
  440.                                | SEC_HAS_CONTENTS
  441.                                | SEC_IN_MEMORY
  442.                                | SEC_READONLY))
  443.               || ! bfd_set_section_alignment (dynobj, sreloc, 2))
  444.             return false;
  445.             }
  446.         }
  447.  
  448.           sreloc->_raw_size += sizeof (Elf32_External_Rel);
  449.         }
  450.          
  451.       break;
  452.  
  453.     default:
  454.       break;
  455.     }
  456.     }
  457.  
  458.   return true;
  459. }
  460.  
  461. /* Adjust a symbol defined by a dynamic object and referenced by a
  462.    regular object.  The current definition is in some section of the
  463.    dynamic object, but we're not including those sections.  We have to
  464.    change the definition to something the rest of the link can
  465.    understand.  */
  466.  
  467. static boolean
  468. elf_i386_adjust_dynamic_symbol (info, h)
  469.      struct bfd_link_info *info;
  470.      struct elf_link_hash_entry *h;
  471. {
  472.   bfd *dynobj;
  473.   asection *s;
  474.   unsigned int power_of_two;
  475.  
  476.   dynobj = elf_hash_table (info)->dynobj;
  477.  
  478.   /* Make sure we know what is going on here.  */
  479.   BFD_ASSERT (dynobj != NULL
  480.           && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
  481.           || h->weakdef != NULL
  482.           || ((h->elf_link_hash_flags
  483.                & ELF_LINK_HASH_DEF_DYNAMIC) != 0
  484.               && (h->elf_link_hash_flags
  485.               & ELF_LINK_HASH_REF_REGULAR) != 0
  486.               && (h->elf_link_hash_flags
  487.               & ELF_LINK_HASH_DEF_REGULAR) == 0)));
  488.  
  489.   /* If this is a function, put it in the procedure linkage table.  We
  490.      will fill in the contents of the procedure linkage table later,
  491.      when we know the address of the .got section.  */
  492.   if (h->type == STT_FUNC
  493.       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
  494.     {
  495.       if (! info->shared
  496.       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
  497.       && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
  498.     {
  499.       /* This case can occur if we saw a PLT32 reloc in an input
  500.              file, but the symbol was never referred to by a dynamic
  501.              object.  In such a case, we don't actually need to build
  502.              a procedure linkage table, and we can just do a PC32
  503.              reloc instead.  */
  504.       BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
  505.       return true;
  506.     }
  507.  
  508.       /* Make sure this symbol is output as a dynamic symbol.  */
  509.       if (h->dynindx == -1)
  510.     {
  511.       if (! bfd_elf32_link_record_dynamic_symbol (info, h))
  512.         return false;
  513.     }
  514.  
  515.       s = bfd_get_section_by_name (dynobj, ".plt");
  516.       BFD_ASSERT (s != NULL);
  517.  
  518.       /* If this is the first .plt entry, make room for the special
  519.      first entry.  */
  520.       if (s->_raw_size == 0)
  521.     s->_raw_size += PLT_ENTRY_SIZE;
  522.  
  523.       /* If this symbol is not defined in a regular file, and we are
  524.      not generating a shared library, then set the symbol to this
  525.      location in the .plt.  This is required to make function
  526.      pointers compare as equal between the normal executable and
  527.      the shared library.  */
  528.       if (! info->shared
  529.       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
  530.     {
  531.       h->root.u.def.section = s;
  532.       h->root.u.def.value = s->_raw_size;
  533.     }
  534.  
  535.       h->plt_offset = s->_raw_size;
  536.  
  537.       /* Make room for this entry.  */
  538.       s->_raw_size += PLT_ENTRY_SIZE;
  539.  
  540.       /* We also need to make an entry in the .got.plt section, which
  541.      will be placed in the .got section by the linker script.  */
  542.  
  543.       s = bfd_get_section_by_name (dynobj, ".got.plt");
  544.       BFD_ASSERT (s != NULL);
  545.       s->_raw_size += 4;
  546.  
  547.       /* We also need to make an entry in the .rel.plt section.  */
  548.  
  549.       s = bfd_get_section_by_name (dynobj, ".rel.plt");
  550.       BFD_ASSERT (s != NULL);
  551.       s->_raw_size += sizeof (Elf32_External_Rel);
  552.  
  553.       return true;
  554.     }
  555.  
  556.   /* If this is a weak symbol, and there is a real definition, the
  557.      processor independent code will have arranged for us to see the
  558.      real definition first, and we can just use the same value.  */
  559.   if (h->weakdef != NULL)
  560.     {
  561.       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
  562.           || h->weakdef->root.type == bfd_link_hash_defweak);
  563.       h->root.u.def.section = h->weakdef->root.u.def.section;
  564.       h->root.u.def.value = h->weakdef->root.u.def.value;
  565.       return true;
  566.     }
  567.  
  568.   /* This is a reference to a symbol defined by a dynamic object which
  569.      is not a function.  */
  570.  
  571.   /* If we are creating a shared library, we must presume that the
  572.      only references to the symbol are via the global offset table.
  573.      For such cases we need not do anything here; the relocations will
  574.      be handled correctly by relocate_section.  */
  575.   if (info->shared)
  576.     return true;
  577.  
  578.   /* We must allocate the symbol in our .dynbss section, which will
  579.      become part of the .bss section of the executable.  There will be
  580.      an entry for this symbol in the .dynsym section.  The dynamic
  581.      object will contain position independent code, so all references
  582.      from the dynamic object to this symbol will go through the global
  583.      offset table.  The dynamic linker will use the .dynsym entry to
  584.      determine the address it must put in the global offset table, so
  585.      both the dynamic object and the regular object will refer to the
  586.      same memory location for the variable.  */
  587.  
  588.   s = bfd_get_section_by_name (dynobj, ".dynbss");
  589.   BFD_ASSERT (s != NULL);
  590.  
  591.   /* If the symbol is currently defined in the .bss section of the
  592.      dynamic object, then it is OK to simply initialize it to zero.
  593.      If the symbol is in some other section, we must generate a
  594.      R_386_COPY reloc to tell the dynamic linker to copy the initial
  595.      value out of the dynamic object and into the runtime process
  596.      image.  We need to remember the offset into the .rel.bss section
  597.      we are going to use.  */
  598.   if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
  599.     {
  600.       asection *srel;
  601.  
  602.       srel = bfd_get_section_by_name (dynobj, ".rel.bss");
  603.       BFD_ASSERT (srel != NULL);
  604.       srel->_raw_size += sizeof (Elf32_External_Rel);
  605.       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
  606.     }
  607.  
  608.   /* We need to figure out the alignment required for this symbol.  I
  609.      have no idea how ELF linkers handle this.  */
  610.   power_of_two = bfd_log2 (h->size);
  611.   if (power_of_two > 3)
  612.     power_of_two = 3;
  613.  
  614.   /* Apply the required alignment.  */
  615.   s->_raw_size = BFD_ALIGN (s->_raw_size,
  616.                 (bfd_size_type) (1 << power_of_two));
  617.   if (power_of_two > bfd_get_section_alignment (dynobj, s))
  618.     {
  619.       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
  620.     return false;
  621.     }
  622.  
  623.   /* Define the symbol as being at this point in the section.  */
  624.   h->root.u.def.section = s;
  625.   h->root.u.def.value = s->_raw_size;
  626.  
  627.   /* Increment the section size to make room for the symbol.  */
  628.   s->_raw_size += h->size;
  629.  
  630.   return true;
  631. }
  632.  
  633. /* Set the sizes of the dynamic sections.  */
  634.  
  635. static boolean
  636. elf_i386_size_dynamic_sections (output_bfd, info)
  637.      bfd *output_bfd;
  638.      struct bfd_link_info *info;
  639. {
  640.   bfd *dynobj;
  641.   asection *s;
  642.   boolean plt;
  643.   boolean relocs;
  644.   boolean reltext;
  645.  
  646.   dynobj = elf_hash_table (info)->dynobj;
  647.   BFD_ASSERT (dynobj != NULL);
  648.  
  649.   if (elf_hash_table (info)->dynamic_sections_created)
  650.     {
  651.       /* Set the contents of the .interp section to the interpreter.  */
  652.       if (! info->shared)
  653.     {
  654.       s = bfd_get_section_by_name (dynobj, ".interp");
  655.       BFD_ASSERT (s != NULL);
  656.       s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
  657.       s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  658.     }
  659.     }
  660.   else
  661.     {
  662.       /* We may have created entries in the .rel.got section.
  663.          However, if we are not creating the dynamic sections, we will
  664.          not actually use these entries.  Reset the size of .rel.got,
  665.          which will cause it to get stripped from the output file
  666.          below.  */
  667.       s = bfd_get_section_by_name (dynobj, ".rel.got");
  668.       if (s != NULL)
  669.     s->_raw_size = 0;
  670.     }
  671.  
  672.   /* The check_relocs and adjust_dynamic_symbol entry points have
  673.      determined the sizes of the various dynamic sections.  Allocate
  674.      memory for them.  */
  675.   plt = false;
  676.   relocs = false;
  677.   reltext = false;
  678.   for (s = dynobj->sections; s != NULL; s = s->next)
  679.     {
  680.       const char *name;
  681.       boolean strip;
  682.  
  683.       if ((s->flags & SEC_IN_MEMORY) == 0)
  684.     continue;
  685.  
  686.       /* It's OK to base decisions on the section name, because none
  687.      of the dynobj section names depend upon the input files.  */
  688.       name = bfd_get_section_name (dynobj, s);
  689.  
  690.       strip = false;
  691.  
  692.       if (strcmp (name, ".plt") == 0)
  693.     {
  694.       if (s->_raw_size == 0)
  695.         {
  696.           /* Strip this section if we don't need it; see the
  697.                  comment below.  */
  698.           strip = true;
  699.         }
  700.       else
  701.         {
  702.           /* Remember whether there is a PLT.  */
  703.           plt = true;
  704.         }
  705.     }
  706.       else if (strncmp (name, ".rel", 4) == 0)
  707.     {
  708.       if (s->_raw_size == 0)
  709.         {
  710.           /* If we don't need this section, strip it from the
  711.          output file.  This is mostly to handle .rel.bss and
  712.          .rel.plt.  We must create both sections in
  713.          create_dynamic_sections, because they must be created
  714.          before the linker maps input sections to output
  715.          sections.  The linker does that before
  716.          adjust_dynamic_symbol is called, and it is that
  717.          function which decides whether anything needs to go
  718.          into these sections.  */
  719.           strip = true;
  720.         }
  721.       else
  722.         {
  723.           asection *target;
  724.  
  725.           /* Remember whether there are any reloc sections other
  726.                  than .rel.plt.  */
  727.           if (strcmp (name, ".rel.plt") != 0)
  728.         {
  729.           relocs = true;
  730.  
  731.           /* If this relocation section applies to a read only
  732.              section, then we probably need a DT_TEXTREL
  733.              entry.  The entries in the .rel.plt section
  734.              really apply to the .got section, which we
  735.              created ourselves and so know is not readonly.  */
  736.           target = bfd_get_section_by_name (output_bfd, name + 4);
  737.           if (target != NULL
  738.               && (target->flags & SEC_READONLY) != 0)
  739.             reltext = true;
  740.         }
  741.  
  742.           /* We use the reloc_count field as a counter if we need
  743.          to copy relocs into the output file.  */
  744.           s->reloc_count = 0;
  745.         }
  746.     }
  747.       else if (strncmp (name, ".got", 4) != 0)
  748.     {
  749.       /* It's not one of our sections, so don't allocate space.  */
  750.       continue;
  751.     }
  752.  
  753.       if (strip)
  754.     {
  755.       asection **spp;
  756.  
  757.       for (spp = &s->output_section->owner->sections;
  758.            *spp != s->output_section;
  759.            spp = &(*spp)->next)
  760.         ;
  761.       *spp = s->output_section->next;
  762.       --s->output_section->owner->section_count;
  763.  
  764.       continue;
  765.     }
  766.  
  767.       /* Allocate memory for the section contents.  */
  768.       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
  769.       if (s->contents == NULL && s->_raw_size != 0)
  770.     return false;
  771.     }
  772.       
  773.   if (elf_hash_table (info)->dynamic_sections_created)
  774.     {
  775.       /* Add some entries to the .dynamic section.  We fill in the
  776.      values later, in elf_i386_finish_dynamic_sections, but we
  777.      must add the entries now so that we get the correct size for
  778.      the .dynamic section.  The DT_DEBUG entry is filled in by the
  779.      dynamic linker and used by the debugger.  */
  780.       if (! info->shared)
  781.     {
  782.       if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
  783.         return false;
  784.     }
  785.  
  786.       if (plt)
  787.     {
  788.       if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
  789.           || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
  790.           || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL)
  791.           || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
  792.         return false;
  793.     }
  794.  
  795.       if (relocs)
  796.     {
  797.       if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0)
  798.           || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0)
  799.           || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT,
  800.                         sizeof (Elf32_External_Rel)))
  801.         return false;
  802.     }
  803.  
  804.       if (reltext)
  805.     {
  806.       if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
  807.         return false;
  808.     }
  809.     }
  810.  
  811.   return true;
  812. }
  813.  
  814. /* Relocate an i386 ELF section.  */
  815.  
  816. static boolean
  817. elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
  818.                contents, relocs, local_syms, local_sections)
  819.      bfd *output_bfd;
  820.      struct bfd_link_info *info;
  821.      bfd *input_bfd;
  822.      asection *input_section;
  823.      bfd_byte *contents;
  824.      Elf_Internal_Rela *relocs;
  825.      Elf_Internal_Sym *local_syms;
  826.      asection **local_sections;
  827. {
  828.   bfd *dynobj;
  829.   Elf_Internal_Shdr *symtab_hdr;
  830.   struct elf_link_hash_entry **sym_hashes;
  831.   bfd_vma *local_got_offsets;
  832.   asection *sgot;
  833.   asection *splt;
  834.   asection *sreloc;
  835.   Elf_Internal_Rela *rel;
  836.   Elf_Internal_Rela *relend;
  837.  
  838.   dynobj = elf_hash_table (info)->dynobj;
  839.   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  840.   sym_hashes = elf_sym_hashes (input_bfd);
  841.   local_got_offsets = elf_local_got_offsets (input_bfd);
  842.  
  843.   sgot = NULL;
  844.   splt = NULL;
  845.   sreloc = NULL;
  846.  
  847.   rel = relocs;
  848.   relend = relocs + input_section->reloc_count;
  849.   for (; rel < relend; rel++)
  850.     {
  851.       int r_type;
  852.       reloc_howto_type *howto;
  853.       unsigned long r_symndx;
  854.       struct elf_link_hash_entry *h;
  855.       Elf_Internal_Sym *sym;
  856.       asection *sec;
  857.       bfd_vma relocation;
  858.       bfd_reloc_status_type r;
  859.  
  860.       r_type = ELF32_R_TYPE (rel->r_info);
  861.       if (r_type < 0 || r_type >= (int) R_386_max)
  862.     {
  863.       bfd_set_error (bfd_error_bad_value);
  864.       return false;
  865.     }
  866.       howto = elf_howto_table + r_type;
  867.  
  868.       r_symndx = ELF32_R_SYM (rel->r_info);
  869.  
  870.       if (info->relocateable)
  871.     {
  872.       /* This is a relocateable link.  We don't have to change
  873.          anything, unless the reloc is against a section symbol,
  874.          in which case we have to adjust according to where the
  875.          section symbol winds up in the output section.  */
  876.       if (r_symndx < symtab_hdr->sh_info)
  877.         {
  878.           sym = local_syms + r_symndx;
  879.           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  880.         {
  881.           bfd_vma val;
  882.  
  883.           sec = local_sections[r_symndx];
  884.           val = bfd_get_32 (input_bfd, contents + rel->r_offset);
  885.           val += sec->output_offset + sym->st_value;
  886.           bfd_put_32 (input_bfd, val, contents + rel->r_offset);
  887.         }
  888.         }
  889.  
  890.       continue;
  891.     }
  892.  
  893.       /* This is a final link.  */
  894.       h = NULL;
  895.       sym = NULL;
  896.       sec = NULL;
  897.       if (r_symndx < symtab_hdr->sh_info)
  898.     {
  899.       sym = local_syms + r_symndx;
  900.       sec = local_sections[r_symndx];
  901.       relocation = (sec->output_section->vma
  902.             + sec->output_offset
  903.             + sym->st_value);
  904.     }
  905.       else
  906.     {
  907.       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  908.       while (h->root.type == bfd_link_hash_indirect
  909.          || h->root.type == bfd_link_hash_warning)
  910.         h = (struct elf_link_hash_entry *) h->root.u.i.link;
  911.       if (h->root.type == bfd_link_hash_defined
  912.           || h->root.type == bfd_link_hash_defweak)
  913.         {
  914.           sec = h->root.u.def.section;
  915.           if (r_type == R_386_GOTPC
  916.           || (r_type == R_386_PLT32
  917.               && h->plt_offset != (bfd_vma) -1)
  918.           || (r_type == R_386_GOT32
  919.               && elf_hash_table (info)->dynamic_sections_created
  920.               && (! info->shared
  921.               || ! info->symbolic
  922.               || (h->elf_link_hash_flags
  923.                   & ELF_LINK_HASH_DEF_REGULAR) == 0))
  924.           || (info->shared
  925.               && (! info->symbolic
  926.               || (h->elf_link_hash_flags
  927.                   & ELF_LINK_HASH_DEF_REGULAR) == 0)
  928.               && (r_type == R_386_32
  929.               || r_type == R_386_PC32)
  930.               && (input_section->flags & SEC_ALLOC) != 0))
  931.         {
  932.           /* In these cases, we don't need the relocation
  933.                      value.  We check specially because in some
  934.                      obscure cases sec->output_section will be NULL.  */
  935.           relocation = 0;
  936.         }
  937.           else
  938.         relocation = (h->root.u.def.value
  939.                   + sec->output_section->vma
  940.                   + sec->output_offset);
  941.         }
  942.       else if (h->root.type == bfd_link_hash_undefweak)
  943.         relocation = 0;
  944.       else if (info->shared && !info->symbolic)
  945.         relocation = 0;
  946.       else
  947.         {
  948.           if (! ((*info->callbacks->undefined_symbol)
  949.              (info, h->root.root.string, input_bfd,
  950.               input_section, rel->r_offset)))
  951.         return false;
  952.           relocation = 0;
  953.         }
  954.     }
  955.  
  956.       switch (r_type)
  957.     {
  958.     case R_386_GOT32:
  959.       /* Relocation is to the entry for this symbol in the global
  960.          offset table.  */
  961.       if (sgot == NULL)
  962.         {
  963.           sgot = bfd_get_section_by_name (dynobj, ".got");
  964.           BFD_ASSERT (sgot != NULL);
  965.         }
  966.  
  967.       if (h != NULL)
  968.         {
  969.           bfd_vma off;
  970.  
  971.           off = h->got_offset;
  972.           BFD_ASSERT (off != (bfd_vma) -1);
  973.  
  974.           if (! elf_hash_table (info)->dynamic_sections_created
  975.           || (info->shared
  976.               && info->symbolic
  977.               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
  978.         {
  979.           /* This is actually a static link, or it is a
  980.              -Bsymbolic link and the symbol is defined
  981.              locally.  We must initialize this entry in the
  982.              global offset table.  Since the offset must
  983.              always be a multiple of 4, we use the least
  984.              significant bit to record whether we have
  985.              initialized it already.
  986.  
  987.              When doing a dynamic link, we create a .rel.got
  988.              relocation entry to initialize the value.  This
  989.              is done in the finish_dynamic_symbol routine.  */
  990.           if ((off & 1) != 0)
  991.             off &= ~1;
  992.           else
  993.             {
  994.               bfd_put_32 (output_bfd, relocation,
  995.                   sgot->contents + off);
  996.               h->got_offset |= 1;
  997.             }
  998.         }
  999.  
  1000.           relocation = sgot->output_offset + off;
  1001.         }
  1002.       else
  1003.         {
  1004.           bfd_vma off;
  1005.  
  1006.           BFD_ASSERT (local_got_offsets != NULL
  1007.               && local_got_offsets[r_symndx] != (bfd_vma) -1);
  1008.  
  1009.           off = local_got_offsets[r_symndx];
  1010.  
  1011.           /* The offset must always be a multiple of 4.  We use
  1012.                  the least significant bit to record whether we have
  1013.                  already generated the necessary reloc.  */
  1014.           if ((off & 1) != 0)
  1015.         off &= ~1;
  1016.           else
  1017.         {
  1018.           bfd_put_32 (output_bfd, relocation, sgot->contents + off);
  1019.  
  1020.           if (info->shared)
  1021.             {
  1022.               asection *srelgot;
  1023.               Elf_Internal_Rel outrel;
  1024.  
  1025.               srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
  1026.               BFD_ASSERT (srelgot != NULL);
  1027.  
  1028.               outrel.r_offset = (sgot->output_section->vma
  1029.                      + sgot->output_offset
  1030.                      + off);
  1031.               outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
  1032.               bfd_elf32_swap_reloc_out (output_bfd, &outrel,
  1033.                         (((Elf32_External_Rel *)
  1034.                           srelgot->contents)
  1035.                          + srelgot->reloc_count));
  1036.               ++srelgot->reloc_count;
  1037.             }
  1038.  
  1039.           local_got_offsets[r_symndx] |= 1;
  1040.         }
  1041.  
  1042.           relocation = sgot->output_offset + off;
  1043.         }
  1044.  
  1045.       break;
  1046.  
  1047.     case R_386_GOTOFF:
  1048.       /* Relocation is relative to the start of the global offset
  1049.          table.  */
  1050.  
  1051.       if (sgot == NULL)
  1052.         {
  1053.           sgot = bfd_get_section_by_name (dynobj, ".got");
  1054.           BFD_ASSERT (sgot != NULL);
  1055.         }
  1056.  
  1057.       /* Note that sgot->output_offset is not involved in this
  1058.          calculation.  We always want the start of .got.  If we
  1059.          defined _GLOBAL_OFFSET_TABLE in a different way, as is
  1060.          permitted by the ABI, we might have to change this
  1061.          calculation.  */
  1062.       relocation -= sgot->output_section->vma;
  1063.  
  1064.       break;
  1065.  
  1066.     case R_386_GOTPC:
  1067.       /* Use global offset table as symbol value.  */
  1068.  
  1069.       if (sgot == NULL)
  1070.         {
  1071.           sgot = bfd_get_section_by_name (dynobj, ".got");
  1072.           BFD_ASSERT (sgot != NULL);
  1073.         }
  1074.  
  1075.       relocation = sgot->output_section->vma;
  1076.  
  1077.       break;
  1078.  
  1079.     case R_386_PLT32:
  1080.       /* Relocation is to the entry for this symbol in the
  1081.          procedure linkage table.  */
  1082.  
  1083.       /* Resolve a PLT32 reloc again a local symbol directly,
  1084.              without using the procedure linkage table.  */
  1085.       if (h == NULL)
  1086.         break;
  1087.  
  1088.       if (h->plt_offset == (bfd_vma) -1)
  1089.         {
  1090.           /* We didn't make a PLT entry for this symbol.  This
  1091.                  happens when statically linking PIC code, or when
  1092.                  using -Bsymbolic.  */
  1093.           break;
  1094.         }
  1095.  
  1096.       if (splt == NULL)
  1097.         {
  1098.           splt = bfd_get_section_by_name (dynobj, ".plt");
  1099.           BFD_ASSERT (splt != NULL);
  1100.         }
  1101.  
  1102.       relocation = (splt->output_section->vma
  1103.             + splt->output_offset
  1104.             + h->plt_offset);
  1105.  
  1106.       break;
  1107.  
  1108.     case R_386_32:
  1109.     case R_386_PC32:
  1110.       if (info->shared
  1111.           && (input_section->flags & SEC_ALLOC) != 0
  1112.           && (r_type != R_386_PC32
  1113.           || (h != NULL
  1114.               && (! info->symbolic
  1115.               || (h->elf_link_hash_flags
  1116.                   & ELF_LINK_HASH_DEF_REGULAR) == 0))))
  1117.         {
  1118.           Elf_Internal_Rel outrel;
  1119.           boolean relocate;
  1120.  
  1121.           /* When generating a shared object, these relocations
  1122.          are copied into the output file to be resolved at run
  1123.          time.  */
  1124.  
  1125.           if (sreloc == NULL)
  1126.         {
  1127.           const char *name;
  1128.  
  1129.           name = (bfd_elf_string_from_elf_section
  1130.               (input_bfd,
  1131.                elf_elfheader (input_bfd)->e_shstrndx,
  1132.                elf_section_data (input_section)->rel_hdr.sh_name));
  1133.           if (name == NULL)
  1134.             return false;
  1135.  
  1136.           BFD_ASSERT (strncmp (name, ".rel", 4) == 0
  1137.                   && strcmp (bfd_get_section_name (input_bfd,
  1138.                                    input_section),
  1139.                      name + 4) == 0);
  1140.  
  1141.           sreloc = bfd_get_section_by_name (dynobj, name);
  1142.           BFD_ASSERT (sreloc != NULL);
  1143.         }
  1144.  
  1145.           outrel.r_offset = (rel->r_offset
  1146.                  + input_section->output_section->vma
  1147.                  + input_section->output_offset);
  1148.           if (r_type == R_386_PC32)
  1149.         {
  1150.           BFD_ASSERT (h != NULL && h->dynindx != -1);
  1151.           relocate = false;
  1152.           outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_PC32);
  1153.         }
  1154.           else
  1155.         {
  1156.           if (h == NULL
  1157.               || (info->symbolic
  1158.               && (h->elf_link_hash_flags
  1159.                   & ELF_LINK_HASH_DEF_REGULAR) != 0))
  1160.             {
  1161.               relocate = true;
  1162.               outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
  1163.             }
  1164.           else
  1165.             {
  1166.               BFD_ASSERT (h->dynindx != -1);
  1167.               relocate = false;
  1168.               outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_32);
  1169.             }
  1170.         }
  1171.  
  1172.           bfd_elf32_swap_reloc_out (output_bfd, &outrel,
  1173.                     (((Elf32_External_Rel *)
  1174.                       sreloc->contents)
  1175.                      + sreloc->reloc_count));
  1176.           ++sreloc->reloc_count;
  1177.  
  1178.           /* If this reloc is against an external symbol, we do
  1179.          not want to fiddle with the addend.  Otherwise, we
  1180.          need to include the symbol value so that it becomes
  1181.          an addend for the dynamic reloc.  */
  1182.           if (! relocate)
  1183.         continue;
  1184.         }
  1185.  
  1186.       break;
  1187.  
  1188.     default:
  1189.       break;
  1190.     }
  1191.  
  1192.       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  1193.                     contents, rel->r_offset,
  1194.                     relocation, (bfd_vma) 0);
  1195.  
  1196.       if (r != bfd_reloc_ok)
  1197.     {
  1198.       switch (r)
  1199.         {
  1200.         default:
  1201.         case bfd_reloc_outofrange:
  1202.           abort ();
  1203.         case bfd_reloc_overflow:
  1204.           {
  1205.         const char *name;
  1206.  
  1207.         if (h != NULL)
  1208.           name = h->root.root.string;
  1209.         else
  1210.           {
  1211.             name = bfd_elf_string_from_elf_section (input_bfd,
  1212.                                 symtab_hdr->sh_link,
  1213.                                 sym->st_name);
  1214.             if (name == NULL)
  1215.               return false;
  1216.             if (*name == '\0')
  1217.               name = bfd_section_name (input_bfd, sec);
  1218.           }
  1219.         if (! ((*info->callbacks->reloc_overflow)
  1220.                (info, name, howto->name, (bfd_vma) 0,
  1221.             input_bfd, input_section, rel->r_offset)))
  1222.           return false;
  1223.           }
  1224.           break;
  1225.         }
  1226.     }
  1227.     }
  1228.  
  1229.   return true;
  1230. }
  1231.  
  1232. /* Finish up dynamic symbol handling.  We set the contents of various
  1233.    dynamic sections here.  */
  1234.  
  1235. static boolean
  1236. elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
  1237.      bfd *output_bfd;
  1238.      struct bfd_link_info *info;
  1239.      struct elf_link_hash_entry *h;
  1240.      Elf_Internal_Sym *sym;
  1241. {
  1242.   bfd *dynobj;
  1243.  
  1244.   dynobj = elf_hash_table (info)->dynobj;
  1245.  
  1246.   if (h->plt_offset != (bfd_vma) -1)
  1247.     {
  1248.       asection *splt;
  1249.       asection *sgot;
  1250.       asection *srel;
  1251.       bfd_vma plt_index;
  1252.       bfd_vma got_offset;
  1253.       Elf_Internal_Rel rel;
  1254.  
  1255.       /* This symbol has an entry in the procedure linkage table.  Set
  1256.      it up.  */
  1257.  
  1258.       BFD_ASSERT (h->dynindx != -1);
  1259.  
  1260.       splt = bfd_get_section_by_name (dynobj, ".plt");
  1261.       sgot = bfd_get_section_by_name (dynobj, ".got.plt");
  1262.       srel = bfd_get_section_by_name (dynobj, ".rel.plt");
  1263.       BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
  1264.  
  1265.       /* Get the index in the procedure linkage table which
  1266.      corresponds to this symbol.  This is the index of this symbol
  1267.      in all the symbols for which we are making plt entries.  The
  1268.      first entry in the procedure linkage table is reserved.  */
  1269.       plt_index = h->plt_offset / PLT_ENTRY_SIZE - 1;
  1270.  
  1271.       /* Get the offset into the .got table of the entry that
  1272.      corresponds to this function.  Each .got entry is 4 bytes.
  1273.      The first three are reserved.  */
  1274.       got_offset = (plt_index + 3) * 4;
  1275.  
  1276.       /* Fill in the entry in the procedure linkage table.  */
  1277.       if (! info->shared)
  1278.     {
  1279.       memcpy (splt->contents + h->plt_offset, elf_i386_plt_entry,
  1280.           PLT_ENTRY_SIZE);
  1281.       bfd_put_32 (output_bfd,
  1282.               (sgot->output_section->vma
  1283.                + sgot->output_offset
  1284.                + got_offset),
  1285.               splt->contents + h->plt_offset + 2);
  1286.     }
  1287.       else
  1288.     {
  1289.       memcpy (splt->contents + h->plt_offset, elf_i386_pic_plt_entry,
  1290.           PLT_ENTRY_SIZE);
  1291.       bfd_put_32 (output_bfd, got_offset,
  1292.               splt->contents + h->plt_offset + 2);
  1293.     }
  1294.  
  1295.       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
  1296.           splt->contents + h->plt_offset + 7);
  1297.       bfd_put_32 (output_bfd, - (h->plt_offset + PLT_ENTRY_SIZE),
  1298.           splt->contents + h->plt_offset + 12);
  1299.  
  1300.       /* Fill in the entry in the global offset table.  */
  1301.       bfd_put_32 (output_bfd,
  1302.           (splt->output_section->vma
  1303.            + splt->output_offset
  1304.            + h->plt_offset
  1305.            + 6),
  1306.           sgot->contents + got_offset);
  1307.  
  1308.       /* Fill in the entry in the .rel.plt section.  */
  1309.       rel.r_offset = (sgot->output_section->vma
  1310.               + sgot->output_offset
  1311.               + got_offset);
  1312.       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
  1313.       bfd_elf32_swap_reloc_out (output_bfd, &rel,
  1314.                 ((Elf32_External_Rel *) srel->contents
  1315.                  + plt_index));
  1316.  
  1317.       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
  1318.     {
  1319.       /* Mark the symbol as undefined, rather than as defined in
  1320.          the .plt section.  Leave the value alone.  */
  1321.       sym->st_shndx = SHN_UNDEF;
  1322.     }
  1323.     }
  1324.  
  1325.   if (h->got_offset != (bfd_vma) -1)
  1326.     {
  1327.       asection *sgot;
  1328.       asection *srel;
  1329.       Elf_Internal_Rel rel;
  1330.  
  1331.       /* This symbol has an entry in the global offset table.  Set it
  1332.      up.  */
  1333.       
  1334.       BFD_ASSERT (h->dynindx != -1);
  1335.  
  1336.       sgot = bfd_get_section_by_name (dynobj, ".got");
  1337.       srel = bfd_get_section_by_name (dynobj, ".rel.got");
  1338.       BFD_ASSERT (sgot != NULL && srel != NULL);
  1339.  
  1340.       rel.r_offset = (sgot->output_section->vma
  1341.               + sgot->output_offset
  1342.               + (h->got_offset &~ 1));
  1343.  
  1344.       /* If this is a -Bsymbolic link, and the symbol is defined
  1345.      locally, we just want to emit a RELATIVE reloc.  The entry in
  1346.      the global offset table will already have been initialized in
  1347.      the relocate_section function.  */
  1348.       if (info->shared
  1349.       && info->symbolic
  1350.       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
  1351.     rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
  1352.       else
  1353.     {
  1354.       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
  1355.       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
  1356.     }
  1357.  
  1358.       bfd_elf32_swap_reloc_out (output_bfd, &rel,
  1359.                 ((Elf32_External_Rel *) srel->contents
  1360.                  + srel->reloc_count));
  1361.       ++srel->reloc_count;
  1362.     }
  1363.  
  1364.   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
  1365.     {
  1366.       asection *s;
  1367.       Elf_Internal_Rel rel;
  1368.  
  1369.       /* This symbol needs a copy reloc.  Set it up.  */
  1370.  
  1371.       BFD_ASSERT (h->dynindx != -1
  1372.           && (h->root.type == bfd_link_hash_defined
  1373.               || h->root.type == bfd_link_hash_defweak));
  1374.  
  1375.       s = bfd_get_section_by_name (h->root.u.def.section->owner,
  1376.                    ".rel.bss");
  1377.       BFD_ASSERT (s != NULL);
  1378.  
  1379.       rel.r_offset = (h->root.u.def.value
  1380.               + h->root.u.def.section->output_section->vma
  1381.               + h->root.u.def.section->output_offset);
  1382.       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
  1383.       bfd_elf32_swap_reloc_out (output_bfd, &rel,
  1384.                 ((Elf32_External_Rel *) s->contents
  1385.                  + s->reloc_count));
  1386.       ++s->reloc_count;
  1387.     }
  1388.  
  1389.   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
  1390.   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
  1391.       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  1392.     sym->st_shndx = SHN_ABS;
  1393.  
  1394.   return true;
  1395. }
  1396.  
  1397. /* Finish up the dynamic sections.  */
  1398.  
  1399. static boolean
  1400. elf_i386_finish_dynamic_sections (output_bfd, info)
  1401.      bfd *output_bfd;
  1402.      struct bfd_link_info *info;
  1403. {
  1404.   bfd *dynobj;
  1405.   asection *sgot;
  1406.   asection *sdyn;
  1407.  
  1408.   dynobj = elf_hash_table (info)->dynobj;
  1409.  
  1410.   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
  1411.   BFD_ASSERT (sgot != NULL);
  1412.   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
  1413.  
  1414.   if (elf_hash_table (info)->dynamic_sections_created)
  1415.     {
  1416.       asection *splt;
  1417.       Elf32_External_Dyn *dyncon, *dynconend;
  1418.  
  1419.       splt = bfd_get_section_by_name (dynobj, ".plt");
  1420.       BFD_ASSERT (splt != NULL && sdyn != NULL);
  1421.  
  1422.       dyncon = (Elf32_External_Dyn *) sdyn->contents;
  1423.       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
  1424.       for (; dyncon < dynconend; dyncon++)
  1425.     {
  1426.       Elf_Internal_Dyn dyn;
  1427.       const char *name;
  1428.       asection *s;
  1429.  
  1430.       bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  1431.  
  1432.       switch (dyn.d_tag)
  1433.         {
  1434.         default:
  1435.           break;
  1436.  
  1437.         case DT_PLTGOT:
  1438.           name = ".got";
  1439.           goto get_vma;
  1440.         case DT_JMPREL:
  1441.           name = ".rel.plt";
  1442.         get_vma:
  1443.           s = bfd_get_section_by_name (output_bfd, name);
  1444.           BFD_ASSERT (s != NULL);
  1445.           dyn.d_un.d_ptr = s->vma;
  1446.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  1447.           break;
  1448.  
  1449.         case DT_PLTRELSZ:
  1450.           s = bfd_get_section_by_name (output_bfd, ".rel.plt");
  1451.           BFD_ASSERT (s != NULL);
  1452.           if (s->_cooked_size != 0)
  1453.         dyn.d_un.d_val = s->_cooked_size;
  1454.           else
  1455.         dyn.d_un.d_val = s->_raw_size;
  1456.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  1457.           break;
  1458.  
  1459.         case DT_RELSZ:
  1460.           /* My reading of the SVR4 ABI indicates that the
  1461.          procedure linkage table relocs (DT_JMPREL) should be
  1462.          included in the overall relocs (DT_REL).  This is
  1463.          what Solaris does.  However, UnixWare can not handle
  1464.          that case.  Therefore, we override the DT_RELSZ entry
  1465.          here to make it not include the JMPREL relocs.  Since
  1466.          the linker script arranges for .rel.plt to follow all
  1467.          other relocation sections, we don't have to worry
  1468.          about changing the DT_REL entry.  */
  1469.           s = bfd_get_section_by_name (output_bfd, ".rel.plt");
  1470.           if (s != NULL)
  1471.         {
  1472.           if (s->_cooked_size != 0)
  1473.             dyn.d_un.d_val -= s->_cooked_size;
  1474.           else
  1475.             dyn.d_un.d_val -= s->_raw_size;
  1476.         }
  1477.           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  1478.           break;
  1479.         }
  1480.     }
  1481.  
  1482.       /* Fill in the first entry in the procedure linkage table.  */
  1483.       if (splt->_raw_size > 0)
  1484.     {
  1485.       if (info->shared)
  1486.         memcpy (splt->contents, elf_i386_pic_plt0_entry, PLT_ENTRY_SIZE);
  1487.       else
  1488.         {
  1489.           memcpy (splt->contents, elf_i386_plt0_entry, PLT_ENTRY_SIZE);
  1490.           bfd_put_32 (output_bfd,
  1491.               sgot->output_section->vma + sgot->output_offset + 4,
  1492.               splt->contents + 2);
  1493.           bfd_put_32 (output_bfd,
  1494.               sgot->output_section->vma + sgot->output_offset + 8,
  1495.               splt->contents + 8);
  1496.         }
  1497.     }
  1498.  
  1499.       /* UnixWare sets the entsize of .plt to 4, although that doesn't
  1500.      really seem like the right value.  */
  1501.       elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
  1502.     }
  1503.  
  1504.   /* Fill in the first three entries in the global offset table.  */
  1505.   if (sgot->_raw_size > 0)
  1506.     {
  1507.       if (sdyn == NULL)
  1508.     bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
  1509.       else
  1510.     bfd_put_32 (output_bfd,
  1511.             sdyn->output_section->vma + sdyn->output_offset,
  1512.             sgot->contents);
  1513.       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
  1514.       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
  1515.     }
  1516.  
  1517.   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
  1518.  
  1519.   return true;
  1520. }
  1521.  
  1522. #define TARGET_LITTLE_SYM        bfd_elf32_i386_vec
  1523. #define TARGET_LITTLE_NAME        "elf32-i386"
  1524. #define ELF_ARCH            bfd_arch_i386
  1525. #define ELF_MACHINE_CODE        EM_386
  1526. #define elf_info_to_howto        elf_i386_info_to_howto
  1527. #define elf_info_to_howto_rel        elf_i386_info_to_howto_rel
  1528. #define bfd_elf32_bfd_reloc_type_lookup    elf_i386_reloc_type_lookup
  1529. #define ELF_MAXPAGESIZE            0x1000
  1530. #define elf_backend_create_dynamic_sections \
  1531.                     _bfd_elf_create_dynamic_sections
  1532. #define elf_backend_check_relocs    elf_i386_check_relocs
  1533. #define elf_backend_adjust_dynamic_symbol \
  1534.                     elf_i386_adjust_dynamic_symbol
  1535. #define elf_backend_size_dynamic_sections \
  1536.                     elf_i386_size_dynamic_sections
  1537. #define elf_backend_relocate_section    elf_i386_relocate_section
  1538. #define elf_backend_finish_dynamic_symbol \
  1539.                     elf_i386_finish_dynamic_symbol
  1540. #define elf_backend_finish_dynamic_sections \
  1541.                     elf_i386_finish_dynamic_sections
  1542. #define elf_backend_want_got_plt 1
  1543. #define elf_backend_plt_readonly 1
  1544. #define elf_backend_want_plt_sym 0
  1545.  
  1546. #include "elf32-target.h"
  1547.