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