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