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. |
4 | |
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. |
9 | |
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. |
14 | |
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/>. */ |
18 | |
19 | #if ENABLE_STATIC_PIE |
20 | /* Mark symbols hidden in static PIE for early self relocation to work. */ |
21 | # pragma GCC visibility push(hidden) |
22 | #include <assert.h> |
23 | #include <unistd.h> |
24 | #include <ldsodefs.h> |
25 | |
26 | #include <dl-machine.h> |
27 | #include <dl-debug.h> |
28 | |
29 | #define RESOLVE_MAP(map, scope, sym, version, flags) map |
30 | #include "dynamic-link.h" |
31 | #include "get-dynamic-info.h" |
32 | |
33 | /* Relocate static executable with PIE. */ |
34 | |
35 | void |
36 | _dl_relocate_static_pie (void) |
37 | { |
38 | struct link_map *main_map = _dl_get_dl_main_map (); |
39 | |
40 | /* Figure out the run-time load address of static PIE. */ |
41 | main_map->l_addr = elf_machine_load_address (); |
42 | |
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 ()); |
45 | |
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) |
50 | { |
51 | main_map->l_ld_readonly = (ph->p_flags & PF_W) == 0; |
52 | break; |
53 | } |
54 | |
55 | elf_get_dynamic_info (main_map, false, true); |
56 | |
57 | # ifdef ELF_MACHINE_BEFORE_RTLD_RELOC |
58 | ELF_MACHINE_BEFORE_RTLD_RELOC (main_map, main_map->l_info); |
59 | # endif |
60 | |
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; |
65 | |
66 | /* Initialize _r_debug_extended. */ |
67 | struct r_debug *r = _dl_debug_initialize (0, LM_ID_BASE); |
68 | r->r_state = RT_CONSISTENT; |
69 | |
70 | /* Set up debugging before the debugger is notified for the first |
71 | time. */ |
72 | elf_setup_debug_entry (main_map, r); |
73 | } |
74 | #endif |
75 | |