summaryrefslogtreecommitdiffstats
path: root/dwarflint/readctx.cc
blob: d7ed1c1646b867c95a0a0830f29de366dcd08982 (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
/* Pedantic checking of DWARF files.
   Copyright (C) 2009, 2010 Red Hat, Inc.
   This file is part of Red Hat elfutils.
   Written by Petr Machata <pmachata@redhat.com>, 2009.

   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>.  */

#include "readctx.hh"
#include "../libdw/dwarf.h"

#include <stdlib.h>
#include <assert.h>
#include <byteswap.h>
#include <inttypes.h>

/* read_Xubyte_* is basically cut'n'paste from memory-access.h.  */
union unaligned
  {
    void *p;
    uint16_t u2;
    uint32_t u4;
    uint64_t u8;
    int16_t s2;
    int32_t s4;
    int64_t s8;
  } __attribute__ ((packed));

static uint16_t
read_2ubyte_unaligned (const void *p, bool other_byte_order)
{
  const union unaligned *up = (const union unaligned *)p;
  if (other_byte_order)
    return bswap_16 (up->u2);
  return up->u2;
}

/* Prefix with dwarflint_ for export, so that it doesn't get confused
   with functions and macros in memory-access.h.  */
uint32_t
dwarflint_read_4ubyte_unaligned (const void *p, bool other_byte_order)
{
  const union unaligned *up = (const union unaligned *)p;
  if (other_byte_order)
    return bswap_32 (up->u4);
  return up->u4;
}

uint64_t
dwarflint_read_8ubyte_unaligned (const void *p, bool other_byte_order)
{
  const union unaligned *up = (const union unaligned *)p;
  if (other_byte_order)
    return bswap_64 (up->u8);
  return up->u8;
}


#define read_2ubyte_unaligned_inc(Addr, OtherByteOrder)			\
  ({ uint16_t t_ = read_2ubyte_unaligned (Addr, OtherByteOrder);	\
    Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2);		\
    t_; })

#define read_4ubyte_unaligned_inc(Addr, OtherByteOrder)			\
  ({ uint32_t t_ = dwarflint_read_4ubyte_unaligned (Addr, OtherByteOrder); \
    Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 4);		\
    t_; })

#define read_8ubyte_unaligned_inc(Addr, OtherByteOrder)			\
  ({ uint64_t t_ = dwarflint_read_8ubyte_unaligned (Addr, OtherByteOrder); \
    Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 8);		\
    t_; })



void
read_ctx_init (struct read_ctx *ctx, Elf_Data *data, bool other_byte_order)
{
  if (data == NULL)
    abort ();

  ctx->data = data;
  ctx->begin = (const unsigned char *)data->d_buf;
  ctx->end = (const unsigned char *)data->d_buf + data->d_size;
  ctx->ptr = (const unsigned char *)data->d_buf;
  ctx->other_byte_order = other_byte_order;
}

bool
read_ctx_init_sub (struct read_ctx *ctx, struct read_ctx *parent,
		   const unsigned char *begin, const unsigned char *end)
{
  if (parent == NULL)
    abort ();

  if (begin < parent->begin
      || end > parent->end)
    return false;

  ctx->data = parent->data;
  ctx->begin = begin;
  ctx->end = end;
  ctx->ptr = begin;
  ctx->other_byte_order = parent->other_byte_order;
  return true;
}

uint64_t
read_ctx_get_offset (struct read_ctx *ctx)
{
  assert (ctx->ptr >= ctx->begin);
  return (uint64_t)(ctx->ptr - ctx->begin);
}

bool
read_ctx_need_data (struct read_ctx *ctx, size_t length)
{
  const unsigned char *ptr = ctx->ptr + length;
  return ptr <= ctx->end && (length == 0 || ptr > ctx->ptr);
}

bool
read_ctx_read_ubyte (struct read_ctx *ctx, unsigned char *ret)
{
  if (!read_ctx_need_data (ctx, 1))
    return false;
  if (ret != NULL)
    *ret = *ctx->ptr;
  ctx->ptr++;
  return true;
}

int
read_ctx_read_uleb128 (struct read_ctx *ctx, uint64_t *ret)
{
  uint64_t result = 0;
  int shift = 0;
  int size = 8 * sizeof (result);
  bool zero_tail = false;

  while (1)
    {
      uint8_t byte;
      if (!read_ctx_read_ubyte (ctx, &byte))
	return -1;

      uint8_t payload = byte & 0x7f;
      zero_tail = payload == 0 && shift > 0;
      result |= (uint64_t)payload << shift;
      shift += 7;
      if (shift > size && byte != 0x1)
	return -1;
      if ((byte & 0x80) == 0)
	break;
    }

  if (ret != NULL)
    *ret = result;
  return zero_tail ? 1 : 0;
}

int
read_ctx_read_sleb128 (struct read_ctx *ctx, int64_t *ret)
{
  int64_t result = 0;
  int shift = 0;
  int size = 8 * sizeof (result);
  bool zero_tail = false;
  bool sign = false;

  while (1)
    {
      uint8_t byte;
      if (!read_ctx_read_ubyte (ctx, &byte))
	return -1;

      uint8_t payload = byte & 0x7f;
      zero_tail = shift > 0 && ((payload == 0x7f && sign)
				|| (payload == 0 && !sign));
      sign = (byte & 0x40) != 0; /* Set sign for rest of loop & next round.  */
      result |= (int64_t)payload << shift;
      shift += 7;
      if ((byte & 0x80) == 0)
	{
	  if (shift < size && sign)
	    result |= -((int64_t)1 << shift);
	  break;
	}
      if (shift > size)
	return -1;
    }

  if (ret != NULL)
    *ret = result;
  return zero_tail ? 1 : 0;
}

bool
read_ctx_read_2ubyte (struct read_ctx *ctx, uint16_t *ret)
{
  if (!read_ctx_need_data (ctx, 2))
    return false;
  uint16_t val = read_2ubyte_unaligned_inc (ctx->ptr, ctx->other_byte_order);
  if (ret != NULL)
    *ret = val;
  return true;
}

bool
read_ctx_read_4ubyte (struct read_ctx *ctx, uint32_t *ret)
{
  if (!read_ctx_need_data (ctx, 4))
    return false;
  uint32_t val = read_4ubyte_unaligned_inc (ctx->ptr, ctx->other_byte_order);
  if (ret != NULL)
    *ret = val;
  return true;
}

bool
read_ctx_read_8ubyte (struct read_ctx *ctx, uint64_t *ret)
{
  if (!read_ctx_need_data (ctx, 8))
    return false;
  uint64_t val = read_8ubyte_unaligned_inc (ctx->ptr, ctx->other_byte_order);
  if (ret != NULL)
    *ret = val;
  return true;
}

bool
read_ctx_read_offset (struct read_ctx *ctx, bool dwarf64, uint64_t *ret)
{
  if (dwarf64)
    return read_ctx_read_8ubyte (ctx, ret);

  uint32_t v;
  if (!read_ctx_read_4ubyte (ctx, &v))
    return false;

  if (ret != NULL)
    *ret = (uint64_t)v;
  return true;
}

bool
read_ctx_read_var (struct read_ctx *ctx, int width, uint64_t *ret)
{
  switch (width)
    {
    case 4:
    case 8:
      return read_ctx_read_offset (ctx, width == 8, ret);
    case 2:
      {
	uint16_t val;
	if (!read_ctx_read_2ubyte (ctx, &val))
	  return false;
	*ret = val;
	return true;
      }
    case 1:
      {
	uint8_t val;
	if (!read_ctx_read_ubyte (ctx, &val))
	  return false;
	*ret = val;
	return true;
      }
    default:
      return false;
    };
}

const char *
read_ctx_read_str (struct read_ctx *ctx)
{
  const char *ret = (const char *)ctx->ptr;
  uint8_t byte;
  do
    if (!read_ctx_read_ubyte (ctx, &byte))
      return NULL;
  while (byte != 0);
  return ret;
}

bool
read_ctx_skip (struct read_ctx *ctx, uint64_t len)
{
  if (!read_ctx_need_data (ctx, len))
    return false;
  ctx->ptr += len;
  return true;
}

bool
read_ctx_eof (struct read_ctx *ctx)
{
  return !read_ctx_need_data (ctx, 1);
}

static void
update_off (struct read_ctx *ctx,
	    uint64_t *ret_off)
{
  if (ret_off != NULL)
    *ret_off = (uint64_t)(ctx->ptr - ctx->begin);
}

bool
read_check_zero_padding (struct read_ctx *ctx,
			 uint64_t *ret_off_start,
			 uint64_t *ret_off_end)
{
  assert (ctx->ptr != ctx->end);
  update_off (ctx, ret_off_start);
  bool ret = true;

  const unsigned char *save_ptr = ctx->ptr;
  while (!read_ctx_eof (ctx))
    if (*ctx->ptr++ != 0)
      {
	ctx->ptr = save_ptr;
	goto done;
      }

 done:
  update_off (ctx, ret_off_end);
  return ret;
}