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