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

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