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