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