Use a hash table when writing out ELF symbol names.
authorIan Lance Taylor <ian@airs.com>
Mon, 17 Oct 1994 22:03:14 +0000 (22:03 +0000)
committerIan Lance Taylor <ian@airs.com>
Mon, 17 Oct 1994 22:03:14 +0000 (22:03 +0000)
* elfcode.h (elf_stringtab_init): New static function.
(bfd_new_strtab, bfd_add_to_strtab, bfd_add_2_to_strtab): Remove.
Change all callers to use elf_stringtab_init or
_bfd_stringtab_add, and get stringtab lengths using
_bfd_stringtab_size.
(elf_fake_sections): Change ignored argument to pointer to
boolean, and set the boolean to true if an error occurs.  If an
error has already occurred, don't do anything.
(assign_section_numbers): Just set sh_size, not contents.
(elf_compute_section_file_positions): Pass the address of a
boolean to elf_fake_sections.  Pass the address of a
bfd_strtab_hash to swap_out_syms.  Write out the .strtab section.
(prep_headers): Change shstrtab to bfd_strtab_hash.
(swap_out_syms): Take a pointer to a bfd_strtab_hash as an
argument.  Set it to the symbol names.
(NAME(bfd_elf,write_object_contents)): Write out the section
header names using _bfd_stringtab_emit.
(elf_debug_section): Remove first argument; get the section name
via the bfd_section pointer.  Change caller.
(elf_bfd_final_link): Write out the symbol names using
_bfd_stringtab_emit.  Likewise for the .dynstr section contents.
Free the symbol names at the end of the function.
(elf_link_input_bfd): Remove the last argument, output_names,
from relocate_section.  Save the old symbol contents before
calling elf_link_output_sym, and restore them afterward.
* libelf.h (struct elf_link_hash_table): Change dynstr field to
struct bfd_strtab_hash.
(struct elf_backend_data): Remove last argument, output_names,
from elf_backend_relocate_section field.
(struct strtab): Don't define.
(struct elf_obj_tdata): Change strtab_ptr field to struct
bfd_strtab_hash.
* elf32-hppa.c (elf32_hppa_relocate_section): Remove last
argument, output_names.
* elf32-i386.c (elf_i386_relocate_section): Likewise.
* elf32-mips.c (mips_elf_relocate_section): Likewise.
* elf32-sparc.c (elf32_sparc_relocate_section): Likewise.

bfd/ChangeLog
bfd/elf32-hppa.c
bfd/elf32-i386.c
bfd/elfcode.h
bfd/libelf.h

index d4d9472..d2a54c3 100644 (file)
@@ -1,5 +1,44 @@
 Mon Oct 17 11:38:16 1994  Ian Lance Taylor  <ian@sanguine.cygnus.com>
 
+       Use a hash table when writing out ELF symbol names.
+       * elfcode.h (elf_stringtab_init): New static function.
+       (bfd_new_strtab, bfd_add_to_strtab, bfd_add_2_to_strtab): Remove.
+       Change all callers to use elf_stringtab_init or
+       _bfd_stringtab_add, and get stringtab lengths using
+       _bfd_stringtab_size.
+       (elf_fake_sections): Change ignored argument to pointer to
+       boolean, and set the boolean to true if an error occurs.  If an
+       error has already occurred, don't do anything.
+       (assign_section_numbers): Just set sh_size, not contents.
+       (elf_compute_section_file_positions): Pass the address of a
+       boolean to elf_fake_sections.  Pass the address of a
+       bfd_strtab_hash to swap_out_syms.  Write out the .strtab section.
+       (prep_headers): Change shstrtab to bfd_strtab_hash.
+       (swap_out_syms): Take a pointer to a bfd_strtab_hash as an
+       argument.  Set it to the symbol names.
+       (NAME(bfd_elf,write_object_contents)): Write out the section
+       header names using _bfd_stringtab_emit.
+       (elf_debug_section): Remove first argument; get the section name
+       via the bfd_section pointer.  Change caller.
+       (elf_bfd_final_link): Write out the symbol names using
+       _bfd_stringtab_emit.  Likewise for the .dynstr section contents.
+       Free the symbol names at the end of the function.
+       (elf_link_input_bfd): Remove the last argument, output_names,
+       from relocate_section.  Save the old symbol contents before
+       calling elf_link_output_sym, and restore them afterward.
+       * libelf.h (struct elf_link_hash_table): Change dynstr field to
+       struct bfd_strtab_hash.
+       (struct elf_backend_data): Remove last argument, output_names,
+       from elf_backend_relocate_section field.
+       (struct strtab): Don't define.
+       (struct elf_obj_tdata): Change strtab_ptr field to struct
+       bfd_strtab_hash.
+       * elf32-hppa.c (elf32_hppa_relocate_section): Remove last
+       argument, output_names.
+       * elf32-i386.c (elf_i386_relocate_section): Likewise.
+       * elf32-mips.c (mips_elf_relocate_section): Likewise.
+       * elf32-sparc.c (elf32_sparc_relocate_section): Likewise.
+
        * libbfd-in.h (DEFAULT_STRING_SPACE_SIZE): Don't define.
        (bfd_add_to_string_table): Don't declare.
        * libbfd.h: Rebuild.
index fb112b3..d362b58 100644 (file)
@@ -257,8 +257,7 @@ elf32_hppa_args_hash_newfunc
 static boolean
 elf32_hppa_relocate_section
   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
-          bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **,
-          char *));
+          bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
 
 static boolean
 elf32_hppa_stub_hash_table_init
@@ -762,8 +761,7 @@ hppa_elf_relocate_insn (abfd, input_sect, insn, address, sym_value,
 
 static boolean
 elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
-                            contents, relocs, local_syms, local_sections,
-                            output_names)
+                            contents, relocs, local_syms, local_sections)
      bfd *output_bfd;
      struct bfd_link_info *info;
      bfd *input_bfd;
@@ -772,7 +770,6 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
      Elf_Internal_Rela *relocs;
      Elf_Internal_Sym *local_syms;
      asection **local_sections;
-     char *output_names;
 {
   Elf_Internal_Shdr *symtab_hdr;
   Elf_Internal_Rela *rel;
@@ -865,7 +862,9 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
        sym_name = h->root.root.string;
       else
        {
-         sym_name = output_names + sym->st_name;
+         sym_name = elf_string_from_elf_section (input_bfd,
+                                                 symtab_hdr->sh_link,
+                                                 sym->st_name);
          if (sym_name == NULL)
            return false;
          if (*sym_name == '\0')
index c5b5a45..4d13c88 100644 (file)
@@ -31,15 +31,16 @@ static void elf_i386_info_to_howto_rel
   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
 static boolean elf_i386_create_dynamic_sections
   PARAMS ((bfd *, struct bfd_link_info *));
+static boolean elf_i386_check_relocs
+  PARAMS ((bfd *, struct bfd_link_info *, asection *,
+          const Elf_Internal_Rela *));
 static boolean elf_i386_adjust_dynamic_symbol
   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
-static boolean elf_i386_allocate_dynamic_section
-  PARAMS ((bfd *, const char *));
 static boolean elf_i386_size_dynamic_sections
   PARAMS ((bfd *, struct bfd_link_info *));
 static boolean elf_i386_relocate_section
   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
-          Elf_Internal_Rela *, Elf_Internal_Sym *, asection **, char *));
+          Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
 static boolean elf_i386_finish_dynamic_symbol
   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
           Elf_Internal_Sym *));
@@ -87,13 +88,13 @@ static reloc_howto_type elf_howto_table[]=
   HOWTO(R_386_32,       0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_32",       true,0xffffffff,0xffffffff,false),
   HOWTO(R_386_PC32,     0,2,32,true, 0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PC32",     true,0xffffffff,0xffffffff,true),
   HOWTO(R_386_GOT32,    0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOT32",    true,0xffffffff,0xffffffff,false),
-  HOWTO(R_386_PLT32,    0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PLT32",    true,0xffffffff,0xffffffff,false),
+  HOWTO(R_386_PLT32,    0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PLT32",    true,0xffffffff,0xffffffff,true),
   HOWTO(R_386_COPY,      0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_COPY",            true,0xffffffff,0xffffffff,false),
   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),
   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),
   HOWTO(R_386_RELATIVE,  0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_RELATIVE", true,0xffffffff,0xffffffff,false),
   HOWTO(R_386_GOTOFF,    0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTOFF",   true,0xffffffff,0xffffffff,false),
-  HOWTO(R_386_GOTPC,     0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTPC",    true,0xffffffff,0xffffffff,false),
+  HOWTO(R_386_GOTPC,     0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTPC",    true,0xffffffff,0xffffffff,true),
 };
 
 #ifdef DEBUG_GEN_RELOC
@@ -219,6 +220,27 @@ static bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
   0, 0, 0, 0   /* replaced with offset to start of .plt.  */
 };
 
+/* The first entry in a PIC procedure linkage table look like this.  */
+
+static bfd_byte elf_i386_pic_plt0_entry[PLT_ENTRY_SIZE] =
+{
+  0xff, 0xb3, 4, 0, 0, 0,      /* pushl 4(%ebx) */     
+  0xff, 0xa3, 8, 0, 0, 0,      /* jmp *8(%ebx) */      
+  0, 0, 0, 0                   /* pad out to 16 bytes.  */
+};
+
+/* Subsequent entries in a PIC procedure linkage table look like this.  */
+
+static bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
+{
+  0xff, 0xa3,  /* jmp *offset(%ebx) */
+  0, 0, 0, 0,  /* replaced with offset of this symbol in .got.  */
+  0x68,                /* pushl immediate */
+  0, 0, 0, 0,  /* replaced with offset into relocation table.  */
+  0xe9,                /* jmp relative */
+  0, 0, 0, 0   /* replaced with offset to start of .plt.  */
+};
+
 /* Create dynamic sections when linking against a dynamic object.  */
 
 static boolean
@@ -230,8 +252,8 @@ elf_i386_create_dynamic_sections (abfd, info)
   register asection *s;
   struct elf_link_hash_entry *h;
 
-  /* We need to create .plt, .rel.plt, .got, .dynbss, and .rel.bss
-     sections.  */
+  /* We need to create .plt, .rel.plt, .got, .got.plt, .dynbss, and
+     .rel.bss sections.  */
 
   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
 
@@ -253,10 +275,17 @@ elf_i386_create_dynamic_sections (abfd, info)
       || ! bfd_set_section_alignment (abfd, s, 2))
     return false;
 
-  /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
-     section.  We don't do this in the linker script because we don't
-     want to define the symbol if we are not creating a global offset
-     table.  */
+  s = bfd_make_section (abfd, ".got.plt");
+  if (s == NULL
+      || ! bfd_set_section_flags (abfd, s, flags)
+      || ! bfd_set_section_alignment (abfd, s, 2))
+    return false;
+
+  /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
+     .got.plt section, which will be placed at the start of the output
+     .got section.  We don't do this in the linker script because we
+     don't want to define the symbol if we are not creating a global
+     offset table.  */
   h = NULL;
   if (! (_bfd_generic_link_add_one_symbol
         (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0,
@@ -264,6 +293,11 @@ elf_i386_create_dynamic_sections (abfd, info)
          (struct bfd_link_hash_entry **) &h)))
     return false;
   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
+  h->type = STT_OBJECT;
+
+  if (info->shared
+      && ! bfd_elf32_link_record_dynamic_symbol (info, h))
+    return false;
 
   /* The first three global offset table entries are reserved.  */
   s->_raw_size += 3 * 4;
@@ -281,13 +315,270 @@ elf_i386_create_dynamic_sections (abfd, info)
 
   /* The .rel.bss section holds copy relocs.  This section is not
      normally needed.  We need to create it here, though, so that the
-     linker will map it to an output section.  If it turns out not to
-     be needed, we can discard it later.  */
-  s = bfd_make_section (abfd, ".rel.bss");
-  if (s == NULL
-      || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
-      || ! bfd_set_section_alignment (abfd, s, 2))
-    return false;
+     linker will map it to an output section.  We can't just create it
+     only if we need it, because we will not know whether we need it
+     until we have seen all the input files, and the first time the
+     main linker code calls BFD after examining all the input files
+     (size_dynamic_sections) the input sections have already been
+     mapped to the output sections.  If the section turns out not to
+     be needed, we can discard it later.  We will never need this
+     section when generating a shared object, since they do not use
+     copy relocs.  */
+  if (! info->shared)
+    {
+      s = bfd_make_section (abfd, ".rel.bss");
+      if (s == NULL
+         || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
+         || ! bfd_set_section_alignment (abfd, s, 2))
+       return false;
+    }
+
+  return true;
+}
+
+/* Look through the relocs for a section during the first phase, and
+   allocate space in the global offset table or procedure linkage
+   table.  */
+
+static boolean
+elf_i386_check_relocs (abfd, info, sec, relocs)
+     bfd *abfd;
+     struct bfd_link_info *info;
+     asection *sec;
+     const Elf_Internal_Rela *relocs;
+{
+  bfd *dynobj;
+  Elf_Internal_Shdr *symtab_hdr;
+  struct elf_link_hash_entry **sym_hashes;
+  bfd_vma *local_got_offsets;
+  const Elf_Internal_Rela *rel;
+  const Elf_Internal_Rela *rel_end;
+  asection *sgot;
+  asection *srelgot;
+  asection *splt;
+  asection *sgotplt;
+  asection *srelplt;
+  asection *sreloc;
+
+  if (info->relocateable)
+    return true;
+
+  dynobj = elf_hash_table (info)->dynobj;
+  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
+  sym_hashes = elf_sym_hashes (abfd);
+  local_got_offsets = elf_local_got_offsets (abfd);
+
+  sgot = NULL;
+  srelgot = NULL;
+  splt = NULL;
+  sgotplt = NULL;
+  srelplt = NULL;
+  sreloc = NULL;
+
+  rel_end = relocs + sec->reloc_count;
+  for (rel = relocs; rel < rel_end; rel++)
+    {
+      long r_symndx;
+      struct elf_link_hash_entry *h;
+
+      r_symndx = ELF32_R_SYM (rel->r_info);
+
+      if (r_symndx < symtab_hdr->sh_info)
+       h = NULL;
+      else
+       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+
+      /* Some relocs require a global offset table.  FIXME: If this is
+        a static link of PIC code, we need a global offset table but
+        we don't really need to create the full dynamic linking
+        information.  */
+      if (dynobj == NULL)
+       {
+         switch (ELF32_R_TYPE (rel->r_info))
+           {
+           case R_386_GOT32:
+           case R_386_PLT32:
+           case R_386_GOTOFF:
+           case R_386_GOTPC:
+             elf_hash_table (info)->dynobj = dynobj = abfd;
+             if (! bfd_elf32_link_create_dynamic_sections (dynobj, info))
+               return false;
+             break;
+
+           default:
+             break;
+           }
+       }
+
+      switch (ELF32_R_TYPE (rel->r_info))
+       {
+       case R_386_GOT32:
+         /* This symbol requires a global offset table entry.  */
+     
+         if (sgot == NULL)
+           {
+             sgot = bfd_get_section_by_name (dynobj, ".got");
+             srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
+             if (srelgot == NULL)
+               {
+                 srelgot = bfd_make_section (dynobj, ".rel.got");
+                 if (srelgot == NULL
+                     || ! bfd_set_section_flags (dynobj, srelgot,
+                                                 (SEC_ALLOC
+                                                  | SEC_LOAD
+                                                  | SEC_HAS_CONTENTS
+                                                  | SEC_IN_MEMORY
+                                                  | SEC_READONLY))
+                     || ! bfd_set_section_alignment (dynobj, srelgot, 2))
+                   return false;
+               }
+             BFD_ASSERT (sgot != NULL && srelgot != NULL);
+           }
+
+         if (h != NULL)
+           {
+             if (h->got_offset != (bfd_vma) -1)
+               {
+                 /* We have already allocated space in the .got.  */
+                 break;
+               }
+             h->got_offset = sgot->_raw_size;
+
+             /* Make sure this symbol is output as a dynamic symbol.  */
+             if (h->dynindx == -1)
+               {
+                 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
+                   return false;
+               }
+           }
+         else
+           {
+             /* This is a global offset table entry for a local
+                 symbol.  */
+             if (local_got_offsets == NULL)
+               {
+                 size_t size;
+                 register int i;
+
+                 size = symtab_hdr->sh_info * sizeof (bfd_vma);
+                 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
+                 if (local_got_offsets == NULL)
+                   {
+                     bfd_set_error (bfd_error_no_memory);
+                     return false;
+                   }
+                 elf_local_got_offsets (abfd) = local_got_offsets;
+                 for (i = 0; i < symtab_hdr->sh_info; i++)
+                   local_got_offsets[i] = (bfd_vma) -1;
+               }
+             if (local_got_offsets[r_symndx] != (bfd_vma) -1)
+               {
+                 /* We have already allocated space in the .got.  */
+                 break;
+               }
+             local_got_offsets[r_symndx] = sgot->_raw_size;
+           }
+
+         sgot->_raw_size += 4;
+         srelgot->_raw_size += sizeof (Elf32_External_Rel);
+
+         break;
+
+       case R_386_PLT32:
+         /* This symbol requires a procedure linkage table entry.  */
+         
+         /* If this is a local symbol, we resolve it directly without
+             creating a procedure linkage table entry.  */
+         if (h == NULL)
+           continue;
+
+         if (h->plt_offset != (bfd_vma) -1)
+           {
+             /* There is already an entry for this symbol in the
+                 procedure linkage table.  */
+             break;
+           }
+
+         if (splt == NULL)
+           {
+             splt = bfd_get_section_by_name (dynobj, ".plt");
+             sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
+             srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
+             BFD_ASSERT (splt != NULL && sgotplt != NULL && srelplt != NULL);
+           }
+
+         /* If this is the first .plt entry, make room for the
+            special first entry.  */
+         if (splt->_raw_size == 0)
+           splt->_raw_size += PLT_ENTRY_SIZE;
+
+         /* Make sure this symbol is output as a dynamic symbol.  */
+         if (h->dynindx == -1)
+           {
+             if (! bfd_elf32_link_record_dynamic_symbol (info, h))
+               return false;
+           }
+
+         h->plt_offset = splt->_raw_size;
+
+         /* Make room for this entry.  We need a procedure linkage
+            table entry in .plt, a global offset table entry in
+            .got.plt (which is placed in .got by the linker script),
+            and a relocation in .rel.plt.  */
+         splt->_raw_size += PLT_ENTRY_SIZE;
+         sgotplt->_raw_size += 4;
+         srelplt->_raw_size += sizeof (Elf32_External_Rel);
+
+         break;
+
+       case R_386_32:
+       case R_386_PC32:
+         if (info->shared
+             && (sec->flags & SEC_ALLOC) != 0)
+           {
+             /* When creating a shared object, we must output a
+                 R_386_RELATIVE reloc for this location.  We create a
+                 reloc section in dynobj and make room for this reloc.  */
+             if (sreloc == NULL)
+               {
+                 const char *name;
+
+                 name = (elf_string_from_elf_section
+                         (abfd,
+                          elf_elfheader (abfd)->e_shstrndx,
+                          elf_section_data (sec)->rel_hdr.sh_name));
+                 if (name == NULL)
+                   return false;
+
+                 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
+                             && strcmp (bfd_get_section_name (abfd, sec),
+                                        name + 4) == 0);
+
+                 sreloc = bfd_get_section_by_name (dynobj, name);
+                 if (sreloc == NULL)
+                   {
+                     sreloc = bfd_make_section (dynobj, name);
+                     if (sreloc == NULL
+                         || ! bfd_set_section_flags (dynobj, sreloc,
+                                                     (SEC_ALLOC
+                                                      | SEC_LOAD
+                                                      | SEC_HAS_CONTENTS
+                                                      | SEC_IN_MEMORY
+                                                      | SEC_READONLY))
+                         || ! bfd_set_section_alignment (dynobj, sreloc, 2))
+                       return false;
+                   }
+               }
+
+             sreloc->_raw_size += sizeof (Elf32_External_Rel);
+           }
+            
+         break;
+
+       default:
+         break;
+       }
+    }
 
   return true;
 }
@@ -329,29 +620,41 @@ elf_i386_adjust_dynamic_symbol (info, h)
       s = bfd_get_section_by_name (dynobj, ".plt");
       BFD_ASSERT (s != NULL);
 
-      /* If this is the first .plt entry, make room for the special
-        first entry.  */
-      if (s->_raw_size == 0)
-       s->_raw_size += PLT_ENTRY_SIZE;
+      if (h->plt_offset != (bfd_vma) -1)
+       {
+         h->root.u.def.section = s;
+         h->root.u.def.value = h->plt_offset;
+       }
+      else
+       {
+         /* If this is the first .plt entry, make room for the
+            special first entry.  */
+         if (s->_raw_size == 0)
+           s->_raw_size += PLT_ENTRY_SIZE;
 
-      /* Set the symbol to this location in the .plt.  */
-      h->root.u.def.section = s;
-      h->root.u.def.value = s->_raw_size;
+         /* Set the symbol to this location in the .plt.  */
+         h->root.u.def.section = s;
+         h->root.u.def.value = s->_raw_size;
 
-      /* Make room for this entry.  */
-      s->_raw_size += PLT_ENTRY_SIZE;
+         h->plt_offset = s->_raw_size;
 
-      /* We also need to make an entry in the .got section.  */
+         /* Make room for this entry.  */
+         s->_raw_size += PLT_ENTRY_SIZE;
 
-      s = bfd_get_section_by_name (dynobj, ".got");
-      BFD_ASSERT (s != NULL);
-      s->_raw_size += 4;
+         /* We also need to make an entry in the .got.plt section,
+            which will be placed in the .got section by the linker
+            script.  */
 
-      /* We also need to make an entry in the .rel.plt section.  */
+         s = bfd_get_section_by_name (dynobj, ".got.plt");
+         BFD_ASSERT (s != NULL);
+         s->_raw_size += 4;
 
-      s = bfd_get_section_by_name (dynobj, ".rel.plt");
-      BFD_ASSERT (s != NULL);
-      s->_raw_size += sizeof (Elf32_External_Rel);
+         /* We also need to make an entry in the .rel.plt section.  */
+
+         s = bfd_get_section_by_name (dynobj, ".rel.plt");
+         BFD_ASSERT (s != NULL);
+         s->_raw_size += sizeof (Elf32_External_Rel);
+       }
 
       return true;
     }
@@ -364,20 +667,28 @@ elf_i386_adjust_dynamic_symbol (info, h)
       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined);
       h->root.u.def.section = h->weakdef->root.u.def.section;
       h->root.u.def.value = h->weakdef->root.u.def.value;
-      h->copy_offset = (bfd_vma) -1;
       return true;
     }
 
   /* This is a reference to a symbol defined by a dynamic object which
-     is not a function.  We must allocate it in our .dynbss section,
-     which will become part of the .bss section of the executable.
-     There will be an entry for this symbol in the .dynsym section.
-     The dynamic object will contain position independent code, so all
-     references from the dynamic object to this symbol will go through
-     the global offset table.  The dynamic linker will use the .dynsym
-     entry to determine the address it must put in the global offset
-     table, so both the dynamic object and the regular object will
-     refer to the same memory location for the variable.  */
+     is not a function.  */
+
+  /* If we are creating a shared library, we must presume that the
+     only references to the symbol are via the global offset table.
+     For such cases we need not do anything here; the relocations will
+     be handled correctly by relocate_section.  */
+  if (info->shared)
+    return true;
+
+  /* We must allocate the symbol in our .dynbss section, which will
+     become part of the .bss section of the executable.  There will be
+     an entry for this symbol in the .dynsym section.  The dynamic
+     object will contain position independent code, so all references
+     from the dynamic object to this symbol will go through the global
+     offset table.  The dynamic linker will use the .dynsym entry to
+     determine the address it must put in the global offset table, so
+     both the dynamic object and the regular object will refer to the
+     same memory location for the variable.  */
 
   s = bfd_get_section_by_name (dynobj, ".dynbss");
   BFD_ASSERT (s != NULL);
@@ -389,16 +700,14 @@ elf_i386_adjust_dynamic_symbol (info, h)
      value out of the dynamic object and into the runtime process
      image.  We need to remember the offset into the .rel.bss section
      we are going to use.  */
-  if ((h->root.u.def.section->flags & SEC_LOAD) == 0)
-    h->copy_offset = (bfd_vma) -1;
-  else
+  if ((h->root.u.def.section->flags & SEC_LOAD) != 0)
     {
       asection *srel;
 
       srel = bfd_get_section_by_name (dynobj, ".rel.bss");
       BFD_ASSERT (srel != NULL);
-      h->copy_offset = srel->_raw_size;
       srel->_raw_size += sizeof (Elf32_External_Rel);
+      h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
     }
 
   /* We need to figure out the alignment required for this symbol.  I
@@ -426,26 +735,6 @@ elf_i386_adjust_dynamic_symbol (info, h)
   return true;
 }
 
-/* Allocate contents for a section.  */
-
-static INLINE boolean
-elf_i386_allocate_dynamic_section (dynobj, name)
-     bfd *dynobj;
-     const char *name;
-{
-  register asection *s;
-
-  s = bfd_get_section_by_name (dynobj, name);
-  BFD_ASSERT (s != NULL);
-  s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
-  if (s->contents == NULL && s->_raw_size != 0)
-    {
-      bfd_set_error (bfd_error_no_memory);
-      return false;
-    }
-  return true;
-}
-
 /* Set the sizes of the dynamic sections.  */
 
 static boolean
@@ -455,6 +744,9 @@ elf_i386_size_dynamic_sections (output_bfd, info)
 {
   bfd *dynobj;
   asection *s;
+  boolean plt;
+  boolean relocs;
+  boolean reltext;
 
   dynobj = elf_hash_table (info)->dynobj;
   BFD_ASSERT (dynobj != NULL);
@@ -468,51 +760,126 @@ elf_i386_size_dynamic_sections (output_bfd, info)
       s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
     }
 
-  /* The adjust_dynamic_symbol entry point has determined the sizes of
-     the various dynamic sections.  Allocate some memory for them to
-     hold contents.  */
-  if (! elf_i386_allocate_dynamic_section (dynobj, ".plt")
-      || ! elf_i386_allocate_dynamic_section (dynobj, ".rel.plt")
-      || ! elf_i386_allocate_dynamic_section (dynobj, ".got")
-      || ! elf_i386_allocate_dynamic_section (dynobj, ".rel.bss"))
-    return false;
+  /* The check_relocs and adjust_dynamic_symbol entry points have
+     determined the sizes of the various dynamic sections.  Allocate
+     memory for them.  */
+  plt = false;
+  relocs = false;
+  reltext = false;
+  for (s = dynobj->sections; s != NULL; s = s->next)
+    {
+      const char *name;
+      boolean strip;
+
+      if ((s->flags & SEC_IN_MEMORY) == 0)
+       continue;
+
+      /* It's OK to base decisions on the section name, because none
+        of the dynobj section names depend upon the input files.  */
+      name = bfd_get_section_name (dynobj, s);
+
+      strip = false;
+
+      if (strcmp (name, ".plt") == 0)
+       {
+         if (s->_raw_size == 0)
+           {
+             /* Strip this section if we don't need it; see the
+                 comment below.  */
+             strip = true;
+           }
+         else
+           {
+             /* Remember whether there is a PLT.  */
+             plt = true;
+           }
+       }
+      else if (strncmp (name, ".rel", 4) == 0)
+       {
+         if (s->_raw_size == 0)
+           {
+             /* If we don't need this section, strip it from the
+                output file.  This is mostly to handle .rel.bss and
+                .rel.plt.  We must create both sections in
+                create_dynamic_sections, because they must be created
+                before the linker maps input sections to output
+                sections.  The linker does that before
+                adjust_dynamic_symbol is called, and it is that
+                function which decides whether anything needs to go
+                into these sections.  */
+             strip = true;
+           }
+         else
+           {
+             asection *target;
+
+             /* Remember whether there are any reloc sections other
+                 than .rel.plt.  */
+             if (strcmp (name, ".rel.plt") != 0)
+               relocs = true;
+
+             /* If this relocation section applies to a read only
+                 section, then we probably need a DT_TEXTREL entry.  */
+             target = bfd_get_section_by_name (output_bfd, name + 4);
+             if (target != NULL
+                 && (target->flags & SEC_READONLY) != 0)
+               reltext = true;
+
+             /* We use the reloc_count field as a counter if we need
+                to copy relocs into the output file.  */
+             s->reloc_count = 0;
+           }
+       }
+      else if (strncmp (name, ".got", 4) != 0)
+       {
+         /* It's not one of our sections, so don't allocate space.  */
+         continue;
+       }
+
+      if (strip)
+       {
+         asection **spp;
+
+         for (spp = &s->output_section->owner->sections;
+              *spp != s->output_section;
+              spp = &(*spp)->next)
+           ;
+         *spp = s->output_section->next;
+         --s->output_section->owner->section_count;
+
+         continue;
+       }
 
+      /* Allocate memory for the section contents.  */
+      s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
+      if (s->contents == NULL && s->_raw_size != 0)
+       {
+         bfd_set_error (bfd_error_no_memory);
+         return false;
+       }
+    }
+         
   /* Add some entries to the .dynamic section.  We fill in the values
      later, in elf_i386_finish_dynamic_sections, but we must add the
      entries now so that we get the correct size for the .dynamic
      section.  The DT_DEBUG entry is filled in by the dynamic linker
      and used by the debugger.  */
-  if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0)
-      || ! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
-    return false;
+  if (! info->shared)
+    {
+      if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
+       return false;
+    }
 
-  s = bfd_get_section_by_name (dynobj, ".plt");
-  BFD_ASSERT (s != NULL);
-  if (s->_raw_size != 0)
+  if (plt)
     {
-      if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
+      if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
+         || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
          || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL)
          || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
        return false;
     }
 
-  /* If we didn't need the .rel.bss section, then discard it from the
-     output file.  This is a hack.  We don't bother to do it for the
-     other sections because they normally are needed.  */
-  s = bfd_get_section_by_name (dynobj, ".rel.bss");
-  BFD_ASSERT (s != NULL);
-  if (s->_raw_size == 0)
-    {
-      asection **spp;
-
-      for (spp = &s->output_section->owner->sections;
-          *spp != s->output_section;
-          spp = &(*spp)->next)
-       ;
-      *spp = s->output_section->next;
-      --s->output_section->owner->section_count;
-    }
-  else
+  if (relocs)
     {
       if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0)
          || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0)
@@ -521,6 +888,12 @@ elf_i386_size_dynamic_sections (output_bfd, info)
        return false;
     }
 
+  if (reltext)
+    {
+      if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
+       return false;
+    }
+
   return true;
 }
 
@@ -528,8 +901,7 @@ elf_i386_size_dynamic_sections (output_bfd, info)
 
 static boolean
 elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
-                          contents, relocs, local_syms, local_sections,
-                          output_names)
+                          contents, relocs, local_syms, local_sections)
      bfd *output_bfd;
      struct bfd_link_info *info;
      bfd *input_bfd;
@@ -540,11 +912,24 @@ elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
      asection **local_sections;
      char *output_names;
 {
+  bfd *dynobj;
   Elf_Internal_Shdr *symtab_hdr;
+  struct elf_link_hash_entry **sym_hashes;
+  bfd_vma *local_got_offsets;
+  asection *sgot;
+  asection *splt;
+  asection *sreloc;
   Elf_Internal_Rela *rel;
   Elf_Internal_Rela *relend;
 
+  dynobj = elf_hash_table (info)->dynobj;
   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
+  sym_hashes = elf_sym_hashes (input_bfd);
+  local_got_offsets = elf_local_got_offsets (input_bfd);
+
+  sgot = NULL;
+  splt = NULL;
+  sreloc = NULL;
 
   rel = relocs;
   relend = relocs + input_section->reloc_count;
@@ -606,10 +991,7 @@ elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
        }
       else
        {
-         long indx;
-
-         indx = r_symndx - symtab_hdr->sh_info;
-         h = elf_sym_hashes (input_bfd)[indx];
+         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
          if (h->root.type == bfd_link_hash_defined)
            {
              sec = h->root.u.def.section;
@@ -619,6 +1001,8 @@ elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
            }
          else if (h->root.type == bfd_link_hash_weak)
            relocation = 0;
+         else if (info->shared)
+           relocation = 0;
          else
            {
              if (! ((*info->callbacks->undefined_symbol)
@@ -629,6 +1013,188 @@ elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
            }
        }
 
+      switch (r_type)
+       {
+       case R_386_GOT32:
+         /* Relocation is to the entry for this symbol in the global
+            offset table.  */
+         if (sgot == NULL)
+           {
+             sgot = bfd_get_section_by_name (dynobj, ".got");
+             BFD_ASSERT (sgot != NULL);
+           }
+
+         if (h != NULL)
+           {
+             BFD_ASSERT (h->got_offset != (bfd_vma) -1);
+             relocation = sgot->output_offset + h->got_offset;
+           }
+         else
+           {
+             bfd_vma off;
+
+             BFD_ASSERT (local_got_offsets != NULL
+                         && local_got_offsets[r_symndx] != (bfd_vma) -1);
+
+             off = local_got_offsets[r_symndx];
+
+             /* The offset must always be a multiple of 4.  We use
+                 the least significant bit to record whether we have
+                 already generated the necessary reloc.  */
+             if ((off & 1) != 0)
+               off &= ~1;
+             else
+               {
+                 asection *srelgot;
+                 Elf_Internal_Rel outrel;
+
+                 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
+
+                 srelgot = bfd_get_section_by_name (dynobj, ".rel.got");
+                 BFD_ASSERT (srelgot != NULL);
+
+                 outrel.r_offset = (sgot->output_section->vma
+                                    + sgot->output_offset
+                                    + off);
+                 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
+                 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
+                                           (((Elf32_External_Rel *)
+                                             srelgot->contents)
+                                            + srelgot->reloc_count));
+                 ++srelgot->reloc_count;
+
+                 local_got_offsets[r_symndx] |= 1;
+               }
+
+             relocation = sgot->output_offset + off;
+           }
+
+         break;
+
+       case R_386_GOTOFF:
+         /* Relocation is relative to the start of the global offset
+            table.  */
+
+         if (sgot == NULL)
+           {
+             sgot = bfd_get_section_by_name (dynobj, ".got");
+             BFD_ASSERT (sgot != NULL);
+           }
+
+         /* Note that sgot->output_offset is not involved in this
+            calculation.  We always want the start of .got.  If we
+            defined _GLOBAL_OFFSET_TABLE in a different way, as is
+            permitted by the ABI, we might have to change this
+            calculation.  */
+         relocation -= sgot->output_section->vma;
+
+         break;
+
+       case R_386_GOTPC:
+         /* Use global offset table as symbol value.  */
+
+         if (sgot == NULL)
+           {
+             sgot = bfd_get_section_by_name (dynobj, ".got");
+             BFD_ASSERT (sgot != NULL);
+           }
+
+         relocation = sgot->output_section->vma;
+
+         break;
+
+       case R_386_PLT32:
+         /* Relocation is to the entry for this symbol in the
+            procedure linkage table.  */
+
+         /* Resolve a PLT32 reloc again a local symbol directly,
+             without using the procedure linkage table.  */
+         if (h == NULL)
+           break;
+
+         if (splt == NULL)
+           {
+             splt = bfd_get_section_by_name (dynobj, ".plt");
+             BFD_ASSERT (splt != NULL);
+           }
+
+         BFD_ASSERT (h != NULL && h->plt_offset != (bfd_vma) -1);
+         relocation = (splt->output_section->vma
+                       + splt->output_offset
+                       + h->plt_offset);
+
+         break;
+
+       case R_386_32:
+       case R_386_PC32:
+         if (info->shared
+             && (input_section->flags & SEC_ALLOC) != 0)
+           {
+             Elf_Internal_Rel outrel;
+
+             /* When generating a shared object, these relocations
+                are copied into the output file to be resolved at run
+                time.  */
+
+             if (sreloc == NULL)
+               {
+                 const char *name;
+
+                 name = (elf_string_from_elf_section
+                         (input_bfd,
+                          elf_elfheader (input_bfd)->e_shstrndx,
+                          elf_section_data (input_section)->rel_hdr.sh_name));
+                 if (name == NULL)
+                   return false;
+
+                 BFD_ASSERT (strncmp (name, ".rel", 4) == 0
+                             && strcmp (bfd_get_section_name (input_bfd,
+                                                              input_section),
+                                        name + 4) == 0);
+
+                 sreloc = bfd_get_section_by_name (dynobj, name);
+                 BFD_ASSERT (sreloc != NULL);
+               }
+
+             outrel.r_offset = (rel->r_offset
+                                + input_section->output_section->vma
+                                + input_section->output_offset);
+             if (r_type == R_386_PC32)
+               {
+                 BFD_ASSERT (h != NULL && h->dynindx != (bfd_vma) -1);
+                 outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_PC32);
+               }
+             else
+               {
+                 if (h == NULL)
+                   outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
+                 else
+                   {
+                     BFD_ASSERT (h->dynindx != (bfd_vma) -1);
+                     outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_32);
+                   }
+               }
+
+             bfd_elf32_swap_reloc_out (output_bfd, &outrel,
+                                       (((Elf32_External_Rel *)
+                                         sreloc->contents)
+                                        + sreloc->reloc_count));
+             ++sreloc->reloc_count;
+
+             /* If this reloc is against an external symbol, we do
+                not want to fiddle with the addend.  Otherwise, we
+                need to include the symbol value so that it becomes
+                an addend for the dynamic reloc.  */
+             if (h != NULL)
+               continue;
+           }
+
+         break;
+
+       default:
+         break;
+       }
+
       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
                                    contents, rel->r_offset,
                                    relocation, (bfd_vma) 0);
@@ -648,7 +1214,9 @@ elf_i386_relocate_section (output_bfd, info, input_bfd, input_section,
                  name = h->root.root.string;
                else
                  {
-                   name = output_names + sym->st_name;
+                   name = elf_string_from_elf_section (input_bfd,
+                                                       symtab_hdr->sh_link,
+                                                       sym->st_name);
                    if (name == NULL)
                      return false;
                    if (*name == '\0')
@@ -677,23 +1245,11 @@ elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
      struct elf_link_hash_entry *h;
      Elf_Internal_Sym *sym;
 {
-  /* If this symbol is not defined by a dynamic object, or is not
-     referenced by a regular object, ignore it.  */
-  if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
-      || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
-      || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
-    {
-      /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
-      if (strcmp (h->root.root.string, "_DYNAMIC") == 0
-         || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
-       sym->st_shndx = SHN_ABS;
-      return true;
-    }
+  bfd *dynobj;
 
-  BFD_ASSERT (h->root.type == bfd_link_hash_defined);
-  BFD_ASSERT (h->dynindx != -1);
+  dynobj = elf_hash_table (info)->dynobj;
 
-  if (h->type == STT_FUNC)
+  if (h->plt_offset != (bfd_vma) -1)
     {
       asection *splt;
       asection *sgot;
@@ -702,23 +1258,21 @@ elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
       bfd_vma got_offset;
       Elf_Internal_Rel rel;
 
-      splt = h->root.u.def.section;
-      BFD_ASSERT (strcmp (bfd_get_section_name (splt->owner, splt), ".plt")
-                 == 0);
-      sgot = bfd_get_section_by_name (splt->owner, ".got");
-      BFD_ASSERT (sgot != NULL);
-      srel = bfd_get_section_by_name (splt->owner, ".rel.plt");
-      BFD_ASSERT (srel != NULL);
+      /* This symbol has an entry in the procedure linkage table.  Set
+        it up.  */
 
-      /* FIXME: This only handles an absolute procedure linkage table.
-        When producing a dynamic object, we need to generate a
-        position independent procedure linkage table.  */
+      BFD_ASSERT (h->dynindx != -1);
+
+      splt = bfd_get_section_by_name (dynobj, ".plt");
+      sgot = bfd_get_section_by_name (dynobj, ".got.plt");
+      srel = bfd_get_section_by_name (dynobj, ".rel.plt");
+      BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
 
       /* Get the index in the procedure linkage table which
         corresponds to this symbol.  This is the index of this symbol
         in all the symbols for which we are making plt entries.  The
         first entry in the procedure linkage table is reserved.  */
-      plt_index = h->root.u.def.value / PLT_ENTRY_SIZE - 1;
+      plt_index = h->plt_offset / PLT_ENTRY_SIZE - 1;
 
       /* Get the offset into the .got table of the entry that
         corresponds to this function.  Each .got entry is 4 bytes.
@@ -726,23 +1280,34 @@ elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
       got_offset = (plt_index + 3) * 4;
 
       /* Fill in the entry in the procedure linkage table.  */
-      memcpy (splt->contents + h->root.u.def.value, elf_i386_plt_entry,
-             PLT_ENTRY_SIZE);
-      bfd_put_32 (output_bfd,
-                 (sgot->output_section->vma
-                  + sgot->output_offset
-                  + got_offset),
-                 splt->contents + h->root.u.def.value + 2);
+      if (! info->shared)
+       {
+         memcpy (splt->contents + h->plt_offset, elf_i386_plt_entry,
+                 PLT_ENTRY_SIZE);
+         bfd_put_32 (output_bfd,
+                     (sgot->output_section->vma
+                      + sgot->output_offset
+                      + got_offset),
+                     splt->contents + h->plt_offset + 2);
+       }
+      else
+       {
+         memcpy (splt->contents + h->plt_offset, elf_i386_pic_plt_entry,
+                 PLT_ENTRY_SIZE);
+         bfd_put_32 (output_bfd, got_offset,
+                     splt->contents + h->plt_offset + 2);
+       }
+
       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
-                 splt->contents + h->root.u.def.value + 7);
-      bfd_put_32 (output_bfd, - (h->root.u.def.value + PLT_ENTRY_SIZE),
-                 splt->contents + h->root.u.def.value + 12);
+                 splt->contents + h->plt_offset + 7);
+      bfd_put_32 (output_bfd, - (h->plt_offset + PLT_ENTRY_SIZE),
+                 splt->contents + h->plt_offset + 12);
 
       /* Fill in the entry in the global offset table.  */
       bfd_put_32 (output_bfd,
                  (splt->output_section->vma
                   + splt->output_offset
-                  + h->root.u.def.value
+                  + h->plt_offset
                   + 6),
                  sgot->contents + got_offset);
 
@@ -755,34 +1320,70 @@ elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym)
                                ((Elf32_External_Rel *) srel->contents
                                 + plt_index));
 
-      /* Mark the symbol as undefined, rather than as defined in the
-        .plt section.  Leave the value alone.  */
-      sym->st_shndx = SHN_UNDEF;
+      if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
+       {
+         /* Mark the symbol as undefined, rather than as defined in
+            the .plt section.  Leave the value alone.  */
+         sym->st_shndx = SHN_UNDEF;
+       }
     }
-  else
+
+  if (h->got_offset != (bfd_vma) -1)
     {
-      /* This is not a function.  We have already allocated memory for
-        it in the .bss section (via .dynbss).  All we have to do here
-        is create a COPY reloc if required.  */
-      if (h->copy_offset != (bfd_vma) -1)
-       {
-         asection *s;
-         Elf_Internal_Rel rel;
+      asection *sgot;
+      asection *srel;
+      Elf_Internal_Rel rel;
 
-         s = bfd_get_section_by_name (h->root.u.def.section->owner,
-                                      ".rel.bss");
-         BFD_ASSERT (s != NULL);
+      /* This symbol has an entry in the global offset table.  Set it
+        up.  */
+      
+      BFD_ASSERT (h->dynindx != -1);
 
-         rel.r_offset = (h->root.u.def.value
-                         + h->root.u.def.section->output_section->vma
-                         + h->root.u.def.section->output_offset);
-         rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
-         bfd_elf32_swap_reloc_out (output_bfd, &rel,
-                                   ((Elf32_External_Rel *)
-                                    (s->contents + h->copy_offset)));
-       }
+      sgot = bfd_get_section_by_name (dynobj, ".got");
+      srel = bfd_get_section_by_name (dynobj, ".rel.got");
+      BFD_ASSERT (sgot != NULL && srel != NULL);
+
+      bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset);
+
+      rel.r_offset = (sgot->output_section->vma
+                     + sgot->output_offset
+                     + h->got_offset);
+      rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
+      bfd_elf32_swap_reloc_out (output_bfd, &rel,
+                               ((Elf32_External_Rel *) srel->contents
+                                + srel->reloc_count));
+      ++srel->reloc_count;
+    }
+
+  if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
+    {
+      asection *s;
+      Elf_Internal_Rel rel;
+
+      /* This symbol needs a copy reloc.  Set it up.  */
+
+      BFD_ASSERT (h->dynindx != -1
+                 && h->root.type == bfd_link_hash_defined);
+
+      s = bfd_get_section_by_name (h->root.u.def.section->owner,
+                                  ".rel.bss");
+      BFD_ASSERT (s != NULL);
+
+      rel.r_offset = (h->root.u.def.value
+                     + h->root.u.def.section->output_section->vma
+                     + h->root.u.def.section->output_offset);
+      rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
+      bfd_elf32_swap_reloc_out (output_bfd, &rel,
+                               ((Elf32_External_Rel *) s->contents
+                                + s->reloc_count));
+      ++s->reloc_count;
     }
 
+  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
+  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
+      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
+    sym->st_shndx = SHN_ABS;
+
   return true;
 }
 
@@ -793,14 +1394,17 @@ elf_i386_finish_dynamic_sections (output_bfd, info)
      bfd *output_bfd;
      struct bfd_link_info *info;
 {
+  bfd *dynobj;
   asection *splt;
   asection *sgot;
   asection *sdyn;
   Elf32_External_Dyn *dyncon, *dynconend;
 
-  splt = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".plt");
-  sgot = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".got");
-  sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".dynamic");
+  dynobj = elf_hash_table (info)->dynobj;
+
+  splt = bfd_get_section_by_name (dynobj, ".plt");
+  sgot = bfd_get_section_by_name (dynobj, ".got.plt");
+  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
   BFD_ASSERT (splt != NULL && sgot != NULL && sdyn != NULL);
 
   dyncon = (Elf32_External_Dyn *) sdyn->contents;
@@ -809,56 +1413,75 @@ elf_i386_finish_dynamic_sections (output_bfd, info)
     {
       Elf_Internal_Dyn dyn;
       const char *name;
-      boolean size;
-
-      bfd_elf32_swap_dyn_in (elf_hash_table (info)->dynobj, dyncon, &dyn);
+      asection *s;
 
-      /* My reading of the SVR4 ABI indicates that the procedure
-        linkage table relocs (DT_JMPREL) should be included in the
-        overall relocs (DT_REL).  This is what Solaris does.
-        However, UnixWare can not handle that case.  Therefore, we
-        override the DT_REL and DT_RELSZ entries here to make them
-        not include the JMPREL relocs.  */
+      bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
 
       switch (dyn.d_tag)
        {
-       case DT_PLTGOT:   name = ".got"; size = false; break;
-       case DT_PLTRELSZ: name = ".rel.plt"; size = true; break;
-       case DT_JMPREL:   name = ".rel.plt"; size = false; break;
-       case DT_REL:      name = ".rel.bss"; size = false; break;
-       case DT_RELSZ:    name = ".rel.bss"; size = true; break;
-       default:          name = NULL; size = false; break;
-       }
-
-      if (name != NULL)
-       {
-         asection *s;
-
+       default:
+         break;
+
+       case DT_PLTGOT:
+         name = ".got";
+         goto get_vma;
+       case DT_JMPREL:
+         name = ".rel.plt";
+       get_vma:
          s = bfd_get_section_by_name (output_bfd, name);
          BFD_ASSERT (s != NULL);
-         if (! size)
-           dyn.d_un.d_ptr = s->vma;
+         dyn.d_un.d_ptr = s->vma;
+         bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
+         break;
+
+       case DT_PLTRELSZ:
+         s = bfd_get_section_by_name (output_bfd, ".rel.plt");
+         BFD_ASSERT (s != NULL);
+         if (s->_cooked_size != 0)
+           dyn.d_un.d_val = s->_cooked_size;
          else
+           dyn.d_un.d_val = s->_raw_size;
+         bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
+         break;
+
+       case DT_RELSZ:
+         /* My reading of the SVR4 ABI indicates that the procedure
+            linkage table relocs (DT_JMPREL) should be included in
+            the overall relocs (DT_REL).  This is what Solaris does.
+            However, UnixWare can not handle that case.  Therefore,
+            we override the DT_RELSZ entry here to make it not
+            include the JMPREL relocs.  Since the linker script
+            arranges for .rel.plt to follow all other relocation
+            sections, we don't have to worry about changing the
+            DT_REL entry.  */
+         s = bfd_get_section_by_name (output_bfd, ".rel.plt");
+         if (s != NULL)
            {
              if (s->_cooked_size != 0)
-               dyn.d_un.d_val = s->_cooked_size;
+               dyn.d_un.d_val -= s->_cooked_size;
              else
-               dyn.d_un.d_val = s->_raw_size;
+               dyn.d_un.d_val -= s->_raw_size;
            }
          bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
+         break;
        }
     }
 
   /* Fill in the first entry in the procedure linkage table.  */
   if (splt->_raw_size > 0)
     {
-      memcpy (splt->contents, elf_i386_plt0_entry, PLT_ENTRY_SIZE);
-      bfd_put_32 (output_bfd,
-                 sgot->output_section->vma + sgot->output_offset + 4,
-                 splt->contents + 2);
-      bfd_put_32 (output_bfd,
-                 sgot->output_section->vma + sgot->output_offset + 8,
-                 splt->contents + 8);
+      if (info->shared)
+       memcpy (splt->contents, elf_i386_pic_plt0_entry, PLT_ENTRY_SIZE);
+      else
+       {
+         memcpy (splt->contents, elf_i386_plt0_entry, PLT_ENTRY_SIZE);
+         bfd_put_32 (output_bfd,
+                     sgot->output_section->vma + sgot->output_offset + 4,
+                     splt->contents + 2);
+         bfd_put_32 (output_bfd,
+                     sgot->output_section->vma + sgot->output_offset + 8,
+                     splt->contents + 8);
+       }
     }
 
   /* Fill in the first three entries in the global offset table.  */
@@ -890,6 +1513,7 @@ elf_i386_finish_dynamic_sections (output_bfd, info)
 #define ELF_MAXPAGESIZE                        0x1000
 #define elf_backend_create_dynamic_sections \
                                        elf_i386_create_dynamic_sections
+#define elf_backend_check_relocs       elf_i386_check_relocs
 #define elf_backend_adjust_dynamic_symbol \
                                        elf_i386_adjust_dynamic_symbol
 #define elf_backend_size_dynamic_sections \
index 60b3a49..5249ad3 100644 (file)
@@ -140,8 +140,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 /* Forward declarations of static functions */
 
-static unsigned long bfd_add_to_strtab
-  PARAMS ((bfd *, struct strtab *, const char *));
+static struct bfd_strtab_hash *elf_stringtab_init PARAMS ((void));
 static asection *section_from_elf_index PARAMS ((bfd *, unsigned int));
 
 static int elf_section_from_bfd_section PARAMS ((bfd *, struct sec *));
@@ -168,12 +167,12 @@ static file_ptr map_program_segments
   PARAMS ((bfd *, file_ptr, Elf_Internal_Shdr *, bfd_size_type));
 
 static boolean elf_map_symbols PARAMS ((bfd *));
-static boolean swap_out_syms PARAMS ((bfd *));
+static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **));
 
 static boolean bfd_section_from_shdr PARAMS ((bfd *, unsigned int shindex));
 
 #ifdef DEBUG
-static void elf_debug_section PARAMS ((char *, int, Elf_Internal_Shdr *));
+static void elf_debug_section PARAMS ((int, Elf_Internal_Shdr *));
 static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *));
 #endif
 
@@ -427,85 +426,27 @@ elf_swap_dyn_out (abfd, src, dst)
   put_word (abfd, src->d_un.d_val, dst->d_un.d_val);
 }
 \f
-/* String table creation/manipulation routines */
+/* Allocate an ELF string table--force the first byte to be zero.  */
 
-static struct strtab *
-bfd_new_strtab (abfd)
-     bfd *abfd;
+static struct bfd_strtab_hash *
+elf_stringtab_init ()
 {
-  struct strtab *ss;
+  struct bfd_strtab_hash *ret;
 
-  ss = (struct strtab *) malloc (sizeof (struct strtab));
-  if (!ss)
-    {
-      bfd_set_error (bfd_error_no_memory);
-      return NULL;
-    }
-  ss->tab = malloc (1);
-  if (!ss->tab)
+  ret = _bfd_stringtab_init ();
+  if (ret != NULL)
     {
-      bfd_set_error (bfd_error_no_memory);
-      return NULL;
-    }
-  *ss->tab = 0;
-  ss->nentries = 0;
-  ss->length = 1;
+      bfd_size_type loc;
 
-  return ss;
-}
-
-static unsigned long
-bfd_add_to_strtab (abfd, ss, str)
-     bfd *abfd;
-     struct strtab *ss;
-     const char *str;
-{
-  /* should search first, but for now: */
-  /* include the trailing NUL */
-  int ln = strlen (str) + 1;
-
-  /* FIXME: This is slow.  Also, we could combine this with the a.out
-     string table building and use a hash table, although it might not
-     be worth it since ELF symbols don't include debugging information
-     and thus have much less overlap.  */
-  ss->tab = realloc (ss->tab, ss->length + ln);
-  if (ss->tab == NULL)
-    {
-      bfd_set_error (bfd_error_no_memory);
-      return (unsigned long) -1;
+      loc = _bfd_stringtab_add (ret, "", true, false);
+      BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
+      if (loc == (bfd_size_type) -1)
+       {
+         _bfd_stringtab_free (ret);
+         ret = NULL;
+       }
     }
-
-  strcpy (ss->tab + ss->length, str);
-  ss->nentries++;
-  ss->length += ln;
-
-  return ss->length - ln;
-}
-
-static int
-bfd_add_2_to_strtab (abfd, ss, str, str2)
-     bfd *abfd;
-     struct strtab *ss;
-     char *str;
-     CONST char *str2;
-{
-  /* should search first, but for now: */
-  /* include the trailing NUL */
-  int ln = strlen (str) + strlen (str2) + 1;
-
-  /* should this be using obstacks? */
-  if (ss->length)
-    ss->tab = realloc (ss->tab, ss->length + ln);
-  else
-    ss->tab = malloc (ln);
-
-  BFD_ASSERT (ss->tab != 0);   /* FIXME */
-  strcpy (ss->tab + ss->length, str);
-  strcpy (ss->tab + ss->length + strlen (str), str2);
-  ss->nentries++;
-  ss->length += ln;
-
-  return ss->length - ln;
+  return ret;
 }
 \f
 /* ELF .o/exec file reading */
@@ -1160,19 +1101,31 @@ write_relocs (abfd, sec, xxx)
 
 /*ARGSUSED*/
 static void
-elf_fake_sections (abfd, asect, ignore)
+elf_fake_sections (abfd, asect, failedptrarg)
      bfd *abfd;
      asection *asect;
-     PTR ignore;
+     PTR failedptrarg;
 {
+  boolean *failedptr = (boolean *) failedptrarg;
   Elf_Internal_Shdr *this_hdr;
 
+  if (*failedptr)
+    {
+      /* We already failed; just get out of the bfd_map_over_sections
+         loop.  */
+      return;
+    }
+
   this_hdr = &elf_section_data (asect)->this_hdr;
 
-  this_hdr->sh_name = bfd_add_to_strtab (abfd, elf_shstrtab (abfd),
-                                        asect->name);
+  this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
+                                                         asect->name,
+                                                         true, false);
   if (this_hdr->sh_name == (unsigned long) -1)
-    abort (); /* FIXME */
+    {
+      *failedptr = true;
+      return;
+    }
 
   this_hdr->sh_flags = 0;
   if ((asect->flags & SEC_ALLOC) != 0)
@@ -1261,12 +1214,25 @@ elf_fake_sections (abfd, asect, ignore)
     {
       Elf_Internal_Shdr *rela_hdr;
       int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
+      char *name;
 
       rela_hdr = &elf_section_data (asect)->rel_hdr;
+      name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
+      if (name == NULL)
+       {
+         bfd_set_error (bfd_error_no_memory);
+         *failedptr = true;
+         return;
+       }
+      sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
       rela_hdr->sh_name =
-       bfd_add_2_to_strtab (abfd, elf_shstrtab (abfd),
-                            use_rela_p ? ".rela" : ".rel",
-                            asect->name);
+       (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
+                                          true, false);
+      if (rela_hdr->sh_name == (unsigned int) -1)
+       {
+         *failedptr = true;
+         return;
+       }
       rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
       rela_hdr->sh_entsize = (use_rela_p
                              ? sizeof (Elf_External_Rela)
@@ -1307,8 +1273,7 @@ assign_section_numbers (abfd)
 
   t->shstrtab_section = section_number++;
   elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
-  t->shstrtab_hdr.sh_size = elf_shstrtab (abfd)->length;
-  t->shstrtab_hdr.contents = (PTR) elf_shstrtab (abfd)->tab;
+  t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
 
   if (abfd->symcount > 0)
     {
@@ -1645,6 +1610,8 @@ elf_compute_section_file_positions (abfd, link_info)
      struct bfd_link_info *link_info;
 {
   struct elf_backend_data *bed = get_elf_backend_data (abfd);
+  boolean failed;
+  struct bfd_strtab_hash *strtab;
   Elf_Internal_Shdr *shstrtab_hdr;
 
   if (abfd->output_has_begun)
@@ -1657,7 +1624,10 @@ elf_compute_section_file_positions (abfd, link_info)
   if (! prep_headers (abfd))
     return false;
 
-  bfd_map_over_sections (abfd, elf_fake_sections, 0);
+  failed = false;
+  bfd_map_over_sections (abfd, elf_fake_sections, &failed);
+  if (failed)
+    return false;
 
   if (!assign_section_numbers (abfd))
     return false;
@@ -1665,7 +1635,7 @@ elf_compute_section_file_positions (abfd, link_info)
   /* The backend linker builds symbol table information itself.  */
   if (link_info == NULL)
     {
-      if (! swap_out_syms (abfd))
+      if (! swap_out_syms (abfd, &strtab))
        return false;
     }
 
@@ -1674,18 +1644,28 @@ elf_compute_section_file_positions (abfd, link_info)
   shstrtab_hdr->sh_type = SHT_STRTAB;
   shstrtab_hdr->sh_flags = 0;
   shstrtab_hdr->sh_addr = 0;
-  shstrtab_hdr->sh_size = elf_shstrtab (abfd)->length;
+  shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
   shstrtab_hdr->sh_entsize = 0;
   shstrtab_hdr->sh_link = 0;
   shstrtab_hdr->sh_info = 0;
   /* sh_offset is set in assign_file_positions_for_symtabs_and_strtabs.  */
   shstrtab_hdr->sh_addralign = 1;
-  shstrtab_hdr->contents = (PTR) elf_shstrtab (abfd)->tab;
 
   if (!assign_file_positions_except_relocs (abfd,
                                            link_info == NULL ? true : false))
     return false;
 
+  if (link_info == NULL)
+    {
+      /* Now that we know where the .strtab section goes, write it
+         out.  */
+      if ((bfd_seek (abfd, elf_tdata (abfd)->strtab_hdr.sh_offset, SEEK_SET)
+          != 0)
+         || ! _bfd_stringtab_emit (abfd, strtab))
+       return false;
+      _bfd_stringtab_free (strtab);
+    }
+
   abfd->output_has_begun = true;
 
   return true;
@@ -2144,13 +2124,13 @@ prep_headers (abfd)
   Elf_Internal_Phdr *i_phdrp = 0;      /* Program header table, internal form */
   Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
   int count;
-  struct strtab *shstrtab;
+  struct bfd_strtab_hash *shstrtab;
 
   i_ehdrp = elf_elfheader (abfd);
   i_shdrp = elf_elfsections (abfd);
 
-  shstrtab = bfd_new_strtab (abfd);
-  if (!shstrtab)
+  shstrtab = elf_stringtab_init ();
+  if (shstrtab == NULL)
     return false;
 
   elf_shstrtab (abfd) = shstrtab;
@@ -2245,12 +2225,12 @@ prep_headers (abfd)
       i_ehdrp->e_phoff = 0;
     }
 
-  elf_tdata (abfd)->symtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
-                                                           ".symtab");
-  elf_tdata (abfd)->strtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
-                                                           ".strtab");
-  elf_tdata (abfd)->shstrtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
-                                                             ".shstrtab");
+  elf_tdata (abfd)->symtab_hdr.sh_name =
+    (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
+  elf_tdata (abfd)->strtab_hdr.sh_name =
+    (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
+  elf_tdata (abfd)->shstrtab_hdr.sh_name =
+    (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
       || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
@@ -2260,8 +2240,9 @@ prep_headers (abfd)
 }
 
 static boolean
-swap_out_syms (abfd)
+swap_out_syms (abfd, sttp)
      bfd *abfd;
+     struct bfd_strtab_hash **sttp;
 {
   if (!elf_map_symbols (abfd))
     return false;
@@ -2270,14 +2251,16 @@ swap_out_syms (abfd)
   {
     int symcount = bfd_get_symcount (abfd);
     asymbol **syms = bfd_get_outsymbols (abfd);
-    struct strtab *stt = bfd_new_strtab (abfd);
+    struct bfd_strtab_hash *stt;
     Elf_Internal_Shdr *symtab_hdr;
     Elf_Internal_Shdr *symstrtab_hdr;
     Elf_External_Sym *outbound_syms;
     int idx;
 
-    if (!stt)
+    stt = elf_stringtab_init ();
+    if (stt == NULL)
       return false;
+
     symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
     symtab_hdr->sh_type = SHT_SYMTAB;
     symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
@@ -2318,7 +2301,9 @@ swap_out_syms (abfd)
          sym.st_name = 0;
        else
          {
-           sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name);
+           sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
+                                                             syms[idx]->name,
+                                                             true, false);
            if (sym.st_name == (unsigned long) -1)
              return false;
          }
@@ -2406,8 +2391,9 @@ swap_out_syms (abfd)
       }
 
     symtab_hdr->contents = (PTR) outbound_syms;
-    symstrtab_hdr->contents = (PTR) stt->tab;
-    symstrtab_hdr->sh_size = stt->length;
+
+    *sttp = stt;
+    symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
     symstrtab_hdr->sh_type = SHT_STRTAB;
 
     symstrtab_hdr->sh_flags = 0;
@@ -2430,11 +2416,9 @@ write_shdrs_and_ehdr (abfd)
   Elf_External_Shdr *x_shdrp;  /* Section header table, external form */
   Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
   unsigned int count;
-  struct strtab *shstrtab;
 
   i_ehdrp = elf_elfheader (abfd);
   i_shdrp = elf_elfsections (abfd);
-  shstrtab = elf_shstrtab (abfd);
 
   /* swap the header before spitting it out... */
 
@@ -2459,8 +2443,7 @@ write_shdrs_and_ehdr (abfd)
   for (count = 0; count < i_ehdrp->e_shnum; count++)
     {
 #if DEBUG & 2
-      elf_debug_section (shstrtab->tab + i_shdrp[count]->sh_name, count,
-                        i_shdrp[count]);
+      elf_debug_section (count, i_shdrp[count]);
 #endif
       elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count);
     }
@@ -2537,6 +2520,11 @@ NAME(bfd_elf,write_object_contents) (abfd)
        }
     }
 
+  /* Write out the section header names.  */
+  if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
+      || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
+    return false;
+
   if (bed->elf_backend_final_write_processing)
     (*bed->elf_backend_final_write_processing) (abfd,
                                                elf_tdata (abfd)->linker);
@@ -3001,12 +2989,13 @@ elf_slurp_reloc_table (abfd, asect, symbols)
 
 #ifdef DEBUG
 static void
-elf_debug_section (str, num, hdr)
-     char *str;
+elf_debug_section (num, hdr)
      int num;
      Elf_Internal_Shdr *hdr;
 {
-  fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, str, (long) hdr);
+  fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num,
+          hdr->bfd_section != NULL ? hfd->bfd_section->name : "",
+          (long) hdr);
   fprintf (stderr,
           "sh_name      = %ld\tsh_type      = %ld\tsh_flags     = %ld\n",
           (long) hdr->sh_name,
@@ -3895,9 +3884,10 @@ elf_link_record_dynamic_symbol (info, h)
     {
       h->dynindx = elf_hash_table (info)->dynsymcount;
       ++elf_hash_table (info)->dynsymcount;
-      h->dynstr_index = bfd_add_to_strtab (elf_hash_table (info)->dynobj,
-                                          elf_hash_table (info)->dynstr,
-                                          h->root.root.string);
+      h->dynstr_index =
+       (unsigned long) _bfd_stringtab_add (elf_hash_table (info)->dynstr,
+                                           h->root.root.string,
+                                           true, false);
       if (h->dynstr_index == (unsigned long) -1)
        return false;
     }
@@ -4002,7 +3992,7 @@ elf_link_add_object_symbols (abfd, info)
     {
       asection *s;
       const char *name;
-      unsigned long strindex;
+      bfd_size_type strindex;
 
       dynamic = true;
 
@@ -4091,10 +4081,9 @@ elf_link_add_object_symbols (abfd, info)
        }
 
       /* Add a DT_NEEDED entry for this dynamic object.  */
-      strindex = bfd_add_to_strtab (abfd,
-                                   elf_hash_table (info)->dynstr,
-                                   name);
-      if (strindex == (unsigned long) -1)
+      strindex = _bfd_stringtab_add (elf_hash_table (info)->dynstr, name,
+                                    true, false);
+      if (strindex == (bfd_size_type) -1)
        goto error_return;
       if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
        goto error_return;
@@ -4525,7 +4514,7 @@ elf_link_create_dynamic_sections (abfd, info)
     return false;
 
   /* Create a strtab to hold the dynamic symbol names.  */
-  elf_hash_table (info)->dynstr = bfd_new_strtab (abfd);
+  elf_hash_table (info)->dynstr = elf_stringtab_init ();
   if (elf_hash_table (info)->dynstr == NULL)
     return false;
 
@@ -4864,28 +4853,29 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath, info,
 
   if (soname != NULL)
     {
-      unsigned long indx;
+      bfd_size_type indx;
 
-      indx = bfd_add_to_strtab (dynobj, elf_hash_table (info)->dynstr, soname);
-      if (indx == (unsigned long) -1
+      indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, soname,
+                                true, true);
+      if (indx == (bfd_size_type) -1
          || ! elf_add_dynamic_entry (info, DT_SONAME, indx))
        return false;
     }      
 
   if (rpath != NULL)
     {
-      unsigned long indx;
+      bfd_size_type indx;
 
-      indx = bfd_add_to_strtab (dynobj, elf_hash_table (info)->dynstr, rpath);
-      if (indx == (unsigned long) -1
+      indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, rpath,
+                                true, true);
+      if (indx == (bfd_size_type) -1
          || ! elf_add_dynamic_entry (info, DT_RPATH, indx))
        return false;
     }
 
   s = bfd_get_section_by_name (dynobj, ".dynstr");
   BFD_ASSERT (s != NULL);
-  s->_raw_size = elf_hash_table (info)->dynstr->length;
-  s->contents = (unsigned char *) elf_hash_table (info)->dynstr->tab;
+  s->_raw_size = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
 
   /* Find all symbols which were defined in a dynamic object and make
      the backend pick a reasonable value for them.  */
@@ -4910,7 +4900,8 @@ NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath, info,
       || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
       || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
       || ! elf_add_dynamic_entry (info, DT_STRSZ,
-                                 elf_hash_table (info)->dynstr->length)
+                                 _bfd_stringtab_size (elf_hash_table (info)
+                                                      ->dynstr))
       || ! elf_add_dynamic_entry (info, DT_SYMENT,
                                  sizeof (Elf_External_Sym)))
     return false;
@@ -5032,7 +5023,7 @@ struct elf_final_link_info
   /* Output BFD.  */
   bfd *output_bfd;
   /* Symbol string table.  */
-  struct strtab *symstrtab;
+  struct bfd_strtab_hash *symstrtab;
   /* .dynsym section.  */
   asection *dynsym_sec;
   /* .hash section.  */
@@ -5106,7 +5097,7 @@ elf_bfd_final_link (abfd, info)
 
   finfo.info = info;
   finfo.output_bfd = abfd;
-  finfo.symstrtab = bfd_new_strtab (abfd);
+  finfo.symstrtab = elf_stringtab_init ();
   if (finfo.symstrtab == NULL)
     return false;
   if (dynobj == NULL)
@@ -5443,23 +5434,27 @@ elf_bfd_final_link (abfd, info)
   /* Now we know the size of the symtab section.  */
   off += symtab_hdr->sh_size;
 
-  /* Finish up the symbol string table (.strtab) section.  */
+  /* Finish up and write out the symbol string table (.strtab)
+     section.  */
   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
   /* sh_name was set in prep_headers.  */
   symstrtab_hdr->sh_type = SHT_STRTAB;
   symstrtab_hdr->sh_flags = 0;
   symstrtab_hdr->sh_addr = 0;
-  symstrtab_hdr->sh_size = finfo.symstrtab->length;
+  symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
   symstrtab_hdr->sh_entsize = 0;
   symstrtab_hdr->sh_link = 0;
   symstrtab_hdr->sh_info = 0;
   /* sh_offset is set just below.  */
   symstrtab_hdr->sh_addralign = 1;
-  symstrtab_hdr->contents = (PTR) finfo.symstrtab->tab;
 
   off = assign_file_position_for_section (symstrtab_hdr, off, true);
   elf_tdata (abfd)->next_file_pos = off;
 
+  if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
+      || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
+    return false;
+
   /* Adjust the relocs to have the correct symbol indices.  */
   for (o = abfd->sections; o != NULL; o = o->next)
     {
@@ -5602,13 +5597,32 @@ elf_bfd_final_link (abfd, info)
                  This test is fragile.  */
              continue;
            }
-         if (! bfd_set_section_contents (abfd, o->output_section,
-                                         o->contents, o->output_offset,
-                                         o->_raw_size))
-           goto error_return;
+         if ((elf_section_data (o->output_section)->this_hdr.sh_type
+              != SHT_STRTAB)
+             || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
+           {
+             if (! bfd_set_section_contents (abfd, o->output_section,
+                                             o->contents, o->output_offset,
+                                             o->_raw_size))
+               goto error_return;
+           }
+         else
+           {
+             file_ptr off;
+
+             /* The contents of the .dynstr section are actually in a
+                 stringtab.  */
+             off = elf_section_data (o->output_section)->this_hdr.sh_offset;
+             if (bfd_seek (abfd, off, SEEK_SET) != 0
+                 || ! _bfd_stringtab_emit (abfd,
+                                           elf_hash_table (info)->dynstr))
+               goto error_return;
+           }
        }
     }
 
+  if (finfo.symstrtab != NULL)
+    _bfd_stringtab_free (finfo.symstrtab);
   if (finfo.contents != NULL)
     free (finfo.contents);
   if (finfo.external_relocs != NULL)
@@ -5637,6 +5651,8 @@ elf_bfd_final_link (abfd, info)
   return true;
 
  error_return:
+  if (finfo.symstrtab != NULL)
+    _bfd_stringtab_free (finfo.symstrtab);
   if (finfo.contents != NULL)
     free (finfo.contents);
   if (finfo.external_relocs != NULL)
@@ -5691,8 +5707,9 @@ elf_link_output_sym (finfo, name, elfsym, input_sec)
     elfsym->st_name = 0;
   else
     {
-      elfsym->st_name = bfd_add_to_strtab (finfo->output_bfd,
-                                          finfo->symstrtab, name);
+      elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
+                                                           name, true,
+                                                           false);
       if (elfsym->st_name == (unsigned long) -1)
        return false;
     }
@@ -5802,12 +5819,12 @@ elf_link_output_extsym (h, data)
 
     case bfd_link_hash_defined:
       {
-
        input_sec = h->root.u.def.section;
        if (input_sec->output_section != NULL)
          {
-           sym.st_shndx = elf_section_from_bfd_section (finfo->output_bfd,
-                                                        input_sec->output_section);
+           sym.st_shndx =
+             elf_section_from_bfd_section (finfo->output_bfd,
+                                           input_sec->output_section);
            if (sym.st_shndx == (unsigned short) -1)
              {
                /* FIXME: No way to handle errors.  */
@@ -5912,8 +5929,7 @@ elf_link_input_bfd (finfo, input_bfd)
   boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
                                       bfd *, asection *, bfd_byte *,
                                       Elf_Internal_Rela *,
-                                      Elf_Internal_Sym *,
-                                      asection **, char *));
+                                      Elf_Internal_Sym *, asection **));
   bfd *output_bfd;
   Elf_Internal_Shdr *symtab_hdr;
   size_t locsymcount;
@@ -5966,7 +5982,7 @@ elf_link_input_bfd (finfo, input_bfd)
     {
       asection *isec;
       const char *name;
-      bfd_vma oldval;
+      Elf_Internal_Sym osym;
 
       elf_swap_symbol_in (input_bfd, esym, isym);
       *pindex = -1;
@@ -6035,10 +6051,12 @@ elf_link_input_bfd (finfo, input_bfd)
 
       /* If we get here, we are going to output this symbol.  */
 
+      osym = *isym;
+
       /* Adjust the section index for the output file.  */
-      isym->st_shndx = elf_section_from_bfd_section (output_bfd,
-                                                    isec->output_section);
-      if (isym->st_shndx == (unsigned short) -1)
+      osym.st_shndx = elf_section_from_bfd_section (output_bfd,
+                                                   isec->output_section);
+      if (osym.st_shndx == (unsigned short) -1)
        return false;
 
       *pindex = output_bfd->symcount;
@@ -6050,16 +6068,12 @@ elf_link_input_bfd (finfo, input_bfd)
         we assume that they also have a reasonable value for
         output_section.  Any special sections must be set up to meet
         these requirements.  */
-      oldval = isym->st_value;
-      isym->st_value += isec->output_offset;
+      osym.st_value += isec->output_offset;
       if (! finfo->info->relocateable)
-       isym->st_value += isec->output_section->vma;
+       osym.st_value += isec->output_section->vma;
 
-      if (! elf_link_output_sym (finfo, name, isym, isec))
+      if (! elf_link_output_sym (finfo, name, &osym, isec))
        return false;
-
-      /* Restore the old value for reloc handling.  */
-      isym->st_value = oldval;
     }
 
   /* Relocate the contents of each section.  */
@@ -6119,8 +6133,7 @@ elf_link_input_bfd (finfo, input_bfd)
                                     finfo->contents,
                                     internal_relocs,
                                     finfo->internal_syms,
-                                    finfo->sections,
-                                    finfo->symstrtab->tab))
+                                    finfo->sections))
            return false;
 
          if (finfo->info->relocateable)
index 5000e20..a92d166 100644 (file)
@@ -137,7 +137,7 @@ struct elf_link_hash_table
   size_t dynsymcount;
   /* The string table of dynamic symbols, which becomes the .dynstr
      section.  */
-  struct strtab *dynstr;
+  struct bfd_strtab_hash *dynstr;
   /* The number of buckets in the hash table in the .hash section.
      This is based on the number of dynamic symbols.  */
   size_t bucketcount;
@@ -357,7 +357,7 @@ struct elf_backend_data
     PARAMS ((bfd *output_bfd, struct bfd_link_info *info,
             bfd *input_bfd, asection *input_section, bfd_byte *contents,
             Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
-            asection **local_sections, char *output_names));
+            asection **local_sections));
 
   /* The FINISH_DYNAMIC_SYMBOL function is called by the ELF backend
      linker just before it writes a symbol out to the .dynsym section.
@@ -429,13 +429,6 @@ struct bfd_elf_section_data {
 #define get_elf_backend_data(abfd) \
   ((struct elf_backend_data *) (abfd)->xvec->backend_data)
 
-struct strtab
-{
-  char *tab;
-  int nentries;
-  int length;
-};
-
 /* Some private data is stashed away for future use using the tdata pointer
    in the bfd structure.  */
 
@@ -444,7 +437,7 @@ struct elf_obj_tdata
   Elf_Internal_Ehdr elf_header[1];     /* Actual data, but ref like ptr */
   Elf_Internal_Shdr **elf_sect_ptr;
   Elf_Internal_Phdr *phdr;
-  struct strtab *strtab_ptr;
+  struct bfd_strtab_hash *strtab_ptr;
   int num_locals;
   int num_globals;
   Elf_Sym_Extra *sym_extra;