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