* targets.c (proto write_armap). Changed orl_count to unsigned.
[deliverable/binutils-gdb.git] / bfd / libbfd.c
1 /* libbfd.c -- random BFD support routines, only used internally.
2 Copyright (C) 1990-1991 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 /* $Id$ */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26
27 /** Dummies for targets that don't want or need to implement
28 certain operations */
29
30 boolean
31 DEFUN(_bfd_dummy_new_section_hook,(ignore, ignore_newsect),
32 bfd *ignore AND
33 asection *ignore_newsect)
34 {
35 return true;
36 }
37
38 boolean
39 DEFUN(bfd_false ,(ignore),
40 bfd *ignore)
41 {
42 return false;
43 }
44
45 boolean
46 DEFUN(bfd_true,(ignore),
47 bfd *ignore)
48 {
49 return true;
50 }
51
52 PTR
53 DEFUN(bfd_nullvoidptr,(ignore),
54 bfd *ignore)
55 {
56 return (PTR)NULL;
57 }
58
59 int
60 DEFUN(bfd_0,(ignore),
61 bfd *ignore)
62 {
63 return 0;
64 }
65
66 unsigned int
67 DEFUN(bfd_0u,(ignore),
68 bfd *ignore)
69 {
70 return 0;
71 }
72
73 void
74 DEFUN(bfd_void,(ignore),
75 bfd *ignore)
76 {
77 }
78
79 boolean
80 DEFUN(_bfd_dummy_core_file_matches_executable_p,(ignore_core_bfd, ignore_exec_bfd),
81 bfd *ignore_core_bfd AND
82 bfd *ignore_exec_bfd)
83 {
84 bfd_error = invalid_operation;
85 return false;
86 }
87
88 /* of course you can't initialize a function to be the same as another, grr */
89
90 char *
91 DEFUN(_bfd_dummy_core_file_failing_command,(ignore_abfd),
92 bfd *ignore_abfd)
93 {
94 return (char *)NULL;
95 }
96
97 int
98 DEFUN(_bfd_dummy_core_file_failing_signal,(ignore_abfd),
99 bfd *ignore_abfd)
100 {
101 return 0;
102 }
103
104 bfd_target *
105 DEFUN(_bfd_dummy_target,(ignore_abfd),
106 bfd *ignore_abfd)
107 {
108 return 0;
109 }
110 \f
111 /** zalloc -- allocate and clear storage */
112
113
114 #ifndef zalloc
115 char *
116 DEFUN(zalloc,(size),
117 bfd_size_type size)
118 {
119 char *ptr = (char *) malloc ((int)size);
120
121 if ((ptr != NULL) && (size != 0))
122 memset(ptr,0, size);
123
124 return ptr;
125 }
126 #endif
127 \f
128 /* Some IO code */
129
130
131 /* Note that archive entries don't have streams; they share their parent's.
132 This allows someone to play with the iostream behind BFD's back.
133
134 Also, note that the origin pointer points to the beginning of a file's
135 contents (0 for non-archive elements). For archive entries this is the
136 first octet in the file, NOT the beginning of the archive header. */
137
138 static
139 int DEFUN(real_read,(where, a,b, file),
140 PTR where AND
141 int a AND
142 int b AND
143 FILE *file)
144 {
145 return fread(where, a,b,file);
146 }
147 bfd_size_type
148 DEFUN(bfd_read,(ptr, size, nitems, abfd),
149 PTR ptr AND
150 bfd_size_type size AND
151 bfd_size_type nitems AND
152 bfd *abfd)
153 {
154 return (bfd_size_type)real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
155 }
156
157 bfd_size_type
158 DEFUN(bfd_write,(ptr, size, nitems, abfd),
159 CONST PTR ptr AND
160 bfd_size_type size AND
161 bfd_size_type nitems AND
162 bfd *abfd)
163 {
164 return fwrite (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
165 }
166
167 void
168 DEFUN(bfd_write_bigendian_4byte_int,(abfd, i),
169 bfd *abfd AND
170 int i)
171 {
172 char buffer[4];
173 _do_putb32(i, buffer);
174 bfd_write(buffer, 4, 1, abfd);
175 }
176
177 int
178 DEFUN(bfd_seek,(abfd, position, direction),
179 bfd * CONST abfd AND
180 CONST file_ptr position AND
181 CONST int direction)
182 {
183 /* For the time being, a BFD may not seek to it's end. The
184 problem is that we don't easily have a way to recognize
185 the end of an element in an archive. */
186
187 BFD_ASSERT(direction == SEEK_SET
188 || direction == SEEK_CUR);
189
190 if (direction == SEEK_SET && abfd->my_archive != NULL)
191 {
192 /* This is a set within an archive, so we need to
193 add the base of the object within the archive */
194 return(fseek(bfd_cache_lookup(abfd),
195 position + abfd->origin,
196 direction));
197 }
198 else
199 {
200 return(fseek(bfd_cache_lookup(abfd), position, direction));
201 }
202 }
203
204 long
205 DEFUN(bfd_tell,(abfd),
206 bfd *abfd)
207 {
208 file_ptr ptr;
209
210 ptr = ftell (bfd_cache_lookup(abfd));
211
212 if (abfd->my_archive)
213 ptr -= abfd->origin;
214 return ptr;
215 }
216 \f
217 /** Make a string table */
218
219 /*>bfd.h<
220 Add string to table pointed to by table, at location starting with free_ptr.
221 resizes the table if necessary (if it's NULL, creates it, ignoring
222 table_length). Updates free_ptr, table, table_length */
223
224 boolean
225 DEFUN(bfd_add_to_string_table,(table, new_string, table_length, free_ptr),
226 char **table AND
227 char *new_string AND
228 unsigned int *table_length AND
229 char **free_ptr)
230 {
231 size_t string_length = strlen (new_string) + 1; /* include null here */
232 char *base = *table;
233 size_t space_length = *table_length;
234 unsigned int offset = (base ? *free_ptr - base : 0);
235
236 if (base == NULL) {
237 /* Avoid a useless regrow if we can (but of course we still
238 take it next time */
239 space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
240 DEFAULT_STRING_SPACE_SIZE : string_length+1);
241 base = zalloc (space_length);
242
243 if (base == NULL) {
244 bfd_error = no_memory;
245 return false;
246 }
247 }
248
249 if ((size_t)(offset + string_length) >= space_length) {
250 /* Make sure we will have enough space */
251 while ((size_t)(offset + string_length) >= space_length)
252 space_length += space_length/2; /* grow by 50% */
253
254 base = (char *) realloc (base, space_length);
255 if (base == NULL) {
256 bfd_error = no_memory;
257 return false;
258 }
259
260 }
261
262 memcpy (base + offset, new_string, string_length);
263 *table = base;
264 *table_length = space_length;
265 *free_ptr = base + offset + string_length;
266
267 return true;
268 }
269 \f
270 /** The do-it-yourself (byte) sex-change kit */
271
272 /* The middle letter e.g. get<b>short indicates Big or Little endian
273 target machine. It doesn't matter what the byte order of the host
274 machine is; these routines work for either. */
275
276 /* FIXME: Should these take a count argument?
277 Answer (gnu@cygnus.com): No, but perhaps they should be inline
278 functions in swap.h #ifdef __GNUC__.
279 Gprof them later and find out. */
280
281 /*proto*
282 *i bfd_put_size
283 *i bfd_get_size
284 These macros as used for reading and writing raw data in sections;
285 each access (except for bytes) is vectored through the target format
286 of the BFD and mangled accordingly. The mangling performs any
287 necessary endian translations and removes alignment restrictions.
288 *+
289 #define bfd_put_8(abfd, val, ptr) \
290 (*((char *)ptr) = (char)val)
291 #define bfd_get_8(abfd, ptr) \
292 (*((char *)ptr))
293 #define bfd_put_16(abfd, val, ptr) \
294 BFD_SEND(abfd, bfd_putx16, (val,ptr))
295 #define bfd_get_16(abfd, ptr) \
296 BFD_SEND(abfd, bfd_getx16, (ptr))
297 #define bfd_put_32(abfd, val, ptr) \
298 BFD_SEND(abfd, bfd_putx32, (val,ptr))
299 #define bfd_get_32(abfd, ptr) \
300 BFD_SEND(abfd, bfd_getx32, (ptr))
301 #define bfd_put_64(abfd, val, ptr) \
302 BFD_SEND(abfd, bfd_putx64, (val, ptr))
303 #define bfd_get_64(abfd, ptr) \
304 BFD_SEND(abfd, bfd_getx64, (ptr))
305 *-
306 *-*/
307
308 /*proto*
309 *i bfd_h_put_size
310 *i bfd_h_get_size
311 These macros have the same function as their @code{bfd_get_x}
312 bretherin, except that they are used for removing information for the
313 header records of object files. Believe it or not, some object files
314 keep their header records in big endian order, and their data in little
315 endan order.
316 *+
317 #define bfd_h_put_8(abfd, val, ptr) \
318 (*((char *)ptr) = (char)val)
319 #define bfd_h_get_8(abfd, ptr) \
320 (*((char *)ptr))
321 #define bfd_h_put_16(abfd, val, ptr) \
322 BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
323 #define bfd_h_get_16(abfd, ptr) \
324 BFD_SEND(abfd, bfd_h_getx16,(ptr))
325 #define bfd_h_put_32(abfd, val, ptr) \
326 BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
327 #define bfd_h_get_32(abfd, ptr) \
328 BFD_SEND(abfd, bfd_h_getx32,(ptr))
329 #define bfd_h_put_64(abfd, val, ptr) \
330 BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
331 #define bfd_h_get_64(abfd, ptr) \
332 BFD_SEND(abfd, bfd_h_getx64,(ptr))
333 *-
334 *-*/
335
336 bfd_vma
337 DEFUN(_do_getb16,(addr),
338 register bfd_byte *addr)
339 {
340 return (addr[0] << 8) | addr[1];
341 }
342
343 bfd_vma
344 DEFUN(_do_getl16,(addr),
345 register bfd_byte *addr)
346 {
347 return (addr[1] << 8) | addr[0];
348 }
349
350 void
351 DEFUN(_do_putb16,(data, addr),
352 bfd_vma data AND
353 register bfd_byte *addr)
354 {
355 addr[0] = (bfd_byte)(data >> 8);
356 addr[1] = (bfd_byte )data;
357 }
358
359 void
360 DEFUN(_do_putl16,(data, addr),
361 bfd_vma data AND
362 register bfd_byte *addr)
363 {
364 addr[0] = (bfd_byte )data;
365 addr[1] = (bfd_byte)(data >> 8);
366 }
367
368 bfd_vma
369 DEFUN(_do_getb32,(addr),
370 register bfd_byte *addr)
371 {
372 return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3];
373 }
374
375 bfd_vma
376 _do_getl32 (addr)
377 register bfd_byte *addr;
378 {
379 return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0];
380 }
381
382 bfd_vma
383 DEFUN(_do_getb64,(addr),
384 register bfd_byte *addr)
385 {
386 #ifdef HOST_64_BIT
387 bfd_64_type low, high;
388
389 high= ((((((((addr[0]) << 8) |
390 addr[1]) << 8) |
391 addr[2]) << 8) |
392 addr[3]) );
393
394 low = ((((((((addr[4]) << 8) |
395 addr[5]) << 8) |
396 addr[6]) << 8) |
397 addr[7]));
398
399 return high << 32 | low;
400 #else
401 BFD_FAIL();
402 return 0;
403 #endif
404
405 }
406
407 bfd_vma
408 DEFUN(_do_getl64,(addr),
409 register bfd_byte *addr)
410 {
411
412 #ifdef HOST_64_BIT
413 bfd_64_type low, high;
414 high= (((((((addr[7] << 8) |
415 addr[6]) << 8) |
416 addr[5]) << 8) |
417 addr[4]));
418
419 low = (((((((addr[3] << 8) |
420 addr[2]) << 8) |
421 addr[1]) << 8) |
422 addr[0]) );
423
424 return high << 32 | low;
425 #else
426 BFD_FAIL();
427 return 0;
428 #endif
429
430 }
431
432 void
433 DEFUN(_do_putb32,(data, addr),
434 bfd_vma data AND
435 register bfd_byte *addr)
436 {
437 addr[0] = (bfd_byte)(data >> 24);
438 addr[1] = (bfd_byte)(data >> 16);
439 addr[2] = (bfd_byte)(data >> 8);
440 addr[3] = (bfd_byte)data;
441 }
442
443 void
444 DEFUN(_do_putl32,(data, addr),
445 bfd_vma data AND
446 register bfd_byte *addr)
447 {
448 addr[0] = (bfd_byte)data;
449 addr[1] = (bfd_byte)(data >> 8);
450 addr[2] = (bfd_byte)(data >> 16);
451 addr[3] = (bfd_byte)(data >> 24);
452 }
453 void
454 DEFUN(_do_putb64,(data, addr),
455 bfd_vma data AND
456 register bfd_byte *addr)
457 {
458 #ifdef HOST_64_BIT
459 addr[0] = (bfd_byte)(data >> (7*8));
460 addr[1] = (bfd_byte)(data >> (6*8));
461 addr[2] = (bfd_byte)(data >> (5*8));
462 addr[3] = (bfd_byte)(data >> (4*8));
463 addr[4] = (bfd_byte)(data >> (3*8));
464 addr[5] = (bfd_byte)(data >> (2*8));
465 addr[6] = (bfd_byte)(data >> (1*8));
466 addr[7] = (bfd_byte)(data >> (0*8));
467 #else
468 BFD_FAIL();
469 #endif
470
471 }
472
473 void
474 DEFUN(_do_putl64,(data, addr),
475 bfd_vma data AND
476 register bfd_byte *addr)
477 {
478 #ifdef HOST_64_BIT
479 addr[7] = (bfd_byte)(data >> (7*8));
480 addr[6] = (bfd_byte)(data >> (6*8));
481 addr[5] = (bfd_byte)(data >> (5*8));
482 addr[4] = (bfd_byte)(data >> (4*8));
483 addr[3] = (bfd_byte)(data >> (3*8));
484 addr[2] = (bfd_byte)(data >> (2*8));
485 addr[1] = (bfd_byte)(data >> (1*8));
486 addr[0] = (bfd_byte)(data >> (0*8));
487 #else
488 BFD_FAIL();
489 #endif
490
491 }
492
493 \f
494 /* Default implementation */
495
496 boolean
497 DEFUN(bfd_generic_get_section_contents, (abfd, section, location, offset, count),
498 bfd *abfd AND
499 sec_ptr section AND
500 PTR location AND
501 file_ptr offset AND
502 bfd_size_type count)
503 {
504 if (count == 0)
505 return true;
506 if ((bfd_size_type)(offset+count) > section->size
507 || bfd_seek(abfd,(file_ptr)( section->filepos + offset), SEEK_SET) == -1
508 || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
509 return (false); /* on error */
510 return (true);
511 }
512
513 /* This generic function can only be used in implementations where creating
514 NEW sections is disallowed. It is useful in patching existing sections
515 in read-write files, though. See other set_section_contents functions
516 to see why it doesn't work for new sections. */
517 boolean
518 DEFUN(bfd_generic_set_section_contents, (abfd, section, location, offset, count),
519 bfd *abfd AND
520 sec_ptr section AND
521 PTR location AND
522 file_ptr offset AND
523 bfd_size_type count)
524 {
525 if (count == 0)
526 return true;
527 if ((bfd_size_type)(offset+count) > section->size
528 || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
529 || bfd_write(location, (bfd_size_type)1, count, abfd) != count)
530 return (false); /* on error */
531 return (true);
532 }
533
534 /*proto-internal*
535 *i bfd_log2
536 Return the log base 2 of the value supplied, rounded up. eg an arg
537 of 1025 would return 11.
538 *; PROTO(bfd_vma, bfd_log2,(bfd_vma x));
539 *-*/
540
541 bfd_vma bfd_log2(x)
542 bfd_vma x;
543 {
544 bfd_vma result = 0;
545 while ( (bfd_vma)(1<< result) < x)
546 result++;
547 return result;
548 }
This page took 0.090995 seconds and 4 git commands to generate.