home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / bfd / elf32-i386.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-26  |  29.2 KB  |  932 lines

  1. /* Intel 80386/80486-specific support for 32-bit ELF
  2.    Copyright 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "bfd.h"
  21. #include "sysdep.h"
  22. #include "bfdlink.h"
  23. #include "libbfd.h"
  24. #include "libelf.h"
  25.  
  26. static CONST struct reloc_howto_struct *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_create_dynamic_sections
  33.   PARAMS ((bfd *, struct bfd_link_info *));
  34. static boolean elf_i386_adjust_dynamic_symbol
  35.   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
  36. static boolean elf_i386_allocate_dynamic_section
  37.   PARAMS ((bfd *, const char *));
  38. static boolean elf_i386_size_dynamic_sections
  39.   PARAMS ((bfd *, struct bfd_link_info *));
  40. static boolean elf_i386_relocate_section
  41.   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  42.        Elf_Internal_Rela *, Elf_Internal_Sym *, asection **, char *));
  43. static boolean elf_i386_finish_dynamic_symbol
  44.   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
  45.        Elf_Internal_Sym *));
  46. static boolean elf_i386_finish_dynamic_sections
  47.   PARAMS ((bfd *, struct bfd_link_info *));
  48.  
  49. #define USE_REL    1        /* 386 uses REL relocations instead of RELA */
  50.  
  51. enum reloc_type
  52.   {
  53.     R_386_NONE = 0,
  54.     R_386_32,
  55.     R_386_PC32,
  56.     R_386_GOT32,
  57.     R_386_PLT32,
  58.     R_386_COPY,
  59.     R_386_GLOB_DAT,
  60.     R_386_JUMP_SLOT,
  61.     R_386_RELATIVE,
  62.     R_386_GOTOFF,
  63.     R_386_GOTPC,
  64.     R_386_max
  65.   };
  66.  
  67. #if 0
  68. static CONST char *CONST reloc_type_names[] =
  69. {
  70.   "R_386_NONE",
  71.   "R_386_32",
  72.   "R_386_PC32",
  73.   "R_386_GOT32",
  74.   "R_386_PLT32",
  75.   "R_386_COPY",
  76.   "R_386_GLOB_DAT",
  77.   "R_386_JUMP_SLOT",
  78.   "R_386_RELATIVE",
  79.   "R_386_GOTOFF",
  80.   "R_386_GOTPC",
  81. };
  82. #endif
  83.  
  84. static reloc_howto_type elf_howto_table[]=
  85. {
  86.   HOWTO(R_386_NONE,     0,0, 0,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_NONE",        true,0x00000000,0x00000000,false),
  87.   HOWTO(R_386_32,     0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_32",        true,0xffffffff,0xffffffff,false),
  88.   HOWTO(R_386_PC32,     0,2,32,true, 0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PC32",        true,0xffffffff,0xffffffff,true),
  89.   HOWTO(R_386_GOT32,     0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOT32",    true,0xffffffff,0xffffffff,false),
  90.   HOWTO(R_386_PLT32,     0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PLT32",    true,0xffffffff,0xffffffff,false),
  91.   HOWTO(R_386_COPY,      0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_COPY",        true,0xffffffff,0xffffffff,false),
  92.   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),
  93.   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),
  94.   HOWTO(R_386_RELATIVE,  0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_RELATIVE", true,0xffffffff,0xffffffff,false),
  95.   HOWTO(R_386_GOTOFF,    0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTOFF",   true,0xffffffff,0xffffffff,false),
  96.   HOWTO(R_386_GOTPC,     0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTPC",    true,0xffffffff,0xffffffff,false),
  97. };
  98.  
  99. #ifdef DEBUG_GEN_RELOC
  100. #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
  101. #else
  102. #define TRACE(str)
  103. #endif
  104.  
  105. static CONST struct reloc_howto_struct *
  106. elf_i386_reloc_type_lookup (abfd, code)
  107.      bfd *abfd;
  108.      bfd_reloc_code_real_type code;
  109. {
  110.   switch (code)
  111.     {
  112.     case BFD_RELOC_NONE:
  113.       TRACE ("BFD_RELOC_NONE");
  114.       return &elf_howto_table[ (int)R_386_NONE ];
  115.  
  116.     case BFD_RELOC_32:
  117.       TRACE ("BFD_RELOC_32");
  118.       return &elf_howto_table[ (int)R_386_32 ];
  119.  
  120.     case BFD_RELOC_32_PCREL:
  121.       TRACE ("BFD_RELOC_PC32");
  122.       return &elf_howto_table[ (int)R_386_PC32 ];
  123.  
  124.     case BFD_RELOC_386_GOT32:
  125.       TRACE ("BFD_RELOC_386_GOT32");
  126.       return &elf_howto_table[ (int)R_386_GOT32 ];
  127.  
  128.     case BFD_RELOC_386_PLT32:
  129.       TRACE ("BFD_RELOC_386_PLT32");
  130.       return &elf_howto_table[ (int)R_386_PLT32 ];
  131.  
  132.     case BFD_RELOC_386_COPY:
  133.       TRACE ("BFD_RELOC_386_COPY");
  134.       return &elf_howto_table[ (int)R_386_COPY ];
  135.  
  136.     case BFD_RELOC_386_GLOB_DAT:
  137.       TRACE ("BFD_RELOC_386_GLOB_DAT");
  138.       return &elf_howto_table[ (int)R_386_GLOB_DAT ];
  139.  
  140.     case BFD_RELOC_386_JUMP_SLOT:
  141.       TRACE ("BFD_RELOC_386_JUMP_SLOT");
  142.       return &elf_howto_table[ (int)R_386_JUMP_SLOT ];
  143.  
  144.     case BFD_RELOC_386_RELATIVE:
  145.       TRACE ("BFD_RELOC_386_RELATIVE");
  146.       return &elf_howto_table[ (int)R_386_RELATIVE ];
  147.  
  148.     case BFD_RELOC_386_GOTOFF:
  149.       TRACE ("BFD_RELOC_386_GOTOFF");
  150.       return &elf_howto_table[ (int)R_386_GOTOFF ];
  151.  
  152.     case BFD_RELOC_386_GOTPC:
  153.       TRACE ("BFD_RELOC_386_GOTPC");
  154.       return &elf_howto_table[ (int)R_386_GOTPC ];
  155.  
  156.     default:
  157.       break;
  158.     }
  159.  
  160.   TRACE ("Unknown");
  161.   return 0;
  162. }
  163.  
  164. static void
  165. elf_i386_info_to_howto (abfd, cache_ptr, dst)
  166.      bfd        *abfd;
  167.      arelent        *cache_ptr;
  168.      Elf32_Internal_Rela *dst;
  169. {
  170.   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max);
  171.  
  172.   cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
  173. }
  174.  
  175. static void
  176. elf_i386_info_to_howto_rel (abfd, cache_ptr, dst)
  177.      bfd        *abfd;
  178.      arelent        *cache_ptr;
  179.      Elf32_Internal_Rel *dst;
  180. {
  181.   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max);
  182.  
  183.   cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)];
  184. }
  185.  
  186. /* Functions for the i386 ELF linker.  */
  187.  
  188. /* The name of the dynamic interpreter.  This is put in the .interp
  189.    section.  */
  190.  
  191. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
  192.  
  193. /* The size in bytes of an entry in the procedure linkage table.  */
  194.  
  195. #define PLT_ENTRY_SIZE 16
  196.  
  197. /* The first entry in an absolute procedure linkage table looks like
  198.    this.  See the SVR4 ABI i386 supplement to see how this works.  */
  199.  
  200. static bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] =
  201. {
  202.   0xff, 0x35,    /* pushl contents of address */
  203.   0, 0, 0, 0,    /* replaced with address of .got + 4.  */
  204.   0xff, 0x25,    /* jmp indirect */
  205.   0, 0, 0, 0,    /* replaced with address of .got + 8.  */
  206.   0, 0, 0, 0    /* pad out to 16 bytes.  */
  207. };
  208.  
  209. /* Subsequent entries in an absolute procedure linkage table look like
  210.    this.  */
  211.  
  212. static bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
  213. {
  214.   0xff, 0x25,    /* jmp indirect */
  215.   0, 0, 0, 0,    /* replaced with address of this symbol in .got.  */
  216.   0x68,        /* pushl immediate */
  217.   0, 0, 0, 0,    /* replaced with offset into relocation table.  */
  218.   0xe9,        /* jmp relative */
  219.   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
  220. };
  221.  
  222. /* Create dynamic sections when linking against a dynamic object.  */
  223.  
  224. static boolean
  225. elf_i386_create_dynamic_sections (abfd, info)
  226.      bfd *abfd;
  227.      struct bfd_link_info *info;
  228. {
  229.   flagword flags;
  230.   register asection *s;
  231.   struct elf_link_hash_entry *h;
  232.  
  233.   /* We need to create .plt, .rel.plt, .got, .dynbss, and .rel.bss
  234.      sections.  */
  235.  
  236.   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
  237.  
  238.   s = bfd_make_section (abfd, ".plt");
  239.   if (s == NULL
  240.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY | SEC_CODE)
  241.       || ! bfd_set_section_alignment (abfd, s, 2))
  242.     return false;
  243.  
  244.   s = bfd_make_section (abfd, ".rel.plt");
  245.   if (s == NULL
  246.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
  247.       || ! bfd_set_section_alignment (abfd, s, 2))
  248.     return false;
  249.  
  250.   s = bfd_make_section (abfd, ".got");
  251.   if (s == NULL
  252.       || ! bfd_set_section_flags (abfd, s, flags)
  253.       || ! bfd_set_section_alignment (abfd, s, 2))
  254.     return false;
  255.  
  256.   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
  257.      section.  We don't do this in the linker script because we don't
  258.      want to define the symbol if we are not creating a global offset
  259.      table.  */
  260.   h = NULL;
  261.   if (! (_bfd_generic_link_add_one_symbol
  262.      (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0,
  263.       (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
  264.       (struct bfd_link_hash_entry **) &h)))
  265.     return false;
  266.   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  267.  
  268.   /* The first three global offset table entries are reserved.  */
  269.   s->_raw_size += 3 * 4;
  270.  
  271.   /* The .dynbss section is a place to put symbols which are defined
  272.      by dynamic objects, are referenced by regular objects, and are
  273.      not functions.  We must allocate space for them in the process
  274.      image and use a R_386_COPY reloc to tell the dynamic linker to
  275.      initialize them at run time.  The linker script puts the .dynbss
  276.      section into the .bss section of the final image.  */
  277.   s = bfd_make_section (abfd, ".dynbss");
  278.   if (s == NULL
  279.       || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
  280.     return false;
  281.  
  282.   /* The .rel.bss section holds copy relocs.  This section is not
  283.      normally needed.  We need to create it here, though, so that the
  284.      linker will map it to an output section.  If it turns out not to
  285.      be needed, we can discard it later.  */
  286.   s = bfd_make_section (abfd, ".rel.bss");
  287.   if (s == NULL
  288.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
  289.       || ! bfd_set_section_alignment (abfd, s, 2))
  290.     return false;
  291.  
  292.   return true;
  293. }
  294.  
  295. /* Adjust a symbol defined by a dynamic object and referenced by a
  296.    regular object.  The current definition is in some section of the
  297.    dynamic object, but we're not including those sections.  We have to
  298.    change the definition to something the rest of the link can
  299.    understand.  */
  300.  
  301. static boolean
  302. elf_i386_adjust_dynamic_symbol (info, h)
  303.      struct bfd_link_info *info;
  304.      struct elf_link_hash_entry *h;
  305. {
  306.   bfd *dynobj;
  307.   asection *s;
  308.   unsigned int power_of_two;
  309.   size_t align;
  310.  
  311.   dynobj = elf_hash_table (info)->dynobj;
  312.  
  313.   /* Make sure we know what is going on here.  */
  314.   BFD_ASSERT (dynobj != NULL
  315.           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
  316.           && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
  317.           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
  318.           && h->root.type == bfd_link_hash_defined
  319.           && (bfd_get_flavour (h->root.u.def.section->owner)
  320.           == bfd_target_elf_flavour)
  321.           && (elf_elfheader (h->root.u.def.section->owner)->e_type
  322.           == ET_DYN)
  323.           && h->root.u.def.section->output_section == NULL);
  324.  
  325.   /* If this is a function, put it in the procedure linkage table.  We
  326.      will fill in the contents of the procedure linkage table later,
  327.      when we know the address of the .got section.  */
  328.   if (h->type == STT_FUNC)
  329.     {
  330.       s = bfd_get_section_by_name (dynobj, ".plt");
  331.       BFD_ASSERT (s != NULL);
  332.  
  333.       /* If this is the first .plt entry, make room for the special
  334.      first entry.  */
  335.       if (s->_raw_size == 0)
  336.     s->_raw_size += PLT_ENTRY_SIZE;
  337.  
  338.       /* Set the symbol to this location in the .plt.  */
  339.       h->root.u.def.section = s;
  340.       h->root.u.def.value = s->_raw_size;
  341.  
  342.       /* Make room for this entry.  */
  343.       s->_raw_size += PLT_ENTRY_SIZE;
  344.  
  345.       /* We also need to make an entry in the .got section.  */
  346.  
  347.       s = bfd_get_section_by_name (dynobj, ".got");
  348.       BFD_ASSERT (s != NULL);
  349.       s->_raw_size += 4;
  350.  
  351.       /* We also need to make an entry in the .rel.plt section.  */
  352.  
  353.       s = bfd_get_section_by_name (dynobj, ".rel.plt");
  354.       BFD_ASSERT (s != NULL);
  355.       s->_raw_size += sizeof (Elf32_External_Rel);
  356.  
  357.       return true;
  358.     }
  359.  
  360.   /* If this is a weak symbol, and there is a real definition, the
  361.      processor independent code will have arranged for us to see the
  362.      real definition first, and we can just use the same value.  */
  363.   if (h->weakdef != NULL)
  364.     {
  365.       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined);
  366.       h->root.u.def.section = h->weakdef->root.u.def.section;
  367.       h->root.u.def.value = h->weakdef->root.u.def.value;
  368.       h->align = (bfd_size_type) -1;
  369.       return true;
  370.     }
  371.  
  372.   /* This is a reference to a symbol defined by a dynamic object which
  373.      is not a function.  We must allocate it in our .dynbss section,
  374.      which will become part of the .bss section of the executable.
  375.      There will be an entry for this symbol in the .dynsym section.
  376.      The dynamic object will contain position independent code, so all
  377.      references from the dynamic object to this symbol will go through
  378.      the global offset table.  The dynamic linker will use the .dynsym
  379.      entry to determine the address it must put in the global offset
  380.      table, so both the dynamic object and the regular object will
  381.      refer to the same memory location for the variable.  */
  382.  
  383.   s = bfd_get_section_by_name (dynobj, ".dynbss");
  384.   BFD_ASSERT (s != NULL);
  385.  
  386.   /* If the symbol is currently defined in the .bss section of the
  387.      dynamic object, then it is OK to simply initialize it to zero.
  388.      If the symbol is in some other section, we must generate a
  389.      R_386_COPY reloc to tell the dynamic linker to copy the initial
  390.      value out of the dynamic object and into the runtime process
  391.      image.  We need to remember the offset into the .rel.bss section
  392.      we are going to use, and we coopt the align field for this
  393.      purpose (the align field is only used for common symbols, and
  394.      these symbols are always defined).  It would be cleaner to use a
  395.      new field, but that would waste memory.  */
  396.   if ((h->root.u.def.section->flags & SEC_LOAD) == 0)
  397.     h->align = (bfd_size_type) -1;
  398.   else
  399.     {
  400.       asection *srel;
  401.  
  402.       srel = bfd_get_section_by_name (dynobj, ".rel.bss");
  403.       BFD_ASSERT (srel != NULL);
  404.       h->align = srel->_raw_size;
  405.       srel->_raw_size += sizeof (Elf32_External_Rel);
  406.     }
  407.  
  408.   /* We need to figure out the alignment required for this symbol.  I
  409.      have no idea how ELF linkers handle this.  */
  410.   switch (h->size)
  411.     {
  412.     case 0:
  413.     case 1:
  414.       power_of_two = 0;
  415.       align = 1;
  416.       break;
  417.     case 2:
  418.       power_of_two = 1;
  419.       align = 2;
  420.       break;
  421.     case 3:
  422.     case 4:
  423.       power_of_two = 2;
  424.       align = 4;
  425.       break;
  426.     case 5:
  427.     case 6:
  428.     case 7:
  429.     case 8:
  430.       power_of_two = 3;
  431.       align = 8;
  432.       break;
  433.     default:
  434.       power_of_two = 4;
  435.       align = 16;
  436.       break;
  437.     }
  438.  
  439.   /* Apply the required alignment.  */
  440.   s->_raw_size = BFD_ALIGN (s->_raw_size, align);
  441.   if (power_of_two > bfd_get_section_alignment (dynobj, s))
  442.     {
  443.       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
  444.     return false;
  445.     }
  446.  
  447.   /* Define the symbol as being at this point in the section.  */
  448.   h->root.u.def.section = s;
  449.   h->root.u.def.value = s->_raw_size;
  450.  
  451.   /* Increment the section size to make room for the symbol.  */
  452.   s->_raw_size += h->size;
  453.  
  454.   return true;
  455. }
  456.  
  457. /* Allocate contents for a section.  */
  458.  
  459. static INLINE boolean
  460. elf_i386_allocate_dynamic_section (dynobj, name)
  461.      bfd *dynobj;
  462.      const char *name;
  463. {
  464.   register asection *s;
  465.  
  466.   s = bfd_get_section_by_name (dynobj, name);
  467.   BFD_ASSERT (s != NULL);
  468.   s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
  469.   if (s->contents == NULL && s->_raw_size != 0)
  470.     {
  471.       bfd_set_error (bfd_error_no_memory);
  472.       return false;
  473.     }
  474.   return true;
  475. }
  476.  
  477. /* Set the sizes of the dynamic sections.  */
  478.  
  479. static boolean
  480. elf_i386_size_dynamic_sections (output_bfd, info)
  481.      bfd *output_bfd;
  482.      struct bfd_link_info *info;
  483. {
  484.   bfd *dynobj;
  485.   asection *s;
  486.  
  487.   dynobj = elf_hash_table (info)->dynobj;
  488.   BFD_ASSERT (dynobj != NULL);
  489.  
  490.   /* Set the contents of the .interp section to the interpreter.  */
  491.   if (! info->shared)
  492.     {
  493.       s = bfd_get_section_by_name (dynobj, ".interp");
  494.       BFD_ASSERT (s != NULL);
  495.       s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
  496.       s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  497.     }
  498.  
  499.   /* The adjust_dynamic_symbol entry point has determined the sizes of
  500.      the various dynamic sections.  Allocate some memory for them to
  501.      hold contents.  */
  502.   if (! elf_i386_allocate_dynamic_section (dynobj, ".plt")
  503.       || ! elf_i386_allocate_dynamic_section (dynobj, ".rel.plt")
  504.       || ! elf_i386_allocate_dynamic_section (dynobj, ".got")
  505.       || ! elf_i386_allocate_dynamic_section (dynobj, ".rel.bss"))
  506.     return false;
  507.  
  508.   /* Add some entries to the .dynamic section.  We fill in the values
  509.      later, in elf_i386_finish_dynamic_sections, but we must add the
  510.      entries now so that we get the correct size for the .dynamic
  511.      section.  The DT_DEBUG entry is filled in by the dynamic linker
  512.      and used by the debugger.  */
  513.   if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0)
  514.       || ! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
  515.     return false;
  516.  
  517.   s = bfd_get_section_by_name (dynobj, ".plt");
  518.   BFD_ASSERT (s != NULL);
  519.   if (s->_raw_size != 0)
  520.     {
  521.       if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
  522.       || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL)
  523.       || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
  524.     return false;
  525.     }
  526.  
  527.   /* If we didn't need the .rel.bss section, then discard it from the
  528.      output file.  This is a hack.  We don't bother to do it for the
  529.      other sections because they normally are needed.  */
  530.   s = bfd_get_section_by_name (dynobj, ".rel.bss");
  531.   BFD_ASSERT (s != NULL);
  532.   if (s->_raw_size == 0)
  533.     {
  534.       asection **spp;
  535.  
  536.       for (spp = &s->output_section->owner->sections;
  537.        *spp != s->output_section;
  538.        spp = &(*spp)->next)
  539.     ;
  540.       *spp = s->output_section->next;
  541.       --s->output_section->owner->section_count;
  542.     }
  543.   else
  544.     {
  545.       if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0)
  546.       || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0)
  547.       || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT,
  548.                         sizeof (Elf32_External_Rel)))
  549.     return false;
  550.     }
  551.  
  552.   return true;
  553. }
  554.  
  555. /* Relocate an i386 ELF section.  */
  556.  
  557. static boolean
  558. elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
  559.                contents, relocs, local_syms, local_sections,
  560.                output_names)
  561.      bfd *output_bfd;
  562.      struct bfd_link_info *info;
  563.      bfd *input_bfd;
  564.      asection *input_section;
  565.      bfd_byte *contents;
  566.      Elf_Internal_Rela *relocs;
  567.      Elf_Internal_Sym *local_syms;
  568.      asection **local_sections;
  569.      char *output_names;
  570. {
  571.   Elf_Internal_Shdr *symtab_hdr;
  572.   Elf_Internal_Rela *rel;
  573.   Elf_Internal_Rela *relend;
  574.  
  575.   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  576.  
  577.   rel = relocs;
  578.   relend = relocs + input_section->reloc_count;
  579.   for (; rel < relend; rel++)
  580.     {
  581.       int r_type;
  582.       const reloc_howto_type *howto;
  583.       long r_symndx;
  584.       struct elf_link_hash_entry *h;
  585.       Elf_Internal_Sym *sym;
  586.       asection *sec;
  587.       bfd_vma relocation;
  588.       bfd_reloc_status_type r;
  589.  
  590.       r_type = ELF32_R_TYPE (rel->r_info);
  591.       if (r_type < 0 || r_type >= (int) R_386_max)
  592.     {
  593.       bfd_set_error (bfd_error_bad_value);
  594.       return false;
  595.     }
  596.       howto = elf_howto_table + r_type;
  597.  
  598.       r_symndx = ELF32_R_SYM (rel->r_info);
  599.  
  600.       if (info->relocateable)
  601.     {
  602.       /* This is a relocateable link.  We don't have to change
  603.          anything, unless the reloc is against a section symbol,
  604.          in which case we have to adjust according to where the
  605.          section symbol winds up in the output section.  */
  606.       if (r_symndx < symtab_hdr->sh_info)
  607.         {
  608.           sym = local_syms + r_symndx;
  609.           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  610.         {
  611.           bfd_vma val;
  612.  
  613.           sec = local_sections[r_symndx];
  614.           val = bfd_get_32 (input_bfd, contents + rel->r_offset);
  615.           val += sec->output_offset + sym->st_value;
  616.           bfd_put_32 (input_bfd, val, contents + rel->r_offset);
  617.         }
  618.         }
  619.  
  620.       continue;
  621.     }
  622.  
  623.       /* This is a final link.  */
  624.       h = NULL;
  625.       sym = NULL;
  626.       sec = NULL;
  627.       if (r_symndx < symtab_hdr->sh_info)
  628.     {
  629.       sym = local_syms + r_symndx;
  630.       sec = local_sections[r_symndx];
  631.       relocation = (sec->output_section->vma
  632.             + sec->output_offset
  633.             + sym->st_value);
  634.     }
  635.       else
  636.     {
  637.       long indx;
  638.  
  639.       indx = r_symndx - symtab_hdr->sh_info;
  640.       h = elf_sym_hashes (input_bfd)[indx];
  641.       if (h->root.type == bfd_link_hash_defined)
  642.         {
  643.           sec = h->root.u.def.section;
  644.           relocation = (h->root.u.def.value
  645.                 + sec->output_section->vma
  646.                 + sec->output_offset);
  647.         }
  648.       else if (h->root.type == bfd_link_hash_weak)
  649.         relocation = 0;
  650.       else
  651.         {
  652.           if (! ((*info->callbacks->undefined_symbol)
  653.              (info, h->root.root.string, input_bfd,
  654.               input_section, rel->r_offset)))
  655.         return false;
  656.           relocation = 0;
  657.         }
  658.     }
  659.  
  660.       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  661.                     contents, rel->r_offset,
  662.                     relocation, (bfd_vma) 0);
  663.  
  664.       if (r != bfd_reloc_ok)
  665.     {
  666.       switch (r)
  667.         {
  668.         default:
  669.         case bfd_reloc_outofrange:
  670.           abort ();
  671.         case bfd_reloc_overflow:
  672.           {
  673.         const char *name;
  674.  
  675.         if (h != NULL)
  676.           name = h->root.root.string;
  677.         else
  678.           {
  679.             name = output_names + sym->st_name;
  680.             if (name == NULL)
  681.               return false;
  682.             if (*name == '\0')
  683.               name = bfd_section_name (input_bfd, sec);
  684.           }
  685.         if (! ((*info->callbacks->reloc_overflow)
  686.                (info, name, howto->name, (bfd_vma) 0,
  687.             input_bfd, input_section, rel->r_offset)))
  688.           return false;
  689.           }
  690.           break;
  691.         }
  692.     }
  693.     }
  694.  
  695.   return true;
  696. }
  697.  
  698. /* Finish up dynamic symbol handling.  We set the contents of various
  699.    dynamic sections here.  */
  700.  
  701. static boolean
  702. elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
  703.      bfd *output_bfd;
  704.      struct bfd_link_info *info;
  705.      struct elf_link_hash_entry *h;
  706.      Elf_Internal_Sym *sym;
  707. {
  708.   /* If this symbol is not defined by a dynamic object, or is not
  709.      referenced by a regular object, ignore it.  */
  710.   if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
  711.       || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
  712.       || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
  713.     {
  714.       /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
  715.       if (strcmp (h->root.root.string, "_DYNAMIC") == 0
  716.       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  717.     sym->st_shndx = SHN_ABS;
  718.       return true;
  719.     }
  720.  
  721.   BFD_ASSERT (h->root.type == bfd_link_hash_defined);
  722.   BFD_ASSERT (h->dynindx != -1);
  723.  
  724.   if (h->type == STT_FUNC)
  725.     {
  726.       asection *splt;
  727.       asection *sgot;
  728.       asection *srel;
  729.       bfd_vma plt_index;
  730.       bfd_vma got_offset;
  731.       Elf_Internal_Rel rel;
  732.  
  733.       splt = h->root.u.def.section;
  734.       BFD_ASSERT (strcmp (bfd_get_section_name (splt->owner, splt), ".plt")
  735.           == 0);
  736.       sgot = bfd_get_section_by_name (splt->owner, ".got");
  737.       BFD_ASSERT (sgot != NULL);
  738.       srel = bfd_get_section_by_name (splt->owner, ".rel.plt");
  739.       BFD_ASSERT (srel != NULL);
  740.  
  741.       /* FIXME: This only handles an absolute procedure linkage table.
  742.      When producing a dynamic object, we need to generate a
  743.      position independent procedure linkage table.  */
  744.  
  745.       /* Get the index in the procedure linkage table which
  746.      corresponds to this symbol.  This is the index of this symbol
  747.      in all the symbols for which we are making plt entries.  The
  748.      first entry in the procedure linkage table is reserved.  */
  749.       plt_index = h->root.u.def.value / PLT_ENTRY_SIZE - 1;
  750.  
  751.       /* Get the offset into the .got table of the entry that
  752.      corresponds to this function.  Each .got entry is 4 bytes.
  753.      The first three are reserved.  */
  754.       got_offset = (plt_index + 3) * 4;
  755.  
  756.       /* Fill in the entry in the procedure linkage table.  */
  757.       memcpy (splt->contents + h->root.u.def.value, elf_i386_plt_entry,
  758.           PLT_ENTRY_SIZE);
  759.       bfd_put_32 (output_bfd,
  760.           (sgot->output_section->vma
  761.            + sgot->output_offset
  762.            + got_offset),
  763.           splt->contents + h->root.u.def.value + 2);
  764.       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
  765.           splt->contents + h->root.u.def.value + 7);
  766.       bfd_put_32 (output_bfd, - (h->root.u.def.value + PLT_ENTRY_SIZE),
  767.           splt->contents + h->root.u.def.value + 12);
  768.  
  769.       /* Fill in the entry in the global offset table.  */
  770.       bfd_put_32 (output_bfd,
  771.           (splt->output_section->vma
  772.            + splt->output_offset
  773.            + h->root.u.def.value
  774.            + 6),
  775.           sgot->contents + got_offset);
  776.  
  777.       /* Fill in the entry in the .rel.plt section.  */
  778.       rel.r_offset = (sgot->output_section->vma
  779.               + sgot->output_offset
  780.               + got_offset);
  781.       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
  782.       bfd_elf32_swap_reloc_out (output_bfd, &rel,
  783.                 ((Elf32_External_Rel *) srel->contents
  784.                  + plt_index));
  785.  
  786.       /* Mark the symbol as undefined, rather than as defined in the
  787.      .plt section.  Leave the value alone.  */
  788.       sym->st_shndx = SHN_UNDEF;
  789.     }
  790.   else
  791.     {
  792.       /* This is not a function.  We have already allocated memory for
  793.      it in the .bss section (via .dynbss).  All we have to do here
  794.      is create a COPY reloc if required.  */
  795.       if (h->align != (bfd_size_type) -1)
  796.     {
  797.       asection *s;
  798.       Elf_Internal_Rel rel;
  799.  
  800.       s = bfd_get_section_by_name (h->root.u.def.section->owner,
  801.                        ".rel.bss");
  802.       BFD_ASSERT (s != NULL);
  803.  
  804.       rel.r_offset = (h->root.u.def.value
  805.               + h->root.u.def.section->output_section->vma
  806.               + h->root.u.def.section->output_offset);
  807.       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
  808.       bfd_elf32_swap_reloc_out (output_bfd, &rel,
  809.                     ((Elf32_External_Rel *)
  810.                      (s->contents + h->align)));
  811.     }
  812.     }
  813.  
  814.   return true;
  815. }
  816.  
  817. /* Finish up the dynamic sections.  */
  818.  
  819. static boolean
  820. elf_i386_finish_dynamic_sections (output_bfd, info)
  821.      bfd *output_bfd;
  822.      struct bfd_link_info *info;
  823. {
  824.   asection *splt;
  825.   asection *sgot;
  826.   asection *sdyn;
  827.   Elf32_External_Dyn *dyncon, *dynconend;
  828.  
  829.   splt = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".plt");
  830.   sgot = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".got");
  831.   sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".dynamic");
  832.   BFD_ASSERT (splt != NULL && sgot != NULL && sdyn != NULL);
  833.  
  834.   dyncon = (Elf32_External_Dyn *) sdyn->contents;
  835.   dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
  836.   for (; dyncon < dynconend; dyncon++)
  837.     {
  838.       Elf_Internal_Dyn dyn;
  839.       const char *name;
  840.       boolean size;
  841.  
  842.       bfd_elf32_swap_dyn_in (elf_hash_table (info)->dynobj, dyncon, &dyn);
  843.  
  844.       /* My reading of the SVR4 ABI indicates that the procedure
  845.      linkage table relocs (DT_JMPREL) should be included in the
  846.      overall relocs (DT_REL).  This is what Solaris does.
  847.      However, UnixWare can not handle that case.  Therefore, we
  848.      override the DT_REL and DT_RELSZ entries here to make them
  849.      not include the JMPREL relocs.  */
  850.  
  851.       switch (dyn.d_tag)
  852.     {
  853.     case DT_PLTGOT:   name = ".got"; size = false; break;
  854.     case DT_PLTRELSZ: name = ".rel.plt"; size = true; break;
  855.     case DT_JMPREL:   name = ".rel.plt"; size = false; break;
  856.     case DT_REL:      name = ".rel.bss"; size = false; break;
  857.     case DT_RELSZ:      name = ".rel.bss"; size = true; break;
  858.     default:      name = NULL; size = false; break;
  859.     }
  860.  
  861.       if (name != NULL)
  862.     {
  863.       asection *s;
  864.  
  865.       s = bfd_get_section_by_name (output_bfd, name);
  866.       BFD_ASSERT (s != NULL);
  867.       if (! size)
  868.         dyn.d_un.d_ptr = s->vma;
  869.       else
  870.         {
  871.           if (s->_cooked_size != 0)
  872.         dyn.d_un.d_val = s->_cooked_size;
  873.           else
  874.         dyn.d_un.d_val = s->_raw_size;
  875.         }
  876.       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  877.     }
  878.     }
  879.  
  880.   /* Fill in the first entry in the procedure linkage table.  */
  881.   if (splt->_raw_size > 0)
  882.     {
  883.       memcpy (splt->contents, elf_i386_plt0_entry, PLT_ENTRY_SIZE);
  884.       bfd_put_32 (output_bfd,
  885.           sgot->output_section->vma + sgot->output_offset + 4,
  886.           splt->contents + 2);
  887.       bfd_put_32 (output_bfd,
  888.           sgot->output_section->vma + sgot->output_offset + 8,
  889.           splt->contents + 8);
  890.     }
  891.  
  892.   /* Fill in the first three entries in the global offset table.  */
  893.   if (sgot->_raw_size > 0)
  894.     {
  895.       bfd_put_32 (output_bfd,
  896.           sdyn->output_section->vma + sdyn->output_offset,
  897.           sgot->contents);
  898.       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
  899.       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
  900.     }
  901.  
  902.   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
  903.  
  904.   /* UnixWare sets the entsize of .plt to 4, although that doesn't
  905.      really seem like the right value.  */
  906.   elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
  907.  
  908.   return true;
  909. }
  910.  
  911. #define TARGET_LITTLE_SYM        bfd_elf32_i386_vec
  912. #define TARGET_LITTLE_NAME        "elf32-i386"
  913. #define ELF_ARCH            bfd_arch_i386
  914. #define ELF_MACHINE_CODE        EM_386
  915. #define elf_info_to_howto        elf_i386_info_to_howto
  916. #define elf_info_to_howto_rel        elf_i386_info_to_howto_rel
  917. #define bfd_elf32_bfd_reloc_type_lookup    elf_i386_reloc_type_lookup
  918. #define ELF_MAXPAGESIZE            0x1000
  919. #define elf_backend_create_dynamic_sections \
  920.                     elf_i386_create_dynamic_sections
  921. #define elf_backend_adjust_dynamic_symbol \
  922.                     elf_i386_adjust_dynamic_symbol
  923. #define elf_backend_size_dynamic_sections \
  924.                     elf_i386_size_dynamic_sections
  925. #define elf_backend_relocate_section    elf_i386_relocate_section
  926. #define elf_backend_finish_dynamic_symbol \
  927.                     elf_i386_finish_dynamic_symbol
  928. #define elf_backend_finish_dynamic_sections \
  929.                     elf_i386_finish_dynamic_sections
  930.  
  931. #include "elf32-target.h"
  932.