file as.info-5 was initially added on branch binutils-2_10-branch.
[binutils.git] / bfd / vms-misc.c
blob311192c8b9177cebbca04e5ea3f4dfd9268d0b35
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. */
22 #if __STDC__
23 #include <stdarg.h>
24 #endif
26 #include "bfd.h"
27 #include "sysdep.h"
28 #include "bfdlink.h"
29 #include "libbfd.h"
31 #include "vms.h"
33 /*-----------------------------------------------------------------------------*/
34 #if VMS_DEBUG
35 /* debug functions */
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
42 levels:
43 1 toplevel bfd calls (functions from the bfd vector)
44 2 functions called by bfd calls
45 ...
46 9 almost everything
48 level is also identation level. Indentation is performed
49 if level > 0
52 #if __STDC__
53 void
54 _bfd_vms_debug (int level, char *format, ...)
56 static int min_level = -1;
57 static FILE *output = NULL;
58 char *eptr;
59 va_list args;
60 int abslvl = (level > 0)?level:-level;
62 if (min_level == -1)
64 if ((eptr = getenv("VMS_DEBUG")) != NULL)
66 min_level = atoi(eptr);
67 output = stderr;
69 else
70 min_level = 0;
72 if (output == NULL)
73 return;
74 if (abslvl > min_level)
75 return;
77 while(--level>0)
78 fprintf(output, " ");
79 va_start(args, format);
80 vfprintf(output, format, args);
81 fflush(output);
82 va_end(args);
84 return;
87 #else /* not __STDC__ */
89 void
90 _bfd_vms_debug (level, format, a1, a2, a3, a4, a5, a6)
91 int level;
92 char *format;
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;
98 char *eptr;
100 if (min_level == -1)
102 if ((eptr = getenv("VMS_DEBUG")) != NULL)
104 min_level = atoi(eptr);
105 output = stderr;
107 else
108 min_level = 0;
110 if (output == NULL)
111 return;
112 if (level > min_level)
113 return;
115 while(--level>0)
116 fprintf(output, " ");
117 fprintf(output, format, a1, a2, a3, a4, a5, a6);
118 fflush(output);
120 return;
122 #endif /* __STDC__ */
125 /* a debug function
126 hex dump 'size' bytes starting at 'ptr' */
128 void
129 _bfd_hexdump (level, ptr, size, offset)
130 int level;
131 unsigned char *ptr;
132 int size;
133 int offset;
135 unsigned char *lptr = ptr;
136 int count = 0;
137 long start = offset;
139 while (size-- > 0)
141 if ((count%16) == 0)
142 vms_debug (level, "%08lx:", start);
143 vms_debug (-level, " %02x", *ptr++);
144 count++;
145 start++;
146 if (size == 0)
148 while ((count%16) != 0)
150 vms_debug (-level, " ");
151 count++;
154 if ((count%16) == 0)
156 vms_debug (-level, " ");
157 while (lptr < ptr)
159 vms_debug (-level, "%c", (*lptr < 32)?'.':*lptr);
160 lptr++;
162 vms_debug (-level, "\n");
165 if ((count%16) != 0)
166 vms_debug (-level, "\n");
168 return;
170 #endif
173 /* hash functions
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;
184 const char *string;
186 vms_symbol_entry *ret;
188 #if VMS_DEBUG
189 vms_debug (5, "_bfd_vms_hash_newfunc(%p, %p, %s)\n", entry, table, string);
190 #endif
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);
207 #if VMS_DEBUG
208 vms_debug (6, "_bfd_vms_hash_newfunc ret %p\n", ret);
209 #endif
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. */
221 void
222 _bfd_vms_get_header_values (abfd, buf, type, length)
223 bfd *abfd ATTRIBUTE_UNUSED;
224 unsigned char *buf;
225 int *type;
226 int *length;
228 if (type != 0)
229 *type = bfd_getl16 (buf);
230 buf += 2;
231 if (length != 0)
232 *length = bfd_getl16 (buf);
234 #if VMS_DEBUG
235 vms_debug (10, "_bfd_vms_get_header_values type %x, length %x\n", (type?*type:0), (length?*length:0));
236 #endif
239 return;
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
271 by get_header_values
272 then the read buffer is adjusted and the remaining bytes are
273 read in.
275 all file i/o is always done on even file positions */
278 _bfd_vms_get_record (abfd)
279 bfd *abfd;
281 int test_len, test_start, remaining;
282 unsigned char *vms_buf;
284 #if VMS_DEBUG
285 vms_debug (8, "_bfd_vms_get_record\n");
286 #endif
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)
296 if (PRIV(is_vax))
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;
302 else
303 PRIV(vms_buf) = (unsigned char *) malloc (6);
306 vms_buf = PRIV(vms_buf);
308 if (vms_buf == 0)
310 bfd_set_error (bfd_error_no_memory);
311 return -1;
314 switch (PRIV(file_format))
316 case FF_UNKNOWN:
317 case FF_FOREIGN:
318 test_len = 6; /* probe 6 bytes */
319 test_start = 2; /* where the record starts */
320 break;
322 case FF_NATIVE:
323 test_len = 4;
324 test_start = 0;
325 break;
327 case FF_VAX:
328 test_len = 0;
329 test_start = 0;
330 break;
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);
340 return 0;
344 /* read the record header on Alpha. */
346 if ((test_len != 0)
347 && (bfd_read (PRIV(vms_buf), 1, test_len, abfd)
348 != (bfd_size_type) test_len))
350 bfd_set_error (bfd_error_file_truncated);
351 return 0;
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 */
362 test_start = 2;
364 else
366 PRIV(file_format) = FF_NATIVE; /* N: native environment */
367 test_start = 0;
371 if (PRIV(is_vax))
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);
377 return 0;
379 PRIV(vms_rec) = vms_buf;
381 else /* Alpha */
383 /* extract vms record length */
385 _bfd_vms_get_header_values (abfd, vms_buf+test_start, NULL,
386 &PRIV(rec_length));
388 if (PRIV(rec_length) <= 0)
390 bfd_set_error (bfd_error_file_truncated);
391 return 0;
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);
399 return 0;
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);
408 if (vms_buf == 0)
410 bfd_set_error (bfd_error_no_memory);
411 return -1;
413 PRIV(buf_size) = PRIV(rec_length);
416 /* read the remaining record */
418 remaining = PRIV(rec_length) - test_len + test_start;
420 #if VMS_DEBUG
421 vms_debug (10, "bfd_read remaining %d\n", remaining);
422 #endif
423 if (bfd_read (vms_buf + test_len, 1, remaining, abfd) !=
424 (bfd_size_type) remaining)
426 bfd_set_error (bfd_error_file_truncated);
427 return 0;
429 PRIV(vms_rec) = vms_buf + test_start;
432 #if VMS_DEBUG
433 vms_debug (11, "bfd_read rec_length %d\n", PRIV(rec_length));
434 #endif
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)
445 bfd *abfd;
447 #if VMS_DEBUG
448 vms_debug (8, "_bfd_vms_next_record (len %d, size %d)\n",
449 PRIV(rec_length), PRIV(rec_size));
450 #endif
452 if (PRIV(rec_length) > 0)
454 PRIV(vms_rec) += PRIV(rec_size);
456 else
458 if (_bfd_vms_get_record (abfd) <= 0)
459 return -1;
462 if (PRIV(is_vax))
464 PRIV(rec_type) = *(PRIV(vms_rec));
465 PRIV(rec_size) = PRIV(rec_length);
467 else
469 _bfd_vms_get_header_values (abfd, PRIV(vms_rec), &PRIV(rec_type),
470 &PRIV(rec_size));
472 PRIV(rec_length) -= PRIV(rec_size);
474 #if VMS_DEBUG
475 vms_debug (8, "_bfd_vms_next_record: rec %p, size %d, length %d, type %d\n",
476 PRIV(vms_rec), PRIV(rec_size), PRIV(rec_length),
477 PRIV(rec_type));
478 #endif
480 return PRIV(rec_type);
485 /* Copy sized string (string with fixed length) to new allocated area
486 size is string length (size of record) */
488 char *
489 _bfd_vms_save_sized_string (str, size)
490 unsigned char *str;
491 int size;
493 char *newstr = bfd_malloc (size + 1);
495 if (newstr == NULL)
496 return 0;
497 strncpy (newstr, (char *)str, size);
498 newstr[size] = 0;
500 return newstr;
503 /* Copy counted string (string with length at first byte) to new allocated area
504 ptr points to length byte on entry */
506 char *
507 _bfd_vms_save_counted_string (ptr)
508 unsigned char *ptr;
510 int len = *ptr++;
512 return _bfd_vms_save_sized_string (ptr, len);
516 /* stack routines for vms ETIR commands */
518 /* Push value and section index */
520 void
521 _bfd_vms_push (abfd, val, psect)
522 bfd *abfd;
523 uquad val;
524 int psect;
526 static int last_psect;
528 #if VMS_DEBUG
529 vms_debug (4, "<push %016lx(%d) at %d>\n", val, psect, PRIV(stackptr));
530 #endif
532 if (psect >= 0)
533 last_psect = psect;
535 PRIV(stack[PRIV(stackptr)]).value = val;
536 PRIV(stack[PRIV(stackptr)]).psect = last_psect;
537 PRIV(stackptr)++;
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));
542 exit (1);
544 return;
548 /* Pop value and section index */
550 uquad
551 _bfd_vms_pop (abfd, psect)
552 bfd *abfd;
553 int *psect;
555 uquad value;
557 if (PRIV(stackptr) == 0)
559 bfd_set_error (bfd_error_bad_value);
560 (*_bfd_error_handler) (_("Stack underflow in _bfd_vms_pop"));
561 exit (1);
563 PRIV(stackptr)--;
564 value = PRIV(stack[PRIV(stackptr)]).value;
565 if ((psect != NULL) && (PRIV(stack[PRIV(stackptr)]).psect >= 0))
566 *psect = PRIV(stack[PRIV(stackptr)]).psect;
568 #if VMS_DEBUG
569 vms_debug (4, "<pop %016lx(%d)>\n", value, PRIV(stack[PRIV(stackptr)]).psect);
570 #endif
572 return value;
576 /* object file output functions */
578 /* GAS tends to write sections in little chunks (bfd_set_section_contents)
579 which we can't use directly. So we save the little chunks in linked
580 lists (one per section) and write them later. */
582 /* Add a new vms_section structure to vms_section_table
583 - forward chaining - */
585 static vms_section *
586 add_new_contents (abfd, section)
587 bfd *abfd;
588 sec_ptr section;
590 vms_section *sptr, *newptr;
592 sptr = PRIV(vms_section_table)[section->index];
593 if (sptr != NULL)
594 return sptr;
596 newptr = (vms_section *) bfd_malloc (sizeof (vms_section));
597 if (newptr == (vms_section *) NULL)
598 return NULL;
599 newptr->contents = (unsigned char *) bfd_alloc (abfd, (int)section->_raw_size);
600 if (newptr->contents == (unsigned char *)NULL)
601 return NULL;
602 newptr->offset = 0;
603 newptr->size = section->_raw_size;
604 newptr->next = 0;
605 PRIV(vms_section_table)[section->index] = newptr;
606 return newptr;
610 /* Save section data & offset to an vms_section structure
611 vms_section_table[] holds the vms_section chain */
613 boolean
614 _bfd_save_vms_section (abfd, section, data, offset, count)
615 bfd *abfd;
616 sec_ptr section;
617 PTR data;
618 file_ptr offset;
619 bfd_size_type count;
621 vms_section *sptr;
623 if (section->index >= VMS_SECTION_COUNT)
625 bfd_set_error (bfd_error_nonrepresentable_section);
626 return false;
628 if (count == (bfd_size_type)0)
629 return true;
630 sptr = add_new_contents (abfd, section);
631 if (sptr == NULL)
632 return false;
633 memcpy (sptr->contents + offset, data, (size_t) count);
635 return true;
639 /* Get vms_section pointer to saved contents for section # index */
641 vms_section *
642 _bfd_get_vms_section (abfd, index)
643 bfd *abfd;
644 int index;
646 if (index >= VMS_SECTION_COUNT)
648 bfd_set_error (bfd_error_nonrepresentable_section);
649 return NULL;
651 return PRIV(vms_section_table)[index];
655 /* Object output routines */
657 /* Begin new record or record header
658 write 2 bytes rectype
659 write 2 bytes record length (filled in at flush)
660 write 2 bytes header type (ommitted if rechead == -1) */
662 void
663 _bfd_vms_output_begin (abfd, rectype, rechead)
664 bfd *abfd;
665 int rectype;
666 int rechead;
668 #if VMS_DEBUG
669 vms_debug (6, "_bfd_vms_output_begin(type %d, head %d)\n", rectype,
670 rechead);
671 #endif
673 _bfd_vms_output_short (abfd,rectype);
675 /* save current output position to fill in lenght later */
677 if (PRIV(push_level) > 0)
678 PRIV(length_pos) = PRIV(output_size);
680 #if VMS_DEBUG
681 vms_debug (6, "_bfd_vms_output_begin: length_pos = %d\n",
682 PRIV(length_pos));
683 #endif
685 _bfd_vms_output_short (abfd,0); /* placeholder for length */
687 if (rechead != -1)
688 _bfd_vms_output_short (abfd,rechead);
690 return;
694 /* Set record/subrecord alignment */
696 void
697 _bfd_vms_output_alignment (abfd, alignto)
698 bfd *abfd;
699 int alignto;
701 #if VMS_DEBUG
702 vms_debug (6, "_bfd_vms_output_alignment(%d)\n", alignto);
703 #endif
705 PRIV(output_alignment) = alignto;
706 return;
710 /* Prepare for subrecord fields */
712 void
713 _bfd_vms_output_push (abfd)
714 bfd *abfd;
716 #if VMS_DEBUG
717 vms_debug (6, "vms_output_push(pushed_size = %d)\n", PRIV(output_size));
718 #endif
720 PRIV(push_level)++;
721 PRIV(pushed_size) = PRIV(output_size);
722 return;
726 /* End of subrecord fields */
728 void
729 _bfd_vms_output_pop (abfd)
730 bfd *abfd;
732 #if VMS_DEBUG
733 vms_debug (6, "vms_output_pop(pushed_size = %d)\n", PRIV(pushed_size));
734 #endif
736 _bfd_vms_output_flush (abfd);
737 PRIV(length_pos) = 2;
739 #if VMS_DEBUG
740 vms_debug (6, "vms_output_pop: length_pos = %d\n", PRIV(length_pos));
741 #endif
743 PRIV(pushed_size) = 0;
744 PRIV(push_level)--;
745 return;
749 /* Flush unwritten output, ends current record */
751 void
752 _bfd_vms_output_flush (abfd)
753 bfd *abfd;
755 int real_size = PRIV(output_size);
756 int aligncount;
757 int length;
759 #if VMS_DEBUG
760 vms_debug (6, "_bfd_vms_output_flush(real_size = %d, pushed_size %d at lenpos %d)\n",
761 real_size, PRIV(pushed_size), PRIV(length_pos));
762 #endif
764 if (PRIV(push_level) > 0)
765 length = real_size - PRIV(pushed_size);
766 else
767 length = real_size;
769 if (length == 0)
770 return;
771 aligncount = (PRIV(output_alignment)
772 - (length % PRIV(output_alignment))) % PRIV(output_alignment);
774 #if VMS_DEBUG
775 vms_debug (6, "align: adding %d bytes\n", aligncount);
776 #endif
778 while(aligncount-- > 0)
780 PRIV(output_buf)[real_size++] = 0;
781 #if 0
782 /* this is why I *love* vms: inconsistency :-}
783 alignment is added to the subrecord length
784 but not to the record length */
785 if (PRIV(push_level) > 0)
786 #endif
787 length++;
790 /* put length to buffer */
791 PRIV(output_size) = PRIV(length_pos);
792 _bfd_vms_output_short (abfd, (unsigned int)length);
794 if (PRIV(push_level) == 0)
796 #ifndef VMS
797 /* write length first, see FF_FOREIGN in the input routines */
798 fwrite (PRIV(output_buf)+2, 2, 1, (FILE *)abfd->iostream);
799 #endif
800 fwrite (PRIV(output_buf), real_size, 1, (FILE *)abfd->iostream);
802 PRIV(output_size) = 0;
804 else
806 PRIV(output_size) = real_size;
807 PRIV(pushed_size) = PRIV(output_size);
810 return;
814 /* End record output */
816 void
817 _bfd_vms_output_end (abfd)
818 bfd *abfd;
820 #if VMS_DEBUG
821 vms_debug (6, "_bfd_vms_output_end\n");
822 #endif
824 _bfd_vms_output_flush (abfd);
826 return;
830 /* check remaining buffer size
832 return what's left. */
835 _bfd_vms_output_check (abfd, size)
836 bfd *abfd;
837 int size;
839 #if VMS_DEBUG
840 vms_debug (6, "_bfd_vms_output_check(%d)\n", size);
841 #endif
843 return (MAX_OUTREC_SIZE - (PRIV(output_size) + size + MIN_OUTREC_LUFT));
847 /* Output byte (8 bit) value */
849 void
850 _bfd_vms_output_byte (abfd, value)
851 bfd *abfd;
852 unsigned int value;
854 #if VMS_DEBUG
855 vms_debug (6, "_bfd_vms_output_byte(%02x)\n", value);
856 #endif
858 bfd_put_8 (abfd, value & 0xff, PRIV(output_buf) + PRIV(output_size));
859 PRIV(output_size) += 1;
860 return;
864 /* Output short (16 bit) value */
866 void
867 _bfd_vms_output_short (abfd, value)
868 bfd *abfd;
869 unsigned int value;
871 #if VMS_DEBUG
872 vms_debug (6, "_bfd_vms_output_short (%04x)\n", value);
873 #endif
875 bfd_put_16 (abfd, value & 0xffff, PRIV(output_buf) + PRIV(output_size));
876 PRIV(output_size) += 2;
877 return;
881 /* Output long (32 bit) value */
883 void
884 _bfd_vms_output_long (abfd, value)
885 bfd *abfd;
886 unsigned long value;
888 #if VMS_DEBUG
889 vms_debug (6, "_bfd_vms_output_long (%08lx)\n", value);
890 #endif
892 bfd_put_32 (abfd, value, PRIV(output_buf) + PRIV(output_size));
893 PRIV(output_size) += 4;
894 return;
898 /* Output quad (64 bit) value */
900 void
901 _bfd_vms_output_quad (abfd, value)
902 bfd *abfd;
903 uquad value;
905 #if VMS_DEBUG
906 vms_debug (6, "_bfd_vms_output_quad(%016lx)\n", value);
907 #endif
909 bfd_put_64(abfd, value, PRIV(output_buf) + PRIV(output_size));
910 PRIV(output_size) += 8;
911 return;
915 /* Output c-string as counted string */
917 void
918 _bfd_vms_output_counted (abfd, value)
919 bfd *abfd;
920 char *value;
922 int len;
924 #if VMS_DEBUG
925 vms_debug (6, "_bfd_vms_output_counted(%s)\n", value);
926 #endif
928 len = strlen (value);
929 if (len == 0)
931 (*_bfd_error_handler) (_("_bfd_vms_output_counted called with zero bytes"));
932 return;
934 if (len > 255)
936 (*_bfd_error_handler) (_("_bfd_vms_output_counted called with too many bytes"));
937 return;
939 _bfd_vms_output_byte (abfd, len & 0xff);
940 _bfd_vms_output_dump (abfd, (unsigned char *)value, len);
944 /* Output character area */
946 void
947 _bfd_vms_output_dump (abfd, data, length)
948 bfd *abfd;
949 unsigned char *data;
950 int length;
952 #if VMS_DEBUG
953 vms_debug (6, "_bfd_vms_output_dump(%d)\n", length);
954 #endif
956 if (length == 0)
957 return;
959 memcpy (PRIV(output_buf) + PRIV(output_size), data, length);
960 PRIV(output_size) += length;
962 return;
966 /* Output count bytes of value */
968 void
969 _bfd_vms_output_fill (abfd, value, count)
970 bfd *abfd;
971 int value;
972 int count;
974 #if VMS_DEBUG
975 vms_debug (6, "_bfd_vms_output_fill(val %02x times %d)\n", value, count);
976 #endif
978 if (count == 0)
979 return;
980 memset (PRIV(output_buf) + PRIV(output_size), value, count);
981 PRIV(output_size) += count;
983 return;
986 /* this hash routine borrowed from GNU-EMACS, and strengthened slightly ERY*/
988 static int
989 hash_string (ptr)
990 const char *ptr;
992 register const unsigned char *p = (unsigned char *) ptr;
993 register const unsigned char *end = p + strlen (ptr);
994 register unsigned char c;
995 register int hash = 0;
997 while (p != end)
999 c = *p++;
1000 hash = ((hash << 3) + (hash << 15) + (hash >> 28) + c);
1002 return hash;
1005 /* Generate a length-hashed VMS symbol name (limited to maxlen chars). */
1007 char *
1008 _bfd_vms_length_hash_symbol (abfd, in, maxlen)
1009 bfd *abfd;
1010 const char *in;
1011 int maxlen;
1013 long int result;
1014 int in_len;
1015 char *new_name;
1016 const char *old_name;
1017 int i;
1018 static char outbuf[EOBJ_S_C_SYMSIZ+1];
1019 char *out = outbuf;
1021 #if VMS_DEBUG
1022 vms_debug(4, "_bfd_vms_length_hash_symbol \"%s\"\n", in);
1023 #endif
1025 if (maxlen > EOBJ_S_C_SYMSIZ)
1026 maxlen = EOBJ_S_C_SYMSIZ;
1028 new_name = out; /* save this for later. */
1030 /* We may need to truncate the symbol, save the hash for later. */
1032 in_len = strlen (in);
1034 result = (in_len > maxlen) ? hash_string (in) : 0;
1036 old_name = in;
1038 /* Do the length checking. */
1040 if (in_len <= maxlen)
1042 i = in_len;
1044 else
1046 if (PRIV(flag_hash_long_names))
1047 i = maxlen-9;
1048 else
1049 i = maxlen;
1052 strncpy (out, in, i);
1053 in += i;
1054 out += i;
1056 if ((in_len > maxlen)
1057 && PRIV(flag_hash_long_names))
1058 sprintf (out, "_%08lx", result);
1059 else
1060 *out = 0;
1062 #if VMS_DEBUG
1063 vms_debug(4, "--> [%d]\"%s\"\n", strlen (outbuf), outbuf);
1064 #endif
1066 if (in_len > maxlen
1067 && PRIV(flag_hash_long_names)
1068 && PRIV(flag_show_after_trunc))
1069 printf (_("Symbol %s replaced by %s\n"), old_name, new_name);
1071 return outbuf;
1075 /* Allocate and initialize a new symbol. */
1077 static asymbol *
1078 new_symbol (abfd, name)
1079 bfd *abfd;
1080 char *name;
1082 asymbol *symbol;
1084 #if VMS_DEBUG
1085 _bfd_vms_debug (7, "new_symbol %s\n", name);
1086 #endif
1088 symbol = _bfd_vms_make_empty_symbol (abfd);
1089 if (symbol == 0)
1090 return symbol;
1091 symbol->name = name;
1092 symbol->section = bfd_make_section (abfd, BFD_UND_SECTION_NAME);
1094 return symbol;
1098 /* Allocate and enter a new private symbol. */
1100 vms_symbol_entry *
1101 _bfd_vms_enter_symbol (abfd, name)
1102 bfd *abfd;
1103 char *name;
1105 vms_symbol_entry *entry;
1107 #if VMS_DEBUG
1108 _bfd_vms_debug (6, "_bfd_vms_enter_symbol %s\n", name);
1109 #endif
1111 entry = (vms_symbol_entry *)
1112 bfd_hash_lookup (PRIV(vms_symbol_table), name, false, false);
1113 if (entry == 0)
1115 #if VMS_DEBUG
1116 _bfd_vms_debug (8, "creating hash entry for %s\n", name);
1117 #endif
1118 entry = (vms_symbol_entry *)bfd_hash_lookup (PRIV(vms_symbol_table), name, true, false);
1119 if (entry != 0)
1121 asymbol *symbol;
1122 symbol = new_symbol (abfd, name);
1123 if (symbol != 0)
1125 entry->symbol = symbol;
1126 PRIV(gsd_sym_count)++;
1127 abfd->symcount++;
1129 else
1130 entry = 0;
1132 else
1133 (*_bfd_error_handler) (_("failed to enter %s"), name);
1135 else
1137 #if VMS_DEBUG
1138 _bfd_vms_debug (8, "found hash entry for %s\n", name);
1139 #endif
1142 #if VMS_DEBUG
1143 _bfd_vms_debug (7, "-> entry %p, entry->symbol %p\n", entry, entry->symbol);
1144 #endif
1145 return entry;