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