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 malloc
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));
47 obstack_begin(&nbfd->memory, 128);
49 nbfd->direction = no_direction;
50 nbfd->iostream = NULL;
52 nbfd->sections = (asection *)NULL;
53 nbfd->format = bfd_unknown;
54 nbfd->my_archive = (bfd *)NULL;
56 nbfd->opened_once = false;
57 nbfd->output_has_begun = false;
58 nbfd->section_count = 0;
59 nbfd->usrdata = (PTR)NULL;
60 nbfd->sections = (asection *)NULL;
61 nbfd->cacheable = false;
62 nbfd->flags = NO_FLAGS;
67 /* Allocate a new BFD as a member of archive OBFD. */
69 bfd *new_bfd_contained_in(obfd)
72 bfd *nbfd = new_bfd();
73 nbfd->xvec = obfd->xvec;
74 nbfd->my_archive = obfd;
75 nbfd->direction = read_direction;
80 @section Opening and Closing BFDs
85 Opens the file supplied (using @code{fopen}) with the target supplied, it
86 returns a pointer to the created BFD.
88 If NULL is returned then an error has occured.
89 Possible errors are no_memory, invalid_target or system_call error.
90 *; PROTO(bfd*, bfd_openr, (CONST char *filename,CONST char*target));
94 DEFUN(bfd_openr, (filename, target),
95 CONST char *filename AND
99 bfd_target *target_vec;
103 bfd_error = no_memory;
107 target_vec = bfd_find_target (target, nbfd);
108 if (target_vec == NULL) {
109 bfd_error = invalid_target;
113 nbfd->filename = filename;
114 nbfd->direction = read_direction;
116 if (bfd_open_file (nbfd) == NULL) {
117 bfd_error = system_call_error; /* File didn't exist, or some such */
125 /* Don't try to `optimize' this function:
127 o - We lock using stack space so that interrupting the locking
128 won't cause a storage leak.
129 o - We open the file stream last, since we don't want to have to
130 close it if anything goes wrong. Closing the stream means closing
131 the file descriptor too, even though we didn't open it.
135 bfd_fdopenr is to bfd_fopenr much like fdopen is to fopen. It opens a BFD on
136 a file already described by the @var{fd} supplied.
138 Possible errors are no_memory, invalid_target and system_call error.
139 *; PROTO(bfd *, bfd_fdopenr,
140 (CONST char *filename, CONST char *target, int fd));
144 DEFUN(bfd_fdopenr,(filename, target, fd),
145 CONST char *filename AND
146 CONST char *target AND
150 bfd_target *target_vec;
153 bfd_error = system_call_error;
156 fdflags = O_RDWR; /* Assume full access */
158 fdflags = fcntl (fd, F_GETFL, NULL);
160 if (fdflags == -1) return NULL;
165 bfd_error = no_memory;
169 target_vec = bfd_find_target (target, nbfd);
170 if (target_vec == NULL) {
171 bfd_error = invalid_target;
175 /* if the fd were open for read only, this still would not hurt: */
176 nbfd->iostream = (char *) fdopen (fd, "r+");
177 if (nbfd->iostream == NULL) {
178 (void) obstack_free (&nbfd->memory, (PTR)0);
182 /* OK, put everything where it belongs */
184 nbfd->filename = filename;
186 /* As a special case we allow a FD open for read/write to
187 be written through, although doing so requires that we end
188 the previous clause with a preposition. */
189 switch (fdflags & O_ACCMODE) {
190 case O_RDONLY: nbfd->direction = read_direction; break;
191 case O_WRONLY: nbfd->direction = write_direction; break;
192 case O_RDWR: nbfd->direction = both_direction; break;
196 bfd_cache_init (nbfd);
201 /** bfd_openw -- open for writing.
202 Returns a pointer to a freshly-allocated BFD on success, or NULL.
204 See comment by bfd_fdopenr before you try to modify this function. */
207 Creates a BFD, associated with file @var{filename}, using the file
208 format @var{target}, and returns a pointer to it.
210 Possible errors are system_call_error, no_memory, invalid_target.
211 *; PROTO(bfd *, bfd_openw, (CONST char *filename, CONST char *target));
215 DEFUN(bfd_openw,(filename, target),
216 CONST char *filename AND
220 bfd_target *target_vec;
222 bfd_error = system_call_error;
224 /* nbfd has to point to head of malloc'ed block so that bfd_close may
225 reclaim it correctly. */
229 bfd_error = no_memory;
233 target_vec = bfd_find_target (target, nbfd);
234 if (target_vec == NULL) return NULL;
236 nbfd->filename = filename;
237 nbfd->direction = write_direction;
239 if (bfd_open_file (nbfd) == NULL) {
240 bfd_error = system_call_error; /* File not writeable, etc */
241 (void) obstack_free (&nbfd->memory, (PTR)0);
248 This function closes a BFD. If the BFD was open for writing, then
249 pending operations are completed and the file written out and closed.
250 If the created file is executable, then @code{chmod} is called to mark
253 All memory attached to the BFD's obstacks is released.
255 @code{true} is returned if all is ok, otherwise @code{false}.
256 *; PROTO(boolean, bfd_close,(bfd *));
260 DEFUN(bfd_close,(abfd),
263 if (!bfd_read_p(abfd))
264 if (BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)) != true)
267 if (BFD_SEND (abfd, _close_and_cleanup, (abfd)) != true) return false;
269 bfd_cache_close(abfd);
271 /* If the file was open for writing and is now executable,
273 if (abfd->direction == write_direction
274 && abfd->flags & EXEC_P) {
276 stat(abfd->filename, &buf);
278 #define S_IXUSR 0100 /* Execute by owner. */
281 #define S_IXGRP 0010 /* Execute by group. */
284 #define S_IXOTH 0001 /* Execute by others. */
287 chmod(abfd->filename,buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH);
289 (void) obstack_free (&abfd->memory, (PTR)0);
290 /* FIXME, shouldn't we de-allocate the bfd as well? */
295 This routine creates a new BFD in the manner of @code{bfd_openw}, but without
296 opening a file. The new BFD takes the target from the target used by
297 @var{template}. The format is always set to @code{bfd_object}.
299 *; PROTO(bfd *, bfd_create, (CONST char *filename, bfd *template));
303 DEFUN(bfd_create,(filename, template),
304 CONST char *filename AND
307 bfd *nbfd = new_bfd();
308 if (nbfd == (bfd *)NULL) {
309 bfd_error = no_memory;
312 nbfd->filename = filename;
314 nbfd->xvec = template->xvec;
316 nbfd->direction = no_direction;
317 bfd_set_format(nbfd, bfd_object);
321 /* Memory allocation */
323 DEFUN(PTR bfd_alloc_by_size_t,(abfd, size),
327 PTR res = obstack_alloc(&(abfd->memory), size);
331 DEFUN(void bfd_alloc_grow,(abfd, ptr, size),
336 (void) obstack_grow(&(abfd->memory), ptr, size);
338 DEFUN(PTR bfd_alloc_finish,(abfd),
341 return obstack_finish(&(abfd->memory));
344 DEFUN(PTR bfd_alloc, (abfd, size),
348 return bfd_alloc_by_size_t(abfd, (size_t)size);
351 DEFUN(PTR bfd_zalloc,(abfd, size),
355 PTR res = bfd_alloc(abfd, size);
356 memset(res, 0, (size_t)size);
360 DEFUN(PTR bfd_realloc,(abfd, old, size),
365 PTR res = bfd_alloc(abfd, size);
366 memcpy(res, old, (size_t)size);
370 /*proto* bfd_alloc_size
371 Return the number of bytes in the obstacks connected to the supplied
373 *; PROTO(bfd_size_type,bfd_alloc_size,(bfd *abfd));
377 DEFUN( bfd_alloc_size,(abfd),
380 struct _obstack_chunk *chunk = abfd->memory.chunk;
383 size += chunk->limit - &(chunk->contents[0]);