Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* opncls.c -- open and close a BFD. |
7898deda | 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, |
c4f3d130 | 3 | 2001, 2002 |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | ||
6 | Written by Cygnus Support. | |
7 | ||
c4f3d130 | 8 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 9 | |
c4f3d130 NC |
10 | This program is free software; you can redistribute it and/or modify |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation; either version 2 of the License, or | |
13 | (at your option) any later version. | |
252b5132 | 14 | |
c4f3d130 NC |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
252b5132 | 19 | |
c4f3d130 NC |
20 | You should have received a copy of the GNU General Public License |
21 | along with this program; if not, write to the Free Software | |
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
252b5132 RH |
23 | |
24 | #include "bfd.h" | |
25 | #include "sysdep.h" | |
26 | #include "objalloc.h" | |
27 | #include "libbfd.h" | |
28 | ||
29 | #ifndef S_IXUSR | |
30 | #define S_IXUSR 0100 /* Execute by owner. */ | |
31 | #endif | |
32 | #ifndef S_IXGRP | |
33 | #define S_IXGRP 0010 /* Execute by group. */ | |
34 | #endif | |
35 | #ifndef S_IXOTH | |
36 | #define S_IXOTH 0001 /* Execute by others. */ | |
37 | #endif | |
38 | ||
39 | /* fdopen is a loser -- we should use stdio exclusively. Unfortunately | |
40 | if we do that we can't use fcntl. */ | |
41 | ||
252b5132 RH |
42 | /* Return a new BFD. All BFD's are allocated through this routine. */ |
43 | ||
44 | bfd * | |
45 | _bfd_new_bfd () | |
46 | { | |
47 | bfd *nbfd; | |
48 | ||
dc810e39 | 49 | nbfd = (bfd *) bfd_zmalloc ((bfd_size_type) sizeof (bfd)); |
252b5132 RH |
50 | if (nbfd == NULL) |
51 | return NULL; | |
52 | ||
53 | nbfd->memory = (PTR) objalloc_create (); | |
54 | if (nbfd->memory == NULL) | |
55 | { | |
56 | bfd_set_error (bfd_error_no_memory); | |
73e87d70 | 57 | free (nbfd); |
252b5132 RH |
58 | return NULL; |
59 | } | |
60 | ||
61 | nbfd->arch_info = &bfd_default_arch_struct; | |
62 | ||
63 | nbfd->direction = no_direction; | |
64 | nbfd->iostream = NULL; | |
65 | nbfd->where = 0; | |
73e87d70 AM |
66 | if (!bfd_hash_table_init (&nbfd->section_htab, bfd_section_hash_newfunc)) |
67 | { | |
68 | free (nbfd); | |
69 | return NULL; | |
70 | } | |
252b5132 | 71 | nbfd->sections = (asection *) NULL; |
73e87d70 | 72 | nbfd->section_tail = &nbfd->sections; |
252b5132 RH |
73 | nbfd->format = bfd_unknown; |
74 | nbfd->my_archive = (bfd *) NULL; | |
dc810e39 | 75 | nbfd->origin = 0; |
252b5132 RH |
76 | nbfd->opened_once = false; |
77 | nbfd->output_has_begun = false; | |
78 | nbfd->section_count = 0; | |
79 | nbfd->usrdata = (PTR) NULL; | |
80 | nbfd->cacheable = false; | |
81 | nbfd->flags = BFD_NO_FLAGS; | |
82 | nbfd->mtime_set = false; | |
83 | ||
84 | return nbfd; | |
85 | } | |
86 | ||
87 | /* Allocate a new BFD as a member of archive OBFD. */ | |
88 | ||
89 | bfd * | |
90 | _bfd_new_bfd_contained_in (obfd) | |
91 | bfd *obfd; | |
92 | { | |
93 | bfd *nbfd; | |
94 | ||
95 | nbfd = _bfd_new_bfd (); | |
96 | nbfd->xvec = obfd->xvec; | |
97 | nbfd->my_archive = obfd; | |
98 | nbfd->direction = read_direction; | |
99 | nbfd->target_defaulted = obfd->target_defaulted; | |
100 | return nbfd; | |
101 | } | |
102 | ||
73e87d70 AM |
103 | /* Delete a BFD. */ |
104 | ||
105 | void | |
106 | _bfd_delete_bfd (abfd) | |
107 | bfd *abfd; | |
108 | { | |
109 | bfd_hash_table_free (&abfd->section_htab); | |
110 | objalloc_free ((struct objalloc *) abfd->memory); | |
111 | free (abfd); | |
112 | } | |
113 | ||
252b5132 RH |
114 | /* |
115 | SECTION | |
116 | Opening and closing BFDs | |
117 | ||
118 | */ | |
119 | ||
120 | /* | |
121 | FUNCTION | |
122 | bfd_openr | |
123 | ||
124 | SYNOPSIS | |
dc810e39 | 125 | bfd *bfd_openr(const char *filename, const char *target); |
252b5132 RH |
126 | |
127 | DESCRIPTION | |
128 | Open the file @var{filename} (using <<fopen>>) with the target | |
129 | @var{target}. Return a pointer to the created BFD. | |
130 | ||
131 | Calls <<bfd_find_target>>, so @var{target} is interpreted as by | |
132 | that function. | |
133 | ||
134 | If <<NULL>> is returned then an error has occured. Possible errors | |
135 | are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or <<system_call>> error. | |
136 | */ | |
137 | ||
138 | bfd * | |
139 | bfd_openr (filename, target) | |
dc810e39 AM |
140 | const char *filename; |
141 | const char *target; | |
252b5132 RH |
142 | { |
143 | bfd *nbfd; | |
144 | const bfd_target *target_vec; | |
145 | ||
146 | nbfd = _bfd_new_bfd (); | |
147 | if (nbfd == NULL) | |
148 | return NULL; | |
149 | ||
150 | target_vec = bfd_find_target (target, nbfd); | |
151 | if (target_vec == NULL) | |
152 | { | |
252b5132 | 153 | bfd_set_error (bfd_error_invalid_target); |
73e87d70 | 154 | _bfd_delete_bfd (nbfd); |
252b5132 RH |
155 | return NULL; |
156 | } | |
157 | ||
158 | nbfd->filename = filename; | |
159 | nbfd->direction = read_direction; | |
160 | ||
161 | if (bfd_open_file (nbfd) == NULL) | |
162 | { | |
c4f3d130 | 163 | /* File didn't exist, or some such. */ |
252b5132 | 164 | bfd_set_error (bfd_error_system_call); |
73e87d70 | 165 | _bfd_delete_bfd (nbfd); |
252b5132 RH |
166 | return NULL; |
167 | } | |
168 | ||
169 | return nbfd; | |
170 | } | |
171 | ||
172 | /* Don't try to `optimize' this function: | |
173 | ||
174 | o - We lock using stack space so that interrupting the locking | |
175 | won't cause a storage leak. | |
176 | o - We open the file stream last, since we don't want to have to | |
177 | close it if anything goes wrong. Closing the stream means closing | |
c4f3d130 | 178 | the file descriptor too, even though we didn't open it. */ |
252b5132 RH |
179 | /* |
180 | FUNCTION | |
181 | bfd_fdopenr | |
182 | ||
183 | SYNOPSIS | |
dc810e39 | 184 | bfd *bfd_fdopenr(const char *filename, const char *target, int fd); |
252b5132 RH |
185 | |
186 | DESCRIPTION | |
187 | <<bfd_fdopenr>> is to <<bfd_fopenr>> much like <<fdopen>> is to <<fopen>>. | |
188 | It opens a BFD on a file already described by the @var{fd} | |
189 | supplied. | |
190 | ||
191 | When the file is later <<bfd_close>>d, the file descriptor will be closed. | |
192 | ||
193 | If the caller desires that this file descriptor be cached by BFD | |
194 | (opened as needed, closed as needed to free descriptors for | |
195 | other opens), with the supplied @var{fd} used as an initial | |
196 | file descriptor (but subject to closure at any time), call | |
197 | bfd_set_cacheable(bfd, 1) on the returned BFD. The default is to | |
198 | assume no cacheing; the file descriptor will remain open until | |
199 | <<bfd_close>>, and will not be affected by BFD operations on other | |
200 | files. | |
201 | ||
202 | Possible errors are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> and <<bfd_error_system_call>>. | |
203 | */ | |
204 | ||
205 | bfd * | |
206 | bfd_fdopenr (filename, target, fd) | |
dc810e39 AM |
207 | const char *filename; |
208 | const char *target; | |
252b5132 RH |
209 | int fd; |
210 | { | |
211 | bfd *nbfd; | |
212 | const bfd_target *target_vec; | |
213 | int fdflags; | |
214 | ||
215 | bfd_set_error (bfd_error_system_call); | |
216 | #if ! defined(HAVE_FCNTL) || ! defined(F_GETFL) | |
c4f3d130 | 217 | fdflags = O_RDWR; /* Assume full access. */ |
252b5132 RH |
218 | #else |
219 | fdflags = fcntl (fd, F_GETFL, NULL); | |
220 | #endif | |
221 | if (fdflags == -1) return NULL; | |
222 | ||
223 | nbfd = _bfd_new_bfd (); | |
224 | if (nbfd == NULL) | |
225 | return NULL; | |
226 | ||
227 | target_vec = bfd_find_target (target, nbfd); | |
228 | if (target_vec == NULL) | |
229 | { | |
230 | bfd_set_error (bfd_error_invalid_target); | |
73e87d70 | 231 | _bfd_delete_bfd (nbfd); |
252b5132 RH |
232 | return NULL; |
233 | } | |
234 | ||
235 | #ifndef HAVE_FDOPEN | |
236 | nbfd->iostream = (PTR) fopen (filename, FOPEN_RB); | |
237 | #else | |
c4f3d130 | 238 | /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */ |
252b5132 RH |
239 | switch (fdflags & (O_ACCMODE)) |
240 | { | |
241 | case O_RDONLY: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RB); break; | |
242 | case O_WRONLY: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RUB); break; | |
243 | case O_RDWR: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RUB); break; | |
244 | default: abort (); | |
245 | } | |
246 | #endif | |
247 | ||
248 | if (nbfd->iostream == NULL) | |
249 | { | |
73e87d70 | 250 | _bfd_delete_bfd (nbfd); |
252b5132 RH |
251 | return NULL; |
252 | } | |
253 | ||
c4f3d130 | 254 | /* OK, put everything where it belongs. */ |
252b5132 RH |
255 | nbfd->filename = filename; |
256 | ||
257 | /* As a special case we allow a FD open for read/write to | |
258 | be written through, although doing so requires that we end | |
259 | the previous clause with a preposition. */ | |
c4f3d130 | 260 | /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */ |
d768008d | 261 | switch (fdflags & (O_ACCMODE)) |
252b5132 RH |
262 | { |
263 | case O_RDONLY: nbfd->direction = read_direction; break; | |
264 | case O_WRONLY: nbfd->direction = write_direction; break; | |
265 | case O_RDWR: nbfd->direction = both_direction; break; | |
266 | default: abort (); | |
267 | } | |
268 | ||
269 | if (! bfd_cache_init (nbfd)) | |
270 | { | |
73e87d70 | 271 | _bfd_delete_bfd (nbfd); |
252b5132 RH |
272 | return NULL; |
273 | } | |
274 | nbfd->opened_once = true; | |
275 | ||
276 | return nbfd; | |
277 | } | |
278 | ||
279 | /* | |
280 | FUNCTION | |
281 | bfd_openstreamr | |
282 | ||
283 | SYNOPSIS | |
284 | bfd *bfd_openstreamr(const char *, const char *, PTR); | |
285 | ||
286 | DESCRIPTION | |
287 | ||
288 | Open a BFD for read access on an existing stdio stream. When | |
289 | the BFD is passed to <<bfd_close>>, the stream will be closed. | |
290 | */ | |
291 | ||
292 | bfd * | |
293 | bfd_openstreamr (filename, target, streamarg) | |
294 | const char *filename; | |
295 | const char *target; | |
296 | PTR streamarg; | |
297 | { | |
298 | FILE *stream = (FILE *) streamarg; | |
299 | bfd *nbfd; | |
300 | const bfd_target *target_vec; | |
301 | ||
302 | nbfd = _bfd_new_bfd (); | |
303 | if (nbfd == NULL) | |
304 | return NULL; | |
305 | ||
306 | target_vec = bfd_find_target (target, nbfd); | |
307 | if (target_vec == NULL) | |
308 | { | |
309 | bfd_set_error (bfd_error_invalid_target); | |
73e87d70 | 310 | _bfd_delete_bfd (nbfd); |
252b5132 RH |
311 | return NULL; |
312 | } | |
313 | ||
314 | nbfd->iostream = (PTR) stream; | |
315 | nbfd->filename = filename; | |
316 | nbfd->direction = read_direction; | |
dc810e39 | 317 | |
252b5132 RH |
318 | if (! bfd_cache_init (nbfd)) |
319 | { | |
73e87d70 | 320 | _bfd_delete_bfd (nbfd); |
252b5132 RH |
321 | return NULL; |
322 | } | |
323 | ||
324 | return nbfd; | |
325 | } | |
326 | \f | |
c4f3d130 NC |
327 | /* bfd_openw -- open for writing. |
328 | Returns a pointer to a freshly-allocated BFD on success, or NULL. | |
252b5132 | 329 | |
c4f3d130 | 330 | See comment by bfd_fdopenr before you try to modify this function. */ |
252b5132 RH |
331 | |
332 | /* | |
333 | FUNCTION | |
334 | bfd_openw | |
335 | ||
336 | SYNOPSIS | |
dc810e39 | 337 | bfd *bfd_openw(const char *filename, const char *target); |
252b5132 RH |
338 | |
339 | DESCRIPTION | |
340 | Create a BFD, associated with file @var{filename}, using the | |
341 | file format @var{target}, and return a pointer to it. | |
342 | ||
343 | Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>, | |
344 | <<bfd_error_invalid_target>>. | |
345 | */ | |
346 | ||
347 | bfd * | |
348 | bfd_openw (filename, target) | |
dc810e39 AM |
349 | const char *filename; |
350 | const char *target; | |
252b5132 RH |
351 | { |
352 | bfd *nbfd; | |
353 | const bfd_target *target_vec; | |
354 | ||
355 | bfd_set_error (bfd_error_system_call); | |
356 | ||
357 | /* nbfd has to point to head of malloc'ed block so that bfd_close may | |
c4f3d130 | 358 | reclaim it correctly. */ |
252b5132 RH |
359 | nbfd = _bfd_new_bfd (); |
360 | if (nbfd == NULL) | |
361 | return NULL; | |
362 | ||
363 | target_vec = bfd_find_target (target, nbfd); | |
364 | if (target_vec == NULL) | |
365 | { | |
73e87d70 | 366 | _bfd_delete_bfd (nbfd); |
252b5132 RH |
367 | return NULL; |
368 | } | |
369 | ||
370 | nbfd->filename = filename; | |
371 | nbfd->direction = write_direction; | |
372 | ||
373 | if (bfd_open_file (nbfd) == NULL) | |
374 | { | |
c4f3d130 NC |
375 | /* File not writeable, etc. */ |
376 | bfd_set_error (bfd_error_system_call); | |
73e87d70 | 377 | _bfd_delete_bfd (nbfd); |
252b5132 RH |
378 | return NULL; |
379 | } | |
380 | ||
381 | return nbfd; | |
382 | } | |
383 | ||
384 | /* | |
385 | ||
386 | FUNCTION | |
387 | bfd_close | |
388 | ||
389 | SYNOPSIS | |
390 | boolean bfd_close(bfd *abfd); | |
391 | ||
392 | DESCRIPTION | |
393 | ||
394 | Close a BFD. If the BFD was open for writing, | |
395 | then pending operations are completed and the file written out | |
396 | and closed. If the created file is executable, then | |
397 | <<chmod>> is called to mark it as such. | |
398 | ||
399 | All memory attached to the BFD is released. | |
400 | ||
401 | The file descriptor associated with the BFD is closed (even | |
402 | if it was passed in to BFD by <<bfd_fdopenr>>). | |
403 | ||
404 | RETURNS | |
405 | <<true>> is returned if all is ok, otherwise <<false>>. | |
406 | */ | |
407 | ||
408 | ||
409 | boolean | |
410 | bfd_close (abfd) | |
411 | bfd *abfd; | |
412 | { | |
413 | boolean ret; | |
414 | ||
c4f3d130 | 415 | if (bfd_write_p (abfd)) |
252b5132 RH |
416 | { |
417 | if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd))) | |
418 | return false; | |
419 | } | |
420 | ||
421 | if (! BFD_SEND (abfd, _close_and_cleanup, (abfd))) | |
422 | return false; | |
423 | ||
424 | ret = bfd_cache_close (abfd); | |
425 | ||
426 | /* If the file was open for writing and is now executable, | |
c4f3d130 | 427 | make it so. */ |
252b5132 RH |
428 | if (ret |
429 | && abfd->direction == write_direction | |
430 | && abfd->flags & EXEC_P) | |
431 | { | |
432 | struct stat buf; | |
433 | ||
434 | if (stat (abfd->filename, &buf) == 0) | |
435 | { | |
dc810e39 | 436 | unsigned int mask = umask (0); |
c4f3d130 | 437 | |
252b5132 RH |
438 | umask (mask); |
439 | chmod (abfd->filename, | |
440 | (0777 | |
441 | & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask)))); | |
442 | } | |
443 | } | |
444 | ||
73e87d70 | 445 | _bfd_delete_bfd (abfd); |
252b5132 RH |
446 | |
447 | return ret; | |
448 | } | |
449 | ||
450 | /* | |
451 | FUNCTION | |
452 | bfd_close_all_done | |
453 | ||
454 | SYNOPSIS | |
455 | boolean bfd_close_all_done(bfd *); | |
456 | ||
457 | DESCRIPTION | |
458 | Close a BFD. Differs from <<bfd_close>> | |
459 | since it does not complete any pending operations. This | |
460 | routine would be used if the application had just used BFD for | |
461 | swapping and didn't want to use any of the writing code. | |
462 | ||
463 | If the created file is executable, then <<chmod>> is called | |
464 | to mark it as such. | |
465 | ||
466 | All memory attached to the BFD is released. | |
467 | ||
468 | RETURNS | |
469 | <<true>> is returned if all is ok, otherwise <<false>>. | |
252b5132 RH |
470 | */ |
471 | ||
472 | boolean | |
473 | bfd_close_all_done (abfd) | |
474 | bfd *abfd; | |
475 | { | |
476 | boolean ret; | |
477 | ||
478 | ret = bfd_cache_close (abfd); | |
479 | ||
480 | /* If the file was open for writing and is now executable, | |
c4f3d130 | 481 | make it so. */ |
252b5132 RH |
482 | if (ret |
483 | && abfd->direction == write_direction | |
484 | && abfd->flags & EXEC_P) | |
485 | { | |
486 | struct stat buf; | |
487 | ||
488 | if (stat (abfd->filename, &buf) == 0) | |
489 | { | |
dc810e39 | 490 | unsigned int mask = umask (0); |
c4f3d130 | 491 | |
252b5132 RH |
492 | umask (mask); |
493 | chmod (abfd->filename, | |
b6cdd0fd | 494 | (0777 |
252b5132 RH |
495 | & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask)))); |
496 | } | |
497 | } | |
498 | ||
73e87d70 | 499 | _bfd_delete_bfd (abfd); |
252b5132 RH |
500 | |
501 | return ret; | |
502 | } | |
503 | ||
504 | /* | |
505 | FUNCTION | |
506 | bfd_create | |
507 | ||
508 | SYNOPSIS | |
dc810e39 | 509 | bfd *bfd_create(const char *filename, bfd *templ); |
252b5132 RH |
510 | |
511 | DESCRIPTION | |
512 | Create a new BFD in the manner of | |
513 | <<bfd_openw>>, but without opening a file. The new BFD | |
514 | takes the target from the target used by @var{template}. The | |
515 | format is always set to <<bfd_object>>. | |
252b5132 RH |
516 | */ |
517 | ||
518 | bfd * | |
519 | bfd_create (filename, templ) | |
dc810e39 | 520 | const char *filename; |
252b5132 RH |
521 | bfd *templ; |
522 | { | |
523 | bfd *nbfd; | |
524 | ||
525 | nbfd = _bfd_new_bfd (); | |
526 | if (nbfd == NULL) | |
527 | return NULL; | |
528 | nbfd->filename = filename; | |
529 | if (templ) | |
530 | nbfd->xvec = templ->xvec; | |
531 | nbfd->direction = no_direction; | |
532 | bfd_set_format (nbfd, bfd_object); | |
c4f3d130 | 533 | |
252b5132 RH |
534 | return nbfd; |
535 | } | |
536 | ||
537 | /* | |
538 | FUNCTION | |
539 | bfd_make_writable | |
540 | ||
541 | SYNOPSIS | |
542 | boolean bfd_make_writable(bfd *abfd); | |
543 | ||
544 | DESCRIPTION | |
545 | Takes a BFD as created by <<bfd_create>> and converts it | |
546 | into one like as returned by <<bfd_openw>>. It does this | |
547 | by converting the BFD to BFD_IN_MEMORY. It's assumed that | |
548 | you will call <<bfd_make_readable>> on this bfd later. | |
549 | ||
550 | RETURNS | |
551 | <<true>> is returned if all is ok, otherwise <<false>>. | |
552 | */ | |
553 | ||
554 | boolean | |
555 | bfd_make_writable(abfd) | |
556 | bfd *abfd; | |
557 | { | |
558 | struct bfd_in_memory *bim; | |
559 | ||
560 | if (abfd->direction != no_direction) | |
561 | { | |
562 | bfd_set_error (bfd_error_invalid_operation); | |
563 | return false; | |
564 | } | |
565 | ||
dc810e39 AM |
566 | bim = ((struct bfd_in_memory *) |
567 | bfd_malloc ((bfd_size_type) sizeof (struct bfd_in_memory))); | |
252b5132 | 568 | abfd->iostream = (PTR) bim; |
c4f3d130 | 569 | /* bfd_bwrite will grow these as needed. */ |
252b5132 RH |
570 | bim->size = 0; |
571 | bim->buffer = 0; | |
572 | ||
573 | abfd->flags |= BFD_IN_MEMORY; | |
574 | abfd->direction = write_direction; | |
575 | abfd->where = 0; | |
576 | ||
577 | return true; | |
578 | } | |
579 | ||
580 | /* | |
581 | FUNCTION | |
582 | bfd_make_readable | |
583 | ||
584 | SYNOPSIS | |
585 | boolean bfd_make_readable(bfd *abfd); | |
586 | ||
587 | DESCRIPTION | |
588 | Takes a BFD as created by <<bfd_create>> and | |
589 | <<bfd_make_writable>> and converts it into one like as | |
590 | returned by <<bfd_openr>>. It does this by writing the | |
591 | contents out to the memory buffer, then reversing the | |
592 | direction. | |
593 | ||
594 | RETURNS | |
595 | <<true>> is returned if all is ok, otherwise <<false>>. */ | |
596 | ||
597 | boolean | |
598 | bfd_make_readable(abfd) | |
599 | bfd *abfd; | |
600 | { | |
601 | if (abfd->direction != write_direction || !(abfd->flags & BFD_IN_MEMORY)) | |
602 | { | |
603 | bfd_set_error (bfd_error_invalid_operation); | |
604 | return false; | |
605 | } | |
606 | ||
607 | if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd))) | |
608 | return false; | |
609 | ||
610 | if (! BFD_SEND (abfd, _close_and_cleanup, (abfd))) | |
611 | return false; | |
612 | ||
613 | ||
614 | abfd->arch_info = &bfd_default_arch_struct; | |
615 | ||
616 | abfd->where = 0; | |
617 | abfd->sections = (asection *) NULL; | |
618 | abfd->format = bfd_unknown; | |
619 | abfd->my_archive = (bfd *) NULL; | |
dc810e39 | 620 | abfd->origin = 0; |
252b5132 RH |
621 | abfd->opened_once = false; |
622 | abfd->output_has_begun = false; | |
623 | abfd->section_count = 0; | |
624 | abfd->usrdata = (PTR) NULL; | |
625 | abfd->cacheable = false; | |
626 | abfd->flags = BFD_IN_MEMORY; | |
627 | abfd->mtime_set = false; | |
628 | ||
629 | abfd->target_defaulted = true; | |
630 | abfd->direction = read_direction; | |
631 | abfd->sections = 0; | |
632 | abfd->symcount = 0; | |
633 | abfd->outsymbols = 0; | |
634 | abfd->tdata.any = 0; | |
635 | ||
636 | bfd_check_format(abfd, bfd_object); | |
637 | ||
638 | return true; | |
639 | } | |
640 | ||
641 | /* | |
642 | INTERNAL_FUNCTION | |
643 | bfd_alloc | |
644 | ||
645 | SYNOPSIS | |
646 | PTR bfd_alloc (bfd *abfd, size_t wanted); | |
647 | ||
648 | DESCRIPTION | |
649 | Allocate a block of @var{wanted} bytes of memory attached to | |
650 | <<abfd>> and return a pointer to it. | |
651 | */ | |
652 | ||
653 | ||
654 | PTR | |
655 | bfd_alloc (abfd, size) | |
656 | bfd *abfd; | |
dc810e39 | 657 | bfd_size_type size; |
252b5132 RH |
658 | { |
659 | PTR ret; | |
660 | ||
dc810e39 AM |
661 | if (size != (unsigned long) size) |
662 | { | |
663 | bfd_set_error (bfd_error_no_memory); | |
664 | return NULL; | |
665 | } | |
666 | ||
252b5132 RH |
667 | ret = objalloc_alloc (abfd->memory, (unsigned long) size); |
668 | if (ret == NULL) | |
669 | bfd_set_error (bfd_error_no_memory); | |
670 | return ret; | |
671 | } | |
672 | ||
673 | PTR | |
674 | bfd_zalloc (abfd, size) | |
675 | bfd *abfd; | |
dc810e39 | 676 | bfd_size_type size; |
252b5132 RH |
677 | { |
678 | PTR res; | |
679 | ||
680 | res = bfd_alloc (abfd, size); | |
681 | if (res) | |
dc810e39 | 682 | memset (res, 0, (size_t) size); |
252b5132 RH |
683 | return res; |
684 | } | |
685 | ||
73e87d70 AM |
686 | /* Free a block allocated for a BFD. |
687 | Note: Also frees all more recently allocated blocks! */ | |
252b5132 RH |
688 | |
689 | void | |
690 | bfd_release (abfd, block) | |
691 | bfd *abfd; | |
692 | PTR block; | |
693 | { | |
694 | objalloc_free_block ((struct objalloc *) abfd->memory, block); | |
695 | } |