From patchwork Fri Jan 9 22:19:03 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergio Durigan Junior X-Patchwork-Id: 4601 Received: (qmail 19892 invoked by alias); 9 Jan 2015 22:19:17 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 19762 invoked by uid 89); 9 Jan 2015 22:19:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.0 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Fri, 09 Jan 2015 22:19:11 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t09MJA79016175 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 9 Jan 2015 17:19:10 -0500 Received: from psique.yyz.redhat.com (dhcp-10-15-16-169.yyz.redhat.com [10.15.16.169]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t09MJ9Pu025112; Fri, 9 Jan 2015 17:19:10 -0500 From: Sergio Durigan Junior To: GDB Patches Cc: Pedro Alves , Gary Benson , Sergio Durigan Junior Subject: [PATCH 2/2] Move code to disable ASR to nat/ Date: Fri, 9 Jan 2015 17:19:03 -0500 Message-Id: <1420841943-24290-3-git-send-email-sergiodj@redhat.com> In-Reply-To: <1420841943-24290-1-git-send-email-sergiodj@redhat.com> References: <1420841943-24290-1-git-send-email-sergiodj@redhat.com> X-IsSubscribed: yes 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 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-09 Sergio Durigan Junior * Makefile.in (linux-personality.o): New rule. * common/common-defs.h: Include . * 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 (moved to common/common-defs.h). * linux-nat.c: Remove #include ; 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-osdata.h (maybe_disable_address_space_randomization): New prototype. * nat/linux-personality.c: New file. gdb/gdbserver/ChangeLog 2015-01-09 Sergio Durigan Junior * 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 (linux_create_inferior): Remove code to disable address space randomization (moved to ../nat/linux-personality.c). Create cleanup to disable address space randomization. --- gdb/Makefile.in | 4 ++ gdb/common/common-defs.h | 1 + gdb/config/aarch64/linux.mh | 3 +- gdb/config/alpha/alpha-linux.mh | 2 +- gdb/config/arm/linux.mh | 2 +- gdb/config/i386/linux.mh | 2 +- gdb/config/i386/linux64.mh | 2 +- gdb/config/ia64/linux.mh | 1 + gdb/config/m32r/linux.mh | 2 +- gdb/config/m68k/linux.mh | 1 + gdb/config/mips/linux.mh | 1 + gdb/config/pa/linux.mh | 3 +- gdb/config/powerpc/linux.mh | 2 +- gdb/config/powerpc/ppc64-linux.mh | 1 + gdb/config/powerpc/spu-linux.mh | 2 +- gdb/config/s390/linux.mh | 1 + gdb/config/sparc/linux.mh | 3 +- gdb/config/sparc/linux64.mh | 3 +- gdb/config/tilegx/linux.mh | 3 +- gdb/config/xtensa/linux.mh | 2 +- gdb/defs.h | 1 - gdb/gdbserver/Makefile.in | 4 ++ gdb/gdbserver/configure.srv | 2 +- gdb/gdbserver/linux-low.c | 33 ++-------------- gdb/linux-nat.c | 40 ++----------------- gdb/nat/linux-osdata.h | 2 + gdb/nat/linux-personality.c | 81 +++++++++++++++++++++++++++++++++++++++ 27 files changed, 122 insertions(+), 82 deletions(-) create mode 100644 gdb/nat/linux-personality.c diff --git a/gdb/Makefile.in b/gdb/Makefile.in index a8bae82..4245e01 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -2279,6 +2279,10 @@ mips-linux-watch.o: ${srcdir}/nat/mips-linux-watch.c $(COMPILE) $(srcdir)/nat/mips-linux-watch.c $(POSTCOMPILE) +linux-personality.o: ${srcdir}/nat/linux-personality.c + $(COMPILE) $(srcdir)/nat/linux-personality.c + $(POSTCOMPILE) + # # gdb/tui/ dependencies # diff --git a/gdb/common/common-defs.h b/gdb/common/common-defs.h index 1873211..e80d332 100644 --- a/gdb/common/common-defs.h +++ b/gdb/common/common-defs.h @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include diff --git a/gdb/config/aarch64/linux.mh b/gdb/config/aarch64/linux.mh index af04f46..7f96e4d 100644 --- a/gdb/config/aarch64/linux.mh +++ b/gdb/config/aarch64/linux.mh @@ -21,7 +21,8 @@ NAT_FILE= config/nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o aarch64-linux-nat.o \ proc-service.o linux-thread-db.o linux-nat.o linux-fork.o \ - linux-procfs.o linux-ptrace.o linux-osdata.o linux-waitpid.o + linux-procfs.o linux-ptrace.o linux-osdata.o linux-waitpid.o \ + linux-personality.o NAT_CDEPS = $(srcdir)/proc-service.list LOADLIBES= -ldl $(RDYNAMIC) diff --git a/gdb/config/alpha/alpha-linux.mh b/gdb/config/alpha/alpha-linux.mh index 81819a1..2ea02a1 100644 --- a/gdb/config/alpha/alpha-linux.mh +++ b/gdb/config/alpha/alpha-linux.mh @@ -3,7 +3,7 @@ NAT_FILE= config/nm-linux.h NATDEPFILES= inf-ptrace.o alpha-linux-nat.o \ fork-child.o proc-service.o linux-thread-db.o \ linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \ - linux-waitpid.o + linux-waitpid.o linux-personality.o NAT_CDEPS = $(srcdir)/proc-service.list # The dynamically loaded libthread_db needs access to symbols in the diff --git a/gdb/config/arm/linux.mh b/gdb/config/arm/linux.mh index 63c3eae..549bf42 100644 --- a/gdb/config/arm/linux.mh +++ b/gdb/config/arm/linux.mh @@ -4,7 +4,7 @@ NAT_FILE= config/nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o arm-linux-nat.o \ proc-service.o linux-thread-db.o \ linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \ - linux-waitpid.o + linux-waitpid.o linux-personality.o NAT_CDEPS = $(srcdir)/proc-service.list LOADLIBES= -ldl $(RDYNAMIC) diff --git a/gdb/config/i386/linux.mh b/gdb/config/i386/linux.mh index a4e4842..33fb281 100644 --- a/gdb/config/i386/linux.mh +++ b/gdb/config/i386/linux.mh @@ -5,7 +5,7 @@ NATDEPFILES= inf-ptrace.o fork-child.o \ x86-nat.o x86-dregs.o i386-linux-nat.o x86-linux-nat.o \ proc-service.o linux-thread-db.o \ linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \ - linux-btrace.o linux-waitpid.o + linux-btrace.o linux-waitpid.o linux-personality.o NAT_CDEPS = $(srcdir)/proc-service.list # The dynamically loaded libthread_db needs access to symbols in the diff --git a/gdb/config/i386/linux64.mh b/gdb/config/i386/linux64.mh index d557202..d52eb52 100644 --- a/gdb/config/i386/linux64.mh +++ b/gdb/config/i386/linux64.mh @@ -5,7 +5,7 @@ NATDEPFILES= inf-ptrace.o fork-child.o \ linux-nat.o linux-osdata.o \ proc-service.o linux-thread-db.o linux-fork.o \ linux-procfs.o linux-ptrace.o linux-btrace.o \ - linux-waitpid.o + linux-waitpid.o linux-personality.o NAT_FILE= config/nm-linux.h NAT_CDEPS = $(srcdir)/proc-service.list diff --git a/gdb/config/ia64/linux.mh b/gdb/config/ia64/linux.mh index baa8709..9dce22b 100644 --- a/gdb/config/ia64/linux.mh +++ b/gdb/config/ia64/linux.mh @@ -5,6 +5,7 @@ NATDEPFILES= inf-ptrace.o fork-child.o \ ia64-linux-nat.o \ proc-service.o linux-thread-db.o \ linux-nat.o linux-osdata.o linux-fork.o \ + linux-personality.o \ linux-procfs.o linux-ptrace.o linux-waitpid.o NAT_CDEPS = $(srcdir)/proc-service.list diff --git a/gdb/config/m32r/linux.mh b/gdb/config/m32r/linux.mh index e9bb82c..6b810e6 100644 --- a/gdb/config/m32r/linux.mh +++ b/gdb/config/m32r/linux.mh @@ -4,7 +4,7 @@ NAT_FILE= config/nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o \ m32r-linux-nat.o proc-service.o linux-thread-db.o \ linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \ - linux-waitpid.o + linux-waitpid.o linux-personality.o NAT_CDEPS = $(srcdir)/proc-service.list LOADLIBES= -ldl $(RDYNAMIC) diff --git a/gdb/config/m68k/linux.mh b/gdb/config/m68k/linux.mh index bcec295..f3b3baa 100644 --- a/gdb/config/m68k/linux.mh +++ b/gdb/config/m68k/linux.mh @@ -5,6 +5,7 @@ NATDEPFILES= inf-ptrace.o fork-child.o \ m68klinux-nat.o \ proc-service.o linux-thread-db.o \ linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \ + linux-personality.o \ linux-waitpid.o NAT_CDEPS = $(srcdir)/proc-service.list diff --git a/gdb/config/mips/linux.mh b/gdb/config/mips/linux.mh index 021838e..d6a802f 100644 --- a/gdb/config/mips/linux.mh +++ b/gdb/config/mips/linux.mh @@ -4,6 +4,7 @@ NATDEPFILES= inf-ptrace.o fork-child.o mips-linux-nat.o \ linux-thread-db.o proc-service.o \ linux-nat.o linux-osdata.o linux-fork.o \ linux-procfs.o linux-ptrace.o linux-waitpid.o \ + linux-personality.o \ mips-linux-watch.o NAT_CDEPS = $(srcdir)/proc-service.list diff --git a/gdb/config/pa/linux.mh b/gdb/config/pa/linux.mh index f1c0c32..9539b64 100644 --- a/gdb/config/pa/linux.mh +++ b/gdb/config/pa/linux.mh @@ -3,7 +3,8 @@ NAT_FILE= config/nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o \ hppa-linux-nat.o proc-service.o linux-thread-db.o \ linux-nat.o linux-osdata.o linux-fork.o \ - linux-procfs.o linux-ptrace.o linux-waitpid.o + linux-procfs.o linux-ptrace.o linux-waitpid.o \ + linux-personality.o NAT_CDEPS = $(srcdir)/proc-service.list LOADLIBES = -ldl $(RDYNAMIC) diff --git a/gdb/config/powerpc/linux.mh b/gdb/config/powerpc/linux.mh index a807d3f..76e62c0 100644 --- a/gdb/config/powerpc/linux.mh +++ b/gdb/config/powerpc/linux.mh @@ -6,7 +6,7 @@ NAT_FILE= config/nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o \ ppc-linux-nat.o proc-service.o linux-thread-db.o \ linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \ - linux-waitpid.o + linux-waitpid.o linux-personality.o NAT_CDEPS = $(srcdir)/proc-service.list LOADLIBES = -ldl $(RDYNAMIC) diff --git a/gdb/config/powerpc/ppc64-linux.mh b/gdb/config/powerpc/ppc64-linux.mh index 4b91408..a050d8b 100644 --- a/gdb/config/powerpc/ppc64-linux.mh +++ b/gdb/config/powerpc/ppc64-linux.mh @@ -6,6 +6,7 @@ NAT_FILE= config/nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o \ ppc-linux-nat.o proc-service.o linux-thread-db.o \ linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \ + linux-personality.o \ linux-waitpid.o NAT_CDEPS = $(srcdir)/proc-service.list diff --git a/gdb/config/powerpc/spu-linux.mh b/gdb/config/powerpc/spu-linux.mh index 7ce7a5f..d44aeeb 100644 --- a/gdb/config/powerpc/spu-linux.mh +++ b/gdb/config/powerpc/spu-linux.mh @@ -4,5 +4,5 @@ # PPU side of the Cell BE and debugging the SPU side. NATDEPFILES = spu-linux-nat.o fork-child.o inf-ptrace.o \ - linux-procfs.o linux-ptrace.o linux-waitpid.o + linux-procfs.o linux-ptrace.o linux-waitpid.o linux-personality.o diff --git a/gdb/config/s390/linux.mh b/gdb/config/s390/linux.mh index 59bab83..e1ad899 100644 --- a/gdb/config/s390/linux.mh +++ b/gdb/config/s390/linux.mh @@ -3,6 +3,7 @@ NAT_FILE= config/nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o s390-linux-nat.o \ linux-thread-db.o proc-service.o \ linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \ + linux-personality.o \ linux-waitpid.o NAT_CDEPS = $(srcdir)/proc-service.list LOADLIBES = -ldl $(RDYNAMIC) diff --git a/gdb/config/sparc/linux.mh b/gdb/config/sparc/linux.mh index 009ea95..bd7fc86 100644 --- a/gdb/config/sparc/linux.mh +++ b/gdb/config/sparc/linux.mh @@ -4,7 +4,8 @@ NATDEPFILES= sparc-nat.o sparc-linux-nat.o \ fork-child.o inf-ptrace.o \ proc-service.o linux-thread-db.o \ linux-nat.o linux-osdata.o linux-fork.o \ - linux-procfs.o linux-ptrace.o linux-waitpid.o + linux-procfs.o linux-ptrace.o linux-waitpid.o \ + linux-personality.o NAT_CDEPS = $(srcdir)/proc-service.list # The dynamically loaded libthread_db needs access to symbols in the diff --git a/gdb/config/sparc/linux64.mh b/gdb/config/sparc/linux64.mh index aad77c7..86f984f 100644 --- a/gdb/config/sparc/linux64.mh +++ b/gdb/config/sparc/linux64.mh @@ -4,7 +4,8 @@ NATDEPFILES= sparc-nat.o sparc64-nat.o sparc64-linux-nat.o \ fork-child.o inf-ptrace.o \ proc-service.o linux-thread-db.o \ linux-nat.o linux-osdata.o linux-fork.o \ - linux-procfs.o linux-ptrace.o linux-waitpid.o + linux-procfs.o linux-ptrace.o linux-waitpid.o \ + linux-personality.o NAT_CDEPS = $(srcdir)/proc-service.list # The dynamically loaded libthread_db needs access to symbols in the diff --git a/gdb/config/tilegx/linux.mh b/gdb/config/tilegx/linux.mh index 1ed9c33..b5edcd4 100644 --- a/gdb/config/tilegx/linux.mh +++ b/gdb/config/tilegx/linux.mh @@ -5,7 +5,8 @@ NATDEPFILES= inf-ptrace.o fork-child.o \ tilegx-linux-nat.o \ proc-service.o linux-thread-db.o \ linux-nat.o linux-osdata.o linux-fork.o \ - linux-procfs.o linux-ptrace.o linux-waitpid.o + linux-procfs.o linux-ptrace.o linux-waitpid.o \ + linux-personality.o # The dynamically loaded libthread_db needs access to symbols in the # gdb executable. diff --git a/gdb/config/xtensa/linux.mh b/gdb/config/xtensa/linux.mh index 8ef84b4..b4e59b3 100644 --- a/gdb/config/xtensa/linux.mh +++ b/gdb/config/xtensa/linux.mh @@ -5,7 +5,7 @@ NAT_FILE= config/nm-linux.h NATDEPFILES= inf-ptrace.o fork-child.o xtensa-linux-nat.o \ linux-thread-db.o proc-service.o \ linux-nat.o linux-osdata.o linux-fork.o linux-procfs.o linux-ptrace.o \ - linux-waitpid.o + linux-waitpid.o linux-personality.o NAT_CDEPS = $(srcdir)/proc-service.list LOADLIBES = -ldl $(RDYNAMIC) diff --git a/gdb/defs.h b/gdb/defs.h index 99db465..a1cd45f 100644 --- a/gdb/defs.h +++ b/gdb/defs.h @@ -29,7 +29,6 @@ #include #include -#include /* The libdecnumber library, on which GDB depends, includes a header file called gstdint.h instead of relying directly on stdint.h. GDB, on the diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in index f05f450..75798ec 100644 --- a/gdb/gdbserver/Makefile.in +++ b/gdb/gdbserver/Makefile.in @@ -167,6 +167,7 @@ SFILES= $(srcdir)/gdbreplay.c $(srcdir)/inferiors.c $(srcdir)/dll.c \ $(srcdir)/common/vec.c $(srcdir)/common/gdb_vecs.c \ $(srcdir)/common/common-utils.c $(srcdir)/common/xml-utils.c \ $(srcdir)/nat/linux-osdata.c $(srcdir)/common/ptid.c \ + $(srcdir)/nat/linux-personality.c \ $(srcdir)/common/buffer.c $(srcdir)/nat/linux-btrace.c \ $(srcdir)/common/filestuff.c $(srcdir)/target/waitstatus.c \ $(srcdir)/nat/mips-linux-watch.c $(srcdir)/common/print-utils.c \ @@ -587,6 +588,9 @@ linux-waitpid.o: ../nat/linux-waitpid.c mips-linux-watch.o: ../nat/mips-linux-watch.c $(COMPILE) $< $(POSTCOMPILE) +linux-personality.o: ../nat/linux-personality.c + $(COMPILE) $< + $(POSTCOMPILE) aarch64.c : $(srcdir)/../regformats/aarch64.dat $(regdat_sh) $(SHELL) $(regdat_sh) $(srcdir)/../regformats/aarch64.dat aarch64.c diff --git a/gdb/gdbserver/configure.srv b/gdb/gdbserver/configure.srv index 679fc9f..0d48ddd 100644 --- a/gdb/gdbserver/configure.srv +++ b/gdb/gdbserver/configure.srv @@ -42,7 +42,7 @@ srv_amd64_linux_xmlfiles="i386/amd64-linux.xml i386/amd64-avx-linux.xml i386/amd # Linux object files. This is so we don't have to repeat # these files over and over again. -srv_linux_obj="linux-low.o linux-osdata.o linux-procfs.o linux-ptrace.o linux-waitpid.o" +srv_linux_obj="linux-low.o linux-osdata.o linux-procfs.o linux-ptrace.o linux-waitpid.o linux-personality.o" # Input is taken from the "${target}" variable. diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c index 4d19c87..0ebb87a 100644 --- a/gdb/gdbserver/linux-low.c +++ b/gdb/gdbserver/linux-low.c @@ -597,29 +597,11 @@ add_lwp (ptid_t ptid) static int linux_create_inferior (char *program, char **allargs) { -#ifdef HAVE_PERSONALITY - int personality_orig = 0, personality_set = 0; -#endif struct lwp_info *new_lwp; int pid; ptid_t ptid; - -#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", - strerror (errno)); - } -#endif + struct cleanup *restore_personality + = maybe_disable_address_space_randomization (disable_randomization); #if defined(__UCLIBC__) && defined(HAS_NOMMU) pid = vfork (); @@ -665,16 +647,7 @@ linux_create_inferior (char *program, char **allargs) _exit (0177); } -#ifdef HAVE_PERSONALITY - if (personality_set) - { - errno = 0; - personality (personality_orig); - if (errno != 0) - warning ("Error restoring address space randomization: %s", - strerror (errno)); - } -#endif + do_cleanups (restore_personality); linux_add_process (pid, 0); diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 5f2c176b..8d45cf6 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -70,13 +70,6 @@ #define SPUFS_MAGIC 0x23c9b64e #endif -#ifdef HAVE_PERSONALITY -# include -# if !HAVE_DECL_ADDR_NO_RANDOMIZE -# define ADDR_NO_RANDOMIZE 0x0040000 -# endif -#endif /* HAVE_PERSONALITY */ - /* This comment documents high-level logic of this file. Waiting for events in sync mode @@ -1103,45 +1096,18 @@ linux_nat_create_inferior (struct target_ops *ops, char *exec_file, char *allargs, char **env, int from_tty) { -#ifdef HAVE_PERSONALITY - int personality_orig = 0, personality_set = 0; -#endif /* HAVE_PERSONALITY */ + struct cleanup *restore_personality + = maybe_disable_address_space_randomization (disable_randomization); /* The fork_child mechanism is synchronous and calls target_wait, so we have to mask the async mode. */ -#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 */ - /* Make sure we report all signals during startup. */ linux_nat_pass_signals (ops, 0, NULL); linux_ops->to_create_inferior (ops, exec_file, allargs, env, from_tty); -#ifdef HAVE_PERSONALITY - if (personality_set) - { - errno = 0; - personality (personality_orig); - if (errno != 0) - warning (_("Error restoring address space randomization: %s"), - safe_strerror (errno)); - } -#endif /* HAVE_PERSONALITY */ + do_cleanups (restore_personality); } /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not diff --git a/gdb/nat/linux-osdata.h b/gdb/nat/linux-osdata.h index db8b445..ff501a2 100644 --- a/gdb/nat/linux-osdata.h +++ b/gdb/nat/linux-osdata.h @@ -24,4 +24,6 @@ extern int linux_common_core_of_thread (ptid_t ptid); extern LONGEST linux_common_xfer_osdata (const char *annex, gdb_byte *readbuf, ULONGEST offset, ULONGEST len); +extern struct cleanup *maybe_disable_address_space_randomization (int disable_randomization); + #endif diff --git a/gdb/nat/linux-personality.c b/gdb/nat/linux-personality.c new file mode 100644 index 0000000..4b0349f --- /dev/null +++ b/gdb/nat/linux-personality.c @@ -0,0 +1,81 @@ +/* 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 . */ + +#include "common-defs.h" +#include "nat/linux-osdata.h" + +#ifdef HAVE_PERSONALITY +# include +# ifndef HAVE_DECL_ADDR_NO_RANDOMIZE +# define ADDR_NO_RANDOMIZE 0x0040000 +# endif /* ! HAVE_DECL_ADDR_NO_RANDOMIZE */ +#endif /* HAVE_PERSONALITY */ + +#ifdef HAVE_PERSONALITY +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", + strerror (errno)); +} +#endif /* HAVE_PERSONALITY */ + +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); +} + +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); +}