Make arg to zalloc a bfd_size_type not a size_t
[external/binutils.git] / bfd / libbfd.c
1 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
2
3 This file is part of BFD, the Binary File Diddler.
4
5 BFD is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
8 any later version.
9
10 BFD is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with BFD; see the file COPYING.  If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
18
19 /* $Id$ */
20
21 /*** libbfd.c -- random bfd support routines used internally only. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25
26
27 \f
28 /** Dummies for targets that don't want or need to implement
29    certain operations */
30
31 boolean
32 _bfd_dummy_new_section_hook (ignore, ignore_newsect)
33      bfd *ignore;
34      asection *ignore_newsect;
35 {
36   return true;
37 }
38
39 boolean
40 bfd_false (ignore)
41      bfd *ignore;
42 {
43   return false;
44 }
45
46 boolean
47 bfd_true (ignore)
48      bfd *ignore;
49 {
50   return true;
51 }
52
53 void *
54 bfd_nullvoidptr(ignore)
55 bfd *ignore;
56 {
57   return (void *)NULL;
58 }
59
60 int 
61 bfd_0(ignore)
62 bfd *ignore;
63 {
64   return 0;
65 }
66
67 unsigned int 
68 bfd_0u(ignore)
69 bfd *ignore;
70 {
71    return 0;
72 }
73
74 void 
75 bfd_void(ignore)
76 bfd *ignore;
77 {
78 }
79
80 boolean
81 _bfd_dummy_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd)
82      bfd *ignore_core_bfd;
83 bfd *ignore_exec_bfd;
84 {
85   bfd_error = invalid_operation;
86   return false;
87 }
88
89 /* of course you can't initialize a function to be the same as another, grr */
90
91 char *
92 _bfd_dummy_core_file_failing_command (ignore_abfd)
93      bfd *ignore_abfd;
94 {
95   return (char *)NULL;
96 }
97
98 int
99 _bfd_dummy_core_file_failing_signal (ignore_abfd)
100      bfd *ignore_abfd;
101 {
102   return 0;
103 }
104
105 bfd_target *
106 _bfd_dummy_target (ignore_abfd)
107      bfd *ignore_abfd;
108 {
109   return 0;
110 }
111 \f
112 /** zalloc -- allocate and clear storage */
113
114
115 #ifndef zalloc
116 char *
117 zalloc (size)
118      bfd_size_type size;
119 {
120   char *ptr = (char *) malloc (size);
121
122   if ((ptr != NULL) && (size != 0))
123    memset(ptr,0, size);
124
125   return ptr;
126 }
127 #endif
128 \f
129 /* Some IO code */
130
131
132 /* Note that archive entries don't have streams; they share their parent's.
133    This allows someone to play with the iostream behind bfd's back.
134
135    Also, note that the origin pointer points to the beginning of a file's
136    contents (0 for non-archive elements).  For archive entries this is the
137    first octet in the file, NOT the beginning of the archive header. */
138
139 bfd_size_type
140 bfd_read (ptr, size, nitems, abfd)
141      PTR ptr;
142      bfd_size_type size;
143      bfd_size_type nitems;
144      bfd *abfd;
145 {
146   return fread (ptr, 1, size*nitems, bfd_cache_lookup(abfd));
147 }
148
149 bfd_size_type
150 bfd_write (ptr, size, nitems, abfd)
151      PTR ptr;
152      bfd_size_type size;
153      bfd_size_type nitems;
154      bfd *abfd;
155 {
156   return fwrite (ptr, 1, size*nitems, bfd_cache_lookup(abfd));
157 }
158
159 int
160 bfd_seek (abfd, position, direction)
161 bfd * CONST abfd;
162 CONST file_ptr position;
163 CONST int direction;
164 {
165         /* For the time being, a bfd may not seek to it's end.  The
166            problem is that we don't easily have a way to recognize
167            the end of an element in an archive. */
168
169         BFD_ASSERT(direction == SEEK_SET
170                    || direction == SEEK_CUR);
171         
172         if (direction == SEEK_SET && abfd->my_archive != NULL) 
173             {
174                     /* This is a set within an archive, so we need to
175                        add the base of the object within the archive */
176                     return(fseek(bfd_cache_lookup(abfd),
177                                  position + abfd->origin,
178                                  direction));
179             }
180         else 
181             {
182                     return(fseek(bfd_cache_lookup(abfd),  position, direction));
183             }   
184 }
185
186 long
187 bfd_tell (abfd)
188      bfd *abfd;
189 {
190         file_ptr ptr;
191
192         ptr = ftell (bfd_cache_lookup(abfd));
193
194         if (abfd->my_archive)
195             ptr -= abfd->origin;
196         return ptr;
197 }
198 \f
199 /** Make a string table */
200
201 /* Add string to table pointed to by table, at location starting with free_ptr.
202    resizes the table if necessary (if it's NULL, creates it, ignoring
203    table_length).  Updates free_ptr, table, table_length */
204
205 boolean
206 bfd_add_to_string_table (table, new_string, table_length, free_ptr)
207      char **table, **free_ptr;
208      char *new_string;
209      unsigned int *table_length;
210 {
211   size_t string_length = strlen (new_string) + 1; /* include null here */
212   char *base = *table;
213   size_t space_length = *table_length;
214   unsigned int offset = (base ? *free_ptr - base : 0);
215
216   if (base == NULL) {
217     /* Avoid a useless regrow if we can (but of course we still
218        take it next time */
219     space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
220                     DEFAULT_STRING_SPACE_SIZE : string_length+1);
221     base = zalloc (space_length);
222
223     if (base == NULL) {
224       bfd_error = no_memory;
225       return false;
226     }
227   }
228
229   if ((size_t)(offset + string_length) >= space_length) {
230     /* Make sure we will have enough space */
231     while ((size_t)(offset + string_length) >= space_length) 
232       space_length += space_length/2; /* grow by 50% */
233
234     base = (char *) realloc (base, space_length);
235     if (base == NULL) {
236       bfd_error = no_memory;
237       return false;
238     }
239
240   }
241
242   memcpy (base + offset, new_string, string_length);
243   *table = base;
244   *table_length = space_length;
245   *free_ptr = base + offset + string_length;
246   
247   return true;
248 }
249 \f
250 /** The do-it-yourself (byte) sex-change kit */
251
252 /* The middle letter e.g. get<b>short indicates Big or Little endian
253    target machine.  It doesn't matter what the byte order of the host
254    machine is; these routines work for either.  */
255
256 /* FIXME: Should these take a count argument?
257    Answer (gnu@cygnus.com):  No, but perhaps they should be inline
258                              functions in swap.h #ifdef __GNUC__. 
259                              Gprof them later and find out.  */
260
261 short
262 _do_getbshort (addr)
263         register bfd_byte *addr;
264 {
265         return (addr[0] << 8) | addr[1];
266 }
267
268 short
269 _do_getlshort (addr)
270         register bfd_byte *addr;
271 {
272         return (addr[1] << 8) | addr[0];
273 }
274
275 void
276 _do_putbshort (data, addr)
277         int data;               /* Actually short, but ansi C sucks */
278         register bfd_byte *addr;
279 {
280         addr[0] = (bfd_byte)(data >> 8);
281         addr[1] = (bfd_byte )data;
282 }
283
284 void
285 _do_putlshort (data, addr)
286         int data;               /* Actually short, but ansi C sucks */
287         register bfd_byte *addr;
288 {
289         addr[0] = (bfd_byte )data;
290         addr[1] = (bfd_byte)(data >> 8);
291 }
292
293 long
294 _do_getblong (addr)
295         register bfd_byte *addr;
296 {
297         return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
298 }
299
300 long
301 _do_getllong (addr)
302         register bfd_byte *addr;
303 {
304         return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
305 }
306
307 void
308 _do_putblong (data, addr)
309         unsigned long data;
310         register bfd_byte *addr;
311 {
312         addr[0] = (bfd_byte)(data >> 24);
313         addr[1] = (bfd_byte)(data >> 16);
314         addr[2] = (bfd_byte)(data >>  8);
315         addr[3] = (bfd_byte)data;
316 }
317
318 void
319 _do_putllong (data, addr)
320         unsigned long data;
321         register bfd_byte *addr;
322 {
323         addr[0] = (bfd_byte)data;
324         addr[1] = (bfd_byte)(data >>  8);
325         addr[2] = (bfd_byte)(data >> 16);
326         addr[3] = (bfd_byte)(data >> 24);
327 }
328
329
330
331
332
333
334