Mark PIE executables as OBJF_SHARED.

Change-Id: I99ba0e4ee2103e66e76c25ed1cc62cafa09bf34b
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 75aa2ee..281ddc1 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,14 @@
 2016-12-16  Doug Evans  <dje@google.com>
 
+	* objfile-flags.h (objfile_flag): Add comment.
+	* objfiles.c (objfile_purge_solibs): Check OBJF_MAINLINE.
+	* solib-svr4.c (svr4_same_1): Add check for Fuchsia.
+	* symfile.c (symbol_file_add_with_addrs): Mark PIE executables as
+	OBJF_SHARED.
+	* target.c (target_translate_tls_address): Add check for OBJF_MAINLINE.
+
+2016-12-16  Doug Evans  <dje@google.com>
+
 	* fuchsia-tdep.c (fuchsia_solib_create_inferior_hook): New function.
 	(fuchsia_init_abi): Provide solib_create_inferior_hook.
 	* solib-svr4.c (svr4_apply_exec_displacement): New function.
diff --git a/gdb/objfile-flags.h b/gdb/objfile-flags.h
index da03918..11ff6a3 100644
--- a/gdb/objfile-flags.h
+++ b/gdb/objfile-flags.h
@@ -36,9 +36,12 @@
     OBJF_REORDERED = 1 << 0,	/* Functions are reordered */
 
     /* Distinguish between an objfile for a shared library and a
-       "vanilla" objfile.  This may come from a target's
-       implementation of the solib interface, from add-symbol-file, or
-       any other mechanism that loads dynamic objects.  */
+       "vanilla" objfile.  This is also set for PIE executables.
+       To distinguish whether an objfile is the main executable, check
+       OBJF_MAINLINE.
+       This may come from a target's implementation of the solib interface,
+       from add-symbol-file, or any other mechanism that loads dynamic
+       objects.  */
     OBJF_SHARED = 1 << 1,	/* From a shared library */
 
     /* User requested that this objfile be read in it's entirety.  */
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 4dc6a0a..377ad67 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -1095,7 +1095,9 @@
     /* We assume that the solib package has been purged already, or will
        be soon.  */
 
-    if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
+    if (!(objf->flags & OBJF_USERLOADED)
+	&& !(objf->flags & OBJF_MAINLINE)
+	&& (objf->flags & OBJF_SHARED))
       free_objfile (objf);
   }
 }
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index f9446b0..d19b48c 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -179,6 +179,15 @@
       && strcmp (inferior_so_name, "/lib/sparcv9/ld.so.1") == 0)
     return 1;
 
+  /* On Fuchsia, ld.so.1 doesn't have a path, and is the same as libc.so,
+     which doesn't have a path either.  */
+  if (gdbarch_osabi (target_gdbarch ()) == GDB_OSABI_FUCHSIA)
+    {
+      if (strcmp (gdb_so_name, "ld.so.1") == 0
+	  && strcmp (inferior_so_name, "libc.so") == 0)
+	return 1;
+    }
+
   return 0;
 }
 
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 52f99bf..d019981 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -1166,7 +1166,17 @@
     error (_("Not confirmed."));
 
   if (mainline)
-    flags |= OBJF_MAINLINE;
+    {
+      flags |= OBJF_MAINLINE;
+
+      /* Mark PIE executables as shared.
+	 This is important on Fuchsia as the main executable isn't loaded yet
+	 when the process starts, it's loaded by the dynamic linker similarly
+	 to shared libs.  */
+      if ((bfd_get_file_flags (exec_bfd) & DYNAMIC) != 0)
+	flags |= OBJF_SHARED;
+    }
+
   objfile = allocate_objfile (abfd, name, flags);
 
   if (parent)
diff --git a/gdb/target.c b/gdb/target.c
index 246d292..8ad8ee3 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -879,7 +879,9 @@
          throw the error to some higher catcher.  */
       CATCH (ex, RETURN_MASK_ALL)
 	{
-	  int objfile_is_library = (objfile->flags & OBJF_SHARED);
+	  int objfile_is_library
+	    = ((objfile->flags & OBJF_SHARED)
+	       && !(objfile->flags & OBJF_MAINLINE));
 
 	  switch (ex.error)
 	    {