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