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