1 /* Low-level I/O routines for BFDs.
3 Copyright (C) 1990-2022 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
35 #define S_IXUSR 0100 /* Execute by owner. */
38 #define S_IXGRP 0010 /* Execute by group. */
41 #define S_IXOTH 0001 /* Execute by others. */
49 _bfd_real_ftell (FILE *file
)
51 #if defined (HAVE_FTELLO64)
52 return ftello64 (file
);
53 #elif defined (HAVE_FTELLO)
61 _bfd_real_fseek (FILE *file
, file_ptr offset
, int whence
)
63 #if defined (HAVE_FSEEKO64)
64 return fseeko64 (file
, offset
, whence
);
65 #elif defined (HAVE_FSEEKO)
66 return fseeko (file
, offset
, whence
);
68 return fseek (file
, offset
, whence
);
72 /* Mark FILE as close-on-exec. Return FILE. FILE may be NULL, in
73 which case nothing is done. */
75 close_on_exec (FILE *file
)
77 #if defined (HAVE_FILENO) && defined (F_GETFD)
80 int fd
= fileno (file
);
81 int old
= fcntl (fd
, F_GETFD
, 0);
83 fcntl (fd
, F_SETFD
, old
| FD_CLOEXEC
);
90 _bfd_real_fopen (const char *filename
, const char *modes
)
95 /* On VMS, fopen allows file attributes as optional arguments.
96 We need to use them but we'd better to use the common prototype.
97 In fopen-vms.h, they are separated from the mode with a comma.
99 vms_attr
= strchr (modes
, ',');
100 if (vms_attr
!= NULL
)
102 /* Attributes found. Split. */
103 size_t modes_len
= strlen (modes
) + 1;
104 char attrs
[modes_len
+ 1];
108 memcpy (attrs
, modes
, modes_len
);
110 for (i
= 0; i
< 2; i
++)
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. */
116 return close_on_exec (fopen (filename
, at
[0], at
[1], at
[2]));
119 #elif defined (_WIN32)
120 /* PR 25713: Handle extra long path names possibly containing '..' and '.'. */
121 wchar_t ** lpFilePart
= {NULL
};
122 const wchar_t prefix
[] = L
"\\\\?\\";
123 const size_t partPathLen
= strlen (filename
) + 1;
125 #if !HAVE_DECL____LC_CODEPAGE_FUNC
126 /* This prototype was added to locale.h in version 9.0 of MinGW-w64. */
127 _CRTIMP
unsigned int __cdecl
___lc_codepage_func (void);
129 const unsigned int cp
= ___lc_codepage_func ();
131 const unsigned int cp
= CP_UTF8
;
134 /* Converting the partial path from ascii to unicode.
135 1) Get the length: Calling with lpWideCharStr set to null returns the length.
136 2) Convert the string: Calling with cbMultiByte set to -1 includes the terminating null. */
137 size_t partPathWSize
= MultiByteToWideChar (cp
, 0, filename
, -1, NULL
, 0);
138 wchar_t * partPath
= calloc (partPathWSize
, sizeof(wchar_t));
141 MultiByteToWideChar (cp
, 0, filename
, -1, partPath
, partPathWSize
);
143 /* Convert any UNIX style path separators into the DOS i.e. backslash separator. */
144 for (ix
= 0; ix
< partPathLen
; ix
++)
145 if (IS_UNIX_DIR_SEPARATOR(filename
[ix
]))
148 /* Getting the full path from the provided partial path.
150 2) Resolve the path. */
151 long fullPathWSize
= GetFullPathNameW (partPath
, 0, NULL
, lpFilePart
);
152 wchar_t * fullPath
= calloc (fullPathWSize
+ sizeof(prefix
) + 1, sizeof(wchar_t));
154 wcscpy (fullPath
, prefix
);
156 int prefixLen
= sizeof(prefix
) / sizeof(wchar_t);
157 wchar_t * fullPathOffset
= fullPath
+ prefixLen
- 1;
159 GetFullPathNameW (partPath
, fullPathWSize
, fullPathOffset
, lpFilePart
);
162 /* It is non-standard for modes to exceed 16 characters. */
165 MultiByteToWideChar (cp
, 0, modes
, -1, modesW
, sizeof(modesW
));
167 FILE * file
= _wfopen (fullPath
, modesW
);
170 return close_on_exec (file
);
172 #elif defined (HAVE_FOPEN64)
173 return close_on_exec (fopen64 (filename
, modes
));
176 return close_on_exec (fopen (filename
, modes
));
186 The <<struct bfd_iovec>> contains the internal file I/O class.
187 Each <<BFD>> has an instance of this class and all file I/O is
188 routed through it (it is assumed that the instance implements
189 all methods listed below).
193 . {* To avoid problems with macros, a "b" rather than "f"
194 . prefix is prepended to each method name. *}
195 . {* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching
196 . bytes starting at PTR. Return the number of bytes actually
197 . transfered (a read past end-of-file returns less than NBYTES),
198 . or -1 (setting <<bfd_error>>) if an error occurs. *}
199 . file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes);
200 . file_ptr (*bwrite) (struct bfd *abfd, const void *ptr,
202 . {* Return the current IOSTREAM file offset, or -1 (setting <<bfd_error>>
203 . if an error occurs. *}
204 . file_ptr (*btell) (struct bfd *abfd);
205 . {* For the following, on successful completion a value of 0 is returned.
206 . Otherwise, a value of -1 is returned (and <<bfd_error>> is set). *}
207 . int (*bseek) (struct bfd *abfd, file_ptr offset, int whence);
208 . int (*bclose) (struct bfd *abfd);
209 . int (*bflush) (struct bfd *abfd);
210 . int (*bstat) (struct bfd *abfd, struct stat *sb);
211 . {* Mmap a part of the files. ADDR, LEN, PROT, FLAGS and OFFSET are the usual
212 . mmap parameter, except that LEN and OFFSET do not need to be page
213 . aligned. Returns (void *)-1 on failure, mmapped address on success.
214 . Also write in MAP_ADDR the address of the page aligned buffer and in
215 . MAP_LEN the size mapped (a page multiple). Use unmap with MAP_ADDR and
216 . MAP_LEN to unmap. *}
217 . void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len,
218 . int prot, int flags, file_ptr offset,
219 . void **map_addr, bfd_size_type *map_len);
222 .extern const struct bfd_iovec _bfd_memory_iovec;
227 /* Return value is amount read. */
230 bfd_bread (void *ptr
, bfd_size_type size
, bfd
*abfd
)
233 bfd
*element_bfd
= abfd
;
234 ufile_ptr offset
= 0;
236 while (abfd
->my_archive
!= NULL
237 && !bfd_is_thin_archive (abfd
->my_archive
))
239 offset
+= abfd
->origin
;
240 abfd
= abfd
->my_archive
;
242 offset
+= abfd
->origin
;
244 /* If this is a non-thin archive element, don't read past the end of
246 if (element_bfd
->arelt_data
!= NULL
247 && element_bfd
->my_archive
!= NULL
248 && !bfd_is_thin_archive (element_bfd
->my_archive
))
250 bfd_size_type maxbytes
= arelt_size (element_bfd
);
252 if (abfd
->where
< offset
|| abfd
->where
- offset
>= maxbytes
)
254 bfd_set_error (bfd_error_invalid_operation
);
257 if (abfd
->where
- offset
+ size
> maxbytes
)
258 size
= maxbytes
- (abfd
->where
- offset
);
261 if (abfd
->iovec
== NULL
)
263 bfd_set_error (bfd_error_invalid_operation
);
267 nread
= abfd
->iovec
->bread (abfd
, ptr
, size
);
269 abfd
->where
+= nread
;
275 bfd_bwrite (const void *ptr
, bfd_size_type size
, bfd
*abfd
)
279 while (abfd
->my_archive
!= NULL
280 && !bfd_is_thin_archive (abfd
->my_archive
))
281 abfd
= abfd
->my_archive
;
283 if (abfd
->iovec
== NULL
)
285 bfd_set_error (bfd_error_invalid_operation
);
289 nwrote
= abfd
->iovec
->bwrite (abfd
, ptr
, size
);
291 abfd
->where
+= nwrote
;
292 if ((bfd_size_type
) nwrote
!= size
)
297 bfd_set_error (bfd_error_system_call
);
305 ufile_ptr offset
= 0;
308 while (abfd
->my_archive
!= NULL
309 && !bfd_is_thin_archive (abfd
->my_archive
))
311 offset
+= abfd
->origin
;
312 abfd
= abfd
->my_archive
;
314 offset
+= abfd
->origin
;
316 if (abfd
->iovec
== NULL
)
319 ptr
= abfd
->iovec
->btell (abfd
);
325 bfd_flush (bfd
*abfd
)
327 while (abfd
->my_archive
!= NULL
328 && !bfd_is_thin_archive (abfd
->my_archive
))
329 abfd
= abfd
->my_archive
;
331 if (abfd
->iovec
== NULL
)
334 return abfd
->iovec
->bflush (abfd
);
337 /* Returns 0 for success, negative value for failure (in which case
338 bfd_get_error can retrieve the error code). */
340 bfd_stat (bfd
*abfd
, struct stat
*statbuf
)
344 while (abfd
->my_archive
!= NULL
345 && !bfd_is_thin_archive (abfd
->my_archive
))
346 abfd
= abfd
->my_archive
;
348 if (abfd
->iovec
== NULL
)
350 bfd_set_error (bfd_error_invalid_operation
);
354 result
= abfd
->iovec
->bstat (abfd
, statbuf
);
356 bfd_set_error (bfd_error_system_call
);
360 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
361 can retrieve the error code). */
364 bfd_seek (bfd
*abfd
, file_ptr position
, int direction
)
367 ufile_ptr offset
= 0;
369 while (abfd
->my_archive
!= NULL
370 && !bfd_is_thin_archive (abfd
->my_archive
))
372 offset
+= abfd
->origin
;
373 abfd
= abfd
->my_archive
;
375 offset
+= abfd
->origin
;
377 if (abfd
->iovec
== NULL
)
379 bfd_set_error (bfd_error_invalid_operation
);
383 /* For the time being, a BFD may not seek to it's end. The problem
384 is that we don't easily have a way to recognize the end of an
385 element in an archive. */
386 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
388 if (direction
!= SEEK_CUR
)
391 if ((direction
== SEEK_CUR
&& position
== 0)
392 || (direction
== SEEK_SET
&& (ufile_ptr
) position
== abfd
->where
))
395 result
= abfd
->iovec
->bseek (abfd
, position
, direction
);
398 /* An EINVAL error probably means that the file offset was
401 bfd_set_error (bfd_error_file_truncated
);
403 bfd_set_error (bfd_error_system_call
);
407 /* Adjust `where' field. */
408 if (direction
== SEEK_CUR
)
409 abfd
->where
+= position
;
411 abfd
->where
= position
;
422 long bfd_get_mtime (bfd *abfd);
425 Return the file modification time (as read from the file system, or
426 from the archive header for archive members).
431 bfd_get_mtime (bfd
*abfd
)
438 if (bfd_stat (abfd
, &buf
) != 0)
441 abfd
->mtime
= buf
.st_mtime
; /* Save value in case anyone wants it */
450 ufile_ptr bfd_get_size (bfd *abfd);
453 Return the file size (as read from file system) for the file
454 associated with BFD @var{abfd}.
456 The initial motivation for, and use of, this routine is not
457 so we can get the exact size of the object the BFD applies to, since
458 that might not be generally possible (archive members for example).
459 It would be ideal if someone could eventually modify
460 it so that such results were guaranteed.
462 Instead, we want to ask questions like "is this NNN byte sized
463 object I'm about to try read from file offset YYY reasonable?"
464 As as example of where we might do this, some object formats
465 use string tables for which the first <<sizeof (long)>> bytes of the
466 table contain the size of the table itself, including the size bytes.
467 If an application tries to read what it thinks is one of these
468 string tables, without some way to validate the size, and for
469 some reason the size is wrong (byte swapping error, wrong location
470 for the string table, etc.), the only clue is likely to be a read
471 error when it tries to read the table, or a "virtual memory
472 exhausted" error when it tries to allocate 15 bazillon bytes
473 of space for the 15 bazillon byte table it is about to read.
474 This function at least allows us to answer the question, "is the
477 A return value of zero indicates the file size is unknown.
481 bfd_get_size (bfd
*abfd
)
483 /* A size of 0 means we haven't yet called bfd_stat. A size of 1
484 means we have a cached value of 0, ie. unknown. */
485 if (abfd
->size
<= 1 || bfd_write_p (abfd
))
489 if (abfd
->size
== 1 && !bfd_write_p (abfd
))
492 if (bfd_stat (abfd
, &buf
) != 0
494 || buf
.st_size
- (ufile_ptr
) buf
.st_size
!= 0)
499 abfd
->size
= buf
.st_size
;
509 ufile_ptr bfd_get_file_size (bfd *abfd);
512 Return the file size (as read from file system) for the file
513 associated with BFD @var{abfd}. It supports both normal files
514 and archive elements.
519 bfd_get_file_size (bfd
*abfd
)
521 ufile_ptr file_size
, archive_size
= (ufile_ptr
) -1;
523 if (abfd
->my_archive
!= NULL
524 && !bfd_is_thin_archive (abfd
->my_archive
))
526 struct areltdata
*adata
= (struct areltdata
*) abfd
->arelt_data
;
529 archive_size
= adata
->parsed_size
;
530 /* If the archive is compressed we can't compare against
532 if (adata
->arch_header
!= NULL
533 && memcmp (((struct ar_hdr
*) adata
->arch_header
)->ar_fmag
,
536 abfd
= abfd
->my_archive
;
540 file_size
= bfd_get_size (abfd
);
541 if (archive_size
< file_size
)
551 void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
552 int prot, int flags, file_ptr offset,
553 void **map_addr, bfd_size_type *map_len);
556 Return mmap()ed region of the file, if possible and implemented.
557 LEN and OFFSET do not need to be page aligned. The page aligned
558 address and length are written to MAP_ADDR and MAP_LEN.
563 bfd_mmap (bfd
*abfd
, void *addr
, bfd_size_type len
,
564 int prot
, int flags
, file_ptr offset
,
565 void **map_addr
, bfd_size_type
*map_len
)
567 while (abfd
->my_archive
!= NULL
568 && !bfd_is_thin_archive (abfd
->my_archive
))
570 offset
+= abfd
->origin
;
571 abfd
= abfd
->my_archive
;
573 offset
+= abfd
->origin
;
575 if (abfd
->iovec
== NULL
)
577 bfd_set_error (bfd_error_invalid_operation
);
581 return abfd
->iovec
->bmmap (abfd
, addr
, len
, prot
, flags
, offset
,
585 /* Memory file I/O operations. */
588 memory_bread (bfd
*abfd
, void *ptr
, file_ptr size
)
590 struct bfd_in_memory
*bim
;
593 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
595 if (abfd
->where
+ get
> bim
->size
)
597 if (bim
->size
< (bfd_size_type
) abfd
->where
)
600 get
= bim
->size
- abfd
->where
;
601 bfd_set_error (bfd_error_file_truncated
);
603 memcpy (ptr
, bim
->buffer
+ abfd
->where
, (size_t) get
);
608 memory_bwrite (bfd
*abfd
, const void *ptr
, file_ptr size
)
610 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
612 if (abfd
->where
+ size
> bim
->size
)
614 bfd_size_type newsize
, oldsize
;
616 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
617 bim
->size
= abfd
->where
+ size
;
618 /* Round up to cut down on memory fragmentation */
619 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
620 if (newsize
> oldsize
)
622 bim
->buffer
= (bfd_byte
*) bfd_realloc_or_free (bim
->buffer
, newsize
);
623 if (bim
->buffer
== NULL
)
628 if (newsize
> bim
->size
)
629 memset (bim
->buffer
+ bim
->size
, 0, newsize
- bim
->size
);
632 memcpy (bim
->buffer
+ abfd
->where
, ptr
, (size_t) size
);
637 memory_btell (bfd
*abfd
)
643 memory_bseek (bfd
*abfd
, file_ptr position
, int direction
)
646 struct bfd_in_memory
*bim
;
648 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
650 if (direction
== SEEK_SET
)
653 nwhere
= abfd
->where
+ position
;
662 if ((bfd_size_type
)nwhere
> bim
->size
)
664 if (abfd
->direction
== write_direction
665 || abfd
->direction
== both_direction
)
667 bfd_size_type newsize
, oldsize
;
669 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
671 /* Round up to cut down on memory fragmentation */
672 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
673 if (newsize
> oldsize
)
675 bim
->buffer
= (bfd_byte
*) bfd_realloc_or_free (bim
->buffer
, newsize
);
676 if (bim
->buffer
== NULL
)
682 memset (bim
->buffer
+ oldsize
, 0, newsize
- oldsize
);
687 abfd
->where
= bim
->size
;
689 bfd_set_error (bfd_error_file_truncated
);
697 memory_bclose (struct bfd
*abfd
)
699 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
703 abfd
->iostream
= NULL
;
709 memory_bflush (bfd
*abfd ATTRIBUTE_UNUSED
)
715 memory_bstat (bfd
*abfd
, struct stat
*statbuf
)
717 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
719 memset (statbuf
, 0, sizeof (*statbuf
));
720 statbuf
->st_size
= bim
->size
;
726 memory_bmmap (bfd
*abfd ATTRIBUTE_UNUSED
, void *addr ATTRIBUTE_UNUSED
,
727 bfd_size_type len ATTRIBUTE_UNUSED
, int prot ATTRIBUTE_UNUSED
,
728 int flags ATTRIBUTE_UNUSED
, file_ptr offset ATTRIBUTE_UNUSED
,
729 void **map_addr ATTRIBUTE_UNUSED
,
730 bfd_size_type
*map_len ATTRIBUTE_UNUSED
)
735 const struct bfd_iovec _bfd_memory_iovec
=
737 &memory_bread
, &memory_bwrite
, &memory_btell
, &memory_bseek
,
738 &memory_bclose
, &memory_bflush
, &memory_bstat
, &memory_bmmap