aboutsummaryrefslogtreecommitdiff
path: root/gdb/nat
diff options
context:
space:
mode:
authorSergio Durigan Junior <sergiodj@redhat.com>2015-01-15 15:10:49 -0500
committerSergio Durigan Junior <sergiodj@redhat.com>2015-01-15 15:10:49 -0500
commit8cc73a3902a68269626274e15d7c25bef0a61759 (patch)
treeac425182a79a90d9636ec4c618f64325c062c20a /gdb/nat
parentfb23d554428f1d379fd8c3e959a294108fa59f88 (diff)
downloadgdb-8cc73a3902a68269626274e15d7c25bef0a61759.zip
gdb-8cc73a3902a68269626274e15d7c25bef0a61759.tar.gz
gdb-8cc73a3902a68269626274e15d7c25bef0a61759.tar.bz2
Move code to disable ASR to nat/
This patch moves the shared code present on gdb/linux-nat.c:linux_nat_create_inferior and gdb/gdbserver/linux-low.c:linux_create_inferior to nat/linux-personality.c. This code is responsible for disabling address space randomization based on user setting, and using <sys/personality.h> to do that. I decided to put the prototype of the maybe_disable_address_space_randomization on nat/linux-osdata.h because it seemed the best place to put it. I regression-tested this patch on Fedora 20 x86_64, and found no regressions. gdb/ChangeLog 2015-01-15 Sergio Durigan Junior <sergiodj@redhat.com> * Makefile.in (HFILES_NO_SRCDIR): Add nat/linux-personality.h. (linux-personality.o): New rule. * common/common-defs.h: Include <stdint.h>. * config/aarch64/linux.mh (NATDEPFILES): Include linux-personality.o. * config/alpha/alpha-linux.mh (NATDEPFILES): Likewise. * config/arm/linux.mh (NATDEPFILES): Likewise. * config/i386/linux64.mh (NATDEPFILES): Likewise. * config/i386/linux.mh (NATDEPFILES): Likewise. * config/ia64/linux.mh (NATDEPFILES): Likewise. * config/m32r/linux.mh (NATDEPFILES): Likewise. * config/m68k/linux.mh (NATDEPFILES): Likewise. * config/mips/linux.mh (NATDEPFILES): Likewise. * config/pa/linux.mh (NATDEPFILES): Likewise. * config/powerpc/linux.mh (NATDEPFILES): Likewise. * config/powerpc/ppc64-linux.mh (NATDEPFILES): Likewise. * config/powerpc/spu-linux.mh (NATDEPFILES): Likewise. * config/s390/linux.mh (NATDEPFILES): Likewise. * config/sparc/linux64.mh (NATDEPFILES): Likewise. * config/sparc/linux.mh (NATDEPFILES): Likewise. * config/tilegx/linux.mh (NATDEPFILES): Likewise. * config/xtensa/linux.mh (NATDEPFILES): Likewise. * defs.h: Remove #include <stdint.h> (moved to common/common-defs.h). * linux-nat.c: Include nat/linux-personality.h. Remove #include <sys/personality.h>; do not define ADDR_NO_RANDOMIZE (moved to nat/linux-personality.c). (linux_nat_create_inferior): Remove code to disable address space randomization (moved to nat/linux-personality.c). Create cleanup to disable address space randomization. * nat/linux-personality.c: New file. * nat/linux-personality.h: Likewise. gdb/gdbserver/ChangeLog 2015-01-15 Sergio Durigan Junior <sergiodj@redhat.com> * Makefile.in (SFILES): Add linux-personality.c. (linux-personality.o): New rule. * configure.srv (srv_linux_obj): Add linux-personality.o to the list of objects to be built. * linux-low.c: Include nat/linux-personality.h. (linux_create_inferior): Remove code to disable address space randomization (moved to ../nat/linux-personality.c). Create cleanup to disable address space randomization.
Diffstat (limited to 'gdb/nat')
-rw-r--r--gdb/nat/linux-personality.c94
-rw-r--r--gdb/nat/linux-personality.h31
2 files changed, 125 insertions, 0 deletions
diff --git a/gdb/nat/linux-personality.c b/gdb/nat/linux-personality.c
new file mode 100644
index 0000000..f61a2c6
--- /dev/null
+++ b/gdb/nat/linux-personality.c
@@ -0,0 +1,94 @@
+/* Disable address space randomization based on inferior personality.
+
+ Copyright (C) 2008-2015 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ 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 "common-defs.h"
+#include "nat/linux-personality.h"
+
+#ifdef HAVE_PERSONALITY
+# include <sys/personality.h>
+# ifndef HAVE_DECL_ADDR_NO_RANDOMIZE
+# define ADDR_NO_RANDOMIZE 0x0040000
+# endif /* ! HAVE_DECL_ADDR_NO_RANDOMIZE */
+#endif /* HAVE_PERSONALITY */
+
+#ifdef HAVE_PERSONALITY
+
+/* Restore address space randomization of the inferior. ARG is the
+ original inferior's personality value before the address space
+ randomization was disabled. */
+
+static void
+restore_personality (void *arg)
+{
+ int personality_orig = (int) (uintptr_t) arg;
+
+ errno = 0;
+ personality (personality_orig);
+ if (errno != 0)
+ warning (_("Error restoring address space randomization: %s"),
+ safe_strerror (errno));
+}
+#endif /* HAVE_PERSONALITY */
+
+/* Return a cleanup responsible for restoring the inferior's
+ personality (and restoring the inferior's address space
+ randomization) if HAVE_PERSONALITY and if PERSONALITY_SET is not
+ zero; return a null cleanup if not HAVE_PERSONALITY or if
+ PERSONALITY_SET is zero. */
+
+static struct cleanup *
+make_disable_asr_cleanup (int personality_set, int personality_orig)
+{
+#ifdef HAVE_PERSONALITY
+ if (personality_set != 0)
+ return make_cleanup (restore_personality,
+ (void *) (uintptr_t) personality_orig);
+#endif /* HAVE_PERSONALITY */
+
+ return make_cleanup (null_cleanup, NULL);
+}
+
+/* See comment on nat/linux-personality.h. */
+
+struct cleanup *
+maybe_disable_address_space_randomization (int disable_randomization)
+{
+ int personality_orig = 0;
+ int personality_set = 0;
+
+#ifdef HAVE_PERSONALITY
+ if (disable_randomization)
+ {
+ errno = 0;
+ personality_orig = personality (0xffffffff);
+ if (errno == 0 && !(personality_orig & ADDR_NO_RANDOMIZE))
+ {
+ personality_set = 1;
+ personality (personality_orig | ADDR_NO_RANDOMIZE);
+ }
+ if (errno != 0 || (personality_set
+ && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE)))
+ warning (_("Error disabling address space randomization: %s"),
+ safe_strerror (errno));
+ }
+#endif /* HAVE_PERSONALITY */
+
+ return make_disable_asr_cleanup (personality_set,
+ personality_orig);
+}
diff --git a/gdb/nat/linux-personality.h b/gdb/nat/linux-personality.h
new file mode 100644
index 0000000..009e614
--- /dev/null
+++ b/gdb/nat/linux-personality.h
@@ -0,0 +1,31 @@
+/* Disable address space randomization based on inferior personality.
+
+ Copyright (C) 2008-2015 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef NAT_LINUX_PERSONALITY_H
+#define NAT_LINUX_PERSONALITY_H
+
+/* Disable the inferior's address space randomization if
+ DISABLE_RANDOMIZATION is not zero and if we have
+ <sys/personality.h>. Return a cleanup which, when called, will
+ re-enable the inferior's address space randomization. */
+
+extern struct cleanup *maybe_disable_address_space_randomization
+ (int disable_randomization);
+
+#endif /* ! NAT_LINUX_PERSONALITY_H */