* gas/mn10300/udf.s: New test.
[deliverable/binutils-gdb.git] / gas / write.c
CommitLineData
fecd2382 1/* write.c - emit .o file
99fcd8a0 2 Copyright (C) 1986, 87, 90, 91, 92, 93, 94, 95, 96, 97, 1998
5767cfb7 3 Free Software Foundation, Inc.
6efd877d 4
a39116f1 5 This file is part of GAS, the GNU Assembler.
6efd877d 6
a39116f1
RP
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
6efd877d 11
a39116f1
RP
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
6efd877d 16
a39116f1 17 You should have received a copy of the GNU General Public License
f7ab9441
DE
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
fecd2382 21
c593cf41 22/* This thing should be set up to do byteordering correctly. But... */
fecd2382
RP
23
24#include "as.h"
fecd2382
RP
25#include "subsegs.h"
26#include "obstack.h"
27#include "output-file.h"
28
d9d6f094
KR
29/* This looks like a good idea. Let's try turning it on always, for now. */
30#undef BFD_FAST_SECTION_FILL
31#define BFD_FAST_SECTION_FILL
32
43ca9aa6
KR
33/* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
34 instruction so that the disassembler does not choke on it. */
6d5460ab
RP
35#ifndef NOP_OPCODE
36#define NOP_OPCODE 0x00
37#endif
38
f5c32424
KR
39#ifndef TC_ADJUST_RELOC_COUNT
40#define TC_ADJUST_RELOC_COUNT(FIXP,COUNT)
41#endif
42
335d35c8
JL
43#ifndef TC_FORCE_RELOCATION
44#define TC_FORCE_RELOCATION(FIXP) 0
45#endif
46
96fe71e1
MM
47#ifndef TC_FORCE_RELOCATION_SECTION
48#define TC_FORCE_RELOCATION_SECTION(FIXP,SEG) TC_FORCE_RELOCATION(FIXP)
49#endif
50
51#ifndef MD_PCREL_FROM_SECTION
52#define MD_PCREL_FROM_SECTION(FIXP, SEC) md_pcrel_from(FIXP)
53#endif
54
43ca9aa6
KR
55#ifndef WORKING_DOT_WORD
56extern CONST int md_short_jump_size;
57extern CONST int md_long_jump_size;
58#endif
59
1cf7548e 60int symbol_table_frozen;
a55774a1 61void print_fixup PARAMS ((fixS *));
1cf7548e 62
1b96bdce 63#ifdef BFD_ASSEMBLER
a55774a1
KR
64static void renumber_sections PARAMS ((bfd *, asection *, PTR));
65
1b96bdce
ILT
66/* We generally attach relocs to frag chains. However, after we have
67 chained these all together into a segment, any relocs we add after
68 that must be attached to a segment. This will include relocs added
69 in md_estimate_size_for_relax, for example. */
70static int frags_chained = 0;
71#endif
72
43ca9aa6
KR
73#ifndef BFD_ASSEMBLER
74
45432836 75#ifndef MANY_SEGMENTS
09952cd9
KR
76struct frag *text_frag_root;
77struct frag *data_frag_root;
78struct frag *bss_frag_root;
fecd2382 79
09952cd9
KR
80struct frag *text_last_frag; /* Last frag in segment. */
81struct frag *data_last_frag; /* Last frag in segment. */
65bfcf2e 82static struct frag *bss_last_frag; /* Last frag in segment. */
45432836 83#endif
fecd2382 84
1cf7548e 85#ifndef BFD
fecd2382 86static object_headers headers;
80aab579
ILT
87#endif
88
89long string_byte_count;
fecd2382
RP
90char *next_object_file_charP; /* Tracks object file bytes. */
91
3eb802b5 92#ifndef OBJ_VMS
fecd2382 93int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
3eb802b5 94#endif
fecd2382 95
43ca9aa6
KR
96#endif /* BFD_ASSEMBLER */
97
a043f579
ILT
98static int n_fixups;
99
7a0405b9 100#ifdef BFD_ASSEMBLER
b23f6743 101static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
5ac34ac3
ILT
102 symbolS *add, symbolS *sub,
103 offsetT offset, int pcrel,
7a0405b9 104 bfd_reloc_code_real_type r_type));
5ac34ac3 105#else
b23f6743 106static fixS *fix_new_internal PARAMS ((fragS *, int where, int size,
7a0405b9
ILT
107 symbolS *add, symbolS *sub,
108 offsetT offset, int pcrel,
109 int r_type));
5ac34ac3 110#endif
3f81f3cf 111#if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
6efd877d 112static long fixup_segment PARAMS ((fixS * fixP, segT this_segment_type));
80aab579 113#endif
d5364748 114static relax_addressT relax_align PARAMS ((relax_addressT addr, int align));
374bdac7
DE
115#if defined (BFD_ASSEMBLER) || ! defined (BFD)
116static fragS *chain_frchains_together_1 PARAMS ((segT, struct frchain *));
117#endif
118#ifdef BFD_ASSEMBLER
119static void chain_frchains_together PARAMS ((bfd *, segT, PTR));
120static void cvt_frag_to_fill PARAMS ((segT, fragS *));
121static void relax_and_size_seg PARAMS ((bfd *, asection *, PTR));
122static void adjust_reloc_syms PARAMS ((bfd *, asection *, PTR));
123static void write_relocs PARAMS ((bfd *, asection *, PTR));
124static void write_contents PARAMS ((bfd *, asection *, PTR));
125static void set_symtab PARAMS ((void));
126#endif
127#if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
128static void merge_data_into_text PARAMS ((void));
129#endif
130#if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
131static void cvt_frag_to_fill PARAMS ((object_headers *, segT, fragS *));
132static void remove_subsegs PARAMS ((frchainS *, int, fragS **, fragS **));
133static void relax_and_size_all_segments PARAMS ((void));
134#endif
fecd2382
RP
135
136/*
137 * fix_new()
138 *
139 * Create a fixS in obstack 'notes'.
140 */
5ac34ac3
ILT
141static fixS *
142fix_new_internal (frag, where, size, add_symbol, sub_symbol, offset, pcrel,
143 r_type)
6efd877d
KR
144 fragS *frag; /* Which frag? */
145 int where; /* Where in that frag? */
b23f6743 146 int size; /* 1, 2, or 4 usually. */
6efd877d 147 symbolS *add_symbol; /* X_add_symbol. */
5ac34ac3 148 symbolS *sub_symbol; /* X_op_symbol. */
d5364748 149 offsetT offset; /* X_add_number. */
6efd877d 150 int pcrel; /* TRUE if PC-relative relocation. */
43ca9aa6
KR
151#ifdef BFD_ASSEMBLER
152 bfd_reloc_code_real_type r_type; /* Relocation type */
153#else
6efd877d 154 int r_type; /* Relocation type */
43ca9aa6 155#endif
fecd2382 156{
6efd877d
KR
157 fixS *fixP;
158
a043f579
ILT
159 n_fixups++;
160
6efd877d
KR
161 fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
162
163 fixP->fx_frag = frag;
164 fixP->fx_where = where;
165 fixP->fx_size = size;
f00f5ecd
ILT
166 /* We've made fx_size a narrow field; check that it's wide enough. */
167 if (fixP->fx_size != size)
168 {
99fcd8a0 169 as_bad (_("field fx_size too small to hold %d"), size);
f00f5ecd
ILT
170 abort ();
171 }
6efd877d
KR
172 fixP->fx_addsy = add_symbol;
173 fixP->fx_subsy = sub_symbol;
174 fixP->fx_offset = offset;
175 fixP->fx_pcrel = pcrel;
a55774a1 176 fixP->fx_plt = 0;
43ca9aa6 177#if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
6efd877d 178 fixP->fx_r_type = r_type;
c593cf41 179#endif
6efd877d
KR
180 fixP->fx_im_disp = 0;
181 fixP->fx_pcrel_adjust = 0;
6efd877d 182 fixP->fx_bit_fixP = 0;
43ca9aa6 183 fixP->fx_addnumber = 0;
a58374d7 184 fixP->fx_tcbit = 0;
98c6bbbe 185 fixP->fx_done = 0;
a78bc551
ILT
186 fixP->fx_no_overflow = 0;
187 fixP->fx_signed = 0;
43ca9aa6 188
f00f5ecd
ILT
189#ifdef TC_FIX_TYPE
190 TC_INIT_FIX_DATA(fixP);
43ca9aa6 191#endif
43ca9aa6 192
84fa9814
KR
193 as_where (&fixP->fx_file, &fixP->fx_line);
194
43ca9aa6
KR
195 /* Usually, we want relocs sorted numerically, but while
196 comparing to older versions of gas that have relocs
197 reverse sorted, it is convenient to have this compile
198 time option. xoxorich. */
199
200 {
6efd877d 201
43ca9aa6 202#ifdef BFD_ASSEMBLER
1b96bdce
ILT
203 fixS **seg_fix_rootP = (frags_chained
204 ? &seg_info (now_seg)->fix_root
205 : &frchain_now->fix_root);
206 fixS **seg_fix_tailP = (frags_chained
207 ? &seg_info (now_seg)->fix_tail
208 : &frchain_now->fix_tail);
43ca9aa6 209#endif
09952cd9 210
f6e504fe 211#ifdef REVERSE_SORT_RELOCS
6efd877d 212
43ca9aa6
KR
213 fixP->fx_next = *seg_fix_rootP;
214 *seg_fix_rootP = fixP;
6efd877d 215
f6e504fe 216#else /* REVERSE_SORT_RELOCS */
6efd877d 217
43ca9aa6 218 fixP->fx_next = NULL;
6efd877d 219
43ca9aa6
KR
220 if (*seg_fix_tailP)
221 (*seg_fix_tailP)->fx_next = fixP;
222 else
223 *seg_fix_rootP = fixP;
224 *seg_fix_tailP = fixP;
6efd877d 225
f6e504fe 226#endif /* REVERSE_SORT_RELOCS */
6efd877d 227
43ca9aa6
KR
228 }
229
230 return fixP;
231}
232
5ac34ac3
ILT
233/* Create a fixup relative to a symbol (plus a constant). */
234
235fixS *
236fix_new (frag, where, size, add_symbol, offset, pcrel, r_type)
237 fragS *frag; /* Which frag? */
238 int where; /* Where in that frag? */
f7da4a99 239 int size; /* 1, 2, or 4 usually. */
5ac34ac3
ILT
240 symbolS *add_symbol; /* X_add_symbol. */
241 offsetT offset; /* X_add_number. */
242 int pcrel; /* TRUE if PC-relative relocation. */
243#ifdef BFD_ASSEMBLER
244 bfd_reloc_code_real_type r_type; /* Relocation type */
245#else
246 int r_type; /* Relocation type */
247#endif
248{
249 return fix_new_internal (frag, where, size, add_symbol,
250 (symbolS *) NULL, offset, pcrel, r_type);
251}
252
253/* Create a fixup for an expression. Currently we only support fixups
254 for difference expressions. That is itself more than most object
255 file formats support anyhow. */
256
257fixS *
258fix_new_exp (frag, where, size, exp, pcrel, r_type)
259 fragS *frag; /* Which frag? */
260 int where; /* Where in that frag? */
f7da4a99 261 int size; /* 1, 2, or 4 usually. */
5ac34ac3
ILT
262 expressionS *exp; /* Expression. */
263 int pcrel; /* TRUE if PC-relative relocation. */
264#ifdef BFD_ASSEMBLER
265 bfd_reloc_code_real_type r_type; /* Relocation type */
266#else
267 int r_type; /* Relocation type */
268#endif
269{
270 symbolS *add = NULL;
271 symbolS *sub = NULL;
272 offsetT off = 0;
96fe71e1 273
5ac34ac3
ILT
274 switch (exp->X_op)
275 {
276 case O_absent:
277 break;
278
4acf8c78
KR
279 case O_add:
280 /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
281 the difference expression cannot immediately be reduced. */
282 {
4acf8c78
KR
283 symbolS *stmp = make_expr_symbol (exp);
284 exp->X_op = O_symbol;
285 exp->X_op_symbol = 0;
286 exp->X_add_symbol = stmp;
287 exp->X_add_number = 0;
288 return fix_new_exp (frag, where, size, exp, pcrel, r_type);
289 }
290
96fe71e1
MM
291 case O_symbol_rva:
292 add = exp->X_add_symbol;
293 off = exp->X_add_number;
294
295#if defined(BFD_ASSEMBLER)
296 r_type = BFD_RELOC_RVA;
8095b665
ILT
297#else
298#if defined(TC_RVA_RELOC)
96fe71e1
MM
299 r_type = TC_RVA_RELOC;
300#else
99fcd8a0 301 as_fatal(_("rva not supported"));
8095b665 302#endif
96fe71e1
MM
303#endif
304 break;
305
b23f6743
KR
306 case O_uminus:
307 sub = exp->X_add_symbol;
308 off = exp->X_add_number;
309 break;
310
5ac34ac3
ILT
311 case O_subtract:
312 sub = exp->X_op_symbol;
313 /* Fall through. */
314 case O_symbol:
315 add = exp->X_add_symbol;
316 /* Fall through. */
317 case O_constant:
318 off = exp->X_add_number;
319 break;
5d0cd0b8 320
5ac34ac3 321 default:
00a69b89
ILT
322 add = make_expr_symbol (exp);
323 break;
5ac34ac3
ILT
324 }
325
326 return fix_new_internal (frag, where, size, add, sub, off,
327 pcrel, r_type);
328}
329
43ca9aa6
KR
330/* Append a string onto another string, bumping the pointer along. */
331void
332append (charPP, fromP, length)
333 char **charPP;
334 char *fromP;
335 unsigned long length;
336{
337 /* Don't trust memcpy() of 0 chars. */
338 if (length == 0)
339 return;
340
80aab579 341 memcpy (*charPP, fromP, length);
43ca9aa6
KR
342 *charPP += length;
343}
344
5d0cd0b8 345#ifndef BFD_ASSEMBLER
43ca9aa6
KR
346int section_alignment[SEG_MAXIMUM_ORDINAL];
347#endif
348
349/*
350 * This routine records the largest alignment seen for each segment.
351 * If the beginning of the segment is aligned on the worst-case
352 * boundary, all of the other alignments within it will work. At
353 * least one object format really uses this info.
354 */
5d0cd0b8 355void
43ca9aa6
KR
356record_alignment (seg, align)
357 /* Segment to which alignment pertains */
358 segT seg;
359 /* Alignment, as a power of 2 (e.g., 1 => 2-byte boundary, 2 => 4-byte
360 boundary, etc.) */
361 int align;
362{
5c800455
ILT
363 if (seg == absolute_section)
364 return;
43ca9aa6 365#ifdef BFD_ASSEMBLER
99fcd8a0 366 if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
43ca9aa6
KR
367 bfd_set_section_alignment (stdoutput, seg, align);
368#else
369 if (align > section_alignment[(int) seg])
370 section_alignment[(int) seg] = align;
371#endif
372}
373
a55774a1
KR
374#ifdef BFD_ASSEMBLER
375
376/* Reset the section indices after removing the gas created sections. */
377
378static void
379renumber_sections (abfd, sec, countparg)
380 bfd *abfd;
381 asection *sec;
382 PTR countparg;
383{
384 int *countp = (int *) countparg;
385
386 sec->index = *countp;
387 ++*countp;
388}
389
390#endif /* defined (BFD_ASSEMBLER) */
391
43ca9aa6
KR
392#if defined (BFD_ASSEMBLER) || ! defined (BFD)
393
394static fragS *
395chain_frchains_together_1 (section, frchp)
396 segT section;
397 struct frchain *frchp;
398{
399 fragS dummy, *prev_frag = &dummy;
b04bc423 400#ifdef BFD_ASSEMBLER
3f81f3cf 401 fixS fix_dummy, *prev_fix = &fix_dummy;
b04bc423 402#endif
262b22cd 403
43ca9aa6
KR
404 for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
405 {
406 prev_frag->fr_next = frchp->frch_root;
407 prev_frag = frchp->frch_last;
96fe71e1 408 assert (prev_frag->fr_type != 0);
3a0e38ee 409#ifdef BFD_ASSEMBLER
262b22cd
ILT
410 if (frchp->fix_root != (fixS *) NULL)
411 {
412 if (seg_info (section)->fix_root == (fixS *) NULL)
413 seg_info (section)->fix_root = frchp->fix_root;
414 prev_fix->fx_next = frchp->fix_root;
1b96bdce 415 seg_info (section)->fix_tail = frchp->fix_tail;
262b22cd
ILT
416 prev_fix = frchp->fix_tail;
417 }
3a0e38ee 418#endif
43ca9aa6 419 }
96fe71e1 420 assert (prev_frag->fr_type != 0);
43ca9aa6
KR
421 prev_frag->fr_next = 0;
422 return prev_frag;
423}
424
425#endif
426
427#ifdef BFD_ASSEMBLER
428
429static void
430chain_frchains_together (abfd, section, xxx)
431 bfd *abfd; /* unused */
432 segT section;
a58374d7 433 PTR xxx; /* unused */
43ca9aa6 434{
f2f7d044
ILT
435 segment_info_type *info;
436
437 /* BFD may have introduced its own sections without using
438 subseg_new, so it is possible that seg_info is NULL. */
439 info = seg_info (section);
440 if (info != (segment_info_type *) NULL)
0f894895
JL
441 info->frchainP->frch_last
442 = chain_frchains_together_1 (section, info->frchainP);
1b96bdce
ILT
443
444 /* Now that we've chained the frags together, we must add new fixups
445 to the segment, not to the frag chain. */
446 frags_chained = 1;
43ca9aa6
KR
447}
448
449#endif
542e1629 450
d5364748 451#if !defined (BFD) && !defined (BFD_ASSEMBLER)
65bfcf2e 452
374bdac7 453static void
6efd877d
KR
454remove_subsegs (head, seg, root, last)
455 frchainS *head;
456 int seg;
457 fragS **root;
458 fragS **last;
65bfcf2e 459{
65bfcf2e 460 *root = head->frch_root;
43ca9aa6
KR
461 *last = chain_frchains_together_1 (seg, head);
462}
463
464#endif /* BFD */
465
80aab579
ILT
466#if defined (BFD_ASSEMBLER) || !defined (BFD)
467
43ca9aa6 468#ifdef BFD_ASSEMBLER
58d4951d
ILT
469static void
470cvt_frag_to_fill (sec, fragP)
471 segT sec;
43ca9aa6 472 fragS *fragP;
43ca9aa6 473#else
58d4951d 474static void
d4083e29 475cvt_frag_to_fill (headersP, sec, fragP)
3f81f3cf 476 object_headers *headersP;
d4083e29 477 segT sec;
58d4951d 478 fragS *fragP;
43ca9aa6 479#endif
58d4951d 480{
43ca9aa6 481 switch (fragP->fr_type)
6efd877d 482 {
43ca9aa6 483 case rs_align:
cd3b81bd 484 case rs_align_code:
43ca9aa6 485 case rs_org:
cd3b81bd 486 case rs_space:
43ca9aa6
KR
487#ifdef HANDLE_ALIGN
488 HANDLE_ALIGN (fragP);
489#endif
43ca9aa6 490 know (fragP->fr_next != NULL);
43ca9aa6
KR
491 fragP->fr_offset = (fragP->fr_next->fr_address
492 - fragP->fr_address
98c6bbbe 493 - fragP->fr_fix) / fragP->fr_var;
c151fd1e
KR
494 if (fragP->fr_offset < 0)
495 {
99fcd8a0 496 as_bad (_("attempt to .org/.space backwards? (%ld)"),
c151fd1e
KR
497 (long) fragP->fr_offset);
498 }
cd3b81bd 499 fragP->fr_type = rs_fill;
43ca9aa6 500 break;
65bfcf2e 501
43ca9aa6
KR
502 case rs_fill:
503 break;
504
66a6a381
ILT
505 case rs_leb128:
506 {
507 valueT value = S_GET_VALUE (fragP->fr_symbol);
508 int size;
509
510 size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
511 fragP->fr_subtype);
512
513 fragP->fr_fix += size;
514 fragP->fr_type = rs_fill;
515 fragP->fr_var = 0;
516 fragP->fr_offset = 0;
517 fragP->fr_symbol = NULL;
518 }
519 break;
520
99fcd8a0
MM
521 case rs_cfa:
522 eh_frame_convert_frag (fragP);
523 break;
524
43ca9aa6
KR
525 case rs_machine_dependent:
526#ifdef BFD_ASSEMBLER
527 md_convert_frag (stdoutput, sec, fragP);
528#else
d4083e29 529 md_convert_frag (headersP, sec, fragP);
43ca9aa6
KR
530#endif
531
99fcd8a0
MM
532 assert (fragP->fr_next == NULL
533 || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
534 == fragP->fr_fix));
43ca9aa6
KR
535
536 /*
537 * After md_convert_frag, we make the frag into a ".space 0".
538 * Md_convert_frag() should set up any fixSs and constants
539 * required.
540 */
541 frag_wane (fragP);
542 break;
543
544#ifndef WORKING_DOT_WORD
545 case rs_broken_word:
546 {
547 struct broken_word *lie;
548
549 if (fragP->fr_subtype)
550 {
551 fragP->fr_fix += md_short_jump_size;
552 for (lie = (struct broken_word *) (fragP->fr_symbol);
553 lie && lie->dispfrag == fragP;
554 lie = lie->next_broken_word)
555 if (lie->added == 1)
556 fragP->fr_fix += md_long_jump_size;
557 }
558 frag_wane (fragP);
559 }
560 break;
561#endif
562
563 default:
564 BAD_CASE (fragP->fr_type);
565 break;
6efd877d 566 }
65bfcf2e 567}
6efd877d 568
80aab579
ILT
569#endif /* defined (BFD_ASSEMBLER) || !defined (BFD) */
570
43ca9aa6
KR
571#ifdef BFD_ASSEMBLER
572static void
573relax_and_size_seg (abfd, sec, xxx)
574 bfd *abfd;
575 asection *sec;
a58374d7 576 PTR xxx;
43ca9aa6
KR
577{
578 flagword flags;
13e9182d
KR
579 fragS *fragp;
580 segment_info_type *seginfo;
581 int x;
582 valueT size, newsize;
43ca9aa6 583
f00f5ecd
ILT
584 subseg_change (sec, 0);
585
43ca9aa6
KR
586 flags = bfd_get_section_flags (abfd, sec);
587
d9d6f094 588 seginfo = seg_info (sec);
13e9182d 589 if (seginfo && seginfo->frchainP)
43ca9aa6 590 {
13e9182d
KR
591 relax_segment (seginfo->frchainP->frch_root, sec);
592 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
593 cvt_frag_to_fill (sec, fragp);
594 for (fragp = seginfo->frchainP->frch_root;
595 fragp->fr_next;
596 fragp = fragp->fr_next)
597 /* walk to last elt */;
598 size = fragp->fr_address + fragp->fr_fix;
599 }
600 else
601 size = 0;
a58374d7
ILT
602
603 if (size > 0 && ! seginfo->bss)
604 flags |= SEC_HAS_CONTENTS;
605
606 /* @@ This is just an approximation. */
5f6efd5a 607 if (seginfo && seginfo->fix_root)
a58374d7
ILT
608 flags |= SEC_RELOC;
609 else
610 flags &= ~SEC_RELOC;
611 x = bfd_set_section_flags (abfd, sec, flags);
612 assert (x == true);
613
20b39b6f
JL
614 newsize = md_section_align (sec, size);
615 x = bfd_set_section_size (abfd, sec, newsize);
13e9182d
KR
616 assert (x == true);
617
618 /* If the size had to be rounded up, add some padding in the last
619 non-empty frag. */
13e9182d
KR
620 assert (newsize >= size);
621 if (size != newsize)
622 {
623 fragS *last = seginfo->frchainP->frch_last;
624 fragp = seginfo->frchainP->frch_root;
625 while (fragp->fr_next != last)
626 fragp = fragp->fr_next;
627 last->fr_address = size;
628 fragp->fr_offset += newsize - size;
629 }
630
43ca9aa6
KR
631#ifdef tc_frob_section
632 tc_frob_section (sec);
633#endif
634#ifdef obj_frob_section
635 obj_frob_section (sec);
636#endif
637}
638
d5364748
KR
639#ifdef DEBUG2
640static void
641dump_section_relocs (abfd, sec, stream_)
642 bfd *abfd;
643 asection *sec;
644 char *stream_;
645{
646 FILE *stream = (FILE *) stream_;
647 segment_info_type *seginfo = seg_info (sec);
648 fixS *fixp = seginfo->fix_root;
649
650 if (!fixp)
651 return;
652
653 fprintf (stream, "sec %s relocs:\n", sec->name);
654 while (fixp)
655 {
656 symbolS *s = fixp->fx_addsy;
657 if (s)
1cf7548e
KR
658 {
659 fprintf (stream, " %08x: %s(%s", fixp, S_GET_NAME (s),
660 s->bsym->section->name);
661 if (s->bsym->flags & BSF_SECTION_SYM)
662 {
663 fprintf (stream, " section sym");
664 if (S_GET_VALUE (s))
665 fprintf (stream, "+%x", S_GET_VALUE (s));
666 }
667 else
668 fprintf (stream, "+%x", S_GET_VALUE (s));
669 fprintf (stream, ")+%x\n", fixp->fx_offset);
670 }
d5364748
KR
671 else
672 fprintf (stream, " %08x: type %d no sym\n", fixp, fixp->fx_r_type);
673 fixp = fixp->fx_next;
674 }
675}
676#else
8095b665 677#define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
d5364748
KR
678#endif
679
98c6bbbe
KR
680#ifndef EMIT_SECTION_SYMBOLS
681#define EMIT_SECTION_SYMBOLS 1
682#endif
683
d5364748
KR
684static void
685adjust_reloc_syms (abfd, sec, xxx)
686 bfd *abfd;
687 asection *sec;
a58374d7 688 PTR xxx;
d5364748
KR
689{
690 segment_info_type *seginfo = seg_info (sec);
691 fixS *fixp;
692
693 if (seginfo == NULL)
694 return;
695
696 dump_section_relocs (abfd, sec, stderr);
697
698 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
98c6bbbe
KR
699 if (fixp->fx_done)
700 /* ignore it */;
701 else if (fixp->fx_addsy)
d5364748 702 {
a55774a1 703 symbolS *sym;
4acf8c78
KR
704 asection *symsec;
705
a55774a1
KR
706#ifdef DEBUG5
707 fprintf (stderr, "\n\nadjusting fixup:\n");
708 print_fixup (fixp);
709#endif
710
711 sym = fixp->fx_addsy;
d5364748 712
73d25784
ILT
713 /* All symbols should have already been resolved at this
714 point. It is possible to see unresolved expression
715 symbols, though, since they are not in the regular symbol
716 table. */
717 if (sym != NULL && ! sym->sy_resolved)
66a6a381 718 resolve_symbol_value (sym, 1);
73d25784 719 if (fixp->fx_subsy != NULL && ! fixp->fx_subsy->sy_resolved)
66a6a381 720 resolve_symbol_value (fixp->fx_subsy, 1);
73d25784 721
9c199842
ILT
722 /* If this symbol is equated to an undefined symbol, convert
723 the fixup to being against that symbol. */
724 if (sym->sy_value.X_op == O_symbol
725 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
726 {
727 fixp->fx_offset += sym->sy_value.X_add_number;
728 sym = sym->sy_value.X_add_symbol;
729 fixp->fx_addsy = sym;
730 }
731
732 symsec = S_GET_SEGMENT (sym);
733
e120d9fb 734 if (sym != NULL && sym->sy_mri_common)
1356d77d
ILT
735 {
736 /* These symbols are handled specially in fixup_segment. */
737 goto done;
738 }
739
8095b665
ILT
740 if (bfd_is_abs_section (symsec))
741 {
742 /* The fixup_segment routine will not use this symbol in a
743 relocation unless TC_FORCE_RELOCATION returns 1. */
744 if (TC_FORCE_RELOCATION (fixp))
745 {
746 fixp->fx_addsy->sy_used_in_reloc = 1;
747#ifdef UNDEFINED_DIFFERENCE_OK
748 if (fixp->fx_subsy != NULL)
749 fixp->fx_subsy->sy_used_in_reloc = 1;
750#endif
751 }
752 goto done;
753 }
754
a58374d7
ILT
755 /* If it's one of these sections, assume the symbol is
756 definitely going to be output. The code in
757 md_estimate_size_before_relax in tc-mips.c uses this test
758 as well, so if you change this code you should look at that
759 code. */
cd3b81bd 760 if (bfd_is_und_section (symsec)
d5364748 761 || bfd_is_com_section (symsec))
80aab579
ILT
762 {
763 fixp->fx_addsy->sy_used_in_reloc = 1;
d4083e29
KR
764#ifdef UNDEFINED_DIFFERENCE_OK
765 /* We have the difference of an undefined symbol and some
766 other symbol. Make sure to mark the other symbol as used
767 in a relocation so that it will always be output. */
768 if (fixp->fx_subsy)
769 fixp->fx_subsy->sy_used_in_reloc = 1;
770#endif
a55774a1 771 goto done;
80aab579 772 }
d5364748 773
54815b9a
ILT
774 /* Don't try to reduce relocs which refer to .linkonce
775 sections. It can lead to confusion when a debugging
776 section refers to a .linkonce section. I hope this will
777 always be correct. */
6259c65f 778 if (symsec != sec)
54815b9a 779 {
6259c65f
ILT
780 boolean linkonce;
781
782 linkonce = false;
783#ifdef BFD_ASSEMBLER
784 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
785 != 0)
786 linkonce = true;
787#endif
788#ifdef OBJ_ELF
789 /* The GNU toolchain uses an extension for ELF: a section
790 beginning with the magic string .gnu.linkonce is a
791 linkonce section. */
792 if (strncmp (segment_name (symsec), ".gnu.linkonce",
793 sizeof ".gnu.linkonce" - 1) == 0)
794 linkonce = true;
795#endif
796
797 if (linkonce)
798 {
799 fixp->fx_addsy->sy_used_in_reloc = 1;
54815b9a 800#ifdef UNDEFINED_DIFFERENCE_OK
6259c65f
ILT
801 if (fixp->fx_subsy != NULL)
802 fixp->fx_subsy->sy_used_in_reloc = 1;
54815b9a 803#endif
6259c65f
ILT
804 goto done;
805 }
54815b9a 806 }
54815b9a 807
d5364748
KR
808 /* Since we're reducing to section symbols, don't attempt to reduce
809 anything that's already using one. */
98c6bbbe 810 if (sym->bsym->flags & BSF_SECTION_SYM)
80aab579
ILT
811 {
812 fixp->fx_addsy->sy_used_in_reloc = 1;
a55774a1 813 goto done;
80aab579 814 }
d5364748
KR
815
816 /* Is there some other reason we can't adjust this one? (E.g.,
817 call/bal links in i960-bout symbols.) */
818#ifdef obj_fix_adjustable
819 if (! obj_fix_adjustable (fixp))
80aab579
ILT
820 {
821 fixp->fx_addsy->sy_used_in_reloc = 1;
a55774a1 822 goto done;
80aab579 823 }
d5364748 824#endif
efa0c22e
KR
825
826 /* Is there some other (target cpu dependent) reason we can't adjust
5d0cd0b8 827 this one? (E.g. relocations involving function addresses on
efa0c22e
KR
828 the PA. */
829#ifdef tc_fix_adjustable
830 if (! tc_fix_adjustable (fixp))
20b39b6f
JL
831 {
832 fixp->fx_addsy->sy_used_in_reloc = 1;
a55774a1 833 goto done;
20b39b6f 834 }
efa0c22e
KR
835#endif
836
d5364748
KR
837 /* If the section symbol isn't going to be output, the relocs
838 at least should still work. If not, figure out what to do
5d0cd0b8
ILT
839 when we run into that case.
840
841 We refetch the segment when calling section_symbol, rather
842 than using symsec, because S_GET_VALUE may wind up changing
843 the section when it calls resolve_symbol_value. */
d5364748 844 fixp->fx_offset += S_GET_VALUE (sym);
5d0cd0b8 845 fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
80aab579 846 fixp->fx_addsy->sy_used_in_reloc = 1;
a55774a1
KR
847
848 done:
849 ;
d5364748 850 }
1cf7548e 851#if 1/*def RELOC_REQUIRES_SYMBOL*/
e67a0640
KR
852 else
853 {
854 /* There was no symbol required by this relocation. However,
855 BFD doesn't really handle relocations without symbols well.
856 (At least, the COFF support doesn't.) So for now we fake up
857 a local symbol in the absolute section. */
e8501a72 858
1cf7548e 859 fixp->fx_addsy = section_symbol (absolute_section);
a55774a1 860/* fixp->fx_addsy->sy_used_in_reloc = 1; */
e67a0640
KR
861 }
862#endif
d5364748
KR
863
864 dump_section_relocs (abfd, sec, stderr);
865}
866
43ca9aa6 867static void
8d6c34a1 868write_relocs (abfd, sec, xxx)
43ca9aa6
KR
869 bfd *abfd;
870 asection *sec;
a58374d7 871 PTR xxx;
43ca9aa6
KR
872{
873 segment_info_type *seginfo = seg_info (sec);
80aab579
ILT
874 int i;
875 unsigned int n;
43ca9aa6
KR
876 arelent **relocs;
877 fixS *fixp;
98c6bbbe 878 char *err;
43ca9aa6 879
d5364748
KR
880 /* If seginfo is NULL, we did not create this section; don't do
881 anything with it. */
882 if (seginfo == NULL)
43ca9aa6
KR
883 return;
884
885 fixup_segment (seginfo->fix_root, sec);
886
3d3c5039 887 n = 0;
d5364748
KR
888 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
889 n++;
43ca9aa6 890
d5364748 891#ifndef RELOC_EXPANSION_POSSIBLE
43ca9aa6 892 /* Set up reloc information as well. */
590c50d8 893 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
8d6c34a1 894 memset ((char*)relocs, 0, n * sizeof (arelent*));
43ca9aa6 895
3d3c5039
ILT
896 i = 0;
897 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
898 {
899 arelent *reloc;
3d3c5039 900 bfd_reloc_status_type s;
a043f579 901 symbolS *sym;
3d3c5039 902
98c6bbbe 903 if (fixp->fx_done)
3d3c5039 904 {
3d3c5039
ILT
905 n--;
906 continue;
907 }
a043f579
ILT
908
909 /* If this is an undefined symbol which was equated to another
910 symbol, then use generate the reloc against the latter symbol
911 rather than the former. */
912 sym = fixp->fx_addsy;
913 while (sym->sy_value.X_op == O_symbol
914 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
3569064f
ILT
915 {
916 symbolS *n;
917
918 /* We must avoid looping, as that can occur with a badly
919 written program. */
920 n = sym->sy_value.X_add_symbol;
921 if (n == sym)
922 break;
923 fixp->fx_offset += sym->sy_value.X_add_number;
924 sym = n;
925 }
a043f579
ILT
926 fixp->fx_addsy = sym;
927
3d3c5039
ILT
928 reloc = tc_gen_reloc (sec, fixp);
929 if (!reloc)
930 {
931 n--;
932 continue;
933 }
5d0cd0b8
ILT
934
935#if 0
936 /* This test is triggered inappropriately for the SH. */
c43d56f7 937 if (fixp->fx_where + fixp->fx_size
3d3c5039
ILT
938 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
939 abort ();
5d0cd0b8 940#endif
335d35c8 941
4acf8c78
KR
942 s = bfd_install_relocation (stdoutput, reloc,
943 fixp->fx_frag->fr_literal,
944 fixp->fx_frag->fr_address,
945 sec, &err);
3d3c5039
ILT
946 switch (s)
947 {
948 case bfd_reloc_ok:
949 break;
df44a852 950 case bfd_reloc_overflow:
99fcd8a0 951 as_bad_where (fixp->fx_file, fixp->fx_line, _("relocation overflow"));
df44a852 952 break;
3d3c5039 953 default:
99fcd8a0 954 as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
4acf8c78 955 fixp->fx_file, fixp->fx_line);
3d3c5039
ILT
956 }
957 relocs[i++] = reloc;
958 }
d5364748
KR
959#else
960 n = n * MAX_RELOC_EXPANSION;
961 /* Set up reloc information as well. */
b1400218 962 relocs = (arelent **) xmalloc (n * sizeof (arelent *));
d5364748
KR
963
964 i = 0;
965 for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
966 {
967 arelent **reloc;
d5364748
KR
968 char *data;
969 bfd_reloc_status_type s;
a043f579 970 symbolS *sym;
d5364748
KR
971 int j;
972
98c6bbbe 973 if (fixp->fx_done)
d5364748 974 {
d5364748
KR
975 n--;
976 continue;
977 }
a043f579
ILT
978
979 /* If this is an undefined symbol which was equated to another
980 symbol, then use generate the reloc against the latter symbol
981 rather than the former. */
982 sym = fixp->fx_addsy;
983 while (sym->sy_value.X_op == O_symbol
984 && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
985 sym = sym->sy_value.X_add_symbol;
986 fixp->fx_addsy = sym;
987
d5364748
KR
988 reloc = tc_gen_reloc (sec, fixp);
989
990 for (j = 0; reloc[j]; j++)
991 {
992 relocs[i++] = reloc[j];
993 assert(i <= n);
994 }
995 data = fixp->fx_frag->fr_literal + fixp->fx_where;
c43d56f7 996 if (fixp->fx_where + fixp->fx_size
d5364748 997 > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
73d25784 998 as_bad_where (fixp->fx_file, fixp->fx_line,
99fcd8a0 999 _("internal error: fixup not contained within frag"));
d5364748
KR
1000 for (j = 0; reloc[j]; j++)
1001 {
a55774a1
KR
1002 s = bfd_install_relocation (stdoutput, reloc[j],
1003 fixp->fx_frag->fr_literal,
1004 fixp->fx_frag->fr_address,
1005 sec, &err);
d5364748
KR
1006 switch (s)
1007 {
98c6bbbe
KR
1008 case bfd_reloc_ok:
1009 break;
4acf8c78
KR
1010 case bfd_reloc_overflow:
1011 as_bad_where (fixp->fx_file, fixp->fx_line,
99fcd8a0 1012 _("relocation overflow"));
4acf8c78 1013 break;
98c6bbbe 1014 default:
99fcd8a0 1015 as_fatal (_("%s:%u: bad return from bfd_install_relocation"),
4acf8c78 1016 fixp->fx_file, fixp->fx_line);
d5364748
KR
1017 }
1018 }
1019 }
1020 n = i;
1021#endif
1022
1cf7548e
KR
1023#ifdef DEBUG4
1024 {
1025 int i, j, nsyms;
1026 asymbol **sympp;
1027 sympp = bfd_get_outsymbols (stdoutput);
1028 nsyms = bfd_get_symcount (stdoutput);
1029 for (i = 0; i < n; i++)
1030 if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
1031 {
1032 for (j = 0; j < nsyms; j++)
1033 if (sympp[j] == *relocs[i]->sym_ptr_ptr)
1034 break;
1035 if (j == nsyms)
1036 abort ();
1037 }
1038 }
1039#endif
1040
3d3c5039
ILT
1041 if (n)
1042 bfd_set_reloc (stdoutput, sec, relocs, n);
d5364748
KR
1043 else
1044 bfd_set_section_flags (abfd, sec,
80aab579
ILT
1045 (bfd_get_section_flags (abfd, sec)
1046 & (flagword) ~SEC_RELOC));
98c6bbbe
KR
1047
1048#ifdef DEBUG3
d5364748
KR
1049 {
1050 int i;
1051 arelent *r;
1052 asymbol *s;
1053 fprintf (stderr, "relocs for sec %s\n", sec->name);
1054 for (i = 0; i < n; i++)
1055 {
1056 r = relocs[i];
1057 s = *r->sym_ptr_ptr;
1058 fprintf (stderr, " reloc %2d @%08x off %4x : sym %-10s addend %x\n",
1059 i, r, r->address, s->name, r->addend);
1060 }
1061 }
1062#endif
8d6c34a1 1063}
d5364748 1064
8d6c34a1
KR
1065static void
1066write_contents (abfd, sec, xxx)
1067 bfd *abfd;
1068 asection *sec;
a58374d7 1069 PTR xxx;
8d6c34a1
KR
1070{
1071 segment_info_type *seginfo = seg_info (sec);
1072 unsigned long offset = 0;
80aab579 1073 fragS *f;
3d3c5039
ILT
1074
1075 /* Write out the frags. */
f37449aa
ILT
1076 if (seginfo == NULL
1077 || ! (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
d5364748
KR
1078 return;
1079
80aab579
ILT
1080 for (f = seginfo->frchainP->frch_root;
1081 f;
1082 f = f->fr_next)
43ca9aa6
KR
1083 {
1084 int x;
1085 unsigned long fill_size;
1086 char *fill_literal;
1087 long count;
1088
80aab579
ILT
1089 assert (f->fr_type == rs_fill);
1090 if (f->fr_fix)
43ca9aa6
KR
1091 {
1092 x = bfd_set_section_contents (stdoutput, sec,
80aab579
ILT
1093 f->fr_literal, (file_ptr) offset,
1094 (bfd_size_type) f->fr_fix);
1cf7548e
KR
1095 if (x == false)
1096 {
1097 bfd_perror (stdoutput->filename);
99fcd8a0 1098 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
4acf8c78 1099 exit (EXIT_FAILURE);
1cf7548e 1100 }
80aab579 1101 offset += f->fr_fix;
43ca9aa6 1102 }
80aab579
ILT
1103 fill_literal = f->fr_literal + f->fr_fix;
1104 fill_size = f->fr_var;
1105 count = f->fr_offset;
43ca9aa6
KR
1106 assert (count >= 0);
1107 if (fill_size && count)
d9d6f094
KR
1108 {
1109 char buf[256];
305a3af6
SC
1110 if (fill_size > sizeof(buf))
1111 {
1112 /* Do it the old way. Can this ever happen? */
1113 while (count--)
1114 {
1115 x = bfd_set_section_contents (stdoutput, sec,
1116 fill_literal,
1117 (file_ptr) offset,
1118 (bfd_size_type) fill_size);
1119 if (x == false)
1120 {
1121 bfd_perror (stdoutput->filename);
99fcd8a0 1122 as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
305a3af6
SC
1123 exit (EXIT_FAILURE);
1124 }
1125 offset += fill_size;
1126 }
1127 }
1128 else
1129 {
1130 /* Build a buffer full of fill objects and output it as
1131 often as necessary. This saves on the overhead of
1132 potentially lots of bfd_set_section_contents calls. */
1133 int n_per_buf, i;
1134 if (fill_size == 1)
1135 {
1136 n_per_buf = sizeof (buf);
1137 memset (buf, *fill_literal, n_per_buf);
1138 }
1139 else
1140 {
1141 char *bufp;
1142 n_per_buf = sizeof(buf)/fill_size;
1143 for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
1144 memcpy(bufp, fill_literal, fill_size);
1145 }
1146 for (; count > 0; count -= n_per_buf)
1147 {
1148 n_per_buf = n_per_buf > count ? count : n_per_buf;
1149 x = bfd_set_section_contents (stdoutput, sec,
1150 buf, (file_ptr) offset,
1151 (bfd_size_type) n_per_buf * fill_size);
1152 if (x != true)
99fcd8a0 1153 as_fatal (_("Cannot write to output file."));
305a3af6
SC
1154 offset += n_per_buf * fill_size;
1155 }
1156 }
d9d6f094 1157 }
43ca9aa6 1158 }
43ca9aa6
KR
1159}
1160#endif
1161
80aab579 1162#if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
b23f6743
KR
1163static void
1164merge_data_into_text ()
1165{
4064305e 1166#if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
b23f6743
KR
1167 seg_info (text_section)->frchainP->frch_last->fr_next =
1168 seg_info (data_section)->frchainP->frch_root;
1169 seg_info (text_section)->frchainP->frch_last =
1170 seg_info (data_section)->frchainP->frch_last;
1171 seg_info (data_section)->frchainP = 0;
1172#else
1173 fixS *tmp;
1174
1175 text_last_frag->fr_next = data_frag_root;
1176 text_last_frag = data_last_frag;
1177 data_last_frag = NULL;
1178 data_frag_root = NULL;
1179 if (text_fix_root)
1180 {
1181 for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
1182 tmp->fx_next = data_fix_root;
1183 text_fix_tail = data_fix_tail;
1184 }
1185 else
1186 text_fix_root = data_fix_root;
1187 data_fix_root = NULL;
1188#endif
1189}
80aab579 1190#endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT) */
b23f6743
KR
1191
1192#if !defined (BFD_ASSEMBLER) && !defined (BFD)
1193static void
1194relax_and_size_all_segments ()
1195{
13e9182d
KR
1196 fragS *fragP;
1197
b23f6743
KR
1198 relax_segment (text_frag_root, SEG_TEXT);
1199 relax_segment (data_frag_root, SEG_DATA);
1200 relax_segment (bss_frag_root, SEG_BSS);
1201 /*
1202 * Now the addresses of frags are correct within the segment.
1203 */
1204
1205 know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
1206 H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
1207 text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
1208
1209 /*
1210 * Join the 2 segments into 1 huge segment.
1211 * To do this, re-compute every rn_address in the SEG_DATA frags.
1212 * Then join the data frags after the text frags.
1213 *
1214 * Determine a_data [length of data segment].
1215 */
1216 if (data_frag_root)
1217 {
1218 register relax_addressT slide;
1219
1220 know ((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0));
1221
1222 H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
1223 data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
1224 slide = H_GET_TEXT_SIZE (&headers); /* & in file of the data segment. */
1225#ifdef OBJ_BOUT
1226#define RoundUp(N,S) (((N)+(S)-1)&-(S))
1227 /* For b.out: If the data section has a strict alignment
1228 requirement, its load address in the .o file will be
1229 rounded up from the size of the text section. These
1230 two values are *not* the same! Similarly for the bss
1231 section.... */
1232 slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
1233#endif
1234
1235 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
1236 {
1237 fragP->fr_address += slide;
1238 } /* for each data frag */
1239
1240 know (text_last_frag != 0);
1241 text_last_frag->fr_next = data_frag_root;
1242 }
1243 else
1244 {
1245 H_SET_DATA_SIZE (&headers, 0);
1246 }
1247
1248#ifdef OBJ_BOUT
1249 /* See above comments on b.out data section address. */
1250 {
1251 long bss_vma;
1252 if (data_last_frag == 0)
1253 bss_vma = H_GET_TEXT_SIZE (&headers);
1254 else
1255 bss_vma = data_last_frag->fr_address;
1256 bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
1257 bss_address_frag.fr_address = bss_vma;
1258 }
1259#else /* ! OBJ_BOUT */
1260 bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
1261 H_GET_DATA_SIZE (&headers));
1262
efa0c22e 1263#endif /* ! OBJ_BOUT */
b23f6743
KR
1264
1265 /* Slide all the frags */
1266 if (bss_frag_root)
1267 {
1268 relax_addressT slide = bss_address_frag.fr_address;
1269
1270 for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
1271 {
1272 fragP->fr_address += slide;
1273 } /* for each bss frag */
1274 }
1275
b23f6743
KR
1276 if (bss_last_frag)
1277 H_SET_BSS_SIZE (&headers,
1278 bss_last_frag->fr_address - bss_frag_root->fr_address);
1279 else
1280 H_SET_BSS_SIZE (&headers, 0);
1281}
1282#endif /* ! BFD_ASSEMBLER && ! BFD */
1283
d5364748
KR
1284#if defined (BFD_ASSEMBLER) || !defined (BFD)
1285
1b96bdce 1286#ifdef BFD_ASSEMBLER
1cf7548e
KR
1287static void
1288set_symtab ()
1289{
1290 int nsyms;
1291 asymbol **asympp;
1292 symbolS *symp;
1293 boolean result;
1294 extern PTR bfd_alloc PARAMS ((bfd *, size_t));
1295
1296 /* Count symbols. We can't rely on a count made by the loop in
1297 write_object_file, because *_frob_file may add a new symbol or
1298 two. */
1299 nsyms = 0;
1300 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1301 nsyms++;
1302
1303 if (nsyms)
1304 {
1305 int i;
1306
1307 asympp = (asymbol **) bfd_alloc (stdoutput,
1308 nsyms * sizeof (asymbol *));
1309 symp = symbol_rootP;
1310 for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
1311 {
1312 asympp[i] = symp->bsym;
1313 symp->written = 1;
1314 }
1315 }
1316 else
1317 asympp = 0;
1318 result = bfd_set_symtab (stdoutput, asympp, nsyms);
1319 assert (result == true);
1320 symbol_table_frozen = 1;
1321}
1b96bdce 1322#endif
1cf7548e 1323
66a6a381
ILT
1324/* Finish the subsegments. After every sub-segment, we fake an
1325 ".align ...". This conforms to BSD4.2 brane-damage. We then fake
1326 ".fill 0" because that is the kind of frag that requires least
1327 thought. ".align" frags like to have a following frag since that
1328 makes calculating their intended length trivial. */
1329
1330#ifndef SUB_SEGMENT_ALIGN
1331#ifdef BFD_ASSEMBLER
1332#define SUB_SEGMENT_ALIGN(SEG) (0)
1333#else
1334#define SUB_SEGMENT_ALIGN(SEG) (2)
1335#endif
1336#endif
1337
1338void
1339subsegs_finish ()
1340{
1341 struct frchain *frchainP;
1342
1343 for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1344 {
1345 subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1346 frag_align (SUB_SEGMENT_ALIGN (now_seg), NOP_OPCODE, 0);
1347
1348 /* frag_align will have left a new frag.
1349 Use this last frag for an empty ".fill".
1350
1351 For this segment ...
1352 Create a last frag. Do not leave a "being filled in frag". */
1353
1354 frag_wane (frag_now);
1355 frag_now->fr_fix = 0;
1356 know (frag_now->fr_next == NULL);
1357 }
1358}
1359
1360/* Write the object file. */
1361
5d0cd0b8 1362void
6efd877d 1363write_object_file ()
45432836 1364{
58d4951d 1365#if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
741f4d66 1366 fragS *fragP; /* Track along all frags. */
58d4951d 1367#endif
6efd877d 1368
43ca9aa6
KR
1369 /* Do we really want to write it? */
1370 {
1371 int n_warns, n_errs;
1372 n_warns = had_warnings ();
1373 n_errs = had_errors ();
1374 /* The -Z flag indicates that an object file should be generated,
1375 regardless of warnings and errors. */
def66e24 1376 if (flag_always_generate_output)
43ca9aa6
KR
1377 {
1378 if (n_warns || n_errs)
99fcd8a0 1379 as_warn (_("%d error%s, %d warning%s, generating bad object file.\n"),
43ca9aa6
KR
1380 n_errs, n_errs == 1 ? "" : "s",
1381 n_warns, n_warns == 1 ? "" : "s");
1382 }
1383 else
1384 {
1385 if (n_errs)
99fcd8a0 1386 as_fatal (_("%d error%s, %d warning%s, no object file generated.\n"),
43ca9aa6
KR
1387 n_errs, n_errs == 1 ? "" : "s",
1388 n_warns, n_warns == 1 ? "" : "s");
1389 }
1390 }
1391
3eb802b5 1392#ifdef OBJ_VMS
1cf7548e
KR
1393 /* Under VMS we try to be compatible with VAX-11 "C". Thus, we call
1394 a routine to check for the definition of the procedure "_main",
1395 and if so -- fix it up so that it can be program entry point. */
8e86815b 1396 vms_check_for_main ();
3f81f3cf 1397#endif /* OBJ_VMS */
43ca9aa6 1398
43ca9aa6
KR
1399 /* From now on, we don't care about sub-segments. Build one frag chain
1400 for each segment. Linked thru fr_next. */
65bfcf2e 1401
43ca9aa6
KR
1402#ifdef BFD_ASSEMBLER
1403 /* Remove the sections created by gas for its own purposes. */
1404 {
1405 asection **seclist, *sec;
a55774a1
KR
1406 int i;
1407
43ca9aa6
KR
1408 seclist = &stdoutput->sections;
1409 while (seclist && *seclist)
1410 {
1411 sec = *seclist;
5ac34ac3 1412 while (sec == reg_section || sec == expr_section)
43ca9aa6
KR
1413 {
1414 sec = sec->next;
1415 *seclist = sec;
1416 stdoutput->section_count--;
1417 if (!sec)
1418 break;
1419 }
1420 if (*seclist)
1421 seclist = &(*seclist)->next;
1422 }
a55774a1
KR
1423 i = 0;
1424 bfd_map_over_sections (stdoutput, renumber_sections, &i);
43ca9aa6
KR
1425 }
1426
1427 bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
1428#else
6efd877d
KR
1429 remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
1430 remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
1431 remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
43ca9aa6 1432#endif
6efd877d 1433
43ca9aa6
KR
1434 /* We have two segments. If user gave -R flag, then we must put the
1435 data frags into the text segment. Do this before relaxing so
1436 we know to take advantage of -R and make shorter addresses. */
1437#if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
def66e24 1438 if (flag_readonly_data_in_text)
6efd877d 1439 {
b23f6743 1440 merge_data_into_text ();
6efd877d
KR
1441 }
1442#endif
43ca9aa6
KR
1443
1444#ifdef BFD_ASSEMBLER
1445 bfd_map_over_sections (stdoutput, relax_and_size_seg, (char *) 0);
1446#else
b23f6743 1447 relax_and_size_all_segments ();
43ca9aa6 1448#endif /* BFD_ASSEMBLER */
65bfcf2e 1449
43ca9aa6 1450#ifndef BFD_ASSEMBLER
6efd877d 1451 /*
7f2cb270
KR
1452 *
1453 * Crawl the symbol chain.
1454 *
1455 * For each symbol whose value depends on a frag, take the address of
1456 * that frag and subsume it into the value of the symbol.
1457 * After this, there is just one way to lookup a symbol value.
1458 * Values are left in their final state for object file emission.
1459 * We adjust the values of 'L' local symbols, even if we do
1460 * not intend to emit them to the object file, because their values
1461 * are needed for fix-ups.
1462 *
1463 * Unless we saw a -L flag, remove all symbols that begin with 'L'
1464 * from the symbol chain. (They are still pointed to by the fixes.)
1465 *
1466 * Count the remaining symbols.
1467 * Assign a symbol number to each symbol.
1468 * Count the number of string-table chars we will emit.
1469 * Put this info into the headers as appropriate.
1470 *
1471 */
6efd877d
KR
1472 know (zero_address_frag.fr_address == 0);
1473 string_byte_count = sizeof (string_byte_count);
1474
1475 obj_crawl_symbol_chain (&headers);
1476
1477 if (string_byte_count == sizeof (string_byte_count))
43ca9aa6 1478 string_byte_count = 0;
6efd877d
KR
1479
1480 H_SET_STRING_SIZE (&headers, string_byte_count);
1481
1482 /*
7f2cb270
KR
1483 * Addresses of frags now reflect addresses we use in the object file.
1484 * Symbol values are correct.
1485 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1486 * Also converting any machine-dependent frags using md_convert_frag();
1487 */
6efd877d
KR
1488 subseg_change (SEG_TEXT, 0);
1489
1490 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1491 {
7799bb2c
ILT
1492 /* At this point we have linked all the frags into a single
1493 chain. However, cvt_frag_to_fill may call md_convert_frag
1494 which may call fix_new. We need to ensure that fix_new adds
1495 the fixup to the right section. */
1496 if (fragP == data_frag_root)
1497 subseg_change (SEG_DATA, 0);
1498
d4083e29 1499 cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
6efd877d 1500
43ca9aa6
KR
1501 /* Some assert macros don't work with # directives mixed in. */
1502#ifndef NDEBUG
1503 if (!(fragP->fr_next == NULL
ebfb4167 1504#ifdef OBJ_BOUT
43ca9aa6 1505 || fragP->fr_next == data_frag_root
ebfb4167 1506#endif
6efd877d 1507 || ((fragP->fr_next->fr_address - fragP->fr_address)
43ca9aa6
KR
1508 == (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
1509 abort ();
1510#endif
1511 }
1512#endif /* ! BFD_ASSEMBLER */
6efd877d 1513
fecd2382 1514#ifndef WORKING_DOT_WORD
6efd877d
KR
1515 {
1516 struct broken_word *lie;
1517 struct broken_word **prevP;
1518
1519 prevP = &broken_words;
1520 for (lie = broken_words; lie; lie = lie->next_broken_word)
1521 if (!lie->added)
a39116f1 1522 {
5ac34ac3
ILT
1523 expressionS exp;
1524
4ea49053 1525 subseg_change (lie->seg, lie->subseg);
5ac34ac3
ILT
1526 exp.X_op = O_subtract;
1527 exp.X_add_symbol = lie->add;
1528 exp.X_op_symbol = lie->sub;
1529 exp.X_add_number = lie->addnum;
43ca9aa6 1530#ifdef BFD_ASSEMBLER
d9d6f094
KR
1531#ifdef TC_CONS_FIX_NEW
1532 TC_CONS_FIX_NEW (lie->frag,
1533 lie->word_goes_here - lie->frag->fr_literal,
1534 2, &exp);
1535#else
5ac34ac3
ILT
1536 fix_new_exp (lie->frag,
1537 lie->word_goes_here - lie->frag->fr_literal,
5d0cd0b8 1538 2, &exp, 0, BFD_RELOC_16);
d9d6f094 1539#endif
43ca9aa6
KR
1540#else
1541#if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
5ac34ac3
ILT
1542 fix_new_exp (lie->frag,
1543 lie->word_goes_here - lie->frag->fr_literal,
1544 2, &exp, 0, NO_RELOC);
43ca9aa6 1545#else
fecd2382 1546#ifdef TC_NS32K
5ac34ac3
ILT
1547 fix_new_ns32k_exp (lie->frag,
1548 lie->word_goes_here - lie->frag->fr_literal,
1549 2, &exp, 0, 0, 2, 0, 0);
343fb08d 1550#else
5ac34ac3
ILT
1551 fix_new_exp (lie->frag,
1552 lie->word_goes_here - lie->frag->fr_literal,
1553 2, &exp, 0, 0);
fecd2382 1554#endif /* TC_NS32K */
43ca9aa6
KR
1555#endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE */
1556#endif /* BFD_ASSEMBLER */
6efd877d 1557 *prevP = lie->next_broken_word;
a39116f1 1558 }
6efd877d
KR
1559 else
1560 prevP = &(lie->next_broken_word);
1561
1562 for (lie = broken_words; lie;)
1563 {
1564 struct broken_word *untruth;
1565 char *table_ptr;
d5364748
KR
1566 addressT table_addr;
1567 addressT from_addr, to_addr;
6efd877d
KR
1568 int n, m;
1569
4ea49053 1570 subseg_change (lie->seg, lie->subseg);
6efd877d
KR
1571 fragP = lie->dispfrag;
1572
43ca9aa6 1573 /* Find out how many broken_words go here. */
6efd877d
KR
1574 n = 0;
1575 for (untruth = lie; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
1576 if (untruth->added == 1)
1577 n++;
1578
1579 table_ptr = lie->dispfrag->fr_opcode;
1580 table_addr = lie->dispfrag->fr_address + (table_ptr - lie->dispfrag->fr_literal);
43ca9aa6
KR
1581 /* Create the jump around the long jumps. This is a short
1582 jump from table_ptr+0 to table_ptr+n*long_jump_size. */
6efd877d
KR
1583 from_addr = table_addr;
1584 to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1585 md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
1586 table_ptr += md_short_jump_size;
1587 table_addr += md_short_jump_size;
1588
1589 for (m = 0; lie && lie->dispfrag == fragP; m++, lie = lie->next_broken_word)
1590 {
1591 if (lie->added == 2)
1592 continue;
1593 /* Patch the jump table */
1594 /* This is the offset from ??? to table_ptr+0 */
d5364748 1595 to_addr = table_addr - S_GET_VALUE (lie->sub);
d9d6f094
KR
1596#ifdef BFD_ASSEMBLER
1597 to_addr -= lie->sub->sy_frag->fr_address;
1598#endif
6efd877d
KR
1599 md_number_to_chars (lie->word_goes_here, to_addr, 2);
1600 for (untruth = lie->next_broken_word; untruth && untruth->dispfrag == fragP; untruth = untruth->next_broken_word)
1601 {
1602 if (untruth->use_jump == lie)
1603 md_number_to_chars (untruth->word_goes_here, to_addr, 2);
1604 }
1605
1606 /* Install the long jump */
1607 /* this is a long jump from table_ptr+0 to the final target */
1608 from_addr = table_addr;
1609 to_addr = S_GET_VALUE (lie->add) + lie->addnum;
d9d6f094
KR
1610#ifdef BFD_ASSEMBLER
1611 to_addr += lie->add->sy_frag->fr_address;
1612#endif
6efd877d
KR
1613 md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
1614 table_ptr += md_long_jump_size;
1615 table_addr += md_long_jump_size;
1616 }
1617 }
1618 }
fecd2382 1619#endif /* not WORKING_DOT_WORD */
6efd877d 1620
43ca9aa6 1621#ifndef BFD_ASSEMBLER
3eb802b5 1622#ifndef OBJ_VMS
6efd877d 1623 { /* not vms */
3f81f3cf 1624 char *the_object_file;
1cf7548e 1625 long object_file_size;
6efd877d 1626 /*
3eb802b5
ILT
1627 * Scan every FixS performing fixups. We had to wait until now to do
1628 * this because md_convert_frag() may have made some fixSs.
1629 */
6efd877d
KR
1630 int trsize, drsize;
1631
1632 subseg_change (SEG_TEXT, 0);
d5364748 1633 trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
6efd877d 1634 subseg_change (SEG_DATA, 0);
d5364748 1635 drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
6efd877d
KR
1636 H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
1637
1638 /* FIXME move this stuff into the pre-write-hook */
1639 H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
1640 H_SET_ENTRY_POINT (&headers, 0);
1641
1642 obj_pre_write_hook (&headers); /* extra coff stuff */
6efd877d
KR
1643
1644 object_file_size = H_GET_FILE_SIZE (&headers);
1645 next_object_file_charP = the_object_file = xmalloc (object_file_size);
1646
1647 output_file_create (out_file_name);
1648
1649 obj_header_append (&next_object_file_charP, &headers);
1650
1651 know ((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE (&headers));
1652
1653 /*
43ca9aa6
KR
1654 * Emit code.
1655 */
6efd877d
KR
1656 for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
1657 {
1658 register long count;
1659 register char *fill_literal;
1660 register long fill_size;
1661
c151fd1e 1662 PROGRESS (1);
6efd877d
KR
1663 know (fragP->fr_type == rs_fill);
1664 append (&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix);
1665 fill_literal = fragP->fr_literal + fragP->fr_fix;
1666 fill_size = fragP->fr_var;
1667 know (fragP->fr_offset >= 0);
1668
1669 for (count = fragP->fr_offset; count; count--)
1670 {
1671 append (&next_object_file_charP, fill_literal, (unsigned long) fill_size);
1672 } /* for each */
1673
1674 } /* for each code frag. */
1675
1676 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers)));
1677
1678 /*
43ca9aa6
KR
1679 * Emit relocations.
1680 */
6efd877d
KR
1681 obj_emit_relocations (&next_object_file_charP, text_fix_root, (relax_addressT) 0);
1682 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers)));
fecd2382 1683#ifdef TC_I960
6efd877d 1684 /* Make addresses in data relocation directives relative to beginning of
43ca9aa6
KR
1685 * first data fragment, not end of last text fragment: alignment of the
1686 * start of the data segment may place a gap between the segments.
1687 */
6efd877d 1688 obj_emit_relocations (&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
fecd2382 1689#else /* TC_I960 */
6efd877d 1690 obj_emit_relocations (&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
fecd2382 1691#endif /* TC_I960 */
6efd877d
KR
1692
1693 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers)));
1694
1695 /*
43ca9aa6
KR
1696 * Emit line number entries.
1697 */
6efd877d
KR
1698 OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1699 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers) + H_GET_LINENO_SIZE (&headers)));
1700
1701 /*
3eb802b5
ILT
1702 * Emit symbols.
1703 */
6efd877d
KR
1704 obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1705 know ((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE (&headers) + H_GET_TEXT_SIZE (&headers) + H_GET_DATA_SIZE (&headers) + H_GET_TEXT_RELOCATION_SIZE (&headers) + H_GET_DATA_RELOCATION_SIZE (&headers) + H_GET_LINENO_SIZE (&headers) + H_GET_SYMBOL_TABLE_SIZE (&headers)));
1706
1707 /*
3eb802b5
ILT
1708 * Emit strings.
1709 */
6efd877d
KR
1710
1711 if (string_byte_count > 0)
1712 {
1713 obj_emit_strings (&next_object_file_charP);
1714 } /* only if we have a string table */
1715
45432836 1716#ifdef BFD_HEADERS
6efd877d
KR
1717 bfd_seek (stdoutput, 0, 0);
1718 bfd_write (the_object_file, 1, object_file_size, stdoutput);
45432836 1719#else
6efd877d
KR
1720
1721 /* Write the data to the file */
1722 output_file_append (the_object_file, object_file_size, out_file_name);
3f81f3cf 1723 free (the_object_file);
45432836 1724#endif
6efd877d 1725 } /* non vms output */
3f81f3cf 1726#else /* OBJ_VMS */
6efd877d 1727 /*
3eb802b5
ILT
1728 * Now do the VMS-dependent part of writing the object file
1729 */
8e86815b 1730 vms_write_object_file (H_GET_TEXT_SIZE (&headers),
85825401
ILT
1731 H_GET_DATA_SIZE (&headers),
1732 H_GET_BSS_SIZE (&headers),
3eb802b5 1733 text_frag_root, data_frag_root);
3f81f3cf 1734#endif /* OBJ_VMS */
43ca9aa6 1735#else /* BFD_ASSEMBLER */
6efd877d 1736
4acf8c78
KR
1737 /* Resolve symbol values. This needs to be done before processing
1738 the relocations. */
1739 if (symbol_rootP)
1740 {
1741 symbolS *symp;
1742
1743 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1744 if (!symp->sy_resolved)
66a6a381 1745 resolve_symbol_value (symp, 1);
4acf8c78
KR
1746 }
1747
c151fd1e
KR
1748 PROGRESS (1);
1749
5d0cd0b8
ILT
1750#ifdef tc_frob_file_before_adjust
1751 tc_frob_file_before_adjust ();
1752#endif
1753#ifdef obj_frob_file_before_adjust
1754 obj_frob_file_before_adjust ();
1755#endif
1756
d5364748
KR
1757 bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *)0);
1758
43ca9aa6
KR
1759 /* Set up symbol table, and write it out. */
1760 if (symbol_rootP)
1761 {
43ca9aa6
KR
1762 symbolS *symp;
1763
1764 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1765 {
262b22cd 1766 int punt = 0;
c151fd1e
KR
1767 const char *name;
1768
1356d77d
ILT
1769 if (symp->sy_mri_common)
1770 {
1771 if (S_IS_EXTERNAL (symp))
99fcd8a0 1772 as_bad (_("%s: global symbols not supported in common sections"),
1356d77d
ILT
1773 S_GET_NAME (symp));
1774 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1775 continue;
1776 }
1777
c151fd1e
KR
1778 name = S_GET_NAME (symp);
1779 if (name)
1780 {
a2a5a4fa 1781 const char *name2 = decode_local_label_name ((char *)S_GET_NAME (symp));
c151fd1e
KR
1782 /* They only differ if `name' is a fb or dollar local
1783 label name. */
1784 if (name2 != name && ! S_IS_DEFINED (symp))
99fcd8a0 1785 as_bad (_("local label %s is not defined"), name2);
c151fd1e 1786 }
262b22cd 1787
4acf8c78
KR
1788 /* Do it again, because adjust_reloc_syms might introduce
1789 more symbols. They'll probably only be section symbols,
1790 but they'll still need to have the values computed. */
5868b1fe
ILT
1791 if (! symp->sy_resolved)
1792 {
5ac34ac3 1793 if (symp->sy_value.X_op == O_constant)
5868b1fe
ILT
1794 {
1795 /* This is the normal case; skip the call. */
1796 S_SET_VALUE (symp,
1797 (S_GET_VALUE (symp)
1798 + symp->sy_frag->fr_address));
1799 symp->sy_resolved = 1;
1800 }
1801 else
66a6a381 1802 resolve_symbol_value (symp, 1);
5868b1fe
ILT
1803 }
1804
a043f579
ILT
1805 /* Skip symbols which were equated to undefined or common
1806 symbols. */
1807 if (symp->sy_value.X_op == O_symbol
1808 && (! S_IS_DEFINED (symp) || S_IS_COMMON (symp)))
1809 {
1810 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1811 continue;
1812 }
1813
43ca9aa6
KR
1814 /* So far, common symbols have been treated like undefined symbols.
1815 Put them in the common section now. */
1816 if (S_IS_DEFINED (symp) == 0
1817 && S_GET_VALUE (symp) != 0)
d9d6f094 1818 S_SET_SEGMENT (symp, bfd_com_section_ptr);
43ca9aa6 1819#if 0
5868b1fe 1820 printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
43ca9aa6
KR
1821 S_GET_NAME (symp), symp,
1822 S_GET_VALUE (symp),
d5364748 1823 symp->bsym->flags,
43ca9aa6
KR
1824 segment_name (symp->bsym->section));
1825#endif
335d35c8 1826
80aab579 1827#ifdef obj_frob_symbol
262b22cd 1828 obj_frob_symbol (symp, punt);
43ca9aa6
KR
1829#endif
1830#ifdef tc_frob_symbol
262b22cd 1831 if (! punt || symp->sy_used_in_reloc)
335d35c8 1832 tc_frob_symbol (symp, punt);
43ca9aa6 1833#endif
80aab579 1834
262b22cd
ILT
1835 /* If we don't want to keep this symbol, splice it out of
1836 the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
1837 want section symbols. Otherwise, we skip local symbols
1838 and symbols that the frob_symbol macros told us to punt,
1839 but we keep such symbols if they are used in relocs. */
1840 if ((! EMIT_SECTION_SYMBOLS
1841 && (symp->bsym->flags & BSF_SECTION_SYM) != 0)
1cf7548e
KR
1842 /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
1843 opposites. Sometimes the former checks flags and the
1844 latter examines the name... */
1845 || (!S_IS_EXTERN (symp)
1846 && (S_IS_LOCAL (symp) || punt)
262b22cd 1847 && ! symp->sy_used_in_reloc))
43ca9aa6 1848 {
1cf7548e
KR
1849 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1850 /* After symbol_remove, symbol_next(symp) still returns
1851 the one that came after it in the chain. So we don't
1852 need to do any extra cleanup work here. */
262b22cd 1853
262b22cd 1854 continue;
43ca9aa6 1855 }
85051959 1856
80aab579
ILT
1857 /* Make sure we really got a value for the symbol. */
1858 if (! symp->sy_resolved)
1859 {
99fcd8a0 1860 as_bad (_("can't resolve value for symbol \"%s\""),
80aab579
ILT
1861 S_GET_NAME (symp));
1862 symp->sy_resolved = 1;
1863 }
1864
85051959
ILT
1865 /* Set the value into the BFD symbol. Up til now the value
1866 has only been kept in the gas symbolS struct. */
1867 symp->bsym->value = S_GET_VALUE (symp);
43ca9aa6
KR
1868 }
1869 }
1870
c151fd1e
KR
1871 PROGRESS (1);
1872
def66e24
DM
1873 /* Now do any format-specific adjustments to the symbol table, such
1874 as adding file symbols. */
a043f579
ILT
1875#ifdef tc_adjust_symtab
1876 tc_adjust_symtab ();
1877#endif
def66e24
DM
1878#ifdef obj_adjust_symtab
1879 obj_adjust_symtab ();
1880#endif
1881
1882 /* Now that all the sizes are known, and contents correct, we can
1883 start writing to the file. */
1884 set_symtab ();
1885
1cf7548e
KR
1886 /* If *_frob_file changes the symbol value at this point, it is
1887 responsible for moving the changed value into symp->bsym->value
1888 as well. Hopefully all symbol value changing can be done in
1889 *_frob_symbol. */
c79e67a3
KR
1890#ifdef tc_frob_file
1891 tc_frob_file ();
1892#endif
f2f7d044
ILT
1893#ifdef obj_frob_file
1894 obj_frob_file ();
1895#endif
1896
8d6c34a1
KR
1897 bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
1898
5d0cd0b8
ILT
1899#ifdef tc_frob_file_after_relocs
1900 tc_frob_file_after_relocs ();
1901#endif
1902#ifdef obj_frob_file_after_relocs
1903 obj_frob_file_after_relocs ();
1904#endif
1905
43ca9aa6 1906 bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
43ca9aa6
KR
1907#endif /* BFD_ASSEMBLER */
1908}
d5364748 1909#endif /* ! BFD */
fecd2382
RP
1910
1911/*
1912 * relax_segment()
1913 *
1914 * Now we have a segment, not a crowd of sub-segments, we can make fr_address
1915 * values.
1916 *
1917 * Relax the frags.
1918 *
1919 * After this, all frags in this segment have addresses that are correct
1920 * within the segment. Since segments live in different file addresses,
1921 * these frag addresses may not be the same as final object-file addresses.
1922 */
45432836 1923
793f9558 1924#ifdef TC_GENERIC_RELAX_TABLE
a58374d7 1925
374bdac7 1926static int is_dnrange PARAMS ((fragS *, fragS *));
374bdac7 1927
d5364748 1928/* Subroutines of relax_segment. */
5d0cd0b8 1929static int
43ca9aa6 1930is_dnrange (f1, f2)
374bdac7
DE
1931 fragS *f1;
1932 fragS *f2;
43ca9aa6
KR
1933{
1934 for (; f1; f1 = f1->fr_next)
1935 if (f1->fr_next == f2)
1936 return 1;
1937 return 0;
1938}
1939
f7ab9441
DE
1940/* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
1941
7799bb2c 1942long
f7ab9441
DE
1943relax_frag (fragP, stretch)
1944 fragS *fragP;
1945 long stretch;
1946{
1947 const relax_typeS *this_type;
1948 const relax_typeS *start_type;
1949 relax_substateT next_state;
1950 relax_substateT this_state;
1951 long aim, target, growth;
1952 symbolS *symbolP = fragP->fr_symbol;
1953 long offset = fragP->fr_offset;
1954 /* Recompute was_address by undoing "+= stretch" done by relax_segment. */
1955 unsigned long was_address = fragP->fr_address - stretch;
1956 unsigned long address = fragP->fr_address;
1957 const relax_typeS *table = TC_GENERIC_RELAX_TABLE;
1958
1959 this_state = fragP->fr_subtype;
1960 start_type = this_type = table + this_state;
1961 target = offset;
1962
1963 if (symbolP)
1964 {
1965#ifndef DIFF_EXPR_OK
1966#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
1967 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
1968 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
1969 || (S_GET_SEGMENT (symbolP) == SEG_BSS)
1970 || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
1971#endif
1972 know (symbolP->sy_frag);
1973#endif
1974 know (!(S_GET_SEGMENT (symbolP) == absolute_section)
1975 || symbolP->sy_frag == &zero_address_frag);
1976 target +=
1977 S_GET_VALUE (symbolP)
1978 + symbolP->sy_frag->fr_address;
1979
1980 /* If frag has yet to be reached on this pass,
1981 assume it will move by STRETCH just as we did.
1982 If this is not so, it will be because some frag
1983 between grows, and that will force another pass.
1984
1985 Beware zero-length frags.
1986
1987 There should be a faster way to do this. */
1988
1989 if (symbolP->sy_frag->fr_address >= was_address
1990 && is_dnrange (fragP, symbolP->sy_frag))
1991 {
1992 target += stretch;
1993 }
1994 }
1995
1996 aim = target - address - fragP->fr_fix;
1997#ifdef TC_PCREL_ADJUST
1998 /* Currently only the ns32k family needs this */
1999 aim += TC_PCREL_ADJUST(fragP);
2000/*#else*/
2001 /* This machine doesn't want to use pcrel_adjust.
2002 In that case, pcrel_adjust should be zero. */
374bdac7 2003/* assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);*/
f7ab9441
DE
2004#endif
2005#ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE */
2006 md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2007#endif
2008
2009 if (aim < 0)
2010 {
2011 /* Look backwards. */
2012 for (next_state = this_type->rlx_more; next_state;)
2013 if (aim >= this_type->rlx_backward)
2014 next_state = 0;
2015 else
2016 {
2017 /* Grow to next state. */
2018 this_state = next_state;
2019 this_type = table + this_state;
2020 next_state = this_type->rlx_more;
2021 }
2022 }
2023 else
2024 {
2025 /* Look forwards. */
2026 for (next_state = this_type->rlx_more; next_state;)
2027 if (aim <= this_type->rlx_forward)
2028 next_state = 0;
2029 else
2030 {
2031 /* Grow to next state. */
2032 this_state = next_state;
2033 this_type = table + this_state;
2034 next_state = this_type->rlx_more;
2035 }
2036 }
2037
2038 growth = this_type->rlx_length - start_type->rlx_length;
2039 if (growth != 0)
2040 fragP->fr_subtype = this_state;
2041 return growth;
2042}
2043
793f9558 2044#endif /* defined (TC_GENERIC_RELAX_TABLE) */
a58374d7 2045
43ca9aa6 2046/* Relax_align. Advance location counter to next address that has 'alignment'
d5364748 2047 lowest order bits all 0s, return size of adjustment made. */
43ca9aa6
KR
2048static relax_addressT
2049relax_align (address, alignment)
2050 register relax_addressT address; /* Address now. */
d5364748 2051 register int alignment; /* Alignment (binary). */
43ca9aa6
KR
2052{
2053 relax_addressT mask;
2054 relax_addressT new_address;
2055
2056 mask = ~((~0) << alignment);
2057 new_address = (address + mask) & (~mask);
d4083e29 2058#ifdef LINKER_RELAXING_SHRINKS_ONLY
43ca9aa6
KR
2059 if (linkrelax)
2060 /* We must provide lots of padding, so the linker can discard it
2061 when needed. The linker will not add extra space, ever. */
2062 new_address += (1 << alignment);
d4083e29 2063#endif
43ca9aa6
KR
2064 return (new_address - address);
2065}
45432836 2066
5d0cd0b8 2067void
6efd877d
KR
2068relax_segment (segment_frag_root, segment)
2069 struct frag *segment_frag_root;
43ca9aa6 2070 segT segment;
fecd2382 2071{
6efd877d
KR
2072 register struct frag *fragP;
2073 register relax_addressT address;
43ca9aa6 2074#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
6efd877d 2075 know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
45432836 2076#endif
6efd877d
KR
2077 /* In case md_estimate_size_before_relax() wants to make fixSs. */
2078 subseg_change (segment, 0);
2079
7f2cb270
KR
2080 /* For each frag in segment: count and store (a 1st guess of)
2081 fr_address. */
6efd877d
KR
2082 address = 0;
2083 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2084 {
2085 fragP->fr_address = address;
2086 address += fragP->fr_fix;
2087
2088 switch (fragP->fr_type)
2089 {
2090 case rs_fill:
2091 address += fragP->fr_offset * fragP->fr_var;
2092 break;
2093
2094 case rs_align:
cd3b81bd 2095 case rs_align_code:
98c6bbbe 2096 {
590c50d8
ILT
2097 addressT offset = relax_align (address, (int) fragP->fr_offset);
2098
2099 if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2100 offset = 0;
2101
98c6bbbe
KR
2102 if (offset % fragP->fr_var != 0)
2103 {
99fcd8a0 2104 as_bad (_("alignment padding (%lu bytes) not a multiple of %ld"),
590c50d8 2105 (unsigned long) offset, (long) fragP->fr_var);
98c6bbbe
KR
2106 offset -= (offset % fragP->fr_var);
2107 }
590c50d8 2108
98c6bbbe
KR
2109 address += offset;
2110 }
6efd877d
KR
2111 break;
2112
2113 case rs_org:
cd3b81bd 2114 case rs_space:
7f2cb270 2115 /* Assume .org is nugatory. It will grow with 1st relax. */
6efd877d
KR
2116 break;
2117
2118 case rs_machine_dependent:
2119 address += md_estimate_size_before_relax (fragP, segment);
2120 break;
2121
fecd2382 2122#ifndef WORKING_DOT_WORD
6efd877d
KR
2123 /* Broken words don't concern us yet */
2124 case rs_broken_word:
2125 break;
fecd2382 2126#endif
6efd877d 2127
66a6a381
ILT
2128 case rs_leb128:
2129 /* Initial guess is always 1; doing otherwise can result in
2130 stable solutions that are larger than the minimum. */
2131 address += fragP->fr_offset = 1;
2132 break;
2133
99fcd8a0
MM
2134 case rs_cfa:
2135 address += eh_frame_estimate_size_before_relax (fragP);
2136 break;
2137
6efd877d
KR
2138 default:
2139 BAD_CASE (fragP->fr_type);
2140 break;
2141 } /* switch(fr_type) */
2142 } /* for each frag in the segment */
2143
7f2cb270 2144 /* Do relax(). */
6efd877d 2145 {
7f2cb270 2146 long stretch; /* May be any size, 0 or negative. */
6efd877d
KR
2147 /* Cumulative number of addresses we have */
2148 /* relaxed this pass. */
2149 /* We may have relaxed more than one address. */
7f2cb270
KR
2150 long stretched; /* Have we stretched on this pass? */
2151 /* This is 'cuz stretch may be zero, when, in fact some piece of code
2152 grew, and another shrank. If a branch instruction doesn't fit anymore,
2153 we could be scrod. */
6efd877d
KR
2154
2155 do
2156 {
2157 stretch = stretched = 0;
2158 for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2159 {
7f2cb270 2160 long growth = 0;
590c50d8
ILT
2161 addressT was_address;
2162 offsetT offset;
7f2cb270 2163 symbolS *symbolP;
6efd877d
KR
2164
2165 was_address = fragP->fr_address;
2166 address = fragP->fr_address += stretch;
2167 symbolP = fragP->fr_symbol;
2168 offset = fragP->fr_offset;
6efd877d
KR
2169
2170 switch (fragP->fr_type)
2171 {
2172 case rs_fill: /* .fill never relaxes. */
2173 growth = 0;
2174 break;
2175
fecd2382 2176#ifndef WORKING_DOT_WORD
6efd877d 2177 /* JF: This is RMS's idea. I do *NOT* want to be blamed
7f2cb270
KR
2178 for it I do not want to write it. I do not want to have
2179 anything to do with it. This is not the proper way to
2180 implement this misfeature. */
6efd877d
KR
2181 case rs_broken_word:
2182 {
2183 struct broken_word *lie;
2184 struct broken_word *untruth;
6efd877d
KR
2185
2186 /* Yes this is ugly (storing the broken_word pointer
7f2cb270
KR
2187 in the symbol slot). Still, this whole chunk of
2188 code is ugly, and I don't feel like doing anything
2189 about it. Think of it as stubbornness in action. */
6efd877d
KR
2190 growth = 0;
2191 for (lie = (struct broken_word *) (fragP->fr_symbol);
2192 lie && lie->dispfrag == fragP;
2193 lie = lie->next_broken_word)
2194 {
2195
2196 if (lie->added)
2197 continue;
2198
7f2cb270
KR
2199 offset = (lie->add->sy_frag->fr_address
2200 + S_GET_VALUE (lie->add)
2201 + lie->addnum
2202 - (lie->sub->sy_frag->fr_address
2203 + S_GET_VALUE (lie->sub)));
6efd877d
KR
2204 if (offset <= -32768 || offset >= 32767)
2205 {
def66e24 2206 if (flag_warn_displacement)
d5364748
KR
2207 {
2208 char buf[50];
80aab579 2209 sprint_value (buf, (addressT) lie->addnum);
99fcd8a0 2210 as_warn (_(".word %s-%s+%s didn't fit"),
d5364748
KR
2211 S_GET_NAME (lie->add),
2212 S_GET_NAME (lie->sub),
2213 buf);
2214 }
6efd877d
KR
2215 lie->added = 1;
2216 if (fragP->fr_subtype == 0)
2217 {
2218 fragP->fr_subtype++;
2219 growth += md_short_jump_size;
2220 }
7f2cb270
KR
2221 for (untruth = lie->next_broken_word;
2222 untruth && untruth->dispfrag == lie->dispfrag;
2223 untruth = untruth->next_broken_word)
6efd877d
KR
2224 if ((untruth->add->sy_frag == lie->add->sy_frag)
2225 && S_GET_VALUE (untruth->add) == S_GET_VALUE (lie->add))
2226 {
2227 untruth->added = 2;
2228 untruth->use_jump = lie;
2229 }
2230 growth += md_long_jump_size;
2231 }
2232 }
2233
2234 break;
2235 } /* case rs_broken_word */
fecd2382 2236#endif
6efd877d 2237 case rs_align:
cd3b81bd 2238 case rs_align_code:
590c50d8
ILT
2239 {
2240 addressT oldoff, newoff;
2241
2242 oldoff = relax_align (was_address + fragP->fr_fix,
2243 (int) offset);
2244 newoff = relax_align (address + fragP->fr_fix,
2245 (int) offset);
2246
2247 if (fragP->fr_subtype != 0)
2248 {
2249 if (oldoff > fragP->fr_subtype)
2250 oldoff = 0;
2251 if (newoff > fragP->fr_subtype)
2252 newoff = 0;
2253 }
2254
2255 growth = newoff - oldoff;
2256 }
6efd877d
KR
2257 break;
2258
2259 case rs_org:
f7ab9441
DE
2260 {
2261 long target = offset;
2262 long after;
6efd877d 2263
f7ab9441
DE
2264 if (symbolP)
2265 {
43ca9aa6 2266#if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
f7ab9441
DE
2267 know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2268 || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2269 || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
2270 || S_GET_SEGMENT (symbolP) == SEG_BSS);
2271 know (symbolP->sy_frag);
2272 know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2273 || (symbolP->sy_frag == &zero_address_frag));
2274#endif
2275 target += S_GET_VALUE (symbolP)
2276 + symbolP->sy_frag->fr_address;
2277 } /* if we have a symbol */
6efd877d 2278
f7ab9441
DE
2279 know (fragP->fr_next);
2280 after = fragP->fr_next->fr_address;
2281 growth = target - after;
2282 if (growth < 0)
2283 {
2284 /* Growth may be negative, but variable part of frag
2285 cannot have fewer than 0 chars. That is, we can't
2286 .org backwards. */
99fcd8a0 2287 as_bad (_("attempt to .org backwards ignored"));
f7ab9441
DE
2288 growth = 0;
2289 }
2290
2291 growth -= stretch; /* This is an absolute growth factor */
2292 break;
2293 }
6efd877d 2294
cd3b81bd
KR
2295 case rs_space:
2296 if (symbolP)
2297 {
2298 growth = S_GET_VALUE (symbolP);
a75abb6f
ILT
2299 if (symbolP->sy_frag != &zero_address_frag
2300 || S_IS_COMMON (symbolP)
2301 || ! S_IS_DEFINED (symbolP))
2302 as_bad_where (fragP->fr_file, fragP->fr_line,
99fcd8a0 2303 _(".space specifies non-absolute value"));
cd3b81bd
KR
2304 fragP->fr_symbol = 0;
2305 if (growth < 0)
2306 {
99fcd8a0 2307 as_warn (_(".space or .fill with negative value, ignored"));
cd3b81bd
KR
2308 growth = 0;
2309 }
2310 }
2311 else
2312 growth = 0;
2313 break;
2314
6efd877d 2315 case rs_machine_dependent:
a58374d7
ILT
2316#ifdef md_relax_frag
2317 growth = md_relax_frag (fragP, stretch);
2318#else
c151fd1e 2319#ifdef TC_GENERIC_RELAX_TABLE
a58374d7 2320 /* The default way to relax a frag is to look through
f7ab9441
DE
2321 TC_GENERIC_RELAX_TABLE. */
2322 growth = relax_frag (fragP, stretch);
c151fd1e 2323#endif /* TC_GENERIC_RELAX_TABLE */
a58374d7 2324#endif
7f2cb270 2325 break;
6efd877d 2326
66a6a381
ILT
2327 case rs_leb128:
2328 {
2329 valueT value;
2330 int size;
2331
2332 value = resolve_symbol_value (fragP->fr_symbol, 0);
2333 size = sizeof_leb128 (value, fragP->fr_subtype);
2334 growth = size - fragP->fr_offset;
2335 fragP->fr_offset = size;
2336 }
2337 break;
2338
99fcd8a0
MM
2339 case rs_cfa:
2340 growth = eh_frame_relax_frag (fragP);
2341 break;
2342
6efd877d
KR
2343 default:
2344 BAD_CASE (fragP->fr_type);
2345 break;
2346 }
2347 if (growth)
2348 {
2349 stretch += growth;
2350 stretched++;
2351 }
2352 } /* For each frag in the segment. */
2353 }
2354 while (stretched); /* Until nothing further to relax. */
2355 } /* do_relax */
2356
2357 /*
7f2cb270
KR
2358 * We now have valid fr_address'es for each frag.
2359 */
6efd877d
KR
2360
2361 /*
7f2cb270
KR
2362 * All fr_address's are correct, relative to their own segment.
2363 * We have made all the fixS we will ever make.
2364 */
6efd877d 2365} /* relax_segment() */
fecd2382 2366
3f81f3cf 2367#if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
80aab579 2368
d9d6f094
KR
2369#ifndef TC_RELOC_RTSYM_LOC_FIXUP
2370#define TC_RELOC_RTSYM_LOC_FIXUP(X) (1)
2371#endif
2372
fecd2382 2373/* fixup_segment()
6efd877d 2374
fecd2382
RP
2375 Go through all the fixS's in a segment and see which ones can be
2376 handled now. (These consist of fixS where we have since discovered
2377 the value of a symbol, or the address of the frag involved.)
2378 For each one, call md_apply_fix to put the fix into the frag data.
6efd877d 2379
fecd2382
RP
2380 Result is a count of how many relocation structs will be needed to
2381 handle the remaining fixS's that we couldn't completely handle here.
2382 These will be output later by emit_relocations(). */
2383
09952cd9 2384static long
6efd877d 2385fixup_segment (fixP, this_segment_type)
09952cd9 2386 register fixS *fixP;
6efd877d 2387 segT this_segment_type; /* N_TYPE bits for segment. */
fecd2382 2388{
f5c32424
KR
2389 long seg_reloc_count = 0;
2390 symbolS *add_symbolP;
2391 symbolS *sub_symbolP;
d5364748 2392 valueT add_number;
f5c32424
KR
2393 int size;
2394 char *place;
2395 long where;
a55774a1 2396 int pcrel, plt;
f5c32424
KR
2397 fragS *fragP;
2398 segT add_symbol_segment = absolute_section;
5d0cd0b8 2399
f5c32424
KR
2400 /* If the linker is doing the relaxing, we must not do any fixups.
2401
2402 Well, strictly speaking that's not true -- we could do any that are
2403 PC-relative and don't cross regions that could change size. And for the
2404 i960 (the only machine for which we've got a relaxing linker right now),
2405 we might be able to turn callx/callj into bal anyways in cases where we
2406 know the maximum displacement. */
6efd877d 2407 if (linkrelax)
f5c32424
KR
2408 {
2409 for (; fixP; fixP = fixP->fx_next)
2410 seg_reloc_count++;
2411 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2412 return seg_reloc_count;
2413 }
6efd877d 2414
f5c32424
KR
2415 for (; fixP; fixP = fixP->fx_next)
2416 {
a55774a1
KR
2417#ifdef DEBUG5
2418 fprintf (stderr, "\nprocessing fixup:\n");
2419 print_fixup (fixP);
2420#endif
2421
f5c32424
KR
2422 fragP = fixP->fx_frag;
2423 know (fragP);
2424 where = fixP->fx_where;
2425 place = fragP->fr_literal + where;
2426 size = fixP->fx_size;
2427 add_symbolP = fixP->fx_addsy;
2428#ifdef TC_VALIDATE_FIX
2429 TC_VALIDATE_FIX (fixP, this_segment_type, skip);
fecd2382 2430#endif
f5c32424
KR
2431 sub_symbolP = fixP->fx_subsy;
2432 add_number = fixP->fx_offset;
2433 pcrel = fixP->fx_pcrel;
a55774a1
KR
2434 plt = fixP->fx_plt;
2435
e120d9fb
ILT
2436 if (add_symbolP != NULL
2437 && add_symbolP->sy_mri_common)
1356d77d
ILT
2438 {
2439 know (add_symbolP->sy_value.X_op == O_symbol);
2440 add_number += S_GET_VALUE (add_symbolP);
2441 fixP->fx_offset = add_number;
2442 add_symbolP = fixP->fx_addsy = add_symbolP->sy_value.X_add_symbol;
2443 }
2444
f5c32424
KR
2445 if (add_symbolP)
2446 add_symbol_segment = S_GET_SEGMENT (add_symbolP);
5d0cd0b8 2447
f5c32424
KR
2448 if (sub_symbolP)
2449 {
66a6a381 2450 resolve_symbol_value (sub_symbolP, 1);
96fe71e1 2451 if (add_symbolP == NULL || add_symbol_segment == absolute_section)
f5c32424 2452 {
96fe71e1
MM
2453 if (add_symbolP != NULL)
2454 {
2455 add_number += S_GET_VALUE (add_symbolP);
2456 add_symbolP = NULL;
2457 fixP->fx_addsy = NULL;
2458 }
2459
2460 /* It's just -sym */
f5c32424 2461 if (S_GET_SEGMENT (sub_symbolP) == absolute_section)
96fe71e1
MM
2462 {
2463 add_number -= S_GET_VALUE (sub_symbolP);
2464 fixP->fx_subsy = NULL;
2465 }
f5c32424
KR
2466 else if (pcrel
2467 && S_GET_SEGMENT (sub_symbolP) == this_segment_type)
2468 {
2469 /* Should try converting to a constant. */
2470 goto bad_sub_reloc;
2471 }
2472 else
2473 bad_sub_reloc:
741f4d66 2474 as_bad_where (fixP->fx_file, fixP->fx_line,
99fcd8a0 2475 _("Negative of non-absolute symbol %s"),
741f4d66 2476 S_GET_NAME (sub_symbolP));
f5c32424 2477 }
96fe71e1
MM
2478 else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment
2479 && SEG_NORMAL (add_symbol_segment))
f5c32424
KR
2480 {
2481 /* Difference of 2 symbols from same segment.
2482 Can't make difference of 2 undefineds: 'value' means
2483 something different for N_UNDF. */
fecd2382 2484#ifdef TC_I960
f5c32424
KR
2485 /* Makes no sense to use the difference of 2 arbitrary symbols
2486 as the target of a call instruction. */
2487 if (fixP->fx_tcbit)
741f4d66 2488 as_bad_where (fixP->fx_file, fixP->fx_line,
99fcd8a0 2489 _("callj to difference of 2 symbols"));
6efd877d 2490#endif /* TC_I960 */
f5c32424
KR
2491 add_number += S_GET_VALUE (add_symbolP) -
2492 S_GET_VALUE (sub_symbolP);
6efd877d 2493
f5c32424 2494 add_symbolP = NULL;
c151fd1e 2495 pcrel = 0; /* No further pcrel processing. */
335d35c8 2496
f5c32424
KR
2497 /* Let the target machine make the final determination
2498 as to whether or not a relocation will be needed to
2499 handle this fixup. */
96fe71e1 2500 if (!TC_FORCE_RELOCATION_SECTION (fixP, this_segment_type))
98c6bbbe 2501 {
c151fd1e 2502 fixP->fx_pcrel = 0;
98c6bbbe 2503 fixP->fx_addsy = NULL;
96fe71e1 2504 fixP->fx_subsy = NULL;
98c6bbbe 2505 }
f5c32424
KR
2506 }
2507 else
2508 {
2509 /* Different segments in subtraction. */
2510 know (!(S_IS_EXTERNAL (sub_symbolP)
2511 && (S_GET_SEGMENT (sub_symbolP) == absolute_section)));
2512
2513 if ((S_GET_SEGMENT (sub_symbolP) == absolute_section))
2514 add_number -= S_GET_VALUE (sub_symbolP);
6efd877d 2515
80aab579 2516#ifdef DIFF_EXPR_OK
f5c32424
KR
2517 else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type
2518#if 0 /* Do this even if it's already described as pc-relative. For example,
2519 on the m68k, an operand of "pc@(foo-.-2)" should address "foo" in a
2520 pc-relative mode. */
2521 && pcrel
0f894895 2522#endif
f5c32424
KR
2523 )
2524 {
2525 /* Make it pc-relative. */
96fe71e1 2526 add_number += (MD_PCREL_FROM_SECTION (fixP, this_segment_type)
f5c32424
KR
2527 - S_GET_VALUE (sub_symbolP));
2528 pcrel = 1;
2529 fixP->fx_pcrel = 1;
2530 sub_symbolP = 0;
2531 fixP->fx_subsy = 0;
2532 }
98c6bbbe 2533#endif
d4083e29
KR
2534#ifdef UNDEFINED_DIFFERENCE_OK
2535 /* The PA needs this for PIC code generation. We basically
2536 don't want to do anything if we have the difference of two
2537 symbols at this point. */
2538 else if (1)
2539 {
2540 /* Leave it alone. */
2541 }
2542#endif
98c6bbbe
KR
2543#ifdef BFD_ASSEMBLER
2544 else if (fixP->fx_r_type == BFD_RELOC_GPREL32
2545 || fixP->fx_r_type == BFD_RELOC_GPREL16)
2546 {
2547 /* Leave it alone. */
2548 }
80aab579 2549#endif
f5c32424
KR
2550 else
2551 {
2552 char buf[50];
2553 sprint_value (buf, fragP->fr_address + where);
741f4d66 2554 as_bad_where (fixP->fx_file, fixP->fx_line,
4ea49053 2555 _("Subtraction of two symbols in different sections \"%s\" {%s section} - \"%s\" {%s section} at file address %s."),
99fcd8a0
MM
2556 S_GET_NAME (add_symbolP),
2557 segment_name (S_GET_SEGMENT (add_symbolP)),
2558 S_GET_NAME (sub_symbolP),
741f4d66 2559 segment_name (S_GET_SEGMENT (sub_symbolP)),
99fcd8a0 2560 buf);
f5c32424
KR
2561 }
2562 }
2563 }
6efd877d 2564
f5c32424
KR
2565 if (add_symbolP)
2566 {
a55774a1 2567 if (add_symbol_segment == this_segment_type && pcrel && !plt
5767cfb7 2568 && TC_RELOC_RTSYM_LOC_FIXUP (fixP))
f5c32424
KR
2569 {
2570 /*
2571 * This fixup was made when the symbol's segment was
2572 * SEG_UNKNOWN, but it is now in the local segment.
2573 * So we know how to do the address without relocation.
2574 */
fecd2382 2575#ifdef TC_I960
f5c32424
KR
2576 /* reloc_callj() may replace a 'call' with a 'calls' or a
2577 'bal', in which cases it modifies *fixP as appropriate.
2578 In the case of a 'calls', no further work is required,
2579 and *fixP has been set up to make the rest of the code
2580 below a no-op. */
2581 reloc_callj (fixP);
fecd2382 2582#endif /* TC_I960 */
6efd877d 2583
f5c32424 2584 add_number += S_GET_VALUE (add_symbolP);
96fe71e1 2585 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
f5c32424 2586 pcrel = 0; /* Lie. Don't want further pcrel processing. */
5d0cd0b8 2587
f5c32424
KR
2588 /* Let the target machine make the final determination
2589 as to whether or not a relocation will be needed to
2590 handle this fixup. */
2591 if (!TC_FORCE_RELOCATION (fixP))
98c6bbbe
KR
2592 {
2593 fixP->fx_pcrel = 0;
2594 fixP->fx_addsy = NULL;
2595 }
f5c32424
KR
2596 }
2597 else
2598 {
00a69b89
ILT
2599 if (add_symbol_segment == absolute_section
2600 && ! pcrel)
f5c32424 2601 {
fecd2382 2602#ifdef TC_I960
f5c32424
KR
2603 /* See comment about reloc_callj() above. */
2604 reloc_callj (fixP);
fecd2382 2605#endif /* TC_I960 */
f5c32424
KR
2606 add_number += S_GET_VALUE (add_symbolP);
2607
2608 /* Let the target machine make the final determination
2609 as to whether or not a relocation will be needed to
2610 handle this fixup. */
5d0cd0b8 2611
f00f5ecd 2612 if (!TC_FORCE_RELOCATION (fixP))
e67a0640
KR
2613 {
2614 fixP->fx_addsy = NULL;
2615 add_symbolP = NULL;
2616 }
f5c32424
KR
2617 }
2618 else if (add_symbol_segment == undefined_section
43ca9aa6 2619#ifdef BFD_ASSEMBLER
f5c32424 2620 || bfd_is_com_section (add_symbol_segment)
43ca9aa6 2621#endif
f5c32424
KR
2622 )
2623 {
fecd2382 2624#ifdef TC_I960
f5c32424
KR
2625 if ((int) fixP->fx_bit_fixP == 13)
2626 {
2627 /* This is a COBR instruction. They have only a
2628 * 13-bit displacement and are only to be used
2629 * for local branches: flag as error, don't generate
2630 * relocation.
2631 */
741f4d66 2632 as_bad_where (fixP->fx_file, fixP->fx_line,
99fcd8a0 2633 _("can't use COBR format with external label"));
98c6bbbe
KR
2634 fixP->fx_addsy = NULL;
2635 fixP->fx_done = 1;
f5c32424
KR
2636 continue;
2637 } /* COBR */
fecd2382 2638#endif /* TC_I960 */
5d0cd0b8 2639
fecd2382 2640#ifdef OBJ_COFF
c593cf41 2641#ifdef TE_I386AIX
f5c32424
KR
2642 if (S_IS_COMMON (add_symbolP))
2643 add_number += S_GET_VALUE (add_symbolP);
c593cf41 2644#endif /* TE_I386AIX */
fecd2382 2645#endif /* OBJ_COFF */
f5c32424
KR
2646 ++seg_reloc_count;
2647 }
2648 else
2649 {
2650 seg_reloc_count++;
00a69b89 2651#if !(defined (TC_V850) && defined (OBJ_ELF))
a043f579 2652#if !(defined (TC_M68K) && defined (OBJ_ELF))
66a6a381 2653#if !defined (TC_I386) || !(defined (OBJ_ELF) || defined (OBJ_COFF)) || defined (TE_PE)
f5c32424 2654 add_number += S_GET_VALUE (add_symbolP);
00a69b89 2655#endif
a043f579 2656#endif
a55774a1 2657#endif
f5c32424
KR
2658 }
2659 }
2660 }
6efd877d 2661
f5c32424
KR
2662 if (pcrel)
2663 {
96fe71e1 2664 add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment_type);
f5c32424
KR
2665 if (add_symbolP == 0)
2666 {
1cf7548e 2667#ifndef BFD_ASSEMBLER
f5c32424 2668 fixP->fx_addsy = &abs_symbol;
1cf7548e
KR
2669#else
2670 fixP->fx_addsy = section_symbol (absolute_section);
2671#endif
2672 fixP->fx_addsy->sy_used_in_reloc = 1;
f5c32424 2673 ++seg_reloc_count;
98c6bbbe
KR
2674 }
2675 }
6efd877d 2676
c7d7eed0 2677 if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && size > 0)
f5c32424 2678 {
99fcd8a0 2679 if ((size_t) size < sizeof (valueT))
f5c32424 2680 {
f7ab9441
DE
2681 valueT mask, hibit;
2682
df44a852 2683 /* set all bits to one */
f7ab9441 2684 mask = 0;
df44a852
KR
2685 mask--;
2686 /* Technically, combining these produces an undefined result
2687 if size is sizeof (valueT), though I think these two
2688 half-way operations should both be defined. And the
2689 compiler should be able to combine them if it's valid on
2690 the host architecture. */
2691 mask <<= size * 4;
2692 mask <<= size * 4;
f7ab9441 2693 hibit = (valueT) 1 << (size * 8 - 1);
a78bc551
ILT
2694 if (((add_number & mask) != 0
2695 || (fixP->fx_signed
f7ab9441 2696 && (add_number & hibit) != 0))
a78bc551 2697 && ((add_number & mask) != mask
f7ab9441 2698 || (add_number & hibit) == 0))
df44a852
KR
2699 {
2700 char buf[50], buf2[50];
2701 sprint_value (buf, fragP->fr_address + where);
2702 if (add_number > 1000)
2703 sprint_value (buf2, add_number);
2704 else
2705 sprintf (buf2, "%ld", (long) add_number);
2706 as_bad_where (fixP->fx_file, fixP->fx_line,
99fcd8a0 2707 _("Value of %s too large for field of %d bytes at %s"),
df44a852
KR
2708 buf2, size, buf);
2709 } /* generic error checking */
2710 }
f5c32424
KR
2711#ifdef WARN_SIGNED_OVERFLOW_WORD
2712 /* Warn if a .word value is too large when treated as a signed
2713 number. We already know it is not too negative. This is to
2714 catch over-large switches generated by gcc on the 68k. */
def66e24 2715 if (!flag_signed_overflow_ok
f5c32424
KR
2716 && size == 2
2717 && add_number > 0x7fff)
2718 as_bad_where (fixP->fx_file, fixP->fx_line,
99fcd8a0 2719 _("Signed .word overflow; switch may be too large; %ld at 0x%lx"),
f5c32424
KR
2720 (long) add_number,
2721 (unsigned long) (fragP->fr_address + where));
6efd877d 2722#endif
f5c32424 2723 } /* not a bit fix */
6efd877d 2724
98c6bbbe
KR
2725 if (!fixP->fx_done)
2726 {
3f81f3cf
MM
2727#ifdef MD_APPLY_FIX3
2728 md_apply_fix3 (fixP, &add_number, this_segment_type);
2729#else
43ca9aa6 2730#ifdef BFD_ASSEMBLER
98c6bbbe 2731 md_apply_fix (fixP, &add_number);
43ca9aa6 2732#else
98c6bbbe
KR
2733 md_apply_fix (fixP, add_number);
2734#endif
3f81f3cf 2735#endif
98c6bbbe
KR
2736
2737#ifndef TC_HANDLES_FX_DONE
2738 /* If the tc-* files haven't been converted, assume it's handling
2739 it the old way, where a null fx_addsy means that the fix has
2740 been applied completely, and no further work is needed. */
2741 if (fixP->fx_addsy == 0 && fixP->fx_pcrel == 0)
2742 fixP->fx_done = 1;
43ca9aa6 2743#endif
98c6bbbe
KR
2744 }
2745#ifdef TC_VALIDATE_FIX
a55774a1
KR
2746 skip: ;
2747#endif
2748#ifdef DEBUG5
2749 fprintf (stderr, "result:\n");
2750 print_fixup (fixP);
98c6bbbe 2751#endif
f5c32424 2752 } /* For each fixS in this segment. */
6efd877d 2753
f5c32424
KR
2754 TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
2755 return seg_reloc_count;
2756}
6efd877d 2757
3f81f3cf 2758#endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
6efd877d 2759
f5c32424
KR
2760void
2761number_to_chars_bigendian (buf, val, n)
2762 char *buf;
2763 valueT val;
2764 int n;
2765{
99fcd8a0 2766 if ((size_t) n > sizeof (val) || n <= 0)
f5c32424
KR
2767 abort ();
2768 while (n--)
2769 {
2770 buf[n] = val & 0xff;
2771 val >>= 8;
2772 }
d5364748 2773}
fecd2382 2774
f5c32424
KR
2775void
2776number_to_chars_littleendian (buf, val, n)
2777 char *buf;
2778 valueT val;
2779 int n;
2780{
99fcd8a0 2781 if ((size_t) n > sizeof (val) || n <= 0)
f5c32424
KR
2782 abort ();
2783 while (n--)
2784 {
2785 *buf++ = val & 0xff;
2786 val >>= 8;
2787 }
2788}
80aab579 2789
a043f579
ILT
2790void
2791write_print_statistics (file)
2792 FILE *file;
2793{
2794 fprintf (stderr, "fixups: %d\n", n_fixups);
2795}
2796
4acf8c78
KR
2797/* for debugging */
2798extern int indent_level;
2799
2800void
2801print_fixup (fixp)
2802 fixS *fixp;
2803{
2804 indent_level = 1;
a55774a1 2805 fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
4acf8c78
KR
2806 if (fixp->fx_pcrel)
2807 fprintf (stderr, " pcrel");
2808 if (fixp->fx_pcrel_adjust)
2809 fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
2810 if (fixp->fx_im_disp)
2811 {
2812#ifdef TC_NS32K
2813 fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
2814#else
2815 fprintf (stderr, " im_disp");
2816#endif
2817 }
2818 if (fixp->fx_tcbit)
2819 fprintf (stderr, " tcbit");
2820 if (fixp->fx_done)
2821 fprintf (stderr, " done");
a55774a1
KR
2822 fprintf (stderr, "\n size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
2823 fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
2824 (long) fixp->fx_offset, (long) fixp->fx_addnumber);
9dc6c00f
KR
2825#ifdef BFD_ASSEMBLER
2826 fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
2827 fixp->fx_r_type);
2828#else
a55774a1 2829#ifdef NEED_FX_R_TYPE
9dc6c00f 2830 fprintf (stderr, " r_type=%d", fixp->fx_r_type);
a55774a1 2831#endif
9dc6c00f
KR
2832#endif
2833 if (fixp->fx_addsy)
2834 {
a55774a1 2835 fprintf (stderr, "\n +<");
9dc6c00f
KR
2836 print_symbol_value_1 (stderr, fixp->fx_addsy);
2837 fprintf (stderr, ">");
2838 }
a55774a1
KR
2839 if (fixp->fx_subsy)
2840 {
2841 fprintf (stderr, "\n -<");
2842 print_symbol_value_1 (stderr, fixp->fx_subsy);
2843 fprintf (stderr, ">");
2844 }
9dc6c00f 2845 fprintf (stderr, "\n");
99fcd8a0
MM
2846#ifdef TC_FIX_DATA_PRINT
2847 TC_FIX_DATA_PRINT (stderr, fixp);
2848#endif
4acf8c78
KR
2849}
2850
fecd2382 2851/* end of write.c */
This page took 0.401754 seconds and 4 git commands to generate.