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