| 1 | /* Support for relocating static PIE. | 
|---|
| 2 | Copyright (C) 2017-2020 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 | #include <unistd.h> | 
|---|
| 21 | #include <ldsodefs.h> | 
|---|
| 22 | #include "dynamic-link.h" | 
|---|
| 23 |  | 
|---|
| 24 | /* Relocate static executable with PIE.  */ | 
|---|
| 25 |  | 
|---|
| 26 | void | 
|---|
| 27 | _dl_relocate_static_pie (void) | 
|---|
| 28 | { | 
|---|
| 29 | struct link_map *main_map = _dl_get_dl_main_map (); | 
|---|
| 30 |  | 
|---|
| 31 | # define STATIC_PIE_BOOTSTRAP | 
|---|
| 32 | # define BOOTSTRAP_MAP (main_map) | 
|---|
| 33 | # define RESOLVE_MAP(sym, version, flags) BOOTSTRAP_MAP | 
|---|
| 34 | # include "dynamic-link.h" | 
|---|
| 35 |  | 
|---|
| 36 | /* Figure out the run-time load address of static PIE.  */ | 
|---|
| 37 | main_map->l_addr = elf_machine_load_address (); | 
|---|
| 38 |  | 
|---|
| 39 | /* Read our own dynamic section and fill in the info array.  */ | 
|---|
| 40 | main_map->l_ld = ((void *) main_map->l_addr + elf_machine_dynamic ()); | 
|---|
| 41 | elf_get_dynamic_info (main_map, NULL); | 
|---|
| 42 |  | 
|---|
| 43 | # ifdef ELF_MACHINE_BEFORE_RTLD_RELOC | 
|---|
| 44 | ELF_MACHINE_BEFORE_RTLD_RELOC (main_map->l_info); | 
|---|
| 45 | # endif | 
|---|
| 46 |  | 
|---|
| 47 | /* Relocate ourselves so we can do normal function calls and | 
|---|
| 48 | data access using the global offset table.  */ | 
|---|
| 49 | ELF_DYNAMIC_RELOCATE (main_map, 0, 0, 0); | 
|---|
| 50 | main_map->l_relocated = 1; | 
|---|
| 51 |  | 
|---|
| 52 | /* Initialize _r_debug.  */ | 
|---|
| 53 | struct r_debug *r = _dl_debug_initialize (0, LM_ID_BASE); | 
|---|
| 54 | r->r_state = RT_CONSISTENT; | 
|---|
| 55 |  | 
|---|
| 56 | /* Set up debugging before the debugger is notified for the first | 
|---|
| 57 | time.  */ | 
|---|
| 58 | # ifdef ELF_MACHINE_DEBUG_SETUP | 
|---|
| 59 | /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way.  */ | 
|---|
| 60 | ELF_MACHINE_DEBUG_SETUP (main_map, r); | 
|---|
| 61 | # else | 
|---|
| 62 | if (main_map->l_info[DT_DEBUG] != NULL) | 
|---|
| 63 | /* There is a DT_DEBUG entry in the dynamic section.  Fill it in | 
|---|
| 64 | with the run-time address of the r_debug structure  */ | 
|---|
| 65 | main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r; | 
|---|
| 66 | # endif | 
|---|
| 67 | } | 
|---|
| 68 | #endif | 
|---|
| 69 |  | 
|---|