1 /* evax-egsd.c -- BFD back-end for ALPHA EVAX (openVMS/AXP) files.
2 Copyright 1996 Free Software Foundation Inc.
4 go and read the openVMS linker manual (esp. appendix B)
5 if you don't know what's going on here :-)
7 Written by Klaus Kämpf (kkaempf@progis.de)
8 of proGIS Softwareentwicklung, Aachen, Germany
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. */
35 /*-----------------------------------------------------------------------------*/
37 /* sections every evax object file has */
39 #define EVAX_ABS_NAME "$ABS$"
40 #define EVAX_CODE_NAME "$CODE$"
41 #define EVAX_LINK_NAME "$LINK$"
42 #define EVAX_DATA_NAME "$DATA$"
43 #define EVAX_BSS_NAME "$BSS$"
44 #define EVAX_READONLY_NAME "$READONLY$"
45 #define EVAX_LITERAL_NAME "$LITERAL$"
47 struct sec_flags_struct {
48 char *name; /* name of section */
50 flagword flags_always; /* flags we set always */
52 flagword flags_hassize; /* flags we set if the section has a size > 0 */
55 /* just a dummy flag array since i don't understand it yet */
57 static struct sec_flags_struct evax_section_flags[] = {
62 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
64 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_EXE),
66 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_EXE),
67 (SEC_IN_MEMORY|SEC_CODE|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
69 (EGPS_S_V_REL|EGPS_S_V_RD),
70 (SEC_DATA|SEC_READONLY),
71 (EGPS_S_V_REL|EGPS_S_V_RD),
72 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_READONLY|SEC_LOAD) },
74 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
76 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
77 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
79 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT|EGPS_S_V_NOMOD),
81 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT|EGPS_S_V_NOMOD),
82 (SEC_IN_MEMORY|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
84 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_RD),
85 (SEC_DATA|SEC_READONLY),
86 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_RD),
87 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_READONLY|SEC_LOAD) },
89 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_RD),
90 (SEC_DATA|SEC_READONLY),
91 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_RD),
92 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_READONLY|SEC_LOAD) },
94 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
96 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
97 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) }
100 /* Retrieve bfd section flags by name and size */
103 evax_secflag_by_name(name, size)
109 while (evax_section_flags[i].name != NULL)
111 if (strcmp (name, evax_section_flags[i].name) == 0)
114 return evax_section_flags[i].flags_hassize;
116 return evax_section_flags[i].flags_always;
121 return evax_section_flags[i].flags_hassize;
122 return evax_section_flags[i].flags_always;
126 /* Retrieve evax section flags by name and size */
129 evax_esecflag_by_name(name, size)
135 while (evax_section_flags[i].name != NULL)
137 if (strcmp (name, evax_section_flags[i].name) == 0)
140 return evax_section_flags[i].eflags_hassize;
142 return evax_section_flags[i].eflags_always;
147 return evax_section_flags[i].eflags_hassize;
148 return evax_section_flags[i].eflags_always;
151 /*-----------------------------------------------------------------------------*/
155 struct flagdescstruct { char *name; flagword value; };
157 /* Convert flag to printable string */
160 flag2str(flagdesc, flags)
161 struct flagdescstruct *flagdesc;
169 while (flagdesc->name != NULL)
171 if ((flags & flagdesc->value) != 0)
177 strcat (res, flagdesc->name);
185 /*-----------------------------------------------------------------------------*/
188 /* Process EGSD record
189 return 0 on success, -1 on error */
192 _bfd_evax_slurp_egsd (abfd)
196 static struct flagdescstruct gpsflagdesc[] =
213 static struct flagdescstruct gsyflagdesc[] =
226 int gsd_type, gsd_size;
228 unsigned char *evax_rec;
229 flagword new_flags, old_flags;
232 evax_symbol_entry *entry;
233 unsigned long base_addr;
234 unsigned long align_addr;
237 evax_debug (2, "EGSD\n");
240 PRIV(evax_rec) += 8; /* skip type, size, l_temp */
243 /* calculate base address for each section */
248 while (PRIV(rec_size) > 0)
250 evax_rec = PRIV(evax_rec);
251 _bfd_evax_get_header_values (abfd, evax_rec, &gsd_type, &gsd_size);
256 /* program section definition */
258 name = _bfd_evax_save_counted_string ((char *)evax_rec+12);
259 section = bfd_make_section (abfd, name);
262 old_flags = bfd_getl16 (evax_rec + 6);
263 section->_raw_size = bfd_getl32 (evax_rec + 8); /* allocation */
264 new_flags = evax_secflag_by_name (name, section->_raw_size);
265 if (old_flags & EGPS_S_V_REL)
266 new_flags |= SEC_RELOC;
267 if (!bfd_set_section_flags (abfd, section, new_flags))
269 section->alignment_power = evax_rec[4];
270 align_addr = (1 << section->alignment_power);
271 if ((base_addr % align_addr) != 0)
272 base_addr += (align_addr - (base_addr % align_addr));
273 section->vma = (bfd_vma)base_addr;
274 base_addr += section->_raw_size; /* FIXME: should be cooked size */
275 section->contents = (unsigned char *) malloc (section->_raw_size);
276 if (section->contents == NULL)
278 bfd_set_error (bfd_error_no_memory);
281 memset (section->contents, 0, section->_raw_size);
283 evax_debug(3, "egsd psc %d (%s, flags %04x=%s) ",
284 section->index, name, old_flags, flag2str(gpsflagdesc, old_flags));
285 evax_debug(3, "%d bytes at 0x%08lx (mem %p)\n",
286 section->_raw_size, section->vma, section->contents);
293 /* symbol specification (definition or reference) */
295 symbol = _bfd_evax_make_empty_symbol (abfd);
299 old_flags = bfd_getl16 (evax_rec + 6);
300 new_flags = BSF_NO_FLAGS;
302 if (old_flags & EGSY_S_V_WEAK)
303 new_flags |= BSF_WEAK;
305 if (evax_rec[6] & EGSY_S_V_DEF) /* symbol definition */
308 _bfd_evax_save_counted_string ((char *)evax_rec+32);
309 if (old_flags & EGSY_S_V_NORM)
311 new_flags |= BSF_FUNCTION;
313 symbol->value = bfd_getl64 (evax_rec+8);
314 symbol->section = (asection *)((unsigned long) bfd_getl32 (evax_rec+28));
316 evax_debug(3, "egsd sym def #%d (%s, %d, %04x=%s)\n", abfd->symcount,
317 symbol->name, (int)symbol->section, old_flags, flag2str(gsyflagdesc, old_flags));
320 else /* symbol reference */
323 evax_debug(3, "egsd sym ref #%d (%s, %04x=%s)\n", abfd->symcount, evax_rec+9, old_flags, flag2str(gsyflagdesc, old_flags));
326 _bfd_evax_save_counted_string ((char *)evax_rec+8);
327 symbol->section = bfd_make_section (abfd, BFD_UND_SECTION_NAME);
330 symbol->flags = new_flags;
332 /* save symbol in evax_symbol_table */
334 entry = (evax_symbol_entry *) bfd_hash_lookup (PRIV(evax_symbol_table), symbol->name, true, false);
335 if (entry == (evax_symbol_entry *)NULL)
337 bfd_set_error (bfd_error_no_memory);
340 if (entry->symbol != (asymbol *)NULL)
341 { /* FIXME ?, DEC C generates this */
343 evax_debug(3, "EGSD_S_C_SYM: duplicate \"%s\"\n", symbol->name);
348 entry->symbol = symbol;
349 PRIV(egsd_sym_count)++;
359 (*_bfd_error_handler) ("unknown egsd subtype %d", gsd_type);
360 bfd_set_error (bfd_error_bad_value);
365 PRIV(rec_size) -= gsd_size;
366 PRIV(evax_rec) += gsd_size;
368 } /* while (recsize > 0) */
370 if (abfd->symcount > 0)
371 abfd->flags |= HAS_SYMS;
376 /*-----------------------------------------------------------------------------*/
377 /* output routines */
379 /* Write section and symbol directory of bfd abfd */
382 _bfd_evax_write_egsd (abfd)
391 flagword new_flags, old_flags;
396 evax_debug (2, "evax_write_egsd(%p)\n", abfd);
399 /* output sections */
401 section = abfd->sections;
403 evax_debug (3, "%d sections found\n", abfd->section_count);
406 /* egsd is quadword aligned */
408 _bfd_evax_output_alignment (abfd, 8);
410 _bfd_evax_output_begin (abfd, EOBJ_S_C_EGSD, -1);
411 _bfd_evax_output_long (abfd, 0);
412 _bfd_evax_output_push (abfd); /* prepare output for subrecords */
417 evax_debug (3, "Section #%d %s, %d bytes\n", section->index, section->name, (int)section->_raw_size);
420 /* 13 bytes egsd, max 31 chars name -> should be 44 bytes */
421 if (_bfd_evax_output_check (abfd, 64) < 0)
423 _bfd_evax_output_pop (abfd);
424 _bfd_evax_output_end (abfd);
425 _bfd_evax_output_begin (abfd, EOBJ_S_C_EGSD, -1);
426 _bfd_evax_output_long (abfd, 0);
427 _bfd_evax_output_push (abfd); /* prepare output for subrecords */
430 /* Create dummy sections to keep consecutive indices */
432 while (section->index - last_index > 1)
435 evax_debug (3, "index %d, last %d\n", section->index, last_index);
437 _bfd_evax_output_begin (abfd, EGSD_S_C_PSC, -1);
438 _bfd_evax_output_short (abfd, 0);
439 _bfd_evax_output_short (abfd, 0);
440 _bfd_evax_output_long (abfd, 0);
441 sprintf (dummy_name, ".DUMMY%02d", last_index);
442 _bfd_evax_output_counted (abfd, dummy_name);
443 _bfd_evax_output_flush (abfd);
447 /* Don't know if this is neccesary for the linker but for now it keeps
448 evax_slurp_egsd happy */
450 sname = (char *)section->name;
454 if ((*sname == 't') && (strcmp (sname, "text") == 0))
455 sname = EVAX_CODE_NAME;
456 else if ((*sname == 'd') && (strcmp (sname, "data") == 0))
457 sname = EVAX_DATA_NAME;
458 else if ((*sname == 'b') && (strcmp (sname, "bss") == 0))
459 sname = EVAX_BSS_NAME;
460 else if ((*sname == 'l') && (strcmp (sname, "link") == 0))
461 sname = EVAX_LINK_NAME;
462 else if ((*sname == 'r') && (strcmp (sname, "rdata") == 0))
463 sname = EVAX_READONLY_NAME;
464 else if ((*sname == 'l') && (strcmp (sname, "literal") == 0))
465 sname = EVAX_LITERAL_NAME;
468 _bfd_evax_output_begin (abfd, EGSD_S_C_PSC, -1);
469 _bfd_evax_output_short (abfd, section->alignment_power & 0xff);
470 _bfd_evax_output_short (abfd,
471 evax_esecflag_by_name (sname,
472 section->_raw_size));
473 _bfd_evax_output_long (abfd, section->_raw_size);
474 _bfd_evax_output_counted (abfd, sname);
475 _bfd_evax_output_flush (abfd);
477 last_index = section->index;
478 section = section->next;
484 evax_debug (3, "%d symbols found\n", abfd->symcount);
487 bfd_set_start_address (abfd, (bfd_vma)-1);
489 for (symnum = 0; symnum < abfd->symcount; symnum++)
492 symbol = abfd->outsymbols[symnum];
493 if (*(symbol->name) == '_')
495 if (strcmp (symbol->name, "__main") == 0)
496 bfd_set_start_address (abfd, (bfd_vma)symbol->value);
498 old_flags = symbol->flags;
500 if (((old_flags & BSF_GLOBAL) == 0) /* not xdef */
501 && (!bfd_is_und_section (symbol->section))) /* and not xref */
502 continue; /* dont output */
504 /* 13 bytes egsd, max 64 chars name -> should be 77 bytes */
506 if (_bfd_evax_output_check (abfd, 80) < 0)
508 _bfd_evax_output_pop (abfd);
509 _bfd_evax_output_end (abfd);
510 _bfd_evax_output_begin (abfd, EOBJ_S_C_EGSD, -1);
511 _bfd_evax_output_long (abfd, 0);
512 _bfd_evax_output_push (abfd); /* prepare output for subrecords */
515 _bfd_evax_output_begin (abfd, EGSD_S_C_SYM, -1);
517 _bfd_evax_output_short (abfd, 0); /* data type, alignment */
520 if (old_flags & BSF_WEAK)
521 new_flags |= EGSY_S_V_WEAK;
522 if (old_flags & BSF_FUNCTION)
524 new_flags |= EGSY_S_V_NORM;
525 new_flags |= EGSY_S_V_REL;
527 if (old_flags & BSF_GLOBAL)
529 new_flags |= EGSY_S_V_DEF;
530 if (!bfd_is_abs_section (symbol->section))
531 new_flags |= EGSY_S_V_REL;
533 _bfd_evax_output_short (abfd, new_flags);
535 if (old_flags & BSF_GLOBAL) /* symbol definition */
537 if (old_flags & BSF_FUNCTION)
539 _bfd_evax_output_quad (abfd, symbol->value);
540 _bfd_evax_output_quad (abfd,
541 ((asymbol *)(symbol->udata.p))->value);
542 _bfd_evax_output_long (abfd,
543 (((asymbol *)(symbol->udata.p))
545 _bfd_evax_output_long (abfd, symbol->section->index);
549 _bfd_evax_output_quad (abfd, symbol->value); /* L_VALUE */
550 _bfd_evax_output_quad (abfd, 0); /* L_CODE_ADDRESS */
551 _bfd_evax_output_long (abfd, 0); /* L_CA_PSINDX */
552 _bfd_evax_output_long (abfd, symbol->section->index);/* L_PSINDX, FIXME */
555 if (strlen ((char *)symbol->name) > 198)
557 (*_bfd_error_handler) ("Name '%s' too long\n", symbol->name);
560 nptr = (char *)symbol->name;
565 *uptr = toupper (*nptr);
572 _bfd_evax_output_counted (abfd, uname);
574 _bfd_evax_output_flush (abfd);
578 _bfd_evax_output_alignment (abfd, 8);
579 _bfd_evax_output_pop (abfd);
580 _bfd_evax_output_end (abfd);