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