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