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