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