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