* vms.c (vms_object_p): Restore the start address when returning
[external/binutils.git] / bfd / vms.c
1 /* vms.c -- BFD back-end for VAX (openVMS/VAX) and
2    EVAX (openVMS/Alpha) files.
3    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
4    Free Software Foundation, Inc.
5
6    Written by Klaus K"ampf (kkaempf@rmi.de)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26
27 #include "vms.h"
28
29 static boolean vms_initialize PARAMS ((bfd *));
30 static unsigned int priv_section_count;
31 static boolean fill_section_ptr PARAMS ((struct bfd_hash_entry *, PTR));
32 static boolean vms_fixup_sections PARAMS ((bfd *));
33 static boolean copy_symbols PARAMS ((struct bfd_hash_entry *, PTR));
34 static bfd_reloc_status_type reloc_nil
35   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
36 static const struct bfd_target *vms_object_p PARAMS ((bfd *abfd));
37 static const struct bfd_target *vms_archive_p PARAMS ((bfd *abfd));
38 static boolean vms_mkobject PARAMS ((bfd *abfd));
39 static boolean vms_write_object_contents PARAMS ((bfd *abfd));
40 static boolean vms_close_and_cleanup PARAMS ((bfd *abfd));
41 static boolean vms_bfd_free_cached_info PARAMS ((bfd *abfd));
42 static boolean vms_new_section_hook PARAMS ((bfd *abfd, asection *section));
43 static boolean vms_get_section_contents
44   PARAMS ((bfd *abfd, asection *section, PTR x1, file_ptr x2,
45            bfd_size_type x3));
46 static boolean vms_get_section_contents_in_window
47   PARAMS ((bfd *abfd, asection *section, bfd_window *w, file_ptr offset,
48            bfd_size_type count));
49 static boolean vms_bfd_copy_private_bfd_data PARAMS ((bfd *src, bfd *dest));
50 static boolean vms_bfd_copy_private_section_data
51   PARAMS ((bfd *srcbfd, asection *srcsec, bfd *dstbfd, asection *dstsec));
52 static boolean vms_bfd_copy_private_symbol_data
53   PARAMS ((bfd *ibfd, asymbol *isym, bfd *obfd, asymbol *osym));
54 static boolean vms_bfd_print_private_bfd_data
55   PARAMS ((bfd *abfd, void *file));
56 static char *vms_core_file_failing_command PARAMS ((bfd *abfd));
57 static int vms_core_file_failing_signal PARAMS ((bfd *abfd));
58 static boolean vms_core_file_matches_executable_p
59   PARAMS ((bfd *abfd, bfd *bbfd));
60 static boolean vms_slurp_armap PARAMS ((bfd *abfd));
61 static boolean vms_slurp_extended_name_table PARAMS ((bfd *abfd));
62 static boolean vms_construct_extended_name_table
63   PARAMS ((bfd *abfd, char **tabloc, bfd_size_type *tablen,
64            const char **name));
65 static void vms_truncate_arname
66   PARAMS ((bfd *abfd, const char *pathname, char *arhdr));
67 static boolean vms_write_armap
68   PARAMS ((bfd *arch, unsigned int elength, struct orl *map,
69            unsigned int orl_count, int stridx));
70 static PTR vms_read_ar_hdr PARAMS ((bfd *abfd));
71 static bfd *vms_get_elt_at_index PARAMS ((bfd *abfd, symindex index));
72 static bfd *vms_openr_next_archived_file PARAMS ((bfd *arch, bfd *prev));
73 static boolean vms_update_armap_timestamp PARAMS ((bfd *abfd));
74 static int vms_generic_stat_arch_elt PARAMS ((bfd *, struct stat *));
75 static long vms_get_symtab_upper_bound PARAMS ((bfd *abfd));
76 static long vms_get_symtab PARAMS ((bfd *abfd, asymbol **symbols));
77 static void vms_print_symbol
78   PARAMS ((bfd *abfd, PTR file, asymbol *symbol, bfd_print_symbol_type how));
79 static void vms_get_symbol_info
80   PARAMS ((bfd *abfd, asymbol *symbol, symbol_info *ret));
81 static boolean vms_bfd_is_local_label_name PARAMS ((bfd *abfd, const char *));
82 static alent *vms_get_lineno PARAMS ((bfd *abfd, asymbol *symbol));
83 static boolean vms_find_nearest_line
84   PARAMS ((bfd *abfd, asection *section, asymbol **symbols, bfd_vma offset,
85            const char **file, const char **func, unsigned int *line));
86 static asymbol *vms_bfd_make_debug_symbol
87   PARAMS ((bfd *abfd, void *ptr, unsigned long size));
88 static long vms_read_minisymbols
89   PARAMS ((bfd *abfd, boolean dynamic, PTR *minisymsp, unsigned int *sizep));
90 static asymbol *vms_minisymbol_to_symbol
91   PARAMS ((bfd *abfd, boolean dynamic, const PTR minisym, asymbol *sym));
92 static long vms_get_reloc_upper_bound PARAMS ((bfd *abfd, asection *sect));
93 static long vms_canonicalize_reloc
94   PARAMS ((bfd *abfd, asection *srcsec, arelent **location,
95            asymbol **symbols));
96 static const struct reloc_howto_struct *vms_bfd_reloc_type_lookup
97   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
98 static boolean vms_set_arch_mach
99   PARAMS ((bfd *abfd, enum bfd_architecture arch, unsigned long mach));
100 static boolean vms_set_section_contents
101   PARAMS ((bfd *abfd, asection *section, PTR location, file_ptr offset,
102            bfd_size_type count));
103 static int vms_sizeof_headers PARAMS ((bfd *abfd, boolean reloc));
104 static bfd_byte *vms_bfd_get_relocated_section_contents
105   PARAMS ((bfd *abfd, struct bfd_link_info *link_info,
106            struct bfd_link_order *link_order, bfd_byte *data,
107            boolean relocateable, asymbol **symbols));
108 static boolean vms_bfd_relax_section
109   PARAMS ((bfd *abfd, asection *section, struct bfd_link_info *link_info,
110            boolean *again));
111 static boolean vms_bfd_gc_sections
112   PARAMS ((bfd *abfd, struct bfd_link_info *link_info));
113 static boolean vms_bfd_merge_sections
114   PARAMS ((bfd *abfd, struct bfd_link_info *link_info));
115 static struct bfd_link_hash_table *vms_bfd_link_hash_table_create
116   PARAMS ((bfd *abfd));
117 static void vms_bfd_link_hash_table_free
118   PARAMS ((struct bfd_link_hash_table *hash));
119 static boolean vms_bfd_link_add_symbols
120   PARAMS ((bfd *abfd, struct bfd_link_info *link_info));
121 static boolean vms_bfd_final_link
122   PARAMS ((bfd *abfd, struct bfd_link_info *link_info));
123 static boolean vms_bfd_link_split_section
124   PARAMS ((bfd *abfd, asection *section));
125 static long vms_get_dynamic_symtab_upper_bound PARAMS ((bfd *abfd));
126 static long vms_canonicalize_dynamic_symtab
127   PARAMS ((bfd *abfd, asymbol **symbols));
128 static long vms_get_dynamic_reloc_upper_bound PARAMS ((bfd *abfd));
129 static long vms_canonicalize_dynamic_reloc
130   PARAMS ((bfd *abfd, arelent **arel, asymbol **symbols));
131 static boolean vms_bfd_merge_private_bfd_data PARAMS ((bfd *ibfd, bfd *obfd));
132 static boolean vms_bfd_set_private_flags PARAMS ((bfd *abfd, flagword flags));
133
134 #define vms_make_empty_symbol _bfd_generic_make_empty_symbol
135 #define vms_bfd_link_just_syms _bfd_generic_link_just_syms
136 #define vms_bfd_discard_group bfd_generic_discard_group
137 \f
138 /*===========================================================================*/
139
140 const bfd_target vms_alpha_vec =
141 {
142   "vms-alpha",                  /* name */
143   bfd_target_evax_flavour,
144   BFD_ENDIAN_LITTLE,            /* data byte order is little */
145   BFD_ENDIAN_LITTLE,            /* header byte order is little */
146
147   (HAS_RELOC | HAS_SYMS
148    | WP_TEXT | D_PAGED),        /* object flags */
149   (SEC_ALLOC | SEC_LOAD | SEC_RELOC
150    | SEC_READONLY | SEC_CODE | SEC_DATA
151    | SEC_HAS_CONTENTS | SEC_IN_MEMORY),         /* sect flags */
152   0,                            /* symbol_leading_char */
153   ' ',                          /* ar_pad_char */
154   15,                           /* ar_max_namelen */
155   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
156   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
157   bfd_getl16, bfd_getl_signed_16, bfd_putl16,
158   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
159   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
160   bfd_getl16, bfd_getl_signed_16, bfd_putl16,
161
162   {_bfd_dummy_target, vms_object_p,             /* bfd_check_format */
163    vms_archive_p, _bfd_dummy_target},
164   {bfd_false, vms_mkobject,                     /* bfd_set_format */
165    _bfd_generic_mkarchive, bfd_false},
166   {bfd_false, vms_write_object_contents,        /* bfd_write_contents */
167    _bfd_write_archive_contents, bfd_false},
168
169   BFD_JUMP_TABLE_GENERIC (vms),
170   BFD_JUMP_TABLE_COPY (vms),
171   BFD_JUMP_TABLE_CORE (vms),
172   BFD_JUMP_TABLE_ARCHIVE (vms),
173   BFD_JUMP_TABLE_SYMBOLS (vms),
174   BFD_JUMP_TABLE_RELOCS (vms),
175   BFD_JUMP_TABLE_WRITE (vms),
176   BFD_JUMP_TABLE_LINK (vms),
177   BFD_JUMP_TABLE_DYNAMIC (vms),
178
179   NULL,
180
181   (PTR) 0
182 };
183
184 const bfd_target vms_vax_vec =
185 {
186   "vms-vax",                    /* name */
187   bfd_target_ovax_flavour,
188   BFD_ENDIAN_LITTLE,            /* data byte order is little */
189   BFD_ENDIAN_LITTLE,            /* header byte order is little */
190
191   (HAS_RELOC | HAS_SYMS         /* object flags */
192    | WP_TEXT | D_PAGED
193    | HAS_LINENO | HAS_DEBUG | HAS_LOCALS),
194
195   (SEC_ALLOC | SEC_LOAD | SEC_RELOC
196    | SEC_READONLY | SEC_CODE | SEC_DATA
197    | SEC_HAS_CONTENTS | SEC_IN_MEMORY),         /* sect flags */
198   0,                            /* symbol_leading_char */
199   ' ',                          /* ar_pad_char */
200   15,                           /* ar_max_namelen */
201   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
202   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
203   bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
204   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
205   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
206   bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
207
208   {_bfd_dummy_target, vms_object_p,             /* bfd_check_format */
209    vms_archive_p, _bfd_dummy_target},
210   {bfd_false, vms_mkobject,                     /* bfd_set_format */
211    _bfd_generic_mkarchive, bfd_false},
212   {bfd_false, vms_write_object_contents,        /* bfd_write_contents */
213    _bfd_write_archive_contents, bfd_false},
214
215   BFD_JUMP_TABLE_GENERIC (vms),
216   BFD_JUMP_TABLE_COPY (vms),
217   BFD_JUMP_TABLE_CORE (vms),
218   BFD_JUMP_TABLE_ARCHIVE (vms),
219   BFD_JUMP_TABLE_SYMBOLS (vms),
220   BFD_JUMP_TABLE_RELOCS (vms),
221   BFD_JUMP_TABLE_WRITE (vms),
222   BFD_JUMP_TABLE_LINK (vms),
223   BFD_JUMP_TABLE_DYNAMIC (vms),
224
225   NULL,
226
227   (PTR) 0
228 };
229 \f
230 /*===========================================================================*/
231
232 /* Initialize private data  */
233
234 static boolean
235 vms_initialize (abfd)
236      bfd *abfd;
237 {
238   int i;
239   bfd_size_type amt;
240
241   bfd_set_start_address (abfd, (bfd_vma) -1);
242
243   amt = sizeof (struct vms_private_data_struct);
244   abfd->tdata.any = (struct vms_private_data_struct*) bfd_alloc (abfd, amt);
245   if (abfd->tdata.any == 0)
246     return false;
247
248 #ifdef __ALPHA
249   PRIV (is_vax) = 0;
250 #else
251   PRIV (is_vax) = 1;
252 #endif
253   PRIV (vms_buf) = 0;
254   PRIV (buf_size) = 0;
255   PRIV (rec_length) = 0;
256   PRIV (file_format) = FF_UNKNOWN;
257   PRIV (fixup_done) = false;
258   PRIV (sections) = NULL;
259
260   amt = sizeof (struct stack_struct) * STACKSIZE;
261   PRIV (stack) = (struct stack_struct *) bfd_alloc (abfd, amt);
262   if (PRIV (stack) == 0)
263     goto error_ret1;
264   PRIV (stackptr) = 0;
265
266   amt = sizeof (struct bfd_hash_table);
267   PRIV (vms_symbol_table) = (struct bfd_hash_table *) bfd_alloc (abfd, amt);
268   if (PRIV (vms_symbol_table) == 0)
269     goto error_ret1;
270
271   if (!bfd_hash_table_init (PRIV (vms_symbol_table), _bfd_vms_hash_newfunc))
272     goto error_ret1;
273
274   amt = sizeof (struct location_struct) * LOCATION_SAVE_SIZE;
275   PRIV (location_stack) = (struct location_struct *) bfd_alloc (abfd, amt);
276   if (PRIV (location_stack) == 0)
277     goto error_ret2;
278
279   for (i = 0; i < VMS_SECTION_COUNT; i++)
280     PRIV (vms_section_table)[i] = NULL;
281
282   amt = MAX_OUTREC_SIZE;
283   PRIV (output_buf) = (unsigned char *) bfd_alloc (abfd, amt);
284   if (PRIV (output_buf) == 0)
285     goto error_ret2;
286
287   PRIV (push_level) = 0;
288   PRIV (pushed_size) = 0;
289   PRIV (length_pos) = 2;
290   PRIV (output_size) = 0;
291   PRIV (output_alignment) = 1;
292
293   return true;
294
295  error_ret2:
296   bfd_hash_table_free (PRIV (vms_symbol_table));
297  error_ret1:
298   bfd_release (abfd, abfd->tdata.any);
299   abfd->tdata.any = 0;
300   return false;
301 }
302
303 /* Fill symbol->section with section ptr
304    symbol->section is filled with the section index for defined symbols
305    during reading the GSD/EGSD section. But we need the pointer to the
306    bfd section later.
307
308    It has the correct value for referenced (undefined section) symbols
309
310    called from bfd_hash_traverse in vms_fixup_sections  */
311
312 static boolean
313 fill_section_ptr (entry, sections)
314      struct bfd_hash_entry *entry;
315      PTR sections;
316 {
317   asection *sec;
318   asymbol *sym;
319
320   sym =  ((vms_symbol_entry *)entry)->symbol;
321   sec = sym->section;
322
323 #if VMS_DEBUG
324   vms_debug (6, "fill_section_ptr: sym %p, sec %p\n", sym, sec);
325 #endif
326
327   /* fill forward references (these contain section number, not section ptr).  */
328
329   if ((unsigned int) sec < priv_section_count)
330     {
331       sec = ((vms_symbol_entry *)entry)->symbol->section =
332         ((asection **)sections)[(int)sec];
333     }
334
335   if (strcmp (sym->name, sec->name) == 0)
336     sym->flags |= BSF_SECTION_SYM;
337
338   return true;
339 }
340
341 /* Fixup sections
342    set up all pointers and arrays, counters and sizes are fixed now
343
344    we build a private sections vector for easy access since sections
345    are always referenced by an index number.
346
347    alloc PRIV(sections) according to abfd->section_count
348         copy abfd->sections to PRIV(sections)  */
349
350 static boolean
351 vms_fixup_sections (abfd)
352      bfd *abfd;
353 {
354   if (PRIV (fixup_done))
355     return true;
356
357   /*
358    * traverse symbol table and fill in all section pointers
359    */
360
361   /* can't provide section count as argument to fill_section_ptr().  */
362   priv_section_count = PRIV (section_count);
363   bfd_hash_traverse (PRIV (vms_symbol_table), fill_section_ptr,
364                     (PTR) (PRIV (sections)));
365
366   PRIV (fixup_done) = true;
367
368   return true;
369 }
370
371 /*===========================================================================*/
372
373 /* Check the format for a file being read.
374    Return a (bfd_target *) if it's an object file or zero if not.  */
375
376 static const struct bfd_target *
377 vms_object_p (abfd)
378      bfd *abfd;
379 {
380   int err = 0;
381   int prev_type;
382   const struct bfd_target *target_vector = 0;
383   const bfd_arch_info_type *arch = 0;
384   PTR tdata_save = abfd->tdata.any;
385   bfd_vma saddr_save = bfd_get_start_address (abfd);
386
387 #if VMS_DEBUG
388   vms_debug (1, "vms_object_p(%p)\n", abfd);
389 #endif
390
391   if (!vms_initialize (abfd))
392     goto error_ret;
393
394   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET))
395     goto err_wrong_format;
396
397   prev_type = -1;
398
399   do
400     {
401 #if VMS_DEBUG
402       vms_debug (7, "reading at %08lx\n", bfd_tell(abfd));
403 #endif
404       if (_bfd_vms_next_record (abfd) < 0)
405         {
406 #if VMS_DEBUG
407           vms_debug (2, "next_record failed\n");
408 #endif
409           goto err_wrong_format;
410         }
411
412       if ((prev_type == EOBJ_S_C_EGSD)
413            && (PRIV (rec_type) != EOBJ_S_C_EGSD))
414         {
415           if (! vms_fixup_sections (abfd))
416             {
417 #if VMS_DEBUG
418               vms_debug (2, "vms_fixup_sections failed\n");
419 #endif
420               goto err_wrong_format;
421             }
422         }
423
424       prev_type = PRIV (rec_type);
425
426       if (target_vector == 0)
427         {
428           if (prev_type <= OBJ_S_C_MAXRECTYP)
429             target_vector = &vms_vax_vec;
430           else
431             target_vector = &vms_alpha_vec;
432         }
433
434       switch (prev_type)
435         {
436           case OBJ_S_C_HDR:
437           case EOBJ_S_C_EMH:
438             err = _bfd_vms_slurp_hdr (abfd, prev_type);
439             break;
440           case OBJ_S_C_EOM:
441           case OBJ_S_C_EOMW:
442           case EOBJ_S_C_EEOM:
443             err = _bfd_vms_slurp_eom (abfd, prev_type);
444             break;
445           case OBJ_S_C_GSD:
446           case EOBJ_S_C_EGSD:
447             err = _bfd_vms_slurp_gsd (abfd, prev_type);
448             break;
449           case OBJ_S_C_TIR:
450           case EOBJ_S_C_ETIR:
451             err = _bfd_vms_slurp_tir (abfd, prev_type);
452             break;
453           case OBJ_S_C_DBG:
454           case EOBJ_S_C_EDBG:
455             err = _bfd_vms_slurp_dbg (abfd, prev_type);
456             break;
457           case OBJ_S_C_TBT:
458           case EOBJ_S_C_ETBT:
459             err = _bfd_vms_slurp_tbt (abfd, prev_type);
460             break;
461           case OBJ_S_C_LNK:
462             err = _bfd_vms_slurp_lnk (abfd, prev_type);
463             break;
464           default:
465             err = -1;
466         }
467       if (err != 0)
468         {
469 #if VMS_DEBUG
470           vms_debug (2, "slurp type %d failed with %d\n", prev_type, err);
471 #endif
472           goto err_wrong_format;
473         }
474     }
475   while ((prev_type != EOBJ_S_C_EEOM) && (prev_type != OBJ_S_C_EOM) && (prev_type != OBJ_S_C_EOMW));
476
477   if (target_vector == &vms_vax_vec)
478     {
479       if (! vms_fixup_sections (abfd))
480         {
481 #if VMS_DEBUG
482           vms_debug (2, "vms_fixup_sections failed\n");
483 #endif
484           goto err_wrong_format;
485         }
486
487       /* set arch_info to vax  */
488
489       arch = bfd_scan_arch ("vax");
490       PRIV (is_vax) = 1;
491 #if VMS_DEBUG
492       vms_debug (2, "arch is vax\n");
493 #endif
494     }
495   else if (target_vector == &vms_alpha_vec)
496     {
497       /* set arch_info to alpha  */
498
499       arch = bfd_scan_arch ("alpha");
500       PRIV (is_vax) = 0;
501 #if VMS_DEBUG
502       vms_debug (2, "arch is alpha\n");
503 #endif
504     }
505
506   if (arch == 0)
507     {
508 #if VMS_DEBUG
509       vms_debug (2, "arch not found\n");
510 #endif
511       goto err_wrong_format;
512     }
513   abfd->arch_info = arch;
514
515   return target_vector;
516
517  err_wrong_format:
518   bfd_set_error (bfd_error_wrong_format);
519  error_ret:
520   if (abfd->tdata.any != tdata_save && abfd->tdata.any != NULL)
521     bfd_release (abfd, abfd->tdata.any);
522   abfd->tdata.any = tdata_save;
523   bfd_set_start_address (abfd, saddr_save);
524   return NULL;
525 }
526
527 /* Check the format for a file being read.
528    Return a (bfd_target *) if it's an archive file or zero.  */
529
530 static const struct bfd_target *
531 vms_archive_p (abfd)
532      bfd *abfd ATTRIBUTE_UNUSED;
533 {
534 #if VMS_DEBUG
535   vms_debug (1, "vms_archive_p(%p)\n", abfd);
536 #endif
537
538   return 0;
539 }
540
541 /* Set the format of a file being written.  */
542
543 static boolean
544 vms_mkobject (abfd)
545      bfd *abfd;
546 {
547 #if VMS_DEBUG
548   vms_debug (1, "vms_mkobject(%p)\n", abfd);
549 #endif
550
551   if (!vms_initialize (abfd))
552     return 0;
553
554   {
555 #ifdef __VAX
556     const bfd_arch_info_type *arch = bfd_scan_arch ("vax");
557 #else
558     const bfd_arch_info_type *arch = bfd_scan_arch ("alpha");
559 #endif
560     if (arch == 0)
561       {
562         bfd_set_error(bfd_error_wrong_format);
563         return 0;
564       }
565     abfd->arch_info = arch;
566   }
567
568   return true;
569 }
570
571 /* Write cached information into a file being written, at bfd_close.  */
572
573 static boolean
574 vms_write_object_contents (abfd)
575      bfd *abfd;
576 {
577 #if VMS_DEBUG
578   vms_debug (1, "vms_write_object_contents(%p)\n", abfd);
579 #endif
580
581   if (abfd->section_count > 0)                  /* we have sections */
582     {
583       if (PRIV (is_vax))
584         {
585           if (_bfd_vms_write_hdr (abfd, OBJ_S_C_HDR) != 0)
586             return false;
587           if (_bfd_vms_write_gsd (abfd, OBJ_S_C_GSD) != 0)
588             return false;
589           if (_bfd_vms_write_tir (abfd, OBJ_S_C_TIR) != 0)
590             return false;
591           if (_bfd_vms_write_tbt (abfd, OBJ_S_C_TBT) != 0)
592             return false;
593           if (_bfd_vms_write_dbg (abfd, OBJ_S_C_DBG) != 0)
594             return false;
595           if (abfd->section_count > 255)
596             {
597               if (_bfd_vms_write_eom (abfd, OBJ_S_C_EOMW) != 0)
598                 return false;
599             }
600           else
601             {
602               if (_bfd_vms_write_eom (abfd, OBJ_S_C_EOM) != 0)
603                 return false;
604             }
605         }
606       else
607         {
608           if (_bfd_vms_write_hdr (abfd, EOBJ_S_C_EMH) != 0)
609             return false;
610           if (_bfd_vms_write_gsd (abfd, EOBJ_S_C_EGSD) != 0)
611             return false;
612           if (_bfd_vms_write_tir (abfd, EOBJ_S_C_ETIR) != 0)
613             return false;
614           if (_bfd_vms_write_tbt (abfd, EOBJ_S_C_ETBT) != 0)
615             return false;
616           if (_bfd_vms_write_dbg (abfd, EOBJ_S_C_EDBG) != 0)
617             return false;
618           if (_bfd_vms_write_eom (abfd, EOBJ_S_C_EEOM) != 0)
619             return false;
620         }
621     }
622   return true;
623 }
624
625 /*-- 4.1, generic -----------------------------------------------------------*/
626
627 /* Called when the BFD is being closed to do any necessary cleanup.  */
628
629 static boolean
630 vms_close_and_cleanup (abfd)
631      bfd *abfd;
632 {
633 #if VMS_DEBUG
634   vms_debug (1, "vms_close_and_cleanup(%p)\n", abfd);
635 #endif
636   if (abfd == 0)
637     return true;
638
639   if (PRIV (vms_buf) != NULL)
640     free (PRIV (vms_buf));
641
642   if (PRIV (sections) != NULL)
643     free (PRIV (sections));
644
645   if (PRIV (vms_symbol_table))
646     bfd_hash_table_free (PRIV (vms_symbol_table));
647
648   bfd_release (abfd, abfd->tdata.any);
649   abfd->tdata.any = NULL;
650
651   return true;
652 }
653
654 /* Ask the BFD to free all cached information.  */
655 static boolean
656 vms_bfd_free_cached_info (abfd)
657      bfd *abfd ATTRIBUTE_UNUSED;
658 {
659 #if VMS_DEBUG
660   vms_debug (1, "vms_bfd_free_cached_info(%p)\n", abfd);
661 #endif
662   return true;
663 }
664
665 /* Called when a new section is created.  */
666
667 static boolean
668 vms_new_section_hook (abfd, section)
669      bfd *abfd;
670      asection *section;
671 {
672   /* Count hasn't been incremented yet.  */
673   unsigned int section_count = abfd->section_count + 1;
674
675 #if VMS_DEBUG
676   vms_debug (1, "vms_new_section_hook (%p, [%d]%s), count %d\n",
677              abfd, section->index, section->name, section_count);
678 #endif
679   bfd_set_section_alignment (abfd, section, 4);
680
681   if (section_count > PRIV (section_count))
682     {
683       bfd_size_type amt = section_count;
684       amt *= sizeof (asection *);
685       PRIV (sections) = (asection **) bfd_realloc (PRIV (sections), amt);
686       if (PRIV (sections) == 0)
687         return false;
688       PRIV (section_count) = section_count;
689     }
690 #if VMS_DEBUG
691   vms_debug (6, "section_count: %d\n", PRIV (section_count));
692 #endif
693   PRIV (sections)[section->index] = section;
694 #if VMS_DEBUG
695   vms_debug (7, "%d: %s\n", section->index, section->name);
696 #endif
697
698   return true;
699 }
700
701 /* Read the contents of a section.
702    buf points to a buffer of buf_size bytes to be filled with
703    section data (starting at offset into section)  */
704
705 static boolean
706 vms_get_section_contents (abfd, section, buf, offset, buf_size)
707      bfd *abfd ATTRIBUTE_UNUSED;
708      asection *section ATTRIBUTE_UNUSED;
709      PTR buf ATTRIBUTE_UNUSED;
710      file_ptr offset ATTRIBUTE_UNUSED;
711      bfd_size_type buf_size ATTRIBUTE_UNUSED;
712 {
713 #if VMS_DEBUG
714   vms_debug (1, "vms_get_section_contents(%p, %s, %p, off %ld, size %d)\n",
715                  abfd, section->name, buf, offset, (int)buf_size);
716 #endif
717
718   /* shouldn't be called, since all sections are IN_MEMORY  */
719
720   return false;
721 }
722
723 /* Read the contents of a section.
724    buf points to a buffer of buf_size bytes to be filled with
725    section data (starting at offset into section)  */
726
727 static boolean
728 vms_get_section_contents_in_window (abfd, section, w, offset, count)
729      bfd *abfd ATTRIBUTE_UNUSED;
730      asection *section ATTRIBUTE_UNUSED;
731      bfd_window *w ATTRIBUTE_UNUSED;
732      file_ptr offset ATTRIBUTE_UNUSED;
733      bfd_size_type count ATTRIBUTE_UNUSED;
734 {
735 #if VMS_DEBUG
736   vms_debug (1, "vms_get_section_contents_in_window(%p, %s, %p, off %ld, count %d)\n",
737                  abfd, section->name, w, offset, (int)count);
738 #endif
739
740   /* shouldn't be called, since all sections are IN_MEMORY  */
741
742   return false;
743 }
744
745 /*-- Part 4.2, copy private data --------------------------------------------*/
746
747 /* Called to copy BFD general private data from one object file
748    to another.  */
749
750 static boolean
751 vms_bfd_copy_private_bfd_data (src, dest)
752      bfd *src ATTRIBUTE_UNUSED;
753      bfd *dest ATTRIBUTE_UNUSED;
754 {
755 #if VMS_DEBUG
756   vms_debug (1, "vms_bfd_copy_private_bfd_data(%p, %p)\n", src, dest);
757 #endif
758   return true;
759 }
760
761 /* Merge private BFD information from the BFD @var{ibfd} to the
762    the output file BFD @var{obfd} when linking.  Return <<true>>
763    on success, <<false>> on error.  Possible error returns are:
764
765    o <<bfd_error_no_memory>> -
766      Not enough memory exists to create private data for @var{obfd}.  */
767
768 static boolean
769 vms_bfd_merge_private_bfd_data (ibfd, obfd)
770      bfd *ibfd ATTRIBUTE_UNUSED;
771      bfd *obfd ATTRIBUTE_UNUSED;
772 {
773 #if VMS_DEBUG
774   vms_debug (1,"vms_bfd_merge_private_bfd_data(%p, %p)\n", ibfd, obfd);
775 #endif
776   return true;
777 }
778
779 /* Set private BFD flag information in the BFD @var{abfd}.
780    Return <<true>> on success, <<false>> on error.  Possible error
781    returns are:
782
783    o <<bfd_error_no_memory>> -
784      Not enough memory exists to create private data for @var{obfd}.  */
785
786 static boolean
787 vms_bfd_set_private_flags (abfd, flags)
788      bfd *abfd ATTRIBUTE_UNUSED;
789      flagword flags ATTRIBUTE_UNUSED;
790 {
791 #if VMS_DEBUG
792   vms_debug (1,"vms_bfd_set_private_flags(%p, %lx)\n", abfd, (long)flags);
793 #endif
794   return true;
795 }
796
797 /* Called to copy BFD private section data from one object file
798    to another.  */
799
800 static boolean
801 vms_bfd_copy_private_section_data (srcbfd, srcsec, dstbfd, dstsec)
802      bfd *srcbfd ATTRIBUTE_UNUSED;
803      asection *srcsec ATTRIBUTE_UNUSED;
804      bfd *dstbfd ATTRIBUTE_UNUSED;
805      asection *dstsec ATTRIBUTE_UNUSED;
806 {
807 #if VMS_DEBUG
808   vms_debug (1, "vms_bfd_copy_private_section_data(%p, %s, %p, %s)\n",
809                  srcbfd, srcsec->name, dstbfd, dstsec->name);
810 #endif
811   return true;
812 }
813
814 /* Called to copy BFD private symbol data from one object file
815    to another.  */
816
817 static boolean
818 vms_bfd_copy_private_symbol_data (ibfd, isym, obfd, osym)
819      bfd *ibfd ATTRIBUTE_UNUSED;
820      asymbol *isym ATTRIBUTE_UNUSED;
821      bfd *obfd ATTRIBUTE_UNUSED;
822      asymbol *osym ATTRIBUTE_UNUSED;
823 {
824 #if VMS_DEBUG
825   vms_debug (1, "vms_bfd_copy_private_symbol_data(%p, %s, %p, %s)\n",
826                  ibfd, isym->name, obfd, osym->name);
827 #endif
828   return true;
829 }
830
831 /*-- Part 4.3, core file ----------------------------------------------------*/
832
833 /* Return a read-only string explaining which program was running
834    when it failed and produced the core file abfd.  */
835
836 static char *
837 vms_core_file_failing_command (abfd)
838      bfd *abfd ATTRIBUTE_UNUSED;
839 {
840 #if VMS_DEBUG
841   vms_debug (1, "vms_core_file_failing_command(%p)\n", abfd);
842 #endif
843   return 0;
844 }
845
846 /* Returns the signal number which caused the core dump which
847    generated the file the BFD abfd is attached to.  */
848
849 static int
850 vms_core_file_failing_signal (abfd)
851      bfd *abfd ATTRIBUTE_UNUSED;
852 {
853 #if VMS_DEBUG
854   vms_debug (1, "vms_core_file_failing_signal(%p)\n", abfd);
855 #endif
856   return 0;
857 }
858
859 /* Return true if the core file attached to core_bfd was generated
860    by a run of the executable file attached to exec_bfd, false otherwise.  */
861
862 static boolean
863 vms_core_file_matches_executable_p (abfd, bbfd)
864      bfd *abfd ATTRIBUTE_UNUSED;
865      bfd *bbfd ATTRIBUTE_UNUSED;
866 {
867 #if VMS_DEBUG
868   vms_debug (1, "vms_core_file_matches_executable_p(%p, %p)\n", abfd, bbfd);
869 #endif
870   return false;
871 }
872
873 /*-- Part 4.4, archive ------------------------------------------------------*/
874
875 /* ???  do something with an archive map.
876    Return false on error, true otherwise.  */
877
878 static boolean
879 vms_slurp_armap (abfd)
880      bfd *abfd ATTRIBUTE_UNUSED;
881 {
882 #if VMS_DEBUG
883   vms_debug (1, "vms_slurp_armap(%p)\n", abfd);
884 #endif
885   return false;
886 }
887
888 /* ???  do something with an extended name table.
889    Return false on error, true otherwise.  */
890
891 static boolean
892 vms_slurp_extended_name_table (abfd)
893      bfd *abfd ATTRIBUTE_UNUSED;
894 {
895 #if VMS_DEBUG
896   vms_debug (1, "vms_slurp_extended_name_table(%p)\n", abfd);
897 #endif
898   return false;
899 }
900
901 /* ???  do something with an extended name table.
902    Return false on error, true otherwise.  */
903
904 static boolean
905 vms_construct_extended_name_table (abfd, tabloc, tablen, name)
906      bfd *abfd ATTRIBUTE_UNUSED;
907      char **tabloc ATTRIBUTE_UNUSED;
908      bfd_size_type *tablen ATTRIBUTE_UNUSED;
909      const char **name ATTRIBUTE_UNUSED;
910 {
911 #if VMS_DEBUG
912   vms_debug (1, "vms_construct_extended_name_table(%p)\n", abfd);
913 #endif
914   return false;
915 }
916
917 /* Truncate the name of an archive to match system-dependent restrictions  */
918
919 static void
920 vms_truncate_arname (abfd, pathname, arhdr)
921      bfd *abfd ATTRIBUTE_UNUSED;
922      const char *pathname ATTRIBUTE_UNUSED;
923      char *arhdr ATTRIBUTE_UNUSED;
924 {
925 #if VMS_DEBUG
926   vms_debug (1, "vms_truncate_arname(%p, %s, %s)\n", abfd, pathname, arhdr);
927 #endif
928   return;
929 }
930
931 /* ???  write archive map  */
932
933 static boolean
934 vms_write_armap (arch, elength, map, orl_count, stridx)
935      bfd *arch ATTRIBUTE_UNUSED;
936      unsigned int elength ATTRIBUTE_UNUSED;
937      struct orl *map ATTRIBUTE_UNUSED;
938      unsigned int orl_count ATTRIBUTE_UNUSED;
939      int stridx ATTRIBUTE_UNUSED;
940 {
941 #if VMS_DEBUG
942   vms_debug (1, "vms_write_armap(%p, %d, %p, %d %d)\n",
943         arch, elength, map, orl_count, stridx);
944 #endif
945   return true;
946 }
947
948 /* Read archive header ???  */
949
950 static PTR
951 vms_read_ar_hdr (abfd)
952     bfd * abfd ATTRIBUTE_UNUSED;
953 {
954 #if VMS_DEBUG
955   vms_debug (1, "vms_read_ar_hdr(%p)\n", abfd);
956 #endif
957   return (PTR)0;
958 }
959
960 /* Provided a BFD, @var{archive}, containing an archive and NULL, open
961    an input BFD on the first contained element and returns that.
962    Subsequent calls should pass the archive and the previous return value
963    to return a created BFD to the next contained element.
964    NULL is returned when there are no more.  */
965
966 static bfd *
967 vms_openr_next_archived_file (arch, prev)
968      bfd *arch ATTRIBUTE_UNUSED;
969      bfd *prev ATTRIBUTE_UNUSED;
970 {
971 #if VMS_DEBUG
972   vms_debug (1, "vms_openr_next_archived_file(%p, %p)\n", arch, prev);
973 #endif
974   return NULL;
975 }
976
977 /* Return the BFD which is referenced by the symbol in ABFD indexed by
978    INDEX.  INDEX should have been returned by bfd_get_next_mapent.  */
979
980 static bfd *
981 vms_get_elt_at_index (abfd, index)
982      bfd *abfd;
983      symindex index;
984 {
985 #if VMS_DEBUG
986   vms_debug (1, "vms_get_elt_at_index(%p, %p)\n", abfd, index);
987 #endif
988   return _bfd_generic_get_elt_at_index(abfd, index);
989 }
990
991 /* ???
992    -> bfd_generic_stat_arch_elt  */
993
994 static int
995 vms_generic_stat_arch_elt (abfd, st)
996      bfd *abfd;
997      struct stat *st;
998 {
999 #if VMS_DEBUG
1000   vms_debug (1, "vms_generic_stat_arch_elt(%p, %p)\n", abfd, st);
1001 #endif
1002   return bfd_generic_stat_arch_elt (abfd, st);
1003 }
1004
1005 /* This is a new function in bfd 2.5  */
1006
1007 static boolean
1008 vms_update_armap_timestamp (abfd)
1009      bfd *abfd ATTRIBUTE_UNUSED;
1010 {
1011 #if VMS_DEBUG
1012   vms_debug (1, "vms_update_armap_timestamp(%p)\n", abfd);
1013 #endif
1014   return true;
1015 }
1016
1017 /*-- Part 4.5, symbols --------------------------------------------------------*/
1018
1019 /* Return the number of bytes required to store a vector of pointers
1020    to asymbols for all the symbols in the BFD abfd, including a
1021    terminal NULL pointer. If there are no symbols in the BFD,
1022    then return 0.  If an error occurs, return -1.  */
1023
1024 static long
1025 vms_get_symtab_upper_bound (abfd)
1026      bfd *abfd;
1027 {
1028 #if VMS_DEBUG
1029   vms_debug (1, "vms_get_symtab_upper_bound(%p), %d symbols\n", abfd, PRIV (gsd_sym_count));
1030 #endif
1031   return (PRIV (gsd_sym_count)+1) * sizeof (asymbol *);
1032 }
1033
1034 /* Copy symbols from hash table to symbol vector
1035
1036    called from bfd_hash_traverse in vms_get_symtab
1037    init counter to 0 if entry == 0  */
1038
1039 static boolean
1040 copy_symbols (entry, arg)
1041      struct bfd_hash_entry *entry;
1042      PTR arg;
1043 {
1044   bfd *abfd = (bfd *) arg;
1045
1046   if (entry == NULL)    /* init counter */
1047     PRIV (symnum) = 0;
1048   else                  /* fill vector, inc counter */
1049     PRIV (symcache)[PRIV (symnum)++] = ((vms_symbol_entry *)entry)->symbol;
1050
1051   return true;
1052 }
1053
1054 /* Read the symbols from the BFD abfd, and fills in the vector
1055    location with pointers to the symbols and a trailing NULL.
1056
1057    return # of symbols read  */
1058
1059 static long
1060 vms_get_symtab (abfd, symbols)
1061      bfd *abfd;
1062      asymbol **symbols;
1063 {
1064 #if VMS_DEBUG
1065   vms_debug (1, "vms_get_symtab(%p, <ret>)\n", abfd);
1066 #endif
1067
1068         /* init counter */
1069   (void)copy_symbols((struct bfd_hash_entry *)0, abfd);
1070
1071         /* traverse table and fill symbols vector */
1072
1073   PRIV (symcache) = symbols;
1074   bfd_hash_traverse(PRIV (vms_symbol_table), copy_symbols, (PTR)abfd);
1075
1076   symbols[PRIV (gsd_sym_count)] = NULL;
1077
1078   return PRIV (gsd_sym_count);
1079 }
1080
1081 /* Print symbol to file according to how. how is one of
1082    bfd_print_symbol_name        just print the name
1083    bfd_print_symbol_more        print more (???)
1084    bfd_print_symbol_all print all we know, which is not much right now :-)  */
1085
1086 static void
1087 vms_print_symbol (abfd, file, symbol, how)
1088      bfd *abfd;
1089      PTR file;
1090      asymbol *symbol;
1091      bfd_print_symbol_type how;
1092 {
1093 #if VMS_DEBUG
1094   vms_debug (1, "vms_print_symbol(%p, %p, %p, %d)\n", abfd, file, symbol, how);
1095 #endif
1096
1097   switch (how)
1098     {
1099       case bfd_print_symbol_name:
1100       case bfd_print_symbol_more:
1101         fprintf ((FILE *)file," %s", symbol->name);
1102       break;
1103
1104       case bfd_print_symbol_all:
1105         {
1106           const char *section_name = symbol->section->name;
1107
1108           bfd_print_symbol_vandf (abfd, (PTR)file, symbol);
1109
1110           fprintf ((FILE *)file," %-8s %s", section_name, symbol->name);
1111         }
1112       break;
1113     }
1114   return;
1115 }
1116
1117 /* Return information about symbol in ret.
1118
1119    fill type, value and name
1120    type:
1121         A       absolute
1122         B       bss segment symbol
1123         C       common symbol
1124         D       data segment symbol
1125         f       filename
1126         t       a static function symbol
1127         T       text segment symbol
1128         U       undefined
1129         -       debug  */
1130
1131 static void
1132 vms_get_symbol_info (abfd, symbol, ret)
1133      bfd *abfd ATTRIBUTE_UNUSED;
1134      asymbol *symbol;
1135      symbol_info *ret;
1136 {
1137   asection *sec;
1138
1139 #if VMS_DEBUG
1140   vms_debug (1, "vms_get_symbol_info(%p, %p, %p)\n", abfd, symbol, ret);
1141 #endif
1142
1143   sec = symbol->section;
1144
1145   if (ret == 0)
1146     return;
1147
1148   if (bfd_is_com_section (sec))
1149     ret->type = 'C';
1150   else if (bfd_is_abs_section (sec))
1151     ret->type = 'A';
1152   else if (bfd_is_und_section (sec))
1153     ret->type = 'U';
1154   else if (bfd_is_ind_section (sec))
1155     ret->type = 'I';
1156   else if (bfd_get_section_flags (abfd, sec) & SEC_CODE)
1157     ret->type = 'T';
1158   else if (bfd_get_section_flags (abfd, sec) & SEC_DATA)
1159     ret->type = 'D';
1160   else if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
1161     ret->type = 'B';
1162   else
1163     ret->type = '-';
1164
1165   if (ret->type != 'U')
1166     ret->value = symbol->value + symbol->section->vma;
1167   else
1168     ret->value = 0;
1169   ret->name = symbol->name;
1170
1171   return;
1172 }
1173
1174 /* Return true if the given symbol sym in the BFD abfd is
1175    a compiler generated local label, else return false.  */
1176
1177 static boolean
1178 vms_bfd_is_local_label_name (abfd, name)
1179      bfd *abfd ATTRIBUTE_UNUSED;
1180      const char *name;
1181 {
1182 #if VMS_DEBUG
1183   vms_debug (1, "vms_bfd_is_local_label_name(%p, %s)\n", abfd, name);
1184 #endif
1185   return name[0] == '$';
1186 }
1187
1188 /* Get source line number for symbol  */
1189
1190 static alent *
1191 vms_get_lineno (abfd, symbol)
1192      bfd *abfd ATTRIBUTE_UNUSED;
1193      asymbol *symbol ATTRIBUTE_UNUSED;
1194 {
1195 #if VMS_DEBUG
1196   vms_debug (1, "vms_get_lineno(%p, %p)\n", abfd, symbol);
1197 #endif
1198   return 0;
1199 }
1200
1201 /* Provided a BFD, a section and an offset into the section, calculate and
1202    return the name of the source file and the line nearest to the wanted
1203    location.  */
1204
1205 static boolean
1206 vms_find_nearest_line (abfd, section, symbols, offset, file, func, line)
1207      bfd *abfd ATTRIBUTE_UNUSED;
1208      asection *section ATTRIBUTE_UNUSED;
1209      asymbol **symbols ATTRIBUTE_UNUSED;
1210      bfd_vma offset ATTRIBUTE_UNUSED;
1211      const char **file ATTRIBUTE_UNUSED;
1212      const char **func ATTRIBUTE_UNUSED;
1213      unsigned int *line ATTRIBUTE_UNUSED;
1214 {
1215 #if VMS_DEBUG
1216   vms_debug (1, "vms_find_nearest_line(%p, %s, %p, %ld, <ret>, <ret>, <ret>)\n",
1217               abfd, section->name, symbols, (long int)offset);
1218 #endif
1219   return false;
1220 }
1221
1222 /* Back-door to allow format-aware applications to create debug symbols
1223    while using BFD for everything else.  Currently used by the assembler
1224    when creating COFF files.  */
1225
1226 static asymbol *
1227 vms_bfd_make_debug_symbol (abfd, ptr, size)
1228      bfd *abfd ATTRIBUTE_UNUSED;
1229      void *ptr ATTRIBUTE_UNUSED;
1230      unsigned long size ATTRIBUTE_UNUSED;
1231 {
1232 #if VMS_DEBUG
1233   vms_debug (1, "vms_bfd_make_debug_symbol(%p, %p, %ld)\n", abfd, ptr, size);
1234 #endif
1235   return 0;
1236 }
1237
1238 /* Read minisymbols.  For minisymbols, we use the unmodified a.out
1239    symbols.  The minisymbol_to_symbol function translates these into
1240    BFD asymbol structures.  */
1241
1242 static long
1243 vms_read_minisymbols (abfd, dynamic, minisymsp, sizep)
1244      bfd *abfd;
1245      boolean dynamic;
1246      PTR *minisymsp;
1247      unsigned int *sizep;
1248 {
1249 #if VMS_DEBUG
1250   vms_debug (1, "vms_read_minisymbols(%p, %d, %p, %d)\n", abfd, dynamic, minisymsp, *sizep);
1251 #endif
1252   return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
1253 }
1254
1255 /* Convert a minisymbol to a BFD asymbol.  A minisymbol is just an
1256    unmodified a.out symbol.  The SYM argument is a structure returned
1257    by bfd_make_empty_symbol, which we fill in here.  */
1258
1259 static asymbol *
1260 vms_minisymbol_to_symbol (abfd, dynamic, minisym, sym)
1261      bfd *abfd;
1262      boolean dynamic;
1263      const PTR minisym;
1264      asymbol *sym;
1265 {
1266 #if VMS_DEBUG
1267   vms_debug (1, "vms_minisymbol_to_symbol(%p, %d, %p, %p)\n", abfd, dynamic, minisym, sym);
1268 #endif
1269   return _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym);
1270 }
1271
1272 /*-- Part 4.6, relocations --------------------------------------------------*/
1273
1274 /* Return the number of bytes required to store the relocation information
1275    associated with section sect attached to bfd abfd.
1276    If an error occurs, return -1.  */
1277
1278 static long
1279 vms_get_reloc_upper_bound (abfd, section)
1280      bfd *abfd ATTRIBUTE_UNUSED;
1281      asection *section ATTRIBUTE_UNUSED;
1282 {
1283 #if VMS_DEBUG
1284   vms_debug (1, "vms_get_reloc_upper_bound(%p, %s)\n", abfd, section->name);
1285 #endif
1286   return -1L;
1287 }
1288
1289 /* Call the back end associated with the open BFD abfd and translate the
1290    external form of the relocation information attached to sec into the
1291    internal canonical form.  Place the table into memory at loc, which has
1292    been preallocated, usually by a call to bfd_get_reloc_upper_bound.
1293    Returns the number of relocs, or -1 on error.  */
1294
1295 static long
1296 vms_canonicalize_reloc (abfd, section, location, symbols)
1297      bfd *abfd ATTRIBUTE_UNUSED;
1298      asection *section ATTRIBUTE_UNUSED;
1299      arelent **location ATTRIBUTE_UNUSED;
1300      asymbol **symbols ATTRIBUTE_UNUSED;
1301 {
1302 #if VMS_DEBUG
1303   vms_debug (1, "vms_canonicalize_reloc(%p, %s, <ret>, <ret>)\n", abfd, section->name);
1304 #endif
1305   return false;
1306 }
1307
1308 /*---------------------------------------------------------------------------*/
1309 /* this is just copied from ecoff-alpha, needs to be fixed probably */
1310
1311 /* How to process the various reloc types.  */
1312
1313 static bfd_reloc_status_type
1314 reloc_nil (abfd, reloc, sym, data, sec, output_bfd, error_message)
1315      bfd *abfd ATTRIBUTE_UNUSED;
1316      arelent *reloc ATTRIBUTE_UNUSED;
1317      asymbol *sym ATTRIBUTE_UNUSED;
1318      PTR data ATTRIBUTE_UNUSED;
1319      asection *sec ATTRIBUTE_UNUSED;
1320      bfd *output_bfd ATTRIBUTE_UNUSED;
1321      char **error_message ATTRIBUTE_UNUSED;
1322 {
1323 #if VMS_DEBUG
1324   vms_debug (1, "reloc_nil(abfd %p, output_bfd %p)\n", abfd, output_bfd);
1325   vms_debug (2, "In section %s, symbol %s\n",
1326         sec->name, sym->name);
1327   vms_debug (2, "reloc sym %s, addr %08lx, addend %08lx, reloc is a %s\n",
1328                 reloc->sym_ptr_ptr[0]->name,
1329                 (unsigned long)reloc->address,
1330                 (unsigned long)reloc->addend, reloc->howto->name);
1331   vms_debug (2, "data at %p\n", data);
1332 /*  _bfd_hexdump (2, data, bfd_get_reloc_size(reloc->howto),0); */
1333 #endif
1334
1335   return bfd_reloc_ok;
1336 }
1337
1338 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
1339    from smaller values.  Start with zero, widen, *then* decrement.  */
1340 #define MINUS_ONE       (((bfd_vma)0) - 1)
1341
1342 static reloc_howto_type alpha_howto_table[] =
1343 {
1344   HOWTO (ALPHA_R_IGNORE,        /* type */
1345          0,                     /* rightshift */
1346          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1347          8,                     /* bitsize */
1348          true,                  /* pc_relative */
1349          0,                     /* bitpos */
1350          complain_overflow_dont, /* complain_on_overflow */
1351          reloc_nil,             /* special_function */
1352          "IGNORE",              /* name */
1353          true,                  /* partial_inplace */
1354          0,                     /* src_mask */
1355          0,                     /* dst_mask */
1356          true),                 /* pcrel_offset */
1357
1358   /* A 64 bit reference to a symbol.  */
1359   HOWTO (ALPHA_R_REFQUAD,       /* type */
1360          0,                     /* rightshift */
1361          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1362          64,                    /* bitsize */
1363          false,                 /* pc_relative */
1364          0,                     /* bitpos */
1365          complain_overflow_bitfield, /* complain_on_overflow */
1366          reloc_nil,             /* special_function */
1367          "REFQUAD",             /* name */
1368          true,                  /* partial_inplace */
1369          MINUS_ONE,             /* src_mask */
1370          MINUS_ONE,             /* dst_mask */
1371          false),                /* pcrel_offset */
1372
1373   /* A 21 bit branch.  The native assembler generates these for
1374      branches within the text segment, and also fills in the PC
1375      relative offset in the instruction.  */
1376   HOWTO (ALPHA_R_BRADDR,        /* type */
1377          2,                     /* rightshift */
1378          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1379          21,                    /* bitsize */
1380          true,                  /* pc_relative */
1381          0,                     /* bitpos */
1382          complain_overflow_signed, /* complain_on_overflow */
1383          reloc_nil,             /* special_function */
1384          "BRADDR",              /* name */
1385          true,                  /* partial_inplace */
1386          0x1fffff,              /* src_mask */
1387          0x1fffff,              /* dst_mask */
1388          false),                /* pcrel_offset */
1389
1390   /* A hint for a jump to a register.  */
1391   HOWTO (ALPHA_R_HINT,          /* type */
1392          2,                     /* rightshift */
1393          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1394          14,                    /* bitsize */
1395          true,                  /* pc_relative */
1396          0,                     /* bitpos */
1397          complain_overflow_dont, /* complain_on_overflow */
1398          reloc_nil,             /* special_function */
1399          "HINT",                /* name */
1400          true,                  /* partial_inplace */
1401          0x3fff,                /* src_mask */
1402          0x3fff,                /* dst_mask */
1403          false),                /* pcrel_offset */
1404
1405   /* 16 bit PC relative offset.  */
1406   HOWTO (ALPHA_R_SREL16,        /* type */
1407          0,                     /* rightshift */
1408          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1409          16,                    /* bitsize */
1410          true,                  /* pc_relative */
1411          0,                     /* bitpos */
1412          complain_overflow_signed, /* complain_on_overflow */
1413          reloc_nil,             /* special_function */
1414          "SREL16",              /* name */
1415          true,                  /* partial_inplace */
1416          0xffff,                /* src_mask */
1417          0xffff,                /* dst_mask */
1418          false),                /* pcrel_offset */
1419
1420   /* 32 bit PC relative offset.  */
1421   HOWTO (ALPHA_R_SREL32,        /* type */
1422          0,                     /* rightshift */
1423          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1424          32,                    /* bitsize */
1425          true,                  /* pc_relative */
1426          0,                     /* bitpos */
1427          complain_overflow_signed, /* complain_on_overflow */
1428          reloc_nil,             /* special_function */
1429          "SREL32",              /* name */
1430          true,                  /* partial_inplace */
1431          0xffffffff,            /* src_mask */
1432          0xffffffff,            /* dst_mask */
1433          false),                /* pcrel_offset */
1434
1435   /* A 64 bit PC relative offset.  */
1436   HOWTO (ALPHA_R_SREL64,        /* type */
1437          0,                     /* rightshift */
1438          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1439          64,                    /* bitsize */
1440          true,                  /* pc_relative */
1441          0,                     /* bitpos */
1442          complain_overflow_signed, /* complain_on_overflow */
1443          reloc_nil,             /* special_function */
1444          "SREL64",              /* name */
1445          true,                  /* partial_inplace */
1446          MINUS_ONE,             /* src_mask */
1447          MINUS_ONE,             /* dst_mask */
1448          false),                /* pcrel_offset */
1449
1450   /* Push a value on the reloc evaluation stack.  */
1451   HOWTO (ALPHA_R_OP_PUSH,       /* type */
1452          0,                     /* rightshift */
1453          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1454          0,                     /* bitsize */
1455          false,                 /* pc_relative */
1456          0,                     /* bitpos */
1457          complain_overflow_dont, /* complain_on_overflow */
1458          reloc_nil,             /* special_function */
1459          "OP_PUSH",             /* name */
1460          false,                 /* partial_inplace */
1461          0,                     /* src_mask */
1462          0,                     /* dst_mask */
1463          false),                /* pcrel_offset */
1464
1465   /* Store the value from the stack at the given address.  Store it in
1466      a bitfield of size r_size starting at bit position r_offset.  */
1467   HOWTO (ALPHA_R_OP_STORE,      /* type */
1468          0,                     /* rightshift */
1469          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1470          64,                    /* bitsize */
1471          false,                 /* pc_relative */
1472          0,                     /* bitpos */
1473          complain_overflow_dont, /* complain_on_overflow */
1474          reloc_nil,             /* special_function */
1475          "OP_STORE",            /* name */
1476          false,                 /* partial_inplace */
1477          0,                     /* src_mask */
1478          MINUS_ONE,             /* dst_mask */
1479          false),                /* pcrel_offset */
1480
1481   /* Subtract the reloc address from the value on the top of the
1482      relocation stack.  */
1483   HOWTO (ALPHA_R_OP_PSUB,       /* type */
1484          0,                     /* rightshift */
1485          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1486          0,                     /* bitsize */
1487          false,                 /* pc_relative */
1488          0,                     /* bitpos */
1489          complain_overflow_dont, /* complain_on_overflow */
1490          reloc_nil,             /* special_function */
1491          "OP_PSUB",             /* name */
1492          false,                 /* partial_inplace */
1493          0,                     /* src_mask */
1494          0,                     /* dst_mask */
1495          false),                /* pcrel_offset */
1496
1497   /* Shift the value on the top of the relocation stack right by the
1498      given value.  */
1499   HOWTO (ALPHA_R_OP_PRSHIFT,    /* type */
1500          0,                     /* rightshift */
1501          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1502          0,                     /* bitsize */
1503          false,                 /* pc_relative */
1504          0,                     /* bitpos */
1505          complain_overflow_dont, /* complain_on_overflow */
1506          reloc_nil,             /* special_function */
1507          "OP_PRSHIFT",          /* name */
1508          false,                 /* partial_inplace */
1509          0,                     /* src_mask */
1510          0,                     /* dst_mask */
1511          false),                /* pcrel_offset */
1512
1513   /* Hack. Linkage is done by linker.  */
1514   HOWTO (ALPHA_R_LINKAGE,       /* type */
1515          0,                     /* rightshift */
1516          8,                     /* size (0 = byte, 1 = short, 2 = long) */
1517          256,                   /* bitsize */
1518          false,                 /* pc_relative */
1519          0,                     /* bitpos */
1520          complain_overflow_dont, /* complain_on_overflow */
1521          reloc_nil,             /* special_function */
1522          "LINKAGE",             /* name */
1523          false,                 /* partial_inplace */
1524          0,                     /* src_mask */
1525          0,                     /* dst_mask */
1526          false),                /* pcrel_offset */
1527
1528   /* A 32 bit reference to a symbol.  */
1529   HOWTO (ALPHA_R_REFLONG,       /* type */
1530          0,                     /* rightshift */
1531          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1532          32,                    /* bitsize */
1533          false,                 /* pc_relative */
1534          0,                     /* bitpos */
1535          complain_overflow_bitfield, /* complain_on_overflow */
1536          reloc_nil,             /* special_function */
1537          "REFLONG",             /* name */
1538          true,                  /* partial_inplace */
1539          0xffffffff,            /* src_mask */
1540          0xffffffff,            /* dst_mask */
1541          false),                /* pcrel_offset */
1542
1543   /* A 64 bit reference to a procedure, written as 32 bit value.  */
1544   HOWTO (ALPHA_R_CODEADDR,      /* type */
1545          0,                     /* rightshift */
1546          4,                     /* size (0 = byte, 1 = short, 2 = long) */
1547          64,                    /* bitsize */
1548          false,                 /* pc_relative */
1549          0,                     /* bitpos */
1550          complain_overflow_signed,/* complain_on_overflow */
1551          reloc_nil,             /* special_function */
1552          "CODEADDR",            /* name */
1553          false,                 /* partial_inplace */
1554          0xffffffff,            /* src_mask */
1555          0xffffffff,            /* dst_mask */
1556          false),                /* pcrel_offset */
1557
1558 };
1559
1560 /* Return a pointer to a howto structure which, when invoked, will perform
1561    the relocation code on data from the architecture noted.  */
1562
1563 static const struct reloc_howto_struct *
1564 vms_bfd_reloc_type_lookup (abfd, code)
1565      bfd *abfd ATTRIBUTE_UNUSED;
1566      bfd_reloc_code_real_type code;
1567 {
1568   int alpha_type;
1569
1570 #if VMS_DEBUG
1571   vms_debug (1, "vms_bfd_reloc_type_lookup(%p, %d)\t", abfd, code);
1572 #endif
1573
1574   switch (code)
1575     {
1576       case BFD_RELOC_16:                alpha_type = ALPHA_R_SREL16;    break;
1577       case BFD_RELOC_32:                alpha_type = ALPHA_R_REFLONG;   break;
1578       case BFD_RELOC_64:                alpha_type = ALPHA_R_REFQUAD;   break;
1579       case BFD_RELOC_CTOR:              alpha_type = ALPHA_R_REFQUAD;   break;
1580       case BFD_RELOC_23_PCREL_S2:       alpha_type = ALPHA_R_BRADDR;    break;
1581       case BFD_RELOC_ALPHA_HINT:        alpha_type = ALPHA_R_HINT;      break;
1582       case BFD_RELOC_16_PCREL:          alpha_type = ALPHA_R_SREL16;    break;
1583       case BFD_RELOC_32_PCREL:          alpha_type = ALPHA_R_SREL32;    break;
1584       case BFD_RELOC_64_PCREL:          alpha_type = ALPHA_R_SREL64;    break;
1585       case BFD_RELOC_ALPHA_LINKAGE:     alpha_type = ALPHA_R_LINKAGE;   break;
1586       case BFD_RELOC_ALPHA_CODEADDR:    alpha_type = ALPHA_R_CODEADDR;  break;
1587       default:
1588         (*_bfd_error_handler) ("reloc (%d) is *UNKNOWN*", code);
1589         return (const struct reloc_howto_struct *) NULL;
1590     }
1591 #if VMS_DEBUG
1592   vms_debug (2, "reloc is %s\n", alpha_howto_table[alpha_type].name);
1593 #endif
1594   return &alpha_howto_table[alpha_type];
1595 }
1596
1597 /*-- Part 4.7, writing an object file ---------------------------------------*/
1598
1599 /* Set the architecture and machine type in BFD abfd to arch and mach.
1600    Find the correct pointer to a structure and insert it into the arch_info
1601    pointer.  */
1602
1603 static boolean
1604 vms_set_arch_mach (abfd, arch, mach)
1605      bfd *abfd;
1606      enum bfd_architecture arch ATTRIBUTE_UNUSED;
1607      unsigned long mach ATTRIBUTE_UNUSED;
1608 {
1609 #if VMS_DEBUG
1610   vms_debug (1, "vms_set_arch_mach(%p, %d, %ld)\n", abfd, arch, mach);
1611 #endif
1612   abfd->arch_info = bfd_scan_arch("alpha");
1613
1614   return true;
1615 }
1616
1617 /* Sets the contents of the section section in BFD abfd to the data starting
1618    in memory at data. The data is written to the output section starting at
1619    offset offset for count bytes.
1620
1621    Normally true is returned, else false. Possible error returns are:
1622    o bfd_error_no_contents - The output section does not have the
1623         SEC_HAS_CONTENTS attribute, so nothing can be written to it.
1624    o and some more too  */
1625
1626 static boolean
1627 vms_set_section_contents (abfd, section, location, offset, count)
1628      bfd *abfd;
1629      asection *section;
1630      PTR location;
1631      file_ptr offset;
1632      bfd_size_type count;
1633 {
1634 #if VMS_DEBUG
1635   vms_debug (1, "vms_set_section_contents(%p, sec %s, loc %p, off %ld, count %d)\n",
1636                                         abfd, section->name, location, (long int)offset, (int)count);
1637   vms_debug (2, "secraw %d, seccooked %d\n", (int)section->_raw_size, (int)section->_cooked_size);
1638 #endif
1639   return _bfd_save_vms_section(abfd, section, location, offset, count);
1640 }
1641
1642 /*-- Part 4.8, linker -------------------------------------------------------*/
1643
1644 /* Get the size of the section headers.  */
1645
1646 static int
1647 vms_sizeof_headers (abfd, reloc)
1648      bfd *abfd ATTRIBUTE_UNUSED;
1649      boolean reloc ATTRIBUTE_UNUSED;
1650 {
1651 #if VMS_DEBUG
1652   vms_debug (1, "vms_sizeof_headers(%p, %s)\n", abfd, (reloc)?"True":"False");
1653 #endif
1654   return 0;
1655 }
1656
1657 /* Provides default handling of relocation effort for back ends
1658    which can't be bothered to do it efficiently.  */
1659
1660 static bfd_byte *
1661 vms_bfd_get_relocated_section_contents (abfd, link_info, link_order, data,
1662                                          relocateable, symbols)
1663      bfd *abfd ATTRIBUTE_UNUSED;
1664      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
1665      struct bfd_link_order *link_order ATTRIBUTE_UNUSED;
1666      bfd_byte *data ATTRIBUTE_UNUSED;
1667      boolean relocateable ATTRIBUTE_UNUSED;
1668      asymbol **symbols ATTRIBUTE_UNUSED;
1669 {
1670 #if VMS_DEBUG
1671   vms_debug (1, "vms_bfd_get_relocated_section_contents(%p, %p, %p, %p, %s, %p)\n",
1672                         abfd, link_info, link_order, data, (relocateable)?"True":"False", symbols);
1673 #endif
1674   return 0;
1675 }
1676
1677 /* ???  */
1678
1679 static boolean
1680 vms_bfd_relax_section (abfd, section, link_info, again)
1681      bfd *abfd ATTRIBUTE_UNUSED;
1682      asection *section ATTRIBUTE_UNUSED;
1683      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
1684      boolean *again ATTRIBUTE_UNUSED;
1685 {
1686 #if VMS_DEBUG
1687   vms_debug (1, "vms_bfd_relax_section(%p, %s, %p, <ret>)\n",
1688                                         abfd, section->name, link_info);
1689 #endif
1690   return true;
1691 }
1692
1693 static boolean
1694 vms_bfd_gc_sections (abfd, link_info)
1695      bfd *abfd ATTRIBUTE_UNUSED;
1696      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
1697 {
1698 #if VMS_DEBUG
1699   vms_debug (1, "vms_bfd_gc_sections(%p, %p)\n", abfd, link_info);
1700 #endif
1701   return true;
1702 }
1703
1704 static boolean
1705 vms_bfd_merge_sections (abfd, link_info)
1706      bfd *abfd ATTRIBUTE_UNUSED;
1707      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
1708 {
1709 #if VMS_DEBUG
1710   vms_debug (1, "vms_bfd_merge_sections(%p, %p)\n", abfd, link_info);
1711 #endif
1712   return true;
1713 }
1714
1715 /* Create a hash table for the linker.  Different backends store
1716    different information in this table.  */
1717
1718 static struct bfd_link_hash_table *
1719 vms_bfd_link_hash_table_create (abfd)
1720      bfd *abfd ATTRIBUTE_UNUSED;
1721 {
1722 #if VMS_DEBUG
1723   vms_debug (1, "vms_bfd_link_hash_table_create(%p)\n", abfd);
1724 #endif
1725   return 0;
1726 }
1727
1728 /* Free a linker hash table.  */
1729
1730 static void
1731 vms_bfd_link_hash_table_free (hash)
1732      struct bfd_link_hash_table *hash ATTRIBUTE_UNUSED;
1733 {
1734 #if VMS_DEBUG
1735   vms_debug (1, "vms_bfd_link_hash_table_free(%p)\n", abfd);
1736 #endif
1737 }
1738
1739 /* Add symbols from this object file into the hash table.  */
1740
1741 static boolean
1742 vms_bfd_link_add_symbols (abfd, link_info)
1743      bfd *abfd ATTRIBUTE_UNUSED;
1744      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
1745 {
1746 #if VMS_DEBUG
1747   vms_debug (1, "vms_bfd_link_add_symbols(%p, %p)\n", abfd, link_info);
1748 #endif
1749   return false;
1750 }
1751
1752 /* Do a link based on the link_order structures attached to each
1753    section of the BFD.  */
1754
1755 static boolean
1756 vms_bfd_final_link (abfd, link_info)
1757      bfd *abfd ATTRIBUTE_UNUSED;
1758      struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
1759 {
1760 #if VMS_DEBUG
1761   vms_debug (1, "vms_bfd_final_link(%p, %p)\n", abfd, link_info);
1762 #endif
1763   return true;
1764 }
1765
1766 /* Should this section be split up into smaller pieces during linking.  */
1767
1768 static boolean
1769 vms_bfd_link_split_section (abfd, section)
1770      bfd *abfd ATTRIBUTE_UNUSED;
1771      asection *section ATTRIBUTE_UNUSED;
1772 {
1773 #if VMS_DEBUG
1774   vms_debug (1, "vms_bfd_link_split_section(%p, %s)\n", abfd, section->name);
1775 #endif
1776   return false;
1777 }
1778
1779 /*-- Part 4.9, dynamic symbols and relocations ------------------------------*/
1780
1781 /* Get the amount of memory required to hold the dynamic symbols.  */
1782
1783 static long
1784 vms_get_dynamic_symtab_upper_bound (abfd)
1785      bfd *abfd ATTRIBUTE_UNUSED;
1786 {
1787 #if VMS_DEBUG
1788   vms_debug (1, "vms_get_dynamic_symtab_upper_bound(%p)\n", abfd);
1789 #endif
1790   return 0;
1791 }
1792
1793 static boolean
1794 vms_bfd_print_private_bfd_data (abfd, file)
1795     bfd *abfd ATTRIBUTE_UNUSED;
1796     void *file ATTRIBUTE_UNUSED;
1797 {
1798 #if VMS_DEBUG
1799   vms_debug (1, "vms_bfd_print_private_bfd_data(%p)\n", abfd);
1800 #endif
1801   return 0;
1802 }
1803
1804 /* Read in the dynamic symbols.  */
1805
1806 static long
1807 vms_canonicalize_dynamic_symtab (abfd, symbols)
1808      bfd *abfd ATTRIBUTE_UNUSED;
1809      asymbol **symbols ATTRIBUTE_UNUSED;
1810 {
1811 #if VMS_DEBUG
1812   vms_debug (1, "vms_canonicalize_dynamic_symtab(%p, <ret>)\n", abfd);
1813 #endif
1814   return 0L;
1815 }
1816
1817 /* Get the amount of memory required to hold the dynamic relocs.  */
1818
1819 static long
1820 vms_get_dynamic_reloc_upper_bound (abfd)
1821      bfd *abfd ATTRIBUTE_UNUSED;
1822 {
1823 #if VMS_DEBUG
1824   vms_debug (1, "vms_get_dynamic_reloc_upper_bound(%p)\n", abfd);
1825 #endif
1826   return 0L;
1827 }
1828
1829 /* Read in the dynamic relocs.  */
1830
1831 static long
1832 vms_canonicalize_dynamic_reloc (abfd, arel, symbols)
1833      bfd *abfd ATTRIBUTE_UNUSED;
1834      arelent **arel ATTRIBUTE_UNUSED;
1835      asymbol **symbols ATTRIBUTE_UNUSED;
1836 {
1837 #if VMS_DEBUG
1838   vms_debug (1, "vms_canonicalize_dynamic_reloc(%p)\n", abfd);
1839 #endif
1840   return 0L;
1841 }