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