offline.c revision 361df7da6dfecd817b27e62b91752ac316d7cdd4
1d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath/* Recover relocatibility for addresses computed from debug information.
2687d7e9bd154a4b1f22b5e3ed7c6c904e421e871Roland McGrath   Copyright (C) 2005, 2006 Red Hat, Inc.
3361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   This file is part of Red Hat elfutils.
4d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath
5361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   Red Hat elfutils is free software; you can redistribute it and/or modify
6361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   it under the terms of the GNU General Public License as published by the
7361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   Free Software Foundation; version 2 of the License.
8d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath
9361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   Red Hat elfutils is distributed in the hope that it will be useful, but
10361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   WITHOUT ANY WARRANTY; without even the implied warranty of
11361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   General Public License for more details.
13361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper
14361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   You should have received a copy of the GNU General Public License along
15361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   with Red Hat elfutils; if not, write to the Free Software Foundation,
16361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper
18361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   In addition, as a special exception, Red Hat, Inc. gives You the
19361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   additional right to link the code of Red Hat elfutils with code licensed
20361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   under any Open Source Initiative certified open source license
21361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   (http://www.opensource.org/licenses/index.php) which requires the
22361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   distribution of source code with any binary distribution and to
23361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   distribute linked combinations of the two.  Non-GPL Code permitted under
24361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   this exception must only link to the code of Red Hat elfutils through
25361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   those well defined interfaces identified in the file named EXCEPTION
26361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   found in the source code files (the "Approved Interfaces").  The files
27361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   of Non-GPL Code may instantiate templates or use macros or inline
28361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   functions from the Approved Interfaces without causing the resulting
29361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   work to be covered by the GNU General Public License.  Only Red Hat,
30361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   Inc. may make changes or additions to the list of Approved Interfaces.
31361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   Red Hat's grant of this exception is conditioned upon your not adding
32361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   any new exceptions.  If you wish to add a new Approved Interface or
33361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   exception, please contact Red Hat.  You must obey the GNU General Public
34361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   License in all respects for all of the Red Hat elfutils code and other
35361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   code used in conjunction with Red Hat elfutils except the Non-GPL Code
36361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   covered by this exception.  If you modify this file, you may extend this
37361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   exception to your version of the file, but you are not obligated to do
38361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   so.  If you do not wish to provide this exception without modification,
39361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   you must delete this exception statement from your version and license
40361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   this file solely under the GPL without exception.
41361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper
42361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   Red Hat elfutils is an included package of the Open Invention Network.
43361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   An included package of the Open Invention Network is a package for which
44361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   Open Invention Network licensees cross-license their patents.  No patent
45361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   license is granted, either expressly or impliedly, by designation as an
46361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   included package.  Should you wish to participate in the Open Invention
47361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   Network licensing program, please visit www.openinventionnetwork.com
48361df7da6dfecd817b27e62b91752ac316d7cdd4Ulrich Drepper   <http://www.openinventionnetwork.com>.  */
49d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath
50d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath#include "libdwflP.h"
51d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath#include <unistd.h>
52d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath
53d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath/* Since dwfl_report_elf lays out the sections already, this will only be
54d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath   called when the section headers of the debuginfo file are being
55d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath   consulted instead.  With binutils strip-to-debug, the symbol table
56d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath   is in the debuginfo file and relocation looks there.  */
57d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrathint
58d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrathdwfl_offline_section_address (Dwfl_Module *mod,
59d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath			      void **userdata __attribute__ ((unused)),
60d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath			      const char *modname __attribute__ ((unused)),
61d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath			      Dwarf_Addr base __attribute__ ((unused)),
62d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath			      const char *secname __attribute__ ((unused)),
63d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath			      Elf32_Word shndx,
64d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath			      const GElf_Shdr *shdr __attribute__ ((unused)),
65d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath			      Dwarf_Addr *addr)
66d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath{
67d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  assert (mod->symfile != &mod->main);
68d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath
69d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  GElf_Shdr shdr_mem;
70d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  GElf_Shdr *main_shdr = gelf_getshdr (elf_getscn (mod->main.elf, shndx),
71d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath				       &shdr_mem);
72d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  if (unlikely (main_shdr == NULL))
73d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath    return -1;
74d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath
75d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  assert (shdr->sh_addr == 0);
76d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  assert (shdr->sh_flags & SHF_ALLOC);
77d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  assert (main_shdr->sh_addr != 0);
78d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  assert (main_shdr->sh_flags == shdr->sh_flags);
79d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath
80d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  *addr = main_shdr->sh_addr;
81d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  return 0;
82d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath}
83d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrathINTDEF (dwfl_offline_section_address)
84d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath
85d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrathDwfl_Module *
86d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrathdwfl_report_offline (Dwfl *dwfl, const char *name,
87d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath		     const char *file_name, int fd)
88d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath{
89d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  if (dwfl == NULL)
90d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath    return NULL;
91d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath
92d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  Dwfl_Module *mod = INTUSE(dwfl_report_elf) (dwfl, name, file_name, fd,
93d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath					      dwfl->offline_next_address);
94d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  if (mod != NULL)
95d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath    {
96d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath      /* If this is an ET_EXEC file with fixed addresses, the address range
97d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath	 it consumed may or may not intersect with the arbitrary range we
98d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath	 will use for relocatable modules.  Make sure we always use a free
99687d7e9bd154a4b1f22b5e3ed7c6c904e421e871Roland McGrath	 range for the offline allocations.  If this module did use
100687d7e9bd154a4b1f22b5e3ed7c6c904e421e871Roland McGrath	 offline_next_address, it may have rounded it up for the module's
101687d7e9bd154a4b1f22b5e3ed7c6c904e421e871Roland McGrath	 alignment requirements.  */
102687d7e9bd154a4b1f22b5e3ed7c6c904e421e871Roland McGrath      if ((dwfl->offline_next_address >= mod->low_addr
103687d7e9bd154a4b1f22b5e3ed7c6c904e421e871Roland McGrath	   || mod->low_addr - dwfl->offline_next_address < OFFLINE_REDZONE)
104d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath	  && dwfl->offline_next_address < mod->high_addr + OFFLINE_REDZONE)
105d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath	dwfl->offline_next_address = mod->high_addr + OFFLINE_REDZONE;
106d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath
107d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath      /* Don't keep the file descriptor around.  */
108d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath      if (mod->main.fd != -1 && elf_cntl (mod->main.elf, ELF_C_FDREAD) == 0)
109d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath	{
110d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath	  close (mod->main.fd);
111d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath	  mod->main.fd = -1;
112d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath	}
113d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath    }
114d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath
115d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath  return mod;
116d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrath}
117d17fac7e89666b47811581b10b5ca0d253a3a82dRoland McGrathINTDEF (dwfl_report_offline)
118