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