aboutsummaryrefslogtreecommitdiff
path: root/libdwfl/dwfl_report_elf.c
blob: a76d36816c6f1a50e813e4c688f41b51a851a0b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* Report a module to libdwfl based on ELF program headers.
   Copyright (C) 2005-2010 Red Hat, Inc.
   This file is part of elfutils.

   This file is free software; you can redistribute it and/or modify
   it under the terms of either

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at
       your option) any later version

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at
       your option) any later version

   or both in parallel, as here.

   elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see <http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "libdwflP.h"
#include <fcntl.h>

/* We start every ET_REL module at a moderately aligned boundary.
   This keeps the low addresses easy to read compared to a layout
   starting at 0 (as when using -e).  It also makes it unlikely
   that a middle section will have a larger alignment and require
   rejiggering (see below).  */
#define REL_MIN_ALIGN	((GElf_Xword) 0x100)

bool
internal_function
__libdwfl_elf_address_range (Elf *elf, GElf_Addr base, bool add_p_vaddr,
			     bool sanity, GElf_Addr *vaddrp,
			     GElf_Addr *address_syncp, GElf_Addr *startp,
			     GElf_Addr *endp, GElf_Addr *biasp,
			     GElf_Half *e_typep)
{
  GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (elf, &ehdr_mem);
  if (ehdr == NULL)
    {
    elf_error:
      __libdwfl_seterrno (DWFL_E_LIBELF);
      return false;
    }

  GElf_Addr vaddr = 0;
  GElf_Addr address_sync = 0;
  GElf_Addr start = 0, end = 0, bias = 0;
  switch (ehdr->e_type)
    {
    case ET_REL:
      /* For a relocatable object, we do an arbitrary section layout.
	 By updating the section header in place, we leave the layout
	 information to be found by relocation.  */

      start = end = base = (base + REL_MIN_ALIGN - 1) & -REL_MIN_ALIGN;

      bool first = true;
      Elf_Scn *scn = NULL;
      while ((scn = elf_nextscn (elf, scn)) != NULL)
	{
	  GElf_Shdr shdr_mem;
	  GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
	  if (unlikely (shdr == NULL))
	    goto elf_error;

	  if (shdr->sh_flags & SHF_ALLOC)
	    {
	      const GElf_Xword align = shdr->sh_addralign ?: 1;
	      const GElf_Addr next = (end + align - 1) & -align;
	      if (shdr->sh_addr == 0
		  /* Once we've started doing layout we have to do it all,
		     unless we just laid out the first section at 0 when
		     it already was at 0.  */
		  || (bias == 0 && end > start && end != next))
		{
		  shdr->sh_addr = next;
		  if (end == base)
		    /* This is the first section assigned a location.
		       Use its aligned address as the module's base.  */
		    start = base = shdr->sh_addr;
		  else if (unlikely (base & (align - 1)))
		    {
		      /* If BASE has less than the maximum alignment of
			 any section, we eat more than the optimal amount
			 of padding and so make the module's apparent
			 size come out larger than it would when placed
			 at zero.  So reset the layout with a better base.  */

		      start = end = base = (base + align - 1) & -align;
		      Elf_Scn *prev_scn = NULL;
		      do
			{
			  prev_scn = elf_nextscn (elf, prev_scn);
			  GElf_Shdr prev_shdr_mem;
			  GElf_Shdr *prev_shdr = gelf_getshdr (prev_scn,
							       &prev_shdr_mem);
			  if (unlikely (prev_shdr == NULL))
			    goto elf_error;
			  if (prev_shdr->sh_flags & SHF_ALLOC)
			    {
			      const GElf_Xword prev_align
				= prev_shdr->sh_addralign ?: 1;

			      prev_shdr->sh_addr
				= (end + prev_align - 1) & -prev_align;
			      end = prev_shdr->sh_addr + prev_shdr->sh_size;

			      if (unlikely (! gelf_update_shdr (prev_scn,
								prev_shdr)))
				goto elf_error;
			    }
			}
		      while (prev_scn != scn);
		      continue;
		    }

		  end = shdr->sh_addr + shdr->sh_size;
		  if (likely (shdr->sh_addr != 0)
		      && unlikely (! gelf_update_shdr (scn, shdr)))
		    goto elf_error;
		}
	      else
		{
		  /* The address is already assigned.  Just track it.  */
		  if (first || end < shdr->sh_addr + shdr->sh_size)
		    end = shdr->sh_addr + shdr->sh_size;
		  if (first || bias > shdr->sh_addr)
		    /* This is the lowest address in the module.  */
		    bias = shdr->sh_addr;

		  if ((shdr->sh_addr - bias + base) & (align - 1))
		    /* This section winds up misaligned using BASE.
		       Adjust BASE upwards to make it congruent to
		       the lowest section address in the file modulo ALIGN.  */
		    base = (((base + align - 1) & -align)
			    + (bias & (align - 1)));
		}

	      first = false;
	    }
	}

      if (bias != 0)
	{
	  /* The section headers had nonzero sh_addr values.  The layout
	     was already done.  We've just collected the total span.
	     Now just compute the bias from the requested base.  */
	  start = base;
	  end = end - bias + start;
	  bias = start - bias;
	}
      break;

      /* Everything else has to have program headers.  */

    case ET_EXEC:
    case ET_CORE:
      /* An assigned base address is meaningless for these.  */
      base = 0;
      add_p_vaddr = true;
      FALLTHROUGH;
    case ET_DYN:
    default:;
      size_t phnum;
      if (unlikely (elf_getphdrnum (elf, &phnum) != 0))
	goto elf_error;
      for (size_t i = 0; i < phnum; ++i)
	{
	  GElf_Phdr phdr_mem, *ph = gelf_getphdr (elf, i, &phdr_mem);
	  if (unlikely (ph == NULL))
	    goto elf_error;
	  if (ph->p_type == PT_LOAD)
	    {
	      vaddr = ph->p_vaddr & -ph->p_align;
	      address_sync = ph->p_vaddr + ph->p_memsz;
	      break;
	    }
	}
      if (add_p_vaddr)
	{
	  start = base + vaddr;
	  bias = base;
	}
      else
	{
	  start = base;
	  bias = base - vaddr;
	}

      for (size_t i = phnum; i-- > 0;)
	{
	  GElf_Phdr phdr_mem, *ph = gelf_getphdr (elf, i, &phdr_mem);
	  if (unlikely (ph == NULL))
	    goto elf_error;
	  if (ph->p_type == PT_LOAD
	      && ph->p_vaddr + ph->p_memsz > 0)
	    {
	      end = bias + (ph->p_vaddr + ph->p_memsz);
	      break;
	    }
	}

      if (end == 0 && sanity)
	{
	  __libdwfl_seterrno (DWFL_E_NO_PHDR);
	  return false;
	}
      break;
    }
  if (vaddrp)
    *vaddrp = vaddr;
  if (address_syncp)
    *address_syncp = address_sync;
  if (startp)
    *startp = start;
  if (endp)
    *endp = end;
  if (biasp)
    *biasp = bias;
  if (e_typep)
    *e_typep = ehdr->e_type;
  return true;
}

Dwfl_Module *
internal_function
__libdwfl_report_elf (Dwfl *dwfl, const char *name, const char *file_name,
		      int fd, Elf *elf, GElf_Addr base, bool add_p_vaddr,
		      bool sanity)
{
  GElf_Addr vaddr, address_sync, start, end, bias;
  GElf_Half e_type;
  if (! __libdwfl_elf_address_range (elf, base, add_p_vaddr, sanity, &vaddr,
				     &address_sync, &start, &end, &bias,
				     &e_type))
    return NULL;
  Dwfl_Module *m = INTUSE(dwfl_report_module) (dwfl, name, start, end);
  if (m != NULL)
    {
      if (m->main.name == NULL)
	{
	  m->main.name = strdup (file_name);
	  m->main.fd = fd;
	}
      else if ((fd >= 0 && m->main.fd != fd)
	       || strcmp (m->main.name, file_name))
	{
	overlap:
	  m->gc = true;
	  __libdwfl_seterrno (DWFL_E_OVERLAP);
	  return NULL;
	}

      /* Preinstall the open ELF handle for the module.  */
      if (m->main.elf == NULL)
	{
	  m->main.elf = elf;
	  m->main.vaddr = vaddr;
	  m->main.address_sync = address_sync;
	  m->main_bias = bias;
	  m->e_type = e_type;
	}
      else
	{
	  if (m->main_bias != bias
	      || m->main.vaddr != vaddr || m->main.address_sync != address_sync)
	    goto overlap;
	  elf_end (m->main.elf);
	  m->main.elf = elf;
	}
    }
  return m;
}

NEW_VERSION (dwfl_report_elf, ELFUTILS_0.156)
Dwfl_Module *
dwfl_report_elf (Dwfl *dwfl, const char *name, const char *file_name, int fd,
		 GElf_Addr base, bool add_p_vaddr)
{
  bool closefd = false;
  if (fd < 0)
    {
      closefd = true;
      fd = open (file_name, O_RDONLY);
      if (fd < 0)
	{
	  __libdwfl_seterrno (DWFL_E_ERRNO);
	  return NULL;
	}
    }

  Elf *elf;
  Dwfl_Error error = __libdw_open_file (&fd, &elf, closefd, false);
  if (error != DWFL_E_NOERROR)
    {
      __libdwfl_seterrno (error);
      return NULL;
    }

  Dwfl_Module *mod = __libdwfl_report_elf (dwfl, name, file_name,
					   fd, elf, base, add_p_vaddr, true);
  if (mod == NULL)
    {
      elf_end (elf);
      if (closefd)
	close (fd);
    }

  return mod;
}
NEW_INTDEF (dwfl_report_elf)

#ifdef SYMBOL_VERSIONING
Dwfl_Module *
  _compat_without_add_p_vaddr_dwfl_report_elf (Dwfl *dwfl, const char *name,
					       const char *file_name, int fd,
					       GElf_Addr base);
COMPAT_VERSION_NEWPROTO (dwfl_report_elf, ELFUTILS_0.122, without_add_p_vaddr)

Dwfl_Module *
_compat_without_add_p_vaddr_dwfl_report_elf (Dwfl *dwfl, const char *name,
					     const char *file_name, int fd,
					     GElf_Addr base)
{
  return dwfl_report_elf (dwfl, name, file_name, fd, base, true);
}
#endif