Blame elf/dl-reloc-static-pie.c

Packit Service 82fcde
/* Support for relocating static PIE.
Packit Service 82fcde
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#if ENABLE_STATIC_PIE
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
#include <ldsodefs.h>
Packit Service 82fcde
#include "dynamic-link.h"
Packit Service 82fcde
Packit Service 82fcde
/* Relocate static executable with PIE.  */
Packit Service 82fcde
Packit Service 82fcde
void
Packit Service 82fcde
_dl_relocate_static_pie (void)
Packit Service 82fcde
{
Packit Service 82fcde
  struct link_map *main_map = _dl_get_dl_main_map ();
Packit Service 82fcde
Packit Service 82fcde
# define STATIC_PIE_BOOTSTRAP
Packit Service 82fcde
# define BOOTSTRAP_MAP (main_map)
Packit Service 82fcde
# define RESOLVE_MAP(sym, version, flags) BOOTSTRAP_MAP
Packit Service 82fcde
# include "dynamic-link.h"
Packit Service 82fcde
Packit Service 82fcde
  /* Figure out the run-time load address of static PIE.  */
Packit Service 82fcde
  main_map->l_addr = elf_machine_load_address ();
Packit Service 82fcde
Packit Service 82fcde
  /* Read our own dynamic section and fill in the info array.  */
Packit Service 82fcde
  main_map->l_ld = ((void *) main_map->l_addr + elf_machine_dynamic ());
Packit Service 82fcde
  elf_get_dynamic_info (main_map, NULL);
Packit Service 82fcde
Packit Service 82fcde
# ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
Packit Service 82fcde
  ELF_MACHINE_BEFORE_RTLD_RELOC (main_map->l_info);
Packit Service 82fcde
# endif
Packit Service 82fcde
Packit Service 82fcde
  /* Relocate ourselves so we can do normal function calls and
Packit Service 82fcde
     data access using the global offset table.  */
Packit Service 82fcde
  ELF_DYNAMIC_RELOCATE (main_map, 0, 0, 0);
Packit Service 82fcde
  main_map->l_relocated = 1;
Packit Service 82fcde
Packit Service 82fcde
  /* Initialize _r_debug.  */
Packit Service 82fcde
  struct r_debug *r = _dl_debug_initialize (0, LM_ID_BASE);
Packit Service 82fcde
  r->r_state = RT_CONSISTENT;
Packit Service 82fcde
Packit Service 82fcde
  /* Set up debugging before the debugger is notified for the first
Packit Service 82fcde
     time.  */
Packit Service 82fcde
# ifdef ELF_MACHINE_DEBUG_SETUP
Packit Service 82fcde
  /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way.  */
Packit Service 82fcde
  ELF_MACHINE_DEBUG_SETUP (main_map, r);
Packit Service 82fcde
# else
Packit Service 82fcde
  if (main_map->l_info[DT_DEBUG] != NULL)
Packit Service 82fcde
    /* There is a DT_DEBUG entry in the dynamic section.  Fill it in
Packit Service 82fcde
       with the run-time address of the r_debug structure  */
Packit Service 82fcde
    main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
Packit Service 82fcde
# endif
Packit Service 82fcde
}
Packit Service 82fcde
#endif