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