Coverage Report

Created: 2025-07-08 11:15

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