Coverage Report

Created: 2026-03-10 08:46

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