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