| 1 | /* Disable address space randomization based on inferior personality. |
| 2 | |
| 3 | Copyright (C) 2008-2021 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "gdbsupport/common-defs.h" |
| 21 | #include "nat/linux-personality.h" |
| 22 | |
| 23 | #include <sys/personality.h> |
| 24 | |
| 25 | # if !HAVE_DECL_ADDR_NO_RANDOMIZE |
| 26 | # define ADDR_NO_RANDOMIZE 0x0040000 |
| 27 | # endif /* ! HAVE_DECL_ADDR_NO_RANDOMIZE */ |
| 28 | |
| 29 | /* See comment on nat/linux-personality.h. */ |
| 30 | |
| 31 | maybe_disable_address_space_randomization:: |
| 32 | maybe_disable_address_space_randomization (int disable_randomization) |
| 33 | : m_personality_set (false), |
| 34 | m_personality_orig (0) |
| 35 | { |
| 36 | if (disable_randomization) |
| 37 | { |
| 38 | errno = 0; |
| 39 | m_personality_orig = personality (0xffffffff); |
| 40 | if (errno == 0 && !(m_personality_orig & ADDR_NO_RANDOMIZE)) |
| 41 | { |
| 42 | m_personality_set = true; |
| 43 | personality (m_personality_orig | ADDR_NO_RANDOMIZE); |
| 44 | } |
| 45 | if (errno != 0 || (m_personality_set |
| 46 | && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE))) |
| 47 | warning (_("Error disabling address space randomization: %s"), |
| 48 | safe_strerror (errno)); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | maybe_disable_address_space_randomization:: |
| 53 | ~maybe_disable_address_space_randomization () |
| 54 | { |
| 55 | if (m_personality_set) |
| 56 | { |
| 57 | errno = 0; |
| 58 | personality (m_personality_orig); |
| 59 | if (errno != 0) |
| 60 | warning (_("Error restoring address space randomization: %s"), |
| 61 | safe_strerror (errno)); |
| 62 | } |
| 63 | } |