Subversion Repositories Kolibri OS

Rev

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