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