go32 sanity check
[binutils-gdb.git] / bfd / bfdio.c
1 /* Low-level I/O routines for BFDs.
2
3 Copyright (C) 1990-2022 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 #include "aout/ar.h"
29 #if defined (_WIN32)
30 #include <windows.h>
31 #include <locale.h>
32 #endif
33
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);
37 #endif
38
39 #ifndef S_IXUSR
40 #define S_IXUSR 0100 /* Execute by owner. */
41 #endif
42 #ifndef S_IXGRP
43 #define S_IXGRP 0010 /* Execute by group. */
44 #endif
45 #ifndef S_IXOTH
46 #define S_IXOTH 0001 /* Execute by others. */
47 #endif
48
49 #ifndef FD_CLOEXEC
50 #define FD_CLOEXEC 1
51 #endif
52
53 file_ptr
54 _bfd_real_ftell (FILE *file)
55 {
56 #if defined (HAVE_FTELLO64)
57 return ftello64 (file);
58 #elif defined (HAVE_FTELLO)
59 return ftello (file);
60 #else
61 return ftell (file);
62 #endif
63 }
64
65 int
66 _bfd_real_fseek (FILE *file, file_ptr offset, int whence)
67 {
68 #if defined (HAVE_FSEEKO64)
69 return fseeko64 (file, offset, whence);
70 #elif defined (HAVE_FSEEKO)
71 return fseeko (file, offset, whence);
72 #else
73 return fseek (file, offset, whence);
74 #endif
75 }
76
77 /* Mark FILE as close-on-exec. Return FILE. FILE may be NULL, in
78 which case nothing is done. */
79 static FILE *
80 close_on_exec (FILE *file)
81 {
82 #if defined (HAVE_FILENO) && defined (F_GETFD)
83 if (file)
84 {
85 int fd = fileno (file);
86 int old = fcntl (fd, F_GETFD, 0);
87 if (old >= 0)
88 fcntl (fd, F_SETFD, old | FD_CLOEXEC);
89 }
90 #endif
91 return file;
92 }
93
94 FILE *
95 _bfd_real_fopen (const char *filename, const char *modes)
96 {
97 #ifdef VMS
98 char *vms_attr;
99
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.
103 Split here. */
104 vms_attr = strchr (modes, ',');
105 if (vms_attr != NULL)
106 {
107 /* Attributes found. Split. */
108 size_t modes_len = strlen (modes) + 1;
109 char attrs[modes_len + 1];
110 char *at[3];
111 int i;
112
113 memcpy (attrs, modes, modes_len);
114 at[0] = attrs;
115 for (i = 0; i < 2; i++)
116 {
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. */
120 }
121 return close_on_exec (fopen (filename, at[0], at[1], at[2]));
122 }
123
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;
129 #ifdef __MINGW32__
130 const unsigned int cp = ___lc_codepage_func();
131 #else
132 const unsigned int cp = CP_UTF8;
133 #endif
134
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));
140 size_t ix;
141
142 MultiByteToWideChar (cp, 0, filename, -1, partPath, partPathWSize);
143
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]))
147 partPath[ix] = '\\';
148
149 /* Getting the full path from the provided partial path.
150 1) Get the length.
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));
154
155 wcscpy (fullPath, prefix);
156
157 int prefixLen = sizeof(prefix) / sizeof(wchar_t);
158 wchar_t * fullPathOffset = fullPath + prefixLen - 1;
159
160 GetFullPathNameW (partPath, fullPathWSize, fullPathOffset, lpFilePart);
161 free (partPath);
162
163 /* It is non-standard for modes to exceed 16 characters. */
164 wchar_t modesW[16];
165
166 MultiByteToWideChar (cp, 0, modes, -1, modesW, sizeof(modesW));
167
168 FILE * file = _wfopen (fullPath, modesW);
169 free (fullPath);
170
171 return close_on_exec (file);
172
173 #elif defined (HAVE_FOPEN64)
174 return close_on_exec (fopen64 (filename, modes));
175
176 #else
177 return close_on_exec (fopen (filename, modes));
178 #endif
179 }
180
181 /*
182 INTERNAL_DEFINITION
183 struct bfd_iovec
184
185 DESCRIPTION
186
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).
191
192 .struct bfd_iovec
193 .{
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,
202 . file_ptr nbytes);
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);
221 .};
222
223 .extern const struct bfd_iovec _bfd_memory_iovec;
224
225 */
226
227
228 /* Return value is amount read. */
229
230 bfd_size_type
231 bfd_bread (void *ptr, bfd_size_type size, bfd *abfd)
232 {
233 file_ptr nread;
234 bfd *element_bfd = abfd;
235 ufile_ptr offset = 0;
236
237 while (abfd->my_archive != NULL
238 && !bfd_is_thin_archive (abfd->my_archive))
239 {
240 offset += abfd->origin;
241 abfd = abfd->my_archive;
242 }
243 offset += abfd->origin;
244
245 /* If this is a non-thin archive element, don't read past the end of
246 this element. */
247 if (element_bfd->arelt_data != NULL
248 && element_bfd->my_archive != NULL
249 && !bfd_is_thin_archive (element_bfd->my_archive))
250 {
251 bfd_size_type maxbytes = arelt_size (element_bfd);
252
253 if (abfd->where < offset || abfd->where - offset >= maxbytes)
254 {
255 bfd_set_error (bfd_error_invalid_operation);
256 return -1;
257 }
258 if (abfd->where - offset + size > maxbytes)
259 size = maxbytes - (abfd->where - offset);
260 }
261
262 if (abfd->iovec == NULL)
263 {
264 bfd_set_error (bfd_error_invalid_operation);
265 return -1;
266 }
267
268 nread = abfd->iovec->bread (abfd, ptr, size);
269 if (nread != -1)
270 abfd->where += nread;
271
272 return nread;
273 }
274
275 bfd_size_type
276 bfd_bwrite (const void *ptr, bfd_size_type size, bfd *abfd)
277 {
278 file_ptr nwrote;
279
280 while (abfd->my_archive != NULL
281 && !bfd_is_thin_archive (abfd->my_archive))
282 abfd = abfd->my_archive;
283
284 if (abfd->iovec == NULL)
285 {
286 bfd_set_error (bfd_error_invalid_operation);
287 return -1;
288 }
289
290 nwrote = abfd->iovec->bwrite (abfd, ptr, size);
291 if (nwrote != -1)
292 abfd->where += nwrote;
293 if ((bfd_size_type) nwrote != size)
294 {
295 #ifdef ENOSPC
296 errno = ENOSPC;
297 #endif
298 bfd_set_error (bfd_error_system_call);
299 }
300 return nwrote;
301 }
302
303 file_ptr
304 bfd_tell (bfd *abfd)
305 {
306 ufile_ptr offset = 0;
307 file_ptr ptr;
308
309 while (abfd->my_archive != NULL
310 && !bfd_is_thin_archive (abfd->my_archive))
311 {
312 offset += abfd->origin;
313 abfd = abfd->my_archive;
314 }
315 offset += abfd->origin;
316
317 if (abfd->iovec == NULL)
318 return 0;
319
320 ptr = abfd->iovec->btell (abfd);
321 abfd->where = ptr;
322 return ptr - offset;
323 }
324
325 int
326 bfd_flush (bfd *abfd)
327 {
328 while (abfd->my_archive != NULL
329 && !bfd_is_thin_archive (abfd->my_archive))
330 abfd = abfd->my_archive;
331
332 if (abfd->iovec == NULL)
333 return 0;
334
335 return abfd->iovec->bflush (abfd);
336 }
337
338 /* Returns 0 for success, negative value for failure (in which case
339 bfd_get_error can retrieve the error code). */
340 int
341 bfd_stat (bfd *abfd, struct stat *statbuf)
342 {
343 int result;
344
345 while (abfd->my_archive != NULL
346 && !bfd_is_thin_archive (abfd->my_archive))
347 abfd = abfd->my_archive;
348
349 if (abfd->iovec == NULL)
350 {
351 bfd_set_error (bfd_error_invalid_operation);
352 return -1;
353 }
354
355 result = abfd->iovec->bstat (abfd, statbuf);
356 if (result < 0)
357 bfd_set_error (bfd_error_system_call);
358 return result;
359 }
360
361 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
362 can retrieve the error code). */
363
364 int
365 bfd_seek (bfd *abfd, file_ptr position, int direction)
366 {
367 int result;
368 ufile_ptr offset = 0;
369
370 while (abfd->my_archive != NULL
371 && !bfd_is_thin_archive (abfd->my_archive))
372 {
373 offset += abfd->origin;
374 abfd = abfd->my_archive;
375 }
376 offset += abfd->origin;
377
378 if (abfd->iovec == NULL)
379 {
380 bfd_set_error (bfd_error_invalid_operation);
381 return -1;
382 }
383
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);
388
389 if (direction != SEEK_CUR)
390 position += offset;
391
392 if ((direction == SEEK_CUR && position == 0)
393 || (direction == SEEK_SET && (ufile_ptr) position == abfd->where))
394 return 0;
395
396 result = abfd->iovec->bseek (abfd, position, direction);
397 if (result != 0)
398 {
399 /* An EINVAL error probably means that the file offset was
400 absurd. */
401 if (errno == EINVAL)
402 bfd_set_error (bfd_error_file_truncated);
403 else
404 bfd_set_error (bfd_error_system_call);
405 }
406 else
407 {
408 /* Adjust `where' field. */
409 if (direction == SEEK_CUR)
410 abfd->where += position;
411 else
412 abfd->where = position;
413 }
414
415 return result;
416 }
417
418 /*
419 FUNCTION
420 bfd_get_mtime
421
422 SYNOPSIS
423 long bfd_get_mtime (bfd *abfd);
424
425 DESCRIPTION
426 Return the file modification time (as read from the file system, or
427 from the archive header for archive members).
428
429 */
430
431 long
432 bfd_get_mtime (bfd *abfd)
433 {
434 struct stat buf;
435
436 if (abfd->mtime_set)
437 return abfd->mtime;
438
439 if (bfd_stat (abfd, &buf) != 0)
440 return 0;
441
442 abfd->mtime = buf.st_mtime; /* Save value in case anyone wants it */
443 return buf.st_mtime;
444 }
445
446 /*
447 FUNCTION
448 bfd_get_size
449
450 SYNOPSIS
451 ufile_ptr bfd_get_size (bfd *abfd);
452
453 DESCRIPTION
454 Return the file size (as read from file system) for the file
455 associated with BFD @var{abfd}.
456
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.
462
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
476 size reasonable?".
477
478 A return value of zero indicates the file size is unknown.
479 */
480
481 ufile_ptr
482 bfd_get_size (bfd *abfd)
483 {
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))
487 {
488 struct stat buf;
489
490 if (abfd->size == 1 && !bfd_write_p (abfd))
491 return 0;
492
493 if (bfd_stat (abfd, &buf) != 0
494 || buf.st_size == 0
495 || buf.st_size - (ufile_ptr) buf.st_size != 0)
496 {
497 abfd->size = 1;
498 return 0;
499 }
500 abfd->size = buf.st_size;
501 }
502 return abfd->size;
503 }
504
505 /*
506 FUNCTION
507 bfd_get_file_size
508
509 SYNOPSIS
510 ufile_ptr bfd_get_file_size (bfd *abfd);
511
512 DESCRIPTION
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.
516
517 */
518
519 ufile_ptr
520 bfd_get_file_size (bfd *abfd)
521 {
522 ufile_ptr file_size, archive_size = (ufile_ptr) -1;
523
524 if (abfd->my_archive != NULL
525 && !bfd_is_thin_archive (abfd->my_archive))
526 {
527 struct areltdata *adata = (struct areltdata *) abfd->arelt_data;
528 if (adata != NULL)
529 {
530 archive_size = adata->parsed_size;
531 /* If the archive is compressed we can't compare against
532 file size. */
533 if (adata->arch_header != NULL
534 && memcmp (((struct ar_hdr *) adata->arch_header)->ar_fmag,
535 "Z\012", 2) == 0)
536 return archive_size;
537 abfd = abfd->my_archive;
538 }
539 }
540
541 file_size = bfd_get_size (abfd);
542 if (archive_size < file_size)
543 return archive_size;
544 return file_size;
545 }
546
547 /*
548 FUNCTION
549 bfd_mmap
550
551 SYNOPSIS
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);
555
556 DESCRIPTION
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.
560
561 */
562
563 void *
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)
567 {
568 while (abfd->my_archive != NULL
569 && !bfd_is_thin_archive (abfd->my_archive))
570 {
571 offset += abfd->origin;
572 abfd = abfd->my_archive;
573 }
574 offset += abfd->origin;
575
576 if (abfd->iovec == NULL)
577 {
578 bfd_set_error (bfd_error_invalid_operation);
579 return (void *) -1;
580 }
581
582 return abfd->iovec->bmmap (abfd, addr, len, prot, flags, offset,
583 map_addr, map_len);
584 }
585
586 /* Memory file I/O operations. */
587
588 static file_ptr
589 memory_bread (bfd *abfd, void *ptr, file_ptr size)
590 {
591 struct bfd_in_memory *bim;
592 bfd_size_type get;
593
594 bim = (struct bfd_in_memory *) abfd->iostream;
595 get = size;
596 if (abfd->where + get > bim->size)
597 {
598 if (bim->size < (bfd_size_type) abfd->where)
599 get = 0;
600 else
601 get = bim->size - abfd->where;
602 bfd_set_error (bfd_error_file_truncated);
603 }
604 memcpy (ptr, bim->buffer + abfd->where, (size_t) get);
605 return get;
606 }
607
608 static file_ptr
609 memory_bwrite (bfd *abfd, const void *ptr, file_ptr size)
610 {
611 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
612
613 if (abfd->where + size > bim->size)
614 {
615 bfd_size_type newsize, oldsize;
616
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)
622 {
623 bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
624 if (bim->buffer == NULL)
625 {
626 bim->size = 0;
627 return 0;
628 }
629 if (newsize > bim->size)
630 memset (bim->buffer + bim->size, 0, newsize - bim->size);
631 }
632 }
633 memcpy (bim->buffer + abfd->where, ptr, (size_t) size);
634 return size;
635 }
636
637 static file_ptr
638 memory_btell (bfd *abfd)
639 {
640 return abfd->where;
641 }
642
643 static int
644 memory_bseek (bfd *abfd, file_ptr position, int direction)
645 {
646 file_ptr nwhere;
647 struct bfd_in_memory *bim;
648
649 bim = (struct bfd_in_memory *) abfd->iostream;
650
651 if (direction == SEEK_SET)
652 nwhere = position;
653 else
654 nwhere = abfd->where + position;
655
656 if (nwhere < 0)
657 {
658 abfd->where = 0;
659 errno = EINVAL;
660 return -1;
661 }
662
663 if ((bfd_size_type)nwhere > bim->size)
664 {
665 if (abfd->direction == write_direction
666 || abfd->direction == both_direction)
667 {
668 bfd_size_type newsize, oldsize;
669
670 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
671 bim->size = nwhere;
672 /* Round up to cut down on memory fragmentation */
673 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
674 if (newsize > oldsize)
675 {
676 bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
677 if (bim->buffer == NULL)
678 {
679 errno = EINVAL;
680 bim->size = 0;
681 return -1;
682 }
683 memset (bim->buffer + oldsize, 0, newsize - oldsize);
684 }
685 }
686 else
687 {
688 abfd->where = bim->size;
689 errno = EINVAL;
690 bfd_set_error (bfd_error_file_truncated);
691 return -1;
692 }
693 }
694 return 0;
695 }
696
697 static int
698 memory_bclose (struct bfd *abfd)
699 {
700 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
701
702 free (bim->buffer);
703 free (bim);
704 abfd->iostream = NULL;
705
706 return 0;
707 }
708
709 static int
710 memory_bflush (bfd *abfd ATTRIBUTE_UNUSED)
711 {
712 return 0;
713 }
714
715 static int
716 memory_bstat (bfd *abfd, struct stat *statbuf)
717 {
718 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
719
720 memset (statbuf, 0, sizeof (*statbuf));
721 statbuf->st_size = bim->size;
722
723 return 0;
724 }
725
726 static void *
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)
732 {
733 return (void *)-1;
734 }
735
736 const struct bfd_iovec _bfd_memory_iovec =
737 {
738 &memory_bread, &memory_bwrite, &memory_btell, &memory_bseek,
739 &memory_bclose, &memory_bflush, &memory_bstat, &memory_bmmap
740 };