This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC] PR/9723: gdb breakpoints silently fail on PIE binaries


Hi,

This is my implementation of the warning about PIE binaries that I was to 
implement for GDB 7.0. More details at this thread in gdb@:

http://sourceware.org/ml/gdb/2009-07/msg00131.html

It is able to discern binaries compiled as PIE or non-PIE in
Debian amd64-linux. Example session with positive case:

% buildg/gdb/gdb -q bin-test/hello

warning: The current binary is a PIE (Position Independent Executable), which
GDB does NOT currently support. Most debugger features will fail if used
in this session.

(gdb) start
Temporary breakpoint 1 at 0x754: file hello.c, line 12.
Starting program: /home/bauermann/pessoais/hobbies/computaÃÃo/gdb/pie-
warning/bin-test/hello
Error in re-setting breakpoint 1: Cannot access memory at address 0x745
Error in re-setting breakpoint 1: Cannot access memory at address 0x745
Error in re-setting breakpoint 1: Cannot access memory at address 0x745
a = 1

Program exited normally.
(gdb) 

Comments? If not, I'll write the testcases, regression test and submit as RFA.
-- 
[]'s
Thiago Jung Bauermann
2009-07-27  Thiago Jung Bauermann  <thiago.bauermann@gmail.com>

	* linux-tdep.c (check_is_pie_binary): New function.
	(_initialize_linux_tdep): New function.

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 37770f5..7a1f2f1 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -17,9 +17,16 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+#include <string.h>
 #include "defs.h"
+#include "arch-utils.h"
+#include "gdbcore.h"
 #include "gdbtypes.h"
+#include "gdb_assert.h"
 #include "linux-tdep.h"
+#include "elf/external.h"
+#include "elf/common.h"
+#include "observer.h"
 
 /* This function is suitable for architectures that don't
    extend/override the standard siginfo structure.  */
@@ -134,3 +141,60 @@ linux_get_siginfo_type (struct gdbarch *gdbarch)
 
   return siginfo_type;
 }
+
+/* Observer for the executable_changed event, to check whether the new
+   exec binary is a PIE (Position Independent Executable) specimen, which
+   is currently unsupported.  */
+
+static void
+check_is_pie_binary (void)
+{
+  char *filename, is_pie = 0;
+  FILE *fp;
+  struct gdbarch_tdep *tdep;
+  int wordsize;
+  Elf32_External_Ehdr elfhdr32;
+  Elf64_External_Ehdr elfhdr64;
+
+  if (!exec_bfd)
+    return;
+
+  filename = bfd_get_filename (exec_bfd);
+  fp = fopen (filename, "r");
+  if (!fp)
+    {
+      warning (_("Cannot check type of ELF binary: %s\n"), strerror(errno));
+
+      return;
+    }
+
+  wordsize = gdbarch_ptr_bit (gdbarch_from_bfd (exec_bfd));
+  if (wordsize == 32)
+    {
+      if (fread (&elfhdr32, sizeof (elfhdr32), 1, fp) != 1)
+	error (_("blah"));
+
+      is_pie = (*((uint16_t *) elfhdr32.e_type) == ET_DYN)? 1 : 0;
+    }
+  else if (wordsize == 64)
+    {
+      if (fread (&elfhdr64, sizeof (elfhdr64), 1, fp) != 1)
+	error (_("blah"));
+
+      is_pie = (*((uint16_t *) elfhdr64.e_type) == ET_DYN)? 1 : 0;
+    }
+
+  fclose (fp);
+
+  if (is_pie)
+    warning (_("\
+The current binary is a PIE (Position Independent Executable), which\n\
+GDB does NOT currently support. Most debugger features will fail if used\n\
+in this session.\n"));
+}
+
+void
+_initialize_linux_tdep (void)
+{
+  observer_attach_executable_changed (check_is_pie_binary);
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]