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