* cpu-h8300.c: Add support for MEMIND addressing mode
[deliverable/binutils-gdb.git] / bfd / opncls.c
CommitLineData
6724ff46
RP
1/* opncls.c -- open and close a BFD.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
fc723380 4
6724ff46 5This file is part of BFD, the Binary File Descriptor library.
4a81b561 6
6724ff46 7This program is free software; you can redistribute it and/or modify
4a81b561 8it under the terms of the GNU General Public License as published by
6724ff46
RP
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
4a81b561 11
6724ff46 12This program is distributed in the hope that it will be useful,
4a81b561
DHW
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
6724ff46
RP
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
4a81b561
DHW
20
21/* $Id$ */
22
4a81b561 23#include "bfd.h"
ff7ce170 24#include "sysdep.h"
4a81b561 25#include "libbfd.h"
6724ff46 26#include "obstack.h"
4a81b561
DHW
27extern void bfd_cache_init();
28FILE *bfd_open_file();
29
30/* fdopen is a loser -- we should use stdio exclusively. Unfortunately
31 if we do that we can't use fcntl. */
6724ff46 32
4a81b561 33
ff7ce170 34#define obstack_chunk_alloc bfd_xmalloc
9872a49c
SC
35#define obstack_chunk_free free
36
fc723380
JG
37/* Return a new BFD. All BFD's are allocated through this routine. */
38
4a81b561
DHW
39bfd *new_bfd()
40{
9872a49c 41 bfd *nbfd;
fc723380 42
b1847ba9
JG
43 nbfd = (bfd *)zalloc (sizeof (bfd));
44 if (!nbfd)
45 return 0;
4a81b561 46
ff7ce170
PB
47 bfd_check_init();
48 obstack_begin((PTR)&nbfd->memory, 128);
49
50 nbfd->arch_info = &bfd_default_arch_struct;
51
4a81b561
DHW
52 nbfd->direction = no_direction;
53 nbfd->iostream = NULL;
54 nbfd->where = 0;
55 nbfd->sections = (asection *)NULL;
56 nbfd->format = bfd_unknown;
57 nbfd->my_archive = (bfd *)NULL;
58 nbfd->origin = 0;
59 nbfd->opened_once = false;
60 nbfd->output_has_begun = false;
61 nbfd->section_count = 0;
9846338e 62 nbfd->usrdata = (PTR)NULL;
4a81b561
DHW
63 nbfd->sections = (asection *)NULL;
64 nbfd->cacheable = false;
65 nbfd->flags = NO_FLAGS;
fc723380 66 nbfd->mtime_set = 0;
4a81b561
DHW
67 return nbfd;
68}
fc723380
JG
69
70/* Allocate a new BFD as a member of archive OBFD. */
71
4a81b561
DHW
72bfd *new_bfd_contained_in(obfd)
73bfd *obfd;
74{
9846338e 75 bfd *nbfd = new_bfd();
4a81b561
DHW
76 nbfd->xvec = obfd->xvec;
77 nbfd->my_archive = obfd;
78 nbfd->direction = read_direction;
79 return nbfd;
80}
81
6f715d66
SC
82/*doc*
83@section Opening and Closing BFDs
84
85*/
86/*proto*
87*i bfd_openr
6724ff46
RP
88Opens the file supplied (using @code{fopen}) with the target supplied, it
89returns a pointer to the created BFD.
6f715d66
SC
90
91If NULL is returned then an error has occured.
92Possible errors are no_memory, invalid_target or system_call error.
93*; PROTO(bfd*, bfd_openr, (CONST char *filename,CONST char*target));
94*-*/
4a81b561
DHW
95
96bfd *
9846338e
SC
97DEFUN(bfd_openr, (filename, target),
98 CONST char *filename AND
99 CONST char *target)
4a81b561
DHW
100{
101 bfd *nbfd;
102 bfd_target *target_vec;
103
4a81b561
DHW
104 nbfd = new_bfd();
105 if (nbfd == NULL) {
106 bfd_error = no_memory;
107 return NULL;
108 }
109
c0e5039e
JG
110 target_vec = bfd_find_target (target, nbfd);
111 if (target_vec == NULL) {
112 bfd_error = invalid_target;
113 return NULL;
114 }
115
4a81b561 116 nbfd->filename = filename;
4a81b561
DHW
117 nbfd->direction = read_direction;
118
119 if (bfd_open_file (nbfd) == NULL) {
120 bfd_error = system_call_error; /* File didn't exist, or some such */
9872a49c 121 bfd_release(nbfd,0);
4a81b561
DHW
122 return NULL;
123 }
124 return nbfd;
125}
126
127
128/* Don't try to `optimize' this function:
129
130 o - We lock using stack space so that interrupting the locking
131 won't cause a storage leak.
132 o - We open the file stream last, since we don't want to have to
133 close it if anything goes wrong. Closing the stream means closing
134 the file descriptor too, even though we didn't open it.
135 */
6f715d66
SC
136/*proto*
137*i bfd_fdopenr
6724ff46 138bfd_fdopenr is to bfd_fopenr much like fdopen is to fopen. It opens a BFD on
6f715d66
SC
139a file already described by the @var{fd} supplied.
140
141Possible errors are no_memory, invalid_target and system_call error.
142*; PROTO(bfd *, bfd_fdopenr,
143 (CONST char *filename, CONST char *target, int fd));
144*-*/
4a81b561
DHW
145
146bfd *
9846338e
SC
147DEFUN(bfd_fdopenr,(filename, target, fd),
148 CONST char *filename AND
149 CONST char *target AND
150 int fd)
4a81b561
DHW
151{
152 bfd *nbfd;
153 bfd_target *target_vec;
154 int fdflags;
4a81b561 155
4a81b561
DHW
156 bfd_error = system_call_error;
157
fb3be09b
JG
158#ifdef NO_FCNTL
159 fdflags = O_RDWR; /* Assume full access */
160#else
6f715d66 161 fdflags = fcntl (fd, F_GETFL, NULL);
4a81b561 162#endif
fb3be09b 163 if (fdflags == -1) return NULL;
4a81b561
DHW
164
165 nbfd = new_bfd();
166
167 if (nbfd == NULL) {
168 bfd_error = no_memory;
169 return NULL;
170 }
c0e5039e
JG
171
172 target_vec = bfd_find_target (target, nbfd);
173 if (target_vec == NULL) {
174 bfd_error = invalid_target;
175 return NULL;
176 }
177
ff7ce170
PB
178#ifdef FASCIST_FDOPEN
179 nbfd->iostream = (char *) fdopen (fd, "r");
180#else
4a81b561
DHW
181 /* if the fd were open for read only, this still would not hurt: */
182 nbfd->iostream = (char *) fdopen (fd, "r+");
ff7ce170 183#endif
4a81b561 184 if (nbfd->iostream == NULL) {
fc723380 185 (void) obstack_free (&nbfd->memory, (PTR)0);
4a81b561
DHW
186 return NULL;
187 }
188
189 /* OK, put everything where it belongs */
190
191 nbfd->filename = filename;
4a81b561
DHW
192
193 /* As a special case we allow a FD open for read/write to
194 be written through, although doing so requires that we end
195 the previous clause with a preposition. */
ff7ce170
PB
196 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
197 switch (fdflags & (O_ACCMODE)) {
4a81b561
DHW
198 case O_RDONLY: nbfd->direction = read_direction; break;
199 case O_WRONLY: nbfd->direction = write_direction; break;
200 case O_RDWR: nbfd->direction = both_direction; break;
201 default: abort ();
202 }
203
c0e5039e 204 bfd_cache_init (nbfd);
4a81b561
DHW
205
206 return nbfd;
207}
208\f
209/** bfd_openw -- open for writing.
6724ff46 210 Returns a pointer to a freshly-allocated BFD on success, or NULL.
4a81b561
DHW
211
212 See comment by bfd_fdopenr before you try to modify this function. */
213
6f715d66 214/*proto* bfd_openw
6724ff46 215Creates a BFD, associated with file @var{filename}, using the file
6f715d66
SC
216format @var{target}, and returns a pointer to it.
217
218Possible errors are system_call_error, no_memory, invalid_target.
219*; PROTO(bfd *, bfd_openw, (CONST char *filename, CONST char *target));
220*/
221
4a81b561 222bfd *
9846338e
SC
223DEFUN(bfd_openw,(filename, target),
224 CONST char *filename AND
225 CONST char *target)
4a81b561
DHW
226{
227 bfd *nbfd;
228 bfd_target *target_vec;
229
4a81b561
DHW
230 bfd_error = system_call_error;
231
232 /* nbfd has to point to head of malloc'ed block so that bfd_close may
233 reclaim it correctly. */
234
235 nbfd = new_bfd();
236 if (nbfd == NULL) {
237 bfd_error = no_memory;
238 return NULL;
239 }
240
c0e5039e
JG
241 target_vec = bfd_find_target (target, nbfd);
242 if (target_vec == NULL) return NULL;
243
4a81b561 244 nbfd->filename = filename;
4a81b561
DHW
245 nbfd->direction = write_direction;
246
247 if (bfd_open_file (nbfd) == NULL) {
248 bfd_error = system_call_error; /* File not writeable, etc */
fc723380 249 (void) obstack_free (&nbfd->memory, (PTR)0);
4a81b561
DHW
250 return NULL;
251 }
252 return nbfd;
253}
6f715d66
SC
254
255/*proto* bfd_close
6724ff46 256This function closes a BFD. If the BFD was open for writing, then
6f715d66
SC
257pending operations are completed and the file written out and closed.
258If the created file is executable, then @code{chmod} is called to mark
259it as such.
260
188d6d22 261All memory attached to the BFD's obstacks is released.
6f715d66
SC
262
263@code{true} is returned if all is ok, otherwise @code{false}.
264*; PROTO(boolean, bfd_close,(bfd *));
265*/
266
4a81b561 267boolean
6f715d66
SC
268DEFUN(bfd_close,(abfd),
269 bfd *abfd)
4a81b561 270{
2b1d8a50
JG
271 if (!bfd_read_p(abfd))
272 if (BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)) != true)
273 return false;
274
4a81b561
DHW
275 if (BFD_SEND (abfd, _close_and_cleanup, (abfd)) != true) return false;
276
277 bfd_cache_close(abfd);
2b1d8a50
JG
278
279 /* If the file was open for writing and is now executable,
280 make it so */
4a81b561
DHW
281 if (abfd->direction == write_direction
282 && abfd->flags & EXEC_P) {
283 struct stat buf;
284 stat(abfd->filename, &buf);
7ed4093a
SC
285#ifndef S_IXUSR
286#define S_IXUSR 0100 /* Execute by owner. */
287#endif
288#ifndef S_IXGRP
289#define S_IXGRP 0010 /* Execute by group. */
290#endif
291#ifndef S_IXOTH
292#define S_IXOTH 0001 /* Execute by others. */
293#endif
294
4a81b561
DHW
295 chmod(abfd->filename,buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH);
296 }
fc723380 297 (void) obstack_free (&abfd->memory, (PTR)0);
ff7ce170
PB
298 (void) free(abfd);
299 return true;
300}
301
302/*proto* bfd_close_all_done
303This function closes a BFD. It differs from @code{bfd_close} since it
304does not complete any pending operations. This routine would be used
305if the application had just used BFD for swapping and didn't want to
306use any of the writing code.
307
308If the created file is executable, then @code{chmod} is called to mark
309it as such.
310
311All memory attached to the BFD's obstacks is released.
312
313@code{true} is returned if all is ok, otherwise @code{false}.
314*; PROTO(boolean, bfd_close_all_done,(bfd *));
315*/
316
317boolean
318DEFUN(bfd_close_all_done,(abfd),
319 bfd *abfd)
320{
321 bfd_cache_close(abfd);
322
323 /* If the file was open for writing and is now executable,
324 make it so */
325 if (abfd->direction == write_direction
326 && abfd->flags & EXEC_P) {
327 struct stat buf;
328 stat(abfd->filename, &buf);
329#ifndef S_IXUSR
330#define S_IXUSR 0100 /* Execute by owner. */
331#endif
332#ifndef S_IXGRP
333#define S_IXGRP 0010 /* Execute by group. */
334#endif
335#ifndef S_IXOTH
336#define S_IXOTH 0001 /* Execute by others. */
337#endif
338
339 chmod(abfd->filename,buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH);
340 }
341 (void) obstack_free (&abfd->memory, (PTR)0);
342 (void) free(abfd);
4a81b561
DHW
343 return true;
344}
fc723380 345
6f715d66 346/*proto* bfd_create
6724ff46
RP
347This routine creates a new BFD in the manner of @code{bfd_openw}, but without
348opening a file. The new BFD takes the target from the target used by
6f715d66
SC
349@var{template}. The format is always set to @code{bfd_object}.
350
351*; PROTO(bfd *, bfd_create, (CONST char *filename, bfd *template));
352*/
fc723380 353
4a81b561 354bfd *
9846338e
SC
355DEFUN(bfd_create,(filename, template),
356 CONST char *filename AND
6f715d66 357 bfd *template)
4a81b561
DHW
358{
359 bfd *nbfd = new_bfd();
360 if (nbfd == (bfd *)NULL) {
361 bfd_error = no_memory;
362 return (bfd *)NULL;
363 }
364 nbfd->filename = filename;
9872a49c
SC
365 if(template) {
366 nbfd->xvec = template->xvec;
367 }
4a81b561 368 nbfd->direction = no_direction;
9872a49c 369 bfd_set_format(nbfd, bfd_object);
4a81b561 370 return nbfd;
4a81b561 371}
9872a49c 372
fc723380
JG
373/* Memory allocation */
374
7ed4093a
SC
375DEFUN(PTR bfd_alloc_by_size_t,(abfd, size),
376 bfd *abfd AND
377 size_t size)
378{
379 PTR res = obstack_alloc(&(abfd->memory), size);
380 return res;
381}
6f715d66
SC
382
383DEFUN(void bfd_alloc_grow,(abfd, ptr, size),
384 bfd *abfd AND
385 PTR ptr AND
386 bfd_size_type size)
387{
6724ff46 388 (void) obstack_grow(&(abfd->memory), ptr, size);
6f715d66
SC
389}
390DEFUN(PTR bfd_alloc_finish,(abfd),
391 bfd *abfd)
392{
393 return obstack_finish(&(abfd->memory));
394}
395
9872a49c
SC
396DEFUN(PTR bfd_alloc, (abfd, size),
397 bfd *abfd AND
fc723380 398 bfd_size_type size)
9872a49c 399{
7ed4093a 400 return bfd_alloc_by_size_t(abfd, (size_t)size);
9872a49c
SC
401}
402
403DEFUN(PTR bfd_zalloc,(abfd, size),
404 bfd *abfd AND
fc723380 405 bfd_size_type size)
9872a49c
SC
406{
407 PTR res = bfd_alloc(abfd, size);
fc723380 408 memset(res, 0, (size_t)size);
9872a49c
SC
409 return res;
410}
411
412DEFUN(PTR bfd_realloc,(abfd, old, size),
413 bfd *abfd AND
414 PTR old AND
fc723380 415 bfd_size_type size)
9872a49c
SC
416{
417 PTR res = bfd_alloc(abfd, size);
fc723380 418 memcpy(res, old, (size_t)size);
9872a49c
SC
419 return res;
420}
421
6f715d66
SC
422/*proto* bfd_alloc_size
423Return the number of bytes in the obstacks connected to the supplied
6724ff46 424BFD.
6f715d66
SC
425*; PROTO(bfd_size_type,bfd_alloc_size,(bfd *abfd));
426*/
9872a49c 427
6f715d66
SC
428bfd_size_type
429DEFUN( bfd_alloc_size,(abfd),
9872a49c
SC
430 bfd *abfd)
431{
432 struct _obstack_chunk *chunk = abfd->memory.chunk;
433 size_t size = 0;
434 while (chunk) {
435 size += chunk->limit - &(chunk->contents[0]);
436 chunk = chunk->prev;
437 }
438 return size;
439}
This page took 0.056114 seconds and 4 git commands to generate.