summaryrefslogtreecommitdiff
path: root/libdwfl/dwfl_report_elf.c
blob: 82545631f1692a6afdcfa2c9f050eb6ec8b8623d (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
/* Report a module to libdwfl based on ELF program headers.
   Copyright (C) 2005 Red Hat, Inc.

   This program is Open Source software; you can redistribute it and/or
   modify it under the terms of the Open Software License version 1.0 as
   published by the Open Source Initiative.

   You should have received a copy of the Open Software License along
   with this program; if not, you may obtain a copy of the Open Software
   License version 1.0 from http://www.opensource.org/licenses/osl.php or
   by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
   3001 King Ranch Road, Ukiah, CA 95482.   */

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


Dwfl_Module *
dwfl_report_elf (Dwfl *dwfl, const char *name,
		 const char *file_name, int fd, GElf_Addr base)
{
  bool closefd = false;

  if (fd < 0)
    {
      fd = open64 (file_name, O_RDONLY);
      if (fd < 0)
	{
	  __libdwfl_seterrno (DWFL_E_ERRNO);
	  return NULL;
	}
      closefd = true;
    }

  Elf *elf = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, NULL);

  GElf_Ehdr ehdr_mem, *ehdr = gelf_getehdr (elf, &ehdr_mem);
  if (ehdr == NULL)
    {
    elf_error:
      __libdwfl_seterrno (DWFL_E_LIBELF);
      if (closefd)
	close (fd);
      return NULL;
    }

  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;

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

	  if (shdr->sh_flags & SHF_ALLOC)
	    {
	      const GElf_Xword align = shdr->sh_addralign ?: 1;
	      shdr->sh_addr = (end + align - 1) & -align;
	      if (end == base)
		/* This is the first section assigned a location.
		   Use its aligned address as the module's base.  */
		start = shdr->sh_addr;
	      end = shdr->sh_addr + shdr->sh_size;
	      if (! gelf_update_shdr (scn, shdr))
		goto elf_error;
	    }
	}

      if (end == start)
	{
	  __libdwfl_seterrno (DWFL_E_BADELF);
	  if (closefd)
	    close (fd);
	  return NULL;
	}
      break;

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

    case ET_EXEC:
    case ET_CORE:
      /* An assigned base address is meaningless for these.  */
      base = 0;

    case ET_DYN:
    default:
      for (uint_fast16_t i = 0; i < ehdr->e_phnum; ++i)
	{
	  GElf_Phdr phdr_mem, *ph = gelf_getphdr (elf, i, &phdr_mem);
	  if (ph == NULL)
	    goto elf_error;
	  if (ph->p_type == PT_LOAD)
	    {
	      if ((base & (ph->p_align - 1)) != 0)
		base = (base + ph->p_align - 1) & -ph->p_align;
	      start = base + (ph->p_vaddr & -ph->p_align);
	      break;
	    }
	}
      bias = base;

      for (uint_fast16_t i = ehdr->e_phnum; i-- > 0;)
	{
	  GElf_Phdr phdr_mem, *ph = gelf_getphdr (elf, i, &phdr_mem);
	  if (ph == NULL)
	    goto elf_error;
	  if (ph->p_type == PT_LOAD)
	    {
	      end = base + (ph->p_vaddr + ph->p_memsz);
	      break;
	    }
	}

      if (end == 0)
	{
	  __libdwfl_seterrno (DWFL_E_NO_PHDR);
	  if (closefd)
	    close (fd);
	  return NULL;
	}
      break;
    }

  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))
	{
	  elf_end (elf);
	overlap:
	  if (closefd)
	    close (fd);
	  m->gc = true;
	  __libdwfl_seterrno (DWFL_E_OVERLAP);
	  m = NULL;
	}

      /* Preinstall the open ELF handle for the module.  */
      if (m->main.elf == NULL)
	{
	  m->main.elf = elf;
	  m->main.bias = bias;
	  m->e_type = ehdr->e_type;
	}
      else
	{
	  elf_end (elf);
	  if (m->main.bias != base)
	    goto overlap;
	}
    }
  return m;
}
INTDEF (dwfl_report_elf)