* config/tc-hppa.c (pa_equ): Handle both .reg and .equ correctly.
[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 {
3919ffeb 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
355afbcd 1128#ifndef NO_LISTING
c593cf41 1129 {
355afbcd
KR
1130 extern int listing;
1131 if (listing && 0)
1132 {
4f0bccc7 1133 listing_source_line ((unsigned int) line_base);
355afbcd 1134 }
c593cf41 1135 }
9ce31b66 1136#endif
355afbcd 1137 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
8304c303 1138 SA_SET_SYM_LNNO (def_symbol_in_progress, this_base);
c593cf41 1139
355afbcd 1140 demand_empty_rest_of_line ();
c999fd9f 1141}
db40ba14 1142
355afbcd 1143static void
604633ae
ILT
1144obj_coff_size (ignore)
1145 int ignore;
355afbcd
KR
1146{
1147 if (def_symbol_in_progress == NULL)
1148 {
1149 as_warn (".size pseudo-op used outside of .def/.endef ignored.");
1150 demand_empty_rest_of_line ();
1151 return;
1152 } /* if not inside .def/.endef */
1153
1154 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
1155 SA_SET_SYM_SIZE (def_symbol_in_progress, get_absolute_expression ());
1156 demand_empty_rest_of_line ();
c999fd9f 1157}
355afbcd
KR
1158
1159static void
604633ae
ILT
1160obj_coff_scl (ignore)
1161 int ignore;
355afbcd
KR
1162{
1163 if (def_symbol_in_progress == NULL)
1164 {
1165 as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
1166 demand_empty_rest_of_line ();
1167 return;
1168 } /* if not inside .def/.endef */
1169
1170 S_SET_STORAGE_CLASS (def_symbol_in_progress, get_absolute_expression ());
1171 demand_empty_rest_of_line ();
c999fd9f 1172}
355afbcd
KR
1173
1174static void
604633ae
ILT
1175obj_coff_tag (ignore)
1176 int ignore;
355afbcd
KR
1177{
1178 char *symbol_name;
1179 char name_end;
1180
1181 if (def_symbol_in_progress == NULL)
1182 {
1183 as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
1184 demand_empty_rest_of_line ();
1185 return;
1186 } /* if not inside .def/.endef */
1187
1188 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
1189 symbol_name = input_line_pointer;
1190 name_end = get_symbol_end ();
1191
1192 /* Assume that the symbol referred to by .tag is always defined. */
1193 /* This was a bad assumption. I've added find_or_make. xoxorich. */
1194 SA_SET_SYM_TAGNDX (def_symbol_in_progress, (long) tag_find_or_make (symbol_name));
1195 if (SA_GET_SYM_TAGNDX (def_symbol_in_progress) == 0L)
1196 {
1197 as_warn ("tag not found for .tag %s", symbol_name);
1198 } /* not defined */
1199
1200 SF_SET_TAGGED (def_symbol_in_progress);
1201 *input_line_pointer = name_end;
1202
1203 demand_empty_rest_of_line ();
c999fd9f 1204}
355afbcd
KR
1205
1206static void
604633ae
ILT
1207obj_coff_type (ignore)
1208 int ignore;
355afbcd
KR
1209{
1210 if (def_symbol_in_progress == NULL)
1211 {
1212 as_warn (".type pseudo-op used outside of .def/.endef ignored.");
1213 demand_empty_rest_of_line ();
1214 return;
1215 } /* if not inside .def/.endef */
1216
1217 S_SET_DATA_TYPE (def_symbol_in_progress, get_absolute_expression ());
1218
1219 if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress)) &&
1220 S_GET_STORAGE_CLASS (def_symbol_in_progress) != C_TPDEF)
1221 {
1222 SF_SET_FUNCTION (def_symbol_in_progress);
1223 } /* is a function */
1224
1225 demand_empty_rest_of_line ();
c999fd9f 1226}
355afbcd
KR
1227
1228static void
604633ae
ILT
1229obj_coff_val (ignore)
1230 int ignore;
355afbcd
KR
1231{
1232 if (def_symbol_in_progress == NULL)
1233 {
1234 as_warn (".val pseudo-op used outside of .def/.endef ignored.");
1235 demand_empty_rest_of_line ();
1236 return;
1237 } /* if not inside .def/.endef */
1238
1239 if (is_name_beginner (*input_line_pointer))
1240 {
1241 char *symbol_name = input_line_pointer;
1242 char name_end = get_symbol_end ();
1243
1244 if (!strcmp (symbol_name, "."))
1245 {
1246 def_symbol_in_progress->sy_frag = frag_now;
4f0bccc7 1247 S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ());
355afbcd
KR
1248 /* If the .val is != from the .def (e.g. statics) */
1249 }
1250 else if (strcmp (S_GET_NAME (def_symbol_in_progress), symbol_name))
1251 {
5ac34ac3 1252 def_symbol_in_progress->sy_value.X_op = O_symbol;
5868b1fe
ILT
1253 def_symbol_in_progress->sy_value.X_add_symbol =
1254 symbol_find_or_make (symbol_name);
5ac34ac3 1255 def_symbol_in_progress->sy_value.X_op_symbol = NULL;
5868b1fe 1256 def_symbol_in_progress->sy_value.X_add_number = 0;
5868b1fe
ILT
1257
1258 /* If the segment is undefined when the forward reference is
1259 resolved, then copy the segment id from the forward
1260 symbol. */
355afbcd 1261 SF_SET_GET_SEGMENT (def_symbol_in_progress);
016e0d42
ILT
1262
1263 /* FIXME: gcc can generate address expressions
1264 here in unusual cases (search for "obscure"
1265 in sdbout.c). We just ignore the offset
1266 here, thus generating incorrect debugging
1267 information. We ignore the rest of the
1268 line just below. */
355afbcd 1269 }
016e0d42
ILT
1270 /* Otherwise, it is the name of a non debug symbol and
1271 its value will be calculated later. */
355afbcd 1272 *input_line_pointer = name_end;
016e0d42
ILT
1273
1274 /* FIXME: this is to avoid an error message in the
1275 FIXME case mentioned just above. */
604633ae 1276 while (! is_end_of_line[(unsigned char) *input_line_pointer])
016e0d42 1277 ++input_line_pointer;
355afbcd
KR
1278 }
1279 else
1280 {
4f0bccc7
ILT
1281 S_SET_VALUE (def_symbol_in_progress,
1282 (valueT) get_absolute_expression ());
355afbcd 1283 } /* if symbol based */
c593cf41 1284
355afbcd 1285 demand_empty_rest_of_line ();
c999fd9f 1286}
db40ba14
SC
1287
1288/*
1289 * Maintain a list of the tagnames of the structres.
1290 */
1291
355afbcd
KR
1292static void
1293tag_init ()
1294{
1295 tag_hash = hash_new ();
c999fd9f 1296}
db40ba14 1297
355afbcd
KR
1298static void
1299tag_insert (name, symbolP)
1300 char *name;
1301 symbolS *symbolP;
db40ba14 1302{
604633ae 1303 register const char *error_string;
c593cf41 1304
43029a8f 1305 if ((error_string = hash_jam (tag_hash, name, (char *) symbolP)))
355afbcd
KR
1306 {
1307 as_fatal ("Inserting \"%s\" into structure table failed: %s",
1308 name, error_string);
1309 }
c999fd9f 1310}
db40ba14 1311
355afbcd
KR
1312static symbolS *
1313tag_find_or_make (name)
1314 char *name;
db40ba14 1315{
355afbcd 1316 symbolS *symbolP;
c593cf41 1317
355afbcd
KR
1318 if ((symbolP = tag_find (name)) == NULL)
1319 {
1320 symbolP = symbol_new (name,
1321 SEG_UNKNOWN,
1322 0,
1323 &zero_address_frag);
c593cf41 1324
355afbcd 1325 tag_insert (S_GET_NAME (symbolP), symbolP);
355afbcd 1326 } /* not found */
c593cf41 1327
355afbcd
KR
1328 return (symbolP);
1329} /* tag_find_or_make() */
db40ba14 1330
355afbcd
KR
1331static symbolS *
1332tag_find (name)
1333 char *name;
db40ba14
SC
1334{
1335#ifdef STRIP_UNDERSCORE
355afbcd
KR
1336 if (*name == '_')
1337 name++;
db40ba14 1338#endif /* STRIP_UNDERSCORE */
355afbcd
KR
1339 return ((symbolS *) hash_find (tag_hash, name));
1340} /* tag_find() */
db40ba14 1341
355afbcd
KR
1342void
1343obj_read_begin_hook ()
1344{
1345 /* These had better be the same. Usually 18 bytes. */
db40ba14 1346#ifndef BFD_HEADERS
355afbcd
KR
1347 know (sizeof (SYMENT) == sizeof (AUXENT));
1348 know (SYMESZ == AUXESZ);
db40ba14 1349#endif
355afbcd 1350 tag_init ();
c999fd9f 1351}
db40ba14
SC
1352
1353/* This function runs through the symbol table and puts all the
1354 externals onto another chain */
1355
1356/* The chain of externals */
1357symbolS *symbol_externP = NULL;
1358symbolS *symbol_extern_lastP = NULL;
1359
355afbcd
KR
1360stack *block_stack;
1361symbolS *last_functionP = NULL;
1362symbolS *last_tagP;
db40ba14 1363
355afbcd
KR
1364static unsigned int
1365DEFUN_VOID (yank_symbols)
db40ba14 1366{
c593cf41 1367 symbolS *symbolP;
355afbcd 1368 unsigned int symbol_number = 0;
c0f1bbb6 1369 unsigned int last_file_symno = 0;
355afbcd 1370
c593cf41
SC
1371 for (symbolP = symbol_rootP;
1372 symbolP;
355afbcd
KR
1373 symbolP = symbolP ? symbol_next (symbolP) : symbol_rootP)
1374 {
1375 if (!SF_GET_DEBUG (symbolP))
1376 {
c593cf41 1377 /* Debug symbols do not need all this rubbish */
355afbcd 1378 symbolS *real_symbolP;
c593cf41
SC
1379
1380 /* L* and C_EFCN symbols never merge. */
355afbcd 1381 if (!SF_GET_LOCAL (symbolP)
016e0d42 1382 && S_GET_STORAGE_CLASS (symbolP) != C_LABEL
5ac34ac3 1383 && symbolP->sy_value.X_op == O_constant
355afbcd
KR
1384 && (real_symbolP = symbol_find_base (S_GET_NAME (symbolP), DO_NOT_STRIP))
1385 && real_symbolP != symbolP)
1386 {
c593cf41
SC
1387 /* FIXME-SOON: where do dups come from?
1388 Maybe tag references before definitions? xoxorich. */
1389 /* Move the debug data from the debug symbol to the
1390 real symbol. Do NOT do the oposite (i.e. move from
1391 real symbol to debug symbol and remove real symbol from the
1392 list.) Because some pointers refer to the real symbol
1393 whereas no pointers refer to the debug symbol. */
355afbcd 1394 c_symbol_merge (symbolP, real_symbolP);
c593cf41
SC
1395 /* Replace the current symbol by the real one */
1396 /* The symbols will never be the last or the first
1397 because : 1st symbol is .file and 3 last symbols are
1398 .text, .data, .bss */
355afbcd
KR
1399 symbol_remove (real_symbolP, &symbol_rootP, &symbol_lastP);
1400 symbol_insert (real_symbolP, symbolP, &symbol_rootP, &symbol_lastP);
1401 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
c593cf41
SC
1402 symbolP = real_symbolP;
1403 } /* if not local but dup'd */
1404
355afbcd
KR
1405 if (flagseen['R'] && (S_GET_SEGMENT (symbolP) == SEG_E1))
1406 {
1407 S_SET_SEGMENT (symbolP, SEG_E0);
c593cf41
SC
1408 } /* push data into text */
1409
5868b1fe 1410 resolve_symbol_value (symbolP);
c593cf41 1411
e0209756 1412 if (S_GET_STORAGE_CLASS (symbolP) == C_NULL)
c593cf41 1413 {
e0209756
ILT
1414 if (!S_IS_DEFINED (symbolP) && !SF_GET_LOCAL (symbolP))
1415 {
1416 S_SET_EXTERNAL (symbolP);
1417 }
1418 else if (S_GET_SEGMENT (symbolP) == SEG_E0)
355afbcd
KR
1419 {
1420 S_SET_STORAGE_CLASS (symbolP, C_LABEL);
1421 }
1422 else
1423 {
1424 S_SET_STORAGE_CLASS (symbolP, C_STAT);
1425 }
c593cf41 1426 }
c593cf41
SC
1427
1428 /* Mainly to speed up if not -g */
355afbcd
KR
1429 if (SF_GET_PROCESS (symbolP))
1430 {
1431 /* Handle the nested blocks auxiliary info. */
1432 if (S_GET_STORAGE_CLASS (symbolP) == C_BLOCK)
1433 {
1434 if (!strcmp (S_GET_NAME (symbolP), ".bb"))
1435 stack_push (block_stack, (char *) &symbolP);
1436 else
1437 { /* .eb */
1438 register symbolS *begin_symbolP;
1439 begin_symbolP = *(symbolS **) stack_pop (block_stack);
1440 if (begin_symbolP == (symbolS *) 0)
1441 as_warn ("mismatched .eb");
1442 else
1443 SA_SET_SYM_ENDNDX (begin_symbolP, symbol_number + 2);
1444 }
1445 }
1446 /* If we are able to identify the type of a function, and we
c593cf41
SC
1447 are out of a function (last_functionP == 0) then, the
1448 function symbol will be associated with an auxiliary
1449 entry. */
355afbcd
KR
1450 if (last_functionP == (symbolS *) 0 &&
1451 SF_GET_FUNCTION (symbolP))
1452 {
1453 last_functionP = symbolP;
c593cf41 1454
355afbcd
KR
1455 if (S_GET_NUMBER_AUXILIARY (symbolP) < 1)
1456 {
1457 S_SET_NUMBER_AUXILIARY (symbolP, 1);
1458 } /* make it at least 1 */
c593cf41 1459
355afbcd 1460 /* Clobber possible stale .dim information. */
c593cf41 1461#if 0
355afbcd
KR
1462 /* Iffed out by steve - this fries the lnnoptr info too */
1463 bzero (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen,
1464 sizeof (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen));
c593cf41 1465#endif
355afbcd 1466 }
c0f1bbb6
KR
1467 /* The C_FCN doesn't need any additional information. I
1468 don't even know if this is needed for sdb. But the
1469 standard assembler generates it, so... */
355afbcd
KR
1470 if (S_GET_STORAGE_CLASS (symbolP) == C_EFCN)
1471 {
1472 if (last_functionP == (symbolS *) 0)
1473 as_fatal ("C_EFCN symbol out of scope");
1474 SA_SET_SYM_FSIZE (last_functionP,
1475 (long) (S_GET_VALUE (symbolP) -
1476 S_GET_VALUE (last_functionP)));
1477 SA_SET_SYM_ENDNDX (last_functionP, symbol_number);
ec0bcbba 1478 last_functionP = (symbolS *) 0;
355afbcd
KR
1479 }
1480 }
1481 }
1482 else if (SF_GET_TAG (symbolP))
1483 {
1484 /* First descriptor of a structure must point to
c593cf41 1485 the first slot after the structure description. */
355afbcd
KR
1486 last_tagP = symbolP;
1487
1488 }
1489 else if (S_GET_STORAGE_CLASS (symbolP) == C_EOS)
1490 {
1491 /* +2 take in account the current symbol */
1492 SA_SET_SYM_ENDNDX (last_tagP, symbol_number + 2);
1493 }
1494 else if (S_GET_STORAGE_CLASS (symbolP) == C_FILE)
1495 {
1496 if (S_GET_VALUE (symbolP))
1497 {
c0f1bbb6
KR
1498 S_SET_VALUE (symbolP, last_file_symno);
1499 last_file_symno = symbol_number;
355afbcd
KR
1500 } /* no one points at the first .file symbol */
1501 } /* if debug or tag or eos or file */
c593cf41
SC
1502
1503 /* We must put the external symbols apart. The loader
1504 does not bomb if we do not. But the references in
1505 the endndx field for a .bb symbol are not corrected
1506 if an external symbol is removed between .bb and .be.
1507 I.e in the following case :
1508 [20] .bb endndx = 22
1509 [21] foo external
1510 [22] .be
1511 ld will move the symbol 21 to the end of the list but
1512 endndx will still be 22 instead of 21. */
1513
1514
355afbcd
KR
1515 if (SF_GET_LOCAL (symbolP))
1516 {
c593cf41
SC
1517 /* remove C_EFCN and LOCAL (L...) symbols */
1518 /* next pointer remains valid */
355afbcd 1519 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
c593cf41
SC
1520
1521 }
355afbcd
KR
1522 else if (!S_IS_DEFINED (symbolP)
1523 && !S_IS_DEBUG (symbolP)
1524 && !SF_GET_STATICS (symbolP) &&
1525 S_GET_STORAGE_CLASS (symbolP) == C_EXT)
1526 { /* C_EXT && !SF_GET_FUNCTION(symbolP)) */
1527 /* if external, Remove from the list */
1528 symbolS *hold = symbol_previous (symbolP);
1529
1530 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
1531 symbol_clear_list_pointers (symbolP);
1532 symbol_append (symbolP, symbol_extern_lastP, &symbol_externP, &symbol_extern_lastP);
1533 symbolP = hold;
1534 }
1535 else
1536 {
1537 if (SF_GET_STRING (symbolP))
1538 {
1539 symbolP->sy_name_offset = string_byte_count;
1540 string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
1541 }
1542 else
1543 {
1544 symbolP->sy_name_offset = 0;
1545 } /* fix "long" names */
1546
1547 symbolP->sy_number = symbol_number;
1548 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
1549 } /* if local symbol */
c593cf41
SC
1550 } /* traverse the symbol list */
1551 return symbol_number;
355afbcd 1552
db40ba14
SC
1553}
1554
1555
355afbcd
KR
1556static unsigned int
1557DEFUN_VOID (glue_symbols)
db40ba14 1558{
c593cf41 1559 unsigned int symbol_number = 0;
355afbcd
KR
1560 symbolS *symbolP;
1561 for (symbolP = symbol_externP; symbol_externP;)
1562 {
c593cf41
SC
1563 symbolS *tmp = symbol_externP;
1564
1565 /* append */
355afbcd
KR
1566 symbol_remove (tmp, &symbol_externP, &symbol_extern_lastP);
1567 symbol_append (tmp, symbol_lastP, &symbol_rootP, &symbol_lastP);
c593cf41
SC
1568
1569 /* and process */
355afbcd
KR
1570 if (SF_GET_STRING (tmp))
1571 {
c593cf41 1572 tmp->sy_name_offset = string_byte_count;
355afbcd
KR
1573 string_byte_count += strlen (S_GET_NAME (tmp)) + 1;
1574 }
1575 else
1576 {
1577 tmp->sy_name_offset = 0;
1578 } /* fix "long" names */
c593cf41
SC
1579
1580 tmp->sy_number = symbol_number;
355afbcd 1581 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (tmp);
c593cf41
SC
1582 } /* append the entire extern chain */
1583 return symbol_number;
355afbcd 1584
db40ba14
SC
1585}
1586
355afbcd
KR
1587static unsigned int
1588DEFUN_VOID (tie_tags)
db40ba14 1589{
c593cf41 1590 unsigned int symbol_number = 0;
355afbcd
KR
1591
1592 symbolS *symbolP;
c593cf41 1593 for (symbolP = symbol_rootP; symbolP; symbolP =
355afbcd
KR
1594 symbol_next (symbolP))
1595 {
1596 symbolP->sy_number = symbol_number;
c593cf41
SC
1597
1598
1599
355afbcd
KR
1600 if (SF_GET_TAGGED (symbolP))
1601 {
1602 SA_SET_SYM_TAGNDX
1603 (symbolP,
1604 ((symbolS *) SA_GET_SYM_TAGNDX (symbolP))->sy_number);
1605 }
c593cf41 1606
355afbcd
KR
1607 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
1608 }
c593cf41 1609 return symbol_number;
355afbcd 1610
db40ba14
SC
1611}
1612
355afbcd 1613static void
9a75dc1f
ILT
1614DEFUN (crawl_symbols, (h, abfd),
1615 object_headers *h AND
355afbcd 1616 bfd * abfd)
db40ba14 1617{
355afbcd 1618 unsigned int i;
c593cf41
SC
1619
1620 /* Initialize the stack used to keep track of the matching .bb .be */
1621
355afbcd 1622 block_stack = stack_init (512, sizeof (symbolS *));
c593cf41
SC
1623
1624 /* The symbol list should be ordered according to the following sequence
1625 * order :
1626 * . .file symbol
1627 * . debug entries for functions
1628 * . fake symbols for the sections, including.text .data and .bss
1629 * . defined symbols
1630 * . undefined symbols
1631 * But this is not mandatory. The only important point is to put the
1632 * undefined symbols at the end of the list.
1633 */
1634
1635 if (symbol_rootP == NULL
355afbcd
KR
1636 || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE)
1637 {
1638 c_dot_file_symbol ("fake");
c593cf41
SC
1639 }
1640 /* Is there a .file symbol ? If not insert one at the beginning. */
1641
1642 /*
1643 * Build up static symbols for the sections, they are filled in later
1644 */
1645
1646
355afbcd 1647 for (i = SEG_E0; i < SEG_E9; i++)
c593cf41 1648 {
355afbcd
KR
1649 if (segment_info[i].scnhdr.s_name[0])
1650 {
9a75dc1f 1651 char name[9];
355afbcd 1652
9a75dc1f
ILT
1653 strncpy (name, segment_info[i].scnhdr.s_name, 8);
1654 name[8] = '\0';
1655 segment_info[i].dot = c_section_symbol (name, i - SEG_E0 + 1);
355afbcd 1656 }
c593cf41 1657 }
c593cf41
SC
1658
1659
1660 /* Take all the externals out and put them into another chain */
9a75dc1f 1661 H_SET_SYMBOL_TABLE_SIZE (h, yank_symbols ());
c593cf41 1662 /* Take the externals and glue them onto the end.*/
9a75dc1f 1663 H_SET_SYMBOL_TABLE_SIZE (h, H_GET_SYMBOL_COUNT (h) + glue_symbols ());
c593cf41 1664
9a75dc1f 1665 H_SET_SYMBOL_TABLE_SIZE (h, tie_tags ());
355afbcd
KR
1666 know (symbol_externP == NULL);
1667 know (symbol_extern_lastP == NULL);
db40ba14
SC
1668}
1669
1670/*
1671 * Find strings by crawling along symbol table chain.
1672 */
1673
355afbcd
KR
1674void
1675DEFUN (w_strings, (where),
1676 char *where)
db40ba14 1677{
c593cf41
SC
1678 symbolS *symbolP;
1679
1680 /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
4f0bccc7 1681 md_number_to_chars (where, (valueT) string_byte_count, 4);
ad86fa70 1682 where += 4;
c593cf41
SC
1683 for (symbolP = symbol_rootP;
1684 symbolP;
355afbcd
KR
1685 symbolP = symbol_next (symbolP))
1686 {
1687 unsigned int size;
1688
1689 if (SF_GET_STRING (symbolP))
1690 {
1691 size = strlen (S_GET_NAME (symbolP)) + 1;
1692
1693 memcpy (where, S_GET_NAME (symbolP), size);
1694 where += size;
1695
1696 }
1697 }
db40ba14
SC
1698}
1699
355afbcd 1700static void
9a75dc1f 1701DEFUN (do_linenos_for, (abfd, h, file_cursor),
355afbcd 1702 bfd * abfd AND
9a75dc1f 1703 object_headers * h AND
355afbcd 1704 unsigned long *file_cursor)
db40ba14 1705{
c593cf41 1706 unsigned int idx;
9a75dc1f 1707 unsigned long start = *file_cursor;
c593cf41 1708
355afbcd 1709 for (idx = SEG_E0; idx < SEG_E9; idx++)
c593cf41 1710 {
355afbcd 1711 segment_info_type *s = segment_info + idx;
c593cf41 1712
c593cf41 1713
355afbcd
KR
1714 if (s->scnhdr.s_nlnno != 0)
1715 {
1716 struct lineno_list *line_ptr;
1717
1718 struct external_lineno *buffer =
1719 (struct external_lineno *) xmalloc (s->scnhdr.s_nlnno * LINESZ);
c593cf41 1720
355afbcd
KR
1721 struct external_lineno *dst = buffer;
1722
1723 /* Run through the table we've built and turn it into its external
c593cf41
SC
1724 form, take this chance to remove duplicates */
1725
355afbcd
KR
1726 for (line_ptr = s->lineno_list_head;
1727 line_ptr != (struct lineno_list *) NULL;
1728 line_ptr = line_ptr->next)
1729 {
c593cf41 1730
355afbcd
KR
1731 if (line_ptr->line.l_lnno == 0)
1732 {
1733 /* Turn a pointer to a symbol into the symbols' index */
1734 line_ptr->line.l_addr.l_symndx =
1735 ((symbolS *) line_ptr->line.l_addr.l_symndx)->sy_number;
1736 }
1737 else
1738 {
1739 line_ptr->line.l_addr.l_paddr += ((struct frag *) (line_ptr->frag))->fr_address;
1740 }
c593cf41 1741
c593cf41 1742
355afbcd
KR
1743 (void) bfd_coff_swap_lineno_out (abfd, &(line_ptr->line), dst);
1744 dst++;
1745
1746 }
1747
1748 s->scnhdr.s_lnnoptr = *file_cursor;
1749
1750 bfd_write (buffer, 1, s->scnhdr.s_nlnno * LINESZ, abfd);
1751 free (buffer);
1752
1753 *file_cursor += s->scnhdr.s_nlnno * LINESZ;
1754 }
c593cf41 1755 }
9a75dc1f 1756 H_SET_LINENO_SIZE (h, *file_cursor - start);
db40ba14
SC
1757}
1758
1759
1760/* Now we run through the list of frag chains in a segment and
1761 make all the subsegment frags appear at the end of the
1762 list, as if the seg 0 was extra long */
1763
355afbcd
KR
1764static void
1765DEFUN_VOID (remove_subsegs)
db40ba14 1766{
355afbcd
KR
1767 unsigned int i;
1768
1769 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1770 {
1771 frchainS *head = segment_info[i].frchainP;
1772 fragS dummy;
1773 fragS *prev_frag = &dummy;
1774
1775 while (head && head->frch_seg == i)
c593cf41 1776 {
355afbcd
KR
1777 prev_frag->fr_next = head->frch_root;
1778 prev_frag = head->frch_last;
1779 head = head->frch_next;
c593cf41 1780 }
355afbcd 1781 prev_frag->fr_next = 0;
c593cf41 1782 }
db40ba14
SC
1783}
1784
4f0bccc7 1785unsigned long machine;
163107a1 1786int coff_flags;
355afbcd
KR
1787extern void
1788DEFUN_VOID (write_object_file)
db40ba14 1789{
355afbcd 1790 int i;
8f3956b3 1791 char *name;
355afbcd 1792 struct frchain *frchain_ptr;
c593cf41 1793
9a75dc1f 1794 object_headers headers;
355afbcd
KR
1795 unsigned long file_cursor;
1796 bfd *abfd;
1797 unsigned int addr;
1798 abfd = bfd_openw (out_file_name, TARGET_FORMAT);
c593cf41
SC
1799
1800
355afbcd
KR
1801 if (abfd == 0)
1802 {
1803 as_perror ("FATAL: Can't create %s", out_file_name);
1804 exit (42);
1805 }
1806 bfd_set_format (abfd, bfd_object);
1807 bfd_set_arch_mach (abfd, BFD_ARCH, machine);
c593cf41 1808
355afbcd
KR
1809 string_byte_count = 4;
1810
1811 for (frchain_ptr = frchain_root;
1812 frchain_ptr != (struct frchain *) NULL;
1813 frchain_ptr = frchain_ptr->frch_next)
1814 {
1815 /* Run through all the sub-segments and align them up. Also close any
c593cf41
SC
1816 open frags. We tack a .fill onto the end of the frag chain so
1817 that any .align's size can be worked by looking at the next
1818 frag */
1819
604633ae 1820 subseg_set (frchain_ptr->frch_seg, frchain_ptr->frch_subseg);
016e0d42 1821#ifndef SUB_SEGMENT_ALIGN
2492e118 1822#define SUB_SEGMENT_ALIGN(SEG) 1
016e0d42 1823#endif
2492e118 1824 frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE);
355afbcd
KR
1825 frag_wane (frag_now);
1826 frag_now->fr_fix = 0;
1827 know (frag_now->fr_next == NULL);
1828 }
c593cf41
SC
1829
1830
355afbcd 1831 remove_subsegs ();
c593cf41 1832
355afbcd
KR
1833
1834 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
c593cf41 1835 {
355afbcd 1836 relax_segment (segment_info[i].frchainP->frch_root, i);
c593cf41 1837 }
c593cf41 1838
9a75dc1f 1839 H_SET_NUMBER_OF_SECTIONS (&headers, 0);
355afbcd
KR
1840
1841 /* Find out how big the sections are, and set the addresses. */
1842 addr = 0;
1843 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1844 {
9a75dc1f
ILT
1845 long size;
1846
355afbcd 1847 segment_info[i].scnhdr.s_paddr = addr;
e63164f9 1848 segment_info[i].scnhdr.s_vaddr = addr;
c593cf41 1849
355afbcd 1850 if (segment_info[i].scnhdr.s_name[0])
a39116f1 1851 {
9a75dc1f
ILT
1852 H_SET_NUMBER_OF_SECTIONS (&headers,
1853 H_GET_NUMBER_OF_SECTIONS (&headers) + 1);
a39116f1 1854 }
355afbcd 1855
4f0bccc7 1856 size = size_section (abfd, (unsigned int) i);
9a75dc1f 1857 addr += size;
016e0d42 1858
9a75dc1f
ILT
1859 if (i == SEG_E0)
1860 H_SET_TEXT_SIZE (&headers, size);
1861 else if (i == SEG_E1)
1862 H_SET_DATA_SIZE (&headers, size);
1863 else if (i == SEG_E2)
1864 H_SET_BSS_SIZE (&headers, size);
355afbcd 1865 }
c593cf41 1866
9a75dc1f
ILT
1867 /* Turn the gas native symbol table shape into a coff symbol table */
1868 crawl_symbols (&headers, abfd);
1869
1870 if (string_byte_count == 4)
1871 string_byte_count = 0;
c593cf41 1872
9a75dc1f 1873 H_SET_STRING_SIZE (&headers, string_byte_count);
c593cf41 1874
033400ec 1875#if !defined(TC_H8300) && !defined(TC_Z8K)
355afbcd 1876 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
c593cf41 1877 {
9a75dc1f 1878 fixup_mdeps (segment_info[i].frchainP->frch_root, &headers, i);
016e0d42 1879 fixup_segment (&segment_info[i], i);
c593cf41
SC
1880 }
1881#endif
1882
8f3956b3
SS
1883 /* Look for ".stab" segments and fill in their initial symbols
1884 correctly. */
1885 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
1886 {
1887 name = segment_info[i].scnhdr.s_name;
1888
1889 if (name != NULL
1890 && strncmp (".stab", name, 5) == 0
1891 && strncmp (".stabstr", name, 8) != 0)
1892 adjust_stab_section (abfd, i);
1893 }
1894
9a75dc1f 1895 file_cursor = H_GET_TEXT_FILE_OFFSET (&headers);
355afbcd 1896
4f0bccc7 1897 bfd_seek (abfd, (file_ptr) file_cursor, 0);
c593cf41 1898
355afbcd 1899 /* Plant the data */
c593cf41 1900
9a75dc1f 1901 fill_section (abfd, &headers, &file_cursor);
c593cf41 1902
9a75dc1f 1903 do_relocs_for (abfd, &headers, &file_cursor);
c593cf41 1904
9a75dc1f 1905 do_linenos_for (abfd, &headers, &file_cursor);
c593cf41 1906
9a75dc1f
ILT
1907 H_SET_FILE_MAGIC_NUMBER (&headers, COFF_MAGIC);
1908#ifndef OBJ_COFF_OMIT_TIMESTAMP
1909 H_SET_TIME_STAMP (&headers, (long)time((long*)0));
1910#else
1911 H_SET_TIME_STAMP (&headers, 0);
1912#endif
c593cf41 1913
9a75dc1f
ILT
1914#ifdef KEEP_RELOC_INFO
1915 H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) |
1916 COFF_FLAGS | coff_flags));
1917#else
1918 H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) |
1919 (H_GET_RELOCATION_SIZE(&headers) ? 0 : F_RELFLG) |
1920 COFF_FLAGS | coff_flags));
1921#endif
c593cf41
SC
1922
1923 {
9a75dc1f
ILT
1924 unsigned int symtable_size = H_GET_SYMBOL_TABLE_SIZE (&headers);
1925 char *buffer1 = xmalloc (symtable_size + string_byte_count + 1);
604633ae 1926
9a75dc1f 1927 H_SET_SYMBOL_TABLE_POINTER (&headers, bfd_tell (abfd));
355afbcd 1928 w_symbols (abfd, buffer1, symbol_rootP);
9a75dc1f
ILT
1929 if (string_byte_count > 0)
1930 w_strings (buffer1 + symtable_size);
1931 bfd_write (buffer1, 1, symtable_size + string_byte_count, abfd);
355afbcd 1932 free (buffer1);
c593cf41 1933 }
9a75dc1f
ILT
1934
1935 coff_header_append (abfd, &headers);
03a372ed
SC
1936#if 0
1937 /* Recent changes to write need this, but where it should
1938 go is up to Ken.. */
355afbcd
KR
1939 if (bfd_close_all_done (abfd) == false)
1940 as_fatal ("Can't close %s: %s", out_file_name,
380fa957 1941 bfd_errmsg (bfd_get_error ()));
03a372ed
SC
1942#else
1943 {
1944 extern bfd *stdoutput;
1945 stdoutput = abfd;
1946 }
1947#endif
1948
db40ba14
SC
1949}
1950
604633ae
ILT
1951/* Add a new segment. This is called from subseg_new via the
1952 obj_new_segment macro. */
db40ba14 1953
604633ae
ILT
1954segT
1955obj_coff_add_segment (name)
1956 const char *name;
db40ba14 1957{
604633ae 1958 unsigned int len;
355afbcd 1959 unsigned int i;
604633ae
ILT
1960
1961 /* Find out if we've already got a section of this name. */
1962 len = strlen (name);
1963 if (len < sizeof (segment_info[i].scnhdr.s_name))
1964 ++len;
1965 else
1966 len = sizeof (segment_info[i].scnhdr.s_name);
355afbcd 1967 for (i = SEG_E0; i < SEG_E9 && segment_info[i].scnhdr.s_name[0]; i++)
604633ae
ILT
1968 if (strncmp (segment_info[i].scnhdr.s_name, name, len) == 0
1969 && (len == sizeof (segment_info[i].scnhdr.s_name)
1970 || segment_info[i].scnhdr.s_name[len] == '\0'))
1971 return (segT) i;
1972
1973 if (i == SEG_E9)
c593cf41 1974 {
604633ae
ILT
1975 as_bad ("Too many new sections; can't add \"%s\"", name);
1976 return now_seg;
c593cf41 1977 }
604633ae
ILT
1978
1979 /* Add a new section. */
1980 strncpy (segment_info[i].scnhdr.s_name, name,
1981 sizeof (segment_info[i].scnhdr.s_name));
b27caf27 1982 segment_info[i].scnhdr.s_flags = STYP_REG;
604633ae
ILT
1983
1984 return (segT) i;
db40ba14
SC
1985}
1986
9a75dc1f
ILT
1987/*
1988 * implement the .section pseudo op:
1989 * .section name {, "flags"}
1990 * ^ ^
1991 * | +--- optional flags: 'b' for bss
1992 * | 'i' for info
1993 * +-- section name 'l' for lib
1994 * 'n' for noload
1995 * 'o' for over
1996 * 'w' for data
56607c4e 1997 * 'd' (apparently m88k for data)
9a75dc1f
ILT
1998 * 'x' for text
1999 * But if the argument is not a quoted string, treat it as a
2000 * subsegment number.
2001 */
2002
355afbcd 2003void
604633ae
ILT
2004obj_coff_section (ignore)
2005 int ignore;
db40ba14 2006{
355afbcd
KR
2007 /* Strip out the section name */
2008 char *section_name;
2009 char *section_name_end;
2010 char c;
9a75dc1f 2011 int argp;
355afbcd
KR
2012 unsigned int len;
2013 unsigned int exp;
9a75dc1f 2014 long flags;
355afbcd
KR
2015
2016 section_name = input_line_pointer;
2017 c = get_symbol_end ();
2018 section_name_end = input_line_pointer;
2019
2020 len = section_name_end - section_name;
2021 input_line_pointer++;
2022 SKIP_WHITESPACE ();
016e0d42 2023
9a75dc1f
ILT
2024 argp = 0;
2025 if (c == ',')
2026 argp = 1;
2027 else if (*input_line_pointer == ',')
355afbcd 2028 {
9a75dc1f
ILT
2029 argp = 1;
2030 ++input_line_pointer;
2031 SKIP_WHITESPACE ();
c593cf41 2032 }
9a75dc1f
ILT
2033
2034 exp = 0;
2035 flags = 0;
2036 if (argp)
c593cf41 2037 {
9a75dc1f
ILT
2038 if (*input_line_pointer != '"')
2039 exp = get_absolute_expression ();
2040 else
2041 {
2042 ++input_line_pointer;
2043 while (*input_line_pointer != '"'
604633ae 2044 && ! is_end_of_line[(unsigned char) *input_line_pointer])
9a75dc1f
ILT
2045 {
2046 switch (*input_line_pointer)
2047 {
2048 case 'b': flags |= STYP_BSS; break;
2049 case 'i': flags |= STYP_INFO; break;
2050 case 'l': flags |= STYP_LIB; break;
2051 case 'n': flags |= STYP_NOLOAD; break;
2052 case 'o': flags |= STYP_OVER; break;
56607c4e 2053 case 'd':
9a75dc1f
ILT
2054 case 'w': flags |= STYP_DATA; break;
2055 case 'x': flags |= STYP_TEXT; break;
2056 default:
2057 as_warn("unknown section attribute '%c'",
2058 *input_line_pointer);
2059 break;
2060 }
2061 ++input_line_pointer;
2062 }
2063 if (*input_line_pointer == '"')
2064 ++input_line_pointer;
2065 }
c593cf41 2066 }
355afbcd 2067
4f0bccc7 2068 subseg_new (section_name, (subsegT) exp);
9a75dc1f
ILT
2069
2070 segment_info[now_seg].scnhdr.s_flags |= flags;
2071
355afbcd 2072 *section_name_end = c;
db40ba14
SC
2073}
2074
2075
355afbcd 2076static void
604633ae
ILT
2077obj_coff_text (ignore)
2078 int ignore;
db40ba14 2079{
604633ae 2080 subseg_new (".text", get_absolute_expression ());
db40ba14
SC
2081}
2082
2083
355afbcd 2084static void
604633ae
ILT
2085obj_coff_data (ignore)
2086 int ignore;
db40ba14 2087{
ffffc8fb 2088 if (flagseen['R'])
604633ae 2089 subseg_new (".text", get_absolute_expression () + 1000);
ffffc8fb 2090 else
604633ae 2091 subseg_new (".data", get_absolute_expression ());
db40ba14
SC
2092}
2093
9a75dc1f 2094static void
604633ae
ILT
2095obj_coff_bss (ignore)
2096 int ignore;
9a75dc1f
ILT
2097{
2098 if (*input_line_pointer == '\n') /* .bss */
604633ae 2099 subseg_new(".bss", get_absolute_expression());
9a75dc1f 2100 else /* .bss id,expr */
604633ae 2101 obj_coff_lcomm(0);
9a75dc1f
ILT
2102}
2103
2104static void
604633ae
ILT
2105obj_coff_ident (ignore)
2106 int ignore;
9a75dc1f
ILT
2107{
2108 segT current_seg = now_seg; /* save current seg */
2109 subsegT current_subseg = now_subseg;
604633ae 2110 subseg_new (".comment", 0); /* .comment seg */
9a75dc1f 2111 stringer (1); /* read string */
604633ae 2112 subseg_set (current_seg, current_subseg); /* restore current seg */
9a75dc1f
ILT
2113}
2114
355afbcd
KR
2115void
2116c_symbol_merge (debug, normal)
2117 symbolS *debug;
2118 symbolS *normal;
db40ba14 2119{
355afbcd
KR
2120 S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug));
2121 S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug));
c593cf41 2122
355afbcd
KR
2123 if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal))
2124 {
2125 S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug));
2126 } /* take the most we have */
c593cf41 2127
355afbcd
KR
2128 if (S_GET_NUMBER_AUXILIARY (debug) > 0)
2129 {
4f0bccc7
ILT
2130 memcpy ((char *) &normal->sy_symbol.ost_auxent[0],
2131 (char *) &debug->sy_symbol.ost_auxent[0],
2132 (unsigned int) (S_GET_NUMBER_AUXILIARY (debug) * AUXESZ));
355afbcd 2133 } /* Move all the auxiliary information */
c593cf41 2134
355afbcd
KR
2135 /* Move the debug flags. */
2136 SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug));
2137} /* c_symbol_merge() */
db40ba14
SC
2138
2139static int
355afbcd
KR
2140DEFUN (c_line_new, (symbol, paddr, line_number, frag),
2141 symbolS * symbol AND
2142 long paddr AND
2143 unsigned short line_number AND
2144 fragS * frag)
db40ba14 2145{
355afbcd
KR
2146 struct lineno_list *new_line =
2147 (struct lineno_list *) xmalloc (sizeof (struct lineno_list));
2148
2149 segment_info_type *s = segment_info + now_seg;
c593cf41
SC
2150 new_line->line.l_lnno = line_number;
2151
355afbcd
KR
2152 if (line_number == 0)
2153 {
2154 last_line_symbol = symbol;
2155 new_line->line.l_addr.l_symndx = (long) symbol;
2156 }
2157 else
2158 {
2159 new_line->line.l_addr.l_paddr = paddr;
2160 }
c593cf41 2161
355afbcd
KR
2162 new_line->frag = (char *) frag;
2163 new_line->next = (struct lineno_list *) NULL;
2164
2165
2166 if (s->lineno_list_head == (struct lineno_list *) NULL)
2167 {
2168 s->lineno_list_head = new_line;
2169 }
2170 else
2171 {
2172 s->lineno_list_tail->next = new_line;
2173 }
c58dbabf 2174 s->lineno_list_tail = new_line;
355afbcd 2175 return LINESZ * s->scnhdr.s_nlnno++;
db40ba14
SC
2176}
2177
355afbcd
KR
2178void
2179c_dot_file_symbol (filename)
2180 char *filename;
db40ba14 2181{
355afbcd 2182 symbolS *symbolP;
c593cf41 2183
355afbcd
KR
2184 symbolP = symbol_new (".file",
2185 SEG_DEBUG,
2186 0,
2187 &zero_address_frag);
c593cf41 2188
355afbcd
KR
2189 S_SET_STORAGE_CLASS (symbolP, C_FILE);
2190 S_SET_NUMBER_AUXILIARY (symbolP, 1);
2191 SA_SET_FILE_FNAME (symbolP, filename);
9ce31b66 2192#ifndef NO_LISTING
c593cf41
SC
2193 {
2194 extern int listing;
355afbcd
KR
2195 if (listing)
2196 {
2197 listing_source_file (filename);
2198 }
2199
c593cf41 2200 }
c593cf41 2201
355afbcd
KR
2202#endif
2203 SF_SET_DEBUG (symbolP);
4f0bccc7 2204 S_SET_VALUE (symbolP, (valueT) previous_file_symbol);
355afbcd
KR
2205
2206 previous_file_symbol = symbolP;
c593cf41 2207
355afbcd
KR
2208 /* Make sure that the symbol is first on the symbol chain */
2209 if (symbol_rootP != symbolP)
2210 {
2211 if (symbolP == symbol_lastP)
2212 {
2213 symbol_lastP = symbol_lastP->sy_previous;
2214 } /* if it was the last thing on the list */
c593cf41 2215
355afbcd
KR
2216 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2217 symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP);
2218 symbol_rootP = symbolP;
2219 } /* if not first on the list */
c593cf41 2220
355afbcd 2221} /* c_dot_file_symbol() */
db40ba14
SC
2222
2223/*
2224 * Build a 'section static' symbol.
2225 */
2226
355afbcd
KR
2227symbolS *
2228c_section_symbol (name, idx)
2229 char *name;
2230 int idx;
db40ba14 2231{
355afbcd 2232 symbolS *symbolP;
c593cf41 2233
355afbcd
KR
2234 symbolP = symbol_new (name, idx,
2235 0,
2236 &zero_address_frag);
c593cf41 2237
355afbcd
KR
2238 S_SET_STORAGE_CLASS (symbolP, C_STAT);
2239 S_SET_NUMBER_AUXILIARY (symbolP, 1);
c593cf41 2240
355afbcd 2241 SF_SET_STATICS (symbolP);
c593cf41 2242
355afbcd
KR
2243 return symbolP;
2244} /* c_section_symbol() */
db40ba14 2245
355afbcd
KR
2246static void
2247DEFUN (w_symbols, (abfd, where, symbol_rootP),
2248 bfd * abfd AND
2249 char *where AND
2250 symbolS * symbol_rootP)
db40ba14 2251{
355afbcd
KR
2252 symbolS *symbolP;
2253 unsigned int i;
2254
2255 /* First fill in those values we have only just worked out */
2256 for (i = SEG_E0; i < SEG_E9; i++)
2257 {
2258 symbolP = segment_info[i].dot;
2259 if (symbolP)
c593cf41 2260 {
355afbcd
KR
2261
2262 SA_SET_SCN_SCNLEN (symbolP, segment_info[i].scnhdr.s_size);
2263 SA_SET_SCN_NRELOC (symbolP, segment_info[i].scnhdr.s_nreloc);
2264 SA_SET_SCN_NLINNO (symbolP, segment_info[i].scnhdr.s_nlnno);
2265
c593cf41
SC
2266 }
2267 }
355afbcd
KR
2268
2269 /*
c593cf41
SC
2270 * Emit all symbols left in the symbol chain.
2271 */
355afbcd
KR
2272 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
2273 {
2274 /* Used to save the offset of the name. It is used to point
c593cf41 2275 to the string in memory but must be a file offset. */
355afbcd 2276 register char *temp;
c593cf41 2277
355afbcd 2278 tc_coff_symbol_emit_hook (symbolP);
c593cf41 2279
355afbcd
KR
2280 temp = S_GET_NAME (symbolP);
2281 if (SF_GET_STRING (symbolP))
2282 {
2283 S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
2284 S_SET_ZEROES (symbolP, 0);
db40ba14 2285 }
355afbcd
KR
2286 else
2287 {
604633ae 2288 memset (symbolP->sy_symbol.ost_entry.n_name, 0, SYMNMLEN);
355afbcd
KR
2289 strncpy (symbolP->sy_symbol.ost_entry.n_name, temp, SYMNMLEN);
2290 }
2291 where = symbol_to_chars (abfd, where, symbolP);
2292 S_SET_NAME (symbolP, temp);
2293 }
2294
db40ba14
SC
2295} /* w_symbols() */
2296
355afbcd 2297static void
604633ae
ILT
2298obj_coff_lcomm (ignore)
2299 int ignore;
db40ba14 2300{
442f17dc
KR
2301 s_lcomm(0);
2302 return;
2303#if 0
b066f445
SEF
2304 char *name;
2305 char c;
2306 int temp;
2307 char *p;
d752f749 2308
b066f445 2309 symbolS *symbolP;
442f17dc 2310
b066f445 2311 name = input_line_pointer;
c593cf41 2312
355afbcd 2313 c = get_symbol_end ();
b066f445
SEF
2314 p = input_line_pointer;
2315 *p = c;
355afbcd
KR
2316 SKIP_WHITESPACE ();
2317 if (*input_line_pointer != ',')
2318 {
2319 as_bad ("Expected comma after name");
2320 ignore_rest_of_line ();
2321 return;
2322 }
2323 if (*input_line_pointer == '\n')
2324 {
2325 as_bad ("Missing size expression");
2326 return;
2327 }
b066f445 2328 input_line_pointer++;
355afbcd
KR
2329 if ((temp = get_absolute_expression ()) < 0)
2330 {
2331 as_warn ("lcomm length (%d.) <0! Ignored.", temp);
2332 ignore_rest_of_line ();
2333 return;
2334 }
b066f445 2335 *p = 0;
c593cf41 2336
9a75dc1f
ILT
2337 symbolP = symbol_find_or_make(name);
2338
2339 if (S_GET_SEGMENT(symbolP) == SEG_UNKNOWN &&
2340 S_GET_VALUE(symbolP) == 0)
2341 {
2342 if (! need_pass_2)
2343 {
2344 char *p;
2345 segT current_seg = now_seg; /* save current seg */
2346 subsegT current_subseg = now_subseg;
2347
604633ae 2348 subseg_set (SEG_E2, 1);
9a75dc1f
ILT
2349 symbolP->sy_frag = frag_now;
2350 p = frag_var(rs_org, 1, 1, (relax_substateT)0, symbolP,
2351 temp, (char *)0);
2352 *p = 0;
604633ae 2353 subseg_set (current_seg, current_subseg); /* restore current seg */
9a75dc1f
ILT
2354 S_SET_SEGMENT(symbolP, SEG_E2);
2355 S_SET_STORAGE_CLASS(symbolP, C_STAT);
2356 }
2357 }
2358 else
2359 as_bad("Symbol %s already defined", name);
2360
2361 demand_empty_rest_of_line();
442f17dc 2362#endif
db40ba14
SC
2363}
2364
355afbcd 2365static void
9a75dc1f
ILT
2366DEFUN (fixup_mdeps, (frags, h, this_segment),
2367 fragS * frags AND
2368 object_headers * h AND
2369 segT this_segment)
3ad9ec6a 2370{
9a75dc1f 2371 subseg_change (this_segment, 0);
355afbcd 2372 while (frags)
3ad9ec6a 2373 {
355afbcd
KR
2374 switch (frags->fr_type)
2375 {
2376 case rs_align:
2377 case rs_org:
2378 frags->fr_type = rs_fill;
2379 frags->fr_offset =
2380 (frags->fr_next->fr_address - frags->fr_address - frags->fr_fix);
2381 break;
2382 case rs_machine_dependent:
9a75dc1f
ILT
2383 md_convert_frag (h, frags);
2384 frag_wane (frags);
355afbcd
KR
2385 break;
2386 default:
2387 ;
2388 }
2389 frags = frags->fr_next;
3ad9ec6a 2390 }
3ad9ec6a 2391}
355afbcd 2392
db40ba14 2393#if 1
355afbcd 2394static void
2cb0bdc7 2395DEFUN (fixup_segment, (segP, this_segment_type),
016e0d42 2396 segment_info_type * segP AND
355afbcd 2397 segT this_segment_type)
db40ba14 2398{
016e0d42 2399 register fixS * fixP;
355afbcd
KR
2400 register symbolS *add_symbolP;
2401 register symbolS *sub_symbolP;
2402 register long add_number;
2403 register int size;
2404 register char *place;
2405 register long where;
2406 register char pcrel;
2407 register fragS *fragP;
2408 register segT add_symbol_segment = SEG_ABSOLUTE;
2409
2410
016e0d42 2411 for (fixP = segP->fix_root; fixP; fixP = fixP->fx_next)
355afbcd
KR
2412 {
2413 fragP = fixP->fx_frag;
2414 know (fragP);
2415 where = fixP->fx_where;
2416 place = fragP->fr_literal + where;
2417 size = fixP->fx_size;
2418 add_symbolP = fixP->fx_addsy;
db40ba14 2419#ifdef TC_I960
ca16b5e5 2420 if (fixP->fx_tcbit && TC_S_IS_CALLNAME (add_symbolP))
355afbcd 2421 {
98c6bbbe
KR
2422 /* Relocation should be done via the associated 'bal' entry
2423 point symbol. */
c593cf41 2424
355afbcd
KR
2425 if (!TC_S_IS_BALNAME (tc_get_bal_of_call (add_symbolP)))
2426 {
2427 as_bad ("No 'bal' entry point for leafproc %s",
2428 S_GET_NAME (add_symbolP));
2429 continue;
2430 }
2431 fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP);
98c6bbbe 2432 }
db40ba14 2433#endif
355afbcd
KR
2434 sub_symbolP = fixP->fx_subsy;
2435 add_number = fixP->fx_offset;
2436 pcrel = fixP->fx_pcrel;
2437
2438 if (add_symbolP)
2439 {
2440 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
2441 } /* if there is an addend */
2442
2443 if (sub_symbolP)
2444 {
2445 if (!add_symbolP)
2446 {
2447 /* Its just -sym */
2448 if (S_GET_SEGMENT (sub_symbolP) != SEG_ABSOLUTE)
2449 {
2450 as_bad ("Negative of non-absolute symbol %s", S_GET_NAME (sub_symbolP));
2451 } /* not absolute */
2452
2453 add_number -= S_GET_VALUE (sub_symbolP);
2454 fixP->fx_subsy = 0;
2455
2456 /* if sub_symbol is in the same segment that add_symbol
c593cf41 2457 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
355afbcd
KR
2458 }
2459 else if ((S_GET_SEGMENT (sub_symbolP) == add_symbol_segment)
2460 && (SEG_NORMAL (add_symbol_segment)
2461 || (add_symbol_segment == SEG_ABSOLUTE)))
2462 {
98c6bbbe
KR
2463 /* Difference of 2 symbols from same segment. Can't
2464 make difference of 2 undefineds: 'value' means
2465 something different for N_UNDF. */
db40ba14 2466#ifdef TC_I960
355afbcd 2467 /* Makes no sense to use the difference of 2 arbitrary symbols
eeeaa778 2468 as the target of a call instruction. */
ca16b5e5 2469 if (fixP->fx_tcbit)
355afbcd
KR
2470 {
2471 as_bad ("callj to difference of 2 symbols");
2472 }
2473#endif /* TC_I960 */
2474 add_number += S_GET_VALUE (add_symbolP) -
2475 S_GET_VALUE (sub_symbolP);
2476
2477 add_symbolP = NULL;
2478 fixP->fx_addsy = NULL;
98c6bbbe 2479 fixP->fx_done = 1;
355afbcd
KR
2480 }
2481 else
2482 {
2483 /* Different segments in subtraction. */
2484 know (!(S_IS_EXTERNAL (sub_symbolP) && (S_GET_SEGMENT (sub_symbolP) == SEG_ABSOLUTE)));
2485
2486 if ((S_GET_SEGMENT (sub_symbolP) == SEG_ABSOLUTE))
2487 {
2488 add_number -= S_GET_VALUE (sub_symbolP);
2489 }
56f61233
KR
2490#ifdef DIFF_EXPR_OK
2491 else if (!pcrel
2492 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2493 {
2494 /* Make it pc-relative. */
2495 add_number += (md_pcrel_from (fixP)
2496 - S_GET_VALUE (sub_symbolP));
2497 pcrel = 1;
2498 fixP->fx_pcrel = 1;
2499 sub_symbolP = 0;
2500 fixP->fx_subsy = 0;
2501 }
2502#endif
355afbcd
KR
2503 else
2504 {
604633ae 2505 as_bad ("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %ld.",
355afbcd 2506 segment_name (S_GET_SEGMENT (sub_symbolP)),
604633ae
ILT
2507 S_GET_NAME (sub_symbolP),
2508 (long) (fragP->fr_address + where));
355afbcd
KR
2509 } /* if absolute */
2510 }
2511 } /* if sub_symbolP */
2512
2513 if (add_symbolP)
2514 {
2515 if (add_symbol_segment == this_segment_type && pcrel)
2516 {
2517 /*
eeeaa778
KR
2518 * This fixup was made when the symbol's segment was
2519 * SEG_UNKNOWN, but it is now in the local segment.
2520 * So we know how to do the address without relocation.
2521 */
db40ba14 2522#ifdef TC_I960
355afbcd 2523 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
eeeaa778
KR
2524 * in which cases it modifies *fixP as appropriate. In the case
2525 * of a 'calls', no further work is required, and *fixP has been
2526 * set up to make the rest of the code below a no-op.
2527 */
355afbcd
KR
2528 reloc_callj (fixP);
2529#endif /* TC_I960 */
2530
2531 add_number += S_GET_VALUE (add_symbolP);
2532 add_number -= md_pcrel_from (fixP);
016e0d42
ILT
2533#ifdef TC_I386
2534 /* On the 386 we must adjust by the segment
2535 vaddr as well. Ian Taylor. */
2536 add_number -= segP->scnhdr.s_vaddr;
2537#endif
355afbcd 2538 pcrel = 0; /* Lie. Don't want further pcrel processing. */
98c6bbbe
KR
2539 fixP->fx_addsy = NULL;
2540 fixP->fx_done = 1;
355afbcd
KR
2541 }
2542 else
2543 {
2544 switch (add_symbol_segment)
2545 {
2546 case SEG_ABSOLUTE:
db40ba14 2547#ifdef TC_I960
355afbcd
KR
2548 reloc_callj (fixP); /* See comment about reloc_callj() above*/
2549#endif /* TC_I960 */
2550 add_number += S_GET_VALUE (add_symbolP);
2551 fixP->fx_addsy = NULL;
98c6bbbe 2552 fixP->fx_done = 1;
355afbcd
KR
2553 add_symbolP = NULL;
2554 break;
2555 default:
2556
61001d96
ILT
2557#ifdef TC_A29K
2558 /* This really should be handled in the linker, but
2559 backward compatibility forbids. */
2560 add_number += S_GET_VALUE (add_symbolP);
2561#else
355afbcd
KR
2562 add_number += S_GET_VALUE (add_symbolP) +
2563 segment_info[S_GET_SEGMENT (add_symbolP)].scnhdr.s_paddr;
61001d96 2564#endif
355afbcd
KR
2565 break;
2566
2567 case SEG_UNKNOWN:
db40ba14 2568#ifdef TC_I960
355afbcd
KR
2569 if ((int) fixP->fx_bit_fixP == 13)
2570 {
2571 /* This is a COBR instruction. They have only a
eeeaa778
KR
2572 * 13-bit displacement and are only to be used
2573 * for local branches: flag as error, don't generate
2574 * relocation.
2575 */
355afbcd 2576 as_bad ("can't use COBR format with external label");
98c6bbbe
KR
2577 fixP->fx_addsy = NULL;
2578 fixP->fx_done = 1;
355afbcd
KR
2579 continue;
2580 } /* COBR */
2581#endif /* TC_I960 */
6142210d 2582#ifdef TC_I386
355afbcd 2583 /* 386 COFF uses a peculiar format in
016e0d42
ILT
2584 which the value of a common symbol is
2585 stored in the .text segment (I've
2586 checked this on SVR3.2 and SCO 3.2.2)
2587 Ian Taylor <ian@cygnus.com>. */
9a75dc1f
ILT
2588 if (S_IS_COMMON (add_symbolP))
2589 add_number += S_GET_VALUE (add_symbolP);
6142210d 2590#endif
355afbcd
KR
2591 break;
2592
2593
2594 } /* switch on symbol seg */
2595 } /* if not in local seg */
2596 } /* if there was a + symbol */
2597
2598 if (pcrel)
2599 {
2368ec63
ILT
2600#ifndef TC_M88K
2601 /* This adjustment is not correct on the m88k, for which the
2602 linker does all the computation. */
355afbcd 2603 add_number -= md_pcrel_from (fixP);
2368ec63 2604#endif
355afbcd
KR
2605 if (add_symbolP == 0)
2606 {
2607 fixP->fx_addsy = &abs_symbol;
2608 } /* if there's an add_symbol */
016e0d42
ILT
2609#ifdef TC_I386
2610 /* On the 386 we must adjust by the segment vaddr
2611 as well. Ian Taylor. */
2612 add_number -= segP->scnhdr.s_vaddr;
2613#endif
355afbcd
KR
2614 } /* if pcrel */
2615
2616 if (!fixP->fx_bit_fixP)
2617 {
8ff6f40e
ILT
2618#ifndef TC_M88K
2619 /* The m88k uses the offset field of the reloc to get around
2620 this problem. */
355afbcd
KR
2621 if ((size == 1 &&
2622 (add_number & ~0xFF) && ((add_number & ~0xFF) != (-1 & ~0xFF))) ||
2623 (size == 2 &&
2624 (add_number & ~0xFFFF) && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF))))
2625 {
604633ae
ILT
2626 as_bad ("Value of %ld too large for field of %d bytes at 0x%lx",
2627 (long) add_number, size,
2628 (unsigned long) (fragP->fr_address + where));
355afbcd 2629 } /* generic error checking */
8ff6f40e 2630#endif
e41474b7 2631#ifdef WARN_SIGNED_OVERFLOW_WORD
355afbcd 2632 /* Warn if a .word value is too large when treated as
eeeaa778
KR
2633 a signed number. We already know it is not too
2634 negative. This is to catch over-large switches
2635 generated by gcc on the 68k. */
355afbcd
KR
2636 if (!flagseen['J']
2637 && size == 2
2638 && add_number > 0x7fff)
604633ae
ILT
2639 as_bad ("Signed .word overflow; switch may be too large; %ld at 0x%lx",
2640 (long) add_number,
2641 (unsigned long) (fragP->fr_address + where));
e41474b7 2642#endif
355afbcd
KR
2643 } /* not a bit fix */
2644 /* once this fix has been applied, we don't have to output anything
c593cf41 2645 nothing more need be done -*/
355afbcd 2646 md_apply_fix (fixP, add_number);
355afbcd 2647 } /* For each fixS in this segment. */
355afbcd 2648} /* fixup_segment() */
c593cf41 2649
355afbcd 2650#endif
8f3956b3
SS
2651
2652/* The first entry in a .stab section is special. */
2653
2654void
2655obj_coff_init_stab_section (seg)
2656 segT seg;
2657{
8efed14f 2658 char *file;
8f3956b3 2659 char *p;
b89b2880 2660 char *stabstr_name;
8f3956b3
SS
2661 unsigned int stroff;
2662
2663 /* Make space for this first symbol. */
2664 p = frag_more (12);
41b5ef0f 2665 /* Zero it out. */
66c774bb 2666 memset (p, 0, 12);
8efed14f 2667 as_where (&file, (unsigned int *) NULL);
442f17dc 2668 stabstr_name = (char *) alloca (strlen (segment_info[seg].scnhdr.s_name) + 4);
b89b2880
JL
2669 strcpy (stabstr_name, segment_info[seg].scnhdr.s_name);
2670 strcat (stabstr_name, "str");
2671 stroff = get_stab_string_offset (file, stabstr_name);
8f3956b3
SS
2672 know (stroff == 1);
2673 md_number_to_chars (p, stroff, 4);
2674}
2675
2676/* Fill in the counts in the first entry in a .stab section. */
2677
e0209756 2678static void
8f3956b3
SS
2679adjust_stab_section(abfd, seg)
2680 bfd *abfd;
2681 segT seg;
2682{
d6e6bc1c 2683 segT stabstrseg = SEG_UNKNOWN;
8f3956b3 2684 char *secname, *name, *name2;
8f3956b3
SS
2685 char *p = NULL;
2686 int i, strsz = 0, nsyms;
2687 fragS *frag = segment_info[seg].frchainP->frch_root;
2688
2689 /* Look for the associated string table section. */
2690
2691 secname = segment_info[seg].scnhdr.s_name;
2692 name = (char *) alloca (strlen (secname) + 4);
2693 strcpy (name, secname);
2694 strcat (name, "str");
2695
2696 for (i = SEG_E0; i < SEG_UNKNOWN; i++)
2697 {
2698 name2 = segment_info[i].scnhdr.s_name;
2699 if (name2 != NULL && strncmp(name2, name, 8) == 0)
2700 {
2701 stabstrseg = i;
2702 break;
2703 }
2704 }
2705
2706 /* If we found the section, get its size. */
d6e6bc1c 2707 if (stabstrseg != SEG_UNKNOWN)
8f3956b3
SS
2708 strsz = size_section (abfd, stabstrseg);
2709
2710 nsyms = size_section (abfd, seg) / 12 - 1;
2711
2712 /* Look for the first frag of sufficient size for the initial stab
2713 symbol, and collect a pointer to it. */
2714 while (frag && frag->fr_fix < 12)
2715 frag = frag->fr_next;
2716 assert (frag != 0);
2717 p = frag->fr_literal;
2718 assert (p != 0);
2719
2720 /* Write in the number of stab symbols and the size of the string
2721 table. */
2722 bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
2723 bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
2724}
This page took 0.361588 seconds and 4 git commands to generate.