Subversion Repositories Kolibri OS

Rev

Rev 5199 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5197 serge 1
/* BFD library -- caching of file descriptors.
2
 
6324 serge 3
   Copyright (C) 1990-2015 Free Software Foundation, Inc.
5197 serge 4
 
5
   Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
6
 
7
   This file is part of BFD, the Binary File Descriptor library.
8
 
9
   This program is free software; you can redistribute it and/or modify
10
   it under the terms of the GNU General Public License as published by
11
   the Free Software Foundation; either version 3 of the License, or
12
   (at your option) any later version.
13
 
14
   This program is distributed in the hope that it will be useful,
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
   GNU General Public License for more details.
18
 
19
   You should have received a copy of the GNU General Public License
20
   along with this program; if not, write to the Free Software
21
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22
   MA 02110-1301, USA.  */
23
 
24
/*
25
SECTION
26
	File caching
27
 
28
	The file caching mechanism is embedded within BFD and allows
29
	the application to open as many BFDs as it wants without
30
	regard to the underlying operating system's file descriptor
31
	limit (often as low as 20 open files).  The module in
32
	<> maintains a least recently used list of
33
	<> files, and exports the name
34
	<>, which runs around and makes sure that
35
	the required BFD is open. If not, then it chooses a file to
36
	close, closes it and opens the one wanted, returning its file
37
	handle.
38
 
39
SUBSECTION
40
	Caching functions
41
*/
42
 
43
#include "sysdep.h"
44
#include "bfd.h"
45
#include "libbfd.h"
46
#include "libiberty.h"
47
#include "bfd_stdint.h"
48
 
49
#ifdef HAVE_MMAP
50
#include 
51
#endif
52
 
53
/* In some cases we can optimize cache operation when reopening files.
54
   For instance, a flush is entirely unnecessary if the file is already
55
   closed, so a flush would use CACHE_NO_OPEN.  Similarly, a seek using
56
   SEEK_SET or SEEK_END need not first seek to the current position.
57
   For stat we ignore seek errors, just in case the file has changed
58
   while we weren't looking.  If it has, then it's possible that the
59
   file is shorter and we don't want a seek error to prevent us doing
60
   the stat.  */
61
enum cache_flag {
62
  CACHE_NORMAL = 0,
63
  CACHE_NO_OPEN = 1,
64
  CACHE_NO_SEEK = 2,
65
  CACHE_NO_SEEK_ERROR = 4
66
};
67
 
68
/* The maximum number of files which the cache will keep open at
69
   one time.  When needed call bfd_cache_max_open to initialize.  */
70
 
71
static int max_open_files = 0;
72
 
73
/* Set max_open_files, if not already set, to 12.5% of the allowed open
74
   file descriptors, but at least 10, and return the value.  */
75
static int
76
bfd_cache_max_open (void)
77
{
78
 
5199 serge 79
  max_open_files = 16;
80
 
5197 serge 81
  return max_open_files;
82
}
83
 
84
/* The number of BFD files we have open.  */
85
 
86
static int open_files;
87
 
88
/* Zero, or a pointer to the topmost BFD on the chain.  This is
89
   used by the <> macro in @file{libbfd.h} to
90
   determine when it can avoid a function call.  */
91
 
92
static bfd *bfd_last_cache = NULL;
93
 
94
/* Insert a BFD into the cache.  */
95
 
96
static void
97
insert (bfd *abfd)
98
{
99
  if (bfd_last_cache == NULL)
100
    {
101
      abfd->lru_next = abfd;
102
      abfd->lru_prev = abfd;
103
    }
104
  else
105
    {
106
      abfd->lru_next = bfd_last_cache;
107
      abfd->lru_prev = bfd_last_cache->lru_prev;
108
      abfd->lru_prev->lru_next = abfd;
109
      abfd->lru_next->lru_prev = abfd;
110
    }
111
  bfd_last_cache = abfd;
112
}
113
 
114
/* Remove a BFD from the cache.  */
115
 
116
static void
117
snip (bfd *abfd)
118
{
119
  abfd->lru_prev->lru_next = abfd->lru_next;
120
  abfd->lru_next->lru_prev = abfd->lru_prev;
121
  if (abfd == bfd_last_cache)
122
    {
123
      bfd_last_cache = abfd->lru_next;
124
      if (abfd == bfd_last_cache)
125
	bfd_last_cache = NULL;
126
    }
127
}
128
 
129
/* Close a BFD and remove it from the cache.  */
130
 
131
static bfd_boolean
132
bfd_cache_delete (bfd *abfd)
133
{
134
  bfd_boolean ret;
135
 
136
  if (fclose ((FILE *) abfd->iostream) == 0)
137
    ret = TRUE;
138
  else
139
    {
140
      ret = FALSE;
141
      bfd_set_error (bfd_error_system_call);
142
    }
143
 
144
  snip (abfd);
145
 
146
  abfd->iostream = NULL;
147
  --open_files;
148
 
149
  return ret;
150
}
151
 
152
/* We need to open a new file, and the cache is full.  Find the least
153
   recently used cacheable BFD and close it.  */
154
 
155
static bfd_boolean
156
close_one (void)
157
{
158
  register bfd *to_kill;
159
 
160
  if (bfd_last_cache == NULL)
161
    to_kill = NULL;
162
  else
163
    {
164
      for (to_kill = bfd_last_cache->lru_prev;
165
	   ! to_kill->cacheable;
166
	   to_kill = to_kill->lru_prev)
167
	{
168
	  if (to_kill == bfd_last_cache)
169
	    {
170
	      to_kill = NULL;
171
	      break;
172
	    }
173
	}
174
    }
175
 
176
  if (to_kill == NULL)
177
    {
178
      /* There are no open cacheable BFD's.  */
179
      return TRUE;
180
    }
181
 
182
  to_kill->where = real_ftell ((FILE *) to_kill->iostream);
183
 
184
  return bfd_cache_delete (to_kill);
185
}
186
 
187
/* Check to see if the required BFD is the same as the last one
188
   looked up. If so, then it can use the stream in the BFD with
189
   impunity, since it can't have changed since the last lookup;
190
   otherwise, it has to perform the complicated lookup function.  */
191
 
192
#define bfd_cache_lookup(x, flag) \
193
  ((x) == bfd_last_cache			\
194
   ? (FILE *) (bfd_last_cache->iostream)	\
195
   : bfd_cache_lookup_worker (x, flag))
196
 
197
/* Called when the macro <> fails to find a
198
   quick answer.  Find a file descriptor for @var{abfd}.  If
199
   necessary, it open it.  If there are already more than
200
   <> files open, it tries to close one first, to
201
   avoid running out of file descriptors.  It will return NULL
202
   if it is unable to (re)open the @var{abfd}.  */
203
 
204
static FILE *
205
bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
206
{
207
  bfd *orig_bfd = abfd;
208
  if ((abfd->flags & BFD_IN_MEMORY) != 0)
209
    abort ();
210
 
211
  while (abfd->my_archive)
212
    abfd = abfd->my_archive;
213
 
214
  if (abfd->iostream != NULL)
215
    {
216
      /* Move the file to the start of the cache.  */
217
      if (abfd != bfd_last_cache)
218
	{
219
	  snip (abfd);
220
	  insert (abfd);
221
	}
222
      return (FILE *) abfd->iostream;
223
    }
224
 
225
  if (flag & CACHE_NO_OPEN)
226
    return NULL;
227
 
228
  if (bfd_open_file (abfd) == NULL)
229
    ;
230
  else if (!(flag & CACHE_NO_SEEK)
231
	   && real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0
232
	   && !(flag & CACHE_NO_SEEK_ERROR))
233
    bfd_set_error (bfd_error_system_call);
234
  else
235
    return (FILE *) abfd->iostream;
236
 
237
  (*_bfd_error_handler) (_("reopening %B: %s\n"),
238
			 orig_bfd, bfd_errmsg (bfd_get_error ()));
239
  return NULL;
240
}
241
 
242
static file_ptr
243
cache_btell (struct bfd *abfd)
244
{
245
  FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
246
  if (f == NULL)
247
    return abfd->where;
248
  return real_ftell (f);
249
}
250
 
251
static int
252
cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
253
{
254
  FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL);
255
  if (f == NULL)
256
    return -1;
257
  return real_fseek (f, offset, whence);
258
}
259
 
260
/* Note that archive entries don't have streams; they share their parent's.
261
   This allows someone to play with the iostream behind BFD's back.
262
 
263
   Also, note that the origin pointer points to the beginning of a file's
264
   contents (0 for non-archive elements).  For archive entries this is the
265
   first octet in the file, NOT the beginning of the archive header.  */
266
 
267
static file_ptr
268
cache_bread_1 (struct bfd *abfd, void *buf, file_ptr nbytes)
269
{
270
  FILE *f;
271
  file_ptr nread;
272
  /* FIXME - this looks like an optimization, but it's really to cover
273
     up for a feature of some OSs (not solaris - sigh) that
274
     ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
275
     internally and tries to link against them.  BFD seems to be smart
276
     enough to realize there are no symbol records in the "file" that
277
     doesn't exist but attempts to read them anyway.  On Solaris,
278
     attempting to read zero bytes from a NULL file results in a core
279
     dump, but on other platforms it just returns zero bytes read.
280
     This makes it to something reasonable. - DJ */
281
  if (nbytes == 0)
282
    return 0;
283
 
284
  f = bfd_cache_lookup (abfd, CACHE_NORMAL);
285
  if (f == NULL)
286
    return 0;
287
 
288
#if defined (__VAX) && defined (VMS)
289
  /* Apparently fread on Vax VMS does not keep the record length
290
     information.  */
291
  nread = read (fileno (f), buf, nbytes);
292
  /* Set bfd_error if we did not read as much data as we expected.  If
293
     the read failed due to an error set the bfd_error_system_call,
294
     else set bfd_error_file_truncated.  */
295
  if (nread == (file_ptr)-1)
296
    {
297
      bfd_set_error (bfd_error_system_call);
6324 serge 298
      return nread;
5197 serge 299
    }
300
#else
301
  nread = fread (buf, 1, nbytes, f);
302
  /* Set bfd_error if we did not read as much data as we expected.  If
303
     the read failed due to an error set the bfd_error_system_call,
304
     else set bfd_error_file_truncated.  */
305
  if (nread < nbytes && ferror (f))
306
    {
307
      bfd_set_error (bfd_error_system_call);
6324 serge 308
      return nread;
5197 serge 309
    }
310
#endif
311
  if (nread < nbytes)
312
    /* This may or may not be an error, but in case the calling code
313
       bails out because of it, set the right error code.  */
314
    bfd_set_error (bfd_error_file_truncated);
315
  return nread;
316
}
317
 
318
static file_ptr
319
cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
320
{
321
  file_ptr nread = 0;
322
 
323
  /* Some filesystems are unable to handle reads that are too large
324
     (for instance, NetApp shares with oplocks turned off).  To avoid
325
     hitting this limitation, we read the buffer in chunks of 8MB max.  */
326
  while (nread < nbytes)
327
    {
328
      const file_ptr max_chunk_size = 0x800000;
329
      file_ptr chunk_size = nbytes - nread;
330
      file_ptr chunk_nread;
331
 
332
      if (chunk_size > max_chunk_size)
333
        chunk_size = max_chunk_size;
334
 
335
      chunk_nread = cache_bread_1 (abfd, (char *) buf + nread, chunk_size);
336
 
337
      /* Update the nread count.
338
 
339
         We just have to be careful of the case when cache_bread_1 returns
340
         a negative count:  If this is our first read, then set nread to
341
         that negative count in order to return that negative value to the
342
         caller.  Otherwise, don't add it to our total count, or we would
343
         end up returning a smaller number of bytes read than we actually
344
         did.  */
345
      if (nread == 0 || chunk_nread > 0)
346
        nread += chunk_nread;
347
 
348
      if (chunk_nread < chunk_size)
349
        break;
350
    }
351
 
352
  return nread;
353
}
354
 
355
static file_ptr
356
cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
357
{
358
  file_ptr nwrite;
359
  FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL);
360
 
361
  if (f == NULL)
362
    return 0;
363
  nwrite = fwrite (where, 1, nbytes, f);
364
  if (nwrite < nbytes && ferror (f))
365
    {
366
      bfd_set_error (bfd_error_system_call);
367
      return -1;
368
    }
369
  return nwrite;
370
}
371
 
372
static int
373
cache_bclose (struct bfd *abfd)
374
{
375
  return bfd_cache_close (abfd) - 1;
376
}
377
 
378
static int
379
cache_bflush (struct bfd *abfd)
380
{
381
  int sts;
382
  FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
383
 
384
  if (f == NULL)
385
    return 0;
386
  sts = fflush (f);
387
  if (sts < 0)
388
    bfd_set_error (bfd_error_system_call);
389
  return sts;
390
}
391
 
392
static int
393
cache_bstat (struct bfd *abfd, struct stat *sb)
394
{
395
  int sts;
396
  FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
397
 
398
  if (f == NULL)
399
    return -1;
400
  sts = fstat (fileno (f), sb);
401
  if (sts < 0)
402
    bfd_set_error (bfd_error_system_call);
403
  return sts;
404
}
405
 
406
static void *
407
cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
408
	     void *addr ATTRIBUTE_UNUSED,
409
	     bfd_size_type len ATTRIBUTE_UNUSED,
410
	     int prot ATTRIBUTE_UNUSED,
411
	     int flags ATTRIBUTE_UNUSED,
412
	     file_ptr offset ATTRIBUTE_UNUSED,
413
             void **map_addr ATTRIBUTE_UNUSED,
414
             bfd_size_type *map_len ATTRIBUTE_UNUSED)
415
{
416
  void *ret = (void *) -1;
417
 
418
  if ((abfd->flags & BFD_IN_MEMORY) != 0)
419
    abort ();
420
#ifdef HAVE_MMAP
421
  else
422
    {
423
      static uintptr_t pagesize_m1;
424
      FILE *f;
425
      file_ptr pg_offset;
426
      bfd_size_type pg_len;
427
 
428
      f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
429
      if (f == NULL)
430
	return ret;
431
 
432
      if (pagesize_m1 == 0)
433
        pagesize_m1 = getpagesize () - 1;
434
 
435
      /* Handle archive members.  */
436
      if (abfd->my_archive != NULL)
437
        offset += abfd->origin;
438
 
439
      /* Align.  */
440
      pg_offset = offset & ~pagesize_m1;
441
      pg_len = (len + (offset - pg_offset) + pagesize_m1) & ~pagesize_m1;
442
 
443
      ret = mmap (addr, pg_len, prot, flags, fileno (f), pg_offset);
444
      if (ret == (void *) -1)
445
	bfd_set_error (bfd_error_system_call);
446
      else
447
        {
448
          *map_addr = ret;
449
          *map_len = pg_len;
450
          ret = (char *) ret + (offset & pagesize_m1);
451
        }
452
    }
453
#endif
454
 
455
  return ret;
456
}
457
 
458
static const struct bfd_iovec cache_iovec =
459
{
460
  &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
461
  &cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap
462
};
463
 
464
/*
465
INTERNAL_FUNCTION
466
	bfd_cache_init
467
 
468
SYNOPSIS
469
	bfd_boolean bfd_cache_init (bfd *abfd);
470
 
471
DESCRIPTION
472
	Add a newly opened BFD to the cache.
473
*/
474
 
475
bfd_boolean
476
bfd_cache_init (bfd *abfd)
477
{
478
  BFD_ASSERT (abfd->iostream != NULL);
479
  if (open_files >= bfd_cache_max_open ())
480
    {
481
      if (! close_one ())
482
	return FALSE;
483
    }
484
  abfd->iovec = &cache_iovec;
485
  insert (abfd);
486
  ++open_files;
487
  return TRUE;
488
}
489
 
490
/*
491
INTERNAL_FUNCTION
492
	bfd_cache_close
493
 
494
SYNOPSIS
495
	bfd_boolean bfd_cache_close (bfd *abfd);
496
 
497
DESCRIPTION
498
	Remove the BFD @var{abfd} from the cache. If the attached file is open,
499
	then close it too.
500
 
501
RETURNS
502
	<> is returned if closing the file fails, <> is
503
	returned if all is well.
504
*/
505
 
506
bfd_boolean
507
bfd_cache_close (bfd *abfd)
508
{
509
  if (abfd->iovec != &cache_iovec)
510
    return TRUE;
511
 
512
  if (abfd->iostream == NULL)
513
    /* Previously closed.  */
514
    return TRUE;
515
 
516
  return bfd_cache_delete (abfd);
517
}
518
 
519
/*
520
FUNCTION
521
	bfd_cache_close_all
522
 
523
SYNOPSIS
524
	bfd_boolean bfd_cache_close_all (void);
525
 
526
DESCRIPTION
527
	Remove all BFDs from the cache. If the attached file is open,
528
	then close it too.
529
 
530
RETURNS
531
	<> is returned if closing one of the file fails, <> is
532
	returned if all is well.
533
*/
534
 
535
bfd_boolean
536
bfd_cache_close_all ()
537
{
538
  bfd_boolean ret = TRUE;
539
 
540
  while (bfd_last_cache != NULL)
541
    ret &= bfd_cache_close (bfd_last_cache);
542
 
543
  return ret;
544
}
545
 
546
/*
547
INTERNAL_FUNCTION
548
	bfd_open_file
549
 
550
SYNOPSIS
551
	FILE* bfd_open_file (bfd *abfd);
552
 
553
DESCRIPTION
554
	Call the OS to open a file for @var{abfd}.  Return the <>
555
	(possibly <>) that results from this operation.  Set up the
556
	BFD so that future accesses know the file is open. If the <>
557
	returned is <>, then it won't have been put in the
558
	cache, so it won't have to be removed from it.
559
*/
560
 
561
FILE *
562
bfd_open_file (bfd *abfd)
563
{
564
  abfd->cacheable = TRUE;	/* Allow it to be closed later.  */
565
 
566
  if (open_files >= bfd_cache_max_open ())
567
    {
568
      if (! close_one ())
569
	return NULL;
570
    }
571
 
572
  switch (abfd->direction)
573
    {
574
    case read_direction:
575
    case no_direction:
576
      abfd->iostream = real_fopen (abfd->filename, FOPEN_RB);
577
      break;
578
    case both_direction:
579
    case write_direction:
580
      if (abfd->opened_once)
581
	{
582
	  abfd->iostream = real_fopen (abfd->filename, FOPEN_RUB);
583
	  if (abfd->iostream == NULL)
584
	    abfd->iostream = real_fopen (abfd->filename, FOPEN_WUB);
585
	}
586
      else
587
	{
588
	  /* Create the file.
589
 
590
	     Some operating systems won't let us overwrite a running
591
	     binary.  For them, we want to unlink the file first.
592
 
593
	     However, gcc 2.95 will create temporary files using
594
	     O_EXCL and tight permissions to prevent other users from
595
	     substituting other .o files during the compilation.  gcc
596
	     will then tell the assembler to use the newly created
597
	     file as an output file.  If we unlink the file here, we
598
	     open a brief window when another user could still
599
	     substitute a file.
600
 
601
	     So we unlink the output file if and only if it has
602
	     non-zero size.  */
603
#ifndef __MSDOS__
604
	  /* Don't do this for MSDOS: it doesn't care about overwriting
605
	     a running binary, but if this file is already open by
606
	     another BFD, we will be in deep trouble if we delete an
607
	     open file.  In fact, objdump does just that if invoked with
608
	     the --info option.  */
609
	  struct stat s;
610
 
611
	  if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
612
	    unlink_if_ordinary (abfd->filename);
613
#endif
614
	  abfd->iostream = real_fopen (abfd->filename, FOPEN_WUB);
615
	  abfd->opened_once = TRUE;
616
	}
617
      break;
618
    }
619
 
620
  if (abfd->iostream == NULL)
621
    bfd_set_error (bfd_error_system_call);
622
  else
623
    {
624
      if (! bfd_cache_init (abfd))
625
	return NULL;
626
    }
627
 
628
  return (FILE *) abfd->iostream;
629
}