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