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