write.c 76.3 KB
Newer Older
Richard Henderson's avatar
Richard Henderson committed
1
/* write.c - emit .o file
Nick Clifton's avatar
Nick Clifton committed
2
   Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
Alan Modra's avatar
Alan Modra committed
3
   1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4
   2010, 2011, 2012 Free Software Foundation, Inc.
Richard Henderson's avatar
Richard Henderson committed
5 6 7 8 9

   This file is part of GAS, the GNU Assembler.

   GAS is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
Nick Clifton's avatar
Nick Clifton committed
10
   the Free Software Foundation; either version 3, or (at your option)
Richard Henderson's avatar
Richard Henderson committed
11 12 13 14 15 16 17 18 19
   any later version.

   GAS is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GAS; see the file COPYING.  If not, write to the Free
20 21
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */
Richard Henderson's avatar
Richard Henderson committed
22

Nick Clifton's avatar
Nick Clifton committed
23
/* This thing should be set up to do byteordering correctly.  But...  */
Richard Henderson's avatar
Richard Henderson committed
24 25 26 27 28

#include "as.h"
#include "subsegs.h"
#include "obstack.h"
#include "output-file.h"
29
#include "dwarf2dbg.h"
30
#include "libbfd.h"
Cary Coutant's avatar
Cary Coutant committed
31
#include "compress-debug.h"
Richard Henderson's avatar
Richard Henderson committed
32 33

#ifndef TC_FORCE_RELOCATION
Alan Modra's avatar
Alan Modra committed
34
#define TC_FORCE_RELOCATION(FIX)		\
35
  (generic_force_reloc (FIX))
Richard Henderson's avatar
Richard Henderson committed
36 37
#endif

Alan Modra's avatar
Alan Modra committed
38 39 40 41 42 43 44 45 46 47 48 49 50
#ifndef TC_FORCE_RELOCATION_ABS
#define TC_FORCE_RELOCATION_ABS(FIX)		\
  (TC_FORCE_RELOCATION (FIX))
#endif

#ifndef TC_FORCE_RELOCATION_LOCAL
#define TC_FORCE_RELOCATION_LOCAL(FIX)		\
  (!(FIX)->fx_pcrel				\
   || TC_FORCE_RELOCATION (FIX))
#endif

#ifndef TC_FORCE_RELOCATION_SUB_SAME
#define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG)	\
51
  (! SEG_NORMAL (SEG))
Alan Modra's avatar
Alan Modra committed
52 53
#endif

54 55 56 57
#ifndef md_register_arithmetic
# define md_register_arithmetic 1
#endif

Alan Modra's avatar
Alan Modra committed
58
#ifndef TC_FORCE_RELOCATION_SUB_ABS
59 60
#define TC_FORCE_RELOCATION_SUB_ABS(FIX, SEG)	\
  (!md_register_arithmetic && (SEG) == reg_section)
Alan Modra's avatar
Alan Modra committed
61 62 63 64
#endif

#ifndef TC_FORCE_RELOCATION_SUB_LOCAL
#ifdef DIFF_EXPR_OK
65 66
#define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG)	\
  (!md_register_arithmetic && (SEG) == reg_section)
Alan Modra's avatar
Alan Modra committed
67
#else
68
#define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG)	1
Alan Modra's avatar
Alan Modra committed
69 70 71 72 73 74
#endif
#endif

#ifndef TC_VALIDATE_FIX_SUB
#ifdef UNDEFINED_DIFFERENCE_OK
/* The PA needs this for PIC code generation.  */
75 76
#define TC_VALIDATE_FIX_SUB(FIX, SEG)			\
  (md_register_arithmetic || (SEG) != reg_section)
Alan Modra's avatar
Alan Modra committed
77
#else
78 79 80 81
#define TC_VALIDATE_FIX_SUB(FIX, SEG)			\
  ((md_register_arithmetic || (SEG) != reg_section)	\
   && ((FIX)->fx_r_type == BFD_RELOC_GPREL32		\
       || (FIX)->fx_r_type == BFD_RELOC_GPREL16))
Alan Modra's avatar
Alan Modra committed
82
#endif
Richard Henderson's avatar
Richard Henderson committed
83 84
#endif

85 86 87 88
#ifndef TC_LINKRELAX_FIXUP
#define TC_LINKRELAX_FIXUP(SEG) 1
#endif

Alan Modra's avatar
Alan Modra committed
89 90
#ifndef MD_APPLY_SYM_VALUE
#define MD_APPLY_SYM_VALUE(FIX) 1
91
#endif
92

93 94 95 96
#ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
#define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
#endif

Richard Henderson's avatar
Richard Henderson committed
97
#ifndef	MD_PCREL_FROM_SECTION
98
#define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
Richard Henderson's avatar
Richard Henderson committed
99 100
#endif

101 102 103 104
#ifndef TC_FAKE_LABEL
#define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
#endif

105 106 107 108 109 110 111 112 113 114
/* Positive values of TC_FX_SIZE_SLACK allow a target to define
   fixups that far past the end of a frag.  Having such fixups
   is of course most most likely a bug in setting fx_size correctly.
   A negative value disables the fixup check entirely, which is
   appropriate for something like the Renesas / SuperH SH_COUNT
   reloc.  */
#ifndef TC_FX_SIZE_SLACK
#define TC_FX_SIZE_SLACK(FIX) 0
#endif

115 116
/* Used to control final evaluation of expressions.  */
int finalize_syms = 0;
117

Richard Henderson's avatar
Richard Henderson committed
118
int symbol_table_frozen;
Alan Modra's avatar
Alan Modra committed
119 120 121

symbolS *abs_section_sym;

122 123 124
/* Remember the value of dot when parsing expressions.  */
addressT dot_value;

125 126 127
/* The frag that dot_value is based from.  */
fragS *dot_frag;

128 129 130
/* Relocs generated by ".reloc" pseudo.  */
struct reloc_list* reloc_list;

131
void print_fixup (fixS *);
Richard Henderson's avatar
Richard Henderson committed
132 133 134 135 136 137 138 139 140

/* We generally attach relocs to frag chains.  However, after we have
   chained these all together into a segment, any relocs we add after
   that must be attached to a segment.  This will include relocs added
   in md_estimate_size_for_relax, for example.  */
static int frags_chained = 0;

static int n_fixups;

141 142
#define RELOC_ENUM enum bfd_reloc_code_real

Nick Clifton's avatar
Nick Clifton committed
143 144
/* Create a fixS in obstack 'notes'.  */

Richard Henderson's avatar
Richard Henderson committed
145
static fixS *
146 147 148 149 150 151 152
fix_new_internal (fragS *frag,		/* Which frag?  */
		  int where,		/* Where in that frag?  */
		  int size,		/* 1, 2, or 4 usually.  */
		  symbolS *add_symbol,	/* X_add_symbol.  */
		  symbolS *sub_symbol,	/* X_op_symbol.  */
		  offsetT offset,	/* X_add_number.  */
		  int pcrel,		/* TRUE if PC-relative relocation.  */
153
		  RELOC_ENUM r_type	/* Relocation type.  */,
Richard Sandiford's avatar
bfd/  
Richard Sandiford committed
154
		  int at_beginning)	/* Add to the start of the list?  */
Richard Henderson's avatar
Richard Henderson committed
155 156 157 158 159
{
  fixS *fixP;

  n_fixups++;

160
  fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
Richard Henderson's avatar
Richard Henderson committed
161 162 163 164 165 166 167 168 169 170 171 172 173

  fixP->fx_frag = frag;
  fixP->fx_where = where;
  fixP->fx_size = size;
  /* We've made fx_size a narrow field; check that it's wide enough.  */
  if (fixP->fx_size != size)
    {
      as_bad (_("field fx_size too small to hold %d"), size);
      abort ();
    }
  fixP->fx_addsy = add_symbol;
  fixP->fx_subsy = sub_symbol;
  fixP->fx_offset = offset;
174
  fixP->fx_dot_value = dot_value;
175
  fixP->fx_dot_frag = dot_frag;
Richard Henderson's avatar
Richard Henderson committed
176 177 178 179 180 181 182
  fixP->fx_pcrel = pcrel;
  fixP->fx_r_type = r_type;
  fixP->fx_im_disp = 0;
  fixP->fx_pcrel_adjust = 0;
  fixP->fx_bit_fixP = 0;
  fixP->fx_addnumber = 0;
  fixP->fx_tcbit = 0;
183
  fixP->fx_tcbit2 = 0;
Richard Henderson's avatar
Richard Henderson committed
184 185 186 187 188 189 190 191 192 193
  fixP->fx_done = 0;
  fixP->fx_no_overflow = 0;
  fixP->fx_signed = 0;

#ifdef USING_CGEN
  fixP->fx_cgen.insn = NULL;
  fixP->fx_cgen.opinfo = 0;
#endif

#ifdef TC_FIX_TYPE
Nick Clifton's avatar
Nick Clifton committed
194
  TC_INIT_FIX_DATA (fixP);
Richard Henderson's avatar
Richard Henderson committed
195 196 197 198 199 200 201 202 203 204 205 206 207
#endif

  as_where (&fixP->fx_file, &fixP->fx_line);

  {

    fixS **seg_fix_rootP = (frags_chained
			    ? &seg_info (now_seg)->fix_root
			    : &frchain_now->fix_root);
    fixS **seg_fix_tailP = (frags_chained
			    ? &seg_info (now_seg)->fix_tail
			    : &frchain_now->fix_tail);

Richard Sandiford's avatar
bfd/  
Richard Sandiford committed
208 209 210 211 212 213 214
    if (at_beginning)
      {
	fixP->fx_next = *seg_fix_rootP;
	*seg_fix_rootP = fixP;
	if (fixP->fx_next == NULL)
	  *seg_fix_tailP = fixP;
      }
Richard Henderson's avatar
Richard Henderson committed
215
    else
Richard Sandiford's avatar
bfd/  
Richard Sandiford committed
216 217 218 219 220 221 222 223
      {
	fixP->fx_next = NULL;
	if (*seg_fix_tailP)
	  (*seg_fix_tailP)->fx_next = fixP;
	else
	  *seg_fix_rootP = fixP;
	*seg_fix_tailP = fixP;
      }
Richard Henderson's avatar
Richard Henderson committed
224 225 226 227 228 229 230 231
  }

  return fixP;
}

/* Create a fixup relative to a symbol (plus a constant).  */

fixS *
232 233 234 235 236 237 238
fix_new (fragS *frag,		/* Which frag?  */
	 int where,			/* Where in that frag?  */
	 int size,			/* 1, 2, or 4 usually.  */
	 symbolS *add_symbol,	/* X_add_symbol.  */
	 offsetT offset,		/* X_add_number.  */
	 int pcrel,			/* TRUE if PC-relative relocation.  */
	 RELOC_ENUM r_type		/* Relocation type.  */)
Richard Henderson's avatar
Richard Henderson committed
239 240
{
  return fix_new_internal (frag, where, size, add_symbol,
Richard Sandiford's avatar
bfd/  
Richard Sandiford committed
241
			   (symbolS *) NULL, offset, pcrel, r_type, FALSE);
Richard Henderson's avatar
Richard Henderson committed
242 243 244 245 246 247 248
}

/* Create a fixup for an expression.  Currently we only support fixups
   for difference expressions.  That is itself more than most object
   file formats support anyhow.  */

fixS *
249 250 251 252 253 254
fix_new_exp (fragS *frag,		/* Which frag?  */
	     int where,			/* Where in that frag?  */
	     int size,			/* 1, 2, or 4 usually.  */
	     expressionS *exp,		/* Expression.  */
	     int pcrel,			/* TRUE if PC-relative relocation.  */
	     RELOC_ENUM r_type		/* Relocation type.  */)
Richard Henderson's avatar
Richard Henderson committed
255 256 257 258 259 260 261 262 263 264
{
  symbolS *add = NULL;
  symbolS *sub = NULL;
  offsetT off = 0;

  switch (exp->X_op)
    {
    case O_absent:
      break;

265 266 267 268
    case O_register:
      as_bad (_("register value used as expression"));
      break;

Richard Henderson's avatar
Richard Henderson committed
269 270 271 272 273
    case O_add:
      /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
	 the difference expression cannot immediately be reduced.  */
      {
	symbolS *stmp = make_expr_symbol (exp);
274

Richard Henderson's avatar
Richard Henderson committed
275 276 277 278
	exp->X_op = O_symbol;
	exp->X_op_symbol = 0;
	exp->X_add_symbol = stmp;
	exp->X_add_number = 0;
279

Richard Henderson's avatar
Richard Henderson committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	return fix_new_exp (frag, where, size, exp, pcrel, r_type);
      }

    case O_symbol_rva:
      add = exp->X_add_symbol;
      off = exp->X_add_number;
      r_type = BFD_RELOC_RVA;
      break;

    case O_uminus:
      sub = exp->X_add_symbol;
      off = exp->X_add_number;
      break;

    case O_subtract:
      sub = exp->X_op_symbol;
      /* Fall through.  */
    case O_symbol:
      add = exp->X_add_symbol;
Nick Clifton's avatar
Nick Clifton committed
299
      /* Fall through.  */
Richard Henderson's avatar
Richard Henderson committed
300 301 302 303 304 305 306 307 308
    case O_constant:
      off = exp->X_add_number;
      break;

    default:
      add = make_expr_symbol (exp);
      break;
    }

Richard Sandiford's avatar
bfd/  
Richard Sandiford committed
309 310 311 312 313 314 315 316 317 318 319 320 321
  return fix_new_internal (frag, where, size, add, sub, off, pcrel,
			   r_type, FALSE);
}

/* Create a fixup at the beginning of FRAG.  The arguments are the same
   as for fix_new, except that WHERE is implicitly 0.  */

fixS *
fix_at_start (fragS *frag, int size, symbolS *add_symbol,
	      offsetT offset, int pcrel, RELOC_ENUM r_type)
{
  return fix_new_internal (frag, 0, size, add_symbol,
			   (symbolS *) NULL, offset, pcrel, r_type, TRUE);
Richard Henderson's avatar
Richard Henderson committed
322 323
}

324 325
/* Generic function to determine whether a fixup requires a relocation.  */
int
326
generic_force_reloc (fixS *fix)
327 328 329 330
{
  if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
      || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
    return 1;
Alan Modra's avatar
Alan Modra committed
331

332 333 334
  if (fix->fx_addsy == NULL)
    return 0;

335 336 337
  return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
}

Richard Henderson's avatar
Richard Henderson committed
338 339
/* Append a string onto another string, bumping the pointer along.  */
void
340
append (char **charPP, char *fromP, unsigned long length)
Richard Henderson's avatar
Richard Henderson committed
341
{
Nick Clifton's avatar
Nick Clifton committed
342
  /* Don't trust memcpy() of 0 chars.  */
Richard Henderson's avatar
Richard Henderson committed
343 344 345 346 347 348 349
  if (length == 0)
    return;

  memcpy (*charPP, fromP, length);
  *charPP += length;
}

Nick Clifton's avatar
Nick Clifton committed
350 351 352 353 354
/* This routine records the largest alignment seen for each segment.
   If the beginning of the segment is aligned on the worst-case
   boundary, all of the other alignments within it will work.  At
   least one object format really uses this info.  */

Richard Henderson's avatar
Richard Henderson committed
355
void
356 357 358 359 360
record_alignment (/* Segment to which alignment pertains.  */
		  segT seg,
		  /* Alignment, as a power of 2 (e.g., 1 => 2-byte
		     boundary, 2 => 4-byte boundary, etc.)  */
		  int align)
Richard Henderson's avatar
Richard Henderson committed
361 362 363
{
  if (seg == absolute_section)
    return;
Alan Modra's avatar
Alan Modra committed
364

Richard Henderson's avatar
Richard Henderson committed
365 366 367 368
  if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
    bfd_set_section_alignment (stdoutput, seg, align);
}

369
int
370
get_recorded_alignment (segT seg)
371 372 373
{
  if (seg == absolute_section)
    return 0;
Alan Modra's avatar
Alan Modra committed
374

375 376 377
  return bfd_get_section_alignment (stdoutput, seg);
}

Richard Henderson's avatar
Richard Henderson committed
378 379 380
/* Reset the section indices after removing the gas created sections.  */

static void
381
renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
Richard Henderson's avatar
Richard Henderson committed
382 383 384 385 386 387 388 389
{
  int *countp = (int *) countparg;

  sec->index = *countp;
  ++*countp;
}

static fragS *
390
chain_frchains_together_1 (segT section, struct frchain *frchp)
Richard Henderson's avatar
Richard Henderson committed
391 392 393 394
{
  fragS dummy, *prev_frag = &dummy;
  fixS fix_dummy, *prev_fix = &fix_dummy;

395
  for (; frchp; frchp = frchp->frch_next)
Richard Henderson's avatar
Richard Henderson committed
396 397 398
    {
      prev_frag->fr_next = frchp->frch_root;
      prev_frag = frchp->frch_last;
399
      gas_assert (prev_frag->fr_type != 0);
Richard Henderson's avatar
Richard Henderson committed
400 401 402 403 404 405 406 407 408
      if (frchp->fix_root != (fixS *) NULL)
	{
	  if (seg_info (section)->fix_root == (fixS *) NULL)
	    seg_info (section)->fix_root = frchp->fix_root;
	  prev_fix->fx_next = frchp->fix_root;
	  seg_info (section)->fix_tail = frchp->fix_tail;
	  prev_fix = frchp->fix_tail;
	}
    }
409 410
  gas_assert (prev_frag != &dummy
	      && prev_frag->fr_type != 0);
Richard Henderson's avatar
Richard Henderson committed
411 412 413 414 415
  prev_frag->fr_next = 0;
  return prev_frag;
}

static void
416 417
chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
			 segT section,
418
			 void *xxx ATTRIBUTE_UNUSED)
Richard Henderson's avatar
Richard Henderson committed
419 420 421 422 423 424 425
{
  segment_info_type *info;

  /* BFD may have introduced its own sections without using
     subseg_new, so it is possible that seg_info is NULL.  */
  info = seg_info (section);
  if (info != (segment_info_type *) NULL)
Nick Clifton's avatar
Nick Clifton committed
426 427
    info->frchainP->frch_last
      = chain_frchains_together_1 (section, info->frchainP);
Richard Henderson's avatar
Richard Henderson committed
428 429 430 431 432 433 434

  /* Now that we've chained the frags together, we must add new fixups
     to the segment, not to the frag chain.  */
  frags_chained = 1;
}

static void
435
cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
Richard Henderson's avatar
Richard Henderson committed
436 437 438 439 440
{
  switch (fragP->fr_type)
    {
    case rs_align:
    case rs_align_code:
441
    case rs_align_test:
Richard Henderson's avatar
Richard Henderson committed
442 443 444 445 446 447 448 449 450 451 452
    case rs_org:
    case rs_space:
#ifdef HANDLE_ALIGN
      HANDLE_ALIGN (fragP);
#endif
      know (fragP->fr_next != NULL);
      fragP->fr_offset = (fragP->fr_next->fr_address
			  - fragP->fr_address
			  - fragP->fr_fix) / fragP->fr_var;
      if (fragP->fr_offset < 0)
	{
453 454 455
	  as_bad_where (fragP->fr_file, fragP->fr_line,
			_("attempt to .org/.space backwards? (%ld)"),
			(long) fragP->fr_offset);
456
	  fragP->fr_offset = 0;
Richard Henderson's avatar
Richard Henderson committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	}
      fragP->fr_type = rs_fill;
      break;

    case rs_fill:
      break;

    case rs_leb128:
      {
	valueT value = S_GET_VALUE (fragP->fr_symbol);
	int size;

	size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
			      fragP->fr_subtype);

	fragP->fr_fix += size;
	fragP->fr_type = rs_fill;
	fragP->fr_var = 0;
	fragP->fr_offset = 0;
	fragP->fr_symbol = NULL;
      }
      break;

    case rs_cfa:
      eh_frame_convert_frag (fragP);
      break;

484 485 486 487
    case rs_dwarf2dbg:
      dwarf2dbg_convert_frag (fragP);
      break;

Richard Henderson's avatar
Richard Henderson committed
488 489 490
    case rs_machine_dependent:
      md_convert_frag (stdoutput, sec, fragP);

491
      gas_assert (fragP->fr_next == NULL
Richard Henderson's avatar
Richard Henderson committed
492 493 494
	      || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
		  == fragP->fr_fix));

Nick Clifton's avatar
Nick Clifton committed
495 496 497
      /* After md_convert_frag, we make the frag into a ".space 0".
	 md_convert_frag() should set up any fixSs and constants
	 required.  */
Richard Henderson's avatar
Richard Henderson committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
      frag_wane (fragP);
      break;

#ifndef WORKING_DOT_WORD
    case rs_broken_word:
      {
	struct broken_word *lie;

	if (fragP->fr_subtype)
	  {
	    fragP->fr_fix += md_short_jump_size;
	    for (lie = (struct broken_word *) (fragP->fr_symbol);
		 lie && lie->dispfrag == fragP;
		 lie = lie->next_broken_word)
	      if (lie->added == 1)
		fragP->fr_fix += md_long_jump_size;
	  }
	frag_wane (fragP);
      }
      break;
#endif

    default:
      BAD_CASE (fragP->fr_type);
      break;
    }
524 525 526
#ifdef md_frag_check
  md_frag_check (fragP);
#endif
Richard Henderson's avatar
Richard Henderson committed
527 528
}

529 530 531 532 533
struct relax_seg_info
{
  int pass;
  int changed;
};
534

Richard Henderson's avatar
Richard Henderson committed
535
static void
536
relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
537 538
{
  segment_info_type *seginfo = seg_info (sec);
539
  struct relax_seg_info *info = (struct relax_seg_info *) xxx;
540 541

  if (seginfo && seginfo->frchainP
542 543
      && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
    info->changed = 1;
544 545 546
}

static void
547
size_seg (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
Richard Henderson's avatar
Richard Henderson committed
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
{
  flagword flags;
  fragS *fragp;
  segment_info_type *seginfo;
  int x;
  valueT size, newsize;

  subseg_change (sec, 0);

  seginfo = seg_info (sec);
  if (seginfo && seginfo->frchainP)
    {
      for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
	cvt_frag_to_fill (sec, fragp);
      for (fragp = seginfo->frchainP->frch_root;
	   fragp->fr_next;
	   fragp = fragp->fr_next)
Nick Clifton's avatar
Nick Clifton committed
565 566
	/* Walk to last elt.  */
	;
Richard Henderson's avatar
Richard Henderson committed
567 568 569 570 571
      size = fragp->fr_address + fragp->fr_fix;
    }
  else
    size = 0;

572
  flags = bfd_get_section_flags (abfd, sec);
573 574 575
  if (size == 0 && bfd_get_section_size (sec) != 0 &&
    (flags & SEC_HAS_CONTENTS) != 0)
    return;
576

Richard Henderson's avatar
Richard Henderson committed
577 578 579
  if (size > 0 && ! seginfo->bss)
    flags |= SEC_HAS_CONTENTS;

580
  flags &= ~SEC_RELOC;
Richard Henderson's avatar
Richard Henderson committed
581
  x = bfd_set_section_flags (abfd, sec, flags);
582
  gas_assert (x);
Richard Henderson's avatar
Richard Henderson committed
583 584 585

  newsize = md_section_align (sec, size);
  x = bfd_set_section_size (abfd, sec, newsize);
586
  gas_assert (x);
Richard Henderson's avatar
Richard Henderson committed
587 588 589

  /* If the size had to be rounded up, add some padding in the last
     non-empty frag.  */
590
  gas_assert (newsize >= size);
Richard Henderson's avatar
Richard Henderson committed
591 592 593 594 595 596 597
  if (size != newsize)
    {
      fragS *last = seginfo->frchainP->frch_last;
      fragp = seginfo->frchainP->frch_root;
      while (fragp->fr_next != last)
	fragp = fragp->fr_next;
      last->fr_address = size;
598 599 600 601 602 603 604 605
      if ((newsize - size) % fragp->fr_var == 0)
	fragp->fr_offset += (newsize - size) / fragp->fr_var;
      else
	/* If we hit this abort, it's likely due to subsegs_finish not
	   providing sufficient alignment on the last frag, and the
	   machine dependent code using alignment frags with fr_var
	   greater than 1.  */
	abort ();
Richard Henderson's avatar
Richard Henderson committed
606 607 608 609 610 611 612 613 614 615 616 617
    }

#ifdef tc_frob_section
  tc_frob_section (sec);
#endif
#ifdef obj_frob_section
  obj_frob_section (sec);
#endif
}

#ifdef DEBUG2
static void
618
dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
Richard Henderson's avatar
Richard Henderson committed
619 620 621 622 623 624 625 626 627 628 629
{
  segment_info_type *seginfo = seg_info (sec);
  fixS *fixp = seginfo->fix_root;

  if (!fixp)
    return;

  fprintf (stream, "sec %s relocs:\n", sec->name);
  while (fixp)
    {
      symbolS *s = fixp->fx_addsy;
630 631 632 633 634 635

      fprintf (stream, "  %08lx: type %d ", (unsigned long) fixp,
	       (int) fixp->fx_r_type);
      if (s == NULL)
	fprintf (stream, "no sym\n");
      else
Richard Henderson's avatar
Richard Henderson committed
636
	{
637 638
	  print_symbol_value_1 (stream, s);
	  fprintf (stream, "\n");
Richard Henderson's avatar
Richard Henderson committed
639 640 641 642 643 644 645 646 647 648 649 650
	}
      fixp = fixp->fx_next;
    }
}
#else
#define dump_section_relocs(ABFD,SEC,STREAM)	((void) 0)
#endif

#ifndef EMIT_SECTION_SYMBOLS
#define EMIT_SECTION_SYMBOLS 1
#endif

651 652 653 654 655 656
/* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
   and check for validity.  Convert RELOC_LIST from using U.A fields
   to U.B fields.  */
static void
resolve_reloc_expr_symbols (void)
{
657
  bfd_vma addr_mask = 1;
658 659
  struct reloc_list *r;

660 661 662 663 664
  /* Avoid a shift by the width of type.  */
  addr_mask <<= bfd_arch_bits_per_address (stdoutput) - 1;
  addr_mask <<= 1;
  addr_mask -= 1;

665 666
  for (r = reloc_list; r; r = r->next)
    {
667
      reloc_howto_type *howto = r->u.a.howto;
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
      expressionS *symval;
      symbolS *sym;
      bfd_vma offset, addend;
      asection *sec;

      resolve_symbol_value (r->u.a.offset_sym);
      symval = symbol_get_value_expression (r->u.a.offset_sym);

      offset = 0;
      sym = NULL;
      if (symval->X_op == O_constant)
	sym = r->u.a.offset_sym;
      else if (symval->X_op == O_symbol)
	{
	  sym = symval->X_add_symbol;
	  offset = symval->X_add_number;
	  symval = symbol_get_value_expression (symval->X_add_symbol);
	}
      if (sym == NULL
	  || symval->X_op != O_constant
	  || (sec = S_GET_SEGMENT (sym)) == NULL
	  || !SEG_NORMAL (sec))
	{
	  as_bad_where (r->file, r->line, _("invalid offset expression"));
	  sec = NULL;
	}
      else
	offset += S_GET_VALUE (sym);

      sym = NULL;
      addend = r->u.a.addend;
      if (r->u.a.sym != NULL)
	{
	  resolve_symbol_value (r->u.a.sym);
	  symval = symbol_get_value_expression (r->u.a.sym);
	  if (symval->X_op == O_constant)
	    sym = r->u.a.sym;
	  else if (symval->X_op == O_symbol)
	    {
	      sym = symval->X_add_symbol;
	      addend += symval->X_add_number;
	      symval = symbol_get_value_expression (symval->X_add_symbol);
	    }
	  if (symval->X_op != O_constant)
	    {
	      as_bad_where (r->file, r->line, _("invalid reloc expression"));
	      sec = NULL;
	    }
	  else if (sym != NULL)
717
	    {
718 719 720 721 722 723 724
	      /* Convert relocs against local symbols to refer to the
	         corresponding section symbol plus offset instead.  Keep
	         PC-relative relocs of the REL variety intact though to
		 prevent the offset from overflowing the relocated field,
	         unless it has enough bits to cover the whole address
	         space.  */
	      if (S_IS_LOCAL (sym) && !symbol_section_p (sym)
Maciej W. Rozycki's avatar
Maciej W. Rozycki committed
725 726 727 728
		  && (sec->use_rela_p
		      || (howto->partial_inplace
			  && (!howto->pc_relative
			      || howto->src_mask == addr_mask))))
729 730 731 732 733 734 735 736 737 738 739 740
		{
		  asection *symsec = S_GET_SEGMENT (sym);
		  if (!(((symsec->flags & SEC_MERGE) != 0
			 && addend != 0)
			|| (symsec->flags & SEC_THREAD_LOCAL) != 0))
		    {
		      addend += S_GET_VALUE (sym);
		      sym = section_symbol (symsec);
		    }
		}
	      symbol_mark_used_in_reloc (sym);
	    }
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
	}
      if (sym == NULL)
	{
	  if (abs_section_sym == NULL)
	    abs_section_sym = section_symbol (absolute_section);
	  sym = abs_section_sym;
	}

      r->u.b.sec = sec;
      r->u.b.s = symbol_get_bfdsym (sym);
      r->u.b.r.sym_ptr_ptr = &r->u.b.s;
      r->u.b.r.address = offset;
      r->u.b.r.addend = addend;
      r->u.b.r.howto = howto;
    }
}

758 759 760
/* This pass over fixups decides whether symbols can be replaced with
   section symbols.  */

Richard Henderson's avatar
Richard Henderson committed
761
static void
762 763
adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
		   asection *sec,
764
		   void *xxx ATTRIBUTE_UNUSED)
Richard Henderson's avatar
Richard Henderson committed
765 766 767 768 769 770 771 772 773 774 775
{
  segment_info_type *seginfo = seg_info (sec);
  fixS *fixp;

  if (seginfo == NULL)
    return;

  dump_section_relocs (abfd, sec, stderr);

  for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
    if (fixp->fx_done)
Nick Clifton's avatar
Nick Clifton committed
776 777
      /* Ignore it.  */
      ;
Richard Henderson's avatar
Richard Henderson committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
    else if (fixp->fx_addsy)
      {
	symbolS *sym;
	asection *symsec;

#ifdef DEBUG5
	fprintf (stderr, "\n\nadjusting fixup:\n");
	print_fixup (fixp);
#endif

	sym = fixp->fx_addsy;

	/* All symbols should have already been resolved at this
	   point.  It is possible to see unresolved expression
	   symbols, though, since they are not in the regular symbol
	   table.  */
794
	resolve_symbol_value (sym);
Nick Clifton's avatar
Nick Clifton committed
795

796
	if (fixp->fx_subsy != NULL)
797
	  resolve_symbol_value (fixp->fx_subsy);
Richard Henderson's avatar
Richard Henderson committed
798

H.J. Lu's avatar
gas/  
H.J. Lu committed
799 800
	/* If this symbol is equated to an undefined or common symbol,
	   convert the fixup to being against that symbol.  */
801 802
	while (symbol_equated_reloc_p (sym)
	       || S_IS_WEAKREFR (sym))
Richard Henderson's avatar
Richard Henderson committed
803
	  {
804 805 806
	    symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
	    if (sym == newsym)
	      break;
807
	    fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
808 809
	    fixp->fx_addsy = newsym;
	    sym = newsym;
Richard Henderson's avatar
Richard Henderson committed
810 811
	  }

812
	if (symbol_mri_common_p (sym))
Richard Henderson's avatar
Richard Henderson committed
813
	  {
814 815
	    fixp->fx_offset += S_GET_VALUE (sym);
	    fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
816
	    continue;
Richard Henderson's avatar
Richard Henderson committed
817 818
	  }

Alan Modra's avatar
Alan Modra committed
819 820
	/* If the symbol is undefined, common, weak, or global (ELF
	   shared libs), we can't replace it with the section symbol.  */
821
	if (S_FORCE_RELOC (fixp->fx_addsy, 1))
Alan Modra's avatar
Alan Modra committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
	  continue;

	/* Is there some other (target cpu dependent) reason we can't adjust
	   this one?  (E.g. relocations involving function addresses on
	   the PA.  */
#ifdef tc_fix_adjustable
	if (! tc_fix_adjustable (fixp))
	  continue;
#endif

	/* Since we're reducing to section symbols, don't attempt to reduce
	   anything that's already using one.  */
	if (symbol_section_p (sym))
	  continue;

Richard Henderson's avatar
Richard Henderson committed
837 838 839
	symsec = S_GET_SEGMENT (sym);
	if (symsec == NULL)
	  abort ();
Nick Clifton's avatar
Nick Clifton committed
840

Richard Henderson's avatar
Richard Henderson committed
841 842
	if (bfd_is_abs_section (symsec))
	  {
Alan Modra's avatar
Alan Modra committed
843
	    /* The fixup_segment routine normally will not use this
844
	       symbol in a relocation.  */
845
	    continue;
Richard Henderson's avatar
Richard Henderson committed
846 847
	  }

848
	/* Don't try to reduce relocs which refer to non-local symbols
849 850 851
	   in .linkonce sections.  It can lead to confusion when a
	   debugging section refers to a .linkonce section.  I hope
	   this will always be correct.  */
852
	if (symsec != sec && ! S_IS_LOCAL (sym))
Richard Henderson's avatar
Richard Henderson committed
853
	  {
Alan Modra's avatar
Alan Modra committed
854 855 856 857 858 859 860 861
	    if ((symsec->flags & SEC_LINK_ONCE) != 0
		|| (IS_ELF
		    /* The GNU toolchain uses an extension for ELF: a
		       section beginning with the magic string
		       .gnu.linkonce is a linkonce section.  */
		    && strncmp (segment_name (symsec), ".gnu.linkonce",
				sizeof ".gnu.linkonce" - 1) == 0))
	      continue;
Richard Henderson's avatar
Richard Henderson committed
862
	  }
863 864 865

	/* Never adjust a reloc against local symbol in a merge section
	   with non-zero addend.  */
866 867
	if ((symsec->flags & SEC_MERGE) != 0
	    && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
Alan Modra's avatar
Alan Modra committed
868
	  continue;
869 870

	/* Never adjust a reloc against TLS local symbol.  */
871
	if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
Alan Modra's avatar
Alan Modra committed
872
	  continue;
Richard Henderson's avatar
Richard Henderson committed
873

Alan Modra's avatar
Alan Modra committed
874
	/* We refetch the segment when calling section_symbol, rather
Richard Henderson's avatar
Richard Henderson committed
875
	   than using symsec, because S_GET_VALUE may wind up changing
Nick Clifton's avatar
Nick Clifton committed
876
	   the section when it calls resolve_symbol_value.  */
Richard Henderson's avatar
Richard Henderson committed
877 878
	fixp->fx_offset += S_GET_VALUE (sym);
	fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
879 880 881 882
#ifdef DEBUG5
	fprintf (stderr, "\nadjusted fixup:\n");
	print_fixup (fixp);
#endif
Richard Henderson's avatar
Richard Henderson committed
883 884 885 886 887
      }

  dump_section_relocs (abfd, sec, stderr);
}

888 889 890 891 892 893
/* fixup_segment()

   Go through all the fixS's in a segment and see which ones can be
   handled now.  (These consist of fixS where we have since discovered
   the value of a symbol, or the address of the frag involved.)
   For each one, call md_apply_fix to put the fix into the frag data.
894 895
   Ones that we couldn't completely handle here will be output later
   by emit_relocations.  */
896

897
static void
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
fixup_segment (fixS *fixP, segT this_segment)
{
  valueT add_number;
  fragS *fragP;
  segT add_symbol_segment = absolute_section;

  if (fixP != NULL && abs_section_sym == NULL)
    abs_section_sym = section_symbol (absolute_section);

  /* If the linker is doing the relaxing, we must not do any fixups.

     Well, strictly speaking that's not true -- we could do any that
     are PC-relative and don't cross regions that could change size.
     And for the i960 we might be able to turn callx/callj into bal
     anyways in cases where we know the maximum displacement.  */
  if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
    {
      for (; fixP; fixP = fixP->fx_next)
	if (!fixP->fx_done)
	  {
	    if (fixP->fx_addsy == NULL)
	      {
		/* There was no symbol required by this relocation.
		   However, BFD doesn't really handle relocations
		   without symbols well. So fake up a local symbol in
		   the absolute section.  */
		fixP->fx_addsy = abs_section_sym;
	      }
	    symbol_mark_used_in_reloc (fixP->fx_addsy);
	    if (fixP->fx_subsy != NULL)
	      symbol_mark_used_in_reloc (fixP->fx_subsy);
	  }
930
      return;
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
    }

  for (; fixP; fixP = fixP->fx_next)
    {
#ifdef DEBUG5
      fprintf (stderr, "\nprocessing fixup:\n");
      print_fixup (fixP);
#endif

      fragP = fixP->fx_frag;
      know (fragP);
#ifdef TC_VALIDATE_FIX
      TC_VALIDATE_FIX (fixP, this_segment, skip);
#endif
      add_number = fixP->fx_offset;

      if (fixP->fx_addsy != NULL)
	add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);

      if (fixP->fx_subsy != NULL)
	{
	  segT sub_symbol_segment;
	  resolve_symbol_value (fixP->fx_subsy);
	  sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
	  if (fixP->fx_addsy != NULL
	      && sub_symbol_segment == add_symbol_segment
Richard Sandiford's avatar
gas/  
Richard Sandiford committed
957 958
	      && !S_FORCE_RELOC (fixP->fx_addsy, 0)
	      && !S_FORCE_RELOC (fixP->fx_subsy, 0)
959 960 961 962 963 964 965 966 967 968 969 970 971
	      && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
	    {
	      add_number += S_GET_VALUE (fixP->fx_addsy);
	      add_number -= S_GET_VALUE (fixP->fx_subsy);
	      fixP->fx_offset = add_number;
	      fixP->fx_addsy = NULL;
	      fixP->fx_subsy = NULL;
#ifdef TC_M68K
	      /* See the comment below about 68k weirdness.  */
	      fixP->fx_pcrel = 0;
#endif
	    }
	  else if (sub_symbol_segment == absolute_section
Richard Sandiford's avatar
gas/  
Richard Sandiford committed
972
		   && !S_FORCE_RELOC (fixP->fx_subsy, 0)
973
		   && !TC_FORCE_RELOCATION_SUB_ABS (fixP, add_symbol_segment))
974 975 976 977 978 979
	    {
	      add_number -= S_GET_VALUE (fixP->fx_subsy);
	      fixP->fx_offset = add_number;
	      fixP->fx_subsy = NULL;
	    }
	  else if (sub_symbol_segment == this_segment
Richard Sandiford's avatar
gas/  
Richard Sandiford committed
980
		   && !S_FORCE_RELOC (fixP->fx_subsy, 0)
981
		   && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP, add_symbol_segment))
982 983 984
	    {
	      add_number -= S_GET_VALUE (fixP->fx_subsy);
	      fixP->fx_offset = (add_number + fixP->fx_dot_value
985
				 + fixP->fx_dot_frag->fr_address);
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002

	      /* Make it pc-relative.  If the back-end code has not
		 selected a pc-relative reloc, cancel the adjustment
		 we do later on all pc-relative relocs.  */
	      if (0
#ifdef TC_M68K
		  /* Do this for m68k even if it's already described
		     as pc-relative.  On the m68k, an operand of
		     "pc@(foo-.-2)" should address "foo" in a
		     pc-relative mode.  */
		  || 1
#endif
		  || !fixP->fx_pcrel)
		add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
	      fixP->fx_subsy = NULL;
	      fixP->fx_pcrel = 1;
	    }
1003
	  else if (!TC_VALIDATE_FIX_SUB (fixP, add_symbol_segment))
1004
	    {
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
	      if (!md_register_arithmetic
		  && (add_symbol_segment == reg_section
		      || sub_symbol_segment == reg_section))
		as_bad_where (fixP->fx_file, fixP->fx_line,
			      _("register value used as expression"));
	      else
		as_bad_where (fixP->fx_file, fixP->fx_line,
			      _("can't resolve `%s' {%s section} - `%s' {%s section}"),
			      fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
			      segment_name (add_symbol_segment),
			      S_GET_NAME (fixP->fx_subsy),
			      segment_name (sub_symbol_segment));
1017
	    }
1018 1019 1020 1021
	  else if (sub_symbol_segment != undefined_section
		   && ! bfd_is_com_section (sub_symbol_segment)
		   && MD_APPLY_SYM_VALUE (fixP))
	    add_number -= S_GET_VALUE (fixP->fx_subsy);
1022 1023 1024 1025
	}

      if (fixP->fx_addsy)
	{
1026
	  if (add_symbol_segment == this_segment
Richard Sandiford's avatar
gas/  
Richard Sandiford committed
1027
	      && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
	      && !TC_FORCE_RELOCATION_LOCAL (fixP))
	    {
	      /* This fixup was made when the symbol's segment was
		 SEG_UNKNOWN, but it is now in the local segment.
		 So we know how to do the address without relocation.  */
	      add_number += S_GET_VALUE (fixP->fx_addsy);
	      fixP->fx_offset = add_number;
	      if (fixP->fx_pcrel)
		add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
	      fixP->fx_addsy = NULL;
	      fixP->fx_pcrel = 0;
	    }
	  else if (add_symbol_segment == absolute_section
Richard Sandiford's avatar
gas/  
Richard Sandiford committed
1041
		   && !S_FORCE_RELOC (fixP->fx_addsy, 0)
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
		   && !TC_FORCE_RELOCATION_ABS (fixP))
	    {
	      add_number += S_GET_VALUE (fixP->fx_addsy);
	      fixP->fx_offset = add_number;
	      fixP->fx_addsy = NULL;
	    }
	  else if (add_symbol_segment != undefined_section
		   && ! bfd_is_com_section (add_symbol_segment)
		   && MD_APPLY_SYM_VALUE (fixP))
	    add_number += S_GET_VALUE (fixP->fx_addsy);
	}

      if (fixP->fx_pcrel)
	{
	  add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
	  if (!fixP->fx_done && fixP->fx_addsy == NULL)
	    {
	      /* There was no symbol required by this relocation.
		 However, BFD doesn't really handle relocations
		 without symbols well. So fake up a local symbol in
		 the absolute section.  */
	      fixP->fx_addsy = abs_section_sym;
	    }
	}

      if (!fixP->fx_done)
	md_apply_fix (fixP, &add_number, this_segment);

      if (!fixP->fx_done)
	{
	  if (fixP->fx_addsy == NULL)
	    fixP->fx_addsy = abs_section_sym;
	  symbol_mark_used_in_reloc (fixP->fx_addsy);
	  if (fixP->fx_subsy != NULL)
	    symbol_mark_used_in_reloc (fixP->fx_subsy);
	}

      if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
	{
	  if (fixP->fx_size < sizeof (valueT))
	    {
	      valueT mask;

	      mask = 0;
	      mask--;		/* Set all bits to one.  */
	      mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
	      if ((add_number & mask) != 0 && (add_number & mask) != mask)
		{
		  char buf[50], buf2[50];
		  sprint_value (buf, fragP->fr_address + fixP->fx_where);
		  if (add_number > 1000)
		    sprint_value (buf2, add_number);
		  else
		    sprintf (buf2, "%ld", (long) add_number);
		  as_bad_where (fixP->fx_file, fixP->fx_line,
				_("value of %s too large for field of %d bytes at %s"),
				buf2, fixP->fx_size, buf);
		} /* Generic error checking.  */
	    }
#ifdef WARN_SIGNED_OVERFLOW_WORD
	  /* Warn if a .word value is too large when treated as a signed
	     number.  We already know it is not too negative.  This is to
	     catch over-large switches generated by gcc on the 68k.  */
	  if (!flag_signed_overflow_ok
	      && fixP->fx_size == 2
	      && add_number > 0x7fff)
	    as_bad_where (fixP->fx_file, fixP->fx_line,
			  _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
			  (long) add_number,
			  (long) (fragP->fr_address + fixP->fx_where));
#endif
	}			/* Not a bit fix.  */

#ifdef TC_VALIDATE_FIX
    skip:  ATTRIBUTE_UNUSED_LABEL
      ;
#endif
#ifdef DEBUG5
      fprintf (stderr, "result:\n");
      print_fixup (fixP);
#endif
    }				/* For each fixS in this segment.  */
}

Alan Modra's avatar
Alan Modra committed
1126
static void
1127 1128
fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
	     asection *sec,
1129
	     void *xxx ATTRIBUTE_UNUSED)
Alan Modra's avatar
Alan Modra committed
1130 1131 1132 1133 1134 1135
{
  segment_info_type *seginfo = seg_info (sec);

  fixup_segment (seginfo->fix_root, sec);
}

1136 1137 1138 1139 1140 1141
static void
install_reloc (asection *sec, arelent *reloc, fragS *fragp,
	       char *file, unsigned int line)
{
  char *err;
  bfd_reloc_status_type s;
Alan Modra's avatar
Alan Modra committed
1142 1143 1144 1145 1146 1147
  asymbol *sym;

  if (reloc->sym_ptr_ptr != NULL
      && (sym = *reloc->sym_ptr_ptr) != NULL
      && (sym->flags & BSF_KEEP) == 0
      && ((sym->flags & BSF_SECTION_SYM) == 0
1148 1149
	  || (EMIT_SECTION_SYMBOLS
	      && !bfd_is_abs_section (sym->section))))
Alan Modra's avatar
Alan Modra committed
1150
    as_bad_where (file, line, _("redefined symbol cannot be used on reloc"));
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170

  s = bfd_install_relocation (stdoutput, reloc,
			      fragp->fr_literal, fragp->fr_address,
			      sec, &err);
  switch (s)
    {
    case bfd_reloc_ok:
      break;
    case bfd_reloc_overflow:
      as_bad_where (file, line, _("relocation overflow"));
      break;
    case bfd_reloc_outofrange:
      as_bad_where (file, line, _("relocation out of range"));
      break;
    default:
      as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
		file, line, s);
    }
}

1171 1172 1173 1174 1175 1176
static fragS *
get_frag_for_reloc (fragS *last_frag,
		    const segment_info_type *seginfo,
		    const struct reloc_list *r)
{
  fragS *f;
1177

1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
  for (f = last_frag; f != NULL; f = f->fr_next)
    if (f->fr_address <= r->u.b.r.address
	&& r->u.b.r.address < f->fr_address + f->fr_fix)
      return f;

  for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
    if (f->fr_address <= r->u.b.r.address
	&& r->u.b.r.address < f->fr_address + f->fr_fix)
      return f;

  as_bad_where (r->file, r->line,
		_("reloc not within (fixed part of) section"));
  return NULL;
}

Richard Henderson's avatar
Richard Henderson committed
1193
static void
1194
write_relocs (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
Richard Henderson's avatar
Richard Henderson committed
1195 1196 1197
{
  segment_info_type *seginfo = seg_info (sec);
  unsigned int n;
1198
  struct reloc_list *my_reloc_list, **rp, *r;
Richard Henderson's avatar
Richard Henderson committed
1199 1200
  arelent **relocs;
  fixS *fixp;
1201
  fragS *last_frag;
Richard Henderson's avatar
Richard Henderson committed
1202 1203 1204 1205 1206 1207 1208 1209

  /* If seginfo is NULL, we did not create this section; don't do
     anything with it.  */
  if (seginfo == NULL)
    return;

  n = 0;
  for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
1210 1211 1212 1213 1214 1215
    if (!fixp->fx_done)
      n++;

#ifdef RELOC_EXPANSION_POSSIBLE
  n *= MAX_RELOC_EXPANSION;
#endif
Richard Henderson's avatar
Richard Henderson committed
1216

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
  /* Extract relocs for this section from reloc_list.  */
  rp = &reloc_list;
  my_reloc_list = NULL;
  while ((r = *rp) != NULL)
    {
      if (r->u.b.sec == sec)
	{
	  *rp = r->next;
	  r->next = my_reloc_list;
	  my_reloc_list = r;
	  n++;
	}
      else
	rp = &r->next;
    }

1233
  relocs = (arelent **) xcalloc (n, sizeof (arelent *));
Richard Henderson's avatar
Richard Henderson committed
1234

1235 1236 1237
  n = 0;
  r = my_reloc_list;
  last_frag = NULL;
Richard Henderson's avatar
Richard Henderson committed
1238 1239
  for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
    {
1240 1241
      int fx_size, slack;
      offsetT loc;
1242 1243 1244 1245 1246 1247
      arelent **reloc;
#ifndef RELOC_EXPANSION_POSSIBLE
      arelent *rel;

      reloc = &rel;
#endif
Richard Henderson's avatar
Richard Henderson committed
1248 1249

      if (fixp->fx_done)
1250
	continue;
Richard Henderson's avatar
Richard Henderson committed
1251

1252 1253 1254 1255 1256
      fx_size = fixp->fx_size;
      slack = TC_FX_SIZE_SLACK (fixp);
      if (slack > 0)
	fx_size = fx_size > slack ? fx_size - slack : 0;
      loc = fixp->fx_where + fx_size;
1257
      if (slack >= 0 && loc > fixp->fx_frag->fr_fix)
1258 1259
	as_bad_where (fixp->fx_file, fixp->fx_line,
		      _("internal error: fixup not contained within frag"));
Richard Henderson's avatar
Richard Henderson committed
1260

1261
#ifndef RELOC_EXPANSION_POSSIBLE
1262
      *reloc = tc_gen_reloc (sec, fixp);
1263
#else
1264 1265
      reloc = tc_gen_reloc (sec, fixp);
#endif
Richard Henderson's avatar
Richard Henderson committed
1266

1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
      while (*reloc)
	{
	  while (r != NULL && r->u.b.r.address < (*reloc)->address)
	    {
	      fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
	      if (f != NULL)
		{
		  last_frag = f;
		  relocs[n++] = &r->u.b.r;
		  install_reloc (sec, &r->u.b.r, f, r->file, r->line);
		}
	      r = r->next;
	    }
	  relocs[n++] = *reloc;
	  install_reloc (sec, *reloc, fixp->fx_frag,
			 fixp->fx_file, fixp->fx_line);
#ifndef RELOC_EXPANSION_POSSIBLE
	  break;
#else
	  reloc++;
1287
#endif
1288 1289
	}
    }
1290

1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
  while (r != NULL)
    {
      fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
      if (f != NULL)
	{
	  last_frag = f;
	  relocs[n++] = &r->u.b.r;
	  install_reloc (sec, &r->u.b.r, f, r->file, r->line);
	}
      r = r->next;
Richard Henderson's avatar
Richard Henderson committed
1301 1302 1303 1304
    }

#ifdef DEBUG4
  {
1305
    unsigned int k, j, nsyms;
Richard Henderson's avatar
Richard Henderson committed
1306 1307 1308
    asymbol **sympp;
    sympp = bfd_get_outsymbols (stdoutput);
    nsyms = bfd_get_symcount (stdoutput);
1309 1310
    for (k = 0; k < n; k++)
      if (((*relocs[k]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
Richard Henderson's avatar
Richard Henderson committed
1311 1312
	{
	  for (j = 0; j < nsyms; j++)
1313
	    if (sympp[j] == *relocs[k]->sym_ptr_ptr)
Richard Henderson's avatar
Richard Henderson committed
1314 1315 1316 1317 1318 1319 1320 1321
	      break;
	  if (j == nsyms)
	    abort ();
	}
  }
#endif

  if (n)
1322 1323 1324 1325 1326 1327
    {
      flagword flags = bfd_get_section_flags (abfd, sec);
      flags |= SEC_RELOC;
      bfd_set_section_flags (abfd, sec, flags);
      bfd_set_reloc (stdoutput, sec, relocs, n);
    }
Richard Henderson's avatar
Richard Henderson committed
1328

1329 1330 1331 1332
#ifdef SET_SECTION_RELOCS
  SET_SECTION_RELOCS (sec, relocs, n);
#endif

Richard Henderson's avatar
Richard Henderson committed
1333 1334
#ifdef DEBUG3
  {
1335 1336
    unsigned int k;

Richard Henderson's avatar
Richard Henderson committed
1337
    fprintf (stderr, "relocs for sec %s\n", sec->name);
1338
    for (k = 0; k < n; k++)
Richard Henderson's avatar
Richard Henderson committed
1339
      {
1340 1341
	arelent *rel = relocs[k];
	asymbol *s = *rel->sym_ptr_ptr;
1342
	fprintf (stderr, "  reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
1343 1344
		 k, rel, (unsigned long)rel->address, s->name,
		 (unsigned long)rel->addend);
Richard Henderson's avatar
Richard Henderson committed
1345 1346 1347 1348 1349
      }
  }
#endif
}

Cary Coutant's avatar
Cary Coutant committed
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
static int
compress_frag (struct z_stream_s *strm, const char *contents, int in_size,
	       fragS **last_newf, struct obstack *ob)
{
  int out_size;
  int total_out_size = 0;
  fragS *f = *last_newf;
  char *next_out;
  int avail_out;

  /* Call the compression routine repeatedly until it has finished
     processing the frag.  */
  while (in_size > 0)
    {
      /* Reserve all the space available in the current chunk.
         If none is available, start a new frag.  */
      avail_out = obstack_room (ob);
      if (avail_out <= 0)
        {
          obstack_finish (ob);
          f = frag_alloc (ob);
	  f->fr_type = rs_fill;
          (*last_newf)->fr_next = f;
          *last_newf = f;
          avail_out = obstack_room (ob);
        }
      if (avail_out <= 0)
	as_fatal (_("can't extend frag"));
      next_out = obstack_next_free (ob);
      obstack_blank_fast (ob, avail_out);
      out_size = compress_data (strm, &contents, &in_size,
				&next_out, &avail_out);
      if (out_size < 0)
        return -1;

      f->fr_fix += out_size;
      total_out_size += out_size;

      /* Return unused space.  */
      if (avail_out > 0)
	obstack_blank_fast (ob, -avail_out);
    }

  return total_out_size;
}

static void
compress_debug (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
{
  segment_info_type *seginfo = seg_info (sec);
  fragS *f;
  fragS *first_newf;
  fragS *last_newf;
  struct obstack *ob = &seginfo->frchainP->frch_obstack;
  bfd_size_type uncompressed_size = (bfd_size_type) sec->size;
  bfd_size_type compressed_size;
  const char *section_name;
  char *compressed_name;
  char *header;
  struct z_stream_s *strm;
  int x;
1411
  flagword flags = bfd_get_section_flags (abfd, sec);
Cary Coutant's avatar
Cary Coutant committed
1412 1413

  if (seginfo == NULL
1414
      || sec->size < 32
1415
      || (flags & (SEC_ALLOC | SEC_HAS_CONTENTS)) == SEC_ALLOC)
Cary Coutant's avatar
Cary Coutant committed
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
    return;

  section_name = bfd_get_section_name (stdoutput, sec);
  if (strncmp (section_name, ".debug_", 7) != 0)
    return;

  strm = compress_init ();
  if (strm == NULL)
    return;

  /* Create a new frag to contain the "ZLIB" header.  */
  first_newf = frag_alloc (ob);
  if (obstack_room (ob) < 12)
    first_newf = frag_alloc (ob);
  if (obstack_room (ob) < 12)
    as_fatal (_("can't extend frag %u chars"), 12);
  last_newf = first_newf;
  obstack_blank_fast (ob, 12);
  last_newf->fr_type = rs_fill;
  last_newf->fr_fix = 12;
  header = last_newf->fr_literal;
  memcpy (header, "ZLIB", 4);
  header[11] = uncompressed_size; uncompressed_size >>= 8;
  header[10] = uncompressed_size; uncompressed_size >>= 8;
  header[9] = uncompressed_size; uncompressed_size >>= 8;
  header[8] = uncompressed_size; uncompressed_size >>= 8;
  header[7] = uncompressed_size; uncompressed_size >>= 8;
  header[6] = uncompressed_size; uncompressed_size >>= 8;
  header[5] = uncompressed_size; uncompressed_size >>= 8;
  header[4] = uncompressed_size;
  compressed_size = 12;

  /* Stream the frags through the compression engine, adding new frags
     as necessary to accomodate the compressed output.  */
  for (f = seginfo->frchainP->frch_root;
       f;
       f = f->fr_next)
    {
      offsetT fill_size;
      char *fill_literal;
      offsetT count;
      int out_size;

      gas_assert (f->fr_type == rs_fill);
      if (f->fr_fix)
	{
	  out_size = compress_frag (strm, f->fr_literal, f->fr_fix,
				    &last_newf, ob);
	  if (out_size < 0)
	    return;
	  compressed_size += out_size;
	}
      fill_literal = f->fr_literal + f->fr_fix;
      fill_size = f->fr_var;
      count = f->fr_offset;
      gas_assert (count >= 0);
      if (fill_size && count)
	{
	  while (count--)
	    {
	      out_size = compress_frag (strm, fill_literal, (int) fill_size,
				        &last_newf, ob);
	      if (out_size < 0)
		return;
	      compressed_size += out_size;
	    }
	}
    }

  /* Flush the compression state.  */
  for (;;)
    {
      int avail_out;
      char *next_out;
      int out_size;

      /* Reserve all the space available in the current chunk.
	 If none is available, start a new frag.  */
      avail_out = obstack_room (ob);
      if (avail_out <= 0)
	{
	  fragS *newf;

	  obstack_finish (ob);
	  newf = frag_alloc (ob);
	  newf->fr_type = rs_fill;
	  last_newf->fr_next = newf;
	  last_newf = newf;
	  avail_out = obstack_room (ob);
	}
      if (avail_out <= 0)
	as_fatal (_("can't extend frag"));
      next_out = obstack_next_free (ob);
      obstack_blank_fast (ob, avail_out);
      x = compress_finish (strm, &next_out, &avail_out, &out_size);
      if (x < 0)
	return;

      last_newf->fr_fix += out_size;
      compressed_size += out_size;

      /* Return unused space.  */
      if (avail_out > 0)
	obstack_blank_fast (ob, -avail_out);

      if (x == 0)
	break;
    }

  /* Replace the uncompressed frag list with the compressed frag list.  */
  seginfo->frchainP->frch_root = first_newf;
  seginfo->frchainP->frch_last = last_newf;

  /* Update the section size and its name.  */
  x = bfd_set_section_size (abfd, sec, compressed_size);
  gas_assert (x);
  compressed_name = (char *) xmalloc (strlen (section_name) + 2);
  compressed_name[0] = '.';
  compressed_name[1] = 'z';
  strcpy (compressed_name + 2, section_name + 1);
  bfd_section_name (stdoutput, sec) = compressed_name;
}

Richard Henderson's avatar
Richard Henderson committed
1539
static void
1540 1541
write_contents (bfd *abfd ATTRIBUTE_UNUSED,
		asection *sec,
1542
		void *xxx ATTRIBUTE_UNUSED)
Richard Henderson's avatar
Richard Henderson committed
1543 1544
{
  segment_info_type *seginfo = seg_info (sec);
1545
  addressT offset = 0;
Richard Henderson's avatar
Richard Henderson committed
1546 1547 1548 1549
  fragS *f;

  /* Write out the frags.  */
  if (seginfo == NULL
Nick Clifton's avatar
Nick Clifton committed
1550
      || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
Richard Henderson's avatar
Richard Henderson committed
1551 1552 1553 1554 1555 1556 1557
    return;

  for (f = seginfo->frchainP->frch_root;
       f;
       f = f->fr_next)
    {
      int x;
1558
      addressT fill_size;
Richard Henderson's avatar
Richard Henderson committed
1559
      char *fill_literal;
1560
      offsetT count;
Richard Henderson's avatar
Richard Henderson committed
1561

1562
      gas_assert (f->fr_type == rs_fill);
Richard Henderson's avatar
Richard Henderson committed
1563 1564 1565 1566 1567
      if (f->fr_fix)
	{
	  x = bfd_set_section_contents (stdoutput, sec,
					f->fr_literal, (file_ptr) offset,
					(bfd_size_type) f->fr_fix);
1568
	  if (!x)
1569 1570
	    as_fatal (_("can't write %s: %s"), stdoutput->filename,
		      bfd_errmsg (bfd_get_error ()));
Richard Henderson's avatar
Richard Henderson committed
1571 1572 1573 1574 1575
	  offset += f->fr_fix;
	}
      fill_literal = f->fr_literal + f->fr_fix;
      fill_size = f->fr_var;
      count = f->fr_offset;
1576
      gas_assert (count >= 0);
Richard Henderson's avatar
Richard Henderson committed
1577 1578 1579
      if (fill_size && count)
	{
	  char buf[256];
Nick Clifton's avatar
Nick Clifton committed
1580
	  if (fill_size > sizeof (buf))
Richard Henderson's avatar
Richard Henderson committed
1581
	    {
Nick Clifton's avatar
Nick Clifton committed
1582
	      /* Do it the old way. Can this ever happen?  */
Richard Henderson's avatar
Richard Henderson committed
1583 1584 1585 1586 1587 1588
	      while (count--)
		{
		  x = bfd_set_section_contents (stdoutput, sec,
						fill_literal,
						(file_ptr) offset,
						(bfd_size_type) fill_size);
1589
		  if (!x)
1590 1591
		    as_fatal (_("can't write %s: %s"), stdoutput->filename,
			      bfd_errmsg (bfd_get_error ()));
Richard Henderson's avatar
Richard Henderson committed
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
		  offset += fill_size;
		}
	    }
	  else
	    {
	      /* Build a buffer full of fill objects and output it as
		 often as necessary. This saves on the overhead of
		 potentially lots of bfd_set_section_contents calls.  */
	      int n_per_buf, i;
	      if (fill_size == 1)
		{
		  n_per_buf = sizeof (buf);
		  memset (buf, *fill_literal, n_per_buf);
		}
	      else
		{
		  char *bufp;
Nick Clifton's avatar
Nick Clifton committed
1609
		  n_per_buf = sizeof (buf) / fill_size;
Richard Henderson's avatar
Richard Henderson committed
1610
		  for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
Nick Clifton's avatar
Nick Clifton committed
1611
		    memcpy (bufp, fill_literal, fill_size);
Richard Henderson's avatar
Richard Henderson committed
1612 1613 1614 1615
		}
	      for (; count > 0; count -= n_per_buf)
		{
		  n_per_buf = n_per_buf > count ? count : n_per_buf;
Nick Clifton's avatar
Nick Clifton committed
1616 1617 1618
		  x = bfd_set_section_contents
		    (stdoutput, sec, buf, (file_ptr) offset,
		     (bfd_size_type) n_per_buf * fill_size);
1619
		  if (!x)
1620 1621 1622
		    as_fatal (_("cannot write to output file '%s': %s"),
			      stdoutput->filename,
			      bfd_errmsg (bfd_get_error ()));
Richard Henderson's avatar
Richard Henderson committed
1623 1624 1625 1626 1627 1628 1629 1630
		  offset += n_per_buf * fill_size;
		}
	    }
	}
    }
}

static void
1631
merge_data_into_text (void)
Richard Henderson's avatar
Richard Henderson committed
1632 1633 1634 1635 1636 1637 1638 1639 1640
{
  seg_info (text_section)->frchainP->frch_last->fr_next =
    seg_info (data_section)->frchainP->frch_root;
  seg_info (text_section)->frchainP->frch_last =
    seg_info (data_section)->frchainP->frch_last;
  seg_info (data_section)->frchainP = 0;
}

static void
1641
set_symtab (void)