home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / bfd / opncls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-20  |  12.3 KB  |  549 lines

  1. /* opncls.c -- open and close a BFD.
  2.    Copyright (C) 1990 91, 92, 93, 94 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  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.
  11.  
  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.
  16.  
  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.  */
  20.  
  21. #include "bfd.h"
  22. #include "sysdep.h"
  23. #include "libbfd.h"
  24. #include "obstack.h"
  25.  
  26. /* fdopen is a loser -- we should use stdio exclusively.  Unfortunately
  27.    if we do that we can't use fcntl.  */
  28.  
  29.  
  30. #define obstack_chunk_alloc malloc
  31. #define obstack_chunk_free free
  32.  
  33. /* Return a new BFD.  All BFD's are allocated through this routine.  */
  34.  
  35. bfd *
  36. _bfd_new_bfd ()
  37. {
  38.   bfd *nbfd;
  39.  
  40.   nbfd = (bfd *)bfd_zmalloc (sizeof (bfd));
  41.   if (!nbfd)
  42.     {
  43.       bfd_set_error (bfd_error_no_memory);
  44.       return 0;
  45.     }
  46.  
  47.   bfd_check_init();
  48.   if (!obstack_begin(&nbfd->memory, 128))
  49.     {
  50.       bfd_set_error (bfd_error_no_memory);
  51.       return 0;
  52.     }
  53.  
  54.   nbfd->arch_info = &bfd_default_arch_struct;
  55.  
  56.   nbfd->direction = no_direction;
  57.   nbfd->iostream = NULL;
  58.   nbfd->where = 0;
  59.   nbfd->sections = (asection *)NULL;
  60.   nbfd->format = bfd_unknown;
  61.   nbfd->my_archive = (bfd *)NULL;
  62.   nbfd->origin = 0;                
  63.   nbfd->opened_once = false;
  64.   nbfd->output_has_begun = false;
  65.   nbfd->section_count = 0;
  66.   nbfd->usrdata = (PTR)NULL;
  67.   nbfd->cacheable = false;
  68.   nbfd->flags = NO_FLAGS;
  69.   nbfd->mtime_set = false;
  70.  
  71.   return nbfd;
  72. }
  73.  
  74. /* Allocate a new BFD as a member of archive OBFD.  */
  75.  
  76. bfd *
  77. _bfd_new_bfd_contained_in (obfd)
  78.      bfd *obfd;
  79. {
  80.   bfd *nbfd;
  81.  
  82.   nbfd = _bfd_new_bfd();
  83.   nbfd->xvec = obfd->xvec;
  84.   nbfd->my_archive = obfd;
  85.   nbfd->direction = read_direction;
  86.   nbfd->target_defaulted = obfd->target_defaulted;
  87.   return nbfd;
  88. }
  89.  
  90. /*
  91. SECTION
  92.     Opening and closing BFDs
  93.  
  94. */
  95.  
  96. /*
  97. FUNCTION
  98.     bfd_openr
  99.  
  100. SYNOPSIS
  101.         bfd *bfd_openr(CONST char *filename, CONST char *target);
  102.  
  103. DESCRIPTION
  104.     Open the file @var{filename} (using <<fopen>>) with the target
  105.     @var{target}.  Return a pointer to the created BFD.
  106.  
  107.     Calls <<bfd_find_target>>, so @var{target} is interpreted as by
  108.     that function.
  109.  
  110.     If <<NULL>> is returned then an error has occured.   Possible errors
  111.     are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or <<system_call>> error.
  112. */
  113.  
  114. bfd *
  115. bfd_openr (filename, target)
  116.      CONST char *filename;
  117.      CONST char *target;
  118. {
  119.   bfd *nbfd;
  120.   const bfd_target *target_vec;
  121.  
  122.   nbfd = _bfd_new_bfd();
  123.   if (nbfd == NULL) {
  124.     bfd_set_error (bfd_error_no_memory);
  125.     return NULL;
  126.   }
  127.  
  128.   target_vec = bfd_find_target (target, nbfd);
  129.   if (target_vec == NULL) {
  130.     bfd_set_error (bfd_error_invalid_target);
  131.     return NULL;
  132.   }
  133.  
  134.   nbfd->filename = filename;
  135.   nbfd->direction = read_direction;
  136.  
  137.   if (bfd_open_file (nbfd) == NULL) {
  138.     bfd_set_error (bfd_error_system_call);    /* File didn't exist, or some such */
  139.     bfd_release(nbfd,0);
  140.     return NULL;
  141.   }
  142.   return nbfd;
  143. }
  144.  
  145.  
  146. /* Don't try to `optimize' this function:
  147.  
  148.    o - We lock using stack space so that interrupting the locking
  149.        won't cause a storage leak.
  150.    o - We open the file stream last, since we don't want to have to
  151.        close it if anything goes wrong.  Closing the stream means closing
  152.        the file descriptor too, even though we didn't open it.
  153.  */
  154. /*
  155. FUNCTION
  156.          bfd_fdopenr
  157.  
  158. SYNOPSIS
  159.          bfd *bfd_fdopenr(CONST char *filename, CONST char *target, int fd);
  160.  
  161. DESCRIPTION
  162.          <<bfd_fdopenr>> is to <<bfd_fopenr>> much like <<fdopen>> is to <<fopen>>.
  163.      It opens a BFD on a file already described by the @var{fd}
  164.      supplied.
  165.  
  166.      When the file is later <<bfd_close>>d, the file descriptor will be closed.
  167.  
  168.      If the caller desires that this file descriptor be cached by BFD
  169.      (opened as needed, closed as needed to free descriptors for
  170.      other opens), with the supplied @var{fd} used as an initial
  171.      file descriptor (but subject to closure at any time), call
  172.      bfd_set_cacheable(bfd, 1) on the returned BFD.  The default is to
  173.      assume no cacheing; the file descriptor will remain open until
  174.      <<bfd_close>>, and will not be affected by BFD operations on other
  175.      files.
  176.  
  177.          Possible errors are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> and <<bfd_error_system_call>>.
  178. */
  179.  
  180. bfd *
  181. bfd_fdopenr (filename, target, fd)
  182.      CONST char *filename;
  183.      CONST char *target;
  184.      int fd;
  185. {
  186.   bfd *nbfd;
  187.   const bfd_target *target_vec;
  188.   int fdflags;
  189.  
  190.   bfd_set_error (bfd_error_system_call);
  191.  
  192. #ifdef NO_FCNTL
  193.   fdflags = O_RDWR;            /* Assume full access */
  194. #else
  195.   fdflags = fcntl (fd, F_GETFL, NULL);
  196. #endif
  197.   if (fdflags == -1) return NULL;
  198.  
  199.   nbfd = _bfd_new_bfd();
  200.  
  201.   if (nbfd == NULL) {
  202.     bfd_set_error (bfd_error_no_memory);
  203.     return NULL;
  204.   }
  205.  
  206.   target_vec = bfd_find_target (target, nbfd);
  207.   if (target_vec == NULL) {
  208.     bfd_set_error (bfd_error_invalid_target);
  209.     return NULL;
  210.   }
  211. #if defined(VMS) || defined(__GO32__)
  212.   nbfd->iostream = (char *)fopen(filename, FOPEN_RB);
  213. #else
  214.   /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
  215.   switch (fdflags & (O_ACCMODE)) {
  216.   case O_RDONLY: nbfd->iostream = (char *) fdopen (fd, FOPEN_RB);   break;
  217.   case O_WRONLY: nbfd->iostream = (char *) fdopen (fd, FOPEN_RUB);  break;
  218.   case O_RDWR:   nbfd->iostream = (char *) fdopen (fd, FOPEN_RUB);  break;
  219.   default: abort ();
  220.   }
  221. #endif
  222.   if (nbfd->iostream == NULL) {
  223.     (void) obstack_free (&nbfd->memory, (PTR)0);
  224.     return NULL;
  225.   }
  226.  
  227.   /* OK, put everything where it belongs */
  228.  
  229.   nbfd->filename = filename;
  230.  
  231.   /* As a special case we allow a FD open for read/write to
  232.      be written through, although doing so requires that we end
  233.      the previous clause with a preposition.  */
  234.   /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
  235.   switch (fdflags & (O_ACCMODE)) {
  236.   case O_RDONLY: nbfd->direction = read_direction; break;
  237.   case O_WRONLY: nbfd->direction = write_direction; break;
  238.   case O_RDWR: nbfd->direction = both_direction; break;
  239.   default: abort ();
  240.   }
  241.                 
  242.   if (! bfd_cache_init (nbfd))
  243.     return NULL;
  244.  
  245.   return nbfd;
  246. }
  247.  
  248. /** bfd_openw -- open for writing.
  249.   Returns a pointer to a freshly-allocated BFD on success, or NULL.
  250.  
  251.   See comment by bfd_fdopenr before you try to modify this function. */
  252.  
  253. /*
  254. FUNCTION
  255.     bfd_openw
  256.  
  257. SYNOPSIS
  258.     bfd *bfd_openw(CONST char *filename, CONST char *target);
  259.  
  260. DESCRIPTION
  261.     Create a BFD, associated with file @var{filename}, using the
  262.     file format @var{target}, and return a pointer to it.
  263.  
  264.     Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>,
  265.     <<bfd_error_invalid_target>>.
  266. */
  267.  
  268. bfd *
  269. bfd_openw (filename, target)
  270.      CONST char *filename;
  271.      CONST char *target;
  272. {
  273.   bfd *nbfd;
  274.   const bfd_target *target_vec;
  275.  
  276.   bfd_set_error (bfd_error_system_call);
  277.  
  278.   /* nbfd has to point to head of malloc'ed block so that bfd_close may
  279.      reclaim it correctly. */
  280.  
  281.   nbfd = _bfd_new_bfd();
  282.   if (nbfd == NULL) {
  283.     bfd_set_error (bfd_error_no_memory);
  284.     return NULL;
  285.   }
  286.  
  287.   target_vec = bfd_find_target (target, nbfd);
  288.   if (target_vec == NULL) return NULL;
  289.  
  290.   nbfd->filename = filename;
  291.   nbfd->direction = write_direction;
  292.  
  293.   if (bfd_open_file (nbfd) == NULL) {
  294.     bfd_set_error (bfd_error_system_call);    /* File not writeable, etc */
  295.     (void) obstack_free (&nbfd->memory, (PTR)0);
  296.     return NULL;
  297.   }
  298.   return nbfd;
  299. }
  300.  
  301. /*
  302.  
  303. FUNCTION
  304.     bfd_close
  305.  
  306. SYNOPSIS
  307.     boolean bfd_close(bfd *abfd);
  308.  
  309. DESCRIPTION
  310.  
  311.     Close a BFD. If the BFD was open for writing,
  312.     then pending operations are completed and the file written out
  313.     and closed. If the created file is executable, then
  314.     <<chmod>> is called to mark it as such.
  315.  
  316.     All memory attached to the BFD's obstacks is released.
  317.  
  318.     The file descriptor associated with the BFD is closed (even
  319.     if it was passed in to BFD by <<bfd_fdopenr>>).
  320.  
  321. RETURNS
  322.     <<true>> is returned if all is ok, otherwise <<false>>.
  323. */
  324.  
  325.  
  326. boolean
  327. bfd_close (abfd)
  328.      bfd *abfd;
  329. {
  330.   boolean ret;
  331.  
  332.   if (!bfd_read_p(abfd))
  333.     if (BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)) != true)
  334.       return false;
  335.  
  336.   if (BFD_SEND (abfd, _close_and_cleanup, (abfd)) != true) return false;
  337.  
  338.   ret = bfd_cache_close(abfd);
  339.  
  340.   /* If the file was open for writing and is now executable,
  341.      make it so */
  342.   if (ret == true
  343.       && abfd->direction == write_direction
  344.       && abfd->flags & EXEC_P) {
  345.     struct stat buf;
  346.     stat(abfd->filename, &buf);
  347. #ifndef S_IXUSR
  348. #define S_IXUSR 0100    /* Execute by owner.  */
  349. #endif
  350. #ifndef S_IXGRP
  351. #define S_IXGRP 0010    /* Execute by group.  */
  352. #endif
  353. #ifndef S_IXOTH
  354. #define S_IXOTH 0001    /* Execute by others.  */
  355. #endif
  356.  
  357.     chmod(abfd->filename, 0777  & (buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH));
  358.   }
  359.   (void) obstack_free (&abfd->memory, (PTR)0);
  360.   (void) free(abfd);
  361.   return ret;
  362. }
  363.  
  364. /*
  365. FUNCTION
  366.     bfd_close_all_done
  367.  
  368. SYNOPSIS
  369.     boolean bfd_close_all_done(bfd *);
  370.  
  371. DESCRIPTION
  372.     Close a BFD.  Differs from <<bfd_close>>
  373.     since it does not complete any pending operations.  This
  374.     routine would be used if the application had just used BFD for
  375.     swapping and didn't want to use any of the writing code.
  376.  
  377.     If the created file is executable, then <<chmod>> is called
  378.     to mark it as such.
  379.  
  380.     All memory attached to the BFD's obstacks is released.
  381.  
  382. RETURNS
  383.     <<true>> is returned if all is ok, otherwise <<false>>.
  384.  
  385. */
  386.  
  387. boolean
  388. bfd_close_all_done (abfd)
  389.      bfd *abfd;
  390. {
  391.   boolean ret;
  392.  
  393.   ret = bfd_cache_close(abfd);
  394.  
  395.   /* If the file was open for writing and is now executable,
  396.      make it so */
  397.   if (ret == true
  398.       && abfd->direction == write_direction
  399.       && abfd->flags & EXEC_P) {
  400.     struct stat buf;
  401.     stat(abfd->filename, &buf);
  402. #ifndef S_IXUSR
  403. #define S_IXUSR 0100    /* Execute by owner.  */
  404. #endif
  405. #ifndef S_IXGRP
  406. #define S_IXGRP 0010    /* Execute by group.  */
  407. #endif
  408. #ifndef S_IXOTH
  409. #define S_IXOTH 0001    /* Execute by others.  */
  410. #endif
  411.  
  412.     chmod(abfd->filename, 0x777 &(buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH));
  413.   }
  414.   (void) obstack_free (&abfd->memory, (PTR)0);
  415.   (void) free(abfd);
  416.   return ret;
  417. }
  418.  
  419.  
  420. /*
  421. FUNCTION    
  422.     bfd_alloc_size
  423.  
  424. SYNOPSIS
  425.     bfd_size_type bfd_alloc_size(bfd *abfd);
  426.  
  427. DESCRIPTION
  428.         Return the number of bytes in the obstacks connected to @var{abfd}.
  429.  
  430. */
  431.  
  432. bfd_size_type
  433. bfd_alloc_size (abfd)
  434.      bfd *abfd;
  435. {
  436.   struct _obstack_chunk *chunk = abfd->memory.chunk;
  437.   size_t size = 0;
  438.   while (chunk) {
  439.     size += chunk->limit - &(chunk->contents[0]);
  440.     chunk = chunk->prev;
  441.   }
  442.   return size;
  443. }
  444.  
  445.  
  446.  
  447. /*
  448. FUNCTION
  449.     bfd_create
  450.  
  451. SYNOPSIS
  452.     bfd *bfd_create(CONST char *filename, bfd *templ);
  453.  
  454. DESCRIPTION
  455.     Create a new BFD in the manner of
  456.     <<bfd_openw>>, but without opening a file. The new BFD
  457.     takes the target from the target used by @var{template}. The
  458.     format is always set to <<bfd_object>>.
  459.  
  460. */
  461.  
  462. bfd *
  463. bfd_create (filename, templ)
  464.      CONST char *filename;
  465.      bfd *templ;
  466. {
  467.   bfd *nbfd = _bfd_new_bfd();
  468.   if (nbfd == (bfd *)NULL) {
  469.     bfd_set_error (bfd_error_no_memory);
  470.     return (bfd *)NULL;
  471.   }
  472.   nbfd->filename = filename;
  473.   if(templ) {
  474.     nbfd->xvec = templ->xvec;
  475.   }
  476.   nbfd->direction = no_direction;
  477.   bfd_set_format(nbfd, bfd_object);
  478.   return nbfd;
  479. }
  480.  
  481. /*
  482. INTERNAL_FUNCTION
  483.     bfd_alloc_by_size_t
  484.  
  485. SYNOPSIS
  486.     PTR bfd_alloc_by_size_t(bfd *abfd, size_t wanted);
  487.  
  488. DESCRIPTION
  489.     Allocate a block of @var{wanted} bytes of memory in the obstack
  490.     attatched to <<abfd>> and return a pointer to it.
  491. */
  492.  
  493.  
  494. PTR
  495. bfd_alloc_by_size_t (abfd, size)
  496.      bfd *abfd;
  497.      size_t size;
  498. {
  499.   return obstack_alloc(&(abfd->memory), size);
  500. }
  501.  
  502. void
  503. bfd_alloc_grow (abfd, ptr, size)
  504.      bfd *abfd;
  505.      PTR ptr;
  506.      size_t size;
  507. {
  508.   (void) obstack_grow(&(abfd->memory), ptr, size);
  509. }
  510.  
  511. PTR
  512. bfd_alloc_finish (abfd)
  513.      bfd *abfd;
  514. {
  515.   return obstack_finish(&(abfd->memory));
  516. }
  517.  
  518. PTR
  519. bfd_alloc (abfd, size)
  520.      bfd *abfd;
  521.      size_t size;
  522. {
  523.   return bfd_alloc_by_size_t(abfd, (size_t)size);
  524. }
  525.  
  526. PTR
  527. bfd_zalloc (abfd, size)
  528.      bfd *abfd;
  529.      size_t size;
  530. {
  531.   PTR res;
  532.   res = bfd_alloc(abfd, size);
  533.   if (res)
  534.     memset(res, 0, (size_t)size);
  535.   return res;
  536. }
  537.  
  538. PTR
  539. bfd_realloc (abfd, old, size)
  540.      bfd *abfd;
  541.      PTR old;
  542.      size_t size;
  543. {
  544.   PTR res = bfd_alloc(abfd, size);
  545.   if (res)
  546.     memcpy(res, old, (size_t)size);
  547.   return res;
  548. }
  549.