summaryrefslogtreecommitdiff
path: root/dwarflint/sections.cc
blob: b1fb2a6723921bbfb746b69f27be89bfc8538db0 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* Low-level section handling.
   Copyright (C) 2009, 2010 Red Hat, Inc.
   This file is part of Red Hat elfutils.

   Red Hat elfutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by the
   Free Software Foundation; version 2 of the License.

   Red Hat 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 a copy of the GNU General Public License along
   with Red Hat elfutils; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.

   Red Hat elfutils is an included package of the Open Invention Network.
   An included package of the Open Invention Network is a package for which
   Open Invention Network licensees cross-license their patents.  No patent
   license is granted, either expressly or impliedly, by designation as an
   included package.  Should you wish to participate in the Open Invention
   Network licensing program, please visit www.openinventionnetwork.com
   <http://www.openinventionnetwork.com>.  */

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

#include <cstdlib>
#include <cstring>
#include "../libelf/gelf.h"

#include "sections.hh"
#include "messages.hh"
#include "pri.hh"
#include "misc.hh"

checkdescriptor const *
load_sections::descriptor ()
{
  static checkdescriptor cd
    (checkdescriptor::create ("load_sections")
     .hidden ());
  return &cd;
}

checkdescriptor const *
section_base::descriptor ()
{
  static checkdescriptor cd
    (checkdescriptor::create ()
     .prereq<typeof (*sections)> ());
  return &cd;
}

namespace
{
  int
  layout_rel_file (Elf *elf)
  {
    GElf_Ehdr ehdr;
    if (gelf_getehdr (elf, &ehdr) == NULL)
      return 1;

    if (ehdr.e_type != ET_REL)
      return 0;

    /* Taken from libdwfl. */
    GElf_Addr base = 0;
    GElf_Addr start = 0, end = 0, bias = 0;

    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))
	  return 1;

	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 layed 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))
			  return 1;
			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)))
			      return 1;
			  }
		      }
		    while (prev_scn != scn);
		    continue;
		  }

		end = shdr->sh_addr + shdr->sh_size;
		if (likely (shdr->sh_addr != 0)
		    && unlikely (! gelf_update_shdr (scn, shdr)))
		  return 1;
	      }
	    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;
	  }
      }
    return 0;
  }

  Elf *
  open_elf (int fd)
  {
    Elf *elf = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, NULL);
    if (unlikely (elf == NULL))
      {
	wr_error ()
	  << "Error opening file: " << elf_errmsg (-1) << std::endl;
	throw check_base::failed ();
      }

    if (layout_rel_file (elf))
      {
	wr_error ()
	  << "Couldn't layout ET_REL file." << std::endl;
	throw check_base::failed ();
      }

    return elf;
  }

  struct secentry
  {
    Elf_Data *reldata;	/* Relocation data if any found.  */
    size_t reltype;	/* SHT_REL or SHT_RELA.  We need this
			   temporary store to be able to resolve
			   relocation section appearing before
			   relocated section.  */
    size_t secndx;	/* Index into file->sec or 0 if not yet loaded.  */
    section_id id;	/* Section type.  */

    explicit secentry (section_id a_id = sec_invalid)
      : reldata (NULL)
      , reltype (0)
      , secndx (0)
      , id (a_id)
    {}
  };

  struct secinfo_map
    : public std::map <std::string, secentry>
  {
    secinfo_map ()
    {
      // 0 is invalid section
      for (unsigned i = 1; i < count_debuginfo_sections; ++i)
	(*this)[section_name[i]] = secentry (static_cast<section_id> (i));
    }

    secentry *get (const char *name)
    {
      iterator it = find (std::string (name));
      if (it == end ())
	return NULL;
      else
	return &it->second;
    }
  };

  bool
  elf_file_init (struct elf_file *file, int fd)
  {
    Elf *elf = open_elf (fd);
    assert (elf != NULL);
    memset (file, 0, sizeof (*file));

    file->elf = elf;
    file->ebl = ebl_openbackend (elf);

    if (file->ebl == NULL
	|| gelf_getehdr (elf, &file->ehdr) == NULL)
      return false;

    file->addr_64 = file->ehdr.e_ident[EI_CLASS] == ELFCLASS64;

    /* Taken from dwarf_begin_elf.c.  */
    if ((BYTE_ORDER == LITTLE_ENDIAN
	 && file->ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
	|| (BYTE_ORDER == BIG_ENDIAN
	    && file->ehdr.e_ident[EI_DATA] == ELFDATA2LSB))
      file->other_byte_order = true;

    Elf_Scn *reloc_symtab = NULL;
    secinfo_map secinfo;

    /* Now find all necessary debuginfo sections and associated
       relocation sections.  */

    /* Section 0 is special, skip it.  */
    REALLOC (file, sec);
    file->sec[file->size++].id = sec_invalid;

    bool check_rel = true;

    for (Elf_Scn *scn = NULL; (scn = elf_nextscn (elf, scn)); )
      {
	REALLOC (file, sec);
	size_t curndx = file->size++;
	struct sec *cursec = file->sec + curndx;

	GElf_Shdr *shdr = gelf_getshdr (scn, &cursec->shdr);
	if (shdr == NULL)
	  {
	  invalid_elf:
	    wr_error () << "Broken ELF." << std::endl;
	    goto close_and_out;
	  }

	const char *scnname = elf_strptr (elf, file->ehdr.e_shstrndx,
					  shdr->sh_name);
	if (scnname == NULL)
	  goto invalid_elf;

	if (!address_aligned (shdr->sh_addr, shdr->sh_addralign))
	  wr_error ()
	    << "Base address of section " << scnname << ", "
	    << pri::addr (shdr->sh_addr) << ", should have an alignment of "
	    << shdr->sh_addralign << std::endl;

	secentry *entry = secinfo.get (scnname);
	cursec->scn = scn;
	cursec->id = entry != NULL ? entry->id : sec_invalid;
	cursec->name = scnname;
	cursec->rel = (struct relocation_data){NULL, SHT_NULL, NULL, 0, 0, 0};

	/* Dwarf section.  */
	if (entry != NULL)
	  {
	    if (unlikely (entry->secndx != 0))
	      wr_error ()
		<< "Multiple occurrences of section " << scnname << std::endl;
	    else
	      {
		/* Haven't seen a section of that name yet.  */
		cursec->data = elf_getdata (scn, NULL);
		if (cursec->data == NULL || cursec->data->d_buf == NULL)
		  /* Don't print out a warning, we'll get to that in
		     process_file.  */
		  cursec->data = NULL;
		entry->secndx = curndx;
	      }
	  }
	/* Relocation section.  */
	else if (shdr->sh_type == SHT_RELA || shdr->sh_type == SHT_REL)
	  {
	    /* Get data of section that this REL(A) section relocates.  */
	    Elf_Scn *relocated_scn = elf_getscn (elf, shdr->sh_info);
	    Elf_Scn *symtab_scn = elf_getscn (elf, shdr->sh_link);
	    if (relocated_scn == NULL || symtab_scn == NULL)
	      goto invalid_elf;

	    GElf_Shdr relocated_shdr_mem;
	    GElf_Shdr *relocated_shdr = gelf_getshdr (relocated_scn,
						      &relocated_shdr_mem);
	    if (relocated_shdr == NULL)
	      goto invalid_elf;

	    const char *relocated_scnname
	      = elf_strptr (elf, file->ehdr.e_shstrndx,
			    relocated_shdr->sh_name);

	    secentry *relocated = secinfo.get (relocated_scnname);

	    if (relocated != NULL)
	      {
		if (relocated->reldata != NULL)
		  wr_error ()
		    << "Several relocation sections for debug section "
		    << relocated_scnname << ". Ignoring " << scnname
		    << "." << std::endl;
		else
		  {
		    relocated->reldata = elf_getdata (scn, NULL);
		    if (unlikely (relocated->reldata == NULL
				  || relocated->reldata->d_buf == NULL))
		      {
			wr_error ()
			  << "Data-less relocation section " << scnname
			  << "." << std::endl;
			relocated->reldata = NULL;
		      }
		    else
		      relocated->reltype = shdr->sh_type;
		  }

		if (reloc_symtab == NULL)
		  reloc_symtab = symtab_scn;
		else if (reloc_symtab != symtab_scn)
		  wr_error ()
		    << "Relocation sections use multiple symbol tables."
		    << std::endl;
	      }
	  }
      }

    for (secinfo_map::iterator it = secinfo.begin (); it != secinfo.end (); ++it)
      if (it->second.secndx != 0)
	file->debugsec[it->second.id] = file->sec + it->second.secndx;

    if (check_rel)
      {
	Elf_Data *reloc_symdata = NULL;
	if (reloc_symtab != NULL)
	  {
	    reloc_symdata = elf_getdata (reloc_symtab, NULL);
	    if (reloc_symdata == NULL)
	      /* Not a show stopper, we can check a lot of stuff even
		 without a symbol table.  */
	      wr_error () << "Couldn't obtain symtab data." << std::endl;
	  }

	/* Check relocation sections that we've got.  */
	for (secinfo_map::iterator it = secinfo.begin (); it != secinfo.end (); ++it)
	  {
	    secentry *cur = &it->second;
	    if (cur->secndx != 0 && cur->reldata != NULL)
	      {
		struct sec *sec = file->sec + cur->secndx;
		sec->rel.type = cur->reltype;
		if (sec->data == NULL)
		  wr_error (WHERE (sec->id, NULL))
		    << "this data-less section has a relocation section."
		    << std::endl;
		else if (read_rel (file, sec, cur->reldata, file->addr_64))
		  sec->rel.symdata = reloc_symdata;
	      }
	  }

	if (secentry *str = secinfo.get (".debug_str"))
	  if (str->reldata != NULL)
	    wr_message (WHERE (sec_str, NULL), cat (mc_impact_2, mc_elf))
	      << "there's a relocation section associated with this section."
	      << std::endl;
      }

    return true;

  close_and_out:
    if (elf != NULL)
      {
	elf_errno (); // clear errno
	elf_end (elf);
	int err = elf_errno ();
	if (err != 0)
	  wr_error ()
	    << "error while closing Elf descriptor: "
	    << elf_errmsg (err) << std::endl;
      }
    return false;
  }
}

load_sections::load_sections (checkstack &stack __attribute__ ((unused)),
			      dwarflint &lint)
{
  if (!elf_file_init (&file, lint.fd ()))
    throw check_base::failed ();
}

load_sections::~load_sections ()
{
  if (file.ebl != NULL)
    ebl_closebackend (file.ebl);
  free (file.sec);
  elf_end (file.elf);
}

sec &
section_base::get_sec_or_throw (section_id secid)
{
  if (sec *s = sections->file.debugsec[secid])
    if (s->data != NULL)
      return *s;

  throw check_base::failed ();
}

section_base::section_base (checkstack &stack,
			    dwarflint &lint, section_id secid)
  : sections (lint.check (stack, sections))
  , sect (get_sec_or_throw (secid))
  , file (sections->file)
{
}