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