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