summaryrefslogtreecommitdiffstats
path: root/trunk/src/data.c
blob: 13380dac472cda4e420004a3466ab57dfaf123e1 (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
/* Copyright (C) 2001, 2002 Red Hat, Inc.
   Written by Jakub Jelinek <jakub@redhat.com>, 2001.

   This program 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; either version 2, or (at your option)
   any later version.

   This program 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 this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include <config.h>
#include "prelink.h"

#define UREAD(le,nn)						\
uint##nn##_t							\
read_u##le##nn (DSO *dso, GElf_Addr addr)			\
{								\
  Elf_Type type;						\
  unsigned char *data = get_data (dso, addr, NULL, &type);	\
								\
  if (data == NULL)						\
    return 0;							\
								\
  if (type == ELF_T_BYTE)					\
    return buf_read_u##le##nn (data);				\
  else								\
    return *(uint##nn##_t *)data;				\
}

#define WRITE(le,nn)						\
int								\
write_##le##nn (DSO *dso, GElf_Addr addr, uint##nn##_t val)	\
{								\
  int sec;							\
  Elf_Type type;						\
  unsigned char *data = get_data (dso, addr, &sec, &type);	\
								\
  if (data == NULL)						\
    return -1;							\
								\
  if (type == ELF_T_BYTE)					\
    buf_write_##le##nn (data, val);				\
  else								\
    *(uint##nn##_t *)data = val;				\
  elf_flagscn (dso->scn[sec], ELF_C_SET, ELF_F_DIRTY);		\
  return 0;							\
}

#define BUFREADUNE(nn)						\
uint##nn##_t							\
buf_read_une##nn (DSO *dso, unsigned char *buf)			\
{								\
  if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB)		\
    return buf_read_ule32 (buf);				\
  else								\
    return buf_read_ube32 (buf);				\
}

#define READUNE(nn)						\
uint##nn##_t							\
read_une##nn (DSO *dso, GElf_Addr addr)				\
{								\
  if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB)		\
    return read_ule##nn (dso, addr);				\
  else								\
    return read_ube##nn (dso, addr);				\
}

#define WRITENE(nn)						\
void								\
write_ne##nn (DSO *dso, GElf_Addr addr, uint##nn##_t val)	\
{								\
  if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB)		\
    write_le##nn (dso, addr, val);				\
  else								\
    write_be##nn (dso, addr, val);				\
}

#define BUFWRITENE(nn)						\
void								\
buf_write_ne##nn (DSO *dso, unsigned char *buf,			\
		  uint##nn##_t val)				\
{								\
  if (dso->ehdr.e_ident[EI_DATA] == ELFDATA2LSB)		\
    buf_write_le##nn (buf, val);				\
  else								\
    buf_write_be##nn (buf, val);				\
}

#define READWRITE(le,nn) UREAD(le,nn) WRITE(le,nn)
#define READWRITESIZE(nn) \
  READWRITE(le,nn) READWRITE(be,nn) \
  BUFREADUNE(nn) READUNE(nn) \
  WRITENE(nn) BUFWRITENE(nn)

unsigned char *
get_data (DSO *dso, GElf_Addr addr, int *secp, Elf_Type *typep)
{
  int sec = addr_to_sec (dso, addr);
  Elf_Data *data = NULL;

  if (sec == -1)
    return NULL;

  if (secp)
    *secp = sec;

  addr -= dso->shdr[sec].sh_addr;
  while ((data = elf_getdata (dso->scn[sec], data)) != NULL)
    if (data->d_off <= addr && data->d_off + data->d_size > addr)
    {
      if (typep) *typep = data->d_type;
      return (unsigned char *) data->d_buf + (addr - data->d_off);
    }
  return NULL;
}

/* Initialize IT so that the first byte it provides is address ADDR
   of DSO.  */

void
init_data_iterator (struct data_iterator *it, DSO *dso, GElf_Addr addr)
{
  it->dso = dso;
  it->data = NULL;
  it->addr = addr;
}

/* Return a pointer to the next SIZE bytes pointed to by IT, and move
   IT to the end of the returned block.  Return null if the data could
   not be read for some reason.  */

unsigned char *
get_data_from_iterator (struct data_iterator *it, GElf_Addr size)
{
  unsigned char *ptr;

  /* If we're at the end of a data block, move onto the next.  */
  if (it->data && it->data->d_off + it->data->d_size == it->sec_offset)
    it->data = elf_getdata (it->dso->scn[it->sec], it->data);

  if (it->data == NULL)
    {
      /* Find out which section contains the next byte.  */
      it->sec = addr_to_sec (it->dso, it->addr);
      if (it->sec < 0)
	return NULL;

      /* Fast-forward to the block that contains ADDR, if any.  */
      it->sec_offset = it->addr - it->dso->shdr[it->sec].sh_addr;
      do
	it->data = elf_getdata (it->dso->scn[it->sec], it->data);
      while (it->data && it->data->d_off + it->data->d_size <= it->sec_offset);
    }

  /* Make sure that all the data we want is included in this block.  */
  if (it->data == NULL
      || it->data->d_off > it->sec_offset
      || it->data->d_off + it->data->d_size < it->sec_offset + size)
    return NULL;

  ptr = (unsigned char *) it->data->d_buf + (it->sec_offset - it->data->d_off);
  it->sec_offset += size;
  it->addr += size;
  return ptr;
}

/* Read the symbol pointed to by IT into SYM and move IT onto the
   next symbol.  Return true on success.  */

int
get_sym_from_iterator (struct data_iterator *it, GElf_Sym *sym)
{
  GElf_Addr offset, size;
  unsigned char *ptr;

  size = gelf_fsize (it->dso->elf, ELF_T_SYM, 1, EV_CURRENT);
  ptr = get_data_from_iterator (it, size);
  if (ptr != NULL)
    {
      offset = ptr - (unsigned char *) it->data->d_buf;
      if (offset % size == 0)
	{
	  gelfx_getsym (it->dso->elf, it->data, offset / size, sym);
	  return 1;
	}
    }
  return 0;
}

inline uint8_t
buf_read_u8 (unsigned char *data)
{
  return *data;
}

inline uint16_t
buf_read_ule16 (unsigned char *data)
{
  return data[0] | (data[1] << 8);
}

inline uint16_t
buf_read_ube16 (unsigned char *data)
{
  return data[1] | (data[0] << 8);
}

inline uint32_t
buf_read_ule32 (unsigned char *data)
{
  return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
}

inline uint32_t
buf_read_ube32 (unsigned char *data)
{
  return data[3] | (data[2] << 8) | (data[1] << 16) | (data[0] << 24);
}

inline uint64_t
buf_read_ule64 (unsigned char *data)
{
  return (data[0] | (data[1] << 8) | (data[2] << 16))
	 | (((uint64_t)data[3]) << 24)
	 | (((uint64_t)data[4]) << 32)
	 | (((uint64_t)data[5]) << 40)
	 | (((uint64_t)data[6]) << 48)
	 | (((uint64_t)data[7]) << 56);
}

inline uint64_t
buf_read_ube64 (unsigned char *data)
{
  return (data[7] | (data[6] << 8) | (data[5] << 16))
	 | (((uint64_t)data[4]) << 24)
	 | (((uint64_t)data[3]) << 32)
	 | (((uint64_t)data[2]) << 40)
	 | (((uint64_t)data[1]) << 48)
	 | (((uint64_t)data[0]) << 56);
}

inline void
buf_write_8 (unsigned char *data, uint8_t val)
{
  *data = val;
}

inline void
buf_write_le16 (unsigned char *data, uint16_t val)
{
  data[0] = val;
  data[1] = val >> 8;
}

inline void
buf_write_be16 (unsigned char *data, uint16_t val)
{
  data[1] = val;
  data[0] = val >> 8;
}

inline void
buf_write_le32 (unsigned char *data, uint32_t val)
{
  data[0] = val;
  data[1] = val >> 8;
  data[2] = val >> 16;
  data[3] = val >> 24;
}

inline void
buf_write_be32 (unsigned char *data, uint32_t val)
{
  data[3] = val;
  data[2] = val >> 8;
  data[1] = val >> 16;
  data[0] = val >> 24;
}

inline void
buf_write_le64 (unsigned char *data, uint64_t val)
{
  data[0] = val;
  data[1] = val >> 8;
  data[2] = val >> 16;
  data[3] = val >> 24;
  data[4] = val >> 32;
  data[5] = val >> 40;
  data[6] = val >> 48;
  data[7] = val >> 56;
}

inline void
buf_write_be64 (unsigned char *data, uint64_t val)
{
  data[7] = val;
  data[6] = val >> 8;
  data[5] = val >> 16;
  data[4] = val >> 24;
  data[3] = val >> 32;
  data[2] = val >> 40;
  data[1] = val >> 48;
  data[0] = val >> 56;
}

READWRITE(,8)
READWRITESIZE(16)
READWRITESIZE(32)
READWRITESIZE(64)

const char *
strptr (DSO *dso, int sec, off_t offset)
{
  Elf_Scn *scn;
  Elf_Data *data;

  scn = dso->scn[sec];
  if (offset >= 0 && offset < dso->shdr[sec].sh_size)
    {
      data = NULL;
      while ((data = elf_getdata (scn, data)) != NULL)
	{
	  if (data->d_buf
	      && offset >= data->d_off
	      && offset < data->d_off + data->d_size)
	    return (const char *) data->d_buf + (offset - data->d_off);
	}
    }

  return NULL;
}