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