Thu Aug 8 12:21:56 1996 Klaus Kaempf <kkaempf@progis.de>
[deliverable/binutils-gdb.git] / bfd / evax-egsd.c
1 /* evax-egsd.c -- BFD back-end for ALPHA EVAX (openVMS/AXP) files.
2 Copyright 1996 Free Software Foundation Inc.
3
4 go and read the openVMS linker manual (esp. appendix B)
5 if you don't know what's going on here :-)
6
7 Written by Klaus Kämpf (kkaempf@progis.de)
8 of proGIS Softwareentwicklung, Aachen, Germany
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
25 #include <stdio.h>
26 #include <ctype.h>
27
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include "bfdlink.h"
31 #include "libbfd.h"
32
33 #include "evax.h"
34
35 /*-----------------------------------------------------------------------------*/
36
37 /* sections every evax object file has */
38
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$"
46
47 struct sec_flags_struct {
48 char *name; /* name of section */
49 int eflags_always;
50 flagword flags_always; /* flags we set always */
51 int eflags_hassize;
52 flagword flags_hassize; /* flags we set if the section has a size > 0 */
53 };
54
55 /* just a dummy flag array since i don't understand it yet */
56
57 static struct sec_flags_struct evax_section_flags[] = {
58 { EVAX_ABS_NAME,
59 (EGPS_S_V_SHR),
60 (SEC_DATA),
61 (EGPS_S_V_SHR),
62 (SEC_IN_MEMORY|SEC_DATA|SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD) },
63 { EVAX_CODE_NAME,
64 (EGPS_S_V_PIC|EGPS_S_V_REL|EGPS_S_V_SHR|EGPS_S_V_EXE),
65 (SEC_CODE),
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) },
68 { EVAX_LINK_NAME,
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) },
73 { EVAX_DATA_NAME,
74 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
75 (SEC_DATA),
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) },
78 { EVAX_BSS_NAME,
79 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT|EGPS_S_V_NOMOD),
80 (SEC_NO_FLAGS),
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) },
83 { EVAX_READONLY_NAME,
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) },
88 { EVAX_LITERAL_NAME,
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) },
93 { NULL,
94 (EGPS_S_V_REL|EGPS_S_V_RD|EGPS_S_V_WRT),
95 (SEC_DATA),
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) }
98 };
99
100 /* Retrieve bfd section flags by name and size */
101
102 static flagword
103 evax_secflag_by_name(name, size)
104 char *name;
105 int size;
106 {
107 int i = 0;
108
109 while (evax_section_flags[i].name != NULL)
110 {
111 if (strcmp (name, evax_section_flags[i].name) == 0)
112 {
113 if (size > 0)
114 return evax_section_flags[i].flags_hassize;
115 else
116 return evax_section_flags[i].flags_always;
117 }
118 i++;
119 }
120 if (size > 0)
121 return evax_section_flags[i].flags_hassize;
122 return evax_section_flags[i].flags_always;
123 }
124
125
126 /* Retrieve evax section flags by name and size */
127
128 static flagword
129 evax_esecflag_by_name(name, size)
130 char *name;
131 int size;
132 {
133 int i = 0;
134
135 while (evax_section_flags[i].name != NULL)
136 {
137 if (strcmp (name, evax_section_flags[i].name) == 0)
138 {
139 if (size > 0)
140 return evax_section_flags[i].eflags_hassize;
141 else
142 return evax_section_flags[i].eflags_always;
143 }
144 i++;
145 }
146 if (size > 0)
147 return evax_section_flags[i].eflags_hassize;
148 return evax_section_flags[i].eflags_always;
149 }
150
151 /*-----------------------------------------------------------------------------*/
152 #if EVAX_DEBUG
153 /* debug */
154
155 struct flagdescstruct { char *name; flagword value; };
156
157 /* Convert flag to printable string */
158
159 static char *
160 flag2str(flagdesc, flags)
161 struct flagdescstruct *flagdesc;
162 flagword flags;
163 {
164
165 static char res[64];
166 int next = 0;
167
168 res[0] = 0;
169 while (flagdesc->name != NULL)
170 {
171 if ((flags & flagdesc->value) != 0)
172 {
173 if (next)
174 strcat(res, ",");
175 else
176 next = 1;
177 strcat (res, flagdesc->name);
178 }
179 flagdesc++;
180 }
181 return res;
182 }
183 #endif
184
185 /*-----------------------------------------------------------------------------*/
186 /* input routines */
187
188 /* Process EGSD record
189 return 0 on success, -1 on error */
190
191 int
192 _bfd_evax_slurp_egsd (abfd)
193 bfd *abfd;
194 {
195 #if EVAX_DEBUG
196 static struct flagdescstruct gpsflagdesc[] =
197 {
198 { "PIC", 0x0001 },
199 { "LIB", 0x0002 },
200 { "OVR", 0x0004 },
201 { "REL", 0x0008 },
202 { "GBL", 0x0010 },
203 { "SHR", 0x0020 },
204 { "EXE", 0x0040 },
205 { "RD", 0x0080 },
206 { "WRT", 0x0100 },
207 { "VEC", 0x0200 },
208 { "NOMOD", 0x0400 },
209 { "COM", 0x0800 },
210 { NULL, 0 }
211 };
212
213 static struct flagdescstruct gsyflagdesc[] =
214 {
215 { "WEAK", 0x0001 },
216 { "DEF", 0x0002 },
217 { "UNI", 0x0004 },
218 { "REL", 0x0008 },
219 { "COMM", 0x0010 },
220 { "VECEP", 0x0020 },
221 { "NORM", 0x0040 },
222 { NULL, 0 }
223 };
224 #endif
225
226 int gsd_type, gsd_size;
227 asection *section;
228 unsigned char *evax_rec;
229 flagword new_flags, old_flags;
230 char *name;
231 asymbol *symbol;
232 evax_symbol_entry *entry;
233 unsigned long base_addr;
234 unsigned long align_addr;
235
236 #if EVAX_DEBUG
237 evax_debug (2, "EGSD\n");
238 #endif
239
240 PRIV(evax_rec) += 8; /* skip type, size, l_temp */
241 PRIV(rec_size) -= 8;
242
243 /* calculate base address for each section */
244 base_addr = 0L;
245
246 abfd->symcount = 0;
247
248 while (PRIV(rec_size) > 0)
249 {
250 evax_rec = PRIV(evax_rec);
251 _bfd_evax_get_header_values (abfd, evax_rec, &gsd_type, &gsd_size);
252 switch (gsd_type)
253 {
254 case EGSD_S_C_PSC:
255 {
256 /* program section definition */
257
258 name = _bfd_evax_save_counted_string ((char *)evax_rec+12);
259 section = bfd_make_section (abfd, name);
260 if (!section)
261 return -1;
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, (int) 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))
268 return -1;
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;
275 section->contents = ((unsigned char *)
276 bfd_malloc (section->_raw_size));
277 if (section->contents == NULL)
278 return -1;
279 memset (section->contents, 0, (size_t) section->_raw_size);
280 section->_cooked_size = section->_raw_size;
281 #if EVAX_DEBUG
282 evax_debug(3, "egsd psc %d (%s, flags %04x=%s) ",
283 section->index, name, old_flags, flag2str(gpsflagdesc, old_flags));
284 evax_debug(3, "%d bytes at 0x%08lx (mem %p)\n",
285 section->_raw_size, section->vma, section->contents);
286 #endif
287 }
288 break;
289
290 case EGSD_S_C_SYM:
291 {
292 /* symbol specification (definition or reference) */
293
294 symbol = _bfd_evax_make_empty_symbol (abfd);
295 if (symbol == 0)
296 return -1;
297
298 old_flags = bfd_getl16 (evax_rec + 6);
299 new_flags = BSF_NO_FLAGS;
300
301 if (old_flags & EGSY_S_V_WEAK)
302 new_flags |= BSF_WEAK;
303
304 if (evax_rec[6] & EGSY_S_V_DEF) /* symbol definition */
305 {
306 symbol->name =
307 _bfd_evax_save_counted_string ((char *)evax_rec+32);
308 if (old_flags & EGSY_S_V_NORM)
309 { /* proc def */
310 new_flags |= BSF_FUNCTION;
311 }
312 symbol->value = bfd_getl64 (evax_rec+8);
313 symbol->section = (asection *)((unsigned long) bfd_getl32 (evax_rec+28));
314 #if EVAX_DEBUG
315 evax_debug(3, "egsd sym def #%d (%s, %d, %04x=%s)\n", abfd->symcount,
316 symbol->name, (int)symbol->section, old_flags, flag2str(gsyflagdesc, old_flags));
317 #endif
318 }
319 else /* symbol reference */
320 {
321 #if EVAX_DEBUG
322 evax_debug(3, "egsd sym ref #%d (%s, %04x=%s)\n", abfd->symcount, evax_rec+9, old_flags, flag2str(gsyflagdesc, old_flags));
323 #endif
324 symbol->name =
325 _bfd_evax_save_counted_string ((char *)evax_rec+8);
326 symbol->section = bfd_make_section (abfd, BFD_UND_SECTION_NAME);
327 }
328
329 symbol->flags = new_flags;
330
331 /* save symbol in evax_symbol_table */
332
333 entry = (evax_symbol_entry *) bfd_hash_lookup (PRIV(evax_symbol_table), symbol->name, true, false);
334 if (entry == (evax_symbol_entry *)NULL)
335 {
336 bfd_set_error (bfd_error_no_memory);
337 return -1;
338 }
339 if (entry->symbol != (asymbol *)NULL)
340 { /* FIXME ?, DEC C generates this */
341 #if EVAX_DEBUG
342 evax_debug(3, "EGSD_S_C_SYM: duplicate \"%s\"\n", symbol->name);
343 #endif
344 }
345 else
346 {
347 entry->symbol = symbol;
348 PRIV(egsd_sym_count)++;
349 abfd->symcount++;
350 }
351 }
352 break;
353
354 case EGSD_S_C_IDC:
355 break;
356
357 default:
358 (*_bfd_error_handler) ("unknown egsd subtype %d", gsd_type);
359 bfd_set_error (bfd_error_bad_value);
360 return -1;
361
362 } /* switch */
363
364 PRIV(rec_size) -= gsd_size;
365 PRIV(evax_rec) += gsd_size;
366
367 } /* while (recsize > 0) */
368
369 if (abfd->symcount > 0)
370 abfd->flags |= HAS_SYMS;
371
372 return 0;
373 }
374
375 /*-----------------------------------------------------------------------------*/
376 /* output routines */
377
378 /* Write section and symbol directory of bfd abfd */
379
380 int
381 _bfd_evax_write_egsd (abfd)
382 bfd *abfd;
383 {
384 asection *section;
385 asymbol *symbol;
386 int symnum;
387 int last_index = -1;
388 char dummy_name[10];
389 char *sname;
390 flagword new_flags, old_flags;
391 char uname[200];
392 char *nptr, *uptr;
393
394 #if EVAX_DEBUG
395 evax_debug (2, "evax_write_egsd(%p)\n", abfd);
396 #endif
397
398 /* output sections */
399
400 section = abfd->sections;
401 #if EVAX_DEBUG
402 evax_debug (3, "%d sections found\n", abfd->section_count);
403 #endif
404
405 /* egsd is quadword aligned */
406
407 _bfd_evax_output_alignment (abfd, 8);
408
409 _bfd_evax_output_begin (abfd, EOBJ_S_C_EGSD, -1);
410 _bfd_evax_output_long (abfd, 0);
411 _bfd_evax_output_push (abfd); /* prepare output for subrecords */
412
413 while (section != 0)
414 {
415 #if EVAX_DEBUG
416 evax_debug (3, "Section #%d %s, %d bytes\n", section->index, section->name, (int)section->_raw_size);
417 #endif
418
419 /* 13 bytes egsd, max 31 chars name -> should be 44 bytes */
420 if (_bfd_evax_output_check (abfd, 64) < 0)
421 {
422 _bfd_evax_output_pop (abfd);
423 _bfd_evax_output_end (abfd);
424 _bfd_evax_output_begin (abfd, EOBJ_S_C_EGSD, -1);
425 _bfd_evax_output_long (abfd, 0);
426 _bfd_evax_output_push (abfd); /* prepare output for subrecords */
427 }
428
429 /* Create dummy sections to keep consecutive indices */
430
431 while (section->index - last_index > 1)
432 {
433 #if EVAX_DEBUG
434 evax_debug (3, "index %d, last %d\n", section->index, last_index);
435 #endif
436 _bfd_evax_output_begin (abfd, EGSD_S_C_PSC, -1);
437 _bfd_evax_output_short (abfd, 0);
438 _bfd_evax_output_short (abfd, 0);
439 _bfd_evax_output_long (abfd, 0);
440 sprintf (dummy_name, ".DUMMY%02d", last_index);
441 _bfd_evax_output_counted (abfd, dummy_name);
442 _bfd_evax_output_flush (abfd);
443 last_index++;
444 }
445
446 /* Don't know if this is neccesary for the linker but for now it keeps
447 evax_slurp_egsd happy */
448
449 sname = (char *)section->name;
450 if (*sname == '.')
451 {
452 sname++;
453 if ((*sname == 't') && (strcmp (sname, "text") == 0))
454 sname = EVAX_CODE_NAME;
455 else if ((*sname == 'd') && (strcmp (sname, "data") == 0))
456 sname = EVAX_DATA_NAME;
457 else if ((*sname == 'b') && (strcmp (sname, "bss") == 0))
458 sname = EVAX_BSS_NAME;
459 else if ((*sname == 'l') && (strcmp (sname, "link") == 0))
460 sname = EVAX_LINK_NAME;
461 else if ((*sname == 'r') && (strcmp (sname, "rdata") == 0))
462 sname = EVAX_READONLY_NAME;
463 else if ((*sname == 'l') && (strcmp (sname, "literal") == 0))
464 sname = EVAX_LITERAL_NAME;
465 }
466
467 _bfd_evax_output_begin (abfd, EGSD_S_C_PSC, -1);
468 _bfd_evax_output_short (abfd, section->alignment_power & 0xff);
469 _bfd_evax_output_short (abfd,
470 evax_esecflag_by_name (sname,
471 section->_raw_size));
472 _bfd_evax_output_long (abfd, section->_raw_size);
473 _bfd_evax_output_counted (abfd, sname);
474 _bfd_evax_output_flush (abfd);
475
476 last_index = section->index;
477 section = section->next;
478 }
479
480 /* output symbols */
481
482 #if EVAX_DEBUG
483 evax_debug (3, "%d symbols found\n", abfd->symcount);
484 #endif
485
486 bfd_set_start_address (abfd, (bfd_vma)-1);
487
488 for (symnum = 0; symnum < abfd->symcount; symnum++)
489 {
490
491 symbol = abfd->outsymbols[symnum];
492 if (*(symbol->name) == '_')
493 {
494 if (strcmp (symbol->name, "__main") == 0)
495 bfd_set_start_address (abfd, (bfd_vma)symbol->value);
496 }
497 old_flags = symbol->flags;
498
499 if (((old_flags & BSF_GLOBAL) == 0) /* not xdef */
500 && (!bfd_is_und_section (symbol->section))) /* and not xref */
501 continue; /* dont output */
502
503 /* 13 bytes egsd, max 64 chars name -> should be 77 bytes */
504
505 if (_bfd_evax_output_check (abfd, 80) < 0)
506 {
507 _bfd_evax_output_pop (abfd);
508 _bfd_evax_output_end (abfd);
509 _bfd_evax_output_begin (abfd, EOBJ_S_C_EGSD, -1);
510 _bfd_evax_output_long (abfd, 0);
511 _bfd_evax_output_push (abfd); /* prepare output for subrecords */
512 }
513
514 _bfd_evax_output_begin (abfd, EGSD_S_C_SYM, -1);
515
516 _bfd_evax_output_short (abfd, 0); /* data type, alignment */
517
518 new_flags = 0;
519 if (old_flags & BSF_WEAK)
520 new_flags |= EGSY_S_V_WEAK;
521 if (old_flags & BSF_FUNCTION)
522 {
523 new_flags |= EGSY_S_V_NORM;
524 new_flags |= EGSY_S_V_REL;
525 }
526 if (old_flags & BSF_GLOBAL)
527 {
528 new_flags |= EGSY_S_V_DEF;
529 if (!bfd_is_abs_section (symbol->section))
530 new_flags |= EGSY_S_V_REL;
531 }
532 _bfd_evax_output_short (abfd, new_flags);
533
534 if (old_flags & BSF_GLOBAL) /* symbol definition */
535 {
536 if (old_flags & BSF_FUNCTION)
537 {
538 _bfd_evax_output_quad (abfd, symbol->value);
539 _bfd_evax_output_quad (abfd,
540 ((asymbol *)(symbol->udata.p))->value);
541 _bfd_evax_output_long (abfd,
542 (((asymbol *)(symbol->udata.p))
543 ->section->index));
544 _bfd_evax_output_long (abfd, symbol->section->index);
545 }
546 else
547 {
548 _bfd_evax_output_quad (abfd, symbol->value); /* L_VALUE */
549 _bfd_evax_output_quad (abfd, 0); /* L_CODE_ADDRESS */
550 _bfd_evax_output_long (abfd, 0); /* L_CA_PSINDX */
551 _bfd_evax_output_long (abfd, symbol->section->index);/* L_PSINDX, FIXME */
552 }
553 }
554 if (strlen ((char *)symbol->name) > 198)
555 {
556 (*_bfd_error_handler) ("Name '%s' too long\n", symbol->name);
557 abort ();
558 }
559 nptr = (char *)symbol->name;
560 uptr = uname;
561 while (*nptr)
562 {
563 if (islower (*nptr))
564 *uptr = toupper (*nptr);
565 else
566 *uptr = *nptr;
567 uptr++;
568 nptr++;
569 }
570 *uptr = 0;
571 _bfd_evax_output_counted (abfd, uname);
572
573 _bfd_evax_output_flush (abfd);
574
575 }
576
577 _bfd_evax_output_alignment (abfd, 8);
578 _bfd_evax_output_pop (abfd);
579 _bfd_evax_output_end (abfd);
580
581 return 0;
582 }
This page took 0.041242 seconds and 4 git commands to generate.