1 /* opncls.c -- open and close a BFD.
2 Copyright (C) 1990 91, 92, 93, 94, 1995 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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);
57 if (!obstack_begin(&nbfd->memory, 128))
59 bfd_set_error (bfd_error_no_memory);
63 nbfd->arch_info = &bfd_default_arch_struct;
65 nbfd->direction = no_direction;
66 nbfd->iostream = NULL;
68 nbfd->sections = (asection *)NULL;
69 nbfd->format = bfd_unknown;
70 nbfd->my_archive = (bfd *)NULL;
72 nbfd->opened_once = false;
73 nbfd->output_has_begun = false;
74 nbfd->section_count = 0;
75 nbfd->usrdata = (PTR)NULL;
76 nbfd->cacheable = false;
77 nbfd->flags = NO_FLAGS;
78 nbfd->mtime_set = false;
83 /* Allocate a new BFD as a member of archive OBFD. */
86 _bfd_new_bfd_contained_in (obfd)
91 nbfd = _bfd_new_bfd();
92 nbfd->xvec = obfd->xvec;
93 nbfd->my_archive = obfd;
94 nbfd->direction = read_direction;
95 nbfd->target_defaulted = obfd->target_defaulted;
101 Opening and closing BFDs
110 bfd *bfd_openr(CONST char *filename, CONST char *target);
113 Open the file @var{filename} (using <<fopen>>) with the target
114 @var{target}. Return a pointer to the created BFD.
116 Calls <<bfd_find_target>>, so @var{target} is interpreted as by
119 If <<NULL>> is returned then an error has occured. Possible errors
120 are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or <<system_call>> error.
124 bfd_openr (filename, target)
125 CONST char *filename;
129 const bfd_target *target_vec;
131 nbfd = _bfd_new_bfd();
133 bfd_set_error (bfd_error_no_memory);
137 target_vec = bfd_find_target (target, nbfd);
138 if (target_vec == NULL) {
139 bfd_set_error (bfd_error_invalid_target);
143 nbfd->filename = filename;
144 nbfd->direction = read_direction;
146 if (bfd_open_file (nbfd) == NULL) {
147 bfd_set_error (bfd_error_system_call); /* File didn't exist, or some such */
155 /* Don't try to `optimize' this function:
157 o - We lock using stack space so that interrupting the locking
158 won't cause a storage leak.
159 o - We open the file stream last, since we don't want to have to
160 close it if anything goes wrong. Closing the stream means closing
161 the file descriptor too, even though we didn't open it.
168 bfd *bfd_fdopenr(CONST char *filename, CONST char *target, int fd);
171 <<bfd_fdopenr>> is to <<bfd_fopenr>> much like <<fdopen>> is to <<fopen>>.
172 It opens a BFD on a file already described by the @var{fd}
175 When the file is later <<bfd_close>>d, the file descriptor will be closed.
177 If the caller desires that this file descriptor be cached by BFD
178 (opened as needed, closed as needed to free descriptors for
179 other opens), with the supplied @var{fd} used as an initial
180 file descriptor (but subject to closure at any time), call
181 bfd_set_cacheable(bfd, 1) on the returned BFD. The default is to
182 assume no cacheing; the file descriptor will remain open until
183 <<bfd_close>>, and will not be affected by BFD operations on other
186 Possible errors are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> and <<bfd_error_system_call>>.
190 bfd_fdopenr (filename, target, fd)
191 CONST char *filename;
196 const bfd_target *target_vec;
199 bfd_set_error (bfd_error_system_call);
201 #if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
202 fdflags = O_RDWR; /* Assume full access */
204 fdflags = fcntl (fd, F_GETFL, NULL);
206 if (fdflags == -1) return NULL;
208 nbfd = _bfd_new_bfd();
211 bfd_set_error (bfd_error_no_memory);
215 target_vec = bfd_find_target (target, nbfd);
216 if (target_vec == NULL) {
217 bfd_set_error (bfd_error_invalid_target);
220 #if defined(VMS) || defined(__GO32__) || defined (WIN32)
221 nbfd->iostream = (char *)fopen(filename, FOPEN_RB);
223 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
224 switch (fdflags & (O_ACCMODE)) {
225 case O_RDONLY: nbfd->iostream = (char *) fdopen (fd, FOPEN_RB); break;
226 case O_WRONLY: nbfd->iostream = (char *) fdopen (fd, FOPEN_RUB); break;
227 case O_RDWR: nbfd->iostream = (char *) fdopen (fd, FOPEN_RUB); break;
231 if (nbfd->iostream == NULL) {
232 (void) obstack_free (&nbfd->memory, (PTR)0);
236 /* OK, put everything where it belongs */
238 nbfd->filename = filename;
240 /* As a special case we allow a FD open for read/write to
241 be written through, although doing so requires that we end
242 the previous clause with a preposition. */
243 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
244 switch (fdflags & (O_ACCMODE)) {
245 case O_RDONLY: nbfd->direction = read_direction; break;
246 case O_WRONLY: nbfd->direction = write_direction; break;
247 case O_RDWR: nbfd->direction = both_direction; break;
251 if (! bfd_cache_init (nbfd))
262 bfd *bfd_openstreamr();
266 Open a BFD for read access on an existing stdio stream. When
267 the BFD is passed to <<bfd_close>>, the stream will be closed.
271 bfd_openstreamr (filename, target, stream)
272 const char *filename;
277 const bfd_target *target_vec;
279 nbfd = _bfd_new_bfd ();
282 bfd_set_error (bfd_error_no_memory);
286 target_vec = bfd_find_target (target, nbfd);
287 if (target_vec == NULL)
289 bfd_set_error (bfd_error_invalid_target);
293 nbfd->iostream = (char *) stream;
294 nbfd->filename = filename;
295 nbfd->direction = read_direction;
297 if (! bfd_cache_init (nbfd))
303 /** bfd_openw -- open for writing.
304 Returns a pointer to a freshly-allocated BFD on success, or NULL.
306 See comment by bfd_fdopenr before you try to modify this function. */
313 bfd *bfd_openw(CONST char *filename, CONST char *target);
316 Create a BFD, associated with file @var{filename}, using the
317 file format @var{target}, and return a pointer to it.
319 Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>,
320 <<bfd_error_invalid_target>>.
324 bfd_openw (filename, target)
325 CONST char *filename;
329 const bfd_target *target_vec;
331 bfd_set_error (bfd_error_system_call);
333 /* nbfd has to point to head of malloc'ed block so that bfd_close may
334 reclaim it correctly. */
336 nbfd = _bfd_new_bfd();
338 bfd_set_error (bfd_error_no_memory);
342 target_vec = bfd_find_target (target, nbfd);
343 if (target_vec == NULL) return NULL;
345 nbfd->filename = filename;
346 nbfd->direction = write_direction;
348 if (bfd_open_file (nbfd) == NULL) {
349 bfd_set_error (bfd_error_system_call); /* File not writeable, etc */
350 (void) obstack_free (&nbfd->memory, (PTR)0);
362 boolean bfd_close(bfd *abfd);
366 Close a BFD. If the BFD was open for writing,
367 then pending operations are completed and the file written out
368 and closed. If the created file is executable, then
369 <<chmod>> is called to mark it as such.
371 All memory attached to the BFD's obstacks is released.
373 The file descriptor associated with the BFD is closed (even
374 if it was passed in to BFD by <<bfd_fdopenr>>).
377 <<true>> is returned if all is ok, otherwise <<false>>.
387 if (!bfd_read_p (abfd))
389 if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
393 if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
396 ret = bfd_cache_close (abfd);
398 /* If the file was open for writing and is now executable,
401 && abfd->direction == write_direction
402 && abfd->flags & EXEC_P)
406 if (stat (abfd->filename, &buf) == 0)
408 int mask = umask (0);
410 chmod (abfd->filename,
412 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
416 (void) obstack_free (&abfd->memory, (PTR)0);
427 boolean bfd_close_all_done(bfd *);
430 Close a BFD. Differs from <<bfd_close>>
431 since it does not complete any pending operations. This
432 routine would be used if the application had just used BFD for
433 swapping and didn't want to use any of the writing code.
435 If the created file is executable, then <<chmod>> is called
438 All memory attached to the BFD's obstacks is released.
441 <<true>> is returned if all is ok, otherwise <<false>>.
446 bfd_close_all_done (abfd)
451 ret = bfd_cache_close (abfd);
453 /* If the file was open for writing and is now executable,
456 && abfd->direction == write_direction
457 && abfd->flags & EXEC_P)
461 if (stat (abfd->filename, &buf) == 0)
463 int mask = umask (0);
465 chmod (abfd->filename,
467 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
470 (void) obstack_free (&abfd->memory, (PTR)0);
481 bfd_size_type bfd_alloc_size(bfd *abfd);
484 Return the number of bytes in the obstacks connected to @var{abfd}.
489 bfd_alloc_size (abfd)
492 struct _obstack_chunk *chunk = abfd->memory.chunk;
495 size += chunk->limit - &(chunk->contents[0]);
508 bfd *bfd_create(CONST char *filename, bfd *templ);
511 Create a new BFD in the manner of
512 <<bfd_openw>>, but without opening a file. The new BFD
513 takes the target from the target used by @var{template}. The
514 format is always set to <<bfd_object>>.
519 bfd_create (filename, templ)
520 CONST char *filename;
523 bfd *nbfd = _bfd_new_bfd();
524 if (nbfd == (bfd *)NULL) {
525 bfd_set_error (bfd_error_no_memory);
528 nbfd->filename = filename;
530 nbfd->xvec = templ->xvec;
532 nbfd->direction = no_direction;
533 bfd_set_format(nbfd, bfd_object);
542 PTR bfd_alloc_by_size_t(bfd *abfd, size_t wanted);
545 Allocate a block of @var{wanted} bytes of memory in the obstack
546 attatched to <<abfd>> and return a pointer to it.
551 bfd_alloc_by_size_t (abfd, size)
555 return obstack_alloc(&(abfd->memory), size);
559 bfd_alloc_grow (abfd, ptr, size)
564 (void) obstack_grow(&(abfd->memory), ptr, size);
568 bfd_alloc_finish (abfd)
571 return obstack_finish(&(abfd->memory));
575 bfd_alloc (abfd, size)
579 return bfd_alloc_by_size_t(abfd, (size_t)size);
583 bfd_zalloc (abfd, size)
588 res = bfd_alloc(abfd, size);
590 memset(res, 0, (size_t)size);