* Makefile.in (diststuff): Don't make headers.
[deliverable/binutils-gdb.git] / bfd / opncls.c
CommitLineData
6724ff46 1/* opncls.c -- open and close a BFD.
b7577823 2 Copyright (C) 1990 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
6724ff46 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 18along with this program; if not, write to the Free Software
b7577823 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
4a81b561 20
4a81b561 21#include "bfd.h"
ff7ce170 22#include "sysdep.h"
4a81b561 23#include "libbfd.h"
6724ff46 24#include "obstack.h"
9560e662
SC
25
26#ifndef S_IXUSR
27#define S_IXUSR 0100 /* Execute by owner. */
28#endif
29#ifndef S_IXGRP
30#define S_IXGRP 0010 /* Execute by group. */
31#endif
32#ifndef S_IXOTH
33#define S_IXOTH 0001 /* Execute by others. */
34#endif
4a81b561
DHW
35
36/* fdopen is a loser -- we should use stdio exclusively. Unfortunately
37 if we do that we can't use fcntl. */
6724ff46 38
4a81b561 39
9783e04a 40#define obstack_chunk_alloc malloc
9872a49c
SC
41#define obstack_chunk_free free
42
fc723380
JG
43/* Return a new BFD. All BFD's are allocated through this routine. */
44
baf205c4 45bfd *
751446f6 46_bfd_new_bfd ()
4a81b561 47{
9872a49c 48 bfd *nbfd;
fc723380 49
f4bd7a8f 50 nbfd = (bfd *)bfd_zmalloc (sizeof (bfd));
b1847ba9 51 if (!nbfd)
9783e04a 52 {
9560e662 53 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
54 return 0;
55 }
4a81b561 56
9783e04a
DM
57 if (!obstack_begin(&nbfd->memory, 128))
58 {
9560e662 59 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
60 return 0;
61 }
ff7ce170
PB
62
63 nbfd->arch_info = &bfd_default_arch_struct;
64
4a81b561
DHW
65 nbfd->direction = no_direction;
66 nbfd->iostream = NULL;
67 nbfd->where = 0;
68 nbfd->sections = (asection *)NULL;
69 nbfd->format = bfd_unknown;
70 nbfd->my_archive = (bfd *)NULL;
70e00914 71 nbfd->origin = 0;
4a81b561
DHW
72 nbfd->opened_once = false;
73 nbfd->output_has_begun = false;
74 nbfd->section_count = 0;
9846338e 75 nbfd->usrdata = (PTR)NULL;
4a81b561
DHW
76 nbfd->cacheable = false;
77 nbfd->flags = NO_FLAGS;
9068cbe7
SC
78 nbfd->mtime_set = false;
79
4a81b561
DHW
80 return nbfd;
81}
fc723380
JG
82
83/* Allocate a new BFD as a member of archive OBFD. */
84
baf205c4 85bfd *
f4bd7a8f 86_bfd_new_bfd_contained_in (obfd)
baf205c4 87 bfd *obfd;
4a81b561 88{
baf205c4
JG
89 bfd *nbfd;
90
f4bd7a8f 91 nbfd = _bfd_new_bfd();
baf205c4
JG
92 nbfd->xvec = obfd->xvec;
93 nbfd->my_archive = obfd;
94 nbfd->direction = read_direction;
95 nbfd->target_defaulted = obfd->target_defaulted;
96 return nbfd;
4a81b561
DHW
97}
98
b645b632
SC
99/*
100SECTION
f4bd7a8f 101 Opening and closing BFDs
6f715d66
SC
102
103*/
6f715d66 104
b645b632
SC
105/*
106FUNCTION
107 bfd_openr
108
109SYNOPSIS
c188b0be 110 bfd *bfd_openr(CONST char *filename, CONST char *target);
b645b632
SC
111
112DESCRIPTION
c188b0be
DM
113 Open the file @var{filename} (using <<fopen>>) with the target
114 @var{target}. Return a pointer to the created BFD.
b645b632 115
c188b0be
DM
116 Calls <<bfd_find_target>>, so @var{target} is interpreted as by
117 that function.
f4bd7a8f
DM
118
119 If <<NULL>> is returned then an error has occured. Possible errors
9560e662 120 are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or <<system_call>> error.
b645b632 121*/
4a81b561
DHW
122
123bfd *
f4bd7a8f
DM
124bfd_openr (filename, target)
125 CONST char *filename;
126 CONST char *target;
4a81b561
DHW
127{
128 bfd *nbfd;
9560e662 129 const bfd_target *target_vec;
4a81b561 130
f4bd7a8f 131 nbfd = _bfd_new_bfd();
4a81b561 132 if (nbfd == NULL) {
9560e662 133 bfd_set_error (bfd_error_no_memory);
4a81b561
DHW
134 return NULL;
135 }
136
c0e5039e
JG
137 target_vec = bfd_find_target (target, nbfd);
138 if (target_vec == NULL) {
9560e662 139 bfd_set_error (bfd_error_invalid_target);
c0e5039e
JG
140 return NULL;
141 }
142
4a81b561 143 nbfd->filename = filename;
70e00914 144 nbfd->direction = read_direction;
4a81b561
DHW
145
146 if (bfd_open_file (nbfd) == NULL) {
9560e662 147 bfd_set_error (bfd_error_system_call); /* File didn't exist, or some such */
9872a49c 148 bfd_release(nbfd,0);
4a81b561
DHW
149 return NULL;
150 }
151 return nbfd;
152}
153
154
155/* Don't try to `optimize' this function:
156
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.
162 */
b645b632
SC
163/*
164FUNCTION
165 bfd_fdopenr
6f715d66 166
b645b632
SC
167SYNOPSIS
168 bfd *bfd_fdopenr(CONST char *filename, CONST char *target, int fd);
169
170DESCRIPTION
c188b0be 171 <<bfd_fdopenr>> is to <<bfd_fopenr>> much like <<fdopen>> is to <<fopen>>.
b645b632 172 It opens a BFD on a file already described by the @var{fd}
70e00914
KR
173 supplied.
174
c188b0be 175 When the file is later <<bfd_close>>d, the file descriptor will be closed.
70e00914
KR
176
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
baf205c4
JG
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
70e00914 182 assume no cacheing; the file descriptor will remain open until
c188b0be 183 <<bfd_close>>, and will not be affected by BFD operations on other
70e00914 184 files.
b645b632 185
9560e662 186 Possible errors are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> and <<bfd_error_system_call>>.
b645b632 187*/
4a81b561
DHW
188
189bfd *
f4bd7a8f
DM
190bfd_fdopenr (filename, target, fd)
191 CONST char *filename;
192 CONST char *target;
193 int fd;
4a81b561
DHW
194{
195 bfd *nbfd;
9560e662 196 const bfd_target *target_vec;
4a81b561 197 int fdflags;
4a81b561 198
9560e662 199 bfd_set_error (bfd_error_system_call);
70e00914 200
b7577823 201#if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
fb3be09b
JG
202 fdflags = O_RDWR; /* Assume full access */
203#else
6f715d66 204 fdflags = fcntl (fd, F_GETFL, NULL);
4a81b561 205#endif
fb3be09b 206 if (fdflags == -1) return NULL;
4a81b561 207
f4bd7a8f 208 nbfd = _bfd_new_bfd();
4a81b561
DHW
209
210 if (nbfd == NULL) {
9560e662 211 bfd_set_error (bfd_error_no_memory);
4a81b561
DHW
212 return NULL;
213 }
c0e5039e
JG
214
215 target_vec = bfd_find_target (target, nbfd);
216 if (target_vec == NULL) {
9560e662 217 bfd_set_error (bfd_error_invalid_target);
c0e5039e
JG
218 return NULL;
219 }
9560e662 220#if defined(VMS) || defined(__GO32__) || defined (WIN32)
70e00914 221 nbfd->iostream = (char *)fopen(filename, FOPEN_RB);
ff7ce170 222#else
70e00914
KR
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;
228 default: abort ();
229 }
ff7ce170 230#endif
4a81b561 231 if (nbfd->iostream == NULL) {
fc723380 232 (void) obstack_free (&nbfd->memory, (PTR)0);
4a81b561
DHW
233 return NULL;
234 }
70e00914 235
4a81b561
DHW
236 /* OK, put everything where it belongs */
237
238 nbfd->filename = filename;
4a81b561
DHW
239
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. */
ff7ce170
PB
243 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
244 switch (fdflags & (O_ACCMODE)) {
4a81b561 245 case O_RDONLY: nbfd->direction = read_direction; break;
70e00914 246 case O_WRONLY: nbfd->direction = write_direction; break;
4a81b561
DHW
247 case O_RDWR: nbfd->direction = both_direction; break;
248 default: abort ();
249 }
70e00914 250
9560e662
SC
251 if (! bfd_cache_init (nbfd))
252 return NULL;
4a81b561
DHW
253
254 return nbfd;
255}
123bfaa5
ILT
256
257/*
258FUNCTION
259 bfd_openstreamr
260
261SYNOPSIS
262 bfd *bfd_openstreamr();
263
264DESCRIPTION
265
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.
268*/
269
270bfd *
271bfd_openstreamr (filename, target, stream)
272 const char *filename;
273 const char *target;
274 FILE *stream;
275{
276 bfd *nbfd;
277 const bfd_target *target_vec;
278
279 nbfd = _bfd_new_bfd ();
280 if (nbfd == NULL)
281 {
282 bfd_set_error (bfd_error_no_memory);
283 return NULL;
284 }
285
286 target_vec = bfd_find_target (target, nbfd);
287 if (target_vec == NULL)
288 {
289 bfd_set_error (bfd_error_invalid_target);
290 return NULL;
291 }
292
293 nbfd->iostream = (char *) stream;
294 nbfd->filename = filename;
295 nbfd->direction = read_direction;
296
297 if (! bfd_cache_init (nbfd))
298 return NULL;
299
300 return nbfd;
301}
4a81b561
DHW
302\f
303/** bfd_openw -- open for writing.
6724ff46 304 Returns a pointer to a freshly-allocated BFD on success, or NULL.
4a81b561
DHW
305
306 See comment by bfd_fdopenr before you try to modify this function. */
307
b645b632
SC
308/*
309FUNCTION
310 bfd_openw
311
312SYNOPSIS
313 bfd *bfd_openw(CONST char *filename, CONST char *target);
6f715d66 314
b645b632 315DESCRIPTION
c188b0be
DM
316 Create a BFD, associated with file @var{filename}, using the
317 file format @var{target}, and return a pointer to it.
b645b632 318
9560e662
SC
319 Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>,
320 <<bfd_error_invalid_target>>.
6f715d66
SC
321*/
322
4a81b561 323bfd *
f4bd7a8f
DM
324bfd_openw (filename, target)
325 CONST char *filename;
326 CONST char *target;
4a81b561
DHW
327{
328 bfd *nbfd;
9560e662 329 const bfd_target *target_vec;
70e00914 330
9560e662 331 bfd_set_error (bfd_error_system_call);
4a81b561
DHW
332
333 /* nbfd has to point to head of malloc'ed block so that bfd_close may
334 reclaim it correctly. */
335
f4bd7a8f 336 nbfd = _bfd_new_bfd();
4a81b561 337 if (nbfd == NULL) {
9560e662 338 bfd_set_error (bfd_error_no_memory);
4a81b561
DHW
339 return NULL;
340 }
341
c0e5039e
JG
342 target_vec = bfd_find_target (target, nbfd);
343 if (target_vec == NULL) return NULL;
344
4a81b561 345 nbfd->filename = filename;
4a81b561
DHW
346 nbfd->direction = write_direction;
347
348 if (bfd_open_file (nbfd) == NULL) {
9560e662 349 bfd_set_error (bfd_error_system_call); /* File not writeable, etc */
fc723380 350 (void) obstack_free (&nbfd->memory, (PTR)0);
4a81b561
DHW
351 return NULL;
352 }
353 return nbfd;
354}
6f715d66 355
b645b632
SC
356/*
357
358FUNCTION
359 bfd_close
360
361SYNOPSIS
c188b0be 362 boolean bfd_close(bfd *abfd);
b645b632
SC
363
364DESCRIPTION
6f715d66 365
c188b0be 366 Close a BFD. If the BFD was open for writing,
b645b632
SC
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.
6f715d66 370
70e00914
KR
371 All memory attached to the BFD's obstacks is released.
372
373 The file descriptor associated with the BFD is closed (even
c188b0be 374 if it was passed in to BFD by <<bfd_fdopenr>>).
b645b632
SC
375
376RETURNS
377 <<true>> is returned if all is ok, otherwise <<false>>.
6f715d66
SC
378*/
379
b645b632 380
4a81b561 381boolean
f4bd7a8f
DM
382bfd_close (abfd)
383 bfd *abfd;
4a81b561 384{
70e00914
KR
385 boolean ret;
386
9560e662
SC
387 if (!bfd_read_p (abfd))
388 {
389 if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
390 return false;
391 }
2b1d8a50 392
9560e662
SC
393 if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
394 return false;
4a81b561 395
9560e662 396 ret = bfd_cache_close (abfd);
2b1d8a50
JG
397
398 /* If the file was open for writing and is now executable,
399 make it so */
9560e662 400 if (ret
70e00914 401 && abfd->direction == write_direction
9560e662
SC
402 && abfd->flags & EXEC_P)
403 {
404 struct stat buf;
405
406 if (stat (abfd->filename, &buf) == 0)
407 {
408 int mask = umask (0);
409 umask (mask);
410 chmod (abfd->filename,
411 (0777
412 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
413 }
414 }
7ed4093a 415
fc723380 416 (void) obstack_free (&abfd->memory, (PTR)0);
9560e662
SC
417 (void) free (abfd);
418
70e00914 419 return ret;
ff7ce170
PB
420}
421
b645b632
SC
422/*
423FUNCTION
424 bfd_close_all_done
425
426SYNOPSIS
427 boolean bfd_close_all_done(bfd *);
428
429DESCRIPTION
c188b0be 430 Close a BFD. Differs from <<bfd_close>>
b645b632
SC
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.
ff7ce170 434
b645b632
SC
435 If the created file is executable, then <<chmod>> is called
436 to mark it as such.
ff7ce170 437
70e00914 438 All memory attached to the BFD's obstacks is released.
b645b632
SC
439
440RETURNS
441 <<true>> is returned if all is ok, otherwise <<false>>.
ff7ce170 442
ff7ce170
PB
443*/
444
445boolean
f4bd7a8f
DM
446bfd_close_all_done (abfd)
447 bfd *abfd;
ff7ce170 448{
70e00914
KR
449 boolean ret;
450
9560e662 451 ret = bfd_cache_close (abfd);
ff7ce170
PB
452
453 /* If the file was open for writing and is now executable,
454 make it so */
9560e662 455 if (ret
70e00914 456 && abfd->direction == write_direction
9560e662
SC
457 && abfd->flags & EXEC_P)
458 {
459 struct stat buf;
460
461 if (stat (abfd->filename, &buf) == 0)
462 {
463 int mask = umask (0);
464 umask (mask);
465 chmod (abfd->filename,
466 (0x777
467 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
468 }
469 }
ff7ce170
PB
470 (void) obstack_free (&abfd->memory, (PTR)0);
471 (void) free(abfd);
70e00914 472 return ret;
4a81b561 473}
fc723380 474
6f715d66 475
b645b632
SC
476/*
477FUNCTION
478 bfd_alloc_size
479
480SYNOPSIS
481 bfd_size_type bfd_alloc_size(bfd *abfd);
482
483DESCRIPTION
c188b0be 484 Return the number of bytes in the obstacks connected to @var{abfd}.
b645b632
SC
485
486*/
487
488bfd_size_type
f4bd7a8f
DM
489bfd_alloc_size (abfd)
490 bfd *abfd;
b645b632
SC
491{
492 struct _obstack_chunk *chunk = abfd->memory.chunk;
493 size_t size = 0;
494 while (chunk) {
495 size += chunk->limit - &(chunk->contents[0]);
496 chunk = chunk->prev;
497 }
498 return size;
499}
500
501
502
503/*
504FUNCTION
505 bfd_create
506
507SYNOPSIS
baf205c4 508 bfd *bfd_create(CONST char *filename, bfd *templ);
b645b632
SC
509
510DESCRIPTION
c188b0be 511 Create a new BFD in the manner of
b645b632
SC
512 <<bfd_openw>>, but without opening a file. The new BFD
513 takes the target from the target used by @var{template}. The
70e00914 514 format is always set to <<bfd_object>>.
b645b632 515
6f715d66 516*/
fc723380 517
4a81b561 518bfd *
f4bd7a8f
DM
519bfd_create (filename, templ)
520 CONST char *filename;
521 bfd *templ;
4a81b561 522{
f4bd7a8f 523 bfd *nbfd = _bfd_new_bfd();
4a81b561 524 if (nbfd == (bfd *)NULL) {
9560e662 525 bfd_set_error (bfd_error_no_memory);
4a81b561
DHW
526 return (bfd *)NULL;
527 }
528 nbfd->filename = filename;
baf205c4
JG
529 if(templ) {
530 nbfd->xvec = templ->xvec;
9872a49c 531 }
4a81b561 532 nbfd->direction = no_direction;
9872a49c 533 bfd_set_format(nbfd, bfd_object);
4a81b561 534 return nbfd;
4a81b561 535}
9872a49c 536
70e00914 537/*
b645b632
SC
538INTERNAL_FUNCTION
539 bfd_alloc_by_size_t
540
541SYNOPSIS
542 PTR bfd_alloc_by_size_t(bfd *abfd, size_t wanted);
fc723380 543
b645b632 544DESCRIPTION
c188b0be
DM
545 Allocate a block of @var{wanted} bytes of memory in the obstack
546 attatched to <<abfd>> and return a pointer to it.
b645b632
SC
547*/
548
549
70e00914 550PTR
f4bd7a8f
DM
551bfd_alloc_by_size_t (abfd, size)
552 bfd *abfd;
553 size_t size;
7ed4093a 554{
9783e04a 555 return obstack_alloc(&(abfd->memory), size);
7ed4093a 556}
6f715d66 557
f4bd7a8f
DM
558void
559bfd_alloc_grow (abfd, ptr, size)
560 bfd *abfd;
561 PTR ptr;
562 size_t size;
6f715d66 563{
70e00914 564 (void) obstack_grow(&(abfd->memory), ptr, size);
6f715d66 565}
f4bd7a8f
DM
566
567PTR
568bfd_alloc_finish (abfd)
569 bfd *abfd;
6f715d66
SC
570{
571 return obstack_finish(&(abfd->memory));
572}
573
f4bd7a8f
DM
574PTR
575bfd_alloc (abfd, size)
576 bfd *abfd;
577 size_t size;
9872a49c 578{
7ed4093a 579 return bfd_alloc_by_size_t(abfd, (size_t)size);
9872a49c
SC
580}
581
f4bd7a8f
DM
582PTR
583bfd_zalloc (abfd, size)
584 bfd *abfd;
585 size_t size;
9872a49c 586{
70e00914
KR
587 PTR res;
588 res = bfd_alloc(abfd, size);
9783e04a
DM
589 if (res)
590 memset(res, 0, (size_t)size);
9872a49c
SC
591 return res;
592}
This page took 0.185448 seconds and 4 git commands to generate.