1 /* Low-level I/O routines for BFDs.
3 Copyright (C) 1990-2023 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);
158 /* Do not add a prefix to the null device. */
159 if (stricmp (filename
, "nul") == 0)
162 wchar_t * fullPathOffset
= fullPath
+ prefixLen
- 1;
164 GetFullPathNameW (partPath
, fullPathWSize
, fullPathOffset
, lpFilePart
);
167 /* It is non-standard for modes to exceed 16 characters. */
170 MultiByteToWideChar (cp
, 0, modes
, -1, modesW
, sizeof(modesW
));
172 FILE * file
= _wfopen (fullPath
, modesW
);
175 return close_on_exec (file
);
177 #elif defined (HAVE_FOPEN64)
178 return close_on_exec (fopen64 (filename
, modes
));
181 return close_on_exec (fopen (filename
, modes
));
191 The <<struct bfd_iovec>> contains the internal file I/O class.
192 Each <<BFD>> has an instance of this class and all file I/O is
193 routed through it (it is assumed that the instance implements
194 all methods listed below).
198 . {* To avoid problems with macros, a "b" rather than "f"
199 . prefix is prepended to each method name. *}
200 . {* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching
201 . bytes starting at PTR. Return the number of bytes actually
202 . transfered (a read past end-of-file returns less than NBYTES),
203 . or -1 (setting <<bfd_error>>) if an error occurs. *}
204 . file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes);
205 . file_ptr (*bwrite) (struct bfd *abfd, const void *ptr,
207 . {* Return the current IOSTREAM file offset, or -1 (setting <<bfd_error>>
208 . if an error occurs. *}
209 . file_ptr (*btell) (struct bfd *abfd);
210 . {* For the following, on successful completion a value of 0 is returned.
211 . Otherwise, a value of -1 is returned (and <<bfd_error>> is set). *}
212 . int (*bseek) (struct bfd *abfd, file_ptr offset, int whence);
213 . int (*bclose) (struct bfd *abfd);
214 . int (*bflush) (struct bfd *abfd);
215 . int (*bstat) (struct bfd *abfd, struct stat *sb);
216 . {* Mmap a part of the files. ADDR, LEN, PROT, FLAGS and OFFSET are the usual
217 . mmap parameter, except that LEN and OFFSET do not need to be page
218 . aligned. Returns (void *)-1 on failure, mmapped address on success.
219 . Also write in MAP_ADDR the address of the page aligned buffer and in
220 . MAP_LEN the size mapped (a page multiple). Use unmap with MAP_ADDR and
221 . MAP_LEN to unmap. *}
222 . void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len,
223 . int prot, int flags, file_ptr offset,
224 . void **map_addr, bfd_size_type *map_len);
227 .extern const struct bfd_iovec _bfd_memory_iovec;
232 /* Return value is amount read. */
235 bfd_bread (void *ptr
, bfd_size_type size
, bfd
*abfd
)
238 bfd
*element_bfd
= abfd
;
239 ufile_ptr offset
= 0;
241 while (abfd
->my_archive
!= NULL
242 && !bfd_is_thin_archive (abfd
->my_archive
))
244 offset
+= abfd
->origin
;
245 abfd
= abfd
->my_archive
;
247 offset
+= abfd
->origin
;
249 /* If this is a non-thin archive element, don't read past the end of
251 if (element_bfd
->arelt_data
!= NULL
252 && element_bfd
->my_archive
!= NULL
253 && !bfd_is_thin_archive (element_bfd
->my_archive
))
255 bfd_size_type maxbytes
= arelt_size (element_bfd
);
257 if (abfd
->where
< offset
|| abfd
->where
- offset
>= maxbytes
)
259 bfd_set_error (bfd_error_invalid_operation
);
262 if (abfd
->where
- offset
+ size
> maxbytes
)
263 size
= maxbytes
- (abfd
->where
- offset
);
266 if (abfd
->iovec
== NULL
)
268 bfd_set_error (bfd_error_invalid_operation
);
272 nread
= abfd
->iovec
->bread (abfd
, ptr
, size
);
274 abfd
->where
+= nread
;
280 bfd_bwrite (const void *ptr
, bfd_size_type size
, bfd
*abfd
)
284 while (abfd
->my_archive
!= NULL
285 && !bfd_is_thin_archive (abfd
->my_archive
))
286 abfd
= abfd
->my_archive
;
288 if (abfd
->iovec
== NULL
)
290 bfd_set_error (bfd_error_invalid_operation
);
294 nwrote
= abfd
->iovec
->bwrite (abfd
, ptr
, size
);
296 abfd
->where
+= nwrote
;
297 if ((bfd_size_type
) nwrote
!= size
)
302 bfd_set_error (bfd_error_system_call
);
310 ufile_ptr offset
= 0;
313 while (abfd
->my_archive
!= NULL
314 && !bfd_is_thin_archive (abfd
->my_archive
))
316 offset
+= abfd
->origin
;
317 abfd
= abfd
->my_archive
;
319 offset
+= abfd
->origin
;
321 if (abfd
->iovec
== NULL
)
324 ptr
= abfd
->iovec
->btell (abfd
);
330 bfd_flush (bfd
*abfd
)
332 while (abfd
->my_archive
!= NULL
333 && !bfd_is_thin_archive (abfd
->my_archive
))
334 abfd
= abfd
->my_archive
;
336 if (abfd
->iovec
== NULL
)
339 return abfd
->iovec
->bflush (abfd
);
342 /* Returns 0 for success, negative value for failure (in which case
343 bfd_get_error can retrieve the error code). */
345 bfd_stat (bfd
*abfd
, struct stat
*statbuf
)
349 while (abfd
->my_archive
!= NULL
350 && !bfd_is_thin_archive (abfd
->my_archive
))
351 abfd
= abfd
->my_archive
;
353 if (abfd
->iovec
== NULL
)
355 bfd_set_error (bfd_error_invalid_operation
);
359 result
= abfd
->iovec
->bstat (abfd
, statbuf
);
361 bfd_set_error (bfd_error_system_call
);
365 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
366 can retrieve the error code). */
369 bfd_seek (bfd
*abfd
, file_ptr position
, int direction
)
372 ufile_ptr offset
= 0;
374 while (abfd
->my_archive
!= NULL
375 && !bfd_is_thin_archive (abfd
->my_archive
))
377 offset
+= abfd
->origin
;
378 abfd
= abfd
->my_archive
;
380 offset
+= abfd
->origin
;
382 if (abfd
->iovec
== NULL
)
384 bfd_set_error (bfd_error_invalid_operation
);
388 /* For the time being, a BFD may not seek to it's end. The problem
389 is that we don't easily have a way to recognize the end of an
390 element in an archive. */
391 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
393 if (direction
!= SEEK_CUR
)
396 if ((direction
== SEEK_CUR
&& position
== 0)
397 || (direction
== SEEK_SET
&& (ufile_ptr
) position
== abfd
->where
))
400 result
= abfd
->iovec
->bseek (abfd
, position
, direction
);
403 /* An EINVAL error probably means that the file offset was
406 bfd_set_error (bfd_error_file_truncated
);
408 bfd_set_error (bfd_error_system_call
);
412 /* Adjust `where' field. */
413 if (direction
== SEEK_CUR
)
414 abfd
->where
+= position
;
416 abfd
->where
= position
;
427 long bfd_get_mtime (bfd *abfd);
430 Return the file modification time (as read from the file system, or
431 from the archive header for archive members).
436 bfd_get_mtime (bfd
*abfd
)
443 if (bfd_stat (abfd
, &buf
) != 0)
446 abfd
->mtime
= buf
.st_mtime
; /* Save value in case anyone wants it */
455 ufile_ptr bfd_get_size (bfd *abfd);
458 Return the file size (as read from file system) for the file
459 associated with BFD @var{abfd}.
461 The initial motivation for, and use of, this routine is not
462 so we can get the exact size of the object the BFD applies to, since
463 that might not be generally possible (archive members for example).
464 It would be ideal if someone could eventually modify
465 it so that such results were guaranteed.
467 Instead, we want to ask questions like "is this NNN byte sized
468 object I'm about to try read from file offset YYY reasonable?"
469 As as example of where we might do this, some object formats
470 use string tables for which the first <<sizeof (long)>> bytes of the
471 table contain the size of the table itself, including the size bytes.
472 If an application tries to read what it thinks is one of these
473 string tables, without some way to validate the size, and for
474 some reason the size is wrong (byte swapping error, wrong location
475 for the string table, etc.), the only clue is likely to be a read
476 error when it tries to read the table, or a "virtual memory
477 exhausted" error when it tries to allocate 15 bazillon bytes
478 of space for the 15 bazillon byte table it is about to read.
479 This function at least allows us to answer the question, "is the
482 A return value of zero indicates the file size is unknown.
486 bfd_get_size (bfd
*abfd
)
488 /* A size of 0 means we haven't yet called bfd_stat. A size of 1
489 means we have a cached value of 0, ie. unknown. */
490 if (abfd
->size
<= 1 || bfd_write_p (abfd
))
494 if (abfd
->size
== 1 && !bfd_write_p (abfd
))
497 if (bfd_stat (abfd
, &buf
) != 0
499 || buf
.st_size
- (ufile_ptr
) buf
.st_size
!= 0)
504 abfd
->size
= buf
.st_size
;
514 ufile_ptr bfd_get_file_size (bfd *abfd);
517 Return the file size (as read from file system) for the file
518 associated with BFD @var{abfd}. It supports both normal files
519 and archive elements.
524 bfd_get_file_size (bfd
*abfd
)
526 ufile_ptr file_size
, archive_size
= (ufile_ptr
) -1;
528 if (abfd
->my_archive
!= NULL
529 && !bfd_is_thin_archive (abfd
->my_archive
))
531 struct areltdata
*adata
= (struct areltdata
*) abfd
->arelt_data
;
534 archive_size
= adata
->parsed_size
;
535 /* If the archive is compressed we can't compare against
537 if (adata
->arch_header
!= NULL
538 && memcmp (((struct ar_hdr
*) adata
->arch_header
)->ar_fmag
,
541 abfd
= abfd
->my_archive
;
545 file_size
= bfd_get_size (abfd
);
546 if (archive_size
< file_size
)
556 void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
557 int prot, int flags, file_ptr offset,
558 void **map_addr, bfd_size_type *map_len);
561 Return mmap()ed region of the file, if possible and implemented.
562 LEN and OFFSET do not need to be page aligned. The page aligned
563 address and length are written to MAP_ADDR and MAP_LEN.
568 bfd_mmap (bfd
*abfd
, void *addr
, bfd_size_type len
,
569 int prot
, int flags
, file_ptr offset
,
570 void **map_addr
, bfd_size_type
*map_len
)
572 while (abfd
->my_archive
!= NULL
573 && !bfd_is_thin_archive (abfd
->my_archive
))
575 offset
+= abfd
->origin
;
576 abfd
= abfd
->my_archive
;
578 offset
+= abfd
->origin
;
580 if (abfd
->iovec
== NULL
)
582 bfd_set_error (bfd_error_invalid_operation
);
586 return abfd
->iovec
->bmmap (abfd
, addr
, len
, prot
, flags
, offset
,
590 /* Memory file I/O operations. */
593 memory_bread (bfd
*abfd
, void *ptr
, file_ptr size
)
595 struct bfd_in_memory
*bim
;
598 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
600 if (abfd
->where
+ get
> bim
->size
)
602 if (bim
->size
< (bfd_size_type
) abfd
->where
)
605 get
= bim
->size
- abfd
->where
;
606 bfd_set_error (bfd_error_file_truncated
);
608 memcpy (ptr
, bim
->buffer
+ abfd
->where
, (size_t) get
);
613 memory_bwrite (bfd
*abfd
, const void *ptr
, file_ptr size
)
615 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
617 if (abfd
->where
+ size
> bim
->size
)
619 bfd_size_type newsize
, oldsize
;
621 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
622 bim
->size
= abfd
->where
+ size
;
623 /* Round up to cut down on memory fragmentation */
624 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
625 if (newsize
> oldsize
)
627 bim
->buffer
= (bfd_byte
*) bfd_realloc_or_free (bim
->buffer
, newsize
);
628 if (bim
->buffer
== NULL
)
633 if (newsize
> bim
->size
)
634 memset (bim
->buffer
+ bim
->size
, 0, newsize
- bim
->size
);
637 memcpy (bim
->buffer
+ abfd
->where
, ptr
, (size_t) size
);
642 memory_btell (bfd
*abfd
)
648 memory_bseek (bfd
*abfd
, file_ptr position
, int direction
)
651 struct bfd_in_memory
*bim
;
653 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
655 if (direction
== SEEK_SET
)
658 nwhere
= abfd
->where
+ position
;
667 if ((bfd_size_type
)nwhere
> bim
->size
)
669 if (abfd
->direction
== write_direction
670 || abfd
->direction
== both_direction
)
672 bfd_size_type newsize
, oldsize
;
674 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
676 /* Round up to cut down on memory fragmentation */
677 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
678 if (newsize
> oldsize
)
680 bim
->buffer
= (bfd_byte
*) bfd_realloc_or_free (bim
->buffer
, newsize
);
681 if (bim
->buffer
== NULL
)
687 memset (bim
->buffer
+ oldsize
, 0, newsize
- oldsize
);
692 abfd
->where
= bim
->size
;
694 bfd_set_error (bfd_error_file_truncated
);
702 memory_bclose (struct bfd
*abfd
)
704 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
708 abfd
->iostream
= NULL
;
714 memory_bflush (bfd
*abfd ATTRIBUTE_UNUSED
)
720 memory_bstat (bfd
*abfd
, struct stat
*statbuf
)
722 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
724 memset (statbuf
, 0, sizeof (*statbuf
));
725 statbuf
->st_size
= bim
->size
;
731 memory_bmmap (bfd
*abfd ATTRIBUTE_UNUSED
, void *addr ATTRIBUTE_UNUSED
,
732 bfd_size_type len ATTRIBUTE_UNUSED
, int prot ATTRIBUTE_UNUSED
,
733 int flags ATTRIBUTE_UNUSED
, file_ptr offset ATTRIBUTE_UNUSED
,
734 void **map_addr ATTRIBUTE_UNUSED
,
735 bfd_size_type
*map_len ATTRIBUTE_UNUSED
)
740 const struct bfd_iovec _bfd_memory_iovec
=
742 &memory_bread
, &memory_bwrite
, &memory_btell
, &memory_bseek
,
743 &memory_bclose
, &memory_bflush
, &memory_bstat
, &memory_bmmap