From f64b9b13ce4bb927f47a1fe0257c3794c6357c8c Mon Sep 17 00:00:00 2001
From: Alan Modra <amodra@gmail.com>
Date: Fri, 11 Jun 2021 14:06:47 +0930
Subject: PR27952, Disallow ET_DYN DF_1_PIE linker input

This patch adds a new elf_tdata flag, is_pie, set during the linker's
open_input_bfds processing.  The flag is then used to reject attempts
to link a PIE as if it were a shared library.

bfd/
	PR 27952
	* elf-bfd.h (struct elf_obj_tdata): Add is_pie.
	* elflink.c (elf_link_add_object_symbols): Set is_pie.
ld/
	PR 27952
	* ldelf.c (ldelf_after_open): Error on input PIEs too.
---
 bfd/ChangeLog | 6 ++++++
 bfd/elf-bfd.h | 3 +++
 bfd/elflink.c | 2 ++
 3 files changed, 11 insertions(+)

(limited to 'bfd')

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index d1a92dc..ad63f9c 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,9 @@
+2021-06-11  Alan Modra  <amodra@gmail.com>
+
+	PR 27952
+	* elf-bfd.h (struct elf_obj_tdata): Add is_pie.
+	* elflink.c (elf_link_add_object_symbols): Set is_pie.
+
 2021-06-09  Nick Clifton  <nickc@redhat.com>
 
 	PR 27666
diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 92b1722..8f985ab 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -2080,6 +2080,9 @@ struct elf_obj_tdata
      symbols.  */
   unsigned int bad_symtab : 1;
 
+  /* Set if DT_FLAGS_1 has DF_1_PIE set.  */
+  unsigned int is_pie : 1;
+
   /* Information grabbed from an elf core file.  */
   struct core_elf_obj_tdata *core;
 
diff --git a/bfd/elflink.c b/bfd/elflink.c
index 0e1871a..c9a6e78 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -4349,6 +4349,8 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
 		  unsigned int tagv = dyn.d_un.d_val;
 		  audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
 		}
+	      if (dyn.d_tag == DT_FLAGS_1)
+		elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
 	    }
 
 	  free (dynbuf);
-- 
cgit v1.1