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