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