Home | History | Annotate | Download | only in bfd
      1 /* Low-level I/O routines for BFDs.
      2 
      3    Copyright (C) 1990-2016 Free Software Foundation, Inc.
      4 
      5    Written by Cygnus Support.
      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 #include "sysdep.h"
     25 #include <limits.h>
     26 #include "bfd.h"
     27 #include "libbfd.h"
     28 
     29 #ifndef S_IXUSR
     30 #define S_IXUSR 0100    /* Execute by owner.  */
     31 #endif
     32 #ifndef S_IXGRP
     33 #define S_IXGRP 0010    /* Execute by group.  */
     34 #endif
     35 #ifndef S_IXOTH
     36 #define S_IXOTH 0001    /* Execute by others.  */
     37 #endif
     38 
     39 #ifndef FD_CLOEXEC
     40 #define FD_CLOEXEC 1
     41 #endif
     42 
     43 file_ptr
     44 real_ftell (FILE *file)
     45 {
     46 #if defined (HAVE_FTELLO64)
     47   return ftello64 (file);
     48 #elif defined (HAVE_FTELLO)
     49   return ftello (file);
     50 #else
     51   return ftell (file);
     52 #endif
     53 }
     54 
     55 int
     56 real_fseek (FILE *file, file_ptr offset, int whence)
     57 {
     58 #if defined (HAVE_FSEEKO64)
     59   return fseeko64 (file, offset, whence);
     60 #elif defined (HAVE_FSEEKO)
     61   return fseeko (file, offset, whence);
     62 #else
     63   return fseek (file, offset, whence);
     64 #endif
     65 }
     66 
     67 /* Mark FILE as close-on-exec.  Return FILE.  FILE may be NULL, in
     68    which case nothing is done.  */
     69 static FILE *
     70 close_on_exec (FILE *file)
     71 {
     72 #if defined (HAVE_FILENO) && defined (F_GETFD)
     73   if (file)
     74     {
     75       int fd = fileno (file);
     76       int old = fcntl (fd, F_GETFD, 0);
     77       if (old >= 0)
     78 	fcntl (fd, F_SETFD, old | FD_CLOEXEC);
     79     }
     80 #endif
     81   return file;
     82 }
     83 
     84 FILE *
     85 real_fopen (const char *filename, const char *modes)
     86 {
     87 #ifdef VMS
     88   char *vms_attr;
     89 
     90   /* On VMS, fopen allows file attributes as optional arguments.
     91      We need to use them but we'd better to use the common prototype.
     92      In fopen-vms.h, they are separated from the mode with a comma.
     93      Split here.  */
     94   vms_attr = strchr (modes, ',');
     95   if (vms_attr == NULL)
     96     {
     97       /* No attributes.  */
     98       return close_on_exec (fopen (filename, modes));
     99     }
    100   else
    101     {
    102       /* Attributes found.  Split.  */
    103       size_t modes_len = strlen (modes) + 1;
    104       char attrs[modes_len + 1];
    105       char *at[3];
    106       int i;
    107 
    108       memcpy (attrs, modes, modes_len);
    109       at[0] = attrs;
    110       for (i = 0; i < 2; i++)
    111 	{
    112 	  at[i + 1] = strchr (at[i], ',');
    113 	  BFD_ASSERT (at[i + 1] != NULL);
    114 	  *(at[i + 1]++) = 0; /* Replace ',' with a nul, and skip it.  */
    115 	}
    116       return close_on_exec (fopen (filename, at[0], at[1], at[2]));
    117     }
    118 #else /* !VMS */
    119 #if defined (HAVE_FOPEN64)
    120   return close_on_exec (fopen64 (filename, modes));
    121 #else
    122   return close_on_exec (fopen (filename, modes));
    123 #endif
    124 #endif /* !VMS */
    125 }
    126 
    127 /*
    128 INTERNAL_DEFINITION
    129 	struct bfd_iovec
    130 
    131 DESCRIPTION
    132 
    133 	The <<struct bfd_iovec>> contains the internal file I/O class.
    134 	Each <<BFD>> has an instance of this class and all file I/O is
    135 	routed through it (it is assumed that the instance implements
    136 	all methods listed below).
    137 
    138 .struct bfd_iovec
    139 .{
    140 .  {* To avoid problems with macros, a "b" rather than "f"
    141 .     prefix is prepended to each method name.  *}
    142 .  {* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching
    143 .     bytes starting at PTR.  Return the number of bytes actually
    144 .     transfered (a read past end-of-file returns less than NBYTES),
    145 .     or -1 (setting <<bfd_error>>) if an error occurs.  *}
    146 .  file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes);
    147 .  file_ptr (*bwrite) (struct bfd *abfd, const void *ptr,
    148 .                      file_ptr nbytes);
    149 .  {* Return the current IOSTREAM file offset, or -1 (setting <<bfd_error>>
    150 .     if an error occurs.  *}
    151 .  file_ptr (*btell) (struct bfd *abfd);
    152 .  {* For the following, on successful completion a value of 0 is returned.
    153 .     Otherwise, a value of -1 is returned (and  <<bfd_error>> is set).  *}
    154 .  int (*bseek) (struct bfd *abfd, file_ptr offset, int whence);
    155 .  int (*bclose) (struct bfd *abfd);
    156 .  int (*bflush) (struct bfd *abfd);
    157 .  int (*bstat) (struct bfd *abfd, struct stat *sb);
    158 .  {* Mmap a part of the files. ADDR, LEN, PROT, FLAGS and OFFSET are the usual
    159 .     mmap parameter, except that LEN and OFFSET do not need to be page
    160 .     aligned.  Returns (void *)-1 on failure, mmapped address on success.
    161 .     Also write in MAP_ADDR the address of the page aligned buffer and in
    162 .     MAP_LEN the size mapped (a page multiple).  Use unmap with MAP_ADDR and
    163 .     MAP_LEN to unmap.  *}
    164 .  void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len,
    165 .                  int prot, int flags, file_ptr offset,
    166 .                  void **map_addr, bfd_size_type *map_len);
    167 .};
    168 
    169 .extern const struct bfd_iovec _bfd_memory_iovec;
    170 
    171 */
    172 
    173 
    174 /* Return value is amount read.  */
    175 
    176 bfd_size_type
    177 bfd_bread (void *ptr, bfd_size_type size, bfd *abfd)
    178 {
    179   size_t nread;
    180 
    181   /* If this is an archive element, don't read past the end of
    182      this element.  */
    183   if (abfd->arelt_data != NULL)
    184     {
    185       bfd_size_type maxbytes = arelt_size (abfd);
    186 
    187       if (abfd->where + size > maxbytes)
    188         {
    189           if (abfd->where >= maxbytes)
    190             return 0;
    191           size = maxbytes - abfd->where;
    192         }
    193     }
    194 
    195   if (abfd->iovec)
    196     nread = abfd->iovec->bread (abfd, ptr, size);
    197   else
    198     nread = 0;
    199   if (nread != (size_t) -1)
    200     abfd->where += nread;
    201 
    202   return nread;
    203 }
    204 
    205 bfd_size_type
    206 bfd_bwrite (const void *ptr, bfd_size_type size, bfd *abfd)
    207 {
    208   size_t nwrote;
    209 
    210   if (abfd->iovec)
    211     nwrote = abfd->iovec->bwrite (abfd, ptr, size);
    212   else
    213     nwrote = 0;
    214 
    215   if (nwrote != (size_t) -1)
    216     abfd->where += nwrote;
    217   if (nwrote != size)
    218     {
    219 #ifdef ENOSPC
    220       errno = ENOSPC;
    221 #endif
    222       bfd_set_error (bfd_error_system_call);
    223     }
    224   return nwrote;
    225 }
    226 
    227 file_ptr
    228 bfd_tell (bfd *abfd)
    229 {
    230   file_ptr ptr;
    231 
    232   if (abfd->iovec)
    233     {
    234       bfd *parent_bfd = abfd;
    235       ptr = abfd->iovec->btell (abfd);
    236 
    237       while (parent_bfd->my_archive != NULL
    238 	     && !bfd_is_thin_archive (parent_bfd->my_archive))
    239 	{
    240 	  ptr -= parent_bfd->origin;
    241 	  parent_bfd = parent_bfd->my_archive;
    242 	}
    243     }
    244   else
    245     ptr = 0;
    246 
    247   abfd->where = ptr;
    248   return ptr;
    249 }
    250 
    251 int
    252 bfd_flush (bfd *abfd)
    253 {
    254   if (abfd->iovec)
    255     return abfd->iovec->bflush (abfd);
    256   return 0;
    257 }
    258 
    259 /* Returns 0 for success, negative value for failure (in which case
    260    bfd_get_error can retrieve the error code).  */
    261 int
    262 bfd_stat (bfd *abfd, struct stat *statbuf)
    263 {
    264   int result;
    265 
    266   if (abfd->iovec)
    267     result = abfd->iovec->bstat (abfd, statbuf);
    268   else
    269     result = -1;
    270 
    271   if (result < 0)
    272     bfd_set_error (bfd_error_system_call);
    273   return result;
    274 }
    275 
    276 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
    277    can retrieve the error code).  */
    278 
    279 int
    280 bfd_seek (bfd *abfd, file_ptr position, int direction)
    281 {
    282   int result;
    283   file_ptr file_position;
    284   /* For the time being, a BFD may not seek to it's end.  The problem
    285      is that we don't easily have a way to recognize the end of an
    286      element in an archive.  */
    287 
    288   BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
    289 
    290   if (direction == SEEK_CUR && position == 0)
    291     return 0;
    292 
    293   if (abfd->my_archive == NULL || bfd_is_thin_archive (abfd->my_archive))
    294     {
    295       if (direction == SEEK_SET && (bfd_vma) position == abfd->where)
    296 	return 0;
    297     }
    298   else
    299     {
    300       /* We need something smarter to optimize access to archives.
    301 	 Currently, anything inside an archive is read via the file
    302 	 handle for the archive.  Which means that a bfd_seek on one
    303 	 component affects the `current position' in the archive, as
    304 	 well as in any other component.
    305 
    306 	 It might be sufficient to put a spike through the cache
    307 	 abstraction, and look to the archive for the file position,
    308 	 but I think we should try for something cleaner.
    309 
    310 	 In the meantime, no optimization for archives.  */
    311     }
    312 
    313   file_position = position;
    314   if (direction == SEEK_SET)
    315     {
    316       bfd *parent_bfd = abfd;
    317 
    318       while (parent_bfd->my_archive != NULL
    319 	     && !bfd_is_thin_archive (parent_bfd->my_archive))
    320         {
    321           file_position += parent_bfd->origin;
    322           parent_bfd = parent_bfd->my_archive;
    323         }
    324     }
    325 
    326   if (abfd->iovec)
    327     result = abfd->iovec->bseek (abfd, file_position, direction);
    328   else
    329     result = -1;
    330 
    331   if (result != 0)
    332     {
    333       int hold_errno = errno;
    334 
    335       /* Force redetermination of `where' field.  */
    336       bfd_tell (abfd);
    337 
    338       /* An EINVAL error probably means that the file offset was
    339          absurd.  */
    340       if (hold_errno == EINVAL)
    341 	bfd_set_error (bfd_error_file_truncated);
    342       else
    343 	{
    344 	  bfd_set_error (bfd_error_system_call);
    345 	  errno = hold_errno;
    346 	}
    347     }
    348   else
    349     {
    350       /* Adjust `where' field.  */
    351       if (direction == SEEK_SET)
    352 	abfd->where = position;
    353       else
    354 	abfd->where += position;
    355     }
    356   return result;
    357 }
    358 
    359 /*
    360 FUNCTION
    361 	bfd_get_mtime
    362 
    363 SYNOPSIS
    364 	long bfd_get_mtime (bfd *abfd);
    365 
    366 DESCRIPTION
    367 	Return the file modification time (as read from the file system, or
    368 	from the archive header for archive members).
    369 
    370 */
    371 
    372 long
    373 bfd_get_mtime (bfd *abfd)
    374 {
    375   struct stat buf;
    376 
    377   if (abfd->mtime_set)
    378     return abfd->mtime;
    379 
    380   if (abfd->iovec == NULL)
    381     return 0;
    382 
    383   if (abfd->iovec->bstat (abfd, &buf) != 0)
    384     return 0;
    385 
    386   abfd->mtime = buf.st_mtime;		/* Save value in case anyone wants it */
    387   return buf.st_mtime;
    388 }
    389 
    390 /*
    391 FUNCTION
    392 	bfd_get_size
    393 
    394 SYNOPSIS
    395 	file_ptr bfd_get_size (bfd *abfd);
    396 
    397 DESCRIPTION
    398 	Return the file size (as read from file system) for the file
    399 	associated with BFD @var{abfd}.
    400 
    401 	The initial motivation for, and use of, this routine is not
    402 	so we can get the exact size of the object the BFD applies to, since
    403 	that might not be generally possible (archive members for example).
    404 	It would be ideal if someone could eventually modify
    405 	it so that such results were guaranteed.
    406 
    407 	Instead, we want to ask questions like "is this NNN byte sized
    408 	object I'm about to try read from file offset YYY reasonable?"
    409 	As as example of where we might do this, some object formats
    410 	use string tables for which the first <<sizeof (long)>> bytes of the
    411 	table contain the size of the table itself, including the size bytes.
    412 	If an application tries to read what it thinks is one of these
    413 	string tables, without some way to validate the size, and for
    414 	some reason the size is wrong (byte swapping error, wrong location
    415 	for the string table, etc.), the only clue is likely to be a read
    416 	error when it tries to read the table, or a "virtual memory
    417 	exhausted" error when it tries to allocate 15 bazillon bytes
    418 	of space for the 15 bazillon byte table it is about to read.
    419 	This function at least allows us to answer the question, "is the
    420 	size reasonable?".
    421 */
    422 
    423 file_ptr
    424 bfd_get_size (bfd *abfd)
    425 {
    426   struct stat buf;
    427 
    428   if (abfd->iovec == NULL)
    429     return 0;
    430 
    431   if (abfd->iovec->bstat (abfd, &buf) != 0)
    432     return 0;
    433 
    434   return buf.st_size;
    435 }
    436 
    437 
    438 /*
    439 FUNCTION
    440 	bfd_mmap
    441 
    442 SYNOPSIS
    443 	void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
    444 	                int prot, int flags, file_ptr offset,
    445 	                void **map_addr, bfd_size_type *map_len);
    446 
    447 DESCRIPTION
    448 	Return mmap()ed region of the file, if possible and implemented.
    449         LEN and OFFSET do not need to be page aligned.  The page aligned
    450         address and length are written to MAP_ADDR and MAP_LEN.
    451 
    452 */
    453 
    454 void *
    455 bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
    456 	  int prot, int flags, file_ptr offset,
    457           void **map_addr, bfd_size_type *map_len)
    458 {
    459   void *ret = (void *)-1;
    460 
    461   if (abfd->iovec == NULL)
    462     return ret;
    463 
    464   return abfd->iovec->bmmap (abfd, addr, len, prot, flags, offset,
    465                              map_addr, map_len);
    466 }
    467 
    468 /* Memory file I/O operations.  */
    469 
    470 static file_ptr
    471 memory_bread (bfd *abfd, void *ptr, file_ptr size)
    472 {
    473   struct bfd_in_memory *bim;
    474   bfd_size_type get;
    475 
    476   bim = (struct bfd_in_memory *) abfd->iostream;
    477   get = size;
    478   if (abfd->where + get > bim->size)
    479     {
    480       if (bim->size < (bfd_size_type) abfd->where)
    481         get = 0;
    482       else
    483         get = bim->size - abfd->where;
    484       bfd_set_error (bfd_error_file_truncated);
    485     }
    486   memcpy (ptr, bim->buffer + abfd->where, (size_t) get);
    487   return get;
    488 }
    489 
    490 static file_ptr
    491 memory_bwrite (bfd *abfd, const void *ptr, file_ptr size)
    492 {
    493   struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
    494 
    495   if (abfd->where + size > bim->size)
    496     {
    497       bfd_size_type newsize, oldsize;
    498 
    499       oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
    500       bim->size = abfd->where + size;
    501       /* Round up to cut down on memory fragmentation */
    502       newsize = (bim->size + 127) & ~(bfd_size_type) 127;
    503       if (newsize > oldsize)
    504         {
    505           bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
    506           if (bim->buffer == NULL)
    507             {
    508               bim->size = 0;
    509               return 0;
    510             }
    511           if (newsize > bim->size)
    512             memset (bim->buffer + bim->size, 0, newsize - bim->size);
    513         }
    514     }
    515   memcpy (bim->buffer + abfd->where, ptr, (size_t) size);
    516   return size;
    517 }
    518 
    519 static file_ptr
    520 memory_btell (bfd *abfd)
    521 {
    522   return abfd->where;
    523 }
    524 
    525 static int
    526 memory_bseek (bfd *abfd, file_ptr position, int direction)
    527 {
    528   file_ptr nwhere;
    529   struct bfd_in_memory *bim;
    530 
    531   bim = (struct bfd_in_memory *) abfd->iostream;
    532 
    533   if (direction == SEEK_SET)
    534     nwhere = position;
    535   else
    536     nwhere = abfd->where + position;
    537 
    538   if (nwhere < 0)
    539     {
    540       abfd->where = 0;
    541       errno = EINVAL;
    542       return -1;
    543     }
    544 
    545   if ((bfd_size_type)nwhere > bim->size)
    546     {
    547       if (abfd->direction == write_direction
    548           || abfd->direction == both_direction)
    549         {
    550           bfd_size_type newsize, oldsize;
    551 
    552           oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
    553           bim->size = nwhere;
    554           /* Round up to cut down on memory fragmentation */
    555           newsize = (bim->size + 127) & ~(bfd_size_type) 127;
    556           if (newsize > oldsize)
    557             {
    558               bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
    559               if (bim->buffer == NULL)
    560                 {
    561                   errno = EINVAL;
    562                   bim->size = 0;
    563                   return -1;
    564                 }
    565               memset (bim->buffer + oldsize, 0, newsize - oldsize);
    566             }
    567         }
    568       else
    569         {
    570           abfd->where = bim->size;
    571           errno = EINVAL;
    572           bfd_set_error (bfd_error_file_truncated);
    573           return -1;
    574         }
    575     }
    576   return 0;
    577 }
    578 
    579 static int
    580 memory_bclose (struct bfd *abfd)
    581 {
    582   struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
    583 
    584   if (bim->buffer != NULL)
    585     free (bim->buffer);
    586   free (bim);
    587   abfd->iostream = NULL;
    588 
    589   return 0;
    590 }
    591 
    592 static int
    593 memory_bflush (bfd *abfd ATTRIBUTE_UNUSED)
    594 {
    595   return 0;
    596 }
    597 
    598 static int
    599 memory_bstat (bfd *abfd, struct stat *statbuf)
    600 {
    601   struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
    602 
    603   memset (statbuf, 0, sizeof (*statbuf));
    604   statbuf->st_size = bim->size;
    605 
    606   return 0;
    607 }
    608 
    609 static void *
    610 memory_bmmap (bfd *abfd ATTRIBUTE_UNUSED, void *addr ATTRIBUTE_UNUSED,
    611               bfd_size_type len ATTRIBUTE_UNUSED, int prot ATTRIBUTE_UNUSED,
    612               int flags ATTRIBUTE_UNUSED, file_ptr offset ATTRIBUTE_UNUSED,
    613               void **map_addr ATTRIBUTE_UNUSED,
    614               bfd_size_type *map_len ATTRIBUTE_UNUSED)
    615 {
    616   return (void *)-1;
    617 }
    618 
    619 const struct bfd_iovec _bfd_memory_iovec =
    620 {
    621   &memory_bread, &memory_bwrite, &memory_btell, &memory_bseek,
    622   &memory_bclose, &memory_bflush, &memory_bstat, &memory_bmmap
    623 };
    624