From ae49f218daca0b7cab27764da4081e6509bc7345 Mon Sep 17 00:00:00 2001 From: Samuel Thibault Date: Tue, 28 Dec 2021 10:27:06 +0100 Subject: hurd: Fix static-PIE startup hurd initialization stages use RUN_HOOK to run various initialization functions. That is however using absolute addresses which need to be relocated, which is done later by csu. We can however easily make the linker compute relative addresses which thus don't need a relocation. The new SET_RELHOOK and RUN_RELHOOK macros implement this. --- sysdeps/generic/set-hooks-arch.h | 31 +++++++++++++++++++++++++++++++ sysdeps/i386/set-hooks-arch.h | 28 ++++++++++++++++++++++++++++ sysdeps/mach/hurd/brk.c | 8 ++++---- sysdeps/mach/hurd/check_fds.c | 6 ++---- sysdeps/mach/hurd/i386/init-first.c | 2 +- sysdeps/x86_64/set-hooks-arch.h | 28 ++++++++++++++++++++++++++++ 6 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 sysdeps/generic/set-hooks-arch.h create mode 100644 sysdeps/i386/set-hooks-arch.h create mode 100644 sysdeps/x86_64/set-hooks-arch.h (limited to 'sysdeps') diff --git a/sysdeps/generic/set-hooks-arch.h b/sysdeps/generic/set-hooks-arch.h new file mode 100644 index 0000000000..4b47fa2f2e --- /dev/null +++ b/sysdeps/generic/set-hooks-arch.h @@ -0,0 +1,31 @@ +/* Machine-dependent macros for using symbol sets for running lists of + functions. Generic/stub version. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _SET_HOOKS_ARCH_H +#define _SET_HOOKS_ARCH_H + +/* Define SET_RELHOOK to a variant of text_set_element that records a relative + offset rather than an absolute address. See sysdeps/i386/set-hooks-arch.h + for an example. + +#define SET_RELHOOK(NAME, HOOK) ... + + */ + +#endif /* set_hooks_arch.h */ diff --git a/sysdeps/i386/set-hooks-arch.h b/sysdeps/i386/set-hooks-arch.h new file mode 100644 index 0000000000..97513bf897 --- /dev/null +++ b/sysdeps/i386/set-hooks-arch.h @@ -0,0 +1,28 @@ +/* Machine-dependent macros for using symbol sets for running lists of + functions. i386 version. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _SET_HOOKS_ARCH_H +#define _SET_HOOKS_ARCH_H + +#define SET_RELHOOK(NAME, HOOK) \ + asm(".section " #NAME",\"aR\"\n" \ + ".long "#HOOK" - .\n" \ + ".section .text"); + +#endif /* set_hooks_arch.h */ diff --git a/sysdeps/mach/hurd/brk.c b/sysdeps/mach/hurd/brk.c index a4294b9eae..088c99b2c8 100644 --- a/sysdeps/mach/hurd/brk.c +++ b/sysdeps/mach/hurd/brk.c @@ -21,6 +21,8 @@ #include /* For `struct mutex'. */ #include +#include "set-hooks.h" + /* Initial maximum size of the data segment (this is arbitrary). */ #define DATA_SIZE (128 * 1024 * 1024) @@ -130,7 +132,7 @@ _hurd_set_brk (vm_address_t addr) return 0; } -static void +static void attribute_used_retain init_brk (void) { vm_address_t pagend; @@ -160,7 +162,5 @@ init_brk (void) /* Couldn't allocate the memory. The break will be very short. */ _hurd_data_end = pagend; } - - (void) &init_brk; /* Avoid ``defined but not used'' warning. */ } -text_set_element (_hurd_preinit_hook, init_brk); +SET_RELHOOK (_hurd_preinit_hook, init_brk); diff --git a/sysdeps/mach/hurd/check_fds.c b/sysdeps/mach/hurd/check_fds.c index 155e9dd3e9..61e6055d35 100644 --- a/sysdeps/mach/hurd/check_fds.c +++ b/sysdeps/mach/hurd/check_fds.c @@ -79,7 +79,7 @@ check_standard_fds (void) check_one_fd (STDERR_FILENO, O_RDWR); } -static void +static void attribute_used_retain init_standard_fds (void) { /* Now that we have FDs, make sure that, if this is a SUID program, @@ -87,10 +87,8 @@ init_standard_fds (void) ourselves. If that's not possible we stop the program. */ if (__builtin_expect (__libc_enable_secure, 0)) check_standard_fds (); - - (void) &init_standard_fds; /* Avoid "defined but not used" warning. */ } -text_set_element (_hurd_fd_subinit, init_standard_fds); +SET_RELHOOK (_hurd_fd_subinit, init_standard_fds); #ifndef SHARED diff --git a/sysdeps/mach/hurd/i386/init-first.c b/sysdeps/mach/hurd/i386/init-first.c index 5e85aa2bc5..c6ae370daf 100644 --- a/sysdeps/mach/hurd/i386/init-first.c +++ b/sysdeps/mach/hurd/i386/init-first.c @@ -242,7 +242,7 @@ first_init (void) /* Initialize data structures so we can do RPCs. */ __mach_init (); - RUN_HOOK (_hurd_preinit_hook, ()); + RUN_RELHOOK (_hurd_preinit_hook, ()); } #ifdef SHARED diff --git a/sysdeps/x86_64/set-hooks-arch.h b/sysdeps/x86_64/set-hooks-arch.h new file mode 100644 index 0000000000..227d4f8e08 --- /dev/null +++ b/sysdeps/x86_64/set-hooks-arch.h @@ -0,0 +1,28 @@ +/* Machine-dependent macros for using symbol sets for running lists of + functions. x86-64 version. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _SET_HOOKS_ARCH_H +#define _SET_HOOKS_ARCH_H + +#define SET_RELHOOK(NAME, HOOK) \ + asm(".section " #NAME",\"aR\"\n" \ + ".quad "#HOOK" - .\n" \ + ".section .text"); + +#endif /* set_hooks_arch.h */ -- cgit 1.4.1