Coverage Report

Created: 2026-07-25 10:20

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