pe.em and pep.em make_import_fixup

Message ID ZFh8AXXazWkP9+0w@squeak.grove.modra.org
State Unresolved
Headers
Series pe.em and pep.em make_import_fixup |

Checks

Context Check Description
snail/binutils-gdb-check warning Git am fail log

Commit Message

Alan Modra May 8, 2023, 4:35 a.m. UTC
  This is a little cleanup that I made when looking at pr30343 that
makes it more obvious that make_import_fixup in both files are
identical (and in fact the new pep.em read_addend could be used in
both files).

	* emultempl/pep.em (read_addend): Extract from..
	(make_import_fixup): ..here.
	* emultempl/pe.em (read_addend): Similarly.
	(make_import_fixup): Similarly.  Add debug code from pep.em.
  

Patch

diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em
index 4fe0cf203fb..38cb61138bd 100644
--- a/ld/emultempl/pe.em
+++ b/ld/emultempl/pe.em
@@ -1236,23 +1236,42 @@  pe_fixup_stdcalls (void)
       }
 }
 
+static bfd_vma
+read_addend (arelent *rel, asection *s)
+{
+  char buf[4];
+  bfd_vma addend = 0;
+
+  if (!bfd_get_section_contents (s->owner, s, buf, rel->address, sizeof (buf)))
+    einfo (_("%P: %C: cannot get section contents - auto-import exception\n"),
+	   s->owner, s, rel->address);
+  else
+    addend = bfd_get_32 (s->owner, buf);
+  return addend;
+}
+
 static void
 make_import_fixup (arelent *rel, asection *s, char *name, const char *symname)
 {
   struct bfd_symbol *sym = *rel->sym_ptr_ptr;
-  char addend[4];
-  bfd_vma _addend;
+  bfd_vma addend;
 
   if (pe_dll_extra_pe_debug)
     printf ("arelent: %s@%#lx: add=%li\n", sym->name,
 	    (unsigned long) rel->address, (long) rel->addend);
 
-  if (! bfd_get_section_contents (s->owner, s, addend, rel->address, sizeof (addend)))
-    einfo (_("%P: %C: cannot get section contents - auto-import exception\n"),
-	   s->owner, s, rel->address);
+  addend = read_addend (rel, s);
+
+  if (pe_dll_extra_pe_debug)
+    {
+      printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx",
+	      (long) addend, (long) rel->addend, (long) rel->address);
+      if (rel->howto->pc_relative)
+	printf (" pcrel");
+      printf (" %d bit rel.\n", (int) rel->howto->bitsize);
+    }
 
-  _addend = bfd_get_32 (s->owner, addend);
-  pe_create_import_fixup (rel, s, _addend, name, symname);
+  pe_create_import_fixup (rel, s, addend, name, symname);
 }
 
 static void
diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em
index 5770df8ed0a..32883b27706 100644
--- a/ld/emultempl/pep.em
+++ b/ld/emultempl/pep.em
@@ -1197,63 +1197,80 @@  pep_fixup_stdcalls (void)
       }
 }
 
-static void
-make_import_fixup (arelent *rel, asection *s, char *name, const char *symname)
+static bfd_vma
+read_addend (arelent *rel, asection *s)
 {
-  struct bfd_symbol *sym = *rel->sym_ptr_ptr;
-  char addend[8];
-  bfd_vma _addend = 0;
-  int suc = 0;
+  char buf[8];
+  bfd_vma addend = 0;
+  bool ok = false;
 
-  if (pep_dll_extra_pe_debug)
-    printf ("arelent: %s@%#lx: add=%li\n", sym->name,
-	    (unsigned long) rel->address, (long) rel->addend);
-
-  memset (addend, 0, sizeof (addend));
-  switch ((rel->howto->bitsize))
+  switch (rel->howto->bitsize)
     {
     case 8:
-      suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 1);
-      if (suc && rel->howto->pc_relative)
-	_addend = bfd_get_signed_8 (s->owner, addend);
-      else if (suc)
-	_addend = bfd_get_8 (s->owner, addend);
+      ok = bfd_get_section_contents (s->owner, s, buf, rel->address, 1);
+      if (ok)
+	{
+	  if (rel->howto->pc_relative)
+	    addend = bfd_get_signed_8 (s->owner, buf);
+	  else
+	    addend = bfd_get_8 (s->owner, buf);
+	}
       break;
     case 16:
-      suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 2);
-      if (suc && rel->howto->pc_relative)
-	_addend = bfd_get_signed_16 (s->owner, addend);
-      else if (suc)
-	_addend = bfd_get_16 (s->owner, addend);
+      ok = bfd_get_section_contents (s->owner, s, buf, rel->address, 2);
+      if (ok)
+	{
+	  if (rel->howto->pc_relative)
+	    addend = bfd_get_signed_16 (s->owner, buf);
+	  else
+	    addend = bfd_get_16 (s->owner, buf);
+	}
       break;
     case 26:
     case 32:
-      suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 4);
-      if (suc && rel->howto->pc_relative)
-	_addend = bfd_get_signed_32 (s->owner, addend);
-      else if (suc)
-	_addend = bfd_get_32 (s->owner, addend);
+      ok = bfd_get_section_contents (s->owner, s, buf, rel->address, 4);
+      if (ok)
+	{
+	  if (rel->howto->pc_relative)
+	    addend = bfd_get_signed_32 (s->owner, buf);
+	  else
+	    addend = bfd_get_32 (s->owner, buf);
+	}
       break;
     case 64:
-      suc = bfd_get_section_contents (s->owner, s, addend, rel->address, 8);
-      if (suc)
-	_addend = bfd_get_64 (s->owner, addend);
+      ok = bfd_get_section_contents (s->owner, s, buf, rel->address, 8);
+      if (ok)
+	addend = bfd_get_64 (s->owner, buf);
       break;
     }
-  if (! suc)
+  if (!ok)
     einfo (_("%P: %C: cannot get section contents - auto-import exception\n"),
 	   s->owner, s, rel->address);
+  return addend;
+}
+
+static void
+make_import_fixup (arelent *rel, asection *s, char *name, const char *symname)
+{
+  struct bfd_symbol *sym = *rel->sym_ptr_ptr;
+  bfd_vma addend;
+
+  if (pep_dll_extra_pe_debug)
+    printf ("arelent: %s@%#lx: add=%li\n", sym->name,
+	    (unsigned long) rel->address, (long) rel->addend);
+
+  addend = read_addend (rel, s);
 
   if (pep_dll_extra_pe_debug)
     {
       printf ("import of 0x%lx(0x%lx) sec_addr=0x%lx",
-	      (long) _addend, (long) rel->addend, (long) rel->address);
+	      (long) addend, (long) rel->addend, (long) rel->address);
       if (rel->howto->pc_relative)
 	printf (" pcrel");
       printf (" %d bit rel.\n", (int) rel->howto->bitsize);
     }
 
-  pep_create_import_fixup (rel, s, _addend, name, symname);
+  pep_create_import_fixup (rel, s, addend, name, symname);
 }
 
 static void