1 /* opncls.c -- open and close a BFD.
2 Copyright (C) 1990-1991 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 extern void bfd_cache_init();
28 FILE *bfd_open_file();
30 /* fdopen is a loser -- we should use stdio exclusively. Unfortunately
31 if we do that we can't use fcntl. */
34 #define obstack_chunk_alloc bfd_xmalloc
35 #define obstack_chunk_free free
37 /* Return a new BFD. All BFD's are allocated through this routine. */
43 nbfd = (bfd *)zalloc (sizeof (bfd));
48 obstack_begin((PTR)&nbfd->memory, 128);
50 nbfd->arch_info = &bfd_default_arch_struct;
52 nbfd->direction = no_direction;
53 nbfd->iostream = NULL;
55 nbfd->sections = (asection *)NULL;
56 nbfd->format = bfd_unknown;
57 nbfd->my_archive = (bfd *)NULL;
59 nbfd->opened_once = false;
60 nbfd->output_has_begun = false;
61 nbfd->section_count = 0;
62 nbfd->usrdata = (PTR)NULL;
63 nbfd->sections = (asection *)NULL;
64 nbfd->cacheable = false;
65 nbfd->flags = NO_FLAGS;
70 /* Allocate a new BFD as a member of archive OBFD. */
72 bfd *new_bfd_contained_in(obfd)
75 bfd *nbfd = new_bfd();
76 nbfd->xvec = obfd->xvec;
77 nbfd->my_archive = obfd;
78 nbfd->direction = read_direction;
84 Opening and Closing BFDs
93 bfd *bfd_openr(CONST char *filename, CONST char*target);
96 This function opens the file supplied (using <<fopen>>) with the target
97 supplied, it returns a pointer to the created BFD.
99 If NULL is returned then an error has occured. Possible errors
100 are <<no_memory>>, <<invalid_target>> or <<system_call>> error.
104 DEFUN(bfd_openr, (filename, target),
105 CONST char *filename AND
109 bfd_target *target_vec;
113 bfd_error = no_memory;
117 target_vec = bfd_find_target (target, nbfd);
118 if (target_vec == NULL) {
119 bfd_error = invalid_target;
123 nbfd->filename = filename;
124 nbfd->direction = read_direction;
126 if (bfd_open_file (nbfd) == NULL) {
127 bfd_error = system_call_error; /* File didn't exist, or some such */
135 /* Don't try to `optimize' this function:
137 o - We lock using stack space so that interrupting the locking
138 won't cause a storage leak.
139 o - We open the file stream last, since we don't want to have to
140 close it if anything goes wrong. Closing the stream means closing
141 the file descriptor too, even though we didn't open it.
148 bfd *bfd_fdopenr(CONST char *filename, CONST char *target, int fd);
151 bfd_fdopenr is to bfd_fopenr much like fdopen is to fopen.
152 It opens a BFD on a file already described by the @var{fd}
155 Possible errors are no_memory, invalid_target and system_call
160 DEFUN(bfd_fdopenr,(filename, target, fd),
161 CONST char *filename AND
162 CONST char *target AND
166 bfd_target *target_vec;
169 bfd_error = system_call_error;
172 fdflags = O_RDWR; /* Assume full access */
174 fdflags = fcntl (fd, F_GETFL, NULL);
176 if (fdflags == -1) return NULL;
181 bfd_error = no_memory;
185 target_vec = bfd_find_target (target, nbfd);
186 if (target_vec == NULL) {
187 bfd_error = invalid_target;
191 #ifdef FASCIST_FDOPEN
192 nbfd->iostream = (char *) fdopen (fd, "rb");
194 /* if the fd were open for read only, this still would not hurt: */
195 nbfd->iostream = (char *) fdopen (fd, "r+b");
197 if (nbfd->iostream == NULL) {
198 (void) obstack_free (&nbfd->memory, (PTR)0);
202 /* OK, put everything where it belongs */
204 nbfd->filename = filename;
206 /* As a special case we allow a FD open for read/write to
207 be written through, although doing so requires that we end
208 the previous clause with a preposition. */
209 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
210 switch (fdflags & (O_ACCMODE)) {
211 case O_RDONLY: nbfd->direction = read_direction; break;
212 case O_WRONLY: nbfd->direction = write_direction; break;
213 case O_RDWR: nbfd->direction = both_direction; break;
217 bfd_cache_init (nbfd);
222 /** bfd_openw -- open for writing.
223 Returns a pointer to a freshly-allocated BFD on success, or NULL.
225 See comment by bfd_fdopenr before you try to modify this function. */
232 bfd *bfd_openw(CONST char *filename, CONST char *target);
235 Creates a BFD, associated with file @var{filename}, using the
236 file format @var{target}, and returns a pointer to it.
238 Possible errors are system_call_error, no_memory,
243 DEFUN(bfd_openw,(filename, target),
244 CONST char *filename AND
248 bfd_target *target_vec;
250 bfd_error = system_call_error;
252 /* nbfd has to point to head of malloc'ed block so that bfd_close may
253 reclaim it correctly. */
257 bfd_error = no_memory;
261 target_vec = bfd_find_target (target, nbfd);
262 if (target_vec == NULL) return NULL;
264 nbfd->filename = filename;
265 nbfd->direction = write_direction;
267 if (bfd_open_file (nbfd) == NULL) {
268 bfd_error = system_call_error; /* File not writeable, etc */
269 (void) obstack_free (&nbfd->memory, (PTR)0);
281 boolean bfd_close(bfd *);
285 This function closes a BFD. If the BFD was open for writing,
286 then pending operations are completed and the file written out
287 and closed. If the created file is executable, then
288 <<chmod>> is called to mark it as such.
290 All memory attached to the BFD's obstacks is released.
293 <<true>> is returned if all is ok, otherwise <<false>>.
298 DEFUN(bfd_close,(abfd),
301 if (!bfd_read_p(abfd))
302 if (BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)) != true)
305 if (BFD_SEND (abfd, _close_and_cleanup, (abfd)) != true) return false;
307 bfd_cache_close(abfd);
309 /* If the file was open for writing and is now executable,
311 if (abfd->direction == write_direction
312 && abfd->flags & EXEC_P) {
314 stat(abfd->filename, &buf);
316 #define S_IXUSR 0100 /* Execute by owner. */
319 #define S_IXGRP 0010 /* Execute by group. */
322 #define S_IXOTH 0001 /* Execute by others. */
325 chmod(abfd->filename, 0777 & (buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH));
327 (void) obstack_free (&abfd->memory, (PTR)0);
337 boolean bfd_close_all_done(bfd *);
340 This function closes a BFD. It differs from <<bfd_close>>
341 since it does not complete any pending operations. This
342 routine would be used if the application had just used BFD for
343 swapping and didn't want to use any of the writing code.
345 If the created file is executable, then <<chmod>> is called
348 All memory attached to the BFD's obstacks is released.
351 <<true>> is returned if all is ok, otherwise <<false>>.
356 DEFUN(bfd_close_all_done,(abfd),
359 bfd_cache_close(abfd);
361 /* If the file was open for writing and is now executable,
363 if (abfd->direction == write_direction
364 && abfd->flags & EXEC_P) {
366 stat(abfd->filename, &buf);
368 #define S_IXUSR 0100 /* Execute by owner. */
371 #define S_IXGRP 0010 /* Execute by group. */
374 #define S_IXOTH 0001 /* Execute by others. */
377 chmod(abfd->filename, 0x777 &(buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH));
379 (void) obstack_free (&abfd->memory, (PTR)0);
390 bfd_size_type bfd_alloc_size(bfd *abfd);
393 Return the number of bytes in the obstacks connected to the
399 DEFUN(bfd_alloc_size,(abfd),
402 struct _obstack_chunk *chunk = abfd->memory.chunk;
405 size += chunk->limit - &(chunk->contents[0]);
418 bfd *bfd_create(CONST char *filename, bfd *template);
421 This routine creates a new BFD in the manner of
422 <<bfd_openw>>, but without opening a file. The new BFD
423 takes the target from the target used by @var{template}. The
424 format is always set to <<bfd_object>>.
429 DEFUN(bfd_create,(filename, template),
430 CONST char *filename AND
433 bfd *nbfd = new_bfd();
434 if (nbfd == (bfd *)NULL) {
435 bfd_error = no_memory;
438 nbfd->filename = filename;
440 nbfd->xvec = template->xvec;
442 nbfd->direction = no_direction;
443 bfd_set_format(nbfd, bfd_object);
452 PTR bfd_alloc_by_size_t(bfd *abfd, size_t wanted);
455 This function allocates a block of memory in the obstack
456 attatched to <<abfd>> and returns a pointer to it.
461 DEFUN(bfd_alloc_by_size_t,(abfd, size),
465 PTR res = obstack_alloc(&(abfd->memory), size);
469 DEFUN(void bfd_alloc_grow,(abfd, ptr, size),
474 (void) obstack_grow(&(abfd->memory), ptr, size);
476 DEFUN(PTR bfd_alloc_finish,(abfd),
479 return obstack_finish(&(abfd->memory));
482 DEFUN(PTR bfd_alloc, (abfd, size),
486 return bfd_alloc_by_size_t(abfd, (size_t)size);
489 DEFUN(PTR bfd_zalloc,(abfd, size),
493 PTR res = bfd_alloc(abfd, size);
494 memset(res, 0, (size_t)size);
498 DEFUN(PTR bfd_realloc,(abfd, old, size),
503 PTR res = bfd_alloc(abfd, size);
504 memcpy(res, old, (size_t)size);