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