1 /* vms-misc.c -- Miscellaneous functions for VAX (openVMS/VAX) and
2 EVAX (openVMS/Alpha) files.
3 Copyright 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
5 Written by Klaus K"ampf (kkaempf@rmi.de)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 /*-----------------------------------------------------------------------------*/
37 /* debug function for all vms extensions
38 evaluates environment variable VMS_DEBUG for a
39 numerical value on the first call
40 all error levels below this value are printed
43 1 toplevel bfd calls (functions from the bfd vector)
44 2 functions called by bfd calls
48 level is also identation level. Indentation is performed
54 _bfd_vms_debug (int level
, char *format
, ...)
56 static int min_level
= -1;
57 static FILE *output
= NULL
;
60 int abslvl
= (level
> 0)?level
:-level
;
64 if ((eptr
= getenv("VMS_DEBUG")) != NULL
)
66 min_level
= atoi(eptr
);
74 if (abslvl
> min_level
)
79 va_start(args
, format
);
80 vfprintf(output
, format
, args
);
87 #else /* not __STDC__ */
90 _bfd_vms_debug (level
, format
, a1
, a2
, a3
, a4
, a5
, a6
)
93 long a1
; long a2
; long a3
;
94 long a4
; long a5
; long a6
;
96 static int min_level
= -1;
97 static FILE *output
= NULL
;
102 if ((eptr
= getenv("VMS_DEBUG")) != NULL
)
104 min_level
= atoi(eptr
);
112 if (level
> min_level
)
116 fprintf(output
, " ");
117 fprintf(output
, format
, a1
, a2
, a3
, a4
, a5
, a6
);
122 #endif /* __STDC__ */
126 hex dump 'size' bytes starting at 'ptr' */
129 _bfd_hexdump (level
, ptr
, size
, offset
)
135 unsigned char *lptr
= ptr
;
142 vms_debug (level
, "%08lx:", start
);
143 vms_debug (-level
, " %02x", *ptr
++);
148 while ((count
%16) != 0)
150 vms_debug (-level
, " ");
156 vms_debug (-level
, " ");
159 vms_debug (-level
, "%c", (*lptr
< 32)?'.':*lptr
);
162 vms_debug (-level
, "\n");
166 vms_debug (-level
, "\n");
175 These are needed when reading an object file. */
177 /* allocate new vms_hash_entry
178 keep the symbol name and a pointer to the bfd symbol in the table */
180 struct bfd_hash_entry
*
181 _bfd_vms_hash_newfunc (entry
, table
, string
)
182 struct bfd_hash_entry
*entry
;
183 struct bfd_hash_table
*table
;
186 vms_symbol_entry
*ret
;
189 vms_debug (5, "_bfd_vms_hash_newfunc(%p, %p, %s)\n", entry
, table
, string
);
192 if (entry
== (struct bfd_hash_entry
*)NULL
)
194 ret
= (vms_symbol_entry
*)
195 bfd_hash_allocate (table
, sizeof (vms_symbol_entry
));
196 if (ret
== (vms_symbol_entry
*) NULL
)
198 bfd_set_error (bfd_error_no_memory
);
199 return (struct bfd_hash_entry
*)NULL
;
201 entry
= (struct bfd_hash_entry
*) ret
;
204 /* Call the allocation method of the base class. */
206 ret
= (vms_symbol_entry
*) bfd_hash_newfunc (entry
, table
, string
);
208 vms_debug (6, "_bfd_vms_hash_newfunc ret %p\n", ret
);
211 ret
->symbol
= (asymbol
*)NULL
;
213 return (struct bfd_hash_entry
*)ret
;
217 /* object file input functions */
219 /* Return type and length from record header (buf) on Alpha. */
222 _bfd_vms_get_header_values (abfd
, buf
, type
, length
)
223 bfd
*abfd ATTRIBUTE_UNUSED
;
229 *type
= bfd_getl16 (buf
);
232 *length
= bfd_getl16 (buf
);
235 vms_debug (10, "_bfd_vms_get_header_values type %x, length %x\n", (type
?*type
:0), (length
?*length
:0));
243 /* Get next record from object file to vms_buf
244 set PRIV(buf_size) and return it
246 this is a little tricky since it should be portable.
248 the openVMS object file has 'variable length' which means that
249 read() returns data in chunks of (hopefully) correct and expected
250 size. The linker (and other tools on vms) depend on that. Unix doesn't
251 know about 'formatted' files, so reading and writing such an object
252 file in a unix environment is not trivial.
254 With the tool 'file' (available on all vms ftp sites), one
255 can view and change the attributes of a file. Changing from
256 'variable length' to 'fixed length, 512 bytes' reveals the
257 record length at the first 2 bytes of every record. The same
258 happens during the transfer of object files from vms to unix,
259 at least with ucx, dec's implementation of tcp/ip.
261 The vms format repeats the length at bytes 2 & 3 of every record.
263 On the first call (file_format == FF_UNKNOWN) we check if
264 the first and the third byte pair (!) of the record match.
265 If they do it's an object file in an unix environment or with
266 wrong attributes (FF_FOREIGN), else we should be in a vms
267 environment where read() returns the record size (FF_NATIVE).
269 reading is always done in 2 steps.
270 first just the record header is read and the length extracted
272 then the read buffer is adjusted and the remaining bytes are
275 all file i/o is always done on even file positions */
278 _bfd_vms_get_record (abfd
)
281 int test_len
, test_start
, remaining
;
282 unsigned char *vms_buf
;
285 vms_debug (8, "_bfd_vms_get_record\n");
288 /* minimum is 6 bytes on Alpha
289 (2 bytes length, 2 bytes record id, 2 bytes length repeated)
291 on VAX there's no length information in the record
292 so start with OBJ_S_C_MAXRECSIZ */
294 if (PRIV(buf_size
) == 0)
298 PRIV(vms_buf
) = (unsigned char *) malloc (OBJ_S_C_MAXRECSIZ
);
299 PRIV(buf_size
) = OBJ_S_C_MAXRECSIZ
;
300 PRIV(file_format
) = FF_VAX
;
303 PRIV(vms_buf
) = (unsigned char *) malloc (6);
306 vms_buf
= PRIV(vms_buf
);
310 bfd_set_error (bfd_error_no_memory
);
314 switch (PRIV(file_format
))
318 test_len
= 6; /* probe 6 bytes */
319 test_start
= 2; /* where the record starts */
333 /* skip odd alignment byte */
335 if (bfd_tell (abfd
) & 1)
337 if (bfd_read (PRIV(vms_buf
), 1, 1, abfd
) != 1)
339 bfd_set_error (bfd_error_file_truncated
);
344 /* read the record header on Alpha. */
347 && (bfd_read (PRIV(vms_buf
), 1, test_len
, abfd
)
348 != (bfd_size_type
) test_len
))
350 bfd_set_error (bfd_error_file_truncated
);
354 /* check file format on first call */
356 if (PRIV(file_format
) == FF_UNKNOWN
)
357 { /* record length repeats ? */
358 if ( (vms_buf
[0] == vms_buf
[4])
359 && (vms_buf
[1] == vms_buf
[5]))
361 PRIV(file_format
) = FF_FOREIGN
; /* Y: foreign environment */
366 PRIV(file_format
) = FF_NATIVE
; /* N: native environment */
373 PRIV(rec_length
) = bfd_read (vms_buf
, 1, PRIV(buf_size
), abfd
);
374 if (PRIV(rec_length
) <= 0)
376 bfd_set_error (bfd_error_file_truncated
);
379 PRIV(vms_rec
) = vms_buf
;
383 /* extract vms record length */
385 _bfd_vms_get_header_values (abfd
, vms_buf
+test_start
, NULL
,
388 if (PRIV(rec_length
) <= 0)
390 bfd_set_error (bfd_error_file_truncated
);
394 /* that's what the linker manual says */
396 if (PRIV(rec_length
) > EOBJ_S_C_MAXRECSIZ
)
398 bfd_set_error (bfd_error_file_truncated
);
402 /* adjust the buffer */
404 if (PRIV(rec_length
) > PRIV(buf_size
))
406 PRIV(vms_buf
) = (unsigned char *) realloc (vms_buf
, PRIV(rec_length
));
407 vms_buf
= PRIV(vms_buf
);
410 bfd_set_error (bfd_error_no_memory
);
413 PRIV(buf_size
) = PRIV(rec_length
);
416 /* read the remaining record */
418 remaining
= PRIV(rec_length
) - test_len
+ test_start
;
421 vms_debug (10, "bfd_read remaining %d\n", remaining
);
423 if (bfd_read (vms_buf
+ test_len
, 1, remaining
, abfd
) !=
424 (bfd_size_type
) remaining
)
426 bfd_set_error (bfd_error_file_truncated
);
429 PRIV(vms_rec
) = vms_buf
+ test_start
;
433 vms_debug (11, "bfd_read rec_length %d\n", PRIV(rec_length
));
436 return PRIV(rec_length
);
440 /* get next vms record from file
441 update vms_rec and rec_length to new (remaining) values */
444 _bfd_vms_next_record (abfd
)
448 vms_debug (8, "_bfd_vms_next_record (len %d, size %d)\n",
449 PRIV(rec_length
), PRIV(rec_size
));
452 if (PRIV(rec_length
) > 0)
454 PRIV(vms_rec
) += PRIV(rec_size
);
458 if (_bfd_vms_get_record (abfd
) <= 0)
467 PRIV(rec_type
) = *(PRIV(vms_rec
));
468 PRIV(rec_size
) = PRIV(rec_length
);
472 _bfd_vms_get_header_values (abfd
, PRIV(vms_rec
), &PRIV(rec_type
),
475 PRIV(rec_length
) -= PRIV(rec_size
);
478 vms_debug (8, "_bfd_vms_next_record: rec %p, size %d, length %d, type %d\n",
479 PRIV(vms_rec
), PRIV(rec_size
), PRIV(rec_length
),
483 return PRIV(rec_type
);
488 /* Copy sized string (string with fixed length) to new allocated area
489 size is string length (size of record) */
492 _bfd_vms_save_sized_string (str
, size
)
496 char *newstr
= bfd_malloc (size
+ 1);
500 strncpy (newstr
, (char *)str
, size
);
506 /* Copy counted string (string with length at first byte) to new allocated area
507 ptr points to length byte on entry */
510 _bfd_vms_save_counted_string (ptr
)
515 return _bfd_vms_save_sized_string (ptr
, len
);
519 /* stack routines for vms ETIR commands */
521 /* Push value and section index */
524 _bfd_vms_push (abfd
, val
, psect
)
529 static int last_psect
;
532 vms_debug (4, "<push %016lx(%d) at %d>\n", val
, psect
, PRIV(stackptr
));
538 PRIV(stack
[PRIV(stackptr
)]).value
= val
;
539 PRIV(stack
[PRIV(stackptr
)]).psect
= last_psect
;
541 if (PRIV(stackptr
) >= STACKSIZE
)
543 bfd_set_error (bfd_error_bad_value
);
544 (*_bfd_error_handler
) (_("Stack overflow (%d) in _bfd_vms_push"), PRIV(stackptr
));
551 /* Pop value and section index */
554 _bfd_vms_pop (abfd
, psect
)
560 if (PRIV(stackptr
) == 0)
562 bfd_set_error (bfd_error_bad_value
);
563 (*_bfd_error_handler
) (_("Stack underflow in _bfd_vms_pop"));
567 value
= PRIV(stack
[PRIV(stackptr
)]).value
;
568 if ((psect
!= NULL
) && (PRIV(stack
[PRIV(stackptr
)]).psect
>= 0))
569 *psect
= PRIV(stack
[PRIV(stackptr
)]).psect
;
572 vms_debug (4, "<pop %016lx(%d)>\n", value
, PRIV(stack
[PRIV(stackptr
)]).psect
);
579 /* object file output functions */
581 /* GAS tends to write sections in little chunks (bfd_set_section_contents)
582 which we can't use directly. So we save the little chunks in linked
583 lists (one per section) and write them later. */
585 /* Add a new vms_section structure to vms_section_table
586 - forward chaining - */
589 add_new_contents (abfd
, section
)
593 vms_section
*sptr
, *newptr
;
595 sptr
= PRIV(vms_section_table
)[section
->index
];
599 newptr
= (vms_section
*) bfd_malloc (sizeof (vms_section
));
600 if (newptr
== (vms_section
*) NULL
)
602 newptr
->contents
= (unsigned char *) bfd_alloc (abfd
, (int)section
->_raw_size
);
603 if (newptr
->contents
== (unsigned char *)NULL
)
606 newptr
->size
= section
->_raw_size
;
608 PRIV(vms_section_table
)[section
->index
] = newptr
;
613 /* Save section data & offset to an vms_section structure
614 vms_section_table[] holds the vms_section chain */
617 _bfd_save_vms_section (abfd
, section
, data
, offset
, count
)
626 if (section
->index
>= VMS_SECTION_COUNT
)
628 bfd_set_error (bfd_error_nonrepresentable_section
);
631 if (count
== (bfd_size_type
)0)
633 sptr
= add_new_contents (abfd
, section
);
636 memcpy (sptr
->contents
+ offset
, data
, (size_t) count
);
642 /* Get vms_section pointer to saved contents for section # index */
645 _bfd_get_vms_section (abfd
, index
)
649 if (index
>= VMS_SECTION_COUNT
)
651 bfd_set_error (bfd_error_nonrepresentable_section
);
654 return PRIV(vms_section_table
)[index
];
658 /* Object output routines */
660 /* Begin new record or record header
661 write 2 bytes rectype
662 write 2 bytes record length (filled in at flush)
663 write 2 bytes header type (ommitted if rechead == -1) */
666 _bfd_vms_output_begin (abfd
, rectype
, rechead
)
672 vms_debug (6, "_bfd_vms_output_begin(type %d, head %d)\n", rectype
,
676 _bfd_vms_output_short (abfd
,rectype
);
678 /* save current output position to fill in lenght later */
680 if (PRIV(push_level
) > 0)
681 PRIV(length_pos
) = PRIV(output_size
);
684 vms_debug (6, "_bfd_vms_output_begin: length_pos = %d\n",
688 _bfd_vms_output_short (abfd
,0); /* placeholder for length */
691 _bfd_vms_output_short (abfd
,rechead
);
697 /* Set record/subrecord alignment */
700 _bfd_vms_output_alignment (abfd
, alignto
)
705 vms_debug (6, "_bfd_vms_output_alignment(%d)\n", alignto
);
708 PRIV(output_alignment
) = alignto
;
713 /* Prepare for subrecord fields */
716 _bfd_vms_output_push (abfd
)
720 vms_debug (6, "vms_output_push(pushed_size = %d)\n", PRIV(output_size
));
724 PRIV(pushed_size
) = PRIV(output_size
);
729 /* End of subrecord fields */
732 _bfd_vms_output_pop (abfd
)
736 vms_debug (6, "vms_output_pop(pushed_size = %d)\n", PRIV(pushed_size
));
739 _bfd_vms_output_flush (abfd
);
740 PRIV(length_pos
) = 2;
743 vms_debug (6, "vms_output_pop: length_pos = %d\n", PRIV(length_pos
));
746 PRIV(pushed_size
) = 0;
752 /* Flush unwritten output, ends current record */
755 _bfd_vms_output_flush (abfd
)
758 int real_size
= PRIV(output_size
);
763 vms_debug (6, "_bfd_vms_output_flush(real_size = %d, pushed_size %d at lenpos %d)\n",
764 real_size
, PRIV(pushed_size
), PRIV(length_pos
));
767 if (PRIV(push_level
) > 0)
768 length
= real_size
- PRIV(pushed_size
);
774 aligncount
= (PRIV(output_alignment
)
775 - (length
% PRIV(output_alignment
))) % PRIV(output_alignment
);
778 vms_debug (6, "align: adding %d bytes\n", aligncount
);
781 while(aligncount
-- > 0)
783 PRIV(output_buf
)[real_size
++] = 0;
785 /* this is why I *love* vms: inconsistency :-}
786 alignment is added to the subrecord length
787 but not to the record length */
788 if (PRIV(push_level
) > 0)
793 /* put length to buffer */
794 PRIV(output_size
) = PRIV(length_pos
);
795 _bfd_vms_output_short (abfd
, (unsigned int)length
);
797 if (PRIV(push_level
) == 0)
800 /* write length first, see FF_FOREIGN in the input routines */
801 fwrite (PRIV(output_buf
)+2, 2, 1, (FILE *)abfd
->iostream
);
803 fwrite (PRIV(output_buf
), real_size
, 1, (FILE *)abfd
->iostream
);
805 PRIV(output_size
) = 0;
809 PRIV(output_size
) = real_size
;
810 PRIV(pushed_size
) = PRIV(output_size
);
817 /* End record output */
820 _bfd_vms_output_end (abfd
)
824 vms_debug (6, "_bfd_vms_output_end\n");
827 _bfd_vms_output_flush (abfd
);
833 /* check remaining buffer size
835 return what's left. */
838 _bfd_vms_output_check (abfd
, size
)
843 vms_debug (6, "_bfd_vms_output_check(%d)\n", size
);
846 return (MAX_OUTREC_SIZE
- (PRIV(output_size
) + size
+ MIN_OUTREC_LUFT
));
850 /* Output byte (8 bit) value */
853 _bfd_vms_output_byte (abfd
, value
)
858 vms_debug (6, "_bfd_vms_output_byte(%02x)\n", value
);
861 bfd_put_8 (abfd
, value
& 0xff, PRIV(output_buf
) + PRIV(output_size
));
862 PRIV(output_size
) += 1;
867 /* Output short (16 bit) value */
870 _bfd_vms_output_short (abfd
, value
)
875 vms_debug (6, "_bfd_vms_output_short (%04x)\n", value
);
878 bfd_put_16 (abfd
, value
& 0xffff, PRIV(output_buf
) + PRIV(output_size
));
879 PRIV(output_size
) += 2;
884 /* Output long (32 bit) value */
887 _bfd_vms_output_long (abfd
, value
)
892 vms_debug (6, "_bfd_vms_output_long (%08lx)\n", value
);
895 bfd_put_32 (abfd
, value
, PRIV(output_buf
) + PRIV(output_size
));
896 PRIV(output_size
) += 4;
901 /* Output quad (64 bit) value */
904 _bfd_vms_output_quad (abfd
, value
)
909 vms_debug (6, "_bfd_vms_output_quad(%016lx)\n", value
);
912 bfd_put_64(abfd
, value
, PRIV(output_buf
) + PRIV(output_size
));
913 PRIV(output_size
) += 8;
918 /* Output c-string as counted string */
921 _bfd_vms_output_counted (abfd
, value
)
928 vms_debug (6, "_bfd_vms_output_counted(%s)\n", value
);
931 len
= strlen (value
);
934 (*_bfd_error_handler
) (_("_bfd_vms_output_counted called with zero bytes"));
939 (*_bfd_error_handler
) (_("_bfd_vms_output_counted called with too many bytes"));
942 _bfd_vms_output_byte (abfd
, len
& 0xff);
943 _bfd_vms_output_dump (abfd
, (unsigned char *)value
, len
);
947 /* Output character area */
950 _bfd_vms_output_dump (abfd
, data
, length
)
956 vms_debug (6, "_bfd_vms_output_dump(%d)\n", length
);
962 memcpy (PRIV(output_buf
) + PRIV(output_size
), data
, length
);
963 PRIV(output_size
) += length
;
969 /* Output count bytes of value */
972 _bfd_vms_output_fill (abfd
, value
, count
)
978 vms_debug (6, "_bfd_vms_output_fill(val %02x times %d)\n", value
, count
);
983 memset (PRIV(output_buf
) + PRIV(output_size
), value
, count
);
984 PRIV(output_size
) += count
;
989 /* this hash routine borrowed from GNU-EMACS, and strengthened slightly ERY*/
995 register const unsigned char *p
= (unsigned char *) ptr
;
996 register const unsigned char *end
= p
+ strlen (ptr
);
997 register unsigned char c
;
998 register int hash
= 0;
1003 hash
= ((hash
<< 3) + (hash
<< 15) + (hash
>> 28) + c
);
1008 /* Generate a length-hashed VMS symbol name (limited to maxlen chars). */
1011 _bfd_vms_length_hash_symbol (abfd
, in
, maxlen
)
1019 const char *old_name
;
1021 static char outbuf
[EOBJ_S_C_SYMSIZ
+1];
1025 vms_debug(4, "_bfd_vms_length_hash_symbol \"%s\"\n", in
);
1028 if (maxlen
> EOBJ_S_C_SYMSIZ
)
1029 maxlen
= EOBJ_S_C_SYMSIZ
;
1031 new_name
= out
; /* save this for later. */
1033 /* We may need to truncate the symbol, save the hash for later. */
1035 in_len
= strlen (in
);
1037 result
= (in_len
> maxlen
) ? hash_string (in
) : 0;
1041 /* Do the length checking. */
1043 if (in_len
<= maxlen
)
1049 if (PRIV(flag_hash_long_names
))
1055 strncpy (out
, in
, i
);
1059 if ((in_len
> maxlen
)
1060 && PRIV(flag_hash_long_names
))
1061 sprintf (out
, "_%08lx", result
);
1066 vms_debug(4, "--> [%d]\"%s\"\n", strlen (outbuf
), outbuf
);
1070 && PRIV(flag_hash_long_names
)
1071 && PRIV(flag_show_after_trunc
))
1072 printf (_("Symbol %s replaced by %s\n"), old_name
, new_name
);
1078 /* Allocate and initialize a new symbol. */
1081 new_symbol (abfd
, name
)
1088 _bfd_vms_debug (7, "new_symbol %s\n", name
);
1091 symbol
= _bfd_vms_make_empty_symbol (abfd
);
1094 symbol
->name
= name
;
1095 symbol
->section
= bfd_make_section (abfd
, BFD_UND_SECTION_NAME
);
1101 /* Allocate and enter a new private symbol. */
1104 _bfd_vms_enter_symbol (abfd
, name
)
1108 vms_symbol_entry
*entry
;
1111 _bfd_vms_debug (6, "_bfd_vms_enter_symbol %s\n", name
);
1114 entry
= (vms_symbol_entry
*)
1115 bfd_hash_lookup (PRIV(vms_symbol_table
), name
, false, false);
1119 _bfd_vms_debug (8, "creating hash entry for %s\n", name
);
1121 entry
= (vms_symbol_entry
*)bfd_hash_lookup (PRIV(vms_symbol_table
), name
, true, false);
1125 symbol
= new_symbol (abfd
, name
);
1128 entry
->symbol
= symbol
;
1129 PRIV(gsd_sym_count
)++;
1136 (*_bfd_error_handler
) (_("failed to enter %s"), name
);
1141 _bfd_vms_debug (8, "found hash entry for %s\n", name
);
1146 _bfd_vms_debug (7, "-> entry %p, entry->symbol %p\n", entry
, entry
->symbol
);