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