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