* config/obj-coffbfd.c: lint, don't fixup relocs if H8300, use
[deliverable/binutils-gdb.git] / bfd / libbfd.c
index aad52fef5bcafb08cc831791b2658dd6617e68a1..394ac5e6cfc38a6e7e6386a49d904f85ac9b441b 100644 (file)
@@ -1,30 +1,29 @@
-/* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
+/* libbfd.c -- random BFD support routines, only used internally.
+   Copyright (C) 1990-1991 Free Software Foundation, Inc.
+   Written by Cygnus Support.
 
-This file is part of BFD, the Binary File Diddler.
+This file is part of BFD, the Binary File Descriptor library.
 
-BFD is free software; you can redistribute it and/or modify
+This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 1, or (at your option)
-any later version.
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
 
-BFD is distributed in the hope that it will be useful,
+This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with BFD; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+along with this program; if not, write to the Free Software
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 /* $Id$ */
 
-/*** libbfd.c -- random bfd support routines used internally only. */
-#include <sysdep.h>
 #include "bfd.h"
+#include "sysdep.h"
 #include "libbfd.h"
 
-
-\f
 /** Dummies for targets that don't want or need to implement
    certain operations */
 
@@ -125,12 +124,37 @@ DEFUN(zalloc,(size),
   return ptr;
 }
 #endif
+
+/*proto-internal* bfd_xmalloc
+bfd_xmalloc -- Like malloc, but exit if no more memory.
+*; PROTO(PTR, bfd_xmalloc,( bfd_size_type size));
+*/
+/** There is major inconsistency in how running out of memory is handled.
+  Some routines return a NULL, and set bfd_error to no_memory.
+  However, obstack routines can't do this ... */
+
+
+DEFUN(PTR bfd_xmalloc,(size),
+      bfd_size_type size)
+{
+  static char no_memory_message[] = "Virtual memory exhausted!\n";
+  PTR ptr;
+  if (size == 0) size = 1;
+  ptr = (PTR)malloc(size);
+  if (ptr == NULL)
+  if (!ptr)
+    {
+      write (2, no_memory_message, sizeof(no_memory_message)-1);
+      exit (-1);
+    }
+  return ptr;
+}
 \f
 /* Some IO code */
 
 
 /* Note that archive entries don't have streams; they share their parent's.
-   This allows someone to play with the iostream behind bfd's back.
+   This allows someone to play with the iostream behind BFD's back.
 
    Also, note that the origin pointer points to the beginning of a file's
    contents (0 for non-archive elements).  For archive entries this is the
@@ -138,10 +162,10 @@ DEFUN(zalloc,(size),
 
 static 
 int DEFUN(real_read,(where, a,b, file),
-         PTR where AND
-         int a AND
-         int b AND
-         FILE *file)
+          PTR where AND
+          int a AND
+          int b AND
+          FILE *file)
 {
   return fread(where, a,b,file);
 }
@@ -157,7 +181,7 @@ DEFUN(bfd_read,(ptr, size, nitems, abfd),
 
 bfd_size_type
 DEFUN(bfd_write,(ptr, size, nitems, abfd),
-      PTR ptr AND
+      CONST PTR ptr AND
       bfd_size_type size AND
       bfd_size_type nitems AND
       bfd *abfd)
@@ -165,49 +189,64 @@ DEFUN(bfd_write,(ptr, size, nitems, abfd),
   return fwrite (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
 }
 
+/*proto-internal* bfd_write_bigendian_4byte_int
+
+*; PROTO(void, bfd_write_bigendian_4byte_int,( bfd *abfd,  int i));
+*/
+void
+DEFUN(bfd_write_bigendian_4byte_int,(abfd, i),
+      bfd *abfd AND
+      int i)
+{
+  bfd_byte buffer[4];
+  _do_putb32(i, buffer);
+  bfd_write((PTR)buffer, 4, 1, abfd);
+}
+
 int
 DEFUN(bfd_seek,(abfd, position, direction),
       bfd * CONST abfd AND
       CONST file_ptr position AND
       CONST int direction)
 {
-       /* For the time being, a bfd may not seek to it's end.  The
-          problem is that we don't easily have a way to recognize
-          the end of an element in an archive. */
-
-       BFD_ASSERT(direction == SEEK_SET
-                  || direction == SEEK_CUR);
-       
-       if (direction == SEEK_SET && abfd->my_archive != NULL) 
-           {
-                   /* This is a set within an archive, so we need to
-                      add the base of the object within the archive */
-                   return(fseek(bfd_cache_lookup(abfd),
-                                position + abfd->origin,
-                                direction));
-           }
-       else 
-           {
-                   return(fseek(bfd_cache_lookup(abfd),  position, direction));
-           }   
+        /* For the time being, a BFD may not seek to it's end.  The
+           problem is that we don't easily have a way to recognize
+           the end of an element in an archive. */
+
+        BFD_ASSERT(direction == SEEK_SET
+                   || direction == SEEK_CUR);
+        
+        if (direction == SEEK_SET && abfd->my_archive != NULL) 
+            {
+                    /* This is a set within an archive, so we need to
+                       add the base of the object within the archive */
+                    return(fseek(bfd_cache_lookup(abfd),
+                                 position + abfd->origin,
+                                 direction));
+            }
+        else 
+            {
+                    return(fseek(bfd_cache_lookup(abfd),  position, direction));
+            }   
 }
 
 long
 DEFUN(bfd_tell,(abfd),
       bfd *abfd)
 {
-       file_ptr ptr;
+        file_ptr ptr;
 
-       ptr = ftell (bfd_cache_lookup(abfd));
+        ptr = ftell (bfd_cache_lookup(abfd));
 
-       if (abfd->my_archive)
-           ptr -= abfd->origin;
-       return ptr;
+        if (abfd->my_archive)
+            ptr -= abfd->origin;
+        return ptr;
 }
 \f
 /** Make a string table */
 
-/* Add string to table pointed to by table, at location starting with free_ptr.
+/*>bfd.h<
+ Add string to table pointed to by table, at location starting with free_ptr.
    resizes the table if necessary (if it's NULL, creates it, ignoring
    table_length).  Updates free_ptr, table, table_length */
 
@@ -215,8 +254,8 @@ boolean
 DEFUN(bfd_add_to_string_table,(table, new_string, table_length, free_ptr),
       char **table AND
       char *new_string AND
-      char **free_ptr AND
-      unsigned int *table_length)
+      unsigned int *table_length AND
+      char **free_ptr)
 {
   size_t string_length = strlen (new_string) + 1; /* include null here */
   char *base = *table;
@@ -227,7 +266,7 @@ DEFUN(bfd_add_to_string_table,(table, new_string, table_length, free_ptr),
     /* Avoid a useless regrow if we can (but of course we still
        take it next time */
     space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
-                   DEFAULT_STRING_SPACE_SIZE : string_length+1);
+                    DEFAULT_STRING_SPACE_SIZE : string_length+1);
     base = zalloc (space_length);
 
     if (base == NULL) {
@@ -265,56 +304,111 @@ DEFUN(bfd_add_to_string_table,(table, new_string, table_length, free_ptr),
 
 /* FIXME: Should these take a count argument?
    Answer (gnu@cygnus.com):  No, but perhaps they should be inline
-                            functions in swap.h #ifdef __GNUC__. 
-                            Gprof them later and find out.  */
-
-unsigned int
+                             functions in swap.h #ifdef __GNUC__. 
+                             Gprof them later and find out.  */
+
+/*proto*
+*i bfd_put_size
+*i bfd_get_size
+These macros as used for reading and writing raw data in sections;
+each access (except for bytes) is vectored through the target format
+of the BFD and mangled accordingly. The mangling performs any
+necessary endian translations and removes alignment restrictions.
+*+
+#define bfd_put_8(abfd, val, ptr) \
+                (*((char *)ptr) = (char)val)
+#define bfd_get_8(abfd, ptr) \
+                (*((char *)ptr))
+#define bfd_put_16(abfd, val, ptr) \
+                BFD_SEND(abfd, bfd_putx16, (val,ptr))
+#define bfd_get_16(abfd, ptr) \
+                BFD_SEND(abfd, bfd_getx16, (ptr))
+#define bfd_put_32(abfd, val, ptr) \
+                BFD_SEND(abfd, bfd_putx32, (val,ptr))
+#define bfd_get_32(abfd, ptr) \
+                BFD_SEND(abfd, bfd_getx32, (ptr))
+#define bfd_put_64(abfd, val, ptr) \
+                BFD_SEND(abfd, bfd_putx64, (val, ptr))
+#define bfd_get_64(abfd, ptr) \
+                BFD_SEND(abfd, bfd_getx64, (ptr))
+*-
+*-*/ 
+
+/*proto*
+*i bfd_h_put_size
+*i bfd_h_get_size
+These macros have the same function as their @code{bfd_get_x}
+bretherin, except that they are used for removing information for the
+header records of object files. Believe it or not, some object files
+keep their header records in big endian order, and their data in little
+endan order.
+*+
+#define bfd_h_put_8(abfd, val, ptr) \
+                (*((char *)ptr) = (char)val)
+#define bfd_h_get_8(abfd, ptr) \
+                (*((char *)ptr))
+#define bfd_h_put_16(abfd, val, ptr) \
+                BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
+#define bfd_h_get_16(abfd, ptr) \
+                BFD_SEND(abfd, bfd_h_getx16,(ptr))
+#define bfd_h_put_32(abfd, val, ptr) \
+                BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
+#define bfd_h_get_32(abfd, ptr) \
+                BFD_SEND(abfd, bfd_h_getx32,(ptr))
+#define bfd_h_put_64(abfd, val, ptr) \
+                BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
+#define bfd_h_get_64(abfd, ptr) \
+                BFD_SEND(abfd, bfd_h_getx64,(ptr))
+*-
+*-*/ 
+
+bfd_vma
 DEFUN(_do_getb16,(addr),
       register bfd_byte *addr)
 {
-       return (addr[0] << 8) | addr[1];
+        return (addr[0] << 8) | addr[1];
 }
 
-unsigned int
+bfd_vma
 DEFUN(_do_getl16,(addr),
       register bfd_byte *addr)
 {
-       return (addr[1] << 8) | addr[0];
+        return (addr[1] << 8) | addr[0];
 }
 
 void
 DEFUN(_do_putb16,(data, addr),
-      int data AND
+      bfd_vma data AND
       register bfd_byte *addr)
 {
-       addr[0] = (bfd_byte)(data >> 8);
-       addr[1] = (bfd_byte )data;
+        addr[0] = (bfd_byte)(data >> 8);
+        addr[1] = (bfd_byte )data;
 }
 
 void
 DEFUN(_do_putl16,(data, addr),
-      int data AND             
+      bfd_vma data AND              
       register bfd_byte *addr)
 {
-       addr[0] = (bfd_byte )data;
-       addr[1] = (bfd_byte)(data >> 8);
+        addr[0] = (bfd_byte )data;
+        addr[1] = (bfd_byte)(data >> 8);
 }
 
-unsigned int
+bfd_vma
 DEFUN(_do_getb32,(addr),
       register bfd_byte *addr)
 {
-       return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
+        return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
 }
 
-unsigned int
+bfd_vma
 _do_getl32 (addr)
-       register bfd_byte *addr;
+        register bfd_byte *addr;
 {
-       return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
+        return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
 }
 
-bfd_64_type
+bfd_vma
 DEFUN(_do_getb64,(addr),
       register bfd_byte *addr)
 {
@@ -322,69 +416,73 @@ DEFUN(_do_getb64,(addr),
   bfd_64_type low, high;
 
   high= ((((((((addr[0]) << 8) |
-             addr[1]) << 8) |
-           addr[2]) << 8) |
-         addr[3]) );
+              addr[1]) << 8) |
+            addr[2]) << 8) |
+          addr[3]) );
 
   low = ((((((((addr[4]) << 8) |
-             addr[5]) << 8) |
-           addr[6]) << 8) |
-         addr[7]));
+              addr[5]) << 8) |
+            addr[6]) << 8) |
+          addr[7]));
 
   return high << 32 | low;
 #else
   BFD_FAIL();
+  return 0;
 #endif
 
 }
 
-bfd_64_type
-DEFUN(do_getl64,(addr),
+bfd_vma
+DEFUN(_do_getl64,(addr),
       register bfd_byte *addr)
 {
-  bfd_64_type low, high;
+
 #ifdef HOST_64_BIT
+  bfd_64_type low, high;
   high= (((((((addr[7] << 8) |
-             addr[6]) << 8) |
-           addr[5]) << 8) |
-         addr[4]));
+              addr[6]) << 8) |
+            addr[5]) << 8) |
+          addr[4]));
 
   low = (((((((addr[3] << 8) |
-             addr[2]) << 8) |
-           addr[1]) << 8) |
-         addr[0]) );
+              addr[2]) << 8) |
+            addr[1]) << 8) |
+          addr[0]) );
 
   return high << 32 | low;
 #else
   BFD_FAIL();
+  return 0;
 #endif
+
 }
 
 void
 DEFUN(_do_putb32,(data, addr),
-      unsigned long data AND
+      bfd_vma data AND
       register bfd_byte *addr)
 {
-       addr[0] = (bfd_byte)(data >> 24);
-       addr[1] = (bfd_byte)(data >> 16);
-       addr[2] = (bfd_byte)(data >>  8);
-       addr[3] = (bfd_byte)data;
+        addr[0] = (bfd_byte)(data >> 24);
+        addr[1] = (bfd_byte)(data >> 16);
+        addr[2] = (bfd_byte)(data >>  8);
+        addr[3] = (bfd_byte)data;
 }
 
 void
 DEFUN(_do_putl32,(data, addr),
-      unsigned long data AND
+      bfd_vma data AND
       register bfd_byte *addr)
 {
-       addr[0] = (bfd_byte)data;
-       addr[1] = (bfd_byte)(data >>  8);
-       addr[2] = (bfd_byte)(data >> 16);
-       addr[3] = (bfd_byte)(data >> 24);
+        addr[0] = (bfd_byte)data;
+        addr[1] = (bfd_byte)(data >>  8);
+        addr[2] = (bfd_byte)(data >> 16);
+        addr[3] = (bfd_byte)(data >> 24);
 }
 void
 DEFUN(_do_putb64,(data, addr),
-       bfd_64_type data AND
-       register bfd_byte *addr)
+        bfd_vma data AND
+        register bfd_byte *addr)
 {
 #ifdef HOST_64_BIT
   addr[0] = (bfd_byte)(data >> (7*8));
@@ -403,7 +501,7 @@ DEFUN(_do_putb64,(data, addr),
 
 void
 DEFUN(_do_putl64,(data, addr),
-      bfd_64_type data AND
+      bfd_vma data AND
       register bfd_byte *addr)
 {
 #ifdef HOST_64_BIT
@@ -433,10 +531,47 @@ DEFUN(bfd_generic_get_section_contents, (abfd, section, location, offset, count)
       bfd_size_type count)
 {
     if (count == 0)
-       return true;
-    if ((bfd_size_type)offset >= section->size
-       || bfd_seek(abfd,(file_ptr)( section->filepos + offset), SEEK_SET) == -1
-       || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
-       return (false); /* on error */
+        return true;
+    if ((bfd_size_type)(offset+count) > section->size
+        || bfd_seek(abfd,(file_ptr)( section->filepos + offset), SEEK_SET) == -1
+        || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
+        return (false); /* on error */
     return (true);
 }
+
+/* This generic function can only be used in implementations where creating
+   NEW sections is disallowed.  It is useful in patching existing sections
+   in read-write files, though.  See other set_section_contents functions
+   to see why it doesn't work for new sections.  */
+boolean
+DEFUN(bfd_generic_set_section_contents, (abfd, section, location, offset, count),
+      bfd *abfd AND
+      sec_ptr section AND
+      PTR location AND
+      file_ptr offset AND
+      bfd_size_type count)
+{
+    if (count == 0)
+        return true;
+    if ((bfd_size_type)(offset+count) > section->size
+        || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
+        || bfd_write(location, (bfd_size_type)1, count, abfd) != count)
+        return (false); /* on error */
+    return (true);
+}
+
+/*proto-internal*
+*i bfd_log2
+Return the log base 2 of the value supplied, rounded up. eg an arg
+of 1025 would return 11.
+*; PROTO(bfd_vma, bfd_log2,(bfd_vma x));
+*-*/
+
+bfd_vma bfd_log2(x)
+bfd_vma x;
+{
+  bfd_vma  result = 0;
+  while ( (bfd_vma)(1<< result) < x)
+    result++;
+  return result;
+}
This page took 0.0322 seconds and 4 git commands to generate.