summaryrefslogtreecommitdiff
path: root/dwarflint/check_debug_aranges.cc
blob: a2fcd450ccde2e38b4250022759924ed79dbde84 (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
/* Low-level checking of .debug_aranges.
   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>.  */

// xxx drop as soon as not necessary
#define __STDC_FORMAT_MACROS

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

#include <inttypes.h>

#include "elf_file.hh"
#include "sections.hh"
#include "check_debug_aranges.hh"
#include "check_debug_info.hh"
#include "check_debug_loc_range.hh"
#include "cu_coverage.hh"
#include "checked_read.hh"
#include "misc.hh"
#include "pri.hh"

checkdescriptor const *
check_debug_aranges::descriptor ()
{
  static checkdescriptor cd
    (checkdescriptor::create ("check_debug_aranges")
     .groups ("@low")
     .prereq<typeof (*_m_sec_aranges)> ()
     .prereq<typeof (*_m_info)> ()
     .prereq<typeof (*_m_cu_coverage)> ()
     .description (
"Checks for low-level structure of .debug_aranges.  In addition it "
"checks:\n"
" - that relocations are valid.  In ET_REL files that certain fields "
"are relocated\n"
" - for dangling and duplicate CU references\n"
" - for garbage inside padding\n"
" - for zero-length ranges\n"
" - that the ranges cover all the address range covered by CUs\n"
		   ));
  return &cd;
}

static struct cu *
cu_find_cu (struct cu *cu_chain, uint64_t offset)
{
  for (struct cu *it = cu_chain; it != NULL; it = it->next)
    if (it->head->offset == offset)
      return it;
  return NULL;
}

#define PRI_CU "CU 0x%" PRIx64

namespace
{
  struct hole_user
  {
    elf_file *elf;
    section_id id;
    char const *what;
    bool reverse;

    hole_user (elf_file *a_elf, section_id a_id,
	       char const *a_what, bool a_reverse)
      : elf (a_elf)
      , id (a_id)
      , what (a_what)
      , reverse (a_reverse)
    {}
  };
}

static bool
hole (uint64_t start, uint64_t length, void *user)
{
  /* We need to check alignment vs. the covered section.  Find
     where the hole lies.  */
  ::hole_user &info = *static_cast< ::hole_user *> (user);
  struct elf_file *elf = info.elf;
  struct sec *sec = NULL;
  for (size_t i = 1; i < elf->size; ++i)
    {
      struct sec *it = elf->sec + i;
      GElf_Shdr *shdr = &it->shdr;
      Elf64_Addr s_end = shdr->sh_addr + shdr->sh_size;
      if (start >= shdr->sh_addr && start + length < s_end)
	{
	  sec = it;
	  /* Simply assume the first section that the hole
	     intersects. */
	  break;
	}
    }

  if (sec == NULL
      || !necessary_alignment (start, length, sec->shdr.sh_addralign))
    {
      char buf[128];
      where where = WHERE (info.id, NULL);
      char const *what = info.what;
      char const *cu = "CUs";
      if (info.reverse)
	{
	  char const *tmp = what;
	  what = cu;
	  cu = tmp;
	}
      wr_message (mc_aranges | mc_impact_3, &where,
		  ": addresses %s are covered with %s, but not with %s.\n",
		  range_fmt (buf, sizeof (buf), start, start + length),
		  cu, what);
    }

  if (sec == NULL)
    wr_error (NULL, "Couldn't find the section containing the above hole.\n");

  return true;
}

static void
compare_coverage_1 (struct elf_file *file,
		    struct coverage *coverage, struct coverage *other,
		    enum section_id id, char const *what,
		    bool reverse)
{
  struct coverage cov = *coverage - *other;
  hole_user info (file, id, what, reverse);
  cov.find_ranges (hole, &info);
}

static void
compare_coverage (struct elf_file *file,
		  struct coverage *coverage, struct coverage *other,
		  enum section_id id, char const *what)
{
  compare_coverage_1 (file, coverage, other, id, what, false);
  compare_coverage_1 (file, other, coverage, id, what, false);
}

inline static void
aranges_coverage_add (struct coverage *aranges_coverage,
		      uint64_t begin, uint64_t length,
		      struct where *where)
{
  if (aranges_coverage->is_overlap (begin, length))
    {
      char buf[128];
      /* Not a show stopper, this shouldn't derail high-level.  */
      wr_message (mc_aranges | mc_impact_2 | mc_error, where,
		  ": the range %s overlaps with another one.\n",
		  range_fmt (buf, sizeof buf, begin, begin + length));
    }

  aranges_coverage->add (begin, length);
}

/* COVERAGE is portion of address space covered by CUs (either via
   low_pc/high_pc pairs, or via DW_AT_ranges references).  If
   non-NULL, analysis of arange coverage is done against that set. */
static bool
check_aranges_structural (struct elf_file *file,
			  struct sec *sec,
			  struct cu *cu_chain,
			  struct coverage *coverage)
{
  struct read_ctx ctx;
  read_ctx_init (&ctx, sec->data, file->other_byte_order);

  bool retval = true;

  struct coverage *aranges_coverage
    = coverage != NULL ? new struct coverage () : NULL;

  while (!read_ctx_eof (&ctx))
    {
      struct where where = WHERE (sec_aranges, NULL);
      where_reset_1 (&where, read_ctx_get_offset (&ctx));
      const unsigned char *atab_begin = ctx.ptr;

      /* Size.  */
      uint32_t size32;
      uint64_t size;
      int offset_size;
      if (!read_ctx_read_4ubyte (&ctx, &size32))
	{
	  wr_error (&where, ": can't read table length.\n");
	  return false;
	}
      if (!read_size_extra (&ctx, size32, &size, &offset_size, &where))
	return false;

      struct read_ctx sub_ctx;
      const unsigned char *atab_end = ctx.ptr + size;
      if (false)
	{
	next:
	  if (!read_ctx_skip (&ctx, size))
	    /* A "can't happen" error.  */
	    goto not_enough;
	  continue;
	}
      if (!read_ctx_init_sub (&sub_ctx, &ctx, atab_begin, atab_end))
	{
	not_enough:
	  wr_error (&where, PRI_NOT_ENOUGH, "next table");
	  return false;
	}

      sub_ctx.ptr = ctx.ptr;

      /* Version.  */
      uint16_t version;
      if (!read_ctx_read_2ubyte (&sub_ctx, &version))
	{
	  wr_error (&where, ": can't read version.\n");
	  retval = false;
	  goto next;
	}
      if (!supported_version (version, 1, &where, 2))
	{
	  retval = false;
	  goto next;
	}

      /* CU offset.  */
      uint64_t cu_offset;
      uint64_t ctx_offset = sub_ctx.ptr - ctx.begin;
      if (!read_ctx_read_offset (&sub_ctx, offset_size == 8, &cu_offset))
	{
	  wr_error (&where, ": can't read debug info offset.\n");
	  retval = false;
	  goto next;
	}

      struct relocation *rel;
      if ((rel = relocation_next (&sec->rel, ctx_offset,
				  &where, skip_mismatched)))
	relocate_one (file, &sec->rel, rel, offset_size,
		      &cu_offset, &where, sec_info, NULL);
      else if (file->ehdr.e_type == ET_REL)
	wr_message (mc_impact_2 | mc_aranges | mc_reloc | mc_header, &where,
		    PRI_LACK_RELOCATION, "debug info offset");

      struct cu *cu = NULL;
      if (cu_chain != NULL && (cu = cu_find_cu (cu_chain, cu_offset)) == NULL)
	wr_error (&where, ": unresolved reference to " PRI_CU ".\n", cu_offset);

      struct where where_cudie;
      if (cu != NULL)
	{
	  where_cudie = WHERE (sec_info, NULL);
	  where_reset_1 (&where_cudie, cu->cudie_offset);
	  where.ref = &where_cudie;
	  where_cudie.formatting = wf_cudie;
	  if (cu->has_arange)
	    wr_error (where)
	      << "there has already been arange section for this CU."
	      << std::endl;
	  else
	    cu->has_arange = true;
	}

      /* Address size.  */
      int address_size;
      if (!read_address_size (&sub_ctx, file->addr_64, &address_size, &where))
	{
	  retval = false;
	  goto next;
	}

      /* Segment size.  */
      uint8_t segment_size;
      if (!read_ctx_read_ubyte (&sub_ctx, &segment_size))
	{
	  wr_error (&where, ": can't read unit segment size.\n");
	  retval = false;
	  goto next;
	}
      if (segment_size != 0)
	{
	  wr_warning (&where, ": dwarflint can't handle segment_size != 0.\n");
	  retval = false;
	  goto next;
	}


      /* 7.20: The first tuple following the header in each set begins
	 at an offset that is a multiple of the size of a single tuple
	 (that is, twice the size of an address). The header is
	 padded, if necessary, to the appropriate boundary.  */
      const uint8_t tuple_size = 2 * address_size;
      uint64_t off = read_ctx_get_offset (&sub_ctx);
      if ((off % tuple_size) != 0)
	{
	  uint64_t noff = ((off / tuple_size) + 1) * tuple_size;
	  for (uint64_t i = off; i < noff; ++i)
	    {
	      uint8_t c;
	      if (!read_ctx_read_ubyte (&sub_ctx, &c))
		{
		  wr_error (&where,
			    ": section ends after the header, "
			    "but before the first entry.\n");
		  retval = false;
		  goto next;
		}
	      if (c != 0)
		wr_message (mc_impact_2 | mc_aranges | mc_header, &where,
			    ": non-zero byte at 0x%" PRIx64
			    " in padding before the first entry.\n",
			    read_ctx_get_offset (&sub_ctx));
	    }
	}
      assert ((read_ctx_get_offset (&sub_ctx) % tuple_size) == 0);

      while (!read_ctx_eof (&sub_ctx))
	{
	  /* We would like to report aranges the same way that readelf
	     does.  But readelf uses index of the arange in the array
	     as returned by dwarf_getaranges, which sorts the aranges
	     beforehand.  We don't want to disturb the memory this
	     way, the better to catch structural errors accurately.
	     So report arange offset instead.  If this becomes a
	     problem, we will achieve this by two-pass analysis.  */
	  where_reset_2 (&where, read_ctx_get_offset (&sub_ctx));

	  /* Record address.  */
	  uint64_t address;
	  ctx_offset = sub_ctx.ptr - ctx.begin;
	  bool address_relocated = false;
	  if (!read_ctx_read_var (&sub_ctx, address_size, &address))
	    {
	      wr_error (&where, ": can't read address field.\n");
	      retval = false;
	      goto next;
	    }

	  if ((rel = relocation_next (&sec->rel, ctx_offset,
				      &where, skip_mismatched)))
	    {
	      address_relocated = true;
	      relocate_one (file, &sec->rel, rel, address_size,
			    &address, &where, rel_address, NULL);
	    }
	  else if (file->ehdr.e_type == ET_REL && address != 0)
	    wr_message (mc_impact_2 | mc_aranges | mc_reloc, &where,
			PRI_LACK_RELOCATION, "address field");

	  /* Record length.  */
	  uint64_t length;
	  if (!read_ctx_read_var (&sub_ctx, address_size, &length))
	    {
	      wr_error (&where, ": can't read length field.\n");
	      retval = false;
	      goto next;
	    }

	  if (address == 0 && length == 0 && !address_relocated)
	    break;

	  if (length == 0)
	    /* DWARF 3 spec, 6.1.2 Lookup by Address: Each descriptor
	       is a pair consisting of the beginning address [...],
	       followed by the _non-zero_ length of that range.  */
	    wr_error (&where, ": zero-length address range.\n");
	  /* Skip coverage analysis if we have errors.  */
	  else if (retval && aranges_coverage != NULL)
	    aranges_coverage_add (aranges_coverage, address, length, &where);
	}

      if (sub_ctx.ptr != sub_ctx.end)
	{
	  uint64_t start, end;
	  struct where wh = WHERE (where.section, NULL);
	  if (read_check_zero_padding (&sub_ctx, &start, &end))
	    wr_message_padding_0 (mc_aranges, &wh, start, end);
	  else
	    {
	      wr_message_padding_n0 (mc_aranges | mc_error, &wh,
				     start, start + size);
	      retval = false;
	    }
	}

      goto next;
    }

  if (aranges_coverage != NULL)
    {
      compare_coverage (file, coverage, aranges_coverage,
			sec_aranges, "aranges");
      delete aranges_coverage;
    }

  return retval;
}

check_debug_aranges::check_debug_aranges (checkstack &stack, dwarflint &lint)
  : _m_sec_aranges (lint.check (stack, _m_sec_aranges))
  , _m_info (lint.toplev_check (stack, _m_info))
  , _m_cu_coverage (lint.toplev_check (stack, _m_cu_coverage))
{
  coverage *cov = _m_cu_coverage != NULL ? &_m_cu_coverage->cov : NULL;

  if (!check_aranges_structural (&_m_sec_aranges->file,
				 &_m_sec_aranges->sect,
				 _m_info != NULL
				   ? &_m_info->cus.front () : NULL,
				 cov))
    throw check_base::failed ();
}