Coverage Report

Created: 2023-08-28 06:31

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