Coverage Report

Created: 2026-09-14 08:07

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