Touches most files in bfd/, so likely will be blamed for everything..
[deliverable/binutils-gdb.git] / bfd / vms-gsd.c
1 /* vms-gsd.c -- BFD back-end for VAX (openVMS/VAX) and
2 EVAX (openVMS/Alpha) files.
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4
5 go and read the openVMS linker manual (esp. appendix B)
6 if you don't know what's going on here :-)
7
8 Written by Klaus K"ampf (kkaempf@rmi.de)
9
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.
14
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.
19
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. */
23
24 #include <ctype.h>
25
26 #include "bfd.h"
27 #include "sysdep.h"
28 #include "bfdlink.h"
29 #include "libbfd.h"
30
31 #include "vms.h"
32
33 /*-----------------------------------------------------------------------------*/
34
35 /* typical sections for vax object files */
36
37 #define VAX_CODE_NAME "$CODE"
38 #define VAX_DATA_NAME "$DATA"
39 #define VAX_ADDRESS_DATA_NAME "$ADDRESS_DATA"
40
41 /* typical sections for evax object files */
42
43 #define EVAX_ABS_NAME "$ABS$"
44 #define EVAX_CODE_NAME "$CODE$"
45 #define EVAX_LINK_NAME "$LINK$"
46 #define EVAX_DATA_NAME "$DATA$"
47 #define EVAX_BSS_NAME "$BSS$"
48 #define EVAX_READONLYADDR_NAME "$READONLY_ADDR$"
49 #define EVAX_READONLY_NAME "$READONLY$"
50 #define EVAX_LITERAL_NAME "$LITERAL$"
51 #define EVAX_COMMON_NAME "$COMMON$"
52 #define EVAX_LOCAL_NAME "$LOCAL$"
53
54 struct sec_flags_struct {
55 char *name; /* name of section */
56 int vflags_always;
57 flagword flags_always; /* flags we set always */
58 int vflags_hassize;
59 flagword flags_hassize; /* flags we set if the section has a size > 0 */
60 };
61
62 /* These flags are deccrtl/vaxcrtl (openVMS 6.2 VAX) compatible */
63
64 static struct sec_flags_struct vax_section_flags[] = {
65 { VAX_CODE_NAME,
66 (GPS_S_M_PIC|GPS_S_M_REL|GPS_S_M_SHR|GPS_S_M_EXE|GPS_S_M_RD),
67 (SEC_CODE),
68 (GPS_S_M_PIC|GPS_S_M_REL|GPS_S_M_SHR|GPS_S_M_EXE|GPS_S_M_RD),
69 (SEC_IN_MEMORY|SEC_CODE|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
70 { VAX_DATA_NAME,
71 (GPS_S_M_PIC|GPS_S_M_REL|GPS_S_M_RD|GPS_S_M_WRT),
72 (SEC_DATA),
73 (GPS_S_M_PIC|GPS_S_M_REL|GPS_S_M_RD|GPS_S_M_WRT),
74 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
75 { VAX_ADDRESS_DATA_NAME,
76 (GPS_S_M_PIC|GPS_S_M_REL|GPS_S_M_RD),
77 (SEC_DATA|SEC_READONLY),
78 (GPS_S_M_PIC|GPS_S_M_REL|GPS_S_M_RD),
79 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_READONLY|SEC_LOAD) },
80 { NULL,
81 (GPS_S_M_PIC|GPS_S_M_OVR|GPS_S_M_REL|GPS_S_M_GBL|GPS_S_M_RD|GPS_S_M_WRT),
82 (SEC_DATA),
83 (GPS_S_M_PIC|GPS_S_M_OVR|GPS_S_M_REL|GPS_S_M_GBL|GPS_S_M_RD|GPS_S_M_WRT),
84 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) }
85 };
86
87 /* These flags are deccrtl/vaxcrtl (openVMS 6.2 Alpha) compatible */
88
89 static struct sec_flags_struct evax_section_flags[] = {
90 { EVAX_ABS_NAME,
91 (EGPS_S_V_SHR),
92 (SEC_DATA),
93 (EGPS_S_V_SHR),
94 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
95 { EVAX_CODE_NAME,
96 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_EXE),
97 (SEC_CODE),
98 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_EXE),
99 (SEC_IN_MEMORY|SEC_CODE|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
100 { EVAX_LITERAL_NAME,
101 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_RD|EGPS_S_V_NOMOD),
102 (SEC_DATA|SEC_READONLY),
103 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_RD),
104 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_READONLY|SEC_LOAD) },
105 { EVAX_LINK_NAME,
106 (EGPS_S_V_REL|EGPS_S_V_RD),
107 (SEC_DATA|SEC_READONLY),
108 (EGPS_S_V_REL|EGPS_S_V_RD),
109 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_READONLY|SEC_LOAD) },
110 { EVAX_DATA_NAME,
111 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT|EGPS_S_V_NOMOD),
112 (SEC_DATA),
113 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
114 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
115 { EVAX_BSS_NAME,
116 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT|EGPS_S_V_NOMOD),
117 (SEC_NO_FLAGS),
118 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT|EGPS_S_V_NOMOD),
119 (SEC_IN_MEMORY|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
120 { EVAX_READONLYADDR_NAME,
121 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_RD),
122 (SEC_DATA|SEC_READONLY),
123 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_RD),
124 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_READONLY|SEC_LOAD) },
125 { EVAX_READONLY_NAME,
126 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_RD|EGPS_S_V_NOMOD),
127 (SEC_DATA|SEC_READONLY),
128 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_RD),
129 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_READONLY|SEC_LOAD) },
130 { EVAX_LOCAL_NAME,
131 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
132 (SEC_DATA),
133 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
134 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
135 { NULL,
136 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
137 (SEC_DATA),
138 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
139 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) }
140 };
141
142 static flagword vms_secflag_by_name PARAMS ((bfd *, struct sec_flags_struct *, char *, int));
143 static flagword vms_esecflag_by_name PARAMS ((struct sec_flags_struct *, char *, int));
144
145 /* Retrieve bfd section flags by name and size */
146
147 static flagword
148 vms_secflag_by_name (abfd, section_flags, name, hassize)
149 bfd *abfd;
150 struct sec_flags_struct *section_flags;
151 char *name;
152 int hassize;
153 {
154 int i = 0;
155
156 while (section_flags[i].name != NULL)
157 {
158 if ((PRIV(is_vax)?
159 strcasecmp (name, section_flags[i].name):
160 strcmp (name, section_flags[i].name)) == 0)
161 {
162 if (hassize)
163 return section_flags[i].flags_hassize;
164 else
165 return section_flags[i].flags_always;
166 }
167 i++;
168 }
169 if (hassize)
170 return section_flags[i].flags_hassize;
171 return section_flags[i].flags_always;
172 }
173
174 /* Retrieve vms section flags by name and size */
175
176 static flagword
177 vms_esecflag_by_name (section_flags, name, hassize)
178 struct sec_flags_struct *section_flags;
179 char *name;
180 int hassize;
181 {
182 int i = 0;
183
184 while (section_flags[i].name != NULL)
185 {
186 if (strcmp (name, section_flags[i].name) == 0)
187 {
188 if (hassize)
189 return section_flags[i].vflags_hassize;
190 else
191 return section_flags[i].vflags_always;
192 }
193 i++;
194 }
195 if (hassize)
196 return section_flags[i].vflags_hassize;
197 return section_flags[i].vflags_always;
198 }
199
200 /*-----------------------------------------------------------------------------*/
201 #if VMS_DEBUG
202 /* debug */
203
204 struct flagdescstruct { char *name; flagword value; };
205
206 /* Convert flag to printable string */
207
208 static char *
209 flag2str(flagdesc, flags)
210 struct flagdescstruct *flagdesc;
211 flagword flags;
212 {
213
214 static char res[64];
215 int next = 0;
216
217 res[0] = 0;
218 while (flagdesc->name != NULL)
219 {
220 if ((flags & flagdesc->value) != 0)
221 {
222 if (next)
223 strcat(res, ",");
224 else
225 next = 1;
226 strcat (res, flagdesc->name);
227 }
228 flagdesc++;
229 }
230 return res;
231 }
232 #endif
233
234 /*-----------------------------------------------------------------------------*/
235 /* input routines */
236
237 /* Process GSD/EGSD record
238 return 0 on success, -1 on error */
239
240 int
241 _bfd_vms_slurp_gsd (abfd, objtype)
242 bfd *abfd;
243 int objtype;
244 {
245 #if VMS_DEBUG
246 static struct flagdescstruct gpsflagdesc[] =
247 {
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 }
261 };
262
263 static struct flagdescstruct gsyflagdesc[] =
264 {
265 { "WEAK", 0x0001 },
266 { "DEF", 0x0002 },
267 { "UNI", 0x0004 },
268 { "REL", 0x0008 },
269 { "COMM", 0x0010 },
270 { "VECEP", 0x0020 },
271 { "NORM", 0x0040 },
272 { NULL, 0 }
273 };
274 #endif
275
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 unsigned int psect_idx = 0;
286
287 #if VMS_DEBUG
288 vms_debug (2, "GSD/EGSD (%d/%x)\n", objtype, objtype);
289 #endif
290
291 switch (objtype)
292 {
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;
303 }
304
305 /* calculate base address for each section */
306 base_addr = 0L;
307
308 abfd->symcount = 0;
309
310 while (PRIV(rec_size) > 0)
311 {
312 vms_rec = PRIV(vms_rec);
313
314 if (objtype == OBJ_S_C_GSD)
315 {
316 gsd_type = *vms_rec;
317 }
318 else
319 {
320 _bfd_vms_get_header_values (abfd, vms_rec, &gsd_type, &gsd_size);
321 gsd_type += EVAX_OFFSET;
322 }
323
324 #if VMS_DEBUG
325 vms_debug (3, "gsd_type %d\n", gsd_type);
326 #endif
327
328 switch (gsd_type)
329 {
330 case GSD_S_C_PSC:
331 {
332 /*
333 * program section definition
334 */
335
336 asection *old_section = 0;
337
338 #if VMS_DEBUG
339 vms_debug (4, "GSD_S_C_PSC\n");
340 #endif
341 /* If this section isn't a bfd section. */
342
343 if (PRIV(is_vax) && (psect_idx < (abfd->section_count-1)))
344 {
345 /* check for temporary section from TIR record. */
346
347 if (psect_idx < PRIV(section_count))
348 old_section = PRIV(sections)[psect_idx];
349 else
350 old_section = 0;
351 }
352
353 name = _bfd_vms_save_counted_string (vms_rec + 8);
354 section = bfd_make_section (abfd, name);
355 if (!section)
356 {
357 (*_bfd_error_handler) (_("bfd_make_section (%s) failed"),
358 name);
359 return -1;
360 }
361 old_flags = bfd_getl16 (vms_rec + 2);
362 section->_raw_size = bfd_getl32 (vms_rec + 4); /* allocation */
363 new_flags = vms_secflag_by_name (abfd, vax_section_flags, name,
364 section->_raw_size > 0);
365 if (old_flags & EGPS_S_V_REL)
366 new_flags |= SEC_RELOC;
367 if (old_flags & GPS_S_M_OVR)
368 new_flags |= SEC_IS_COMMON;
369 if (!bfd_set_section_flags (abfd, section, new_flags))
370 {
371 (*_bfd_error_handler)
372 (_("bfd_set_section_flags (%s, %x) failed"),
373 name, new_flags);
374 return -1;
375 }
376 section->alignment_power = vms_rec[1];
377 align_addr = (1 << section->alignment_power);
378 if ((base_addr % align_addr) != 0)
379 base_addr += (align_addr - (base_addr % align_addr));
380 section->vma = (bfd_vma)base_addr;
381 base_addr += section->_raw_size;
382
383 /* global section is common symbol */
384
385 if (old_flags & GPS_S_M_GBL)
386 {
387 entry = _bfd_vms_enter_symbol (abfd, name);
388 if (entry == (vms_symbol_entry *)NULL)
389 {
390 bfd_set_error (bfd_error_no_memory);
391 return -1;
392 }
393 symbol = entry->symbol;
394
395 symbol->value = 0;
396 symbol->section = section;
397 symbol->flags = (BSF_GLOBAL|BSF_SECTION_SYM|BSF_OLD_COMMON);
398 }
399
400 /* copy saved contents if old_section set */
401
402 if (old_section != 0)
403 {
404 section->contents = old_section->contents;
405 if (section->_raw_size < old_section->_raw_size)
406 {
407 (*_bfd_error_handler)
408 (_("Size mismatch section %s=%lx, %s=%lx"),
409 old_section->name,
410 (unsigned long) old_section->_raw_size,
411 section->name,
412 (unsigned long) section->_raw_size);
413 return -1;
414 }
415 else if (section->_raw_size > old_section->_raw_size)
416 {
417 section->contents = ((unsigned char *)
418 bfd_realloc (old_section->contents,
419 section->_raw_size));
420 if (section->contents == NULL)
421 {
422 bfd_set_error (bfd_error_no_memory);
423 return -1;
424 }
425 }
426 }
427 else
428 {
429 section->contents = ((unsigned char *)
430 bfd_malloc (section->_raw_size));
431 if (section->contents == NULL)
432 {
433 bfd_set_error (bfd_error_no_memory);
434 return -1;
435 }
436 memset (section->contents, 0, (size_t) section->_raw_size);
437 }
438 section->_cooked_size = section->_raw_size;
439 #if VMS_DEBUG
440 vms_debug (4, "gsd psc %d (%s, flags %04x=%s) ",
441 section->index, name, old_flags, flag2str (gpsflagdesc, old_flags));
442 vms_debug (4, "%d bytes at 0x%08lx (mem %p)\n",
443 section->_raw_size, section->vma, section->contents);
444 #endif
445
446 gsd_size = vms_rec[8] + 9;
447
448 psect_idx++;
449 }
450 break;
451
452 case GSD_S_C_EPM:
453 case GSD_S_C_EPMW:
454 #if VMS_DEBUG
455 vms_debug(4, "gsd epm\n");
456 #endif
457 /*FALLTHRU*/
458 case GSD_S_C_SYM:
459 case GSD_S_C_SYMW:
460 {
461 int name_offset = 0, value_offset = 0;
462
463 /*
464 * symbol specification (definition or reference)
465 */
466
467 #if VMS_DEBUG
468 vms_debug (4, "GSD_S_C_SYM(W)\n");
469 #endif
470 old_flags = bfd_getl16 (vms_rec + 2);
471 new_flags = BSF_NO_FLAGS;
472
473 if (old_flags & GSY_S_M_WEAK)
474 new_flags |= BSF_WEAK;
475
476 switch (gsd_type)
477 {
478 case GSD_S_C_EPM:
479 name_offset = 11;
480 value_offset = 5;
481 new_flags |= BSF_FUNCTION;
482 break;
483 case GSD_S_C_EPMW:
484 name_offset = 12;
485 value_offset = 6;
486 new_flags |= BSF_FUNCTION;
487 break;
488 case GSD_S_C_SYM:
489 if (old_flags & GSY_S_M_DEF) /* symbol definition */
490 name_offset = 9;
491 else
492 name_offset = 4;
493 value_offset = 5;
494 break;
495 case GSD_S_C_SYMW:
496 if (old_flags & GSY_S_M_DEF) /* symbol definition */
497 name_offset = 10;
498 else
499 name_offset = 5;
500 value_offset = 6;
501 break;
502 }
503
504 /* save symbol in vms_symbol_table */
505
506 entry = _bfd_vms_enter_symbol (abfd,
507 _bfd_vms_save_counted_string (vms_rec + name_offset));
508 if (entry == (vms_symbol_entry *)NULL)
509 {
510 bfd_set_error (bfd_error_no_memory);
511 return -1;
512 }
513 symbol = entry->symbol;
514
515 if (old_flags & GSY_S_M_DEF) /* symbol definition */
516 {
517 int psect;
518
519 symbol->value = bfd_getl32 (vms_rec+value_offset);
520 if ((gsd_type == GSD_S_C_SYMW)
521 || (gsd_type == GSD_S_C_EPMW))
522 psect = bfd_getl16 (vms_rec + value_offset - 2);
523 else
524 psect = vms_rec[value_offset-1];
525
526 symbol->section = (asection *)psect;
527 #if VMS_DEBUG
528 vms_debug(4, "gsd sym def #%d (%s, %d [%p], %04x=%s)\n", abfd->symcount,
529 symbol->name, (int)symbol->section, symbol->section, old_flags, flag2str(gsyflagdesc, old_flags));
530 #endif
531 }
532 else /* symbol reference */
533 {
534 symbol->section = bfd_make_section (abfd, BFD_UND_SECTION_NAME);
535 #if VMS_DEBUG
536 vms_debug (4, "gsd sym ref #%d (%s, %s [%p], %04x=%s)\n", abfd->symcount,
537 symbol->name, symbol->section->name, symbol->section, old_flags, flag2str (gsyflagdesc, old_flags));
538 #endif
539 }
540
541 gsd_size = vms_rec[name_offset] + name_offset + 1;
542 symbol->flags = new_flags;
543 }
544
545 break;
546
547 case GSD_S_C_PRO:
548 case GSD_S_C_PROW:
549 #if VMS_DEBUG
550 vms_debug(4, "gsd pro\n");
551 #endif
552 break;
553 case GSD_S_C_IDC:
554 #if VMS_DEBUG
555 vms_debug(4, "gsd idc\n");
556 #endif
557 break;
558 case GSD_S_C_ENV:
559 #if VMS_DEBUG
560 vms_debug(4, "gsd env\n");
561 #endif
562 break;
563 case GSD_S_C_LSY:
564 #if VMS_DEBUG
565 vms_debug(4, "gsd lsy\n");
566 #endif
567 break;
568 case GSD_S_C_LEPM:
569 #if VMS_DEBUG
570 vms_debug(4, "gsd lepm\n");
571 #endif
572 break;
573 case GSD_S_C_LPRO:
574 #if VMS_DEBUG
575 vms_debug(4, "gsd lpro\n");
576 #endif
577 break;
578 case GSD_S_C_SPSC:
579 #if VMS_DEBUG
580 vms_debug(4, "gsd spsc\n");
581 #endif
582 break;
583 case GSD_S_C_SYMV:
584 #if VMS_DEBUG
585 vms_debug(4, "gsd symv\n");
586 #endif
587 break;
588 case GSD_S_C_EPMV:
589 #if VMS_DEBUG
590 vms_debug(4, "gsd epmv\n");
591 #endif
592 break;
593 case GSD_S_C_PROV:
594 #if VMS_DEBUG
595 vms_debug(4, "gsd prov\n");
596 #endif
597 break;
598
599 case EGSD_S_C_PSC + EVAX_OFFSET:
600 {
601 /* program section definition */
602
603 name = _bfd_vms_save_counted_string (vms_rec+12);
604 section = bfd_make_section (abfd, name);
605 if (!section)
606 return -1;
607 old_flags = bfd_getl16 (vms_rec + 6);
608 section->_raw_size = bfd_getl32 (vms_rec + 8); /* allocation */
609 new_flags = vms_secflag_by_name (abfd, evax_section_flags, name,
610 section->_raw_size > 0);
611 if (old_flags & EGPS_S_V_REL)
612 new_flags |= SEC_RELOC;
613 if (!bfd_set_section_flags (abfd, section, new_flags))
614 return -1;
615 section->alignment_power = vms_rec[4];
616 align_addr = (1 << section->alignment_power);
617 if ((base_addr % align_addr) != 0)
618 base_addr += (align_addr - (base_addr % align_addr));
619 section->vma = (bfd_vma)base_addr;
620 base_addr += section->_raw_size;
621 section->contents = ((unsigned char *)
622 bfd_malloc (section->_raw_size));
623 if (section->contents == NULL)
624 return -1;
625 memset (section->contents, 0, (size_t) section->_raw_size);
626 section->_cooked_size = section->_raw_size;
627 #if VMS_DEBUG
628 vms_debug(4, "egsd psc %d (%s, flags %04x=%s) ",
629 section->index, name, old_flags, flag2str(gpsflagdesc, old_flags));
630 vms_debug(4, "%d bytes at 0x%08lx (mem %p)\n",
631 section->_raw_size, section->vma, section->contents);
632 #endif
633 }
634 break;
635
636 case EGSD_S_C_SYM + EVAX_OFFSET:
637 {
638 /* symbol specification (definition or reference) */
639
640 symbol = _bfd_vms_make_empty_symbol (abfd);
641 if (symbol == 0)
642 return -1;
643
644 old_flags = bfd_getl16 (vms_rec + 6);
645 new_flags = BSF_NO_FLAGS;
646
647 if (old_flags & EGSY_S_V_WEAK)
648 new_flags |= BSF_WEAK;
649
650 if (vms_rec[6] & EGSY_S_V_DEF) /* symbol definition */
651 {
652 symbol->name =
653 _bfd_vms_save_counted_string (vms_rec+32);
654 if (old_flags & EGSY_S_V_NORM)
655 { /* proc def */
656 new_flags |= BSF_FUNCTION;
657 }
658 symbol->value = bfd_getl64 (vms_rec+8);
659 symbol->section = (asection *) ((unsigned long) bfd_getl32 (vms_rec+28));
660 #if VMS_DEBUG
661 vms_debug(4, "egsd sym def #%d (%s, %d, %04x=%s)\n", abfd->symcount,
662 symbol->name, (int)symbol->section, old_flags, flag2str(gsyflagdesc, old_flags));
663 #endif
664 }
665 else /* symbol reference */
666 {
667 symbol->name =
668 _bfd_vms_save_counted_string (vms_rec+8);
669 #if VMS_DEBUG
670 vms_debug(4, "egsd sym ref #%d (%s, %04x=%s)\n", abfd->symcount,
671 symbol->name, old_flags, flag2str(gsyflagdesc, old_flags));
672 #endif
673 symbol->section = bfd_make_section (abfd, BFD_UND_SECTION_NAME);
674 }
675
676 symbol->flags = new_flags;
677
678 /* save symbol in vms_symbol_table */
679
680 entry = (vms_symbol_entry *) bfd_hash_lookup (PRIV(vms_symbol_table), symbol->name, true, false);
681 if (entry == (vms_symbol_entry *)NULL)
682 {
683 bfd_set_error (bfd_error_no_memory);
684 return -1;
685 }
686 if (entry->symbol != (asymbol *)NULL)
687 { /* FIXME ?, DEC C generates this */
688 #if VMS_DEBUG
689 vms_debug(4, "EGSD_S_C_SYM: duplicate \"%s\"\n", symbol->name);
690 #endif
691 }
692 else
693 {
694 entry->symbol = symbol;
695 PRIV(gsd_sym_count)++;
696 abfd->symcount++;
697 }
698 }
699 break;
700
701 case EGSD_S_C_IDC + EVAX_OFFSET:
702 break;
703
704 default:
705 (*_bfd_error_handler) (_("unknown gsd/egsd subtype %d"), gsd_type);
706 bfd_set_error (bfd_error_bad_value);
707 return -1;
708
709 } /* switch */
710
711 PRIV(rec_size) -= gsd_size;
712 PRIV(vms_rec) += gsd_size;
713
714 } /* while (recsize > 0) */
715
716 if (abfd->symcount > 0)
717 abfd->flags |= HAS_SYMS;
718
719 return 0;
720 }
721
722 /*-----------------------------------------------------------------------------*/
723 /* output routines */
724
725 /* Write section and symbol directory of bfd abfd */
726
727 int
728 _bfd_vms_write_gsd (abfd, objtype)
729 bfd *abfd;
730 int objtype ATTRIBUTE_UNUSED;
731 {
732 asection *section;
733 asymbol *symbol;
734 unsigned int symnum;
735 int last_index = -1;
736 char dummy_name[10];
737 char *sname;
738 flagword new_flags, old_flags;
739
740 #if VMS_DEBUG
741 vms_debug (2, "vms_write_gsd (%p, %d)\n", abfd, objtype);
742 #endif
743
744 /* output sections */
745
746 section = abfd->sections;
747 #if VMS_DEBUG
748 vms_debug (3, "%d sections found\n", abfd->section_count);
749 #endif
750
751 /* egsd is quadword aligned */
752
753 _bfd_vms_output_alignment (abfd, 8);
754
755 _bfd_vms_output_begin (abfd, EOBJ_S_C_EGSD, -1);
756 _bfd_vms_output_long (abfd, 0);
757 _bfd_vms_output_push (abfd); /* prepare output for subrecords */
758
759 while (section != 0)
760 {
761 #if VMS_DEBUG
762 vms_debug (3, "Section #%d %s, %d bytes\n", section->index, section->name, (int)section->_raw_size);
763 #endif
764
765 /* 13 bytes egsd, max 31 chars name -> should be 44 bytes */
766 if (_bfd_vms_output_check (abfd, 64) < 0)
767 {
768 _bfd_vms_output_pop (abfd);
769 _bfd_vms_output_end (abfd);
770 _bfd_vms_output_begin (abfd, EOBJ_S_C_EGSD, -1);
771 _bfd_vms_output_long (abfd, 0);
772 _bfd_vms_output_push (abfd); /* prepare output for subrecords */
773 }
774
775 /* Create dummy sections to keep consecutive indices */
776
777 while (section->index - last_index > 1)
778 {
779 #if VMS_DEBUG
780 vms_debug (3, "index %d, last %d\n", section->index, last_index);
781 #endif
782 _bfd_vms_output_begin (abfd, EGSD_S_C_PSC, -1);
783 _bfd_vms_output_short (abfd, 0);
784 _bfd_vms_output_short (abfd, 0);
785 _bfd_vms_output_long (abfd, 0);
786 sprintf (dummy_name, ".DUMMY%02d", last_index);
787 _bfd_vms_output_counted (abfd, dummy_name);
788 _bfd_vms_output_flush (abfd);
789 last_index++;
790 }
791
792 /* Don't know if this is neccesary for the linker but for now it keeps
793 vms_slurp_gsd happy */
794
795 sname = (char *)section->name;
796 if (*sname == '.')
797 {
798 sname++;
799 if ((*sname == 't') && (strcmp (sname, "text") == 0))
800 sname = PRIV(is_vax)?VAX_CODE_NAME:EVAX_CODE_NAME;
801 else if ((*sname == 'd') && (strcmp (sname, "data") == 0))
802 sname = PRIV(is_vax)?VAX_DATA_NAME:EVAX_DATA_NAME;
803 else if ((*sname == 'b') && (strcmp (sname, "bss") == 0))
804 sname = EVAX_BSS_NAME;
805 else if ((*sname == 'l') && (strcmp (sname, "link") == 0))
806 sname = EVAX_LINK_NAME;
807 else if ((*sname == 'r') && (strcmp (sname, "rdata") == 0))
808 sname = EVAX_READONLY_NAME;
809 else if ((*sname == 'l') && (strcmp (sname, "literal") == 0))
810 sname = EVAX_LITERAL_NAME;
811 else if ((*sname == 'c') && (strcmp (sname, "comm") == 0))
812 sname = EVAX_COMMON_NAME;
813 else if ((*sname == 'l') && (strcmp (sname, "lcomm") == 0))
814 sname = EVAX_LOCAL_NAME;
815 }
816 else
817 sname = _bfd_vms_length_hash_symbol (abfd, sname, EOBJ_S_C_SECSIZ);
818
819 _bfd_vms_output_begin (abfd, EGSD_S_C_PSC, -1);
820 _bfd_vms_output_short (abfd, section->alignment_power & 0xff);
821 if (bfd_is_com_section (section))
822 {
823 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);
824 }
825 else
826 {
827 new_flags = vms_esecflag_by_name (evax_section_flags, sname,
828 section->_raw_size > 0);
829 }
830 _bfd_vms_output_short (abfd, new_flags);
831 _bfd_vms_output_long (abfd, (unsigned long) section->_raw_size);
832 _bfd_vms_output_counted (abfd, sname);
833 _bfd_vms_output_flush (abfd);
834
835 last_index = section->index;
836 section = section->next;
837 }
838
839 /* output symbols */
840
841 #if VMS_DEBUG
842 vms_debug (3, "%d symbols found\n", abfd->symcount);
843 #endif
844
845 bfd_set_start_address (abfd, (bfd_vma)-1);
846
847 for (symnum = 0; symnum < abfd->symcount; symnum++)
848 {
849 char *hash;
850
851 symbol = abfd->outsymbols[symnum];
852 if (*(symbol->name) == '_')
853 {
854 if (strcmp (symbol->name, "__main") == 0)
855 bfd_set_start_address (abfd, (bfd_vma)symbol->value);
856 }
857 old_flags = symbol->flags;
858
859 if (old_flags & BSF_FILE)
860 continue;
861
862 if (((old_flags & (BSF_GLOBAL|BSF_WEAK)) == 0) /* not xdef */
863 && (!bfd_is_und_section (symbol->section))) /* and not xref */
864 continue; /* dont output */
865
866 /* 13 bytes egsd, max 64 chars name -> should be 77 bytes */
867
868 if (_bfd_vms_output_check (abfd, 80) < 0)
869 {
870 _bfd_vms_output_pop (abfd);
871 _bfd_vms_output_end (abfd);
872 _bfd_vms_output_begin (abfd, EOBJ_S_C_EGSD, -1);
873 _bfd_vms_output_long (abfd, 0);
874 _bfd_vms_output_push (abfd); /* prepare output for subrecords */
875 }
876
877 _bfd_vms_output_begin (abfd, EGSD_S_C_SYM, -1);
878
879 _bfd_vms_output_short (abfd, 0); /* data type, alignment */
880
881 new_flags = 0;
882
883 if (old_flags & BSF_WEAK)
884 new_flags |= EGSY_S_V_WEAK;
885 if (bfd_is_com_section (symbol->section)) /* .comm */
886 new_flags |= (EGSY_S_V_WEAK|EGSY_S_V_COMM);
887
888 if (old_flags & BSF_FUNCTION)
889 {
890 new_flags |= EGSY_S_V_NORM;
891 new_flags |= EGSY_S_V_REL;
892 }
893 if (old_flags & (BSF_GLOBAL|BSF_WEAK))
894 {
895 new_flags |= EGSY_S_V_DEF;
896 if (!bfd_is_abs_section (symbol->section))
897 new_flags |= EGSY_S_V_REL;
898 }
899 _bfd_vms_output_short (abfd, new_flags);
900
901 if (old_flags & (BSF_GLOBAL | BSF_WEAK)) /* symbol definition */
902 {
903 uquad code_address = 0;
904 unsigned long ca_psindx = 0;
905 unsigned long psindx;
906
907 if (old_flags & BSF_FUNCTION)
908 {
909 code_address = ((asymbol *) (symbol->udata.p))->value;
910 ca_psindx = ((asymbol *) (symbol->udata.p))->section->index;
911 }
912 psindx = symbol->section->index;
913
914 _bfd_vms_output_quad (abfd, symbol->value);
915 _bfd_vms_output_quad (abfd, code_address);
916 _bfd_vms_output_long (abfd, ca_psindx);
917 _bfd_vms_output_long (abfd, psindx);
918 }
919 hash = _bfd_vms_length_hash_symbol (abfd, symbol->name, EOBJ_S_C_SYMSIZ);
920 _bfd_vms_output_counted (abfd, hash);
921
922 _bfd_vms_output_flush (abfd);
923
924 }
925
926 _bfd_vms_output_alignment (abfd, 8);
927 _bfd_vms_output_pop (abfd);
928 _bfd_vms_output_end (abfd);
929
930 return 0;
931 }
This page took 0.051374 seconds and 4 git commands to generate.