Coverage Report

Created: 2025-06-24 06:45

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