OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [gas/] [write.c] - Blame information for rev 241

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.