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