1 /* Support for relocating static PIE.
2    Copyright (C) 2017-2021 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 
28 #define RESOLVE_MAP(map, scope, sym, version, flags) map
29 #include "dynamic-link.h"
30 #include "get-dynamic-info.h"
31 
32 /* Relocate static executable with PIE.  */
33 
34 void
_dl_relocate_static_pie(void)35 _dl_relocate_static_pie (void)
36 {
37   struct link_map *main_map = _dl_get_dl_main_map ();
38 
39   /* Figure out the run-time load address of static PIE.  */
40   main_map->l_addr = elf_machine_load_address ();
41 
42   /* Read our own dynamic section and fill in the info array.  */
43   main_map->l_ld = ((void *) main_map->l_addr + elf_machine_dynamic ());
44 
45   const ElfW(Phdr) *ph, *phdr = GL(dl_phdr);
46   size_t phnum = GL(dl_phnum);
47   for (ph = phdr; ph < &phdr[phnum]; ++ph)
48     if (ph->p_type == PT_DYNAMIC)
49       {
50 	main_map->l_ld_readonly = (ph->p_flags & PF_W) == 0;
51 	break;
52       }
53 
54   elf_get_dynamic_info (main_map, false, true);
55 
56 # ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
57   ELF_MACHINE_BEFORE_RTLD_RELOC (main_map, main_map->l_info);
58 # endif
59 
60   /* Relocate ourselves so we can do normal function calls and
61      data access using the global offset table.  */
62   ELF_DYNAMIC_RELOCATE (main_map, NULL, 0, 0, 0);
63   main_map->l_relocated = 1;
64 
65   /* Initialize _r_debug_extended.  */
66   struct r_debug *r = _dl_debug_initialize (0, LM_ID_BASE);
67   r->r_state = RT_CONSISTENT;
68 
69   /* Set up debugging before the debugger is notified for the first
70      time.  */
71 # ifdef ELF_MACHINE_DEBUG_SETUP
72   /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way.  */
73   ELF_MACHINE_DEBUG_SETUP (main_map, r);
74 # else
75   if (main_map->l_info[DT_DEBUG] != NULL)
76     /* There is a DT_DEBUG entry in the dynamic section.  Fill it in
77        with the run-time address of the r_debug structure  */
78     main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
79 # endif
80 }
81 #endif
82