977cd99e1c87fe0e6a99b9bedc438ee5bdb535bb
1 /* opncls.c -- open and close a BFD.
2 Copyright (C) 1990 91, 92, 93, 94 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #define S_IXUSR 0100 /* Execute by owner. */
30 #define S_IXGRP 0010 /* Execute by group. */
33 #define S_IXOTH 0001 /* Execute by others. */
36 /* fdopen is a loser -- we should use stdio exclusively. Unfortunately
37 if we do that we can't use fcntl. */
40 #define obstack_chunk_alloc malloc
41 #define obstack_chunk_free free
43 /* Return a new BFD. All BFD's are allocated through this routine. */
50 nbfd
= (bfd
*)bfd_zmalloc (sizeof (bfd
));
53 bfd_set_error (bfd_error_no_memory
);
58 if (!obstack_begin(&nbfd
->memory
, 128))
60 bfd_set_error (bfd_error_no_memory
);
64 nbfd
->arch_info
= &bfd_default_arch_struct
;
66 nbfd
->direction
= no_direction
;
67 nbfd
->iostream
= NULL
;
69 nbfd
->sections
= (asection
*)NULL
;
70 nbfd
->format
= bfd_unknown
;
71 nbfd
->my_archive
= (bfd
*)NULL
;
73 nbfd
->opened_once
= false;
74 nbfd
->output_has_begun
= false;
75 nbfd
->section_count
= 0;
76 nbfd
->usrdata
= (PTR
)NULL
;
77 nbfd
->cacheable
= false;
78 nbfd
->flags
= NO_FLAGS
;
79 nbfd
->mtime_set
= false;
84 /* Allocate a new BFD as a member of archive OBFD. */
87 _bfd_new_bfd_contained_in (obfd
)
92 nbfd
= _bfd_new_bfd();
93 nbfd
->xvec
= obfd
->xvec
;
94 nbfd
->my_archive
= obfd
;
95 nbfd
->direction
= read_direction
;
96 nbfd
->target_defaulted
= obfd
->target_defaulted
;
102 Opening and closing BFDs
111 bfd *bfd_openr(CONST char *filename, CONST char *target);
114 Open the file @var{filename} (using <<fopen>>) with the target
115 @var{target}. Return a pointer to the created BFD.
117 Calls <<bfd_find_target>>, so @var{target} is interpreted as by
120 If <<NULL>> is returned then an error has occured. Possible errors
121 are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or <<system_call>> error.
125 bfd_openr (filename
, target
)
126 CONST
char *filename
;
130 const bfd_target
*target_vec
;
132 nbfd
= _bfd_new_bfd();
134 bfd_set_error (bfd_error_no_memory
);
138 target_vec
= bfd_find_target (target
, nbfd
);
139 if (target_vec
== NULL
) {
140 bfd_set_error (bfd_error_invalid_target
);
144 nbfd
->filename
= filename
;
145 nbfd
->direction
= read_direction
;
147 if (bfd_open_file (nbfd
) == NULL
) {
148 bfd_set_error (bfd_error_system_call
); /* File didn't exist, or some such */
156 /* Don't try to `optimize' this function:
158 o - We lock using stack space so that interrupting the locking
159 won't cause a storage leak.
160 o - We open the file stream last, since we don't want to have to
161 close it if anything goes wrong. Closing the stream means closing
162 the file descriptor too, even though we didn't open it.
169 bfd *bfd_fdopenr(CONST char *filename, CONST char *target, int fd);
172 <<bfd_fdopenr>> is to <<bfd_fopenr>> much like <<fdopen>> is to <<fopen>>.
173 It opens a BFD on a file already described by the @var{fd}
176 When the file is later <<bfd_close>>d, the file descriptor will be closed.
178 If the caller desires that this file descriptor be cached by BFD
179 (opened as needed, closed as needed to free descriptors for
180 other opens), with the supplied @var{fd} used as an initial
181 file descriptor (but subject to closure at any time), call
182 bfd_set_cacheable(bfd, 1) on the returned BFD. The default is to
183 assume no cacheing; the file descriptor will remain open until
184 <<bfd_close>>, and will not be affected by BFD operations on other
187 Possible errors are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> and <<bfd_error_system_call>>.
191 bfd_fdopenr (filename
, target
, fd
)
192 CONST
char *filename
;
197 const bfd_target
*target_vec
;
200 bfd_set_error (bfd_error_system_call
);
203 fdflags
= O_RDWR
; /* Assume full access */
205 fdflags
= fcntl (fd
, F_GETFL
, NULL
);
207 if (fdflags
== -1) return NULL
;
209 nbfd
= _bfd_new_bfd();
212 bfd_set_error (bfd_error_no_memory
);
216 target_vec
= bfd_find_target (target
, nbfd
);
217 if (target_vec
== NULL
) {
218 bfd_set_error (bfd_error_invalid_target
);
221 #if defined(VMS) || defined(__GO32__) || defined (WIN32)
222 nbfd
->iostream
= (char *)fopen(filename
, FOPEN_RB
);
224 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
225 switch (fdflags
& (O_ACCMODE
)) {
226 case O_RDONLY
: nbfd
->iostream
= (char *) fdopen (fd
, FOPEN_RB
); break;
227 case O_WRONLY
: nbfd
->iostream
= (char *) fdopen (fd
, FOPEN_RUB
); break;
228 case O_RDWR
: nbfd
->iostream
= (char *) fdopen (fd
, FOPEN_RUB
); break;
232 if (nbfd
->iostream
== NULL
) {
233 (void) obstack_free (&nbfd
->memory
, (PTR
)0);
237 /* OK, put everything where it belongs */
239 nbfd
->filename
= filename
;
241 /* As a special case we allow a FD open for read/write to
242 be written through, although doing so requires that we end
243 the previous clause with a preposition. */
244 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
245 switch (fdflags
& (O_ACCMODE
)) {
246 case O_RDONLY
: nbfd
->direction
= read_direction
; break;
247 case O_WRONLY
: nbfd
->direction
= write_direction
; break;
248 case O_RDWR
: nbfd
->direction
= both_direction
; break;
252 if (! bfd_cache_init (nbfd
))
258 /** bfd_openw -- open for writing.
259 Returns a pointer to a freshly-allocated BFD on success, or NULL.
261 See comment by bfd_fdopenr before you try to modify this function. */
268 bfd *bfd_openw(CONST char *filename, CONST char *target);
271 Create a BFD, associated with file @var{filename}, using the
272 file format @var{target}, and return a pointer to it.
274 Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>,
275 <<bfd_error_invalid_target>>.
279 bfd_openw (filename
, target
)
280 CONST
char *filename
;
284 const bfd_target
*target_vec
;
286 bfd_set_error (bfd_error_system_call
);
288 /* nbfd has to point to head of malloc'ed block so that bfd_close may
289 reclaim it correctly. */
291 nbfd
= _bfd_new_bfd();
293 bfd_set_error (bfd_error_no_memory
);
297 target_vec
= bfd_find_target (target
, nbfd
);
298 if (target_vec
== NULL
) return NULL
;
300 nbfd
->filename
= filename
;
301 nbfd
->direction
= write_direction
;
303 if (bfd_open_file (nbfd
) == NULL
) {
304 bfd_set_error (bfd_error_system_call
); /* File not writeable, etc */
305 (void) obstack_free (&nbfd
->memory
, (PTR
)0);
317 boolean bfd_close(bfd *abfd);
321 Close a BFD. If the BFD was open for writing,
322 then pending operations are completed and the file written out
323 and closed. If the created file is executable, then
324 <<chmod>> is called to mark it as such.
326 All memory attached to the BFD's obstacks is released.
328 The file descriptor associated with the BFD is closed (even
329 if it was passed in to BFD by <<bfd_fdopenr>>).
332 <<true>> is returned if all is ok, otherwise <<false>>.
342 if (!bfd_read_p (abfd
))
344 if (! BFD_SEND_FMT (abfd
, _bfd_write_contents
, (abfd
)))
348 if (! BFD_SEND (abfd
, _close_and_cleanup
, (abfd
)))
351 ret
= bfd_cache_close (abfd
);
353 /* If the file was open for writing and is now executable,
356 && abfd
->direction
== write_direction
357 && abfd
->flags
& EXEC_P
)
361 if (stat (abfd
->filename
, &buf
) == 0)
363 int mask
= umask (0);
365 chmod (abfd
->filename
,
367 & (buf
.st_mode
| ((S_IXUSR
| S_IXGRP
| S_IXOTH
) &~ mask
))));
371 (void) obstack_free (&abfd
->memory
, (PTR
)0);
382 boolean bfd_close_all_done(bfd *);
385 Close a BFD. Differs from <<bfd_close>>
386 since it does not complete any pending operations. This
387 routine would be used if the application had just used BFD for
388 swapping and didn't want to use any of the writing code.
390 If the created file is executable, then <<chmod>> is called
393 All memory attached to the BFD's obstacks is released.
396 <<true>> is returned if all is ok, otherwise <<false>>.
401 bfd_close_all_done (abfd
)
406 ret
= bfd_cache_close (abfd
);
408 /* If the file was open for writing and is now executable,
411 && abfd
->direction
== write_direction
412 && abfd
->flags
& EXEC_P
)
416 if (stat (abfd
->filename
, &buf
) == 0)
418 int mask
= umask (0);
420 chmod (abfd
->filename
,
422 & (buf
.st_mode
| ((S_IXUSR
| S_IXGRP
| S_IXOTH
) &~ mask
))));
425 (void) obstack_free (&abfd
->memory
, (PTR
)0);
436 bfd_size_type bfd_alloc_size(bfd *abfd);
439 Return the number of bytes in the obstacks connected to @var{abfd}.
444 bfd_alloc_size (abfd
)
447 struct _obstack_chunk
*chunk
= abfd
->memory
.chunk
;
450 size
+= chunk
->limit
- &(chunk
->contents
[0]);
463 bfd *bfd_create(CONST char *filename, bfd *templ);
466 Create a new BFD in the manner of
467 <<bfd_openw>>, but without opening a file. The new BFD
468 takes the target from the target used by @var{template}. The
469 format is always set to <<bfd_object>>.
474 bfd_create (filename
, templ
)
475 CONST
char *filename
;
478 bfd
*nbfd
= _bfd_new_bfd();
479 if (nbfd
== (bfd
*)NULL
) {
480 bfd_set_error (bfd_error_no_memory
);
483 nbfd
->filename
= filename
;
485 nbfd
->xvec
= templ
->xvec
;
487 nbfd
->direction
= no_direction
;
488 bfd_set_format(nbfd
, bfd_object
);
497 PTR bfd_alloc_by_size_t(bfd *abfd, size_t wanted);
500 Allocate a block of @var{wanted} bytes of memory in the obstack
501 attatched to <<abfd>> and return a pointer to it.
506 bfd_alloc_by_size_t (abfd
, size
)
510 return obstack_alloc(&(abfd
->memory
), size
);
514 bfd_alloc_grow (abfd
, ptr
, size
)
519 (void) obstack_grow(&(abfd
->memory
), ptr
, size
);
523 bfd_alloc_finish (abfd
)
526 return obstack_finish(&(abfd
->memory
));
530 bfd_alloc (abfd
, size
)
534 return bfd_alloc_by_size_t(abfd
, (size_t)size
);
538 bfd_zalloc (abfd
, size
)
543 res
= bfd_alloc(abfd
, size
);
545 memset(res
, 0, (size_t)size
);