1 /* vms-misc.c -- Miscellaneous functions for VAX (openVMS/VAX) and
2 EVAX (openVMS/Alpha) files.
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
4 Free Software Foundation, Inc.
6 Written by Klaus K"ampf (kkaempf@rmi.de)
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 2 of the License, or
11 (at your option) any later version.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 static vms_section
*add_new_contents
PARAMS ((bfd
*, sec_ptr
));
34 static int hash_string
PARAMS ((const char *));
35 static asymbol
*new_symbol
PARAMS ((bfd
*, char *));
37 /*-----------------------------------------------------------------------------*/
41 /* debug function for all vms extensions
42 evaluates environment variable VMS_DEBUG for a
43 numerical value on the first call
44 all error levels below this value are printed
47 1 toplevel bfd calls (functions from the bfd vector)
48 2 functions called by bfd calls
52 level is also identation level. Indentation is performed
58 _bfd_vms_debug (int level
, char *format
, ...)
60 static int min_level
= -1;
61 static FILE *output
= NULL
;
64 int abslvl
= (level
> 0)?level
:-level
;
68 if ((eptr
= getenv("VMS_DEBUG")) != NULL
)
70 min_level
= atoi(eptr
);
78 if (abslvl
> min_level
)
82 fprintf (output
, " ");
83 va_start(args
, format
);
84 vfprintf (output
, format
, args
);
91 #else /* not __STDC__ */
94 _bfd_vms_debug (level
, format
, a1
, a2
, a3
, a4
, a5
, a6
)
97 long a1
; long a2
; long a3
;
98 long a4
; long a5
; long a6
;
100 static int min_level
= -1;
101 static FILE *output
= NULL
;
106 if ((eptr
= getenv("VMS_DEBUG")) != NULL
)
108 min_level
= atoi(eptr
);
116 if (level
> min_level
)
120 fprintf (output
, " ");
121 fprintf (output
, format
, a1
, a2
, a3
, a4
, a5
, a6
);
126 #endif /* __STDC__ */
129 hex dump 'size' bytes starting at 'ptr' */
132 _bfd_hexdump (level
, ptr
, size
, offset
)
138 unsigned char *lptr
= ptr
;
145 vms_debug (level
, "%08lx:", start
);
146 vms_debug (-level
, " %02x", *ptr
++);
151 while ((count
%16) != 0)
153 vms_debug (-level
, " ");
159 vms_debug (-level
, " ");
162 vms_debug (-level
, "%c", (*lptr
< 32)?'.':*lptr
);
165 vms_debug (-level
, "\n");
169 vms_debug (-level
, "\n");
177 These are needed when reading an object file. */
179 /* allocate new vms_hash_entry
180 keep the symbol name and a pointer to the bfd symbol in the table */
182 struct bfd_hash_entry
*
183 _bfd_vms_hash_newfunc (entry
, table
, string
)
184 struct bfd_hash_entry
*entry
;
185 struct bfd_hash_table
*table
;
188 vms_symbol_entry
*ret
;
191 vms_debug (5, "_bfd_vms_hash_newfunc(%p, %p, %s)\n", entry
, table
, string
);
194 if (entry
== (struct bfd_hash_entry
*)NULL
)
196 ret
= (vms_symbol_entry
*)
197 bfd_hash_allocate (table
, sizeof (vms_symbol_entry
));
198 if (ret
== (vms_symbol_entry
*) NULL
)
200 bfd_set_error (bfd_error_no_memory
);
201 return (struct bfd_hash_entry
*)NULL
;
203 entry
= (struct bfd_hash_entry
*) ret
;
206 /* Call the allocation method of the base class. */
208 ret
= (vms_symbol_entry
*) bfd_hash_newfunc (entry
, table
, string
);
210 vms_debug (6, "_bfd_vms_hash_newfunc ret %p\n", ret
);
213 ret
->symbol
= (asymbol
*)NULL
;
215 return (struct bfd_hash_entry
*)ret
;
218 /* object file input functions */
220 /* Return type and length from record header (buf) on Alpha. */
223 _bfd_vms_get_header_values (abfd
, buf
, type
, length
)
224 bfd
*abfd ATTRIBUTE_UNUSED
;
230 *type
= bfd_getl16 (buf
);
233 *length
= bfd_getl16 (buf
);
236 vms_debug (10, "_bfd_vms_get_header_values type %x, length %x\n", (type
?*type
:0), (length
?*length
:0));
242 /* Get next record from object file to vms_buf
243 set PRIV(buf_size) and return it
245 this is a little tricky since it should be portable.
247 the openVMS object file has 'variable length' which means that
248 read() returns data in chunks of (hopefully) correct and expected
249 size. The linker (and other tools on vms) depend on that. Unix doesn't
250 know about 'formatted' files, so reading and writing such an object
251 file in a unix environment is not trivial.
253 With the tool 'file' (available on all vms ftp sites), one
254 can view and change the attributes of a file. Changing from
255 'variable length' to 'fixed length, 512 bytes' reveals the
256 record length at the first 2 bytes of every record. The same
257 happens during the transfer of object files from vms to unix,
258 at least with ucx, dec's implementation of tcp/ip.
260 The vms format repeats the length at bytes 2 & 3 of every record.
262 On the first call (file_format == FF_UNKNOWN) we check if
263 the first and the third byte pair (!) of the record match.
264 If they do it's an object file in an unix environment or with
265 wrong attributes (FF_FOREIGN), else we should be in a vms
266 environment where read() returns the record size (FF_NATIVE).
268 reading is always done in 2 steps.
269 first just the record header is read and the length extracted
271 then the read buffer is adjusted and the remaining bytes are
274 all file i/o is always done on even file positions */
277 _bfd_vms_get_record (abfd
)
280 int test_len
, test_start
, remaining
;
281 unsigned char *vms_buf
;
284 vms_debug (8, "_bfd_vms_get_record\n");
287 /* minimum is 6 bytes on Alpha
288 (2 bytes length, 2 bytes record id, 2 bytes length repeated)
290 on VAX there's no length information in the record
291 so start with OBJ_S_C_MAXRECSIZ */
293 if (PRIV (buf_size
) == 0)
299 amt
= OBJ_S_C_MAXRECSIZ
;
300 PRIV (file_format
) = FF_VAX
;
304 PRIV (vms_buf
) = (unsigned char *) bfd_malloc (amt
);
305 PRIV (buf_size
) = amt
;
308 vms_buf
= PRIV (vms_buf
);
313 switch (PRIV (file_format
))
317 test_len
= 6; /* probe 6 bytes */
318 test_start
= 2; /* where the record starts */
333 /* skip odd alignment byte */
335 if (bfd_tell (abfd
) & 1)
337 if (bfd_bread (PRIV (vms_buf
), (bfd_size_type
) 1, abfd
) != 1)
339 bfd_set_error (bfd_error_file_truncated
);
344 /* read the record header on Alpha. */
347 && (bfd_bread (PRIV (vms_buf
), (bfd_size_type
) 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_bread (vms_buf
, (bfd_size_type
) PRIV (buf_size
),
375 if (PRIV (rec_length
) <= 0)
377 bfd_set_error (bfd_error_file_truncated
);
380 PRIV (vms_rec
) = vms_buf
;
384 /* extract vms record length */
386 _bfd_vms_get_header_values (abfd
, vms_buf
+ test_start
, NULL
,
389 if (PRIV (rec_length
) <= 0)
391 bfd_set_error (bfd_error_file_truncated
);
395 /* that's what the linker manual says */
397 if (PRIV (rec_length
) > EOBJ_S_C_MAXRECSIZ
)
399 bfd_set_error (bfd_error_file_truncated
);
403 /* adjust the buffer */
405 if (PRIV (rec_length
) > PRIV (buf_size
))
407 PRIV (vms_buf
) = ((unsigned char *)
408 bfd_realloc (vms_buf
,
409 (bfd_size_type
) PRIV (rec_length
)));
410 vms_buf
= PRIV (vms_buf
);
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_bread remaining %d\n", remaining
);
423 if (bfd_bread (vms_buf
+ test_len
, (bfd_size_type
) 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_bread rec_length %d\n", PRIV (rec_length
));
436 return PRIV (rec_length
);
439 /* get next vms record from file
440 update vms_rec and rec_length to new (remaining) values */
443 _bfd_vms_next_record (abfd
)
447 vms_debug (8, "_bfd_vms_next_record (len %d, size %d)\n",
448 PRIV (rec_length
), PRIV (rec_size
));
451 if (PRIV (rec_length
) > 0)
453 PRIV (vms_rec
) += PRIV (rec_size
);
457 if (_bfd_vms_get_record (abfd
) <= 0)
461 if (!PRIV (vms_rec
) || !PRIV (vms_buf
)
462 || PRIV (vms_rec
) >= (PRIV (vms_buf
) + PRIV (buf_size
)))
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
);
486 /* Copy sized string (string with fixed length) to new allocated area
487 size is string length (size of record) */
490 _bfd_vms_save_sized_string (str
, size
)
494 char *newstr
= bfd_malloc ((bfd_size_type
) size
+ 1);
498 strncpy (newstr
, (char *) str
, (size_t) size
);
504 /* Copy counted string (string with length at first byte) to new allocated area
505 ptr points to length byte on entry */
508 _bfd_vms_save_counted_string (ptr
)
513 return _bfd_vms_save_sized_string (ptr
, len
);
516 /* stack routines for vms ETIR commands */
518 /* Push value and section index */
521 _bfd_vms_push (abfd
, val
, psect
)
526 static int last_psect
;
529 vms_debug (4, "<push %016lx(%d) at %d>\n", val
, psect
, PRIV (stackptr
));
535 PRIV (stack
[PRIV (stackptr
)]).value
= val
;
536 PRIV (stack
[PRIV (stackptr
)]).psect
= last_psect
;
538 if (PRIV (stackptr
) >= STACKSIZE
)
540 bfd_set_error (bfd_error_bad_value
);
541 (*_bfd_error_handler
) (_("Stack overflow (%d) in _bfd_vms_push"), PRIV (stackptr
));
547 /* Pop value and section index */
550 _bfd_vms_pop (abfd
, psect
)
556 if (PRIV (stackptr
) == 0)
558 bfd_set_error (bfd_error_bad_value
);
559 (*_bfd_error_handler
) (_("Stack underflow in _bfd_vms_pop"));
563 value
= PRIV (stack
[PRIV (stackptr
)]).value
;
564 if ((psect
!= NULL
) && (PRIV (stack
[PRIV (stackptr
)]).psect
>= 0))
565 *psect
= PRIV (stack
[PRIV (stackptr
)]).psect
;
568 vms_debug (4, "<pop %016lx(%d)>\n", value
, PRIV (stack
[PRIV (stackptr
)]).psect
);
574 /* object file output functions */
576 /* GAS tends to write sections in little chunks (bfd_set_section_contents)
577 which we can't use directly. So we save the little chunks in linked
578 lists (one per section) and write them later. */
580 /* Add a new vms_section structure to vms_section_table
581 - forward chaining - */
584 add_new_contents (abfd
, section
)
588 vms_section
*sptr
, *newptr
;
590 sptr
= PRIV (vms_section_table
)[section
->index
];
594 newptr
= (vms_section
*) bfd_alloc (abfd
,
595 (bfd_size_type
) sizeof (vms_section
));
596 if (newptr
== (vms_section
*) NULL
)
598 newptr
->contents
= (unsigned char *) bfd_alloc (abfd
, section
->_raw_size
);
599 if (newptr
->contents
== (unsigned char *) NULL
)
602 newptr
->size
= section
->_raw_size
;
604 PRIV (vms_section_table
)[section
->index
] = newptr
;
608 /* Save section data & offset to a vms_section structure
609 vms_section_table[] holds the vms_section chain. */
612 _bfd_save_vms_section (abfd
, section
, data
, offset
, count
)
621 if (section
->index
>= VMS_SECTION_COUNT
)
623 bfd_set_error (bfd_error_nonrepresentable_section
);
626 if (count
== (bfd_size_type
)0)
628 sptr
= add_new_contents (abfd
, section
);
631 memcpy (sptr
->contents
+ offset
, data
, (size_t) count
);
636 /* Get vms_section pointer to saved contents for section # index */
639 _bfd_get_vms_section (abfd
, index
)
643 if (index
>= VMS_SECTION_COUNT
)
645 bfd_set_error (bfd_error_nonrepresentable_section
);
648 return PRIV (vms_section_table
)[index
];
651 /* Object output routines */
653 /* Begin new record or record header
654 write 2 bytes rectype
655 write 2 bytes record length (filled in at flush)
656 write 2 bytes header type (ommitted if rechead == -1) */
659 _bfd_vms_output_begin (abfd
, rectype
, rechead
)
665 vms_debug (6, "_bfd_vms_output_begin(type %d, head %d)\n", rectype
,
669 _bfd_vms_output_short (abfd
, (unsigned int) rectype
);
671 /* save current output position to fill in lenght later */
673 if (PRIV (push_level
) > 0)
674 PRIV (length_pos
) = PRIV (output_size
);
677 vms_debug (6, "_bfd_vms_output_begin: length_pos = %d\n",
681 _bfd_vms_output_short (abfd
, 0); /* placeholder for length */
684 _bfd_vms_output_short (abfd
, (unsigned int) rechead
);
689 /* Set record/subrecord alignment */
692 _bfd_vms_output_alignment (abfd
, alignto
)
697 vms_debug (6, "_bfd_vms_output_alignment(%d)\n", alignto
);
700 PRIV (output_alignment
) = alignto
;
704 /* Prepare for subrecord fields */
707 _bfd_vms_output_push (abfd
)
711 vms_debug (6, "vms_output_push(pushed_size = %d)\n", PRIV (output_size
));
715 PRIV (pushed_size
) = PRIV (output_size
);
719 /* End of subrecord fields */
722 _bfd_vms_output_pop (abfd
)
726 vms_debug (6, "vms_output_pop(pushed_size = %d)\n", PRIV (pushed_size
));
729 _bfd_vms_output_flush (abfd
);
730 PRIV (length_pos
) = 2;
733 vms_debug (6, "vms_output_pop: length_pos = %d\n", PRIV (length_pos
));
736 PRIV (pushed_size
) = 0;
741 /* Flush unwritten output, ends current record */
744 _bfd_vms_output_flush (abfd
)
747 int real_size
= PRIV (output_size
);
752 vms_debug (6, "_bfd_vms_output_flush(real_size = %d, pushed_size %d at lenpos %d)\n",
753 real_size
, PRIV (pushed_size
), PRIV (length_pos
));
756 if (PRIV (push_level
) > 0)
757 length
= real_size
- PRIV (pushed_size
);
763 aligncount
= (PRIV (output_alignment
)
764 - (length
% PRIV (output_alignment
))) % PRIV (output_alignment
);
767 vms_debug (6, "align: adding %d bytes\n", aligncount
);
770 while (aligncount
-- > 0)
772 PRIV (output_buf
)[real_size
++] = 0;
774 /* this is why I *love* vms: inconsistency :-}
775 alignment is added to the subrecord length
776 but not to the record length */
777 if (PRIV (push_level
) > 0)
782 /* put length to buffer */
783 PRIV (output_size
) = PRIV (length_pos
);
784 _bfd_vms_output_short (abfd
, (unsigned int) length
);
786 if (PRIV (push_level
) == 0)
789 /* write length first, see FF_FOREIGN in the input routines */
790 fwrite (PRIV (output_buf
) + 2, 2, 1, (FILE *) abfd
->iostream
);
792 fwrite (PRIV (output_buf
), (size_t) real_size
, 1,
793 (FILE *) abfd
->iostream
);
795 PRIV (output_size
) = 0;
799 PRIV (output_size
) = real_size
;
800 PRIV (pushed_size
) = PRIV (output_size
);
806 /* End record output */
809 _bfd_vms_output_end (abfd
)
813 vms_debug (6, "_bfd_vms_output_end\n");
816 _bfd_vms_output_flush (abfd
);
821 /* check remaining buffer size
823 return what's left. */
826 _bfd_vms_output_check (abfd
, size
)
831 vms_debug (6, "_bfd_vms_output_check(%d)\n", size
);
834 return (MAX_OUTREC_SIZE
- (PRIV (output_size
) + size
+ MIN_OUTREC_LUFT
));
837 /* Output byte (8 bit) value */
840 _bfd_vms_output_byte (abfd
, value
)
845 vms_debug (6, "_bfd_vms_output_byte(%02x)\n", value
);
848 bfd_put_8 (abfd
, value
& 0xff, PRIV (output_buf
) + PRIV (output_size
));
849 PRIV (output_size
) += 1;
853 /* Output short (16 bit) value */
856 _bfd_vms_output_short (abfd
, value
)
861 vms_debug (6, "_bfd_vms_output_short (%04x)\n", value
);
864 bfd_put_16 (abfd
, (bfd_vma
) value
& 0xffff,
865 PRIV (output_buf
) + PRIV (output_size
));
866 PRIV (output_size
) += 2;
870 /* Output long (32 bit) value */
873 _bfd_vms_output_long (abfd
, value
)
878 vms_debug (6, "_bfd_vms_output_long (%08lx)\n", value
);
881 bfd_put_32 (abfd
, (bfd_vma
) value
, PRIV (output_buf
) + PRIV (output_size
));
882 PRIV (output_size
) += 4;
886 /* Output quad (64 bit) value */
889 _bfd_vms_output_quad (abfd
, value
)
894 vms_debug (6, "_bfd_vms_output_quad(%016lx)\n", value
);
897 bfd_put_64(abfd
, value
, PRIV (output_buf
) + PRIV (output_size
));
898 PRIV (output_size
) += 8;
902 /* Output c-string as counted string */
905 _bfd_vms_output_counted (abfd
, value
)
912 vms_debug (6, "_bfd_vms_output_counted(%s)\n", value
);
915 len
= strlen (value
);
918 (*_bfd_error_handler
) (_("_bfd_vms_output_counted called with zero bytes"));
923 (*_bfd_error_handler
) (_("_bfd_vms_output_counted called with too many bytes"));
926 _bfd_vms_output_byte (abfd
, (unsigned int) len
& 0xff);
927 _bfd_vms_output_dump (abfd
, (unsigned char *)value
, len
);
930 /* Output character area */
933 _bfd_vms_output_dump (abfd
, data
, length
)
939 vms_debug (6, "_bfd_vms_output_dump(%d)\n", length
);
945 memcpy (PRIV (output_buf
) + PRIV (output_size
), data
, (size_t) length
);
946 PRIV (output_size
) += length
;
951 /* Output count bytes of value */
954 _bfd_vms_output_fill (abfd
, value
, count
)
960 vms_debug (6, "_bfd_vms_output_fill(val %02x times %d)\n", value
, count
);
965 memset (PRIV (output_buf
) + PRIV (output_size
), value
, (size_t) count
);
966 PRIV (output_size
) += count
;
971 /* this hash routine borrowed from GNU-EMACS, and strengthened slightly ERY*/
977 register const unsigned char *p
= (unsigned char *) ptr
;
978 register const unsigned char *end
= p
+ strlen (ptr
);
979 register unsigned char c
;
980 register int hash
= 0;
985 hash
= ((hash
<< 3) + (hash
<< 15) + (hash
>> 28) + c
);
990 /* Generate a length-hashed VMS symbol name (limited to maxlen chars). */
993 _bfd_vms_length_hash_symbol (abfd
, in
, maxlen
)
1001 const char *old_name
;
1003 static char outbuf
[EOBJ_S_C_SYMSIZ
+1];
1007 vms_debug(4, "_bfd_vms_length_hash_symbol \"%s\"\n", in
);
1010 if (maxlen
> EOBJ_S_C_SYMSIZ
)
1011 maxlen
= EOBJ_S_C_SYMSIZ
;
1013 new_name
= out
; /* save this for later. */
1015 /* We may need to truncate the symbol, save the hash for later. */
1017 in_len
= strlen (in
);
1019 result
= (in_len
> maxlen
) ? hash_string (in
) : 0;
1023 /* Do the length checking. */
1025 if (in_len
<= maxlen
)
1031 if (PRIV (flag_hash_long_names
))
1037 strncpy (out
, in
, (size_t) i
);
1041 if ((in_len
> maxlen
)
1042 && PRIV (flag_hash_long_names
))
1043 sprintf (out
, "_%08lx", result
);
1048 vms_debug(4, "--> [%d]\"%s\"\n", strlen (outbuf
), outbuf
);
1052 && PRIV (flag_hash_long_names
)
1053 && PRIV (flag_show_after_trunc
))
1054 printf (_("Symbol %s replaced by %s\n"), old_name
, new_name
);
1059 /* Allocate and initialize a new symbol. */
1062 new_symbol (abfd
, name
)
1069 _bfd_vms_debug (7, "new_symbol %s\n", name
);
1072 symbol
= bfd_make_empty_symbol (abfd
);
1075 symbol
->name
= name
;
1076 symbol
->section
= bfd_make_section (abfd
, BFD_UND_SECTION_NAME
);
1081 /* Allocate and enter a new private symbol. */
1084 _bfd_vms_enter_symbol (abfd
, name
)
1088 vms_symbol_entry
*entry
;
1091 _bfd_vms_debug (6, "_bfd_vms_enter_symbol %s\n", name
);
1094 entry
= (vms_symbol_entry
*)
1095 bfd_hash_lookup (PRIV (vms_symbol_table
), name
, FALSE
, FALSE
);
1099 _bfd_vms_debug (8, "creating hash entry for %s\n", name
);
1101 entry
= (vms_symbol_entry
*) bfd_hash_lookup (PRIV (vms_symbol_table
),
1106 symbol
= new_symbol (abfd
, name
);
1109 entry
->symbol
= symbol
;
1110 PRIV (gsd_sym_count
)++;
1117 (*_bfd_error_handler
) (_("failed to enter %s"), name
);
1122 _bfd_vms_debug (8, "found hash entry for %s\n", name
);
1127 _bfd_vms_debug (7, "-> entry %p, entry->symbol %p\n", entry
, entry
->symbol
);