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