Blame elf/dl-addr-obj.c

Packit 6c4009
/* Determine if address is inside object load segments.
Packit 6c4009
   Copyright (C) 1996-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
#include <link.h>
Packit 6c4009
#include <elf.h>
Packit 6c4009
Packit 6c4009
/* Return non-zero if ADDR lies within one of L's loadable segments.
Packit 6c4009
   We have three cases we care about.
Packit 6c4009
Packit 6c4009
   Case 1: addr is above a segment.
Packit 6c4009
   +==================+<- l_map_end
Packit 6c4009
   |                  |<- addr
Packit 6c4009
   |------------------|<- l_addr + p_vaddr + p_memsz
Packit 6c4009
   |                  |
Packit 6c4009
   |                  |
Packit 6c4009
   |------------------|<- l_addr + p_vaddr
Packit 6c4009
   |------------------|<- l_addr
Packit 6c4009
   |                  |
Packit 6c4009
   +==================+<- l_map_start
Packit 6c4009
Packit 6c4009
   Case 2: addr is within a segments.
Packit 6c4009
   +==================+<- l_map_end
Packit 6c4009
   |                  |
Packit 6c4009
   |------------------|<- l_addr + p_vaddr + p_memsz
Packit 6c4009
   |                  |<- addr
Packit 6c4009
   |                  |
Packit 6c4009
   |------------------|<- l_addr + p_vaddr
Packit 6c4009
   |------------------|<- l_addr
Packit 6c4009
   |                  |
Packit 6c4009
   +==================+<- l_map_start
Packit 6c4009
Packit 6c4009
   Case 3: addr is below a segments.
Packit 6c4009
   +==================+<- l_map_end
Packit 6c4009
   |                  |
Packit 6c4009
   |------------------|<- l_addr + p_vaddr + p_memsz
Packit 6c4009
   |                  |
Packit 6c4009
   |                  |
Packit 6c4009
   |------------------|<- l_addr + p_vaddr
Packit 6c4009
   |------------------|<- l_addr
Packit 6c4009
   |                  |<- addr
Packit 6c4009
   +==================+<- l_map_start
Packit 6c4009
Packit 6c4009
   All the arithmetic is unsigned and we shift all the values down by
Packit 6c4009
   l_addr + p_vaddr and then compare the normalized addr to the range
Packit 6c4009
   of interest i.e. 0 <= addr < p_memsz.
Packit 6c4009
Packit 6c4009
*/
Packit 6c4009
int
Packit 6c4009
_dl_addr_inside_object (struct link_map *l, const ElfW(Addr) addr)
Packit 6c4009
{
Packit 6c4009
  int n = l->l_phnum;
Packit 6c4009
  const ElfW(Addr) reladdr = addr - l->l_addr;
Packit 6c4009
Packit 6c4009
  while (--n >= 0)
Packit 6c4009
    if (l->l_phdr[n].p_type == PT_LOAD
Packit 6c4009
	&& reladdr - l->l_phdr[n].p_vaddr < l->l_phdr[n].p_memsz)
Packit 6c4009
      return 1;
Packit 6c4009
  return 0;
Packit 6c4009
}