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
))
263 bfd *bfd_openstreamr();
267 Open a BFD for read access on an existing stdio stream. When
268 the BFD is passed to <<bfd_close>>, the stream will be closed.
272 bfd_openstreamr (filename
, target
, stream
)
273 const char *filename
;
278 const bfd_target
*target_vec
;
280 nbfd
= _bfd_new_bfd ();
283 bfd_set_error (bfd_error_no_memory
);
287 target_vec
= bfd_find_target (target
, nbfd
);
288 if (target_vec
== NULL
)
290 bfd_set_error (bfd_error_invalid_target
);
294 nbfd
->iostream
= (char *) stream
;
295 nbfd
->filename
= filename
;
296 nbfd
->direction
= read_direction
;
298 if (! bfd_cache_init (nbfd
))
304 /** bfd_openw -- open for writing.
305 Returns a pointer to a freshly-allocated BFD on success, or NULL.
307 See comment by bfd_fdopenr before you try to modify this function. */
314 bfd *bfd_openw(CONST char *filename, CONST char *target);
317 Create a BFD, associated with file @var{filename}, using the
318 file format @var{target}, and return a pointer to it.
320 Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>,
321 <<bfd_error_invalid_target>>.
325 bfd_openw (filename
, target
)
326 CONST
char *filename
;
330 const bfd_target
*target_vec
;
332 bfd_set_error (bfd_error_system_call
);
334 /* nbfd has to point to head of malloc'ed block so that bfd_close may
335 reclaim it correctly. */
337 nbfd
= _bfd_new_bfd();
339 bfd_set_error (bfd_error_no_memory
);
343 target_vec
= bfd_find_target (target
, nbfd
);
344 if (target_vec
== NULL
) return NULL
;
346 nbfd
->filename
= filename
;
347 nbfd
->direction
= write_direction
;
349 if (bfd_open_file (nbfd
) == NULL
) {
350 bfd_set_error (bfd_error_system_call
); /* File not writeable, etc */
351 (void) obstack_free (&nbfd
->memory
, (PTR
)0);
363 boolean bfd_close(bfd *abfd);
367 Close a BFD. If the BFD was open for writing,
368 then pending operations are completed and the file written out
369 and closed. If the created file is executable, then
370 <<chmod>> is called to mark it as such.
372 All memory attached to the BFD's obstacks is released.
374 The file descriptor associated with the BFD is closed (even
375 if it was passed in to BFD by <<bfd_fdopenr>>).
378 <<true>> is returned if all is ok, otherwise <<false>>.
388 if (!bfd_read_p (abfd
))
390 if (! BFD_SEND_FMT (abfd
, _bfd_write_contents
, (abfd
)))
394 if (! BFD_SEND (abfd
, _close_and_cleanup
, (abfd
)))
397 ret
= bfd_cache_close (abfd
);
399 /* If the file was open for writing and is now executable,
402 && abfd
->direction
== write_direction
403 && abfd
->flags
& EXEC_P
)
407 if (stat (abfd
->filename
, &buf
) == 0)
409 int mask
= umask (0);
411 chmod (abfd
->filename
,
413 & (buf
.st_mode
| ((S_IXUSR
| S_IXGRP
| S_IXOTH
) &~ mask
))));
417 (void) obstack_free (&abfd
->memory
, (PTR
)0);
428 boolean bfd_close_all_done(bfd *);
431 Close a BFD. Differs from <<bfd_close>>
432 since it does not complete any pending operations. This
433 routine would be used if the application had just used BFD for
434 swapping and didn't want to use any of the writing code.
436 If the created file is executable, then <<chmod>> is called
439 All memory attached to the BFD's obstacks is released.
442 <<true>> is returned if all is ok, otherwise <<false>>.
447 bfd_close_all_done (abfd
)
452 ret
= bfd_cache_close (abfd
);
454 /* If the file was open for writing and is now executable,
457 && abfd
->direction
== write_direction
458 && abfd
->flags
& EXEC_P
)
462 if (stat (abfd
->filename
, &buf
) == 0)
464 int mask
= umask (0);
466 chmod (abfd
->filename
,
468 & (buf
.st_mode
| ((S_IXUSR
| S_IXGRP
| S_IXOTH
) &~ mask
))));
471 (void) obstack_free (&abfd
->memory
, (PTR
)0);
482 bfd_size_type bfd_alloc_size(bfd *abfd);
485 Return the number of bytes in the obstacks connected to @var{abfd}.
490 bfd_alloc_size (abfd
)
493 struct _obstack_chunk
*chunk
= abfd
->memory
.chunk
;
496 size
+= chunk
->limit
- &(chunk
->contents
[0]);
509 bfd *bfd_create(CONST char *filename, bfd *templ);
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>>.
520 bfd_create (filename
, templ
)
521 CONST
char *filename
;
524 bfd
*nbfd
= _bfd_new_bfd();
525 if (nbfd
== (bfd
*)NULL
) {
526 bfd_set_error (bfd_error_no_memory
);
529 nbfd
->filename
= filename
;
531 nbfd
->xvec
= templ
->xvec
;
533 nbfd
->direction
= no_direction
;
534 bfd_set_format(nbfd
, bfd_object
);
543 PTR bfd_alloc_by_size_t(bfd *abfd, size_t wanted);
546 Allocate a block of @var{wanted} bytes of memory in the obstack
547 attatched to <<abfd>> and return a pointer to it.
552 bfd_alloc_by_size_t (abfd
, size
)
556 return obstack_alloc(&(abfd
->memory
), size
);
560 bfd_alloc_grow (abfd
, ptr
, size
)
565 (void) obstack_grow(&(abfd
->memory
), ptr
, size
);
569 bfd_alloc_finish (abfd
)
572 return obstack_finish(&(abfd
->memory
));
576 bfd_alloc (abfd
, size
)
580 return bfd_alloc_by_size_t(abfd
, (size_t)size
);
584 bfd_zalloc (abfd
, size
)
589 res
= bfd_alloc(abfd
, size
);
591 memset(res
, 0, (size_t)size
);
This page took 0.050156 seconds and 4 git commands to generate.