add LOCAL SECDIFF relocation for m32 mach-o
[deliverable/binutils-gdb.git] / bfd / elf32-i386.c
index 38c7c5a61098339926fa754338f9aadc5b90e32a..d1873051b4d9bc692b86b23b6277e3718f00864f 100644 (file)
@@ -806,6 +806,12 @@ struct elf_i386_link_hash_table
 
   /* The index of the next unused R_386_TLS_DESC slot in .rel.plt.  */
   bfd_vma next_tls_desc_index;
+
+  /* The index of the next unused R_386_JUMP_SLOT slot in .rel.plt.  */
+  bfd_vma next_jump_slot_index;
+
+  /* The index of the next unused R_386_IRELATIVE slot in .rel.plt.  */
+  bfd_vma next_irelative_index;
 };
 
 /* Get the i386 ELF linker hash table from a link_info structure.  */
@@ -946,6 +952,8 @@ elf_i386_link_hash_table_create (bfd *abfd)
   ret->sym_cache.abfd = NULL;
   ret->srelplt2 = NULL;
   ret->tls_module_base = NULL;
+  ret->next_jump_slot_index = 0;
+  ret->next_irelative_index = 0;
 
   ret->loc_hash_table = htab_try_create (1024,
                                         elf_i386_local_htab_hash,
@@ -1094,13 +1102,6 @@ elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
     _bfd_elf_link_hash_copy_indirect (info, dir, ind);
 }
 
-typedef union
-  {
-    unsigned char c[2];
-    uint16_t i;
-  }
-i386_opcode16;
-
 /* Return TRUE if the TLS access code sequence support transition
    from R_TYPE.  */
 
@@ -1271,8 +1272,8 @@ elf_i386_check_tls_transition (bfd *abfd, asection *sec,
       if (offset + 2 <= sec->size)
        {
          /* Make sure that it's a call *x@tlsdesc(%rax).  */
-         static i386_opcode16 call = { { 0xff, 0x10 } };
-         return bfd_get_16 (abfd, contents + offset) == call.i;
+         static const unsigned char call[] = { 0xff, 0x10 };
+         return memcmp (contents + offset, call, 2) == 0;
        }
 
       return FALSE;
@@ -2282,7 +2283,7 @@ elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
 
          /* We also need to make an entry in the .rel.plt section.  */
          htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
-         htab->next_tls_desc_index++;
+         htab->elf.srelplt->reloc_count++;
 
          if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks
               && !info->shared)
@@ -2707,9 +2708,19 @@ elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
      incremented.  However, when we reserve space for TLS descriptors,
      it's not incremented, so in order to compute the space reserved
      for them, it suffices to multiply the reloc count by the jump
-     slot size.  */
+     slot size.
+     
+     PR ld/13302: We start next_irelative_index at the end of .rela.plt
+     so that R_386_IRELATIVE entries come last.  */
   if (htab->elf.srelplt)
-    htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
+    {
+      htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
+      htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
+      htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
+    }
+  else if (htab->elf.irelplt)
+    htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
+
 
   if (htab->elf.sgotplt)
     {
@@ -3239,13 +3250,14 @@ elf_i386_relocate_section (bfd *output_bfd,
 
            case R_386_32:
              /* Generate dynamic relcoation only when there is a
-                non-GOF reference in a shared object.  */
+                non-GOT reference in a shared object.  */
              if (info->shared && h->non_got_ref)
                {
                  Elf_Internal_Rela outrel;
                  bfd_byte *loc;
                  asection *sreloc;
                  bfd_vma offset;
+                 bfd_boolean relocate;
 
                  /* Need a dynamic relocation to get the real function
                     adddress.  */
@@ -3266,15 +3278,14 @@ elf_i386_relocate_section (bfd *output_bfd,
                      || info->executable)
                    {
                      /* This symbol is resolved locally.  */
-                     outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
-                     bfd_put_32 (output_bfd,
-                                 (h->root.u.def.value
-                                  + h->root.u.def.section->output_section->vma
-                                  + h->root.u.def.section->output_offset),
-                                 contents + offset);
+                     outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
+                     relocate = TRUE;
                    }
                  else
-                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
+                   {
+                     outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
+                     relocate = FALSE;
+                   }
 
                  sreloc = htab->elf.irelifunc;
                  loc = sreloc->contents;
@@ -3287,7 +3298,8 @@ elf_i386_relocate_section (bfd *output_bfd,
                     we need to include the symbol value so that it
                     becomes an addend for the dynamic reloc.  For an
                     internal symbol, we have updated addend.  */
-                 continue;
+                 if (! relocate)
+                   continue;
                }
              /* FALLTHROUGH */
            case R_386_PC32:
@@ -4244,7 +4256,9 @@ elf_i386_relocate_section (bfd *output_bfd,
         not process them.  */
       if (unresolved_reloc
          && !((input_section->flags & SEC_DEBUGGING) != 0
-              && h->def_dynamic))
+              && h->def_dynamic)
+         && _bfd_elf_section_offset (output_bfd, info, input_section,
+                                     rel->r_offset) != (bfd_vma) -1)
        {
          (*_bfd_error_handler)
            (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
@@ -4369,13 +4383,13 @@ elf_i386_finish_dynamic_symbol (bfd *output_bfd,
 
       if (plt == htab->elf.splt)
        {
-         plt_index = h->plt.offset / plt_entry_size - 1;
-         got_offset = (plt_index + 3) * 4;
+         got_offset = h->plt.offset / plt_entry_size - 1;
+         got_offset = (got_offset + 3) * 4;
        }
       else
        {
-         plt_index = h->plt.offset / plt_entry_size;
-         got_offset = plt_index * 4;
+         got_offset = h->plt.offset / plt_entry_size;
+         got_offset = got_offset * 4;
        }
 
       /* Fill in the entry in the procedure linkage table.  */
@@ -4436,18 +4450,6 @@ elf_i386_finish_dynamic_symbol (bfd *output_bfd,
                       + abed->plt->plt_got_offset);
        }
 
-      /* Don't fill PLT entry for static executables.  */
-      if (plt == htab->elf.splt)
-       {
-         bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
-                     plt->contents + h->plt.offset
-                      + abed->plt->plt_reloc_offset);
-         bfd_put_32 (output_bfd, - (h->plt.offset
-                                     + abed->plt->plt_plt_offset + 4),
-                     plt->contents + h->plt.offset
-                      + abed->plt->plt_plt_offset);
-       }
-
       /* Fill in the entry in the global offset table.  */
       bfd_put_32 (output_bfd,
                  (plt->output_section->vma
@@ -4475,12 +4477,29 @@ elf_i386_finish_dynamic_symbol (bfd *output_bfd,
                       + h->root.u.def.section->output_offset),
                      gotplt->contents + got_offset);
          rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
+         /* R_386_IRELATIVE comes last.  */
+         plt_index = htab->next_irelative_index--;
        }
       else
-       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
+       {
+         rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
+         plt_index = htab->next_jump_slot_index++;
+       }
       loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
 
+      /* Don't fill PLT entry for static executables.  */
+      if (plt == htab->elf.splt)
+       {
+         bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
+                     plt->contents + h->plt.offset
+                      + abed->plt->plt_reloc_offset);
+         bfd_put_32 (output_bfd, - (h->plt.offset
+                                     + abed->plt->plt_plt_offset + 4),
+                     plt->contents + h->plt.offset
+                      + abed->plt->plt_plt_offset);
+       }
+
       if (!h->def_regular)
        {
          /* Mark the symbol as undefined, rather than as defined in
This page took 0.025998 seconds and 4 git commands to generate.