Coverage Report

Created: 2026-11-21 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/opncls.c
Line
Count
Source
1
/* opncls.c -- open and close a BFD.
2
   Copyright (C) 1990-2026 Free Software Foundation, Inc.
3
4
   Written by Cygnus Support.
5
6
   This file is part of BFD, the Binary File Descriptor library.
7
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
12
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
18
   You should have received a copy of the GNU General Public License
19
   along with this program; if not, write to the Free Software
20
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
   MA 02110-1301, USA.  */
22
23
#include "sysdep.h"
24
#include "bfd.h"
25
#include "objalloc.h"
26
#include "libbfd.h"
27
#include "libiberty.h"
28
#include "elf-bfd.h"
29
30
#ifndef S_IXUSR
31
#define S_IXUSR 0100  /* Execute by owner.  */
32
#endif
33
#ifndef S_IXGRP
34
#define S_IXGRP 0010  /* Execute by group.  */
35
#endif
36
#ifndef S_IXOTH
37
#define S_IXOTH 0001  /* Execute by others.  */
38
#endif
39
40
/*
41
SECTION
42
  Opening and closing BFDs
43
44
SUBSECTION
45
  Functions for opening and closing
46
*/
47
48
/* Counter used to initialize the unique bfd identifier.  */
49
50
static unsigned int bfd_id_counter = 0;
51
52
/* fdopen is a loser -- we should use stdio exclusively.  Unfortunately
53
   if we do that we can't use fcntl.  */
54
55
/*
56
INTERNAL_FUNCTION
57
  _bfd_new_bfd
58
59
SYNOPSIS
60
  bfd *_bfd_new_bfd (void);
61
62
DESCRIPTION
63
  Return a new BFD.  All BFD's are allocated through this routine.
64
*/
65
66
bfd *
67
_bfd_new_bfd (void)
68
10.0M
{
69
10.0M
  bfd *nbfd;
70
71
10.0M
  nbfd = (bfd *) bfd_zmalloc (sizeof (bfd));
72
10.0M
  if (nbfd == NULL)
73
0
    return NULL;
74
75
10.0M
  if (!bfd_lock ())
76
0
    goto loser;
77
10.0M
  nbfd->id = bfd_id_counter++;
78
10.0M
  if (!bfd_unlock ())
79
0
    goto loser;
80
81
10.0M
  nbfd->memory = objalloc_create ();
82
10.0M
  if (nbfd->memory == NULL)
83
0
    {
84
0
      bfd_set_error (bfd_error_no_memory);
85
0
      goto loser;
86
0
    }
87
88
10.0M
  nbfd->arch_info = &bfd_default_arch_struct;
89
90
10.0M
  if (!bfd_hash_table_init_n (& nbfd->section_htab, bfd_section_hash_newfunc,
91
10.0M
            sizeof (struct section_hash_entry), 13))
92
0
    {
93
0
      objalloc_free (nbfd->memory);
94
0
      goto loser;
95
0
    }
96
97
10.0M
  nbfd->archive_plugin_fd = -1;
98
10.0M
  return nbfd;
99
100
0
 loser:
101
0
  free (nbfd);
102
0
  return NULL;
103
10.0M
}
104
105
static const struct bfd_iovec opncls_iovec;
106
107
/*
108
INTERNAL_FUNCTION
109
  _bfd_new_bfd_contained_in
110
111
SYNOPSIS
112
  bfd *_bfd_new_bfd_contained_in (bfd *);
113
114
DESCRIPTION
115
  Allocate a new BFD as a member of archive OBFD.
116
*/
117
118
bfd *
119
_bfd_new_bfd_contained_in (bfd *obfd)
120
9.79M
{
121
9.79M
  bfd *nbfd;
122
123
  /* Nested archives in bims are unsupported.  */
124
9.79M
  if ((obfd->flags & BFD_IN_MEMORY) != 0)
125
0
    {
126
0
      bfd_set_error (bfd_error_malformed_archive);
127
0
      return NULL;
128
0
    }
129
9.79M
  nbfd = _bfd_new_bfd ();
130
9.79M
  if (nbfd == NULL)
131
0
    return NULL;
132
9.79M
  nbfd->xvec = obfd->xvec;
133
9.79M
  nbfd->iovec = obfd->iovec;
134
9.79M
  if (obfd->iovec == &opncls_iovec)
135
0
    nbfd->iostream = obfd->iostream;
136
9.79M
  nbfd->my_archive = obfd;
137
9.79M
  nbfd->direction = read_direction;
138
9.79M
  nbfd->target_defaulted = obfd->target_defaulted;
139
9.79M
  nbfd->lto_output = obfd->lto_output;
140
9.79M
  nbfd->no_export = obfd->no_export;
141
9.79M
  return nbfd;
142
9.79M
}
143
144
/* Delete a BFD.  */
145
146
static void
147
_bfd_delete_bfd (bfd *abfd)
148
10.0M
{
149
  /* Give the target _bfd_free_cached_info a chance to free memory.  */
150
10.0M
  if (abfd->memory && abfd->xvec)
151
10.0M
    bfd_free_cached_info (abfd);
152
153
  /* The target _bfd_free_cached_info may not have done anything..  */
154
10.0M
  bfd_hash_table_free (&abfd->section_htab);
155
10.0M
  if (abfd->memory)
156
10.0M
    objalloc_free (abfd->memory);
157
158
10.0M
#ifdef USE_MMAP
159
10.0M
  struct bfd_mmapped *mmapped, *next;
160
10.0M
  for (mmapped = abfd->mmapped; mmapped != NULL; mmapped = next)
161
2
    {
162
2
      struct bfd_mmapped_entry *entries = mmapped->entries;
163
2
      next = mmapped->next;
164
9
      for (unsigned int i = 0; i < mmapped->next_entry; i++)
165
7
  munmap (entries[i].addr, entries[i].size);
166
2
      munmap (mmapped, _bfd_pagesize);
167
2
    }
168
10.0M
#endif
169
170
10.0M
  free (abfd->arelt_data);
171
10.0M
  free (abfd);
172
10.0M
}
173
174
/*
175
INTERNAL_FUNCTION
176
  _bfd_free_cached_info
177
178
SYNOPSIS
179
  bool _bfd_free_cached_info (bfd *);
180
181
DESCRIPTION
182
  Free objalloc memory.
183
*/
184
185
bool
186
_bfd_free_cached_info (bfd *abfd)
187
9.74M
{
188
9.74M
  bfd_hash_table_free (&abfd->section_htab);
189
190
9.74M
  abfd->sections = NULL;
191
9.74M
  abfd->section_last = NULL;
192
9.74M
  abfd->section_count = 0;
193
9.74M
  abfd->outsymbols = NULL;
194
9.74M
  abfd->tdata.any = NULL;
195
9.74M
  abfd->usrdata = NULL;
196
9.74M
  abfd->format = bfd_unknown;
197
198
9.74M
  return true;
199
9.74M
}
200
201
/*
202
FUNCTION
203
  bfd_fopen
204
205
SYNOPSIS
206
  bfd *bfd_fopen (const char *filename, const char *target,
207
      const char *mode, int fd);
208
209
DESCRIPTION
210
  Open the file @var{filename} with the target @var{target}.
211
  Return a pointer to the created BFD.  If @var{fd} is not -1,
212
  then <<fdopen>> is used to open the file; otherwise, <<fopen>>
213
  is used.  @var{mode} is passed directly to <<fopen>> or
214
  <<fdopen>>.
215
216
  Calls <<bfd_find_target>>, so @var{target} is interpreted as by
217
  that function.
218
219
  The new BFD is marked as cacheable iff @var{fd} is -1.
220
221
  If <<NULL>> is returned then an error has occured.   Possible errors
222
  are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or
223
  <<system_call>> error.
224
225
  On error, @var{fd} is always closed.
226
227
  A copy of the @var{filename} argument is stored in the newly created
228
  BFD.  It can be accessed via the bfd_get_filename() macro.
229
*/
230
231
bfd *
232
bfd_fopen (const char *filename, const char *target, const char *mode, int fd)
233
220k
{
234
220k
  bfd *nbfd;
235
220k
  const bfd_target *target_vec;
236
237
220k
  nbfd = _bfd_new_bfd ();
238
220k
  if (nbfd == NULL)
239
0
    {
240
0
      if (fd != -1)
241
0
  close (fd);
242
0
      return NULL;
243
0
    }
244
245
220k
  target_vec = bfd_find_target (target, nbfd);
246
220k
  if (target_vec == NULL)
247
27
    {
248
27
      if (fd != -1)
249
0
  close (fd);
250
27
      _bfd_delete_bfd (nbfd);
251
27
      return NULL;
252
27
    }
253
254
220k
#ifdef HAVE_FDOPEN
255
220k
  if (fd != -1)
256
20.0k
    nbfd->iostream = fdopen (fd, mode);
257
199k
  else
258
199k
#endif
259
199k
    nbfd->iostream = _bfd_real_fopen (filename, mode);
260
220k
  if (nbfd->iostream == NULL)
261
3.47k
    {
262
3.47k
      bfd_set_error (bfd_error_system_call);
263
3.47k
      if (fd != -1)
264
0
  close (fd);
265
3.47k
      _bfd_delete_bfd (nbfd);
266
3.47k
      return NULL;
267
3.47k
    }
268
269
  /* OK, put everything where it belongs.  */
270
271
  /* PR 11983: Do not cache the original filename, but
272
     rather make a copy - the original might go away.  */
273
216k
  if (!bfd_set_filename (nbfd, filename))
274
0
    {
275
0
      fclose (nbfd->iostream);
276
0
      _bfd_delete_bfd (nbfd);
277
0
      return NULL;
278
0
    }
279
280
  /* Figure out whether the user is opening the file for reading,
281
     writing, or both, by looking at the MODE argument.  */
282
216k
  if ((mode[0] == 'r' || mode[0] == 'w' || mode[0] == 'a')
283
216k
      && mode[1] == '+')
284
20.0k
    nbfd->direction = both_direction;
285
196k
  else if (mode[0] == 'r')
286
196k
    nbfd->direction = read_direction;
287
0
  else
288
0
    nbfd->direction = write_direction;
289
290
216k
  if (!bfd_cache_init (nbfd))
291
0
    {
292
0
      fclose (nbfd->iostream);
293
0
      _bfd_delete_bfd (nbfd);
294
0
      return NULL;
295
0
    }
296
216k
  nbfd->opened_once = true;
297
298
  /* If we opened the file by name, mark it cacheable; we can close it
299
     and reopen it later.  However, if a file descriptor was provided,
300
     then it may have been opened with special flags that make it
301
     unsafe to close and reopen the file.  */
302
216k
  if (fd == -1)
303
196k
    (void) bfd_set_cacheable (nbfd, true);
304
305
216k
  return nbfd;
306
216k
}
307
308
/*
309
FUNCTION
310
  bfd_openr
311
312
SYNOPSIS
313
  bfd *bfd_openr (const char *filename, const char *target);
314
315
DESCRIPTION
316
  Open the file @var{filename} (using <<fopen>>) with the target
317
  @var{target}.  Return a pointer to the created BFD.
318
319
  Calls <<bfd_find_target>>, so @var{target} is interpreted as by
320
  that function.
321
322
  If <<NULL>> is returned then an error has occured.   Possible errors
323
  are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or
324
  <<system_call>> error.
325
326
  A copy of the @var{filename} argument is stored in the newly created
327
  BFD.  It can be accessed via the bfd_get_filename() macro.
328
*/
329
330
bfd *
331
bfd_openr (const char *filename, const char *target)
332
199k
{
333
199k
  return bfd_fopen (filename, target, FOPEN_RB, -1);
334
199k
}
335
336
/* Don't try to `optimize' this function:
337
338
   o - We lock using stack space so that interrupting the locking
339
       won't cause a storage leak.
340
   o - We open the file stream last, since we don't want to have to
341
       close it if anything goes wrong.  Closing the stream means closing
342
       the file descriptor too, even though we didn't open it.  */
343
/*
344
FUNCTION
345
  bfd_fdopenr
346
347
SYNOPSIS
348
  bfd *bfd_fdopenr (const char *filename, const char *target, int fd);
349
350
DESCRIPTION
351
  <<bfd_fdopenr>> is to <<bfd_fopenr>> much like <<fdopen>> is to
352
  <<fopen>>.  It opens a BFD on a file already described by the
353
  @var{fd} supplied.
354
355
  When the file is later <<bfd_close>>d, the file descriptor will
356
  be closed.  If the caller desires that this file descriptor be
357
  cached by BFD (opened as needed, closed as needed to free
358
  descriptors for other opens), with the supplied @var{fd} used as
359
  an initial file descriptor (but subject to closure at any time),
360
  call bfd_set_cacheable(bfd, 1) on the returned BFD.  The default
361
  is to assume no caching; the file descriptor will remain open
362
  until <<bfd_close>>, and will not be affected by BFD operations
363
  on other files.
364
365
  Possible errors are <<bfd_error_no_memory>>,
366
  <<bfd_error_invalid_target>> and <<bfd_error_system_call>>.
367
368
  On error, @var{fd} is closed.
369
370
  A copy of the @var{filename} argument is stored in the newly created
371
  BFD.  It can be accessed via the bfd_get_filename() macro.
372
*/
373
374
bfd *
375
bfd_fdopenr (const char *filename, const char *target, int fd)
376
20.0k
{
377
20.0k
  const char *mode;
378
20.0k
#if defined(HAVE_FCNTL) && defined(F_GETFL)
379
20.0k
  int fdflags;
380
20.0k
#endif
381
382
#if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
383
  mode = FOPEN_RUB; /* Assume full access.  */
384
#else
385
20.0k
  fdflags = fcntl (fd, F_GETFL, NULL);
386
20.0k
  if (fdflags == -1)
387
0
    {
388
0
      int save = errno;
389
390
0
      close (fd);
391
0
      errno = save;
392
0
      bfd_set_error (bfd_error_system_call);
393
0
      return NULL;
394
0
    }
395
396
  /* (O_ACCMODE) parens are to avoid Ultrix header file bug.  */
397
20.0k
  switch (fdflags & (O_ACCMODE))
398
20.0k
    {
399
0
    case O_RDONLY: mode = FOPEN_RB; break;
400
0
    case O_WRONLY: mode = FOPEN_RUB; break;
401
20.0k
    case O_RDWR:   mode = FOPEN_RUB; break;
402
0
    default: abort ();
403
20.0k
    }
404
20.0k
#endif
405
406
20.0k
  return bfd_fopen (filename, target, mode, fd);
407
20.0k
}
408
409
/*
410
FUNCTION
411
  bfd_fdopenw
412
413
SYNOPSIS
414
  bfd *bfd_fdopenw (const char *filename, const char *target, int fd);
415
416
DESCRIPTION
417
  <<bfd_fdopenw>> is exactly like <<bfd_fdopenr>> with the exception that
418
  the resulting BFD is suitable for output.
419
*/
420
421
bfd *
422
bfd_fdopenw (const char *filename, const char *target, int fd)
423
0
{
424
0
  bfd *out = bfd_fdopenr (filename, target, fd);
425
426
0
  if (out != NULL)
427
0
    {
428
0
      if (!bfd_write_p (out))
429
0
  {
430
0
    close (fd);
431
0
    _bfd_delete_bfd (out);
432
0
    out = NULL;
433
0
    bfd_set_error (bfd_error_invalid_operation);
434
0
  }
435
0
      else
436
0
  out->direction = write_direction;
437
0
    }
438
439
0
  return out;
440
0
}
441
442
/*
443
FUNCTION
444
  bfd_openstreamr
445
446
SYNOPSIS
447
  bfd *bfd_openstreamr (const char * filename, const char * target,
448
            void * stream);
449
450
DESCRIPTION
451
  Open a BFD for read access on an existing stdio stream.  When
452
  the BFD is passed to <<bfd_close>>, the stream will be closed.
453
454
  A copy of the @var{filename} argument is stored in the newly created
455
  BFD.  It can be accessed via the bfd_get_filename() macro.
456
*/
457
458
bfd *
459
bfd_openstreamr (const char *filename, const char *target, void *streamarg)
460
0
{
461
0
  FILE *stream = (FILE *) streamarg;
462
0
  bfd *nbfd;
463
0
  const bfd_target *target_vec;
464
465
0
  nbfd = _bfd_new_bfd ();
466
0
  if (nbfd == NULL)
467
0
    return NULL;
468
469
0
  target_vec = bfd_find_target (target, nbfd);
470
0
  if (target_vec == NULL)
471
0
    {
472
0
      _bfd_delete_bfd (nbfd);
473
0
      return NULL;
474
0
    }
475
476
0
  nbfd->iostream = stream;
477
  /* PR 11983: Do not cache the original filename, but
478
     rather make a copy - the original might go away.  */
479
0
  if (!bfd_set_filename (nbfd, filename))
480
0
    {
481
0
      _bfd_delete_bfd (nbfd);
482
0
      return NULL;
483
0
    }
484
0
  nbfd->direction = read_direction;
485
486
0
  if (! bfd_cache_init (nbfd))
487
0
    {
488
0
      _bfd_delete_bfd (nbfd);
489
0
      return NULL;
490
0
    }
491
492
0
  return nbfd;
493
0
}
494
495
/*
496
FUNCTION
497
  bfd_openr_fake_archive
498
499
SYNOPSIS
500
  bfd *bfd_openr_fake_archive (bfd *fbfd);
501
502
DESCRIPTION
503
  Open a list of BFDs starting from @var{fbfd} as an artificial
504
  archive.  Subsequent members of the archive are indicated by each
505
  BFD's proxy_handle.abfd member, with the final one holding a NULL
506
  pointer there.  The newly opened archive will necessarily also be
507
  a thin archive as there will be member files only to refer to and
508
  no containing archive file.
509
*/
510
511
bfd *
512
bfd_openr_fake_archive (bfd *fbfd)
513
0
{
514
0
  bfd *abfd, *nbfd;
515
516
0
  if (fbfd == NULL)
517
0
    return NULL;
518
519
0
  nbfd = _bfd_new_bfd ();
520
0
  if (nbfd == NULL)
521
0
    return NULL;
522
523
0
  nbfd->xvec = fbfd->xvec;
524
0
  bfd_set_format (nbfd, bfd_archive);
525
0
  bfd_set_thin_archive (nbfd, true);
526
0
  bfd_set_fake_archive (nbfd, true);
527
0
  bfd_ardata (nbfd)->first_file.abfd = fbfd;
528
0
  nbfd->direction = read_direction;
529
530
0
  for (abfd = fbfd; abfd != NULL; abfd = abfd->proxy_handle.abfd)
531
0
    abfd->my_archive = nbfd;
532
533
0
  return nbfd;
534
0
}
535
536
/*
537
FUNCTION
538
  bfd_openr_iovec
539
540
SYNOPSIS
541
  bfd *bfd_openr_iovec (const char *filename, const char *target,
542
            void *(*open_func) (struct bfd *nbfd,
543
              void *open_closure),
544
            void *open_closure,
545
            file_ptr (*pread_func) (struct bfd *nbfd,
546
                  void *stream,
547
                  void *buf,
548
                  file_ptr nbytes,
549
                  file_ptr offset),
550
            int (*close_func) (struct bfd *nbfd,
551
             void *stream),
552
            int (*stat_func) (struct bfd *abfd,
553
            void *stream,
554
            struct stat *sb));
555
556
DESCRIPTION
557
  Create and return a BFD backed by a read-only @var{stream}.
558
  The @var{stream} is created using @var{open_func}, accessed using
559
  @var{pread_func} and destroyed using @var{close_func}.
560
561
  Calls <<bfd_find_target>>, so @var{target} is interpreted as by
562
  that function.
563
564
  Calls @var{open_func} (which can call <<bfd_zalloc>> and
565
  <<bfd_get_filename>>) to obtain the read-only stream backing
566
  the BFD.  @var{open_func} either succeeds returning the
567
  non-<<NULL>> @var{stream}, or fails returning <<NULL>>
568
  (setting <<bfd_error>>).
569
570
  Calls @var{pread_func} to request @var{nbytes} of data from
571
  @var{stream} starting at @var{offset} (e.g., via a call to
572
  <<bfd_read>>).  @var{pread_func} either succeeds returning the
573
  number of bytes read (which can be less than @var{nbytes} when
574
  end-of-file), or fails returning -1 (setting <<bfd_error>>).
575
576
  Calls @var{close_func} when the BFD is later closed using
577
  <<bfd_close>>.  @var{close_func} either succeeds returning 0, or
578
  fails returning -1 (setting <<bfd_error>>).
579
580
  Calls @var{stat_func} to fill in a stat structure for bfd_stat,
581
  bfd_get_size, and bfd_get_mtime calls.  @var{stat_func} returns 0
582
  on success, or returns -1 on failure (setting <<bfd_error>>).
583
584
  If <<bfd_openr_iovec>> returns <<NULL>> then an error has
585
  occurred.  Possible errors are <<bfd_error_no_memory>>,
586
  <<bfd_error_invalid_target>> and <<bfd_error_system_call>>.
587
588
  A copy of the @var{filename} argument is stored in the newly created
589
  BFD.  It can be accessed via the bfd_get_filename() macro.
590
*/
591
592
struct opncls
593
{
594
  void *stream;
595
  file_ptr (*pread) (struct bfd *abfd, void *stream, void *buf,
596
         file_ptr nbytes, file_ptr offset);
597
  int (*close) (struct bfd *abfd, void *stream);
598
  int (*stat) (struct bfd *abfd, void *stream, struct stat *sb);
599
  file_ptr where;
600
};
601
602
static file_ptr
603
opncls_btell (struct bfd *abfd)
604
0
{
605
0
  struct opncls *vec = (struct opncls *) abfd->iostream;
606
0
  return vec->where;
607
0
}
608
609
static int
610
opncls_bseek (struct bfd *abfd, file_ptr offset, int whence)
611
0
{
612
0
  struct opncls *vec = (struct opncls *) abfd->iostream;
613
0
  switch (whence)
614
0
    {
615
0
    case SEEK_SET: vec->where = offset; break;
616
0
    case SEEK_CUR: vec->where += offset; break;
617
0
    case SEEK_END: return -1;
618
0
    }
619
0
  return 0;
620
0
}
621
622
static file_ptr
623
opncls_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
624
0
{
625
0
  struct opncls *vec = (struct opncls *) abfd->iostream;
626
0
  file_ptr nread = (vec->pread) (abfd, vec->stream, buf, nbytes, vec->where);
627
628
0
  if (nread < 0)
629
0
    return nread;
630
0
  vec->where += nread;
631
0
  return nread;
632
0
}
633
634
static file_ptr
635
opncls_bwrite (struct bfd *abfd ATTRIBUTE_UNUSED,
636
        const void *where ATTRIBUTE_UNUSED,
637
        file_ptr nbytes ATTRIBUTE_UNUSED)
638
0
{
639
0
  return -1;
640
0
}
641
642
static int
643
opncls_bclose (struct bfd *abfd)
644
0
{
645
0
  struct opncls *vec = (struct opncls *) abfd->iostream;
646
  /* Since the VEC's memory is bound to the bfd deleting the bfd will
647
     free it.  */
648
0
  int status = 0;
649
650
0
  if (vec->close != NULL)
651
0
    status = (vec->close) (abfd, vec->stream);
652
0
  abfd->iostream = NULL;
653
0
  return status;
654
0
}
655
656
static int
657
opncls_bflush (struct bfd *abfd ATTRIBUTE_UNUSED)
658
0
{
659
0
  return 0;
660
0
}
661
662
static int
663
opncls_bstat (struct bfd *abfd, struct stat *sb)
664
0
{
665
0
  struct opncls *vec = (struct opncls *) abfd->iostream;
666
667
0
  memset (sb, 0, sizeof (*sb));
668
0
  if (vec->stat == NULL)
669
0
    return 0;
670
671
0
  return (vec->stat) (abfd, vec->stream, sb);
672
0
}
673
674
static void *
675
opncls_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
676
        void *addr ATTRIBUTE_UNUSED,
677
        size_t len ATTRIBUTE_UNUSED,
678
        int prot ATTRIBUTE_UNUSED,
679
        int flags ATTRIBUTE_UNUSED,
680
        file_ptr offset ATTRIBUTE_UNUSED,
681
        void **map_addr ATTRIBUTE_UNUSED,
682
        size_t *map_len ATTRIBUTE_UNUSED)
683
0
{
684
0
  return MAP_FAILED;
685
0
}
686
687
static const struct bfd_iovec opncls_iovec =
688
{
689
  &opncls_bread, &opncls_bwrite, &opncls_btell, &opncls_bseek,
690
  &opncls_bclose, &opncls_bflush, &opncls_bstat, &opncls_bmmap
691
};
692
693
bfd *
694
bfd_openr_iovec (const char *filename, const char *target,
695
     void *(*open_p) (struct bfd *, void *),
696
     void *open_closure,
697
     file_ptr (*pread_p) (struct bfd *, void *, void *,
698
              file_ptr, file_ptr),
699
     int (*close_p) (struct bfd *, void *),
700
     int (*stat_p) (struct bfd *, void *, struct stat *))
701
0
{
702
0
  bfd *nbfd;
703
0
  const bfd_target *target_vec;
704
0
  struct opncls *vec;
705
0
  void *stream;
706
707
0
  nbfd = _bfd_new_bfd ();
708
0
  if (nbfd == NULL)
709
0
    return NULL;
710
711
0
  target_vec = bfd_find_target (target, nbfd);
712
0
  if (target_vec == NULL)
713
0
    {
714
0
      _bfd_delete_bfd (nbfd);
715
0
      return NULL;
716
0
    }
717
718
  /* PR 11983: Do not cache the original filename, but
719
     rather make a copy - the original might go away.  */
720
0
  if (!bfd_set_filename (nbfd, filename))
721
0
    {
722
0
      _bfd_delete_bfd (nbfd);
723
0
      return NULL;
724
0
    }
725
0
  nbfd->direction = read_direction;
726
727
  /* `open_p (...)' would get expanded by an the open(2) syscall macro.  */
728
0
  stream = (*open_p) (nbfd, open_closure);
729
0
  if (stream == NULL)
730
0
    {
731
0
      _bfd_delete_bfd (nbfd);
732
0
      return NULL;
733
0
    }
734
735
0
  vec = (struct opncls *) bfd_zalloc (nbfd, sizeof (struct opncls));
736
0
  vec->stream = stream;
737
0
  vec->pread = pread_p;
738
0
  vec->close = close_p;
739
0
  vec->stat = stat_p;
740
741
0
  nbfd->iovec = &opncls_iovec;
742
0
  nbfd->iostream = vec;
743
744
0
  return nbfd;
745
0
}
746

747
/* bfd_openw -- open for writing.
748
   Returns a pointer to a freshly-allocated BFD on success, or NULL.
749
750
   See comment by bfd_fdopenr before you try to modify this function.  */
751
752
/*
753
FUNCTION
754
  bfd_openw
755
756
SYNOPSIS
757
  bfd *bfd_openw (const char *filename, const char *target);
758
759
DESCRIPTION
760
  Create a BFD, associated with file @var{filename}, using the
761
  file format @var{target}, and return a pointer to it.
762
763
  Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>,
764
  <<bfd_error_invalid_target>>.
765
766
  A copy of the @var{filename} argument is stored in the newly created
767
  BFD.  It can be accessed via the bfd_get_filename() macro.
768
*/
769
770
bfd *
771
bfd_openw (const char *filename, const char *target)
772
27.8k
{
773
27.8k
  bfd *nbfd;
774
27.8k
  const bfd_target *target_vec;
775
776
  /* nbfd has to point to head of malloc'ed block so that bfd_close may
777
     reclaim it correctly.  */
778
27.8k
  nbfd = _bfd_new_bfd ();
779
27.8k
  if (nbfd == NULL)
780
0
    return NULL;
781
782
27.8k
  target_vec = bfd_find_target (target, nbfd);
783
27.8k
  if (target_vec == NULL)
784
0
    {
785
0
      _bfd_delete_bfd (nbfd);
786
0
      return NULL;
787
0
    }
788
789
  /* PR 11983: Do not cache the original filename, but
790
     rather make a copy - the original might go away.  */
791
27.8k
  if (!bfd_set_filename (nbfd, filename))
792
0
    {
793
0
      _bfd_delete_bfd (nbfd);
794
0
      return NULL;
795
0
    }
796
27.8k
  nbfd->direction = write_direction;
797
798
27.8k
  if (bfd_open_file (nbfd) == NULL)
799
920
    {
800
      /* File not writeable, etc.  */
801
920
      bfd_set_error (bfd_error_system_call);
802
920
      _bfd_delete_bfd (nbfd);
803
920
      return NULL;
804
920
  }
805
806
26.9k
  return nbfd;
807
27.8k
}
808
809
/*
810
FUNCTION
811
  bfd_elf_bfd_from_remote_memory
812
813
SYNOPSIS
814
  bfd *bfd_elf_bfd_from_remote_memory
815
    (bfd *templ, bfd_vma ehdr_vma, bfd_size_type size, bfd_vma *loadbasep,
816
     int (*target_read_memory)
817
       (bfd_vma vma, bfd_byte *myaddr, bfd_size_type len));
818
819
DESCRIPTION
820
  Create a new BFD as if by bfd_openr.  Rather than opening a
821
  file, reconstruct an ELF file by reading the segments out of
822
  remote memory based on the ELF file header at EHDR_VMA and the
823
  ELF program headers it points to.  If non-zero, SIZE is the
824
  known extent of the object.  If not null, *LOADBASEP is filled
825
  in with the difference between the VMAs from which the
826
  segments were read, and the VMAs the file headers (and hence
827
  BFD's idea of each section's VMA) put them at.
828
829
  The function TARGET_READ_MEMORY is called to copy LEN bytes
830
  from the remote memory at target address VMA into the local
831
  buffer at MYADDR; it should return zero on success or an
832
  errno code on failure.  TEMPL must be a BFD for an ELF
833
  target with the word size and byte order found in the remote
834
  memory.
835
*/
836
837
bfd *
838
bfd_elf_bfd_from_remote_memory
839
  (bfd *templ,
840
   bfd_vma ehdr_vma,
841
   bfd_size_type size,
842
   bfd_vma *loadbasep,
843
   int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
844
0
{
845
0
  if (bfd_get_flavour (templ) != bfd_target_elf_flavour)
846
0
    {
847
0
      bfd_set_error (bfd_error_invalid_operation);
848
0
      return NULL;
849
0
    }
850
0
  return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
851
0
    (templ, ehdr_vma, size, loadbasep, target_read_memory);
852
0
}
853
854
static inline void
855
_maybe_make_executable (bfd * abfd)
856
10.0M
{
857
  /* If the file was open for writing and is now executable,
858
     make it so.  */
859
10.0M
  if (abfd->direction == write_direction
860
31.3k
      && (abfd->flags & (EXEC_P | DYNAMIC)) != 0)
861
5.89k
    {
862
5.89k
      struct stat buf;
863
864
5.89k
      if (stat (bfd_get_filename (abfd), &buf) == 0
865
    /* Do not attempt to change non-regular files.  This is
866
       here especially for configure scripts and kernel builds
867
       which run tests with "ld [...] -o /dev/null".  */
868
5.89k
    && S_ISREG(buf.st_mode))
869
5.89k
  {
870
5.89k
    unsigned int mask = umask (0);
871
872
5.89k
    umask (mask);
873
5.89k
    chmod (bfd_get_filename (abfd),
874
5.89k
     (0777
875
5.89k
      & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
876
5.89k
  }
877
5.89k
    }
878
10.0M
}
879
880
/*
881
FUNCTION
882
  bfd_close
883
884
SYNOPSIS
885
  bool bfd_close (bfd *abfd);
886
887
DESCRIPTION
888
  Close a BFD. If the BFD was open for writing, then pending
889
  operations are completed and the file written out and closed.
890
  If the created file is executable, then <<chmod>> is called
891
  to mark it as such.
892
893
  All memory attached to the BFD is released.
894
895
  The file descriptor associated with the BFD is closed (even
896
  if it was passed in to BFD by <<bfd_fdopenr>>).
897
898
  <<TRUE>> is returned if all is ok, otherwise <<FALSE>>.
899
*/
900
901
bool
902
bfd_close (bfd *abfd)
903
10.0M
{
904
10.0M
  bool ret = (!bfd_write_p (abfd)
905
30.6k
        || BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)));
906
907
10.0M
  return bfd_close_all_done (abfd) && ret;
908
10.0M
}
909
910
/*
911
FUNCTION
912
  bfd_close_all_done
913
914
SYNOPSIS
915
  bool bfd_close_all_done (bfd *);
916
917
DESCRIPTION
918
  Close a BFD.  Differs from <<bfd_close>> since it does not
919
  complete any pending operations.  This routine would be used
920
  if the application had just used BFD for swapping and didn't
921
  want to use any of the writing code.
922
923
  If the created file is executable, then <<chmod>> is called
924
  to mark it as such.
925
926
  All memory attached to the BFD is released.
927
928
  <<TRUE>> is returned if all is ok, otherwise <<FALSE>>.
929
*/
930
931
bool
932
bfd_close_all_done (bfd *abfd)
933
10.0M
{
934
10.0M
  bool ret = BFD_SEND (abfd, _close_and_cleanup, (abfd));
935
936
10.0M
  if (abfd->iovec != NULL)
937
10.0M
    ret &= abfd->iovec->bclose (abfd) == 0;
938
939
10.0M
  if (ret)
940
10.0M
    _maybe_make_executable (abfd);
941
942
10.0M
  _bfd_delete_bfd (abfd);
943
944
10.0M
  return ret;
945
10.0M
}
946
947
/*
948
FUNCTION
949
  bfd_create
950
951
SYNOPSIS
952
  bfd *bfd_create (const char *filename, bfd *templ);
953
954
DESCRIPTION
955
  Create a new BFD in the manner of <<bfd_openw>>, but without
956
  opening a file. The new BFD takes the target from the target
957
  used by @var{templ}. The format is always set to <<bfd_object>>.
958
959
  A copy of the @var{filename} argument is stored in the newly created
960
  BFD.  It can be accessed via the bfd_get_filename() macro.
961
*/
962
963
bfd *
964
bfd_create (const char *filename, bfd *templ)
965
4.34k
{
966
4.34k
  bfd *nbfd;
967
968
4.34k
  nbfd = _bfd_new_bfd ();
969
4.34k
  if (nbfd == NULL)
970
0
    return NULL;
971
  /* PR 11983: Do not cache the original filename, but
972
     rather make a copy - the original might go away.  */
973
4.34k
  if (!bfd_set_filename (nbfd, filename))
974
0
    {
975
0
      _bfd_delete_bfd (nbfd);
976
0
      return NULL;
977
0
    }
978
4.34k
  if (templ)
979
4.34k
    nbfd->xvec = templ->xvec;
980
4.34k
  nbfd->direction = no_direction;
981
4.34k
  bfd_set_format (nbfd, bfd_object);
982
983
4.34k
  return nbfd;
984
4.34k
}
985
986
/*
987
FUNCTION
988
  bfd_make_writable
989
990
SYNOPSIS
991
  bool bfd_make_writable (bfd *abfd);
992
993
DESCRIPTION
994
  Takes a BFD as created by <<bfd_create>> and converts it
995
  into one like as returned by <<bfd_openw>>.  It does this
996
  by converting the BFD to BFD_IN_MEMORY.  It's assumed that
997
  you will call <<bfd_make_readable>> on this bfd later.
998
999
  <<TRUE>> is returned if all is ok, otherwise <<FALSE>>.
1000
*/
1001
1002
bool
1003
bfd_make_writable (bfd *abfd)
1004
4.34k
{
1005
4.34k
  struct bfd_in_memory *bim;
1006
1007
4.34k
  if (abfd->direction != no_direction)
1008
0
    {
1009
0
      bfd_set_error (bfd_error_invalid_operation);
1010
0
      return false;
1011
0
    }
1012
1013
4.34k
  bim = (struct bfd_in_memory *) bfd_malloc (sizeof (struct bfd_in_memory));
1014
4.34k
  if (bim == NULL)
1015
0
    return false; /* bfd_error already set.  */
1016
4.34k
  abfd->iostream = bim;
1017
  /* bfd_write will grow these as needed.  */
1018
4.34k
  bim->size = 0;
1019
4.34k
  bim->capacity = 0;
1020
4.34k
  bim->buffer = 0;
1021
1022
4.34k
  abfd->flags |= BFD_IN_MEMORY;
1023
4.34k
  abfd->iovec = &_bfd_memory_iovec;
1024
4.34k
  abfd->origin = 0;
1025
4.34k
  abfd->direction = write_direction;
1026
4.34k
  abfd->where = 0;
1027
1028
4.34k
  return true;
1029
4.34k
}
1030
1031
/*
1032
FUNCTION
1033
  bfd_make_readable
1034
1035
SYNOPSIS
1036
  bool bfd_make_readable (bfd *abfd);
1037
1038
DESCRIPTION
1039
  Takes a BFD as created by <<bfd_create>> and
1040
  <<bfd_make_writable>> and converts it into one like as
1041
  returned by <<bfd_openr>>.  It does this by writing the
1042
  contents out to the memory buffer, then reversing the
1043
  direction.
1044
1045
  <<TRUE>> is returned if all is ok, otherwise <<FALSE>>.  */
1046
1047
bool
1048
bfd_make_readable (bfd *abfd)
1049
0
{
1050
0
  if (abfd->direction != write_direction || !(abfd->flags & BFD_IN_MEMORY))
1051
0
    {
1052
0
      bfd_set_error (bfd_error_invalid_operation);
1053
0
      return false;
1054
0
    }
1055
1056
0
  if (!BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
1057
0
    return false;
1058
1059
0
  if (!BFD_SEND (abfd, _bfd_free_cached_info, (abfd)))
1060
0
    return false;
1061
1062
0
  if (!BFD_SEND (abfd, _close_and_cleanup, (abfd)))
1063
0
    return false;
1064
1065
0
  _bfd_free_cached_info (abfd);
1066
0
  if (!bfd_hash_table_init_n (&abfd->section_htab, bfd_section_hash_newfunc,
1067
0
            sizeof (struct section_hash_entry), 13))
1068
0
    return false;
1069
1070
0
  abfd->arch_info = &bfd_default_arch_struct;
1071
1072
0
  abfd->where = 0;
1073
0
  abfd->my_archive = NULL;
1074
0
  abfd->origin = 0;
1075
0
  abfd->opened_once = false;
1076
0
  abfd->output_has_begun = false;
1077
0
  abfd->cacheable = false;
1078
0
  abfd->mtime_set = false;
1079
1080
0
  abfd->target_defaulted = true;
1081
0
  abfd->direction = read_direction;
1082
0
  abfd->symcount = 0;
1083
0
  abfd->size = 0;
1084
1085
0
  bfd_check_format (abfd, bfd_object);
1086
1087
0
  return true;
1088
0
}
1089
1090
/*
1091
   GNU Extension: separate debug-info files
1092
1093
   The idea here is that a special section called .gnu_debuglink might be
1094
   embedded in a binary file, which indicates that some *other* file
1095
   contains the real debugging information. This special section contains a
1096
   filename and CRC32 checksum, which we read and resolve to another file,
1097
   if it exists.
1098
1099
   This facilitates "optional" provision of debugging information, without
1100
   having to provide two complete copies of every binary object (with and
1101
   without debug symbols).  */
1102
1103
3.59k
#define GNU_DEBUGLINK   ".gnu_debuglink"
1104
0
#define GNU_DEBUGALTLINK  ".gnu_debugaltlink"
1105
1106
/*
1107
FUNCTION
1108
  bfd_calc_gnu_debuglink_crc32
1109
1110
SYNOPSIS
1111
  uint32_t bfd_calc_gnu_debuglink_crc32
1112
    (uint32_t crc, const bfd_byte *buf, bfd_size_type len);
1113
1114
DESCRIPTION
1115
  Computes a CRC value as used in the .gnu_debuglink section.
1116
  Advances the previously computed @var{crc} value by computing
1117
  and adding in the crc32 for @var{len} bytes of @var{buf}.
1118
1119
  Return the updated CRC32 value.
1120
*/
1121
1122
uint32_t
1123
bfd_calc_gnu_debuglink_crc32 (uint32_t crc,
1124
            const bfd_byte *buf,
1125
            bfd_size_type len)
1126
0
{
1127
0
  static const uint32_t crc32_table[256] =
1128
0
    {
1129
0
      0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
1130
0
      0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
1131
0
      0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
1132
0
      0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
1133
0
      0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
1134
0
      0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
1135
0
      0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
1136
0
      0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
1137
0
      0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
1138
0
      0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
1139
0
      0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
1140
0
      0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
1141
0
      0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
1142
0
      0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
1143
0
      0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
1144
0
      0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
1145
0
      0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
1146
0
      0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
1147
0
      0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
1148
0
      0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
1149
0
      0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
1150
0
      0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
1151
0
      0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
1152
0
      0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
1153
0
      0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
1154
0
      0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
1155
0
      0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
1156
0
      0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
1157
0
      0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
1158
0
      0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
1159
0
      0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
1160
0
      0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
1161
0
      0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
1162
0
      0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
1163
0
      0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
1164
0
      0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
1165
0
      0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
1166
0
      0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
1167
0
      0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
1168
0
      0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
1169
0
      0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
1170
0
      0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
1171
0
      0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
1172
0
      0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
1173
0
      0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
1174
0
      0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
1175
0
      0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
1176
0
      0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
1177
0
      0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
1178
0
      0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
1179
0
      0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
1180
0
      0x2d02ef8d
1181
0
    };
1182
0
  const bfd_byte *end;
1183
1184
0
  crc = ~crc & 0xffffffff;
1185
0
  for (end = buf + len; buf < end; ++ buf)
1186
0
    crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
1187
0
  return ~crc & 0xffffffff;
1188
0
}
1189
1190
1191
/* Extracts the filename and CRC32 value for any separate debug
1192
   information file associated with @var{abfd}.
1193
1194
   The @var{crc32_out} parameter is an untyped pointer because
1195
   this routine is used as a @code{get_func_type} function, but it
1196
   is expected to be a uint32_t pointer.
1197
1198
   Returns the filename of the associated debug information file,
1199
   or NULL if there is no such file.  If the filename was found
1200
   then the contents of @var{crc32_out} are updated to hold the
1201
   corresponding CRC32 value for the file.
1202
1203
   The returned filename is allocated with @code{malloc}; freeing
1204
   it is the responsibility of the caller.  */
1205
1206
static char *
1207
bfd_get_debug_link_info_1 (bfd *abfd, void *crc32_out)
1208
3.59k
{
1209
3.59k
  asection *sect;
1210
3.59k
  uint32_t *crc32 = crc32_out;
1211
3.59k
  bfd_byte *contents;
1212
3.59k
  unsigned int crc_offset;
1213
3.59k
  char *name;
1214
3.59k
  bfd_size_type size;
1215
1216
3.59k
  BFD_ASSERT (abfd);
1217
3.59k
  BFD_ASSERT (crc32_out);
1218
1219
3.59k
  sect = bfd_get_section_by_name (abfd, GNU_DEBUGLINK);
1220
1221
3.59k
  if (sect == NULL || (sect->flags & SEC_HAS_CONTENTS) == 0)
1222
3.58k
    return NULL;
1223
1224
12
  size = bfd_section_size (sect);
1225
1226
  /* PR 22794: Make sure that the section has a reasonable size.  */
1227
12
  if (size < 8)
1228
2
    return NULL;
1229
1230
10
  if (!bfd_malloc_and_get_section (abfd, sect, &contents))
1231
1
    return NULL;
1232
1233
  /* CRC value is stored after the filename, aligned up to 4 bytes.  */
1234
9
  name = (char *) contents;
1235
  /* PR 17597: Avoid reading off the end of the buffer.  */
1236
9
  crc_offset = strnlen (name, size) + 1;
1237
9
  crc_offset = (crc_offset + 3) & ~3;
1238
9
  if (crc_offset + 4 > size)
1239
2
    {
1240
2
      free (name);
1241
2
      return NULL;
1242
2
    }
1243
1244
7
  *crc32 = bfd_get_32 (abfd, contents + crc_offset);
1245
7
  return name;
1246
9
}
1247
1248
1249
/*
1250
FUNCTION
1251
  bfd_get_debug_link_info
1252
1253
SYNOPSIS
1254
  char *bfd_get_debug_link_info (bfd *abfd, uint32_t *crc32_out);
1255
1256
DESCRIPTION
1257
  Extracts the filename and CRC32 value for any separate debug
1258
  information file associated with @var{abfd}.
1259
1260
  Returns the filename of the associated debug information file,
1261
  or NULL if there is no such file.  If the filename was found
1262
  then the contents of @var{crc32_out} are updated to hold the
1263
  corresponding CRC32 value for the file.
1264
1265
  The returned filename is allocated with @code{malloc}; freeing
1266
  it is the responsibility of the caller.
1267
*/
1268
1269
char *
1270
bfd_get_debug_link_info (bfd *abfd, uint32_t *crc32_out)
1271
0
{
1272
0
  return bfd_get_debug_link_info_1 (abfd, crc32_out);
1273
0
}
1274
1275
/*
1276
FUNCTION
1277
  bfd_get_alt_debug_link_info
1278
1279
SYNOPSIS
1280
  char *bfd_get_alt_debug_link_info (bfd * abfd,
1281
             bfd_size_type *buildid_len,
1282
             bfd_byte **buildid_out);
1283
1284
DESCRIPTION
1285
  Fetch the filename and BuildID value for any alternate debuginfo
1286
  associated with @var{abfd}.  Return NULL if no such info found,
1287
  otherwise return filename and update @var{buildid_len} and
1288
  @var{buildid_out}.  The returned filename and build_id are
1289
  allocated with @code{malloc}; freeing them is the responsibility
1290
  of the caller.
1291
*/
1292
1293
char *
1294
bfd_get_alt_debug_link_info (bfd * abfd, bfd_size_type *buildid_len,
1295
           bfd_byte **buildid_out)
1296
0
{
1297
0
  asection *sect;
1298
0
  bfd_byte *contents;
1299
0
  unsigned int buildid_offset;
1300
0
  char *name;
1301
0
  bfd_size_type size;
1302
1303
0
  BFD_ASSERT (abfd);
1304
0
  BFD_ASSERT (buildid_len);
1305
0
  BFD_ASSERT (buildid_out);
1306
1307
0
  sect = bfd_get_section_by_name (abfd, GNU_DEBUGALTLINK);
1308
1309
0
  if (sect == NULL || (sect->flags & SEC_HAS_CONTENTS) == 0)
1310
0
    return NULL;
1311
1312
0
  size = bfd_section_size (sect);
1313
0
  if (size < 8)
1314
0
    return NULL;
1315
1316
0
  if (!bfd_malloc_and_get_section (abfd, sect, & contents))
1317
0
    return NULL;
1318
1319
  /* BuildID value is stored after the filename.  */
1320
0
  name = (char *) contents;
1321
0
  buildid_offset = strnlen (name, size) + 1;
1322
0
  if (buildid_offset >= bfd_section_size (sect))
1323
0
    return NULL;
1324
1325
0
  *buildid_len = size - buildid_offset;
1326
0
  *buildid_out = bfd_malloc (*buildid_len);
1327
0
  memcpy (*buildid_out, contents + buildid_offset, *buildid_len);
1328
1329
0
  return name;
1330
0
}
1331
1332
/* Checks to see if @var{name} is a file and if its contents match
1333
   @var{crc32}, which is a pointer to a @code{uint32_t}
1334
   containing a CRC32.
1335
1336
   The @var{crc32_p} parameter is an untyped pointer because this
1337
   routine is used as a @code{check_func_type} function.  */
1338
1339
static bool
1340
separate_debug_file_exists (const char *name, void *crc32_p)
1341
25
{
1342
25
  unsigned char buffer[8 * 1024];
1343
25
  uint32_t file_crc = 0;
1344
25
  FILE *f;
1345
25
  bfd_size_type count;
1346
25
  uint32_t crc;
1347
1348
25
  BFD_ASSERT (name);
1349
25
  BFD_ASSERT (crc32_p);
1350
1351
25
  crc = *(uint32_t *) crc32_p;
1352
1353
25
  f = _bfd_real_fopen (name, FOPEN_RB);
1354
25
  if (f == NULL)
1355
25
    return false;
1356
1357
0
  while ((count = fread (buffer, 1, sizeof (buffer), f)) > 0)
1358
0
    file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count);
1359
1360
0
  fclose (f);
1361
1362
0
  return crc == file_crc;
1363
25
}
1364
1365
/* Checks to see if @var{name} is a file.  */
1366
1367
static bool
1368
separate_alt_debug_file_exists (const char *name, void *unused ATTRIBUTE_UNUSED)
1369
0
{
1370
0
  FILE *f;
1371
1372
0
  BFD_ASSERT (name);
1373
1374
0
  f = _bfd_real_fopen (name, FOPEN_RB);
1375
0
  if (f == NULL)
1376
0
    return false;
1377
1378
0
  fclose (f);
1379
1380
0
  return true;
1381
0
}
1382
1383
/* Searches for a debug information file corresponding to @var{abfd}.
1384
1385
   The name of the separate debug info file is returned by the
1386
   @var{get} function.  This function scans various fixed locations
1387
   in the filesystem, including the file tree rooted at @var{dir}.
1388
   If the @var{include_dirs} parameter is true then the directory
1389
   components of @var{abfd}'s filename will be included in the
1390
   searched locations.
1391
1392
   @var{data} is passed unmodified to the @var{get} and @var{check}
1393
   functions.  It is generally used to implement build-id-like
1394
   matching in the callback functions.
1395
1396
   Returns the filename of the first file to be found which
1397
   receives a TRUE result from the @var{check} function.
1398
   Returns NULL if no valid file could be found.  */
1399
1400
typedef char * (*get_func_type) (bfd *, void *);
1401
typedef bool (*check_func_type) (const char *, void *);
1402
1403
static char *
1404
find_separate_debug_file (bfd *abfd,
1405
        const char *debug_file_directory,
1406
        bool include_dirs,
1407
        get_func_type get_func,
1408
        check_func_type check_func,
1409
        void *func_data)
1410
7.18k
{
1411
7.18k
  char *base;
1412
7.18k
  char *dir;
1413
7.18k
  char *debugfile;
1414
7.18k
  char *canon_dir;
1415
7.18k
  size_t dirlen;
1416
7.18k
  size_t canon_dirlen;
1417
1418
7.18k
  BFD_ASSERT (abfd);
1419
7.18k
  if (debug_file_directory == NULL)
1420
0
    debug_file_directory = ".";
1421
1422
  /* BFD may have been opened from a stream.  */
1423
7.18k
  if (bfd_get_filename (abfd) == NULL)
1424
0
    {
1425
0
      bfd_set_error (bfd_error_invalid_operation);
1426
0
      return NULL;
1427
0
    }
1428
1429
7.18k
  base = get_func (abfd, func_data);
1430
1431
7.18k
  if (base == NULL)
1432
7.16k
    return NULL;
1433
1434
25
  if (base[0] == '\0')
1435
2
    {
1436
2
      free (base);
1437
2
      bfd_set_error (bfd_error_no_debug_section);
1438
2
      return NULL;
1439
2
    }
1440
1441
23
  if (include_dirs)
1442
5
    {
1443
5
      const char *fname = bfd_get_filename (abfd);
1444
66
      for (dirlen = strlen (fname); dirlen > 0; dirlen--)
1445
66
  if (IS_DIR_SEPARATOR (fname[dirlen - 1]))
1446
5
    break;
1447
1448
5
      dir = (char *) bfd_malloc (dirlen + 1);
1449
5
      if (dir == NULL)
1450
0
  {
1451
0
    free (base);
1452
0
    return NULL;
1453
0
  }
1454
5
      memcpy (dir, fname, dirlen);
1455
5
      dir[dirlen] = '\0';
1456
5
    }
1457
18
  else
1458
18
    {
1459
18
      dir = (char *) bfd_malloc (1);
1460
18
      * dir = 0;
1461
18
      dirlen = 0;
1462
18
    }
1463
1464
  /* Compute the canonical name of the bfd object with all symbolic links
1465
     resolved, for use in the global debugfile directory.  */
1466
23
  canon_dir = lrealpath (bfd_get_filename (abfd));
1467
310
  for (canon_dirlen = strlen (canon_dir); canon_dirlen > 0; canon_dirlen--)
1468
310
    if (IS_DIR_SEPARATOR (canon_dir[canon_dirlen - 1]))
1469
23
      break;
1470
23
  canon_dir[canon_dirlen] = '\0';
1471
1472
23
#ifndef EXTRA_DEBUG_ROOT1
1473
46
#define EXTRA_DEBUG_ROOT1 "/usr/lib/debug"
1474
23
#endif
1475
23
#ifndef EXTRA_DEBUG_ROOT2
1476
46
#define EXTRA_DEBUG_ROOT2 "/usr/lib/debug/usr"
1477
23
#endif
1478
1479
23
  debugfile = (char *)
1480
23
      bfd_malloc (strlen (debug_file_directory) + 1
1481
23
      + (canon_dirlen > dirlen ? canon_dirlen : dirlen)
1482
23
      + strlen (".debug/")
1483
23
#ifdef EXTRA_DEBUG_ROOT1
1484
23
      + strlen (EXTRA_DEBUG_ROOT1)
1485
23
#endif
1486
23
#ifdef EXTRA_DEBUG_ROOT2
1487
23
      + strlen (EXTRA_DEBUG_ROOT2)
1488
23
#endif
1489
23
      + strlen (base)
1490
23
      + 1);
1491
23
  if (debugfile == NULL)
1492
0
    goto found; /* Actually this returns NULL.  */
1493
1494
  /* First try in the same directory as the original file.
1495
1496
     FIXME: Strictly speaking if we are using the build-id method,
1497
     (ie include_dirs == FALSE) then we should only check absolute
1498
     paths, not relative ones like this one (and the next one).
1499
     The check is left in however as this allows the binutils
1500
     testsuite to exercise this feature without having to install
1501
     a file into the root filesystem.  (See binutils/testsuite/
1502
     binutils-all/objdump.exp for the test).  */
1503
23
  sprintf (debugfile, "%s%s", dir, base);
1504
23
  if (check_func (debugfile, func_data))
1505
0
    goto found;
1506
1507
  /* Then try in a subdirectory called .debug.  */
1508
23
  sprintf (debugfile, "%s.debug/%s", dir, base);
1509
23
  if (check_func (debugfile, func_data))
1510
0
    goto found;
1511
1512
23
#ifdef EXTRA_DEBUG_ROOT1
1513
  /* Try the first extra debug file root.  */
1514
23
  sprintf (debugfile, "%s%s%s", EXTRA_DEBUG_ROOT1,
1515
23
     include_dirs ? canon_dir : "/", base);
1516
23
  if (check_func (debugfile, func_data))
1517
0
    goto found;
1518
23
#endif
1519
1520
23
#ifdef EXTRA_DEBUG_ROOT2
1521
  /* Try the second extra debug file root.  */
1522
23
  sprintf (debugfile, "%s%s%s", EXTRA_DEBUG_ROOT2,
1523
23
     include_dirs ? canon_dir : "/", base);
1524
23
  if (check_func (debugfile, func_data))
1525
0
    goto found;
1526
23
#endif
1527
1528
  /* Then try in the global debugfile directory.  */
1529
23
  strcpy (debugfile, debug_file_directory);
1530
23
  dirlen = strlen (debug_file_directory) - 1;
1531
23
  if (include_dirs)
1532
5
    {
1533
5
      if (dirlen > 0
1534
5
    && debug_file_directory[dirlen] != '/'
1535
5
    && canon_dir[0] != '/')
1536
0
  strcat (debugfile, "/");
1537
5
      strcat (debugfile, canon_dir);
1538
5
    }
1539
18
  else
1540
18
    {
1541
18
      if (dirlen > 0 && debug_file_directory[dirlen] != '/')
1542
18
  strcat (debugfile, "/");
1543
18
    }
1544
23
  strcat (debugfile, base);
1545
1546
23
  if (check_func (debugfile, func_data))
1547
0
    goto found;
1548
1549
  /* Failed to find the file.  */
1550
23
  free (debugfile);
1551
23
  debugfile = NULL;
1552
1553
23
 found:
1554
23
  free (base);
1555
23
  free (dir);
1556
23
  free (canon_dir);
1557
23
  return debugfile;
1558
23
}
1559
1560
/*
1561
FUNCTION
1562
  bfd_follow_gnu_debuglink
1563
1564
SYNOPSIS
1565
  char *bfd_follow_gnu_debuglink (bfd *abfd, const char *dir);
1566
1567
DESCRIPTION
1568
  Takes a BFD and searches it for a .gnu_debuglink section.  If this
1569
  section is found, it examines the section for the name and checksum
1570
  of a '.debug' file containing auxiliary debugging information.  It
1571
  then searches the filesystem for this .debug file in some standard
1572
  locations, including the directory tree rooted at @var{dir}, and if
1573
  found returns the full filename.
1574
1575
  If @var{dir} is NULL, the search will take place starting at
1576
  the current directory.
1577
1578
  Returns <<NULL>> on any errors or failure to locate the .debug
1579
  file, otherwise a pointer to a heap-allocated string
1580
  containing the filename.  The caller is responsible for
1581
  freeing this string.
1582
*/
1583
1584
char *
1585
bfd_follow_gnu_debuglink (bfd *abfd, const char *dir)
1586
3.59k
{
1587
3.59k
  uint32_t crc32;
1588
1589
3.59k
  return find_separate_debug_file (abfd, dir, true,
1590
3.59k
           bfd_get_debug_link_info_1,
1591
3.59k
           separate_debug_file_exists, &crc32);
1592
3.59k
}
1593
1594
/* Helper for bfd_follow_gnu_debugaltlink.  It just returns the name
1595
   of the separate debug file.  */
1596
1597
static char *
1598
get_alt_debug_link_info_shim (bfd * abfd, void *unused ATTRIBUTE_UNUSED)
1599
0
{
1600
0
  bfd_size_type len;
1601
0
  bfd_byte *buildid = NULL;
1602
0
  char *result = bfd_get_alt_debug_link_info (abfd, &len, &buildid);
1603
1604
0
  free (buildid);
1605
1606
0
  return result;
1607
0
}
1608
1609
/*
1610
FUNCTION
1611
  bfd_follow_gnu_debugaltlink
1612
1613
SYNOPSIS
1614
  char *bfd_follow_gnu_debugaltlink (bfd *abfd, const char *dir);
1615
1616
DESCRIPTION
1617
  Takes a BFD and searches it for a .gnu_debugaltlink section.  If this
1618
  section is found, it examines the section for the name of a file
1619
  containing auxiliary debugging information.  It then searches the
1620
  filesystem for this file in a set of standard locations, including
1621
  the directory tree rooted at @var{dir}, and if found returns the
1622
  full filename.
1623
1624
  If @var{dir} is NULL, the search will take place starting at
1625
  the current directory.
1626
1627
  Returns <<NULL>> on any errors or failure to locate the debug
1628
  file, otherwise a pointer to a heap-allocated string
1629
  containing the filename.  The caller is responsible for
1630
  freeing this string.
1631
*/
1632
1633
char *
1634
bfd_follow_gnu_debugaltlink (bfd *abfd, const char *dir)
1635
0
{
1636
0
  return find_separate_debug_file (abfd, dir, true,
1637
0
           get_alt_debug_link_info_shim,
1638
0
           separate_alt_debug_file_exists,
1639
0
           NULL);
1640
0
}
1641
1642
/*
1643
FUNCTION
1644
  bfd_create_gnu_debuglink_section
1645
1646
SYNOPSIS
1647
  struct bfd_section *bfd_create_gnu_debuglink_section
1648
    (bfd *abfd, const char *filename);
1649
1650
DESCRIPTION
1651
  Takes a @var{BFD} and adds a .gnu_debuglink section to it.  The
1652
  section is sized to be big enough to contain a link to the specified
1653
  @var{filename}.
1654
1655
  A pointer to the new section is returned if all is ok.  Otherwise
1656
  <<NULL>> is returned and bfd_error is set.
1657
*/
1658
1659
asection *
1660
bfd_create_gnu_debuglink_section (bfd *abfd, const char *filename)
1661
0
{
1662
0
  asection *sect;
1663
0
  bfd_size_type debuglink_size;
1664
0
  flagword flags;
1665
1666
0
  if (abfd == NULL || filename == NULL)
1667
0
    {
1668
0
      bfd_set_error (bfd_error_invalid_operation);
1669
0
      return NULL;
1670
0
    }
1671
1672
  /* Strip off any path components in filename.  */
1673
0
  filename = lbasename (filename);
1674
1675
0
  sect = bfd_get_section_by_name (abfd, GNU_DEBUGLINK);
1676
0
  if (sect)
1677
0
    {
1678
      /* Section already exists.  */
1679
0
      bfd_set_error (bfd_error_invalid_operation);
1680
0
      return NULL;
1681
0
    }
1682
1683
0
  flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
1684
0
  sect = bfd_make_section_with_flags (abfd, GNU_DEBUGLINK, flags);
1685
0
  if (sect == NULL)
1686
0
    return NULL;
1687
1688
  /* Compute the size of the section.  Allow for the CRC after the filename,
1689
     and padding so that it will start on a 4-byte boundary.  */
1690
0
  debuglink_size = strlen (filename) + 1;
1691
0
  debuglink_size += 3;
1692
0
  debuglink_size &= ~3;
1693
0
  debuglink_size += 4;
1694
1695
0
  if (!bfd_set_section_size (sect, debuglink_size))
1696
    /* XXX Should we delete the section from the bfd ?  */
1697
0
    return NULL;
1698
1699
  /* PR 21193: Ensure that the section has 4-byte alignment for the CRC.
1700
     Note - despite the name of the function being called, we are
1701
     setting an alignment power, not a byte alignment value.  */
1702
0
  bfd_set_section_alignment (sect, 2);
1703
1704
0
  return sect;
1705
0
}
1706
1707
1708
/*
1709
FUNCTION
1710
  bfd_fill_in_gnu_debuglink_section
1711
1712
SYNOPSIS
1713
  bool bfd_fill_in_gnu_debuglink_section
1714
    (bfd *abfd, struct bfd_section *sect, const char *filename);
1715
1716
DESCRIPTION
1717
  Takes a @var{BFD} and containing a .gnu_debuglink section @var{SECT}
1718
  and fills in the contents of the section to contain a link to the
1719
  specified @var{filename}.  The filename should be absolute or
1720
  relative to the current directory.
1721
1722
  <<TRUE>> is returned if all is ok.  Otherwise <<FALSE>> is returned
1723
  and bfd_error is set.
1724
*/
1725
1726
bool
1727
bfd_fill_in_gnu_debuglink_section (bfd *abfd,
1728
           struct bfd_section *sect,
1729
           const char *filename)
1730
0
{
1731
0
  bfd_size_type debuglink_size;
1732
0
  uint32_t crc32;
1733
0
  char * contents;
1734
0
  bfd_size_type crc_offset;
1735
0
  FILE * handle;
1736
0
  unsigned char buffer[8 * 1024];
1737
0
  size_t count;
1738
0
  size_t filelen;
1739
1740
0
  if (abfd == NULL || sect == NULL || filename == NULL)
1741
0
    {
1742
0
      bfd_set_error (bfd_error_invalid_operation);
1743
0
      return false;
1744
0
    }
1745
1746
  /* Open the linked file so that we can compute a CRC.  */
1747
0
  handle = _bfd_real_fopen (filename, FOPEN_RB);
1748
0
  if (handle == NULL)
1749
0
    {
1750
0
      bfd_set_error (bfd_error_system_call);
1751
0
      return false;
1752
0
    }
1753
1754
0
  crc32 = 0;
1755
0
  while ((count = fread (buffer, 1, sizeof buffer, handle)) > 0)
1756
0
    crc32 = bfd_calc_gnu_debuglink_crc32 (crc32, buffer, count);
1757
0
  fclose (handle);
1758
1759
  /* Strip off any path components in filename,
1760
     now that we no longer need them.  */
1761
0
  filename = lbasename (filename);
1762
1763
0
  filelen = strlen (filename);
1764
0
  debuglink_size = filelen + 1;
1765
0
  debuglink_size += 3;
1766
0
  debuglink_size &= ~3;
1767
0
  debuglink_size += 4;
1768
1769
0
  contents = bfd_malloc (debuglink_size);
1770
0
  if (contents == NULL)
1771
0
    {
1772
      /* XXX Should we delete the section from the bfd ?  */
1773
0
      return false;
1774
0
    }
1775
1776
0
  crc_offset = debuglink_size - 4;
1777
0
  memcpy (contents, filename, filelen);
1778
0
  memset (contents + filelen, 0, crc_offset - filelen);
1779
1780
0
  bfd_put_32 (abfd, crc32, contents + crc_offset);
1781
1782
0
  bool ret = bfd_set_section_contents (abfd, sect, contents, 0, debuglink_size);
1783
0
  free (contents);
1784
0
  return ret;
1785
0
}
1786
1787
/* Finds the build-id associated with @var{abfd}.  If the build-id is
1788
   extracted from the note section then a build-id structure is built
1789
   for it, using memory allocated to @var{abfd}, and this is then
1790
   attached to the @var{abfd}.
1791
1792
   Returns a pointer to the build-id structure if a build-id could be
1793
   found.  If no build-id is found NULL is returned and error code is
1794
   set.  */
1795
1796
static struct bfd_build_id *
1797
get_build_id (bfd *abfd)
1798
3.59k
{
1799
3.59k
  struct bfd_build_id *build_id;
1800
3.59k
  Elf_Internal_Note inote;
1801
3.59k
  Elf_External_Note *enote;
1802
3.59k
  bfd_byte *contents;
1803
3.59k
  asection *sect;
1804
3.59k
  bfd_size_type size;
1805
1806
3.59k
  BFD_ASSERT (abfd);
1807
1808
3.59k
  if (abfd->build_id && abfd->build_id->size > 0)
1809
    /* Save some time by using the already computed build-id.  */
1810
17
    return (struct bfd_build_id *) abfd->build_id;
1811
1812
3.57k
  sect = bfd_get_section_by_name (abfd, ".note.gnu.build-id");
1813
3.57k
  if (sect == NULL
1814
8
      || (sect->flags & SEC_HAS_CONTENTS) == 0)
1815
3.56k
    {
1816
3.56k
      bfd_set_error (bfd_error_no_debug_section);
1817
3.56k
      return NULL;
1818
3.56k
    }
1819
1820
8
  size = bfd_section_size (sect);
1821
  /* FIXME: Should we support smaller build-id notes ?  */
1822
8
  if (size < 0x24)
1823
1
    {
1824
1
      bfd_set_error (bfd_error_invalid_operation);
1825
1
      return NULL;
1826
1
    }
1827
1828
7
  if (!bfd_malloc_and_get_section (abfd, sect, & contents))
1829
0
    return NULL;
1830
1831
  /* FIXME: Paranoia - allow for compressed build-id sections.
1832
     Maybe we should complain if this size is different from
1833
     the one obtained above...  */
1834
7
  size = bfd_section_size (sect);
1835
7
  if (size < sizeof (Elf_External_Note))
1836
0
    {
1837
0
      bfd_set_error (bfd_error_invalid_operation);
1838
0
      free (contents);
1839
0
      return NULL;
1840
0
    }
1841
1842
7
  enote = (Elf_External_Note *) contents;
1843
7
  inote.type = H_GET_32 (abfd, enote->type);
1844
7
  inote.namesz = H_GET_32 (abfd, enote->namesz);
1845
7
  inote.namedata = enote->name;
1846
7
  inote.descsz = H_GET_32 (abfd, enote->descsz);
1847
7
  inote.descdata = inote.namedata + BFD_ALIGN (inote.namesz, 4);
1848
  /* FIXME: Should we check for extra notes in this section ?  */
1849
1850
7
  if (inote.descsz <= 0
1851
6
      || inote.type != NT_GNU_BUILD_ID
1852
4
      || inote.namesz != 4 /* sizeof "GNU"  */
1853
3
      || !startswith (inote.namedata, "GNU")
1854
2
      || inote.descsz > 0x7ffffffe
1855
2
      || size < (12 + BFD_ALIGN (inote.namesz, 4) + inote.descsz))
1856
6
    {
1857
6
      free (contents);
1858
6
      bfd_set_error (bfd_error_invalid_operation);
1859
6
      return NULL;
1860
6
    }
1861
1862
1
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) + inote.descsz);
1863
1
  if (build_id == NULL)
1864
0
    {
1865
0
      free (contents);
1866
0
      return NULL;
1867
0
    }
1868
1869
1
  build_id->size = inote.descsz;
1870
1
  memcpy (build_id->data, inote.descdata, inote.descsz);
1871
1
  abfd->build_id = build_id;
1872
1
  free (contents);
1873
1874
1
  return build_id;
1875
1
}
1876
1877
/* Searches @var{abfd} for a build-id, and then constructs a pathname
1878
   from it.  The path is computed as .build-id/NN/NN+NN.debug where
1879
   NNNN+NN is the build-id value as a hexadecimal string.
1880
1881
   Returns the constructed filename or NULL upon error.  It is the
1882
   caller's responsibility to free the memory used to hold the
1883
   filename.  If a filename is returned then the @var{build_id_out_p}
1884
   parameter (which points to a @code{struct bfd_build_id} pointer) is
1885
   set to a pointer to the build_id structure.  */
1886
1887
static char *
1888
get_build_id_name (bfd *abfd, void *build_id_out_p)
1889
3.59k
{
1890
3.59k
  struct bfd_build_id **build_id_out = build_id_out_p;
1891
3.59k
  struct bfd_build_id *build_id;
1892
3.59k
  char *name;
1893
3.59k
  char *n;
1894
3.59k
  bfd_size_type s;
1895
3.59k
  bfd_byte *d;
1896
1897
3.59k
  if (abfd == NULL || bfd_get_filename (abfd) == NULL || build_id_out == NULL)
1898
0
    {
1899
0
      bfd_set_error (bfd_error_invalid_operation);
1900
0
      return NULL;
1901
0
    }
1902
1903
3.59k
  build_id = get_build_id (abfd);
1904
3.59k
  if (build_id == NULL)
1905
3.57k
    return NULL;
1906
1907
  /* Compute the debug pathname corresponding to the build-id.  */
1908
18
  name = bfd_malloc (strlen (".build-id/") + build_id->size * 2 + 2 + strlen (".debug"));
1909
18
  if (name == NULL)
1910
0
    {
1911
0
      bfd_set_error (bfd_error_no_memory);
1912
0
      return NULL;
1913
0
    }
1914
18
  n = name;
1915
18
  d = build_id->data;
1916
18
  s = build_id->size;
1917
1918
18
  n += sprintf (n, ".build-id/");
1919
18
  n += sprintf (n, "%02x", (unsigned) *d++); s--;
1920
18
  n += sprintf (n, "/");
1921
337
  while (s--)
1922
319
    n += sprintf (n, "%02x", (unsigned) *d++);
1923
18
  n += sprintf (n, ".debug");
1924
1925
18
  *build_id_out = build_id;
1926
18
  return name;
1927
18
}
1928
1929
/* Checks to see if @var{name} is a readable file and if its build-id
1930
   matches @var{buildid}.
1931
1932
   Returns TRUE if the file exists, is readable, and contains a
1933
   build-id which matches the build-id pointed at by @var{build_id_p}
1934
   (which is really a @code{struct bfd_build_id **}).  */
1935
1936
static bool
1937
check_build_id_file (const char *name, void *buildid_p)
1938
90
{
1939
90
  struct bfd_build_id *orig_build_id;
1940
90
  struct bfd_build_id *build_id;
1941
90
  bfd * file;
1942
90
  bool result;
1943
1944
90
  BFD_ASSERT (name);
1945
90
  BFD_ASSERT (buildid_p);
1946
1947
90
  file = bfd_openr (name, NULL);
1948
90
  if (file == NULL)
1949
90
    return false;
1950
1951
  /* If the file is an archive, process all of its elements.  */
1952
0
  if (! bfd_check_format (file, bfd_object))
1953
0
    {
1954
0
      bfd_close (file);
1955
0
      return false;
1956
0
    }
1957
1958
0
  build_id = get_build_id (file);
1959
0
  if (build_id == NULL)
1960
0
    {
1961
0
      bfd_close (file);
1962
0
      return false;
1963
0
    }
1964
1965
0
  orig_build_id = *(struct bfd_build_id **) buildid_p;
1966
1967
0
  result = build_id->size == orig_build_id->size
1968
0
    && memcmp (build_id->data, orig_build_id->data, build_id->size) == 0;
1969
1970
0
  (void) bfd_close (file);
1971
1972
0
  return result;
1973
0
}
1974
1975
/*
1976
FUNCTION
1977
  bfd_follow_build_id_debuglink
1978
1979
SYNOPSIS
1980
  char *bfd_follow_build_id_debuglink (bfd *abfd, const char *dir);
1981
1982
DESCRIPTION
1983
  Takes @var{abfd} and searches it for a .note.gnu.build-id section.
1984
  If this section is found, it extracts the value of the NT_GNU_BUILD_ID
1985
  note, which should be a hexadecimal value @var{NNNN+NN} (for
1986
  32+ hex digits).  It then searches the filesystem for a file named
1987
  @var{.build-id/NN/NN+NN.debug} in a set of standard locations,
1988
  including the directory tree rooted at @var{dir}.  The filename
1989
  of the first matching file to be found is returned.  A matching
1990
  file should contain a .note.gnu.build-id section with the same
1991
  @var{NNNN+NN} note as @var{abfd}, although this check is currently
1992
  not implemented.
1993
1994
  If @var{dir} is NULL, the search will take place starting at
1995
  the current directory.
1996
1997
  Returns <<NULL>> on any errors or failure to locate the debug
1998
  file, otherwise a pointer to a heap-allocated string
1999
  containing the filename.  The caller is responsible for
2000
  freeing this string.
2001
*/
2002
2003
char *
2004
bfd_follow_build_id_debuglink (bfd *abfd, const char *dir)
2005
3.59k
{
2006
3.59k
  struct bfd_build_id *build_id;
2007
2008
3.59k
  return find_separate_debug_file (abfd, dir, false,
2009
3.59k
           get_build_id_name,
2010
3.59k
           check_build_id_file, &build_id);
2011
3.59k
}
2012
2013
/*
2014
FUNCTION
2015
  bfd_set_filename
2016
2017
SYNOPSIS
2018
  const char *bfd_set_filename (bfd *abfd, const char *filename);
2019
2020
DESCRIPTION
2021
  Set the filename of @var{abfd}, copying the FILENAME parameter to
2022
  bfd_alloc'd memory owned by @var{abfd}.  Returns a pointer the
2023
  newly allocated name, or NULL if the allocation failed.
2024
*/
2025
2026
const char *
2027
bfd_set_filename (bfd *abfd, const char *filename)
2028
10.0M
{
2029
10.0M
  size_t len = strlen (filename) + 1;
2030
10.0M
  char *n = bfd_alloc (abfd, len);
2031
2032
10.0M
  if (n == NULL)
2033
0
    return NULL;
2034
2035
10.0M
  if (abfd->filename != NULL)
2036
0
    {
2037
      /* PR 29389.  If we attempt to rename a file that has been closed due
2038
   to caching, then we will not be able to reopen it later on.  */
2039
0
      if (abfd->iostream == NULL && (abfd->flags & BFD_CLOSED_BY_CACHE))
2040
0
  {
2041
0
    bfd_set_error (bfd_error_invalid_operation);
2042
0
    return NULL;
2043
0
  }
2044
2045
      /* Similarly if we attempt to close a renamed file because the
2046
   cache is now full, we will not be able to reopen it later on.  */
2047
0
      if (abfd->iostream != NULL)
2048
0
  abfd->cacheable = 0;
2049
0
    }
2050
2051
10.0M
  memcpy (n, filename, len);
2052
10.0M
  abfd->filename = n;
2053
2054
10.0M
  return n;
2055
10.0M
}
2056
2057
/*
2058
FUNCTION
2059
  bfd_extract_object_only_section
2060
2061
SYNOPSIS
2062
  const char *bfd_extract_object_only_section
2063
    (bfd *abfd);
2064
2065
DESCRIPTION
2066
2067
  Takes a @var{ABFD} and extract the .gnu_object_only section into
2068
  a temporary file.
2069
2070
RETURNS
2071
  The name of the temporary file is returned if all is ok.
2072
  Otherwise <<NULL>> is returned and bfd_error is set.
2073
*/
2074
2075
const char *
2076
bfd_extract_object_only_section (bfd *abfd)
2077
0
{
2078
0
  asection *sec = abfd->object_only_section;
2079
0
  const char *name;
2080
0
  FILE *file;
2081
0
  bfd_byte *memhunk = NULL;
2082
0
  size_t off, size;
2083
0
  bfd_error_type err;
2084
2085
  /* Get a temporary object-only file.  */
2086
0
  name = make_temp_file (".obj-only.o");
2087
2088
  /* Open the object-only file.  */
2089
0
  file = _bfd_real_fopen (name, FOPEN_WB);
2090
0
  if (!bfd_get_full_section_contents (abfd, sec, &memhunk))
2091
0
    {
2092
0
      err = bfd_get_error ();
2093
2094
0
loser:
2095
0
      free (memhunk);
2096
0
      fclose (file);
2097
0
      unlink (name);
2098
0
      bfd_set_error (err);
2099
0
      return NULL;
2100
0
    }
2101
2102
0
  off = 0;
2103
0
  size = sec->size;
2104
0
  while (off != size)
2105
0
    {
2106
0
      size_t written, nwrite = size - off;
2107
2108
0
      written = fwrite (memhunk + off, 1, nwrite, file);
2109
0
      if (written < nwrite && ferror (file))
2110
0
  {
2111
0
    err = bfd_error_system_call;
2112
0
    goto loser;
2113
0
  }
2114
2115
0
      off += written;
2116
0
    }
2117
2118
0
  free (memhunk);
2119
0
  fclose (file);
2120
0
  return name;
2121
0
}