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