PR27952, Disallow ET_DYN DF_1_PIE linker input

Message ID YMMYDX9dGV0KIX9X@bubble.grove.modra.org
State New
Headers show
Series
  • PR27952, Disallow ET_DYN DF_1_PIE linker input
Related show

Commit Message

Libor Bukata via Binutils June 11, 2021, 8 a.m.
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.


-- 
Alan Modra
Australia Development Lab, IBM

Patch

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 92b1722eacb..8f985ab8917 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 0e1871aaac9..c9a6e78be79 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);
diff --git a/ld/ldelf.c b/ld/ldelf.c
index 4d4d9ca0517..21e655bb55c 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -1048,7 +1048,9 @@  ldelf_after_open (int use_libpath, int native, int is_linux, int is_freebsd,
 	  && elf_tdata (abfd) != NULL
 	  && elf_tdata (abfd)->elf_header != NULL
 	  /* FIXME: Maybe check for other non-supportable types as well ?  */
-	  && elf_tdata (abfd)->elf_header->e_type == ET_EXEC)
+	  && (elf_tdata (abfd)->elf_header->e_type == ET_EXEC
+	      || (elf_tdata (abfd)->elf_header->e_type == ET_DYN
+		  && elf_tdata (abfd)->is_pie)))
 	einfo (_("%F%P: cannot use executable file '%pB' as input to a link\n"),
 	       abfd);
     }