* config/obj-coffbfd.c (write_object_file): use bfd_get_error (),
[deliverable/binutils-gdb.git] / gas / config / obj-coffbfd.c
CommitLineData
db40ba14 1/* coff object file format with bfd
5ac34ac3 2 Copyright (C) 1989, 1990, 1991, 1993 Free Software Foundation, Inc.
c593cf41
SC
3
4This file is part of GAS.
5
6GAS is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GAS is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GAS; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
db40ba14
SC
19
20/*
c593cf41
SC
21
22 How does this releate to the rest of GAS ?
23
24 Well, all the other files in gas are more or less a black box. It
25 takes care of opening files, parsing command lines, stripping blanks
26 etc etc. This module gets a chance to register what it wants to do by
27 saying that it is interested in various pseduo ops. The other big
28 change is write_object_file. This runs through all the data
29 structures that gas builds, and outputs the file in the format of our
30 choice.
355afbcd 31
c593cf41
SC
32 Hacked for BFDness by steve chamberlain
33
4064305e 34 This object module now supports everything but the i960 and i860.
c593cf41
SC
35
36 sac@cygnus.com
37*/
db40ba14
SC
38
39#include "as.h"
40#include "obstack.h"
41#include "subsegs.h"
42#include "frags.h"
8f3956b3
SS
43/* This is needed because we include internal bfd things. */
44#include <time.h>
db40ba14 45#include "../bfd/libbfd.h"
30d9fb57 46#include "../bfd/libcoff.h"
db40ba14 47
016e0d42
ILT
48/* The NOP_OPCODE is for the alignment fill value. Fill with nop so
49 that we can stick sections together without causing trouble. */
50#ifndef NOP_OPCODE
51#define NOP_OPCODE 0x00
52#endif
db40ba14 53
d752f749 54#define MIN(a,b) ((a) < (b)? (a) : (b))
db40ba14 55/* This vector is used to turn an internal segment into a section #
355afbcd 56 suitable for insertion into a coff symbol table
c593cf41 57 */
db40ba14 58
355afbcd
KR
59const short seg_N_TYPE[] =
60{ /* in: segT out: N_TYPE bits */
61 C_ABS_SECTION,
62 1,
63 2,
64 3,
65 4,
66 5,
67 6,
68 7,
69 8,
70 9,
71 10,
72 C_UNDEF_SECTION, /* SEG_UNKNOWN */
355afbcd 73 C_UNDEF_SECTION, /* SEG_GOOF */
5ac34ac3 74 C_UNDEF_SECTION, /* SEG_EXPR */
355afbcd
KR
75 C_DEBUG_SECTION, /* SEG_DEBUG */
76 C_NTV_SECTION, /* SEG_NTV */
77 C_PTV_SECTION, /* SEG_PTV */
78 C_REGISTER_SECTION, /* SEG_REGISTER */
db40ba14
SC
79};
80
db40ba14
SC
81int function_lineoff = -1; /* Offset in line#s where the last function
82 started (the odd entry for line #0) */
83
355afbcd 84static symbolS *last_line_symbol;
9a75dc1f 85
db40ba14
SC
86/* Add 4 to the real value to get the index and compensate the
87 negatives. This vector is used by S_GET_SEGMENT to turn a coff
355afbcd 88 section number into a segment number
c593cf41 89*/
db40ba14 90static symbolS *previous_file_symbol = NULL;
355afbcd 91void c_symbol_merge ();
9ce31b66 92static int line_base;
db40ba14 93
355afbcd 94symbolS *c_section_symbol ();
db40ba14 95bfd *abfd;
db40ba14 96
e0209756 97static void fixup_segment PARAMS ((segment_info_type *segP,
355afbcd 98 segT this_segment_type));
db40ba14 99
3ad9ec6a 100
e0209756 101static void fixup_mdeps PARAMS ((fragS *,
9a75dc1f
ILT
102 object_headers *,
103 segT));
3ad9ec6a
ILT
104
105
e0209756 106static void fill_section PARAMS ((bfd * abfd,
9a75dc1f
ILT
107 object_headers *,
108 unsigned long *));
db40ba14
SC
109
110
e0209756
ILT
111char *s_get_name PARAMS ((symbolS * s));
112static symbolS *tag_find_or_make PARAMS ((char *name));
113static symbolS *tag_find PARAMS ((char *name));
db40ba14
SC
114
115
e0209756
ILT
116static int c_line_new PARAMS ((symbolS * symbol, long paddr,
117 unsigned short line_number,
118 fragS * frag));
355afbcd
KR
119
120
e0209756
ILT
121static void w_symbols PARAMS ((bfd * abfd, char *where,
122 symbolS * symbol_rootP));
355afbcd 123
604633ae
ILT
124static char *stack_pop PARAMS ((stack * st));
125static char *stack_push PARAMS ((stack * st, char *element));
126#if 0
127static char *stack_top PARAMS ((stack * st));
128#endif
129static stack *stack_init PARAMS ((unsigned long chunk_size,
130 unsigned long element_size));
355afbcd
KR
131
132
604633ae
ILT
133static void tag_init PARAMS ((void));
134static void tag_insert PARAMS ((char *name, symbolS * symbolP));
e0209756 135static void adjust_stab_section PARAMS ((bfd *abfd, segT seg));
db40ba14 136
db40ba14 137static struct hash_control *tag_hash;
604633ae 138
db40ba14
SC
139static symbolS *def_symbol_in_progress = NULL;
140
604633ae
ILT
141static void obj_coff_def PARAMS ((int));
142static void obj_coff_lcomm PARAMS ((int));
143static void obj_coff_dim PARAMS ((int));
144static void obj_coff_text PARAMS ((int));
145static void obj_coff_data PARAMS ((int));
146static void obj_coff_bss PARAMS ((int));
147static void obj_coff_ident PARAMS ((int));
148static void obj_coff_endef PARAMS ((int));
149static void obj_coff_line PARAMS ((int));
150static void obj_coff_ln PARAMS ((int));
151static void obj_coff_scl PARAMS ((int));
152static void obj_coff_size PARAMS ((int));
153static void obj_coff_tag PARAMS ((int));
154static void obj_coff_type PARAMS ((int));
155static void obj_coff_val PARAMS ((int));
156void obj_coff_section PARAMS ((int));
157
355afbcd
KR
158const pseudo_typeS obj_pseudo_table[] =
159{
160 {"def", obj_coff_def, 0},
161 {"dim", obj_coff_dim, 0},
162 {"endef", obj_coff_endef, 0},
163 {"line", obj_coff_line, 0},
164 {"ln", obj_coff_ln, 0},
9a7d824a 165 {"appline", obj_coff_ln, 1},
355afbcd
KR
166 {"scl", obj_coff_scl, 0},
167 {"size", obj_coff_size, 0},
168 {"tag", obj_coff_tag, 0},
169 {"type", obj_coff_type, 0},
170 {"val", obj_coff_val, 0},
171 {"section", obj_coff_section, 0},
172 {"use", obj_coff_section, 0},
173 {"sect", obj_coff_section, 0},
174 {"text", obj_coff_text, 0},
175 {"data", obj_coff_data, 0},
9a75dc1f
ILT
176 {"bss", obj_coff_bss, 0},
177 {"ident", obj_coff_ident, 0},
355afbcd
KR
178 {"ABORT", s_abort, 0},
179 {"lcomm", obj_coff_lcomm, 0},
c978e704
ILT
180#ifdef TC_M88K
181 /* The m88k uses sdef instead of def. */
182 {"sdef", obj_coff_def, 0},
183#endif
355afbcd
KR
184 {NULL} /* end sentinel */
185}; /* obj_pseudo_table */
186
187
188
189/* Section stuff
c593cf41 190
db40ba14
SC
191 We allow more than just the standard 3 sections, infact, we allow
192 10 sections, (though the usual three have to be there).
c593cf41 193
db40ba14 194 This structure performs the mappings for us:
c593cf41
SC
195
196*/
db40ba14 197
355afbcd 198/* OBS stuff
c593cf41
SC
199static struct internal_scnhdr bss_section_header;
200struct internal_scnhdr data_section_header;
201struct internal_scnhdr text_section_header;
202
203const segT N_TYPE_seg [32] =
204{
205
206};
207
208*/
db40ba14
SC
209
210#define N_SEG 32
355afbcd 211typedef struct
db40ba14 212{
2cb0bdc7
SC
213 segT seg_t;
214 int i;
215} seg_info_type;
355afbcd 216
2cb0bdc7 217seg_info_type seg_info_off_by_4[N_SEG] =
db40ba14 218{
2cb0bdc7
SC
219 {SEG_PTV, },
220 {SEG_NTV, },
221 {SEG_DEBUG, },
222 {SEG_ABSOLUTE, },
223 {SEG_UNKNOWN, },
224 {SEG_E0},
225 {SEG_E1},
226 {SEG_E2},
227 {SEG_E3},
228 {SEG_E4},
229 {SEG_E5},
230 {SEG_E6},
231 {SEG_E7},
232 {SEG_E8},
233 {SEG_E9},
234 {(segT)15},
235 {(segT)16},
236 {(segT)17},
237 {(segT)18},
238 {(segT)19},
239 {(segT)20},
240 {(segT)0},
241 {(segT)0},
242 {(segT)0},
243 {SEG_REGISTER}
244};
245
246
db40ba14
SC
247
248#define SEG_INFO_FROM_SECTION_NUMBER(x) (seg_info_off_by_4[(x)+4])
249#define SEG_INFO_FROM_SEG_NUMBER(x) (seg_info_off_by_4[(x)])
250
251
eae7e03c 252static relax_addressT
355afbcd
KR
253DEFUN (relax_align, (address, alignment),
254 register relax_addressT address AND
255 register long alignment)
db40ba14 256{
355afbcd
KR
257 relax_addressT mask;
258 relax_addressT new_address;
c593cf41 259
355afbcd
KR
260 mask = ~((~0) << alignment);
261 new_address = (address + mask) & (~mask);
c593cf41 262 return (new_address - address);
355afbcd 263} /* relax_align() */
db40ba14
SC
264
265
355afbcd
KR
266segT
267DEFUN (s_get_segment, (x),
268 symbolS * x)
db40ba14 269{
355afbcd 270 return SEG_INFO_FROM_SECTION_NUMBER (x->sy_symbol.ost_entry.n_scnum).seg_t;
db40ba14
SC
271}
272
273
274
275/* calculate the size of the frag chain and fill in the section header
276 to contain all of it, also fill in the addr of the sections */
355afbcd
KR
277static unsigned int
278DEFUN (size_section, (abfd, idx),
279 bfd * abfd AND
280 unsigned int idx)
db40ba14 281{
c593cf41
SC
282
283 unsigned int size = 0;
284 fragS *frag = segment_info[idx].frchainP->frch_root;
355afbcd
KR
285 while (frag)
286 {
287 size = frag->fr_address;
355afbcd
KR
288 if (frag->fr_address != size)
289 {
290 printf ("Out of step\n");
c593cf41 291 size = frag->fr_address;
db40ba14 292 }
3ad9ec6a 293
355afbcd
KR
294 switch (frag->fr_type)
295 {
3ad9ec6a 296#ifdef TC_COFF_SIZEMACHDEP
355afbcd
KR
297 case rs_machine_dependent:
298 size += TC_COFF_SIZEMACHDEP (frag);
299 break;
3ad9ec6a 300#endif
c593cf41
SC
301 case rs_fill:
302 case rs_org:
355afbcd
KR
303 size += frag->fr_fix;
304 size += frag->fr_offset * frag->fr_var;
c593cf41
SC
305 break;
306 case rs_align:
355afbcd
KR
307 size += frag->fr_fix;
308 size += relax_align (size, frag->fr_offset);
604633ae
ILT
309 break;
310 default:
311 BAD_CASE (frag->fr_type);
312 break;
c593cf41
SC
313 }
314 frag = frag->fr_next;
315 }
316 segment_info[idx].scnhdr.s_size = size;
317 return size;
db40ba14
SC
318}
319
320
355afbcd
KR
321static unsigned int
322DEFUN (count_entries_in_chain, (idx),
323 unsigned int idx)
db40ba14 324{
355afbcd
KR
325 unsigned int nrelocs;
326 fixS *fixup_ptr;
c593cf41 327
355afbcd
KR
328 /* Count the relocations */
329 fixup_ptr = segment_info[idx].fix_root;
330 nrelocs = 0;
331 while (fixup_ptr != (fixS *) NULL)
c593cf41 332 {
355afbcd 333 if (TC_COUNT_RELOC (fixup_ptr))
c593cf41 334 {
db40ba14 335#ifdef TC_A29K
355afbcd
KR
336 if (fixup_ptr->fx_r_type == RELOC_CONSTH)
337 nrelocs += 2;
338 else
c593cf41 339 nrelocs++;
355afbcd
KR
340#else
341 nrelocs++;
db40ba14 342#endif
c593cf41 343 }
355afbcd
KR
344
345 fixup_ptr = fixup_ptr->fx_next;
c593cf41 346 }
355afbcd 347 return nrelocs;
db40ba14
SC
348}
349
350/* output all the relocations for a section */
355afbcd 351void
9a75dc1f 352DEFUN (do_relocs_for, (abfd, h, file_cursor),
355afbcd 353 bfd * abfd AND
9a75dc1f 354 object_headers * h AND
355afbcd 355 unsigned long *file_cursor)
db40ba14 356{
c593cf41
SC
357 unsigned int nrelocs;
358 unsigned int idx;
9a75dc1f
ILT
359 unsigned long reloc_start = *file_cursor;
360
355afbcd
KR
361 for (idx = SEG_E0; idx < SEG_E9; idx++)
362 {
363 if (segment_info[idx].scnhdr.s_name[0])
364 {
355afbcd
KR
365 struct external_reloc *ext_ptr;
366 struct external_reloc *external_reloc_vec;
367 unsigned int external_reloc_size;
9a75dc1f 368 unsigned int base = segment_info[idx].scnhdr.s_paddr;
355afbcd
KR
369 fixS *fix_ptr = segment_info[idx].fix_root;
370 nrelocs = count_entries_in_chain (idx);
371
155e7bc4
KR
372 if (nrelocs)
373 /* Bypass this stuff if no relocs. This also incidentally
374 avoids a SCO bug, where free(malloc(0)) tends to crash. */
355afbcd 375 {
155e7bc4
KR
376 external_reloc_size = nrelocs * RELSZ;
377 external_reloc_vec =
378 (struct external_reloc *) malloc (external_reloc_size);
c593cf41 379
155e7bc4
KR
380 ext_ptr = external_reloc_vec;
381
382 /* Fill in the internal coff style reloc struct from the
383 internal fix list. */
384 while (fix_ptr)
355afbcd 385 {
155e7bc4
KR
386 symbolS *symbol_ptr;
387 struct internal_reloc intr;
388
389 /* Only output some of the relocations */
390 if (TC_COUNT_RELOC (fix_ptr))
391 {
32e44774 392#ifdef TC_RELOC_MANGLE
155e7bc4 393 TC_RELOC_MANGLE (fix_ptr, &intr, base);
355afbcd 394
410e67eb 395#else
155e7bc4
KR
396 symbolS *dot;
397 symbol_ptr = fix_ptr->fx_addsy;
c593cf41 398
155e7bc4
KR
399 intr.r_type = TC_COFF_FIX2RTYPE (fix_ptr);
400 intr.r_vaddr =
401 base + fix_ptr->fx_frag->fr_address + fix_ptr->fx_where;
c593cf41 402
8ff6f40e 403#ifdef TC_M88K
155e7bc4 404 intr.r_offset = fix_ptr->fx_offset;
8ff6f40e 405#else
155e7bc4 406 intr.r_offset = 0;
8ff6f40e 407#endif
c593cf41 408
155e7bc4
KR
409 /* Turn the segment of the symbol into an offset. */
410 if (symbol_ptr)
355afbcd 411 {
155e7bc4
KR
412 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
413 if (dot)
414 {
415 intr.r_symndx = dot->sy_number;
416 }
417 else
418 {
419 intr.r_symndx = symbol_ptr->sy_number;
420 }
421
355afbcd
KR
422 }
423 else
424 {
155e7bc4 425 intr.r_symndx = -1;
355afbcd 426 }
410e67eb 427#endif
c593cf41 428
155e7bc4
KR
429 (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr);
430 ext_ptr++;
c593cf41 431
db40ba14 432#if defined(TC_A29K)
9a75dc1f 433
155e7bc4
KR
434 /* The 29k has a special kludge for the high 16 bit
435 reloc. Two relocations are emited, R_IHIHALF,
436 and R_IHCONST. The second one doesn't contain a
437 symbol, but uses the value for offset. */
355afbcd 438
155e7bc4
KR
439 if (intr.r_type == R_IHIHALF)
440 {
441 /* now emit the second bit */
442 intr.r_type = R_IHCONST;
443 intr.r_symndx = fix_ptr->fx_addnumber;
155e7bc4
KR
444 (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr);
445 ext_ptr++;
446 }
db40ba14 447#endif
155e7bc4
KR
448 }
449
450 fix_ptr = fix_ptr->fx_next;
355afbcd
KR
451 }
452
155e7bc4
KR
453 /* Write out the reloc table */
454 bfd_write ((PTR) external_reloc_vec, 1, external_reloc_size,
455 abfd);
456 free (external_reloc_vec);
355afbcd 457
155e7bc4
KR
458 /* Fill in section header info. */
459 segment_info[idx].scnhdr.s_relptr = *file_cursor;
460 *file_cursor += external_reloc_size;
e02eaa59 461 segment_info[idx].scnhdr.s_nreloc = nrelocs;
155e7bc4
KR
462 }
463 else
464 {
465 /* No relocs */
466 segment_info[idx].scnhdr.s_relptr = 0;
467 }
c593cf41 468 }
355afbcd 469 }
9a75dc1f
ILT
470 /* Set relocation_size field in file headers */
471 H_SET_RELOCATION_SIZE (h, *file_cursor - reloc_start, 0);
db40ba14
SC
472}
473
474
475/* run through a frag chain and write out the data to go with it, fill
355afbcd 476 in the scnhdrs with the info on the file postions
c593cf41 477*/
355afbcd 478static void
9a75dc1f 479DEFUN (fill_section, (abfd, h, file_cursor),
355afbcd 480 bfd * abfd AND
9a75dc1f 481 object_headers *h AND
355afbcd 482 unsigned long *file_cursor)
db40ba14 483{
c593cf41 484
c58dbabf 485 unsigned int i;
016e0d42 486 unsigned int paddr = 0;
c58dbabf 487
355afbcd
KR
488 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
489 {
490 unsigned int offset = 0;
491
492 struct internal_scnhdr *s = &(segment_info[i].scnhdr);
493
494 if (s->s_name[0])
495 {
496 fragS *frag = segment_info[i].frchainP->frch_root;
016e0d42
ILT
497 char *buffer;
498
9a75dc1f
ILT
499 if (s->s_size == 0)
500 s->s_scnptr = 0;
355afbcd 501 else
016e0d42 502 {
9a75dc1f
ILT
503 buffer = xmalloc (s->s_size);
504 s->s_scnptr = *file_cursor;
016e0d42 505 }
9a75dc1f 506 know (s->s_paddr == paddr);
355afbcd 507
355afbcd
KR
508 if (strcmp (s->s_name, ".text") == 0)
509 s->s_flags |= STYP_TEXT;
510 else if (strcmp (s->s_name, ".data") == 0)
511 s->s_flags |= STYP_DATA;
512 else if (strcmp (s->s_name, ".bss") == 0)
9a75dc1f
ILT
513 {
514 s->s_scnptr = 0;
515 s->s_flags |= STYP_BSS;
516#ifndef TC_I386
543d88e4 517#ifndef TC_A29K
9a75dc1f 518 /* Apparently the SVR3 linker is confused by noload
543d88e4 519 sections. So is the UDI mondfe program. */
9a75dc1f 520 s->s_flags |= STYP_NOLOAD;
543d88e4 521#endif
9a75dc1f
ILT
522#endif
523 }
355afbcd
KR
524 else if (strcmp (s->s_name, ".lit") == 0)
525 s->s_flags = STYP_LIT | STYP_TEXT;
016e0d42
ILT
526 else if (strcmp (s->s_name, ".init") == 0)
527 s->s_flags |= STYP_TEXT;
528 else if (strcmp (s->s_name, ".fini") == 0)
529 s->s_flags |= STYP_TEXT;
9a75dc1f
ILT
530 else if (strncmp (s->s_name, ".comment", 8) == 0)
531 s->s_flags |= STYP_INFO;
355afbcd
KR
532
533 while (frag)
c58dbabf 534 {
355afbcd
KR
535 unsigned int fill_size;
536 switch (frag->fr_type)
537 {
538 case rs_machine_dependent:
539 if (frag->fr_fix)
540 {
541 memcpy (buffer + frag->fr_address,
542 frag->fr_literal,
4f0bccc7 543 (unsigned int) frag->fr_fix);
355afbcd
KR
544 offset += frag->fr_fix;
545 }
546
547 break;
548 case rs_fill:
549 case rs_align:
550 case rs_org:
551 if (frag->fr_fix)
552 {
553 memcpy (buffer + frag->fr_address,
554 frag->fr_literal,
4f0bccc7 555 (unsigned int) frag->fr_fix);
355afbcd
KR
556 offset += frag->fr_fix;
557 }
558
559 fill_size = frag->fr_var;
115147fb 560 if (fill_size && frag->fr_offset > 0)
355afbcd
KR
561 {
562 unsigned int count;
563 unsigned int off = frag->fr_fix;
564 for (count = frag->fr_offset; count; count--)
565 {
03a372ed 566 if (fill_size + frag->fr_address + off < s->s_size)
9a75dc1f
ILT
567 {
568 memcpy (buffer + frag->fr_address + off,
569 frag->fr_literal + frag->fr_fix,
570 fill_size);
571 off += fill_size;
572 offset += fill_size;
573 }
2cb0bdc7 574 }
355afbcd
KR
575 }
576 break;
577 case rs_broken_word:
578 break;
579 default:
580 abort ();
581 }
582 frag = frag->fr_next;
a39116f1 583 }
c58dbabf 584
9a75dc1f 585 if (s->s_size != 0)
016e0d42 586 {
9a75dc1f
ILT
587 if (s->s_scnptr != 0)
588 {
589 bfd_write (buffer, s->s_size, 1, abfd);
590 *file_cursor += s->s_size;
591 }
016e0d42 592 free (buffer);
016e0d42 593 }
016e0d42 594 paddr += s->s_size;
355afbcd
KR
595 }
596 }
db40ba14
SC
597}
598
db40ba14
SC
599/* Coff file generation & utilities */
600
355afbcd 601static void
9a75dc1f 602DEFUN (coff_header_append, (abfd, h),
355afbcd 603 bfd * abfd AND
9a75dc1f 604 object_headers * h)
db40ba14 605{
c593cf41
SC
606 unsigned int i;
607 char buffer[1000];
608 char buffero[1000];
609
355afbcd 610 bfd_seek (abfd, 0, 0);
9a75dc1f
ILT
611
612#ifndef OBJ_COFF_OMIT_OPTIONAL_HEADER
613 H_SET_MAGIC_NUMBER (h, COFF_MAGIC);
614 H_SET_VERSION_STAMP (h, 0);
615 H_SET_ENTRY_POINT (h, 0);
616 H_SET_TEXT_START (h, segment_info[SEG_E0].frchainP->frch_root->fr_address);
617 H_SET_DATA_START (h, segment_info[SEG_E1].frchainP->frch_root->fr_address);
618 H_SET_SIZEOF_OPTIONAL_HEADER (h, bfd_coff_swap_aouthdr_out(abfd, &h->aouthdr,
619 buffero));
620#else /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
621 H_SET_SIZEOF_OPTIONAL_HEADER (h, 0);
622#endif /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
623
624 i = bfd_coff_swap_filehdr_out (abfd, &h->filehdr, buffer);
c593cf41 625
355afbcd 626 bfd_write (buffer, i, 1, abfd);
9a75dc1f 627 bfd_write (buffero, H_GET_SIZEOF_OPTIONAL_HEADER (h), 1, abfd);
c593cf41 628
355afbcd
KR
629 for (i = SEG_E0; i < SEG_E9; i++)
630 {
631 if (segment_info[i].scnhdr.s_name[0])
632 {
633 unsigned int size =
634 bfd_coff_swap_scnhdr_out (abfd,
635 &(segment_info[i].scnhdr),
636 buffer);
637 bfd_write (buffer, size, 1, abfd);
638 }
c593cf41 639 }
db40ba14
SC
640}
641
642
643char *
355afbcd
KR
644DEFUN (symbol_to_chars, (abfd, where, symbolP),
645 bfd * abfd AND
646 char *where AND
647 symbolS * symbolP)
db40ba14 648{
355afbcd
KR
649 unsigned int numaux = symbolP->sy_symbol.ost_entry.n_numaux;
650 unsigned int i;
85051959 651 valueT val;
c593cf41 652
355afbcd
KR
653 /* Turn any symbols with register attributes into abs symbols */
654 if (S_GET_SEGMENT (symbolP) == SEG_REGISTER)
c593cf41 655 {
355afbcd 656 S_SET_SEGMENT (symbolP, SEG_ABSOLUTE);
c593cf41 657 }
355afbcd
KR
658 /* At the same time, relocate all symbols to their output value */
659
85051959
ILT
660 val = (segment_info[S_GET_SEGMENT (symbolP)].scnhdr.s_paddr
661 + S_GET_VALUE (symbolP));
662
663 S_SET_VALUE (symbolP, val);
664
665 symbolP->sy_symbol.ost_entry.n_value = val;
355afbcd
KR
666
667 where += bfd_coff_swap_sym_out (abfd, &symbolP->sy_symbol.ost_entry,
668 where);
669
670 for (i = 0; i < numaux; i++)
671 {
672 where += bfd_coff_swap_aux_out (abfd,
673 &symbolP->sy_symbol.ost_auxent[i],
674 S_GET_DATA_TYPE (symbolP),
675 S_GET_STORAGE_CLASS (symbolP),
96a88760 676 i, numaux, where);
c593cf41 677 }
355afbcd
KR
678 return where;
679
680}
db40ba14
SC
681
682
355afbcd
KR
683void
684obj_symbol_new_hook (symbolP)
685 symbolS *symbolP;
db40ba14 686{
355afbcd
KR
687 char underscore = 0; /* Symbol has leading _ */
688
689 /* Effective symbol */
690 /* Store the pointer in the offset. */
691 S_SET_ZEROES (symbolP, 0L);
692 S_SET_DATA_TYPE (symbolP, T_NULL);
693 S_SET_STORAGE_CLASS (symbolP, 0);
694 S_SET_NUMBER_AUXILIARY (symbolP, 0);
695 /* Additional information */
696 symbolP->sy_symbol.ost_flags = 0;
697 /* Auxiliary entries */
604633ae 698 memset ((char *) &symbolP->sy_symbol.ost_auxent[0], 0, AUXESZ);
c593cf41 699
db40ba14 700#ifdef STRIP_UNDERSCORE
355afbcd 701 /* Remove leading underscore at the beginning of the symbol.
db40ba14
SC
702 * This is to be compatible with the standard librairies.
703 */
355afbcd
KR
704 if (*S_GET_NAME (symbolP) == '_')
705 {
706 underscore = 1;
707 S_SET_NAME (symbolP, S_GET_NAME (symbolP) + 1);
708 } /* strip underscore */
db40ba14 709#endif /* STRIP_UNDERSCORE */
c593cf41 710
355afbcd
KR
711 if (S_IS_STRING (symbolP))
712 SF_SET_STRING (symbolP);
713 if (!underscore && S_IS_LOCAL (symbolP))
714 SF_SET_LOCAL (symbolP);
c999fd9f 715}
db40ba14 716
355afbcd 717/* stack stuff */
604633ae 718static stack *
355afbcd
KR
719stack_init (chunk_size, element_size)
720 unsigned long chunk_size;
721 unsigned long element_size;
db40ba14 722{
355afbcd 723 stack *st;
c593cf41 724
355afbcd
KR
725 if ((st = (stack *) malloc (sizeof (stack))) == (stack *) 0)
726 return (stack *) 0;
727 if ((st->data = malloc (chunk_size)) == (char *) 0)
728 {
729 free (st);
730 return (stack *) 0;
731 }
732 st->pointer = 0;
733 st->size = chunk_size;
734 st->chunk_size = chunk_size;
735 st->element_size = element_size;
736 return st;
737} /* stack_init() */
738
739void
740stack_delete (st)
741 stack *st;
db40ba14 742{
355afbcd
KR
743 free (st->data);
744 free (st);
db40ba14
SC
745}
746
604633ae 747static char *
355afbcd
KR
748stack_push (st, element)
749 stack *st;
750 char *element;
db40ba14 751{
355afbcd
KR
752 if (st->pointer + st->element_size >= st->size)
753 {
754 st->size += st->chunk_size;
755 if ((st->data = xrealloc (st->data, st->size)) == (char *) 0)
756 return (char *) 0;
757 }
758 memcpy (st->data + st->pointer, element, st->element_size);
759 st->pointer += st->element_size;
760 return st->data + st->pointer;
761} /* stack_push() */
db40ba14 762
604633ae 763static char *
355afbcd
KR
764stack_pop (st)
765 stack *st;
db40ba14 766{
604633ae 767 if (st->pointer < st->element_size)
355afbcd
KR
768 {
769 st->pointer = 0;
770 return (char *) 0;
c593cf41 771 }
604633ae 772 st->pointer -= st->element_size;
355afbcd 773 return st->data + st->pointer;
db40ba14
SC
774}
775
604633ae
ILT
776#if 0
777/* Not used. */
778static char *
355afbcd
KR
779stack_top (st)
780 stack *st;
db40ba14 781{
355afbcd 782 return st->data + st->pointer - st->element_size;
db40ba14 783}
604633ae 784#endif
db40ba14
SC
785
786/*
787 * Handle .ln directives.
788 */
789
355afbcd 790static void
9a7d824a
ILT
791obj_coff_ln (appline)
792 int appline;
9ce31b66 793{
c593cf41 794 int l;
355afbcd 795
9a7d824a 796 if (! appline && def_symbol_in_progress != NULL)
355afbcd
KR
797 {
798 as_warn (".ln pseudo-op inside .def/.endef: ignored.");
799 demand_empty_rest_of_line ();
c593cf41
SC
800 return;
801 } /* wrong context */
802
355afbcd
KR
803 c_line_new (0,
804 obstack_next_free (&frags) - frag_now->fr_literal,
805 l = get_absolute_expression (),
806 frag_now);
9ce31b66 807#ifndef NO_LISTING
c593cf41 808 {
355afbcd
KR
809 extern int listing;
810
811 if (listing)
812 {
9a7d824a
ILT
813 if (! appline)
814 l += line_base - 1;
4f0bccc7 815 listing_source_line ((unsigned int) l);
355afbcd
KR
816 }
817
c593cf41 818 }
9ce31b66 819#endif
355afbcd 820 demand_empty_rest_of_line ();
c999fd9f 821}
db40ba14
SC
822
823/*
824 * def()
825 *
826 * Handle .def directives.
827 *
828 * One might ask : why can't we symbol_new if the symbol does not
829 * already exist and fill it with debug information. Because of
830 * the C_EFCN special symbol. It would clobber the value of the
831 * function symbol before we have a chance to notice that it is
832 * a C_EFCN. And a second reason is that the code is more clear this
833 * way. (at least I think it is :-).
834 *
835 */
836
837#define SKIP_SEMI_COLON() while (*input_line_pointer++ != ';')
838#define SKIP_WHITESPACES() while (*input_line_pointer == ' ' || \
c593cf41
SC
839 *input_line_pointer == '\t') \
840 input_line_pointer++;
db40ba14 841
355afbcd
KR
842static void
843DEFUN (obj_coff_def, (what),
844 int what)
db40ba14 845{
355afbcd
KR
846 char name_end; /* Char after the end of name */
847 char *symbol_name; /* Name of the debug symbol */
848 char *symbol_name_copy; /* Temporary copy of the name */
849 unsigned int symbol_name_length;
850 /*$char* directiveP;$ *//* Name of the pseudo opcode */
851 /*$char directive[MAX_DIRECTIVE];$ *//* Backup of the directive */
852 /*$char end = 0;$ *//* If 1, stop parsing */
853
854 if (def_symbol_in_progress != NULL)
855 {
856 as_warn (".def pseudo-op used inside of .def/.endef: ignored.");
857 demand_empty_rest_of_line ();
858 return;
859 } /* if not inside .def/.endef */
860
861 SKIP_WHITESPACES ();
862
863 def_symbol_in_progress = (symbolS *) obstack_alloc (&notes, sizeof (*def_symbol_in_progress));
604633ae 864 memset (def_symbol_in_progress, 0, sizeof (*def_symbol_in_progress));
355afbcd
KR
865
866 symbol_name = input_line_pointer;
867 name_end = get_symbol_end ();
868 symbol_name_length = strlen (symbol_name);
869 symbol_name_copy = xmalloc (symbol_name_length + 1);
870 strcpy (symbol_name_copy, symbol_name);
871
872 /* Initialize the new symbol */
db40ba14 873#ifdef STRIP_UNDERSCORE
355afbcd
KR
874 S_SET_NAME (def_symbol_in_progress, (*symbol_name_copy == '_'
875 ? symbol_name_copy + 1
876 : symbol_name_copy));
877#else /* STRIP_UNDERSCORE */
878 S_SET_NAME (def_symbol_in_progress, symbol_name_copy);
879#endif /* STRIP_UNDERSCORE */
880 /* free(symbol_name_copy); */
4f0bccc7 881 def_symbol_in_progress->sy_name_offset = (unsigned long) ~0;
355afbcd
KR
882 def_symbol_in_progress->sy_number = ~0;
883 def_symbol_in_progress->sy_frag = &zero_address_frag;
5ac34ac3 884 S_SET_VALUE (def_symbol_in_progress, 0);
355afbcd
KR
885
886 if (S_IS_STRING (def_symbol_in_progress))
887 {
888 SF_SET_STRING (def_symbol_in_progress);
889 } /* "long" name */
890
891 *input_line_pointer = name_end;
892
893 demand_empty_rest_of_line ();
c999fd9f 894}
db40ba14
SC
895
896unsigned int dim_index;
604633ae 897
ec0bcbba 898
355afbcd 899static void
604633ae
ILT
900obj_coff_endef (ignore)
901 int ignore;
db40ba14 902{
355afbcd
KR
903 symbolS *symbolP = 0;
904 /* DIM BUG FIX sac@cygnus.com */
905 dim_index = 0;
906 if (def_symbol_in_progress == NULL)
907 {
908 as_warn (".endef pseudo-op used outside of .def/.endef: ignored.");
909 demand_empty_rest_of_line ();
910 return;
911 } /* if not inside .def/.endef */
912
913 /* Set the section number according to storage class. */
914 switch (S_GET_STORAGE_CLASS (def_symbol_in_progress))
915 {
916 case C_STRTAG:
917 case C_ENTAG:
918 case C_UNTAG:
919 SF_SET_TAG (def_symbol_in_progress);
920 /* intentional fallthrough */
921 case C_FILE:
922 case C_TPDEF:
923 SF_SET_DEBUG (def_symbol_in_progress);
924 S_SET_SEGMENT (def_symbol_in_progress, SEG_DEBUG);
925 break;
926
927 case C_EFCN:
928 SF_SET_LOCAL (def_symbol_in_progress); /* Do not emit this symbol. */
929 /* intentional fallthrough */
930 case C_BLOCK:
931 SF_SET_PROCESS (def_symbol_in_progress); /* Will need processing before writing */
932 /* intentional fallthrough */
933 case C_FCN:
934 S_SET_SEGMENT (def_symbol_in_progress, SEG_E0);
935
a36f6645 936 if (strcmp (S_GET_NAME (def_symbol_in_progress), ".bf") == 0)
355afbcd
KR
937 { /* .bf */
938 if (function_lineoff < 0)
939 {
940 fprintf (stderr, "`.bf' symbol without preceding function\n");
941 } /* missing function symbol */
942 SA_GET_SYM_LNNOPTR (last_line_symbol) = function_lineoff;
943
944 SF_SET_PROCESS (last_line_symbol);
945 function_lineoff = -1;
946 }
ec0bcbba
SC
947 /* Value is always set to . */
948 def_symbol_in_progress->sy_frag = frag_now;
949 S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
355afbcd 950 break;
c593cf41 951
db40ba14 952#ifdef C_AUTOARG
355afbcd
KR
953 case C_AUTOARG:
954#endif /* C_AUTOARG */
955 case C_AUTO:
956 case C_REG:
957 case C_MOS:
958 case C_MOE:
959 case C_MOU:
960 case C_ARG:
961 case C_REGPARM:
962 case C_FIELD:
963 case C_EOS:
964 SF_SET_DEBUG (def_symbol_in_progress);
965 S_SET_SEGMENT (def_symbol_in_progress, SEG_ABSOLUTE);
966 break;
967
968 case C_EXT:
969 case C_STAT:
970 case C_LABEL:
971 /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
972 break;
973
974 case C_USTATIC:
975 case C_EXTDEF:
976 case C_ULABEL:
977 as_warn ("unexpected storage class %d", S_GET_STORAGE_CLASS (def_symbol_in_progress));
978 break;
979 } /* switch on storage class */
980
b27caf27
KR
981 /* Now that we have built a debug symbol, try to find if we should
982 merge with an existing symbol or not. If a symbol is C_EFCN or
983 SEG_ABSOLUTE or untagged SEG_DEBUG it never merges. We also
984 don't merge labels, which are in a different namespace, nor
985 symbols which have not yet been defined since they are typically
986 unique, nor do we merge tags with non-tags. */
987
988 /* Two cases for functions. Either debug followed by definition or
989 definition followed by debug. For definition first, we will
990 merge the debug symbol into the definition. For debug first, the
991 lineno entry MUST point to the definition function or else it
992 will point off into space when crawl_symbols() merges the debug
993 symbol into the real symbol. Therefor, let's presume the debug
994 symbol is a real function reference. */
995
996 /* FIXME-SOON If for some reason the definition label/symbol is
997 never seen, this will probably leave an undefined symbol at link
998 time. */
c593cf41 999
355afbcd 1000 if (S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_EFCN
016e0d42 1001 || S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_LABEL
355afbcd
KR
1002 || (S_GET_SEGMENT (def_symbol_in_progress) == SEG_DEBUG
1003 && !SF_GET_TAG (def_symbol_in_progress))
1004 || S_GET_SEGMENT (def_symbol_in_progress) == SEG_ABSOLUTE
5ac34ac3 1005 || def_symbol_in_progress->sy_value.X_op != O_constant
016e0d42
ILT
1006 || (symbolP = symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP)) == NULL
1007 || (SF_GET_TAG (def_symbol_in_progress) != SF_GET_TAG (symbolP)))
355afbcd 1008 {
b27caf27
KR
1009 symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP,
1010 &symbol_lastP);
355afbcd
KR
1011 }
1012 else
1013 {
b27caf27
KR
1014 /* This symbol already exists, merge the newly created symbol
1015 into the This is not mandatory. The linker can handle
1016 duplicate symbols correctly. But I guess that it save a *lot*
1017 of space if the assembly file defines a lot of
1018 symbols. [loic] */
c593cf41 1019
b27caf27
KR
1020 /* The debug entry (def_symbol_in_progress) is merged into the
1021 previous definition. */
c593cf41 1022
355afbcd
KR
1023 c_symbol_merge (def_symbol_in_progress, symbolP);
1024 /* FIXME-SOON Should *def_symbol_in_progress be free'd? xoxorich. */
1025 def_symbol_in_progress = symbolP;
c593cf41 1026
355afbcd
KR
1027 if (SF_GET_FUNCTION (def_symbol_in_progress)
1028 || SF_GET_TAG (def_symbol_in_progress))
1029 {
b27caf27
KR
1030 /* For functions, and tags, the symbol *must* be where the
1031 debug symbol appears. Move the existing symbol to the
1032 current place. */
355afbcd
KR
1033 /* If it already is at the end of the symbol list, do nothing */
1034 if (def_symbol_in_progress != symbol_lastP)
1035 {
b27caf27
KR
1036 symbol_remove (def_symbol_in_progress, &symbol_rootP,
1037 &symbol_lastP);
1038 symbol_append (def_symbol_in_progress, symbol_lastP,
1039 &symbol_rootP, &symbol_lastP);
355afbcd
KR
1040 } /* if not already in place */
1041 } /* if function */
1042 } /* normal or mergable */
1043
1044 if (SF_GET_TAG (def_symbol_in_progress)
1045 && symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP) == NULL)
1046 {
1047 tag_insert (S_GET_NAME (def_symbol_in_progress), def_symbol_in_progress);
b27caf27 1048 }
c593cf41 1049
355afbcd
KR
1050 if (SF_GET_FUNCTION (def_symbol_in_progress))
1051 {
1052 know (sizeof (def_symbol_in_progress) <= sizeof (long));
1053 function_lineoff
1054 = c_line_new (def_symbol_in_progress, 0, 0, &zero_address_frag);
c593cf41 1055
355afbcd 1056 SF_SET_PROCESS (def_symbol_in_progress);
c593cf41 1057
355afbcd
KR
1058 if (symbolP == NULL)
1059 {
b27caf27
KR
1060 /* That is, if this is the first time we've seen the
1061 function... */
355afbcd
KR
1062 symbol_table_insert (def_symbol_in_progress);
1063 } /* definition follows debug */
1064 } /* Create the line number entry pointing to the function being defined */
c593cf41 1065
355afbcd
KR
1066 def_symbol_in_progress = NULL;
1067 demand_empty_rest_of_line ();
b27caf27 1068}
db40ba14 1069
355afbcd 1070static void
604633ae
ILT
1071obj_coff_dim (ignore)
1072 int ignore;
db40ba14 1073{
355afbcd 1074 register int dim_index;
c593cf41 1075
355afbcd 1076 if (def_symbol_in_progress == NULL)
c593cf41 1077 {
355afbcd
KR
1078 as_warn (".dim pseudo-op used outside of .def/.endef: ignored.");
1079 demand_empty_rest_of_line ();
1080 return;
c593cf41
SC
1081 } /* if not inside .def/.endef */
1082
355afbcd 1083 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
c593cf41 1084
355afbcd 1085 for (dim_index = 0; dim_index < DIMNUM; dim_index++)
c593cf41 1086 {
355afbcd
KR
1087 SKIP_WHITESPACES ();
1088 SA_SET_SYM_DIMEN (def_symbol_in_progress, dim_index, get_absolute_expression ());
c593cf41 1089
355afbcd 1090 switch (*input_line_pointer)
c593cf41
SC
1091 {
1092
355afbcd
KR
1093 case ',':
1094 input_line_pointer++;
1095 break;
1096
1097 default:
1098 as_warn ("badly formed .dim directive ignored");
1099 /* intentional fallthrough */
1100 case '\n':
1101 case ';':
1102 dim_index = DIMNUM;
1103 break;
c593cf41
SC
1104 } /* switch on following character */
1105 } /* for each dimension */
1106
355afbcd 1107 demand_empty_rest_of_line ();
c999fd9f 1108}
db40ba14 1109
355afbcd 1110static void
604633ae
ILT
1111obj_coff_line (ignore)
1112 int ignore;
9ce31b66 1113{
c593cf41 1114 int this_base;
355afbcd
KR
1115
1116 if (def_symbol_in_progress == NULL)
1117 {
9a7d824a 1118 obj_coff_ln (0);
c593cf41
SC
1119 return;
1120 } /* if it looks like a stabs style line */
1121
355afbcd
KR
1122 this_base = get_absolute_expression ();
1123 if (this_base > line_base)
1124 {
1125 line_base = this_base;
1126 }
1127
1128
1129#ifndef NO_LISTING
c593cf41 1130 {
355afbcd
KR
1131 extern int listing;
1132 if (listing && 0)
1133 {
4f0bccc7 1134 listing_source_line ((unsigned int) line_base);
355afbcd 1135 }
c593cf41 1136 }
9ce31b66 1137#endif
355afbcd
KR
1138 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
1139 SA_SET_SYM_LNNO (def_symbol_in_progress, line_base);
c593cf41 1140
355afbcd 1141 demand_empty_rest_of_line ();
c999fd9f 1142}
db40ba14 1143
355afbcd 1144static void
604633ae
ILT
1145obj_coff_size (ignore)
1146 int ignore;
355afbcd
KR
1147{
1148 if (def_symbol_in_progress == NULL)
1149 {
1150 as_warn (".size pseudo-op used outside of .def/.endef ignored.");
1151 demand_empty_rest_of_line ();
1152 return;
1153 } /* if not inside .def/.endef */
1154
1155 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
1156 SA_SET_SYM_SIZE (def_symbol_in_progress, get_absolute_expression ());
1157 demand_empty_rest_of_line ();
c999fd9f 1158}
355afbcd
KR
1159
1160static void
604633ae
ILT
1161obj_coff_scl (ignore)
1162 int ignore;
355afbcd
KR
1163{
1164 if (def_symbol_in_progress == NULL)
1165 {
1166 as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
1167 demand_empty_rest_of_line ();
1168 return;
1169 } /* if not inside .def/.endef */
1170
1171 S_SET_STORAGE_CLASS (def_symbol_in_progress, get_absolute_expression ());
1172 demand_empty_rest_of_line ();
c999fd9f 1173}
355afbcd
KR
1174
1175static void
604633ae
ILT
1176obj_coff_tag (ignore)
1177 int ignore;
355afbcd
KR
1178{
1179 char *symbol_name;
1180 char name_end;
1181
1182 if (def_symbol_in_progress == NULL)
1183 {
1184 as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
1185 demand_empty_rest_of_line ();
1186 return;
1187 } /* if not inside .def/.endef */
1188
1189 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
1190 symbol_name = input_line_pointer;
1191 name_end = get_symbol_end ();
1192
1193 /* Assume that the symbol referred to by .tag is always defined. */
1194 /* This was a bad assumption. I've added find_or_make. xoxorich. */
1195 SA_SET_SYM_TAGNDX (def_symbol_in_progress, (long) tag_find_or_make (symbol_name));
1196 if (SA_GET_SYM_TAGNDX (def_symbol_in_progress) == 0L)
1197 {
1198 as_warn ("tag not found for .tag %s", symbol_name);
1199 } /* not defined */
1200
1201 SF_SET_TAGGED (def_symbol_in_progress);
1202 *input_line_pointer = name_end;
1203
1204 demand_empty_rest_of_line ();
c999fd9f 1205}
355afbcd
KR
1206
1207static void
604633ae
ILT
1208obj_coff_type (ignore)
1209 int ignore;
355afbcd
KR
1210{
1211 if (def_symbol_in_progress == NULL)
1212 {
1213 as_warn (".type pseudo-op used outside of .def/.endef ignored.");
1214 demand_empty_rest_of_line ();
1215 return;
1216 } /* if not inside .def/.endef */
1217
1218 S_SET_DATA_TYPE (def_symbol_in_progress, get_absolute_expression ());
1219
1220 if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress)) &&
1221 S_GET_STORAGE_CLASS (def_symbol_in_progress) != C_TPDEF)
1222 {
1223 SF_SET_FUNCTION (def_symbol_in_progress);
1224 } /* is a function */
1225
1226 demand_empty_rest_of_line ();
c999fd9f 1227}
355afbcd
KR
1228
1229static void
604633ae
ILT
1230obj_coff_val (ignore)
1231 int ignore;
355afbcd
KR
1232{
1233 if (def_symbol_in_progress == NULL)
1234 {
1235 as_warn (".val pseudo-op used outside of .def/.endef ignored.");
1236 demand_empty_rest_of_line ();
1237 return;
1238 } /* if not inside .def/.endef */
1239
1240 if (is_name_beginner (*input_line_pointer))
1241 {
1242 char *symbol_name = input_line_pointer;
1243 char name_end = get_symbol_end ();
1244
1245 if (!strcmp (symbol_name, "."))
1246 {
1247 def_symbol_in_progress->sy_frag = frag_now;
4f0bccc7 1248 S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
355afbcd
KR
1249 /* If the .val is != from the .def (e.g. statics) */
1250 }
1251 else if (strcmp (S_GET_NAME (def_symbol_in_progress), symbol_name))
1252 {
5ac34ac3 1253 def_symbol_in_progress->sy_value.X_op = O_symbol;
5868b1fe
ILT
1254 def_symbol_in_progress->sy_value.X_add_symbol =
1255 symbol_find_or_make (symbol_name);
5ac34ac3 1256 def_symbol_in_progress->sy_value.X_op_symbol = NULL;
5868b1fe 1257 def_symbol_in_progress->sy_value.X_add_number = 0;
5868b1fe
ILT
1258
1259 /* If the segment is undefined when the forward reference is
1260 resolved, then copy the segment id from the forward
1261 symbol. */
355afbcd 1262 SF_SET_GET_SEGMENT (def_symbol_in_progress);
016e0d42
ILT
1263
1264 /* FIXME: gcc can generate address expressions
1265 here in unusual cases (search for "obscure"
1266 in sdbout.c). We just ignore the offset
1267 here, thus generating incorrect debugging
1268 information. We ignore the rest of the
1269 line just below. */
355afbcd 1270 }
016e0d42
ILT
1271 /* Otherwise, it is the name of a non debug symbol and
1272 its value will be calculated later. */
355afbcd 1273 *input_line_pointer = name_end;
016e0d42
ILT
1274
1275 /* FIXME: this is to avoid an error message in the
1276 FIXME case mentioned just above. */
604633ae 1277 while (! is_end_of_line[(unsigned char) *input_line_pointer])
016e0d42 1278 ++input_line_pointer;
355afbcd
KR
1279 }
1280 else
1281 {
4f0bccc7
ILT
1282 S_SET_VALUE (def_symbol_in_progress,
1283 (valueT) get_absolute_expression ());
355afbcd 1284 } /* if symbol based */
c593cf41 1285
355afbcd 1286 demand_empty_rest_of_line ();
c999fd9f 1287}
db40ba14
SC
1288
1289/*
1290 * Maintain a list of the tagnames of the structres.
1291 */
1292
355afbcd
KR
1293static void
1294tag_init ()
1295{
1296 tag_hash = hash_new ();
c999fd9f 1297}
db40ba14 1298
355afbcd
KR
1299static void
1300tag_insert (name, symbolP)
1301 char *name;
1302 symbolS *symbolP;
db40ba14 1303{
604633ae 1304 register const char *error_string;
c593cf41 1305
43029a8f 1306 if ((error_string = hash_jam (tag_hash, name, (char *) symbolP)))
355afbcd
KR
1307 {
1308 as_fatal ("Inserting \"%s\" into structure table failed: %s",
1309 name, error_string);
1310 }
c999fd9f 1311}
db40ba14 1312
355afbcd
KR
1313static symbolS *
1314tag_find_or_make (name)
1315 char *name;
db40ba14 1316{
355afbcd 1317 symbolS *symbolP;
c593cf41 1318
355afbcd
KR
1319 if ((symbolP = tag_find (name)) == NULL)
1320 {
1321 symbolP = symbol_new (name,
1322 SEG_UNKNOWN,
1323 0,
1324 &zero_address_frag);
c593cf41 1325
355afbcd 1326 tag_insert (S_GET_NAME (symbolP), symbolP);
355afbcd 1327 } /* not found */
c593cf41 1328
355afbcd
KR
1329 return (symbolP);
1330} /* tag_find_or_make() */
db40ba14 1331
355afbcd
KR
1332static symbolS *
1333tag_find (name)
1334 char *name;
db40ba14
SC
1335{
1336#ifdef STRIP_UNDERSCORE
355afbcd
KR
1337 if (*name == '_')
1338 name++;
db40ba14 1339#endif /* STRIP_UNDERSCORE */
355afbcd
KR
1340 return ((symbolS *) hash_find (tag_hash, name));
1341} /* tag_find() */
db40ba14 1342
355afbcd
KR
1343void
1344obj_read_begin_hook ()
1345{
1346 /* These had better be the same. Usually 18 bytes. */
db40ba14 1347#ifndef BFD_HEADERS
355afbcd
KR
1348 know (sizeof (SYMENT) == sizeof (AUXENT));
1349 know (SYMESZ == AUXESZ);
db40ba14 1350#endif
355afbcd 1351 tag_init ();
c999fd9f 1352}
db40ba14
SC
1353
1354/* This function runs through the symbol table and puts all the
1355 externals onto another chain */
1356
1357/* The chain of externals */
1358symbolS *symbol_externP = NULL;
1359symbolS *symbol_extern_lastP = NULL;
1360
355afbcd
KR
1361stack *block_stack;
1362symbolS *last_functionP = NULL;
1363symbolS *last_tagP;
db40ba14 1364
355afbcd
KR
1365static unsigned int
1366DEFUN_VOID (yank_symbols)
db40ba14 1367{
c593cf41 1368 symbolS *symbolP;
355afbcd 1369 unsigned int symbol_number = 0;
c0f1bbb6 1370 unsigned int last_file_symno = 0;
355afbcd 1371
c593cf41
SC
1372 for (symbolP = symbol_rootP;
1373 symbolP;
355afbcd
KR
1374 symbolP = symbolP ? symbol_next (symbolP) : symbol_rootP)
1375 {
1376 if (!SF_GET_DEBUG (symbolP))
1377 {
c593cf41 1378 /* Debug symbols do not need all this rubbish */
355afbcd 1379 symbolS *real_symbolP;
c593cf41
SC
1380
1381 /* L* and C_EFCN symbols never merge. */
355afbcd 1382 if (!SF_GET_LOCAL (symbolP)
016e0d42 1383 && S_GET_STORAGE_CLASS (symbolP) != C_LABEL
5ac34ac3 1384 && symbolP->sy_value.X_op == O_constant
355afbcd
KR
1385 && (real_symbolP = symbol_find_base (S_GET_NAME (symbolP), DO_NOT_STRIP))
1386 && real_symbolP != symbolP)
1387 {
c593cf41
SC
1388 /* FIXME-SOON: where do dups come from?
1389 Maybe tag references before definitions? xoxorich. */
1390 /* Move the debug data from the debug symbol to the
1391 real symbol. Do NOT do the oposite (i.e. move from
1392 real symbol to debug symbol and remove real symbol from the
1393 list.) Because some pointers refer to the real symbol
1394 whereas no pointers refer to the debug symbol. */
355afbcd 1395 c_symbol_merge (symbolP, real_symbolP);
c593cf41
SC
1396 /* Replace the current symbol by the real one */
1397 /* The symbols will never be the last or the first
1398 because : 1st symbol is .file and 3 last symbols are
1399 .text, .data, .bss */
355afbcd
KR
1400 symbol_remove (real_symbolP, &symbol_rootP, &symbol_lastP);
1401 symbol_insert (real_symbolP, symbolP, &symbol_rootP, &symbol_lastP);
1402 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
c593cf41
SC
1403 symbolP = real_symbolP;
1404 } /* if not local but dup'd */
1405
355afbcd
KR
1406 if (flagseen['R'] && (S_GET_SEGMENT (symbolP) == SEG_E1))
1407 {
1408 S_SET_SEGMENT (symbolP, SEG_E0);
c593cf41
SC
1409 } /* push data into text */
1410
5868b1fe 1411 resolve_symbol_value (symbolP);
c593cf41 1412
e0209756 1413 if (S_GET_STORAGE_CLASS (symbolP) == C_NULL)
c593cf41 1414 {
e0209756
ILT
1415 if (!S_IS_DEFINED (symbolP) && !SF_GET_LOCAL (symbolP))
1416 {
1417 S_SET_EXTERNAL (symbolP);
1418 }
1419 else if (S_GET_SEGMENT (symbolP) == SEG_E0)
355afbcd
KR
1420 {
1421 S_SET_STORAGE_CLASS (symbolP, C_LABEL);
1422 }
1423 else
1424 {
1425 S_SET_STORAGE_CLASS (symbolP, C_STAT);
1426 }
c593cf41 1427 }
c593cf41
SC
1428
1429 /* Mainly to speed up if not -g */
355afbcd
KR
1430 if (SF_GET_PROCESS (symbolP))
1431 {
1432 /* Handle the nested blocks auxiliary info. */
1433 if (S_GET_STORAGE_CLASS (symbolP) == C_BLOCK)
1434 {
1435 if (!strcmp (S_GET_NAME (symbolP), ".bb"))
1436 stack_push (block_stack, (char *) &symbolP);
1437 else
1438 { /* .eb */
1439 register symbolS *begin_symbolP;
1440 begin_symbolP = *(symbolS **) stack_pop (block_stack);
1441 if (begin_symbolP == (symbolS *) 0)
1442 as_warn ("mismatched .eb");
1443 else
1444 SA_SET_SYM_ENDNDX (begin_symbolP, symbol_number + 2);
1445 }
1446 }
1447 /* If we are able to identify the type of a function, and we
c593cf41
SC
1448 are out of a function (last_functionP == 0) then, the
1449 function symbol will be associated with an auxiliary
1450 entry. */
355afbcd
KR
1451 if (last_functionP == (symbolS *) 0 &&
1452 SF_GET_FUNCTION (symbolP))
1453 {
1454 last_functionP = symbolP;
c593cf41 1455
355afbcd
KR
1456 if (S_GET_NUMBER_AUXILIARY (symbolP) < 1)
1457 {
1458 S_SET_NUMBER_AUXILIARY (symbolP, 1);
1459 } /* make it at least 1 */
c593cf41 1460
355afbcd 1461 /* Clobber possible stale .dim information. */
c593cf41 1462#if 0
355afbcd
KR
1463 /* Iffed out by steve - this fries the lnnoptr info too */
1464 bzero (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen,
1465 sizeof (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen));
c593cf41 1466#endif
355afbcd 1467 }
c0f1bbb6
KR
1468 /* The C_FCN doesn't need any additional information. I
1469 don't even know if this is needed for sdb. But the
1470 standard assembler generates it, so... */
355afbcd
KR
1471 if (S_GET_STORAGE_CLASS (symbolP) == C_EFCN)
1472 {
1473 if (last_functionP == (symbolS *) 0)
1474 as_fatal ("C_EFCN symbol out of scope");
1475 SA_SET_SYM_FSIZE (last_functionP,
1476 (long) (S_GET_VALUE (symbolP) -
1477 S_GET_VALUE (last_functionP)));
1478 SA_SET_SYM_ENDNDX (last_functionP, symbol_number);
ec0bcbba 1479 last_functionP = (symbolS *) 0;
355afbcd
KR
1480 }
1481 }
1482 }
1483 else if (SF_GET_TAG (symbolP))
1484 {
1485 /* First descriptor of a structure must point to
c593cf41 1486 the first slot after the structure description. */
355afbcd
KR
1487 last_tagP = symbolP;
1488
1489 }
1490 else if (S_GET_STORAGE_CLASS (symbolP) == C_EOS)
1491 {
1492 /* +2 take in account the current symbol */
1493 SA_SET_SYM_ENDNDX (last_tagP, symbol_number + 2);
1494 }
1495 else if (S_GET_STORAGE_CLASS (symbolP) == C_FILE)
1496 {
1497 if (S_GET_VALUE (symbolP))
1498 {
c0f1bbb6
KR
1499 S_SET_VALUE (symbolP, last_file_symno);
1500 last_file_symno = symbol_number;
355afbcd
KR
1501 } /* no one points at the first .file symbol */
1502 } /* if debug or tag or eos or file */
c593cf41
SC
1503
1504 /* We must put the external symbols apart. The loader
1505 does not bomb if we do not. But the references in
1506 the endndx field for a .bb symbol are not corrected
1507 if an external symbol is removed between .bb and .be.
1508 I.e in the following case :
1509 [20] .bb endndx = 22
1510 [21] foo external
1511 [22] .be
1512 ld will move the symbol 21 to the end of the list but
1513 endndx will still be 22 instead of 21. */
1514
1515
355afbcd
KR
1516 if (SF_GET_LOCAL (symbolP))
1517 {
c593cf41
SC
1518 /* remove C_EFCN and LOCAL (L...) symbols */
1519 /* next pointer remains valid */
355afbcd 1520 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
c593cf41
SC
1521
1522 }
355afbcd
KR
1523 else if (!S_IS_DEFINED (symbolP)
1524 && !S_IS_DEBUG (symbolP)
1525 && !SF_GET_STATICS (symbolP) &&
1526 S_GET_STORAGE_CLASS (symbolP) == C_EXT)
1527 { /* C_EXT && !SF_GET_FUNCTION(symbolP)) */
1528 /* if external, Remove from the list */
1529 symbolS *hold = symbol_previous (symbolP);
1530
1531 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
1532 symbol_clear_list_pointers (symbolP);
1533 symbol_append (symbolP, symbol_extern_lastP, &symbol_externP, &symbol_extern_lastP);
1534 symbolP = hold;
1535 }
1536 else
1537 {
1538 if (SF_GET_STRING (symbolP))
1539 {
1540 symbolP->sy_name_offset = string_byte_count;
1541 string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
1542 }
1543 else
1544 {
1545 symbolP->sy_name_offset = 0;
1546 } /* fix "long" names */
1547
1548 symbolP->sy_number = symbol_number;
1549 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
1550 } /* if local symbol */
c593cf41
SC
1551 } /* traverse the symbol list */
1552 return symbol_number;
355afbcd 1553
db40ba14
SC
1554}
1555
1556
355afbcd
KR
1557static unsigned int
1558DEFUN_VOID (glue_symbols)
db40ba14 1559{
c593cf41 1560 unsigned int symbol_number = 0;
355afbcd
KR
1561 symbolS *symbolP;
1562 for (symbolP = symbol_externP; symbol_externP;)
1563 {
c593cf41
SC
1564 symbolS *tmp = symbol_externP;
1565
1566 /* append */
355afbcd
KR
1567 symbol_remove (tmp, &symbol_externP, &symbol_extern_lastP);
1568 symbol_append (tmp, symbol_lastP, &symbol_rootP, &symbol_lastP);
c593cf41
SC
1569
1570 /* and process */
355afbcd
KR
1571 if (SF_GET_STRING (tmp))
1572 {
c593cf41 1573 tmp->sy_name_offset = string_byte_count;
355afbcd
KR
1574 string_byte_count += strlen (S_GET_NAME (tmp)) + 1;
1575 }
1576 else
1577 {
1578 tmp->sy_name_offset = 0;
1579 } /* fix "long" names */
c593cf41
SC
1580
1581 tmp->sy_number = symbol_number;
355afbcd 1582 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (tmp);
c593cf41
SC
1583 } /* append the entire extern chain */
1584 return symbol_number;
355afbcd 1585
db40ba14
SC
1586}
1587
355afbcd
KR
1588static unsigned int
1589DEFUN_VOID (tie_tags)
db40ba14 1590{
c593cf41 1591 unsigned int symbol_number = 0;
355afbcd
KR
1592
1593 symbolS *symbolP;
c593cf41 1594 for (symbolP = symbol_rootP; symbolP; symbolP =
355afbcd
KR
1595 symbol_next (symbolP))
1596 {
1597 symbolP->sy_number = symbol_number;
c593cf41
SC
1598
1599
1600
355afbcd
KR
1601 if (SF_GET_TAGGED (symbolP))
1602 {
1603 SA_SET_SYM_TAGNDX
1604 (symbolP,
1605 ((symbolS *) SA_GET_SYM_TAGNDX (symbolP))->sy_number);
1606 }
c593cf41 1607
355afbcd
KR
1608 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
1609 }
c593cf41 1610 return symbol_number;
355afbcd 1611
db40ba14
SC
1612}
1613
355afbcd 1614static void
9a75dc1f
ILT
1615DEFUN (crawl_symbols, (h, abfd),
1616 object_headers *h AND
355afbcd 1617 bfd * abfd)
db40ba14 1618{
355afbcd 1619 unsigned int i;
c593cf41
SC
1620
1621 /* Initialize the stack used to keep track of the matching .bb .be */
1622
355afbcd 1623 block_stack = stack_init (512, sizeof (symbolS *));
c593cf41
SC
1624
1625 /* The symbol list should be ordered according to the following sequence
1626 * order :
1627 * . .file symbol
1628 * . debug entries for functions
1629 * . fake symbols for the sections, including.text .data and .bss
1630 * . defined symbols
1631 * . undefined symbols
1632 * But this is not mandatory. The only important point is to put the
1633 * undefined symbols at the end of the list.
1634 */
1635
1636 if (symbol_rootP == NULL
355afbcd
KR
1637 || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE)
1638 {
1639 c_dot_file_symbol ("fake");
c593cf41
SC
1640 }
1641 /* Is there a .file symbol ? If not insert one at the beginning. */
1642
1643 /*
1644 * Build up static symbols for the sections, they are filled in later
1645 */
1646
1647
355afbcd 1648 for (i = SEG_E0; i < SEG_E9; i++)
c593cf41 1649 {
355afbcd
KR
1650 if (segment_info[i].scnhdr.s_name[0])
1651 {
9a75dc1f 1652 char name[9];
355afbcd 1653
9a75dc1f
ILT
1654 strncpy (name, segment_info[i].scnhdr.s_name, 8);
1655 name[8] = '\0';
1656 segment_info[i].dot = c_section_symbol (name, i - SEG_E0 + 1);
355afbcd 1657 }
c593cf41 1658 }
c593cf41
SC
1659
1660
1661 /* Take all the externals out and put them into another chain */
9a75dc1f 1662 H_SET_SYMBOL_TABLE_SIZE (h, yank_symbols ());
c593cf41 1663 /* Take the externals and glue them onto the end.*/
9a75dc1f 1664 H_SET_SYMBOL_TABLE_SIZE (h, H_GET_SYMBOL_COUNT (h) + glue_symbols ());
c593cf41 1665
9a75dc1f 1666 H_SET_SYMBOL_TABLE_SIZE (h, tie_tags ());
355afbcd
KR
1667 know (symbol_externP == NULL);
1668 know (symbol_extern_lastP == NULL);
db40ba14
SC
1669}
1670
1671/*
1672 * Find strings by crawling along symbol table chain.
1673 */
1674
355afbcd
KR
1675void
1676DEFUN (w_strings, (where),
1677 char *where)
db40ba14 1678{
c593cf41
SC
1679 symbolS *symbolP;
1680
1681 /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
4f0bccc7 1682 md_number_to_chars (where, (valueT) string_byte_count, 4);
ad86fa70 1683 where += 4;
c593cf41
SC
1684 for (symbolP = symbol_rootP;
1685 symbolP;
355afbcd
KR
1686 symbolP = symbol_next (symbolP))
1687 {
1688 unsigned int size;
1689
1690 if (SF_GET_STRING (symbolP))
1691 {
1692 size = strlen (S_GET_NAME (symbolP)) + 1;
1693
1694 memcpy (where, S_GET_NAME (symbolP), size);
1695 where += size;
1696
1697 }
1698 }
db40ba14
SC
1699}
1700
355afbcd 1701static void
9a75dc1f 1702DEFUN (do_linenos_for, (abfd, h, file_cursor),
355afbcd 1703 bfd * abfd AND
9a75dc1f 1704 object_headers * h AND
355afbcd 1705 unsigned long *file_cursor)
db40ba14 1706{
c593cf41 1707 unsigned int idx;
9a75dc1f 1708 unsigned long start = *file_cursor;
c593cf41 1709
355afbcd 1710 for (idx = SEG_E0; idx < SEG_E9; idx++)
c593cf41 1711 {
355afbcd 1712 segment_info_type *s = segment_info + idx;
c593cf41 1713
c593cf41 1714
355afbcd
KR
1715 if (s->scnhdr.s_nlnno != 0)
1716 {
1717 struct lineno_list *line_ptr;
1718
1719 struct external_lineno *buffer =
1720 (struct external_lineno *) xmalloc (s->scnhdr.s_nlnno * LINESZ);
c593cf41 1721
355afbcd
KR
1722 struct external_lineno *dst = buffer;
1723
1724 /* Run through the table we've built and turn it into its external
c593cf41
SC
1725 form, take this chance to remove duplicates */
1726
355afbcd
KR
1727 for (line_ptr = s->lineno_list_head;
1728 line_ptr != (struct lineno_list *) NULL;
1729 line_ptr = line_ptr->next)
1730 {
c593cf41 1731
355afbcd
KR
1732 if (line_ptr->line.l_lnno == 0)
1733 {
1734 /* Turn a pointer to a symbol into the symbols' index */
1735 line_ptr->line.l_addr.l_symndx =
1736 ((symbolS *) line_ptr->line.l_addr.l_symndx)->sy_number;
1737 }
1738 else
1739 {
1740 line_ptr->line.l_addr.l_paddr += ((struct frag *) (line_ptr->frag))->fr_address;
1741 }
c593cf41 1742
c593cf41 1743
355afbcd
KR
1744 (void) bfd_coff_swap_lineno_out (abfd, &(line_ptr->line), dst);
1745 dst++;
1746
1747 }
1748
1749 s->scnhdr.s_lnnoptr = *file_cursor;
1750
1751 bfd_write (buffer, 1, s->scnhdr.s_nlnno * LINESZ, abfd);
1752 free (buffer);
1753
1754 *file_cursor += s->scnhdr.s_nlnno * LINESZ;
1755 }
c593cf41 1756 }
9a75dc1f 1757 H_SET_LINENO_SIZE (h, *file_cursor - start);
db40ba14
SC
1758}
1759
1760
1761/* Now we run through the list of frag chains in a segment and
1762 make all the subsegment frags appear at the end of the
1763 list, as if the seg 0 was extra long */
1764
355afbcd
KR
1765static void
1766DEFUN_VOID (remove_subsegs)
db40ba14 1767{
355afbcd
KR
1768 unsigned int i;
1769
1770 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1771 {
1772 frchainS *head = segment_info[i].frchainP;
1773 fragS dummy;
1774 fragS *prev_frag = &dummy;
1775
1776 while (head && head->frch_seg == i)
c593cf41 1777 {
355afbcd
KR
1778 prev_frag->fr_next = head->frch_root;
1779 prev_frag = head->frch_last;
1780 head = head->frch_next;
c593cf41 1781 }
355afbcd 1782 prev_frag->fr_next = 0;
c593cf41 1783 }
db40ba14
SC
1784}
1785
4f0bccc7 1786unsigned long machine;
163107a1 1787int coff_flags;
355afbcd
KR
1788extern void
1789DEFUN_VOID (write_object_file)
db40ba14 1790{
355afbcd 1791 int i;
8f3956b3 1792 char *name;
355afbcd 1793 struct frchain *frchain_ptr;
c593cf41 1794
9a75dc1f 1795 object_headers headers;
355afbcd
KR
1796 unsigned long file_cursor;
1797 bfd *abfd;
1798 unsigned int addr;
1799 abfd = bfd_openw (out_file_name, TARGET_FORMAT);
c593cf41
SC
1800
1801
355afbcd
KR
1802 if (abfd == 0)
1803 {
1804 as_perror ("FATAL: Can't create %s", out_file_name);
1805 exit (42);
1806 }
1807 bfd_set_format (abfd, bfd_object);
1808 bfd_set_arch_mach (abfd, BFD_ARCH, machine);
c593cf41 1809
355afbcd
KR
1810 string_byte_count = 4;
1811
1812 for (frchain_ptr = frchain_root;
1813 frchain_ptr != (struct frchain *) NULL;
1814 frchain_ptr = frchain_ptr->frch_next)
1815 {
1816 /* Run through all the sub-segments and align them up. Also close any
c593cf41
SC
1817 open frags. We tack a .fill onto the end of the frag chain so
1818 that any .align's size can be worked by looking at the next
1819 frag */
1820
604633ae 1821 subseg_set (frchain_ptr->frch_seg, frchain_ptr->frch_subseg);
016e0d42 1822#ifndef SUB_SEGMENT_ALIGN
2492e118 1823#define SUB_SEGMENT_ALIGN(SEG) 1
016e0d42 1824#endif
2492e118 1825 frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE);
355afbcd
KR
1826 frag_wane (frag_now);
1827 frag_now->fr_fix = 0;
1828 know (frag_now->fr_next == NULL);
1829 }
c593cf41
SC
1830
1831
355afbcd 1832 remove_subsegs ();
c593cf41 1833
355afbcd
KR
1834
1835 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
c593cf41 1836 {
355afbcd 1837 relax_segment (segment_info[i].frchainP->frch_root, i);
c593cf41 1838 }
c593cf41 1839
9a75dc1f 1840 H_SET_NUMBER_OF_SECTIONS (&headers, 0);
355afbcd
KR
1841
1842 /* Find out how big the sections are, and set the addresses. */
1843 addr = 0;
1844 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1845 {
9a75dc1f
ILT
1846 long size;
1847
355afbcd 1848 segment_info[i].scnhdr.s_paddr = addr;
e63164f9 1849 segment_info[i].scnhdr.s_vaddr = addr;
c593cf41 1850
355afbcd 1851 if (segment_info[i].scnhdr.s_name[0])
a39116f1 1852 {
9a75dc1f
ILT
1853 H_SET_NUMBER_OF_SECTIONS (&headers,
1854 H_GET_NUMBER_OF_SECTIONS (&headers) + 1);
a39116f1 1855 }
355afbcd 1856
4f0bccc7 1857 size = size_section (abfd, (unsigned int) i);
9a75dc1f 1858 addr += size;
016e0d42 1859
9a75dc1f
ILT
1860 if (i == SEG_E0)
1861 H_SET_TEXT_SIZE (&headers, size);
1862 else if (i == SEG_E1)
1863 H_SET_DATA_SIZE (&headers, size);
1864 else if (i == SEG_E2)
1865 H_SET_BSS_SIZE (&headers, size);
355afbcd 1866 }
c593cf41 1867
9a75dc1f
ILT
1868 /* Turn the gas native symbol table shape into a coff symbol table */
1869 crawl_symbols (&headers, abfd);
1870
1871 if (string_byte_count == 4)
1872 string_byte_count = 0;
c593cf41 1873
9a75dc1f 1874 H_SET_STRING_SIZE (&headers, string_byte_count);
c593cf41 1875
033400ec 1876#if !defined(TC_H8300) && !defined(TC_Z8K)
355afbcd 1877 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
c593cf41 1878 {
9a75dc1f 1879 fixup_mdeps (segment_info[i].frchainP->frch_root, &headers, i);
016e0d42 1880 fixup_segment (&segment_info[i], i);
c593cf41
SC
1881 }
1882#endif
1883
8f3956b3
SS
1884 /* Look for ".stab" segments and fill in their initial symbols
1885 correctly. */
1886 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1887 {
1888 name = segment_info[i].scnhdr.s_name;
1889
1890 if (name != NULL
1891 && strncmp (".stab", name, 5) == 0
1892 && strncmp (".stabstr", name, 8) != 0)
1893 adjust_stab_section (abfd, i);
1894 }
1895
9a75dc1f 1896 file_cursor = H_GET_TEXT_FILE_OFFSET (&headers);
355afbcd 1897
4f0bccc7 1898 bfd_seek (abfd, (file_ptr) file_cursor, 0);
c593cf41 1899
355afbcd 1900 /* Plant the data */
c593cf41 1901
9a75dc1f 1902 fill_section (abfd, &headers, &file_cursor);
c593cf41 1903
9a75dc1f 1904 do_relocs_for (abfd, &headers, &file_cursor);
c593cf41 1905
9a75dc1f 1906 do_linenos_for (abfd, &headers, &file_cursor);
c593cf41 1907
9a75dc1f
ILT
1908 H_SET_FILE_MAGIC_NUMBER (&headers, COFF_MAGIC);
1909#ifndef OBJ_COFF_OMIT_TIMESTAMP
1910 H_SET_TIME_STAMP (&headers, (long)time((long*)0));
1911#else
1912 H_SET_TIME_STAMP (&headers, 0);
1913#endif
c593cf41 1914
9a75dc1f
ILT
1915#ifdef KEEP_RELOC_INFO
1916 H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) |
1917 COFF_FLAGS | coff_flags));
1918#else
1919 H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) |
1920 (H_GET_RELOCATION_SIZE(&headers) ? 0 : F_RELFLG) |
1921 COFF_FLAGS | coff_flags));
1922#endif
c593cf41
SC
1923
1924 {
9a75dc1f
ILT
1925 unsigned int symtable_size = H_GET_SYMBOL_TABLE_SIZE (&headers);
1926 char *buffer1 = xmalloc (symtable_size + string_byte_count + 1);
604633ae 1927
9a75dc1f 1928 H_SET_SYMBOL_TABLE_POINTER (&headers, bfd_tell (abfd));
355afbcd 1929 w_symbols (abfd, buffer1, symbol_rootP);
9a75dc1f
ILT
1930 if (string_byte_count > 0)
1931 w_strings (buffer1 + symtable_size);
1932 bfd_write (buffer1, 1, symtable_size + string_byte_count, abfd);
355afbcd 1933 free (buffer1);
c593cf41 1934 }
9a75dc1f
ILT
1935
1936 coff_header_append (abfd, &headers);
03a372ed
SC
1937#if 0
1938 /* Recent changes to write need this, but where it should
1939 go is up to Ken.. */
355afbcd
KR
1940 if (bfd_close_all_done (abfd) == false)
1941 as_fatal ("Can't close %s: %s", out_file_name,
380fa957 1942 bfd_errmsg (bfd_get_error ()));
03a372ed
SC
1943#else
1944 {
1945 extern bfd *stdoutput;
1946 stdoutput = abfd;
1947 }
1948#endif
1949
db40ba14
SC
1950}
1951
604633ae
ILT
1952/* Add a new segment. This is called from subseg_new via the
1953 obj_new_segment macro. */
db40ba14 1954
604633ae
ILT
1955segT
1956obj_coff_add_segment (name)
1957 const char *name;
db40ba14 1958{
604633ae 1959 unsigned int len;
355afbcd 1960 unsigned int i;
604633ae
ILT
1961
1962 /* Find out if we've already got a section of this name. */
1963 len = strlen (name);
1964 if (len < sizeof (segment_info[i].scnhdr.s_name))
1965 ++len;
1966 else
1967 len = sizeof (segment_info[i].scnhdr.s_name);
355afbcd 1968 for (i = SEG_E0; i < SEG_E9 && segment_info[i].scnhdr.s_name[0]; i++)
604633ae
ILT
1969 if (strncmp (segment_info[i].scnhdr.s_name, name, len) == 0
1970 && (len == sizeof (segment_info[i].scnhdr.s_name)
1971 || segment_info[i].scnhdr.s_name[len] == '\0'))
1972 return (segT) i;
1973
1974 if (i == SEG_E9)
c593cf41 1975 {
604633ae
ILT
1976 as_bad ("Too many new sections; can't add \"%s\"", name);
1977 return now_seg;
c593cf41 1978 }
604633ae
ILT
1979
1980 /* Add a new section. */
1981 strncpy (segment_info[i].scnhdr.s_name, name,
1982 sizeof (segment_info[i].scnhdr.s_name));
b27caf27 1983 segment_info[i].scnhdr.s_flags = STYP_REG;
604633ae
ILT
1984
1985 return (segT) i;
db40ba14
SC
1986}
1987
9a75dc1f
ILT
1988/*
1989 * implement the .section pseudo op:
1990 * .section name {, "flags"}
1991 * ^ ^
1992 * | +--- optional flags: 'b' for bss
1993 * | 'i' for info
1994 * +-- section name 'l' for lib
1995 * 'n' for noload
1996 * 'o' for over
1997 * 'w' for data
56607c4e 1998 * 'd' (apparently m88k for data)
9a75dc1f
ILT
1999 * 'x' for text
2000 * But if the argument is not a quoted string, treat it as a
2001 * subsegment number.
2002 */
2003
355afbcd 2004void
604633ae
ILT
2005obj_coff_section (ignore)
2006 int ignore;
db40ba14 2007{
355afbcd
KR
2008 /* Strip out the section name */
2009 char *section_name;
2010 char *section_name_end;
2011 char c;
9a75dc1f 2012 int argp;
355afbcd
KR
2013 unsigned int len;
2014 unsigned int exp;
9a75dc1f 2015 long flags;
355afbcd
KR
2016
2017 section_name = input_line_pointer;
2018 c = get_symbol_end ();
2019 section_name_end = input_line_pointer;
2020
2021 len = section_name_end - section_name;
2022 input_line_pointer++;
2023 SKIP_WHITESPACE ();
016e0d42 2024
9a75dc1f
ILT
2025 argp = 0;
2026 if (c == ',')
2027 argp = 1;
2028 else if (*input_line_pointer == ',')
355afbcd 2029 {
9a75dc1f
ILT
2030 argp = 1;
2031 ++input_line_pointer;
2032 SKIP_WHITESPACE ();
c593cf41 2033 }
9a75dc1f
ILT
2034
2035 exp = 0;
2036 flags = 0;
2037 if (argp)
c593cf41 2038 {
9a75dc1f
ILT
2039 if (*input_line_pointer != '"')
2040 exp = get_absolute_expression ();
2041 else
2042 {
2043 ++input_line_pointer;
2044 while (*input_line_pointer != '"'
604633ae 2045 && ! is_end_of_line[(unsigned char) *input_line_pointer])
9a75dc1f
ILT
2046 {
2047 switch (*input_line_pointer)
2048 {
2049 case 'b': flags |= STYP_BSS; break;
2050 case 'i': flags |= STYP_INFO; break;
2051 case 'l': flags |= STYP_LIB; break;
2052 case 'n': flags |= STYP_NOLOAD; break;
2053 case 'o': flags |= STYP_OVER; break;
56607c4e 2054 case 'd':
9a75dc1f
ILT
2055 case 'w': flags |= STYP_DATA; break;
2056 case 'x': flags |= STYP_TEXT; break;
2057 default:
2058 as_warn("unknown section attribute '%c'",
2059 *input_line_pointer);
2060 break;
2061 }
2062 ++input_line_pointer;
2063 }
2064 if (*input_line_pointer == '"')
2065 ++input_line_pointer;
2066 }
c593cf41 2067 }
355afbcd 2068
4f0bccc7 2069 subseg_new (section_name, (subsegT) exp);
9a75dc1f
ILT
2070
2071 segment_info[now_seg].scnhdr.s_flags |= flags;
2072
355afbcd 2073 *section_name_end = c;
db40ba14
SC
2074}
2075
2076
355afbcd 2077static void
604633ae
ILT
2078obj_coff_text (ignore)
2079 int ignore;
db40ba14 2080{
604633ae 2081 subseg_new (".text", get_absolute_expression ());
db40ba14
SC
2082}
2083
2084
355afbcd 2085static void
604633ae
ILT
2086obj_coff_data (ignore)
2087 int ignore;
db40ba14 2088{
ffffc8fb 2089 if (flagseen['R'])
604633ae 2090 subseg_new (".text", get_absolute_expression () + 1000);
ffffc8fb 2091 else
604633ae 2092 subseg_new (".data", get_absolute_expression ());
db40ba14
SC
2093}
2094
9a75dc1f 2095static void
604633ae
ILT
2096obj_coff_bss (ignore)
2097 int ignore;
9a75dc1f
ILT
2098{
2099 if (*input_line_pointer == '\n') /* .bss */
604633ae 2100 subseg_new(".bss", get_absolute_expression());
9a75dc1f 2101 else /* .bss id,expr */
604633ae 2102 obj_coff_lcomm(0);
9a75dc1f
ILT
2103}
2104
2105static void
604633ae
ILT
2106obj_coff_ident (ignore)
2107 int ignore;
9a75dc1f
ILT
2108{
2109 segT current_seg = now_seg; /* save current seg */
2110 subsegT current_subseg = now_subseg;
604633ae 2111 subseg_new (".comment", 0); /* .comment seg */
9a75dc1f 2112 stringer (1); /* read string */
604633ae 2113 subseg_set (current_seg, current_subseg); /* restore current seg */
9a75dc1f
ILT
2114}
2115
355afbcd
KR
2116void
2117c_symbol_merge (debug, normal)
2118 symbolS *debug;
2119 symbolS *normal;
db40ba14 2120{
355afbcd
KR
2121 S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug));
2122 S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug));
c593cf41 2123
355afbcd
KR
2124 if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal))
2125 {
2126 S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug));
2127 } /* take the most we have */
c593cf41 2128
355afbcd
KR
2129 if (S_GET_NUMBER_AUXILIARY (debug) > 0)
2130 {
4f0bccc7
ILT
2131 memcpy ((char *) &normal->sy_symbol.ost_auxent[0],
2132 (char *) &debug->sy_symbol.ost_auxent[0],
2133 (unsigned int) (S_GET_NUMBER_AUXILIARY (debug) * AUXESZ));
355afbcd 2134 } /* Move all the auxiliary information */
c593cf41 2135
355afbcd
KR
2136 /* Move the debug flags. */
2137 SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug));
2138} /* c_symbol_merge() */
db40ba14
SC
2139
2140static int
355afbcd
KR
2141DEFUN (c_line_new, (symbol, paddr, line_number, frag),
2142 symbolS * symbol AND
2143 long paddr AND
2144 unsigned short line_number AND
2145 fragS * frag)
db40ba14 2146{
355afbcd
KR
2147 struct lineno_list *new_line =
2148 (struct lineno_list *) xmalloc (sizeof (struct lineno_list));
2149
2150 segment_info_type *s = segment_info + now_seg;
c593cf41
SC
2151 new_line->line.l_lnno = line_number;
2152
355afbcd
KR
2153 if (line_number == 0)
2154 {
2155 last_line_symbol = symbol;
2156 new_line->line.l_addr.l_symndx = (long) symbol;
2157 }
2158 else
2159 {
2160 new_line->line.l_addr.l_paddr = paddr;
2161 }
c593cf41 2162
355afbcd
KR
2163 new_line->frag = (char *) frag;
2164 new_line->next = (struct lineno_list *) NULL;
2165
2166
2167 if (s->lineno_list_head == (struct lineno_list *) NULL)
2168 {
2169 s->lineno_list_head = new_line;
2170 }
2171 else
2172 {
2173 s->lineno_list_tail->next = new_line;
2174 }
c58dbabf 2175 s->lineno_list_tail = new_line;
355afbcd 2176 return LINESZ * s->scnhdr.s_nlnno++;
db40ba14
SC
2177}
2178
355afbcd
KR
2179void
2180c_dot_file_symbol (filename)
2181 char *filename;
db40ba14 2182{
355afbcd 2183 symbolS *symbolP;
c593cf41 2184
355afbcd
KR
2185 symbolP = symbol_new (".file",
2186 SEG_DEBUG,
2187 0,
2188 &zero_address_frag);
c593cf41 2189
355afbcd
KR
2190 S_SET_STORAGE_CLASS (symbolP, C_FILE);
2191 S_SET_NUMBER_AUXILIARY (symbolP, 1);
2192 SA_SET_FILE_FNAME (symbolP, filename);
9ce31b66 2193#ifndef NO_LISTING
c593cf41
SC
2194 {
2195 extern int listing;
355afbcd
KR
2196 if (listing)
2197 {
2198 listing_source_file (filename);
2199 }
2200
c593cf41 2201 }
c593cf41 2202
355afbcd
KR
2203#endif
2204 SF_SET_DEBUG (symbolP);
4f0bccc7 2205 S_SET_VALUE (symbolP, (valueT) previous_file_symbol);
355afbcd
KR
2206
2207 previous_file_symbol = symbolP;
c593cf41 2208
355afbcd
KR
2209 /* Make sure that the symbol is first on the symbol chain */
2210 if (symbol_rootP != symbolP)
2211 {
2212 if (symbolP == symbol_lastP)
2213 {
2214 symbol_lastP = symbol_lastP->sy_previous;
2215 } /* if it was the last thing on the list */
c593cf41 2216
355afbcd
KR
2217 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2218 symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP);
2219 symbol_rootP = symbolP;
2220 } /* if not first on the list */
c593cf41 2221
355afbcd 2222} /* c_dot_file_symbol() */
db40ba14
SC
2223
2224/*
2225 * Build a 'section static' symbol.
2226 */
2227
355afbcd
KR
2228symbolS *
2229c_section_symbol (name, idx)
2230 char *name;
2231 int idx;
db40ba14 2232{
355afbcd 2233 symbolS *symbolP;
c593cf41 2234
355afbcd
KR
2235 symbolP = symbol_new (name, idx,
2236 0,
2237 &zero_address_frag);
c593cf41 2238
355afbcd
KR
2239 S_SET_STORAGE_CLASS (symbolP, C_STAT);
2240 S_SET_NUMBER_AUXILIARY (symbolP, 1);
c593cf41 2241
355afbcd 2242 SF_SET_STATICS (symbolP);
c593cf41 2243
355afbcd
KR
2244 return symbolP;
2245} /* c_section_symbol() */
db40ba14 2246
355afbcd
KR
2247static void
2248DEFUN (w_symbols, (abfd, where, symbol_rootP),
2249 bfd * abfd AND
2250 char *where AND
2251 symbolS * symbol_rootP)
db40ba14 2252{
355afbcd
KR
2253 symbolS *symbolP;
2254 unsigned int i;
2255
2256 /* First fill in those values we have only just worked out */
2257 for (i = SEG_E0; i < SEG_E9; i++)
2258 {
2259 symbolP = segment_info[i].dot;
2260 if (symbolP)
c593cf41 2261 {
355afbcd
KR
2262
2263 SA_SET_SCN_SCNLEN (symbolP, segment_info[i].scnhdr.s_size);
2264 SA_SET_SCN_NRELOC (symbolP, segment_info[i].scnhdr.s_nreloc);
2265 SA_SET_SCN_NLINNO (symbolP, segment_info[i].scnhdr.s_nlnno);
2266
c593cf41
SC
2267 }
2268 }
355afbcd
KR
2269
2270 /*
c593cf41
SC
2271 * Emit all symbols left in the symbol chain.
2272 */
355afbcd
KR
2273 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
2274 {
2275 /* Used to save the offset of the name. It is used to point
c593cf41 2276 to the string in memory but must be a file offset. */
355afbcd 2277 register char *temp;
c593cf41 2278
355afbcd 2279 tc_coff_symbol_emit_hook (symbolP);
c593cf41 2280
355afbcd
KR
2281 temp = S_GET_NAME (symbolP);
2282 if (SF_GET_STRING (symbolP))
2283 {
2284 S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
2285 S_SET_ZEROES (symbolP, 0);
db40ba14 2286 }
355afbcd
KR
2287 else
2288 {
604633ae 2289 memset (symbolP->sy_symbol.ost_entry.n_name, 0, SYMNMLEN);
355afbcd
KR
2290 strncpy (symbolP->sy_symbol.ost_entry.n_name, temp, SYMNMLEN);
2291 }
2292 where = symbol_to_chars (abfd, where, symbolP);
2293 S_SET_NAME (symbolP, temp);
2294 }
2295
db40ba14
SC
2296} /* w_symbols() */
2297
355afbcd 2298static void
604633ae
ILT
2299obj_coff_lcomm (ignore)
2300 int ignore;
db40ba14 2301{
b066f445
SEF
2302 char *name;
2303 char c;
2304 int temp;
2305 char *p;
d752f749 2306
b066f445 2307 symbolS *symbolP;
ec0bcbba
SC
2308 s_lcomm(0);
2309 return;
b066f445 2310 name = input_line_pointer;
c593cf41 2311
355afbcd 2312 c = get_symbol_end ();
b066f445
SEF
2313 p = input_line_pointer;
2314 *p = c;
355afbcd
KR
2315 SKIP_WHITESPACE ();
2316 if (*input_line_pointer != ',')
2317 {
2318 as_bad ("Expected comma after name");
2319 ignore_rest_of_line ();
2320 return;
2321 }
2322 if (*input_line_pointer == '\n')
2323 {
2324 as_bad ("Missing size expression");
2325 return;
2326 }
b066f445 2327 input_line_pointer++;
355afbcd
KR
2328 if ((temp = get_absolute_expression ()) < 0)
2329 {
2330 as_warn ("lcomm length (%d.) <0! Ignored.", temp);
2331 ignore_rest_of_line ();
2332 return;
2333 }
b066f445 2334 *p = 0;
c593cf41 2335
9a75dc1f
ILT
2336 symbolP = symbol_find_or_make(name);
2337
2338 if (S_GET_SEGMENT(symbolP) == SEG_UNKNOWN &&
2339 S_GET_VALUE(symbolP) == 0)
2340 {
2341 if (! need_pass_2)
2342 {
2343 char *p;
2344 segT current_seg = now_seg; /* save current seg */
2345 subsegT current_subseg = now_subseg;
2346
604633ae 2347 subseg_set (SEG_E2, 1);
9a75dc1f
ILT
2348 symbolP->sy_frag = frag_now;
2349 p = frag_var(rs_org, 1, 1, (relax_substateT)0, symbolP,
2350 temp, (char *)0);
2351 *p = 0;
604633ae 2352 subseg_set (current_seg, current_subseg); /* restore current seg */
9a75dc1f
ILT
2353 S_SET_SEGMENT(symbolP, SEG_E2);
2354 S_SET_STORAGE_CLASS(symbolP, C_STAT);
2355 }
2356 }
2357 else
2358 as_bad("Symbol %s already defined", name);
2359
2360 demand_empty_rest_of_line();
db40ba14
SC
2361}
2362
355afbcd 2363static void
9a75dc1f
ILT
2364DEFUN (fixup_mdeps, (frags, h, this_segment),
2365 fragS * frags AND
2366 object_headers * h AND
2367 segT this_segment)
3ad9ec6a 2368{
9a75dc1f 2369 subseg_change (this_segment, 0);
355afbcd 2370 while (frags)
3ad9ec6a 2371 {
355afbcd
KR
2372 switch (frags->fr_type)
2373 {
2374 case rs_align:
2375 case rs_org:
2376 frags->fr_type = rs_fill;
2377 frags->fr_offset =
2378 (frags->fr_next->fr_address - frags->fr_address - frags->fr_fix);
2379 break;
2380 case rs_machine_dependent:
9a75dc1f
ILT
2381 md_convert_frag (h, frags);
2382 frag_wane (frags);
355afbcd
KR
2383 break;
2384 default:
2385 ;
2386 }
2387 frags = frags->fr_next;
3ad9ec6a 2388 }
3ad9ec6a 2389}
355afbcd 2390
db40ba14 2391#if 1
355afbcd 2392static void
2cb0bdc7 2393DEFUN (fixup_segment, (segP, this_segment_type),
016e0d42 2394 segment_info_type * segP AND
355afbcd 2395 segT this_segment_type)
db40ba14 2396{
016e0d42 2397 register fixS * fixP;
355afbcd
KR
2398 register symbolS *add_symbolP;
2399 register symbolS *sub_symbolP;
2400 register long add_number;
2401 register int size;
2402 register char *place;
2403 register long where;
2404 register char pcrel;
2405 register fragS *fragP;
2406 register segT add_symbol_segment = SEG_ABSOLUTE;
2407
2408
016e0d42 2409 for (fixP = segP->fix_root; fixP; fixP = fixP->fx_next)
355afbcd
KR
2410 {
2411 fragP = fixP->fx_frag;
2412 know (fragP);
2413 where = fixP->fx_where;
2414 place = fragP->fr_literal + where;
2415 size = fixP->fx_size;
2416 add_symbolP = fixP->fx_addsy;
db40ba14 2417#ifdef TC_I960
ca16b5e5 2418 if (fixP->fx_tcbit && TC_S_IS_CALLNAME (add_symbolP))
355afbcd 2419 {
98c6bbbe
KR
2420 /* Relocation should be done via the associated 'bal' entry
2421 point symbol. */
c593cf41 2422
355afbcd
KR
2423 if (!TC_S_IS_BALNAME (tc_get_bal_of_call (add_symbolP)))
2424 {
2425 as_bad ("No 'bal' entry point for leafproc %s",
2426 S_GET_NAME (add_symbolP));
2427 continue;
2428 }
2429 fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP);
98c6bbbe 2430 }
db40ba14 2431#endif
355afbcd
KR
2432 sub_symbolP = fixP->fx_subsy;
2433 add_number = fixP->fx_offset;
2434 pcrel = fixP->fx_pcrel;
2435
2436 if (add_symbolP)
2437 {
2438 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2439 } /* if there is an addend */
2440
2441 if (sub_symbolP)
2442 {
2443 if (!add_symbolP)
2444 {
2445 /* Its just -sym */
2446 if (S_GET_SEGMENT (sub_symbolP) != SEG_ABSOLUTE)
2447 {
2448 as_bad ("Negative of non-absolute symbol %s", S_GET_NAME (sub_symbolP));
2449 } /* not absolute */
2450
2451 add_number -= S_GET_VALUE (sub_symbolP);
2452 fixP->fx_subsy = 0;
2453
2454 /* if sub_symbol is in the same segment that add_symbol
c593cf41 2455 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
355afbcd
KR
2456 }
2457 else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment)
2458 && (SEG_NORMAL (add_symbol_segment)
2459 || (add_symbol_segment == SEG_ABSOLUTE)))
2460 {
98c6bbbe
KR
2461 /* Difference of 2 symbols from same segment. Can't
2462 make difference of 2 undefineds: 'value' means
2463 something different for N_UNDF. */
db40ba14 2464#ifdef TC_I960
355afbcd 2465 /* Makes no sense to use the difference of 2 arbitrary symbols
eeeaa778 2466 as the target of a call instruction. */
ca16b5e5 2467 if (fixP->fx_tcbit)
355afbcd
KR
2468 {
2469 as_bad ("callj to difference of 2 symbols");
2470 }
2471#endif /* TC_I960 */
2472 add_number += S_GET_VALUE (add_symbolP) -
2473 S_GET_VALUE (sub_symbolP);
2474
2475 add_symbolP = NULL;
2476 fixP->fx_addsy = NULL;
98c6bbbe 2477 fixP->fx_done = 1;
355afbcd
KR
2478 }
2479 else
2480 {
2481 /* Different segments in subtraction. */
2482 know (!(S_IS_EXTERNAL (sub_symbolP) && (S_GET_SEGMENT (sub_symbolP) == SEG_ABSOLUTE)));
2483
2484 if ((S_GET_SEGMENT (sub_symbolP) == SEG_ABSOLUTE))
2485 {
2486 add_number -= S_GET_VALUE (sub_symbolP);
2487 }
56f61233
KR
2488#ifdef DIFF_EXPR_OK
2489 else if (!pcrel
2490 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2491 {
2492 /* Make it pc-relative. */
2493 add_number += (md_pcrel_from (fixP)
2494 - S_GET_VALUE (sub_symbolP));
2495 pcrel = 1;
2496 fixP->fx_pcrel = 1;
2497 sub_symbolP = 0;
2498 fixP->fx_subsy = 0;
2499 }
2500#endif
355afbcd
KR
2501 else
2502 {
604633ae 2503 as_bad ("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %ld.",
355afbcd 2504 segment_name (S_GET_SEGMENT (sub_symbolP)),
604633ae
ILT
2505 S_GET_NAME (sub_symbolP),
2506 (long) (fragP->fr_address + where));
355afbcd
KR
2507 } /* if absolute */
2508 }
2509 } /* if sub_symbolP */
2510
2511 if (add_symbolP)
2512 {
2513 if (add_symbol_segment == this_segment_type && pcrel)
2514 {
2515 /*
eeeaa778
KR
2516 * This fixup was made when the symbol's segment was
2517 * SEG_UNKNOWN, but it is now in the local segment.
2518 * So we know how to do the address without relocation.
2519 */
db40ba14 2520#ifdef TC_I960
355afbcd 2521 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
eeeaa778
KR
2522 * in which cases it modifies *fixP as appropriate. In the case
2523 * of a 'calls', no further work is required, and *fixP has been
2524 * set up to make the rest of the code below a no-op.
2525 */
355afbcd
KR
2526 reloc_callj (fixP);
2527#endif /* TC_I960 */
2528
2529 add_number += S_GET_VALUE (add_symbolP);
2530 add_number -= md_pcrel_from (fixP);
016e0d42
ILT
2531#ifdef TC_I386
2532 /* On the 386 we must adjust by the segment
2533 vaddr as well. Ian Taylor. */
2534 add_number -= segP->scnhdr.s_vaddr;
2535#endif
355afbcd 2536 pcrel = 0; /* Lie. Don't want further pcrel processing. */
98c6bbbe
KR
2537 fixP->fx_addsy = NULL;
2538 fixP->fx_done = 1;
355afbcd
KR
2539 }
2540 else
2541 {
2542 switch (add_symbol_segment)
2543 {
2544 case SEG_ABSOLUTE:
db40ba14 2545#ifdef TC_I960
355afbcd
KR
2546 reloc_callj (fixP); /* See comment about reloc_callj() above*/
2547#endif /* TC_I960 */
2548 add_number += S_GET_VALUE (add_symbolP);
2549 fixP->fx_addsy = NULL;
98c6bbbe 2550 fixP->fx_done = 1;
355afbcd
KR
2551 add_symbolP = NULL;
2552 break;
2553 default:
2554
61001d96
ILT
2555#ifdef TC_A29K
2556 /* This really should be handled in the linker, but
2557 backward compatibility forbids. */
2558 add_number += S_GET_VALUE (add_symbolP);
2559#else
355afbcd
KR
2560 add_number += S_GET_VALUE (add_symbolP) +
2561 segment_info[S_GET_SEGMENT (add_symbolP)].scnhdr.s_paddr;
61001d96 2562#endif
355afbcd
KR
2563 break;
2564
2565 case SEG_UNKNOWN:
db40ba14 2566#ifdef TC_I960
355afbcd
KR
2567 if ((int) fixP->fx_bit_fixP == 13)
2568 {
2569 /* This is a COBR instruction. They have only a
eeeaa778
KR
2570 * 13-bit displacement and are only to be used
2571 * for local branches: flag as error, don't generate
2572 * relocation.
2573 */
355afbcd 2574 as_bad ("can't use COBR format with external label");
98c6bbbe
KR
2575 fixP->fx_addsy = NULL;
2576 fixP->fx_done = 1;
355afbcd
KR
2577 continue;
2578 } /* COBR */
2579#endif /* TC_I960 */
6142210d 2580#ifdef TC_I386
355afbcd 2581 /* 386 COFF uses a peculiar format in
016e0d42
ILT
2582 which the value of a common symbol is
2583 stored in the .text segment (I've
2584 checked this on SVR3.2 and SCO 3.2.2)
2585 Ian Taylor <ian@cygnus.com>. */
9a75dc1f
ILT
2586 if (S_IS_COMMON (add_symbolP))
2587 add_number += S_GET_VALUE (add_symbolP);
6142210d 2588#endif
355afbcd
KR
2589 break;
2590
2591
2592 } /* switch on symbol seg */
2593 } /* if not in local seg */
2594 } /* if there was a + symbol */
2595
2596 if (pcrel)
2597 {
2368ec63
ILT
2598#ifndef TC_M88K
2599 /* This adjustment is not correct on the m88k, for which the
2600 linker does all the computation. */
355afbcd 2601 add_number -= md_pcrel_from (fixP);
2368ec63 2602#endif
355afbcd
KR
2603 if (add_symbolP == 0)
2604 {
2605 fixP->fx_addsy = &abs_symbol;
2606 } /* if there's an add_symbol */
016e0d42
ILT
2607#ifdef TC_I386
2608 /* On the 386 we must adjust by the segment vaddr
2609 as well. Ian Taylor. */
2610 add_number -= segP->scnhdr.s_vaddr;
2611#endif
355afbcd
KR
2612 } /* if pcrel */
2613
2614 if (!fixP->fx_bit_fixP)
2615 {
8ff6f40e
ILT
2616#ifndef TC_M88K
2617 /* The m88k uses the offset field of the reloc to get around
2618 this problem. */
355afbcd
KR
2619 if ((size == 1 &&
2620 (add_number & ~0xFF) && ((add_number & ~0xFF) != (-1 & ~0xFF))) ||
2621 (size == 2 &&
2622 (add_number & ~0xFFFF) && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF))))
2623 {
604633ae
ILT
2624 as_bad ("Value of %ld too large for field of %d bytes at 0x%lx",
2625 (long) add_number, size,
2626 (unsigned long) (fragP->fr_address + where));
355afbcd 2627 } /* generic error checking */
8ff6f40e 2628#endif
e41474b7 2629#ifdef WARN_SIGNED_OVERFLOW_WORD
355afbcd 2630 /* Warn if a .word value is too large when treated as
eeeaa778
KR
2631 a signed number. We already know it is not too
2632 negative. This is to catch over-large switches
2633 generated by gcc on the 68k. */
355afbcd
KR
2634 if (!flagseen['J']
2635 && size == 2
2636 && add_number > 0x7fff)
604633ae
ILT
2637 as_bad ("Signed .word overflow; switch may be too large; %ld at 0x%lx",
2638 (long) add_number,
2639 (unsigned long) (fragP->fr_address + where));
e41474b7 2640#endif
355afbcd
KR
2641 } /* not a bit fix */
2642 /* once this fix has been applied, we don't have to output anything
c593cf41 2643 nothing more need be done -*/
355afbcd 2644 md_apply_fix (fixP, add_number);
355afbcd 2645 } /* For each fixS in this segment. */
355afbcd 2646} /* fixup_segment() */
c593cf41 2647
355afbcd 2648#endif
8f3956b3
SS
2649
2650/* The first entry in a .stab section is special. */
2651
2652void
2653obj_coff_init_stab_section (seg)
2654 segT seg;
2655{
8efed14f 2656 char *file;
8f3956b3 2657 char *p;
b89b2880 2658 char *stabstr_name;
8f3956b3
SS
2659 unsigned int stroff;
2660
2661 /* Make space for this first symbol. */
2662 p = frag_more (12);
41b5ef0f 2663 /* Zero it out. */
66c774bb 2664 memset (p, 0, 12);
8efed14f 2665 as_where (&file, (unsigned int *) NULL);
b89b2880
JL
2666 stabstr_name = alloca (strlen (segment_info[seg].scnhdr.s_name) + 4);
2667 strcpy (stabstr_name, segment_info[seg].scnhdr.s_name);
2668 strcat (stabstr_name, "str");
2669 stroff = get_stab_string_offset (file, stabstr_name);
8f3956b3
SS
2670 know (stroff == 1);
2671 md_number_to_chars (p, stroff, 4);
2672}
2673
2674/* Fill in the counts in the first entry in a .stab section. */
2675
e0209756 2676static void
8f3956b3
SS
2677adjust_stab_section(abfd, seg)
2678 bfd *abfd;
2679 segT seg;
2680{
2681 segT stabstrseg = -1;
2682 char *secname, *name, *name2;
8f3956b3
SS
2683 char *p = NULL;
2684 int i, strsz = 0, nsyms;
2685 fragS *frag = segment_info[seg].frchainP->frch_root;
2686
2687 /* Look for the associated string table section. */
2688
2689 secname = segment_info[seg].scnhdr.s_name;
2690 name = (char *) alloca (strlen (secname) + 4);
2691 strcpy (name, secname);
2692 strcat (name, "str");
2693
2694 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
2695 {
2696 name2 = segment_info[i].scnhdr.s_name;
2697 if (name2 != NULL && strncmp(name2, name, 8) == 0)
2698 {
2699 stabstrseg = i;
2700 break;
2701 }
2702 }
2703
2704 /* If we found the section, get its size. */
2705 if (stabstrseg >= 0)
2706 strsz = size_section (abfd, stabstrseg);
2707
2708 nsyms = size_section (abfd, seg) / 12 - 1;
2709
2710 /* Look for the first frag of sufficient size for the initial stab
2711 symbol, and collect a pointer to it. */
2712 while (frag && frag->fr_fix < 12)
2713 frag = frag->fr_next;
2714 assert (frag != 0);
2715 p = frag->fr_literal;
2716 assert (p != 0);
2717
2718 /* Write in the number of stab symbols and the size of the string
2719 table. */
2720 bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
2721 bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
2722}
This page took 0.239251 seconds and 4 git commands to generate.