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