Coverage Report

Created: 2024-05-21 06:29

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