* bfd.c (bfd_get_error, bfd_set_error): New functions.
[external/binutils.git] / bfd / targets.c
1 /* Generic target-file-type support for the BFD library.
2    Copyright 1990, 91, 92, 93, 94 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 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24
25 /*
26 SECTION 
27         Targets
28
29 DESCRIPTION
30         Each port of BFD to a different machine requries the creation
31         of a target back end. All the back end provides to the root
32         part of BFD is a structure containing pointers to functions
33         which perform certain low level operations on files. BFD
34         translates the applications's requests through a pointer into
35         calls to the back end routines. 
36
37         When a file is opened with <<bfd_openr>>, its format and
38         target are unknown. BFD uses various mechanisms to determine
39         how to interpret the file. The operations performed are:
40
41         o Create a BFD by calling the internal routine
42         <<_bfd_new_bfd>>, then call <<bfd_find_target>> with the
43         target string supplied to <<bfd_openr>> and the new BFD pointer. 
44
45         o If a null target string was provided to <<bfd_find_target>>,
46         look up the environment variable <<GNUTARGET>> and use
47         that as the target string. 
48
49         o If the target string is still <<NULL>>, or the target string is
50         <<default>>, then use the first item in the target vector
51         as the target type, and set <<target_defaulted>> in the BFD to
52         cause <<bfd_check_format>> to loop through all the targets.
53         @xref{bfd_target}.  @xref{Formats}.
54
55         o Otherwise, inspect the elements in the target vector
56         one by one, until a match on target name is found. When found,
57         use it. 
58
59         o Otherwise return the error <<bfd_error_invalid_target>> to
60         <<bfd_openr>>.
61
62         o <<bfd_openr>> attempts to open the file using
63         <<bfd_open_file>>, and returns the BFD.
64
65         Once the BFD has been opened and the target selected, the file
66         format may be determined. This is done by calling
67         <<bfd_check_format>> on the BFD with a suggested format. 
68         If <<target_defaulted>> has been set, each possible target
69         type is tried to see if it recognizes the specified format.
70         <<bfd_check_format>> returns <<true>> when the caller guesses right.
71 @menu
72 @* bfd_target::
73 @end menu
74 */
75
76
77 /*
78
79 INODE
80         bfd_target,  , Targets, Targets
81 DOCDD
82 SUBSECTION
83         bfd_target
84
85 DESCRIPTION
86         This structure contains everything that BFD knows about a
87         target. It includes things like its byte order, name, and which
88         routines to call to do various operations.   
89
90         Every BFD points to a target structure with its <<xvec>>
91         member. 
92
93         The macros below are used to dispatch to functions through the
94         <<bfd_target>> vector. They are used in a number of macros further
95         down in @file{bfd.h}, and are also used when calling various
96         routines by hand inside the BFD implementation.  The @var{arglist}
97         argument must be parenthesized; it contains all the arguments
98         to the called function. 
99
100         They make the documentation (more) unpleasant to read, so if
101         someone wants to fix this and not break the above, please do.
102
103 .#define BFD_SEND(bfd, message, arglist) \
104 .               ((*((bfd)->xvec->message)) arglist)
105
106         For operations which index on the BFD format:
107
108 .#define BFD_SEND_FMT(bfd, message, arglist) \
109 .            (((bfd)->xvec->message[(int)((bfd)->format)]) arglist)
110
111         This is the structure which defines the type of BFD this is.  The
112         <<xvec>> member of the struct <<bfd>> itself points here.  Each
113         module that implements access to a different target under BFD,
114         defines one of these.
115
116
117         FIXME, these names should be rationalised with the names of
118         the entry points which call them. Too bad we can't have one
119         macro to define them both! 
120
121 .enum bfd_flavour {
122 .  bfd_target_unknown_flavour,
123 .  bfd_target_aout_flavour,
124 .  bfd_target_coff_flavour,
125 .  bfd_target_ecoff_flavour,
126 .  bfd_target_elf_flavour,
127 .  bfd_target_ieee_flavour,
128 .  bfd_target_nlm_flavour,
129 .  bfd_target_oasys_flavour,
130 .  bfd_target_tekhex_flavour,
131 .  bfd_target_srec_flavour,
132 .  bfd_target_som_flavour};
133 .
134 .{* Forward declaration.  *}
135 .typedef struct bfd_link_info _bfd_link_info;
136 .
137 .typedef struct bfd_target
138 .{
139
140 Identifies the kind of target, e.g., SunOS4, Ultrix, etc.
141
142 .  char *name;
143
144 The "flavour" of a back end is a general indication about the contents
145 of a file.
146
147 .  enum bfd_flavour flavour;
148
149 The order of bytes within the data area of a file.
150
151 .  boolean byteorder_big_p;
152
153 The order of bytes within the header parts of a file.
154
155 .  boolean header_byteorder_big_p;
156
157 A mask of all the flags which an executable may have set -
158 from the set <<NO_FLAGS>>, <<HAS_RELOC>>, ...<<D_PAGED>>.
159
160 .  flagword object_flags;       
161
162 A mask of all the flags which a section may have set - from
163 the set <<SEC_NO_FLAGS>>, <<SEC_ALLOC>>, ...<<SET_NEVER_LOAD>>.
164
165 .  flagword section_flags;
166
167 The character normally found at the front of a symbol 
168 (if any), perhaps `_'.
169
170 .  char symbol_leading_char;
171
172 The pad character for file names within an archive header.
173
174 .  char ar_pad_char;            
175
176 The maximum number of characters in an archive header.
177
178 .  unsigned short ar_max_namelen;
179
180 The minimum alignment restriction for any section.
181
182 .  unsigned int align_power_min;
183
184 Entries for byte swapping for data. These are different from the other
185 entry points, since they don't take a BFD asthe first argument.
186 Certain other handlers could do the same.
187
188 .  bfd_vma      (*bfd_getx64) PARAMS ((const bfd_byte *));
189 .  bfd_signed_vma (*bfd_getx_signed_64) PARAMS ((const bfd_byte *));
190 .  void         (*bfd_putx64) PARAMS ((bfd_vma, bfd_byte *));
191 .  bfd_vma      (*bfd_getx32) PARAMS ((const bfd_byte *));
192 .  bfd_signed_vma (*bfd_getx_signed_32) PARAMS ((const bfd_byte *));
193 .  void         (*bfd_putx32) PARAMS ((bfd_vma, bfd_byte *));
194 .  bfd_vma      (*bfd_getx16) PARAMS ((const bfd_byte *));
195 .  bfd_signed_vma (*bfd_getx_signed_16) PARAMS ((const bfd_byte *));
196 .  void         (*bfd_putx16) PARAMS ((bfd_vma, bfd_byte *));
197
198 Byte swapping for the headers
199
200 .  bfd_vma      (*bfd_h_getx64) PARAMS ((const bfd_byte *));
201 .  bfd_signed_vma (*bfd_h_getx_signed_64) PARAMS ((const bfd_byte *));
202 .  void         (*bfd_h_putx64) PARAMS ((bfd_vma, bfd_byte *));
203 .  bfd_vma      (*bfd_h_getx32) PARAMS ((const bfd_byte *));
204 .  bfd_signed_vma (*bfd_h_getx_signed_32) PARAMS ((const bfd_byte *));
205 .  void         (*bfd_h_putx32) PARAMS ((bfd_vma, bfd_byte *));
206 .  bfd_vma      (*bfd_h_getx16) PARAMS ((const bfd_byte *));
207 .  bfd_signed_vma (*bfd_h_getx_signed_16) PARAMS ((const bfd_byte *));
208 .  void         (*bfd_h_putx16) PARAMS ((bfd_vma, bfd_byte *));
209
210 Format dependent routines: these are vectors of entry points
211 within the target vector structure, one for each format to check.
212
213 Check the format of a file being read.  Return a <<bfd_target *>> or zero. 
214
215 .  struct bfd_target * (*_bfd_check_format[bfd_type_end]) PARAMS ((bfd *));
216
217 Set the format of a file being written.  
218
219 .  boolean             (*_bfd_set_format[bfd_type_end]) PARAMS ((bfd *));
220
221 Write cached information into a file being written, at <<bfd_close>>. 
222
223 .  boolean             (*_bfd_write_contents[bfd_type_end]) PARAMS ((bfd *));
224
225 The following functions are defined in <<JUMP_TABLE>>. The idea is
226 that the back end writer of <<foo>> names all the routines
227 <<foo_>>@var{entry_point}; <<JUMP_TABLE>> will build the entries
228 in this structure in the right order.
229
230 Core file entry points.
231
232 .  char *   (*_core_file_failing_command) PARAMS ((bfd *));
233 .  int      (*_core_file_failing_signal) PARAMS ((bfd *));
234 .  boolean  (*_core_file_matches_executable_p) PARAMS ((bfd *, bfd *));
235
236 Archive entry points.
237
238 .  boolean  (*_bfd_slurp_armap) PARAMS ((bfd *));
239 .  boolean  (*_bfd_slurp_extended_name_table) PARAMS ((bfd *));
240 .  void     (*_bfd_truncate_arname) PARAMS ((bfd *, CONST char *, char *));
241 .  boolean  (*write_armap) PARAMS ((bfd *arch, 
242 .                              unsigned int elength,
243 .                              struct orl *map,
244 .                              unsigned int orl_count, 
245 .                              int stridx));
246
247 Standard stuff.
248
249 .  boolean       (*_close_and_cleanup) PARAMS ((bfd *));
250 .  boolean       (*_bfd_set_section_contents) PARAMS ((bfd *, sec_ptr, PTR,
251 .                                            file_ptr, bfd_size_type));
252 .  boolean       (*_bfd_get_section_contents) PARAMS ((bfd *, sec_ptr, PTR, 
253 .                                            file_ptr, bfd_size_type));
254 .  boolean       (*_new_section_hook) PARAMS ((bfd *, sec_ptr));
255
256 Symbols and relocations.
257
258 .  unsigned int  (*_get_symtab_upper_bound) PARAMS ((bfd *));
259 .  unsigned int  (*_bfd_canonicalize_symtab) PARAMS ((bfd *,
260 .                                              struct symbol_cache_entry **));
261 .  unsigned int  (*_get_reloc_upper_bound) PARAMS ((bfd *, sec_ptr));
262 .  unsigned int  (*_bfd_canonicalize_reloc) PARAMS ((bfd *, sec_ptr, arelent **,
263 .                                              struct symbol_cache_entry **));
264 .  struct symbol_cache_entry  *
265 .                (*_bfd_make_empty_symbol) PARAMS ((bfd *));
266 .  void          (*_bfd_print_symbol) PARAMS ((bfd *, PTR,
267 .                                      struct symbol_cache_entry *,
268 .                                      bfd_print_symbol_type));
269 .#define bfd_print_symbol(b,p,s,e) BFD_SEND(b, _bfd_print_symbol, (b,p,s,e))
270 .  void          (*_bfd_get_symbol_info) PARAMS ((bfd *,
271 .                                      struct symbol_cache_entry *,
272 .                                      symbol_info *));
273 .#define bfd_get_symbol_info(b,p,e) BFD_SEND(b, _bfd_get_symbol_info, (b,p,e))
274
275 .  alent *    (*_get_lineno) PARAMS ((bfd *, struct symbol_cache_entry *));
276 .
277 .  boolean    (*_bfd_set_arch_mach) PARAMS ((bfd *, enum bfd_architecture,
278 .                    unsigned long));
279 .
280 .  bfd *      (*openr_next_archived_file) PARAMS ((bfd *arch, bfd *prev));
281
282 .  boolean    (*_bfd_find_nearest_line) PARAMS ((bfd *abfd,
283 .                    struct sec *section, struct symbol_cache_entry **symbols,
284 .                    bfd_vma offset, CONST char **file, CONST char **func,
285 .                    unsigned int *line));
286
287 .  int        (*_bfd_stat_arch_elt) PARAMS ((bfd *, struct stat *));
288 .
289 .  int        (*_bfd_sizeof_headers) PARAMS ((bfd *, boolean));
290 .
291 .  void       (*_bfd_debug_info_start) PARAMS ((bfd *));
292 .  void       (*_bfd_debug_info_end) PARAMS ((bfd *));
293 .  void       (*_bfd_debug_info_accumulate) PARAMS ((bfd *, struct sec *));
294 .
295 .  bfd_byte * (*_bfd_get_relocated_section_contents) PARAMS ((bfd *,
296 .                    struct bfd_link_info *, struct bfd_link_order *,
297 .                    bfd_byte *data, boolean relocateable,
298 .                    struct symbol_cache_entry **));
299 .
300 .  boolean    (*_bfd_relax_section) PARAMS ((bfd *, struct sec *,
301 .                    struct bfd_link_info *, struct symbol_cache_entry **));
302 .
303 . {* See documentation on reloc types.  *}
304 . CONST struct reloc_howto_struct *
305 .       (*reloc_type_lookup) PARAMS ((bfd *abfd,
306 .                                     bfd_reloc_code_real_type code));
307 .
308 . {* Back-door to allow format-aware applications to create debug symbols
309 .    while using BFD for everything else.  Currently used by the assembler
310 .    when creating COFF files.  *}
311 . asymbol *  (*_bfd_make_debug_symbol) PARAMS ((
312 .       bfd *abfd,
313 .       void *ptr,
314 .       unsigned long size));
315 .
316 . {* Create a hash table for the linker.  Different backends store
317 .    different information in this table.  *}
318 . struct bfd_link_hash_table *(*_bfd_link_hash_table_create) PARAMS ((bfd *));
319 .
320 . {* Add symbols from this object file into the hash table.  *}
321 . boolean (*_bfd_link_add_symbols) PARAMS ((bfd *, struct bfd_link_info *));
322 .
323 . {* Do a link based on the link_order structures attached to each
324 .    section of the BFD.  *}
325 . boolean (*_bfd_final_link) PARAMS ((bfd *, struct bfd_link_info *));
326 .
327
328 Data for use by back-end routines, which isn't generic enough to belong
329 in this structure.
330
331 . PTR backend_data;
332 .} bfd_target;
333
334 */
335
336 /* All known xvecs (even those that don't compile on all systems).
337    Alphabetized for easy reference.
338    They are listed a second time below, since
339    we can't intermix extern's and initializers.  */
340 extern bfd_target a29kcoff_big_vec;
341 extern bfd_target a_out_adobe_vec;
342 extern bfd_target aout_mips_big_vec;
343 extern bfd_target aout_mips_little_vec;
344 extern bfd_target apollocoff_vec;
345 extern bfd_target b_out_vec_big_host;
346 extern bfd_target b_out_vec_little_host;
347 extern bfd_target bfd_elf32_big_generic_vec;
348 extern bfd_target bfd_elf32_bigmips_vec;
349 extern bfd_target bfd_elf32_hppa_vec;
350 extern bfd_target bfd_elf32_i386_vec;
351 extern bfd_target bfd_elf32_i860_vec;
352 extern bfd_target bfd_elf32_little_generic_vec;
353 extern bfd_target bfd_elf32_littlemips_vec;
354 extern bfd_target bfd_elf32_m68k_vec;
355 extern bfd_target bfd_elf32_m88k_vec;
356 extern bfd_target bfd_elf32_sparc_vec;
357 extern bfd_target bfd_elf64_big_generic_vec;
358 extern bfd_target bfd_elf64_little_generic_vec;
359 /* start-sanitize-v9 */
360 extern bfd_target bfd_elf64_sparc_vec;
361 /* end-sanitize-v9 */
362 extern bfd_target demo_64_vec;
363 extern bfd_target ecoff_big_vec;
364 extern bfd_target ecoff_little_vec;
365 extern bfd_target ecoffalpha_little_vec;
366 extern bfd_target h8300coff_vec;
367 extern bfd_target h8500coff_vec;
368 extern bfd_target host_aout_vec;
369 extern bfd_target hp300bsd_vec;
370 extern bfd_target hp300hpux_vec;
371 extern bfd_target som_vec;
372 extern bfd_target i386aout_vec;
373 extern bfd_target i386bsd_vec;
374 extern bfd_target netbsd386_vec;
375 extern bfd_target i386coff_vec;
376 extern bfd_target i386linux_vec;
377 extern bfd_target i386lynx_aout_vec;
378 extern bfd_target i386lynx_coff_vec;
379 extern bfd_target i386mach3_vec;
380 extern bfd_target icoff_big_vec;
381 extern bfd_target icoff_little_vec;
382 extern bfd_target ieee_vec;
383 extern bfd_target m68kcoff_vec;
384 extern bfd_target m68kcoffun_vec;
385 extern bfd_target m68klynx_aout_vec;
386 extern bfd_target m68klynx_coff_vec;
387 extern bfd_target m88kbcs_vec;
388 extern bfd_target newsos3_vec;
389 extern bfd_target nlm32_i386_vec;
390 extern bfd_target nlm32_sparc_vec;
391 extern bfd_target nlm32_alpha_vec;
392 extern bfd_target oasys_vec;
393 extern bfd_target rs6000coff_vec;
394 extern bfd_target shcoff_vec;
395 extern bfd_target sparclynx_aout_vec;
396 extern bfd_target sparclynx_coff_vec;
397 extern bfd_target sparccoff_vec;
398 extern bfd_target sunos_big_vec;
399 extern bfd_target tekhex_vec;
400 extern bfd_target we32kcoff_vec;
401 extern bfd_target z8kcoff_vec;
402
403 /* srec is always included.  */
404 extern bfd_target srec_vec;
405 extern bfd_target symbolsrec_vec;
406
407 /* All of the xvecs for core files.  */
408 extern bfd_target aix386_core_vec;
409 extern bfd_target hpux_core_vec;
410 extern bfd_target hppabsd_core_vec;
411 extern bfd_target irix_core_vec;
412 extern bfd_target osf_core_vec;
413 extern bfd_target sco_core_vec;
414 extern bfd_target trad_core_vec;
415 extern bfd_target ptrace_core_vec;
416
417 bfd_target *bfd_target_vector[] = {
418
419 #ifdef SELECT_VECS
420
421         SELECT_VECS,
422
423 #else /* not SELECT_VECS */
424
425 #ifdef DEFAULT_VECTOR
426         &DEFAULT_VECTOR,
427 #endif
428         /* This list is alphabetized to make it easy to compare
429            with other vector lists -- the decls above and
430            the case statement in configure.in.
431            Vectors that don't compile on all systems, or aren't finished,
432            should have an entry here with #if 0 around it, to show that
433            it wasn't omitted by mistake.  */
434         &a29kcoff_big_vec,
435         &a_out_adobe_vec,
436 #if 0                           /* No one seems to use this.  */
437         &aout_mips_big_vec,
438 #endif
439         &aout_mips_little_vec,
440         &b_out_vec_big_host,
441         &b_out_vec_little_host,
442
443         /* This, and other vectors, may not be used in any *.mt configuration.
444            But that does not mean they are unnecessary.  If configured
445            --with-targets=all, objdump or gdb should be able to examine
446            the file even if we don't recognize the machine type.  */
447         &bfd_elf32_big_generic_vec,
448         &bfd_elf32_bigmips_vec,
449         &bfd_elf32_hppa_vec,
450         &bfd_elf32_i386_vec,
451         &bfd_elf32_i860_vec,
452         &bfd_elf32_little_generic_vec,
453         &bfd_elf32_littlemips_vec,
454         &bfd_elf32_m68k_vec,
455         &bfd_elf32_m88k_vec,
456         &bfd_elf32_sparc_vec,
457 #ifdef BFD64                    /* No one seems to use this.  */
458         &bfd_elf64_big_generic_vec,
459         &bfd_elf64_little_generic_vec,
460 #endif
461 /* start-sanitize-v9 */
462 #if 0
463         &bfd_elf64_sparc_vec,
464 #endif
465 /* end-sanitize-v9 */
466 #ifdef BFD64
467         &demo_64_vec,   /* Only compiled if host has long-long support */
468 #endif
469         &ecoff_big_vec,
470         &ecoff_little_vec,
471 #if 0
472         &ecoffalpha_little_vec,
473 #endif
474         &h8300coff_vec,
475         &h8500coff_vec,
476 #if 0
477         /* Since a.out files lack decent magic numbers, no way to recognize
478            which kind of a.out file it is.  */
479         &host_aout_vec,
480 #endif
481 #if 0                           /* Clashes with sunos_big_vec magic no.  */
482         &hp300bsd_vec,
483 #endif
484         &hp300hpux_vec,
485 #if defined (HOST_HPPAHPUX) || defined (HOST_HPPABSD) || defined (HOST_HPPAOSF)
486         &som_vec,
487 #endif
488         &i386aout_vec,
489         &i386bsd_vec,
490         &netbsd386_vec,
491         &i386coff_vec,
492 #if 0
493         /* Since a.out files lack decent magic numbers, no way to recognize
494            which kind of a.out file it is.  */
495         &i386linux_vec,
496 #endif
497         &i386lynx_aout_vec,
498         &i386lynx_coff_vec,
499         &icoff_big_vec,
500         &icoff_little_vec,
501         &ieee_vec,
502         &m68kcoff_vec,
503         &m68kcoffun_vec,
504         &m68klynx_aout_vec,
505         &m68klynx_coff_vec,
506         &m88kbcs_vec,
507         &newsos3_vec,
508         &nlm32_i386_vec,
509         &nlm32_sparc_vec,
510 #ifdef BFD64
511         &nlm32_alpha_vec,
512 #endif
513 #if 0
514         /* We have no oasys tools anymore, so we can't test any of this
515            anymore. If you want to test the stuff yourself, go ahead...
516            steve@cygnus.com
517            Worse, since there is no magic number for archives, there
518            can be annoying target mis-matches.  */
519         &oasys_vec,
520 #endif
521         &rs6000coff_vec,
522         &shcoff_vec,
523         &sparclynx_aout_vec,
524         &sparclynx_coff_vec,
525         &sunos_big_vec,
526 #if 0
527         &tekhex_vec,
528 #endif
529         &we32kcoff_vec,
530         &z8kcoff_vec,
531
532 #endif /* not SELECT_VECS */
533
534 /* Always support S-records, for convenience.  */
535         &srec_vec,
536         &symbolsrec_vec,
537
538 /* Add any required traditional-core-file-handler.  */
539
540 #ifdef AIX386_CORE
541         &aix386_core_vec,
542 #endif
543 #ifdef HPUX_CORE
544         &hpux_core_vec,
545 #endif
546 #ifdef HPPABSD_CORE
547         &hppabsd_core_vec,
548 #endif
549 #ifdef IRIX_CORE
550         &irix_core_vec,
551 #endif
552 #ifdef OSF_CORE
553         &osf_core_vec,
554 #endif
555 #ifdef  TRAD_CORE
556         &trad_core_vec,
557 #endif
558
559 #ifdef  PTRACE_CORE
560         &ptrace_core_vec,
561 #endif
562
563         NULL /* end of list marker */
564 };
565
566 /* bfd_default_vector[0] contains either the address of the default vector,
567    if there is one, or zero if there isn't.  */
568
569 bfd_target *bfd_default_vector[] = {
570 #ifdef DEFAULT_VECTOR
571         &DEFAULT_VECTOR,
572 #endif
573         NULL
574 };
575
576 /* When there is an ambiguous match, bfd_check_format_matches puts the
577    names of the matching targets in an array.  This variable is the maximum
578    number of entries that the array could possibly need.  */
579 CONST size_t _bfd_target_vector_entries = sizeof(bfd_target_vector)/sizeof(*bfd_target_vector);
580
581 /*
582 FUNCTION
583         bfd_find_target
584
585 SYNOPSIS
586         bfd_target *bfd_find_target(CONST char *target_name, bfd *abfd);
587
588 DESCRIPTION
589         Return a pointer to the transfer vector for the object target
590         named @var{target_name}.  If @var{target_name} is <<NULL>>, choose the
591         one in the environment variable <<GNUTARGET>>; if that is null or not
592         defined, then choose the first entry in the target list.
593         Passing in the string "default" or setting the environment
594         variable to "default" will cause the first entry in the target
595         list to be returned, and "target_defaulted" will be set in the
596         BFD.  This causes <<bfd_check_format>> to loop over all the
597         targets to find the one that matches the file being read.   
598 */
599
600 bfd_target *
601 DEFUN(bfd_find_target,(target_name, abfd),
602       CONST char *target_name AND
603       bfd *abfd)
604 {
605   bfd_target **target;
606   extern char *getenv ();
607   CONST char *targname = (target_name ? target_name : 
608                           (CONST char *) getenv ("GNUTARGET"));
609
610   /* This is safe; the vector cannot be null */
611   if (targname == NULL || !strcmp (targname, "default")) {
612     abfd->target_defaulted = true;
613     return abfd->xvec = bfd_target_vector[0];
614   }
615
616   abfd->target_defaulted = false;
617
618   for (target = &bfd_target_vector[0]; *target != NULL; target++) {
619     if (!strcmp (targname, (*target)->name))
620       return abfd->xvec = *target;
621   }
622
623   bfd_set_error (bfd_error_invalid_target);
624   return NULL;
625 }
626
627
628 /*
629 FUNCTION
630         bfd_target_list
631
632 SYNOPSIS
633         CONST char **bfd_target_list(void);
634
635 DESCRIPTION
636         Return a freshly malloced NULL-terminated
637         vector of the names of all the valid BFD targets. Do not
638         modify the names.
639
640 */
641
642 CONST char **
643 DEFUN_VOID(bfd_target_list)
644 {
645   int vec_length= 0;
646 #ifdef NATIVE_HPPAHPUX_COMPILER
647   /* The native compiler on the HP9000/700 has a bug which causes it
648      to loop endlessly when compiling this file.  This avoids it.  */
649   volatile
650 #endif
651     bfd_target **target;
652   CONST  char **name_list, **name_ptr;
653
654   for (target = &bfd_target_vector[0]; *target != NULL; target++)
655     vec_length++;
656
657   name_ptr = name_list = (CONST char **)
658     bfd_zmalloc ((vec_length + 1) * sizeof (char **));
659
660   if (name_list == NULL) {
661     bfd_set_error (bfd_error_no_memory);
662     return NULL;
663   }
664
665   for (target = &bfd_target_vector[0]; *target != NULL; target++)
666     *(name_ptr++) = (*target)->name;
667
668   return name_list;
669 }