Merge from vendor branch OPENSSH:
[dragonfly.git] / contrib / binutils / bfd / opncls.c
1 /* opncls.c -- open and close a BFD.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
3    2001, 2002
4    Free Software Foundation, Inc.
5
6    Written by Cygnus Support.
7
8 This file is part of BFD, the Binary File Descriptor library.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
23
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "objalloc.h"
27 #include "libbfd.h"
28
29 #ifndef S_IXUSR
30 #define S_IXUSR 0100    /* Execute by owner.  */
31 #endif
32 #ifndef S_IXGRP
33 #define S_IXGRP 0010    /* Execute by group.  */
34 #endif
35 #ifndef S_IXOTH
36 #define S_IXOTH 0001    /* Execute by others.  */
37 #endif
38
39 /* fdopen is a loser -- we should use stdio exclusively.  Unfortunately
40    if we do that we can't use fcntl.  */
41
42 /* FIXME: This is no longer used.  */
43 long _bfd_chunksize = -1;
44
45 /* Return a new BFD.  All BFD's are allocated through this routine.  */
46
47 bfd *
48 _bfd_new_bfd ()
49 {
50   bfd *nbfd;
51
52   nbfd = (bfd *) bfd_zmalloc ((bfd_size_type) sizeof (bfd));
53   if (nbfd == NULL)
54     return NULL;
55
56   nbfd->memory = (PTR) objalloc_create ();
57   if (nbfd->memory == NULL)
58     {
59       bfd_set_error (bfd_error_no_memory);
60       free (nbfd);
61       return NULL;
62     }
63
64   nbfd->arch_info = &bfd_default_arch_struct;
65
66   nbfd->direction = no_direction;
67   nbfd->iostream = NULL;
68   nbfd->where = 0;
69   if (!bfd_hash_table_init (&nbfd->section_htab, bfd_section_hash_newfunc))
70     {
71       free (nbfd);
72       return NULL;
73     }
74   nbfd->sections = (asection *) NULL;
75   nbfd->section_tail = &nbfd->sections;
76   nbfd->format = bfd_unknown;
77   nbfd->my_archive = (bfd *) NULL;
78   nbfd->origin = 0;
79   nbfd->opened_once = false;
80   nbfd->output_has_begun = false;
81   nbfd->section_count = 0;
82   nbfd->usrdata = (PTR) NULL;
83   nbfd->cacheable = false;
84   nbfd->flags = BFD_NO_FLAGS;
85   nbfd->mtime_set = false;
86
87   return nbfd;
88 }
89
90 /* Allocate a new BFD as a member of archive OBFD.  */
91
92 bfd *
93 _bfd_new_bfd_contained_in (obfd)
94      bfd *obfd;
95 {
96   bfd *nbfd;
97
98   nbfd = _bfd_new_bfd ();
99   if (nbfd == NULL)
100     return NULL;
101   nbfd->xvec = obfd->xvec;
102   nbfd->my_archive = obfd;
103   nbfd->direction = read_direction;
104   nbfd->target_defaulted = obfd->target_defaulted;
105   return nbfd;
106 }
107
108 /* Delete a BFD.  */
109
110 void
111 _bfd_delete_bfd (abfd)
112      bfd *abfd;
113 {
114   bfd_hash_table_free (&abfd->section_htab);
115   objalloc_free ((struct objalloc *) abfd->memory);
116   free (abfd);
117 }
118
119 /*
120 SECTION
121         Opening and closing BFDs
122
123 */
124
125 /*
126 FUNCTION
127         bfd_openr
128
129 SYNOPSIS
130         bfd *bfd_openr(const char *filename, const char *target);
131
132 DESCRIPTION
133         Open the file @var{filename} (using <<fopen>>) with the target
134         @var{target}.  Return a pointer to the created BFD.
135
136         Calls <<bfd_find_target>>, so @var{target} is interpreted as by
137         that function.
138
139         If <<NULL>> is returned then an error has occured.   Possible errors
140         are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or <<system_call>> error.
141 */
142
143 bfd *
144 bfd_openr (filename, target)
145      const char *filename;
146      const char *target;
147 {
148   bfd *nbfd;
149   const bfd_target *target_vec;
150
151   nbfd = _bfd_new_bfd ();
152   if (nbfd == NULL)
153     return NULL;
154
155   target_vec = bfd_find_target (target, nbfd);
156   if (target_vec == NULL)
157     {
158       bfd_set_error (bfd_error_invalid_target);
159       _bfd_delete_bfd (nbfd);
160       return NULL;
161     }
162
163   nbfd->filename = filename;
164   nbfd->direction = read_direction;
165
166   if (bfd_open_file (nbfd) == NULL)
167     {
168       /* File didn't exist, or some such */
169       bfd_set_error (bfd_error_system_call);
170       _bfd_delete_bfd (nbfd);
171       return NULL;
172     }
173
174   return nbfd;
175 }
176
177 /* Don't try to `optimize' this function:
178
179    o - We lock using stack space so that interrupting the locking
180        won't cause a storage leak.
181    o - We open the file stream last, since we don't want to have to
182        close it if anything goes wrong.  Closing the stream means closing
183        the file descriptor too, even though we didn't open it.
184  */
185 /*
186 FUNCTION
187          bfd_fdopenr
188
189 SYNOPSIS
190          bfd *bfd_fdopenr(const char *filename, const char *target, int fd);
191
192 DESCRIPTION
193          <<bfd_fdopenr>> is to <<bfd_fopenr>> much like <<fdopen>> is to <<fopen>>.
194          It opens a BFD on a file already described by the @var{fd}
195          supplied.
196
197          When the file is later <<bfd_close>>d, the file descriptor will be closed.
198
199          If the caller desires that this file descriptor be cached by BFD
200          (opened as needed, closed as needed to free descriptors for
201          other opens), with the supplied @var{fd} used as an initial
202          file descriptor (but subject to closure at any time), call
203          bfd_set_cacheable(bfd, 1) on the returned BFD.  The default is to
204          assume no cacheing; the file descriptor will remain open until
205          <<bfd_close>>, and will not be affected by BFD operations on other
206          files.
207
208          Possible errors are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> and <<bfd_error_system_call>>.
209 */
210
211 bfd *
212 bfd_fdopenr (filename, target, fd)
213      const char *filename;
214      const char *target;
215      int fd;
216 {
217   bfd *nbfd;
218   const bfd_target *target_vec;
219   int fdflags;
220
221   bfd_set_error (bfd_error_system_call);
222 #if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
223   fdflags = O_RDWR;                     /* Assume full access */
224 #else
225   fdflags = fcntl (fd, F_GETFL, NULL);
226 #endif
227   if (fdflags == -1) return NULL;
228
229   nbfd = _bfd_new_bfd ();
230   if (nbfd == NULL)
231     return NULL;
232
233   target_vec = bfd_find_target (target, nbfd);
234   if (target_vec == NULL)
235     {
236       bfd_set_error (bfd_error_invalid_target);
237       _bfd_delete_bfd (nbfd);
238       return NULL;
239     }
240
241 #ifndef HAVE_FDOPEN
242   nbfd->iostream = (PTR) fopen (filename, FOPEN_RB);
243 #else
244   /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
245   switch (fdflags & (O_ACCMODE))
246     {
247     case O_RDONLY: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RB);   break;
248     case O_WRONLY: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RUB);  break;
249     case O_RDWR:   nbfd->iostream = (PTR) fdopen (fd, FOPEN_RUB);  break;
250     default: abort ();
251     }
252 #endif
253
254   if (nbfd->iostream == NULL)
255     {
256       _bfd_delete_bfd (nbfd);
257       return NULL;
258     }
259
260   /* OK, put everything where it belongs */
261
262   nbfd->filename = filename;
263
264   /* As a special case we allow a FD open for read/write to
265      be written through, although doing so requires that we end
266      the previous clause with a preposition.  */
267   /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
268   switch (fdflags & (O_ACCMODE))
269     {
270     case O_RDONLY: nbfd->direction = read_direction; break;
271     case O_WRONLY: nbfd->direction = write_direction; break;
272     case O_RDWR: nbfd->direction = both_direction; break;
273     default: abort ();
274     }
275
276   if (! bfd_cache_init (nbfd))
277     {
278       _bfd_delete_bfd (nbfd);
279       return NULL;
280     }
281   nbfd->opened_once = true;
282
283   return nbfd;
284 }
285
286 /*
287 FUNCTION
288         bfd_openstreamr
289
290 SYNOPSIS
291         bfd *bfd_openstreamr(const char *, const char *, PTR);
292
293 DESCRIPTION
294
295         Open a BFD for read access on an existing stdio stream.  When
296         the BFD is passed to <<bfd_close>>, the stream will be closed.
297 */
298
299 bfd *
300 bfd_openstreamr (filename, target, streamarg)
301      const char *filename;
302      const char *target;
303      PTR streamarg;
304 {
305   FILE *stream = (FILE *) streamarg;
306   bfd *nbfd;
307   const bfd_target *target_vec;
308
309   nbfd = _bfd_new_bfd ();
310   if (nbfd == NULL)
311     return NULL;
312
313   target_vec = bfd_find_target (target, nbfd);
314   if (target_vec == NULL)
315     {
316       bfd_set_error (bfd_error_invalid_target);
317       _bfd_delete_bfd (nbfd);
318       return NULL;
319     }
320
321   nbfd->iostream = (PTR) stream;
322   nbfd->filename = filename;
323   nbfd->direction = read_direction;
324
325   if (! bfd_cache_init (nbfd))
326     {
327       _bfd_delete_bfd (nbfd);
328       return NULL;
329     }
330
331   return nbfd;
332 }
333 \f
334 /** bfd_openw -- open for writing.
335   Returns a pointer to a freshly-allocated BFD on success, or NULL.
336
337   See comment by bfd_fdopenr before you try to modify this function. */
338
339 /*
340 FUNCTION
341         bfd_openw
342
343 SYNOPSIS
344         bfd *bfd_openw(const char *filename, const char *target);
345
346 DESCRIPTION
347         Create a BFD, associated with file @var{filename}, using the
348         file format @var{target}, and return a pointer to it.
349
350         Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>,
351         <<bfd_error_invalid_target>>.
352 */
353
354 bfd *
355 bfd_openw (filename, target)
356      const char *filename;
357      const char *target;
358 {
359   bfd *nbfd;
360   const bfd_target *target_vec;
361
362   bfd_set_error (bfd_error_system_call);
363
364   /* nbfd has to point to head of malloc'ed block so that bfd_close may
365      reclaim it correctly. */
366
367   nbfd = _bfd_new_bfd ();
368   if (nbfd == NULL)
369     return NULL;
370
371   target_vec = bfd_find_target (target, nbfd);
372   if (target_vec == NULL)
373     {
374       _bfd_delete_bfd (nbfd);
375       return NULL;
376     }
377
378   nbfd->filename = filename;
379   nbfd->direction = write_direction;
380
381   if (bfd_open_file (nbfd) == NULL)
382     {
383       bfd_set_error (bfd_error_system_call);    /* File not writeable, etc */
384       _bfd_delete_bfd (nbfd);
385       return NULL;
386   }
387
388   return nbfd;
389 }
390
391 /*
392
393 FUNCTION
394         bfd_close
395
396 SYNOPSIS
397         boolean bfd_close(bfd *abfd);
398
399 DESCRIPTION
400
401         Close a BFD. If the BFD was open for writing,
402         then pending operations are completed and the file written out
403         and closed. If the created file is executable, then
404         <<chmod>> is called to mark it as such.
405
406         All memory attached to the BFD is released.
407
408         The file descriptor associated with the BFD is closed (even
409         if it was passed in to BFD by <<bfd_fdopenr>>).
410
411 RETURNS
412         <<true>> is returned if all is ok, otherwise <<false>>.
413 */
414
415
416 boolean
417 bfd_close (abfd)
418      bfd *abfd;
419 {
420   boolean ret;
421
422   if (!bfd_read_p (abfd))
423     {
424       if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
425         return false;
426     }
427
428   if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
429     return false;
430
431   ret = bfd_cache_close (abfd);
432
433   /* If the file was open for writing and is now executable,
434      make it so */
435   if (ret
436       && abfd->direction == write_direction
437       && abfd->flags & EXEC_P)
438     {
439       struct stat buf;
440
441       if (stat (abfd->filename, &buf) == 0)
442         {
443           unsigned int mask = umask (0);
444           umask (mask);
445           chmod (abfd->filename,
446                  (0777
447                   & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
448         }
449     }
450
451   _bfd_delete_bfd (abfd);
452
453   return ret;
454 }
455
456 /*
457 FUNCTION
458         bfd_close_all_done
459
460 SYNOPSIS
461         boolean bfd_close_all_done(bfd *);
462
463 DESCRIPTION
464         Close a BFD.  Differs from <<bfd_close>>
465         since it does not complete any pending operations.  This
466         routine would be used if the application had just used BFD for
467         swapping and didn't want to use any of the writing code.
468
469         If the created file is executable, then <<chmod>> is called
470         to mark it as such.
471
472         All memory attached to the BFD is released.
473
474 RETURNS
475         <<true>> is returned if all is ok, otherwise <<false>>.
476
477 */
478
479 boolean
480 bfd_close_all_done (abfd)
481      bfd *abfd;
482 {
483   boolean ret;
484
485   ret = bfd_cache_close (abfd);
486
487   /* If the file was open for writing and is now executable,
488      make it so */
489   if (ret
490       && abfd->direction == write_direction
491       && abfd->flags & EXEC_P)
492     {
493       struct stat buf;
494
495       if (stat (abfd->filename, &buf) == 0)
496         {
497           unsigned int mask = umask (0);
498           umask (mask);
499           chmod (abfd->filename,
500                  (0777
501                   & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
502         }
503     }
504
505   _bfd_delete_bfd (abfd);
506
507   return ret;
508 }
509
510 /*
511 FUNCTION
512         bfd_create
513
514 SYNOPSIS
515         bfd *bfd_create(const char *filename, bfd *templ);
516
517 DESCRIPTION
518         Create a new BFD in the manner of
519         <<bfd_openw>>, but without opening a file. The new BFD
520         takes the target from the target used by @var{template}. The
521         format is always set to <<bfd_object>>.
522
523 */
524
525 bfd *
526 bfd_create (filename, templ)
527      const char *filename;
528      bfd *templ;
529 {
530   bfd *nbfd;
531
532   nbfd = _bfd_new_bfd ();
533   if (nbfd == NULL)
534     return NULL;
535   nbfd->filename = filename;
536   if (templ)
537     nbfd->xvec = templ->xvec;
538   nbfd->direction = no_direction;
539   bfd_set_format (nbfd, bfd_object);
540   return nbfd;
541 }
542
543 /*
544 FUNCTION
545         bfd_make_writable
546
547 SYNOPSIS
548         boolean bfd_make_writable(bfd *abfd);
549
550 DESCRIPTION
551         Takes a BFD as created by <<bfd_create>> and converts it
552         into one like as returned by <<bfd_openw>>.  It does this
553         by converting the BFD to BFD_IN_MEMORY.  It's assumed that
554         you will call <<bfd_make_readable>> on this bfd later.
555
556 RETURNS
557         <<true>> is returned if all is ok, otherwise <<false>>.
558 */
559
560 boolean
561 bfd_make_writable(abfd)
562      bfd *abfd;
563 {
564   struct bfd_in_memory *bim;
565
566   if (abfd->direction != no_direction)
567     {
568       bfd_set_error (bfd_error_invalid_operation);
569       return false;
570     }
571
572   bim = ((struct bfd_in_memory *)
573          bfd_malloc ((bfd_size_type) sizeof (struct bfd_in_memory)));
574   abfd->iostream = (PTR) bim;
575   /* bfd_bwrite will grow these as needed */
576   bim->size = 0;
577   bim->buffer = 0;
578
579   abfd->flags |= BFD_IN_MEMORY;
580   abfd->direction = write_direction;
581   abfd->where = 0;
582
583   return true;
584 }
585
586 /*
587 FUNCTION
588         bfd_make_readable
589
590 SYNOPSIS
591         boolean bfd_make_readable(bfd *abfd);
592
593 DESCRIPTION
594         Takes a BFD as created by <<bfd_create>> and
595         <<bfd_make_writable>> and converts it into one like as
596         returned by <<bfd_openr>>.  It does this by writing the
597         contents out to the memory buffer, then reversing the
598         direction.
599
600 RETURNS
601         <<true>> is returned if all is ok, otherwise <<false>>.  */
602
603 boolean
604 bfd_make_readable(abfd)
605      bfd *abfd;
606 {
607   if (abfd->direction != write_direction || !(abfd->flags & BFD_IN_MEMORY))
608     {
609       bfd_set_error (bfd_error_invalid_operation);
610       return false;
611     }
612
613   if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
614     return false;
615
616   if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
617     return false;
618
619
620   abfd->arch_info = &bfd_default_arch_struct;
621
622   abfd->where = 0;
623   abfd->format = bfd_unknown;
624   abfd->my_archive = (bfd *) NULL;
625   abfd->origin = 0;
626   abfd->opened_once = false;
627   abfd->output_has_begun = false;
628   abfd->section_count = 0;
629   abfd->usrdata = (PTR) NULL;
630   abfd->cacheable = false;
631   abfd->flags = BFD_IN_MEMORY;
632   abfd->mtime_set = false;
633
634   abfd->target_defaulted = true;
635   abfd->direction = read_direction;
636   abfd->sections = 0;
637   abfd->symcount = 0;
638   abfd->outsymbols = 0;
639   abfd->tdata.any = 0;
640
641   bfd_section_list_clear (abfd);
642   bfd_check_format(abfd, bfd_object);
643
644   return true;
645 }
646
647 /*
648 INTERNAL_FUNCTION
649         bfd_alloc
650
651 SYNOPSIS
652         PTR bfd_alloc (bfd *abfd, size_t wanted);
653
654 DESCRIPTION
655         Allocate a block of @var{wanted} bytes of memory attached to
656         <<abfd>> and return a pointer to it.
657 */
658
659
660 PTR
661 bfd_alloc (abfd, size)
662      bfd *abfd;
663      bfd_size_type size;
664 {
665   PTR ret;
666
667   if (size != (unsigned long) size)
668     {
669       bfd_set_error (bfd_error_no_memory);
670       return NULL;
671     }
672
673   ret = objalloc_alloc (abfd->memory, (unsigned long) size);
674   if (ret == NULL)
675     bfd_set_error (bfd_error_no_memory);
676   return ret;
677 }
678
679 PTR
680 bfd_zalloc (abfd, size)
681      bfd *abfd;
682      bfd_size_type size;
683 {
684   PTR res;
685
686   res = bfd_alloc (abfd, size);
687   if (res)
688     memset (res, 0, (size_t) size);
689   return res;
690 }
691
692 /* Free a block allocated for a BFD.
693    Note:  Also frees all more recently allocated blocks!  */
694
695 void
696 bfd_release (abfd, block)
697      bfd *abfd;
698      PTR block;
699 {
700   objalloc_free_block ((struct objalloc *) abfd->memory, block);
701 }