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