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