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