2010-06-25 Tristan Gingold <gingold@adacore.com>
[external/binutils.git] / bfd / vms-alpha.c
1 /* vms.c -- BFD back-end for EVAX (openVMS/Alpha) files.
2    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3    2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5    Initial version written by Klaus Kaempf (kkaempf@rmi.de)
6    Major rewrite by Adacore.
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 3 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., 51 Franklin Street - Fifth Floor, Boston,
21    MA 02110-1301, USA.  */
22
23 /* TODO:
24    o  overlayed sections
25    o  PIC
26    o  Generation of shared image
27    o  Relocation optimizations
28    o  EISD for the stack
29    o  Vectors isect
30    o  64 bits sections
31    o  Entry point
32    o  LIB$INITIALIZE
33    o  protected sections (for messages)
34    ...
35 */
36
37 #include "sysdep.h"
38 #include "bfd.h"
39 #include "bfdlink.h"
40 #include "libbfd.h"
41 #include "bfdver.h"
42
43 #include "vms.h"
44 #include "vms/eihd.h"
45 #include "vms/eiha.h"
46 #include "vms/eihi.h"
47 #include "vms/eihs.h"
48 #include "vms/eisd.h"
49 #include "vms/dmt.h"
50 #include "vms/dst.h"
51 #include "vms/eihvn.h"
52 #include "vms/eobjrec.h"
53 #include "vms/egsd.h"
54 #include "vms/egps.h"
55 #include "vms/esgps.h"
56 #include "vms/eeom.h"
57 #include "vms/emh.h"
58 #include "vms/eiaf.h"
59 #include "vms/shl.h"
60 #include "vms/eicp.h"
61 #include "vms/etir.h"
62 #include "vms/egsy.h"
63 #include "vms/esdf.h"
64 #include "vms/esdfm.h"
65 #include "vms/esdfv.h"
66 #include "vms/esrf.h"
67 #include "vms/egst.h"
68 #include "vms/eidc.h"
69 #include "vms/dsc.h"
70 #include "vms/prt.h"
71 #include "vms/internal.h"
72 \f
73
74 #define MIN(a,b) ((a) < (b) ? (a) : (b))
75
76 /* The r_type field in a reloc is one of the following values.  */
77 #define ALPHA_R_IGNORE          0
78 #define ALPHA_R_REFQUAD         1
79 #define ALPHA_R_BRADDR          2
80 #define ALPHA_R_HINT            3
81 #define ALPHA_R_SREL16          4
82 #define ALPHA_R_SREL32          5
83 #define ALPHA_R_SREL64          6
84 #define ALPHA_R_OP_PUSH         7
85 #define ALPHA_R_OP_STORE        8
86 #define ALPHA_R_OP_PSUB         9
87 #define ALPHA_R_OP_PRSHIFT      10
88 #define ALPHA_R_LINKAGE         11
89 #define ALPHA_R_REFLONG         12
90 #define ALPHA_R_CODEADDR        13
91 #define ALPHA_R_NOP             14
92 #define ALPHA_R_BSR             15
93 #define ALPHA_R_LDA             16
94 #define ALPHA_R_BOH             17
95
96 /* These are used with DST_S_C_LINE_NUM.  */
97 #define DST_S_C_LINE_NUM_HEADER_SIZE 4
98
99 /* These are used with DST_S_C_SOURCE */
100
101 #define DST_S_B_PCLINE_UNSBYTE   1
102 #define DST_S_W_PCLINE_UNSWORD   1
103 #define DST_S_L_PCLINE_UNSLONG   1
104
105 #define DST_S_B_MODBEG_NAME     14
106 #define DST_S_L_RTNBEG_ADDRESS   5
107 #define DST_S_B_RTNBEG_NAME     13
108 #define DST_S_L_RTNEND_SIZE      5
109
110 /* These are used with DST_S_C_SOURCE.  */
111 #define DST_S_C_SOURCE_HEADER_SIZE 4
112
113 #define DST_S_B_SRC_DF_LENGTH     1
114 #define DST_S_W_SRC_DF_FILEID     3
115 #define DST_S_B_SRC_DF_FILENAME  20
116 #define DST_S_B_SRC_UNSBYTE       1
117 #define DST_S_W_SRC_UNSWORD       1
118 #define DST_S_L_SRC_UNSLONG       1
119
120 /* Debugger symbol definitions.  */
121
122 #define DBG_S_L_DMT_MODBEG       0
123 #define DBG_S_L_DST_SIZE         4
124 #define DBG_S_W_DMT_PSECT_COUNT  8
125 #define DBG_S_C_DMT_HEADER_SIZE 12
126
127 #define DBG_S_L_DMT_PSECT_START  0
128 #define DBG_S_L_DMT_PSECT_LENGTH 4
129 #define DBG_S_C_DMT_PSECT_SIZE   8
130
131 /* VMS module header.  */
132
133 struct hdr_struct
134 {
135   char hdr_b_strlvl;
136   int hdr_l_arch1;
137   int hdr_l_arch2;
138   int hdr_l_recsiz;
139   char *hdr_t_name;
140   char *hdr_t_version;
141   char *hdr_t_date;
142   char *hdr_c_lnm;
143   char *hdr_c_src;
144   char *hdr_c_ttl;
145 };
146
147 #define EMH_DATE_LENGTH  17
148
149 /* VMS End-Of-Module records (EOM/EEOM).  */
150
151 struct eom_struct
152 {
153   unsigned int eom_l_total_lps;
154   unsigned short eom_w_comcod;
155   bfd_boolean eom_has_transfer;
156   unsigned char eom_b_tfrflg;
157   unsigned int eom_l_psindx;
158   unsigned int eom_l_tfradr;
159 };
160
161 struct vms_symbol_entry
162 {
163   bfd *owner;
164
165   /* Common fields.  */
166   unsigned char typ;
167   unsigned char data_type;
168   unsigned short flags;
169
170   /* Section and offset/value of the symbol.  */
171   unsigned int value;
172   asection *section;
173
174   /* Section and offset/value for the entry point (only for subprg).  */
175   asection *code_section;
176   unsigned int code_value;
177
178   /* Symbol vector offset.  */
179   unsigned int symbol_vector;
180
181   /* Length of the name.  */
182   unsigned char namelen;
183
184   char name[1];
185 };
186
187 /* Stack value for push/pop commands.  */
188
189 struct stack_struct
190 {
191   bfd_vma value;
192   unsigned int reloc;
193 };
194
195 #define STACKSIZE 128
196
197 /* A minimal decoding of DST compilation units.  We only decode
198    what's needed to get to the line number information.  */
199
200 struct fileinfo
201 {
202   char *name;
203   unsigned int srec;
204 };
205
206 struct srecinfo
207 {
208   struct srecinfo *next;
209   unsigned int line;
210   unsigned int sfile;
211   unsigned int srec;
212 };
213
214 struct lineinfo
215 {
216   struct lineinfo *next;
217   bfd_vma address;
218   unsigned int line;
219 };
220
221 struct funcinfo
222 {
223   struct funcinfo *next;
224   char *name;
225   bfd_vma low;
226   bfd_vma high;
227 };
228
229 struct module
230 {
231   /* Chain the previously read compilation unit.  */
232   struct module *next;
233
234   /* The module name.  */
235   char *name;
236
237   /* The start offset and size of debug info in the DST section.  */
238   unsigned int modbeg;
239   unsigned int size;
240
241   /* The lowest and highest addresses contained in this compilation
242      unit as specified in the compilation unit header.  */
243   bfd_vma low;
244   bfd_vma high;
245
246   /* The listing line table.  */
247   struct lineinfo *line_table;
248
249   /* The source record table.  */
250   struct srecinfo *srec_table;
251
252   /* A list of the functions found in this module.  */
253   struct funcinfo *func_table;
254
255   /* Current allocation of file_table.  */
256   unsigned int file_table_count;
257
258   /* An array of the files making up this module.  */
259   struct fileinfo *file_table;
260 };
261
262 /* BFD private data for alpha-vms.  */
263
264 struct vms_private_data_struct
265 {
266   /* If true, relocs have been read.  */
267   bfd_boolean reloc_done;
268
269   /* Record input buffer.  */
270   struct vms_rec_rd recrd;
271   struct vms_rec_wr recwr;
272
273   struct hdr_struct hdr_data;           /* data from HDR/EMH record  */
274   struct eom_struct eom_data;           /* data from EOM/EEOM record  */
275
276   /* Transfer addresses (entry points).  */
277   bfd_vma transfer_address[4];
278
279   /* Array of GSD sections to get the correspond BFD one.  */
280   unsigned int section_max;             /* Size of the sections array.  */
281   unsigned int section_count;           /* Number of GSD sections.  */
282   asection **sections;
283
284   /* Array of raw symbols.  */
285   struct vms_symbol_entry **syms;
286
287   /* Canonicalized symbols.  */
288   asymbol **csymbols;
289
290   /* Number of symbols.  */
291   unsigned int gsd_sym_count;
292   /* Size of the syms array.  */
293   unsigned int max_sym_count;
294   /* Number of procedure symbols.  */
295   unsigned int norm_sym_count;
296
297   /* Stack used to evaluate TIR/ETIR commands.  */
298   struct stack_struct *stack;
299   int stackptr;
300
301   /* Content reading.  */
302   asection *image_section;              /* section for image_ptr  */
303   file_ptr image_offset;                /* Offset for image_ptr.  */
304
305   struct module *modules;               /* list of all compilation units */
306
307   asection *dst_section;
308
309   unsigned int dst_ptr_offsets_count;   /* # of offsets in following array  */
310   unsigned int *dst_ptr_offsets;        /* array of saved image_ptr offsets */
311
312   /* Shared library support */
313   bfd_vma symvva; /* relative virtual address of symbol vector */
314   unsigned int ident;
315   unsigned char matchctl;
316
317   /* Shared library index.  This is used for input bfd while linking.  */
318   unsigned int shr_index;
319
320   /* Used to place structures in the file.  */
321   file_ptr file_pos;
322
323   /* Simply linked list of eisd.  */
324   struct vms_internal_eisd_map *eisd_head;
325   struct vms_internal_eisd_map *eisd_tail;
326
327   /* Simply linked list of eisd for shared libraries.  */
328   struct vms_internal_eisd_map *gbl_eisd_head;
329   struct vms_internal_eisd_map *gbl_eisd_tail;
330
331   /* linkage index counter used by conditional store commands */
332   int vms_linkage_index;
333
334   /* see tc-alpha.c of gas for a description.  */
335   int flag_hash_long_names;     /* -+, hash instead of truncate */
336   int flag_show_after_trunc;    /* -H, show hashing/truncation */
337 };
338
339 #define PRIV2(abfd, name) \
340   (((struct vms_private_data_struct *)(abfd)->tdata.any)->name)
341 #define PRIV(name) PRIV2(abfd,name)
342
343
344 /* Used to keep extra VMS specific information for a given section.
345
346    reloc_size holds the size of the relocation stream, note this
347    is very different from the number of relocations as VMS relocations
348    are variable length.
349
350    reloc_stream is the actual stream of relocation entries.  */
351
352 struct vms_section_data_struct
353 {
354   /* Maximnum number of entries in sec->relocation.  */
355   unsigned reloc_max;
356
357   /* Corresponding eisd.  Used only while generating executables.  */
358   struct vms_internal_eisd_map *eisd;
359
360   /* PSC flags to be clear.  */
361   flagword no_flags;
362
363   /* PSC flags to be set.  */
364   flagword flags;
365 };
366
367 #define vms_section_data(sec) \
368   ((struct vms_section_data_struct *)sec->used_by_bfd)
369
370 /* To be called from the debugger.  */
371 struct vms_private_data_struct *bfd_vms_get_data (bfd *abfd);
372
373 static int vms_get_remaining_object_record (bfd *abfd, int read_so_far);
374 static bfd_boolean _bfd_vms_slurp_object_records (bfd * abfd);
375 static void alpha_vms_add_fixup_lp (struct bfd_link_info *, bfd *, bfd *);
376 static void alpha_vms_add_fixup_ca (struct bfd_link_info *, bfd *, bfd *);
377 static void alpha_vms_add_fixup_qr (struct bfd_link_info *, bfd *, bfd *,
378                                     bfd_vma);
379 static void alpha_vms_add_lw_reloc (struct bfd_link_info *info);
380 static void alpha_vms_add_qw_reloc (struct bfd_link_info *info);
381 static void alpha_vms_add_lw_fixup (struct bfd_link_info *, unsigned int,
382                                     bfd_vma);
383
384 struct vector_type
385 {
386   unsigned int max_el;
387   unsigned int nbr_el;
388   void *els;
389 };
390
391 /* Number of elements in VEC.  */
392
393 #define VEC_COUNT(VEC) ((VEC).nbr_el)
394
395 /* Get the address of the Nth element.  */
396
397 #define VEC_EL(VEC, TYPE, N) (((TYPE *)((VEC).els))[N])
398
399 #define VEC_INIT(VEC)                           \
400   do {                                          \
401     (VEC).max_el = 0;                           \
402     (VEC).nbr_el = 0;                           \
403     (VEC).els = NULL;                           \
404   } while (0)
405
406 /* Be sure there is room for a new element.  */
407
408 static void vector_grow1 (struct vector_type *vec, size_t elsz);
409
410 /* Allocate room for a new element and return its address.  */
411
412 #define VEC_APPEND(VEC, TYPE)                                   \
413   (vector_grow1 (&VEC, sizeof (TYPE)), &VEC_EL(VEC, TYPE, (VEC).nbr_el++))
414
415 /* Append an element.  */
416
417 #define VEC_APPEND_EL(VEC, TYPE, EL)            \
418   (*(VEC_APPEND (VEC, TYPE)) = EL)
419
420 struct alpha_vms_vma_ref
421 {
422   bfd_vma vma;  /* Vma in the output.  */
423   bfd_vma ref;  /* Reference in the input.  */
424 };
425
426 struct alpha_vms_shlib_el
427 {
428   bfd *abfd;
429   bfd_boolean has_fixups;
430
431   struct vector_type lp;        /* Vector of bfd_vma.  */
432   struct vector_type ca;        /* Vector of bfd_vma.  */
433   struct vector_type qr;        /* Vector of struct alpha_vms_vma_ref.  */
434 };
435
436 /* Alpha VMS linker hash table.  */
437
438 struct alpha_vms_link_hash_table
439 {
440   struct bfd_link_hash_table root;
441
442   /* Vector of shared libaries.  */
443   struct vector_type shrlibs;
444
445   /* Fixup section.  */
446   asection *fixup;
447
448   /* Base address.  Used by fixups.  */
449   bfd_vma base_addr;
450 };
451
452 #define alpha_vms_link_hash(INFO) \
453   ((struct alpha_vms_link_hash_table *)(INFO->hash))
454
455 /* Alpha VMS linker hash table entry.  */
456
457 struct alpha_vms_link_hash_entry
458 {
459   struct bfd_link_hash_entry root;
460
461   /* Pointer to the original vms symbol.  */
462   struct vms_symbol_entry *sym;
463 };
464 \f
465 /* Image reading.  */
466
467 /* Read & process EIHD record.
468    Return TRUE on success, FALSE on error.  */
469
470 static bfd_boolean
471 _bfd_vms_slurp_eihd (bfd *abfd, unsigned int *eisd_offset,
472                      unsigned int *eihs_offset)
473 {
474   unsigned int imgtype, size;
475   bfd_vma symvva;
476   struct vms_eihd *eihd = (struct vms_eihd *)PRIV (recrd.rec);
477
478   vms_debug2 ((8, "_bfd_vms_slurp_eihd\n"));
479
480   size = bfd_getl32 (eihd->size);
481   imgtype = bfd_getl32 (eihd->imgtype);
482
483   if (imgtype == EIHD__K_EXE || imgtype == EIHD__K_LIM)
484     abfd->flags |= EXEC_P;
485
486   symvva = bfd_getl64 (eihd->symvva);
487   if (symvva != 0)
488     {
489       PRIV (symvva) = symvva;
490       abfd->flags |= DYNAMIC;
491     }
492
493   PRIV (ident) = bfd_getl32 (eihd->ident);
494   PRIV (matchctl) = eihd->matchctl;
495
496   *eisd_offset = bfd_getl32 (eihd->isdoff);
497   *eihs_offset = bfd_getl32 (eihd->symdbgoff);
498
499   vms_debug2 ((4, "EIHD size %d imgtype %d symvva 0x%lx eisd %d eihs %d\n",
500                size, imgtype, (unsigned long)symvva,
501                *eisd_offset, *eihs_offset));
502
503   return TRUE;
504 }
505
506 /* Read & process EISD record.
507    Return TRUE on success, FALSE on error.  */
508
509 static bfd_boolean
510 _bfd_vms_slurp_eisd (bfd *abfd, unsigned int offset)
511 {
512   int section_count = 0;
513
514   vms_debug2 ((8, "_bfd_vms_slurp_eisd\n"));
515
516   while (1)
517     {
518       struct vms_eisd *eisd;
519       unsigned int rec_size;
520       unsigned int size;
521       unsigned long long vaddr;
522       unsigned int flags;
523       unsigned int vbn;
524       char *name = NULL;
525       asection *section;
526       flagword bfd_flags;
527
528       eisd = (struct vms_eisd *)(PRIV (recrd.rec) + offset);
529       rec_size = bfd_getl32 (eisd->eisdsize);
530
531       if (rec_size == 0)
532         break;
533
534       /* Skip to next block if pad.  */
535       if (rec_size == 0xffffffff)
536         {
537           offset = (offset + VMS_BLOCK_SIZE) & ~(VMS_BLOCK_SIZE - 1);
538           continue;
539         }
540       else
541         offset += rec_size;
542
543       size = bfd_getl32 (eisd->secsize);
544       vaddr = bfd_getl64 (eisd->virt_addr);
545       flags = bfd_getl32 (eisd->flags);
546       vbn = bfd_getl32 (eisd->vbn);
547
548       vms_debug2 ((4, "EISD at 0x%x size 0x%x addr 0x%lx flags 0x%x blk %d\n",
549                    offset, size, (unsigned long)vaddr, flags, vbn));
550
551       /* VMS combines psects from .obj files into isects in the .exe.  This
552          process doesn't preserve enough information to reliably determine
553          what's in each section without examining the data.  This is
554          especially true of DWARF debug sections.  */
555       bfd_flags = SEC_ALLOC;
556       if (vbn != 0)
557         bfd_flags |= SEC_HAS_CONTENTS | SEC_LOAD;
558
559       if (flags & EISD__M_EXE)
560         bfd_flags |= SEC_CODE;
561
562       if (flags & EISD__M_NONSHRADR)
563         bfd_flags |= SEC_DATA;
564
565       if (!(flags & EISD__M_WRT))
566         bfd_flags |= SEC_READONLY;
567
568       if (flags & EISD__M_DZRO)
569         bfd_flags |= SEC_DATA;
570
571       if (flags & EISD__M_FIXUPVEC)
572         bfd_flags |= SEC_DATA;
573
574       if (flags & EISD__M_CRF)
575         bfd_flags |= SEC_DATA;
576
577       if (flags & EISD__M_GBL)
578         {
579           name = _bfd_vms_save_counted_string (eisd->gblnam);
580           bfd_flags |= SEC_COFF_SHARED_LIBRARY;
581           bfd_flags &= ~(SEC_ALLOC | SEC_LOAD);
582         }
583       else if (flags & EISD__M_FIXUPVEC)
584         name = "$FIXUPVEC$";
585       else if (eisd->type == EISD__K_USRSTACK)
586         name = "$STACK$";
587       else
588         {
589           const char *pfx;
590
591           name = (char*) bfd_alloc (abfd, 32);
592           if (flags & EISD__M_DZRO)
593             pfx = "BSS";
594           else if (flags & EISD__M_EXE)
595             pfx = "CODE";
596           else if (!(flags & EISD__M_WRT))
597             pfx = "RO";
598           else
599             pfx = "LOCAL";
600           BFD_ASSERT (section_count < 999);
601           sprintf (name, "$%s_%03d$", pfx, section_count++);
602         }
603
604       section = bfd_make_section (abfd, name);
605
606       if (!section)
607         return FALSE;
608
609       section->filepos = vbn ? VMS_BLOCK_SIZE * (vbn - 1) : 0;
610       section->size = size;
611       section->vma = vaddr;
612
613       if (!bfd_set_section_flags (abfd, section, bfd_flags))
614         return FALSE;
615     }
616
617   return TRUE;
618 }
619
620 /* Read & process EIHS record.
621    Return TRUE on success, FALSE on error.  */
622
623 static bfd_boolean
624 _bfd_vms_slurp_eihs (bfd *abfd, unsigned int offset)
625 {
626   unsigned char *p = PRIV (recrd.rec) + offset;
627   unsigned int gstvbn = bfd_getl32 (p + EIHS__L_GSTVBN);
628   unsigned int gstsize ATTRIBUTE_UNUSED = bfd_getl32 (p + EIHS__L_GSTSIZE);
629   unsigned int dstvbn = bfd_getl32 (p + EIHS__L_DSTVBN);
630   unsigned int dstsize = bfd_getl32 (p + EIHS__L_DSTSIZE);
631   unsigned int dmtvbn = bfd_getl32 (p + EIHS__L_DMTVBN);
632   unsigned int dmtbytes = bfd_getl32 (p + EIHS__L_DMTBYTES);
633   asection *section;
634
635 #if VMS_DEBUG
636   vms_debug (8, "_bfd_vms_slurp_ihs\n");
637   vms_debug (4, "EIHS record gstvbn %d gstsize %d dstvbn %d dstsize %d dmtvbn %d dmtbytes %d\n",
638              gstvbn, gstsize, dstvbn, dstsize, dmtvbn, dmtbytes);
639 #endif
640
641   if (dstvbn)
642     {
643       flagword bfd_flags = SEC_HAS_CONTENTS | SEC_DEBUGGING;
644
645       section = bfd_make_section (abfd, "$DST$");
646       if (!section)
647         return FALSE;
648
649       section->size = dstsize;
650       section->filepos = VMS_BLOCK_SIZE * (dstvbn - 1);
651
652       if (!bfd_set_section_flags (abfd, section, bfd_flags))
653         return FALSE;
654
655       PRIV (dst_section) = section;
656       abfd->flags |= (HAS_DEBUG | HAS_LINENO);
657     }
658
659   if (dmtvbn)
660     {
661       flagword bfd_flags = SEC_HAS_CONTENTS | SEC_DEBUGGING;
662
663       section = bfd_make_section (abfd, "$DMT$");
664       if (!section)
665         return FALSE;
666
667       section->size = dmtbytes;
668       section->filepos = VMS_BLOCK_SIZE * (dmtvbn - 1);
669
670       if (!bfd_set_section_flags (abfd, section, bfd_flags))
671         return FALSE;
672     }
673
674   if (gstvbn)
675     {
676       if (bfd_seek (abfd, VMS_BLOCK_SIZE * (gstvbn - 1), SEEK_SET))
677         {
678           bfd_set_error (bfd_error_file_truncated);
679           return FALSE;
680         }
681
682       if (_bfd_vms_slurp_object_records (abfd) != TRUE)
683         return FALSE;
684
685       abfd->flags |= HAS_SYMS;
686     }
687
688   return TRUE;
689 }
690 \f
691 /* Object file reading.  */
692
693 /* Object file input functions.  */
694
695 /* Get next record from object file to vms_buf.
696    Set PRIV(buf_size) and return it
697
698    This is a little tricky since it should be portable.
699
700    The openVMS object file has 'variable length' which means that
701    read() returns data in chunks of (hopefully) correct and expected
702    size.  The linker (and other tools on VMS) depend on that. Unix
703    doesn't know about 'formatted' files, so reading and writing such
704    an object file in a Unix environment is not trivial.
705
706    With the tool 'file' (available on all VMS FTP sites), one
707    can view and change the attributes of a file.  Changing from
708    'variable length' to 'fixed length, 512 bytes' reveals the
709    record size at the first 2 bytes of every record.  The same
710    may happen during the transfer of object files from VMS to Unix,
711    at least with UCX, the DEC implementation of TCP/IP.
712
713    The VMS format repeats the size at bytes 2 & 3 of every record.
714
715    On the first call (file_format == FF_UNKNOWN) we check if
716    the first and the third byte pair (!) of the record match.
717    If they do it's an object file in an Unix environment or with
718    wrong attributes (FF_FOREIGN), else we should be in a VMS
719    environment where read() returns the record size (FF_NATIVE).
720
721    Reading is always done in 2 steps:
722     1. first just the record header is read and the size extracted,
723     2. then the read buffer is adjusted and the remaining bytes are
724        read in.
725
726    All file I/O is done on even file positions.  */
727
728 #define VMS_OBJECT_ADJUSTMENT  2
729
730 static void
731 maybe_adjust_record_pointer_for_object (bfd *abfd)
732 {
733   /* Set the file format once for all on the first invocation.  */
734   if (PRIV (recrd.file_format) == FF_UNKNOWN)
735     {
736       if (PRIV (recrd.rec)[0] == PRIV (recrd.rec)[4]
737           && PRIV (recrd.rec)[1] == PRIV (recrd.rec)[5])
738         PRIV (recrd.file_format) = FF_FOREIGN;
739       else
740         PRIV (recrd.file_format) = FF_NATIVE;
741     }
742
743   /* The adjustment is needed only in an Unix environment.  */
744   if (PRIV (recrd.file_format) == FF_FOREIGN)
745     PRIV (recrd.rec) += VMS_OBJECT_ADJUSTMENT;
746 }
747
748 /* Implement step #1 of the object record reading procedure.
749    Return the record type or -1 on failure.  */
750
751 static int
752 _bfd_vms_get_object_record (bfd *abfd)
753 {
754   unsigned int test_len = 6;
755   int type;
756
757   vms_debug2 ((8, "_bfd_vms_get_obj_record\n"));
758
759   /* Skip alignment byte if the current position is odd.  */
760   if (PRIV (recrd.file_format) == FF_FOREIGN && (bfd_tell (abfd) & 1))
761     {
762       if (bfd_bread (PRIV (recrd.buf), 1, abfd) != 1)
763         {
764           bfd_set_error (bfd_error_file_truncated);
765           return -1;
766         }
767     }
768
769   /* Read the record header  */
770   if (bfd_bread (PRIV (recrd.buf), test_len, abfd) != test_len)
771     {
772       bfd_set_error (bfd_error_file_truncated);
773       return -1;
774     }
775
776   /* Reset the record pointer.  */
777   PRIV (recrd.rec) = PRIV (recrd.buf);
778   maybe_adjust_record_pointer_for_object (abfd);
779
780   if (vms_get_remaining_object_record (abfd, test_len) <= 0)
781     return -1;
782
783   type = bfd_getl16 (PRIV (recrd.rec));
784
785   vms_debug2 ((8, "_bfd_vms_get_obj_record: rec %p, size %d, type %d\n",
786                PRIV (recrd.rec), PRIV (recrd.rec_size), type));
787
788   return type;
789 }
790
791 /* Implement step #2 of the object record reading procedure.
792    Return the size of the record or 0 on failure.  */
793
794 static int
795 vms_get_remaining_object_record (bfd *abfd, int read_so_far)
796 {
797   unsigned int to_read;
798
799   vms_debug2 ((8, "vms_get_remaining_obj_record\n"));
800
801   /* Extract record size.  */
802   PRIV (recrd.rec_size) = bfd_getl16 (PRIV (recrd.rec) + 2);
803
804   if (PRIV (recrd.rec_size) <= 0)
805     {
806       bfd_set_error (bfd_error_file_truncated);
807       return 0;
808     }
809
810   /* That's what the linker manual says.  */
811   if (PRIV (recrd.rec_size) > EOBJ__C_MAXRECSIZ)
812     {
813       bfd_set_error (bfd_error_file_truncated);
814       return 0;
815     }
816
817   /* Take into account object adjustment.  */
818   to_read = PRIV (recrd.rec_size);
819   if (PRIV (recrd.file_format) == FF_FOREIGN)
820     to_read += VMS_OBJECT_ADJUSTMENT;
821
822   /* Adjust the buffer.  */
823   if (to_read > PRIV (recrd.buf_size))
824     {
825       PRIV (recrd.buf)
826         = (unsigned char *) bfd_realloc (PRIV (recrd.buf), to_read);
827       if (PRIV (recrd.buf) == NULL)
828         return 0;
829       PRIV (recrd.buf_size) = to_read;
830     }
831
832   /* Read the remaining record.  */
833   to_read -= read_so_far;
834
835   vms_debug2 ((8, "vms_get_remaining_obj_record: to_read %d\n", to_read));
836
837   if (bfd_bread (PRIV (recrd.buf) + read_so_far, to_read, abfd) != to_read)
838     {
839       bfd_set_error (bfd_error_file_truncated);
840       return 0;
841     }
842
843   /* Reset the record pointer.  */
844   PRIV (recrd.rec) = PRIV (recrd.buf);
845   maybe_adjust_record_pointer_for_object (abfd);
846
847   vms_debug2 ((8, "vms_get_remaining_obj_record: size %d\n",
848                PRIV (recrd.rec_size)));
849
850   return PRIV (recrd.rec_size);
851 }
852
853 /* Read and process emh record.
854    Return TRUE on success, FALSE on error.  */
855
856 static bfd_boolean
857 _bfd_vms_slurp_ehdr (bfd *abfd)
858 {
859   unsigned char *ptr;
860   unsigned char *vms_rec;
861   int subtype;
862
863   vms_rec = PRIV (recrd.rec);
864
865   vms_debug2 ((2, "HDR/EMH\n"));
866
867   subtype = bfd_getl16 (vms_rec + 4);
868
869   vms_debug2 ((3, "subtype %d\n", subtype));
870
871   switch (subtype)
872     {
873     case EMH__C_MHD:
874       /* Module header.  */
875       PRIV (hdr_data).hdr_b_strlvl = vms_rec[6];
876       PRIV (hdr_data).hdr_l_arch1  = bfd_getl32 (vms_rec + 8);
877       PRIV (hdr_data).hdr_l_arch2  = bfd_getl32 (vms_rec + 12);
878       PRIV (hdr_data).hdr_l_recsiz = bfd_getl32 (vms_rec + 16);
879       PRIV (hdr_data).hdr_t_name   = _bfd_vms_save_counted_string (vms_rec + 20);
880       ptr = vms_rec + 20 + vms_rec[20] + 1;
881       PRIV (hdr_data).hdr_t_version =_bfd_vms_save_counted_string (ptr);
882       ptr += *ptr + 1;
883       PRIV (hdr_data).hdr_t_date = _bfd_vms_save_sized_string (ptr, 17);
884       break;
885
886     case EMH__C_LNM:
887       PRIV (hdr_data).hdr_c_lnm =
888         _bfd_vms_save_sized_string (vms_rec, PRIV (recrd.rec_size - 6));
889       break;
890
891     case EMH__C_SRC:
892       PRIV (hdr_data).hdr_c_src =
893         _bfd_vms_save_sized_string (vms_rec, PRIV (recrd.rec_size - 6));
894       break;
895
896     case EMH__C_TTL:
897       PRIV (hdr_data).hdr_c_ttl =
898         _bfd_vms_save_sized_string (vms_rec, PRIV (recrd.rec_size - 6));
899       break;
900
901     case EMH__C_CPR:
902     case EMH__C_MTC:
903     case EMH__C_GTX:
904       break;
905
906     default:
907       bfd_set_error (bfd_error_wrong_format);
908       return FALSE;
909     }
910
911   return TRUE;
912 }
913
914 /* Typical sections for evax object files.  */
915
916 #define EVAX_ABS_NAME           "$ABS$"
917 #define EVAX_CODE_NAME          "$CODE$"
918 #define EVAX_LINK_NAME          "$LINK$"
919 #define EVAX_DATA_NAME          "$DATA$"
920 #define EVAX_BSS_NAME           "$BSS$"
921 #define EVAX_READONLYADDR_NAME  "$READONLY_ADDR$"
922 #define EVAX_READONLY_NAME      "$READONLY$"
923 #define EVAX_LITERAL_NAME       "$LITERAL$"
924 #define EVAX_LITERALS_NAME      "$LITERALS"
925 #define EVAX_COMMON_NAME        "$COMMON$"
926 #define EVAX_LOCAL_NAME         "$LOCAL$"
927
928 struct sec_flags_struct
929 {
930   const char *name;             /* Name of section.  */
931   int vflags_always;
932   flagword flags_always;        /* Flags we set always.  */
933   int vflags_hassize;
934   flagword flags_hassize;       /* Flags we set if the section has a size > 0.  */
935 };
936
937 /* These flags are deccrtl/vaxcrtl (openVMS 6.2 Alpha) compatible.  */
938
939 static const struct sec_flags_struct evax_section_flags[] =
940   {
941     { EVAX_ABS_NAME,
942       (EGPS__V_SHR),
943       (SEC_DATA),
944       (EGPS__V_SHR),
945       (SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD) },
946     { EVAX_CODE_NAME,
947       (EGPS__V_PIC | EGPS__V_REL | EGPS__V_SHR | EGPS__V_EXE),
948       (SEC_CODE),
949       (EGPS__V_PIC | EGPS__V_REL | EGPS__V_SHR | EGPS__V_EXE),
950       (SEC_CODE | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD) },
951     { EVAX_LITERAL_NAME,
952       (EGPS__V_PIC | EGPS__V_REL | EGPS__V_SHR | EGPS__V_RD | EGPS__V_NOMOD),
953       (SEC_DATA | SEC_READONLY),
954       (EGPS__V_PIC | EGPS__V_REL | EGPS__V_SHR | EGPS__V_RD),
955       (SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_READONLY | SEC_LOAD) },
956     { EVAX_LINK_NAME,
957       (EGPS__V_REL | EGPS__V_RD),
958       (SEC_DATA | SEC_READONLY),
959       (EGPS__V_REL | EGPS__V_RD),
960       (SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_READONLY | SEC_LOAD) },
961     { EVAX_DATA_NAME,
962       (EGPS__V_REL | EGPS__V_RD | EGPS__V_WRT | EGPS__V_NOMOD),
963       (SEC_DATA),
964       (EGPS__V_REL | EGPS__V_RD | EGPS__V_WRT),
965       (SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD) },
966     { EVAX_BSS_NAME,
967       (EGPS__V_REL | EGPS__V_RD | EGPS__V_WRT | EGPS__V_NOMOD),
968       (SEC_NO_FLAGS),
969       (EGPS__V_REL | EGPS__V_RD | EGPS__V_WRT | EGPS__V_NOMOD),
970       (SEC_ALLOC) },
971     { EVAX_READONLYADDR_NAME,
972       (EGPS__V_PIC | EGPS__V_REL | EGPS__V_RD),
973       (SEC_DATA | SEC_READONLY),
974       (EGPS__V_PIC | EGPS__V_REL | EGPS__V_RD),
975       (SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_READONLY | SEC_LOAD) },
976     { EVAX_READONLY_NAME,
977       (EGPS__V_PIC | EGPS__V_REL | EGPS__V_SHR | EGPS__V_RD | EGPS__V_NOMOD),
978       (SEC_DATA | SEC_READONLY),
979       (EGPS__V_PIC | EGPS__V_REL | EGPS__V_SHR | EGPS__V_RD),
980       (SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_READONLY | SEC_LOAD) },
981     { EVAX_LOCAL_NAME,
982       (EGPS__V_REL | EGPS__V_RD | EGPS__V_WRT),
983       (SEC_DATA),
984       (EGPS__V_REL | EGPS__V_RD | EGPS__V_WRT),
985       (SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD) },
986     { EVAX_LITERALS_NAME,
987       (EGPS__V_PIC | EGPS__V_OVR),
988       (SEC_DATA | SEC_READONLY),
989       (EGPS__V_PIC | EGPS__V_OVR),
990       (SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_READONLY | SEC_LOAD) },
991     { NULL,
992       (EGPS__V_REL | EGPS__V_RD | EGPS__V_WRT),
993       (SEC_DATA),
994       (EGPS__V_REL | EGPS__V_RD | EGPS__V_WRT),
995       (SEC_IN_MEMORY | SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD) }
996   };
997
998 /* Retrieve BFD section flags by name and size.  */
999
1000 static flagword
1001 vms_secflag_by_name (const struct sec_flags_struct *section_flags,
1002                      const char *name,
1003                      int hassize)
1004 {
1005   int i = 0;
1006
1007   while (section_flags[i].name != NULL)
1008     {
1009       if (strcmp (name, section_flags[i].name) == 0)
1010         {
1011           if (hassize)
1012             return section_flags[i].flags_hassize;
1013           else
1014             return section_flags[i].flags_always;
1015         }
1016       i++;
1017     }
1018   if (hassize)
1019     return section_flags[i].flags_hassize;
1020   return section_flags[i].flags_always;
1021 }
1022
1023 /* Retrieve VMS section flags by name and size.  */
1024
1025 static flagword
1026 vms_esecflag_by_name (const struct sec_flags_struct *section_flags,
1027                       const char *name,
1028                       int hassize)
1029 {
1030   int i = 0;
1031
1032   while (section_flags[i].name != NULL)
1033     {
1034       if (strcmp (name, section_flags[i].name) == 0)
1035         {
1036           if (hassize)
1037             return section_flags[i].vflags_hassize;
1038           else
1039             return section_flags[i].vflags_always;
1040         }
1041       i++;
1042     }
1043   if (hassize)
1044     return section_flags[i].vflags_hassize;
1045   return section_flags[i].vflags_always;
1046 }
1047
1048 /* Add SYM to the symbol table of ABFD.
1049    Return FALSE in case of error.  */
1050
1051 static bfd_boolean
1052 add_symbol_entry (bfd *abfd, struct vms_symbol_entry *sym)
1053 {
1054   if (PRIV (gsd_sym_count) >= PRIV (max_sym_count))
1055     {
1056       if (PRIV (max_sym_count) == 0)
1057         {
1058           PRIV (max_sym_count) = 128;
1059           PRIV (syms) = bfd_malloc
1060             (PRIV (max_sym_count) * sizeof (struct vms_symbol_entry *));
1061         }
1062       else
1063         {
1064           PRIV (max_sym_count) *= 2;
1065           PRIV (syms) = bfd_realloc
1066             (PRIV (syms),
1067              (PRIV (max_sym_count) * sizeof (struct vms_symbol_entry *)));
1068         }
1069       if (PRIV (syms) == NULL)
1070         return FALSE;
1071     }
1072
1073   PRIV (syms)[PRIV (gsd_sym_count)++] = sym;
1074   return TRUE;
1075 }
1076
1077 /* Create a symbol whose name is ASCIC and add it to ABFD.
1078    Return NULL in case of error.  */
1079
1080 static struct vms_symbol_entry *
1081 add_symbol (bfd *abfd, const unsigned char *ascic)
1082 {
1083   struct vms_symbol_entry *entry;
1084   int len;
1085
1086   len = *ascic++;
1087   entry = (struct vms_symbol_entry *)bfd_zalloc (abfd, sizeof (*entry) + len);
1088   if (entry == NULL)
1089     return NULL;
1090   entry->namelen = len;
1091   memcpy (entry->name, ascic, len);
1092   entry->name[len] = 0;
1093   entry->owner = abfd;
1094
1095   if (!add_symbol_entry (abfd, entry))
1096     return NULL;
1097   return entry;
1098 }
1099
1100 /* Read and process EGSD.  Return FALSE on failure.  */
1101
1102 static bfd_boolean
1103 _bfd_vms_slurp_egsd (bfd *abfd)
1104 {
1105   int gsd_type, gsd_size;
1106   asection *section;
1107   unsigned char *vms_rec;
1108   flagword new_flags, old_flags;
1109   char *name;
1110   unsigned long base_addr;
1111   unsigned long align_addr;
1112
1113   vms_debug2 ((2, "EGSD\n"));
1114
1115   PRIV (recrd.rec) += 8;        /* Skip type, size, align pad.  */
1116   PRIV (recrd.rec_size) -= 8;
1117
1118   /* Calculate base address for each section.  */
1119   base_addr = 0L;
1120
1121   while (PRIV (recrd.rec_size) > 0)
1122     {
1123       vms_rec = PRIV (recrd.rec);
1124
1125       gsd_type = bfd_getl16 (vms_rec);
1126       gsd_size = bfd_getl16 (vms_rec + 2);
1127
1128       vms_debug2 ((3, "egsd_type %d\n", gsd_type));
1129
1130       switch (gsd_type)
1131         {
1132         case EGSD__C_PSC:
1133           {
1134             /* Program section definition.  */
1135             struct vms_egps *egps = (struct vms_egps *)vms_rec;
1136
1137             name = _bfd_vms_save_counted_string (&egps->namlng);
1138             section = bfd_make_section (abfd, name);
1139             if (!section)
1140               return FALSE;
1141
1142             old_flags = bfd_getl16 (egps->flags);
1143             vms_section_data (section)->flags = old_flags;
1144             vms_section_data (section)->no_flags = 0;
1145             section->size = bfd_getl32 (egps->alloc);
1146             new_flags = vms_secflag_by_name (evax_section_flags, name,
1147                                              section->size > 0);
1148             if (!(old_flags & EGPS__V_NOMOD))
1149               {
1150                 new_flags |= SEC_HAS_CONTENTS;
1151                 if (old_flags & EGPS__V_REL)
1152                   new_flags |= SEC_RELOC;
1153               }
1154             if (!bfd_set_section_flags (abfd, section, new_flags))
1155               return FALSE;
1156             section->alignment_power = egps->align;
1157             if ((old_flags & EGPS__V_REL) != 0)
1158               {
1159                 /* Give a non-overlapping vma to non absolute sections.  */
1160                 align_addr = (1 << section->alignment_power);
1161                 if ((base_addr % align_addr) != 0)
1162                   base_addr += (align_addr - (base_addr % align_addr));
1163                 section->vma = (bfd_vma)base_addr;
1164                 base_addr += section->size;
1165               }
1166             else
1167               section->vma = 0;
1168             section->filepos = 0;
1169
1170             /* Append it to the section array.  */
1171             if (PRIV (section_count) >= PRIV (section_max))
1172               {
1173                 if (PRIV (section_max) == 0)
1174                   PRIV (section_max) = 16;
1175                 else
1176                   PRIV (section_max) *= 2;
1177                 PRIV (sections) = bfd_realloc_or_free
1178                   (PRIV (sections), PRIV (section_max) * sizeof (asection *));
1179                 if (PRIV (sections) == NULL)
1180                   return FALSE;
1181               }
1182
1183             PRIV (sections)[PRIV (section_count)] = section;
1184             PRIV (section_count)++;
1185
1186 #if VMS_DEBUG
1187             vms_debug (4, "EGSD P-section %d (%s, flags %04x) ",
1188                        section->index, name, old_flags);
1189             vms_debug (4, "%lu bytes at 0x%08lx (mem %p)\n",
1190                        (unsigned long)section->size,
1191                        (unsigned long)section->vma, section->contents);
1192 #endif
1193           }
1194           break;
1195
1196         case EGSD__C_SYM:
1197           {
1198             int nameoff;
1199             struct vms_symbol_entry *entry;
1200             struct vms_egsy *egsy = (struct vms_egsy *) vms_rec;
1201
1202             old_flags = bfd_getl16 (egsy->flags);
1203             if (old_flags & EGSY__V_DEF)
1204               nameoff = ESDF__B_NAMLNG;
1205             else
1206               nameoff = ESRF__B_NAMLNG;
1207
1208             entry = add_symbol (abfd, vms_rec + nameoff);
1209             if (entry == NULL)
1210               return FALSE;
1211
1212             /* Allow only duplicate reference.  */
1213             if ((entry->flags & EGSY__V_DEF) && (old_flags & EGSY__V_DEF))
1214               abort ();
1215
1216             if (entry->typ == 0)
1217               {
1218                 entry->typ = gsd_type;
1219                 entry->data_type = egsy->datyp;
1220                 entry->flags = old_flags;
1221               }
1222
1223             if (old_flags & EGSY__V_DEF)
1224               {
1225                 struct vms_esdf *esdf = (struct vms_esdf *)vms_rec;
1226
1227                 entry->value = bfd_getl64 (esdf->value);
1228                 entry->section = PRIV (sections)[bfd_getl32 (esdf->psindx)];
1229
1230                 if (old_flags & EGSY__V_NORM)
1231                   {
1232                     PRIV (norm_sym_count)++;
1233
1234                     entry->code_value = bfd_getl64 (esdf->code_address);
1235                     entry->code_section =
1236                       PRIV (sections)[bfd_getl32 (esdf->ca_psindx)];
1237                   }
1238               }
1239           }
1240           break;
1241
1242         case EGSD__C_SYMG:
1243           {
1244             int nameoff;
1245             struct vms_symbol_entry *entry;
1246             struct vms_egst *egst = (struct vms_egst *)vms_rec;
1247
1248             old_flags = bfd_getl16 (egst->header.flags);
1249             if (old_flags & EGSY__V_DEF)
1250               nameoff = ESDF__B_NAMLNG;
1251             else
1252               nameoff = ESRF__B_NAMLNG;
1253
1254             entry = add_symbol (abfd, &egst->namlng);
1255
1256             if (entry == NULL)
1257               return FALSE;
1258
1259             entry->typ = gsd_type;
1260             entry->data_type = egst->header.datyp;
1261             entry->flags = old_flags;
1262
1263             entry->symbol_vector = bfd_getl32 (egst->value);
1264
1265             if (old_flags & EGSY__V_REL)
1266               entry->section = PRIV (sections)[bfd_getl32 (egst->psindx)];
1267             else
1268               entry->section = bfd_abs_section_ptr;
1269
1270             entry->value = bfd_getl64 (egst->lp_2);
1271
1272             if (old_flags & EGSY__V_NORM)
1273               {
1274                 PRIV (norm_sym_count)++;
1275
1276                 entry->code_value = bfd_getl64 (egst->lp_1);
1277                 entry->code_section = bfd_abs_section_ptr;
1278               }
1279           }
1280           break;
1281
1282         case EGSD__C_SPSC:
1283         case EGSD__C_IDC:
1284           /* Currently ignored.  */
1285           break;
1286         case EGSD__C_SYMM:
1287         case EGSD__C_SYMV:
1288         default:
1289           (*_bfd_error_handler) (_("Unknown EGSD subtype %d"), gsd_type);
1290           bfd_set_error (bfd_error_bad_value);
1291           return FALSE;
1292         }
1293
1294       PRIV (recrd.rec_size) -= gsd_size;
1295       PRIV (recrd.rec) += gsd_size;
1296     }
1297
1298   if (PRIV (gsd_sym_count) > 0)
1299     abfd->flags |= HAS_SYMS;
1300
1301   return TRUE;
1302 }
1303
1304 /* Stack routines for vms ETIR commands.  */
1305
1306 /* Push value and section index.  */
1307
1308 static void
1309 _bfd_vms_push (bfd *abfd, bfd_vma val, unsigned int reloc)
1310 {
1311   vms_debug2 ((4, "<push %08lx (0x%08x) at %d>\n",
1312                (unsigned long)val, reloc, PRIV (stackptr)));
1313
1314   PRIV (stack[PRIV (stackptr)]).value = val;
1315   PRIV (stack[PRIV (stackptr)]).reloc = reloc;
1316   PRIV (stackptr)++;
1317   if (PRIV (stackptr) >= STACKSIZE)
1318     {
1319       bfd_set_error (bfd_error_bad_value);
1320       (*_bfd_error_handler) (_("Stack overflow (%d) in _bfd_vms_push"), PRIV (stackptr));
1321       exit (1);
1322     }
1323 }
1324
1325 /* Pop value and section index.  */
1326
1327 static void
1328 _bfd_vms_pop (bfd *abfd, bfd_vma *val, unsigned int *rel)
1329 {
1330   if (PRIV (stackptr) == 0)
1331     {
1332       bfd_set_error (bfd_error_bad_value);
1333       (*_bfd_error_handler) (_("Stack underflow in _bfd_vms_pop"));
1334       exit (1);
1335     }
1336   PRIV (stackptr)--;
1337   *val = PRIV (stack[PRIV (stackptr)]).value;
1338   *rel = PRIV (stack[PRIV (stackptr)]).reloc;
1339
1340   vms_debug2 ((4, "<pop %08lx (0x%08x)>\n", (unsigned long)*val, *rel));
1341 }
1342
1343 /* Routines to fill sections contents during tir/etir read.  */
1344
1345 /* Initialize image buffer pointer to be filled.  */
1346
1347 static void
1348 image_set_ptr (bfd *abfd, bfd_vma vma, int sect, struct bfd_link_info *info)
1349 {
1350   asection *sec;
1351
1352   vms_debug2 ((4, "image_set_ptr (0x%08x, sect=%d)\n", (unsigned)vma, sect));
1353
1354   sec = PRIV (sections)[sect];
1355
1356   if (info)
1357     {
1358       /* Reading contents to an output bfd.  */
1359
1360       if (sec->output_section == NULL)
1361         {
1362           /* Section discarded.  */
1363           vms_debug2 ((5, " section %s discarded\n", sec->name));
1364
1365           /* This is not used.  */
1366           PRIV (image_section) = NULL;
1367           PRIV (image_offset) = 0;
1368           return;
1369         }
1370       PRIV (image_offset) = sec->output_offset + vma;
1371       PRIV (image_section) = sec->output_section;
1372     }
1373   else
1374     {
1375       PRIV (image_offset) = vma;
1376       PRIV (image_section) = sec;
1377     }
1378 }
1379
1380 /* Increment image buffer pointer by offset.  */
1381
1382 static void
1383 image_inc_ptr (bfd *abfd, bfd_vma offset)
1384 {
1385   vms_debug2 ((4, "image_inc_ptr (%u)\n", (unsigned)offset));
1386
1387   PRIV (image_offset) += offset;
1388 }
1389
1390 /* Save current DST location counter under specified index.  */
1391
1392 static void
1393 dst_define_location (bfd *abfd, unsigned int loc)
1394 {
1395   vms_debug2 ((4, "dst_define_location (%d)\n", (int)loc));
1396
1397   /* Grow the ptr offset table if necessary.  */
1398   if (loc + 1 > PRIV (dst_ptr_offsets_count))
1399     {
1400       PRIV (dst_ptr_offsets) = bfd_realloc (PRIV (dst_ptr_offsets),
1401                                            (loc + 1) * sizeof (unsigned int));
1402       PRIV (dst_ptr_offsets_count) = loc + 1;
1403     }
1404
1405   PRIV (dst_ptr_offsets)[loc] = PRIV (image_offset);
1406 }
1407
1408 /* Restore saved DST location counter from specified index.  */
1409
1410 static void
1411 dst_restore_location (bfd *abfd, unsigned int loc)
1412 {
1413   vms_debug2 ((4, "dst_restore_location (%d)\n", (int)loc));
1414
1415   PRIV (image_offset) = PRIV (dst_ptr_offsets)[loc];
1416 }
1417
1418 /* Retrieve saved DST location counter from specified index.  */
1419
1420 static unsigned int
1421 dst_retrieve_location (bfd *abfd, unsigned int loc)
1422 {
1423   vms_debug2 ((4, "dst_retrieve_location (%d)\n", (int)loc));
1424
1425   return PRIV (dst_ptr_offsets)[loc];
1426 }
1427
1428 /* Write multiple bytes to section image.  */
1429
1430 static bfd_boolean
1431 image_write (bfd *abfd, unsigned char *ptr, int size)
1432 {
1433 #if VMS_DEBUG
1434   _bfd_vms_debug (8, "image_write from (%p, %d) to (%ld)\n", ptr, size,
1435                   (long)PRIV (image_offset));
1436   _bfd_hexdump (9, ptr, size, 0);
1437 #endif
1438
1439   if (PRIV (image_section)->contents != NULL)
1440     {
1441       asection *sec = PRIV (image_section);
1442       file_ptr off = PRIV (image_offset);
1443
1444       /* Check bounds.  */
1445       if (off > (file_ptr)sec->size
1446           || size > (file_ptr)sec->size
1447           || off + size > (file_ptr)sec->size)
1448         {
1449           bfd_set_error (bfd_error_bad_value);
1450           return FALSE;
1451         }
1452
1453       memcpy (sec->contents + off, ptr, size);
1454     }
1455
1456   PRIV (image_offset) += size;
1457   return TRUE;
1458 }
1459
1460 /* Write byte to section image.  */
1461
1462 static bfd_boolean
1463 image_write_b (bfd * abfd, unsigned int value)
1464 {
1465   unsigned char data[1];
1466
1467   vms_debug2 ((6, "image_write_b (%02x)\n", (int) value));
1468
1469   *data = value;
1470
1471   return image_write (abfd, data, sizeof (data));
1472 }
1473
1474 /* Write 2-byte word to image.  */
1475
1476 static bfd_boolean
1477 image_write_w (bfd * abfd, unsigned int value)
1478 {
1479   unsigned char data[2];
1480
1481   vms_debug2 ((6, "image_write_w (%04x)\n", (int) value));
1482
1483   bfd_putl16 (value, data);
1484   return image_write (abfd, data, sizeof (data));
1485 }
1486
1487 /* Write 4-byte long to image.  */
1488
1489 static bfd_boolean
1490 image_write_l (bfd * abfd, unsigned long value)
1491 {
1492   unsigned char data[4];
1493
1494   vms_debug2 ((6, "image_write_l (%08lx)\n", value));
1495
1496   bfd_putl32 (value, data);
1497   return image_write (abfd, data, sizeof (data));
1498 }
1499
1500 /* Write 8-byte quad to image.  */
1501
1502 static bfd_boolean
1503 image_write_q (bfd * abfd, bfd_vma value)
1504 {
1505   unsigned char data[8];
1506
1507   vms_debug2 ((6, "image_write_q (%08lx)\n", (unsigned long)value));
1508
1509   bfd_putl64 (value, data);
1510   return image_write (abfd, data, sizeof (data));
1511 }
1512 \f
1513 static const char *
1514 _bfd_vms_etir_name (int cmd)
1515 {
1516   switch (cmd)
1517     {
1518     case ETIR__C_STA_GBL: return "ETIR__C_STA_GBL";
1519     case ETIR__C_STA_LW: return "ETIR__C_STA_LW";
1520     case ETIR__C_STA_QW: return "ETIR__C_STA_QW";
1521     case ETIR__C_STA_PQ: return "ETIR__C_STA_PQ";
1522     case ETIR__C_STA_LI: return "ETIR__C_STA_LI";
1523     case ETIR__C_STA_MOD: return "ETIR__C_STA_MOD";
1524     case ETIR__C_STA_CKARG: return "ETIR__C_STA_CKARG";
1525     case ETIR__C_STO_B: return "ETIR__C_STO_B";
1526     case ETIR__C_STO_W: return "ETIR__C_STO_W";
1527     case ETIR__C_STO_GBL: return "ETIR__C_STO_GBL";
1528     case ETIR__C_STO_CA: return "ETIR__C_STO_CA";
1529     case ETIR__C_STO_RB: return "ETIR__C_STO_RB";
1530     case ETIR__C_STO_AB: return "ETIR__C_STO_AB";
1531     case ETIR__C_STO_OFF: return "ETIR__C_STO_OFF";
1532     case ETIR__C_STO_IMM: return "ETIR__C_STO_IMM";
1533     case ETIR__C_STO_IMMR: return "ETIR__C_STO_IMMR";
1534     case ETIR__C_STO_LW: return "ETIR__C_STO_LW";
1535     case ETIR__C_STO_QW: return "ETIR__C_STO_QW";
1536     case ETIR__C_STO_GBL_LW: return "ETIR__C_STO_GBL_LW";
1537     case ETIR__C_STO_LP_PSB: return "ETIR__C_STO_LP_PSB";
1538     case ETIR__C_STO_HINT_GBL: return "ETIR__C_STO_HINT_GBL";
1539     case ETIR__C_STO_HINT_PS: return "ETIR__C_STO_HINT_PS";
1540     case ETIR__C_OPR_ADD: return "ETIR__C_OPR_ADD";
1541     case ETIR__C_OPR_SUB: return "ETIR__C_OPR_SUB";
1542     case ETIR__C_OPR_INSV: return "ETIR__C_OPR_INSV";
1543     case ETIR__C_OPR_USH: return "ETIR__C_OPR_USH";
1544     case ETIR__C_OPR_ROT: return "ETIR__C_OPR_ROT";
1545     case ETIR__C_OPR_REDEF: return "ETIR__C_OPR_REDEF";
1546     case ETIR__C_OPR_DFLIT: return "ETIR__C_OPR_DFLIT";
1547     case ETIR__C_STC_LP: return "ETIR__C_STC_LP";
1548     case ETIR__C_STC_GBL: return "ETIR__C_STC_GBL";
1549     case ETIR__C_STC_GCA: return "ETIR__C_STC_GCA";
1550     case ETIR__C_STC_PS: return "ETIR__C_STC_PS";
1551     case ETIR__C_STC_NBH_PS: return "ETIR__C_STC_NBH_PS";
1552     case ETIR__C_STC_NOP_GBL: return "ETIR__C_STC_NOP_GBL";
1553     case ETIR__C_STC_NOP_PS: return "ETIR__C_STC_NOP_PS";
1554     case ETIR__C_STC_BSR_GBL: return "ETIR__C_STC_BSR_GBL";
1555     case ETIR__C_STC_BSR_PS: return "ETIR__C_STC_BSR_PS";
1556     case ETIR__C_STC_LDA_GBL: return "ETIR__C_STC_LDA_GBL";
1557     case ETIR__C_STC_LDA_PS: return "ETIR__C_STC_LDA_PS";
1558     case ETIR__C_STC_BOH_GBL: return "ETIR__C_STC_BOH_GBL";
1559     case ETIR__C_STC_BOH_PS: return "ETIR__C_STC_BOH_PS";
1560     case ETIR__C_STC_NBH_GBL: return "ETIR__C_STC_NBH_GBL";
1561     case ETIR__C_STC_LP_PSB: return "ETIR__C_STC_LP_PSB";
1562     case ETIR__C_CTL_SETRB: return "ETIR__C_CTL_SETRB";
1563     case ETIR__C_CTL_AUGRB: return "ETIR__C_CTL_AUGRB";
1564     case ETIR__C_CTL_DFLOC: return "ETIR__C_CTL_DFLOC";
1565     case ETIR__C_CTL_STLOC: return "ETIR__C_CTL_STLOC";
1566     case ETIR__C_CTL_STKDL: return "ETIR__C_CTL_STKDL";
1567
1568     default:
1569       /* These names have not yet been added to this switch statement.  */
1570       (*_bfd_error_handler) (_("unknown ETIR command %d"), cmd);
1571     }
1572
1573   return NULL;
1574 }
1575 #define HIGHBIT(op) ((op & 0x80000000L) == 0x80000000L)
1576
1577 static void
1578 _bfd_vms_get_value (bfd *abfd, const unsigned char *ascic,
1579                     struct bfd_link_info *info,
1580                     bfd_vma *vma,
1581                     struct alpha_vms_link_hash_entry **hp)
1582 {
1583   char name[257];
1584   int len;
1585   int i;
1586   struct alpha_vms_link_hash_entry *h;
1587
1588   /* Not linking.  Do not try to resolve the symbol.  */
1589   if (info == NULL)
1590     {
1591       *vma = 0;
1592       *hp = NULL;
1593       return;
1594     }
1595
1596   len = *ascic;
1597   for (i = 0; i < len; i++)
1598     name[i] = ascic[i + 1];
1599   name[i] = 0;
1600
1601   h = (struct alpha_vms_link_hash_entry *)
1602     bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
1603
1604   *hp = h;
1605
1606   if (h != NULL
1607       && (h->root.type == bfd_link_hash_defined
1608           || h->root.type == bfd_link_hash_defweak))
1609     *vma = h->root.u.def.value
1610       + h->root.u.def.section->output_offset
1611       + h->root.u.def.section->output_section->vma;
1612   else if (h && h->root.type == bfd_link_hash_undefweak)
1613     *vma = 0;
1614   else
1615     {
1616       if (!(*info->callbacks->undefined_symbol)
1617           (info, name, abfd, PRIV (image_section), PRIV (image_offset), TRUE))
1618         abort ();
1619       *vma = 0;
1620     }
1621 }
1622
1623 #define RELC_NONE 0
1624 #define RELC_REL  1
1625 #define RELC_SHR_BASE 0x10000
1626 #define RELC_SEC_BASE 0x20000
1627 #define RELC_MASK     0x0ffff
1628
1629 static unsigned int
1630 alpha_vms_sym_to_ctxt (struct alpha_vms_link_hash_entry *h)
1631 {
1632   /* Handle undefined symbols.  */
1633   if (h == NULL || h->sym == NULL)
1634     return RELC_NONE;
1635
1636   if (h->sym->typ == EGSD__C_SYMG)
1637     {
1638       if (h->sym->flags & EGSY__V_REL)
1639         return RELC_SHR_BASE + PRIV2 (h->sym->owner, shr_index);
1640       else
1641         {
1642           /* Can this happen (non-relocatable symg) ?  I'd like to see
1643              an example.  */
1644           abort ();
1645         }
1646     }
1647   if (h->sym->typ == EGSD__C_SYM)
1648     {
1649       if (h->sym->flags & EGSY__V_REL)
1650         return RELC_REL;
1651       else
1652         return RELC_NONE;
1653     }
1654   abort ();
1655 }
1656
1657 static bfd_vma
1658 alpha_vms_get_sym_value (asection *sect, bfd_vma addr)
1659 {
1660   return sect->output_section->vma + sect->output_offset + addr;
1661 }
1662
1663 static bfd_vma
1664 alpha_vms_fix_sec_rel (bfd *abfd, struct bfd_link_info *info,
1665                        unsigned int rel, bfd_vma vma)
1666 {
1667   asection *sec = PRIV (sections)[rel & RELC_MASK];
1668
1669   if (info)
1670     {
1671       if (sec->output_section == NULL)
1672         abort ();
1673       return vma + sec->output_section->vma + sec->output_offset;
1674     }
1675   else
1676     return vma + sec->vma;
1677 }
1678
1679 /* Read an ETIR record from ABFD.  If INFO is not null, put the content into
1680    the output section (used during linking).
1681    Return FALSE in case of error.  */
1682
1683 static bfd_boolean
1684 _bfd_vms_slurp_etir (bfd *abfd, struct bfd_link_info *info)
1685 {
1686   unsigned char *ptr;
1687   unsigned int length;
1688   unsigned char *maxptr;
1689   bfd_vma op1;
1690   bfd_vma op2;
1691   unsigned int rel1;
1692   unsigned int rel2;
1693   struct alpha_vms_link_hash_entry *h;
1694
1695   PRIV (recrd.rec) += ETIR__C_HEADER_SIZE;
1696   PRIV (recrd.rec_size) -= ETIR__C_HEADER_SIZE;
1697
1698   ptr = PRIV (recrd.rec);
1699   length = PRIV (recrd.rec_size);
1700   maxptr = ptr + length;
1701
1702   vms_debug2 ((2, "ETIR: %d bytes\n", length));
1703
1704   while (ptr < maxptr)
1705     {
1706       int cmd = bfd_getl16 (ptr);
1707       int cmd_length = bfd_getl16 (ptr + 2);
1708
1709       ptr += 4;
1710
1711 #if VMS_DEBUG
1712       _bfd_vms_debug (4, "etir: %s(%d)\n",
1713                       _bfd_vms_etir_name (cmd), cmd);
1714       _bfd_hexdump (8, ptr, cmd_length - 4, (long) ptr);
1715 #endif
1716
1717       switch (cmd)
1718         {
1719           /* Stack global
1720              arg: cs    symbol name
1721
1722              stack 32 bit value of symbol (high bits set to 0).  */
1723         case ETIR__C_STA_GBL:
1724           _bfd_vms_get_value (abfd, ptr, info, &op1, &h);
1725           _bfd_vms_push (abfd, op1, alpha_vms_sym_to_ctxt (h));
1726           break;
1727
1728           /* Stack longword
1729              arg: lw    value
1730
1731              stack 32 bit value, sign extend to 64 bit.  */
1732         case ETIR__C_STA_LW:
1733           _bfd_vms_push (abfd, bfd_getl32 (ptr), RELC_NONE);
1734           break;
1735
1736           /* Stack quadword
1737              arg: qw    value
1738
1739              stack 64 bit value of symbol.  */
1740         case ETIR__C_STA_QW:
1741           _bfd_vms_push (abfd, bfd_getl64 (ptr), RELC_NONE);
1742           break;
1743
1744           /* Stack psect base plus quadword offset
1745              arg: lw    section index
1746              qw signed quadword offset (low 32 bits)
1747
1748              Stack qw argument and section index
1749              (see ETIR__C_STO_OFF, ETIR__C_CTL_SETRB).  */
1750         case ETIR__C_STA_PQ:
1751           {
1752             int psect;
1753
1754             psect = bfd_getl32 (ptr);
1755             if ((unsigned int) psect >= PRIV (section_count))
1756               {
1757                 (*_bfd_error_handler) (_("bad section index in %s"),
1758                                        _bfd_vms_etir_name (cmd));
1759                 bfd_set_error (bfd_error_bad_value);
1760                 return FALSE;
1761               }
1762             op1 = bfd_getl64 (ptr + 4);
1763             _bfd_vms_push (abfd, op1, psect | RELC_SEC_BASE);
1764           }
1765           break;
1766
1767         case ETIR__C_STA_LI:
1768         case ETIR__C_STA_MOD:
1769         case ETIR__C_STA_CKARG:
1770           (*_bfd_error_handler) (_("unsupported STA cmd %s"),
1771                                  _bfd_vms_etir_name (cmd));
1772           return FALSE;
1773           break;
1774
1775           /* Store byte: pop stack, write byte
1776              arg: -.  */
1777         case ETIR__C_STO_B:
1778           _bfd_vms_pop (abfd, &op1, &rel1);
1779           if (rel1 != RELC_NONE)
1780             goto bad_context;
1781           image_write_b (abfd, (unsigned int) op1 & 0xff);
1782           break;
1783
1784           /* Store word: pop stack, write word
1785              arg: -.  */
1786         case ETIR__C_STO_W:
1787           _bfd_vms_pop (abfd, &op1, &rel1);
1788           if (rel1 != RELC_NONE)
1789             goto bad_context;
1790           image_write_w (abfd, (unsigned int) op1 & 0xffff);
1791           break;
1792
1793           /* Store longword: pop stack, write longword
1794              arg: -.  */
1795         case ETIR__C_STO_LW:
1796           _bfd_vms_pop (abfd, &op1, &rel1);
1797           if (rel1 & RELC_SEC_BASE)
1798             {
1799               op1 = alpha_vms_fix_sec_rel (abfd, info, rel1, op1);
1800               rel1 = RELC_REL;
1801             }
1802           else if (rel1 & RELC_SHR_BASE)
1803             {
1804               alpha_vms_add_lw_fixup (info, rel1 & RELC_MASK, op1);
1805               rel1 = RELC_NONE;
1806             }
1807           if (rel1 != RELC_NONE)
1808             {
1809               if (rel1 != RELC_REL)
1810                 abort ();
1811               alpha_vms_add_lw_reloc (info);
1812             }
1813           image_write_l (abfd, op1);
1814           break;
1815
1816           /* Store quadword: pop stack, write quadword
1817              arg: -.  */
1818         case ETIR__C_STO_QW:
1819           _bfd_vms_pop (abfd, &op1, &rel1);
1820           if (rel1 & RELC_SEC_BASE)
1821             {
1822               op1 = alpha_vms_fix_sec_rel (abfd, info, rel1, op1);
1823               rel1 = RELC_REL;
1824             }
1825           else if (rel1 & RELC_SHR_BASE)
1826             abort ();
1827           if (rel1 != RELC_NONE)
1828             {
1829               if (rel1 != RELC_REL)
1830                 abort ();
1831               alpha_vms_add_qw_reloc (info);
1832             }
1833           image_write_q (abfd, op1);
1834           break;
1835
1836           /* Store immediate repeated: pop stack for repeat count
1837              arg: lw    byte count
1838              da data.  */
1839         case ETIR__C_STO_IMMR:
1840           {
1841             int size;
1842
1843             size = bfd_getl32 (ptr);
1844             _bfd_vms_pop (abfd, &op1, &rel1);
1845             if (rel1 != RELC_NONE)
1846               goto bad_context;
1847             while (op1-- > 0)
1848               image_write (abfd, ptr + 4, size);
1849           }
1850           break;
1851
1852           /* Store global: write symbol value
1853              arg: cs    global symbol name.  */
1854         case ETIR__C_STO_GBL:
1855           _bfd_vms_get_value (abfd, ptr, info, &op1, &h);
1856           if (h && h->sym)
1857             {
1858               if (h->sym->typ == EGSD__C_SYMG)
1859                 {
1860                   alpha_vms_add_fixup_qr
1861                     (info, abfd, h->sym->owner, h->sym->symbol_vector);
1862                   op1 = 0;
1863                 }
1864               else
1865                 {
1866                   op1 = alpha_vms_get_sym_value (h->sym->section,
1867                                                  h->sym->value);
1868                   alpha_vms_add_qw_reloc (info);
1869                 }
1870             }
1871           image_write_q (abfd, op1);
1872           break;
1873
1874           /* Store code address: write address of entry point
1875              arg: cs    global symbol name (procedure).  */
1876         case ETIR__C_STO_CA:
1877           _bfd_vms_get_value (abfd, ptr, info, &op1, &h);
1878           if (h && h->sym)
1879             {
1880               if (h->sym->flags & EGSY__V_NORM)
1881                 {
1882                   /* That's really a procedure.  */
1883                   if (h->sym->typ == EGSD__C_SYMG)
1884                     {
1885                       alpha_vms_add_fixup_ca (info, abfd, h->sym->owner);
1886                       op1 = h->sym->symbol_vector;
1887                     }
1888                   else
1889                     {
1890                       op1 = alpha_vms_get_sym_value (h->sym->code_section,
1891                                                      h->sym->code_value);
1892                       alpha_vms_add_qw_reloc (info);
1893                     }
1894                 }
1895               else
1896                 {
1897                   /* Symbol is not a procedure.  */
1898                   abort ();
1899                 }
1900             }
1901           image_write_q (abfd, op1);
1902           break;
1903
1904           /* Store offset to psect: pop stack, add low 32 bits to base of psect
1905              arg: none.  */
1906         case ETIR__C_STO_OFF:
1907           _bfd_vms_pop (abfd, &op1, &rel1);
1908
1909           if (!(rel1 & RELC_SEC_BASE))
1910             abort ();
1911
1912           op1 = alpha_vms_fix_sec_rel (abfd, info, rel1, op1);
1913           rel1 = RELC_REL;
1914           image_write_q (abfd, op1);
1915           break;
1916
1917           /* Store immediate
1918              arg: lw    count of bytes
1919              da data.  */
1920         case ETIR__C_STO_IMM:
1921           {
1922             int size;
1923
1924             size = bfd_getl32 (ptr);
1925             image_write (abfd, ptr + 4, size);
1926           }
1927           break;
1928
1929           /* This code is 'reserved to digital' according to the openVMS
1930              linker manual, however it is generated by the DEC C compiler
1931              and defined in the include file.
1932              FIXME, since the following is just a guess
1933              store global longword: store 32bit value of symbol
1934              arg: cs    symbol name.  */
1935         case ETIR__C_STO_GBL_LW:
1936           _bfd_vms_get_value (abfd, ptr, info, &op1, &h);
1937 #if 0
1938           abort ();
1939 #endif
1940           image_write_l (abfd, op1);
1941           break;
1942
1943         case ETIR__C_STO_RB:
1944         case ETIR__C_STO_AB:
1945         case ETIR__C_STO_LP_PSB:
1946           (*_bfd_error_handler) (_("%s: not supported"),
1947                                  _bfd_vms_etir_name (cmd));
1948           return FALSE;
1949           break;
1950         case ETIR__C_STO_HINT_GBL:
1951         case ETIR__C_STO_HINT_PS:
1952           (*_bfd_error_handler) (_("%s: not implemented"),
1953                                  _bfd_vms_etir_name (cmd));
1954           return FALSE;
1955           break;
1956
1957           /* 200 Store-conditional Linkage Pair
1958              arg: none.  */
1959         case ETIR__C_STC_LP:
1960
1961           /* 202 Store-conditional Address at global address
1962              lw linkage index
1963              cs global name.  */
1964
1965         case ETIR__C_STC_GBL:
1966
1967           /* 203 Store-conditional Code Address at global address
1968              lw linkage index
1969              cs procedure name.  */
1970         case ETIR__C_STC_GCA:
1971
1972           /* 204 Store-conditional Address at psect + offset
1973              lw linkage index
1974              lw psect index
1975              qw offset.  */
1976         case ETIR__C_STC_PS:
1977           (*_bfd_error_handler) (_("%s: not supported"),
1978                                  _bfd_vms_etir_name (cmd));
1979           return FALSE;
1980           break;
1981
1982           /* 201 Store-conditional Linkage Pair with Procedure Signature
1983              lw linkage index
1984              cs procedure name
1985              by signature length
1986              da signature.  */
1987
1988         case ETIR__C_STC_LP_PSB:
1989           _bfd_vms_get_value (abfd, ptr + 4, info, &op1, &h);
1990           if (h && h->sym)
1991             {
1992               if (h->sym->typ == EGSD__C_SYMG)
1993                 {
1994                   alpha_vms_add_fixup_lp (info, abfd, h->sym->owner);
1995                   op1 = h->sym->symbol_vector;
1996                   op2 = 0;
1997                 }
1998               else
1999                 {
2000                   op1 = alpha_vms_get_sym_value (h->sym->code_section,
2001                                                  h->sym->code_value);
2002                   op2 = alpha_vms_get_sym_value (h->sym->section,
2003                                                 h->sym->value);
2004                 }
2005             }
2006           else
2007             {
2008               /* Undefined symbol.  */
2009               op1 = 0;
2010               op2 = 0;
2011             }
2012           image_write_q (abfd, op1);
2013           image_write_q (abfd, op2);
2014           break;
2015
2016           /* 205 Store-conditional NOP at address of global
2017              arg: none.  */
2018         case ETIR__C_STC_NOP_GBL:
2019           /* ALPHA_R_NOP */
2020
2021           /* 207 Store-conditional BSR at global address
2022              arg: none.  */
2023
2024         case ETIR__C_STC_BSR_GBL:
2025           /* ALPHA_R_BSR */
2026
2027           /* 209 Store-conditional LDA at global address
2028              arg: none.  */
2029
2030         case ETIR__C_STC_LDA_GBL:
2031           /* ALPHA_R_LDA */
2032
2033           /* 211 Store-conditional BSR or Hint at global address
2034              arg: none.  */
2035
2036         case ETIR__C_STC_BOH_GBL:
2037           /* Currentl ignored.  */
2038           break;
2039
2040           /* 213 Store-conditional NOP,BSR or HINT at global address
2041              arg: none.  */
2042
2043         case ETIR__C_STC_NBH_GBL:
2044
2045           /* 206 Store-conditional NOP at pect + offset
2046              arg: none.  */
2047
2048         case ETIR__C_STC_NOP_PS:
2049
2050           /* 208 Store-conditional BSR at pect + offset
2051              arg: none.  */
2052
2053         case ETIR__C_STC_BSR_PS:
2054
2055           /* 210 Store-conditional LDA at psect + offset
2056              arg: none.  */
2057
2058         case ETIR__C_STC_LDA_PS:
2059
2060           /* 212 Store-conditional BSR or Hint at pect + offset
2061              arg: none.  */
2062
2063         case ETIR__C_STC_BOH_PS:
2064
2065           /* 214 Store-conditional NOP, BSR or HINT at psect + offset
2066              arg: none.  */
2067         case ETIR__C_STC_NBH_PS:
2068           (*_bfd_error_handler) ("%s: not supported",
2069                                  _bfd_vms_etir_name (cmd));
2070           return FALSE;
2071           break;
2072
2073           /* Det relocation base: pop stack, set image location counter
2074              arg: none.  */
2075         case ETIR__C_CTL_SETRB:
2076           _bfd_vms_pop (abfd, &op1, &rel1);
2077           if (!(rel1 & RELC_SEC_BASE))
2078             abort ();
2079           image_set_ptr (abfd, op1, rel1 & RELC_MASK, info);
2080           break;
2081
2082           /* Augment relocation base: increment image location counter by offset
2083              arg: lw    offset value.  */
2084         case ETIR__C_CTL_AUGRB:
2085           op1 = bfd_getl32 (ptr);
2086           image_inc_ptr (abfd, op1);
2087           break;
2088
2089           /* Define location: pop index, save location counter under index
2090              arg: none.  */
2091         case ETIR__C_CTL_DFLOC:
2092           _bfd_vms_pop (abfd, &op1, &rel1);
2093           if (rel1 != RELC_NONE)
2094             goto bad_context;
2095           dst_define_location (abfd, op1);
2096           break;
2097
2098           /* Set location: pop index, restore location counter from index
2099              arg: none.  */
2100         case ETIR__C_CTL_STLOC:
2101           _bfd_vms_pop (abfd, &op1, &rel1);
2102           if (rel1 != RELC_NONE)
2103             goto bad_context;
2104           dst_restore_location (abfd, op1);
2105           break;
2106
2107           /* Stack defined location: pop index, push location counter from index
2108              arg: none.  */
2109         case ETIR__C_CTL_STKDL:
2110           _bfd_vms_pop (abfd, &op1, &rel1);
2111           if (rel1 != RELC_NONE)
2112             goto bad_context;
2113           _bfd_vms_push (abfd, dst_retrieve_location (abfd, op1), RELC_NONE);
2114           break;
2115
2116         case ETIR__C_OPR_NOP:      /* No-op.  */
2117           break;
2118
2119         case ETIR__C_OPR_ADD:      /* Add.  */
2120           _bfd_vms_pop (abfd, &op1, &rel1);
2121           _bfd_vms_pop (abfd, &op2, &rel2);
2122           if (rel1 == RELC_NONE && rel2 != RELC_NONE)
2123             rel1 = rel2;
2124           else if (rel1 != RELC_NONE && rel2 != RELC_NONE)
2125             goto bad_context;
2126           _bfd_vms_push (abfd, op1 + op2, rel1);
2127           break;
2128
2129         case ETIR__C_OPR_SUB:      /* Subtract.  */
2130           _bfd_vms_pop (abfd, &op1, &rel1);
2131           _bfd_vms_pop (abfd, &op2, &rel2);
2132           if (rel1 == RELC_NONE && rel2 != RELC_NONE)
2133             rel1 = rel2;
2134           else if ((rel1 & RELC_SEC_BASE) && (rel2 & RELC_SEC_BASE))
2135             {
2136               op1 = alpha_vms_fix_sec_rel (abfd, info, rel1, op1);
2137               op2 = alpha_vms_fix_sec_rel (abfd, info, rel2, op2);
2138               rel1 = RELC_NONE;
2139             }
2140           else if (rel1 != RELC_NONE && rel2 != RELC_NONE)
2141             goto bad_context;
2142           _bfd_vms_push (abfd, op2 - op1, rel1);
2143           break;
2144
2145         case ETIR__C_OPR_MUL:      /* Multiply.  */
2146           _bfd_vms_pop (abfd, &op1, &rel1);
2147           _bfd_vms_pop (abfd, &op2, &rel2);
2148           if (rel1 != RELC_NONE || rel2 != RELC_NONE)
2149             goto bad_context;
2150           _bfd_vms_push (abfd, op1 * op2, RELC_NONE);
2151           break;
2152
2153         case ETIR__C_OPR_DIV:      /* Divide.  */
2154           _bfd_vms_pop (abfd, &op1, &rel1);
2155           _bfd_vms_pop (abfd, &op2, &rel2);
2156           if (rel1 != RELC_NONE || rel2 != RELC_NONE)
2157             goto bad_context;
2158           if (op2 == 0)
2159             _bfd_vms_push (abfd, 0, RELC_NONE);
2160           else
2161             _bfd_vms_push (abfd, op2 / op1, RELC_NONE);
2162           break;
2163
2164         case ETIR__C_OPR_AND:      /* Logical AND.  */
2165           _bfd_vms_pop (abfd, &op1, &rel1);
2166           _bfd_vms_pop (abfd, &op2, &rel2);
2167           if (rel1 != RELC_NONE || rel2 != RELC_NONE)
2168             goto bad_context;
2169           _bfd_vms_push (abfd, op1 & op2, RELC_NONE);
2170           break;
2171
2172         case ETIR__C_OPR_IOR:      /* Logical inclusive OR.  */
2173           _bfd_vms_pop (abfd, &op1, &rel1);
2174           _bfd_vms_pop (abfd, &op2, &rel2);
2175           if (rel1 != RELC_NONE || rel2 != RELC_NONE)
2176             goto bad_context;
2177           _bfd_vms_push (abfd, op1 | op2, RELC_NONE);
2178           break;
2179
2180         case ETIR__C_OPR_EOR:      /* Logical exclusive OR.  */
2181           _bfd_vms_pop (abfd, &op1, &rel1);
2182           _bfd_vms_pop (abfd, &op2, &rel2);
2183           if (rel1 != RELC_NONE || rel2 != RELC_NONE)
2184             goto bad_context;
2185           _bfd_vms_push (abfd, op1 ^ op2, RELC_NONE);
2186           break;
2187
2188         case ETIR__C_OPR_NEG:      /* Negate.  */
2189           _bfd_vms_pop (abfd, &op1, &rel1);
2190           if (rel1 != RELC_NONE)
2191             goto bad_context;
2192           _bfd_vms_push (abfd, -op1, RELC_NONE);
2193           break;
2194
2195         case ETIR__C_OPR_COM:      /* Complement.  */
2196           _bfd_vms_pop (abfd, &op1, &rel1);
2197           if (rel1 != RELC_NONE)
2198             goto bad_context;
2199           _bfd_vms_push (abfd, ~op1, RELC_NONE);
2200           break;
2201
2202         case ETIR__C_OPR_ASH:      /* Arithmetic shift.  */
2203           _bfd_vms_pop (abfd, &op1, &rel1);
2204           _bfd_vms_pop (abfd, &op2, &rel2);
2205           if (rel1 != RELC_NONE || rel2 != RELC_NONE)
2206             {
2207             bad_context:
2208               (*_bfd_error_handler) (_("invalid use of %s with contexts"),
2209                                      _bfd_vms_etir_name (cmd));
2210               return FALSE;
2211             }
2212           if ((int)op2 < 0)             /* Shift right.  */
2213             op1 >>= -(int)op2;
2214           else                  /* Shift left.  */
2215             op1 <<= (int)op2;
2216           _bfd_vms_push (abfd, op1, RELC_NONE); /* FIXME: sym.  */
2217           break;
2218
2219         case ETIR__C_OPR_INSV:      /* Insert field.   */
2220         case ETIR__C_OPR_USH:       /* Unsigned shift.   */
2221         case ETIR__C_OPR_ROT:       /* Rotate.  */
2222         case ETIR__C_OPR_REDEF:     /* Redefine symbol to current location.  */
2223         case ETIR__C_OPR_DFLIT:     /* Define a literal.  */
2224           (*_bfd_error_handler) (_("%s: not supported"),
2225                                  _bfd_vms_etir_name (cmd));
2226           return FALSE;
2227           break;
2228
2229         case ETIR__C_OPR_SEL:      /* Select.  */
2230           _bfd_vms_pop (abfd, &op1, &rel1);
2231           if (op1 & 0x01L)
2232             _bfd_vms_pop (abfd, &op1, &rel1);
2233           else
2234             {
2235               _bfd_vms_pop (abfd, &op1, &rel1);
2236               _bfd_vms_pop (abfd, &op2, &rel2);
2237               _bfd_vms_push (abfd, op1, rel1);
2238             }
2239           break;
2240
2241         default:
2242           (*_bfd_error_handler) (_("reserved cmd %d"), cmd);
2243           return FALSE;
2244           break;
2245         }
2246
2247       ptr += cmd_length - 4;
2248     }
2249
2250   return TRUE;
2251 }
2252
2253 /* Process EDBG/ETBT record.
2254    Return TRUE on success, FALSE on error  */
2255
2256 static bfd_boolean
2257 vms_slurp_debug (bfd *abfd)
2258 {
2259   asection *section = PRIV (dst_section);
2260
2261   if (section == NULL)
2262     {
2263       /* We have no way to find out beforehand how much debug info there
2264          is in an object file, so pick an initial amount and grow it as
2265          needed later.  */
2266       flagword flags = SEC_HAS_CONTENTS | SEC_DEBUGGING | SEC_RELOC
2267         | SEC_IN_MEMORY;
2268
2269       section = bfd_make_section (abfd, "$DST$");
2270       if (!section)
2271         return FALSE;
2272       if (!bfd_set_section_flags (abfd, section, flags))
2273         return FALSE;
2274       PRIV (dst_section) = section;
2275     }
2276
2277   PRIV (image_section) = section;
2278   PRIV (image_offset) = section->size;
2279
2280   if (!_bfd_vms_slurp_etir (abfd, NULL))
2281     return FALSE;
2282
2283   section->size = PRIV (image_offset);
2284   return TRUE;
2285 }
2286
2287 /* Process EDBG record.
2288    Return TRUE on success, FALSE on error.  */
2289
2290 static bfd_boolean
2291 _bfd_vms_slurp_edbg (bfd *abfd)
2292 {
2293   vms_debug2 ((2, "EDBG\n"));
2294
2295   abfd->flags |= HAS_DEBUG | HAS_LINENO;
2296
2297   return vms_slurp_debug (abfd);
2298 }
2299
2300 /* Process ETBT record.
2301    Return TRUE on success, FALSE on error.  */
2302
2303 static bfd_boolean
2304 _bfd_vms_slurp_etbt (bfd *abfd)
2305 {
2306   vms_debug2 ((2, "ETBT\n"));
2307
2308   abfd->flags |= HAS_LINENO;
2309
2310   return vms_slurp_debug (abfd);
2311 }
2312
2313 /* Process EEOM record.
2314    Return TRUE on success, FALSE on error.  */
2315
2316 static bfd_boolean
2317 _bfd_vms_slurp_eeom (bfd *abfd)
2318 {
2319   struct vms_eeom *eeom = (struct vms_eeom *) PRIV (recrd.rec);
2320
2321   vms_debug2 ((2, "EEOM\n"));
2322
2323   PRIV (eom_data).eom_l_total_lps = bfd_getl32 (eeom->total_lps);
2324   PRIV (eom_data).eom_w_comcod = bfd_getl16 (eeom->comcod);
2325   if (PRIV (eom_data).eom_w_comcod > 1)
2326     {
2327       (*_bfd_error_handler) (_("Object module NOT error-free !\n"));
2328       bfd_set_error (bfd_error_bad_value);
2329       return FALSE;
2330     }
2331
2332   PRIV (eom_data).eom_has_transfer = FALSE;
2333   if (PRIV (recrd.rec_size) > 10)
2334     {
2335       PRIV (eom_data).eom_has_transfer = TRUE;
2336       PRIV (eom_data).eom_b_tfrflg = eeom->tfrflg;
2337       PRIV (eom_data).eom_l_psindx = bfd_getl32 (eeom->psindx);
2338       PRIV (eom_data).eom_l_tfradr = bfd_getl32 (eeom->tfradr);
2339
2340       abfd->start_address = PRIV (eom_data).eom_l_tfradr;
2341     }
2342   return TRUE;
2343 }
2344
2345 /* Slurp an ordered set of VMS object records.  Return FALSE on error.  */
2346
2347 static bfd_boolean
2348 _bfd_vms_slurp_object_records (bfd * abfd)
2349 {
2350   bfd_boolean err;
2351   int type;
2352
2353   do
2354     {
2355       vms_debug2 ((7, "reading at %08lx\n", (unsigned long)bfd_tell (abfd)));
2356
2357       type = _bfd_vms_get_object_record (abfd);
2358       if (type < 0)
2359         {
2360           vms_debug2 ((2, "next_record failed\n"));
2361           return FALSE;
2362         }
2363
2364       switch (type)
2365         {
2366         case EOBJ__C_EMH:
2367           err = _bfd_vms_slurp_ehdr (abfd);
2368           break;
2369         case EOBJ__C_EEOM:
2370           err = _bfd_vms_slurp_eeom (abfd);
2371           break;
2372         case EOBJ__C_EGSD:
2373           err = _bfd_vms_slurp_egsd (abfd);
2374           break;
2375         case EOBJ__C_ETIR:
2376           err = TRUE; /* _bfd_vms_slurp_etir (abfd); */
2377           break;
2378         case EOBJ__C_EDBG:
2379           err = _bfd_vms_slurp_edbg (abfd);
2380           break;
2381         case EOBJ__C_ETBT:
2382           err = _bfd_vms_slurp_etbt (abfd);
2383           break;
2384         default:
2385           err = FALSE;
2386         }
2387       if (err != TRUE)
2388         {
2389           vms_debug2 ((2, "slurp type %d failed\n", type));
2390           return FALSE;
2391         }
2392     }
2393   while (type != EOBJ__C_EEOM);
2394
2395   return TRUE;
2396 }
2397
2398 /* Initialize private data  */
2399 static bfd_boolean
2400 vms_initialize (bfd * abfd)
2401 {
2402   bfd_size_type amt;
2403
2404   amt = sizeof (struct vms_private_data_struct);
2405   abfd->tdata.any = bfd_zalloc (abfd, amt);
2406   if (abfd->tdata.any == NULL)
2407     return FALSE;
2408
2409   PRIV (recrd.file_format) = FF_UNKNOWN;
2410
2411   amt = sizeof (struct stack_struct) * STACKSIZE;
2412   PRIV (stack) = bfd_alloc (abfd, amt);
2413   if (PRIV (stack) == NULL)
2414     goto error_ret1;
2415
2416   return TRUE;
2417
2418  error_ret1:
2419   bfd_release (abfd, abfd->tdata.any);
2420   abfd->tdata.any = NULL;
2421   return FALSE;
2422 }
2423
2424 /* Check the format for a file being read.
2425    Return a (bfd_target *) if it's an object file or zero if not.  */
2426
2427 static const struct bfd_target *
2428 alpha_vms_object_p (bfd *abfd)
2429 {
2430   PTR tdata_save = abfd->tdata.any;
2431   unsigned int test_len;
2432   unsigned char *buf;
2433
2434   vms_debug2 ((1, "vms_object_p(%p)\n", abfd));
2435
2436   /* Allocate alpha-vms specific data.  */
2437   if (!vms_initialize (abfd))
2438     goto error_ret;
2439
2440   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET))
2441     goto err_wrong_format;
2442
2443   /* The first challenge with VMS is to discover the kind of the file.
2444
2445      Image files (executable or shared images) are stored as a raw
2446      stream of bytes (like on UNIX), but there is no magic number.
2447
2448      Object files are written with RMS (record management service), ie
2449      each records are preceeded by its length (on a word - 2 bytes), and
2450      padded for word-alignment.  That would be simple but when files
2451      are transfered to a UNIX filesystem (using ftp), records are lost.
2452      Only the raw content of the records are transfered.  Fortunately,
2453      the Alpha Object file format also store the length of the record
2454      in the records.  Is that clear ?  */
2455
2456   /* Minimum is 6 bytes for objects (2 bytes size, 2 bytes record id,
2457      2 bytes size repeated) and 12 bytes for images (4 bytes major id,
2458      4 bytes minor id, 4 bytes length).  */
2459   test_len = 12;
2460
2461   /* Size the main buffer.  */
2462   buf = (unsigned char *) bfd_malloc (test_len);
2463   if (buf == NULL)
2464     goto error_ret;
2465   PRIV (recrd.buf) = buf;
2466   PRIV (recrd.buf_size) = test_len;
2467
2468   /* Initialize the record pointer.  */
2469   PRIV (recrd.rec) = buf;
2470
2471   if (bfd_bread (buf, test_len, abfd) != test_len)
2472     {
2473       bfd_set_error (bfd_error_file_truncated);
2474       goto error_ret;
2475     }
2476
2477   /* Is it an image?  */
2478   if ((bfd_getl32 (buf) == EIHD__K_MAJORID)
2479       && (bfd_getl32 (buf + 4) == EIHD__K_MINORID))
2480     {
2481       unsigned int to_read;
2482       unsigned int read_so_far;
2483       unsigned int remaining;
2484       unsigned int eisd_offset, eihs_offset;
2485
2486       /* Extract the header size.  */
2487       PRIV (recrd.rec_size) = bfd_getl32 (buf + EIHD__L_SIZE);
2488
2489       /* The header size is 0 for DSF files.  */
2490       if (PRIV (recrd.rec_size) == 0)
2491         PRIV (recrd.rec_size) = sizeof (struct vms_eihd);
2492
2493       if (PRIV (recrd.rec_size) > PRIV (recrd.buf_size))
2494         {
2495           buf = bfd_realloc_or_free (buf, PRIV (recrd.rec_size));
2496
2497           if (buf == NULL)
2498             {
2499               PRIV (recrd.buf) = NULL;
2500               bfd_set_error (bfd_error_no_memory);
2501               goto error_ret;
2502             }
2503           PRIV (recrd.buf) = buf;
2504           PRIV (recrd.buf_size) = PRIV (recrd.rec_size);
2505         }
2506
2507       /* Read the remaining record.  */
2508       remaining = PRIV (recrd.rec_size) - test_len;
2509       to_read = MIN (VMS_BLOCK_SIZE - test_len, remaining);
2510       read_so_far = test_len;
2511
2512       while (remaining > 0)
2513         {
2514           if (bfd_bread (buf + read_so_far, to_read, abfd) != to_read)
2515             {
2516               bfd_set_error (bfd_error_file_truncated);
2517               goto err_wrong_format;
2518             }
2519
2520           read_so_far += to_read;
2521           remaining -= to_read;
2522
2523           to_read = MIN (VMS_BLOCK_SIZE, remaining);
2524         }
2525
2526       /* Reset the record pointer.  */
2527       PRIV (recrd.rec) = buf;
2528
2529       vms_debug2 ((2, "file type is image\n"));
2530
2531       if (_bfd_vms_slurp_eihd (abfd, &eisd_offset, &eihs_offset) != TRUE)
2532         goto err_wrong_format;
2533
2534       if (_bfd_vms_slurp_eisd (abfd, eisd_offset) != TRUE)
2535         goto err_wrong_format;
2536
2537       /* EIHS is optional.  */
2538       if (eihs_offset != 0 && _bfd_vms_slurp_eihs (abfd, eihs_offset) != TRUE)
2539         goto err_wrong_format;
2540     }
2541   else
2542     {
2543       int type;
2544
2545       /* Assume it's a module and adjust record pointer if necessary.  */
2546       maybe_adjust_record_pointer_for_object (abfd);
2547
2548       /* But is it really a module?  */
2549       if (bfd_getl16 (PRIV (recrd.rec)) <= EOBJ__C_MAXRECTYP
2550           && bfd_getl16 (PRIV (recrd.rec) + 2) <= EOBJ__C_MAXRECSIZ)
2551         {
2552           if (vms_get_remaining_object_record (abfd, test_len) <= 0)
2553             goto err_wrong_format;
2554
2555           vms_debug2 ((2, "file type is module\n"));
2556
2557           type = bfd_getl16 (PRIV (recrd.rec));
2558           if (type != EOBJ__C_EMH || _bfd_vms_slurp_ehdr (abfd) != TRUE)
2559             goto err_wrong_format;
2560
2561           if (_bfd_vms_slurp_object_records (abfd) != TRUE)
2562             goto err_wrong_format;
2563         }
2564       else
2565         goto err_wrong_format;
2566     }
2567
2568   /* Set arch_info to alpha.   */
2569
2570   if (! bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0))
2571     goto err_wrong_format;
2572
2573   return abfd->xvec;
2574
2575  err_wrong_format:
2576   bfd_set_error (bfd_error_wrong_format);
2577
2578  error_ret:
2579   if (PRIV (recrd.buf))
2580     free (PRIV (recrd.buf));
2581   if (abfd->tdata.any != tdata_save && abfd->tdata.any != NULL)
2582     bfd_release (abfd, abfd->tdata.any);
2583   abfd->tdata.any = tdata_save;
2584   return NULL;
2585 }
2586 \f
2587 /* Image write.  */
2588
2589 /* Write an EMH/MHD record.  */
2590
2591 static void
2592 _bfd_vms_write_emh (bfd *abfd)
2593 {
2594   struct vms_rec_wr *recwr = &PRIV (recwr);
2595
2596   _bfd_vms_output_alignment (recwr, 2);
2597
2598   /* EMH.  */
2599   _bfd_vms_output_begin (recwr, EOBJ__C_EMH);
2600   _bfd_vms_output_short (recwr, EMH__C_MHD);
2601   _bfd_vms_output_short (recwr, EOBJ__C_STRLVL);
2602   _bfd_vms_output_long (recwr, 0);
2603   _bfd_vms_output_long (recwr, 0);
2604   _bfd_vms_output_long (recwr, MAX_OUTREC_SIZE);
2605
2606   /* Create module name from filename.  */
2607   if (bfd_get_filename (abfd) != 0)
2608     {
2609       char *module = vms_get_module_name (bfd_get_filename (abfd), TRUE);
2610       _bfd_vms_output_counted (recwr, module);
2611       free (module);
2612     }
2613   else
2614     _bfd_vms_output_counted (recwr, "NONAME");
2615
2616   _bfd_vms_output_counted (recwr, BFD_VERSION_STRING);
2617   _bfd_vms_output_dump (recwr, get_vms_time_string (), EMH_DATE_LENGTH);
2618   _bfd_vms_output_fill (recwr, 0, EMH_DATE_LENGTH);
2619   _bfd_vms_output_end (abfd, recwr);
2620 }
2621
2622 /* Write an EMH/LMN record.  */
2623
2624 static void
2625 _bfd_vms_write_lmn (bfd *abfd, const char *name)
2626 {
2627   char version [64];
2628   struct vms_rec_wr *recwr = &PRIV (recwr);
2629   unsigned int ver = BFD_VERSION / 10000;
2630
2631   /* LMN.  */
2632   _bfd_vms_output_begin (recwr, EOBJ__C_EMH);
2633   _bfd_vms_output_short (recwr, EMH__C_LNM);
2634   snprintf (version, sizeof (version), "%s %d.%d.%d", name,
2635             ver / 10000, (ver / 100) % 100, ver % 100);
2636   _bfd_vms_output_dump (recwr, (unsigned char *)version, strlen (version));
2637   _bfd_vms_output_end (abfd, recwr);
2638 }
2639
2640
2641 /* Write eom record for bfd abfd.  Return FALSE on error.  */
2642
2643 static bfd_boolean
2644 _bfd_vms_write_eeom (bfd *abfd)
2645 {
2646   struct vms_rec_wr *recwr = &PRIV (recwr);
2647
2648   vms_debug2 ((2, "vms_write_eeom\n"));
2649
2650   _bfd_vms_output_alignment (recwr, 2);
2651
2652   _bfd_vms_output_begin (recwr, EOBJ__C_EEOM);
2653   _bfd_vms_output_long (recwr, (unsigned long) (PRIV (vms_linkage_index) >> 1));
2654   _bfd_vms_output_byte (recwr, 0);      /* Completion code.  */
2655   _bfd_vms_output_byte (recwr, 0);      /* Fill byte.  */
2656
2657   if ((abfd->flags & EXEC_P) == 0
2658       && bfd_get_start_address (abfd) != (bfd_vma)-1)
2659     {
2660       asection *section;
2661
2662       section = bfd_get_section_by_name (abfd, ".link");
2663       if (section == 0)
2664         {
2665           bfd_set_error (bfd_error_nonrepresentable_section);
2666           return FALSE;
2667         }
2668       _bfd_vms_output_short (recwr, 0);
2669       _bfd_vms_output_long (recwr, (unsigned long) section->target_index);
2670       _bfd_vms_output_long (recwr,
2671                              (unsigned long) bfd_get_start_address (abfd));
2672       _bfd_vms_output_long (recwr, 0);
2673     }
2674
2675   _bfd_vms_output_end (abfd, recwr);
2676   return TRUE;
2677 }
2678
2679 /* This hash routine borrowed from GNU-EMACS, and strengthened
2680    slightly.  ERY.  */
2681
2682 static int
2683 hash_string (const char *ptr)
2684 {
2685   const unsigned char *p = (unsigned char *) ptr;
2686   const unsigned char *end = p + strlen (ptr);
2687   unsigned char c;
2688   int hash = 0;
2689
2690   while (p != end)
2691     {
2692       c = *p++;
2693       hash = ((hash << 3) + (hash << 15) + (hash >> 28) + c);
2694     }
2695   return hash;
2696 }
2697
2698 /* Generate a length-hashed VMS symbol name (limited to maxlen chars).  */
2699
2700 static char *
2701 _bfd_vms_length_hash_symbol (bfd *abfd, const char *in, int maxlen)
2702 {
2703   unsigned long result;
2704   int in_len;
2705   char *new_name;
2706   const char *old_name;
2707   int i;
2708   static char outbuf[EOBJ__C_SYMSIZ + 1];
2709   char *out = outbuf;
2710
2711 #if VMS_DEBUG
2712   vms_debug (4, "_bfd_vms_length_hash_symbol \"%s\"\n", in);
2713 #endif
2714
2715   if (maxlen > EOBJ__C_SYMSIZ)
2716     maxlen = EOBJ__C_SYMSIZ;
2717
2718   /* Save this for later.  */
2719   new_name = out;
2720
2721   /* We may need to truncate the symbol, save the hash for later.  */
2722   in_len = strlen (in);
2723
2724   result = (in_len > maxlen) ? hash_string (in) : 0;
2725
2726   old_name = in;
2727
2728   /* Do the length checking.  */
2729   if (in_len <= maxlen)
2730     i = in_len;
2731   else
2732     {
2733       if (PRIV (flag_hash_long_names))
2734         i = maxlen - 9;
2735       else
2736         i = maxlen;
2737     }
2738
2739   strncpy (out, in, (size_t) i);
2740   in += i;
2741   out += i;
2742
2743   if ((in_len > maxlen)
2744       && PRIV (flag_hash_long_names))
2745     sprintf (out, "_%08lx", result);
2746   else
2747     *out = 0;
2748
2749 #if VMS_DEBUG
2750   vms_debug (4, "--> [%d]\"%s\"\n", (int)strlen (outbuf), outbuf);
2751 #endif
2752
2753   if (in_len > maxlen
2754         && PRIV (flag_hash_long_names)
2755         && PRIV (flag_show_after_trunc))
2756     printf (_("Symbol %s replaced by %s\n"), old_name, new_name);
2757
2758   return outbuf;
2759 }
2760
2761 static void
2762 vector_grow1 (struct vector_type *vec, size_t elsz)
2763 {
2764   if (vec->nbr_el + 1 < vec->max_el)
2765     return;
2766
2767   if (vec->max_el == 0)
2768     {
2769       vec->max_el = 16;
2770       vec->els = bfd_malloc2 (vec->max_el, elsz);
2771     }
2772   else
2773     {
2774       vec->max_el *= 2;
2775       vec->els = bfd_realloc2 (vec->els, vec->max_el, elsz);
2776     }
2777 }
2778
2779 /* Bump ABFD file position to next block.  */
2780
2781 static void
2782 alpha_vms_file_position_block (bfd *abfd)
2783 {
2784   /* Next block.  */
2785   PRIV (file_pos) += VMS_BLOCK_SIZE - 1;
2786   PRIV (file_pos) -= (PRIV (file_pos) % VMS_BLOCK_SIZE);
2787 }
2788
2789 /* Convert from internal structure SRC to external structure DST.  */
2790
2791 static void
2792 alpha_vms_swap_eisd_out (struct vms_internal_eisd_map *src,
2793                          struct vms_eisd *dst)
2794 {
2795   bfd_putl32 (src->u.eisd.majorid, dst->majorid);
2796   bfd_putl32 (src->u.eisd.minorid, dst->minorid);
2797   bfd_putl32 (src->u.eisd.eisdsize, dst->eisdsize);
2798   if (src->u.eisd.eisdsize <= EISD__K_LENEND)
2799     return;
2800   bfd_putl32 (src->u.eisd.secsize, dst->secsize);
2801   bfd_putl64 (src->u.eisd.virt_addr, dst->virt_addr);
2802   bfd_putl32 (src->u.eisd.flags, dst->flags);
2803   bfd_putl32 (src->u.eisd.vbn, dst->vbn);
2804   dst->pfc = src->u.eisd.pfc;
2805   dst->matchctl = src->u.eisd.matchctl;
2806   dst->type = src->u.eisd.type;
2807   dst->fill_1 = 0;
2808   if (src->u.eisd.flags & EISD__M_GBL)
2809     {
2810       bfd_putl32 (src->u.gbl_eisd.ident, dst->ident);
2811       memcpy (dst->gblnam, src->u.gbl_eisd.gblnam,
2812               src->u.gbl_eisd.gblnam[0] + 1);
2813     }
2814 }
2815
2816 /* Append EISD to the list of extra eisd for ABFD.  */
2817
2818 static void
2819 alpha_vms_append_extra_eisd (bfd *abfd, struct vms_internal_eisd_map *eisd)
2820 {
2821   eisd->next = NULL;
2822   if (PRIV (gbl_eisd_head) == NULL)
2823     PRIV (gbl_eisd_head) = eisd;
2824   else
2825     PRIV (gbl_eisd_tail)->next = eisd;
2826   PRIV (gbl_eisd_tail) = eisd;
2827 }
2828
2829 /* Create an EISD for shared image SHRIMG.
2830    Return FALSE in case of error.  */
2831
2832 static bfd_boolean
2833 alpha_vms_create_eisd_for_shared (bfd *abfd, bfd *shrimg)
2834 {
2835   struct vms_internal_eisd_map *eisd;
2836   int namlen;
2837
2838   namlen = strlen (PRIV2 (shrimg, hdr_data.hdr_t_name));
2839   if (namlen + 5 > EISD__K_GBLNAMLEN)
2840     {
2841       /* Won't fit.  */
2842       return FALSE;
2843     }
2844
2845   eisd = bfd_alloc (abfd, sizeof (*eisd));
2846   if (eisd == NULL)
2847     return FALSE;
2848
2849   /* Fill the fields.  */
2850   eisd->u.gbl_eisd.common.majorid = EISD__K_MAJORID;
2851   eisd->u.gbl_eisd.common.minorid = EISD__K_MINORID;
2852   eisd->u.gbl_eisd.common.eisdsize = (EISD__K_LEN + 4 + namlen + 5 + 3) & ~3;
2853   eisd->u.gbl_eisd.common.secsize = VMS_BLOCK_SIZE;     /* Must not be 0.  */
2854   eisd->u.gbl_eisd.common.virt_addr = 0;
2855   eisd->u.gbl_eisd.common.flags = EISD__M_GBL;
2856   eisd->u.gbl_eisd.common.vbn = 0;
2857   eisd->u.gbl_eisd.common.pfc = 0;
2858   eisd->u.gbl_eisd.common.matchctl = PRIV2 (shrimg, matchctl);
2859   eisd->u.gbl_eisd.common.type = EISD__K_SHRPIC;
2860
2861   eisd->u.gbl_eisd.ident = PRIV2 (shrimg, ident);
2862   eisd->u.gbl_eisd.gblnam[0] = namlen + 4;
2863   memcpy (eisd->u.gbl_eisd.gblnam + 1, PRIV2 (shrimg, hdr_data.hdr_t_name),
2864           namlen);
2865   memcpy (eisd->u.gbl_eisd.gblnam + 1 + namlen, "_001", 4);
2866
2867   /* Append it to the list.  */
2868   alpha_vms_append_extra_eisd (abfd, eisd);
2869
2870   return TRUE;
2871 }
2872
2873 /* Create an EISD for section SEC.
2874    Return FALSE in case of failure.  */
2875
2876 static bfd_boolean
2877 alpha_vms_create_eisd_for_section (bfd *abfd, asection *sec)
2878 {
2879   struct vms_internal_eisd_map *eisd;
2880
2881   /* Only for allocating section.  */
2882   if (!(sec->flags & SEC_ALLOC))
2883     return TRUE;
2884
2885   BFD_ASSERT (vms_section_data (sec)->eisd == NULL);
2886   eisd = bfd_alloc (abfd, sizeof (*eisd));
2887   if (eisd == NULL)
2888     return FALSE;
2889   vms_section_data (sec)->eisd = eisd;
2890
2891   /* Fill the fields.  */
2892   eisd->u.eisd.majorid = EISD__K_MAJORID;
2893   eisd->u.eisd.minorid = EISD__K_MINORID;
2894   eisd->u.eisd.eisdsize = EISD__K_LEN;
2895   eisd->u.eisd.secsize =
2896     (sec->size + VMS_BLOCK_SIZE - 1) & ~(VMS_BLOCK_SIZE - 1);
2897   eisd->u.eisd.virt_addr = sec->vma;
2898   eisd->u.eisd.flags = 0;
2899   eisd->u.eisd.vbn = 0; /* To be later defined.  */
2900   eisd->u.eisd.pfc = 0; /* Default.  */
2901   eisd->u.eisd.matchctl = EISD__K_MATALL;
2902   eisd->u.eisd.type = EISD__K_NORMAL;
2903
2904   if (sec->flags & SEC_CODE)
2905     eisd->u.eisd.flags |= EISD__M_EXE;
2906   if (!(sec->flags & SEC_READONLY))
2907     eisd->u.eisd.flags |= EISD__M_WRT | EISD__M_CRF;
2908
2909   if (!(sec->flags & SEC_LOAD))
2910     {
2911       eisd->u.eisd.flags |= EISD__M_DZRO;
2912       eisd->u.eisd.flags &= ~EISD__M_CRF;
2913     }
2914   if (sec->flags & SEC_LINKER_CREATED)
2915     {
2916       if (strcmp (sec->name, "$FIXUP$") == 0)
2917         eisd->u.eisd.flags |= EISD__M_FIXUPVEC;
2918     }
2919
2920   /* Append it to the list.  */
2921   eisd->next = NULL;
2922   if (PRIV (eisd_head) == NULL)
2923     PRIV (eisd_head) = eisd;
2924   else
2925     PRIV (eisd_tail)->next = eisd;
2926   PRIV (eisd_tail) = eisd;
2927
2928   return TRUE;
2929 }
2930
2931 /* Layout executable ABFD and write it to the disk.
2932    Return FALSE in case of failure.  */
2933
2934 static bfd_boolean
2935 alpha_vms_write_exec (bfd *abfd)
2936 {
2937   struct vms_eihd eihd;
2938   struct vms_eiha *eiha;
2939   struct vms_eihi *eihi;
2940   struct vms_eihs *eihs = NULL;
2941   asection *sec;
2942   struct vms_internal_eisd_map *first_eisd;
2943   struct vms_internal_eisd_map *eisd;
2944   asection *dst;
2945   asection *dmt;
2946   file_ptr gst_filepos = 0;
2947   unsigned int lnkflags = 0;
2948
2949   /* Build the EIHD.  */
2950   PRIV (file_pos) = EIHD__C_LENGTH;
2951
2952   memset (&eihd, 0, sizeof (eihd));
2953   memset (eihd.fill_2, 0xff, sizeof (eihd.fill_2));
2954
2955   bfd_putl32 (EIHD__K_MAJORID, eihd.majorid);
2956   bfd_putl32 (EIHD__K_MINORID, eihd.minorid);
2957
2958   bfd_putl32 (sizeof (eihd), eihd.size);
2959   bfd_putl32 (0, eihd.isdoff);
2960   bfd_putl32 (0, eihd.activoff);
2961   bfd_putl32 (0, eihd.symdbgoff);
2962   bfd_putl32 (0, eihd.imgidoff);
2963   bfd_putl32 (0, eihd.patchoff);
2964   bfd_putl64 (0, eihd.iafva);
2965   bfd_putl32 (0, eihd.version_array_off);
2966
2967   bfd_putl32 (EIHD__K_EXE, eihd.imgtype);
2968   bfd_putl32 (0, eihd.subtype);
2969
2970   bfd_putl32 (0, eihd.imgiocnt);
2971   bfd_putl32 (-1, eihd.privreqs);
2972   bfd_putl32 (-1, eihd.privreqs + 4);
2973
2974   bfd_putl32 ((sizeof (eihd) + VMS_BLOCK_SIZE - 1) / VMS_BLOCK_SIZE,
2975               eihd.hdrblkcnt);
2976   bfd_putl32 (0, eihd.ident);
2977   bfd_putl32 (0, eihd.sysver);
2978
2979   eihd.matchctl = 0;
2980   bfd_putl32 (0, eihd.symvect_size);
2981   bfd_putl32 (16, eihd.virt_mem_block_size);
2982   bfd_putl32 (0, eihd.ext_fixup_off);
2983   bfd_putl32 (0, eihd.noopt_psect_off);
2984   bfd_putl32 (-1, eihd.alias);
2985
2986   /* Alloc EIHA.  */
2987   eiha = (struct vms_eiha *)((char *) &eihd + PRIV (file_pos));
2988   bfd_putl32 (PRIV (file_pos), eihd.activoff);
2989   PRIV (file_pos) += sizeof (struct vms_eiha);
2990
2991   bfd_putl32 (sizeof (struct vms_eiha), eiha->size);
2992   bfd_putl32 (0, eiha->spare);
2993   bfd_putl64 (PRIV (transfer_address[0]), eiha->tfradr1);
2994   bfd_putl64 (PRIV (transfer_address[1]), eiha->tfradr2);
2995   bfd_putl64 (PRIV (transfer_address[2]), eiha->tfradr3);
2996   bfd_putl64 (PRIV (transfer_address[3]), eiha->tfradr4);
2997   bfd_putl64 (0, eiha->inishr);
2998
2999   /* Alloc EIHI.  */
3000   eihi = (struct vms_eihi *)((char *) &eihd + PRIV (file_pos));
3001   bfd_putl32 (PRIV (file_pos), eihd.imgidoff);
3002   PRIV (file_pos) += sizeof (struct vms_eihi);
3003
3004   bfd_putl32 (EIHI__K_MAJORID, eihi->majorid);
3005   bfd_putl32 (EIHI__K_MINORID, eihi->minorid);
3006   {
3007     char *module;
3008     unsigned int len;
3009
3010     /* Set module name.  */
3011     module = vms_get_module_name (bfd_get_filename (abfd), TRUE);
3012     len = strlen (module);
3013     if (len > sizeof (eihi->imgnam) - 1)
3014       len = sizeof (eihi->imgnam) - 1;
3015     eihi->imgnam[0] = len;
3016     memcpy (eihi->imgnam + 1, module, len);
3017     free (module);
3018   }
3019   {
3020     unsigned int lo;
3021     unsigned int hi;
3022
3023     /* Set time.  */
3024     vms_get_time (&hi, &lo);
3025     bfd_putl32 (lo, eihi->linktime + 0);
3026     bfd_putl32 (hi, eihi->linktime + 4);
3027   }
3028   eihi->imgid[0] = 0;
3029   eihi->linkid[0] = 0;
3030   eihi->imgbid[0] = 0;
3031
3032   /* Alloc EIHS.  */
3033   dst = PRIV (dst_section);
3034   dmt = bfd_get_section_by_name (abfd, "$DMT$");
3035   if (dst != NULL && dst->size != 0)
3036     {
3037       eihs = (struct vms_eihs *)((char *) &eihd + PRIV (file_pos));
3038       bfd_putl32 (PRIV (file_pos), eihd.symdbgoff);
3039       PRIV (file_pos) += sizeof (struct vms_eihs);
3040
3041       bfd_putl32 (EIHS__K_MAJORID, eihs->majorid);
3042       bfd_putl32 (EIHS__K_MINORID, eihs->minorid);
3043       bfd_putl32 (0, eihs->dstvbn);
3044       bfd_putl32 (0, eihs->dstsize);
3045       bfd_putl32 (0, eihs->gstvbn);
3046       bfd_putl32 (0, eihs->gstsize);
3047       bfd_putl32 (0, eihs->dmtvbn);
3048       bfd_putl32 (0, eihs->dmtsize);
3049     }
3050
3051   /* One EISD per section.  */
3052   for (sec = abfd->sections; sec; sec = sec->next)
3053     {
3054       if (!alpha_vms_create_eisd_for_section (abfd, sec))
3055         return FALSE;
3056     }
3057
3058   /* Merge section EIDS which extra ones.  */
3059   if (PRIV (eisd_tail))
3060     PRIV (eisd_tail)->next = PRIV (gbl_eisd_head);
3061   else
3062     PRIV (eisd_head) = PRIV (gbl_eisd_head);
3063   if (PRIV (gbl_eisd_tail))
3064     PRIV (eisd_tail) = PRIV (gbl_eisd_tail);
3065
3066   first_eisd = PRIV (eisd_head);
3067
3068   /* Add end of eisd.  */
3069   if (first_eisd)
3070     {
3071       eisd = bfd_zalloc (abfd, sizeof (*eisd));
3072       if (eisd == NULL)
3073         return FALSE;
3074       eisd->u.eisd.majorid = 0;
3075       eisd->u.eisd.minorid = 0;
3076       eisd->u.eisd.eisdsize = 0;
3077       alpha_vms_append_extra_eisd (abfd, eisd);
3078     }
3079
3080   /* Place EISD in the file.  */
3081   for (eisd = first_eisd; eisd; eisd = eisd->next)
3082     {
3083       file_ptr room = VMS_BLOCK_SIZE - (PRIV (file_pos) % VMS_BLOCK_SIZE);
3084
3085       /* First block is a little bit special: there is a word at the end.  */
3086       if (PRIV (file_pos) < VMS_BLOCK_SIZE && room > 2)
3087         room -= 2;
3088       if (room < eisd->u.eisd.eisdsize + EISD__K_LENEND)
3089         alpha_vms_file_position_block (abfd);
3090
3091       eisd->file_pos = PRIV (file_pos);
3092       PRIV (file_pos) += eisd->u.eisd.eisdsize;
3093
3094       if (eisd->u.eisd.flags & EISD__M_FIXUPVEC)
3095         bfd_putl64 (eisd->u.eisd.virt_addr, eihd.iafva);
3096     }
3097
3098   if (first_eisd != NULL)
3099     {
3100       bfd_putl32 (first_eisd->file_pos, eihd.isdoff);
3101       /* Real size of end of eisd marker.  */
3102       PRIV (file_pos) += EISD__K_LENEND;
3103     }
3104
3105   bfd_putl32 (PRIV (file_pos), eihd.size);
3106   bfd_putl32 ((PRIV (file_pos) + VMS_BLOCK_SIZE - 1) / VMS_BLOCK_SIZE,
3107               eihd.hdrblkcnt);
3108
3109   /* Place sections.  */
3110   for (sec = abfd->sections; sec; sec = sec->next)
3111     {
3112       if (!(sec->flags & SEC_HAS_CONTENTS))
3113         continue;
3114
3115       eisd = vms_section_data (sec)->eisd;
3116
3117       /* Align on a block.  */
3118       alpha_vms_file_position_block (abfd);
3119       sec->filepos = PRIV (file_pos);
3120
3121       if (eisd != NULL)
3122         eisd->u.eisd.vbn = (sec->filepos / VMS_BLOCK_SIZE) + 1;
3123
3124       PRIV (file_pos) += sec->size;
3125     }
3126
3127   /* Update EIHS.  */
3128   if (eihs != NULL && dst != NULL)
3129     {
3130       bfd_putl32 ((dst->filepos / VMS_BLOCK_SIZE) + 1, eihs->dstvbn);
3131       bfd_putl32 (dst->size, eihs->dstsize);
3132
3133       if (dmt != NULL)
3134         {
3135           lnkflags |= EIHD__M_DBGDMT;
3136           bfd_putl32 ((dmt->filepos / VMS_BLOCK_SIZE) + 1, eihs->dmtvbn);
3137           bfd_putl32 (dmt->size, eihs->dmtsize);
3138         }
3139       if (PRIV (gsd_sym_count) != 0)
3140         {
3141           alpha_vms_file_position_block (abfd);
3142           gst_filepos = PRIV (file_pos);
3143           bfd_putl32 ((gst_filepos / VMS_BLOCK_SIZE) + 1, eihs->gstvbn);
3144           bfd_putl32 ((PRIV (gsd_sym_count) + 4) / 5 + 4, eihs->gstsize);
3145         }
3146     }
3147
3148   /* Write EISD in hdr.  */
3149   for (eisd = first_eisd; eisd && eisd->file_pos < VMS_BLOCK_SIZE;
3150        eisd = eisd->next)
3151     alpha_vms_swap_eisd_out
3152       (eisd, (struct vms_eisd *)((char *)&eihd + eisd->file_pos));
3153
3154   /* Write first block.  */
3155   bfd_putl32 (lnkflags, eihd.lnkflags);
3156   if (bfd_bwrite (&eihd, sizeof (eihd), abfd) != sizeof (eihd))
3157     return FALSE;
3158
3159   /* Write remaining eisd.  */
3160   if (eisd != NULL)
3161     {
3162       unsigned char blk[VMS_BLOCK_SIZE];
3163       struct vms_internal_eisd_map *next_eisd;
3164
3165       memset (blk, 0xff, sizeof (blk));
3166       while (eisd != NULL)
3167         {
3168           alpha_vms_swap_eisd_out
3169             (eisd,
3170              (struct vms_eisd *)(blk + (eisd->file_pos % VMS_BLOCK_SIZE)));
3171
3172           next_eisd = eisd->next;
3173           if (next_eisd == NULL
3174               || (next_eisd->file_pos / VMS_BLOCK_SIZE
3175                   != eisd->file_pos / VMS_BLOCK_SIZE))
3176             {
3177               if (bfd_bwrite (blk, sizeof (blk), abfd) != sizeof (blk))
3178                 return FALSE;
3179
3180               memset (blk, 0xff, sizeof (blk));
3181             }
3182           eisd = next_eisd;
3183         }
3184     }
3185
3186   /* Write sections.  */
3187   for (sec = abfd->sections; sec; sec = sec->next)
3188     {
3189       unsigned char blk[VMS_BLOCK_SIZE];
3190       bfd_size_type len;
3191
3192       if (sec->size == 0 || !(sec->flags & SEC_HAS_CONTENTS))
3193         continue;
3194       if (bfd_bwrite (sec->contents, sec->size, abfd) != sec->size)
3195         return FALSE;
3196
3197       /* Pad.  */
3198       len = VMS_BLOCK_SIZE - sec->size % VMS_BLOCK_SIZE;
3199       if (len != VMS_BLOCK_SIZE)
3200         {
3201           memset (blk, 0, len);
3202           if (bfd_bwrite (blk, len, abfd) != len)
3203             return FALSE;
3204         }
3205     }
3206
3207   /* Write GST.  */
3208   if (gst_filepos != 0)
3209     {
3210       struct vms_rec_wr *recwr = &PRIV (recwr);
3211       unsigned int i;
3212
3213       _bfd_vms_write_emh (abfd);
3214       _bfd_vms_write_lmn (abfd, "GNU LD");
3215
3216       /* PSC for the absolute section.  */
3217       _bfd_vms_output_begin (recwr, EOBJ__C_EGSD);
3218       _bfd_vms_output_long (recwr, 0);
3219       _bfd_vms_output_begin_subrec (recwr, EGSD__C_PSC);
3220       _bfd_vms_output_short (recwr, 0);
3221       _bfd_vms_output_short (recwr, EGPS__V_PIC | EGPS__V_LIB | EGPS__V_RD);
3222       _bfd_vms_output_long (recwr, 0);
3223       _bfd_vms_output_counted (recwr, ".$$ABS$$.");
3224       _bfd_vms_output_end_subrec (recwr);
3225       _bfd_vms_output_end (abfd, recwr);
3226
3227       for (i = 0; i < PRIV (gsd_sym_count); i++)
3228         {
3229           struct vms_symbol_entry *sym = PRIV (syms)[i];
3230           char *hash;
3231           bfd_vma val;
3232           bfd_vma ep;
3233
3234           if ((i % 5) == 0)
3235             {
3236               _bfd_vms_output_alignment (recwr, 8);
3237               _bfd_vms_output_begin (recwr, EOBJ__C_EGSD);
3238               _bfd_vms_output_long (recwr, 0);
3239             }
3240           _bfd_vms_output_begin_subrec (recwr, EGSD__C_SYMG);
3241           _bfd_vms_output_short (recwr, 0); /* Data type, alignment.  */
3242           _bfd_vms_output_short (recwr, sym->flags);
3243
3244           if (sym->code_section)
3245             ep = alpha_vms_get_sym_value (sym->code_section, sym->code_value);
3246           else
3247             {
3248               BFD_ASSERT (sym->code_value == 0);
3249               ep = 0;
3250             }
3251           val = alpha_vms_get_sym_value (sym->section, sym->value);
3252           _bfd_vms_output_quad
3253             (recwr, sym->typ == EGSD__C_SYMG ? sym->symbol_vector : val);
3254           _bfd_vms_output_quad (recwr, ep);
3255           _bfd_vms_output_quad (recwr, val);
3256           _bfd_vms_output_long (recwr, 0);
3257           hash = _bfd_vms_length_hash_symbol (abfd, sym->name, EOBJ__C_SYMSIZ);
3258           _bfd_vms_output_counted (recwr, hash);
3259           _bfd_vms_output_end_subrec (recwr);
3260           if ((i % 5) == 4)
3261             _bfd_vms_output_end (abfd, recwr);
3262         }
3263       if ((i % 5) != 0)
3264         _bfd_vms_output_end (abfd, recwr);
3265
3266       if (!_bfd_vms_write_eeom (abfd))
3267         return FALSE;
3268     }
3269   return TRUE;
3270 }
3271 \f
3272 /* Object write.  */
3273
3274 /* Write section and symbol directory of bfd abfd.  Return FALSE on error.  */
3275
3276 static bfd_boolean
3277 _bfd_vms_write_egsd (bfd *abfd)
3278 {
3279   asection *section;
3280   asymbol *symbol;
3281   unsigned int symnum;
3282   const char *sname;
3283   flagword new_flags, old_flags;
3284   int abs_section_index = -1;
3285   unsigned int target_index = 0;
3286   struct vms_rec_wr *recwr = &PRIV (recwr);
3287
3288   vms_debug2 ((2, "vms_write_egsd\n"));
3289
3290   /* Egsd is quadword aligned.  */
3291   _bfd_vms_output_alignment (recwr, 8);
3292
3293   _bfd_vms_output_begin (recwr, EOBJ__C_EGSD);
3294   _bfd_vms_output_long (recwr, 0);
3295
3296   /* Number sections.  */
3297   for (section = abfd->sections; section != NULL; section = section->next)
3298     {
3299       if (section->flags & SEC_DEBUGGING)
3300         continue;
3301       if (!strcmp (section->name, ".vmsdebug"))
3302         {
3303           section->flags |= SEC_DEBUGGING;
3304           continue;
3305         }
3306       section->target_index = target_index++;
3307     }
3308
3309   for (section = abfd->sections; section != NULL; section = section->next)
3310     {
3311       vms_debug2 ((3, "Section #%d %s, %d bytes\n",
3312                    section->target_index, section->name, (int)section->size));
3313
3314       /* Don't write out the VMS debug info section since it is in the
3315          ETBT and EDBG sections in etir. */
3316       if (section->flags & SEC_DEBUGGING)
3317         continue;
3318
3319       /* 13 bytes egsd, max 31 chars name -> should be 44 bytes.  */
3320       if (_bfd_vms_output_check (recwr, 64) < 0)
3321         {
3322           _bfd_vms_output_end (abfd, recwr);
3323           _bfd_vms_output_begin (recwr, EOBJ__C_EGSD);
3324           _bfd_vms_output_long (recwr, 0);
3325         }
3326
3327       /* Don't know if this is necessary for the linker but for now it keeps
3328          vms_slurp_gsd happy.  */
3329       sname = section->name;
3330       if (*sname == '.')
3331         {
3332           /* Remove leading dot.  */
3333           sname++;
3334           if ((*sname == 't') && (strcmp (sname, "text") == 0))
3335             sname = EVAX_CODE_NAME;
3336           else if ((*sname == 'd') && (strcmp (sname, "data") == 0))
3337             sname = EVAX_DATA_NAME;
3338           else if ((*sname == 'b') && (strcmp (sname, "bss") == 0))
3339             sname = EVAX_BSS_NAME;
3340           else if ((*sname == 'l') && (strcmp (sname, "link") == 0))
3341             sname = EVAX_LINK_NAME;
3342           else if ((*sname == 'r') && (strcmp (sname, "rdata") == 0))
3343             sname = EVAX_READONLY_NAME;
3344           else if ((*sname == 'l') && (strcmp (sname, "literal") == 0))
3345             sname = EVAX_LITERAL_NAME;
3346           else if ((*sname == 'l') && (strcmp (sname, "literals") == 0))
3347             sname = EVAX_LITERALS_NAME;
3348           else if ((*sname == 'c') && (strcmp (sname, "comm") == 0))
3349             sname = EVAX_COMMON_NAME;
3350           else if ((*sname == 'l') && (strcmp (sname, "lcomm") == 0))
3351             sname = EVAX_LOCAL_NAME;
3352         }
3353       else
3354         sname = _bfd_vms_length_hash_symbol (abfd, sname, EOBJ__C_SECSIZ);
3355
3356       if (bfd_is_com_section (section))
3357         new_flags = (EGPS__V_OVR | EGPS__V_REL | EGPS__V_GBL | EGPS__V_RD
3358                      | EGPS__V_WRT | EGPS__V_NOMOD | EGPS__V_COM);
3359       else
3360         new_flags = vms_esecflag_by_name (evax_section_flags, sname,
3361                                           section->size > 0);
3362
3363       /* Modify them as directed.  */
3364       if (section->flags & SEC_READONLY)
3365         new_flags &= ~EGPS__V_WRT;
3366
3367       new_flags &= ~vms_section_data (section)->no_flags;
3368       new_flags |= vms_section_data (section)->flags;
3369
3370       vms_debug2 ((3, "sec flags %x\n", section->flags));
3371       vms_debug2 ((3, "new_flags %x, _raw_size %lu\n",
3372                    new_flags, (unsigned long)section->size));
3373
3374       _bfd_vms_output_begin_subrec (recwr, EGSD__C_PSC);
3375       _bfd_vms_output_short (recwr, section->alignment_power & 0xff);
3376       _bfd_vms_output_short (recwr, new_flags);
3377       _bfd_vms_output_long (recwr, (unsigned long) section->size);
3378       _bfd_vms_output_counted (recwr, sname);
3379       _bfd_vms_output_end_subrec (recwr);
3380
3381       /* If the section is an obsolute one, remind its index as it will be
3382          used later for absolute symbols.  */
3383       if ((new_flags & EGPS__V_REL) == 0 && abs_section_index < 0)
3384         abs_section_index = section->target_index;
3385     }
3386
3387   /* Output symbols.  */
3388   vms_debug2 ((3, "%d symbols found\n", abfd->symcount));
3389
3390   bfd_set_start_address (abfd, (bfd_vma) -1);
3391
3392   for (symnum = 0; symnum < abfd->symcount; symnum++)
3393     {
3394       char *hash;
3395
3396       symbol = abfd->outsymbols[symnum];
3397       old_flags = symbol->flags;
3398
3399       /* Work-around a missing feature:  consider __main as the main entry
3400          point.  */
3401       if (*(symbol->name) == '_')
3402         {
3403           if (strcmp (symbol->name, "__main") == 0)
3404             bfd_set_start_address (abfd, (bfd_vma)symbol->value);
3405         }
3406
3407       /* Only put in the GSD the global and the undefined symbols.  */
3408       if (old_flags & BSF_FILE)
3409         continue;
3410
3411       if ((old_flags & BSF_GLOBAL) == 0 && !bfd_is_und_section (symbol->section))
3412         {
3413           /* If the LIB$INITIIALIZE section is present, add a reference to
3414              LIB$INITIALIZE symbol.  FIXME: this should be done explicitely
3415              in the assembly file.  */
3416           if (!((old_flags & BSF_SECTION_SYM) != 0
3417                 && strcmp (symbol->section->name, "LIB$INITIALIZE") == 0))
3418             continue;
3419         }
3420
3421       /* 13 bytes egsd, max 64 chars name -> should be 77 bytes.  Add 16 more
3422          bytes for a possible ABS section.  */
3423       if (_bfd_vms_output_check (recwr, 80 + 16) < 0)
3424         {
3425           _bfd_vms_output_end (abfd, recwr);
3426           _bfd_vms_output_begin (recwr, EOBJ__C_EGSD);
3427           _bfd_vms_output_long (recwr, 0);
3428         }
3429
3430       if ((old_flags & BSF_GLOBAL) != 0
3431           && bfd_is_abs_section (symbol->section)
3432           && abs_section_index <= 0)
3433         {
3434           /* Create an absolute section if none was defined.  It is highly
3435              unlikely that the name $ABS$ clashes with a user defined
3436              non-absolute section name.  */
3437           _bfd_vms_output_begin_subrec (recwr, EGSD__C_PSC);
3438           _bfd_vms_output_short (recwr, 4);
3439           _bfd_vms_output_short (recwr, EGPS__V_SHR);
3440           _bfd_vms_output_long (recwr, 0);
3441           _bfd_vms_output_counted (recwr, "$ABS$");
3442           _bfd_vms_output_end_subrec (recwr);
3443
3444           abs_section_index = target_index++;
3445         }
3446
3447       _bfd_vms_output_begin_subrec (recwr, EGSD__C_SYM);
3448
3449       /* Data type, alignment.  */
3450       _bfd_vms_output_short (recwr, 0);
3451
3452       new_flags = 0;
3453
3454       if (old_flags & BSF_WEAK)
3455         new_flags |= EGSY__V_WEAK;
3456       if (bfd_is_com_section (symbol->section))         /* .comm  */
3457         new_flags |= (EGSY__V_WEAK | EGSY__V_COMM);
3458
3459       if (old_flags & BSF_FUNCTION)
3460         {
3461           new_flags |= EGSY__V_NORM;
3462           new_flags |= EGSY__V_REL;
3463         }
3464       if (old_flags & BSF_GLOBAL)
3465         {
3466           new_flags |= EGSY__V_DEF;
3467           if (!bfd_is_abs_section (symbol->section))
3468             new_flags |= EGSY__V_REL;
3469         }
3470       _bfd_vms_output_short (recwr, new_flags);
3471
3472       if (old_flags & BSF_GLOBAL)
3473         {
3474           /* Symbol definition.  */
3475           bfd_vma code_address = 0;
3476           unsigned long ca_psindx = 0;
3477           unsigned long psindx;
3478
3479           if ((old_flags & BSF_FUNCTION) && symbol->udata.p != NULL)
3480             {
3481               asymbol *sym;
3482
3483               sym =
3484                 ((struct evax_private_udata_struct *)symbol->udata.p)->enbsym;
3485               code_address = sym->value;
3486               ca_psindx = sym->section->target_index;
3487             }
3488           if (bfd_is_abs_section (symbol->section))
3489             psindx = abs_section_index;
3490           else
3491             psindx = symbol->section->target_index;
3492
3493           _bfd_vms_output_quad (recwr, symbol->value);
3494           _bfd_vms_output_quad (recwr, code_address);
3495           _bfd_vms_output_long (recwr, ca_psindx);
3496           _bfd_vms_output_long (recwr, psindx);
3497         }
3498       hash = _bfd_vms_length_hash_symbol (abfd, symbol->name, EOBJ__C_SYMSIZ);
3499       _bfd_vms_output_counted (recwr, hash);
3500
3501       _bfd_vms_output_end_subrec (recwr);
3502     }
3503
3504   _bfd_vms_output_alignment (recwr, 8);
3505   _bfd_vms_output_end (abfd, recwr);
3506
3507   return TRUE;
3508 }
3509
3510 /* Write object header for bfd abfd.  Return FALSE on error.  */
3511
3512 static bfd_boolean
3513 _bfd_vms_write_ehdr (bfd *abfd)
3514 {
3515   asymbol *symbol;
3516   unsigned int symnum;
3517   int had_case = 0;
3518   int had_file = 0;
3519   struct vms_rec_wr *recwr = &PRIV (recwr);
3520
3521   vms_debug2 ((2, "vms_write_ehdr (%p)\n", abfd));
3522
3523   _bfd_vms_output_alignment (recwr, 2);
3524
3525   _bfd_vms_write_emh (abfd);
3526   _bfd_vms_write_lmn (abfd, "GNU AS");
3527
3528   /* SRC.  */
3529   _bfd_vms_output_begin (recwr, EOBJ__C_EMH);
3530   _bfd_vms_output_short (recwr, EMH__C_SRC);
3531
3532   for (symnum = 0; symnum < abfd->symcount; symnum++)
3533     {
3534       symbol = abfd->outsymbols[symnum];
3535
3536       if (symbol->flags & BSF_FILE)
3537         {
3538           if (CONST_STRNEQ ((char *)symbol->name, "<CASE:"))
3539             {
3540               PRIV (flag_hash_long_names) = symbol->name[6] - '0';
3541               PRIV (flag_show_after_trunc) = symbol->name[7] - '0';
3542
3543               if (had_file)
3544                 break;
3545               had_case = 1;
3546               continue;
3547             }
3548
3549           _bfd_vms_output_dump (recwr, (unsigned char *) symbol->name,
3550                                 (int) strlen (symbol->name));
3551           if (had_case)
3552             break;
3553           had_file = 1;
3554         }
3555     }
3556
3557   if (symnum == abfd->symcount)
3558     _bfd_vms_output_dump (recwr, (unsigned char *) STRING_COMMA_LEN ("noname"));
3559
3560   _bfd_vms_output_end (abfd, recwr);
3561
3562   /* TTL.  */
3563   _bfd_vms_output_begin (recwr, EOBJ__C_EMH);
3564   _bfd_vms_output_short (recwr, EMH__C_TTL);
3565   _bfd_vms_output_dump (recwr, (unsigned char *) STRING_COMMA_LEN ("TTL"));
3566   _bfd_vms_output_end (abfd, recwr);
3567
3568   /* CPR.  */
3569   _bfd_vms_output_begin (recwr, EOBJ__C_EMH);
3570   _bfd_vms_output_short (recwr, EMH__C_CPR);
3571   _bfd_vms_output_dump (recwr,
3572                         (unsigned char *)"GNU BFD ported by Klaus Kämpf 1994-1996",
3573                          39);
3574   _bfd_vms_output_end (abfd, recwr);
3575
3576   return TRUE;
3577 }
3578
3579 /* Part 4.6, relocations.  */
3580
3581 \f
3582 /* WRITE ETIR SECTION
3583
3584    This is still under construction and therefore not documented.  */
3585
3586 /* Close the etir/etbt record.  */
3587
3588 static void
3589 end_etir_record (bfd * abfd)
3590 {
3591   struct vms_rec_wr *recwr = &PRIV (recwr);
3592
3593   _bfd_vms_output_end (abfd, recwr);
3594 }
3595
3596 static void
3597 start_etir_or_etbt_record (bfd *abfd, asection *section, bfd_vma offset)
3598 {
3599   struct vms_rec_wr *recwr = &PRIV (recwr);
3600
3601   if (section->flags & SEC_DEBUGGING)
3602     {
3603       _bfd_vms_output_begin (recwr, EOBJ__C_ETBT);
3604
3605       if (offset == 0)
3606         {
3607           /* Push start offset.  */
3608           _bfd_vms_output_begin_subrec (recwr, ETIR__C_STA_LW);
3609           _bfd_vms_output_long (recwr, (unsigned long) 0);
3610           _bfd_vms_output_end_subrec (recwr);
3611
3612           /* Set location.  */
3613           _bfd_vms_output_begin_subrec (recwr, ETIR__C_CTL_DFLOC);
3614           _bfd_vms_output_end_subrec (recwr);
3615         }
3616     }
3617   else
3618     {
3619       _bfd_vms_output_begin (recwr, EOBJ__C_ETIR);
3620
3621       if (offset == 0)
3622         {
3623           /* Push start offset.  */
3624           _bfd_vms_output_begin_subrec (recwr, ETIR__C_STA_PQ);
3625           _bfd_vms_output_long (recwr, (unsigned long) section->target_index);
3626           _bfd_vms_output_quad (recwr, offset);
3627           _bfd_vms_output_end_subrec (recwr);
3628
3629           /* Start = pop ().  */
3630           _bfd_vms_output_begin_subrec (recwr, ETIR__C_CTL_SETRB);
3631           _bfd_vms_output_end_subrec (recwr);
3632         }
3633     }
3634 }
3635
3636 /* Output a STO_IMM command for SSIZE bytes of data from CPR at virtual
3637    address VADDR in section specified by SEC_INDEX and NAME.  */
3638
3639 static void
3640 sto_imm (bfd *abfd, asection *section,
3641          bfd_size_type ssize, unsigned char *cptr, bfd_vma vaddr)
3642 {
3643   bfd_size_type size;
3644   struct vms_rec_wr *recwr = &PRIV (recwr);
3645
3646 #if VMS_DEBUG
3647   _bfd_vms_debug (8, "sto_imm %d bytes\n", (int) ssize);
3648   _bfd_hexdump (9, cptr, (int) ssize, (int) vaddr);
3649 #endif
3650
3651   while (ssize > 0)
3652     {
3653       /* Try all the rest.  */
3654       size = ssize;
3655
3656       if (_bfd_vms_output_check (recwr, size) < 0)
3657         {
3658           /* Doesn't fit, split !  */
3659           end_etir_record (abfd);
3660
3661           start_etir_or_etbt_record (abfd, section, vaddr);
3662
3663           size = _bfd_vms_output_check (recwr, 0);      /* get max size */
3664           if (size > ssize)                     /* more than what's left ? */
3665             size = ssize;
3666         }
3667
3668       _bfd_vms_output_begin_subrec (recwr, ETIR__C_STO_IMM);
3669       _bfd_vms_output_long (recwr, (unsigned long) (size));
3670       _bfd_vms_output_dump (recwr, cptr, size);
3671       _bfd_vms_output_end_subrec (recwr);
3672
3673 #if VMS_DEBUG
3674       _bfd_vms_debug (10, "dumped %d bytes\n", (int) size);
3675       _bfd_hexdump (10, cptr, (int) size, (int) vaddr);
3676 #endif
3677
3678       vaddr += size;
3679       cptr += size;
3680       ssize -= size;
3681     }
3682 }
3683
3684 static void
3685 etir_output_check (bfd *abfd, asection *section, bfd_vma vaddr, int checklen)
3686 {
3687   if (_bfd_vms_output_check (&PRIV (recwr), checklen) < 0)
3688     {
3689       /* Not enough room in this record.  Close it and open a new one.  */
3690       end_etir_record (abfd);
3691       start_etir_or_etbt_record (abfd, section, vaddr);
3692     }
3693 }
3694
3695 /* Return whether RELOC must be deferred till the end.  */
3696
3697 static bfd_boolean
3698 defer_reloc_p (arelent *reloc)
3699 {
3700   switch (reloc->howto->type)
3701     {
3702     case ALPHA_R_NOP:
3703     case ALPHA_R_LDA:
3704     case ALPHA_R_BSR:
3705     case ALPHA_R_BOH:
3706       return TRUE;
3707
3708     default:
3709       return FALSE;
3710     }
3711 }
3712
3713 /* Write section contents for bfd abfd.  Return FALSE on error.  */
3714
3715 static bfd_boolean
3716 _bfd_vms_write_etir (bfd * abfd, int objtype ATTRIBUTE_UNUSED)
3717 {
3718   asection *section;
3719   struct vms_rec_wr *recwr = &PRIV (recwr);
3720
3721   vms_debug2 ((2, "vms_write_tir (%p, %d)\n", abfd, objtype));
3722
3723   _bfd_vms_output_alignment (recwr, 4);
3724
3725   PRIV (vms_linkage_index) = 1;
3726
3727   for (section = abfd->sections; section; section = section->next)
3728     {
3729       vms_debug2 ((4, "writing %d. section '%s' (%d bytes)\n",
3730                    section->target_index, section->name, (int) (section->size)));
3731
3732       if (!(section->flags & SEC_HAS_CONTENTS)
3733           || bfd_is_com_section (section))
3734         continue;
3735
3736       if (!section->contents)
3737         {
3738           bfd_set_error (bfd_error_no_contents);
3739           return FALSE;
3740         }
3741
3742       start_etir_or_etbt_record (abfd, section, 0);
3743
3744       if (section->flags & SEC_RELOC)
3745         {
3746           bfd_vma curr_addr = 0;
3747           unsigned char *curr_data = section->contents;
3748           bfd_size_type size;
3749           int pass2_needed = 0;
3750           int pass2_in_progress = 0;
3751           unsigned int irel;
3752
3753           if (section->reloc_count <= 0)
3754             (*_bfd_error_handler)
3755               (_("SEC_RELOC with no relocs in section %s"), section->name);
3756
3757 #if VMS_DEBUG
3758           else
3759             {
3760               int i = section->reloc_count;
3761               arelent **rptr = section->orelocation;
3762               _bfd_vms_debug (4, "%d relocations:\n", i);
3763               while (i-- > 0)
3764                 {
3765                   _bfd_vms_debug (4, "sym %s in sec %s, value %08lx, "
3766                                      "addr %08lx, off %08lx, len %d: %s\n",
3767                                   (*(*rptr)->sym_ptr_ptr)->name,
3768                                   (*(*rptr)->sym_ptr_ptr)->section->name,
3769                                   (long) (*(*rptr)->sym_ptr_ptr)->value,
3770                                   (unsigned long)(*rptr)->address,
3771                                   (unsigned long)(*rptr)->addend,
3772                                   bfd_get_reloc_size ((*rptr)->howto),
3773                                   ( *rptr)->howto->name);
3774                   rptr++;
3775                 }
3776             }
3777 #endif
3778
3779         new_pass:
3780           for (irel = 0; irel < section->reloc_count; irel++)
3781             {
3782               struct evax_private_udata_struct *udata;
3783               arelent *rptr = section->orelocation [irel];
3784               bfd_vma addr = rptr->address;
3785               asymbol *sym = *rptr->sym_ptr_ptr;
3786               asection *sec = sym->section;
3787               bfd_boolean defer = defer_reloc_p (rptr);
3788               unsigned int slen;
3789               char *hash;
3790
3791               if (pass2_in_progress)
3792                 {
3793                   /* Non-deferred relocs have already been output.  */
3794                   if (!defer)
3795                     continue;
3796                 }
3797               else
3798                 {
3799                   /* Deferred relocs must be output at the very end.  */
3800                   if (defer)
3801                     {
3802                       pass2_needed = 1;
3803                       continue;
3804                     }
3805
3806                   /* Regular relocs are intertwined with binary data.  */
3807                   if (curr_addr > addr)
3808                     (*_bfd_error_handler) (_("Size error in section %s"),
3809                                            section->name);
3810                   size = addr - curr_addr;
3811                   sto_imm (abfd, section, size, curr_data, curr_addr);
3812                   curr_data += size;
3813                   curr_addr += size;
3814                 }
3815
3816               size = bfd_get_reloc_size (rptr->howto);
3817
3818               switch (rptr->howto->type)
3819                 {
3820                 case ALPHA_R_IGNORE:
3821                   break;
3822
3823                 case ALPHA_R_REFLONG:
3824                   if (bfd_is_und_section (sym->section))
3825                     {
3826                       bfd_vma addend = rptr->addend;
3827                       slen = strlen ((char *) sym->name);
3828                       hash = _bfd_vms_length_hash_symbol
3829                         (abfd, sym->name, EOBJ__C_SYMSIZ);
3830                       etir_output_check (abfd, section, curr_addr, slen);
3831                       if (addend)
3832                         {
3833                           _bfd_vms_output_begin_subrec (recwr, ETIR__C_STA_GBL);
3834                           _bfd_vms_output_counted (recwr, hash);
3835                           _bfd_vms_output_end_subrec (recwr);
3836                           _bfd_vms_output_begin_subrec (recwr, ETIR__C_STA_LW);
3837                           _bfd_vms_output_long (recwr, (unsigned long) addend);
3838                           _bfd_vms_output_end_subrec (recwr);
3839                           _bfd_vms_output_begin_subrec (recwr, ETIR__C_OPR_ADD);
3840                           _bfd_vms_output_end_subrec (recwr);
3841                           _bfd_vms_output_begin_subrec (recwr, ETIR__C_STO_LW);
3842                           _bfd_vms_output_end_subrec (recwr);
3843                         }
3844                       else
3845                         {
3846                           _bfd_vms_output_begin_subrec
3847                             (recwr, ETIR__C_STO_GBL_LW);
3848                           _bfd_vms_output_counted (recwr, hash);
3849                           _bfd_vms_output_end_subrec (recwr);
3850                         }
3851                     }
3852                   else if (bfd_is_abs_section (sym->section))
3853                     {
3854                       etir_output_check (abfd, section, curr_addr, 16);
3855                       _bfd_vms_output_begin_subrec (recwr, ETIR__C_STA_LW);
3856                       _bfd_vms_output_long (recwr, (unsigned long) sym->value);
3857                       _bfd_vms_output_end_subrec (recwr);
3858                       _bfd_vms_output_begin_subrec (recwr, ETIR__C_STO_LW);
3859                       _bfd_vms_output_end_subrec (recwr);
3860                     }
3861                   else
3862                     {
3863                       etir_output_check (abfd, section, curr_addr, 32);
3864                       _bfd_vms_output_begin_subrec (recwr, ETIR__C_STA_PQ);
3865                       _bfd_vms_output_long (recwr,
3866                                             (unsigned long) sec->target_index);
3867                       _bfd_vms_output_quad (recwr, rptr->addend + sym->value);
3868                       _bfd_vms_output_end_subrec (recwr);
3869                       /* ??? Table B-8 of the OpenVMS Linker Utilily Manual
3870                          says that we should have a ETIR__C_STO_OFF here.
3871                          But the relocation would not be BFD_RELOC_32 then.
3872                          This case is very likely unreachable.  */
3873                       _bfd_vms_output_begin_subrec (recwr, ETIR__C_STO_LW);
3874                       _bfd_vms_output_end_subrec (recwr);
3875                     }
3876                   break;
3877
3878                 case ALPHA_R_REFQUAD:
3879                   if (bfd_is_und_section (sym->section))
3880                     {
3881                       bfd_vma addend = rptr->addend;
3882                       slen = strlen ((char *) sym->name);
3883                       hash = _bfd_vms_length_hash_symbol
3884                         (abfd, sym->name, EOBJ__C_SYMSIZ);
3885                       etir_output_check (abfd, section, curr_addr, slen);
3886                       if (addend)
3887                         {
3888                           _bfd_vms_output_begin_subrec (recwr, ETIR__C_STA_GBL);
3889                           _bfd_vms_output_counted (recwr, hash);
3890                           _bfd_vms_output_end_subrec (recwr);
3891                           _bfd_vms_output_begin_subrec (recwr, ETIR__C_STA_QW);
3892                           _bfd_vms_output_quad (recwr, addend);
3893                           _bfd_vms_output_end_subrec (recwr);
3894                           _bfd_vms_output_begin_subrec (recwr, ETIR__C_OPR_ADD);
3895                           _bfd_vms_output_end_subrec (recwr);
3896                           _bfd_vms_output_begin_subrec (recwr, ETIR__C_STO_QW);
3897                           _bfd_vms_output_end_subrec (recwr);
3898                         }
3899                       else
3900                         {
3901                           _bfd_vms_output_begin_subrec (recwr, ETIR__C_STO_GBL);
3902                           _bfd_vms_output_counted (recwr, hash);
3903                           _bfd_vms_output_end_subrec (recwr);
3904                         }
3905                     }
3906                   else if (bfd_is_abs_section (sym->section))
3907                     {
3908                       etir_output_check (abfd, section, curr_addr, 16);
3909                       _bfd_vms_output_begin_subrec (recwr, ETIR__C_STA_QW);
3910                       _bfd_vms_output_quad (recwr, sym->value);
3911                       _bfd_vms_output_end_subrec (recwr);
3912                       _bfd_vms_output_begin_subrec (recwr, ETIR__C_STO_QW);
3913                       _bfd_vms_output_end_subrec (recwr);
3914                     }
3915                   else
3916                     {
3917                       etir_output_check (abfd, section, curr_addr, 32);
3918                       _bfd_vms_output_begin_subrec (recwr, ETIR__C_STA_PQ);
3919                       _bfd_vms_output_long (recwr,
3920                                             (unsigned long) sec->target_index);
3921                       _bfd_vms_output_quad (recwr, rptr->addend + sym->value);
3922                       _bfd_vms_output_end_subrec (recwr);
3923                       _bfd_vms_output_begin_subrec (recwr, ETIR__C_STO_OFF);
3924                       _bfd_vms_output_end_subrec (recwr);
3925                     }
3926                   break;
3927
3928                 case ALPHA_R_HINT:
3929                   sto_imm (abfd, section, size, curr_data, curr_addr);
3930                   break;
3931
3932                 case ALPHA_R_LINKAGE:
3933                   etir_output_check (abfd, section, curr_addr, 64);
3934                   _bfd_vms_output_begin_subrec (recwr, ETIR__C_STC_LP_PSB);
3935                   _bfd_vms_output_long
3936                     (recwr, (unsigned long) PRIV (vms_linkage_index));
3937                   PRIV (vms_linkage_index) += 2;
3938                   hash = _bfd_vms_length_hash_symbol
3939                     (abfd, sym->name, EOBJ__C_SYMSIZ);
3940                   _bfd_vms_output_counted (recwr, hash);
3941                   _bfd_vms_output_byte (recwr, 0);
3942                   _bfd_vms_output_end_subrec (recwr);
3943                   break;
3944
3945                 case ALPHA_R_CODEADDR:
3946                   slen = strlen ((char *) sym->name);
3947                   hash = _bfd_vms_length_hash_symbol
3948                     (abfd, sym->name, EOBJ__C_SYMSIZ);
3949                   etir_output_check (abfd, section, curr_addr, slen);
3950                   _bfd_vms_output_begin_subrec (recwr, ETIR__C_STO_CA);
3951                   _bfd_vms_output_counted (recwr, hash);
3952                   _bfd_vms_output_end_subrec (recwr);
3953                   break;
3954
3955                 case ALPHA_R_NOP:
3956                   udata
3957                     = (struct evax_private_udata_struct *) rptr->sym_ptr_ptr;
3958                   etir_output_check (abfd, section, curr_addr,
3959                                      32 + 1 + strlen (udata->origname));
3960                   _bfd_vms_output_begin_subrec (recwr, ETIR__C_STC_NOP_GBL);
3961                   _bfd_vms_output_long (recwr, (unsigned long) udata->lkindex);
3962                   _bfd_vms_output_long
3963                     (recwr,
3964                      (unsigned long) udata->enbsym->section->target_index);
3965                   _bfd_vms_output_quad (recwr, rptr->address);
3966                   _bfd_vms_output_long (recwr, (unsigned long) 0x47ff041f);
3967                   _bfd_vms_output_long
3968                     (recwr,
3969                      (unsigned long) udata->enbsym->section->target_index);
3970                   _bfd_vms_output_quad (recwr, rptr->addend);
3971                   _bfd_vms_output_counted
3972                     (recwr, _bfd_vms_length_hash_symbol
3973                      (abfd, udata->origname, EOBJ__C_SYMSIZ));
3974                   _bfd_vms_output_end_subrec (recwr);
3975                   break;
3976
3977                 case ALPHA_R_BSR:
3978                   (*_bfd_error_handler) (_("Spurious ALPHA_R_BSR reloc"));
3979                   break;
3980
3981                 case ALPHA_R_LDA:
3982                   udata
3983                     = (struct evax_private_udata_struct *) rptr->sym_ptr_ptr;
3984                   etir_output_check (abfd, section, curr_addr,
3985                                      32 + 1 + strlen (udata->origname));
3986                   _bfd_vms_output_begin_subrec (recwr, ETIR__C_STC_LDA_GBL);
3987                   _bfd_vms_output_long
3988                     (recwr, (unsigned long) udata->lkindex + 1);
3989                   _bfd_vms_output_long
3990                     (recwr,
3991                      (unsigned long) udata->enbsym->section->target_index);
3992                   _bfd_vms_output_quad (recwr, rptr->address);
3993                   _bfd_vms_output_long (recwr, (unsigned long) 0x237B0000);
3994                   _bfd_vms_output_long
3995                     (recwr, (unsigned long) udata->bsym->section->target_index);
3996                   _bfd_vms_output_quad (recwr, rptr->addend);
3997                   _bfd_vms_output_counted
3998                     (recwr, _bfd_vms_length_hash_symbol
3999                      (abfd, udata->origname, EOBJ__C_SYMSIZ));
4000                   _bfd_vms_output_end_subrec (recwr);
4001                   break;
4002
4003                 case ALPHA_R_BOH:
4004                   udata
4005                     = (struct evax_private_udata_struct *) rptr->sym_ptr_ptr;
4006                   etir_output_check (abfd, section, curr_addr,
4007                                        32 + 1 + strlen (udata->origname));
4008                   _bfd_vms_output_begin_subrec (recwr, ETIR__C_STC_BOH_GBL);
4009                   _bfd_vms_output_long (recwr, (unsigned long) udata->lkindex);
4010                   _bfd_vms_output_long
4011                     (recwr,
4012                      (unsigned long) udata->enbsym->section->target_index);
4013                   _bfd_vms_output_quad (recwr, rptr->address);
4014                   _bfd_vms_output_long (recwr, (unsigned long) 0xD3400000);
4015                   _bfd_vms_output_long
4016                     (recwr,
4017                      (unsigned long) udata->enbsym->section->target_index);
4018                   _bfd_vms_output_quad (recwr, rptr->addend);
4019                   _bfd_vms_output_counted
4020                     (recwr, _bfd_vms_length_hash_symbol
4021                      (abfd, udata->origname, EOBJ__C_SYMSIZ));
4022                   _bfd_vms_output_end_subrec (recwr);
4023                   break;
4024
4025                 default:
4026                   (*_bfd_error_handler) (_("Unhandled relocation %s"),
4027                                          rptr->howto->name);
4028                   break;
4029                 }
4030
4031               curr_data += size;
4032               curr_addr += size;
4033             } /* End of relocs loop.  */
4034
4035           if (!pass2_in_progress)
4036             {
4037               /* Output rest of section.  */
4038               if (curr_addr > section->size)
4039                 (*_bfd_error_handler) (_("Size error in section %s"),
4040                                        section->name);
4041               size = section->size - curr_addr;
4042               sto_imm (abfd, section, size, curr_data, curr_addr);
4043               curr_data += size;
4044               curr_addr += size;
4045
4046               if (pass2_needed)
4047                 {
4048                   pass2_in_progress = 1;
4049                   goto new_pass;
4050                 }
4051             }
4052         }
4053
4054       else /* (section->flags & SEC_RELOC) */
4055         sto_imm (abfd, section, section->size, section->contents, 0);
4056
4057       end_etir_record (abfd);
4058     }
4059
4060   _bfd_vms_output_alignment (recwr, 2);
4061   return TRUE;
4062 }
4063
4064 /* Write cached information into a file being written, at bfd_close.  */
4065
4066 static bfd_boolean
4067 alpha_vms_write_object_contents (bfd *abfd)
4068 {
4069   vms_debug2 ((1, "vms_write_object_contents (%p)\n", abfd));
4070
4071   if (abfd->flags & (EXEC_P | DYNAMIC))
4072     {
4073       return alpha_vms_write_exec (abfd);
4074     }
4075   else
4076     {
4077       if (abfd->section_count > 0)                      /* we have sections */
4078         {
4079           if (_bfd_vms_write_ehdr (abfd) != TRUE)
4080             return FALSE;
4081           if (_bfd_vms_write_egsd (abfd) != TRUE)
4082             return FALSE;
4083           if (_bfd_vms_write_etir (abfd, EOBJ__C_ETIR) != TRUE)
4084             return FALSE;
4085           if (_bfd_vms_write_eeom (abfd) != TRUE)
4086             return FALSE;
4087         }
4088     }
4089   return TRUE;
4090 }
4091 \f
4092 /* Debug stuff: nearest line.  */
4093
4094 #define SET_MODULE_PARSED(m) \
4095   do { if ((m)->name == NULL) (m)->name = ""; } while (0)
4096 #define IS_MODULE_PARSED(m) ((m)->name != NULL)
4097
4098 /* Build a new module for the specified BFD.  */
4099
4100 static struct module *
4101 new_module (bfd *abfd)
4102 {
4103   struct module *module
4104     = (struct module *) bfd_zalloc (abfd, sizeof (struct module));
4105   module->file_table_count = 16; /* Arbitrary.  */
4106   module->file_table
4107     = bfd_malloc (module->file_table_count * sizeof (struct fileinfo));
4108   return module;
4109 }
4110
4111 /* Parse debug info for a module and internalize it.  */
4112
4113 static void
4114 parse_module (bfd *abfd, struct module *module, unsigned char *ptr,
4115               int length)
4116 {
4117   unsigned char *maxptr = ptr + length;
4118   unsigned char *src_ptr, *pcl_ptr;
4119   unsigned int prev_linum = 0, curr_linenum = 0;
4120   bfd_vma prev_pc = 0, curr_pc = 0;
4121   struct srecinfo *curr_srec, *srec;
4122   struct lineinfo *curr_line, *line;
4123   struct funcinfo *funcinfo;
4124
4125   /* Initialize tables with zero element.  */
4126   curr_srec = (struct srecinfo *) bfd_zalloc (abfd, sizeof (struct srecinfo));
4127   module->srec_table = curr_srec;
4128
4129   curr_line = (struct lineinfo *) bfd_zalloc (abfd, sizeof (struct lineinfo));
4130   module->line_table = curr_line;
4131
4132   while (length == -1 || ptr < maxptr)
4133     {
4134       /* The first byte is not counted in the recorded length.  */
4135       int rec_length = bfd_getl16 (ptr) + 1;
4136       int rec_type = bfd_getl16 (ptr + 2);
4137
4138       vms_debug2 ((2, "DST record: leng %d, type %d\n", rec_length, rec_type));
4139
4140       if (length == -1 && rec_type == DST__K_MODEND)
4141         break;
4142
4143       switch (rec_type)
4144         {
4145         case DST__K_MODBEG:
4146           module->name
4147             = _bfd_vms_save_counted_string (ptr + DST_S_B_MODBEG_NAME);
4148
4149           curr_pc = 0;
4150           prev_pc = 0;
4151           curr_linenum = 0;
4152           prev_linum = 0;
4153
4154           vms_debug2 ((3, "module: %s\n", module->name));
4155           break;
4156
4157         case DST__K_MODEND:
4158           break;
4159
4160         case DST__K_RTNBEG:
4161           funcinfo = (struct funcinfo *)
4162             bfd_zalloc (abfd, sizeof (struct funcinfo));
4163           funcinfo->name
4164             = _bfd_vms_save_counted_string (ptr + DST_S_B_RTNBEG_NAME);
4165           funcinfo->low = bfd_getl32 (ptr + DST_S_L_RTNBEG_ADDRESS);
4166           funcinfo->next = module->func_table;
4167           module->func_table = funcinfo;
4168
4169           vms_debug2 ((3, "routine: %s at 0x%lx\n",
4170                        funcinfo->name, (unsigned long) funcinfo->low));
4171           break;
4172
4173         case DST__K_RTNEND:
4174           module->func_table->high = module->func_table->low
4175             + bfd_getl32 (ptr + DST_S_L_RTNEND_SIZE) - 1;
4176
4177           if (module->func_table->high > module->high)
4178             module->high = module->func_table->high;
4179
4180           vms_debug2 ((3, "end routine\n"));
4181           break;
4182
4183         case DST__K_PROLOG:
4184           vms_debug2 ((3, "prologue\n"));
4185           break;
4186
4187         case DST__K_EPILOG:
4188           vms_debug2 ((3, "epilog\n"));
4189           break;
4190
4191         case DST__K_BLKBEG:
4192           vms_debug2 ((3, "block\n"));
4193           break;
4194
4195         case DST__K_BLKEND:
4196           vms_debug2 ((3, "end block\n"));
4197           break;
4198
4199         case DST__K_SOURCE:
4200           src_ptr = ptr + DST_S_C_SOURCE_HEADER_SIZE;
4201
4202           vms_debug2 ((3, "source info\n"));
4203
4204           while (src_ptr < ptr + rec_length)
4205             {
4206               int cmd = src_ptr[0], cmd_length, data;
4207
4208               switch (cmd)
4209                 {
4210                 case DST__K_SRC_DECLFILE:
4211                   {
4212                     unsigned int fileid
4213                       = bfd_getl16 (src_ptr + DST_S_W_SRC_DF_FILEID);
4214                     char *filename
4215                       = _bfd_vms_save_counted_string (src_ptr
4216                           + DST_S_B_SRC_DF_FILENAME);
4217
4218                     while (fileid >= module->file_table_count)
4219                       {
4220                         module->file_table_count *= 2;
4221                         module->file_table
4222                           = bfd_realloc (module->file_table,
4223                                          module->file_table_count
4224                                            * sizeof (struct fileinfo));
4225                       }
4226
4227                     module->file_table [fileid].name = filename;
4228                     module->file_table [fileid].srec = 1;
4229                     cmd_length = src_ptr[DST_S_B_SRC_DF_LENGTH] + 2;
4230                     vms_debug2 ((4, "DST_S_C_SRC_DECLFILE: %d, %s\n",
4231                                  fileid, module->file_table [fileid].name));
4232                   }
4233                   break;
4234
4235                 case DST__K_SRC_DEFLINES_B:
4236                   /* Perform the association and set the next higher index
4237                      to the limit.  */
4238                   data = src_ptr[DST_S_B_SRC_UNSBYTE];
4239                   srec = (struct srecinfo *)
4240                     bfd_zalloc (abfd, sizeof (struct srecinfo));
4241                   srec->line = curr_srec->line + data;
4242                   srec->srec = curr_srec->srec + data;
4243                   srec->sfile = curr_srec->sfile;
4244                   curr_srec->next = srec;
4245                   curr_srec = srec;
4246                   cmd_length = 2;
4247                   vms_debug2 ((4, "DST_S_C_SRC_DEFLINES_B: %d\n", data));
4248                   break;
4249
4250                 case DST__K_SRC_DEFLINES_W:
4251                   /* Perform the association and set the next higher index
4252                      to the limit.  */
4253                   data = bfd_getl16 (src_ptr + DST_S_W_SRC_UNSWORD);
4254                   srec = (struct srecinfo *)
4255                     bfd_zalloc (abfd, sizeof (struct srecinfo));
4256                   srec->line = curr_srec->line + data;
4257                   srec->srec = curr_srec->srec + data,
4258                   srec->sfile = curr_srec->sfile;
4259                   curr_srec->next = srec;
4260                   curr_srec = srec;
4261                   cmd_length = 3;
4262                   vms_debug2 ((4, "DST_S_C_SRC_DEFLINES_W: %d\n", data));
4263                   break;
4264
4265                 case DST__K_SRC_INCRLNUM_B:
4266                   data = src_ptr[DST_S_B_SRC_UNSBYTE];
4267                   curr_srec->line += data;
4268                   cmd_length = 2;
4269                   vms_debug2 ((4, "DST_S_C_SRC_INCRLNUM_B: %d\n", data));
4270                   break;
4271
4272                 case DST__K_SRC_SETFILE:
4273                   data = bfd_getl16 (src_ptr + DST_S_W_SRC_UNSWORD);
4274                   curr_srec->sfile = data;
4275                   curr_srec->srec = module->file_table[data].srec;
4276                   cmd_length = 3;
4277                   vms_debug2 ((4, "DST_S_C_SRC_SETFILE: %d\n", data));
4278                   break;
4279
4280                 case DST__K_SRC_SETLNUM_L:
4281                   data = bfd_getl32 (src_ptr + DST_S_L_SRC_UNSLONG);
4282                   curr_srec->line = data;
4283                   cmd_length = 5;
4284                   vms_debug2 ((4, "DST_S_C_SRC_SETLNUM_L: %d\n", data));
4285                   break;
4286
4287                 case DST__K_SRC_SETLNUM_W:
4288                   data = bfd_getl16 (src_ptr + DST_S_W_SRC_UNSWORD);
4289                   curr_srec->line = data;
4290                   cmd_length = 3;
4291                   vms_debug2 ((4, "DST_S_C_SRC_SETLNUM_W: %d\n", data));
4292                   break;
4293
4294                 case DST__K_SRC_SETREC_L:
4295                   data = bfd_getl32 (src_ptr + DST_S_L_SRC_UNSLONG);
4296                   curr_srec->srec = data;
4297                   module->file_table[curr_srec->sfile].srec = data;
4298                   cmd_length = 5;
4299                   vms_debug2 ((4, "DST_S_C_SRC_SETREC_L: %d\n", data));
4300                   break;
4301
4302                 case DST__K_SRC_SETREC_W:
4303                   data = bfd_getl16 (src_ptr + DST_S_W_SRC_UNSWORD);
4304                   curr_srec->srec = data;
4305                   module->file_table[curr_srec->sfile].srec = data;
4306                   cmd_length = 3;
4307                   vms_debug2 ((4, "DST_S_C_SRC_SETREC_W: %d\n", data));
4308                   break;
4309
4310                 case DST__K_SRC_FORMFEED:
4311                   cmd_length = 1;
4312                   vms_debug2 ((4, "DST_S_C_SRC_FORMFEED\n"));
4313                   break;
4314
4315                 default:
4316                   (*_bfd_error_handler) (_("unknown source command %d"),
4317                                          cmd);
4318                   cmd_length = 2;
4319                   break;
4320                 }
4321
4322               src_ptr += cmd_length;
4323             }
4324           break;
4325
4326         case DST__K_LINE_NUM:
4327           pcl_ptr = ptr + DST_S_C_LINE_NUM_HEADER_SIZE;
4328
4329           vms_debug2 ((3, "line info\n"));
4330
4331           while (pcl_ptr < ptr + rec_length)
4332             {
4333               /* The command byte is signed so we must sign-extend it.  */
4334               int cmd = ((signed char *)pcl_ptr)[0], cmd_length, data;
4335
4336               switch (cmd)
4337                 {
4338                 case DST__K_DELTA_PC_W:
4339                   data = bfd_getl16 (pcl_ptr + DST_S_W_PCLINE_UNSWORD);
4340                   curr_pc += data;
4341                   curr_linenum += 1;
4342                   cmd_length = 3;
4343                   vms_debug2 ((4, "DST__K_DELTA_PC_W: %d\n", data));
4344                   break;
4345
4346                 case DST__K_DELTA_PC_L:
4347                   data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
4348                   curr_pc += data;
4349                   curr_linenum += 1;
4350                   cmd_length = 5;
4351                   vms_debug2 ((4, "DST__K_DELTA_PC_L: %d\n", data));
4352                   break;
4353
4354                 case DST__K_INCR_LINUM:
4355                   data = pcl_ptr[DST_S_B_PCLINE_UNSBYTE];
4356                   curr_linenum += data;
4357                   cmd_length = 2;
4358                   vms_debug2 ((4, "DST__K_INCR_LINUM: %d\n", data));
4359                   break;
4360
4361                 case DST__K_INCR_LINUM_W:
4362                   data = bfd_getl16 (pcl_ptr + DST_S_W_PCLINE_UNSWORD);
4363                   curr_linenum += data;
4364                   cmd_length = 3;
4365                   vms_debug2 ((4, "DST__K_INCR_LINUM_W: %d\n", data));
4366                   break;
4367
4368                 case DST__K_INCR_LINUM_L:
4369                   data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
4370                   curr_linenum += data;
4371                   cmd_length = 5;
4372                   vms_debug2 ((4, "DST__K_INCR_LINUM_L: %d\n", data));
4373                   break;
4374
4375                 case DST__K_SET_LINUM_INCR:
4376                   (*_bfd_error_handler)
4377                     (_("DST__K_SET_LINUM_INCR not implemented"));
4378                   cmd_length = 2;
4379                   break;
4380
4381                 case DST__K_SET_LINUM_INCR_W:
4382                   (*_bfd_error_handler)
4383                     (_("DST__K_SET_LINUM_INCR_W not implemented"));
4384                   cmd_length = 3;
4385                   break;
4386
4387                 case DST__K_RESET_LINUM_INCR:
4388                   (*_bfd_error_handler)
4389                     (_("DST__K_RESET_LINUM_INCR not implemented"));
4390                   cmd_length = 1;
4391                   break;
4392
4393                 case DST__K_BEG_STMT_MODE:
4394                   (*_bfd_error_handler)
4395                     (_("DST__K_BEG_STMT_MODE not implemented"));
4396                   cmd_length = 1;
4397                   break;
4398
4399                 case DST__K_END_STMT_MODE:
4400                   (*_bfd_error_handler)
4401                     (_("DST__K_END_STMT_MODE not implemented"));
4402                   cmd_length = 1;
4403                   break;
4404
4405                 case DST__K_SET_LINUM_B:
4406                   data = pcl_ptr[DST_S_B_PCLINE_UNSBYTE];
4407                   curr_linenum = data;
4408                   cmd_length = 2;
4409                   vms_debug2 ((4, "DST__K_SET_LINUM_B: %d\n", data));
4410                   break;
4411
4412                 case DST__K_SET_LINUM:
4413                   data = bfd_getl16 (pcl_ptr + DST_S_W_PCLINE_UNSWORD);
4414                   curr_linenum = data;
4415                   cmd_length = 3;
4416                   vms_debug2 ((4, "DST__K_SET_LINE_NUM: %d\n", data));
4417                   break;
4418
4419                 case DST__K_SET_LINUM_L:
4420                   data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
4421                   curr_linenum = data;
4422                   cmd_length = 5;
4423                   vms_debug2 ((4, "DST__K_SET_LINUM_L: %d\n", data));
4424                   break;
4425
4426                 case DST__K_SET_PC:
4427                   (*_bfd_error_handler)
4428                     (_("DST__K_SET_PC not implemented"));
4429                   cmd_length = 2;
4430                   break;
4431
4432                 case DST__K_SET_PC_W:
4433                   (*_bfd_error_handler)
4434                     (_("DST__K_SET_PC_W not implemented"));
4435                   cmd_length = 3;
4436                   break;
4437
4438                 case DST__K_SET_PC_L:
4439                   (*_bfd_error_handler)
4440                     (_("DST__K_SET_PC_L not implemented"));
4441                   cmd_length = 5;
4442                   break;
4443
4444                 case DST__K_SET_STMTNUM:
4445                   (*_bfd_error_handler)
4446                     (_("DST__K_SET_STMTNUM not implemented"));
4447                   cmd_length = 2;
4448                   break;
4449
4450                 case DST__K_TERM:
4451                   data = pcl_ptr[DST_S_B_PCLINE_UNSBYTE];
4452                   curr_pc += data;
4453                   cmd_length = 2;
4454                   vms_debug2 ((4, "DST__K_TERM: %d\n", data));
4455                   break;
4456
4457                 case DST__K_TERM_W:
4458                   data = bfd_getl16 (pcl_ptr + DST_S_W_PCLINE_UNSWORD);
4459                   curr_pc += data;
4460                   cmd_length = 3;
4461                   vms_debug2 ((4, "DST__K_TERM_W: %d\n", data));
4462                   break;
4463
4464                 case DST__K_TERM_L:
4465                   data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
4466                   curr_pc += data;
4467                   cmd_length = 5;
4468                   vms_debug2 ((4, "DST__K_TERM_L: %d\n", data));
4469                   break;
4470
4471                 case DST__K_SET_ABS_PC:
4472                   data = bfd_getl32 (pcl_ptr + DST_S_L_PCLINE_UNSLONG);
4473                   curr_pc = data;
4474                   cmd_length = 5;
4475                   vms_debug2 ((4, "DST__K_SET_ABS_PC: 0x%x\n", data));
4476                   break;
4477
4478                 default:
4479                   if (cmd <= 0)
4480                     {
4481                       curr_pc -= cmd;
4482                       curr_linenum += 1;
4483                       cmd_length = 1;
4484                       vms_debug2 ((4, "bump pc to 0x%lx and line to %d\n",
4485                                    (unsigned long)curr_pc, curr_linenum));
4486                     }
4487                   else
4488                     {
4489                       (*_bfd_error_handler) (_("unknown line command %d"),
4490                                              cmd);
4491                       cmd_length = 2;
4492                     }
4493                   break;
4494                 }
4495
4496               if ((curr_linenum != prev_linum && curr_pc != prev_pc)
4497                   || cmd <= 0
4498                   || cmd == DST__K_DELTA_PC_L
4499                   || cmd == DST__K_DELTA_PC_W)
4500                 {
4501                   line = (struct lineinfo *)
4502                     bfd_zalloc (abfd, sizeof (struct lineinfo));
4503                   line->address = curr_pc;
4504                   line->line = curr_linenum;
4505
4506                   curr_line->next = line;
4507                   curr_line = line;
4508
4509                   prev_linum = curr_linenum;
4510                   prev_pc = curr_pc;
4511                   vms_debug2 ((4, "-> correlate pc 0x%lx with line %d\n",
4512                                (unsigned long)curr_pc, curr_linenum));
4513                 }
4514
4515               pcl_ptr += cmd_length;
4516             }
4517           break;
4518
4519         case 0x17: /* Undocumented type used by DEC C to declare equates.  */
4520           vms_debug2 ((3, "undocumented type 0x17\n"));
4521           break;
4522
4523         default:
4524           vms_debug2 ((3, "ignoring record\n"));
4525           break;
4526
4527         }
4528
4529       ptr += rec_length;
4530     }
4531
4532   /* Finalize tables with EOL marker.  */
4533   srec = (struct srecinfo *) bfd_zalloc (abfd, sizeof (struct srecinfo));
4534   srec->line = (unsigned int) -1;
4535   srec->srec = (unsigned int) -1;
4536   curr_srec->next = srec;
4537
4538   line = (struct lineinfo *) bfd_zalloc (abfd, sizeof (struct lineinfo));
4539   line->line = (unsigned int) -1;
4540   line->address = (bfd_vma) -1;
4541   curr_line->next = line;
4542
4543   /* Advertise that this module has been parsed.  This is needed
4544      because parsing can be either performed at module creation
4545      or deferred until debug info is consumed.  */
4546   SET_MODULE_PARSED (module);
4547 }
4548
4549 /* Build the list of modules for the specified BFD.  */
4550
4551 static struct module *
4552 build_module_list (bfd *abfd)
4553 {
4554   struct module *module, *list = NULL;
4555   asection *dmt;
4556
4557   if ((dmt = bfd_get_section_by_name (abfd, "$DMT$")))
4558     {
4559       /* We have a DMT section so this must be an image.  Parse the
4560          section and build the list of modules.  This is sufficient
4561          since we can compute the start address and the end address
4562          of every module from the section contents.  */
4563       bfd_size_type size = bfd_get_section_size (dmt);
4564       unsigned char *ptr, *end;
4565
4566       ptr = (unsigned char *) bfd_alloc (abfd, size);
4567       if (! ptr)
4568         return NULL;
4569
4570       if (! bfd_get_section_contents (abfd, dmt, ptr, 0, size))
4571         return NULL;
4572
4573       vms_debug2 ((2, "DMT\n"));
4574
4575       end = ptr + size;
4576
4577       while (ptr < end)
4578         {
4579           /* Each header declares a module with its start offset and size
4580              of debug info in the DST section, as well as the count of
4581              program sections (i.e. address spans) it contains.  */
4582           int modbeg = bfd_getl32 (ptr + DBG_S_L_DMT_MODBEG);
4583           int msize = bfd_getl32 (ptr + DBG_S_L_DST_SIZE);
4584           int count = bfd_getl16 (ptr + DBG_S_W_DMT_PSECT_COUNT);
4585           ptr += DBG_S_C_DMT_HEADER_SIZE;
4586
4587           vms_debug2 ((3, "module: modbeg = %d, size = %d, count = %d\n",
4588                        modbeg, msize, count));
4589
4590           /* We create a 'module' structure for each program section since
4591              we only support contiguous addresses in a 'module' structure.
4592              As a consequence, the actual debug info in the DST section is
4593              shared and can be parsed multiple times; that doesn't seem to
4594              cause problems in practice.  */
4595           while (count-- > 0)
4596             {
4597               int start = bfd_getl32 (ptr + DBG_S_L_DMT_PSECT_START);
4598               int length = bfd_getl32 (ptr + DBG_S_L_DMT_PSECT_LENGTH);
4599               module = new_module (abfd);
4600               module->modbeg = modbeg;
4601               module->size = msize;
4602               module->low = start;
4603               module->high = start + length;
4604               module->next = list;
4605               list = module;
4606               ptr += DBG_S_C_DMT_PSECT_SIZE;
4607
4608               vms_debug2 ((4, "section: start = 0x%x, length = %d\n",
4609                            start, length));
4610             }
4611         }
4612     }
4613   else
4614     {
4615       /* We don't have a DMT section so this must be an object.  Parse
4616          the module right now in order to compute its start address and
4617          end address.  */
4618       module = new_module (abfd);
4619       parse_module (abfd, module, PRIV (dst_section)->contents, -1);
4620       list = module;
4621     }
4622
4623   return list;
4624 }
4625
4626 /* Calculate and return the name of the source file and the line nearest
4627    to the wanted location in the specified module.  */
4628
4629 static bfd_boolean
4630 module_find_nearest_line (bfd *abfd, struct module *module, bfd_vma addr,
4631                           const char **file, const char **func,
4632                           unsigned int *line)
4633 {
4634   struct funcinfo *funcinfo;
4635   struct lineinfo *lineinfo;
4636   struct srecinfo *srecinfo;
4637   bfd_boolean ret = FALSE;
4638
4639   /* Parse this module if that was not done at module creation.  */
4640   if (! IS_MODULE_PARSED (module))
4641     {
4642       unsigned int size = module->size;
4643       unsigned int modbeg = PRIV (dst_section)->filepos + module->modbeg;
4644       unsigned char *buffer = (unsigned char *) bfd_malloc (module->size);
4645
4646       if (bfd_seek (abfd, modbeg, SEEK_SET) != 0
4647           || bfd_bread (buffer, size, abfd) != size)
4648         {
4649           bfd_set_error (bfd_error_no_debug_section);
4650           return FALSE;
4651         }
4652
4653       parse_module (abfd, module, buffer, size);
4654       free (buffer);
4655     }
4656
4657   /* Find out the function (if any) that contains the address.  */
4658   for (funcinfo = module->func_table; funcinfo; funcinfo = funcinfo->next)
4659     if (addr >= funcinfo->low && addr <= funcinfo->high)
4660       {
4661         *func = funcinfo->name;
4662         ret = TRUE;
4663         break;
4664       }
4665
4666   /* Find out the source file and the line nearest to the address.  */
4667   for (lineinfo = module->line_table; lineinfo; lineinfo = lineinfo->next)
4668     if (lineinfo->next && addr < lineinfo->next->address)
4669       {
4670         for (srecinfo = module->srec_table; srecinfo; srecinfo = srecinfo->next)
4671           if (srecinfo->next && lineinfo->line < srecinfo->next->line)
4672             {
4673               if (srecinfo->sfile > 0)
4674                 {
4675                   *file = module->file_table[srecinfo->sfile].name;
4676                   *line = srecinfo->srec + lineinfo->line - srecinfo->line;
4677                 }
4678               else
4679                 {
4680                   *file = module->name;
4681                   *line = lineinfo->line;
4682                 }
4683               return TRUE;
4684             }
4685
4686         break;
4687       }
4688
4689   return ret;
4690 }
4691
4692 /* Provided a BFD, a section and an offset into the section, calculate and
4693    return the name of the source file and the line nearest to the wanted
4694    location.  */
4695
4696 static bfd_boolean
4697 _bfd_vms_find_nearest_dst_line (bfd *abfd, asection *section,
4698                                 asymbol **symbols ATTRIBUTE_UNUSED,
4699                                 bfd_vma offset, const char **file,
4700                                 const char **func, unsigned int *line)
4701 {
4702   struct module *module;
4703
4704   /* What address are we looking for?  */
4705   bfd_vma addr = section->vma + offset;
4706
4707   *file = NULL;
4708   *func = NULL;
4709   *line = 0;
4710
4711   if (PRIV (dst_section) == NULL || !(abfd->flags & (EXEC_P | DYNAMIC)))
4712     return FALSE;
4713
4714   if (PRIV (modules) == NULL)
4715     {
4716       PRIV (modules) = build_module_list (abfd);
4717       if (PRIV (modules) == NULL)
4718         return FALSE;
4719     }
4720
4721   for (module = PRIV (modules); module; module = module->next)
4722     if (addr >= module->low && addr <= module->high)
4723       return module_find_nearest_line (abfd, module, addr, file, func, line);
4724
4725   return FALSE;
4726 }
4727 \f
4728 /* Canonicalizations.  */
4729 /* Set name, value, section and flags of SYM from E.  */
4730
4731 static bfd_boolean
4732 alpha_vms_convert_symbol (bfd *abfd, struct vms_symbol_entry *e, asymbol *sym)
4733 {
4734   flagword flags;
4735   symvalue value;
4736   asection *sec;
4737   const char *name;
4738
4739   name = e->name;
4740   value = 0;
4741   flags = BSF_NO_FLAGS;
4742   sec = NULL;
4743
4744   switch (e->typ)
4745     {
4746     case EGSD__C_SYM:
4747       if (e->flags & EGSY__V_WEAK)
4748         flags |= BSF_WEAK;
4749
4750       if (e->flags & EGSY__V_DEF)
4751         {
4752           /* Symbol definition.  */
4753           flags |= BSF_GLOBAL;
4754           if (e->flags & EGSY__V_NORM)
4755             flags |= BSF_FUNCTION;
4756           value = e->value;
4757           sec = e->section;
4758         }
4759       else
4760         {
4761           /* Symbol reference.  */
4762           sec = bfd_und_section_ptr;
4763         }
4764       break;
4765
4766     case EGSD__C_SYMG:
4767       /* A universal symbol is by definition global...  */
4768       flags |= BSF_GLOBAL;
4769
4770       /* ...and dynamic in shared libraries.  */
4771       if (abfd->flags & DYNAMIC)
4772         flags |= BSF_DYNAMIC;
4773
4774       if (e->flags & EGSY__V_WEAK)
4775         flags |= BSF_WEAK;
4776
4777       if (!(e->flags & EGSY__V_DEF))
4778         abort ();
4779
4780       if (e->flags & EGSY__V_NORM)
4781         flags |= BSF_FUNCTION;
4782
4783       value = e->value;
4784       /* sec = e->section; */
4785       sec = bfd_abs_section_ptr;
4786       break;
4787
4788     default:
4789       return FALSE;
4790     }
4791
4792   sym->name = name;
4793   sym->section = sec;
4794   sym->flags = flags;
4795   sym->value = value;
4796   return TRUE;
4797 }
4798
4799
4800 /* Return the number of bytes required to store a vector of pointers
4801    to asymbols for all the symbols in the BFD abfd, including a
4802    terminal NULL pointer. If there are no symbols in the BFD,
4803    then return 0.  If an error occurs, return -1.  */
4804
4805 static long
4806 alpha_vms_get_symtab_upper_bound (bfd *abfd)
4807 {
4808   vms_debug2 ((1, "alpha_vms_get_symtab_upper_bound (%p), %d symbols\n",
4809                abfd, PRIV (gsd_sym_count)));
4810
4811   return (PRIV (gsd_sym_count) + 1) * sizeof (asymbol *);
4812 }
4813
4814 /* Read the symbols from the BFD abfd, and fills in the vector
4815    location with pointers to the symbols and a trailing NULL.
4816
4817    Return number of symbols read.   */
4818
4819 static long
4820 alpha_vms_canonicalize_symtab (bfd *abfd, asymbol **symbols)
4821 {
4822   unsigned int i;
4823
4824   vms_debug2 ((1, "alpha_vms_canonicalize_symtab (%p, <ret>)\n", abfd));
4825
4826   if (PRIV (csymbols) == NULL)
4827     {
4828       PRIV (csymbols) = (asymbol **) bfd_alloc
4829         (abfd, PRIV (gsd_sym_count) * sizeof (asymbol *));
4830
4831       /* Traverse table and fill symbols vector.  */
4832       for (i = 0; i < PRIV (gsd_sym_count); i++)
4833         {
4834           struct vms_symbol_entry *e = PRIV (syms)[i];
4835           asymbol *sym;
4836
4837           sym = bfd_make_empty_symbol (abfd);
4838           if (sym == NULL || !alpha_vms_convert_symbol (abfd, e, sym))
4839             {
4840               bfd_release (abfd, PRIV (csymbols));
4841               PRIV (csymbols) = NULL;
4842               return -1;
4843             }
4844
4845           PRIV (csymbols)[i] = sym;
4846         }
4847     }
4848
4849   if (symbols != NULL)
4850     {
4851       for (i = 0; i < PRIV (gsd_sym_count); i++)
4852         symbols[i] = PRIV (csymbols)[i];
4853       symbols[i] = NULL;
4854     }
4855
4856   return PRIV (gsd_sym_count);
4857 }
4858
4859 /* Read and convert relocations from ETIR.  We do it once for all sections.  */
4860
4861 static bfd_boolean
4862 alpha_vms_slurp_relocs (bfd *abfd)
4863 {
4864   int cur_psect = -1;
4865
4866   vms_debug2 ((3, "alpha_vms_slurp_relocs\n"));
4867
4868   /* We slurp relocs only once, for all sections.  */
4869   if (PRIV (reloc_done))
4870       return TRUE;
4871   PRIV (reloc_done) = TRUE;
4872
4873   if (alpha_vms_canonicalize_symtab (abfd, NULL) < 0)
4874     return FALSE;
4875
4876   if (bfd_seek (abfd, 0, SEEK_SET) != 0)
4877     return FALSE;
4878
4879   while (1)
4880     {
4881       unsigned char *begin;
4882       unsigned char *end;
4883       unsigned char *ptr;
4884       bfd_reloc_code_real_type reloc_code;
4885       int type;
4886       bfd_vma vaddr = 0;
4887
4888       int length;
4889
4890       bfd_vma cur_address;
4891       int cur_psidx = -1;
4892       unsigned char *cur_sym = NULL;
4893       int prev_cmd = -1;
4894       bfd_vma cur_addend = 0;
4895
4896       /* Skip non-ETIR records.  */
4897       type = _bfd_vms_get_object_record (abfd);
4898       if (type == EOBJ__C_EEOM)
4899         break;
4900       if (type != EOBJ__C_ETIR)
4901         continue;
4902
4903       begin = PRIV (recrd.rec) + 4;
4904       end = PRIV (recrd.rec) + PRIV (recrd.rec_size);
4905
4906       for (ptr = begin; ptr < end; ptr += length)
4907         {
4908           int cmd;
4909
4910           cmd = bfd_getl16 (ptr);
4911           length = bfd_getl16 (ptr + 2);
4912
4913           cur_address = vaddr;
4914
4915           vms_debug2 ((4, "alpha_vms_slurp_relocs: etir %s\n",
4916                        _bfd_vms_etir_name (cmd)));
4917
4918           switch (cmd)
4919             {
4920             case ETIR__C_STA_GBL: /* ALPHA_R_REFLONG und_section, step 1 */
4921                                   /* ALPHA_R_REFQUAD und_section, step 1 */
4922               cur_sym = ptr + 4;
4923               prev_cmd = cmd;
4924               continue;
4925
4926             case ETIR__C_STA_PQ: /* ALPHA_R_REF{LONG|QUAD}, others part 1 */
4927               cur_psidx = bfd_getl32 (ptr + 4);
4928               cur_addend = bfd_getl64 (ptr + 8);
4929               prev_cmd = cmd;
4930               continue;
4931
4932             case ETIR__C_CTL_SETRB:
4933               if (prev_cmd != ETIR__C_STA_PQ)
4934                 {
4935                   (*_bfd_error_handler)
4936                     (_("Unknown reloc %s + %s"), _bfd_vms_etir_name (prev_cmd),
4937                      _bfd_vms_etir_name (cmd));
4938                   return FALSE;
4939                 }
4940               cur_psect = cur_psidx;
4941               vaddr = cur_addend;
4942               cur_psidx = -1;
4943               cur_addend = 0;
4944               continue;
4945
4946             case ETIR__C_STA_LW: /* ALPHA_R_REFLONG abs_section, step 1 */
4947                                  /* ALPHA_R_REFLONG und_section, step 2 */
4948               if (prev_cmd != -1)
4949                 {
4950                   if (prev_cmd != ETIR__C_STA_GBL)
4951                     {
4952                       (*_bfd_error_handler)
4953                         (_("Unknown reloc %s + %s"), _bfd_vms_etir_name (cmd),
4954                          _bfd_vms_etir_name (ETIR__C_STA_LW));
4955                       return FALSE;
4956                     }
4957                 }
4958               cur_addend = bfd_getl32 (ptr + 4);
4959               prev_cmd = cmd;
4960               continue;
4961
4962             case ETIR__C_STA_QW: /* ALPHA_R_REFQUAD abs_section, step 1 */
4963                                  /* ALPHA_R_REFQUAD und_section, step 2 */
4964               if (prev_cmd != -1 && prev_cmd != ETIR__C_STA_GBL)
4965                 {
4966                   (*_bfd_error_handler)
4967                     (_("Unknown reloc %s + %s"), _bfd_vms_etir_name (cmd),
4968                      _bfd_vms_etir_name (ETIR__C_STA_QW));
4969                   return FALSE;
4970                 }
4971               cur_addend = bfd_getl64 (ptr + 4);
4972               prev_cmd = cmd;
4973               continue;
4974
4975             case ETIR__C_STO_LW: /* ALPHA_R_REFLONG und_section, step 4 */
4976                                  /* ALPHA_R_REFLONG abs_section, step 2 */
4977                                  /* ALPHA_R_REFLONG others, step 2 */
4978               if (prev_cmd != ETIR__C_OPR_ADD
4979                   && prev_cmd != ETIR__C_STA_LW
4980                   && prev_cmd != ETIR__C_STA_PQ)
4981                 {
4982                   (*_bfd_error_handler) (_("Unknown reloc %s + %s"),
4983                                          _bfd_vms_etir_name (prev_cmd),
4984                                          _bfd_vms_etir_name (ETIR__C_STO_LW));
4985                   return FALSE;
4986                 }
4987               reloc_code = BFD_RELOC_32;
4988               break;
4989
4990             case ETIR__C_STO_QW: /* ALPHA_R_REFQUAD und_section, step 4 */
4991                                  /* ALPHA_R_REFQUAD abs_section, step 2 */
4992               if (prev_cmd != ETIR__C_OPR_ADD && prev_cmd != ETIR__C_STA_QW)
4993                 {
4994                   (*_bfd_error_handler) (_("Unknown reloc %s + %s"),
4995                                          _bfd_vms_etir_name (prev_cmd),
4996                                          _bfd_vms_etir_name (ETIR__C_STO_QW));
4997                   return FALSE;
4998                 }
4999               reloc_code = BFD_RELOC_64;
5000               break;
5001
5002             case ETIR__C_STO_OFF: /* ALPHA_R_REFQUAD others, step 2 */
5003               if (prev_cmd != ETIR__C_STA_PQ)
5004                 {
5005                   (*_bfd_error_handler) (_("Unknown reloc %s + %s"),
5006                                          _bfd_vms_etir_name (prev_cmd),
5007                                          _bfd_vms_etir_name (ETIR__C_STO_OFF));
5008                   return FALSE;
5009                 }
5010               reloc_code = BFD_RELOC_64;
5011               break;
5012
5013             case ETIR__C_OPR_ADD: /* ALPHA_R_REFLONG und_section, step 3 */
5014                                   /* ALPHA_R_REFQUAD und_section, step 3 */
5015               if (prev_cmd != ETIR__C_STA_LW && prev_cmd != ETIR__C_STA_QW)
5016                 {
5017                   (*_bfd_error_handler) (_("Unknown reloc %s + %s"),
5018                                          _bfd_vms_etir_name (prev_cmd),
5019                                          _bfd_vms_etir_name (ETIR__C_OPR_ADD));
5020                   return FALSE;
5021                 }
5022               prev_cmd = ETIR__C_OPR_ADD;
5023               continue;
5024
5025             case ETIR__C_STO_CA: /* ALPHA_R_CODEADDR */
5026               reloc_code = BFD_RELOC_ALPHA_CODEADDR;
5027               cur_sym = ptr + 4;
5028               break;
5029
5030             case ETIR__C_STO_GBL: /* ALPHA_R_REFQUAD und_section */
5031               reloc_code = BFD_RELOC_64;
5032               cur_sym = ptr + 4;
5033               break;
5034
5035             case ETIR__C_STO_GBL_LW: /* ALPHA_R_REFLONG und_section */
5036               reloc_code = BFD_RELOC_32;
5037               cur_sym = ptr + 4;
5038               break;
5039
5040             case ETIR__C_STC_LP_PSB: /* ALPHA_R_LINKAGE */
5041               reloc_code = BFD_RELOC_ALPHA_LINKAGE;
5042               cur_sym = ptr + 8;
5043               break;
5044
5045             case ETIR__C_STC_NOP_GBL: /* ALPHA_R_NOP */
5046               reloc_code = BFD_RELOC_ALPHA_NOP;
5047               goto call_reloc;
5048
5049             case ETIR__C_STC_BSR_GBL: /* ALPHA_R_BSR */
5050               reloc_code = BFD_RELOC_ALPHA_BSR;
5051               goto call_reloc;
5052
5053             case ETIR__C_STC_LDA_GBL: /* ALPHA_R_LDA */
5054               reloc_code = BFD_RELOC_ALPHA_LDA;
5055               goto call_reloc;
5056
5057             case ETIR__C_STC_BOH_GBL: /* ALPHA_R_BOH */
5058               reloc_code = BFD_RELOC_ALPHA_BOH;
5059               goto call_reloc;
5060
5061             call_reloc:
5062               cur_sym = ptr + 4 + 32;
5063               cur_address = bfd_getl64 (ptr + 4 + 8);
5064               cur_addend = bfd_getl64 (ptr + 4 + 24);
5065               break;
5066
5067             case ETIR__C_STO_IMM:
5068               vaddr += bfd_getl32 (ptr + 4);
5069               continue;
5070
5071             default:
5072               (*_bfd_error_handler) (_("Unknown reloc %s"),
5073                                      _bfd_vms_etir_name (cmd));
5074               return FALSE;
5075             }
5076
5077           {
5078             asection *sec;
5079             struct vms_section_data_struct *vms_sec;
5080             arelent *reloc;
5081
5082             /* Get section to which the relocation applies.  */
5083             if (cur_psect < 0 || cur_psect > (int)PRIV (section_count))
5084               {
5085                 (*_bfd_error_handler) (_("Invalid section index in ETIR"));
5086                 return FALSE;
5087               }
5088             sec = PRIV (sections)[cur_psect];
5089             vms_sec = vms_section_data (sec);
5090
5091             /* Allocate a reloc entry.  */
5092             if (sec->reloc_count >= vms_sec->reloc_max)
5093               {
5094                 if (vms_sec->reloc_max == 0)
5095                   {
5096                     vms_sec->reloc_max = 64;
5097                     sec->relocation = bfd_zmalloc
5098                       (vms_sec->reloc_max * sizeof (arelent));
5099                 }
5100                 else
5101                   {
5102                     vms_sec->reloc_max *= 2;
5103                     sec->relocation = bfd_realloc
5104                       (sec->relocation, vms_sec->reloc_max * sizeof (arelent));
5105                   }
5106               }
5107             reloc = &sec->relocation[sec->reloc_count];
5108             sec->reloc_count++;
5109
5110             reloc->howto = bfd_reloc_type_lookup (abfd, reloc_code);
5111
5112             if (cur_sym != NULL)
5113               {
5114                 unsigned int j;
5115                 unsigned int symlen = *cur_sym;
5116                 asymbol **sym;
5117
5118                 /* Linear search.  */
5119                 symlen = *cur_sym;
5120                 cur_sym++;
5121                 sym = NULL;
5122
5123                 for (j = 0; j < PRIV (gsd_sym_count); j++)
5124                   if (PRIV (syms)[j]->namelen == symlen
5125                       && memcmp (PRIV (syms)[j]->name, cur_sym, symlen) == 0)
5126                     {
5127                       sym = &PRIV (csymbols)[j];
5128                       break;
5129                     }
5130                 if (sym == NULL)
5131                   {
5132                     (*_bfd_error_handler) (_("Unknown symbol in command %s"),
5133                                            _bfd_vms_etir_name (cmd));
5134                     reloc->sym_ptr_ptr = NULL;
5135                   }
5136                 else
5137                   reloc->sym_ptr_ptr = sym;
5138               }
5139             else if (cur_psidx >= 0)
5140               reloc->sym_ptr_ptr =
5141                 PRIV (sections)[cur_psidx]->symbol_ptr_ptr;
5142             else
5143               reloc->sym_ptr_ptr = NULL;
5144
5145             reloc->address = cur_address;
5146             reloc->addend = cur_addend;
5147
5148             vaddr += bfd_get_reloc_size (reloc->howto);
5149           }
5150
5151           cur_addend = 0;
5152           prev_cmd = -1;
5153           cur_sym = NULL;
5154           cur_psidx = -1;
5155         }
5156     }
5157   vms_debug2 ((3, "alpha_vms_slurp_relocs: result = TRUE\n"));
5158
5159   return TRUE;
5160 }
5161
5162 /* Return the number of bytes required to store the relocation
5163    information associated with the given section.  */
5164
5165 static long
5166 alpha_vms_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, asection *section)
5167 {
5168   alpha_vms_slurp_relocs (abfd);
5169
5170   return (section->reloc_count + 1) * sizeof (arelent *);
5171 }
5172
5173 /* Convert relocations from VMS (external) form into BFD internal
5174    form.  Return the number of relocations.  */
5175
5176 static long
5177 alpha_vms_canonicalize_reloc (bfd *abfd, asection *section, arelent **relptr,
5178                               asymbol **symbols ATTRIBUTE_UNUSED)
5179 {
5180   arelent *tblptr;
5181   int count;
5182
5183   if (!alpha_vms_slurp_relocs (abfd))
5184     return -1;
5185
5186   count = section->reloc_count;
5187   tblptr = section->relocation;
5188
5189   while (count--)
5190     *relptr++ = tblptr++;
5191
5192   *relptr = (arelent *) NULL;
5193   return section->reloc_count;
5194 }
5195 \f
5196 /* This is just copied from ecoff-alpha, needs to be fixed probably.  */
5197
5198 /* How to process the various reloc types.  */
5199
5200 static bfd_reloc_status_type
5201 reloc_nil (bfd * abfd ATTRIBUTE_UNUSED,
5202            arelent *reloc ATTRIBUTE_UNUSED,
5203            asymbol *sym ATTRIBUTE_UNUSED,
5204            void * data ATTRIBUTE_UNUSED,
5205            asection *sec ATTRIBUTE_UNUSED,
5206            bfd *output_bfd ATTRIBUTE_UNUSED,
5207            char **error_message ATTRIBUTE_UNUSED)
5208 {
5209 #if VMS_DEBUG
5210   vms_debug (1, "reloc_nil (abfd %p, output_bfd %p)\n", abfd, output_bfd);
5211   vms_debug (2, "In section %s, symbol %s\n",
5212         sec->name, sym->name);
5213   vms_debug (2, "reloc sym %s, addr %08lx, addend %08lx, reloc is a %s\n",
5214                 reloc->sym_ptr_ptr[0]->name,
5215                 (unsigned long)reloc->address,
5216                 (unsigned long)reloc->addend, reloc->howto->name);
5217   vms_debug (2, "data at %p\n", data);
5218   /*  _bfd_hexdump (2, data, bfd_get_reloc_size (reloc->howto), 0); */
5219 #endif
5220
5221   return bfd_reloc_ok;
5222 }
5223
5224 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
5225    from smaller values.  Start with zero, widen, *then* decrement.  */
5226 #define MINUS_ONE       (((bfd_vma)0) - 1)
5227
5228 static reloc_howto_type alpha_howto_table[] =
5229 {
5230   HOWTO (ALPHA_R_IGNORE,        /* Type.  */
5231          0,                     /* Rightshift.  */
5232          0,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5233          8,                     /* Bitsize.  */
5234          TRUE,                  /* PC relative.  */
5235          0,                     /* Bitpos.  */
5236          complain_overflow_dont,/* Complain_on_overflow.  */
5237          reloc_nil,             /* Special_function.  */
5238          "IGNORE",              /* Name.  */
5239          TRUE,                  /* Partial_inplace.  */
5240          0,                     /* Source mask */
5241          0,                     /* Dest mask.  */
5242          TRUE),                 /* PC rel offset.  */
5243
5244   /* A 64 bit reference to a symbol.  */
5245   HOWTO (ALPHA_R_REFQUAD,       /* Type.  */
5246          0,                     /* Rightshift.  */
5247          4,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5248          64,                    /* Bitsize.  */
5249          FALSE,                 /* PC relative.  */
5250          0,                     /* Bitpos.  */
5251          complain_overflow_bitfield, /* Complain_on_overflow.  */
5252          reloc_nil,             /* Special_function.  */
5253          "REFQUAD",             /* Name.  */
5254          TRUE,                  /* Partial_inplace.  */
5255          MINUS_ONE,             /* Source mask.  */
5256          MINUS_ONE,             /* Dest mask.  */
5257          FALSE),                /* PC rel offset.  */
5258
5259   /* A 21 bit branch.  The native assembler generates these for
5260      branches within the text segment, and also fills in the PC
5261      relative offset in the instruction.  */
5262   HOWTO (ALPHA_R_BRADDR,        /* Type.  */
5263          2,                     /* Rightshift.  */
5264          2,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5265          21,                    /* Bitsize.  */
5266          TRUE,                  /* PC relative.  */
5267          0,                     /* Bitpos.  */
5268          complain_overflow_signed, /* Complain_on_overflow.  */
5269          reloc_nil,             /* Special_function.  */
5270          "BRADDR",              /* Name.  */
5271          TRUE,                  /* Partial_inplace.  */
5272          0x1fffff,              /* Source mask.  */
5273          0x1fffff,              /* Dest mask.  */
5274          FALSE),                /* PC rel offset.  */
5275
5276   /* A hint for a jump to a register.  */
5277   HOWTO (ALPHA_R_HINT,          /* Type.  */
5278          2,                     /* Rightshift.  */
5279          1,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5280          14,                    /* Bitsize.  */
5281          TRUE,                  /* PC relative.  */
5282          0,                     /* Bitpos.  */
5283          complain_overflow_dont,/* Complain_on_overflow.  */
5284          reloc_nil,             /* Special_function.  */
5285          "HINT",                /* Name.  */
5286          TRUE,                  /* Partial_inplace.  */
5287          0x3fff,                /* Source mask.  */
5288          0x3fff,                /* Dest mask.  */
5289          FALSE),                /* PC rel offset.  */
5290
5291   /* 16 bit PC relative offset.  */
5292   HOWTO (ALPHA_R_SREL16,        /* Type.  */
5293          0,                     /* Rightshift.  */
5294          1,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5295          16,                    /* Bitsize.  */
5296          TRUE,                  /* PC relative.  */
5297          0,                     /* Bitpos.  */
5298          complain_overflow_signed, /* Complain_on_overflow.  */
5299          reloc_nil,             /* Special_function.  */
5300          "SREL16",              /* Name.  */
5301          TRUE,                  /* Partial_inplace.  */
5302          0xffff,                /* Source mask.  */
5303          0xffff,                /* Dest mask.  */
5304          FALSE),                /* PC rel offset.  */
5305
5306   /* 32 bit PC relative offset.  */
5307   HOWTO (ALPHA_R_SREL32,        /* Type.  */
5308          0,                     /* Rightshift.  */
5309          2,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5310          32,                    /* Bitsize.  */
5311          TRUE,                  /* PC relative.  */
5312          0,                     /* Bitpos.  */
5313          complain_overflow_signed, /* Complain_on_overflow.  */
5314          reloc_nil,             /* Special_function.  */
5315          "SREL32",              /* Name.  */
5316          TRUE,                  /* Partial_inplace.  */
5317          0xffffffff,            /* Source mask.  */
5318          0xffffffff,            /* Dest mask.  */
5319          FALSE),                /* PC rel offset.  */
5320
5321   /* A 64 bit PC relative offset.  */
5322   HOWTO (ALPHA_R_SREL64,        /* Type.  */
5323          0,                     /* Rightshift.  */
5324          4,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5325          64,                    /* Bitsize.  */
5326          TRUE,                  /* PC relative.  */
5327          0,                     /* Bitpos.  */
5328          complain_overflow_signed, /* Complain_on_overflow.  */
5329          reloc_nil,             /* Special_function.  */
5330          "SREL64",              /* Name.  */
5331          TRUE,                  /* Partial_inplace.  */
5332          MINUS_ONE,             /* Source mask.  */
5333          MINUS_ONE,             /* Dest mask.  */
5334          FALSE),                /* PC rel offset.  */
5335
5336   /* Push a value on the reloc evaluation stack.  */
5337   HOWTO (ALPHA_R_OP_PUSH,       /* Type.  */
5338          0,                     /* Rightshift.  */
5339          0,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5340          0,                     /* Bitsize.  */
5341          FALSE,                 /* PC relative.  */
5342          0,                     /* Bitpos.  */
5343          complain_overflow_dont,/* Complain_on_overflow.  */
5344          reloc_nil,             /* Special_function.  */
5345          "OP_PUSH",             /* Name.  */
5346          FALSE,                 /* Partial_inplace.  */
5347          0,                     /* Source mask.  */
5348          0,                     /* Dest mask.  */
5349          FALSE),                /* PC rel offset.  */
5350
5351   /* Store the value from the stack at the given address.  Store it in
5352      a bitfield of size r_size starting at bit position r_offset.  */
5353   HOWTO (ALPHA_R_OP_STORE,      /* Type.  */
5354          0,                     /* Rightshift.  */
5355          4,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5356          64,                    /* Bitsize.  */
5357          FALSE,                 /* PC relative.  */
5358          0,                     /* Bitpos.  */
5359          complain_overflow_dont,/* Complain_on_overflow.  */
5360          reloc_nil,             /* Special_function.  */
5361          "OP_STORE",            /* Name.  */
5362          FALSE,                 /* Partial_inplace.  */
5363          0,                     /* Source mask.  */
5364          MINUS_ONE,             /* Dest mask.  */
5365          FALSE),                /* PC rel offset.  */
5366
5367   /* Subtract the reloc address from the value on the top of the
5368      relocation stack.  */
5369   HOWTO (ALPHA_R_OP_PSUB,       /* Type.  */
5370          0,                     /* Rightshift.  */
5371          0,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5372          0,                     /* Bitsize.  */
5373          FALSE,                 /* PC relative.  */
5374          0,                     /* Bitpos.  */
5375          complain_overflow_dont,/* Complain_on_overflow.  */
5376          reloc_nil,             /* Special_function.  */
5377          "OP_PSUB",             /* Name.  */
5378          FALSE,                 /* Partial_inplace.  */
5379          0,                     /* Source mask.  */
5380          0,                     /* Dest mask.  */
5381          FALSE),                /* PC rel offset.  */
5382
5383   /* Shift the value on the top of the relocation stack right by the
5384      given value.  */
5385   HOWTO (ALPHA_R_OP_PRSHIFT,    /* Type.  */
5386          0,                     /* Rightshift.  */
5387          0,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5388          0,                     /* Bitsize.  */
5389          FALSE,                 /* PC relative.  */
5390          0,                     /* Bitpos.  */
5391          complain_overflow_dont,/* Complain_on_overflow.  */
5392          reloc_nil,             /* Special_function.  */
5393          "OP_PRSHIFT",          /* Name.  */
5394          FALSE,                 /* Partial_inplace.  */
5395          0,                     /* Source mask.  */
5396          0,                     /* Dest mask.  */
5397          FALSE),                /* PC rel offset.  */
5398
5399   /* Hack. Linkage is done by linker.  */
5400   HOWTO (ALPHA_R_LINKAGE,       /* Type.  */
5401          0,                     /* Rightshift.  */
5402          8,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5403          256,                   /* Bitsize.  */
5404          FALSE,                 /* PC relative.  */
5405          0,                     /* Bitpos.  */
5406          complain_overflow_dont,/* Complain_on_overflow.  */
5407          reloc_nil,             /* Special_function.  */
5408          "LINKAGE",             /* Name.  */
5409          FALSE,                 /* Partial_inplace.  */
5410          0,                     /* Source mask.  */
5411          0,                     /* Dest mask.  */
5412          FALSE),                /* PC rel offset.  */
5413
5414   /* A 32 bit reference to a symbol.  */
5415   HOWTO (ALPHA_R_REFLONG,       /* Type.  */
5416          0,                     /* Rightshift.  */
5417          2,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5418          32,                    /* Bitsize.  */
5419          FALSE,                 /* PC relative.  */
5420          0,                     /* Bitpos.  */
5421          complain_overflow_bitfield, /* Complain_on_overflow.  */
5422          reloc_nil,             /* Special_function.  */
5423          "REFLONG",             /* Name.  */
5424          TRUE,                  /* Partial_inplace.  */
5425          0xffffffff,            /* Source mask.  */
5426          0xffffffff,            /* Dest mask.  */
5427          FALSE),                /* PC rel offset.  */
5428
5429   /* A 64 bit reference to a procedure, written as 32 bit value.  */
5430   HOWTO (ALPHA_R_CODEADDR,      /* Type.  */
5431          0,                     /* Rightshift.  */
5432          4,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5433          64,                    /* Bitsize.  */
5434          FALSE,                 /* PC relative.  */
5435          0,                     /* Bitpos.  */
5436          complain_overflow_signed,/* Complain_on_overflow.  */
5437          reloc_nil,             /* Special_function.  */
5438          "CODEADDR",            /* Name.  */
5439          FALSE,                 /* Partial_inplace.  */
5440          0xffffffff,            /* Source mask.  */
5441          0xffffffff,            /* Dest mask.  */
5442          FALSE),                /* PC rel offset.  */
5443
5444   HOWTO (ALPHA_R_NOP,           /* Type.  */
5445          0,                     /* Rightshift.  */
5446          3,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5447          0,                     /* Bitsize.  */
5448          /* The following value must match that of ALPHA_R_BSR/ALPHA_R_BOH
5449             because the calculations for the 3 relocations are the same.
5450             See B.4.5.2 of the OpenVMS Linker Utility Manual.  */
5451          TRUE,                  /* PC relative.  */
5452          0,                     /* Bitpos.   */
5453          complain_overflow_dont,/* Complain_on_overflow.  */
5454          reloc_nil,             /* Special_function.  */
5455          "NOP",                 /* Name.  */
5456          FALSE,                 /* Partial_inplace.  */
5457          0xffffffff,            /* Source mask.  */
5458          0xffffffff,            /* Dest mask.  */
5459          FALSE),                /* PC rel offset.  */
5460
5461   HOWTO (ALPHA_R_BSR,           /* Type.  */
5462          0,                     /* Rightshift.  */
5463          3,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5464          0,                     /* Bitsize.  */
5465          TRUE,                  /* PC relative.  */
5466          0,                     /* Bitpos.  */
5467          complain_overflow_dont,/* Complain_on_overflow.  */
5468          reloc_nil,             /* Special_function.  */
5469          "BSR",                 /* Name.  */
5470          FALSE,                 /* Partial_inplace.  */
5471          0xffffffff,            /* Source mask.  */
5472          0xffffffff,            /* Dest mask.  */
5473          FALSE),                /* PC rel offset.  */
5474
5475   HOWTO (ALPHA_R_LDA,           /* Type.  */
5476          0,                     /* Rightshift.  */
5477          3,                     /* Size (0 = byte, 1 = short, 2 = long).  */
5478          0,                     /* Bitsize.  */
5479          FALSE,                 /* PC relative.  */
5480          0,                     /* Bitpos.  */
5481          complain_overflow_dont,/* Complain_on_overflow.  */
5482          reloc_nil,             /* Special_function.  */
5483          "LDA",                 /* Name.  */
5484          FALSE,                 /* Partial_inplace.  */
5485          0xffffffff,            /* Source mask.  */
5486          0xffffffff,            /* Dest mask.  */
5487          FALSE),                /* PC rel offset.  */
5488
5489   HOWTO (ALPHA_R_BOH,           /* Type.  */
5490          0,                     /* Rightshift.  */
5491          3,                     /* Size (0 = byte, 1 = short, 2 = long, 3 = nil).  */
5492          0,                     /* Bitsize.  */
5493          TRUE,                  /* PC relative.  */
5494          0,                     /* Bitpos.  */
5495          complain_overflow_dont,/* Complain_on_overflow.  */
5496          reloc_nil,             /* Special_function.  */
5497          "BOH",                 /* Name.  */
5498          FALSE,                 /* Partial_inplace.  */
5499          0xffffffff,            /* Source mask.  */
5500          0xffffffff,            /* Dest mask.  */
5501          FALSE),                /* PC rel offset.  */
5502 };
5503
5504 /* Return a pointer to a howto structure which, when invoked, will perform
5505    the relocation code on data from the architecture noted.  */
5506
5507 static const struct reloc_howto_struct *
5508 alpha_vms_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
5509                                  bfd_reloc_code_real_type code)
5510 {
5511   int alpha_type;
5512
5513   vms_debug2 ((1, "vms_bfd_reloc_type_lookup (%p, %d)\t", abfd, code));
5514
5515   switch (code)
5516     {
5517       case BFD_RELOC_16:                alpha_type = ALPHA_R_SREL16;    break;
5518       case BFD_RELOC_32:                alpha_type = ALPHA_R_REFLONG;   break;
5519       case BFD_RELOC_64:                alpha_type = ALPHA_R_REFQUAD;   break;
5520       case BFD_RELOC_CTOR:              alpha_type = ALPHA_R_REFQUAD;   break;
5521       case BFD_RELOC_23_PCREL_S2:       alpha_type = ALPHA_R_BRADDR;    break;
5522       case BFD_RELOC_ALPHA_HINT:        alpha_type = ALPHA_R_HINT;      break;
5523       case BFD_RELOC_16_PCREL:          alpha_type = ALPHA_R_SREL16;    break;
5524       case BFD_RELOC_32_PCREL:          alpha_type = ALPHA_R_SREL32;    break;
5525       case BFD_RELOC_64_PCREL:          alpha_type = ALPHA_R_SREL64;    break;
5526       case BFD_RELOC_ALPHA_LINKAGE:     alpha_type = ALPHA_R_LINKAGE;   break;
5527       case BFD_RELOC_ALPHA_CODEADDR:    alpha_type = ALPHA_R_CODEADDR;  break;
5528       case BFD_RELOC_ALPHA_NOP:         alpha_type = ALPHA_R_NOP;       break;
5529       case BFD_RELOC_ALPHA_BSR:         alpha_type = ALPHA_R_BSR;       break;
5530       case BFD_RELOC_ALPHA_LDA:         alpha_type = ALPHA_R_LDA;       break;
5531       case BFD_RELOC_ALPHA_BOH:         alpha_type = ALPHA_R_BOH;       break;
5532       default:
5533         (*_bfd_error_handler) ("reloc (%d) is *UNKNOWN*", code);
5534         return NULL;
5535     }
5536   vms_debug2 ((2, "reloc is %s\n", alpha_howto_table[alpha_type].name));
5537   return & alpha_howto_table[alpha_type];
5538 }
5539
5540 static reloc_howto_type *
5541 alpha_vms_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
5542                                  const char *r_name)
5543 {
5544   unsigned int i;
5545
5546   for (i = 0;
5547        i < sizeof (alpha_howto_table) / sizeof (alpha_howto_table[0]);
5548        i++)
5549     if (alpha_howto_table[i].name != NULL
5550         && strcasecmp (alpha_howto_table[i].name, r_name) == 0)
5551       return &alpha_howto_table[i];
5552
5553   return NULL;
5554 }
5555 \f
5556 static long
5557 alpha_vms_get_synthetic_symtab (bfd *abfd,
5558                                 long symcount ATTRIBUTE_UNUSED,
5559                                 asymbol **usyms ATTRIBUTE_UNUSED,
5560                                 long dynsymcount ATTRIBUTE_UNUSED,
5561                                 asymbol **dynsyms ATTRIBUTE_UNUSED,
5562                                 asymbol **ret)
5563 {
5564   asymbol *syms;
5565   unsigned int i;
5566   unsigned int n = 0;
5567
5568   syms = (asymbol *) bfd_malloc (PRIV (norm_sym_count) * sizeof (asymbol));
5569   *ret = syms;
5570   if (syms == NULL)
5571     return -1;
5572
5573   for (i = 0; i < PRIV (gsd_sym_count); i++)
5574     {
5575       struct vms_symbol_entry *e = PRIV (syms)[i];
5576       asymbol *sym;
5577       flagword flags;
5578       symvalue value;
5579       asection *sec;
5580       const char *name;
5581       char *sname;
5582       int l;
5583
5584       name = e->name;
5585       value = 0;
5586       flags = BSF_LOCAL | BSF_SYNTHETIC;
5587       sec = NULL;
5588
5589       switch (e->typ)
5590         {
5591         case EGSD__C_SYM:
5592         case EGSD__C_SYMG:
5593           if ((e->flags & EGSY__V_DEF) && (e->flags & EGSY__V_NORM))
5594             {
5595               value = e->code_value;
5596               sec = e->code_section;
5597             }
5598           else
5599             continue;
5600           break;
5601
5602         default:
5603           continue;
5604         }
5605
5606       l = strlen (name);
5607       sname = bfd_alloc (abfd, l + 5);
5608       if (sname == NULL)
5609         return FALSE;
5610       memcpy (sname, name, l);
5611       memcpy (sname + l, "..en", 5);
5612
5613       sym = &syms[n++];
5614       sym->name = sname;
5615       sym->section = sec;
5616       sym->flags = flags;
5617       sym->value = value;
5618       sym->udata.p = NULL;
5619     }
5620
5621   return n;
5622 }
5623 \f
5624 /* Private dump.  */
5625
5626 static const char *
5627 vms_time_to_str (unsigned char *buf)
5628 {
5629   time_t t = vms_rawtime_to_time_t (buf);
5630   char *res = ctime (&t);
5631
5632   if (!res)
5633     res = "*invalid time*";
5634   else
5635     res[24] = 0;
5636   return res;
5637 }
5638
5639 static void
5640 evax_bfd_print_emh (FILE *file, unsigned char *rec, unsigned int rec_len)
5641 {
5642   struct vms_emh_common *emh = (struct vms_emh_common *)rec;
5643   unsigned int subtype;
5644
5645   subtype = (unsigned)bfd_getl16 (emh->subtyp);
5646
5647   fprintf (file, _("  EMH %u (len=%u): "), subtype, rec_len);
5648
5649   switch (subtype)
5650     {
5651     case EMH__C_MHD:
5652       {
5653         struct vms_emh_mhd *mhd = (struct vms_emh_mhd *)rec;
5654         const char *name;
5655
5656         fprintf (file, _("Module header\n"));
5657         fprintf (file, _("   structure level: %u\n"), mhd->strlvl);
5658         fprintf (file, _("   max record size: %u\n"),
5659                  (unsigned)bfd_getl32 (mhd->recsiz));
5660         name = (char *)(mhd + 1);
5661         fprintf (file, _("   module name    : %.*s\n"), name[0], name + 1);
5662         name += name[0] + 1;
5663         fprintf (file, _("   module version : %.*s\n"), name[0], name + 1);
5664         name += name[0] + 1;
5665         fprintf (file, _("   compile date   : %.17s\n"), name);
5666       }
5667       break;
5668     case EMH__C_LNM:
5669       {
5670         fprintf (file, _("Language Processor Name\n"));
5671         fprintf (file, _("   language name: %.*s\n"),
5672                  (int)(rec_len - sizeof (struct vms_emh_common)),
5673                  (char *)rec + sizeof (struct vms_emh_common));
5674       }
5675       break;
5676     case EMH__C_SRC:
5677       {
5678         fprintf (file, _("Source Files Header\n"));
5679         fprintf (file, _("   file: %.*s\n"),
5680                  (int)(rec_len - sizeof (struct vms_emh_common)),
5681                  (char *)rec + sizeof (struct vms_emh_common));
5682       }
5683       break;
5684     case EMH__C_TTL:
5685       {
5686         fprintf (file, _("Title Text Header\n"));
5687         fprintf (file, _("   title: %.*s\n"),
5688                  (int)(rec_len - sizeof (struct vms_emh_common)),
5689                  (char *)rec + sizeof (struct vms_emh_common));
5690       }
5691       break;
5692     case EMH__C_CPR:
5693       {
5694         fprintf (file, _("Copyright Header\n"));
5695         fprintf (file, _("   copyright: %.*s\n"),
5696                  (int)(rec_len - sizeof (struct vms_emh_common)),
5697                  (char *)rec + sizeof (struct vms_emh_common));
5698       }
5699       break;
5700     default:
5701       fprintf (file, _("unhandled emh subtype %u\n"), subtype);
5702       break;
5703     }
5704 }
5705
5706 static void
5707 evax_bfd_print_eeom (FILE *file, unsigned char *rec, unsigned int rec_len)
5708 {
5709   struct vms_eeom *eeom = (struct vms_eeom *)rec;
5710
5711   fprintf (file, _("  EEOM (len=%u):\n"), rec_len);
5712   fprintf (file, _("   number of cond linkage pairs: %u\n"),
5713            (unsigned)bfd_getl32 (eeom->total_lps));
5714   fprintf (file, _("   completion code: %u\n"),
5715            (unsigned)bfd_getl16 (eeom->comcod));
5716   if (rec_len > 10)
5717     {
5718       fprintf (file, _("   transfer addr flags: 0x%02x\n"), eeom->tfrflg);
5719       fprintf (file, _("   transfer addr psect: %u\n"),
5720                (unsigned)bfd_getl32 (eeom->psindx));
5721       fprintf (file, _("   transfer address   : 0x%08x\n"),
5722                (unsigned)bfd_getl32 (eeom->tfradr));
5723     }
5724 }
5725
5726 static void
5727 exav_bfd_print_egsy_flags (unsigned int flags, FILE *file)
5728 {
5729   if (flags & EGSY__V_WEAK)
5730     fputs (_(" WEAK"), file);
5731   if (flags & EGSY__V_DEF)
5732     fputs (_(" DEF"), file);
5733   if (flags & EGSY__V_UNI)
5734     fputs (_(" UNI"), file);
5735   if (flags & EGSY__V_REL)
5736     fputs (_(" REL"), file);
5737   if (flags & EGSY__V_COMM)
5738     fputs (_(" COMM"), file);
5739   if (flags & EGSY__V_VECEP)
5740     fputs (_(" VECEP"), file);
5741   if (flags & EGSY__V_NORM)
5742     fputs (_(" NORM"), file);
5743   if (flags & EGSY__V_QUAD_VAL)
5744     fputs (_(" QVAL"), file);
5745 }
5746
5747 static void
5748 evax_bfd_print_egsd_flags (FILE *file, unsigned int flags)
5749 {
5750   if (flags & EGPS__V_PIC)
5751     fputs (_(" PIC"), file);
5752   if (flags & EGPS__V_LIB)
5753     fputs (_(" LIB"), file);
5754   if (flags & EGPS__V_OVR)
5755     fputs (_(" OVR"), file);
5756   if (flags & EGPS__V_REL)
5757     fputs (_(" REL"), file);
5758   if (flags & EGPS__V_GBL)
5759     fputs (_(" GBL"), file);
5760   if (flags & EGPS__V_SHR)
5761     fputs (_(" SHR"), file);
5762   if (flags & EGPS__V_EXE)
5763     fputs (_(" EXE"), file);
5764   if (flags & EGPS__V_RD)
5765     fputs (_(" RD"), file);
5766   if (flags & EGPS__V_WRT)
5767     fputs (_(" WRT"), file);
5768   if (flags & EGPS__V_VEC)
5769     fputs (_(" VEC"), file);
5770   if (flags & EGPS__V_NOMOD)
5771     fputs (_(" NOMOD"), file);
5772   if (flags & EGPS__V_COM)
5773     fputs (_(" COM"), file);
5774   if (flags & EGPS__V_ALLOC_64BIT)
5775     fputs (_(" 64B"), file);
5776 }
5777
5778 static void
5779 evax_bfd_print_egsd (FILE *file, unsigned char *rec, unsigned int rec_len)
5780 {
5781   unsigned int off = sizeof (struct vms_egsd);
5782   unsigned int n;
5783
5784   fprintf (file, _("  EGSD (len=%u):\n"), rec_len);
5785
5786   n = 0;
5787   for (off = sizeof (struct vms_egsd); off < rec_len; )
5788     {
5789       struct vms_egsd_entry *e = (struct vms_egsd_entry *)(rec + off);
5790       unsigned int type;
5791       unsigned int len;
5792
5793       type = (unsigned)bfd_getl16 (e->gsdtyp);
5794       len = (unsigned)bfd_getl16 (e->gsdsiz);
5795
5796       fprintf (file, _("  EGSD entry %2u (type: %u, len: %u): "),
5797                n, type, len);
5798       n++;
5799
5800       switch (type)
5801         {
5802         case EGSD__C_PSC:
5803           {
5804             struct vms_egps *egps = (struct vms_egps *)e;
5805             unsigned int flags = bfd_getl16 (egps->flags);
5806             unsigned int l;
5807
5808             fprintf (file, _("PSC - Program section definition\n"));
5809             fprintf (file, _("   alignment  : 2**%u\n"), egps->align);
5810             fprintf (file, _("   flags      : 0x%04x"), flags);
5811             evax_bfd_print_egsd_flags (file, flags);
5812             fputc ('\n', file);
5813             l = bfd_getl32 (egps->alloc);
5814             fprintf (file, _("   alloc (len): %u (0x%08x)\n"), l, l);
5815             fprintf (file, _("   name       : %.*s\n"),
5816                      egps->namlng, egps->name);
5817           }
5818           break;
5819         case EGSD__C_SPSC:
5820           {
5821             struct vms_esgps *esgps = (struct vms_esgps *)e;
5822             unsigned int flags = bfd_getl16 (esgps->flags);
5823             unsigned int l;
5824
5825             fprintf (file, _("SPSC - Shared Image Program section def\n"));
5826             fprintf (file, _("   alignment  : 2**%u\n"), esgps->align);
5827             fprintf (file, _("   flags      : 0x%04x"), flags);
5828             evax_bfd_print_egsd_flags (file, flags);
5829             fputc ('\n', file);
5830             l = bfd_getl32 (esgps->alloc);
5831             fprintf (file, _("   alloc (len)   : %u (0x%08x)\n"), l, l);
5832             fprintf (file, _("   image offset  : 0x%08x\n"),
5833                      (unsigned int)bfd_getl32 (esgps->base));
5834             fprintf (file, _("   symvec offset : 0x%08x\n"),
5835                      (unsigned int)bfd_getl32 (esgps->value));
5836             fprintf (file, _("   name          : %.*s\n"),
5837                      esgps->namlng, esgps->name);
5838           }
5839           break;
5840         case EGSD__C_SYM:
5841           {
5842             struct vms_egsy *egsy = (struct vms_egsy *)e;
5843             unsigned int flags = bfd_getl16 (egsy->flags);
5844
5845             if (flags & EGSY__V_DEF)
5846               {
5847                 struct vms_esdf *esdf = (struct vms_esdf *)e;
5848
5849                 fprintf (file, _("SYM - Global symbol definition\n"));
5850                 fprintf (file, _("   flags: 0x%04x"), flags);
5851                 exav_bfd_print_egsy_flags (flags, file);
5852                 fputc ('\n', file);
5853                 fprintf (file, _("   psect offset: 0x%08x\n"),
5854                          (unsigned)bfd_getl32 (esdf->value));
5855                 if (flags & EGSY__V_NORM)
5856                   {
5857                     fprintf (file, _("   code address: 0x%08x\n"),
5858                              (unsigned)bfd_getl32 (esdf->code_address));
5859                     fprintf (file, _("   psect index for entry point : %u\n"),
5860                              (unsigned)bfd_getl32 (esdf->ca_psindx));
5861                   }
5862                 fprintf (file, _("   psect index : %u\n"),
5863                          (unsigned)bfd_getl32 (esdf->psindx));
5864                 fprintf (file, _("   name        : %.*s\n"),
5865                          esdf->namlng, esdf->name);
5866               }
5867             else
5868               {
5869                 struct vms_esrf *esrf = (struct vms_esrf *)e;
5870
5871                 fprintf (file, _("SYM - Global symbol reference\n"));
5872                 fprintf (file, _("   name       : %.*s\n"),
5873                          esrf->namlng, esrf->name);
5874               }
5875           }
5876           break;
5877         case EGSD__C_IDC:
5878           {
5879             struct vms_eidc *eidc = (struct vms_eidc *)e;
5880             unsigned int flags = bfd_getl32 (eidc->flags);
5881             unsigned char *p;
5882
5883             fprintf (file, _("IDC - Ident Consistency check\n"));
5884             fprintf (file, _("   flags         : 0x%08x"), flags);
5885             if (flags & EIDC__V_BINIDENT)
5886               fputs (" BINDENT", file);
5887             fputc ('\n', file);
5888             fprintf (file, _("   id match      : %x\n"),
5889                      (flags >> EIDC__V_IDMATCH_SH) & EIDC__V_IDMATCH_MASK);
5890             fprintf (file, _("   error severity: %x\n"),
5891                      (flags >> EIDC__V_ERRSEV_SH) & EIDC__V_ERRSEV_MASK);
5892             p = eidc->name;
5893             fprintf (file, _("   entity name   : %.*s\n"), p[0], p + 1);
5894             p += 1 + p[0];
5895             fprintf (file, _("   object name   : %.*s\n"), p[0], p + 1);
5896             p += 1 + p[0];
5897             if (flags & EIDC__V_BINIDENT)
5898               fprintf (file, _("   binary ident  : 0x%08x\n"),
5899                        (unsigned)bfd_getl32 (p + 1));
5900             else
5901               fprintf (file, _("   ascii ident   : %.*s\n"), p[0], p + 1);
5902           }
5903           break;
5904         case EGSD__C_SYMG:
5905           {
5906             struct vms_egst *egst = (struct vms_egst *)e;
5907             unsigned int flags = bfd_getl16 (egst->header.flags);
5908
5909             fprintf (file, _("SYMG - Universal symbol definition\n"));
5910             fprintf (file, _("   flags: 0x%04x"), flags);
5911             exav_bfd_print_egsy_flags (flags, file);
5912             fputc ('\n', file);
5913             fprintf (file, _("   symbol vector offset: 0x%08x\n"),
5914                      (unsigned)bfd_getl32 (egst->value));
5915             fprintf (file, _("   entry point: 0x%08x\n"),
5916                      (unsigned)bfd_getl32 (egst->lp_1));
5917             fprintf (file, _("   proc descr : 0x%08x\n"),
5918                      (unsigned)bfd_getl32 (egst->lp_2));
5919             fprintf (file, _("   psect index: %u\n"),
5920                      (unsigned)bfd_getl32 (egst->psindx));
5921             fprintf (file, _("   name       : %.*s\n"),
5922                      egst->namlng, egst->name);
5923           }
5924           break;
5925         case EGSD__C_SYMV:
5926           {
5927             struct vms_esdfv *esdfv = (struct vms_esdfv *)e;
5928             unsigned int flags = bfd_getl16 (esdfv->flags);
5929
5930             fprintf (file, _("SYMV - Vectored symbol definition\n"));
5931             fprintf (file, _("   flags: 0x%04x"), flags);
5932             exav_bfd_print_egsy_flags (flags, file);
5933             fputc ('\n', file);
5934             fprintf (file, _("   vector      : 0x%08x\n"),
5935                      (unsigned)bfd_getl32 (esdfv->vector));
5936             fprintf (file, _("   psect offset: %u\n"),
5937                      (unsigned)bfd_getl32 (esdfv->value));
5938             fprintf (file, _("   psect index : %u\n"),
5939                      (unsigned)bfd_getl32 (esdfv->psindx));
5940             fprintf (file, _("   name        : %.*s\n"),
5941                      esdfv->namlng, esdfv->name);
5942           }
5943           break;
5944         case EGSD__C_SYMM:
5945           {
5946             struct vms_esdfm *esdfm = (struct vms_esdfm *)e;
5947             unsigned int flags = bfd_getl16 (esdfm->flags);
5948
5949             fprintf (file, _("SYMM - Global symbol definition with version\n"));
5950             fprintf (file, _("   flags: 0x%04x"), flags);
5951             exav_bfd_print_egsy_flags (flags, file);
5952             fputc ('\n', file);
5953             fprintf (file, _("   version mask: 0x%08x\n"),
5954                      (unsigned)bfd_getl32 (esdfm->version_mask));
5955             fprintf (file, _("   psect offset: %u\n"),
5956                      (unsigned)bfd_getl32 (esdfm->value));
5957             fprintf (file, _("   psect index : %u\n"),
5958                      (unsigned)bfd_getl32 (esdfm->psindx));
5959             fprintf (file, _("   name        : %.*s\n"),
5960                      esdfm->namlng, esdfm->name);
5961           }
5962           break;
5963         default:
5964           fprintf (file, _("unhandled egsd entry type %u\n"), type);
5965           break;
5966         }
5967       off += len;
5968     }
5969 }
5970
5971 static void
5972 evax_bfd_print_hex (FILE *file, const char *pfx,
5973                     const unsigned char *buf, unsigned int len)
5974 {
5975   unsigned int i;
5976   unsigned int n;
5977
5978   n = 0;
5979   for (i = 0; i < len; i++)
5980     {
5981       if (n == 0)
5982         fputs (pfx, file);
5983       fprintf (file, " %02x", buf[i]);
5984       n++;
5985       if (n == 16)
5986         {
5987           n = 0;
5988           fputc ('\n', file);
5989         }
5990     }
5991   if (n != 0)
5992     fputc ('\n', file);
5993 }
5994
5995 static void
5996 evax_bfd_print_etir_stc_ir (FILE *file, const unsigned char *buf, int is_ps)
5997 {
5998   fprintf (file, _("    linkage index: %u, replacement insn: 0x%08x\n"),
5999            (unsigned)bfd_getl32 (buf),
6000            (unsigned)bfd_getl32 (buf + 16));
6001   fprintf (file, _("    psect idx 1: %u, offset 1: 0x%08x %08x\n"),
6002            (unsigned)bfd_getl32 (buf + 4),
6003            (unsigned)bfd_getl32 (buf + 12),
6004            (unsigned)bfd_getl32 (buf + 8));
6005   fprintf (file, _("    psect idx 2: %u, offset 2: 0x%08x %08x\n"),
6006            (unsigned)bfd_getl32 (buf + 20),
6007            (unsigned)bfd_getl32 (buf + 28),
6008            (unsigned)bfd_getl32 (buf + 24));
6009   if (is_ps)
6010     fprintf (file, _("    psect idx 3: %u, offset 3: 0x%08x %08x\n"),
6011              (unsigned)bfd_getl32 (buf + 32),
6012              (unsigned)bfd_getl32 (buf + 40),
6013              (unsigned)bfd_getl32 (buf + 36));
6014   else
6015     fprintf (file, _("    global name: %.*s\n"), buf[32], buf + 33);
6016 }
6017
6018 static void
6019 evax_bfd_print_etir (FILE *file, const char *name,
6020                      unsigned char *rec, unsigned int rec_len)
6021 {
6022   unsigned int off = sizeof (struct vms_egsd);
6023   unsigned int sec_len;
6024
6025   fprintf (file, _("  %s (len=%u+%u):\n"), name,
6026            (unsigned)(rec_len - sizeof (struct vms_eobjrec)),
6027            (unsigned)sizeof (struct vms_eobjrec));
6028
6029   for (off = sizeof (struct vms_eobjrec); off < rec_len; )
6030     {
6031       struct vms_etir *etir = (struct vms_etir *)(rec + off);
6032       unsigned char *buf;
6033       unsigned int type;
6034       unsigned int size;
6035
6036       type = bfd_getl16 (etir->rectyp);
6037       size = bfd_getl16 (etir->size);
6038       buf = rec + off + sizeof (struct vms_etir);
6039
6040       fprintf (file, _("   (type: %3u, size: 4+%3u): "), type, size - 4);
6041       switch (type)
6042         {
6043         case ETIR__C_STA_GBL:
6044           fprintf (file, _("STA_GBL (stack global) %.*s\n"),
6045                    buf[0], buf + 1);
6046           break;
6047         case ETIR__C_STA_LW:
6048           fprintf (file, _("STA_LW (stack longword) 0x%08x\n"),
6049                    (unsigned)bfd_getl32 (buf));
6050           break;
6051         case ETIR__C_STA_QW:
6052           fprintf (file, _("STA_QW (stack quadword) 0x%08x %08x\n"),
6053                    (unsigned)bfd_getl32 (buf + 4),
6054                    (unsigned)bfd_getl32 (buf + 0));
6055           break;
6056         case ETIR__C_STA_PQ:
6057           fprintf (file, _("STA_PQ (stack psect base + offset)\n"));
6058           fprintf (file, _("    psect: %u, offset: 0x%08x %08x\n"),
6059                    (unsigned)bfd_getl32 (buf + 0),
6060                    (unsigned)bfd_getl32 (buf + 8),
6061                    (unsigned)bfd_getl32 (buf + 4));
6062           break;
6063         case ETIR__C_STA_LI:
6064           fprintf (file, _("STA_LI (stack literal)\n"));
6065           break;
6066         case ETIR__C_STA_MOD:
6067           fprintf (file, _("STA_MOD (stack module)\n"));
6068           break;
6069         case ETIR__C_STA_CKARG:
6070           fprintf (file, _("STA_CKARG (compare procedure argument)\n"));
6071           break;
6072
6073         case ETIR__C_STO_B:
6074           fprintf (file, _("STO_B (store byte)\n"));
6075           break;
6076         case ETIR__C_STO_W:
6077           fprintf (file, _("STO_W (store word)\n"));
6078           break;
6079         case ETIR__C_STO_LW:
6080           fprintf (file, _("STO_LW (store longword)\n"));
6081           break;
6082         case ETIR__C_STO_QW:
6083           fprintf (file, _("STO_QW (store quadword)\n"));
6084           break;
6085         case ETIR__C_STO_IMMR:
6086           {
6087             unsigned int len = bfd_getl32 (buf);
6088             fprintf (file,
6089                      _("STO_IMMR (store immediate repeat) %u bytes\n"),
6090                      len);
6091             evax_bfd_print_hex (file, "   ", buf + 4, len);
6092             sec_len += len;
6093           }
6094           break;
6095         case ETIR__C_STO_GBL:
6096           fprintf (file, _("STO_GBL (store global) %.*s\n"),
6097                    buf[0], buf + 1);
6098           break;
6099         case ETIR__C_STO_CA:
6100           fprintf (file, _("STO_CA (store code address) %.*s\n"),
6101                    buf[0], buf + 1);
6102           break;
6103         case ETIR__C_STO_RB:
6104           fprintf (file, _("STO_RB (store relative branch)\n"));
6105           break;
6106         case ETIR__C_STO_AB:
6107           fprintf (file, _("STO_AB (store absolute branch)\n"));
6108           break;
6109         case ETIR__C_STO_OFF:
6110           fprintf (file, _("STO_OFF (store offset to psect)\n"));
6111           break;
6112         case ETIR__C_STO_IMM:
6113           {
6114             unsigned int len = bfd_getl32 (buf);
6115             fprintf (file,
6116                      _("STO_IMM (store immediate) %u bytes\n"),
6117                      len);
6118             evax_bfd_print_hex (file, "   ", buf + 4, len);
6119             sec_len += len;
6120           }
6121           break;
6122         case ETIR__C_STO_GBL_LW:
6123           fprintf (file, _("STO_GBL_LW (store global longword) %.*s\n"),
6124                    buf[0], buf + 1);
6125           break;
6126         case ETIR__C_STO_LP_PSB:
6127           fprintf (file, _("STO_OFF (store LP with procedure signature)\n"));
6128           break;
6129         case ETIR__C_STO_HINT_GBL:
6130           fprintf (file, _("STO_BR_GBL (store branch global) *todo*\n"));
6131           break;
6132         case ETIR__C_STO_HINT_PS:
6133           fprintf (file, _("STO_BR_PS (store branch psect + offset) *todo*\n"));
6134           break;
6135
6136         case ETIR__C_OPR_NOP:
6137           fprintf (file, _("OPR_NOP (no-operation)\n"));
6138           break;
6139         case ETIR__C_OPR_ADD:
6140           fprintf (file, _("OPR_ADD (add)\n"));
6141           break;
6142         case ETIR__C_OPR_SUB:
6143           fprintf (file, _("OPR_SUB (substract)\n"));
6144           break;
6145         case ETIR__C_OPR_MUL:
6146           fprintf (file, _("OPR_MUL (multiply)\n"));
6147           break;
6148         case ETIR__C_OPR_DIV:
6149           fprintf (file, _("OPR_DIV (divide)\n"));
6150           break;
6151         case ETIR__C_OPR_AND:
6152           fprintf (file, _("OPR_AND (logical and)\n"));
6153           break;
6154         case ETIR__C_OPR_IOR:
6155           fprintf (file, _("OPR_IOR (logical inclusive or)\n"));
6156           break;
6157         case ETIR__C_OPR_EOR:
6158           fprintf (file, _("OPR_EOR (logical exclusive or)\n"));
6159           break;
6160         case ETIR__C_OPR_NEG:
6161           fprintf (file, _("OPR_NEG (negate)\n"));
6162           break;
6163         case ETIR__C_OPR_COM:
6164           fprintf (file, _("OPR_COM (complement)\n"));
6165           break;
6166         case ETIR__C_OPR_INSV:
6167           fprintf (file, _("OPR_INSV (insert field)\n"));
6168           break;
6169         case ETIR__C_OPR_ASH:
6170           fprintf (file, _("OPR_ASH (arithmetic shift)\n"));
6171           break;
6172         case ETIR__C_OPR_USH:
6173           fprintf (file, _("OPR_USH (unsigned shift)\n"));
6174           break;
6175         case ETIR__C_OPR_ROT:
6176           fprintf (file, _("OPR_ROT (rotate)\n"));
6177           break;
6178         case ETIR__C_OPR_SEL:
6179           fprintf (file, _("OPR_SEL (select)\n"));
6180           break;
6181         case ETIR__C_OPR_REDEF:
6182           fprintf (file, _("OPR_REDEF (redefine symbol to curr location)\n"));
6183           break;
6184         case ETIR__C_OPR_DFLIT:
6185           fprintf (file, _("OPR_REDEF (define a literal)\n"));
6186           break;
6187
6188         case ETIR__C_STC_LP:
6189           fprintf (file, _("STC_LP (store cond linkage pair)\n"));
6190           break;
6191         case ETIR__C_STC_LP_PSB:
6192           fprintf (file,
6193                    _("STC_LP_PSB (store cond linkage pair + signature)\n"));
6194           fprintf (file, _("   linkage index: %u, procedure: %.*s\n"),
6195                    (unsigned)bfd_getl32 (buf), buf[4], buf + 5);
6196           buf += 4 + 1 + buf[4];
6197           fprintf (file, _("   signature: %.*s\n"), buf[0], buf + 1);
6198           break;
6199         case ETIR__C_STC_GBL:
6200           fprintf (file, _("STC_GBL (store cond global)\n"));
6201           fprintf (file, _("   linkage index: %u, global: %.*s\n"),
6202                    (unsigned)bfd_getl32 (buf), buf[4], buf + 5);
6203           break;
6204         case ETIR__C_STC_GCA:
6205           fprintf (file, _("STC_GCA (store cond code address)\n"));
6206           fprintf (file, _("   linkage index: %u, procedure name: %.*s\n"),
6207                    (unsigned)bfd_getl32 (buf), buf[4], buf + 5);
6208           break;
6209         case ETIR__C_STC_PS:
6210           fprintf (file, _("STC_PS (store cond psect + offset)\n"));
6211           fprintf (file,
6212                    _("   linkage index: %u, psect: %u, offset: 0x%08x %08x\n"),
6213                    (unsigned)bfd_getl32 (buf),
6214                    (unsigned)bfd_getl32 (buf + 4),
6215                    (unsigned)bfd_getl32 (buf + 12),
6216                    (unsigned)bfd_getl32 (buf + 8));
6217           break;
6218         case ETIR__C_STC_NOP_GBL:
6219           fprintf (file, _("STC_NOP_GBL (store cond NOP at global addr)\n"));
6220           evax_bfd_print_etir_stc_ir (file, buf, 0);
6221           break;
6222         case ETIR__C_STC_NOP_PS:
6223           fprintf (file, _("STC_NOP_PS (store cond NOP at psect + offset)\n"));
6224           evax_bfd_print_etir_stc_ir (file, buf, 1);
6225           break;
6226         case ETIR__C_STC_BSR_GBL:
6227           fprintf (file, _("STC_BSR_GBL (store cond BSR at global addr)\n"));
6228           evax_bfd_print_etir_stc_ir (file, buf, 0);
6229           break;
6230         case ETIR__C_STC_BSR_PS:
6231           fprintf (file, _("STC_BSR_PS (store cond BSR at psect + offset)\n"));
6232           evax_bfd_print_etir_stc_ir (file, buf, 1);
6233           break;
6234         case ETIR__C_STC_LDA_GBL:
6235           fprintf (file, _("STC_LDA_GBL (store cond LDA at global addr)\n"));
6236           evax_bfd_print_etir_stc_ir (file, buf, 0);
6237           break;
6238         case ETIR__C_STC_LDA_PS:
6239           fprintf (file, _("STC_LDA_PS (store cond LDA at psect + offset)\n"));
6240           evax_bfd_print_etir_stc_ir (file, buf, 1);
6241           break;
6242         case ETIR__C_STC_BOH_GBL:
6243           fprintf (file, _("STC_BOH_GBL (store cond BOH at global addr)\n"));
6244           evax_bfd_print_etir_stc_ir (file, buf, 0);
6245           break;
6246         case ETIR__C_STC_BOH_PS:
6247           fprintf (file, _("STC_BOH_PS (store cond BOH at psect + offset)\n"));
6248           evax_bfd_print_etir_stc_ir (file, buf, 1);
6249           break;
6250         case ETIR__C_STC_NBH_GBL:
6251           fprintf (file,
6252                    _("STC_NBH_GBL (store cond or hint at global addr)\n"));
6253           break;
6254         case ETIR__C_STC_NBH_PS:
6255           fprintf (file,
6256                    _("STC_NBH_PS (store cond or hint at psect + offset)\n"));
6257           break;
6258
6259         case ETIR__C_CTL_SETRB:
6260           fprintf (file, _("CTL_SETRB (set relocation base)\n"));
6261           sec_len += 4;
6262           break;
6263         case ETIR__C_CTL_AUGRB:
6264           {
6265             unsigned int val = bfd_getl32 (buf);
6266             fprintf (file, _("CTL_AUGRB (augment relocation base) %u\n"), val);
6267           }
6268           break;
6269         case ETIR__C_CTL_DFLOC:
6270           fprintf (file, _("CTL_DFLOC (define location)\n"));
6271           break;
6272         case ETIR__C_CTL_STLOC:
6273           fprintf (file, _("CTL_STLOC (set location)\n"));
6274           break;
6275         case ETIR__C_CTL_STKDL:
6276           fprintf (file, _("CTL_STKDL (stack defined location)\n"));
6277           break;
6278         default:
6279           fprintf (file, _("*unhandled*\n"));
6280           break;
6281         }
6282       off += size;
6283     }
6284 }
6285
6286 static void
6287 evax_bfd_print_eobj (struct bfd *abfd, FILE *file)
6288 {
6289   bfd_boolean is_first = TRUE;
6290   bfd_boolean has_records = FALSE;
6291
6292   while (1)
6293     {
6294       unsigned int rec_len;
6295       unsigned int pad_len;
6296       unsigned char *rec;
6297       unsigned int hdr_size;
6298       unsigned int type;
6299
6300       if (is_first)
6301         {
6302           unsigned char buf[6];
6303
6304           is_first = FALSE;
6305
6306           /* Read 6 bytes.  */
6307           if (bfd_bread (buf, sizeof (buf), abfd) != sizeof (buf))
6308             {
6309               fprintf (file, _("cannot read GST record length\n"));
6310               return;
6311             }
6312           rec_len = bfd_getl16 (buf + 0);
6313           if (rec_len == bfd_getl16 (buf + 4)
6314               && bfd_getl16 (buf + 2) == EOBJ__C_EMH)
6315             {
6316               /* The format is raw: record-size, type, record-size.  */
6317               has_records = TRUE;
6318               pad_len = (rec_len + 1) & ~1U;
6319               hdr_size = 4;
6320             }
6321           else if (rec_len == EOBJ__C_EMH)
6322             {
6323               has_records = FALSE;
6324               pad_len = bfd_getl16 (buf + 2);
6325               hdr_size = 6;
6326             }
6327           else
6328             {
6329               /* Ill-formed.  */
6330               fprintf (file, _("cannot find EMH in first GST record\n"));
6331               return;
6332             }
6333           rec = bfd_malloc (pad_len);
6334           memcpy (rec, buf + sizeof (buf) - hdr_size, hdr_size);
6335         }
6336       else
6337         {
6338           unsigned int rec_len2 = 0;
6339           unsigned char hdr[4];
6340
6341           if (has_records)
6342             {
6343               unsigned char buf_len[2];
6344
6345               if (bfd_bread (buf_len, sizeof (buf_len), abfd)
6346                   != sizeof (buf_len))
6347                 {
6348                   fprintf (file, _("cannot read GST record length\n"));
6349                   return;
6350                 }
6351               rec_len2 = (unsigned)bfd_getl16 (buf_len);
6352             }
6353
6354           if (bfd_bread (hdr, sizeof (hdr), abfd) != sizeof (hdr))
6355             {
6356               fprintf (file, _("cannot read GST record header\n"));
6357               return;
6358             }
6359           rec_len = (unsigned)bfd_getl16 (hdr + 2);
6360           if (has_records)
6361             pad_len = (rec_len + 1) & ~1U;
6362           else
6363             pad_len = rec_len;
6364           rec = bfd_malloc (pad_len);
6365           memcpy (rec, hdr, sizeof (hdr));
6366           hdr_size = sizeof (hdr);
6367           if (has_records && rec_len2 != rec_len)
6368             {
6369               fprintf (file, _(" corrupted GST\n"));
6370               break;
6371             }
6372         }
6373
6374       if (bfd_bread (rec + hdr_size, pad_len - hdr_size, abfd)
6375           != pad_len - hdr_size)
6376         {
6377           fprintf (file, _("cannot read GST record\n"));
6378           return;
6379         }
6380
6381       type = (unsigned)bfd_getl16 (rec);
6382
6383       switch (type)
6384         {
6385         case EOBJ__C_EMH:
6386           evax_bfd_print_emh (file, rec, rec_len);
6387           break;
6388         case EOBJ__C_EGSD:
6389           evax_bfd_print_egsd (file, rec, rec_len);
6390           break;
6391         case EOBJ__C_EEOM:
6392           evax_bfd_print_eeom (file, rec, rec_len);
6393           free (rec);
6394           return;
6395           break;
6396         case EOBJ__C_ETIR:
6397           evax_bfd_print_etir (file, "ETIR", rec, rec_len);
6398           break;
6399         case EOBJ__C_EDBG:
6400           evax_bfd_print_etir (file, "EDBG", rec, rec_len);
6401           break;
6402         case EOBJ__C_ETBT:
6403           evax_bfd_print_etir (file, "ETBT", rec, rec_len);
6404           break;
6405         default:
6406           fprintf (file, _(" unhandled EOBJ record type %u\n"), type);
6407           break;
6408         }
6409       free (rec);
6410     }
6411 }
6412
6413 static void
6414 evax_bfd_print_relocation_records (FILE *file, const unsigned char *rel,
6415                                    unsigned int stride)
6416 {
6417   while (1)
6418     {
6419       unsigned int base;
6420       unsigned int count;
6421       unsigned int j;
6422
6423       count = bfd_getl32 (rel + 0);
6424
6425       if (count == 0)
6426         break;
6427       base = bfd_getl32 (rel + 4);
6428
6429       fprintf (file, _("  bitcount: %u, base addr: 0x%08x\n"),
6430                count, base);
6431
6432       rel += 8;
6433       for (j = 0; count > 0; j += 4, count -= 32)
6434         {
6435           unsigned int k;
6436           unsigned int n = 0;
6437           unsigned int val;
6438
6439           val = bfd_getl32 (rel);
6440           rel += 4;
6441
6442           fprintf (file, _("   bitmap: 0x%08x (count: %u):\n"), val, count);
6443
6444           for (k = 0; k < 32; k++)
6445             if (val & (1 << k))
6446               {
6447                 if (n == 0)
6448                   fputs ("   ", file);
6449                 fprintf (file, _(" %08x"), base + (j * 8 + k) * stride);
6450                 n++;
6451                 if (n == 8)
6452                   {
6453                     fputs ("\n", file);
6454                     n = 0;
6455                   }
6456               }
6457           if (n)
6458             fputs ("\n", file);
6459         }
6460     }
6461 }
6462
6463 static void
6464 evax_bfd_print_address_fixups (FILE *file, const unsigned char *rel)
6465 {
6466   while (1)
6467     {
6468       unsigned int j;
6469       unsigned int count;
6470
6471       count = bfd_getl32 (rel + 0);
6472       if (count == 0)
6473         return;
6474       fprintf (file, _("  image %u (%u entries)\n"),
6475                (unsigned)bfd_getl32 (rel + 4), count);
6476       rel += 8;
6477       for (j = 0; j < count; j++)
6478         {
6479           fprintf (file, _("   offset: 0x%08x, val: 0x%08x\n"),
6480                    (unsigned)bfd_getl32 (rel + 0),
6481                    (unsigned)bfd_getl32 (rel + 4));
6482           rel += 8;
6483         }
6484     }
6485 }
6486
6487 static void
6488 evax_bfd_print_reference_fixups (FILE *file, const unsigned char *rel)
6489 {
6490   unsigned int count;
6491
6492   while (1)
6493     {
6494       unsigned int j;
6495       unsigned int n = 0;
6496
6497       count = bfd_getl32 (rel + 0);
6498       if (count == 0)
6499         break;
6500       fprintf (file, _("  image %u (%u entries), offsets:\n"),
6501                (unsigned)bfd_getl32 (rel + 4), count);
6502       rel += 8;
6503       for (j = 0; j < count; j++)
6504         {
6505           if (n == 0)
6506             fputs ("   ", file);
6507           fprintf (file, _(" 0x%08x"), (unsigned)bfd_getl32 (rel));
6508           n++;
6509           if (n == 7)
6510             {
6511               fputs ("\n", file);
6512               n = 0;
6513             }
6514           rel += 4;
6515         }
6516       if (n)
6517         fputs ("\n", file);
6518     }
6519 }
6520
6521 static void
6522 evax_bfd_print_indent (int indent, FILE *file)
6523 {
6524   for (; indent; indent--)
6525     fputc (' ', file);
6526 }
6527
6528 static const char *
6529 evax_bfd_get_dsc_name (unsigned int v)
6530 {
6531   switch (v)
6532     {
6533     case DSC__K_DTYPE_Z:
6534       return "Z (Unspecified)";
6535     case DSC__K_DTYPE_V:
6536       return "V (Bit)";
6537     case DSC__K_DTYPE_BU:
6538       return "BU (Byte logical)";
6539     case DSC__K_DTYPE_WU:
6540       return "WU (Word logical)";
6541     case DSC__K_DTYPE_LU:
6542       return "LU (Longword logical)";
6543     case DSC__K_DTYPE_QU:
6544       return "QU (Quadword logical)";
6545     case DSC__K_DTYPE_B:
6546       return "B (Byte integer)";
6547     case DSC__K_DTYPE_W:
6548       return "W (Word integer)";
6549     case DSC__K_DTYPE_L:
6550       return "L (Longword integer)";
6551     case DSC__K_DTYPE_Q:
6552       return "Q (Quadword integer)";
6553     case DSC__K_DTYPE_F:
6554       return "F (Single-precision floating)";
6555     case DSC__K_DTYPE_D:
6556       return "D (Double-precision floating)";
6557     case DSC__K_DTYPE_FC:
6558       return "FC (Complex)";
6559     case DSC__K_DTYPE_DC:
6560       return "DC (Double-precision Complex)";
6561     case DSC__K_DTYPE_T:
6562       return "T (ASCII text string)";
6563     case DSC__K_DTYPE_NU:
6564       return "NU (Numeric string, unsigned)";
6565     case DSC__K_DTYPE_NL:
6566       return "NL (Numeric string, left separate sign)";
6567     case DSC__K_DTYPE_NLO:
6568       return "NLO (Numeric string, left overpunched sign)";
6569     case DSC__K_DTYPE_NR:
6570       return "NR (Numeric string, right separate sign)";
6571     case DSC__K_DTYPE_NRO:
6572       return "NRO (Numeric string, right overpunched sig)";
6573     case DSC__K_DTYPE_NZ:
6574       return "NZ (Numeric string, zoned sign)";
6575     case DSC__K_DTYPE_P:
6576       return "P (Packed decimal string)";
6577     case DSC__K_DTYPE_ZI:
6578       return "ZI (Sequence of instructions)";
6579     case DSC__K_DTYPE_ZEM:
6580       return "ZEM (Procedure entry mask)";
6581     case DSC__K_DTYPE_DSC:
6582       return "DSC (Descriptor, used for arrays of dyn strings)";
6583     case DSC__K_DTYPE_OU:
6584       return "OU (Octaword logical)";
6585     case DSC__K_DTYPE_O:
6586       return "O (Octaword integer)";
6587     case DSC__K_DTYPE_G:
6588       return "G (Double precision G floating, 64 bit)";
6589     case DSC__K_DTYPE_H:
6590       return "H (Quadruple precision floating, 128 bit)";
6591     case DSC__K_DTYPE_GC:
6592       return "GC (Double precision complex, G floating)";
6593     case DSC__K_DTYPE_HC:
6594       return "HC (Quadruple precision complex, H floating)";
6595     case DSC__K_DTYPE_CIT:
6596       return "CIT (COBOL intermediate temporary)";
6597     case DSC__K_DTYPE_BPV:
6598       return "BPV (Bound Procedure Value)";
6599     case DSC__K_DTYPE_BLV:
6600       return "BLV (Bound Label Value)";
6601     case DSC__K_DTYPE_VU:
6602       return "VU (Bit Unaligned)";
6603     case DSC__K_DTYPE_ADT:
6604       return "ADT (Absolute Date-Time)";
6605     case DSC__K_DTYPE_VT:
6606       return "VT (Varying Text)";
6607     case DSC__K_DTYPE_T2:
6608       return "T2 (16-bit char)";
6609     case DSC__K_DTYPE_VT2:
6610       return "VT2 (16-bit varying char)";
6611     default:
6612       return "?? (unknown)";
6613     }
6614 }
6615
6616 static void
6617 evax_bfd_print_desc (const unsigned char *buf, int indent, FILE *file)
6618 {
6619   unsigned char bclass = buf[3];
6620   unsigned char dtype = buf[2];
6621   unsigned int len = (unsigned)bfd_getl16 (buf);
6622   unsigned int pointer = (unsigned)bfd_getl32 (buf + 4);
6623
6624   evax_bfd_print_indent (indent, file);
6625
6626   if (len == 1 && pointer == 0xffffffffUL)
6627     {
6628       /* 64 bits.  */
6629       fprintf (file, _("64 bits *unhandled*\n"));
6630     }
6631   else
6632     {
6633       fprintf (file, _("class: %u, dtype: %u, length: %u, pointer: 0x%08x\n"),
6634                bclass, dtype, len, pointer);
6635       switch (bclass)
6636         {
6637         case DSC__K_CLASS_NCA:
6638           {
6639             const struct vms_dsc_nca *dsc = (const void *)buf;
6640             unsigned int i;
6641             const unsigned char *b;
6642
6643             evax_bfd_print_indent (indent, file);
6644             fprintf (file, _("non-contiguous array of %s\n"),
6645                      evax_bfd_get_dsc_name (dsc->dtype));
6646             evax_bfd_print_indent (indent + 1, file);
6647             fprintf (file,
6648                      _("dimct: %u, aflags: 0x%02x, digits: %u, scale: %u\n"),
6649                      dsc->dimct, dsc->aflags, dsc->digits, dsc->scale);
6650             evax_bfd_print_indent (indent + 1, file);
6651             fprintf (file,
6652                      _("arsize: %u, a0: 0x%08x\n"),
6653                      (unsigned)bfd_getl32 (dsc->arsize),
6654                      (unsigned)bfd_getl32 (dsc->a0));
6655             evax_bfd_print_indent (indent + 1, file);
6656             fprintf (file, _("Strides:\n"));
6657             b = buf + sizeof (*dsc);
6658             for (i = 0; i < dsc->dimct; i++)
6659               {
6660                 evax_bfd_print_indent (indent + 2, file);
6661                 fprintf (file, _("[%u]: %u\n"), i + 1,
6662                          (unsigned)bfd_getl32 (b));
6663                 b += 4;
6664               }
6665             evax_bfd_print_indent (indent + 1, file);
6666             fprintf (file, _("Bounds:\n"));
6667             b = buf + sizeof (*dsc);
6668             for (i = 0; i < dsc->dimct; i++)
6669               {
6670                 evax_bfd_print_indent (indent + 2, file);
6671                 fprintf (file, _("[%u]: Lower: %u, upper: %u\n"), i + 1,
6672                          (unsigned)bfd_getl32 (b + 0),
6673                          (unsigned)bfd_getl32 (b + 4));
6674                 b += 8;
6675               }
6676           }
6677           break;
6678         case DSC__K_CLASS_UBS:
6679           {
6680             const struct vms_dsc_ubs *ubs = (const void *)buf;
6681
6682             evax_bfd_print_indent (indent, file);
6683             fprintf (file, _("unaligned bit-string of %s\n"),
6684                      evax_bfd_get_dsc_name (ubs->dtype));
6685             evax_bfd_print_indent (indent + 1, file);
6686             fprintf (file,
6687                      _("base: %u, pos: %u\n"),
6688                      (unsigned)bfd_getl32 (ubs->base),
6689                      (unsigned)bfd_getl32 (ubs->pos));
6690           }
6691           break;
6692         default:
6693           fprintf (file, _("*unhandled*\n"));
6694           break;
6695         }
6696     }
6697 }
6698
6699 static unsigned int
6700 evax_bfd_print_valspec (const unsigned char *buf, int indent, FILE *file)
6701 {
6702   unsigned int vflags = buf[0];
6703   unsigned int value = (unsigned)bfd_getl32 (buf + 1);
6704   unsigned int len = 5;
6705
6706   evax_bfd_print_indent (indent, file);
6707   fprintf (file, _("vflags: 0x%02x, value: 0x%08x "), vflags, value);
6708   buf += 5;
6709
6710   switch (vflags)
6711     {
6712     case DST__K_VFLAGS_NOVAL:
6713       fprintf (file, _("(no value)\n"));
6714       break;
6715     case DST__K_VFLAGS_NOTACTIVE:
6716       fprintf (file, _("(not active)\n"));
6717       break;
6718     case DST__K_VFLAGS_UNALLOC:
6719       fprintf (file, _("(not allocated)\n"));
6720       break;
6721     case DST__K_VFLAGS_DSC:
6722       fprintf (file, _("(descriptor)\n"));
6723       evax_bfd_print_desc (buf + value, indent + 1, file);
6724       break;
6725     case DST__K_VFLAGS_TVS:
6726       fprintf (file, _("(trailing value)\n"));
6727       break;
6728     case DST__K_VS_FOLLOWS:
6729       fprintf (file, _("(value spec follows)\n"));
6730       break;
6731     case DST__K_VFLAGS_BITOFFS:
6732       fprintf (file, _("(at bit offset %u)\n"), value);
6733       break;
6734     default:
6735       fprintf (file, _("(reg: %u, disp: %u, indir: %u, kind: "),
6736                (vflags & DST__K_REGNUM_MASK) >> DST__K_REGNUM_SHIFT,
6737                vflags & DST__K_DISP ? 1 : 0,
6738                vflags & DST__K_INDIR ? 1 : 0);
6739       switch (vflags & DST__K_VALKIND_MASK)
6740         {
6741         case DST__K_VALKIND_LITERAL:
6742           fputs (_("literal"), file);
6743           break;
6744         case DST__K_VALKIND_ADDR:
6745           fputs (_("address"), file);
6746           break;
6747         case DST__K_VALKIND_DESC:
6748           fputs (_("desc"), file);
6749           break;
6750         case DST__K_VALKIND_REG:
6751           fputs (_("reg"), file);
6752           break;
6753         }
6754       fputs (")\n", file);
6755       break;
6756     }
6757   return len;
6758 }
6759
6760 static void
6761 evax_bfd_print_typspec (const unsigned char *buf, int indent, FILE *file)
6762 {
6763   unsigned char kind = buf[2];
6764   unsigned int len = (unsigned)bfd_getl16 (buf);
6765
6766   evax_bfd_print_indent (indent, file);
6767   fprintf (file, ("len: %2u, kind: %2u "), len, kind);
6768   buf += 3;
6769   switch (kind)
6770     {
6771     case DST__K_TS_ATOM:
6772       fprintf (file, ("atomic, type=0x%02x %s\n"),
6773                buf[0], evax_bfd_get_dsc_name (buf[0]));
6774       break;
6775     case DST__K_TS_IND:
6776       fprintf (file, ("indirect, defined at 0x%08x\n"),
6777                (unsigned)bfd_getl32 (buf));
6778       break;
6779     case DST__K_TS_TPTR:
6780       fprintf (file, ("typed pointer\n"));
6781       evax_bfd_print_typspec (buf, indent + 1, file);
6782       break;
6783     case DST__K_TS_PTR:
6784       fprintf (file, ("pointer\n"));
6785       break;
6786     case DST__K_TS_ARRAY:
6787       {
6788         const unsigned char *vs;
6789         unsigned int vec_len;
6790         unsigned int i;
6791
6792         fprintf (file, ("array, dim: %u, bitmap: "), buf[0]);
6793         vec_len = (buf[0] + 1 + 7) / 8;
6794         for (i = 0; i < vec_len; i++)
6795           fprintf (file, " %02x", buf[i + 1]);
6796         fputc ('\n', file);
6797         vs = buf + 1 + vec_len;
6798         evax_bfd_print_indent (indent, file);
6799         fprintf (file, ("array descriptor:\n"));
6800         vs += evax_bfd_print_valspec (vs, indent + 1, file);
6801         for (i = 0; i < buf[0] + 1U; i++)
6802           if (buf[1 + i / 8] & (1 << (i % 8)))
6803             {
6804               evax_bfd_print_indent (indent, file);
6805               if (i == 0)
6806                 fprintf (file, ("type spec for element:\n"));
6807               else
6808                 fprintf (file, ("type spec for subscript %u:\n"), i);
6809               evax_bfd_print_typspec (vs, indent + 1, file);
6810               vs += bfd_getl16 (vs);
6811             }
6812       }
6813       break;
6814     default:
6815       fprintf (file, ("*unhandled*\n"));
6816     }
6817 }
6818
6819 static void
6820 evax_bfd_print_dst (struct bfd *abfd, unsigned int dst_size, FILE *file)
6821 {
6822   unsigned int off = 0;
6823   unsigned int pc = 0;
6824   unsigned int line = 0;
6825
6826   fprintf (file, _("Debug symbol table:\n"));
6827
6828   while (dst_size > 0)
6829     {
6830       struct vms_dst_header dsth;
6831       unsigned int len;
6832       unsigned int type;
6833       unsigned char *buf;
6834
6835       if (bfd_bread (&dsth, sizeof (dsth), abfd) != sizeof (dsth))
6836         {
6837           fprintf (file, _("cannot read DST header\n"));
6838           return;
6839         }
6840       len = bfd_getl16 (dsth.length);
6841       type = bfd_getl16 (dsth.type);
6842       fprintf (file, _(" type: %3u, len: %3u (at 0x%08x): "),
6843                type, len, off);
6844       if (len == 0)
6845         {
6846           fputc ('\n', file);
6847           break;
6848         }
6849       len++;
6850       dst_size -= len;
6851       off += len;
6852       len -= sizeof (dsth);
6853       buf = bfd_malloc (len);
6854       if (bfd_bread (buf, len, abfd) != len)
6855         {
6856           fprintf (file, _("cannot read DST symbol\n"));
6857           return;
6858         }
6859       switch (type)
6860         {
6861         case DSC__K_DTYPE_V:
6862         case DSC__K_DTYPE_BU:
6863         case DSC__K_DTYPE_WU:
6864         case DSC__K_DTYPE_LU:
6865         case DSC__K_DTYPE_QU:
6866         case DSC__K_DTYPE_B:
6867         case DSC__K_DTYPE_W:
6868         case DSC__K_DTYPE_L:
6869         case DSC__K_DTYPE_Q:
6870         case DSC__K_DTYPE_F:
6871         case DSC__K_DTYPE_D:
6872         case DSC__K_DTYPE_FC:
6873         case DSC__K_DTYPE_DC:
6874         case DSC__K_DTYPE_T:
6875         case DSC__K_DTYPE_NU:
6876         case DSC__K_DTYPE_NL:
6877         case DSC__K_DTYPE_NLO:
6878         case DSC__K_DTYPE_NR:
6879         case DSC__K_DTYPE_NRO:
6880         case DSC__K_DTYPE_NZ:
6881         case DSC__K_DTYPE_P:
6882         case DSC__K_DTYPE_ZI:
6883         case DSC__K_DTYPE_ZEM:
6884         case DSC__K_DTYPE_DSC:
6885         case DSC__K_DTYPE_OU:
6886         case DSC__K_DTYPE_O:
6887         case DSC__K_DTYPE_G:
6888         case DSC__K_DTYPE_H:
6889         case DSC__K_DTYPE_GC:
6890         case DSC__K_DTYPE_HC:
6891         case DSC__K_DTYPE_CIT:
6892         case DSC__K_DTYPE_BPV:
6893         case DSC__K_DTYPE_BLV:
6894         case DSC__K_DTYPE_VU:
6895         case DSC__K_DTYPE_ADT:
6896         case DSC__K_DTYPE_VT:
6897         case DSC__K_DTYPE_T2:
6898         case DSC__K_DTYPE_VT2:
6899           fprintf (file, _("standard data: %s\n"),
6900                    evax_bfd_get_dsc_name (type));
6901           evax_bfd_print_valspec (buf, 4, file);
6902           fprintf (file, _("    name: %.*s\n"), buf[5], buf + 6);
6903           break;
6904         case DST__K_MODBEG:
6905           {
6906             struct vms_dst_modbeg *dst = (void *)buf;
6907             const char *name = (const char *)buf + sizeof (*dst);
6908
6909             fprintf (file, _("modbeg\n"));
6910             fprintf (file, _("   flags: %d, language: %u, "
6911                              "major: %u, minor: %u\n"),
6912                      dst->flags,
6913                      (unsigned)bfd_getl32 (dst->language),
6914                      (unsigned)bfd_getl16 (dst->major),
6915                      (unsigned)bfd_getl16 (dst->minor));
6916             fprintf (file, _("   module name: %.*s\n"),
6917                      name[0], name + 1);
6918             name += name[0] + 1;
6919             fprintf (file, _("   compiler   : %.*s\n"),
6920                      name[0], name + 1);
6921           }
6922           break;
6923         case DST__K_MODEND:
6924           fprintf (file, _("modend\n"));
6925           break;
6926         case DST__K_RTNBEG:
6927           {
6928             struct vms_dst_rtnbeg *dst = (void *)buf;
6929             const char *name = (const char *)buf + sizeof (*dst);
6930
6931             fputs (_("rtnbeg\n"), file);
6932             fprintf (file, _("    flags: %u, address: 0x%08x, "
6933                              "pd-address: 0x%08x\n"),
6934                      dst->flags,
6935                      (unsigned)bfd_getl32 (dst->address),
6936                      (unsigned)bfd_getl32 (dst->pd_address));
6937             fprintf (file, _("    routine name: %.*s\n"),
6938                      name[0], name + 1);
6939           }
6940           break;
6941         case DST__K_RTNEND:
6942           {
6943             struct vms_dst_rtnend *dst = (void *)buf;
6944
6945             fprintf (file, _("rtnend: size 0x%08x\n"),
6946                      (unsigned)bfd_getl32 (dst->size));
6947           }
6948           break;
6949         case DST__K_PROLOG:
6950           {
6951             struct vms_dst_prolog *dst = (void *)buf;
6952
6953             fprintf (file, _("prolog: bkpt address 0x%08x\n"),
6954                      (unsigned)bfd_getl32 (dst->bkpt_addr));
6955           }
6956           break;
6957         case DST__K_EPILOG:
6958           {
6959             struct vms_dst_epilog *dst = (void *)buf;
6960
6961             fprintf (file, _("epilog: flags: %u, count: %u\n"),
6962                      dst->flags, (unsigned)bfd_getl32 (dst->count));
6963           }
6964           break;
6965         case DST__K_BLKBEG:
6966           {
6967             struct vms_dst_blkbeg *dst = (void *)buf;
6968             const char *name = (const char *)buf + sizeof (*dst);
6969
6970             fprintf (file, _("blkbeg: address: 0x%08x, name: %.*s\n"),
6971                      (unsigned)bfd_getl32 (dst->address),
6972                      name[0], name + 1);
6973           }
6974           break;
6975         case DST__K_BLKEND:
6976           {
6977             struct vms_dst_blkend *dst = (void *)buf;
6978
6979             fprintf (file, _("blkend: size: 0x%08x\n"),
6980                      (unsigned)bfd_getl32 (dst->size));
6981           }
6982           break;
6983         case DST__K_TYPSPEC:
6984           {
6985             fprintf (file, _("typspec (len: %u)\n"), len);
6986             fprintf (file, _("    name: %.*s\n"), buf[0], buf + 1);
6987             evax_bfd_print_typspec (buf + 1 + buf[0], 5, file);
6988           }
6989           break;
6990         case DST__K_SEPTYP:
6991           {
6992             fprintf (file, _("septyp, name: %.*s\n"), buf[5], buf + 6);
6993             evax_bfd_print_valspec (buf, 4, file);
6994           }
6995           break;
6996         case DST__K_RECBEG:
6997           {
6998             struct vms_dst_recbeg *recbeg = (void *)buf;
6999             const char *name = (const char *)buf + sizeof (*recbeg);
7000
7001             fprintf (file, _("recbeg: name: %.*s\n"), name[0], name + 1);
7002             evax_bfd_print_valspec (buf, 4, file);
7003             fprintf (file, ("    len: %u bits\n"),
7004                      (unsigned)bfd_getl32 (name + 1 + name[0]));
7005           }
7006           break;
7007         case DST__K_RECEND:
7008           fprintf (file, _("recend\n"));
7009           break;
7010         case DST__K_ENUMBEG:
7011           fprintf (file, _("enumbeg, len: %u, name: %.*s\n"),
7012                    buf[0], buf[1], buf + 2);
7013           break;
7014         case DST__K_ENUMELT:
7015           fprintf (file, _("enumelt, name: %.*s\n"), buf[5], buf + 6);
7016           evax_bfd_print_valspec (buf, 4, file);
7017           break;
7018         case DST__K_ENUMEND:
7019           fprintf (file, _("enumend\n"));
7020           break;
7021         case DST__K_LABEL:
7022           {
7023             struct vms_dst_label *lab = (void *)buf;
7024             fprintf (file, ("label, name: %.*s\n"),
7025                      lab->name[0], lab->name + 1);
7026             fprintf (file, ("    address: 0x%08x\n"),
7027                      (unsigned)bfd_getl32 (lab->value));
7028           }
7029           break;
7030         case DST__K_DIS_RANGE:
7031           {
7032             unsigned int cnt = bfd_getl32 (buf);
7033             unsigned char *rng = buf + 4;
7034             unsigned int i;
7035
7036             fprintf (file, _("discontiguous range (nbr: %u)\n"), cnt);
7037             for (i = 0; i < cnt; i++, rng += 8)
7038               fprintf (file, _("    address: 0x%08x, size: %u\n"),
7039                        (unsigned)bfd_getl32 (rng),
7040                        (unsigned)bfd_getl32 (rng + 4));
7041
7042           }
7043           break;
7044         case DST__K_LINE_NUM:
7045           {
7046             unsigned char *buf_orig = buf;
7047
7048             fprintf (file, _("line num  (len: %u)\n"), len);
7049
7050             while (len > 0)
7051               {
7052                 signed char cmd;
7053                 unsigned char cmdlen;
7054                 unsigned int val;
7055
7056                 cmd = buf[0];
7057                 cmdlen = 0;
7058
7059                 fputs ("    ", file);
7060
7061                 switch (cmd)
7062                   {
7063                   case DST__K_DELTA_PC_W:
7064                     val = bfd_getl16 (buf + 1);
7065                     fprintf (file, _("delta_pc_w %u\n"), val);
7066                     pc += val;
7067                     line++;
7068                     cmdlen = 3;
7069                     break;
7070                   case DST__K_INCR_LINUM:
7071                     val = buf[1];
7072                     fprintf (file, _("incr_linum(b): +%u\n"), val);
7073                     line += val;
7074                     cmdlen = 2;
7075                     break;
7076                   case DST__K_INCR_LINUM_W:
7077                     val = bfd_getl16 (buf + 1);
7078                     fprintf (file, _("incr_linum_w: +%u\n"), val);
7079                     line += val;
7080                     cmdlen = 3;
7081                     break;
7082                   case DST__K_INCR_LINUM_L:
7083                     val = bfd_getl32 (buf + 1);
7084                     fprintf (file, _("incr_linum_l: +%u\n"), val);
7085                     line += val;
7086                     cmdlen = 5;
7087                     break;
7088                   case DST__K_SET_LINUM:
7089                     line = bfd_getl16 (buf + 1);
7090                     fprintf (file, _("set_line_num(w) %u\n"), line);
7091                     cmdlen = 3;
7092                     break;
7093                   case DST__K_SET_LINUM_B:
7094                     line = buf[1];
7095                     fprintf (file, _("set_line_num_b %u\n"), line);
7096                     cmdlen = 2;
7097                     break;
7098                   case DST__K_SET_LINUM_L:
7099                     line = bfd_getl32 (buf + 1);
7100                     fprintf (file, _("set_line_num_l %u\n"), line);
7101                     cmdlen = 5;
7102                     break;
7103                   case DST__K_SET_ABS_PC:
7104                     pc = bfd_getl32 (buf + 1);
7105                     fprintf (file, _("set_abs_pc: 0x%08x\n"), pc);
7106                     cmdlen = 5;
7107                     break;
7108                   case DST__K_DELTA_PC_L:
7109                     fprintf (file, _("delta_pc_l: +0x%08x\n"),
7110                              (unsigned)bfd_getl32 (buf + 1));
7111                     cmdlen = 5;
7112                     break;
7113                   case DST__K_TERM:
7114                     fprintf (file, _("term(b): 0x%02x"), buf[1]);
7115                     pc += buf[1];
7116                     fprintf (file, _("        pc: 0x%08x\n"), pc);
7117                     cmdlen = 2;
7118                     break;
7119                   case DST__K_TERM_W:
7120                     val = bfd_getl16 (buf + 1);
7121                     fprintf (file, _("term_w: 0x%04x"), val);
7122                     pc += val;
7123                     fprintf (file, _("    pc: 0x%08x\n"), pc);
7124                     cmdlen = 3;
7125                     break;
7126                   default:
7127                     if (cmd <= 0)
7128                       {
7129                         fprintf (file, _("delta pc +%-4d"), -cmd);
7130                         line++;  /* FIXME: curr increment.  */
7131                         pc += -cmd;
7132                         fprintf (file, _("    pc: 0x%08x line: %5u\n"),
7133                                  pc, line);
7134                         cmdlen = 1;
7135                       }
7136                     else
7137                       fprintf (file, _("    *unhandled* cmd %u\n"), cmd);
7138                     break;
7139                   }
7140                 if (cmdlen == 0)
7141                   break;
7142                 len -= cmdlen;
7143                 buf += cmdlen;
7144               }
7145             buf = buf_orig;
7146           }
7147           break;
7148         case DST__K_SOURCE:
7149           {
7150             unsigned char *buf_orig = buf;
7151
7152             fprintf (file, _("source (len: %u)\n"), len);
7153
7154             while (len > 0)
7155               {
7156                 signed char cmd = buf[0];
7157                 unsigned char cmdlen = 0;
7158
7159                 switch (cmd)
7160                   {
7161                   case DST__K_SRC_DECLFILE:
7162                     {
7163                       struct vms_dst_src_decl_src *src = (void *)(buf + 1);
7164                       const char *name;
7165
7166                       fprintf (file, _("   declfile: len: %u, flags: %u, "
7167                                        "fileid: %u\n"),
7168                                src->length, src->flags,
7169                                (unsigned)bfd_getl16 (src->fileid));
7170                       fprintf (file, _("   rms: cdt: 0x%08x %08x, "
7171                                        "ebk: 0x%08x, ffb: 0x%04x, "
7172                                        "rfo: %u\n"),
7173                                (unsigned)bfd_getl32 (src->rms_cdt + 4),
7174                                (unsigned)bfd_getl32 (src->rms_cdt + 0),
7175                                (unsigned)bfd_getl32 (src->rms_ebk),
7176                                (unsigned)bfd_getl16 (src->rms_ffb),
7177                                src->rms_rfo);
7178                       name = (const char *)buf + 1 + sizeof (*src);
7179                       fprintf (file, _("   filename   : %.*s\n"),
7180                                name[0], name + 1);
7181                       name += name[0] + 1;
7182                       fprintf (file, _("   module name: %.*s\n"),
7183                                name[0], name + 1);
7184                       cmdlen = 2 + src->length;
7185                     }
7186                     break;
7187                   case DST__K_SRC_SETFILE:
7188                     fprintf (file, _("   setfile %u\n"),
7189                              (unsigned)bfd_getl16 (buf + 1));
7190                     cmdlen = 3;
7191                     break;
7192                   case DST__K_SRC_SETREC_W:
7193                     fprintf (file, _("   setrec %u\n"),
7194                              (unsigned)bfd_getl16 (buf + 1));
7195                     cmdlen = 3;
7196                     break;
7197                   case DST__K_SRC_SETREC_L:
7198                     fprintf (file, _("   setrec %u\n"),
7199                              (unsigned)bfd_getl32 (buf + 1));
7200                     cmdlen = 5;
7201                     break;
7202                   case DST__K_SRC_SETLNUM_W:
7203                     fprintf (file, _("   setlnum %u\n"),
7204                              (unsigned)bfd_getl16 (buf + 1));
7205                     cmdlen = 3;
7206                     break;
7207                   case DST__K_SRC_SETLNUM_L:
7208                     fprintf (file, _("   setlnum %u\n"),
7209                              (unsigned)bfd_getl32 (buf + 1));
7210                     cmdlen = 5;
7211                     break;
7212                   case DST__K_SRC_DEFLINES_W:
7213                     fprintf (file, _("   deflines %u\n"),
7214                              (unsigned)bfd_getl16 (buf + 1));
7215                     cmdlen = 3;
7216                     break;
7217                   case DST__K_SRC_DEFLINES_B:
7218                     fprintf (file, _("   deflines %u\n"), buf[1]);
7219                     cmdlen = 2;
7220                     break;
7221                   case DST__K_SRC_FORMFEED:
7222                     fprintf (file, _("   formfeed\n"));
7223                     cmdlen = 1;
7224                     break;
7225                   default:
7226                     fprintf (file, _("   *unhandled* cmd %u\n"), cmd);
7227                     break;
7228                   }
7229                 if (cmdlen == 0)
7230                   break;
7231                 len -= cmdlen;
7232                 buf += cmdlen;
7233               }
7234             buf = buf_orig;
7235           }
7236           break;
7237         default:
7238           fprintf (file, _("*unhandled* dst type %u\n"), type);
7239           break;
7240         }
7241       free (buf);
7242     }
7243 }
7244
7245 static void
7246 evax_bfd_print_image (bfd *abfd, FILE *file)
7247 {
7248   struct vms_eihd eihd;
7249   const char *name;
7250   unsigned int val;
7251   unsigned int eiha_off;
7252   unsigned int eihi_off;
7253   unsigned int eihs_off;
7254   unsigned int eisd_off;
7255   unsigned int eihef_off = 0;
7256   unsigned int eihnp_off = 0;
7257   unsigned int dmt_vbn = 0;
7258   unsigned int dmt_size = 0;
7259   unsigned int dst_vbn = 0;
7260   unsigned int dst_size = 0;
7261   unsigned int gst_vbn = 0;
7262   unsigned int gst_size = 0;
7263   unsigned int eiaf_vbn = 0;
7264   unsigned int eiaf_size = 0;
7265   unsigned int eihvn_off;
7266
7267   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET)
7268       || bfd_bread (&eihd, sizeof (eihd), abfd) != sizeof (eihd))
7269     {
7270       fprintf (file, _("cannot read EIHD\n"));
7271       return;
7272     }
7273   fprintf (file, _("EIHD: (size: %u, nbr blocks: %u)\n"),
7274            (unsigned)bfd_getl32 (eihd.size),
7275            (unsigned)bfd_getl32 (eihd.hdrblkcnt));
7276   fprintf (file, _(" majorid: %u, minorid: %u\n"),
7277            (unsigned)bfd_getl32 (eihd.majorid),
7278            (unsigned)bfd_getl32 (eihd.minorid));
7279
7280   val = (unsigned)bfd_getl32 (eihd.imgtype);
7281   switch (val)
7282     {
7283     case EIHD__K_EXE:
7284       name = _("executable");
7285       break;
7286     case EIHD__K_LIM:
7287       name = _("linkable image");
7288       break;
7289     default:
7290       name = _("unknown");
7291       break;
7292     }
7293   fprintf (file, _(" image type: %u (%s)"), val, name);
7294
7295   val = (unsigned)bfd_getl32 (eihd.subtype);
7296   switch (val)
7297     {
7298     case EIHD__C_NATIVE:
7299       name = _("native");
7300       break;
7301     case EIHD__C_CLI:
7302       name = _("CLI");
7303       break;
7304     default:
7305       name = _("unknown");
7306       break;
7307     }
7308   fprintf (file, _(", subtype: %u (%s)\n"), val, name);
7309
7310   eisd_off = bfd_getl32 (eihd.isdoff);
7311   eiha_off = bfd_getl32 (eihd.activoff);
7312   eihi_off = bfd_getl32 (eihd.imgidoff);
7313   eihs_off = bfd_getl32 (eihd.symdbgoff);
7314   fprintf (file, _(" offsets: isd: %u, activ: %u, symdbg: %u, "
7315                    "imgid: %u, patch: %u\n"),
7316            eisd_off, eiha_off, eihs_off, eihi_off,
7317            (unsigned)bfd_getl32 (eihd.patchoff));
7318   fprintf (file, _(" fixup info rva: "));
7319   bfd_fprintf_vma (abfd, file, bfd_getl64 (eihd.iafva));
7320   fprintf (file, _(", symbol vector rva: "));
7321   bfd_fprintf_vma (abfd, file, bfd_getl64 (eihd.symvva));
7322   eihvn_off = bfd_getl32 (eihd.version_array_off);
7323   fprintf (file, _("\n"
7324                    " version array off: %u\n"),
7325            eihvn_off);
7326   fprintf (file,
7327            _(" img I/O count: %u, nbr channels: %u, req pri: %08x%08x\n"),
7328            (unsigned)bfd_getl32 (eihd.imgiocnt),
7329            (unsigned)bfd_getl32 (eihd.iochancnt),
7330            (unsigned)bfd_getl32 (eihd.privreqs + 4),
7331            (unsigned)bfd_getl32 (eihd.privreqs + 0));
7332   val = (unsigned)bfd_getl32 (eihd.lnkflags);
7333   fprintf (file, _(" linker flags: %08x:"), val);
7334   if (val & EIHD__M_LNKDEBUG)
7335     fprintf (file, " LNKDEBUG");
7336   if (val & EIHD__M_LNKNOTFR)
7337     fprintf (file, " LNKNOTFR");
7338   if (val & EIHD__M_NOP0BUFS)
7339     fprintf (file, " NOP0BUFS");
7340   if (val & EIHD__M_PICIMG)
7341     fprintf (file, " PICIMG");
7342   if (val & EIHD__M_P0IMAGE)
7343     fprintf (file, " P0IMAGE");
7344   if (val & EIHD__M_DBGDMT)
7345     fprintf (file, " DBGDMT");
7346   if (val & EIHD__M_INISHR)
7347     fprintf (file, " INISHR");
7348   if (val & EIHD__M_XLATED)
7349     fprintf (file, " XLATED");
7350   if (val & EIHD__M_BIND_CODE_SEC)
7351     fprintf (file, " BIND_CODE_SEC");
7352   if (val & EIHD__M_BIND_DATA_SEC)
7353     fprintf (file, " BIND_DATA_SEC");
7354   if (val & EIHD__M_MKTHREADS)
7355     fprintf (file, " MKTHREADS");
7356   if (val & EIHD__M_UPCALLS)
7357     fprintf (file, " UPCALLS");
7358   if (val & EIHD__M_OMV_READY)
7359     fprintf (file, " OMV_READY");
7360   if (val & EIHD__M_EXT_BIND_SECT)
7361     fprintf (file, " EXT_BIND_SECT");
7362   fprintf (file, "\n");
7363   fprintf (file, _(" ident: 0x%08x, sysver: 0x%08x, "
7364                    "match ctrl: %u, symvect_size: %u\n"),
7365            (unsigned)bfd_getl32 (eihd.ident),
7366            (unsigned)bfd_getl32 (eihd.sysver),
7367            eihd.matchctl,
7368            (unsigned)bfd_getl32 (eihd.symvect_size));
7369   fprintf (file, _(" BPAGE: %u"),
7370            (unsigned)bfd_getl32 (eihd.virt_mem_block_size));
7371   if (val & (EIHD__M_OMV_READY | EIHD__M_EXT_BIND_SECT))
7372     {
7373       eihef_off = bfd_getl32 (eihd.ext_fixup_off);
7374       eihnp_off = bfd_getl32 (eihd.noopt_psect_off);
7375       fprintf (file, _(", ext fixup offset: %u, no_opt psect off: %u"),
7376                eihef_off, eihnp_off);
7377     }
7378   fprintf (file, _(", alias: %u\n"), (unsigned)bfd_getl16 (eihd.alias));
7379
7380   if (eihvn_off != 0)
7381     {
7382       struct vms_eihvn eihvn;
7383       unsigned int mask;
7384       unsigned int j;
7385
7386       fprintf (file, _("system version array information:\n"));
7387       if (bfd_seek (abfd, (file_ptr) eihvn_off, SEEK_SET)
7388           || bfd_bread (&eihvn, sizeof (eihvn), abfd) != sizeof (eihvn))
7389         {
7390           fprintf (file, _("cannot read EIHVN header\n"));
7391           return;
7392         }
7393       mask = bfd_getl32 (eihvn.subsystem_mask);
7394       for (j = 0; j < 32; j++)
7395         if (mask & (1 << j))
7396           {
7397             struct vms_eihvn_subversion ver;
7398             if (bfd_bread (&ver, sizeof (ver), abfd) != sizeof (ver))
7399               {
7400                 fprintf (file, _("cannot read EIHVN version\n"));
7401                 return;
7402               }
7403             fprintf (file, _("   %02u "), j);
7404             switch (j)
7405               {
7406               case EIHVN__BASE_IMAGE_BIT:
7407                 fputs (_("BASE_IMAGE       "), file);
7408                 break;
7409               case EIHVN__MEMORY_MANAGEMENT_BIT:
7410                 fputs (_("MEMORY_MANAGEMENT"), file);
7411                 break;
7412               case EIHVN__IO_BIT:
7413                 fputs (_("IO               "), file);
7414                 break;
7415               case EIHVN__FILES_VOLUMES_BIT:
7416                 fputs (_("FILES_VOLUMES    "), file);
7417                 break;
7418               case EIHVN__PROCESS_SCHED_BIT:
7419                 fputs (_("PROCESS_SCHED    "), file);
7420                 break;
7421               case EIHVN__SYSGEN_BIT:
7422                 fputs (_("SYSGEN           "), file);
7423                 break;
7424               case EIHVN__CLUSTERS_LOCKMGR_BIT:
7425                 fputs (_("CLUSTERS_LOCKMGR "), file);
7426                 break;
7427               case EIHVN__LOGICAL_NAMES_BIT:
7428                 fputs (_("LOGICAL_NAMES    "), file);
7429                 break;
7430               case EIHVN__SECURITY_BIT:
7431                 fputs (_("SECURITY         "), file);
7432                 break;
7433               case EIHVN__IMAGE_ACTIVATOR_BIT:
7434                 fputs (_("IMAGE_ACTIVATOR  "), file);
7435                 break;
7436               case EIHVN__NETWORKS_BIT:
7437                 fputs (_("NETWORKS         "), file);
7438                 break;
7439               case EIHVN__COUNTERS_BIT:
7440                 fputs (_("COUNTERS         "), file);
7441                 break;
7442               case EIHVN__STABLE_BIT:
7443                 fputs (_("STABLE           "), file);
7444                 break;
7445               case EIHVN__MISC_BIT:
7446                 fputs (_("MISC             "), file);
7447                 break;
7448               case EIHVN__CPU_BIT:
7449                 fputs (_("CPU              "), file);
7450                 break;
7451               case EIHVN__VOLATILE_BIT:
7452                 fputs (_("VOLATILE         "), file);
7453                 break;
7454               case EIHVN__SHELL_BIT:
7455                 fputs (_("SHELL            "), file);
7456                 break;
7457               case EIHVN__POSIX_BIT:
7458                 fputs (_("POSIX            "), file);
7459                 break;
7460               case EIHVN__MULTI_PROCESSING_BIT:
7461                 fputs (_("MULTI_PROCESSING "), file);
7462                 break;
7463               case EIHVN__GALAXY_BIT:
7464                 fputs (_("GALAXY           "), file);
7465                 break;
7466               default:
7467                 fputs (_("*unknown*        "), file);
7468                 break;
7469               }
7470             fprintf (file, _(": %u.%u\n"),
7471                      (unsigned)bfd_getl16 (ver.major),
7472                      (unsigned)bfd_getl16 (ver.minor));
7473           }
7474     }
7475
7476   if (eiha_off != 0)
7477     {
7478       struct vms_eiha eiha;
7479
7480       if (bfd_seek (abfd, (file_ptr) eiha_off, SEEK_SET)
7481           || bfd_bread (&eiha, sizeof (eiha), abfd) != sizeof (eiha))
7482         {
7483           fprintf (file, _("cannot read EIHA\n"));
7484           return;
7485         }
7486       fprintf (file, _("Image activation:  (size=%u)\n"),
7487                (unsigned)bfd_getl32 (eiha.size));
7488       fprintf (file, _(" First address : 0x%08x 0x%08x\n"),
7489                (unsigned)bfd_getl32 (eiha.tfradr1_h),
7490                (unsigned)bfd_getl32 (eiha.tfradr1));
7491       fprintf (file, _(" Second address: 0x%08x 0x%08x\n"),
7492                (unsigned)bfd_getl32 (eiha.tfradr2_h),
7493                (unsigned)bfd_getl32 (eiha.tfradr2));
7494       fprintf (file, _(" Third address : 0x%08x 0x%08x\n"),
7495                (unsigned)bfd_getl32 (eiha.tfradr3_h),
7496                (unsigned)bfd_getl32 (eiha.tfradr3));
7497       fprintf (file, _(" Fourth address: 0x%08x 0x%08x\n"),
7498                (unsigned)bfd_getl32 (eiha.tfradr4_h),
7499                (unsigned)bfd_getl32 (eiha.tfradr4));
7500       fprintf (file, _(" Shared image  : 0x%08x 0x%08x\n"),
7501                (unsigned)bfd_getl32 (eiha.inishr_h),
7502                (unsigned)bfd_getl32 (eiha.inishr));
7503     }
7504   if (eihi_off != 0)
7505     {
7506       struct vms_eihi eihi;
7507
7508       if (bfd_seek (abfd, (file_ptr) eihi_off, SEEK_SET)
7509           || bfd_bread (&eihi, sizeof (eihi), abfd) != sizeof (eihi))
7510         {
7511           fprintf (file, _("cannot read EIHI\n"));
7512           return;
7513         }
7514       fprintf (file, _("Image identification: (major: %u, minor: %u)\n"),
7515                (unsigned)bfd_getl32 (eihi.majorid),
7516                (unsigned)bfd_getl32 (eihi.minorid));
7517       fprintf (file, _(" image name       : %.*s\n"),
7518                eihi.imgnam[0], eihi.imgnam + 1);
7519       fprintf (file, _(" link time        : %s\n"),
7520                vms_time_to_str (eihi.linktime));
7521       fprintf (file, _(" image ident      : %.*s\n"),
7522                eihi.imgid[0], eihi.imgid + 1);
7523       fprintf (file, _(" linker ident     : %.*s\n"),
7524                eihi.linkid[0], eihi.linkid + 1);
7525       fprintf (file, _(" image build ident: %.*s\n"),
7526                eihi.imgbid[0], eihi.imgbid + 1);
7527     }
7528   if (eihs_off != 0)
7529     {
7530       struct vms_eihs eihs;
7531
7532       if (bfd_seek (abfd, (file_ptr) eihs_off, SEEK_SET)
7533           || bfd_bread (&eihs, sizeof (eihs), abfd) != sizeof (eihs))
7534         {
7535           fprintf (file, _("cannot read EIHS\n"));
7536           return;
7537         }
7538       fprintf (file, _("Image symbol & debug table: (major: %u, minor: %u)\n"),
7539                (unsigned)bfd_getl32 (eihs.majorid),
7540                (unsigned)bfd_getl32 (eihs.minorid));
7541       dst_vbn = bfd_getl32 (eihs.dstvbn);
7542       dst_size = bfd_getl32 (eihs.dstsize);
7543       fprintf (file, _(" debug symbol table : vbn: %u, size: %u (0x%x)\n"),
7544                dst_vbn, dst_size, dst_size);
7545       gst_vbn = bfd_getl32 (eihs.gstvbn);
7546       gst_size = bfd_getl32 (eihs.gstsize);
7547       fprintf (file, _(" global symbol table: vbn: %u, records: %u\n"),
7548                gst_vbn, gst_size);
7549       dmt_vbn = bfd_getl32 (eihs.dmtvbn);
7550       dmt_size = bfd_getl32 (eihs.dmtsize);
7551       fprintf (file, _(" debug module table : vbn: %u, size: %u\n"),
7552                dmt_vbn, dmt_size);
7553     }
7554   while (eisd_off != 0)
7555     {
7556       struct vms_eisd eisd;
7557       unsigned int len;
7558
7559       while (1)
7560         {
7561           if (bfd_seek (abfd, (file_ptr) eisd_off, SEEK_SET)
7562               || bfd_bread (&eisd, sizeof (eisd), abfd) != sizeof (eisd))
7563             {
7564               fprintf (file, _("cannot read EISD\n"));
7565               return;
7566             }
7567           len = (unsigned)bfd_getl32 (eisd.eisdsize);
7568           if (len != (unsigned)-1)
7569             break;
7570
7571           /* Next block.  */
7572           eisd_off = (eisd_off + VMS_BLOCK_SIZE) & ~(VMS_BLOCK_SIZE - 1);
7573         }
7574       fprintf (file, _("Image section descriptor: (major: %u, minor: %u, "
7575                        "size: %u, offset: %u)\n"),
7576                (unsigned)bfd_getl32 (eisd.majorid),
7577                (unsigned)bfd_getl32 (eisd.minorid),
7578                len, eisd_off);
7579       if (len == 0)
7580         break;
7581       fprintf (file, _(" section: base: 0x%08x%08x size: 0x%08x\n"),
7582                (unsigned)bfd_getl32 (eisd.virt_addr + 4),
7583                (unsigned)bfd_getl32 (eisd.virt_addr + 0),
7584                (unsigned)bfd_getl32 (eisd.secsize));
7585       val = (unsigned)bfd_getl32 (eisd.flags);
7586       fprintf (file, _(" flags: 0x%04x"), val);
7587       if (val & EISD__M_GBL)
7588         fprintf (file, " GBL");
7589       if (val & EISD__M_CRF)
7590         fprintf (file, " CRF");
7591       if (val & EISD__M_DZRO)
7592         fprintf (file, " DZRO");
7593       if (val & EISD__M_WRT)
7594         fprintf (file, " WRT");
7595       if (val & EISD__M_INITALCODE)
7596         fprintf (file, " INITALCODE");
7597       if (val & EISD__M_BASED)
7598         fprintf (file, " BASED");
7599       if (val & EISD__M_FIXUPVEC)
7600         fprintf (file, " FIXUPVEC");
7601       if (val & EISD__M_RESIDENT)
7602         fprintf (file, " RESIDENT");
7603       if (val & EISD__M_VECTOR)
7604         fprintf (file, " VECTOR");
7605       if (val & EISD__M_PROTECT)
7606         fprintf (file, " PROTECT");
7607       if (val & EISD__M_LASTCLU)
7608         fprintf (file, " LASTCLU");
7609       if (val & EISD__M_EXE)
7610         fprintf (file, " EXE");
7611       if (val & EISD__M_NONSHRADR)
7612         fprintf (file, " NONSHRADR");
7613       if (val & EISD__M_QUAD_LENGTH)
7614         fprintf (file, " QUAD_LENGTH");
7615       if (val & EISD__M_ALLOC_64BIT)
7616         fprintf (file, " ALLOC_64BIT");
7617       fprintf (file, "\n");
7618       if (val & EISD__M_FIXUPVEC)
7619         {
7620           eiaf_vbn = bfd_getl32 (eisd.vbn);
7621           eiaf_size = bfd_getl32 (eisd.secsize);
7622         }
7623       fprintf (file, _(" vbn: %u, pfc: %u, matchctl: %u type: %u ("),
7624                (unsigned)bfd_getl32 (eisd.vbn),
7625                eisd.pfc, eisd.matchctl, eisd.type);
7626       switch (eisd.type)
7627         {
7628         case EISD__K_NORMAL:
7629           fputs (_("NORMAL"), file);
7630           break;
7631         case EISD__K_SHRFXD:
7632           fputs (_("SHRFXD"), file);
7633           break;
7634         case EISD__K_PRVFXD:
7635           fputs (_("PRVFXD"), file);
7636           break;
7637         case EISD__K_SHRPIC:
7638           fputs (_("SHRPIC"), file);
7639           break;
7640         case EISD__K_PRVPIC:
7641           fputs (_("PRVPIC"), file);
7642           break;
7643         case EISD__K_USRSTACK:
7644           fputs (_("USRSTACK"), file);
7645           break;
7646         default:
7647           fputs (_("*unknown*"), file);
7648           break;
7649         }
7650       fputs (_(")\n"), file);
7651       if (val & EISD__M_GBL)
7652         fprintf (file, _(" ident: 0x%08x, name: %.*s\n"),
7653                  (unsigned)bfd_getl32 (eisd.ident),
7654                  eisd.gblnam[0], eisd.gblnam + 1);
7655       eisd_off += len;
7656     }
7657
7658   if (dmt_vbn != 0)
7659     {
7660       if (bfd_seek (abfd, (file_ptr) (dmt_vbn - 1) * VMS_BLOCK_SIZE, SEEK_SET))
7661         {
7662           fprintf (file, _("cannot read DMT\n"));
7663           return;
7664         }
7665
7666       fprintf (file, _("Debug module table:\n"));
7667
7668       while (dmt_size > 0)
7669         {
7670           struct vms_dmt_header dmth;
7671           unsigned int count;
7672
7673           if (bfd_bread (&dmth, sizeof (dmth), abfd) != sizeof (dmth))
7674             {
7675               fprintf (file, _("cannot read DMT header\n"));
7676               return;
7677             }
7678           count = bfd_getl16 (dmth.psect_count);
7679           fprintf (file,
7680                    _(" module offset: 0x%08x, size: 0x%08x, (%u psects)\n"),
7681                    (unsigned)bfd_getl32 (dmth.modbeg),
7682                    (unsigned)bfd_getl32 (dmth.size), count);
7683           dmt_size -= sizeof (dmth);
7684           while (count > 0)
7685             {
7686               struct vms_dmt_psect dmtp;
7687
7688               if (bfd_bread (&dmtp, sizeof (dmtp), abfd) != sizeof (dmtp))
7689                 {
7690                   fprintf (file, _("cannot read DMT psect\n"));
7691                   return;
7692                 }
7693               fprintf (file, _("  psect start: 0x%08x, length: %u\n"),
7694                        (unsigned)bfd_getl32 (dmtp.start),
7695                        (unsigned)bfd_getl32 (dmtp.length));
7696               count--;
7697               dmt_size -= sizeof (dmtp);
7698             }
7699         }
7700     }
7701
7702   if (dst_vbn != 0)
7703     {
7704       if (bfd_seek (abfd, (file_ptr) (dst_vbn - 1) * VMS_BLOCK_SIZE, SEEK_SET))
7705         {
7706           fprintf (file, _("cannot read DST\n"));
7707           return;
7708         }
7709
7710       evax_bfd_print_dst (abfd, dst_size, file);
7711     }
7712   if (gst_vbn != 0)
7713     {
7714       if (bfd_seek (abfd, (file_ptr) (gst_vbn - 1) * VMS_BLOCK_SIZE, SEEK_SET))
7715         {
7716           fprintf (file, _("cannot read GST\n"));
7717           return;
7718         }
7719
7720       fprintf (file, _("Global symbol table:\n"));
7721       evax_bfd_print_eobj (abfd, file);
7722     }
7723   if (eiaf_vbn != 0)
7724     {
7725       unsigned char *buf;
7726       struct vms_eiaf *eiaf;
7727       unsigned int qrelfixoff;
7728       unsigned int lrelfixoff;
7729       unsigned int qdotadroff;
7730       unsigned int ldotadroff;
7731       unsigned int shrimgcnt;
7732       unsigned int shlstoff;
7733       unsigned int codeadroff;
7734       unsigned int lpfixoff;
7735       unsigned int chgprtoff;
7736
7737       buf = bfd_malloc (eiaf_size);
7738
7739       if (bfd_seek (abfd, (file_ptr) (eiaf_vbn - 1) * VMS_BLOCK_SIZE, SEEK_SET)
7740           || bfd_bread (buf, eiaf_size, abfd) != eiaf_size)
7741         {
7742           fprintf (file, _("cannot read EIHA\n"));
7743           free (buf);
7744           return;
7745         }
7746       eiaf = (struct vms_eiaf *)buf;
7747       fprintf (file,
7748                _("Image activator fixup: (major: %u, minor: %u)\n"),
7749                (unsigned)bfd_getl32 (eiaf->majorid),
7750                (unsigned)bfd_getl32 (eiaf->minorid));
7751       fprintf (file, _("  iaflink : 0x%08x %08x\n"),
7752                (unsigned)bfd_getl32 (eiaf->iaflink + 0),
7753                (unsigned)bfd_getl32 (eiaf->iaflink + 4));
7754       fprintf (file, _("  fixuplnk: 0x%08x %08x\n"),
7755                (unsigned)bfd_getl32 (eiaf->fixuplnk + 0),
7756                (unsigned)bfd_getl32 (eiaf->fixuplnk + 4));
7757       fprintf (file, _("  size : %u\n"),
7758                (unsigned)bfd_getl32 (eiaf->size));
7759       fprintf (file, _("  flags: 0x%08x\n"),
7760                (unsigned)bfd_getl32 (eiaf->flags));
7761       qrelfixoff = bfd_getl32 (eiaf->qrelfixoff);
7762       lrelfixoff = bfd_getl32 (eiaf->lrelfixoff);
7763       fprintf (file, _("  qrelfixoff: %5u, lrelfixoff: %5u\n"),
7764                qrelfixoff, lrelfixoff);
7765       qdotadroff = bfd_getl32 (eiaf->qdotadroff);
7766       ldotadroff = bfd_getl32 (eiaf->ldotadroff);
7767       fprintf (file, _("  qdotadroff: %5u, ldotadroff: %5u\n"),
7768                qdotadroff, ldotadroff);
7769       codeadroff = bfd_getl32 (eiaf->codeadroff);
7770       lpfixoff = bfd_getl32 (eiaf->lpfixoff);
7771       fprintf (file, _("  codeadroff: %5u, lpfixoff  : %5u\n"),
7772                codeadroff, lpfixoff);
7773       chgprtoff = bfd_getl32 (eiaf->chgprtoff);
7774       fprintf (file, _("  chgprtoff : %5u\n"), chgprtoff);
7775       shrimgcnt = bfd_getl32 (eiaf->shrimgcnt);
7776       shlstoff = bfd_getl32 (eiaf->shlstoff);
7777       fprintf (file, _("  shlstoff  : %5u, shrimgcnt : %5u\n"),
7778                shlstoff, shrimgcnt);
7779       fprintf (file, _("  shlextra  : %5u, permctx   : %5u\n"),
7780                (unsigned)bfd_getl32 (eiaf->shlextra),
7781                (unsigned)bfd_getl32 (eiaf->permctx));
7782       fprintf (file, _("  base_va : 0x%08x\n"),
7783                (unsigned)bfd_getl32 (eiaf->base_va));
7784       fprintf (file, _("  lppsbfixoff: %5u\n"),
7785                (unsigned)bfd_getl32 (eiaf->lppsbfixoff));
7786
7787       if (shlstoff)
7788         {
7789           struct vms_shl *shl = (struct vms_shl *)(buf + shlstoff);
7790           unsigned int j;
7791
7792           fprintf (file, _(" Shareable images:\n"));
7793           for (j = 0; j < shrimgcnt; j++, shl++)
7794             {
7795               fprintf (file,
7796                        _("  %u: size: %u, flags: 0x%02x, name: %.*s\n"),
7797                        j, shl->size, shl->flags,
7798                        shl->imgnam[0], shl->imgnam + 1);
7799             }
7800         }
7801       if (qrelfixoff != 0)
7802         {
7803           fprintf (file, _(" quad-word relocation fixups:\n"));
7804           evax_bfd_print_relocation_records (file, buf + qrelfixoff, 8);
7805         }
7806       if (lrelfixoff != 0)
7807         {
7808           fprintf (file, _(" long-word relocation fixups:\n"));
7809           evax_bfd_print_relocation_records (file, buf + lrelfixoff, 4);
7810         }
7811       if (qdotadroff != 0)
7812         {
7813           fprintf (file, _(" quad-word .address reference fixups:\n"));
7814           evax_bfd_print_address_fixups (file, buf + qdotadroff);
7815         }
7816       if (ldotadroff != 0)
7817         {
7818           fprintf (file, _(" long-word .address reference fixups:\n"));
7819           evax_bfd_print_address_fixups (file, buf + ldotadroff);
7820         }
7821       if (codeadroff != 0)
7822         {
7823           fprintf (file, _(" Code Address Reference Fixups:\n"));
7824           evax_bfd_print_reference_fixups (file, buf + codeadroff);
7825         }
7826       if (lpfixoff != 0)
7827         {
7828           fprintf (file, _(" Linkage Pairs Referece Fixups:\n"));
7829           evax_bfd_print_reference_fixups (file, buf + lpfixoff);
7830         }
7831       if (chgprtoff)
7832         {
7833           unsigned int count = (unsigned)bfd_getl32 (buf + chgprtoff);
7834           struct vms_eicp *eicp = (struct vms_eicp *)(buf + chgprtoff + 4);
7835           unsigned int j;
7836
7837           fprintf (file, _(" Change Protection (%u entries):\n"), count);
7838           for (j = 0; j < count; j++, eicp++)
7839             {
7840               unsigned int prot = bfd_getl32 (eicp->newprt);
7841               fprintf (file,
7842                        _("  base: 0x%08x %08x, size: 0x%08x, prot: 0x%08x "),
7843                        (unsigned)bfd_getl32 (eicp->baseva + 4),
7844                        (unsigned)bfd_getl32 (eicp->baseva + 0),
7845                        (unsigned)bfd_getl32 (eicp->size),
7846                        (unsigned)bfd_getl32 (eicp->newprt));
7847               switch (prot)
7848                 {
7849                 case PRT__C_NA:
7850                   fprintf (file, "NA");
7851                   break;
7852                 case PRT__C_RESERVED:
7853                   fprintf (file, "RES");
7854                   break;
7855                 case PRT__C_KW:
7856                   fprintf (file, "KW");
7857                   break;
7858                 case PRT__C_KR:
7859                   fprintf (file, "KR");
7860                   break;
7861                 case PRT__C_UW:
7862                   fprintf (file, "UW");
7863                   break;
7864                 case PRT__C_EW:
7865                   fprintf (file, "EW");
7866                   break;
7867                 case PRT__C_ERKW:
7868                   fprintf (file, "ERKW");
7869                   break;
7870                 case PRT__C_ER:
7871                   fprintf (file, "ER");
7872                   break;
7873                 case PRT__C_SW:
7874                   fprintf (file, "SW");
7875                   break;
7876                 case PRT__C_SREW:
7877                   fprintf (file, "SREW");
7878                   break;
7879                 case PRT__C_SRKW:
7880                   fprintf (file, "SRKW");
7881                   break;
7882                 case PRT__C_SR:
7883                   fprintf (file, "SR");
7884                   break;
7885                 case PRT__C_URSW:
7886                   fprintf (file, "URSW");
7887                   break;
7888                 case PRT__C_UREW:
7889                   fprintf (file, "UREW");
7890                   break;
7891                 case PRT__C_URKW:
7892                   fprintf (file, "URKW");
7893                   break;
7894                 case PRT__C_UR:
7895                   fprintf (file, "UR");
7896                   break;
7897                 default:
7898                   fputs ("??", file);
7899                   break;
7900                 }
7901               fputc ('\n', file);
7902             }
7903         }
7904       free (buf);
7905     }
7906 }
7907
7908 static bfd_boolean
7909 vms_bfd_print_private_bfd_data (bfd *abfd, void *ptr)
7910 {
7911   FILE *file = (FILE *)ptr;
7912
7913   if (bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
7914     evax_bfd_print_image (abfd, file);
7915   else
7916     {
7917       if (bfd_seek (abfd, 0, SEEK_SET))
7918         return FALSE;
7919       evax_bfd_print_eobj (abfd, file);
7920     }
7921   return TRUE;
7922 }
7923 \f
7924 /* Linking.  */
7925
7926 /* Slurp ETIR/EDBG/ETBT VMS object records.  */
7927
7928 static bfd_boolean
7929 alpha_vms_read_sections_content (bfd *abfd, struct bfd_link_info *info)
7930 {
7931   asection *cur_section;
7932   file_ptr cur_offset;
7933   asection *dst_section;
7934   file_ptr dst_offset;
7935
7936   if (bfd_seek (abfd, 0, SEEK_SET) != 0)
7937     return FALSE;
7938
7939   cur_section = NULL;
7940   cur_offset = 0;
7941
7942   dst_section = PRIV (dst_section);
7943   dst_offset = 0;
7944   if (info)
7945     {
7946       if (info->strip == strip_all || info->strip == strip_debugger)
7947         {
7948           /* Discard the DST section.  */
7949           dst_offset = 0;
7950           dst_section = NULL;
7951         }
7952       else if (dst_section)
7953         {
7954           dst_offset = dst_section->output_offset;
7955           dst_section = dst_section->output_section;
7956         }
7957     }
7958
7959   while (1)
7960     {
7961       int type;
7962       bfd_boolean res;
7963
7964       type = _bfd_vms_get_object_record (abfd);
7965       if (type < 0)
7966         {
7967           vms_debug2 ((2, "next_record failed\n"));
7968           return FALSE;
7969         }
7970       switch (type)
7971         {
7972         case EOBJ__C_ETIR:
7973           PRIV (image_section) = cur_section;
7974           PRIV (image_offset) = cur_offset;
7975           res = _bfd_vms_slurp_etir (abfd, info);
7976           cur_section = PRIV (image_section);
7977           cur_offset = PRIV (image_offset);
7978           break;
7979         case EOBJ__C_EDBG:
7980         case EOBJ__C_ETBT:
7981           if (dst_section == NULL)
7982             continue;
7983           PRIV (image_section) = dst_section;
7984           PRIV (image_offset) = dst_offset;
7985           res = _bfd_vms_slurp_etir (abfd, info);
7986           dst_offset = PRIV (image_offset);
7987           break;
7988         case EOBJ__C_EEOM:
7989           return TRUE;
7990         default:
7991           continue;
7992         }
7993       if (!res)
7994         {
7995           vms_debug2 ((2, "slurp eobj type %d failed\n", type));
7996           return FALSE;
7997         }
7998     }
7999 }
8000
8001 static int
8002 alpha_vms_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
8003                           struct bfd_link_info *info ATTRIBUTE_UNUSED)
8004 {
8005   return 0;
8006 }
8007
8008 /* Add a linkage pair fixup at address SECT + OFFSET to SHLIB. */
8009
8010 static void
8011 alpha_vms_add_fixup_lp (struct bfd_link_info *info, bfd *src, bfd *shlib)
8012 {
8013   struct alpha_vms_shlib_el *sl;
8014   asection *sect = PRIV2 (src, image_section);
8015   file_ptr offset = PRIV2 (src, image_offset);
8016
8017   sl = &VEC_EL (alpha_vms_link_hash (info)->shrlibs,
8018                 struct alpha_vms_shlib_el, PRIV2 (shlib, shr_index));
8019   sl->has_fixups = TRUE;
8020   VEC_APPEND_EL (sl->lp, bfd_vma,
8021                  sect->output_section->vma + sect->output_offset + offset);
8022 }
8023
8024 static void
8025 alpha_vms_add_fixup_ca (struct bfd_link_info *info, bfd *src, bfd *shlib)
8026 {
8027   struct alpha_vms_shlib_el *sl;
8028   asection *sect = PRIV2 (src, image_section);
8029   file_ptr offset = PRIV2 (src, image_offset);
8030
8031   sl = &VEC_EL (alpha_vms_link_hash (info)->shrlibs,
8032                 struct alpha_vms_shlib_el, PRIV2 (shlib, shr_index));
8033   sl->has_fixups = TRUE;
8034   VEC_APPEND_EL (sl->ca, bfd_vma,
8035                  sect->output_section->vma + sect->output_offset + offset);
8036 }
8037
8038 static void
8039 alpha_vms_add_fixup_qr (struct bfd_link_info *info, bfd *src,
8040                         bfd *shlib, bfd_vma vec)
8041 {
8042   struct alpha_vms_shlib_el *sl;
8043   struct alpha_vms_vma_ref *r;
8044   asection *sect = PRIV2 (src, image_section);
8045   file_ptr offset = PRIV2 (src, image_offset);
8046
8047   sl = &VEC_EL (alpha_vms_link_hash (info)->shrlibs,
8048                 struct alpha_vms_shlib_el, PRIV2 (shlib, shr_index));
8049   sl->has_fixups = TRUE;
8050   r = VEC_APPEND (sl->qr, struct alpha_vms_vma_ref);
8051   r->vma = sect->output_section->vma + sect->output_offset + offset;
8052   r->ref = vec;
8053 }
8054
8055 static void
8056 alpha_vms_add_lw_fixup (struct bfd_link_info *info ATTRIBUTE_UNUSED,
8057                         unsigned int shr ATTRIBUTE_UNUSED,
8058                         bfd_vma vec ATTRIBUTE_UNUSED)
8059 {
8060   abort ();
8061 }
8062
8063 #if 0
8064 static void
8065 alpha_vms_add_qw_fixup (struct bfd_link_info *info ATTRIBUTE_UNUSED,
8066                         unsigned int shr ATTRIBUTE_UNUSED,
8067                         bfd_vma vec ATTRIBUTE_UNUSED)
8068 {
8069   abort ();
8070 }
8071 #endif
8072
8073 static void
8074 alpha_vms_add_lw_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED)
8075 {
8076 }
8077
8078 static void
8079 alpha_vms_add_qw_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED)
8080 {
8081 }
8082
8083 static struct bfd_hash_entry *
8084 alpha_vms_link_hash_newfunc (struct bfd_hash_entry *entry,
8085                              struct bfd_hash_table *table,
8086                              const char *string)
8087 {
8088   struct alpha_vms_link_hash_entry *ret =
8089     (struct alpha_vms_link_hash_entry *) entry;
8090
8091   /* Allocate the structure if it has not already been allocated by a
8092      subclass.  */
8093   if (ret == NULL)
8094     ret = ((struct alpha_vms_link_hash_entry *)
8095            bfd_hash_allocate (table,
8096                               sizeof (struct alpha_vms_link_hash_entry)));
8097   if (ret == NULL)
8098     return NULL;
8099
8100   /* Call the allocation method of the superclass.  */
8101   ret = ((struct alpha_vms_link_hash_entry *)
8102          _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
8103                                  table, string));
8104
8105   ret->sym = NULL;
8106
8107   return (struct bfd_hash_entry *) ret;
8108 }
8109
8110 /* Create an Alpha/VMS link hash table.  */
8111
8112 static struct bfd_link_hash_table *
8113 alpha_vms_bfd_link_hash_table_create (bfd *abfd)
8114 {
8115   struct alpha_vms_link_hash_table *ret;
8116   bfd_size_type amt = sizeof (struct alpha_vms_link_hash_table);
8117
8118   ret = (struct alpha_vms_link_hash_table *) bfd_malloc (amt);
8119   if (ret == NULL)
8120     return NULL;
8121   if (!_bfd_link_hash_table_init (&ret->root, abfd,
8122                                   alpha_vms_link_hash_newfunc,
8123                                   sizeof (struct alpha_vms_link_hash_entry)))
8124     {
8125       free (ret);
8126       return NULL;
8127     }
8128
8129   VEC_INIT (ret->shrlibs);
8130   ret->fixup = NULL;
8131
8132   return &ret->root;
8133 }
8134
8135 static bfd_boolean
8136 alpha_vms_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
8137 {
8138   unsigned int i;
8139
8140   for (i = 0; i < PRIV (gsd_sym_count); i++)
8141     {
8142       struct vms_symbol_entry *e = PRIV (syms)[i];
8143       struct alpha_vms_link_hash_entry *h;
8144       struct bfd_link_hash_entry *h_root;
8145       asymbol sym;
8146
8147       if (!alpha_vms_convert_symbol (abfd, e, &sym))
8148         return FALSE;
8149
8150       if ((e->flags & EGSY__V_DEF) && abfd->selective_search)
8151         {
8152           /* In selective_search mode, only add definition that are
8153              required.  */
8154           h = (struct alpha_vms_link_hash_entry *)bfd_link_hash_lookup
8155             (info->hash, sym.name, FALSE, FALSE, FALSE);
8156           if (h == NULL || h->root.type != bfd_link_hash_undefined)
8157             continue;
8158         }
8159       else
8160         h = NULL;
8161
8162       h_root = (struct bfd_link_hash_entry *) h;
8163       if (_bfd_generic_link_add_one_symbol
8164           (info, abfd, sym.name, sym.flags, sym.section, sym.value,
8165            NULL, FALSE, FALSE, &h_root) == FALSE)
8166         return FALSE;
8167       h = (struct alpha_vms_link_hash_entry *) h_root;
8168
8169       if ((e->flags & EGSY__V_DEF)
8170           && h->sym == NULL
8171           && abfd->xvec == info->output_bfd->xvec)
8172         h->sym = e;
8173     }
8174
8175   if (abfd->flags & DYNAMIC)
8176     {
8177       struct alpha_vms_shlib_el *shlib;
8178
8179       /* We do not want to include any of the sections in a dynamic
8180          object in the output file.  See comment in elflink.c.  */
8181       bfd_section_list_clear (abfd);
8182
8183       shlib = VEC_APPEND (alpha_vms_link_hash (info)->shrlibs,
8184                           struct alpha_vms_shlib_el);
8185       shlib->abfd = abfd;
8186       VEC_INIT (shlib->ca);
8187       VEC_INIT (shlib->lp);
8188       VEC_INIT (shlib->qr);
8189       PRIV (shr_index) = VEC_COUNT (alpha_vms_link_hash (info)->shrlibs) - 1;
8190     }
8191
8192   return TRUE;
8193 }
8194
8195 static bfd_boolean
8196 alpha_vms_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
8197 {
8198   int pass;
8199   struct bfd_link_hash_entry **pundef;
8200   struct bfd_link_hash_entry **next_pundef;
8201
8202   /* We only accept VMS libraries.  */
8203   if (info->output_bfd->xvec != abfd->xvec)
8204     {
8205       bfd_set_error (bfd_error_wrong_format);
8206       return FALSE;
8207     }
8208
8209   /* The archive_pass field in the archive itself is used to
8210      initialize PASS, since we may search the same archive multiple
8211      times.  */
8212   pass = ++abfd->archive_pass;
8213
8214   /* Look through the list of undefined symbols.  */
8215   for (pundef = &info->hash->undefs; *pundef != NULL; pundef = next_pundef)
8216     {
8217       struct bfd_link_hash_entry *h;
8218       symindex symidx;
8219       bfd *element;
8220       bfd *orig_element;
8221
8222       h = *pundef;
8223       next_pundef = &(*pundef)->u.undef.next;
8224
8225       /* When a symbol is defined, it is not necessarily removed from
8226          the list.  */
8227       if (h->type != bfd_link_hash_undefined
8228           && h->type != bfd_link_hash_common)
8229         {
8230           /* Remove this entry from the list, for general cleanliness
8231              and because we are going to look through the list again
8232              if we search any more libraries.  We can't remove the
8233              entry if it is the tail, because that would lose any
8234              entries we add to the list later on.  */
8235           if (*pundef != info->hash->undefs_tail)
8236             {
8237               *pundef = *next_pundef;
8238               next_pundef = pundef;
8239             }
8240           continue;
8241         }
8242
8243       /* Look for this symbol in the archive hash table.  */
8244       symidx = _bfd_vms_lib_find_symbol (abfd, h->root.string);
8245       if (symidx == BFD_NO_MORE_SYMBOLS)
8246         {
8247           /* Nothing in this slot.  */
8248           continue;
8249         }
8250
8251       element = bfd_get_elt_at_index (abfd, symidx);
8252       if (element == NULL)
8253         return FALSE;
8254
8255       if (element->archive_pass == -1 || element->archive_pass == pass)
8256         continue;
8257
8258       if (! bfd_check_format (element, bfd_object))
8259         {
8260           element->archive_pass = -1;
8261           return FALSE;
8262         }
8263
8264       orig_element = element;
8265       if (bfd_is_thin_archive (abfd))
8266         {
8267           element = _bfd_vms_lib_get_imagelib_file (element);
8268           if (element == NULL || !bfd_check_format (element, bfd_object))
8269             {
8270               orig_element->archive_pass = -1;
8271               return FALSE;
8272             }
8273         }
8274
8275       /* Unlike the generic linker, we know that this element provides
8276          a definition for an undefined symbol and we know that we want
8277          to include it.  We don't need to check anything.  */
8278       if (! (*info->callbacks->add_archive_element) (info, element,
8279                                                      h->root.string))
8280         return FALSE;
8281       if (! alpha_vms_link_add_object_symbols (element, info))
8282         return FALSE;
8283
8284       orig_element->archive_pass = pass;
8285     }
8286
8287   return TRUE;
8288 }
8289
8290 static bfd_boolean
8291 alpha_vms_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
8292 {
8293   switch (bfd_get_format (abfd))
8294     {
8295     case bfd_object:
8296       vms_debug2 ((2, "vms_link_add_symbols for object %s\n",
8297                    abfd->filename));
8298       return alpha_vms_link_add_object_symbols (abfd, info);
8299       break;
8300     case bfd_archive:
8301       vms_debug2 ((2, "vms_link_add_symbols for archive %s\n",
8302                    abfd->filename));
8303       return alpha_vms_link_add_archive_symbols (abfd, info);
8304       break;
8305     default:
8306       bfd_set_error (bfd_error_wrong_format);
8307       return FALSE;
8308     }
8309 }
8310
8311 static bfd_boolean
8312 alpha_vms_build_fixups (struct bfd_link_info *info)
8313 {
8314   struct alpha_vms_link_hash_table *t = alpha_vms_link_hash (info);
8315   unsigned char *content;
8316   unsigned int i;
8317   unsigned int sz = 0;
8318   unsigned int lp_sz = 0;
8319   unsigned int ca_sz = 0;
8320   unsigned int qr_sz = 0;
8321   unsigned int shrimg_cnt = 0;
8322   struct vms_eiaf *eiaf;
8323   unsigned int off;
8324   asection *sec;
8325
8326   /* Shared libraries.  */
8327   for (i = 0; i < VEC_COUNT (t->shrlibs); i++)
8328     {
8329       struct alpha_vms_shlib_el *shlib;
8330
8331       shlib = &VEC_EL (t->shrlibs, struct alpha_vms_shlib_el, i);
8332
8333       if (!shlib->has_fixups)
8334         continue;
8335
8336       shrimg_cnt++;
8337
8338       if (VEC_COUNT (shlib->ca) > 0)
8339         {
8340           /* Header + entries.  */
8341           ca_sz += 8;
8342           ca_sz += VEC_COUNT (shlib->ca) * 4;
8343         }
8344       if (VEC_COUNT (shlib->lp) > 0)
8345         {
8346           /* Header + entries.  */
8347           lp_sz += 8;
8348           lp_sz += VEC_COUNT (shlib->lp) * 4;
8349         }
8350       if (VEC_COUNT (shlib->qr) > 0)
8351         {
8352           /* Header + entries.  */
8353           qr_sz += 8;
8354           qr_sz += VEC_COUNT (shlib->qr) * 8;
8355         }
8356     }
8357   /* Add markers.  */
8358   if (ca_sz > 0)
8359     ca_sz += 8;
8360   if (lp_sz > 0)
8361     lp_sz += 8;
8362   if (qr_sz > 0)
8363     qr_sz += 8;
8364
8365   /* Finish now if there is no content.  */
8366   if (ca_sz + lp_sz + qr_sz == 0)
8367     return TRUE;
8368
8369   /* Allocate section content (round-up size)  */
8370   sz = sizeof (struct vms_eiaf) + shrimg_cnt * sizeof (struct vms_shl)
8371     + ca_sz + lp_sz + qr_sz;
8372   sz = (sz + VMS_BLOCK_SIZE - 1) & ~(VMS_BLOCK_SIZE - 1);
8373   content = bfd_zalloc (info->output_bfd, sz);
8374   if (content == NULL)
8375     return FALSE;
8376
8377   sec = alpha_vms_link_hash (info)->fixup;
8378   sec->contents = content;
8379   sec->size = sz;
8380
8381   eiaf = (struct vms_eiaf *)content;
8382   off = sizeof (struct vms_eiaf);
8383   bfd_putl32 (0, eiaf->majorid);
8384   bfd_putl32 (0, eiaf->minorid);
8385   bfd_putl32 (0, eiaf->iaflink);
8386   bfd_putl32 (0, eiaf->fixuplnk);
8387   bfd_putl32 (sizeof (struct vms_eiaf), eiaf->size);
8388   bfd_putl32 (0, eiaf->flags);
8389   bfd_putl32 (0, eiaf->qrelfixoff);
8390   bfd_putl32 (0, eiaf->lrelfixoff);
8391   bfd_putl32 (0, eiaf->qdotadroff);
8392   bfd_putl32 (0, eiaf->ldotadroff);
8393   bfd_putl32 (0, eiaf->codeadroff);
8394   bfd_putl32 (0, eiaf->lpfixoff);
8395   bfd_putl32 (0, eiaf->chgprtoff);
8396   bfd_putl32 (shrimg_cnt ? off : 0, eiaf->shlstoff);
8397   bfd_putl32 (shrimg_cnt, eiaf->shrimgcnt);
8398   bfd_putl32 (0, eiaf->shlextra);
8399   bfd_putl32 (0, eiaf->permctx);
8400   bfd_putl32 (0, eiaf->base_va);
8401   bfd_putl32 (0, eiaf->lppsbfixoff);
8402
8403   if (shrimg_cnt)
8404     {
8405       shrimg_cnt = 0;
8406
8407       /* Write shl.  */
8408       for (i = 0; i < VEC_COUNT (t->shrlibs); i++)
8409         {
8410           struct alpha_vms_shlib_el *shlib;
8411           struct vms_shl *shl;
8412
8413           shlib = &VEC_EL (t->shrlibs, struct alpha_vms_shlib_el, i);
8414
8415           if (!shlib->has_fixups)
8416             continue;
8417
8418           /* Renumber shared images.  */
8419           PRIV2 (shlib->abfd, shr_index) = shrimg_cnt++;
8420
8421           shl = (struct vms_shl *)(content + off);
8422           bfd_putl32 (0, shl->baseva);
8423           bfd_putl32 (0, shl->shlptr);
8424           bfd_putl32 (0, shl->ident);
8425           bfd_putl32 (0, shl->permctx);
8426           shl->size = sizeof (struct vms_shl);
8427           bfd_putl16 (0, shl->fill_1);
8428           shl->flags = 0;
8429           bfd_putl32 (0, shl->icb);
8430           shl->imgnam[0] = strlen (PRIV2 (shlib->abfd, hdr_data.hdr_t_name));
8431           memcpy (shl->imgnam + 1, PRIV2 (shlib->abfd, hdr_data.hdr_t_name),
8432                   shl->imgnam[0]);
8433
8434           off += sizeof (struct vms_shl);
8435         }
8436
8437       /* CA fixups.  */
8438       if (ca_sz != 0)
8439         {
8440           bfd_putl32 (off, eiaf->codeadroff);
8441
8442           for (i = 0; i < VEC_COUNT (t->shrlibs); i++)
8443             {
8444               struct alpha_vms_shlib_el *shlib;
8445               unsigned int j;
8446
8447               shlib = &VEC_EL (t->shrlibs, struct alpha_vms_shlib_el, i);
8448
8449               if (VEC_COUNT (shlib->ca) == 0)
8450                 continue;
8451
8452               bfd_putl32 (VEC_COUNT (shlib->ca), content + off);
8453               bfd_putl32 (PRIV2 (shlib->abfd, shr_index), content + off + 4);
8454               off += 8;
8455
8456               for (j = 0; j < VEC_COUNT (shlib->ca); j++)
8457                 {
8458                   bfd_putl32 (VEC_EL (shlib->ca, bfd_vma, j) - t->base_addr,
8459                               content + off);
8460                   off += 4;
8461                 }
8462             }
8463
8464           bfd_putl32 (0, content + off);
8465           bfd_putl32 (0, content + off + 4);
8466           off += 8;
8467         }
8468
8469       /* LP fixups.  */
8470       if (lp_sz != 0)
8471         {
8472           bfd_putl32 (off, eiaf->lpfixoff);
8473
8474           for (i = 0; i < VEC_COUNT (t->shrlibs); i++)
8475             {
8476               struct alpha_vms_shlib_el *shlib;
8477               unsigned int j;
8478
8479               shlib = &VEC_EL (t->shrlibs, struct alpha_vms_shlib_el, i);
8480
8481               if (VEC_COUNT (shlib->lp) == 0)
8482                 continue;
8483
8484               bfd_putl32 (VEC_COUNT (shlib->lp), content + off);
8485               bfd_putl32 (PRIV2 (shlib->abfd, shr_index), content + off + 4);
8486               off += 8;
8487
8488               for (j = 0; j < VEC_COUNT (shlib->lp); j++)
8489                 {
8490                   bfd_putl32 (VEC_EL (shlib->lp, bfd_vma, j) - t->base_addr,
8491                               content + off);
8492                   off += 4;
8493                 }
8494             }
8495
8496           bfd_putl32 (0, content + off);
8497           bfd_putl32 (0, content + off + 4);
8498           off += 8;
8499         }
8500
8501       /* QR fixups.  */
8502       if (qr_sz != 0)
8503         {
8504           bfd_putl32 (off, eiaf->qdotadroff);
8505
8506           for (i = 0; i < VEC_COUNT (t->shrlibs); i++)
8507             {
8508               struct alpha_vms_shlib_el *shlib;
8509               unsigned int j;
8510
8511               shlib = &VEC_EL (t->shrlibs, struct alpha_vms_shlib_el, i);
8512
8513               if (VEC_COUNT (shlib->qr) == 0)
8514                 continue;
8515
8516               bfd_putl32 (VEC_COUNT (shlib->qr), content + off);
8517               bfd_putl32 (PRIV2 (shlib->abfd, shr_index), content + off + 4);
8518               off += 8;
8519
8520               for (j = 0; j < VEC_COUNT (shlib->qr); j++)
8521                 {
8522                   struct alpha_vms_vma_ref *r;
8523                   r = &VEC_EL (shlib->qr, struct alpha_vms_vma_ref, j);
8524                   bfd_putl32 (r->vma - t->base_addr, content + off);
8525                   bfd_putl32 (r->ref, content + off + 4);
8526                   off += 8;
8527                 }
8528             }
8529
8530           bfd_putl32 (0, content + off);
8531           bfd_putl32 (0, content + off + 4);
8532           off += 8;
8533         }
8534
8535       /* CA fixups.  */
8536     }
8537
8538   return TRUE;
8539 }
8540
8541 /* Called by bfd_link_hash_traverse to fill the symbol table.
8542    Return FALSE in case of failure.  */
8543
8544 static bfd_boolean
8545 alpha_vms_link_output_symbol (struct bfd_link_hash_entry *hc, void *infov)
8546 {
8547   struct bfd_link_info *info = (struct bfd_link_info *)infov;
8548   struct alpha_vms_link_hash_entry *h = (struct alpha_vms_link_hash_entry *)hc;
8549   struct vms_symbol_entry *sym;
8550
8551   switch (h->root.type)
8552     {
8553     case bfd_link_hash_new:
8554     case bfd_link_hash_undefined:
8555       abort ();
8556     case bfd_link_hash_undefweak:
8557       return TRUE;
8558     case bfd_link_hash_defined:
8559     case bfd_link_hash_defweak:
8560       {
8561         asection *sec = h->root.u.def.section;
8562
8563         /* FIXME: this is certainly a symbol from a dynamic library.  */
8564         if (bfd_is_abs_section (sec))
8565           return TRUE;
8566
8567         if (sec->owner->flags & DYNAMIC)
8568           return TRUE;
8569       }
8570       break;
8571     case bfd_link_hash_common:
8572       break;
8573     case bfd_link_hash_indirect:
8574     case bfd_link_hash_warning:
8575       return TRUE;
8576     }
8577
8578   /* Do not write not kept symbols.  */
8579   if (info->strip == strip_some
8580       && bfd_hash_lookup (info->keep_hash, h->root.root.string,
8581                           FALSE, FALSE) != NULL)
8582     return TRUE;
8583
8584   if (h->sym == NULL)
8585     {
8586       /* This symbol doesn't come from a VMS object.  So we suppose it is
8587          a data.  */
8588       int len = strlen (h->root.root.string);
8589
8590       sym = (struct vms_symbol_entry *)bfd_zalloc (info->output_bfd,
8591                                                    sizeof (*sym) + len);
8592       if (sym == NULL)
8593         abort ();
8594       sym->namelen = len;
8595       memcpy (sym->name, h->root.root.string, len);
8596       sym->name[len] = 0;
8597       sym->owner = info->output_bfd;
8598
8599       sym->typ = EGSD__C_SYMG;
8600       sym->data_type = 0;
8601       sym->flags = EGSY__V_DEF | EGSY__V_REL;
8602       sym->symbol_vector = h->root.u.def.value;
8603       sym->section = h->root.u.def.section;
8604       sym->value = h->root.u.def.value;
8605     }
8606   else
8607     sym = h->sym;
8608
8609   if (!add_symbol_entry (info->output_bfd, sym))
8610     return FALSE;
8611
8612   return TRUE;
8613 }
8614
8615 static bfd_boolean
8616 alpha_vms_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
8617 {
8618   asection *o;
8619   struct bfd_link_order *p;
8620   bfd *sub;
8621   asection *fixupsec;
8622   bfd_vma base_addr;
8623   bfd_vma last_addr;
8624   asection *dst;
8625   asection *dmt;
8626
8627   bfd_get_outsymbols (abfd) = NULL;
8628   bfd_get_symcount (abfd) = 0;
8629
8630   /* Mark all sections which will be included in the output file.  */
8631   for (o = abfd->sections; o != NULL; o = o->next)
8632     for (p = o->map_head.link_order; p != NULL; p = p->next)
8633       if (p->type == bfd_indirect_link_order)
8634         p->u.indirect.section->linker_mark = TRUE;
8635
8636 #if 0
8637   /* Handle all the link order information for the sections.  */
8638   for (o = abfd->sections; o != NULL; o = o->next)
8639     {
8640       printf ("For section %s (at 0x%08x, flags=0x%08x):\n",
8641               o->name, (unsigned)o->vma, (unsigned)o->flags);
8642
8643       for (p = o->map_head.link_order; p != NULL; p = p->next)
8644         {
8645           printf (" at 0x%08x - 0x%08x: ",
8646                   (unsigned)p->offset, (unsigned)(p->offset + p->size - 1));
8647           switch (p->type)
8648             {
8649             case bfd_section_reloc_link_order:
8650             case bfd_symbol_reloc_link_order:
8651               printf ("  section/symbol reloc\n");
8652               break;
8653             case bfd_indirect_link_order:
8654               printf ("  section %s of %s\n",
8655                       p->u.indirect.section->name,
8656                       p->u.indirect.section->owner->filename);
8657               break;
8658             case bfd_data_link_order:
8659               printf ("  explicit data\n");
8660               break;
8661             default:
8662               printf ("  *unknown* type %u\n", p->type);
8663               break;
8664             }
8665         }
8666     }
8667 #endif
8668
8669   /* Generate the symbol table.  */
8670   BFD_ASSERT (PRIV (syms) == NULL);
8671   if (info->strip != strip_all)
8672     bfd_link_hash_traverse (info->hash, alpha_vms_link_output_symbol, info);
8673
8674   /* Find the entry point.  */
8675   if (bfd_get_start_address (abfd) == 0)
8676     {
8677       bfd *startbfd = NULL;
8678
8679       for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8680         {
8681           /* Consider only VMS object files.  */
8682           if (sub->xvec != abfd->xvec)
8683             continue;
8684
8685           if (!PRIV2 (sub, eom_data).eom_has_transfer)
8686             continue;
8687           if ((PRIV2 (sub, eom_data).eom_b_tfrflg & EEOM__M_WKTFR) && startbfd)
8688             continue;
8689           if (startbfd != NULL
8690               && !(PRIV2 (sub, eom_data).eom_b_tfrflg & EEOM__M_WKTFR))
8691             {
8692               (*info->callbacks->einfo)
8693                 (_("%P: multiple entry points: in modules %B and %B\n"),
8694                  startbfd, sub);
8695               continue;
8696             }
8697           startbfd = sub;
8698         }
8699
8700       if (startbfd)
8701         {
8702           unsigned int ps_idx = PRIV2 (startbfd, eom_data).eom_l_psindx;
8703           bfd_vma tfradr = PRIV2 (startbfd, eom_data).eom_l_tfradr;
8704           asection *sec;
8705
8706           sec = PRIV2 (startbfd, sections)[ps_idx];
8707
8708           bfd_set_start_address
8709             (abfd, sec->output_section->vma + sec->output_offset + tfradr);
8710         }
8711     }
8712
8713   /* Set transfer addresses.  */
8714   {
8715     int i;
8716     struct bfd_link_hash_entry *h;
8717
8718     i = 0;
8719     PRIV (transfer_address[i++]) = 0xffffffff00000340ULL;       /* SYS$IMGACT */
8720     h = bfd_link_hash_lookup (info->hash, "LIB$INITIALIZE", FALSE, FALSE, TRUE);
8721     if (h != NULL && h->type == bfd_link_hash_defined)
8722       PRIV (transfer_address[i++]) =
8723         alpha_vms_get_sym_value (h->u.def.section, h->u.def.value);
8724     PRIV (transfer_address[i++]) = bfd_get_start_address (abfd);
8725     while (i < 4)
8726       PRIV (transfer_address[i++]) = 0;
8727   }
8728
8729   /* Allocate contents.  */
8730   base_addr = (bfd_vma)-1;
8731   last_addr = 0;
8732   for (o = abfd->sections; o != NULL; o = o->next)
8733     {
8734       if (o->flags & SEC_HAS_CONTENTS)
8735         {
8736           o->contents = bfd_alloc (abfd, o->size);
8737           if (o->contents == NULL)
8738             return FALSE;
8739         }
8740       if (o->flags & SEC_LOAD)
8741         {
8742           if (o->vma < base_addr)
8743             base_addr = o->vma;
8744           if (o->vma + o->size > last_addr)
8745             last_addr = o->vma + o->size;
8746         }
8747     }
8748
8749   /* Create the fixup section.  */
8750   fixupsec = bfd_make_section_anyway_with_flags
8751     (info->output_bfd, "$FIXUP$",
8752      SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_LINKER_CREATED);
8753   if (fixupsec == NULL)
8754     return FALSE;
8755   last_addr = (last_addr + 0xffff) & ~0xffff;
8756   fixupsec->vma = last_addr;
8757
8758   alpha_vms_link_hash (info)->fixup = fixupsec;
8759   alpha_vms_link_hash (info)->base_addr = base_addr;
8760
8761   /* Create the DMT section, if necessary.  */
8762   BFD_ASSERT (PRIV (dst_section) == NULL);
8763   dst = bfd_get_section_by_name (abfd, "$DST$");
8764   if (dst != NULL && dst->size == 0)
8765     dst = NULL;
8766   if (dst != NULL)
8767     {
8768       PRIV (dst_section) = dst;
8769       dmt = bfd_make_section_anyway_with_flags
8770         (info->output_bfd, "$DMT$",
8771          SEC_DEBUGGING | SEC_HAS_CONTENTS | SEC_LINKER_CREATED);
8772       if (dmt == NULL)
8773         return FALSE;
8774     }
8775   else
8776     dmt = NULL;
8777
8778   /* Read all sections from the inputs.  */
8779   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8780     {
8781       if (sub->flags & DYNAMIC)
8782         {
8783           alpha_vms_create_eisd_for_shared (abfd, sub);
8784           continue;
8785         }
8786
8787       if (!alpha_vms_read_sections_content (sub, info))
8788         return FALSE;
8789     }
8790
8791   /* Handle all the link order information for the sections.
8792      Note: past this point, it is not possible to create new sections.  */
8793   for (o = abfd->sections; o != NULL; o = o->next)
8794     {
8795       for (p = o->map_head.link_order; p != NULL; p = p->next)
8796         {
8797           switch (p->type)
8798             {
8799             case bfd_section_reloc_link_order:
8800             case bfd_symbol_reloc_link_order:
8801               abort ();
8802               return FALSE;
8803             case bfd_indirect_link_order:
8804               /* Already done.  */
8805               break;
8806             default:
8807               if (! _bfd_default_link_order (abfd, info, o, p))
8808                 return FALSE;
8809               break;
8810             }
8811         }
8812     }
8813
8814   /* Compute fixups.  */
8815   if (!alpha_vms_build_fixups (info))
8816     return FALSE;
8817
8818   /* Compute the DMT.  */
8819   if (dmt != NULL)
8820     {
8821       int pass;
8822       unsigned char *contents = NULL;
8823
8824       /* In pass 1, compute the size.  In pass 2, write the DMT contents.  */
8825       for (pass = 0; pass < 2; pass++)
8826         {
8827           unsigned int off = 0;
8828
8829           /* For each object file (ie for each module).  */
8830           for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8831             {
8832               asection *sub_dst;
8833               struct vms_dmt_header *dmth = NULL;
8834               unsigned int psect_count;
8835
8836               /* Skip this module if it has no DST.  */
8837               sub_dst = PRIV2 (sub, dst_section);
8838               if (sub_dst == NULL || sub_dst->size == 0)
8839                 continue;
8840
8841               if (pass == 1)
8842                 {
8843                   /* Write the header.  */
8844                   dmth = (struct vms_dmt_header *)(contents + off);
8845                   bfd_putl32 (sub_dst->output_offset, dmth->modbeg);
8846                   bfd_putl32 (sub_dst->size, dmth->size);
8847                 }
8848
8849               off += sizeof (struct vms_dmt_header);
8850               psect_count = 0;
8851
8852               /* For each section (ie for each psect).  */
8853               for (o = sub->sections; o != NULL; o = o->next)
8854                 {
8855                   /* Only consider interesting sections.  */
8856                   if (!(o->flags & SEC_ALLOC))
8857                     continue;
8858                   if (o->flags & SEC_LINKER_CREATED)
8859                     continue;
8860
8861                   if (pass == 1)
8862                     {
8863                       /* Write an entry.  */
8864                       struct vms_dmt_psect *dmtp;
8865
8866                       dmtp = (struct vms_dmt_psect *)(contents + off);
8867                       bfd_putl32 (o->output_offset + o->output_section->vma,
8868                                   dmtp->start);
8869                       bfd_putl32 (o->size, dmtp->length);
8870                       psect_count++;
8871                     }
8872                   off += sizeof (struct vms_dmt_psect);
8873                 }
8874               if (pass == 1)
8875                 bfd_putl32 (psect_count, dmth->psect_count);
8876             }
8877
8878           if (pass == 0)
8879             {
8880               contents = bfd_zalloc (info->output_bfd, off);
8881               if (contents == NULL)
8882                 return FALSE;
8883               dmt->contents = contents;
8884               dmt->size = off;
8885             }
8886           else
8887             {
8888               BFD_ASSERT (off == dmt->size);
8889             }
8890         }
8891     }
8892
8893   return TRUE;
8894 }
8895
8896 /* Read the contents of a section.
8897    buf points to a buffer of buf_size bytes to be filled with
8898    section data (starting at offset into section)  */
8899
8900 static bfd_boolean
8901 alpha_vms_get_section_contents (bfd *abfd, asection *section,
8902                                 void *buf, file_ptr offset,
8903                                 bfd_size_type count)
8904 {
8905   asection *sec;
8906
8907   /* Image are easy.  */
8908   if (bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
8909     return _bfd_generic_get_section_contents (abfd, section,
8910                                               buf, offset, count);
8911
8912   /* Safety check.  */
8913   if (offset + count < count
8914       || offset + count > section->size)
8915     {
8916       bfd_set_error (bfd_error_invalid_operation);
8917       return FALSE;
8918     }
8919
8920   /* Alloc in memory and read ETIRs.  */
8921   BFD_ASSERT (section->contents == NULL);
8922
8923   for (sec = abfd->sections; sec; sec = sec->next)
8924     {
8925       BFD_ASSERT (sec->contents == NULL);
8926
8927       if (sec->size != 0 && (sec->flags & SEC_HAS_CONTENTS))
8928         {
8929           sec->contents = bfd_alloc (abfd, sec->size);
8930           if (sec->contents == NULL)
8931             return FALSE;
8932         }
8933     }
8934   if (!alpha_vms_read_sections_content (abfd, NULL))
8935     return FALSE;
8936   for (sec = abfd->sections; sec; sec = sec->next)
8937     if (section->contents)
8938       section->flags |= SEC_IN_MEMORY;
8939   memcpy (buf, section->contents + offset, count);
8940   return TRUE;
8941 }
8942
8943
8944 /* Set the format of a file being written.  */
8945
8946 static bfd_boolean
8947 alpha_vms_mkobject (bfd * abfd)
8948 {
8949   const bfd_arch_info_type *arch;
8950
8951   vms_debug2 ((1, "alpha_vms_mkobject (%p)\n", abfd));
8952
8953   if (!vms_initialize (abfd))
8954     return FALSE;
8955
8956   PRIV (recwr.buf) = bfd_alloc (abfd, MAX_OUTREC_SIZE);
8957   if (PRIV (recwr.buf) == NULL)
8958     return FALSE;
8959
8960   arch = bfd_scan_arch ("alpha");
8961
8962   if (arch == 0)
8963     {
8964       bfd_set_error (bfd_error_wrong_format);
8965       return FALSE;
8966     }
8967
8968   abfd->arch_info = arch;
8969   return TRUE;
8970 }
8971
8972
8973 /* 4.1, generic.  */
8974
8975 /* Called when the BFD is being closed to do any necessary cleanup.  */
8976
8977 static bfd_boolean
8978 vms_close_and_cleanup (bfd * abfd)
8979 {
8980   vms_debug2 ((1, "vms_close_and_cleanup (%p)\n", abfd));
8981
8982   if (abfd == NULL || abfd->tdata.any == NULL)
8983     return TRUE;
8984
8985   if (abfd->format == bfd_archive)
8986     {
8987       bfd_release (abfd, abfd->tdata.any);
8988       abfd->tdata.any = NULL;
8989       return TRUE;
8990     }
8991
8992   if (PRIV (recrd.buf) != NULL)
8993     free (PRIV (recrd.buf));
8994
8995   if (PRIV (sections) != NULL)
8996     free (PRIV (sections));
8997
8998   bfd_release (abfd, abfd->tdata.any);
8999   abfd->tdata.any = NULL;
9000
9001 #ifdef VMS
9002   if (abfd->direction == write_direction)
9003     {
9004       /* Last step on VMS is to convert the file to variable record length
9005          format.  */
9006       if (bfd_cache_close (abfd) != TRUE)
9007         return FALSE;
9008       if (_bfd_vms_convert_to_var_unix_filename (abfd->filename) != TRUE)
9009         return FALSE;
9010     }
9011 #endif
9012
9013   return TRUE;
9014 }
9015
9016 /* Called when a new section is created.  */
9017
9018 static bfd_boolean
9019 vms_new_section_hook (bfd * abfd, asection *section)
9020 {
9021   bfd_size_type amt;
9022
9023   vms_debug2 ((1, "vms_new_section_hook (%p, [%d]%s)\n",
9024                abfd, section->index, section->name));
9025
9026   bfd_set_section_alignment (abfd, section, 0);
9027
9028   vms_debug2 ((7, "%d: %s\n", section->index, section->name));
9029
9030   amt = sizeof (struct vms_section_data_struct);
9031   section->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
9032   if (section->used_by_bfd == NULL)
9033     return FALSE;
9034
9035   /* Create the section symbol.  */
9036   return _bfd_generic_new_section_hook (abfd, section);
9037 }
9038
9039 /* Part 4.5, symbols.  */
9040
9041 /* Print symbol to file according to how. how is one of
9042    bfd_print_symbol_name        just print the name
9043    bfd_print_symbol_more        print more (???)
9044    bfd_print_symbol_all print all we know, which is not much right now :-).  */
9045
9046 static void
9047 vms_print_symbol (bfd * abfd,
9048                   void * file,
9049                   asymbol *symbol,
9050                   bfd_print_symbol_type how)
9051 {
9052   vms_debug2 ((1, "vms_print_symbol (%p, %p, %p, %d)\n",
9053                abfd, file, symbol, how));
9054
9055   switch (how)
9056     {
9057       case bfd_print_symbol_name:
9058       case bfd_print_symbol_more:
9059         fprintf ((FILE *)file," %s", symbol->name);
9060       break;
9061
9062       case bfd_print_symbol_all:
9063         {
9064           const char *section_name = symbol->section->name;
9065
9066           bfd_print_symbol_vandf (abfd, file, symbol);
9067
9068           fprintf ((FILE *) file," %-8s %s", section_name, symbol->name);
9069         }
9070       break;
9071     }
9072 }
9073
9074 /* Return information about symbol in ret.
9075
9076    fill type, value and name
9077    type:
9078         A       absolute
9079         B       bss segment symbol
9080         C       common symbol
9081         D       data segment symbol
9082         f       filename
9083         t       a static function symbol
9084         T       text segment symbol
9085         U       undefined
9086         -       debug.  */
9087
9088 static void
9089 vms_get_symbol_info (bfd * abfd ATTRIBUTE_UNUSED,
9090                      asymbol *symbol,
9091                      symbol_info *ret)
9092 {
9093   asection *sec;
9094
9095   vms_debug2 ((1, "vms_get_symbol_info (%p, %p, %p)\n", abfd, symbol, ret));
9096
9097   sec = symbol->section;
9098
9099   if (ret == NULL)
9100     return;
9101
9102   if (sec == NULL)
9103     ret->type = 'U';
9104   else if (bfd_is_com_section (sec))
9105     ret->type = 'C';
9106   else if (bfd_is_abs_section (sec))
9107     ret->type = 'A';
9108   else if (bfd_is_und_section (sec))
9109     ret->type = 'U';
9110   else if (bfd_is_ind_section (sec))
9111     ret->type = 'I';
9112   else if ((symbol->flags & BSF_FUNCTION)
9113            || (bfd_get_section_flags (abfd, sec) & SEC_CODE))
9114     ret->type = 'T';
9115   else if (bfd_get_section_flags (abfd, sec) & SEC_DATA)
9116     ret->type = 'D';
9117   else if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
9118     ret->type = 'B';
9119   else
9120     ret->type = '-';
9121
9122   if (ret->type != 'U')
9123     ret->value = symbol->value + symbol->section->vma;
9124   else
9125     ret->value = 0;
9126   ret->name = symbol->name;
9127 }
9128
9129 /* Return TRUE if the given symbol sym in the BFD abfd is
9130    a compiler generated local label, else return FALSE.  */
9131
9132 static bfd_boolean
9133 vms_bfd_is_local_label_name (bfd * abfd ATTRIBUTE_UNUSED,
9134                              const char *name)
9135 {
9136   vms_debug2 ((1, "vms_bfd_is_local_label_name (%p, %s)\n", abfd, name));
9137   return name[0] == '$';
9138 }
9139 \f
9140 /* Part 4.7, writing an object file.  */
9141
9142 /* Sets the contents of the section section in BFD abfd to the data starting
9143    in memory at LOCATION. The data is written to the output section starting
9144    at offset offset for count bytes.
9145
9146    Normally TRUE is returned, else FALSE. Possible error returns are:
9147    o bfd_error_no_contents - The output section does not have the
9148         SEC_HAS_CONTENTS attribute, so nothing can be written to it.
9149    o and some more too  */
9150
9151 static bfd_boolean
9152 _bfd_vms_set_section_contents (bfd * abfd,
9153                                asection *section,
9154                                const void * location,
9155                                file_ptr offset,
9156                                bfd_size_type count)
9157 {
9158   if (section->contents == NULL)
9159     {
9160       section->contents = bfd_alloc (abfd, section->size);
9161       if (section->contents == NULL)
9162         return FALSE;
9163
9164       memcpy (section->contents + offset, location, (size_t) count);
9165     }
9166
9167   return TRUE;
9168 }
9169
9170 /* Set the architecture and machine type in BFD abfd to arch and mach.
9171    Find the correct pointer to a structure and insert it into the arch_info
9172    pointer.  */
9173
9174 static bfd_boolean
9175 alpha_vms_set_arch_mach (bfd *abfd,
9176                          enum bfd_architecture arch, unsigned long mach)
9177 {
9178   if (arch != bfd_arch_alpha
9179       && arch != bfd_arch_unknown)
9180     return FALSE;
9181
9182   return bfd_default_set_arch_mach (abfd, arch, mach);
9183 }
9184
9185 /* Set section VMS flags.  Clear NO_FLAGS and set FLAGS.  */
9186
9187 void
9188 bfd_vms_set_section_flags (bfd *abfd ATTRIBUTE_UNUSED,
9189                            asection *sec, flagword no_flags, flagword flags)
9190 {
9191   vms_section_data (sec)->no_flags = no_flags;
9192   vms_section_data (sec)->flags = flags;
9193 }
9194
9195 struct vms_private_data_struct *
9196 bfd_vms_get_data (bfd *abfd)
9197 {
9198   return (struct vms_private_data_struct *)abfd->tdata.any;
9199 }
9200
9201 #define vms_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
9202 #define vms_bfd_link_just_syms            _bfd_generic_link_just_syms
9203 #define vms_bfd_copy_link_hash_symbol_type \
9204   _bfd_generic_copy_link_hash_symbol_type
9205 #define vms_bfd_is_group_section          bfd_generic_is_group_section
9206 #define vms_bfd_discard_group             bfd_generic_discard_group
9207 #define vms_section_already_linked        _bfd_generic_section_already_linked
9208 #define vms_bfd_define_common_symbol      bfd_generic_define_common_symbol
9209 #define vms_bfd_copy_private_header_data  _bfd_generic_bfd_copy_private_header_data
9210
9211 #define vms_bfd_copy_private_bfd_data     _bfd_generic_bfd_copy_private_bfd_data
9212 #define vms_bfd_free_cached_info          _bfd_generic_bfd_free_cached_info
9213 #define vms_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
9214 #define vms_bfd_copy_private_symbol_data  _bfd_generic_bfd_copy_private_symbol_data
9215 #define vms_bfd_set_private_flags         _bfd_generic_bfd_set_private_flags
9216 #define vms_bfd_merge_private_bfd_data    _bfd_generic_bfd_merge_private_bfd_data
9217
9218 /* Symbols table.  */
9219 #define alpha_vms_make_empty_symbol        _bfd_generic_make_empty_symbol
9220 #define alpha_vms_bfd_is_target_special_symbol \
9221    ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
9222 #define alpha_vms_print_symbol             vms_print_symbol
9223 #define alpha_vms_get_symbol_info          vms_get_symbol_info
9224 #define alpha_vms_read_minisymbols         _bfd_generic_read_minisymbols
9225 #define alpha_vms_minisymbol_to_symbol     _bfd_generic_minisymbol_to_symbol
9226 #define alpha_vms_get_lineno               _bfd_nosymbols_get_lineno
9227 #define alpha_vms_find_inliner_info        _bfd_nosymbols_find_inliner_info
9228 #define alpha_vms_bfd_make_debug_symbol    _bfd_nosymbols_bfd_make_debug_symbol
9229 #define alpha_vms_find_nearest_line        _bfd_vms_find_nearest_dst_line
9230 #define alpha_vms_bfd_is_local_label_name  vms_bfd_is_local_label_name
9231
9232 /* Generic table.  */
9233 #define alpha_vms_close_and_cleanup        vms_close_and_cleanup
9234 #define alpha_vms_bfd_free_cached_info     vms_bfd_free_cached_info
9235 #define alpha_vms_new_section_hook         vms_new_section_hook
9236 #define alpha_vms_set_section_contents     _bfd_vms_set_section_contents
9237 #define alpha_vms_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
9238
9239 #define alpha_vms_bfd_get_relocated_section_contents \
9240   bfd_generic_get_relocated_section_contents
9241
9242 #define alpha_vms_bfd_relax_section bfd_generic_relax_section
9243 #define alpha_vms_bfd_gc_sections bfd_generic_gc_sections
9244 #define alpha_vms_bfd_merge_sections bfd_generic_merge_sections
9245 #define alpha_vms_bfd_is_group_section bfd_generic_is_group_section
9246 #define alpha_vms_bfd_discard_group bfd_generic_discard_group
9247 #define alpha_vms_section_already_linked \
9248   _bfd_generic_section_already_linked
9249
9250 #define alpha_vms_bfd_define_common_symbol bfd_generic_define_common_symbol
9251 #define alpha_vms_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
9252 #define alpha_vms_bfd_link_just_syms _bfd_generic_link_just_syms
9253 #define alpha_vms_bfd_copy_link_hash_symbol_type \
9254   _bfd_generic_copy_link_hash_symbol_type
9255
9256 #define alpha_vms_bfd_link_split_section  _bfd_generic_link_split_section
9257
9258 #define alpha_vms_get_dynamic_symtab_upper_bound \
9259   _bfd_nodynamic_get_dynamic_symtab_upper_bound
9260 #define alpha_vms_canonicalize_dynamic_symtab \
9261   _bfd_nodynamic_canonicalize_dynamic_symtab
9262 #define alpha_vms_get_dynamic_reloc_upper_bound \
9263   _bfd_nodynamic_get_dynamic_reloc_upper_bound
9264 #define alpha_vms_canonicalize_dynamic_reloc \
9265   _bfd_nodynamic_canonicalize_dynamic_reloc
9266
9267 const bfd_target vms_alpha_vec =
9268 {
9269   "vms-alpha",                  /* Name.  */
9270   bfd_target_evax_flavour,
9271   BFD_ENDIAN_LITTLE,            /* Data byte order is little.  */
9272   BFD_ENDIAN_LITTLE,            /* Header byte order is little.  */
9273
9274   (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS
9275    | WP_TEXT | D_PAGED),        /* Object flags.  */
9276   (SEC_ALLOC | SEC_LOAD | SEC_RELOC
9277    | SEC_READONLY | SEC_CODE | SEC_DATA
9278    | SEC_HAS_CONTENTS | SEC_IN_MEMORY),         /* Sect flags.  */
9279   0,                            /* symbol_leading_char.  */
9280   ' ',                          /* ar_pad_char.  */
9281   15,                           /* ar_max_namelen.  */
9282   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
9283   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
9284   bfd_getl16, bfd_getl_signed_16, bfd_putl16,
9285   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
9286   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
9287   bfd_getl16, bfd_getl_signed_16, bfd_putl16,
9288
9289   {_bfd_dummy_target, alpha_vms_object_p,       /* bfd_check_format.  */
9290    _bfd_vms_lib_alpha_archive_p, _bfd_dummy_target},
9291   {bfd_false, alpha_vms_mkobject,               /* bfd_set_format.  */
9292    _bfd_vms_lib_alpha_mkarchive, bfd_false},
9293   {bfd_false, alpha_vms_write_object_contents,  /* bfd_write_contents.  */
9294    _bfd_vms_lib_write_archive_contents, bfd_false},
9295
9296   BFD_JUMP_TABLE_GENERIC (alpha_vms),
9297   BFD_JUMP_TABLE_COPY (vms),
9298   BFD_JUMP_TABLE_CORE (_bfd_nocore),
9299   BFD_JUMP_TABLE_ARCHIVE (_bfd_vms_lib),
9300   BFD_JUMP_TABLE_SYMBOLS (alpha_vms),
9301   BFD_JUMP_TABLE_RELOCS (alpha_vms),
9302   BFD_JUMP_TABLE_WRITE (alpha_vms),
9303   BFD_JUMP_TABLE_LINK (alpha_vms),
9304   BFD_JUMP_TABLE_DYNAMIC (alpha_vms),
9305
9306   NULL,
9307
9308   (PTR) 0
9309 };