bfd.c revision 38889
1260684Skaiw/* Generic BFD library interface and support routines.
2260684Skaiw   Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 1998
3260684Skaiw   Free Software Foundation, Inc.
4260684Skaiw   Written by Cygnus Support.
5260684Skaiw
6260684SkaiwThis file is part of BFD, the Binary File Descriptor library.
7260684Skaiw
8260684SkaiwThis program is free software; you can redistribute it and/or modify
9260684Skaiwit under the terms of the GNU General Public License as published by
10260684Skaiwthe Free Software Foundation; either version 2 of the License, or
11260684Skaiw(at your option) any later version.
12260684Skaiw
13260684SkaiwThis program is distributed in the hope that it will be useful,
14260684Skaiwbut WITHOUT ANY WARRANTY; without even the implied warranty of
15260684SkaiwMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16260684SkaiwGNU General Public License for more details.
17260684Skaiw
18260684SkaiwYou should have received a copy of the GNU General Public License
19260684Skaiwalong with this program; if not, write to the Free Software
20260684SkaiwFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21260684Skaiw
22260684Skaiw/*
23260684SkaiwSECTION
24260684Skaiw	<<typedef bfd>>
25260684Skaiw
26260684Skaiw	A BFD has type <<bfd>>; objects of this type are the
27260684Skaiw	cornerstone of any application using BFD. Using BFD
28260684Skaiw	consists of making references though the BFD and to data in the BFD.
29260684Skaiw
30260684Skaiw	Here is the structure that defines the type <<bfd>>.  It
31260684Skaiw	contains the major data about the file and pointers
32260684Skaiw	to the rest of the data.
33260684Skaiw
34260684SkaiwCODE_FRAGMENT
35260684Skaiw.
36320685Semaste.struct _bfd
37260684Skaiw.{
38260684Skaiw.    {* The filename the application opened the BFD with.  *}
39260684Skaiw.    CONST char *filename;
40260684Skaiw.
41260684Skaiw.    {* A pointer to the target jump table.             *}
42260684Skaiw.    const struct bfd_target *xvec;
43260684Skaiw.
44260684Skaiw.    {* To avoid dragging too many header files into every file that
45260684Skaiw.       includes `<<bfd.h>>', IOSTREAM has been declared as a "char
46260684Skaiw.       *", and MTIME as a "long".  Their correct types, to which they
47260684Skaiw.       are cast when used, are "FILE *" and "time_t".    The iostream
48260684Skaiw.       is the result of an fopen on the filename.  However, if the
49260684Skaiw.       BFD_IN_MEMORY flag is set, then iostream is actually a pointer
50260684Skaiw.       to a bfd_in_memory struct.  *}
51260684Skaiw.    PTR iostream;
52260684Skaiw.
53260684Skaiw.    {* Is the file descriptor being cached?  That is, can it be closed as
54260684Skaiw.       needed, and re-opened when accessed later?  *}
55260684Skaiw.
56260684Skaiw.    boolean cacheable;
57260684Skaiw.
58260684Skaiw.    {* Marks whether there was a default target specified when the
59260684Skaiw.       BFD was opened. This is used to select which matching algorithm
60260684Skaiw.       to use to choose the back end. *}
61260684Skaiw.
62260684Skaiw.    boolean target_defaulted;
63260684Skaiw.
64260684Skaiw.    {* The caching routines use these to maintain a
65260684Skaiw.       least-recently-used list of BFDs *}
66260684Skaiw.
67260684Skaiw.    struct _bfd *lru_prev, *lru_next;
68260684Skaiw.
69260684Skaiw.    {* When a file is closed by the caching routines, BFD retains
70260684Skaiw.       state information on the file here: *}
71260684Skaiw.
72260684Skaiw.    file_ptr where;
73260684Skaiw.
74260684Skaiw.    {* and here: (``once'' means at least once) *}
75260684Skaiw.
76260684Skaiw.    boolean opened_once;
77260684Skaiw.
78260684Skaiw.    {* Set if we have a locally maintained mtime value, rather than
79260684Skaiw.       getting it from the file each time: *}
80260684Skaiw.
81260684Skaiw.    boolean mtime_set;
82260684Skaiw.
83260684Skaiw.    {* File modified time, if mtime_set is true: *}
84260684Skaiw.
85260684Skaiw.    long mtime;
86260684Skaiw.
87260684Skaiw.    {* Reserved for an unimplemented file locking extension.*}
88260684Skaiw.
89260684Skaiw.    int ifd;
90260684Skaiw.
91260684Skaiw.    {* The format which belongs to the BFD. (object, core, etc.) *}
92260684Skaiw.
93260684Skaiw.    bfd_format format;
94260684Skaiw.
95260684Skaiw.    {* The direction the BFD was opened with*}
96260684Skaiw.
97260684Skaiw.    enum bfd_direction {no_direction = 0,
98260684Skaiw.                        read_direction = 1,
99260684Skaiw.                        write_direction = 2,
100260684Skaiw.                        both_direction = 3} direction;
101260684Skaiw.
102260684Skaiw.    {* Format_specific flags*}
103260684Skaiw.
104260684Skaiw.    flagword flags;
105260684Skaiw.
106260684Skaiw.    {* Currently my_archive is tested before adding origin to
107260684Skaiw.       anything. I believe that this can become always an add of
108260684Skaiw.       origin, with origin set to 0 for non archive files.   *}
109260684Skaiw.
110260684Skaiw.    file_ptr origin;
111260684Skaiw.
112260684Skaiw.    {* Remember when output has begun, to stop strange things
113260684Skaiw.       from happening. *}
114260684Skaiw.    boolean output_has_begun;
115260684Skaiw.
116260684Skaiw.    {* Pointer to linked list of sections*}
117260684Skaiw.    struct sec  *sections;
118260684Skaiw.
119260684Skaiw.    {* The number of sections *}
120260684Skaiw.    unsigned int section_count;
121260684Skaiw.
122260684Skaiw.    {* Stuff only useful for object files:
123260684Skaiw.       The start address. *}
124260684Skaiw.    bfd_vma start_address;
125260684Skaiw.
126260684Skaiw.    {* Used for input and output*}
127260684Skaiw.    unsigned int symcount;
128260684Skaiw.
129260684Skaiw.    {* Symbol table for output BFD (with symcount entries) *}
130260684Skaiw.    struct symbol_cache_entry  **outsymbols;
131260684Skaiw.
132260684Skaiw.    {* Pointer to structure which contains architecture information*}
133260684Skaiw.    const struct bfd_arch_info *arch_info;
134260684Skaiw.
135260684Skaiw.    {* Stuff only useful for archives:*}
136260684Skaiw.    PTR arelt_data;
137260684Skaiw.    struct _bfd *my_archive;     {* The containing archive BFD.  *}
138260684Skaiw.    struct _bfd *next;           {* The next BFD in the archive.  *}
139260684Skaiw.    struct _bfd *archive_head;   {* The first BFD in the archive.  *}
140260684Skaiw.    boolean has_armap;
141260684Skaiw.
142260684Skaiw.    {* A chain of BFD structures involved in a link.  *}
143260684Skaiw.    struct _bfd *link_next;
144260684Skaiw.
145260684Skaiw.    {* A field used by _bfd_generic_link_add_archive_symbols.  This will
146260684Skaiw.       be used only for archive elements.  *}
147260684Skaiw.    int archive_pass;
148260684Skaiw.
149260684Skaiw.    {* Used by the back end to hold private data. *}
150260684Skaiw.
151260684Skaiw.    union
152260684Skaiw.      {
153260684Skaiw.      struct aout_data_struct *aout_data;
154260684Skaiw.      struct artdata *aout_ar_data;
155367466Sdim.      struct _oasys_data *oasys_obj_data;
156260684Skaiw.      struct _oasys_ar_data *oasys_ar_data;
157260684Skaiw.      struct coff_tdata *coff_obj_data;
158260684Skaiw.      struct pe_tdata *pe_obj_data;
159260684Skaiw.      struct xcoff_tdata *xcoff_obj_data;
160260684Skaiw.      struct ecoff_tdata *ecoff_obj_data;
161260684Skaiw.      struct ieee_data_struct *ieee_data;
162260684Skaiw.      struct ieee_ar_data_struct *ieee_ar_data;
163260684Skaiw.      struct srec_data_struct *srec_data;
164260684Skaiw.      struct ihex_data_struct *ihex_data;
165260684Skaiw.      struct tekhex_data_struct *tekhex_data;
166260684Skaiw.      struct elf_obj_tdata *elf_obj_data;
167260684Skaiw.      struct nlm_obj_tdata *nlm_obj_data;
168260684Skaiw.      struct bout_data_struct *bout_data;
169260684Skaiw.      struct sun_core_struct *sun_core_data;
170260684Skaiw.      struct trad_core_struct *trad_core_data;
171260684Skaiw.      struct som_data_struct *som_data;
172260684Skaiw.      struct hpux_core_struct *hpux_core_data;
173260684Skaiw.      struct hppabsd_core_struct *hppabsd_core_data;
174260684Skaiw.      struct sgi_core_struct *sgi_core_data;
175260684Skaiw.      struct lynx_core_struct *lynx_core_data;
176260684Skaiw.      struct osf_core_struct *osf_core_data;
177260684Skaiw.      struct cisco_core_struct *cisco_core_data;
178260684Skaiw.      struct versados_data_struct *versados_data;
179260684Skaiw.      struct netbsd_core_struct *netbsd_core_data;
180260684Skaiw.      PTR any;
181260684Skaiw.      } tdata;
182260684Skaiw.
183260684Skaiw.    {* Used by the application to hold private data*}
184260684Skaiw.    PTR usrdata;
185260684Skaiw.
186260684Skaiw.  {* Where all the allocated stuff under this BFD goes.  This is a
187260684Skaiw.     struct objalloc *, but we use PTR to avoid requiring the inclusion of
188260684Skaiw.     objalloc.h.  *}
189260684Skaiw.    PTR memory;
190260684Skaiw.};
191260684Skaiw.
192260684Skaiw*/
193260684Skaiw
194260684Skaiw#include "bfd.h"
195260684Skaiw#include "sysdep.h"
196260684Skaiw
197260684Skaiw#ifdef ANSI_PROTOTYPES
198260684Skaiw#include <stdarg.h>
199260684Skaiw#else
200260684Skaiw#include <varargs.h>
201260684Skaiw#endif
202260684Skaiw
203260684Skaiw#include "libiberty.h"
204260684Skaiw#include "bfdlink.h"
205260684Skaiw#include "libbfd.h"
206260684Skaiw#include "coff/internal.h"
207260684Skaiw#include "coff/sym.h"
208260684Skaiw#include "libcoff.h"
209260684Skaiw#include "libecoff.h"
210260684Skaiw#undef obj_symbols
211260684Skaiw#include "elf-bfd.h"
212260684Skaiw
213260684Skaiw#include <ctype.h>
214260684Skaiw
215260684Skaiw/* provide storage for subsystem, stack and heap data which may have been
216260684Skaiw   passed in on the command line.  Ld puts this data into a bfd_link_info
217260684Skaiw   struct which ultimately gets passed in to the bfd.  When it arrives, copy
218260684Skaiw   it to the following struct so that the data will be available in coffcode.h
219260684Skaiw   where it is needed.  The typedef's used are defined in bfd.h */
220260684Skaiw
221260684Skaiw
222260684Skaiw
223260684Skaiw/*
224260684SkaiwSECTION
225260684Skaiw	Error reporting
226260684Skaiw
227260684Skaiw	Most BFD functions return nonzero on success (check their
228260684Skaiw	individual documentation for precise semantics).  On an error,
229260684Skaiw	they call <<bfd_set_error>> to set an error condition that callers
230260684Skaiw	can check by calling <<bfd_get_error>>.
231260684Skaiw        If that returns <<bfd_error_system_call>>, then check
232260684Skaiw	<<errno>>.
233260684Skaiw
234260684Skaiw	The easiest way to report a BFD error to the user is to
235260684Skaiw	use <<bfd_perror>>.
236260684Skaiw
237260684SkaiwSUBSECTION
238260684Skaiw	Type <<bfd_error_type>>
239260684Skaiw
240260684Skaiw	The values returned by <<bfd_get_error>> are defined by the
241260684Skaiw	enumerated type <<bfd_error_type>>.
242260684Skaiw
243260684SkaiwCODE_FRAGMENT
244260684Skaiw.
245260684Skaiw.typedef enum bfd_error
246260684Skaiw.{
247260684Skaiw.  bfd_error_no_error = 0,
248260684Skaiw.  bfd_error_system_call,
249260684Skaiw.  bfd_error_invalid_target,
250260684Skaiw.  bfd_error_wrong_format,
251260684Skaiw.  bfd_error_invalid_operation,
252260684Skaiw.  bfd_error_no_memory,
253260684Skaiw.  bfd_error_no_symbols,
254260684Skaiw.  bfd_error_no_armap,
255260684Skaiw.  bfd_error_no_more_archived_files,
256367466Sdim.  bfd_error_malformed_archive,
257260684Skaiw.  bfd_error_file_not_recognized,
258260684Skaiw.  bfd_error_file_ambiguously_recognized,
259260684Skaiw.  bfd_error_no_contents,
260260684Skaiw.  bfd_error_nonrepresentable_section,
261260684Skaiw.  bfd_error_no_debug_section,
262260684Skaiw.  bfd_error_bad_value,
263260684Skaiw.  bfd_error_file_truncated,
264260684Skaiw.  bfd_error_file_too_big,
265260684Skaiw.  bfd_error_invalid_error_code
266260684Skaiw.} bfd_error_type;
267260684Skaiw.
268260684Skaiw*/
269260684Skaiw
270260684Skaiwstatic bfd_error_type bfd_error = bfd_error_no_error;
271260684Skaiw
272260684SkaiwCONST char *CONST bfd_errmsgs[] = {
273260684Skaiw                        "No error",
274260684Skaiw                        "System call error",
275260684Skaiw                        "Invalid bfd target",
276260684Skaiw                        "File in wrong format",
277260684Skaiw                        "Invalid operation",
278260684Skaiw                        "Memory exhausted",
279260684Skaiw                        "No symbols",
280260684Skaiw			"Archive has no index; run ranlib to add one",
281260684Skaiw                        "No more archived files",
282260684Skaiw                        "Malformed archive",
283260684Skaiw                        "File format not recognized",
284320685Semaste                        "File format is ambiguous",
285320685Semaste                        "Section has no contents",
286320685Semaste                        "Nonrepresentable section on output",
287320685Semaste			"Symbol needs debug section which does not exist",
288320685Semaste			"Bad value",
289320685Semaste			"File truncated",
290320685Semaste			"File too big",
291320685Semaste                        "#<Invalid error code>"
292320685Semaste                       };
293320685Semaste
294320685Semaste/*
295320685SemasteFUNCTION
296367466Sdim	bfd_get_error
297320685Semaste
298320685SemasteSYNOPSIS
299320685Semaste	bfd_error_type bfd_get_error (void);
300320685Semaste
301320685SemasteDESCRIPTION
302320685Semaste	Return the current BFD error condition.
303320685Semaste*/
304320685Semaste
305320685Semastebfd_error_type
306320685Semastebfd_get_error ()
307320685Semaste{
308320685Semaste  return bfd_error;
309320685Semaste}
310320685Semaste
311320685Semaste/*
312320685SemasteFUNCTION
313320685Semaste	bfd_set_error
314320685Semaste
315320685SemasteSYNOPSIS
316320685Semaste	void bfd_set_error (bfd_error_type error_tag);
317320685Semaste
318320685SemasteDESCRIPTION
319320685Semaste	Set the BFD error condition to be @var{error_tag}.
320320685Semaste*/
321320685Semaste
322320685Semastevoid
323320685Semastebfd_set_error (error_tag)
324320685Semaste     bfd_error_type error_tag;
325260684Skaiw{
326260684Skaiw  bfd_error = error_tag;
327260684Skaiw}
328260684Skaiw
329260684Skaiw/*
330260684SkaiwFUNCTION
331260684Skaiw	bfd_errmsg
332260684Skaiw
333260684SkaiwSYNOPSIS
334260684Skaiw	CONST char *bfd_errmsg (bfd_error_type error_tag);
335260684Skaiw
336260684SkaiwDESCRIPTION
337260684Skaiw	Return a string describing the error @var{error_tag}, or
338260684Skaiw	the system error if @var{error_tag} is <<bfd_error_system_call>>.
339260684Skaiw*/
340260684Skaiw
341260684SkaiwCONST char *
342260684Skaiwbfd_errmsg (error_tag)
343260684Skaiw     bfd_error_type error_tag;
344260684Skaiw{
345260684Skaiw#ifndef errno
346260684Skaiw  extern int errno;
347260684Skaiw#endif
348260684Skaiw  if (error_tag == bfd_error_system_call)
349260684Skaiw    return xstrerror (errno);
350260684Skaiw
351260684Skaiw  if ((((int)error_tag <(int) bfd_error_no_error) ||
352260684Skaiw       ((int)error_tag > (int)bfd_error_invalid_error_code)))
353260684Skaiw    error_tag = bfd_error_invalid_error_code;/* sanity check */
354260684Skaiw
355260684Skaiw  return bfd_errmsgs [(int)error_tag];
356260684Skaiw}
357260684Skaiw
358260684Skaiw/*
359260684SkaiwFUNCTION
360	bfd_perror
361
362SYNOPSIS
363	void bfd_perror (CONST char *message);
364
365DESCRIPTION
366	Print to the standard error stream a string describing the
367	last BFD error that occurred, or the last system error if
368	the last BFD error was a system call failure.  If @var{message}
369	is non-NULL and non-empty, the error string printed is preceded
370	by @var{message}, a colon, and a space.  It is followed by a newline.
371*/
372
373void
374bfd_perror (message)
375     CONST char *message;
376{
377  if (bfd_get_error () == bfd_error_system_call)
378    perror((char *)message);            /* must be system error then... */
379  else {
380    if (message == NULL || *message == '\0')
381      fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
382    else
383      fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
384  }
385}
386
387/*
388SUBSECTION
389	BFD error handler
390
391	Some BFD functions want to print messages describing the
392	problem.  They call a BFD error handler function.  This
393	function may be overriden by the program.
394
395	The BFD error handler acts like printf.
396
397CODE_FRAGMENT
398.
399.typedef void (*bfd_error_handler_type) PARAMS ((const char *, ...));
400.
401*/
402
403/* The program name used when printing BFD error messages.  */
404
405static const char *_bfd_error_program_name;
406
407/* This is the default routine to handle BFD error messages.  */
408
409#ifdef ANSI_PROTOTYPES
410
411static void _bfd_default_error_handler PARAMS ((const char *s, ...));
412
413static void
414_bfd_default_error_handler (const char *s, ...)
415{
416  va_list p;
417
418  if (_bfd_error_program_name != NULL)
419    fprintf (stderr, "%s: ", _bfd_error_program_name);
420  else
421    fprintf (stderr, "BFD: ");
422
423  va_start (p, s);
424
425  vfprintf (stderr, s, p);
426
427  va_end (p);
428
429  fprintf (stderr, "\n");
430}
431
432#else /* ! defined (ANSI_PROTOTYPES) */
433
434static void _bfd_default_error_handler ();
435
436static void
437_bfd_default_error_handler (va_alist)
438     va_dcl
439{
440  va_list p;
441  const char *s;
442
443  if (_bfd_error_program_name != NULL)
444    fprintf (stderr, "%s: ", _bfd_error_program_name);
445  else
446    fprintf (stderr, "BFD: ");
447
448  va_start (p);
449
450  s = va_arg (p, const char *);
451  vfprintf (stderr, s, p);
452
453  va_end (p);
454
455  fprintf (stderr, "\n");
456}
457
458#endif /* ! defined (ANSI_PROTOTYPES) */
459
460/* This is a function pointer to the routine which should handle BFD
461   error messages.  It is called when a BFD routine encounters an
462   error for which it wants to print a message.  Going through a
463   function pointer permits a program linked against BFD to intercept
464   the messages and deal with them itself.  */
465
466bfd_error_handler_type _bfd_error_handler = _bfd_default_error_handler;
467
468/*
469FUNCTION
470	bfd_set_error_handler
471
472SYNOPSIS
473	bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
474
475DESCRIPTION
476	Set the BFD error handler function.  Returns the previous
477	function.
478*/
479
480bfd_error_handler_type
481bfd_set_error_handler (pnew)
482     bfd_error_handler_type pnew;
483{
484  bfd_error_handler_type pold;
485
486  pold = _bfd_error_handler;
487  _bfd_error_handler = pnew;
488  return pold;
489}
490
491/*
492FUNCTION
493	bfd_set_error_program_name
494
495SYNOPSIS
496	void bfd_set_error_program_name (const char *);
497
498DESCRIPTION
499	Set the program name to use when printing a BFD error.  This
500	is printed before the error message followed by a colon and
501	space.  The string must not be changed after it is passed to
502	this function.
503*/
504
505void
506bfd_set_error_program_name (name)
507     const char *name;
508{
509  _bfd_error_program_name = name;
510}
511
512
513/*
514FUNCTION
515	bfd_get_error_handler
516
517SYNOPSIS
518	bfd_error_handler_type bfd_get_error_handler (void);
519
520DESCRIPTION
521	Return the BFD error handler function.
522*/
523
524bfd_error_handler_type
525bfd_get_error_handler ()
526{
527  return _bfd_error_handler;
528}
529
530/*
531SECTION
532	Symbols
533*/
534
535/*
536FUNCTION
537	bfd_get_reloc_upper_bound
538
539SYNOPSIS
540	long bfd_get_reloc_upper_bound(bfd *abfd, asection *sect);
541
542DESCRIPTION
543	Return the number of bytes required to store the
544	relocation information associated with section @var{sect}
545	attached to bfd @var{abfd}.  If an error occurs, return -1.
546
547*/
548
549
550long
551bfd_get_reloc_upper_bound (abfd, asect)
552     bfd *abfd;
553     sec_ptr asect;
554{
555  if (abfd->format != bfd_object) {
556    bfd_set_error (bfd_error_invalid_operation);
557    return -1;
558  }
559
560  return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
561}
562
563/*
564FUNCTION
565	bfd_canonicalize_reloc
566
567SYNOPSIS
568	long bfd_canonicalize_reloc
569        	(bfd *abfd,
570		asection *sec,
571		arelent **loc,
572		asymbol	**syms);
573
574DESCRIPTION
575	Call the back end associated with the open BFD
576	@var{abfd} and translate the external form of the relocation
577	information attached to @var{sec} into the internal canonical
578	form.  Place the table into memory at @var{loc}, which has
579	been preallocated, usually by a call to
580	<<bfd_get_reloc_upper_bound>>.  Returns the number of relocs, or
581	-1 on error.
582
583	The @var{syms} table is also needed for horrible internal magic
584	reasons.
585
586
587*/
588long
589bfd_canonicalize_reloc (abfd, asect, location, symbols)
590     bfd *abfd;
591     sec_ptr asect;
592     arelent **location;
593     asymbol **symbols;
594{
595  if (abfd->format != bfd_object) {
596    bfd_set_error (bfd_error_invalid_operation);
597    return -1;
598  }
599  return BFD_SEND (abfd, _bfd_canonicalize_reloc,
600		   (abfd, asect, location, symbols));
601}
602
603/*
604FUNCTION
605	bfd_set_reloc
606
607SYNOPSIS
608	void bfd_set_reloc
609	  (bfd *abfd, asection *sec, arelent **rel, unsigned int count)
610
611DESCRIPTION
612	Set the relocation pointer and count within
613	section @var{sec} to the values @var{rel} and @var{count}.
614	The argument @var{abfd} is ignored.
615
616*/
617/*ARGSUSED*/
618void
619bfd_set_reloc (ignore_abfd, asect, location, count)
620     bfd *ignore_abfd;
621     sec_ptr asect;
622     arelent **location;
623     unsigned int count;
624{
625  asect->orelocation = location;
626  asect->reloc_count = count;
627}
628
629/*
630FUNCTION
631	bfd_set_file_flags
632
633SYNOPSIS
634	boolean bfd_set_file_flags(bfd *abfd, flagword flags);
635
636DESCRIPTION
637	Set the flag word in the BFD @var{abfd} to the value @var{flags}.
638
639	Possible errors are:
640	o <<bfd_error_wrong_format>> - The target bfd was not of object format.
641	o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
642	o <<bfd_error_invalid_operation>> -
643	The flag word contained a bit which was not applicable to the
644	type of file.  E.g., an attempt was made to set the <<D_PAGED>> bit
645	on a BFD format which does not support demand paging.
646
647*/
648
649boolean
650bfd_set_file_flags (abfd, flags)
651     bfd *abfd;
652     flagword flags;
653{
654  if (abfd->format != bfd_object) {
655    bfd_set_error (bfd_error_wrong_format);
656    return false;
657  }
658
659  if (bfd_read_p (abfd)) {
660    bfd_set_error (bfd_error_invalid_operation);
661    return false;
662  }
663
664  bfd_get_file_flags (abfd) = flags;
665  if ((flags & bfd_applicable_file_flags (abfd)) != flags) {
666    bfd_set_error (bfd_error_invalid_operation);
667    return false;
668  }
669
670return true;
671}
672
673void
674bfd_assert (file, line)
675     const char *file;
676     int line;
677{
678  (*_bfd_error_handler) ("bfd assertion fail %s:%d", file, line);
679}
680
681
682/*
683FUNCTION
684	bfd_set_start_address
685
686SYNOPSIS
687 	boolean bfd_set_start_address(bfd *abfd, bfd_vma vma);
688
689DESCRIPTION
690	Make @var{vma} the entry point of output BFD @var{abfd}.
691
692RETURNS
693	Returns <<true>> on success, <<false>> otherwise.
694*/
695
696boolean
697bfd_set_start_address(abfd, vma)
698bfd *abfd;
699bfd_vma vma;
700{
701  abfd->start_address = vma;
702  return true;
703}
704
705
706/*
707FUNCTION
708	bfd_get_mtime
709
710SYNOPSIS
711	long bfd_get_mtime(bfd *abfd);
712
713DESCRIPTION
714	Return the file modification time (as read from the file system, or
715	from the archive header for archive members).
716
717*/
718
719long
720bfd_get_mtime (abfd)
721     bfd *abfd;
722{
723  FILE *fp;
724  struct stat buf;
725
726  if (abfd->mtime_set)
727    return abfd->mtime;
728
729  fp = bfd_cache_lookup (abfd);
730  if (0 != fstat (fileno (fp), &buf))
731    return 0;
732
733  abfd->mtime = buf.st_mtime;		/* Save value in case anyone wants it */
734  return buf.st_mtime;
735}
736
737/*
738FUNCTION
739	bfd_get_size
740
741SYNOPSIS
742	long bfd_get_size(bfd *abfd);
743
744DESCRIPTION
745	Return the file size (as read from file system) for the file
746	associated with BFD @var{abfd}.
747
748	The initial motivation for, and use of, this routine is not
749	so we can get the exact size of the object the BFD applies to, since
750	that might not be generally possible (archive members for example).
751	It would be ideal if someone could eventually modify
752	it so that such results were guaranteed.
753
754	Instead, we want to ask questions like "is this NNN byte sized
755	object I'm about to try read from file offset YYY reasonable?"
756	As as example of where we might do this, some object formats
757	use string tables for which the first <<sizeof(long)>> bytes of the
758	table contain the size of the table itself, including the size bytes.
759	If an application tries to read what it thinks is one of these
760	string tables, without some way to validate the size, and for
761	some reason the size is wrong (byte swapping error, wrong location
762	for the string table, etc.), the only clue is likely to be a read
763	error when it tries to read the table, or a "virtual memory
764	exhausted" error when it tries to allocate 15 bazillon bytes
765	of space for the 15 bazillon byte table it is about to read.
766	This function at least allows us to answer the quesion, "is the
767	size reasonable?".
768*/
769
770long
771bfd_get_size (abfd)
772     bfd *abfd;
773{
774  FILE *fp;
775  struct stat buf;
776
777  if ((abfd->flags & BFD_IN_MEMORY) != 0)
778    return ((struct bfd_in_memory *) abfd->iostream)->size;
779
780  fp = bfd_cache_lookup (abfd);
781  if (0 != fstat (fileno (fp), &buf))
782    return 0;
783
784  return buf.st_size;
785}
786
787/*
788FUNCTION
789	bfd_get_gp_size
790
791SYNOPSIS
792	int bfd_get_gp_size(bfd *abfd);
793
794DESCRIPTION
795	Return the maximum size of objects to be optimized using the GP
796	register under MIPS ECOFF.  This is typically set by the <<-G>>
797	argument to the compiler, assembler or linker.
798*/
799
800int
801bfd_get_gp_size (abfd)
802     bfd *abfd;
803{
804  if (abfd->format == bfd_object)
805    {
806      if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
807	return ecoff_data (abfd)->gp_size;
808      else if (abfd->xvec->flavour == bfd_target_elf_flavour)
809	return elf_gp_size (abfd);
810    }
811  return 0;
812}
813
814/*
815FUNCTION
816	bfd_set_gp_size
817
818SYNOPSIS
819	void bfd_set_gp_size(bfd *abfd, int i);
820
821DESCRIPTION
822	Set the maximum size of objects to be optimized using the GP
823	register under ECOFF or MIPS ELF.  This is typically set by
824	the <<-G>> argument to the compiler, assembler or linker.
825*/
826
827void
828bfd_set_gp_size (abfd, i)
829     bfd *abfd;
830     int i;
831{
832  /* Don't try to set GP size on an archive or core file! */
833  if (abfd->format != bfd_object)
834    return;
835  if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
836    ecoff_data (abfd)->gp_size = i;
837  else if (abfd->xvec->flavour == bfd_target_elf_flavour)
838    elf_gp_size (abfd) = i;
839}
840
841/* Get the GP value.  This is an internal function used by some of the
842   relocation special_function routines on targets which support a GP
843   register.  */
844
845bfd_vma
846_bfd_get_gp_value (abfd)
847     bfd *abfd;
848{
849  if (abfd->format == bfd_object)
850    {
851      if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
852	return ecoff_data (abfd)->gp;
853      else if (abfd->xvec->flavour == bfd_target_elf_flavour)
854	return elf_gp (abfd);
855    }
856  return 0;
857}
858
859/* Set the GP value.  */
860
861void
862_bfd_set_gp_value (abfd, v)
863     bfd *abfd;
864     bfd_vma v;
865{
866  if (abfd->format != bfd_object)
867    return;
868  if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
869    ecoff_data (abfd)->gp = v;
870  else if (abfd->xvec->flavour == bfd_target_elf_flavour)
871    elf_gp (abfd) = v;
872}
873
874/*
875FUNCTION
876	bfd_scan_vma
877
878SYNOPSIS
879	bfd_vma bfd_scan_vma(CONST char *string, CONST char **end, int base);
880
881DESCRIPTION
882	Convert, like <<strtoul>>, a numerical expression
883	@var{string} into a <<bfd_vma>> integer, and return that integer.
884	(Though without as many bells and whistles as <<strtoul>>.)
885	The expression is assumed to be unsigned (i.e., positive).
886	If given a @var{base}, it is used as the base for conversion.
887	A base of 0 causes the function to interpret the string
888	in hex if a leading "0x" or "0X" is found, otherwise
889	in octal if a leading zero is found, otherwise in decimal.
890
891	Overflow is not detected.
892*/
893
894bfd_vma
895bfd_scan_vma (string, end, base)
896     CONST char *string;
897     CONST char **end;
898     int base;
899{
900  bfd_vma value;
901  int digit;
902
903  /* Let the host do it if possible.  */
904  if (sizeof(bfd_vma) <= sizeof(unsigned long))
905    return (bfd_vma) strtoul (string, (char **) end, base);
906
907  /* A negative base makes no sense, and we only need to go as high as hex.  */
908  if ((base < 0) || (base > 16))
909    return (bfd_vma) 0;
910
911  if (base == 0)
912    {
913      if (string[0] == '0')
914	{
915	  if ((string[1] == 'x') || (string[1] == 'X'))
916	    base = 16;
917	  /* XXX should we also allow "0b" or "0B" to set base to 2? */
918	  else
919	    base = 8;
920	}
921      else
922	base = 10;
923    }
924  if ((base == 16) &&
925      (string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X')))
926    string += 2;
927  /* XXX should we also skip over "0b" or "0B" if base is 2? */
928
929/* Speed could be improved with a table like hex_value[] in gas.  */
930#define HEX_VALUE(c) \
931  (isxdigit ((unsigned char) c)					\
932   ? (isdigit ((unsigned char) c)				\
933      ? (c - '0')						\
934      : (10 + c - (islower ((unsigned char) c) ? 'a' : 'A')))	\
935   : 42)
936
937  for (value = 0; (digit = HEX_VALUE(*string)) < base; string++)
938    {
939      value = value * base + digit;
940    }
941
942  if (end)
943    *end = string;
944
945  return value;
946}
947
948/*
949FUNCTION
950	bfd_copy_private_bfd_data
951
952SYNOPSIS
953	boolean bfd_copy_private_bfd_data(bfd *ibfd, bfd *obfd);
954
955DESCRIPTION
956	Copy private BFD information from the BFD @var{ibfd} to the
957	the BFD @var{obfd}.  Return <<true>> on success, <<false>> on error.
958	Possible error returns are:
959
960	o <<bfd_error_no_memory>> -
961	Not enough memory exists to create private data for @var{obfd}.
962
963.#define bfd_copy_private_bfd_data(ibfd, obfd) \
964.     BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
965.		(ibfd, obfd))
966
967*/
968
969/*
970FUNCTION
971	bfd_merge_private_bfd_data
972
973SYNOPSIS
974	boolean bfd_merge_private_bfd_data(bfd *ibfd, bfd *obfd);
975
976DESCRIPTION
977	Merge private BFD information from the BFD @var{ibfd} to the
978	the output file BFD @var{obfd} when linking.  Return <<true>>
979	on success, <<false>> on error.  Possible error returns are:
980
981	o <<bfd_error_no_memory>> -
982	Not enough memory exists to create private data for @var{obfd}.
983
984.#define bfd_merge_private_bfd_data(ibfd, obfd) \
985.     BFD_SEND (obfd, _bfd_merge_private_bfd_data, \
986.		(ibfd, obfd))
987
988*/
989
990/*
991FUNCTION
992	bfd_set_private_flags
993
994SYNOPSIS
995	boolean bfd_set_private_flags(bfd *abfd, flagword flags);
996
997DESCRIPTION
998	Set private BFD flag information in the BFD @var{abfd}.
999	Return <<true>> on success, <<false>> on error.  Possible error
1000	returns are:
1001
1002	o <<bfd_error_no_memory>> -
1003	Not enough memory exists to create private data for @var{obfd}.
1004
1005.#define bfd_set_private_flags(abfd, flags) \
1006.     BFD_SEND (abfd, _bfd_set_private_flags, \
1007.		(abfd, flags))
1008
1009*/
1010
1011/*
1012FUNCTION
1013	stuff
1014
1015DESCRIPTION
1016	Stuff which should be documented:
1017
1018.#define bfd_sizeof_headers(abfd, reloc) \
1019.     BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc))
1020.
1021.#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
1022.     BFD_SEND (abfd, _bfd_find_nearest_line,  (abfd, sec, syms, off, file, func, line))
1023.
1024.       {* Do these three do anything useful at all, for any back end?  *}
1025.#define bfd_debug_info_start(abfd) \
1026.        BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
1027.
1028.#define bfd_debug_info_end(abfd) \
1029.        BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
1030.
1031.#define bfd_debug_info_accumulate(abfd, section) \
1032.        BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
1033.
1034.
1035.#define bfd_stat_arch_elt(abfd, stat) \
1036.        BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
1037.
1038.#define bfd_update_armap_timestamp(abfd) \
1039.        BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
1040.
1041.#define bfd_set_arch_mach(abfd, arch, mach)\
1042.        BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
1043.
1044.#define bfd_relax_section(abfd, section, link_info, again) \
1045.       BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
1046.
1047.#define bfd_link_hash_table_create(abfd) \
1048.	BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
1049.
1050.#define bfd_link_add_symbols(abfd, info) \
1051.	BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
1052.
1053.#define bfd_final_link(abfd, info) \
1054.	BFD_SEND (abfd, _bfd_final_link, (abfd, info))
1055.
1056.#define bfd_free_cached_info(abfd) \
1057.       BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
1058.
1059.#define bfd_get_dynamic_symtab_upper_bound(abfd) \
1060.	BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
1061.
1062.#define bfd_print_private_bfd_data(abfd, file)\
1063.	BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
1064.
1065.#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
1066.	BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
1067.
1068.#define bfd_get_dynamic_reloc_upper_bound(abfd) \
1069.	BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
1070.
1071.#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
1072.	BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
1073.
1074.extern bfd_byte *bfd_get_relocated_section_contents
1075.	PARAMS ((bfd *, struct bfd_link_info *,
1076.		  struct bfd_link_order *, bfd_byte *,
1077.		  boolean, asymbol **));
1078.
1079
1080*/
1081
1082bfd_byte *
1083bfd_get_relocated_section_contents (abfd, link_info, link_order, data,
1084				    relocateable, symbols)
1085     bfd *abfd;
1086     struct bfd_link_info *link_info;
1087     struct bfd_link_order *link_order;
1088     bfd_byte *data;
1089     boolean relocateable;
1090     asymbol **symbols;
1091{
1092  bfd *abfd2;
1093  bfd_byte *(*fn) PARAMS ((bfd *, struct bfd_link_info *,
1094			   struct bfd_link_order *, bfd_byte *, boolean,
1095			   asymbol **));
1096
1097  if (link_order->type == bfd_indirect_link_order)
1098    {
1099      abfd2 = link_order->u.indirect.section->owner;
1100      if (abfd2 == 0)
1101	abfd2 = abfd;
1102    }
1103  else
1104    abfd2 = abfd;
1105  fn = abfd2->xvec->_bfd_get_relocated_section_contents;
1106
1107  return (*fn) (abfd, link_info, link_order, data, relocateable, symbols);
1108}
1109
1110/* Record information about an ELF program header.  */
1111
1112boolean
1113bfd_record_phdr (abfd, type, flags_valid, flags, at_valid, at,
1114		 includes_filehdr, includes_phdrs, count, secs)
1115     bfd *abfd;
1116     unsigned long type;
1117     boolean flags_valid;
1118     flagword flags;
1119     boolean at_valid;
1120     bfd_vma at;
1121     boolean includes_filehdr;
1122     boolean includes_phdrs;
1123     unsigned int count;
1124     asection **secs;
1125{
1126  struct elf_segment_map *m, **pm;
1127
1128  if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
1129    return true;
1130
1131  m = ((struct elf_segment_map *)
1132       bfd_alloc (abfd,
1133		  (sizeof (struct elf_segment_map)
1134		   + ((size_t) count - 1) * sizeof (asection *))));
1135  if (m == NULL)
1136    return false;
1137
1138  m->next = NULL;
1139  m->p_type = type;
1140  m->p_flags = flags;
1141  m->p_paddr = at;
1142  m->p_flags_valid = flags_valid;
1143  m->p_paddr_valid = at_valid;
1144  m->includes_filehdr = includes_filehdr;
1145  m->includes_phdrs = includes_phdrs;
1146  m->count = count;
1147  if (count > 0)
1148    memcpy (m->sections, secs, count * sizeof (asection *));
1149
1150  for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
1151    ;
1152  *pm = m;
1153
1154  return true;
1155}
1156