1 /* Support for relocating static PIE.
2 Copyright (C) 2017-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
20 /* Mark symbols hidden in static PIE for early self relocation to work. */
21 # pragma GCC visibility push(hidden)
26 #include <dl-machine.h>
29 #define RESOLVE_MAP(map, scope, sym, version, flags) map
30 #include "dynamic-link.h"
31 #include "get-dynamic-info.h"
33 /* Relocate static executable with PIE. */
36 _dl_relocate_static_pie (void)
38 struct link_map
*main_map
= _dl_get_dl_main_map ();
40 /* Figure out the run-time load address of static PIE. */
41 main_map
->l_addr
= elf_machine_load_address ();
43 /* Read our own dynamic section and fill in the info array. */
44 main_map
->l_ld
= ((void *) main_map
->l_addr
+ elf_machine_dynamic ());
46 const ElfW(Phdr
) *ph
, *phdr
= GL(dl_phdr
);
47 size_t phnum
= GL(dl_phnum
);
48 for (ph
= phdr
; ph
< &phdr
[phnum
]; ++ph
)
49 if (ph
->p_type
== PT_DYNAMIC
)
51 main_map
->l_ld_readonly
= (ph
->p_flags
& PF_W
) == 0;
55 elf_get_dynamic_info (main_map
, false, true);
57 # ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
58 ELF_MACHINE_BEFORE_RTLD_RELOC (main_map
, main_map
->l_info
);
61 /* Relocate ourselves so we can do normal function calls and
62 data access using the global offset table. */
63 ELF_DYNAMIC_RELOCATE (main_map
, NULL
, 0, 0, 0);
64 main_map
->l_relocated
= 1;
66 /* Initialize _r_debug_extended. */
67 struct r_debug
*r
= _dl_debug_initialize (0, LM_ID_BASE
);
68 r
->r_state
= RT_CONSISTENT
;
70 /* Set up debugging before the debugger is notified for the first
72 elf_setup_debug_entry (main_map
, r
);