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