Subversion Repositories Kolibri OS

Rev

Rev 5197 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5197 Rev 6324
Line 1... Line 1...
1
/* Assorted BFD support routines, only used internally.
1
/* Assorted BFD support routines, only used internally.
2
   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-
 
3
   2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
-
 
4
   Free Software Foundation, Inc.
2
   Copyright (C) 1990-2015 Free Software Foundation, Inc.
5
   Written by Cygnus Support.
3
   Written by Cygnus Support.
Line 6... Line 4...
6
 
4
 
Line 7... Line 5...
7
   This file is part of BFD, the Binary File Descriptor library.
5
   This file is part of BFD, the Binary File Descriptor library.
Line 171... Line 169...
171
 
169
 
172
void *
170
void *
173
bfd_malloc (bfd_size_type size)
171
bfd_malloc (bfd_size_type size)
174
{
172
{
-
 
173
  void *ptr;
Line 175... Line 174...
175
  void *ptr;
174
  size_t sz = (size_t) size;
-
 
175
 
-
 
176
  if (size != sz
176
 
177
      /* This is to pacify memory checkers like valgrind.  */
177
  if (size != (size_t) size)
178
      || ((signed long) sz) < 0)
178
    {
179
    {
179
      bfd_set_error (bfd_error_no_memory);
180
      bfd_set_error (bfd_error_no_memory);
Line 180... Line 181...
180
      return NULL;
181
      return NULL;
181
    }
182
    }
182
 
183
 
Line 183... Line 184...
183
  ptr = malloc ((size_t) size);
184
  ptr = malloc (sz);
184
  if (ptr == NULL && (size_t) size != 0)
185
  if (ptr == NULL && sz != 0)
Line 185... Line 186...
185
    bfd_set_error (bfd_error_no_memory);
186
    bfd_set_error (bfd_error_no_memory);
Line 186... Line 187...
186
 
187
 
187
  return ptr;
188
  return ptr;
188
}
189
}
189
 
-
 
190
/* Allocate memory using malloc, nmemb * size with overflow checking.  */
-
 
191
 
190
 
192
void *
191
/* Allocate memory using malloc, nmemb * size with overflow checking.  */
193
bfd_malloc2 (bfd_size_type nmemb, bfd_size_type size)
192
 
194
{
193
void *
195
  void *ptr;
194
bfd_malloc2 (bfd_size_type nmemb, bfd_size_type size)
196
 
195
{
197
  if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
196
  if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
Line 198... Line -...
198
      && size != 0
-
 
199
      && nmemb > ~(bfd_size_type) 0 / size)
-
 
200
    {
-
 
201
      bfd_set_error (bfd_error_no_memory);
-
 
202
      return NULL;
-
 
203
    }
-
 
204
 
-
 
205
  size *= nmemb;
-
 
206
 
197
      && size != 0
207
  if (size != (size_t) size)
-
 
208
    {
-
 
209
      bfd_set_error (bfd_error_no_memory);
-
 
210
      return NULL;
-
 
211
    }
198
      && nmemb > ~(bfd_size_type) 0 / size)
Line 212... Line 199...
212
 
199
    {
Line 213... Line 200...
213
  ptr = malloc ((size_t) size);
200
      bfd_set_error (bfd_error_no_memory);
214
  if (ptr == NULL && (size_t) size != 0)
201
      return NULL;
215
    bfd_set_error (bfd_error_no_memory);
202
    }
216
 
203
 
-
 
204
  return bfd_malloc (size * nmemb);
-
 
205
}
-
 
206
 
-
 
207
/* Reallocate memory using realloc.  */
Line 217... Line 208...
217
  return ptr;
208
 
-
 
209
void *
-
 
210
bfd_realloc (void *ptr, bfd_size_type size)
218
}
211
{
219
 
212
  void *ret;
220
/* Reallocate memory using realloc.  */
213
  size_t sz = (size_t) size;
221
 
214
 
Line 222... Line -...
222
void *
-
 
223
bfd_realloc (void *ptr, bfd_size_type size)
-
 
224
{
-
 
225
  void *ret;
215
  if (ptr == NULL)
Line 226... Line 216...
226
 
216
    return bfd_malloc (size);
227
  if (size != (size_t) size)
217
 
Line 228... Line 218...
228
    {
218
  if (size != sz
229
      bfd_set_error (bfd_error_no_memory);
219
      /* This is to pacify memory checkers like valgrind.  */
Line 230... Line 220...
230
      return NULL;
220
      || ((signed long) sz) < 0)
Line 231... Line 221...
231
    }
221
    {
232
 
222
      bfd_set_error (bfd_error_no_memory);
233
  if (ptr == NULL)
223
      return NULL;
234
    ret = malloc ((size_t) size);
-
 
235
  else
-
 
236
    ret = realloc (ptr, (size_t) size);
224
    }
237
 
225
 
238
  if (ret == NULL && (size_t) size != 0)
226
  ret = realloc (ptr, sz);
239
    bfd_set_error (bfd_error_no_memory);
227
 
240
 
228
  if (ret == NULL && sz != 0)
241
  return ret;
229
    bfd_set_error (bfd_error_no_memory);
242
}
230
 
Line 243... Line -...
243
 
-
 
244
/* Reallocate memory using realloc, nmemb * size with overflow checking.  */
-
 
245
 
-
 
246
void *
-
 
247
bfd_realloc2 (void *ptr, bfd_size_type nmemb, bfd_size_type size)
-
 
248
{
-
 
249
  void *ret;
-
 
250
 
-
 
251
  if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
-
 
252
      && size != 0
-
 
253
      && nmemb > ~(bfd_size_type) 0 / size)
-
 
254
    {
231
  return ret;
255
      bfd_set_error (bfd_error_no_memory);
-
 
256
      return NULL;
-
 
257
    }
-
 
258
 
-
 
259
  size *= nmemb;
-
 
260
 
232
}
Line 261... Line 233...
261
  if (size != (size_t) size)
233
 
262
    {
234
/* Reallocate memory using realloc, nmemb * size with overflow checking.  */
Line 263... Line 235...
263
      bfd_set_error (bfd_error_no_memory);
235
 
264
      return NULL;
236
void *
265
    }
237
bfd_realloc2 (void *ptr, bfd_size_type nmemb, bfd_size_type size)
266
 
-
 
267
  if (ptr == NULL)
-
 
268
    ret = malloc ((size_t) size);
-
 
269
  else
-
 
270
    ret = realloc (ptr, (size_t) size);
-
 
271
 
-
 
272
  if (ret == NULL && (size_t) size != 0)
-
 
273
    bfd_set_error (bfd_error_no_memory);
-
 
274
 
238
{
275
  return ret;
-
 
276
}
-
 
277
 
-
 
278
/* Reallocate memory using realloc.
-
 
279
   If this fails the pointer is freed before returning.  */
-
 
Line 280... Line 239...
280
 
239
  if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
281
void *
240
      && size != 0
282
bfd_realloc_or_free (void *ptr, bfd_size_type size)
-
 
Line 283... Line 241...
283
{
241
      && nmemb > ~(bfd_size_type) 0 / size)
284
  size_t amount = (size_t) size;
242
    {
Line 285... Line 243...
285
  void *ret;
243
      bfd_set_error (bfd_error_no_memory);
Line 286... Line 244...
286
 
244
      return NULL;
287
  if (size != amount)
245
    }
288
    ret = NULL;
246
 
289
  else if (ptr == NULL)
247
  return bfd_realloc (ptr, size * nmemb);
Line 290... Line 248...
290
    ret = malloc (amount);
248
}
291
  else
-
 
292
    ret = realloc (ptr, amount);
-
 
293
 
-
 
294
  if (ret == NULL)
-
 
295
    {
-
 
296
      if (amount > 0)
-
 
297
	bfd_set_error (bfd_error_no_memory);
-
 
298
 
-
 
299
      if (ptr != NULL)
-
 
300
	free (ptr);
-
 
301
    }
-
 
302
 
-
 
303
  return ret;
249
 
304
}
-
 
Line 305... Line 250...
305
 
250
/* Reallocate memory using realloc.
306
/* Allocate memory using malloc and clear it.  */
251
   If this fails the pointer is freed before returning.  */
Line 307... Line 252...
307
 
252
 
308
void *
253
void *
Line 309... Line 254...
309
bfd_zmalloc (bfd_size_type size)
254
bfd_realloc_or_free (void *ptr, bfd_size_type size)
310
{
255
{
311
  void *ptr;
256
  void *ret = bfd_realloc (ptr, size);
312
 
-
 
313
  if (size != (size_t) size)
-
 
314
    {
-
 
315
      bfd_set_error (bfd_error_no_memory);
-
 
316
      return NULL;
257
 
317
    }
-
 
318
 
-
 
319
  ptr = malloc ((size_t) size);
-
 
320
 
-
 
321
  if ((size_t) size != 0)
-
 
322
    {
-
 
Line 323... Line 258...
323
      if (ptr == NULL)
258
  if (ret == NULL && ptr != NULL)
324
	bfd_set_error (bfd_error_no_memory);
259
	free (ptr);
325
      else
-
 
326
	memset (ptr, 0, (size_t) size);
-
 
327
    }
-
 
328
 
-
 
329
  return ptr;
260
 
Line 330... Line -...
330
}
-
 
331
 
-
 
332
/* Allocate memory using malloc (nmemb * size) with overflow checking
261
  return ret;
333
   and clear it.  */
-
 
334
 
-
 
335
void *
262
}
336
bfd_zmalloc2 (bfd_size_type nmemb, bfd_size_type size)
263
 
Line 337... Line 264...
337
{
264
/* Allocate memory using malloc and clear it.  */
338
  void *ptr;
265
 
Line 548... Line 475...
548
.#define H_GET_S8  bfd_h_get_signed_8
475
.#define H_GET_S8  bfd_h_get_signed_8
549
.
476
.
550
.*/
477
.*/
Line 551... Line 478...
551
 
478
 
552
/* Sign extension to bfd_signed_vma.  */
479
/* Sign extension to bfd_signed_vma.  */
553
#define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
480
#define COERCE16(x) (((bfd_vma) (x) ^ 0x8000) - 0x8000)
554
#define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
-
 
555
#define EIGHT_GAZILLION ((bfd_int64_t) 1 << 63)
481
#define COERCE32(x) (((bfd_vma) (x) ^ 0x80000000) - 0x80000000)
556
#define COERCE64(x) \
482
#define COERCE64(x) \
Line 557... Line 483...
557
  (((bfd_int64_t) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
483
  (((bfd_uint64_t) (x) ^ ((bfd_uint64_t) 1 << 63)) - ((bfd_uint64_t) 1 << 63))
558
 
484
 
559
bfd_vma
485
bfd_vma
560
bfd_getb16 (const void *p)
486
bfd_getb16 (const void *p)
Line 1075... Line 1001...
1075
  while (byte & 0x80);
1001
  while (byte & 0x80);
1076
  *bytes_read_ptr = num_read;
1002
  *bytes_read_ptr = num_read;
1077
  return result;
1003
  return result;
1078
}
1004
}
Line -... Line 1005...
-
 
1005
 
-
 
1006
/* Read in a LEB128 encoded value from ABFD starting at DATA.
-
 
1007
   If SIGN is true, return a signed LEB128 value.
-
 
1008
   If LENGTH_RETURN is not NULL, return in it the number of bytes read.
-
 
1009
   No bytes will be read at address END or beyond.  */
-
 
1010
 
-
 
1011
bfd_vma
-
 
1012
safe_read_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
-
 
1013
		  bfd_byte *data,
-
 
1014
		  unsigned int *length_return,
-
 
1015
		  bfd_boolean sign,
-
 
1016
		  const bfd_byte * const end)
-
 
1017
{
-
 
1018
  bfd_vma result = 0;
-
 
1019
  unsigned int num_read = 0;
-
 
1020
  unsigned int shift = 0;
-
 
1021
  unsigned char byte = 0;
-
 
1022
 
-
 
1023
  while (data < end)
-
 
1024
    {
-
 
1025
      byte = bfd_get_8 (abfd, data);
-
 
1026
      data++;
-
 
1027
      num_read++;
-
 
1028
 
-
 
1029
      result |= ((bfd_vma) (byte & 0x7f)) << shift;
-
 
1030
 
-
 
1031
      shift += 7;
-
 
1032
      if ((byte & 0x80) == 0)
-
 
1033
	break;
-
 
1034
    }
-
 
1035
 
-
 
1036
  if (length_return != NULL)
-
 
1037
    *length_return = num_read;
-
 
1038
 
-
 
1039
  if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
-
 
1040
    result |= -((bfd_vma) 1 << shift);
-
 
1041
 
-
 
1042
  return result;
-
 
1043
}
1079
 
1044
 
Line 1080... Line 1045...
1080
/* Helper function for reading sleb128 encoded data.  */
1045
/* Helper function for reading sleb128 encoded data.  */
1081
 
1046
 
1082
bfd_signed_vma
1047
bfd_signed_vma
Line 1106... Line 1071...
1106
  *bytes_read_ptr = num_read;
1071
  *bytes_read_ptr = num_read;
1107
  return result;
1072
  return result;
1108
}
1073
}
Line 1109... Line 1074...
1109
 
1074
 
1110
bfd_boolean
-
 
1111
_bfd_generic_find_line (bfd *abfd ATTRIBUTE_UNUSED,
-
 
1112
		       asymbol **symbols ATTRIBUTE_UNUSED,
-
 
1113
		       asymbol *symbol ATTRIBUTE_UNUSED,
-
 
1114
		       const char **filename_ptr ATTRIBUTE_UNUSED,
-
 
1115
		       unsigned int *linenumber_ptr ATTRIBUTE_UNUSED)
-
 
1116
{
-
 
1117
  return FALSE;
-
 
1118
}
-
 
1119
 
-
 
1120
bfd_boolean
-
 
1121
_bfd_generic_find_nearest_line_discriminator (bfd *abfd ATTRIBUTE_UNUSED,
-
 
1122
                                              asection *section ATTRIBUTE_UNUSED,
-
 
1123
                                              asymbol **symbols ATTRIBUTE_UNUSED,
-
 
1124
                                              bfd_vma offset ATTRIBUTE_UNUSED,
-
 
1125
                                              const char **filename_ptr ATTRIBUTE_UNUSED,
-
 
1126
                                              const char **functionname_ptr ATTRIBUTE_UNUSED,
-
 
1127
                                              unsigned int *line_ptr ATTRIBUTE_UNUSED,
-
 
1128
                                              unsigned int *discriminator_ptr ATTRIBUTE_UNUSED)
-
 
1129
{
-
 
1130
  return FALSE;
-
 
1131
}
-
 
1132
 
-
 
1133
bfd_boolean
1075
bfd_boolean
1134
_bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
1076
_bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
1135
					asection *isec ATTRIBUTE_UNUSED,
1077
					asection *isec ATTRIBUTE_UNUSED,
1136
					bfd *obfd ATTRIBUTE_UNUSED,
1078
					bfd *obfd ATTRIBUTE_UNUSED,
1137
					asection *osec ATTRIBUTE_UNUSED,
1079
					asection *osec ATTRIBUTE_UNUSED,