1 /* opncls.c -- open and close a BFD.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 1997
3 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 #define S_IXUSR 0100 /* Execute by owner. */
32 #define S_IXGRP 0010 /* Execute by group. */
35 #define S_IXOTH 0001 /* Execute by others. */
38 /* fdopen is a loser -- we should use stdio exclusively. Unfortunately
39 if we do that we can't use fcntl. */
41 /* FIXME: This is no longer used. */
42 long _bfd_chunksize
= -1;
44 /* Return a new BFD. All BFD's are allocated through this routine. */
51 nbfd
= (bfd
*) bfd_zmalloc (sizeof (bfd
));
55 nbfd
->memory
= (PTR
) objalloc_create ();
56 if (nbfd
->memory
== NULL
)
58 bfd_set_error (bfd_error_no_memory
);
62 nbfd
->arch_info
= &bfd_default_arch_struct
;
64 nbfd
->direction
= no_direction
;
65 nbfd
->iostream
= NULL
;
67 nbfd
->sections
= (asection
*) NULL
;
68 nbfd
->format
= bfd_unknown
;
69 nbfd
->my_archive
= (bfd
*) NULL
;
71 nbfd
->opened_once
= false;
72 nbfd
->output_has_begun
= false;
73 nbfd
->section_count
= 0;
74 nbfd
->usrdata
= (PTR
) NULL
;
75 nbfd
->cacheable
= false;
76 nbfd
->flags
= BFD_NO_FLAGS
;
77 nbfd
->mtime_set
= false;
82 /* Allocate a new BFD as a member of archive OBFD. */
85 _bfd_new_bfd_contained_in (obfd
)
90 nbfd
= _bfd_new_bfd ();
91 nbfd
->xvec
= obfd
->xvec
;
92 nbfd
->my_archive
= obfd
;
93 nbfd
->direction
= read_direction
;
94 nbfd
->target_defaulted
= obfd
->target_defaulted
;
100 Opening and closing BFDs
109 bfd *bfd_openr(CONST char *filename, CONST char *target);
112 Open the file @var{filename} (using <<fopen>>) with the target
113 @var{target}. Return a pointer to the created BFD.
115 Calls <<bfd_find_target>>, so @var{target} is interpreted as by
118 If <<NULL>> is returned then an error has occured. Possible errors
119 are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or <<system_call>> error.
123 bfd_openr (filename
, target
)
124 CONST
char *filename
;
128 const bfd_target
*target_vec
;
130 nbfd
= _bfd_new_bfd ();
134 target_vec
= bfd_find_target (target
, nbfd
);
135 if (target_vec
== NULL
)
137 objalloc_free ((struct objalloc
*) nbfd
->memory
);
139 bfd_set_error (bfd_error_invalid_target
);
143 nbfd
->filename
= filename
;
144 nbfd
->direction
= read_direction
;
146 if (bfd_open_file (nbfd
) == NULL
)
148 /* File didn't exist, or some such */
149 bfd_set_error (bfd_error_system_call
);
150 objalloc_free ((struct objalloc
*) nbfd
->memory
);
158 /* Don't try to `optimize' this function:
160 o - We lock using stack space so that interrupting the locking
161 won't cause a storage leak.
162 o - We open the file stream last, since we don't want to have to
163 close it if anything goes wrong. Closing the stream means closing
164 the file descriptor too, even though we didn't open it.
171 bfd *bfd_fdopenr(CONST char *filename, CONST char *target, int fd);
174 <<bfd_fdopenr>> is to <<bfd_fopenr>> much like <<fdopen>> is to <<fopen>>.
175 It opens a BFD on a file already described by the @var{fd}
178 When the file is later <<bfd_close>>d, the file descriptor will be closed.
180 If the caller desires that this file descriptor be cached by BFD
181 (opened as needed, closed as needed to free descriptors for
182 other opens), with the supplied @var{fd} used as an initial
183 file descriptor (but subject to closure at any time), call
184 bfd_set_cacheable(bfd, 1) on the returned BFD. The default is to
185 assume no cacheing; the file descriptor will remain open until
186 <<bfd_close>>, and will not be affected by BFD operations on other
189 Possible errors are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> and <<bfd_error_system_call>>.
193 bfd_fdopenr (filename
, target
, fd
)
194 CONST
char *filename
;
199 const bfd_target
*target_vec
;
202 bfd_set_error (bfd_error_system_call
);
203 #if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
204 fdflags
= O_RDWR
; /* Assume full access */
206 fdflags
= fcntl (fd
, F_GETFL
, NULL
);
208 if (fdflags
== -1) return NULL
;
210 nbfd
= _bfd_new_bfd ();
214 target_vec
= bfd_find_target (target
, nbfd
);
215 if (target_vec
== NULL
)
217 bfd_set_error (bfd_error_invalid_target
);
218 objalloc_free ((struct objalloc
*) nbfd
->memory
);
224 nbfd
->iostream
= (PTR
) fopen (filename
, FOPEN_RB
);
226 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
227 switch (fdflags
& (O_ACCMODE
))
229 case O_RDONLY
: nbfd
->iostream
= (PTR
) fdopen (fd
, FOPEN_RB
); break;
230 case O_WRONLY
: nbfd
->iostream
= (PTR
) fdopen (fd
, FOPEN_RUB
); break;
231 case O_RDWR
: nbfd
->iostream
= (PTR
) fdopen (fd
, FOPEN_RUB
); break;
236 if (nbfd
->iostream
== NULL
)
238 objalloc_free ((struct objalloc
*) nbfd
->memory
);
243 /* OK, put everything where it belongs */
245 nbfd
->filename
= filename
;
247 /* As a special case we allow a FD open for read/write to
248 be written through, although doing so requires that we end
249 the previous clause with a preposition. */
250 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
251 switch (fdflags
& O_ACCMODE
)
253 case O_RDONLY
: nbfd
->direction
= read_direction
; break;
254 case O_WRONLY
: nbfd
->direction
= write_direction
; break;
255 case O_RDWR
: nbfd
->direction
= both_direction
; break;
259 if (! bfd_cache_init (nbfd
))
261 objalloc_free ((struct objalloc
*) nbfd
->memory
);
265 nbfd
->opened_once
= true;
275 bfd *bfd_openstreamr(const char *, const char *, PTR);
279 Open a BFD for read access on an existing stdio stream. When
280 the BFD is passed to <<bfd_close>>, the stream will be closed.
284 bfd_openstreamr (filename
, target
, streamarg
)
285 const char *filename
;
289 FILE *stream
= (FILE *) streamarg
;
291 const bfd_target
*target_vec
;
293 nbfd
= _bfd_new_bfd ();
297 target_vec
= bfd_find_target (target
, nbfd
);
298 if (target_vec
== NULL
)
300 bfd_set_error (bfd_error_invalid_target
);
301 objalloc_free ((struct objalloc
*) nbfd
->memory
);
306 nbfd
->iostream
= (PTR
) stream
;
307 nbfd
->filename
= filename
;
308 nbfd
->direction
= read_direction
;
310 if (! bfd_cache_init (nbfd
))
312 objalloc_free ((struct objalloc
*) nbfd
->memory
);
320 /** bfd_openw -- open for writing.
321 Returns a pointer to a freshly-allocated BFD on success, or NULL.
323 See comment by bfd_fdopenr before you try to modify this function. */
330 bfd *bfd_openw(CONST char *filename, CONST char *target);
333 Create a BFD, associated with file @var{filename}, using the
334 file format @var{target}, and return a pointer to it.
336 Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>,
337 <<bfd_error_invalid_target>>.
341 bfd_openw (filename
, target
)
342 CONST
char *filename
;
346 const bfd_target
*target_vec
;
348 bfd_set_error (bfd_error_system_call
);
350 /* nbfd has to point to head of malloc'ed block so that bfd_close may
351 reclaim it correctly. */
353 nbfd
= _bfd_new_bfd ();
357 target_vec
= bfd_find_target (target
, nbfd
);
358 if (target_vec
== NULL
)
360 objalloc_free ((struct objalloc
*) nbfd
->memory
);
365 nbfd
->filename
= filename
;
366 nbfd
->direction
= write_direction
;
368 if (bfd_open_file (nbfd
) == NULL
)
370 bfd_set_error (bfd_error_system_call
); /* File not writeable, etc */
371 objalloc_free ((struct objalloc
*) nbfd
->memory
);
385 boolean bfd_close(bfd *abfd);
389 Close a BFD. If the BFD was open for writing,
390 then pending operations are completed and the file written out
391 and closed. If the created file is executable, then
392 <<chmod>> is called to mark it as such.
394 All memory attached to the BFD is released.
396 The file descriptor associated with the BFD is closed (even
397 if it was passed in to BFD by <<bfd_fdopenr>>).
400 <<true>> is returned if all is ok, otherwise <<false>>.
410 if (!bfd_read_p (abfd
))
412 if (! BFD_SEND_FMT (abfd
, _bfd_write_contents
, (abfd
)))
416 if (! BFD_SEND (abfd
, _close_and_cleanup
, (abfd
)))
419 ret
= bfd_cache_close (abfd
);
421 /* If the file was open for writing and is now executable,
424 && abfd
->direction
== write_direction
425 && abfd
->flags
& EXEC_P
)
429 if (stat (abfd
->filename
, &buf
) == 0)
431 int mask
= umask (0);
433 chmod (abfd
->filename
,
435 & (buf
.st_mode
| ((S_IXUSR
| S_IXGRP
| S_IXOTH
) &~ mask
))));
439 objalloc_free ((struct objalloc
*) abfd
->memory
);
450 boolean bfd_close_all_done(bfd *);
453 Close a BFD. Differs from <<bfd_close>>
454 since it does not complete any pending operations. This
455 routine would be used if the application had just used BFD for
456 swapping and didn't want to use any of the writing code.
458 If the created file is executable, then <<chmod>> is called
461 All memory attached to the BFD is released.
464 <<true>> is returned if all is ok, otherwise <<false>>.
469 bfd_close_all_done (abfd
)
474 ret
= bfd_cache_close (abfd
);
476 /* If the file was open for writing and is now executable,
479 && abfd
->direction
== write_direction
480 && abfd
->flags
& EXEC_P
)
484 if (stat (abfd
->filename
, &buf
) == 0)
486 int mask
= umask (0);
488 chmod (abfd
->filename
,
490 & (buf
.st_mode
| ((S_IXUSR
| S_IXGRP
| S_IXOTH
) &~ mask
))));
494 objalloc_free ((struct objalloc
*) abfd
->memory
);
505 bfd *bfd_create(CONST char *filename, bfd *templ);
508 Create a new BFD in the manner of
509 <<bfd_openw>>, but without opening a file. The new BFD
510 takes the target from the target used by @var{template}. The
511 format is always set to <<bfd_object>>.
516 bfd_create (filename
, templ
)
517 CONST
char *filename
;
522 nbfd
= _bfd_new_bfd ();
525 nbfd
->filename
= filename
;
527 nbfd
->xvec
= templ
->xvec
;
528 nbfd
->direction
= no_direction
;
529 bfd_set_format (nbfd
, bfd_object
);
538 boolean bfd_make_writable(bfd *abfd);
541 Takes a BFD as created by <<bfd_create>> and converts it
542 into one like as returned by <<bfd_openw>>. It does this
543 by converting the BFD to BFD_IN_MEMORY. It's assumed that
544 you will call <<bfd_make_readable>> on this bfd later.
547 <<true>> is returned if all is ok, otherwise <<false>>.
551 bfd_make_writable(abfd
)
554 struct bfd_in_memory
*bim
;
556 if (abfd
->direction
!= no_direction
)
558 bfd_set_error (bfd_error_invalid_operation
);
562 bim
= (struct bfd_in_memory
*) bfd_malloc (sizeof (struct bfd_in_memory
));
563 abfd
->iostream
= (PTR
) bim
;
564 /* bfd_write will grow these as needed */
568 abfd
->flags
|= BFD_IN_MEMORY
;
569 abfd
->direction
= write_direction
;
580 boolean bfd_make_readable(bfd *abfd);
583 Takes a BFD as created by <<bfd_create>> and
584 <<bfd_make_writable>> and converts it into one like as
585 returned by <<bfd_openr>>. It does this by writing the
586 contents out to the memory buffer, then reversing the
590 <<true>> is returned if all is ok, otherwise <<false>>. */
593 bfd_make_readable(abfd
)
596 if (abfd
->direction
!= write_direction
|| !(abfd
->flags
& BFD_IN_MEMORY
))
598 bfd_set_error (bfd_error_invalid_operation
);
602 if (! BFD_SEND_FMT (abfd
, _bfd_write_contents
, (abfd
)))
605 if (! BFD_SEND (abfd
, _close_and_cleanup
, (abfd
)))
609 abfd
->arch_info
= &bfd_default_arch_struct
;
612 abfd
->sections
= (asection
*) NULL
;
613 abfd
->format
= bfd_unknown
;
614 abfd
->my_archive
= (bfd
*) NULL
;
616 abfd
->opened_once
= false;
617 abfd
->output_has_begun
= false;
618 abfd
->section_count
= 0;
619 abfd
->usrdata
= (PTR
) NULL
;
620 abfd
->cacheable
= false;
621 abfd
->flags
= BFD_IN_MEMORY
;
622 abfd
->mtime_set
= false;
624 abfd
->target_defaulted
= true;
625 abfd
->direction
= read_direction
;
628 abfd
->outsymbols
= 0;
631 bfd_check_format(abfd
, bfd_object
);
641 PTR bfd_alloc (bfd *abfd, size_t wanted);
644 Allocate a block of @var{wanted} bytes of memory attached to
645 <<abfd>> and return a pointer to it.
650 bfd_alloc (abfd
, size
)
656 ret
= objalloc_alloc (abfd
->memory
, (unsigned long) size
);
658 bfd_set_error (bfd_error_no_memory
);
663 bfd_zalloc (abfd
, size
)
669 res
= bfd_alloc (abfd
, size
);
671 memset (res
, 0, size
);
675 /* Free a block allocated for a BFD. */
678 bfd_release (abfd
, block
)
682 objalloc_free_block ((struct objalloc
*) abfd
->memory
, block
);