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