1/* Recover relocatibility for addresses computed from debug information.
2   Copyright (C) 2005, 2006, 2007, 2008 Red Hat, Inc.
3   This file is part of Red Hat elfutils.
4
5   Red Hat elfutils is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by the
7   Free Software Foundation; version 2 of the License.
8
9   Red Hat elfutils is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   General Public License for more details.
13
14   You should have received a copy of the GNU General Public License along
15   with Red Hat elfutils; if not, write to the Free Software Foundation,
16   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17
18   In addition, as a special exception, Red Hat, Inc. gives You the
19   additional right to link the code of Red Hat elfutils with code licensed
20   under any Open Source Initiative certified open source license
21   (http://www.opensource.org/licenses/index.php) which requires the
22   distribution of source code with any binary distribution and to
23   distribute linked combinations of the two.  Non-GPL Code permitted under
24   this exception must only link to the code of Red Hat elfutils through
25   those well defined interfaces identified in the file named EXCEPTION
26   found in the source code files (the "Approved Interfaces").  The files
27   of Non-GPL Code may instantiate templates or use macros or inline
28   functions from the Approved Interfaces without causing the resulting
29   work to be covered by the GNU General Public License.  Only Red Hat,
30   Inc. may make changes or additions to the list of Approved Interfaces.
31   Red Hat's grant of this exception is conditioned upon your not adding
32   any new exceptions.  If you wish to add a new Approved Interface or
33   exception, please contact Red Hat.  You must obey the GNU General Public
34   License in all respects for all of the Red Hat elfutils code and other
35   code used in conjunction with Red Hat elfutils except the Non-GPL Code
36   covered by this exception.  If you modify this file, you may extend this
37   exception to your version of the file, but you are not obligated to do
38   so.  If you do not wish to provide this exception without modification,
39   you must delete this exception statement from your version and license
40   this file solely under the GPL without exception.
41
42   Red Hat elfutils is an included package of the Open Invention Network.
43   An included package of the Open Invention Network is a package for which
44   Open Invention Network licensees cross-license their patents.  No patent
45   license is granted, either expressly or impliedly, by designation as an
46   included package.  Should you wish to participate in the Open Invention
47   Network licensing program, please visit www.openinventionnetwork.com
48   <http://www.openinventionnetwork.com>.  */
49
50#include "libdwflP.h"
51#include <fcntl.h>
52#include <unistd.h>
53
54/* Since dwfl_report_elf lays out the sections already, this will only be
55   called when the section headers of the debuginfo file are being
56   consulted instead, or for the section placed at 0.  With binutils
57   strip-to-debug, the symbol table is in the debuginfo file and relocation
58   looks there.  */
59int
60dwfl_offline_section_address (Dwfl_Module *mod,
61			      void **userdata __attribute__ ((unused)),
62			      const char *modname __attribute__ ((unused)),
63			      Dwarf_Addr base __attribute__ ((unused)),
64			      const char *secname __attribute__ ((unused)),
65			      Elf32_Word shndx,
66			      const GElf_Shdr *shdr __attribute__ ((unused)),
67			      Dwarf_Addr *addr)
68{
69  assert (mod->e_type == ET_REL);
70  assert (shdr->sh_addr == 0);
71  assert (shdr->sh_flags & SHF_ALLOC);
72
73  if (mod->debug.elf == NULL)
74    /* We are only here because sh_addr is zero even though layout is complete.
75       The first section in the first file under -e is placed at 0.  */
76    return 0;
77
78  /* The section numbers might not match between the two files.
79     The best we can rely on is the order of SHF_ALLOC sections.  */
80
81  Elf_Scn *ourscn = elf_getscn (mod->debug.elf, shndx);
82  Elf_Scn *scn = NULL;
83  uint_fast32_t skip_alloc = 0;
84  while ((scn = elf_nextscn (mod->debug.elf, scn)) != ourscn)
85    {
86      assert (scn != NULL);
87      GElf_Shdr shdr_mem;
88      GElf_Shdr *sh = gelf_getshdr (scn, &shdr_mem);
89      if (unlikely (sh == NULL))
90	return -1;
91      if (sh->sh_flags & SHF_ALLOC)
92	++skip_alloc;
93    }
94
95  scn = NULL;
96  while ((scn = elf_nextscn (mod->main.elf, scn)) != NULL)
97    {
98      GElf_Shdr shdr_mem;
99      GElf_Shdr *main_shdr = gelf_getshdr (scn, &shdr_mem);
100      if (unlikely (main_shdr == NULL))
101	return -1;
102      if ((main_shdr->sh_flags & SHF_ALLOC) && skip_alloc-- == 0)
103	{
104	  assert (main_shdr->sh_flags == shdr->sh_flags);
105	  *addr = main_shdr->sh_addr;
106	  return 0;
107	}
108    }
109
110  /* This should never happen.  */
111  return -1;
112}
113INTDEF (dwfl_offline_section_address)
114
115/* Forward declarations.  */
116static Dwfl_Module *process_elf (Dwfl *dwfl, const char *name,
117				 const char *file_name, int fd, Elf *elf);
118static Dwfl_Module *process_archive (Dwfl *dwfl, const char *name,
119				     const char *file_name, int fd, Elf *elf,
120				     int (*predicate) (const char *module,
121						       const char *file));
122
123/* Report one module for an ELF file, or many for an archive.
124   Always consumes ELF and FD.  */
125static Dwfl_Module *
126process_file (Dwfl *dwfl, const char *name, const char *file_name, int fd,
127	      Elf *elf, int (*predicate) (const char *module,
128					  const char *file))
129{
130  switch (elf_kind (elf))
131    {
132    default:
133    case ELF_K_NONE:
134      __libdwfl_seterrno (elf == NULL ? DWFL_E_LIBELF : DWFL_E_BADELF);
135      return NULL;
136
137    case ELF_K_ELF:
138      return process_elf (dwfl, name, file_name, fd, elf);
139
140    case ELF_K_AR:
141      return process_archive (dwfl, name, file_name, fd, elf, predicate);
142    }
143}
144
145/* Report the open ELF file as a module.  Always consumes ELF and FD.  */
146static Dwfl_Module *
147process_elf (Dwfl *dwfl, const char *name, const char *file_name, int fd,
148	     Elf *elf)
149{
150  Dwfl_Module *mod = __libdwfl_report_elf (dwfl, name, file_name, fd, elf,
151					   dwfl->offline_next_address);
152  if (mod != NULL)
153    {
154      /* If this is an ET_EXEC file with fixed addresses, the address range
155	 it consumed may or may not intersect with the arbitrary range we
156	 will use for relocatable modules.  Make sure we always use a free
157	 range for the offline allocations.  If this module did use
158	 offline_next_address, it may have rounded it up for the module's
159	 alignment requirements.  */
160      if ((dwfl->offline_next_address >= mod->low_addr
161	   || mod->low_addr - dwfl->offline_next_address < OFFLINE_REDZONE)
162	  && dwfl->offline_next_address < mod->high_addr + OFFLINE_REDZONE)
163	dwfl->offline_next_address = mod->high_addr + OFFLINE_REDZONE;
164
165      /* Don't keep the file descriptor around.  */
166      if (mod->main.fd != -1 && elf_cntl (mod->main.elf, ELF_C_FDREAD) == 0)
167	{
168	  close (mod->main.fd);
169	  mod->main.fd = -1;
170	}
171    }
172
173  return mod;
174}
175
176/* Always consumes MEMBER.  Returns elf_next result on success.
177   For errors returns ELF_C_NULL with *MOD set to null.  */
178static Elf_Cmd
179process_archive_member (Dwfl *dwfl, const char *name, const char *file_name,
180			int (*predicate) (const char *module, const char *file),
181			int fd, Elf *member, Dwfl_Module **mod)
182{
183  const Elf_Arhdr *h = elf_getarhdr (member);
184  if (unlikely (h == NULL))
185    {
186      __libdwfl_seterrno (DWFL_E_LIBELF);
187    fail:
188      elf_end (member);
189      *mod = NULL;
190      return ELF_C_NULL;
191    }
192
193  if (!strcmp (h->ar_name, "/") || !strcmp (h->ar_name, "//"))
194    {
195    skip:;
196      /* Skip this and go to the next.  */
197      Elf_Cmd result = elf_next (member);
198      elf_end (member);
199      return result;
200    }
201
202  char *member_name;
203  if (unlikely (asprintf (&member_name, "%s(%s)", file_name, h->ar_name) < 0))
204    {
205    nomem:
206      __libdwfl_seterrno (DWFL_E_NOMEM);
207      elf_end (member);
208      *mod = NULL;
209      return ELF_C_NULL;
210    }
211
212  char *module_name = NULL;
213  if (name == NULL || name[0] == '\0')
214    name = h->ar_name;
215  else if (unlikely (asprintf (&module_name, "%s:%s", name, h->ar_name) < 0))
216    {
217      free (member_name);
218      goto nomem;
219    }
220  else
221    name = module_name;
222
223  if (predicate != NULL)
224    {
225      /* Let the predicate decide whether to use this one.  */
226      int want = (*predicate) (name, member_name);
227      if (want <= 0)
228	{
229	  free (member_name);
230	  free (module_name);
231	  if (unlikely (want < 0))
232	    {
233	      __libdwfl_seterrno (DWFL_E_CB);
234	      goto fail;
235	    }
236	  goto skip;
237	}
238    }
239
240  /* We let __libdwfl_report_elf cache the fd in mod->main.fd,
241     though it's the same fd for all the members.
242     On module teardown we will close it only on the last Elf reference.  */
243  *mod = process_file (dwfl, name, member_name, fd, member, predicate);
244  free (member_name);
245  free (module_name);
246
247  if (*mod == NULL)		/* process_file called elf_end.  */
248    return ELF_C_NULL;
249
250  /* Advance the archive-reading offset for the next iteration.  */
251  return elf_next (member);
252}
253
254/* Report each member of the archive as its own module.  */
255static Dwfl_Module *
256process_archive (Dwfl *dwfl, const char *name, const char *file_name, int fd,
257		 Elf *archive,
258		 int (*predicate) (const char *module, const char *file))
259
260{
261  Dwfl_Module *mod = NULL;
262  Elf *member = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, archive);
263  if (unlikely (member == NULL)) /* Empty archive.  */
264    {
265      __libdwfl_seterrno (DWFL_E_BADELF);
266      return NULL;
267    }
268
269  while (process_archive_member (dwfl, name, file_name, predicate,
270				 fd, member, &mod) != ELF_C_NULL)
271    member = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, archive);
272
273  /* We can drop the archive Elf handle even if we're still using members
274     in live modules.  When the last module's elf_end on a member returns
275     zero, that module will close FD.  If no modules survived the predicate,
276     we are all done with the file right here.  */
277  if (mod != NULL		/* If no modules, caller will clean up.  */
278      && elf_end (archive) == 0)
279    close (fd);
280
281  return mod;
282}
283
284Dwfl_Module *
285internal_function
286__libdwfl_report_offline (Dwfl *dwfl, const char *name,
287			  const char *file_name, int fd, bool closefd,
288			  int (*predicate) (const char *module,
289					    const char *file))
290{
291  Elf *elf = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, NULL);
292  Dwfl_Module *mod = process_file (dwfl, name, file_name, fd, elf, predicate);
293  if (mod == NULL)
294    {
295      elf_end (elf);
296      if (closefd)
297	close (fd);
298    }
299  return mod;
300}
301
302Dwfl_Module *
303dwfl_report_offline (Dwfl *dwfl, const char *name,
304		     const char *file_name, int fd)
305{
306  if (dwfl == NULL)
307    return NULL;
308
309  bool closefd = false;
310  if (fd < 0)
311    {
312      closefd = true;
313      fd = open64 (file_name, O_RDONLY);
314      if (fd < 0)
315	{
316	  __libdwfl_seterrno (DWFL_E_ERRNO);
317	  return NULL;
318	}
319    }
320
321  return __libdwfl_report_offline (dwfl, name, file_name, fd, closefd, NULL);
322}
323INTDEF (dwfl_report_offline)
324