This commit was generated by cvs2svn to track changes on a CVS vendor
[platform/upstream/binutils.git] / bfd / bfd.c
1 /* Generic BFD library interface and support routines.
2    Copyright (C) 1990, 91, 92, 93, 94, 95, 1996 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /*
22 SECTION
23         <<typedef bfd>>
24
25         A BFD has type <<bfd>>; objects of this type are the
26         cornerstone of any application using BFD. Using BFD
27         consists of making references though the BFD and to data in the BFD.
28
29         Here is the structure that defines the type <<bfd>>.  It
30         contains the major data about the file and pointers
31         to the rest of the data.
32
33 CODE_FRAGMENT
34 .
35 .struct _bfd 
36 .{
37 .    {* The filename the application opened the BFD with.  *}
38 .    CONST char *filename;                
39 .
40 .    {* A pointer to the target jump table.             *}
41 .    const struct bfd_target *xvec;
42 .
43 .    {* To avoid dragging too many header files into every file that
44 .       includes `<<bfd.h>>', IOSTREAM has been declared as a "char
45 .       *", and MTIME as a "long".  Their correct types, to which they
46 .       are cast when used, are "FILE *" and "time_t".    The iostream
47 .       is the result of an fopen on the filename.  However, if the
48 .       BFD_IN_MEMORY flag is set, then iostream is actually a pointer
49 .       to a bfd_in_memory struct.  *}
50 .    PTR iostream;
51 .
52 .    {* Is the file descriptor being cached?  That is, can it be closed as
53 .       needed, and re-opened when accessed later?  *}
54 .
55 .    boolean cacheable;
56 .
57 .    {* Marks whether there was a default target specified when the
58 .       BFD was opened. This is used to select which matching algorithm
59 .       to use to choose the back end. *}
60 .
61 .    boolean target_defaulted;
62 .
63 .    {* The caching routines use these to maintain a
64 .       least-recently-used list of BFDs *}
65 .
66 .    struct _bfd *lru_prev, *lru_next;
67 .
68 .    {* When a file is closed by the caching routines, BFD retains
69 .       state information on the file here: *}
70 .
71 .    file_ptr where;              
72 .
73 .    {* and here: (``once'' means at least once) *}
74 .
75 .    boolean opened_once;
76 .
77 .    {* Set if we have a locally maintained mtime value, rather than
78 .       getting it from the file each time: *}
79 .
80 .    boolean mtime_set;
81 .
82 .    {* File modified time, if mtime_set is true: *}
83 .
84 .    long mtime;          
85 .
86 .    {* Reserved for an unimplemented file locking extension.*}
87 .
88 .    int ifd;
89 .
90 .    {* The format which belongs to the BFD. (object, core, etc.) *}
91 .
92 .    bfd_format format;
93 .
94 .    {* The direction the BFD was opened with*}
95 .
96 .    enum bfd_direction {no_direction = 0,
97 .                        read_direction = 1,
98 .                        write_direction = 2,
99 .                        both_direction = 3} direction;
100 .
101 .    {* Format_specific flags*}
102 .
103 .    flagword flags;              
104 .
105 .    {* Currently my_archive is tested before adding origin to
106 .       anything. I believe that this can become always an add of
107 .       origin, with origin set to 0 for non archive files.   *}
108 .
109 .    file_ptr origin;             
110 .
111 .    {* Remember when output has begun, to stop strange things
112 .       from happening. *}
113 .    boolean output_has_begun;
114 .
115 .    {* Pointer to linked list of sections*}
116 .    struct sec  *sections;
117 .
118 .    {* The number of sections *}
119 .    unsigned int section_count;
120 .
121 .    {* Stuff only useful for object files: 
122 .       The start address. *}
123 .    bfd_vma start_address;
124 .
125 .    {* Used for input and output*}
126 .    unsigned int symcount;
127 .
128 .    {* Symbol table for output BFD (with symcount entries) *}
129 .    struct symbol_cache_entry  **outsymbols;             
130 .
131 .    {* Pointer to structure which contains architecture information*}
132 .    const struct bfd_arch_info *arch_info;
133 .
134 .    {* Stuff only useful for archives:*}
135 .    PTR arelt_data;              
136 .    struct _bfd *my_archive;     {* The containing archive BFD.  *}
137 .    struct _bfd *next;           {* The next BFD in the archive.  *}
138 .    struct _bfd *archive_head;   {* The first BFD in the archive.  *}
139 .    boolean has_armap;           
140 .
141 .    {* A chain of BFD structures involved in a link.  *}
142 .    struct _bfd *link_next;
143 .
144 .    {* A field used by _bfd_generic_link_add_archive_symbols.  This will
145 .       be used only for archive elements.  *}
146 .    int archive_pass;
147 .
148 .    {* Used by the back end to hold private data. *}
149 .
150 .    union 
151 .      {
152 .      struct aout_data_struct *aout_data;
153 .      struct artdata *aout_ar_data;
154 .      struct _oasys_data *oasys_obj_data;
155 .      struct _oasys_ar_data *oasys_ar_data;
156 .      struct coff_tdata *coff_obj_data;
157 .      struct pe_tdata *pe_obj_data;
158 .      struct xcoff_tdata *xcoff_obj_data;
159 .      struct ecoff_tdata *ecoff_obj_data;
160 .      struct ieee_data_struct *ieee_data;
161 .      struct ieee_ar_data_struct *ieee_ar_data;
162 .      struct srec_data_struct *srec_data;
163 .      struct ihex_data_struct *ihex_data;
164 .      struct tekhex_data_struct *tekhex_data;
165 .      struct elf_obj_tdata *elf_obj_data;
166 .      struct nlm_obj_tdata *nlm_obj_data;
167 .      struct bout_data_struct *bout_data;
168 .      struct sun_core_struct *sun_core_data;
169 .      struct trad_core_struct *trad_core_data;
170 .      struct som_data_struct *som_data;
171 .      struct hpux_core_struct *hpux_core_data;
172 .      struct hppabsd_core_struct *hppabsd_core_data;
173 .      struct sgi_core_struct *sgi_core_data;
174 .      struct lynx_core_struct *lynx_core_data;
175 .      struct osf_core_struct *osf_core_data;
176 .      struct cisco_core_struct *cisco_core_data;
177 .      struct versados_data_struct *versados_data;
178 .      struct netbsd_core_struct *netbsd_core_data;
179 .      PTR any;
180 .      } tdata;
181 .  
182 .    {* Used by the application to hold private data*}
183 .    PTR usrdata;
184 .
185 .    {* Where all the allocated stuff under this BFD goes *}
186 .    struct obstack memory;
187 .};
188 .
189 */
190
191 #include "bfd.h"
192 #include "sysdep.h"
193
194 #ifdef ANSI_PROTOTYPES
195 #include <stdarg.h>
196 #else
197 #include <varargs.h>
198 #endif
199
200 #include "bfdlink.h"
201 #include "libbfd.h"
202 #include "coff/internal.h"
203 #include "coff/sym.h"
204 #include "libcoff.h"
205 #include "libecoff.h"
206 #undef obj_symbols
207 #include "elf-bfd.h"
208
209 #include <ctype.h>
210 \f
211 /* provide storage for subsystem, stack and heap data which may have been
212    passed in on the command line.  Ld puts this data into a bfd_link_info
213    struct which ultimately gets passed in to the bfd.  When it arrives, copy
214    it to the following struct so that the data will be available in coffcode.h
215    where it is needed.  The typedef's used are defined in bfd.h */
216
217
218 \f
219 /*
220 SECTION
221         Error reporting
222
223         Most BFD functions return nonzero on success (check their
224         individual documentation for precise semantics).  On an error,
225         they call <<bfd_set_error>> to set an error condition that callers
226         can check by calling <<bfd_get_error>>.
227         If that returns <<bfd_error_system_call>>, then check
228         <<errno>>.
229
230         The easiest way to report a BFD error to the user is to
231         use <<bfd_perror>>.
232
233 SUBSECTION
234         Type <<bfd_error_type>>
235
236         The values returned by <<bfd_get_error>> are defined by the
237         enumerated type <<bfd_error_type>>.
238
239 CODE_FRAGMENT
240 .
241 .typedef enum bfd_error
242 .{
243 .  bfd_error_no_error = 0,
244 .  bfd_error_system_call,
245 .  bfd_error_invalid_target,
246 .  bfd_error_wrong_format,
247 .  bfd_error_invalid_operation,
248 .  bfd_error_no_memory,
249 .  bfd_error_no_symbols,
250 .  bfd_error_no_armap,
251 .  bfd_error_no_more_archived_files,
252 .  bfd_error_malformed_archive,
253 .  bfd_error_file_not_recognized,
254 .  bfd_error_file_ambiguously_recognized,
255 .  bfd_error_no_contents,
256 .  bfd_error_nonrepresentable_section,
257 .  bfd_error_no_debug_section,
258 .  bfd_error_bad_value,
259 .  bfd_error_file_truncated,
260 .  bfd_error_file_too_big,
261 .  bfd_error_invalid_error_code
262 .} bfd_error_type;
263 .
264 */
265
266 #undef strerror
267 extern char *strerror();
268
269 static bfd_error_type bfd_error = bfd_error_no_error;
270
271 CONST char *CONST bfd_errmsgs[] = {
272                         "No error",
273                         "System call error",
274                         "Invalid bfd target",
275                         "File in wrong format",
276                         "Invalid operation",
277                         "Memory exhausted",
278                         "No symbols",
279                         "Archive has no index; run ranlib to add one",
280                         "No more archived files",
281                         "Malformed archive",
282                         "File format not recognized",
283                         "File format is ambiguous",
284                         "Section has no contents",
285                         "Nonrepresentable section on output",
286                         "Symbol needs debug section which does not exist",
287                         "Bad value",
288                         "File truncated",
289                         "File too big",
290                         "#<Invalid error code>"
291                        };
292
293 /*
294 FUNCTION
295         bfd_get_error
296
297 SYNOPSIS
298         bfd_error_type bfd_get_error (void);
299
300 DESCRIPTION
301         Return the current BFD error condition.
302 */
303
304 bfd_error_type
305 bfd_get_error ()
306 {
307   return bfd_error;
308 }
309
310 /*
311 FUNCTION
312         bfd_set_error
313
314 SYNOPSIS
315         void bfd_set_error (bfd_error_type error_tag);
316
317 DESCRIPTION
318         Set the BFD error condition to be @var{error_tag}.
319 */
320
321 void
322 bfd_set_error (error_tag)
323      bfd_error_type error_tag;
324 {
325   bfd_error = error_tag;
326 }
327
328 /*
329 FUNCTION
330         bfd_errmsg
331
332 SYNOPSIS
333         CONST char *bfd_errmsg (bfd_error_type error_tag);
334
335 DESCRIPTION
336         Return a string describing the error @var{error_tag}, or
337         the system error if @var{error_tag} is <<bfd_error_system_call>>.
338 */
339
340 CONST char *
341 bfd_errmsg (error_tag)
342      bfd_error_type error_tag;
343 {
344 #ifndef errno
345   extern int errno;
346 #endif
347   if (error_tag == bfd_error_system_call)
348     return strerror (errno);
349
350   if ((((int)error_tag <(int) bfd_error_no_error) ||
351        ((int)error_tag > (int)bfd_error_invalid_error_code)))
352     error_tag = bfd_error_invalid_error_code;/* sanity check */
353
354   return bfd_errmsgs [(int)error_tag];
355 }
356
357 /*
358 FUNCTION
359         bfd_perror
360
361 SYNOPSIS
362         void bfd_perror (CONST char *message);
363
364 DESCRIPTION
365         Print to the standard error stream a string describing the
366         last BFD error that occurred, or the last system error if
367         the last BFD error was a system call failure.  If @var{message}
368         is non-NULL and non-empty, the error string printed is preceded
369         by @var{message}, a colon, and a space.  It is followed by a newline.
370 */
371
372 void
373 bfd_perror (message)
374      CONST char *message;
375 {
376   if (bfd_get_error () == bfd_error_system_call)
377     perror((char *)message);            /* must be system error then... */
378   else {
379     if (message == NULL || *message == '\0')
380       fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
381     else
382       fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
383   }
384 }
385
386 /*
387 SUBSECTION
388         BFD error handler
389
390         Some BFD functions want to print messages describing the
391         problem.  They call a BFD error handler function.  This
392         function may be overriden by the program.
393
394         The BFD error handler acts like printf.
395
396 CODE_FRAGMENT
397 .
398 .typedef void (*bfd_error_handler_type) PARAMS ((const char *, ...));
399 .
400 */
401
402 /* The program name used when printing BFD error messages.  */
403
404 static const char *_bfd_error_program_name;
405
406 /* This is the default routine to handle BFD error messages.  */
407
408 #ifdef ANSI_PROTOTYPES
409
410 static void _bfd_default_error_handler PARAMS ((const char *s, ...));
411
412 static void
413 _bfd_default_error_handler (const char *s, ...)
414 {
415   va_list p;
416
417   if (_bfd_error_program_name != NULL)
418     fprintf (stderr, "%s: ", _bfd_error_program_name);
419
420   va_start (p, s);
421
422   vfprintf (stderr, s, p);
423
424   va_end (p);
425
426   fprintf (stderr, "\n");
427 }
428
429 #else /* ! defined (ANSI_PROTOTYPES) */
430
431 static void _bfd_default_error_handler ();
432
433 static void
434 _bfd_default_error_handler (va_alist)
435      va_dcl
436 {
437   va_list p;
438   const char *s;
439
440   if (_bfd_error_program_name != NULL)
441     fprintf (stderr, "%s: ", _bfd_error_program_name);
442
443   va_start (p);
444
445   s = va_arg (p, const char *);
446   vfprintf (stderr, s, p);
447
448   va_end (p);
449
450   fprintf (stderr, "\n");
451 }
452
453 #endif /* ! defined (ANSI_PROTOTYPES) */
454
455 /* This is a function pointer to the routine which should handle BFD
456    error messages.  It is called when a BFD routine encounters an
457    error for which it wants to print a message.  Going through a
458    function pointer permits a program linked against BFD to intercept
459    the messages and deal with them itself.  */
460
461 bfd_error_handler_type _bfd_error_handler = _bfd_default_error_handler;
462
463 /*
464 FUNCTION
465         bfd_set_error_handler
466
467 SYNOPSIS
468         bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
469
470 DESCRIPTION
471         Set the BFD error handler function.  Returns the previous
472         function.
473 */
474
475 bfd_error_handler_type
476 bfd_set_error_handler (pnew)
477      bfd_error_handler_type pnew;
478 {
479   bfd_error_handler_type pold;
480
481   pold = _bfd_error_handler;
482   _bfd_error_handler = pnew;
483   return pold;
484 }
485
486 /*
487 FUNCTION
488         bfd_set_error_program_name
489
490 SYNOPSIS
491         void bfd_set_error_program_name (const char *);
492
493 DESCRIPTION
494         Set the program name to use when printing a BFD error.  This
495         is printed before the error message followed by a colon and
496         space.  The string must not be changed after it is passed to
497         this function.
498 */
499
500 void
501 bfd_set_error_program_name (name)
502      const char *name;
503 {
504   _bfd_error_program_name = name;
505 }
506 \f
507 /*
508 SECTION
509         Symbols
510 */
511
512 /*
513 FUNCTION
514         bfd_get_reloc_upper_bound
515
516 SYNOPSIS
517         long bfd_get_reloc_upper_bound(bfd *abfd, asection *sect);
518
519 DESCRIPTION
520         Return the number of bytes required to store the
521         relocation information associated with section @var{sect}
522         attached to bfd @var{abfd}.  If an error occurs, return -1.
523
524 */
525
526
527 long
528 bfd_get_reloc_upper_bound (abfd, asect)
529      bfd *abfd;
530      sec_ptr asect;
531 {
532   if (abfd->format != bfd_object) {
533     bfd_set_error (bfd_error_invalid_operation);
534     return -1;
535   }
536
537   return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
538 }
539
540 /*
541 FUNCTION
542         bfd_canonicalize_reloc
543
544 SYNOPSIS
545         long bfd_canonicalize_reloc
546                 (bfd *abfd,
547                 asection *sec,
548                 arelent **loc,
549                 asymbol **syms);
550
551 DESCRIPTION
552         Call the back end associated with the open BFD
553         @var{abfd} and translate the external form of the relocation
554         information attached to @var{sec} into the internal canonical
555         form.  Place the table into memory at @var{loc}, which has
556         been preallocated, usually by a call to
557         <<bfd_get_reloc_upper_bound>>.  Returns the number of relocs, or
558         -1 on error.
559
560         The @var{syms} table is also needed for horrible internal magic
561         reasons.
562
563
564 */
565 long
566 bfd_canonicalize_reloc (abfd, asect, location, symbols)
567      bfd *abfd;
568      sec_ptr asect;
569      arelent **location;
570      asymbol **symbols;
571 {
572   if (abfd->format != bfd_object) {
573     bfd_set_error (bfd_error_invalid_operation);
574     return -1;
575   }
576   return BFD_SEND (abfd, _bfd_canonicalize_reloc,
577                    (abfd, asect, location, symbols));
578 }
579
580 /*
581 FUNCTION
582         bfd_set_reloc
583
584 SYNOPSIS
585         void bfd_set_reloc
586           (bfd *abfd, asection *sec, arelent **rel, unsigned int count)
587
588 DESCRIPTION
589         Set the relocation pointer and count within
590         section @var{sec} to the values @var{rel} and @var{count}.
591         The argument @var{abfd} is ignored.
592
593 */
594 /*ARGSUSED*/
595 void
596 bfd_set_reloc (ignore_abfd, asect, location, count)
597      bfd *ignore_abfd;
598      sec_ptr asect;
599      arelent **location;
600      unsigned int count;
601 {
602   asect->orelocation  = location;
603   asect->reloc_count = count;
604 }
605
606 /*
607 FUNCTION
608         bfd_set_file_flags
609
610 SYNOPSIS
611         boolean bfd_set_file_flags(bfd *abfd, flagword flags);
612
613 DESCRIPTION
614         Set the flag word in the BFD @var{abfd} to the value @var{flags}.
615
616         Possible errors are:
617         o <<bfd_error_wrong_format>> - The target bfd was not of object format.
618         o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
619         o <<bfd_error_invalid_operation>> -
620         The flag word contained a bit which was not applicable to the
621         type of file.  E.g., an attempt was made to set the <<D_PAGED>> bit
622         on a BFD format which does not support demand paging.
623
624 */
625
626 boolean
627 bfd_set_file_flags (abfd, flags)
628      bfd *abfd;
629      flagword flags;
630 {
631   if (abfd->format != bfd_object) {
632     bfd_set_error (bfd_error_wrong_format);
633     return false;
634   }
635
636   if (bfd_read_p (abfd)) {
637     bfd_set_error (bfd_error_invalid_operation);
638     return false;
639   }
640
641   bfd_get_file_flags (abfd) = flags;
642   if ((flags & bfd_applicable_file_flags (abfd)) != flags) {
643     bfd_set_error (bfd_error_invalid_operation);
644     return false;
645   }
646
647 return true;
648 }
649
650 void
651 bfd_assert (file, line)
652      const char *file;
653      int line;
654 {
655   (*_bfd_error_handler) ("bfd assertion fail %s:%d", file, line);
656 }
657
658
659 /*
660 FUNCTION
661         bfd_set_start_address
662
663 SYNOPSIS
664         boolean bfd_set_start_address(bfd *abfd, bfd_vma vma);
665
666 DESCRIPTION
667         Make @var{vma} the entry point of output BFD @var{abfd}.
668
669 RETURNS
670         Returns <<true>> on success, <<false>> otherwise.
671 */
672
673 boolean
674 bfd_set_start_address(abfd, vma)
675 bfd *abfd;
676 bfd_vma vma;
677 {
678   abfd->start_address = vma;
679   return true;
680 }
681
682
683 /*
684 FUNCTION
685         bfd_get_mtime
686
687 SYNOPSIS
688         long bfd_get_mtime(bfd *abfd);
689
690 DESCRIPTION
691         Return the file modification time (as read from the file system, or
692         from the archive header for archive members).
693
694 */
695
696 long
697 bfd_get_mtime (abfd)
698      bfd *abfd;
699 {
700   FILE *fp;
701   struct stat buf;
702
703   if (abfd->mtime_set)
704     return abfd->mtime;
705
706   fp = bfd_cache_lookup (abfd);
707   if (0 != fstat (fileno (fp), &buf))
708     return 0;
709
710   abfd->mtime = buf.st_mtime;           /* Save value in case anyone wants it */
711   return buf.st_mtime;
712 }
713
714 /*
715 FUNCTION
716         bfd_get_size
717
718 SYNOPSIS
719         long bfd_get_size(bfd *abfd);
720
721 DESCRIPTION
722         Return the file size (as read from file system) for the file
723         associated with BFD @var{abfd}.
724
725         The initial motivation for, and use of, this routine is not
726         so we can get the exact size of the object the BFD applies to, since
727         that might not be generally possible (archive members for example).
728         It would be ideal if someone could eventually modify
729         it so that such results were guaranteed.
730
731         Instead, we want to ask questions like "is this NNN byte sized
732         object I'm about to try read from file offset YYY reasonable?"
733         As as example of where we might do this, some object formats
734         use string tables for which the first <<sizeof(long)>> bytes of the
735         table contain the size of the table itself, including the size bytes.
736         If an application tries to read what it thinks is one of these
737         string tables, without some way to validate the size, and for
738         some reason the size is wrong (byte swapping error, wrong location
739         for the string table, etc.), the only clue is likely to be a read
740         error when it tries to read the table, or a "virtual memory
741         exhausted" error when it tries to allocate 15 bazillon bytes
742         of space for the 15 bazillon byte table it is about to read.
743         This function at least allows us to answer the quesion, "is the
744         size reasonable?".
745 */
746
747 long
748 bfd_get_size (abfd)
749      bfd *abfd;
750 {
751   FILE *fp;
752   struct stat buf;
753
754   if ((abfd->flags & BFD_IN_MEMORY) != 0)
755     return ((struct bfd_in_memory *) abfd->iostream)->size;
756
757   fp = bfd_cache_lookup (abfd);
758   if (0 != fstat (fileno (fp), &buf))
759     return 0;
760
761   return buf.st_size;
762 }
763
764 /*
765 FUNCTION
766         bfd_get_gp_size
767
768 SYNOPSIS
769         int bfd_get_gp_size(bfd *abfd);
770
771 DESCRIPTION
772         Return the maximum size of objects to be optimized using the GP
773         register under MIPS ECOFF.  This is typically set by the <<-G>>
774         argument to the compiler, assembler or linker.
775 */
776
777 int
778 bfd_get_gp_size (abfd)
779      bfd *abfd;
780 {
781   if (abfd->format == bfd_object)
782     {
783       if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
784         return ecoff_data (abfd)->gp_size;
785       else if (abfd->xvec->flavour == bfd_target_elf_flavour)
786         return elf_gp_size (abfd);
787     }
788   return 0;
789 }
790
791 /*
792 FUNCTION
793         bfd_set_gp_size
794
795 SYNOPSIS
796         void bfd_set_gp_size(bfd *abfd, int i);
797
798 DESCRIPTION
799         Set the maximum size of objects to be optimized using the GP
800         register under ECOFF or MIPS ELF.  This is typically set by
801         the <<-G>> argument to the compiler, assembler or linker.
802 */
803
804 void
805 bfd_set_gp_size (abfd, i)
806      bfd *abfd;
807      int i;
808 {
809   /* Don't try to set GP size on an archive or core file! */
810   if (abfd->format != bfd_object)
811     return;
812   if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
813     ecoff_data (abfd)->gp_size = i;
814   else if (abfd->xvec->flavour == bfd_target_elf_flavour)
815     elf_gp_size (abfd) = i;
816 }
817
818 /* Get the GP value.  This is an internal function used by some of the
819    relocation special_function routines on targets which support a GP
820    register.  */
821
822 bfd_vma
823 _bfd_get_gp_value (abfd)
824      bfd *abfd;
825 {
826   if (abfd->format == bfd_object)
827     {
828       if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
829         return ecoff_data (abfd)->gp;
830       else if (abfd->xvec->flavour == bfd_target_elf_flavour)
831         return elf_gp (abfd);
832     }
833   return 0;
834 }
835
836 /* Set the GP value.  */
837
838 void
839 _bfd_set_gp_value (abfd, v)
840      bfd *abfd;
841      bfd_vma v;
842 {
843   if (abfd->format != bfd_object)
844     return;
845   if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
846     ecoff_data (abfd)->gp = v;
847   else if (abfd->xvec->flavour == bfd_target_elf_flavour)
848     elf_gp (abfd) = v;
849 }
850
851 /*
852 FUNCTION
853         bfd_scan_vma
854
855 SYNOPSIS
856         bfd_vma bfd_scan_vma(CONST char *string, CONST char **end, int base);
857
858 DESCRIPTION
859         Convert, like <<strtoul>>, a numerical expression
860         @var{string} into a <<bfd_vma>> integer, and return that integer.
861         (Though without as many bells and whistles as <<strtoul>>.)
862         The expression is assumed to be unsigned (i.e., positive).
863         If given a @var{base}, it is used as the base for conversion.
864         A base of 0 causes the function to interpret the string
865         in hex if a leading "0x" or "0X" is found, otherwise
866         in octal if a leading zero is found, otherwise in decimal.
867
868         Overflow is not detected.
869 */
870
871 bfd_vma
872 bfd_scan_vma (string, end, base)
873      CONST char *string;
874      CONST char **end;
875      int base;
876 {
877   bfd_vma value;
878   int digit;
879
880   /* Let the host do it if possible.  */
881   if (sizeof(bfd_vma) <= sizeof(unsigned long))
882     return (bfd_vma) strtoul (string, (char **) end, base);
883
884   /* A negative base makes no sense, and we only need to go as high as hex.  */
885   if ((base < 0) || (base > 16))
886     return (bfd_vma) 0;
887
888   if (base == 0)
889     {
890       if (string[0] == '0')
891         {
892           if ((string[1] == 'x') || (string[1] == 'X'))
893             base = 16;
894           /* XXX should we also allow "0b" or "0B" to set base to 2? */
895           else
896             base = 8;
897         }
898       else
899         base = 10;
900     }
901   if ((base == 16) &&
902       (string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X')))
903     string += 2;
904   /* XXX should we also skip over "0b" or "0B" if base is 2? */
905     
906 /* Speed could be improved with a table like hex_value[] in gas.  */
907 #define HEX_VALUE(c) \
908   (isxdigit(c) ?                                \
909     (isdigit(c) ?                               \
910       (c - '0') :                               \
911       (10 + c - (islower(c) ? 'a' : 'A'))) :    \
912     42)
913
914   for (value = 0; (digit = HEX_VALUE(*string)) < base; string++)
915     {
916       value = value * base + digit;
917     }
918
919   if (end)
920     *end = string;
921
922   return value;
923 }
924
925 /*
926 FUNCTION
927         bfd_copy_private_bfd_data
928
929 SYNOPSIS
930         boolean bfd_copy_private_bfd_data(bfd *ibfd, bfd *obfd);
931
932 DESCRIPTION
933         Copy private BFD information from the BFD @var{ibfd} to the 
934         the BFD @var{obfd}.  Return <<true>> on success, <<false>> on error.
935         Possible error returns are:
936
937         o <<bfd_error_no_memory>> -
938         Not enough memory exists to create private data for @var{obfd}.
939
940 .#define bfd_copy_private_bfd_data(ibfd, obfd) \
941 .     BFD_SEND (ibfd, _bfd_copy_private_bfd_data, \
942 .               (ibfd, obfd))
943
944 */
945
946 /*
947 FUNCTION
948         bfd_merge_private_bfd_data
949
950 SYNOPSIS
951         boolean bfd_merge_private_bfd_data(bfd *ibfd, bfd *obfd);
952
953 DESCRIPTION
954         Merge private BFD information from the BFD @var{ibfd} to the 
955         the output file BFD @var{obfd} when linking.  Return <<true>>
956         on success, <<false>> on error.  Possible error returns are:
957
958         o <<bfd_error_no_memory>> -
959         Not enough memory exists to create private data for @var{obfd}.
960
961 .#define bfd_merge_private_bfd_data(ibfd, obfd) \
962 .     BFD_SEND (ibfd, _bfd_merge_private_bfd_data, \
963 .               (ibfd, obfd))
964
965 */
966
967 /*
968 FUNCTION
969         bfd_set_private_flags
970
971 SYNOPSIS
972         boolean bfd_set_private_flags(bfd *abfd, flagword flags);
973
974 DESCRIPTION
975         Set private BFD flag information in the BFD @var{abfd}.
976         Return <<true>> on success, <<false>> on error.  Possible error
977         returns are:
978
979         o <<bfd_error_no_memory>> -
980         Not enough memory exists to create private data for @var{obfd}.
981
982 .#define bfd_set_private_flags(abfd, flags) \
983 .     BFD_SEND (abfd, _bfd_set_private_flags, \
984 .               (abfd, flags))
985
986 */
987
988 /*
989 FUNCTION
990         stuff
991
992 DESCRIPTION
993         Stuff which should be documented:
994
995 .#define bfd_sizeof_headers(abfd, reloc) \
996 .     BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc))
997 .
998 .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
999 .     BFD_SEND (abfd, _bfd_find_nearest_line,  (abfd, sec, syms, off, file, func, line))
1000 .
1001 .       {* Do these three do anything useful at all, for any back end?  *}
1002 .#define bfd_debug_info_start(abfd) \
1003 .        BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
1004 .
1005 .#define bfd_debug_info_end(abfd) \
1006 .        BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
1007 .
1008 .#define bfd_debug_info_accumulate(abfd, section) \
1009 .        BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
1010 .
1011 .
1012 .#define bfd_stat_arch_elt(abfd, stat) \
1013 .        BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
1014 .
1015 .#define bfd_update_armap_timestamp(abfd) \
1016 .        BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
1017 .
1018 .#define bfd_set_arch_mach(abfd, arch, mach)\
1019 .        BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
1020 .
1021 .#define bfd_relax_section(abfd, section, link_info, again) \
1022 .       BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
1023 .
1024 .#define bfd_link_hash_table_create(abfd) \
1025 .       BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
1026 .
1027 .#define bfd_link_add_symbols(abfd, info) \
1028 .       BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
1029 .
1030 .#define bfd_final_link(abfd, info) \
1031 .       BFD_SEND (abfd, _bfd_final_link, (abfd, info))
1032 .
1033 .#define bfd_free_cached_info(abfd) \
1034 .       BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
1035 .
1036 .#define bfd_get_dynamic_symtab_upper_bound(abfd) \
1037 .       BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
1038 .
1039 .#define bfd_print_private_bfd_data(abfd, file)\
1040 .       BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
1041 .
1042 .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
1043 .       BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
1044 .
1045 .#define bfd_get_dynamic_reloc_upper_bound(abfd) \
1046 .       BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
1047 .
1048 .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
1049 .       BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
1050 .
1051 .extern bfd_byte *bfd_get_relocated_section_contents
1052 .       PARAMS ((bfd *, struct bfd_link_info *,
1053 .                 struct bfd_link_order *, bfd_byte *,
1054 .                 boolean, asymbol **));
1055 .
1056
1057 */
1058
1059 bfd_byte *
1060 bfd_get_relocated_section_contents (abfd, link_info, link_order, data,
1061                                     relocateable, symbols)
1062      bfd *abfd;
1063      struct bfd_link_info *link_info;
1064      struct bfd_link_order *link_order;
1065      bfd_byte *data;
1066      boolean relocateable;
1067      asymbol **symbols;
1068 {
1069   bfd *abfd2;
1070   bfd_byte *(*fn) PARAMS ((bfd *, struct bfd_link_info *,
1071                            struct bfd_link_order *, bfd_byte *, boolean,
1072                            asymbol **));
1073
1074   if (link_order->type == bfd_indirect_link_order)
1075     {
1076       abfd2 = link_order->u.indirect.section->owner;
1077       if (abfd2 == 0)
1078         abfd2 = abfd;
1079     }
1080   else
1081     abfd2 = abfd;
1082   fn = abfd2->xvec->_bfd_get_relocated_section_contents;
1083
1084   return (*fn) (abfd, link_info, link_order, data, relocateable, symbols);
1085 }
1086
1087 /* Record information about an ELF program header.  */
1088
1089 boolean
1090 bfd_record_phdr (abfd, type, flags_valid, flags, at_valid, at,
1091                  includes_filehdr, includes_phdrs, count, secs)
1092      bfd *abfd;
1093      unsigned long type;
1094      boolean flags_valid;
1095      flagword flags;
1096      boolean at_valid;
1097      bfd_vma at;
1098      boolean includes_filehdr;
1099      boolean includes_phdrs;
1100      unsigned int count;
1101      asection **secs;
1102 {
1103   struct elf_segment_map *m, **pm;
1104
1105   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1106     return true;
1107
1108   m = ((struct elf_segment_map *)
1109        bfd_alloc (abfd,
1110                   (sizeof (struct elf_segment_map)
1111                    + (count - 1) * sizeof (asection *))));
1112   if (m == NULL)
1113     return false;
1114
1115   m->next = NULL;
1116   m->p_type = type;
1117   m->p_flags = flags;
1118   m->p_paddr = at;
1119   m->p_flags_valid = flags_valid;
1120   m->p_paddr_valid = at_valid;
1121   m->includes_filehdr = includes_filehdr;
1122   m->includes_phdrs = includes_phdrs;
1123   m->count = count;
1124   if (count > 0)
1125     memcpy (m->sections, secs, count * sizeof (asection *));
1126
1127   for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
1128     ;
1129   *pm = m;
1130
1131   return true;
1132 }