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. */
34 #if defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 9
35 /* This prototype was added to locale.h in version 9.0 of MinGW-w64. */
36 _CRTIMP
unsigned int __cdecl
___lc_codepage_func(void);
40 #define S_IXUSR 0100 /* Execute by owner. */
43 #define S_IXGRP 0010 /* Execute by group. */
46 #define S_IXOTH 0001 /* Execute by others. */
54 _bfd_real_ftell (FILE *file
)
56 #if defined (HAVE_FTELLO64)
57 return ftello64 (file
);
58 #elif defined (HAVE_FTELLO)
66 _bfd_real_fseek (FILE *file
, file_ptr offset
, int whence
)
68 #if defined (HAVE_FSEEKO64)
69 return fseeko64 (file
, offset
, whence
);
70 #elif defined (HAVE_FSEEKO)
71 return fseeko (file
, offset
, whence
);
73 return fseek (file
, offset
, whence
);
77 /* Mark FILE as close-on-exec. Return FILE. FILE may be NULL, in
78 which case nothing is done. */
80 close_on_exec (FILE *file
)
82 #if defined (HAVE_FILENO) && defined (F_GETFD)
85 int fd
= fileno (file
);
86 int old
= fcntl (fd
, F_GETFD
, 0);
88 fcntl (fd
, F_SETFD
, old
| FD_CLOEXEC
);
95 _bfd_real_fopen (const char *filename
, const char *modes
)
100 /* On VMS, fopen allows file attributes as optional arguments.
101 We need to use them but we'd better to use the common prototype.
102 In fopen-vms.h, they are separated from the mode with a comma.
104 vms_attr
= strchr (modes
, ',');
105 if (vms_attr
!= NULL
)
107 /* Attributes found. Split. */
108 size_t modes_len
= strlen (modes
) + 1;
109 char attrs
[modes_len
+ 1];
113 memcpy (attrs
, modes
, modes_len
);
115 for (i
= 0; i
< 2; i
++)
117 at
[i
+ 1] = strchr (at
[i
], ',');
118 BFD_ASSERT (at
[i
+ 1] != NULL
);
119 *(at
[i
+ 1]++) = 0; /* Replace ',' with a nul, and skip it. */
121 return close_on_exec (fopen (filename
, at
[0], at
[1], at
[2]));
124 #elif defined (_WIN32)
125 /* PR 25713: Handle extra long path names possibly containing '..' and '.'. */
126 wchar_t ** lpFilePart
= {NULL
};
127 const wchar_t prefix
[] = L
"\\\\?\\";
128 const size_t partPathLen
= strlen (filename
) + 1;
130 const unsigned int cp
= ___lc_codepage_func();
132 const unsigned int cp
= CP_UTF8
;
135 /* Converting the partial path from ascii to unicode.
136 1) Get the length: Calling with lpWideCharStr set to null returns the length.
137 2) Convert the string: Calling with cbMultiByte set to -1 includes the terminating null. */
138 size_t partPathWSize
= MultiByteToWideChar (cp
, 0, filename
, -1, NULL
, 0);
139 wchar_t * partPath
= calloc (partPathWSize
, sizeof(wchar_t));
142 MultiByteToWideChar (cp
, 0, filename
, -1, partPath
, partPathWSize
);
144 /* Convert any UNIX style path separators into the DOS i.e. backslash separator. */
145 for (ix
= 0; ix
< partPathLen
; ix
++)
146 if (IS_UNIX_DIR_SEPARATOR(filename
[ix
]))
149 /* Getting the full path from the provided partial path.
151 2) Resolve the path. */
152 long fullPathWSize
= GetFullPathNameW (partPath
, 0, NULL
, lpFilePart
);
153 wchar_t * fullPath
= calloc (fullPathWSize
+ sizeof(prefix
) + 1, sizeof(wchar_t));
155 wcscpy (fullPath
, prefix
);
157 int prefixLen
= sizeof(prefix
) / sizeof(wchar_t);
158 wchar_t * fullPathOffset
= fullPath
+ prefixLen
- 1;
160 GetFullPathNameW (partPath
, fullPathWSize
, fullPathOffset
, lpFilePart
);
163 /* It is non-standard for modes to exceed 16 characters. */
166 MultiByteToWideChar (cp
, 0, modes
, -1, modesW
, sizeof(modesW
));
168 FILE * file
= _wfopen (fullPath
, modesW
);
171 return close_on_exec (file
);
173 #elif defined (HAVE_FOPEN64)
174 return close_on_exec (fopen64 (filename
, modes
));
177 return close_on_exec (fopen (filename
, modes
));
187 The <<struct bfd_iovec>> contains the internal file I/O class.
188 Each <<BFD>> has an instance of this class and all file I/O is
189 routed through it (it is assumed that the instance implements
190 all methods listed below).
194 . {* To avoid problems with macros, a "b" rather than "f"
195 . prefix is prepended to each method name. *}
196 . {* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching
197 . bytes starting at PTR. Return the number of bytes actually
198 . transfered (a read past end-of-file returns less than NBYTES),
199 . or -1 (setting <<bfd_error>>) if an error occurs. *}
200 . file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes);
201 . file_ptr (*bwrite) (struct bfd *abfd, const void *ptr,
203 . {* Return the current IOSTREAM file offset, or -1 (setting <<bfd_error>>
204 . if an error occurs. *}
205 . file_ptr (*btell) (struct bfd *abfd);
206 . {* For the following, on successful completion a value of 0 is returned.
207 . Otherwise, a value of -1 is returned (and <<bfd_error>> is set). *}
208 . int (*bseek) (struct bfd *abfd, file_ptr offset, int whence);
209 . int (*bclose) (struct bfd *abfd);
210 . int (*bflush) (struct bfd *abfd);
211 . int (*bstat) (struct bfd *abfd, struct stat *sb);
212 . {* Mmap a part of the files. ADDR, LEN, PROT, FLAGS and OFFSET are the usual
213 . mmap parameter, except that LEN and OFFSET do not need to be page
214 . aligned. Returns (void *)-1 on failure, mmapped address on success.
215 . Also write in MAP_ADDR the address of the page aligned buffer and in
216 . MAP_LEN the size mapped (a page multiple). Use unmap with MAP_ADDR and
217 . MAP_LEN to unmap. *}
218 . void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len,
219 . int prot, int flags, file_ptr offset,
220 . void **map_addr, bfd_size_type *map_len);
223 .extern const struct bfd_iovec _bfd_memory_iovec;
228 /* Return value is amount read. */
231 bfd_bread (void *ptr
, bfd_size_type size
, bfd
*abfd
)
234 bfd
*element_bfd
= abfd
;
235 ufile_ptr offset
= 0;
237 while (abfd
->my_archive
!= NULL
238 && !bfd_is_thin_archive (abfd
->my_archive
))
240 offset
+= abfd
->origin
;
241 abfd
= abfd
->my_archive
;
243 offset
+= abfd
->origin
;
245 /* If this is a non-thin archive element, don't read past the end of
247 if (element_bfd
->arelt_data
!= NULL
248 && element_bfd
->my_archive
!= NULL
249 && !bfd_is_thin_archive (element_bfd
->my_archive
))
251 bfd_size_type maxbytes
= arelt_size (element_bfd
);
253 if (abfd
->where
< offset
|| abfd
->where
- offset
>= maxbytes
)
255 bfd_set_error (bfd_error_invalid_operation
);
258 if (abfd
->where
- offset
+ size
> maxbytes
)
259 size
= maxbytes
- (abfd
->where
- offset
);
262 if (abfd
->iovec
== NULL
)
264 bfd_set_error (bfd_error_invalid_operation
);
268 nread
= abfd
->iovec
->bread (abfd
, ptr
, size
);
270 abfd
->where
+= nread
;
276 bfd_bwrite (const void *ptr
, bfd_size_type size
, bfd
*abfd
)
280 while (abfd
->my_archive
!= NULL
281 && !bfd_is_thin_archive (abfd
->my_archive
))
282 abfd
= abfd
->my_archive
;
284 if (abfd
->iovec
== NULL
)
286 bfd_set_error (bfd_error_invalid_operation
);
290 nwrote
= abfd
->iovec
->bwrite (abfd
, ptr
, size
);
292 abfd
->where
+= nwrote
;
293 if ((bfd_size_type
) nwrote
!= size
)
298 bfd_set_error (bfd_error_system_call
);
306 ufile_ptr offset
= 0;
309 while (abfd
->my_archive
!= NULL
310 && !bfd_is_thin_archive (abfd
->my_archive
))
312 offset
+= abfd
->origin
;
313 abfd
= abfd
->my_archive
;
315 offset
+= abfd
->origin
;
317 if (abfd
->iovec
== NULL
)
320 ptr
= abfd
->iovec
->btell (abfd
);
326 bfd_flush (bfd
*abfd
)
328 while (abfd
->my_archive
!= NULL
329 && !bfd_is_thin_archive (abfd
->my_archive
))
330 abfd
= abfd
->my_archive
;
332 if (abfd
->iovec
== NULL
)
335 return abfd
->iovec
->bflush (abfd
);
338 /* Returns 0 for success, negative value for failure (in which case
339 bfd_get_error can retrieve the error code). */
341 bfd_stat (bfd
*abfd
, struct stat
*statbuf
)
345 while (abfd
->my_archive
!= NULL
346 && !bfd_is_thin_archive (abfd
->my_archive
))
347 abfd
= abfd
->my_archive
;
349 if (abfd
->iovec
== NULL
)
351 bfd_set_error (bfd_error_invalid_operation
);
355 result
= abfd
->iovec
->bstat (abfd
, statbuf
);
357 bfd_set_error (bfd_error_system_call
);
361 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
362 can retrieve the error code). */
365 bfd_seek (bfd
*abfd
, file_ptr position
, int direction
)
368 ufile_ptr offset
= 0;
370 while (abfd
->my_archive
!= NULL
371 && !bfd_is_thin_archive (abfd
->my_archive
))
373 offset
+= abfd
->origin
;
374 abfd
= abfd
->my_archive
;
376 offset
+= abfd
->origin
;
378 if (abfd
->iovec
== NULL
)
380 bfd_set_error (bfd_error_invalid_operation
);
384 /* For the time being, a BFD may not seek to it's end. The problem
385 is that we don't easily have a way to recognize the end of an
386 element in an archive. */
387 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
389 if (direction
!= SEEK_CUR
)
392 if ((direction
== SEEK_CUR
&& position
== 0)
393 || (direction
== SEEK_SET
&& (ufile_ptr
) position
== abfd
->where
))
396 result
= abfd
->iovec
->bseek (abfd
, position
, direction
);
399 /* An EINVAL error probably means that the file offset was
402 bfd_set_error (bfd_error_file_truncated
);
404 bfd_set_error (bfd_error_system_call
);
408 /* Adjust `where' field. */
409 if (direction
== SEEK_CUR
)
410 abfd
->where
+= position
;
412 abfd
->where
= position
;
423 long bfd_get_mtime (bfd *abfd);
426 Return the file modification time (as read from the file system, or
427 from the archive header for archive members).
432 bfd_get_mtime (bfd
*abfd
)
439 if (bfd_stat (abfd
, &buf
) != 0)
442 abfd
->mtime
= buf
.st_mtime
; /* Save value in case anyone wants it */
451 ufile_ptr bfd_get_size (bfd *abfd);
454 Return the file size (as read from file system) for the file
455 associated with BFD @var{abfd}.
457 The initial motivation for, and use of, this routine is not
458 so we can get the exact size of the object the BFD applies to, since
459 that might not be generally possible (archive members for example).
460 It would be ideal if someone could eventually modify
461 it so that such results were guaranteed.
463 Instead, we want to ask questions like "is this NNN byte sized
464 object I'm about to try read from file offset YYY reasonable?"
465 As as example of where we might do this, some object formats
466 use string tables for which the first <<sizeof (long)>> bytes of the
467 table contain the size of the table itself, including the size bytes.
468 If an application tries to read what it thinks is one of these
469 string tables, without some way to validate the size, and for
470 some reason the size is wrong (byte swapping error, wrong location
471 for the string table, etc.), the only clue is likely to be a read
472 error when it tries to read the table, or a "virtual memory
473 exhausted" error when it tries to allocate 15 bazillon bytes
474 of space for the 15 bazillon byte table it is about to read.
475 This function at least allows us to answer the question, "is the
478 A return value of zero indicates the file size is unknown.
482 bfd_get_size (bfd
*abfd
)
484 /* A size of 0 means we haven't yet called bfd_stat. A size of 1
485 means we have a cached value of 0, ie. unknown. */
486 if (abfd
->size
<= 1 || bfd_write_p (abfd
))
490 if (abfd
->size
== 1 && !bfd_write_p (abfd
))
493 if (bfd_stat (abfd
, &buf
) != 0
495 || buf
.st_size
- (ufile_ptr
) buf
.st_size
!= 0)
500 abfd
->size
= buf
.st_size
;
510 ufile_ptr bfd_get_file_size (bfd *abfd);
513 Return the file size (as read from file system) for the file
514 associated with BFD @var{abfd}. It supports both normal files
515 and archive elements.
520 bfd_get_file_size (bfd
*abfd
)
522 ufile_ptr file_size
, archive_size
= (ufile_ptr
) -1;
524 if (abfd
->my_archive
!= NULL
525 && !bfd_is_thin_archive (abfd
->my_archive
))
527 struct areltdata
*adata
= (struct areltdata
*) abfd
->arelt_data
;
530 archive_size
= adata
->parsed_size
;
531 /* If the archive is compressed we can't compare against
533 if (adata
->arch_header
!= NULL
534 && memcmp (((struct ar_hdr
*) adata
->arch_header
)->ar_fmag
,
537 abfd
= abfd
->my_archive
;
541 file_size
= bfd_get_size (abfd
);
542 if (archive_size
< file_size
)
552 void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
553 int prot, int flags, file_ptr offset,
554 void **map_addr, bfd_size_type *map_len);
557 Return mmap()ed region of the file, if possible and implemented.
558 LEN and OFFSET do not need to be page aligned. The page aligned
559 address and length are written to MAP_ADDR and MAP_LEN.
564 bfd_mmap (bfd
*abfd
, void *addr
, bfd_size_type len
,
565 int prot
, int flags
, file_ptr offset
,
566 void **map_addr
, bfd_size_type
*map_len
)
568 while (abfd
->my_archive
!= NULL
569 && !bfd_is_thin_archive (abfd
->my_archive
))
571 offset
+= abfd
->origin
;
572 abfd
= abfd
->my_archive
;
574 offset
+= abfd
->origin
;
576 if (abfd
->iovec
== NULL
)
578 bfd_set_error (bfd_error_invalid_operation
);
582 return abfd
->iovec
->bmmap (abfd
, addr
, len
, prot
, flags
, offset
,
586 /* Memory file I/O operations. */
589 memory_bread (bfd
*abfd
, void *ptr
, file_ptr size
)
591 struct bfd_in_memory
*bim
;
594 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
596 if (abfd
->where
+ get
> bim
->size
)
598 if (bim
->size
< (bfd_size_type
) abfd
->where
)
601 get
= bim
->size
- abfd
->where
;
602 bfd_set_error (bfd_error_file_truncated
);
604 memcpy (ptr
, bim
->buffer
+ abfd
->where
, (size_t) get
);
609 memory_bwrite (bfd
*abfd
, const void *ptr
, file_ptr size
)
611 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
613 if (abfd
->where
+ size
> bim
->size
)
615 bfd_size_type newsize
, oldsize
;
617 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
618 bim
->size
= abfd
->where
+ size
;
619 /* Round up to cut down on memory fragmentation */
620 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
621 if (newsize
> oldsize
)
623 bim
->buffer
= (bfd_byte
*) bfd_realloc_or_free (bim
->buffer
, newsize
);
624 if (bim
->buffer
== NULL
)
629 if (newsize
> bim
->size
)
630 memset (bim
->buffer
+ bim
->size
, 0, newsize
- bim
->size
);
633 memcpy (bim
->buffer
+ abfd
->where
, ptr
, (size_t) size
);
638 memory_btell (bfd
*abfd
)
644 memory_bseek (bfd
*abfd
, file_ptr position
, int direction
)
647 struct bfd_in_memory
*bim
;
649 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
651 if (direction
== SEEK_SET
)
654 nwhere
= abfd
->where
+ position
;
663 if ((bfd_size_type
)nwhere
> bim
->size
)
665 if (abfd
->direction
== write_direction
666 || abfd
->direction
== both_direction
)
668 bfd_size_type newsize
, oldsize
;
670 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
672 /* Round up to cut down on memory fragmentation */
673 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
674 if (newsize
> oldsize
)
676 bim
->buffer
= (bfd_byte
*) bfd_realloc_or_free (bim
->buffer
, newsize
);
677 if (bim
->buffer
== NULL
)
683 memset (bim
->buffer
+ oldsize
, 0, newsize
- oldsize
);
688 abfd
->where
= bim
->size
;
690 bfd_set_error (bfd_error_file_truncated
);
698 memory_bclose (struct bfd
*abfd
)
700 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
704 abfd
->iostream
= NULL
;
710 memory_bflush (bfd
*abfd ATTRIBUTE_UNUSED
)
716 memory_bstat (bfd
*abfd
, struct stat
*statbuf
)
718 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
720 memset (statbuf
, 0, sizeof (*statbuf
));
721 statbuf
->st_size
= bim
->size
;
727 memory_bmmap (bfd
*abfd ATTRIBUTE_UNUSED
, void *addr ATTRIBUTE_UNUSED
,
728 bfd_size_type len ATTRIBUTE_UNUSED
, int prot ATTRIBUTE_UNUSED
,
729 int flags ATTRIBUTE_UNUSED
, file_ptr offset ATTRIBUTE_UNUSED
,
730 void **map_addr ATTRIBUTE_UNUSED
,
731 bfd_size_type
*map_len ATTRIBUTE_UNUSED
)
736 const struct bfd_iovec _bfd_memory_iovec
=
738 &memory_bread
, &memory_bwrite
, &memory_btell
, &memory_bseek
,
739 &memory_bclose
, &memory_bflush
, &memory_bstat
, &memory_bmmap