Fix processing of RVA relocs
[binutils-gdb.git] / bfd / coff-mcore.c
index 199a5ab70f0633482202a6154c43268197a0f1cd..a644db10c1ffb4802b3290b09e746e458000fdf6 100644 (file)
@@ -1,5 +1,5 @@
 /* BFD back-end for Motorolla MCore COFF/PE
-   Copyright 1999 Free Software Foundation, Inc.
+   Copyright (C) 1999 Free Software Foundation, Inc.
 
 This file is part of BFD, the Binary File Descriptor library.
 
@@ -248,6 +248,25 @@ coff_mcore_link_hash_table_create (abfd)
   return & ret->root.root;
 }
 \f
+/* Add an entry to the base file.  */
+static void
+mcore_emit_base_file_entry (info, output_bfd, input_section, reloc_offset)
+      struct bfd_link_info * info;
+      bfd *                  output_bfd;
+      asection *             input_section;
+      bfd_vma                reloc_offset;
+{
+  bfd_vma addr = reloc_offset
+                 - input_section->vma
+                 + input_section->output_offset
+                 + input_section->output_section->vma;
+
+  if (coff_data (output_bfd)->pe)
+     addr -= pe_data (output_bfd)->pe_opthdr.ImageBase;
+  
+  fwrite (&addr, 1, sizeof (addr), (FILE *) info->base_file);
+}
+\f
 /*ARGSUSED*/
 static bfd_reloc_status_type
 mcore_coff_unsupported_reloc (abfd, reloc_entry, symbol, data, input_section,
@@ -320,7 +339,7 @@ coff_mcore_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
   if (rel->r_type == IMAGE_REL_MCORE_RVA)
     * addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
   
-  if (howto->pc_relative)
+  else if (howto->pc_relative)
     {
       * addendp = sec->vma - 2; /* XXX guess - is this right ? */
       
@@ -421,6 +440,8 @@ coff_mcore_relocate_section (output_bfd, info, input_bfd, input_section,
          sym = syms + symndx;
        }
 
+      addend = 0;
+      
       /* Get the howto and initialise the addend.  */
       howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
                                       sym, & addend);
@@ -508,10 +529,25 @@ coff_mcore_relocate_section (output_bfd, info, input_bfd, input_section,
        case IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2:
        case IMAGE_REL_MCORE_ADDR32:
        case IMAGE_REL_MCORE_RVA:
+         /* XXX fixme - shouldn't this be like the code for the RVA reloc ? */
          rstat = _bfd_relocate_contents (howto, input_bfd, val, loc);
          break;
+         
+       case IMAGE_REL_MCORE_RVA:
+         rstat = _bfd_final_link_relocate
+           (howto, input_bfd,
+            input_section, contents, rel->r_vaddr - input_section->vma,
+            val, addend);
+         break;
        }
       
+      if (info->base_file)
+       {
+         /* Emit a reloc if the backend thinks it needs it.  */
+         if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
+            mcore_emit_base_file_entry (info, output_bfd, input_section, rel->r_vaddr);
+       }
+  
       switch (rstat)
        {
        default: