Subversion Repositories Kolibri OS

Rev

Rev 5199 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5199 Rev 6324
Line 1... Line 1...
1
/* opncls.c -- open and close a BFD.
1
/* opncls.c -- open and close a BFD.
2
   Copyright 1990-2013 Free Software Foundation, Inc.
2
   Copyright (C) 1990-2015 Free Software Foundation, Inc.
Line 3... Line 3...
3
 
3
 
Line 4... Line 4...
4
   Written by Cygnus Support.
4
   Written by Cygnus Support.
Line 78... Line 78...
78
      return NULL;
78
      return NULL;
79
    }
79
    }
Line 80... Line 80...
80
 
80
 
Line 81... Line -...
81
  nbfd->arch_info = &bfd_default_arch_struct;
-
 
82
 
-
 
83
  nbfd->direction = no_direction;
-
 
84
  nbfd->iostream = NULL;
81
  nbfd->arch_info = &bfd_default_arch_struct;
85
  nbfd->where = 0;
82
 
86
  if (!bfd_hash_table_init_n (& nbfd->section_htab, bfd_section_hash_newfunc,
83
  if (!bfd_hash_table_init_n (& nbfd->section_htab, bfd_section_hash_newfunc,
87
			      sizeof (struct section_hash_entry), 13))
84
			      sizeof (struct section_hash_entry), 13))
88
    {
85
    {
89
      free (nbfd);
86
      free (nbfd);
90
      return NULL;
-
 
91
    }
-
 
92
  nbfd->sections = NULL;
-
 
93
  nbfd->section_last = NULL;
-
 
94
  nbfd->format = bfd_unknown;
-
 
95
  nbfd->my_archive = NULL;
-
 
96
  nbfd->origin = 0;
-
 
97
  nbfd->opened_once = FALSE;
-
 
98
  nbfd->output_has_begun = FALSE;
-
 
99
  nbfd->section_count = 0;
-
 
100
  nbfd->usrdata = NULL;
-
 
101
  nbfd->cacheable = FALSE;
-
 
Line 102... Line 87...
102
  nbfd->flags = BFD_NO_FLAGS;
87
      return NULL;
103
  nbfd->mtime_set = FALSE;
88
    }
Line 104... Line 89...
104
 
89
 
Line 122... Line 107...
122
  if (obfd->iovec == &opncls_iovec)
107
  if (obfd->iovec == &opncls_iovec)
123
    nbfd->iostream = obfd->iostream;
108
    nbfd->iostream = obfd->iostream;
124
  nbfd->my_archive = obfd;
109
  nbfd->my_archive = obfd;
125
  nbfd->direction = read_direction;
110
  nbfd->direction = read_direction;
126
  nbfd->target_defaulted = obfd->target_defaulted;
111
  nbfd->target_defaulted = obfd->target_defaulted;
-
 
112
  nbfd->lto_output = obfd->lto_output;
-
 
113
  nbfd->no_export = obfd->no_export;
127
  return nbfd;
114
  return nbfd;
128
}
115
}
Line 129... Line 116...
129
 
116
 
Line 136... Line 123...
136
    {
123
    {
137
      bfd_hash_table_free (&abfd->section_htab);
124
      bfd_hash_table_free (&abfd->section_htab);
138
      objalloc_free ((struct objalloc *) abfd->memory);
125
      objalloc_free ((struct objalloc *) abfd->memory);
139
    }
126
    }
Line -... Line 127...
-
 
127
 
-
 
128
  if (abfd->filename)
140
 
129
    free ((char *) abfd->filename);
141
  free (abfd->arelt_data);
130
  free (abfd->arelt_data);
142
  free (abfd);
131
  free (abfd);
Line 143... Line 132...
143
}
132
}
Line 194... Line 183...
194
	If <> is returned then an error has occured.   Possible errors
183
	If <> is returned then an error has occured.   Possible errors
195
	are <>, <> or
184
	are <>, <> or
196
	<> error.
185
	<> error.
Line 197... Line 186...
197
 
186
 
-
 
187
	On error, @var{fd} is always closed.
-
 
188
 
-
 
189
	A copy of the @var{filename} argument is stored in the newly created
198
	On error, @var{fd} is always closed.
190
	BFD.  It can be accessed via the bfd_get_filename() macro.
Line 199... Line 191...
199
*/
191
*/
200
 
192
 
201
bfd *
193
bfd *
Line 233... Line 225...
233
      _bfd_delete_bfd (nbfd);
225
      _bfd_delete_bfd (nbfd);
234
      return NULL;
226
      return NULL;
235
    }
227
    }
Line 236... Line 228...
236
 
228
 
-
 
229
  /* OK, put everything where it belongs.  */
-
 
230
 
-
 
231
  /* PR 11983: Do not cache the original filename, but
237
  /* OK, put everything where it belongs.  */
232
     rather make a copy - the original might go away.  */
Line 238... Line 233...
238
  nbfd->filename = filename;
233
  nbfd->filename = xstrdup (filename);
239
 
234
 
240
  /* Figure out whether the user is opening the file for reading,
235
  /* Figure out whether the user is opening the file for reading,
241
     writing, or both, by looking at the MODE argument.  */
236
     writing, or both, by looking at the MODE argument.  */
Line 279... Line 274...
279
	that function.
274
	that function.
Line 280... Line 275...
280
 
275
 
281
	If <> is returned then an error has occured.   Possible errors
276
	If <> is returned then an error has occured.   Possible errors
282
	are <>, <> or
277
	are <>, <> or
-
 
278
	<> error.
-
 
279
 
-
 
280
	A copy of the @var{filename} argument is stored in the newly created
283
	<> error.
281
	BFD.  It can be accessed via the bfd_get_filename() macro.
Line 284... Line 282...
284
*/
282
*/
285
 
283
 
286
bfd *
284
bfd *
Line 320... Line 318...
320
 
318
 
321
	Possible errors are <>,
319
	Possible errors are <>,
Line 322... Line 320...
322
	<> and <>.
320
	<> and <>.
-
 
321
 
-
 
322
	On error, @var{fd} is closed.
-
 
323
 
323
 
324
	A copy of the @var{filename} argument is stored in the newly created
Line 324... Line 325...
324
	On error, @var{fd} is closed.
325
	BFD.  It can be accessed via the bfd_get_filename() macro.
325
*/
326
*/
326
 
327
 
Line 362... Line 363...
362
/*
363
/*
363
FUNCTION
364
FUNCTION
364
	bfd_openstreamr
365
	bfd_openstreamr
Line 365... Line 366...
365
 
366
 
366
SYNOPSIS
367
SYNOPSIS
Line 367... Line 368...
367
	bfd *bfd_openstreamr (const char *, const char *, void *);
368
	bfd *bfd_openstreamr (const char * filename, const char * target, void * stream);
Line 368... Line 369...
368
 
369
 
369
DESCRIPTION
370
DESCRIPTION
-
 
371
 
-
 
372
	Open a BFD for read access on an existing stdio stream.  When
-
 
373
	the BFD is passed to <>, the stream will be closed.
370
 
374
 
Line 371... Line 375...
371
	Open a BFD for read access on an existing stdio stream.  When
375
	A copy of the @var{filename} argument is stored in the newly created
372
	the BFD is passed to <>, the stream will be closed.
376
	BFD.  It can be accessed via the bfd_get_filename() macro.
373
*/
377
*/
Line 389... Line 393...
389
      _bfd_delete_bfd (nbfd);
393
      _bfd_delete_bfd (nbfd);
390
      return NULL;
394
      return NULL;
391
    }
395
    }
Line 392... Line 396...
392
 
396
 
-
 
397
  nbfd->iostream = stream;
-
 
398
  /* PR 11983: Do not cache the original filename, but
393
  nbfd->iostream = stream;
399
     rather make a copy - the original might go away.  */
394
  nbfd->filename = filename;
400
  nbfd->filename = xstrdup (filename);
Line 395... Line 401...
395
  nbfd->direction = read_direction;
401
  nbfd->direction = read_direction;
396
 
402
 
397
  if (! bfd_cache_init (nbfd))
403
  if (! bfd_cache_init (nbfd))
Line 454... Line 460...
454
 
460
 
455
	If <> returns <> then an error has
461
	If <> returns <> then an error has
456
	occurred.  Possible errors are <>,
462
	occurred.  Possible errors are <>,
Line -... Line 463...
-
 
463
	<> and <>.
-
 
464
 
457
	<> and <>.
465
	A copy of the @var{filename} argument is stored in the newly created
Line 458... Line 466...
458
 
466
	BFD.  It can be accessed via the bfd_get_filename() macro.
459
*/
467
*/
460
 
468
 
Line 579... Line 587...
579
    {
587
    {
580
      _bfd_delete_bfd (nbfd);
588
      _bfd_delete_bfd (nbfd);
581
      return NULL;
589
      return NULL;
582
    }
590
    }
Line -... Line 591...
-
 
591
 
-
 
592
  /* PR 11983: Do not cache the original filename, but
583
 
593
     rather make a copy - the original might go away.  */
584
  nbfd->filename = filename;
594
  nbfd->filename = xstrdup (filename);
Line 585... Line 595...
585
  nbfd->direction = read_direction;
595
  nbfd->direction = read_direction;
586
 
596
 
587
  /* `open_p (...)' would get expanded by an the open(2) syscall macro.  */
597
  /* `open_p (...)' would get expanded by an the open(2) syscall macro.  */
Line 620... Line 630...
620
	Create a BFD, associated with file @var{filename}, using the
630
	Create a BFD, associated with file @var{filename}, using the
621
	file format @var{target}, and return a pointer to it.
631
	file format @var{target}, and return a pointer to it.
Line 622... Line 632...
622
 
632
 
623
	Possible errors are <>, <>,
633
	Possible errors are <>, <>,
-
 
634
	<>.
-
 
635
 
-
 
636
	A copy of the @var{filename} argument is stored in the newly created
624
	<>.
637
	BFD.  It can be accessed via the bfd_get_filename() macro.
Line 625... Line 638...
625
*/
638
*/
626
 
639
 
627
bfd *
640
bfd *
Line 641... Line 654...
641
    {
654
    {
642
      _bfd_delete_bfd (nbfd);
655
      _bfd_delete_bfd (nbfd);
643
      return NULL;
656
      return NULL;
644
    }
657
    }
Line -... Line 658...
-
 
658
 
-
 
659
  /* PR 11983: Do not cache the original filename, but
645
 
660
     rather make a copy - the original might go away.  */
646
  nbfd->filename = filename;
661
  nbfd->filename = xstrdup (filename);
Line 647... Line 662...
647
  nbfd->direction = write_direction;
662
  nbfd->direction = write_direction;
648
 
663
 
649
  if (bfd_open_file (nbfd) == NULL)
664
  if (bfd_open_file (nbfd) == NULL)
Line 780... Line 795...
780
 
795
 
781
DESCRIPTION
796
DESCRIPTION
782
	Create a new BFD in the manner of <>, but without
797
	Create a new BFD in the manner of <>, but without
783
	opening a file. The new BFD takes the target from the target
798
	opening a file. The new BFD takes the target from the target
-
 
799
	used by @var{templ}. The format is always set to <>.
-
 
800
 
-
 
801
	A copy of the @var{filename} argument is stored in the newly created
784
	used by @var{templ}. The format is always set to <>.
802
	BFD.  It can be accessed via the bfd_get_filename() macro.
Line 785... Line 803...
785
*/
803
*/
786
 
804
 
787
bfd *
805
bfd *
788
bfd_create (const char *filename, bfd *templ)
806
bfd_create (const char *filename, bfd *templ)
Line 789... Line 807...
789
{
807
{
790
  bfd *nbfd;
808
  bfd *nbfd;
791
 
809
 
-
 
810
  nbfd = _bfd_new_bfd ();
-
 
811
  if (nbfd == NULL)
792
  nbfd = _bfd_new_bfd ();
812
    return NULL;
793
  if (nbfd == NULL)
813
  /* PR 11983: Do not cache the original filename, but
794
    return NULL;
814
     rather make a copy - the original might go away.  */
795
  nbfd->filename = filename;
815
  nbfd->filename = xstrdup (filename);
796
  if (templ)
816
  if (templ)
Line 919... Line 939...
919
 
939
 
920
void *
940
void *
921
bfd_alloc (bfd *abfd, bfd_size_type size)
941
bfd_alloc (bfd *abfd, bfd_size_type size)
922
{
942
{
-
 
943
  void *ret;
Line 923... Line 944...
923
  void *ret;
944
  unsigned long ul_size = (unsigned long) size;
-
 
945
 
-
 
946
  if (size != ul_size
-
 
947
      /* Note - although objalloc_alloc takes an unsigned long as its
-
 
948
	 argument, internally the size is treated as a signed long.  This can
-
 
949
	 lead to problems where, for example, a request to allocate -1 bytes
-
 
950
	 can result in just 1 byte being allocated, rather than
-
 
951
	 ((unsigned long) -1) bytes.  Also memory checkers will often
-
 
952
	 complain about attempts to allocate a negative amount of memory.
924
 
953
	 So to stop these problems we fail if the size is negative.  */
925
  if (size != (unsigned long) size)
954
      || ((signed long) ul_size) < 0)
926
    {
955
    {
927
      bfd_set_error (bfd_error_no_memory);
956
      bfd_set_error (bfd_error_no_memory);
Line 928... Line 957...
928
      return NULL;
957
      return NULL;
929
    }
958
    }
930
 
959
 
931
  ret = objalloc_alloc ((struct objalloc *) abfd->memory, (unsigned long) size);
960
  ret = objalloc_alloc ((struct objalloc *) abfd->memory, ul_size);
932
  if (ret == NULL)
961
  if (ret == NULL)
Line 947... Line 976...
947
*/
976
*/
Line 948... Line 977...
948
 
977
 
949
void *
978
void *
950
bfd_alloc2 (bfd *abfd, bfd_size_type nmemb, bfd_size_type size)
979
bfd_alloc2 (bfd *abfd, bfd_size_type nmemb, bfd_size_type size)
951
{
-
 
952
  void *ret;
-
 
953
 
980
{
954
  if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
981
  if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
955
      && size != 0
982
      && size != 0
956
      && nmemb > ~(bfd_size_type) 0 / size)
983
      && nmemb > ~(bfd_size_type) 0 / size)
957
    {
984
    {
958
      bfd_set_error (bfd_error_no_memory);
985
      bfd_set_error (bfd_error_no_memory);
959
      return NULL;
986
      return NULL;
Line 960... Line -...
960
    }
-
 
961
 
-
 
962
  size *= nmemb;
-
 
963
 
-
 
964
  if (size != (unsigned long) size)
987
    }
965
    {
-
 
966
      bfd_set_error (bfd_error_no_memory);
-
 
967
      return NULL;
-
 
968
    }
-
 
969
 
-
 
970
  ret = objalloc_alloc ((struct objalloc *) abfd->memory, (unsigned long) size);
-
 
971
  if (ret == NULL)
-
 
972
    bfd_set_error (bfd_error_no_memory);
988
 
Line 973... Line 989...
973
  return ret;
989
  return bfd_alloc (abfd, size * nmemb);
974
}
990
}
975
 
991
 
Line 1147... Line 1163...
1147
 
1163
 
1148
SYNOPSIS
1164
SYNOPSIS
Line 1149... Line 1165...
1149
	char *bfd_get_debug_link_info (bfd *abfd, unsigned long *crc32_out);
1165
	char *bfd_get_debug_link_info (bfd *abfd, unsigned long *crc32_out);
1150
 
1166
 
1151
DESCRIPTION
1167
DESCRIPTION
1152
	fetch the filename and CRC32 value for any separate debuginfo
1168
	Fetch the filename and CRC32 value for any separate debuginfo
1153
	associated with @var{abfd}. Return NULL if no such info found,
1169
	associated with @var{abfd}. Return NULL if no such info found,
1154
	otherwise return filename and update @var{crc32_out}.  The
1170
	otherwise return filename and update @var{crc32_out}.  The
1155
	returned filename is allocated with @code{malloc}; freeing it
1171
	returned filename is allocated with @code{malloc}; freeing it
Line 1160... Line 1176...
1160
bfd_get_debug_link_info (bfd *abfd, unsigned long *crc32_out)
1176
bfd_get_debug_link_info (bfd *abfd, unsigned long *crc32_out)
1161
{
1177
{
1162
  asection *sect;
1178
  asection *sect;
1163
  unsigned long crc32;
1179
  unsigned long crc32;
1164
  bfd_byte *contents;
1180
  bfd_byte *contents;
1165
  int crc_offset;
1181
  unsigned int crc_offset;
1166
  char *name;
1182
  char *name;
Line 1167... Line 1183...
1167
 
1183
 
1168
  BFD_ASSERT (abfd);
1184
  BFD_ASSERT (abfd);
Line 1178... Line 1194...
1178
      if (contents != NULL)
1194
      if (contents != NULL)
1179
	free (contents);
1195
	free (contents);
1180
      return NULL;
1196
      return NULL;
1181
    }
1197
    }
Line 1182... Line 1198...
1182
 
1198
 
1183
  /* Crc value is stored after the filename, aligned up to 4 bytes.  */
1199
  /* CRC value is stored after the filename, aligned up to 4 bytes.  */
-
 
1200
  name = (char *) contents;
1184
  name = (char *) contents;
1201
  /* PR 17597: avoid reading off the end of the buffer.  */
1185
  crc_offset = strlen (name) + 1;
1202
  crc_offset = strnlen (name, bfd_get_section_size (sect)) + 1;
-
 
1203
  crc_offset = (crc_offset + 3) & ~3;
-
 
1204
  if (crc_offset >= bfd_get_section_size (sect))
Line 1186... Line 1205...
1186
  crc_offset = (crc_offset + 3) & ~3;
1205
    return NULL;
Line 1187... Line 1206...
1187
 
1206
 
1188
  crc32 = bfd_get_32 (abfd, contents + crc_offset);
1207
  crc32 = bfd_get_32 (abfd, contents + crc_offset);
Line 1194... Line 1213...
1194
/*
1213
/*
1195
FUNCTION
1214
FUNCTION
1196
	bfd_get_alt_debug_link_info
1215
	bfd_get_alt_debug_link_info
Line 1197... Line 1216...
1197
 
1216
 
1198
SYNOPSIS
1217
SYNOPSIS
-
 
1218
	char *bfd_get_alt_debug_link_info (bfd * abfd,
-
 
1219
					   bfd_size_type *buildid_len,
Line 1199... Line 1220...
1199
	char *bfd_get_alt_debug_link_info (bfd *abfd, unsigned long *crc32_out);
1220
			                   bfd_byte **buildid_out);
1200
 
1221
 
1201
DESCRIPTION
1222
DESCRIPTION
1202
	Fetch the filename and BuildID value for any alternate debuginfo
1223
	Fetch the filename and BuildID value for any alternate debuginfo
-
 
1224
	associated with @var{abfd}.  Return NULL if no such info found,
1203
	associated with @var{abfd}.  Return NULL if no such info found,
1225
	otherwise return filename and update @var{buildid_len} and
1204
	otherwise return filename and update @var{buildid_out}.  The
1226
	@var{buildid_out}.  The returned filename and build_id are
1205
	returned filename is allocated with @code{malloc}; freeing it
1227
	allocated with @code{malloc}; freeing them is the
Line 1206... Line 1228...
1206
	is the responsibility of the caller.
1228
	responsibility of the caller.
1207
*/
1229
*/
-
 
1230
 
1208
 
1231
char *
1209
char *
1232
bfd_get_alt_debug_link_info (bfd * abfd, bfd_size_type *buildid_len,
1210
bfd_get_alt_debug_link_info (bfd * abfd, unsigned long * buildid_out)
1233
			     bfd_byte **buildid_out)
1211
{
1234
{
1212
  asection *sect;
1235
  asection *sect;
Line 1213... Line 1236...
1213
  bfd_byte *contents;
1236
  bfd_byte *contents;
-
 
1237
  unsigned int buildid_offset;
1214
  int buildid_offset;
1238
  char *name;
Line 1215... Line 1239...
1215
  char *name;
1239
 
Line 1216... Line 1240...
1216
 
1240
  BFD_ASSERT (abfd);
Line 1227... Line 1251...
1227
      if (contents != NULL)
1251
      if (contents != NULL)
1228
	free (contents);
1252
	free (contents);
1229
      return NULL;
1253
      return NULL;
1230
    }
1254
    }
Line 1231... Line 1255...
1231
 
1255
 
1232
  /* BuildID value is stored after the filename, aligned up to 4 bytes.  */
1256
  /* BuildID value is stored after the filename.  */
1233
  name = (char *) contents;
1257
  name = (char *) contents;
1234
  buildid_offset = strlen (name) + 1;
1258
  buildid_offset = strnlen (name, bfd_get_section_size (sect)) + 1;
-
 
1259
  if (buildid_offset >= bfd_get_section_size (sect))
Line 1235... Line 1260...
1235
  buildid_offset = (buildid_offset + 3) & ~3;
1260
    return NULL;
-
 
1261
 
-
 
1262
  *buildid_len = bfd_get_section_size (sect) - buildid_offset;
Line 1236... Line 1263...
1236
 
1263
  *buildid_out = bfd_malloc (*buildid_len);
1237
  * buildid_out = bfd_get_32 (abfd, contents + buildid_offset);
1264
  memcpy (*buildid_out, contents + buildid_offset, *buildid_len);
Line 1238... Line 1265...
1238
 
1265
 
Line 1466... Line 1493...
1466
  return find_separate_debug_file (abfd, dir,
1493
  return find_separate_debug_file (abfd, dir,
1467
				   bfd_get_debug_link_info,
1494
				   bfd_get_debug_link_info,
1468
				   separate_debug_file_exists);
1495
				   separate_debug_file_exists);
1469
}
1496
}
Line -... Line 1497...
-
 
1497
 
-
 
1498
/* Helper for bfd_follow_gnu_debugaltlink.  It just pretends to return
-
 
1499
   a CRC.  .gnu_debugaltlink supplies a build-id, which is different,
-
 
1500
   but this is ok because separate_alt_debug_file_exists ignores the
-
 
1501
   CRC anyway.  */
-
 
1502
 
-
 
1503
static char *
-
 
1504
get_alt_debug_link_info_shim (bfd * abfd, unsigned long *crc32_out)
-
 
1505
{
-
 
1506
  bfd_size_type len;
-
 
1507
  bfd_byte *buildid = NULL;
-
 
1508
  char *result = bfd_get_alt_debug_link_info (abfd, &len, &buildid);
-
 
1509
 
-
 
1510
  *crc32_out = 0;
-
 
1511
  free (buildid);
-
 
1512
 
-
 
1513
  return result;
-
 
1514
}
1470
 
1515
 
1471
/*
1516
/*
1472
FUNCTION
1517
FUNCTION
Line 1473... Line 1518...
1473
	bfd_follow_gnu_debugaltlink
1518
	bfd_follow_gnu_debugaltlink
Line 1496... Line 1541...
1496
 
1541
 
1497
char *
1542
char *
1498
bfd_follow_gnu_debugaltlink (bfd *abfd, const char *dir)
1543
bfd_follow_gnu_debugaltlink (bfd *abfd, const char *dir)
1499
{
1544
{
1500
  return find_separate_debug_file (abfd, dir,
1545
  return find_separate_debug_file (abfd, dir,
1501
				   bfd_get_alt_debug_link_info,
1546
				   get_alt_debug_link_info_shim,
1502
				   separate_alt_debug_file_exists);
1547
				   separate_alt_debug_file_exists);
Line 1503... Line 1548...
1503
}
1548
}
1504
 
1549