Initial revision
[binutils.git] / bfd / vms-gsd.c
blob6a52462a06d22405c83e68f4a6af4e19208f69a6
1 /* vms-gsd.c -- BFD back-end for VAX (openVMS/VAX) and
2 EVAX (openVMS/Alpha) files.
3 Copyright 1996, 1997, 1998 Free Software Foundation Inc.
5 go and read the openVMS linker manual (esp. appendix B)
6 if you don't know what's going on here :-)
8 Written by Klaus K"ampf (kkaempf@rmi.de)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include <ctype.h>
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "bfdlink.h"
30 #include "libbfd.h"
32 #include "vms.h"
34 /*-----------------------------------------------------------------------------*/
36 /* typical sections for vax object files */
38 #define VAX_CODE_NAME "$CODE"
39 #define VAX_DATA_NAME "$DATA"
40 #define VAX_ADDRESS_DATA_NAME "$ADDRESS_DATA"
42 /* typical sections for evax object files */
44 #define EVAX_ABS_NAME "$ABS$"
45 #define EVAX_CODE_NAME "$CODE$"
46 #define EVAX_LINK_NAME "$LINK$"
47 #define EVAX_DATA_NAME "$DATA$"
48 #define EVAX_BSS_NAME "$BSS$"
49 #define EVAX_READONLYADDR_NAME "$READONLY_ADDR$"
50 #define EVAX_READONLY_NAME "$READONLY$"
51 #define EVAX_LITERAL_NAME "$LITERAL$"
52 #define EVAX_COMMON_NAME "$COMMON$"
53 #define EVAX_LOCAL_NAME "$LOCAL$"
55 struct sec_flags_struct {
56 char *name; /* name of section */
57 int vflags_always;
58 flagword flags_always; /* flags we set always */
59 int vflags_hassize;
60 flagword flags_hassize; /* flags we set if the section has a size > 0 */
63 /* These flags are deccrtl/vaxcrtl (openVMS 6.2 VAX) compatible */
65 static struct sec_flags_struct vax_section_flags[] = {
66 { VAX_CODE_NAME,
67 (GPS_S_M_PIC|GPS_S_M_REL|GPS_S_M_SHR|GPS_S_M_EXE|GPS_S_M_RD),
68 (SEC_CODE),
69 (GPS_S_M_PIC|GPS_S_M_REL|GPS_S_M_SHR|GPS_S_M_EXE|GPS_S_M_RD),
70 (SEC_IN_MEMORY|SEC_CODE|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
71 { VAX_DATA_NAME,
72 (GPS_S_M_PIC|GPS_S_M_REL|GPS_S_M_RD|GPS_S_M_WRT),
73 (SEC_DATA),
74 (GPS_S_M_PIC|GPS_S_M_REL|GPS_S_M_RD|GPS_S_M_WRT),
75 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
76 { VAX_ADDRESS_DATA_NAME,
77 (GPS_S_M_PIC|GPS_S_M_REL|GPS_S_M_RD),
78 (SEC_DATA|SEC_READONLY),
79 (GPS_S_M_PIC|GPS_S_M_REL|GPS_S_M_RD),
80 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_READONLY|SEC_LOAD) },
81 { NULL,
82 (GPS_S_M_PIC|GPS_S_M_OVR|GPS_S_M_REL|GPS_S_M_GBL|GPS_S_M_RD|GPS_S_M_WRT),
83 (SEC_DATA),
84 (GPS_S_M_PIC|GPS_S_M_OVR|GPS_S_M_REL|GPS_S_M_GBL|GPS_S_M_RD|GPS_S_M_WRT),
85 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) }
89 /* These flags are deccrtl/vaxcrtl (openVMS 6.2 Alpha) compatible */
91 static struct sec_flags_struct evax_section_flags[] = {
92 { EVAX_ABS_NAME,
93 (EGPS_S_V_SHR),
94 (SEC_DATA),
95 (EGPS_S_V_SHR),
96 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
97 { EVAX_CODE_NAME,
98 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_EXE),
99 (SEC_CODE),
100 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_EXE),
101 (SEC_IN_MEMORY|SEC_CODE|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
102 { EVAX_LITERAL_NAME,
103 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_RD|EGPS_S_V_NOMOD),
104 (SEC_DATA|SEC_READONLY),
105 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_RD),
106 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_READONLY|SEC_LOAD) },
107 { EVAX_LINK_NAME,
108 (EGPS_S_V_REL|EGPS_S_V_RD),
109 (SEC_DATA|SEC_READONLY),
110 (EGPS_S_V_REL|EGPS_S_V_RD),
111 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_READONLY|SEC_LOAD) },
112 { EVAX_DATA_NAME,
113 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT|EGPS_S_V_NOMOD),
114 (SEC_DATA),
115 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
116 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
117 { EVAX_BSS_NAME,
118 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT|EGPS_S_V_NOMOD),
119 (SEC_NO_FLAGS),
120 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT|EGPS_S_V_NOMOD),
121 (SEC_IN_MEMORY|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
122 { EVAX_READONLYADDR_NAME,
123 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_RD),
124 (SEC_DATA|SEC_READONLY),
125 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_RD),
126 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_READONLY|SEC_LOAD) },
127 { EVAX_READONLY_NAME,
128 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_RD|EGPS_S_V_NOMOD),
129 (SEC_DATA|SEC_READONLY),
130 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_RD),
131 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_READONLY|SEC_LOAD) },
132 { EVAX_LOCAL_NAME,
133 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
134 (SEC_DATA),
135 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
136 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
137 { NULL,
138 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
139 (SEC_DATA),
140 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
141 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) }
144 /* Retrieve bfd section flags by name and size */
146 static flagword
147 vms_secflag_by_name (abfd, section_flags, name, size)
148 bfd *abfd;
149 struct sec_flags_struct *section_flags;
150 char *name;
151 int size;
153 int i = 0;
155 while (section_flags[i].name != NULL)
157 if ((PRIV(is_vax)?
158 strcasecmp (name, section_flags[i].name):
159 strcmp (name, section_flags[i].name)) == 0)
161 if (size > 0)
162 return section_flags[i].flags_hassize;
163 else
164 return section_flags[i].flags_always;
166 i++;
168 if (size > 0)
169 return section_flags[i].flags_hassize;
170 return section_flags[i].flags_always;
174 /* Retrieve vms section flags by name and size */
176 static flagword
177 vms_esecflag_by_name (section_flags, name, size)
178 struct sec_flags_struct *section_flags;
179 char *name;
180 int size;
182 int i = 0;
184 while (section_flags[i].name != NULL)
186 if (strcmp (name, section_flags[i].name) == 0)
188 if (size > 0)
189 return section_flags[i].vflags_hassize;
190 else
191 return section_flags[i].vflags_always;
193 i++;
195 if (size > 0)
196 return section_flags[i].vflags_hassize;
197 return section_flags[i].vflags_always;
200 /*-----------------------------------------------------------------------------*/
201 #if VMS_DEBUG
202 /* debug */
204 struct flagdescstruct { char *name; flagword value; };
206 /* Convert flag to printable string */
208 static char *
209 flag2str(flagdesc, flags)
210 struct flagdescstruct *flagdesc;
211 flagword flags;
214 static char res[64];
215 int next = 0;
217 res[0] = 0;
218 while (flagdesc->name != NULL)
220 if ((flags & flagdesc->value) != 0)
222 if (next)
223 strcat(res, ",");
224 else
225 next = 1;
226 strcat (res, flagdesc->name);
228 flagdesc++;
230 return res;
232 #endif
234 /*-----------------------------------------------------------------------------*/
235 /* input routines */
237 /* Process GSD/EGSD record
238 return 0 on success, -1 on error */
241 _bfd_vms_slurp_gsd (abfd, objtype)
242 bfd *abfd;
243 int objtype;
245 #if VMS_DEBUG
246 static struct flagdescstruct gpsflagdesc[] =
248 { "PIC", 0x0001 },
249 { "LIB", 0x0002 },
250 { "OVR", 0x0004 },
251 { "REL", 0x0008 },
252 { "GBL", 0x0010 },
253 { "SHR", 0x0020 },
254 { "EXE", 0x0040 },
255 { "RD", 0x0080 },
256 { "WRT", 0x0100 },
257 { "VEC", 0x0200 },
258 { "NOMOD", 0x0400 },
259 { "COM", 0x0800 },
260 { NULL, 0 }
263 static struct flagdescstruct gsyflagdesc[] =
265 { "WEAK", 0x0001 },
266 { "DEF", 0x0002 },
267 { "UNI", 0x0004 },
268 { "REL", 0x0008 },
269 { "COMM", 0x0010 },
270 { "VECEP", 0x0020 },
271 { "NORM", 0x0040 },
272 { NULL, 0 }
274 #endif
276 int gsd_type, gsd_size;
277 asection *section;
278 unsigned char *vms_rec;
279 flagword new_flags, old_flags;
280 char *name;
281 asymbol *symbol;
282 vms_symbol_entry *entry;
283 unsigned long base_addr;
284 unsigned long align_addr;
285 static int psect_idx = 0;
287 #if VMS_DEBUG
288 vms_debug (2, "GSD/EGSD (%d/%x)\n", objtype, objtype);
289 #endif
291 switch (objtype)
293 case EOBJ_S_C_EGSD:
294 PRIV(vms_rec) += 8; /* skip type, size, l_temp */
295 PRIV(rec_size) -= 8;
296 break;
297 case OBJ_S_C_GSD:
298 PRIV(vms_rec) += 1;
299 PRIV(rec_size) -= 1;
300 break;
301 default:
302 return -1;
305 /* calculate base address for each section */
306 base_addr = 0L;
308 abfd->symcount = 0;
310 while (PRIV(rec_size) > 0)
312 vms_rec = PRIV(vms_rec);
314 if (objtype == OBJ_S_C_GSD)
316 gsd_type = *vms_rec;
318 else
320 _bfd_vms_get_header_values (abfd, vms_rec, &gsd_type, &gsd_size);
321 gsd_type += EVAX_OFFSET;
324 #if VMS_DEBUG
325 vms_debug (3, "gsd_type %d\n", gsd_type);
326 #endif
328 switch (gsd_type)
330 case GSD_S_C_PSC:
333 * program section definition
336 asection *old_section = 0;
338 #if VMS_DEBUG
339 vms_debug (4, "GSD_S_C_PSC\n");
340 #endif
341 /* If this section isn't a bfd section. */
343 if (PRIV(is_vax) && (psect_idx < (abfd->section_count-1)))
345 /* check for temporary section from TIR record. */
347 if (psect_idx < PRIV(section_count))
348 old_section = PRIV(sections)[psect_idx];
349 else
350 old_section = 0;
353 name = _bfd_vms_save_counted_string (vms_rec + 8);
354 section = bfd_make_section (abfd, name);
355 if (!section)
357 fprintf (stderr, "bfd_make_section (%s) failed\n", name);
358 return -1;
360 old_flags = bfd_getl16 (vms_rec + 2);
361 section->_raw_size = bfd_getl32(vms_rec + 4); /* allocation */
362 new_flags = vms_secflag_by_name (abfd, vax_section_flags, name, section->_raw_size);
363 if (old_flags & EGPS_S_V_REL)
364 new_flags |= SEC_RELOC;
365 if (old_flags & GPS_S_M_OVR)
366 new_flags |= SEC_IS_COMMON;
367 if (!bfd_set_section_flags (abfd, section, new_flags))
369 fprintf (stderr, "bfd_set_section_flags (%s, %x) failed\n", name, new_flags);
370 return -1;
372 section->alignment_power = vms_rec[1];
373 align_addr = (1 << section->alignment_power);
374 if ((base_addr % align_addr) != 0)
375 base_addr += (align_addr - (base_addr % align_addr));
376 section->vma = (bfd_vma)base_addr;
377 base_addr += section->_raw_size;
379 /* global section is common symbol */
381 if (old_flags & GPS_S_M_GBL)
383 entry = _bfd_vms_enter_symbol (abfd, name);
384 if (entry == (vms_symbol_entry *)NULL)
386 bfd_set_error (bfd_error_no_memory);
387 return -1;
389 symbol = entry->symbol;
391 symbol->value = 0;
392 symbol->section = section;
393 symbol->flags = (BSF_GLOBAL|BSF_SECTION_SYM|BSF_OLD_COMMON);
396 /* copy saved contents if old_section set */
398 if (old_section != 0)
400 section->contents = old_section->contents;
401 if (section->_raw_size < old_section->_raw_size)
403 fprintf (stderr, "Size mismatch section %s=%d, %s=%d\n", old_section->name, old_section->_raw_size, section->name, section->_raw_size);
404 return -1;
406 else if (section->_raw_size > old_section->_raw_size)
408 section->contents = ((unsigned char *)
409 bfd_realloc (old_section->contents, section->_raw_size));
410 if (section->contents == NULL)
412 bfd_set_error (bfd_error_no_memory);
413 return -1;
417 else
419 section->contents = ((unsigned char *)
420 bfd_malloc (section->_raw_size));
421 if (section->contents == NULL)
423 bfd_set_error (bfd_error_no_memory);
424 return -1;
426 memset (section->contents, 0, (size_t)section->_raw_size);
428 section->_cooked_size = section->_raw_size;
429 #if VMS_DEBUG
430 vms_debug (4, "gsd psc %d (%s, flags %04x=%s) ",
431 section->index, name, old_flags, flag2str (gpsflagdesc, old_flags));
432 vms_debug (4, "%d bytes at 0x%08lx (mem %p)\n",
433 section->_raw_size, section->vma, section->contents);
434 #endif
436 gsd_size = vms_rec[8] + 9;
438 psect_idx++;
440 break;
442 case GSD_S_C_EPM:
443 case GSD_S_C_EPMW:
444 #if VMS_DEBUG
445 vms_debug(4, "gsd epm\n");
446 #endif
447 /*FALLTHRU*/
448 case GSD_S_C_SYM:
449 case GSD_S_C_SYMW:
451 int name_offset, value_offset;
454 * symbol specification (definition or reference)
457 #if VMS_DEBUG
458 vms_debug (4, "GSD_S_C_SYM(W)\n");
459 #endif
460 old_flags = bfd_getl16 (vms_rec + 2);
461 new_flags = BSF_NO_FLAGS;
463 if (old_flags & GSY_S_M_WEAK)
464 new_flags |= BSF_WEAK;
466 switch (gsd_type)
468 case GSD_S_C_EPM:
469 name_offset = 11;
470 value_offset = 5;
471 new_flags |= BSF_FUNCTION;
472 break;
473 case GSD_S_C_EPMW:
474 name_offset = 12;
475 value_offset = 6;
476 new_flags |= BSF_FUNCTION;
477 break;
478 case GSD_S_C_SYM:
479 if (old_flags & GSY_S_M_DEF) /* symbol definition */
480 name_offset = 9;
481 else
482 name_offset = 4;
483 value_offset = 5;
484 break;
485 case GSD_S_C_SYMW:
486 if (old_flags & GSY_S_M_DEF) /* symbol definition */
487 name_offset = 10;
488 else
489 name_offset = 5;
490 value_offset = 6;
491 break;
494 /* save symbol in vms_symbol_table */
496 entry = _bfd_vms_enter_symbol (abfd,
497 _bfd_vms_save_counted_string (vms_rec + name_offset));
498 if (entry == (vms_symbol_entry *)NULL)
500 bfd_set_error (bfd_error_no_memory);
501 return -1;
503 symbol = entry->symbol;
505 if (old_flags & GSY_S_M_DEF) /* symbol definition */
507 int psect;
509 symbol->value = bfd_getl32 (vms_rec+value_offset);
510 if ((gsd_type == GSD_S_C_SYMW)
511 || (gsd_type == GSD_S_C_EPMW))
512 psect = bfd_getl16 (vms_rec + value_offset - 2);
513 else
514 psect = vms_rec[value_offset-1];
516 symbol->section = (asection *)psect;
517 #if VMS_DEBUG
518 vms_debug(4, "gsd sym def #%d (%s, %d [%p], %04x=%s)\n", abfd->symcount,
519 symbol->name, (int)symbol->section, symbol->section, old_flags, flag2str(gsyflagdesc, old_flags));
520 #endif
522 else /* symbol reference */
524 symbol->section = bfd_make_section (abfd, BFD_UND_SECTION_NAME);
525 #if VMS_DEBUG
526 vms_debug (4, "gsd sym ref #%d (%s, %s [%p], %04x=%s)\n", abfd->symcount,
527 symbol->name, symbol->section->name, symbol->section, old_flags, flag2str (gsyflagdesc, old_flags));
528 #endif
531 gsd_size = vms_rec[name_offset] + name_offset + 1;
532 symbol->flags = new_flags;
535 break;
537 case GSD_S_C_PRO:
538 case GSD_S_C_PROW:
539 #if VMS_DEBUG
540 vms_debug(4, "gsd pro\n");
541 #endif
542 break;
543 case GSD_S_C_IDC:
544 #if VMS_DEBUG
545 vms_debug(4, "gsd idc\n");
546 #endif
547 break;
548 case GSD_S_C_ENV:
549 #if VMS_DEBUG
550 vms_debug(4, "gsd env\n");
551 #endif
552 break;
553 case GSD_S_C_LSY:
554 #if VMS_DEBUG
555 vms_debug(4, "gsd lsy\n");
556 #endif
557 break;
558 case GSD_S_C_LEPM:
559 #if VMS_DEBUG
560 vms_debug(4, "gsd lepm\n");
561 #endif
562 break;
563 case GSD_S_C_LPRO:
564 #if VMS_DEBUG
565 vms_debug(4, "gsd lpro\n");
566 #endif
567 break;
568 case GSD_S_C_SPSC:
569 #if VMS_DEBUG
570 vms_debug(4, "gsd spsc\n");
571 #endif
572 break;
573 case GSD_S_C_SYMV:
574 #if VMS_DEBUG
575 vms_debug(4, "gsd symv\n");
576 #endif
577 break;
578 case GSD_S_C_EPMV:
579 #if VMS_DEBUG
580 vms_debug(4, "gsd epmv\n");
581 #endif
582 break;
583 case GSD_S_C_PROV:
584 #if VMS_DEBUG
585 vms_debug(4, "gsd prov\n");
586 #endif
587 break;
589 case EGSD_S_C_PSC + EVAX_OFFSET:
591 /* program section definition */
593 name = _bfd_vms_save_counted_string (vms_rec+12);
594 section = bfd_make_section (abfd, name);
595 if (!section)
596 return -1;
597 old_flags = bfd_getl16 (vms_rec + 6);
598 section->_raw_size = bfd_getl32 (vms_rec + 8); /* allocation */
599 new_flags = vms_secflag_by_name (abfd, evax_section_flags, name, (int) section->_raw_size);
600 if (old_flags & EGPS_S_V_REL)
601 new_flags |= SEC_RELOC;
602 if (!bfd_set_section_flags (abfd, section, new_flags))
603 return -1;
604 section->alignment_power = vms_rec[4];
605 align_addr = (1 << section->alignment_power);
606 if ((base_addr % align_addr) != 0)
607 base_addr += (align_addr - (base_addr % align_addr));
608 section->vma = (bfd_vma)base_addr;
609 base_addr += section->_raw_size;
610 section->contents = ((unsigned char *)
611 bfd_malloc (section->_raw_size));
612 if (section->contents == NULL)
613 return -1;
614 memset (section->contents, 0, (size_t) section->_raw_size);
615 section->_cooked_size = section->_raw_size;
616 #if VMS_DEBUG
617 vms_debug(4, "egsd psc %d (%s, flags %04x=%s) ",
618 section->index, name, old_flags, flag2str(gpsflagdesc, old_flags));
619 vms_debug(4, "%d bytes at 0x%08lx (mem %p)\n",
620 section->_raw_size, section->vma, section->contents);
621 #endif
623 break;
625 case EGSD_S_C_SYM + EVAX_OFFSET:
627 /* symbol specification (definition or reference) */
629 symbol = _bfd_vms_make_empty_symbol (abfd);
630 if (symbol == 0)
631 return -1;
633 old_flags = bfd_getl16 (vms_rec + 6);
634 new_flags = BSF_NO_FLAGS;
636 if (old_flags & EGSY_S_V_WEAK)
637 new_flags |= BSF_WEAK;
639 if (vms_rec[6] & EGSY_S_V_DEF) /* symbol definition */
641 symbol->name =
642 _bfd_vms_save_counted_string (vms_rec+32);
643 if (old_flags & EGSY_S_V_NORM)
644 { /* proc def */
645 new_flags |= BSF_FUNCTION;
647 symbol->value = bfd_getl64 (vms_rec+8);
648 symbol->section = (asection *)((unsigned long) bfd_getl32 (vms_rec+28));
649 #if VMS_DEBUG
650 vms_debug(4, "egsd sym def #%d (%s, %d, %04x=%s)\n", abfd->symcount,
651 symbol->name, (int)symbol->section, old_flags, flag2str(gsyflagdesc, old_flags));
652 #endif
654 else /* symbol reference */
656 symbol->name =
657 _bfd_vms_save_counted_string (vms_rec+8);
658 #if VMS_DEBUG
659 vms_debug(4, "egsd sym ref #%d (%s, %04x=%s)\n", abfd->symcount,
660 symbol->name, old_flags, flag2str(gsyflagdesc, old_flags));
661 #endif
662 symbol->section = bfd_make_section (abfd, BFD_UND_SECTION_NAME);
665 symbol->flags = new_flags;
667 /* save symbol in vms_symbol_table */
669 entry = (vms_symbol_entry *) bfd_hash_lookup (PRIV(vms_symbol_table), symbol->name, true, false);
670 if (entry == (vms_symbol_entry *)NULL)
672 bfd_set_error (bfd_error_no_memory);
673 return -1;
675 if (entry->symbol != (asymbol *)NULL)
676 { /* FIXME ?, DEC C generates this */
677 #if VMS_DEBUG
678 vms_debug(4, "EGSD_S_C_SYM: duplicate \"%s\"\n", symbol->name);
679 #endif
681 else
683 entry->symbol = symbol;
684 PRIV(gsd_sym_count)++;
685 abfd->symcount++;
688 break;
690 case EGSD_S_C_IDC + EVAX_OFFSET:
691 break;
693 default:
694 (*_bfd_error_handler) (_("unknown gsd/egsd subtype %d"), gsd_type);
695 bfd_set_error (bfd_error_bad_value);
696 return -1;
698 } /* switch */
700 PRIV(rec_size) -= gsd_size;
701 PRIV(vms_rec) += gsd_size;
703 } /* while (recsize > 0) */
705 if (abfd->symcount > 0)
706 abfd->flags |= HAS_SYMS;
708 return 0;
711 /*-----------------------------------------------------------------------------*/
712 /* output routines */
714 /* Write section and symbol directory of bfd abfd */
717 _bfd_vms_write_gsd (abfd, objtype)
718 bfd *abfd;
719 int objtype;
721 asection *section;
722 asymbol *symbol;
723 int symnum;
724 int last_index = -1;
725 char dummy_name[10];
726 char *sname;
727 flagword new_flags, old_flags;
728 char *nptr, *uptr;
730 #if VMS_DEBUG
731 vms_debug (2, "vms_write_gsd (%p, %d)\n", abfd, objtype);
732 #endif
734 /* output sections */
736 section = abfd->sections;
737 #if VMS_DEBUG
738 vms_debug (3, "%d sections found\n", abfd->section_count);
739 #endif
741 /* egsd is quadword aligned */
743 _bfd_vms_output_alignment (abfd, 8);
745 _bfd_vms_output_begin (abfd, EOBJ_S_C_EGSD, -1);
746 _bfd_vms_output_long (abfd, 0);
747 _bfd_vms_output_push (abfd); /* prepare output for subrecords */
749 while (section != 0)
751 #if VMS_DEBUG
752 vms_debug (3, "Section #%d %s, %d bytes\n", section->index, section->name, (int)section->_raw_size);
753 #endif
755 /* 13 bytes egsd, max 31 chars name -> should be 44 bytes */
756 if (_bfd_vms_output_check (abfd, 64) < 0)
758 _bfd_vms_output_pop (abfd);
759 _bfd_vms_output_end (abfd);
760 _bfd_vms_output_begin (abfd, EOBJ_S_C_EGSD, -1);
761 _bfd_vms_output_long (abfd, 0);
762 _bfd_vms_output_push (abfd); /* prepare output for subrecords */
765 /* Create dummy sections to keep consecutive indices */
767 while (section->index - last_index > 1)
769 #if VMS_DEBUG
770 vms_debug (3, "index %d, last %d\n", section->index, last_index);
771 #endif
772 _bfd_vms_output_begin (abfd, EGSD_S_C_PSC, -1);
773 _bfd_vms_output_short (abfd, 0);
774 _bfd_vms_output_short (abfd, 0);
775 _bfd_vms_output_long (abfd, 0);
776 sprintf (dummy_name, ".DUMMY%02d", last_index);
777 _bfd_vms_output_counted (abfd, dummy_name);
778 _bfd_vms_output_flush (abfd);
779 last_index++;
782 /* Don't know if this is neccesary for the linker but for now it keeps
783 vms_slurp_gsd happy */
785 sname = (char *)section->name;
786 if (*sname == '.')
788 sname++;
789 if ((*sname == 't') && (strcmp (sname, "text") == 0))
790 sname = PRIV(is_vax)?VAX_CODE_NAME:EVAX_CODE_NAME;
791 else if ((*sname == 'd') && (strcmp (sname, "data") == 0))
792 sname = PRIV(is_vax)?VAX_DATA_NAME:EVAX_DATA_NAME;
793 else if ((*sname == 'b') && (strcmp (sname, "bss") == 0))
794 sname = EVAX_BSS_NAME;
795 else if ((*sname == 'l') && (strcmp (sname, "link") == 0))
796 sname = EVAX_LINK_NAME;
797 else if ((*sname == 'r') && (strcmp (sname, "rdata") == 0))
798 sname = EVAX_READONLY_NAME;
799 else if ((*sname == 'l') && (strcmp (sname, "literal") == 0))
800 sname = EVAX_LITERAL_NAME;
801 else if ((*sname == 'c') && (strcmp (sname, "comm") == 0))
802 sname = EVAX_COMMON_NAME;
803 else if ((*sname == 'l') && (strcmp (sname, "lcomm") == 0))
804 sname = EVAX_LOCAL_NAME;
806 else
807 sname = _bfd_vms_length_hash_symbol (abfd, sname, EOBJ_S_C_SECSIZ);
809 _bfd_vms_output_begin (abfd, EGSD_S_C_PSC, -1);
810 _bfd_vms_output_short (abfd, section->alignment_power & 0xff);
811 if (bfd_is_com_section (section))
813 new_flags = (EGPS_S_V_OVR|EGPS_S_V_REL|EGPS_S_V_GBL|EGPS_S_V_RD|EGPS_S_V_WRT|EGPS_S_V_NOMOD|EGPS_S_V_COM);
815 else
817 new_flags = vms_esecflag_by_name (evax_section_flags, sname, section->_raw_size);
819 _bfd_vms_output_short (abfd, new_flags);
820 _bfd_vms_output_long (abfd, section->_raw_size);
821 _bfd_vms_output_counted (abfd, sname);
822 _bfd_vms_output_flush (abfd);
824 last_index = section->index;
825 section = section->next;
828 /* output symbols */
830 #if VMS_DEBUG
831 vms_debug (3, "%d symbols found\n", abfd->symcount);
832 #endif
834 bfd_set_start_address (abfd, (bfd_vma)-1);
836 for (symnum = 0; symnum < abfd->symcount; symnum++)
839 symbol = abfd->outsymbols[symnum];
840 if (*(symbol->name) == '_')
842 if (strcmp (symbol->name, "__main") == 0)
843 bfd_set_start_address (abfd, (bfd_vma)symbol->value);
845 old_flags = symbol->flags;
847 if (old_flags & BSF_FILE)
848 continue;
850 if (((old_flags & (BSF_GLOBAL|BSF_WEAK)) == 0) /* not xdef */
851 && (!bfd_is_und_section (symbol->section))) /* and not xref */
852 continue; /* dont output */
854 /* 13 bytes egsd, max 64 chars name -> should be 77 bytes */
856 if (_bfd_vms_output_check (abfd, 80) < 0)
858 _bfd_vms_output_pop (abfd);
859 _bfd_vms_output_end (abfd);
860 _bfd_vms_output_begin (abfd, EOBJ_S_C_EGSD, -1);
861 _bfd_vms_output_long (abfd, 0);
862 _bfd_vms_output_push (abfd); /* prepare output for subrecords */
865 _bfd_vms_output_begin (abfd, EGSD_S_C_SYM, -1);
867 _bfd_vms_output_short (abfd, 0); /* data type, alignment */
869 new_flags = 0;
871 if (old_flags & BSF_WEAK)
872 new_flags |= EGSY_S_V_WEAK;
873 if (bfd_is_com_section (symbol->section)) /* .comm */
874 new_flags |= (EGSY_S_V_WEAK|EGSY_S_V_COMM);
876 if (old_flags & BSF_FUNCTION)
878 new_flags |= EGSY_S_V_NORM;
879 new_flags |= EGSY_S_V_REL;
881 if (old_flags & (BSF_GLOBAL|BSF_WEAK))
883 new_flags |= EGSY_S_V_DEF;
884 if (!bfd_is_abs_section (symbol->section))
885 new_flags |= EGSY_S_V_REL;
887 _bfd_vms_output_short (abfd, new_flags);
889 if (old_flags & (BSF_GLOBAL|BSF_WEAK)) /* symbol definition */
891 if (old_flags & BSF_FUNCTION)
893 _bfd_vms_output_quad (abfd, symbol->value);
894 _bfd_vms_output_quad (abfd,
895 ((asymbol *)(symbol->udata.p))->value);
896 _bfd_vms_output_long (abfd,
897 (((asymbol *)(symbol->udata.p))
898 ->section->index));
899 _bfd_vms_output_long (abfd, symbol->section->index);
901 else
903 _bfd_vms_output_quad (abfd, symbol->value); /* L_VALUE */
904 _bfd_vms_output_quad (abfd, 0); /* L_CODE_ADDRESS */
905 _bfd_vms_output_long (abfd, 0); /* L_CA_PSINDX */
906 _bfd_vms_output_long (abfd, symbol->section->index);/* L_PSINDX */
909 _bfd_vms_output_counted (abfd, _bfd_vms_length_hash_symbol (abfd, symbol->name, EOBJ_S_C_SYMSIZ));
911 _bfd_vms_output_flush (abfd);
915 _bfd_vms_output_alignment (abfd, 8);
916 _bfd_vms_output_pop (abfd);
917 _bfd_vms_output_end (abfd);
919 return 0;