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