xref: /openbsd-src/gnu/usr.bin/binutils/gas/write.c (revision 007c2a4539b8b8aaa95c5e73e77620090abe113b)
12159047fSniklas /* write.c - emit .o file
2b55d4692Sfgsch    Copyright 1986, 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3c074d1c9Sdrahn    1998, 1999, 2000, 2001, 2002, 2003
42159047fSniklas    Free Software Foundation, Inc.
52159047fSniklas 
62159047fSniklas    This file is part of GAS, the GNU Assembler.
72159047fSniklas 
82159047fSniklas    GAS is free software; you can redistribute it and/or modify
92159047fSniklas    it under the terms of the GNU General Public License as published by
102159047fSniklas    the Free Software Foundation; either version 2, or (at your option)
112159047fSniklas    any later version.
122159047fSniklas 
132159047fSniklas    GAS is distributed in the hope that it will be useful,
142159047fSniklas    but WITHOUT ANY WARRANTY; without even the implied warranty of
152159047fSniklas    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
162159047fSniklas    GNU General Public License for more details.
172159047fSniklas 
182159047fSniklas    You should have received a copy of the GNU General Public License
19b305b0f1Sespie    along with GAS; see the file COPYING.  If not, write to the Free
20b305b0f1Sespie    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21b305b0f1Sespie    02111-1307, USA.  */
222159047fSniklas 
232159047fSniklas /* This thing should be set up to do byteordering correctly.  But...  */
242159047fSniklas 
252159047fSniklas #include "as.h"
262159047fSniklas #include "subsegs.h"
272159047fSniklas #include "obstack.h"
282159047fSniklas #include "output-file.h"
29b55d4692Sfgsch #include "dwarf2dbg.h"
302159047fSniklas 
312159047fSniklas #ifndef TC_ADJUST_RELOC_COUNT
32c074d1c9Sdrahn #define TC_ADJUST_RELOC_COUNT(FIX, COUNT)
332159047fSniklas #endif
342159047fSniklas 
352159047fSniklas #ifndef TC_FORCE_RELOCATION
36c074d1c9Sdrahn #define TC_FORCE_RELOCATION(FIX)		\
37c074d1c9Sdrahn   (generic_force_reloc (FIX))
382159047fSniklas #endif
392159047fSniklas 
40c074d1c9Sdrahn #ifndef TC_FORCE_RELOCATION_ABS
41c074d1c9Sdrahn #define TC_FORCE_RELOCATION_ABS(FIX)		\
42c074d1c9Sdrahn   (TC_FORCE_RELOCATION (FIX))
43c074d1c9Sdrahn #endif
44c074d1c9Sdrahn 
45c074d1c9Sdrahn #ifndef TC_FORCE_RELOCATION_LOCAL
46c074d1c9Sdrahn #define TC_FORCE_RELOCATION_LOCAL(FIX)		\
47c074d1c9Sdrahn   (!(FIX)->fx_pcrel				\
48c074d1c9Sdrahn    || (FIX)->fx_plt				\
49c074d1c9Sdrahn    || TC_FORCE_RELOCATION (FIX))
50c074d1c9Sdrahn #endif
51c074d1c9Sdrahn 
52c074d1c9Sdrahn #ifndef TC_FORCE_RELOCATION_SUB_SAME
53c074d1c9Sdrahn #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEG)	\
54c074d1c9Sdrahn   (! SEG_NORMAL (SEG))
55c074d1c9Sdrahn #endif
56c074d1c9Sdrahn 
57c074d1c9Sdrahn #ifndef TC_FORCE_RELOCATION_SUB_ABS
58c074d1c9Sdrahn #define TC_FORCE_RELOCATION_SUB_ABS(FIX)	0
59c074d1c9Sdrahn #endif
60c074d1c9Sdrahn 
61c074d1c9Sdrahn #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
62c074d1c9Sdrahn #ifdef DIFF_EXPR_OK
63c074d1c9Sdrahn #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX)	0
64c074d1c9Sdrahn #else
65c074d1c9Sdrahn #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX)	1
66c074d1c9Sdrahn #endif
67c074d1c9Sdrahn #endif
68c074d1c9Sdrahn 
69c074d1c9Sdrahn #ifndef TC_VALIDATE_FIX_SUB
70c074d1c9Sdrahn #ifdef UNDEFINED_DIFFERENCE_OK
71c074d1c9Sdrahn /* The PA needs this for PIC code generation.  */
72c074d1c9Sdrahn #define TC_VALIDATE_FIX_SUB(FIX) 1
73c074d1c9Sdrahn #else
74c074d1c9Sdrahn #ifdef BFD_ASSEMBLER
75c074d1c9Sdrahn #define TC_VALIDATE_FIX_SUB(FIX)		\
76c074d1c9Sdrahn   ((FIX)->fx_r_type == BFD_RELOC_GPREL32	\
77c074d1c9Sdrahn    || (FIX)->fx_r_type == BFD_RELOC_GPREL16)
78c074d1c9Sdrahn #else
79c074d1c9Sdrahn #define TC_VALIDATE_FIX_SUB(FIX) 0
80c074d1c9Sdrahn #endif
81c074d1c9Sdrahn #endif
822159047fSniklas #endif
832159047fSniklas 
84b55d4692Sfgsch #ifndef TC_LINKRELAX_FIXUP
85b55d4692Sfgsch #define TC_LINKRELAX_FIXUP(SEG) 1
86b55d4692Sfgsch #endif
87b55d4692Sfgsch 
88c074d1c9Sdrahn #ifndef MD_APPLY_SYM_VALUE
89c074d1c9Sdrahn #define MD_APPLY_SYM_VALUE(FIX) 1
90c074d1c9Sdrahn #endif
91c074d1c9Sdrahn 
92c074d1c9Sdrahn #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
93c074d1c9Sdrahn #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
94b55d4692Sfgsch #endif
95b55d4692Sfgsch 
962159047fSniklas #ifndef	MD_PCREL_FROM_SECTION
97c074d1c9Sdrahn #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
982159047fSniklas #endif
992159047fSniklas 
1002159047fSniklas #ifndef WORKING_DOT_WORD
101c074d1c9Sdrahn extern const int md_short_jump_size;
102c074d1c9Sdrahn extern const int md_long_jump_size;
1032159047fSniklas #endif
1042159047fSniklas 
105c074d1c9Sdrahn /* Used to control final evaluation of expressions.  */
106c074d1c9Sdrahn int finalize_syms = 0;
107c074d1c9Sdrahn 
1082159047fSniklas int symbol_table_frozen;
109c074d1c9Sdrahn 
110c074d1c9Sdrahn symbolS *abs_section_sym;
111c074d1c9Sdrahn 
112c074d1c9Sdrahn /* Remember the value of dot when parsing expressions.  */
113c074d1c9Sdrahn addressT dot_value;
114c074d1c9Sdrahn 
115*007c2a45Smiod void print_fixup (fixS *);
1162159047fSniklas 
1172159047fSniklas #ifdef BFD_ASSEMBLER
118*007c2a45Smiod static void renumber_sections (bfd *, asection *, PTR);
1192159047fSniklas 
1202159047fSniklas /* We generally attach relocs to frag chains.  However, after we have
1212159047fSniklas    chained these all together into a segment, any relocs we add after
1222159047fSniklas    that must be attached to a segment.  This will include relocs added
1232159047fSniklas    in md_estimate_size_for_relax, for example.  */
1242159047fSniklas static int frags_chained = 0;
1252159047fSniklas #endif
1262159047fSniklas 
1272159047fSniklas #ifndef BFD_ASSEMBLER
1282159047fSniklas 
1292159047fSniklas #ifndef MANY_SEGMENTS
1302159047fSniklas struct frag *text_frag_root;
1312159047fSniklas struct frag *data_frag_root;
1322159047fSniklas struct frag *bss_frag_root;
1332159047fSniklas 
1342159047fSniklas struct frag *text_last_frag;	/* Last frag in segment.  */
1352159047fSniklas struct frag *data_last_frag;	/* Last frag in segment.  */
1362159047fSniklas static struct frag *bss_last_frag;	/* Last frag in segment.  */
1372159047fSniklas #endif
1382159047fSniklas 
1392159047fSniklas #ifndef BFD
1402159047fSniklas static object_headers headers;
1412159047fSniklas #endif
1422159047fSniklas 
1432159047fSniklas long string_byte_count;
1442159047fSniklas char *next_object_file_charP;	/* Tracks object file bytes.  */
1452159047fSniklas 
1462159047fSniklas #ifndef OBJ_VMS
1472159047fSniklas int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
1482159047fSniklas #endif
1492159047fSniklas 
1502159047fSniklas #endif /* BFD_ASSEMBLER  */
1512159047fSniklas 
152191aa565Sniklas static int n_fixups;
153191aa565Sniklas 
1542159047fSniklas #ifdef BFD_ASSEMBLER
155c074d1c9Sdrahn #define RELOC_ENUM enum bfd_reloc_code_real
1562159047fSniklas #else
157c074d1c9Sdrahn #define RELOC_ENUM int
158c074d1c9Sdrahn #endif
159c074d1c9Sdrahn 
160*007c2a45Smiod static fixS *fix_new_internal (fragS *, int where, int size,
1612159047fSniklas 			       symbolS *add, symbolS *sub,
1622159047fSniklas 			       offsetT offset, int pcrel,
163*007c2a45Smiod 			       RELOC_ENUM r_type);
1642159047fSniklas #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
165*007c2a45Smiod static long fixup_segment (fixS *, segT);
1662159047fSniklas #endif
167*007c2a45Smiod static relax_addressT relax_align (relax_addressT addr, int align);
168b305b0f1Sespie #if defined (BFD_ASSEMBLER) || ! defined (BFD)
169*007c2a45Smiod static fragS *chain_frchains_together_1 (segT, struct frchain *);
170b305b0f1Sespie #endif
171b305b0f1Sespie #ifdef BFD_ASSEMBLER
172*007c2a45Smiod static void chain_frchains_together (bfd *, segT, PTR);
173*007c2a45Smiod static void cvt_frag_to_fill (segT, fragS *);
174*007c2a45Smiod static void adjust_reloc_syms (bfd *, asection *, PTR);
175*007c2a45Smiod static void fix_segment (bfd *, asection *, PTR);
176*007c2a45Smiod static void write_relocs (bfd *, asection *, PTR);
177*007c2a45Smiod static void write_contents (bfd *, asection *, PTR);
178*007c2a45Smiod static void set_symtab (void);
179b305b0f1Sespie #endif
180b305b0f1Sespie #if defined (BFD_ASSEMBLER) || (! defined (BFD) && ! defined (OBJ_AOUT))
181*007c2a45Smiod static void merge_data_into_text (void);
182b305b0f1Sespie #endif
183b305b0f1Sespie #if ! defined (BFD_ASSEMBLER) && ! defined (BFD)
184*007c2a45Smiod static void cvt_frag_to_fill (object_headers *, segT, fragS *);
185*007c2a45Smiod static void remove_subsegs (frchainS *, int, fragS **, fragS **);
186*007c2a45Smiod static void relax_and_size_all_segments (void);
187b305b0f1Sespie #endif
1882159047fSniklas 
189b55d4692Sfgsch /* Create a fixS in obstack 'notes'.  */
190b55d4692Sfgsch 
1912159047fSniklas static fixS *
fix_new_internal(fragS * frag,int where,int size,symbolS * add_symbol,symbolS * sub_symbol,offsetT offset,int pcrel,RELOC_ENUM r_type ATTRIBUTE_UNUSED)192*007c2a45Smiod fix_new_internal (fragS *frag,		/* Which frag?  */
193*007c2a45Smiod 		  int where,		/* Where in that frag?  */
194*007c2a45Smiod 		  int size,		/* 1, 2, or 4 usually.  */
195*007c2a45Smiod 		  symbolS *add_symbol,	/* X_add_symbol.  */
196*007c2a45Smiod 		  symbolS *sub_symbol,	/* X_op_symbol.  */
197*007c2a45Smiod 		  offsetT offset,	/* X_add_number.  */
198*007c2a45Smiod 		  int pcrel,		/* TRUE if PC-relative relocation.  */
199*007c2a45Smiod 		  RELOC_ENUM r_type ATTRIBUTE_UNUSED /* Relocation type.  */)
2002159047fSniklas {
2012159047fSniklas   fixS *fixP;
2022159047fSniklas 
203191aa565Sniklas   n_fixups++;
204191aa565Sniklas 
2052159047fSniklas   fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
2062159047fSniklas 
2072159047fSniklas   fixP->fx_frag = frag;
2082159047fSniklas   fixP->fx_where = where;
2092159047fSniklas   fixP->fx_size = size;
2102159047fSniklas   /* We've made fx_size a narrow field; check that it's wide enough.  */
2112159047fSniklas   if (fixP->fx_size != size)
2122159047fSniklas     {
213b305b0f1Sespie       as_bad (_("field fx_size too small to hold %d"), size);
2142159047fSniklas       abort ();
2152159047fSniklas     }
2162159047fSniklas   fixP->fx_addsy = add_symbol;
2172159047fSniklas   fixP->fx_subsy = sub_symbol;
2182159047fSniklas   fixP->fx_offset = offset;
219c074d1c9Sdrahn   fixP->fx_dot_value = dot_value;
2202159047fSniklas   fixP->fx_pcrel = pcrel;
2212159047fSniklas   fixP->fx_plt = 0;
2222159047fSniklas #if defined(NEED_FX_R_TYPE) || defined (BFD_ASSEMBLER)
2232159047fSniklas   fixP->fx_r_type = r_type;
2242159047fSniklas #endif
2252159047fSniklas   fixP->fx_im_disp = 0;
2262159047fSniklas   fixP->fx_pcrel_adjust = 0;
2272159047fSniklas   fixP->fx_bit_fixP = 0;
2282159047fSniklas   fixP->fx_addnumber = 0;
2292159047fSniklas   fixP->fx_tcbit = 0;
2302159047fSniklas   fixP->fx_done = 0;
231b305b0f1Sespie   fixP->fx_no_overflow = 0;
232b305b0f1Sespie   fixP->fx_signed = 0;
233b305b0f1Sespie 
234b305b0f1Sespie #ifdef USING_CGEN
235b305b0f1Sespie   fixP->fx_cgen.insn = NULL;
236b305b0f1Sespie   fixP->fx_cgen.opinfo = 0;
237b305b0f1Sespie #endif
2382159047fSniklas 
2392159047fSniklas #ifdef TC_FIX_TYPE
2402159047fSniklas   TC_INIT_FIX_DATA (fixP);
2412159047fSniklas #endif
2422159047fSniklas 
2432159047fSniklas   as_where (&fixP->fx_file, &fixP->fx_line);
2442159047fSniklas 
2452159047fSniklas   /* Usually, we want relocs sorted numerically, but while
2462159047fSniklas      comparing to older versions of gas that have relocs
2472159047fSniklas      reverse sorted, it is convenient to have this compile
2482159047fSniklas      time option.  xoxorich.  */
2492159047fSniklas   {
2502159047fSniklas 
2512159047fSniklas #ifdef BFD_ASSEMBLER
2522159047fSniklas     fixS **seg_fix_rootP = (frags_chained
2532159047fSniklas 			    ? &seg_info (now_seg)->fix_root
2542159047fSniklas 			    : &frchain_now->fix_root);
2552159047fSniklas     fixS **seg_fix_tailP = (frags_chained
2562159047fSniklas 			    ? &seg_info (now_seg)->fix_tail
2572159047fSniklas 			    : &frchain_now->fix_tail);
2582159047fSniklas #endif
2592159047fSniklas 
2602159047fSniklas #ifdef REVERSE_SORT_RELOCS
2612159047fSniklas 
2622159047fSniklas     fixP->fx_next = *seg_fix_rootP;
2632159047fSniklas     *seg_fix_rootP = fixP;
2642159047fSniklas 
2652159047fSniklas #else /* REVERSE_SORT_RELOCS  */
2662159047fSniklas 
2672159047fSniklas     fixP->fx_next = NULL;
2682159047fSniklas 
2692159047fSniklas     if (*seg_fix_tailP)
2702159047fSniklas       (*seg_fix_tailP)->fx_next = fixP;
2712159047fSniklas     else
2722159047fSniklas       *seg_fix_rootP = fixP;
2732159047fSniklas     *seg_fix_tailP = fixP;
2742159047fSniklas 
2752159047fSniklas #endif /* REVERSE_SORT_RELOCS  */
2762159047fSniklas   }
2772159047fSniklas 
2782159047fSniklas   return fixP;
2792159047fSniklas }
2802159047fSniklas 
2812159047fSniklas /* Create a fixup relative to a symbol (plus a constant).  */
2822159047fSniklas 
2832159047fSniklas fixS *
fix_new(fragS * frag,int where,int size,symbolS * add_symbol,offsetT offset,int pcrel,RELOC_ENUM r_type)284*007c2a45Smiod fix_new (fragS *frag,		/* Which frag?  */
285*007c2a45Smiod 	 int where,			/* Where in that frag?  */
286*007c2a45Smiod 	 int size,			/* 1, 2, or 4 usually.  */
287*007c2a45Smiod 	 symbolS *add_symbol,	/* X_add_symbol.  */
288*007c2a45Smiod 	 offsetT offset,		/* X_add_number.  */
289*007c2a45Smiod 	 int pcrel,			/* TRUE if PC-relative relocation.  */
290*007c2a45Smiod 	 RELOC_ENUM r_type		/* Relocation type.  */)
2912159047fSniklas {
2922159047fSniklas   return fix_new_internal (frag, where, size, add_symbol,
2932159047fSniklas 			   (symbolS *) NULL, offset, pcrel, r_type);
2942159047fSniklas }
2952159047fSniklas 
2962159047fSniklas /* Create a fixup for an expression.  Currently we only support fixups
2972159047fSniklas    for difference expressions.  That is itself more than most object
2982159047fSniklas    file formats support anyhow.  */
2992159047fSniklas 
3002159047fSniklas fixS *
fix_new_exp(fragS * frag,int where,int size,expressionS * exp,int pcrel,RELOC_ENUM r_type)301*007c2a45Smiod fix_new_exp (fragS *frag,		/* Which frag?  */
302*007c2a45Smiod 	     int where,			/* Where in that frag?  */
303*007c2a45Smiod 	     int size,			/* 1, 2, or 4 usually.  */
304*007c2a45Smiod 	     expressionS *exp,		/* Expression.  */
305*007c2a45Smiod 	     int pcrel,			/* TRUE if PC-relative relocation.  */
306*007c2a45Smiod 	     RELOC_ENUM r_type		/* Relocation type.  */)
3072159047fSniklas {
3082159047fSniklas   symbolS *add = NULL;
3092159047fSniklas   symbolS *sub = NULL;
3102159047fSniklas   offsetT off = 0;
3112159047fSniklas 
3122159047fSniklas   switch (exp->X_op)
3132159047fSniklas     {
3142159047fSniklas     case O_absent:
3152159047fSniklas       break;
3162159047fSniklas 
317b55d4692Sfgsch     case O_register:
318b55d4692Sfgsch       as_bad (_("register value used as expression"));
319b55d4692Sfgsch       break;
320b55d4692Sfgsch 
3212159047fSniklas     case O_add:
3222159047fSniklas       /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
3232159047fSniklas 	 the difference expression cannot immediately be reduced.  */
3242159047fSniklas       {
3252159047fSniklas 	symbolS *stmp = make_expr_symbol (exp);
326b55d4692Sfgsch 
3272159047fSniklas 	exp->X_op = O_symbol;
3282159047fSniklas 	exp->X_op_symbol = 0;
3292159047fSniklas 	exp->X_add_symbol = stmp;
3302159047fSniklas 	exp->X_add_number = 0;
331b55d4692Sfgsch 
3322159047fSniklas 	return fix_new_exp (frag, where, size, exp, pcrel, r_type);
3332159047fSniklas       }
3342159047fSniklas 
3352159047fSniklas     case O_symbol_rva:
3362159047fSniklas       add = exp->X_add_symbol;
3372159047fSniklas       off = exp->X_add_number;
3382159047fSniklas 
3392159047fSniklas #if defined(BFD_ASSEMBLER)
3402159047fSniklas       r_type = BFD_RELOC_RVA;
341191aa565Sniklas #else
342191aa565Sniklas #if defined(TC_RVA_RELOC)
3432159047fSniklas       r_type = TC_RVA_RELOC;
3442159047fSniklas #else
345b305b0f1Sespie       as_fatal (_("rva not supported"));
3462159047fSniklas #endif
347191aa565Sniklas #endif
3482159047fSniklas       break;
3492159047fSniklas 
3502159047fSniklas     case O_uminus:
3512159047fSniklas       sub = exp->X_add_symbol;
3522159047fSniklas       off = exp->X_add_number;
3532159047fSniklas       break;
3542159047fSniklas 
3552159047fSniklas     case O_subtract:
3562159047fSniklas       sub = exp->X_op_symbol;
3572159047fSniklas       /* Fall through.  */
3582159047fSniklas     case O_symbol:
3592159047fSniklas       add = exp->X_add_symbol;
3602159047fSniklas       /* Fall through.  */
3612159047fSniklas     case O_constant:
3622159047fSniklas       off = exp->X_add_number;
3632159047fSniklas       break;
3642159047fSniklas 
3652159047fSniklas     default:
366e93f7393Sniklas       add = make_expr_symbol (exp);
367e93f7393Sniklas       break;
3682159047fSniklas     }
3692159047fSniklas 
370b55d4692Sfgsch   return fix_new_internal (frag, where, size, add, sub, off, pcrel, r_type);
3712159047fSniklas }
3722159047fSniklas 
373c074d1c9Sdrahn /* Generic function to determine whether a fixup requires a relocation.  */
374c074d1c9Sdrahn int
generic_force_reloc(fixS * fix)375*007c2a45Smiod generic_force_reloc (fixS *fix)
376c074d1c9Sdrahn {
377c074d1c9Sdrahn #ifdef BFD_ASSEMBLER
378c074d1c9Sdrahn   if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
379c074d1c9Sdrahn       || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
380c074d1c9Sdrahn     return 1;
381c074d1c9Sdrahn #endif
382c074d1c9Sdrahn   return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
383c074d1c9Sdrahn }
384c074d1c9Sdrahn 
3852159047fSniklas /* Append a string onto another string, bumping the pointer along.  */
3862159047fSniklas void
append(char ** charPP,char * fromP,unsigned long length)387*007c2a45Smiod append (char **charPP, char *fromP, unsigned long length)
3882159047fSniklas {
3892159047fSniklas   /* Don't trust memcpy() of 0 chars.  */
3902159047fSniklas   if (length == 0)
3912159047fSniklas     return;
3922159047fSniklas 
3932159047fSniklas   memcpy (*charPP, fromP, length);
3942159047fSniklas   *charPP += length;
3952159047fSniklas }
3962159047fSniklas 
3972159047fSniklas #ifndef BFD_ASSEMBLER
3982159047fSniklas int section_alignment[SEG_MAXIMUM_ORDINAL];
3992159047fSniklas #endif
4002159047fSniklas 
401b55d4692Sfgsch /* This routine records the largest alignment seen for each segment.
402b55d4692Sfgsch    If the beginning of the segment is aligned on the worst-case
403b55d4692Sfgsch    boundary, all of the other alignments within it will work.  At
404b55d4692Sfgsch    least one object format really uses this info.  */
405b55d4692Sfgsch 
4062159047fSniklas void
record_alignment(segT seg,int align)407*007c2a45Smiod record_alignment (/* Segment to which alignment pertains.  */
408*007c2a45Smiod 		  segT seg,
409*007c2a45Smiod 		  /* Alignment, as a power of 2 (e.g., 1 => 2-byte
410*007c2a45Smiod 		     boundary, 2 => 4-byte boundary, etc.)  */
411*007c2a45Smiod 		  int align)
4122159047fSniklas {
4132159047fSniklas   if (seg == absolute_section)
4142159047fSniklas     return;
4152159047fSniklas #ifdef BFD_ASSEMBLER
416b305b0f1Sespie   if ((unsigned int) align > bfd_get_section_alignment (stdoutput, seg))
4172159047fSniklas     bfd_set_section_alignment (stdoutput, seg, align);
4182159047fSniklas #else
4192159047fSniklas   if (align > section_alignment[(int) seg])
4202159047fSniklas     section_alignment[(int) seg] = align;
4212159047fSniklas #endif
4222159047fSniklas }
4232159047fSniklas 
424b55d4692Sfgsch int
get_recorded_alignment(segT seg)425*007c2a45Smiod get_recorded_alignment (segT seg)
426b55d4692Sfgsch {
427b55d4692Sfgsch   if (seg == absolute_section)
428b55d4692Sfgsch     return 0;
429b55d4692Sfgsch #ifdef BFD_ASSEMBLER
430b55d4692Sfgsch   return bfd_get_section_alignment (stdoutput, seg);
431b55d4692Sfgsch #else
432b55d4692Sfgsch   return section_alignment[(int) seg];
433b55d4692Sfgsch #endif
434b55d4692Sfgsch }
435b55d4692Sfgsch 
4362159047fSniklas #ifdef BFD_ASSEMBLER
4372159047fSniklas 
4382159047fSniklas /* Reset the section indices after removing the gas created sections.  */
4392159047fSniklas 
4402159047fSniklas static void
renumber_sections(bfd * abfd ATTRIBUTE_UNUSED,asection * sec,PTR countparg)441*007c2a45Smiod renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, PTR countparg)
4422159047fSniklas {
4432159047fSniklas   int *countp = (int *) countparg;
4442159047fSniklas 
4452159047fSniklas   sec->index = *countp;
4462159047fSniklas   ++*countp;
4472159047fSniklas }
4482159047fSniklas 
4492159047fSniklas #endif /* defined (BFD_ASSEMBLER)  */
4502159047fSniklas 
4512159047fSniklas #if defined (BFD_ASSEMBLER) || ! defined (BFD)
4522159047fSniklas 
4532159047fSniklas static fragS *
chain_frchains_together_1(segT section,struct frchain * frchp)454*007c2a45Smiod chain_frchains_together_1 (segT section, struct frchain *frchp)
4552159047fSniklas {
4562159047fSniklas   fragS dummy, *prev_frag = &dummy;
4572159047fSniklas #ifdef BFD_ASSEMBLER
4582159047fSniklas   fixS fix_dummy, *prev_fix = &fix_dummy;
4592159047fSniklas #endif
4602159047fSniklas 
4612159047fSniklas   for (; frchp && frchp->frch_seg == section; frchp = frchp->frch_next)
4622159047fSniklas     {
4632159047fSniklas       prev_frag->fr_next = frchp->frch_root;
4642159047fSniklas       prev_frag = frchp->frch_last;
4652159047fSniklas       assert (prev_frag->fr_type != 0);
4662159047fSniklas #ifdef BFD_ASSEMBLER
4672159047fSniklas       if (frchp->fix_root != (fixS *) NULL)
4682159047fSniklas 	{
4692159047fSniklas 	  if (seg_info (section)->fix_root == (fixS *) NULL)
4702159047fSniklas 	    seg_info (section)->fix_root = frchp->fix_root;
4712159047fSniklas 	  prev_fix->fx_next = frchp->fix_root;
4722159047fSniklas 	  seg_info (section)->fix_tail = frchp->fix_tail;
4732159047fSniklas 	  prev_fix = frchp->fix_tail;
4742159047fSniklas 	}
4752159047fSniklas #endif
4762159047fSniklas     }
4772159047fSniklas   assert (prev_frag->fr_type != 0);
4782159047fSniklas   prev_frag->fr_next = 0;
4792159047fSniklas   return prev_frag;
4802159047fSniklas }
4812159047fSniklas 
4822159047fSniklas #endif
4832159047fSniklas 
4842159047fSniklas #ifdef BFD_ASSEMBLER
4852159047fSniklas 
4862159047fSniklas static void
chain_frchains_together(bfd * abfd ATTRIBUTE_UNUSED,segT section,PTR xxx ATTRIBUTE_UNUSED)487*007c2a45Smiod chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
488*007c2a45Smiod 			 segT section,
489*007c2a45Smiod 			 PTR xxx ATTRIBUTE_UNUSED)
4902159047fSniklas {
4912159047fSniklas   segment_info_type *info;
4922159047fSniklas 
4932159047fSniklas   /* BFD may have introduced its own sections without using
4942159047fSniklas      subseg_new, so it is possible that seg_info is NULL.  */
4952159047fSniklas   info = seg_info (section);
4962159047fSniklas   if (info != (segment_info_type *) NULL)
4972159047fSniklas     info->frchainP->frch_last
4982159047fSniklas       = chain_frchains_together_1 (section, info->frchainP);
4992159047fSniklas 
5002159047fSniklas   /* Now that we've chained the frags together, we must add new fixups
5012159047fSniklas      to the segment, not to the frag chain.  */
5022159047fSniklas   frags_chained = 1;
5032159047fSniklas }
5042159047fSniklas 
5052159047fSniklas #endif
5062159047fSniklas 
5072159047fSniklas #if !defined (BFD) && !defined (BFD_ASSEMBLER)
5082159047fSniklas 
509b305b0f1Sespie static void
remove_subsegs(frchainS * head,int seg,fragS ** root,fragS ** last)510*007c2a45Smiod remove_subsegs (frchainS *head, int seg, fragS **root, fragS **last)
5112159047fSniklas {
5122159047fSniklas   *root = head->frch_root;
5132159047fSniklas   *last = chain_frchains_together_1 (seg, head);
5142159047fSniklas }
5152159047fSniklas 
5162159047fSniklas #endif /* BFD  */
5172159047fSniklas 
5182159047fSniklas #if defined (BFD_ASSEMBLER) || !defined (BFD)
5192159047fSniklas 
5202159047fSniklas #ifdef BFD_ASSEMBLER
5212159047fSniklas static void
cvt_frag_to_fill(segT sec ATTRIBUTE_UNUSED,fragS * fragP)522*007c2a45Smiod cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
5232159047fSniklas #else
5242159047fSniklas static void
525*007c2a45Smiod cvt_frag_to_fill (object_headers *headersP, segT sec, fragS *fragP)
5262159047fSniklas #endif
5272159047fSniklas {
5282159047fSniklas   switch (fragP->fr_type)
5292159047fSniklas     {
5302159047fSniklas     case rs_align:
5312159047fSniklas     case rs_align_code:
532b55d4692Sfgsch     case rs_align_test:
5332159047fSniklas     case rs_org:
5342159047fSniklas     case rs_space:
5352159047fSniklas #ifdef HANDLE_ALIGN
5362159047fSniklas       HANDLE_ALIGN (fragP);
5372159047fSniklas #endif
5382159047fSniklas       know (fragP->fr_next != NULL);
5392159047fSniklas       fragP->fr_offset = (fragP->fr_next->fr_address
5402159047fSniklas 			  - fragP->fr_address
5412159047fSniklas 			  - fragP->fr_fix) / fragP->fr_var;
5422159047fSniklas       if (fragP->fr_offset < 0)
5432159047fSniklas 	{
544b305b0f1Sespie 	  as_bad_where (fragP->fr_file, fragP->fr_line,
545b305b0f1Sespie 			_("attempt to .org/.space backwards? (%ld)"),
5462159047fSniklas 			(long) fragP->fr_offset);
547c074d1c9Sdrahn 	  fragP->fr_offset = 0;
5482159047fSniklas 	}
5492159047fSniklas       fragP->fr_type = rs_fill;
5502159047fSniklas       break;
5512159047fSniklas 
5522159047fSniklas     case rs_fill:
5532159047fSniklas       break;
5542159047fSniklas 
555b305b0f1Sespie     case rs_leb128:
556b305b0f1Sespie       {
557b305b0f1Sespie 	valueT value = S_GET_VALUE (fragP->fr_symbol);
558b305b0f1Sespie 	int size;
559b305b0f1Sespie 
560b305b0f1Sespie 	size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
561b305b0f1Sespie 			      fragP->fr_subtype);
562b305b0f1Sespie 
563b305b0f1Sespie 	fragP->fr_fix += size;
564b305b0f1Sespie 	fragP->fr_type = rs_fill;
565b305b0f1Sespie 	fragP->fr_var = 0;
566b305b0f1Sespie 	fragP->fr_offset = 0;
567b305b0f1Sespie 	fragP->fr_symbol = NULL;
568b305b0f1Sespie       }
569b305b0f1Sespie       break;
570b305b0f1Sespie 
571b305b0f1Sespie     case rs_cfa:
572b305b0f1Sespie       eh_frame_convert_frag (fragP);
573b305b0f1Sespie       break;
574b305b0f1Sespie 
575b55d4692Sfgsch     case rs_dwarf2dbg:
576b55d4692Sfgsch       dwarf2dbg_convert_frag (fragP);
577b55d4692Sfgsch       break;
578b55d4692Sfgsch 
5792159047fSniklas     case rs_machine_dependent:
5802159047fSniklas #ifdef BFD_ASSEMBLER
5812159047fSniklas       md_convert_frag (stdoutput, sec, fragP);
5822159047fSniklas #else
5832159047fSniklas       md_convert_frag (headersP, sec, fragP);
5842159047fSniklas #endif
5852159047fSniklas 
586b305b0f1Sespie       assert (fragP->fr_next == NULL
587b305b0f1Sespie 	      || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
588b305b0f1Sespie 		  == fragP->fr_fix));
5892159047fSniklas 
590b55d4692Sfgsch       /* After md_convert_frag, we make the frag into a ".space 0".
591b55d4692Sfgsch 	 md_convert_frag() should set up any fixSs and constants
592b55d4692Sfgsch 	 required.  */
5932159047fSniklas       frag_wane (fragP);
5942159047fSniklas       break;
5952159047fSniklas 
5962159047fSniklas #ifndef WORKING_DOT_WORD
5972159047fSniklas     case rs_broken_word:
5982159047fSniklas       {
5992159047fSniklas 	struct broken_word *lie;
6002159047fSniklas 
6012159047fSniklas 	if (fragP->fr_subtype)
6022159047fSniklas 	  {
6032159047fSniklas 	    fragP->fr_fix += md_short_jump_size;
6042159047fSniklas 	    for (lie = (struct broken_word *) (fragP->fr_symbol);
6052159047fSniklas 		 lie && lie->dispfrag == fragP;
6062159047fSniklas 		 lie = lie->next_broken_word)
6072159047fSniklas 	      if (lie->added == 1)
6082159047fSniklas 		fragP->fr_fix += md_long_jump_size;
6092159047fSniklas 	  }
6102159047fSniklas 	frag_wane (fragP);
6112159047fSniklas       }
6122159047fSniklas       break;
6132159047fSniklas #endif
6142159047fSniklas 
6152159047fSniklas     default:
6162159047fSniklas       BAD_CASE (fragP->fr_type);
6172159047fSniklas       break;
6182159047fSniklas     }
6192159047fSniklas }
6202159047fSniklas 
6212159047fSniklas #endif /* defined (BFD_ASSEMBLER) || !defined (BFD)  */
6222159047fSniklas 
6232159047fSniklas #ifdef BFD_ASSEMBLER
624*007c2a45Smiod static void relax_seg (bfd *, asection *, PTR);
625*007c2a45Smiod 
6262159047fSniklas static void
relax_seg(bfd * abfd ATTRIBUTE_UNUSED,asection * sec,PTR xxx)627*007c2a45Smiod relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, PTR xxx)
628b55d4692Sfgsch {
629b55d4692Sfgsch   segment_info_type *seginfo = seg_info (sec);
630b55d4692Sfgsch 
631c074d1c9Sdrahn   if (seginfo && seginfo->frchainP
632c074d1c9Sdrahn       && relax_segment (seginfo->frchainP->frch_root, sec))
633c074d1c9Sdrahn     {
634c074d1c9Sdrahn       int *result = (int *) xxx;
635c074d1c9Sdrahn       *result = 1;
636c074d1c9Sdrahn     }
637b55d4692Sfgsch }
638b55d4692Sfgsch 
639*007c2a45Smiod static void size_seg (bfd *, asection *, PTR);
640*007c2a45Smiod 
641b55d4692Sfgsch static void
size_seg(bfd * abfd,asection * sec,PTR xxx ATTRIBUTE_UNUSED)642*007c2a45Smiod size_seg (bfd *abfd, asection *sec, PTR xxx ATTRIBUTE_UNUSED)
6432159047fSniklas {
6442159047fSniklas   flagword flags;
6452159047fSniklas   fragS *fragp;
6462159047fSniklas   segment_info_type *seginfo;
6472159047fSniklas   int x;
6482159047fSniklas   valueT size, newsize;
6492159047fSniklas 
6502159047fSniklas   subseg_change (sec, 0);
6512159047fSniklas 
6522159047fSniklas   seginfo = seg_info (sec);
6532159047fSniklas   if (seginfo && seginfo->frchainP)
6542159047fSniklas     {
6552159047fSniklas       for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
6562159047fSniklas 	cvt_frag_to_fill (sec, fragp);
6572159047fSniklas       for (fragp = seginfo->frchainP->frch_root;
6582159047fSniklas 	   fragp->fr_next;
6592159047fSniklas 	   fragp = fragp->fr_next)
660b55d4692Sfgsch 	/* Walk to last elt.  */
661b55d4692Sfgsch 	;
6622159047fSniklas       size = fragp->fr_address + fragp->fr_fix;
6632159047fSniklas     }
6642159047fSniklas   else
6652159047fSniklas     size = 0;
6662159047fSniklas 
667b55d4692Sfgsch   flags = bfd_get_section_flags (abfd, sec);
668b55d4692Sfgsch 
6692159047fSniklas   if (size > 0 && ! seginfo->bss)
6702159047fSniklas     flags |= SEC_HAS_CONTENTS;
6712159047fSniklas 
6722159047fSniklas   /* @@ This is just an approximation.  */
6732159047fSniklas   if (seginfo && seginfo->fix_root)
6742159047fSniklas     flags |= SEC_RELOC;
6752159047fSniklas   else
6762159047fSniklas     flags &= ~SEC_RELOC;
6772159047fSniklas   x = bfd_set_section_flags (abfd, sec, flags);
678c074d1c9Sdrahn   assert (x);
6792159047fSniklas 
6802159047fSniklas   newsize = md_section_align (sec, size);
6812159047fSniklas   x = bfd_set_section_size (abfd, sec, newsize);
682c074d1c9Sdrahn   assert (x);
6832159047fSniklas 
6842159047fSniklas   /* If the size had to be rounded up, add some padding in the last
6852159047fSniklas      non-empty frag.  */
6862159047fSniklas   assert (newsize >= size);
6872159047fSniklas   if (size != newsize)
6882159047fSniklas     {
6892159047fSniklas       fragS *last = seginfo->frchainP->frch_last;
6902159047fSniklas       fragp = seginfo->frchainP->frch_root;
6912159047fSniklas       while (fragp->fr_next != last)
6922159047fSniklas 	fragp = fragp->fr_next;
6932159047fSniklas       last->fr_address = size;
694c074d1c9Sdrahn       if ((newsize - size) % fragp->fr_var == 0)
695c074d1c9Sdrahn 	fragp->fr_offset += (newsize - size) / fragp->fr_var;
696c074d1c9Sdrahn       else
697c074d1c9Sdrahn 	/* If we hit this abort, it's likely due to subsegs_finish not
698c074d1c9Sdrahn 	   providing sufficient alignment on the last frag, and the
699c074d1c9Sdrahn 	   machine dependent code using alignment frags with fr_var
700c074d1c9Sdrahn 	   greater than 1.  */
701c074d1c9Sdrahn 	abort ();
7022159047fSniklas     }
7032159047fSniklas 
7042159047fSniklas #ifdef tc_frob_section
7052159047fSniklas   tc_frob_section (sec);
7062159047fSniklas #endif
7072159047fSniklas #ifdef obj_frob_section
7082159047fSniklas   obj_frob_section (sec);
7092159047fSniklas #endif
7102159047fSniklas }
7112159047fSniklas 
7122159047fSniklas #ifdef DEBUG2
7132159047fSniklas static void
dump_section_relocs(abfd,sec,stream_)7142159047fSniklas dump_section_relocs (abfd, sec, stream_)
715b305b0f1Sespie      bfd *abfd ATTRIBUTE_UNUSED;
7162159047fSniklas      asection *sec;
7172159047fSniklas      char *stream_;
7182159047fSniklas {
7192159047fSniklas   FILE *stream = (FILE *) stream_;
7202159047fSniklas   segment_info_type *seginfo = seg_info (sec);
7212159047fSniklas   fixS *fixp = seginfo->fix_root;
7222159047fSniklas 
7232159047fSniklas   if (!fixp)
7242159047fSniklas     return;
7252159047fSniklas 
7262159047fSniklas   fprintf (stream, "sec %s relocs:\n", sec->name);
7272159047fSniklas   while (fixp)
7282159047fSniklas     {
7292159047fSniklas       symbolS *s = fixp->fx_addsy;
730b305b0f1Sespie 
731b305b0f1Sespie       fprintf (stream, "  %08lx: type %d ", (unsigned long) fixp,
732b305b0f1Sespie 	       (int) fixp->fx_r_type);
733b305b0f1Sespie       if (s == NULL)
734b305b0f1Sespie 	fprintf (stream, "no sym\n");
7352159047fSniklas       else
736b305b0f1Sespie 	{
737b305b0f1Sespie 	  print_symbol_value_1 (stream, s);
738b305b0f1Sespie 	  fprintf (stream, "\n");
7392159047fSniklas 	}
7402159047fSniklas       fixp = fixp->fx_next;
7412159047fSniklas     }
7422159047fSniklas }
7432159047fSniklas #else
7440c6d0228Sniklas #define dump_section_relocs(ABFD,SEC,STREAM)	((void) 0)
7452159047fSniklas #endif
7462159047fSniklas 
7472159047fSniklas #ifndef EMIT_SECTION_SYMBOLS
7482159047fSniklas #define EMIT_SECTION_SYMBOLS 1
7492159047fSniklas #endif
7502159047fSniklas 
751c074d1c9Sdrahn /* This pass over fixups decides whether symbols can be replaced with
752c074d1c9Sdrahn    section symbols.  */
753c074d1c9Sdrahn 
7542159047fSniklas static void
adjust_reloc_syms(bfd * abfd ATTRIBUTE_UNUSED,asection * sec,PTR xxx ATTRIBUTE_UNUSED)755*007c2a45Smiod adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
756*007c2a45Smiod 		   asection *sec,
757*007c2a45Smiod 		   PTR xxx ATTRIBUTE_UNUSED)
7582159047fSniklas {
7592159047fSniklas   segment_info_type *seginfo = seg_info (sec);
7602159047fSniklas   fixS *fixp;
7612159047fSniklas 
7622159047fSniklas   if (seginfo == NULL)
7632159047fSniklas     return;
7642159047fSniklas 
7652159047fSniklas   dump_section_relocs (abfd, sec, stderr);
7662159047fSniklas 
7672159047fSniklas   for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
7682159047fSniklas     if (fixp->fx_done)
769b55d4692Sfgsch       /* Ignore it.  */
770b55d4692Sfgsch       ;
7712159047fSniklas     else if (fixp->fx_addsy)
7722159047fSniklas       {
7732159047fSniklas 	symbolS *sym;
7742159047fSniklas 	asection *symsec;
7752159047fSniklas 
7762159047fSniklas #ifdef DEBUG5
7772159047fSniklas 	fprintf (stderr, "\n\nadjusting fixup:\n");
7782159047fSniklas 	print_fixup (fixp);
7792159047fSniklas #endif
7802159047fSniklas 
7812159047fSniklas 	sym = fixp->fx_addsy;
7822159047fSniklas 
783b305b0f1Sespie 	/* All symbols should have already been resolved at this
784b305b0f1Sespie 	   point.  It is possible to see unresolved expression
785b305b0f1Sespie 	   symbols, though, since they are not in the regular symbol
786b305b0f1Sespie 	   table.  */
787c074d1c9Sdrahn 	resolve_symbol_value (sym);
788b305b0f1Sespie 
789b305b0f1Sespie 	if (fixp->fx_subsy != NULL)
790c074d1c9Sdrahn 	  resolve_symbol_value (fixp->fx_subsy);
791b305b0f1Sespie 
792b305b0f1Sespie 	/* If this symbol is equated to an undefined symbol, convert
793b305b0f1Sespie            the fixup to being against that symbol.  */
794c074d1c9Sdrahn 	if (symbol_equated_reloc_p (sym))
795b305b0f1Sespie 	  {
796b305b0f1Sespie 	    fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
797b305b0f1Sespie 	    sym = symbol_get_value_expression (sym)->X_add_symbol;
798b305b0f1Sespie 	    fixp->fx_addsy = sym;
799b305b0f1Sespie 	  }
800b305b0f1Sespie 
801c074d1c9Sdrahn 	if (symbol_mri_common_p (sym))
8022159047fSniklas 	  {
8032159047fSniklas 	    /* These symbols are handled specially in fixup_segment.  */
804c074d1c9Sdrahn 	    continue;
8052159047fSniklas 	  }
8062159047fSniklas 
807c074d1c9Sdrahn 	/* If the symbol is undefined, common, weak, or global (ELF
808c074d1c9Sdrahn 	   shared libs), we can't replace it with the section symbol.  */
809c074d1c9Sdrahn 	if (S_FORCE_RELOC (fixp->fx_addsy, 1))
810c074d1c9Sdrahn 	  continue;
811b305b0f1Sespie 
812c074d1c9Sdrahn 	/* Is there some other (target cpu dependent) reason we can't adjust
813c074d1c9Sdrahn 	   this one?  (E.g. relocations involving function addresses on
814c074d1c9Sdrahn 	   the PA.  */
815c074d1c9Sdrahn #ifdef tc_fix_adjustable
816c074d1c9Sdrahn 	if (! tc_fix_adjustable (fixp))
817c074d1c9Sdrahn 	  continue;
818c074d1c9Sdrahn #endif
819c074d1c9Sdrahn 
820c074d1c9Sdrahn 	/* Since we're reducing to section symbols, don't attempt to reduce
821c074d1c9Sdrahn 	   anything that's already using one.  */
822c074d1c9Sdrahn 	if (symbol_section_p (sym))
823c074d1c9Sdrahn 	  continue;
824c074d1c9Sdrahn 
825c074d1c9Sdrahn 	symsec = S_GET_SEGMENT (sym);
826b305b0f1Sespie 	if (symsec == NULL)
827b305b0f1Sespie 	  abort ();
828b305b0f1Sespie 
8290c6d0228Sniklas 	if (bfd_is_abs_section (symsec))
8300c6d0228Sniklas 	  {
831c074d1c9Sdrahn 	    /* The fixup_segment routine normally will not use this
832c074d1c9Sdrahn                symbol in a relocation.  */
833c074d1c9Sdrahn 	    continue;
8342159047fSniklas 	  }
8352159047fSniklas 
836b305b0f1Sespie 	/* Don't try to reduce relocs which refer to non-local symbols
837b305b0f1Sespie            in .linkonce sections.  It can lead to confusion when a
838b305b0f1Sespie            debugging section refers to a .linkonce section.  I hope
839b305b0f1Sespie            this will always be correct.  */
840b305b0f1Sespie 	if (symsec != sec && ! S_IS_LOCAL (sym))
8412159047fSniklas 	  {
842c074d1c9Sdrahn 	    if ((symsec->flags & SEC_LINK_ONCE) != 0
843c074d1c9Sdrahn 		|| (IS_ELF
844c074d1c9Sdrahn 		    /* The GNU toolchain uses an extension for ELF: a
845c074d1c9Sdrahn 		       section beginning with the magic string
846c074d1c9Sdrahn 		       .gnu.linkonce is a linkonce section.  */
847c074d1c9Sdrahn 		    && strncmp (segment_name (symsec), ".gnu.linkonce",
848c074d1c9Sdrahn 				sizeof ".gnu.linkonce" - 1) == 0))
849c074d1c9Sdrahn 	      continue;
850b305b0f1Sespie 	  }
851b305b0f1Sespie 
852c074d1c9Sdrahn 	/* Never adjust a reloc against local symbol in a merge section
853c074d1c9Sdrahn 	   with non-zero addend.  */
854c074d1c9Sdrahn 	if ((symsec->flags & SEC_MERGE) != 0
855c074d1c9Sdrahn 	    && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
856c074d1c9Sdrahn 	  continue;
857b305b0f1Sespie 
858c074d1c9Sdrahn 	/* Never adjust a reloc against TLS local symbol.  */
859c074d1c9Sdrahn 	if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
860c074d1c9Sdrahn 	  continue;
8612159047fSniklas 
862c074d1c9Sdrahn 	/* We refetch the segment when calling section_symbol, rather
8630c6d0228Sniklas 	   than using symsec, because S_GET_VALUE may wind up changing
8640c6d0228Sniklas 	   the section when it calls resolve_symbol_value.  */
8652159047fSniklas 	fixp->fx_offset += S_GET_VALUE (sym);
8660c6d0228Sniklas 	fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
867b305b0f1Sespie #ifdef DEBUG5
868b305b0f1Sespie 	fprintf (stderr, "\nadjusted fixup:\n");
869b305b0f1Sespie 	print_fixup (fixp);
870b305b0f1Sespie #endif
8712159047fSniklas       }
8722159047fSniklas 
8732159047fSniklas   dump_section_relocs (abfd, sec, stderr);
8742159047fSniklas }
8752159047fSniklas 
8762159047fSniklas static void
fix_segment(bfd * abfd ATTRIBUTE_UNUSED,asection * sec,PTR xxx ATTRIBUTE_UNUSED)877*007c2a45Smiod fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
878*007c2a45Smiod 	     asection *sec,
879*007c2a45Smiod 	     PTR xxx ATTRIBUTE_UNUSED)
880c074d1c9Sdrahn {
881c074d1c9Sdrahn   segment_info_type *seginfo = seg_info (sec);
882c074d1c9Sdrahn 
883c074d1c9Sdrahn   fixup_segment (seginfo->fix_root, sec);
884c074d1c9Sdrahn }
885c074d1c9Sdrahn 
886c074d1c9Sdrahn static void
write_relocs(bfd * abfd,asection * sec,PTR xxx ATTRIBUTE_UNUSED)887*007c2a45Smiod write_relocs (bfd *abfd, asection *sec, PTR xxx ATTRIBUTE_UNUSED)
8882159047fSniklas {
8892159047fSniklas   segment_info_type *seginfo = seg_info (sec);
890b55d4692Sfgsch   unsigned int i;
8912159047fSniklas   unsigned int n;
8922159047fSniklas   arelent **relocs;
8932159047fSniklas   fixS *fixp;
8942159047fSniklas   char *err;
8952159047fSniklas 
8962159047fSniklas   /* If seginfo is NULL, we did not create this section; don't do
8972159047fSniklas      anything with it.  */
8982159047fSniklas   if (seginfo == NULL)
8992159047fSniklas     return;
9002159047fSniklas 
9012159047fSniklas   n = 0;
9022159047fSniklas   for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
9032159047fSniklas     n++;
9042159047fSniklas 
9052159047fSniklas #ifndef RELOC_EXPANSION_POSSIBLE
9062159047fSniklas   /* Set up reloc information as well.  */
907*007c2a45Smiod   relocs = (arelent **) xcalloc (n, sizeof (arelent *));
9082159047fSniklas 
9092159047fSniklas   i = 0;
9102159047fSniklas   for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
9112159047fSniklas     {
9122159047fSniklas       arelent *reloc;
9132159047fSniklas       bfd_reloc_status_type s;
914191aa565Sniklas       symbolS *sym;
9152159047fSniklas 
9162159047fSniklas       if (fixp->fx_done)
9172159047fSniklas 	{
9182159047fSniklas 	  n--;
9192159047fSniklas 	  continue;
9202159047fSniklas 	}
921191aa565Sniklas 
922191aa565Sniklas       /* If this is an undefined symbol which was equated to another
923c074d1c9Sdrahn          symbol, then generate the reloc against the latter symbol
924191aa565Sniklas          rather than the former.  */
925191aa565Sniklas       sym = fixp->fx_addsy;
926c074d1c9Sdrahn       while (symbol_equated_reloc_p (sym))
927b305b0f1Sespie 	{
928b305b0f1Sespie 	  symbolS *n;
929b305b0f1Sespie 
930b305b0f1Sespie 	  /* We must avoid looping, as that can occur with a badly
931b305b0f1Sespie 	     written program.  */
932b305b0f1Sespie 	  n = symbol_get_value_expression (sym)->X_add_symbol;
933b305b0f1Sespie 	  if (n == sym)
934b305b0f1Sespie 	    break;
935b305b0f1Sespie 	  fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
936b305b0f1Sespie 	  sym = n;
937b305b0f1Sespie 	}
938191aa565Sniklas       fixp->fx_addsy = sym;
939191aa565Sniklas 
9402159047fSniklas       reloc = tc_gen_reloc (sec, fixp);
9412159047fSniklas       if (!reloc)
9422159047fSniklas 	{
9432159047fSniklas 	  n--;
9442159047fSniklas 	  continue;
9452159047fSniklas 	}
9460c6d0228Sniklas 
9470c6d0228Sniklas #if 0
9480c6d0228Sniklas       /* This test is triggered inappropriately for the SH.  */
9492159047fSniklas       if (fixp->fx_where + fixp->fx_size
9502159047fSniklas 	  > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
9512159047fSniklas 	abort ();
9520c6d0228Sniklas #endif
9532159047fSniklas 
9542159047fSniklas       s = bfd_install_relocation (stdoutput, reloc,
9552159047fSniklas 				  fixp->fx_frag->fr_literal,
9562159047fSniklas 				  fixp->fx_frag->fr_address,
9572159047fSniklas 				  sec, &err);
9582159047fSniklas       switch (s)
9592159047fSniklas 	{
9602159047fSniklas 	case bfd_reloc_ok:
9612159047fSniklas 	  break;
9622159047fSniklas 	case bfd_reloc_overflow:
963c074d1c9Sdrahn 	  as_bad_where (fixp->fx_file, fixp->fx_line,
964c074d1c9Sdrahn 			_("relocation overflow"));
965b305b0f1Sespie 	  break;
966b305b0f1Sespie 	case bfd_reloc_outofrange:
967c074d1c9Sdrahn 	  as_bad_where (fixp->fx_file, fixp->fx_line,
968c074d1c9Sdrahn 			_("relocation out of range"));
9692159047fSniklas 	  break;
9702159047fSniklas 	default:
971b305b0f1Sespie 	  as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
972b305b0f1Sespie 		    fixp->fx_file, fixp->fx_line, s);
9732159047fSniklas 	}
9742159047fSniklas       relocs[i++] = reloc;
9752159047fSniklas     }
9762159047fSniklas #else
9772159047fSniklas   n = n * MAX_RELOC_EXPANSION;
9782159047fSniklas   /* Set up reloc information as well.  */
979*007c2a45Smiod   relocs = (arelent **) xcalloc (n, sizeof (arelent *));
9802159047fSniklas 
9812159047fSniklas   i = 0;
9822159047fSniklas   for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
9832159047fSniklas     {
9842159047fSniklas       arelent **reloc;
9852159047fSniklas       bfd_reloc_status_type s;
986191aa565Sniklas       symbolS *sym;
9872159047fSniklas       int j;
9882159047fSniklas 
9892159047fSniklas       if (fixp->fx_done)
9902159047fSniklas 	{
9912159047fSniklas 	  n--;
9922159047fSniklas 	  continue;
9932159047fSniklas 	}
994191aa565Sniklas 
995191aa565Sniklas       /* If this is an undefined symbol which was equated to another
996b55d4692Sfgsch          symbol, then generate the reloc against the latter symbol
997191aa565Sniklas          rather than the former.  */
998191aa565Sniklas       sym = fixp->fx_addsy;
999c074d1c9Sdrahn       while (symbol_equated_reloc_p (sym))
1000c074d1c9Sdrahn 	{
1001c074d1c9Sdrahn 	  symbolS *n;
1002c074d1c9Sdrahn 
1003c074d1c9Sdrahn 	  /* We must avoid looping, as that can occur with a badly
1004c074d1c9Sdrahn 	     written program.  */
1005c074d1c9Sdrahn 	  n = symbol_get_value_expression (sym)->X_add_symbol;
1006c074d1c9Sdrahn 	  if (n == sym)
1007c074d1c9Sdrahn 	    break;
1008c074d1c9Sdrahn 	  fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
1009c074d1c9Sdrahn 	  sym = n;
1010c074d1c9Sdrahn 	}
1011191aa565Sniklas       fixp->fx_addsy = sym;
1012191aa565Sniklas 
10132159047fSniklas       reloc = tc_gen_reloc (sec, fixp);
10142159047fSniklas 
10152159047fSniklas       for (j = 0; reloc[j]; j++)
10162159047fSniklas 	{
10172159047fSniklas 	  relocs[i++] = reloc[j];
10182159047fSniklas 	  assert (i <= n);
10192159047fSniklas 	}
10202159047fSniklas       if (fixp->fx_where + fixp->fx_size
10212159047fSniklas 	  > fixp->fx_frag->fr_fix + fixp->fx_frag->fr_offset)
1022b305b0f1Sespie 	as_bad_where (fixp->fx_file, fixp->fx_line,
1023b305b0f1Sespie 		      _("internal error: fixup not contained within frag"));
10242159047fSniklas       for (j = 0; reloc[j]; j++)
10252159047fSniklas 	{
10262159047fSniklas 	  s = bfd_install_relocation (stdoutput, reloc[j],
10272159047fSniklas 				      fixp->fx_frag->fr_literal,
10282159047fSniklas 				      fixp->fx_frag->fr_address,
10292159047fSniklas 				      sec, &err);
10302159047fSniklas 	  switch (s)
10312159047fSniklas 	    {
10322159047fSniklas 	    case bfd_reloc_ok:
10332159047fSniklas 	      break;
10342159047fSniklas 	    case bfd_reloc_overflow:
10352159047fSniklas 	      as_bad_where (fixp->fx_file, fixp->fx_line,
1036b305b0f1Sespie 			    _("relocation overflow"));
10372159047fSniklas 	      break;
1038c074d1c9Sdrahn 	    case bfd_reloc_outofrange:
1039c074d1c9Sdrahn 	      as_bad_where (fixp->fx_file, fixp->fx_line,
1040c074d1c9Sdrahn 			    _("relocation out of range"));
1041c074d1c9Sdrahn 	      break;
10422159047fSniklas 	    default:
1043c074d1c9Sdrahn 	      as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
1044c074d1c9Sdrahn 			fixp->fx_file, fixp->fx_line, s);
10452159047fSniklas 	    }
10462159047fSniklas 	}
10472159047fSniklas     }
10482159047fSniklas   n = i;
10492159047fSniklas #endif
10502159047fSniklas 
10512159047fSniklas #ifdef DEBUG4
10522159047fSniklas   {
10532159047fSniklas     int i, j, nsyms;
10542159047fSniklas     asymbol **sympp;
10552159047fSniklas     sympp = bfd_get_outsymbols (stdoutput);
10562159047fSniklas     nsyms = bfd_get_symcount (stdoutput);
10572159047fSniklas     for (i = 0; i < n; i++)
10582159047fSniklas       if (((*relocs[i]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
10592159047fSniklas 	{
10602159047fSniklas 	  for (j = 0; j < nsyms; j++)
10612159047fSniklas 	    if (sympp[j] == *relocs[i]->sym_ptr_ptr)
10622159047fSniklas 	      break;
10632159047fSniklas 	  if (j == nsyms)
10642159047fSniklas 	    abort ();
10652159047fSniklas 	}
10662159047fSniklas   }
10672159047fSniklas #endif
10682159047fSniklas 
10692159047fSniklas   if (n)
10702159047fSniklas     bfd_set_reloc (stdoutput, sec, relocs, n);
10712159047fSniklas   else
10722159047fSniklas     bfd_set_section_flags (abfd, sec,
10732159047fSniklas 			   (bfd_get_section_flags (abfd, sec)
10742159047fSniklas 			    & (flagword) ~SEC_RELOC));
10752159047fSniklas 
1076b305b0f1Sespie #ifdef SET_SECTION_RELOCS
1077b305b0f1Sespie   SET_SECTION_RELOCS (sec, relocs, n);
1078b305b0f1Sespie #endif
1079b305b0f1Sespie 
10802159047fSniklas #ifdef DEBUG3
10812159047fSniklas   {
10822159047fSniklas     int i;
10832159047fSniklas     arelent *r;
10842159047fSniklas     asymbol *s;
10852159047fSniklas     fprintf (stderr, "relocs for sec %s\n", sec->name);
10862159047fSniklas     for (i = 0; i < n; i++)
10872159047fSniklas       {
10882159047fSniklas 	r = relocs[i];
10892159047fSniklas 	s = *r->sym_ptr_ptr;
10902159047fSniklas 	fprintf (stderr, "  reloc %2d @%08x off %4x : sym %-10s addend %x\n",
10912159047fSniklas 		 i, r, r->address, s->name, r->addend);
10922159047fSniklas       }
10932159047fSniklas   }
10942159047fSniklas #endif
10952159047fSniklas }
10962159047fSniklas 
10972159047fSniklas static void
write_contents(bfd * abfd ATTRIBUTE_UNUSED,asection * sec,PTR xxx ATTRIBUTE_UNUSED)1098*007c2a45Smiod write_contents (bfd *abfd ATTRIBUTE_UNUSED,
1099*007c2a45Smiod 		asection *sec,
1100*007c2a45Smiod 		PTR xxx ATTRIBUTE_UNUSED)
11012159047fSniklas {
11022159047fSniklas   segment_info_type *seginfo = seg_info (sec);
1103*007c2a45Smiod   addressT offset = 0;
11042159047fSniklas   fragS *f;
11052159047fSniklas 
11062159047fSniklas   /* Write out the frags.  */
11072159047fSniklas   if (seginfo == NULL
11082159047fSniklas       || !(bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS))
11092159047fSniklas     return;
11102159047fSniklas 
11112159047fSniklas   for (f = seginfo->frchainP->frch_root;
11122159047fSniklas        f;
11132159047fSniklas        f = f->fr_next)
11142159047fSniklas     {
11152159047fSniklas       int x;
1116*007c2a45Smiod       addressT fill_size;
11172159047fSniklas       char *fill_literal;
1118*007c2a45Smiod       offsetT count;
11192159047fSniklas 
11202159047fSniklas       assert (f->fr_type == rs_fill);
11212159047fSniklas       if (f->fr_fix)
11222159047fSniklas 	{
11232159047fSniklas 	  x = bfd_set_section_contents (stdoutput, sec,
11242159047fSniklas 					f->fr_literal, (file_ptr) offset,
11252159047fSniklas 					(bfd_size_type) f->fr_fix);
1126c074d1c9Sdrahn 	  if (!x)
11272159047fSniklas 	    {
11282159047fSniklas 	      bfd_perror (stdoutput->filename);
1129b305b0f1Sespie 	      as_perror (_("FATAL: Can't write %s"), stdoutput->filename);
11302159047fSniklas 	      exit (EXIT_FAILURE);
11312159047fSniklas 	    }
11322159047fSniklas 	  offset += f->fr_fix;
11332159047fSniklas 	}
11342159047fSniklas       fill_literal = f->fr_literal + f->fr_fix;
11352159047fSniklas       fill_size = f->fr_var;
11362159047fSniklas       count = f->fr_offset;
11372159047fSniklas       assert (count >= 0);
11382159047fSniklas       if (fill_size && count)
11392159047fSniklas 	{
11402159047fSniklas 	  char buf[256];
11412159047fSniklas 	  if (fill_size > sizeof (buf))
11422159047fSniklas 	    {
11432159047fSniklas 	      /* Do it the old way. Can this ever happen?  */
11442159047fSniklas 	      while (count--)
11452159047fSniklas 		{
11462159047fSniklas 		  x = bfd_set_section_contents (stdoutput, sec,
11472159047fSniklas 						fill_literal,
11482159047fSniklas 						(file_ptr) offset,
11492159047fSniklas 						(bfd_size_type) fill_size);
1150c074d1c9Sdrahn 		  if (!x)
11512159047fSniklas 		    {
11522159047fSniklas 		      bfd_perror (stdoutput->filename);
1153b55d4692Sfgsch 		      as_perror (_("FATAL: Can't write %s"),
1154b55d4692Sfgsch 				 stdoutput->filename);
11552159047fSniklas 		      exit (EXIT_FAILURE);
11562159047fSniklas 		    }
11572159047fSniklas 		  offset += fill_size;
11582159047fSniklas 		}
11592159047fSniklas 	    }
11602159047fSniklas 	  else
11612159047fSniklas 	    {
11622159047fSniklas 	      /* Build a buffer full of fill objects and output it as
11632159047fSniklas 		 often as necessary. This saves on the overhead of
11642159047fSniklas 		 potentially lots of bfd_set_section_contents calls.  */
11652159047fSniklas 	      int n_per_buf, i;
11662159047fSniklas 	      if (fill_size == 1)
11672159047fSniklas 		{
11682159047fSniklas 		  n_per_buf = sizeof (buf);
11692159047fSniklas 		  memset (buf, *fill_literal, n_per_buf);
11702159047fSniklas 		}
11712159047fSniklas 	      else
11722159047fSniklas 		{
11732159047fSniklas 		  char *bufp;
11742159047fSniklas 		  n_per_buf = sizeof (buf) / fill_size;
11752159047fSniklas 		  for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
11762159047fSniklas 		    memcpy (bufp, fill_literal, fill_size);
11772159047fSniklas 		}
11782159047fSniklas 	      for (; count > 0; count -= n_per_buf)
11792159047fSniklas 		{
11802159047fSniklas 		  n_per_buf = n_per_buf > count ? count : n_per_buf;
1181b55d4692Sfgsch 		  x = bfd_set_section_contents
1182b55d4692Sfgsch 		    (stdoutput, sec, buf, (file_ptr) offset,
11832159047fSniklas 		     (bfd_size_type) n_per_buf * fill_size);
1184c074d1c9Sdrahn 		  if (!x)
1185c074d1c9Sdrahn 		    as_fatal (_("cannot write to output file"));
11862159047fSniklas 		  offset += n_per_buf * fill_size;
11872159047fSniklas 		}
11882159047fSniklas 	    }
11892159047fSniklas 	}
11902159047fSniklas     }
11912159047fSniklas }
11922159047fSniklas #endif
11932159047fSniklas 
11942159047fSniklas #if defined(BFD_ASSEMBLER) || (!defined (BFD) && !defined(OBJ_AOUT))
11952159047fSniklas static void
merge_data_into_text(void)1196*007c2a45Smiod merge_data_into_text (void)
11972159047fSniklas {
11982159047fSniklas #if defined(BFD_ASSEMBLER) || defined(MANY_SEGMENTS)
11992159047fSniklas   seg_info (text_section)->frchainP->frch_last->fr_next =
12002159047fSniklas     seg_info (data_section)->frchainP->frch_root;
12012159047fSniklas   seg_info (text_section)->frchainP->frch_last =
12022159047fSniklas     seg_info (data_section)->frchainP->frch_last;
12032159047fSniklas   seg_info (data_section)->frchainP = 0;
12042159047fSniklas #else
12052159047fSniklas   fixS *tmp;
12062159047fSniklas 
12072159047fSniklas   text_last_frag->fr_next = data_frag_root;
12082159047fSniklas   text_last_frag = data_last_frag;
12092159047fSniklas   data_last_frag = NULL;
12102159047fSniklas   data_frag_root = NULL;
12112159047fSniklas   if (text_fix_root)
12122159047fSniklas     {
12132159047fSniklas       for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next);;
12142159047fSniklas       tmp->fx_next = data_fix_root;
12152159047fSniklas       text_fix_tail = data_fix_tail;
12162159047fSniklas     }
12172159047fSniklas   else
12182159047fSniklas     text_fix_root = data_fix_root;
12192159047fSniklas   data_fix_root = NULL;
12202159047fSniklas #endif
12212159047fSniklas }
12222159047fSniklas #endif /* BFD_ASSEMBLER || (! BFD && ! OBJ_AOUT)  */
12232159047fSniklas 
12242159047fSniklas #if !defined (BFD_ASSEMBLER) && !defined (BFD)
12252159047fSniklas static void
relax_and_size_all_segments()12262159047fSniklas relax_and_size_all_segments ()
12272159047fSniklas {
12282159047fSniklas   fragS *fragP;
12292159047fSniklas 
12302159047fSniklas   relax_segment (text_frag_root, SEG_TEXT);
12312159047fSniklas   relax_segment (data_frag_root, SEG_DATA);
12322159047fSniklas   relax_segment (bss_frag_root, SEG_BSS);
12332159047fSniklas 
1234b55d4692Sfgsch   /* Now the addresses of frags are correct within the segment.  */
12352159047fSniklas   know (text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
12362159047fSniklas   H_SET_TEXT_SIZE (&headers, text_last_frag->fr_address);
12372159047fSniklas   text_last_frag->fr_address = H_GET_TEXT_SIZE (&headers);
12382159047fSniklas 
1239b55d4692Sfgsch   /* Join the 2 segments into 1 huge segment.
1240b55d4692Sfgsch      To do this, re-compute every rn_address in the SEG_DATA frags.
1241b55d4692Sfgsch      Then join the data frags after the text frags.
1242b55d4692Sfgsch 
1243b55d4692Sfgsch      Determine a_data [length of data segment].  */
12442159047fSniklas   if (data_frag_root)
12452159047fSniklas     {
12462159047fSniklas       register relax_addressT slide;
12472159047fSniklas 
1248b55d4692Sfgsch       know ((text_last_frag->fr_type == rs_fill)
1249b55d4692Sfgsch 	    && (text_last_frag->fr_offset == 0));
12502159047fSniklas 
12512159047fSniklas       H_SET_DATA_SIZE (&headers, data_last_frag->fr_address);
12522159047fSniklas       data_last_frag->fr_address = H_GET_DATA_SIZE (&headers);
12532159047fSniklas       slide = H_GET_TEXT_SIZE (&headers);	/* & in file of the data segment.  */
12542159047fSniklas #ifdef OBJ_BOUT
12552159047fSniklas #define RoundUp(N,S) (((N)+(S)-1)&-(S))
12562159047fSniklas       /* For b.out: If the data section has a strict alignment
12572159047fSniklas 	 requirement, its load address in the .o file will be
12582159047fSniklas 	 rounded up from the size of the text section.  These
12592159047fSniklas 	 two values are *not* the same!  Similarly for the bss
12602159047fSniklas 	 section....  */
12612159047fSniklas       slide = RoundUp (slide, 1 << section_alignment[SEG_DATA]);
12622159047fSniklas #endif
12632159047fSniklas 
12642159047fSniklas       for (fragP = data_frag_root; fragP; fragP = fragP->fr_next)
12652159047fSniklas 	fragP->fr_address += slide;
12662159047fSniklas 
12672159047fSniklas       know (text_last_frag != 0);
12682159047fSniklas       text_last_frag->fr_next = data_frag_root;
12692159047fSniklas     }
12702159047fSniklas   else
12712159047fSniklas     {
12722159047fSniklas       H_SET_DATA_SIZE (&headers, 0);
12732159047fSniklas     }
12742159047fSniklas 
12752159047fSniklas #ifdef OBJ_BOUT
12762159047fSniklas   /* See above comments on b.out data section address.  */
12772159047fSniklas   {
1278*007c2a45Smiod     addressT bss_vma;
12792159047fSniklas     if (data_last_frag == 0)
12802159047fSniklas       bss_vma = H_GET_TEXT_SIZE (&headers);
12812159047fSniklas     else
12822159047fSniklas       bss_vma = data_last_frag->fr_address;
12832159047fSniklas     bss_vma = RoundUp (bss_vma, 1 << section_alignment[SEG_BSS]);
12842159047fSniklas     bss_address_frag.fr_address = bss_vma;
12852159047fSniklas   }
12862159047fSniklas #else /* ! OBJ_BOUT  */
12872159047fSniklas   bss_address_frag.fr_address = (H_GET_TEXT_SIZE (&headers) +
12882159047fSniklas 				 H_GET_DATA_SIZE (&headers));
12892159047fSniklas 
12902159047fSniklas #endif /* ! OBJ_BOUT  */
12912159047fSniklas 
1292b55d4692Sfgsch   /* Slide all the frags.  */
12932159047fSniklas   if (bss_frag_root)
12942159047fSniklas     {
12952159047fSniklas       relax_addressT slide = bss_address_frag.fr_address;
12962159047fSniklas 
12972159047fSniklas       for (fragP = bss_frag_root; fragP; fragP = fragP->fr_next)
12982159047fSniklas 	fragP->fr_address += slide;
12992159047fSniklas     }
13002159047fSniklas 
13012159047fSniklas   if (bss_last_frag)
13022159047fSniklas     H_SET_BSS_SIZE (&headers,
13032159047fSniklas 		    bss_last_frag->fr_address - bss_frag_root->fr_address);
13042159047fSniklas   else
13052159047fSniklas     H_SET_BSS_SIZE (&headers, 0);
13062159047fSniklas }
13072159047fSniklas #endif /* ! BFD_ASSEMBLER && ! BFD  */
13082159047fSniklas 
13092159047fSniklas #if defined (BFD_ASSEMBLER) || !defined (BFD)
13102159047fSniklas 
13112159047fSniklas #ifdef BFD_ASSEMBLER
13122159047fSniklas static void
set_symtab(void)1313*007c2a45Smiod set_symtab (void)
13142159047fSniklas {
13152159047fSniklas   int nsyms;
13162159047fSniklas   asymbol **asympp;
13172159047fSniklas   symbolS *symp;
1318c074d1c9Sdrahn   bfd_boolean result;
1319*007c2a45Smiod   extern PTR bfd_alloc (bfd *, bfd_size_type);
13202159047fSniklas 
13212159047fSniklas   /* Count symbols.  We can't rely on a count made by the loop in
13222159047fSniklas      write_object_file, because *_frob_file may add a new symbol or
13232159047fSniklas      two.  */
13242159047fSniklas   nsyms = 0;
13252159047fSniklas   for (symp = symbol_rootP; symp; symp = symbol_next (symp))
13262159047fSniklas     nsyms++;
13272159047fSniklas 
13282159047fSniklas   if (nsyms)
13292159047fSniklas     {
13302159047fSniklas       int i;
1331c074d1c9Sdrahn       bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
13322159047fSniklas 
1333c074d1c9Sdrahn       asympp = (asymbol **) bfd_alloc (stdoutput, amt);
13342159047fSniklas       symp = symbol_rootP;
13352159047fSniklas       for (i = 0; i < nsyms; i++, symp = symbol_next (symp))
13362159047fSniklas 	{
1337b305b0f1Sespie 	  asympp[i] = symbol_get_bfdsym (symp);
1338b305b0f1Sespie 	  symbol_mark_written (symp);
13392159047fSniklas 	}
13402159047fSniklas     }
13412159047fSniklas   else
13422159047fSniklas     asympp = 0;
13432159047fSniklas   result = bfd_set_symtab (stdoutput, asympp, nsyms);
1344c074d1c9Sdrahn   assert (result);
13452159047fSniklas   symbol_table_frozen = 1;
13462159047fSniklas }
13472159047fSniklas #endif
13482159047fSniklas 
1349b305b0f1Sespie /* Finish the subsegments.  After every sub-segment, we fake an
1350b305b0f1Sespie    ".align ...".  This conforms to BSD4.2 brane-damage.  We then fake
1351b305b0f1Sespie    ".fill 0" because that is the kind of frag that requires least
1352b305b0f1Sespie    thought.  ".align" frags like to have a following frag since that
1353b305b0f1Sespie    makes calculating their intended length trivial.  */
1354b305b0f1Sespie 
1355b305b0f1Sespie #ifndef SUB_SEGMENT_ALIGN
1356c074d1c9Sdrahn #ifdef HANDLE_ALIGN
1357*007c2a45Smiod /* The last subsegment gets an alignment corresponding to the alignment
1358c074d1c9Sdrahn    of the section.  This allows proper nop-filling at the end of
1359c074d1c9Sdrahn    code-bearing sections.  */
1360c074d1c9Sdrahn #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN)					\
1361c074d1c9Sdrahn   (!(FRCHAIN)->frch_next || (FRCHAIN)->frch_next->frch_seg != (SEG)	\
1362c074d1c9Sdrahn    ? get_recorded_alignment (SEG) : 0)
1363b305b0f1Sespie #else
1364c074d1c9Sdrahn #ifdef BFD_ASSEMBLER
1365c074d1c9Sdrahn #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
1366c074d1c9Sdrahn #else
1367c074d1c9Sdrahn #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 2
1368c074d1c9Sdrahn #endif
1369b305b0f1Sespie #endif
1370b305b0f1Sespie #endif
1371b305b0f1Sespie 
1372b305b0f1Sespie void
subsegs_finish(void)1373*007c2a45Smiod subsegs_finish (void)
1374b305b0f1Sespie {
1375b305b0f1Sespie   struct frchain *frchainP;
1376b305b0f1Sespie 
1377b305b0f1Sespie   for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next)
1378b305b0f1Sespie     {
1379c074d1c9Sdrahn       int alignment = 0;
1380b55d4692Sfgsch 
1381b305b0f1Sespie       subseg_set (frchainP->frch_seg, frchainP->frch_subseg);
1382b305b0f1Sespie 
1383b305b0f1Sespie       /* This now gets called even if we had errors.  In that case,
1384b305b0f1Sespie          any alignment is meaningless, and, moreover, will look weird
1385b305b0f1Sespie          if we are generating a listing.  */
1386c074d1c9Sdrahn       if (!had_errors ())
1387c074d1c9Sdrahn 	{
1388c074d1c9Sdrahn 	  alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
1389c074d1c9Sdrahn #ifdef BFD_ASSEMBLER
1390c074d1c9Sdrahn 	  if ((bfd_get_section_flags (now_seg->owner, now_seg) & SEC_MERGE)
1391c074d1c9Sdrahn 	      && now_seg->entsize)
1392c074d1c9Sdrahn 	    {
1393c074d1c9Sdrahn 	      unsigned int entsize = now_seg->entsize;
1394c074d1c9Sdrahn 	      int entalign = 0;
1395b55d4692Sfgsch 
1396c074d1c9Sdrahn 	      while ((entsize & 1) == 0)
1397c074d1c9Sdrahn 		{
1398c074d1c9Sdrahn 		  ++entalign;
1399c074d1c9Sdrahn 		  entsize >>= 1;
1400c074d1c9Sdrahn 		}
1401c074d1c9Sdrahn 	      if (entalign > alignment)
1402c074d1c9Sdrahn 		alignment = entalign;
1403c074d1c9Sdrahn 	    }
1404c074d1c9Sdrahn #endif
1405c074d1c9Sdrahn 	}
1406b55d4692Sfgsch 
1407b55d4692Sfgsch       if (subseg_text_p (now_seg))
1408b55d4692Sfgsch 	frag_align_code (alignment, 0);
1409b55d4692Sfgsch       else
1410b55d4692Sfgsch 	frag_align (alignment, 0, 0);
1411b305b0f1Sespie 
1412b305b0f1Sespie       /* frag_align will have left a new frag.
1413b305b0f1Sespie 	 Use this last frag for an empty ".fill".
1414b305b0f1Sespie 
1415b305b0f1Sespie 	 For this segment ...
1416b305b0f1Sespie 	 Create a last frag. Do not leave a "being filled in frag".  */
1417b305b0f1Sespie       frag_wane (frag_now);
1418b305b0f1Sespie       frag_now->fr_fix = 0;
1419b305b0f1Sespie       know (frag_now->fr_next == NULL);
1420b305b0f1Sespie     }
1421b305b0f1Sespie }
1422b305b0f1Sespie 
1423b305b0f1Sespie /* Write the object file.  */
1424b305b0f1Sespie 
14252159047fSniklas void
write_object_file(void)1426*007c2a45Smiod write_object_file (void)
14272159047fSniklas {
14282159047fSniklas #if ! defined (BFD_ASSEMBLER) || ! defined (WORKING_DOT_WORD)
14292159047fSniklas   fragS *fragP;			/* Track along all frags.  */
14302159047fSniklas #endif
14312159047fSniklas 
14322159047fSniklas   /* Do we really want to write it?  */
14332159047fSniklas   {
14342159047fSniklas     int n_warns, n_errs;
14352159047fSniklas     n_warns = had_warnings ();
14362159047fSniklas     n_errs = had_errors ();
14372159047fSniklas     /* The -Z flag indicates that an object file should be generated,
14382159047fSniklas        regardless of warnings and errors.  */
14392159047fSniklas     if (flag_always_generate_output)
14402159047fSniklas       {
14412159047fSniklas 	if (n_warns || n_errs)
1442c074d1c9Sdrahn 	  as_warn (_("%d error%s, %d warning%s, generating bad object file"),
14432159047fSniklas 		   n_errs, n_errs == 1 ? "" : "s",
14442159047fSniklas 		   n_warns, n_warns == 1 ? "" : "s");
14452159047fSniklas       }
14462159047fSniklas     else
14472159047fSniklas       {
14482159047fSniklas 	if (n_errs)
1449c074d1c9Sdrahn 	  as_fatal (_("%d error%s, %d warning%s, no object file generated"),
14502159047fSniklas 		    n_errs, n_errs == 1 ? "" : "s",
14512159047fSniklas 		    n_warns, n_warns == 1 ? "" : "s");
14522159047fSniklas       }
14532159047fSniklas   }
14542159047fSniklas 
14552159047fSniklas #ifdef	OBJ_VMS
14562159047fSniklas   /* Under VMS we try to be compatible with VAX-11 "C".  Thus, we call
14572159047fSniklas      a routine to check for the definition of the procedure "_main",
14582159047fSniklas      and if so -- fix it up so that it can be program entry point.  */
14592159047fSniklas   vms_check_for_main ();
14602159047fSniklas #endif /* OBJ_VMS  */
14612159047fSniklas 
14622159047fSniklas   /* From now on, we don't care about sub-segments.  Build one frag chain
14632159047fSniklas      for each segment. Linked thru fr_next.  */
14642159047fSniklas 
14652159047fSniklas #ifdef BFD_ASSEMBLER
14662159047fSniklas   /* Remove the sections created by gas for its own purposes.  */
14672159047fSniklas   {
1468c074d1c9Sdrahn     asection **seclist;
14692159047fSniklas     int i;
14702159047fSniklas 
14712159047fSniklas     seclist = &stdoutput->sections;
1472c074d1c9Sdrahn     while (*seclist)
14732159047fSniklas       {
1474c074d1c9Sdrahn 	if (*seclist == reg_section || *seclist == expr_section)
14752159047fSniklas 	  {
1476c074d1c9Sdrahn 	    bfd_section_list_remove (stdoutput, seclist);
14772159047fSniklas 	    stdoutput->section_count--;
14782159047fSniklas 	  }
1479c074d1c9Sdrahn 	else
14802159047fSniklas 	  seclist = &(*seclist)->next;
14812159047fSniklas       }
14822159047fSniklas     i = 0;
14832159047fSniklas     bfd_map_over_sections (stdoutput, renumber_sections, &i);
14842159047fSniklas   }
14852159047fSniklas 
14862159047fSniklas   bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
14872159047fSniklas #else
14882159047fSniklas   remove_subsegs (frchain_root, SEG_TEXT, &text_frag_root, &text_last_frag);
14892159047fSniklas   remove_subsegs (data0_frchainP, SEG_DATA, &data_frag_root, &data_last_frag);
14902159047fSniklas   remove_subsegs (bss0_frchainP, SEG_BSS, &bss_frag_root, &bss_last_frag);
14912159047fSniklas #endif
14922159047fSniklas 
14932159047fSniklas   /* We have two segments. If user gave -R flag, then we must put the
14942159047fSniklas      data frags into the text segment. Do this before relaxing so
14952159047fSniklas      we know to take advantage of -R and make shorter addresses.  */
14962159047fSniklas #if !defined (OBJ_AOUT) || defined (BFD_ASSEMBLER)
14972159047fSniklas   if (flag_readonly_data_in_text)
14982159047fSniklas     {
14992159047fSniklas       merge_data_into_text ();
15002159047fSniklas     }
15012159047fSniklas #endif
15022159047fSniklas 
15032159047fSniklas #ifdef BFD_ASSEMBLER
1504c074d1c9Sdrahn   while (1)
1505c074d1c9Sdrahn     {
1506c074d1c9Sdrahn       int changed;
1507c074d1c9Sdrahn 
1508c074d1c9Sdrahn #ifndef WORKING_DOT_WORD
1509c074d1c9Sdrahn       /* We need to reset the markers in the broken word list and
1510c074d1c9Sdrahn 	 associated frags between calls to relax_segment (via
1511c074d1c9Sdrahn 	 relax_seg).  Since the broken word list is global, we do it
1512c074d1c9Sdrahn 	 once per round, rather than locally in relax_segment for each
1513c074d1c9Sdrahn 	 segment.  */
1514c074d1c9Sdrahn       struct broken_word *brokp;
1515c074d1c9Sdrahn 
1516c074d1c9Sdrahn       for (brokp = broken_words;
1517c074d1c9Sdrahn 	   brokp != (struct broken_word *) NULL;
1518c074d1c9Sdrahn 	   brokp = brokp->next_broken_word)
1519c074d1c9Sdrahn 	{
1520c074d1c9Sdrahn 	  brokp->added = 0;
1521c074d1c9Sdrahn 
1522c074d1c9Sdrahn 	  if (brokp->dispfrag != (fragS *) NULL
1523c074d1c9Sdrahn 	      && brokp->dispfrag->fr_type == rs_broken_word)
1524c074d1c9Sdrahn 	    brokp->dispfrag->fr_subtype = 0;
1525c074d1c9Sdrahn 	}
1526c074d1c9Sdrahn #endif
1527c074d1c9Sdrahn 
1528c074d1c9Sdrahn       changed = 0;
1529c074d1c9Sdrahn       bfd_map_over_sections (stdoutput, relax_seg, &changed);
1530c074d1c9Sdrahn       if (!changed)
1531c074d1c9Sdrahn 	break;
1532c074d1c9Sdrahn     }
1533c074d1c9Sdrahn 
1534c074d1c9Sdrahn   /* Note - Most ports will use the default value of
1535c074d1c9Sdrahn      TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1.  This will force
1536c074d1c9Sdrahn      local symbols to be resolved, removing their frag information.
1537c074d1c9Sdrahn      Some ports however, will not have finished relaxing all of
1538c074d1c9Sdrahn      their frags and will still need the local symbol frag
1539c074d1c9Sdrahn      information.  These ports can set
1540c074d1c9Sdrahn      TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0.  */
1541c074d1c9Sdrahn   finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
1542c074d1c9Sdrahn 
1543b55d4692Sfgsch   bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
15442159047fSniklas #else
15452159047fSniklas   relax_and_size_all_segments ();
15462159047fSniklas #endif /* BFD_ASSEMBLER  */
15472159047fSniklas 
1548c074d1c9Sdrahn   /* Relaxation has completed.  Freeze all syms.  */
1549c074d1c9Sdrahn   finalize_syms = 1;
1550c074d1c9Sdrahn 
1551c074d1c9Sdrahn #ifdef md_post_relax_hook
1552c074d1c9Sdrahn   md_post_relax_hook;
1553b55d4692Sfgsch #endif
1554b55d4692Sfgsch 
15552159047fSniklas #ifndef BFD_ASSEMBLER
1556b55d4692Sfgsch   /* Crawl the symbol chain.
1557b55d4692Sfgsch 
1558b55d4692Sfgsch      For each symbol whose value depends on a frag, take the address of
1559b55d4692Sfgsch      that frag and subsume it into the value of the symbol.
1560b55d4692Sfgsch      After this, there is just one way to lookup a symbol value.
1561b55d4692Sfgsch      Values are left in their final state for object file emission.
1562b55d4692Sfgsch      We adjust the values of 'L' local symbols, even if we do
1563b55d4692Sfgsch      not intend to emit them to the object file, because their values
1564b55d4692Sfgsch      are needed for fix-ups.
1565b55d4692Sfgsch 
1566b55d4692Sfgsch      Unless we saw a -L flag, remove all symbols that begin with 'L'
1567b55d4692Sfgsch      from the symbol chain.  (They are still pointed to by the fixes.)
1568b55d4692Sfgsch 
1569b55d4692Sfgsch      Count the remaining symbols.
1570b55d4692Sfgsch      Assign a symbol number to each symbol.
1571b55d4692Sfgsch      Count the number of string-table chars we will emit.
1572b55d4692Sfgsch      Put this info into the headers as appropriate.  */
15732159047fSniklas   know (zero_address_frag.fr_address == 0);
15742159047fSniklas   string_byte_count = sizeof (string_byte_count);
15752159047fSniklas 
15762159047fSniklas   obj_crawl_symbol_chain (&headers);
15772159047fSniklas 
15782159047fSniklas   if (string_byte_count == sizeof (string_byte_count))
15792159047fSniklas     string_byte_count = 0;
15802159047fSniklas 
15812159047fSniklas   H_SET_STRING_SIZE (&headers, string_byte_count);
15822159047fSniklas 
1583b55d4692Sfgsch   /* Addresses of frags now reflect addresses we use in the object file.
1584b55d4692Sfgsch      Symbol values are correct.
1585b55d4692Sfgsch      Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
1586b55d4692Sfgsch      Also converting any machine-dependent frags using md_convert_frag();  */
15872159047fSniklas   subseg_change (SEG_TEXT, 0);
15882159047fSniklas 
15892159047fSniklas   for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
15902159047fSniklas     {
1591b305b0f1Sespie       /* At this point we have linked all the frags into a single
1592b305b0f1Sespie          chain.  However, cvt_frag_to_fill may call md_convert_frag
1593b305b0f1Sespie          which may call fix_new.  We need to ensure that fix_new adds
1594b305b0f1Sespie          the fixup to the right section.  */
1595b305b0f1Sespie       if (fragP == data_frag_root)
1596b305b0f1Sespie 	subseg_change (SEG_DATA, 0);
1597b305b0f1Sespie 
15982159047fSniklas       cvt_frag_to_fill (&headers, SEG_TEXT, fragP);
15992159047fSniklas 
16002159047fSniklas       /* Some assert macros don't work with # directives mixed in.  */
16012159047fSniklas #ifndef NDEBUG
16022159047fSniklas       if (!(fragP->fr_next == NULL
16032159047fSniklas #ifdef OBJ_BOUT
16042159047fSniklas 	    || fragP->fr_next == data_frag_root
16052159047fSniklas #endif
1606c074d1c9Sdrahn 	    || ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address)
16072159047fSniklas 		== (fragP->fr_fix + fragP->fr_offset * fragP->fr_var))))
16082159047fSniklas 	abort ();
16092159047fSniklas #endif
16102159047fSniklas     }
16112159047fSniklas #endif /* ! BFD_ASSEMBLER  */
16122159047fSniklas 
16132159047fSniklas #ifndef WORKING_DOT_WORD
16142159047fSniklas   {
16152159047fSniklas     struct broken_word *lie;
16162159047fSniklas     struct broken_word **prevP;
16172159047fSniklas 
16182159047fSniklas     prevP = &broken_words;
16192159047fSniklas     for (lie = broken_words; lie; lie = lie->next_broken_word)
16202159047fSniklas       if (!lie->added)
16212159047fSniklas 	{
16222159047fSniklas 	  expressionS exp;
16232159047fSniklas 
1624b305b0f1Sespie 	  subseg_change (lie->seg, lie->subseg);
16252159047fSniklas 	  exp.X_op = O_subtract;
16262159047fSniklas 	  exp.X_add_symbol = lie->add;
16272159047fSniklas 	  exp.X_op_symbol = lie->sub;
16282159047fSniklas 	  exp.X_add_number = lie->addnum;
16292159047fSniklas #ifdef BFD_ASSEMBLER
16302159047fSniklas #ifdef TC_CONS_FIX_NEW
16312159047fSniklas 	  TC_CONS_FIX_NEW (lie->frag,
16322159047fSniklas 			   lie->word_goes_here - lie->frag->fr_literal,
16332159047fSniklas 			   2, &exp);
16342159047fSniklas #else
16352159047fSniklas 	  fix_new_exp (lie->frag,
16362159047fSniklas 		       lie->word_goes_here - lie->frag->fr_literal,
16370c6d0228Sniklas 		       2, &exp, 0, BFD_RELOC_16);
16382159047fSniklas #endif
16392159047fSniklas #else
16402159047fSniklas #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
16412159047fSniklas 	  fix_new_exp (lie->frag,
16422159047fSniklas 		       lie->word_goes_here - lie->frag->fr_literal,
16432159047fSniklas 		       2, &exp, 0, NO_RELOC);
16442159047fSniklas #else
16452159047fSniklas #ifdef TC_NS32K
16462159047fSniklas 	  fix_new_ns32k_exp (lie->frag,
16472159047fSniklas 			     lie->word_goes_here - lie->frag->fr_literal,
16482159047fSniklas 			     2, &exp, 0, 0, 2, 0, 0);
16492159047fSniklas #else
16502159047fSniklas 	  fix_new_exp (lie->frag,
16512159047fSniklas 		       lie->word_goes_here - lie->frag->fr_literal,
16522159047fSniklas 		       2, &exp, 0, 0);
16532159047fSniklas #endif /* TC_NS32K  */
16542159047fSniklas #endif /* TC_SPARC|TC_A29K|NEED_FX_R_TYPE  */
16552159047fSniklas #endif /* BFD_ASSEMBLER  */
16562159047fSniklas 	  *prevP = lie->next_broken_word;
16572159047fSniklas 	}
16582159047fSniklas       else
16592159047fSniklas 	prevP = &(lie->next_broken_word);
16602159047fSniklas 
16612159047fSniklas     for (lie = broken_words; lie;)
16622159047fSniklas       {
16632159047fSniklas 	struct broken_word *untruth;
16642159047fSniklas 	char *table_ptr;
16652159047fSniklas 	addressT table_addr;
16662159047fSniklas 	addressT from_addr, to_addr;
16672159047fSniklas 	int n, m;
16682159047fSniklas 
1669b305b0f1Sespie 	subseg_change (lie->seg, lie->subseg);
16702159047fSniklas 	fragP = lie->dispfrag;
16712159047fSniklas 
16722159047fSniklas 	/* Find out how many broken_words go here.  */
16732159047fSniklas 	n = 0;
1674b55d4692Sfgsch 	for (untruth = lie;
1675b55d4692Sfgsch 	     untruth && untruth->dispfrag == fragP;
1676b55d4692Sfgsch 	     untruth = untruth->next_broken_word)
16772159047fSniklas 	  if (untruth->added == 1)
16782159047fSniklas 	    n++;
16792159047fSniklas 
16802159047fSniklas 	table_ptr = lie->dispfrag->fr_opcode;
1681b55d4692Sfgsch 	table_addr = (lie->dispfrag->fr_address
1682b55d4692Sfgsch 		      + (table_ptr - lie->dispfrag->fr_literal));
16832159047fSniklas 	/* Create the jump around the long jumps.  This is a short
16842159047fSniklas 	   jump from table_ptr+0 to table_ptr+n*long_jump_size.  */
16852159047fSniklas 	from_addr = table_addr;
16862159047fSniklas 	to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
1687b55d4692Sfgsch 	md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1688b55d4692Sfgsch 			      lie->add);
16892159047fSniklas 	table_ptr += md_short_jump_size;
16902159047fSniklas 	table_addr += md_short_jump_size;
16912159047fSniklas 
1692b55d4692Sfgsch 	for (m = 0;
1693b55d4692Sfgsch 	     lie && lie->dispfrag == fragP;
1694b55d4692Sfgsch 	     m++, lie = lie->next_broken_word)
16952159047fSniklas 	  {
16962159047fSniklas 	    if (lie->added == 2)
16972159047fSniklas 	      continue;
1698b55d4692Sfgsch 	    /* Patch the jump table.  */
1699b55d4692Sfgsch 	    /* This is the offset from ??? to table_ptr+0.  */
17002159047fSniklas 	    to_addr = table_addr - S_GET_VALUE (lie->sub);
1701b55d4692Sfgsch #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
1702b55d4692Sfgsch 	    TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_addr, lie);
1703b55d4692Sfgsch #endif
17042159047fSniklas 	    md_number_to_chars (lie->word_goes_here, to_addr, 2);
1705b55d4692Sfgsch 	    for (untruth = lie->next_broken_word;
1706b55d4692Sfgsch 		 untruth && untruth->dispfrag == fragP;
1707b55d4692Sfgsch 		 untruth = untruth->next_broken_word)
17082159047fSniklas 	      {
17092159047fSniklas 		if (untruth->use_jump == lie)
17102159047fSniklas 		  md_number_to_chars (untruth->word_goes_here, to_addr, 2);
17112159047fSniklas 	      }
17122159047fSniklas 
1713b55d4692Sfgsch 	    /* Install the long jump.  */
1714b55d4692Sfgsch 	    /* This is a long jump from table_ptr+0 to the final target.  */
17152159047fSniklas 	    from_addr = table_addr;
17162159047fSniklas 	    to_addr = S_GET_VALUE (lie->add) + lie->addnum;
1717b55d4692Sfgsch 	    md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
1718b55d4692Sfgsch 				 lie->add);
17192159047fSniklas 	    table_ptr += md_long_jump_size;
17202159047fSniklas 	    table_addr += md_long_jump_size;
17212159047fSniklas 	  }
17222159047fSniklas       }
17232159047fSniklas   }
17242159047fSniklas #endif /* not WORKING_DOT_WORD  */
17252159047fSniklas 
17262159047fSniklas #ifndef BFD_ASSEMBLER
17272159047fSniklas #ifndef	OBJ_VMS
17282159047fSniklas   {				/* not vms  */
17292159047fSniklas     char *the_object_file;
17302159047fSniklas     long object_file_size;
1731b55d4692Sfgsch     /* Scan every FixS performing fixups. We had to wait until now to
1732b55d4692Sfgsch        do this because md_convert_frag() may have made some fixSs.  */
17332159047fSniklas     int trsize, drsize;
17342159047fSniklas 
17352159047fSniklas     subseg_change (SEG_TEXT, 0);
17362159047fSniklas     trsize = md_reloc_size * fixup_segment (text_fix_root, SEG_TEXT);
17372159047fSniklas     subseg_change (SEG_DATA, 0);
17382159047fSniklas     drsize = md_reloc_size * fixup_segment (data_fix_root, SEG_DATA);
17392159047fSniklas     H_SET_RELOCATION_SIZE (&headers, trsize, drsize);
17402159047fSniklas 
1741b55d4692Sfgsch     /* FIXME: Move this stuff into the pre-write-hook.  */
17422159047fSniklas     H_SET_MAGIC_NUMBER (&headers, magic_number_for_object_file);
17432159047fSniklas     H_SET_ENTRY_POINT (&headers, 0);
17442159047fSniklas 
1745b55d4692Sfgsch     obj_pre_write_hook (&headers);	/* Extra coff stuff.  */
17462159047fSniklas 
17472159047fSniklas     object_file_size = H_GET_FILE_SIZE (&headers);
17482159047fSniklas     next_object_file_charP = the_object_file = xmalloc (object_file_size);
17492159047fSniklas 
17502159047fSniklas     output_file_create (out_file_name);
17512159047fSniklas 
17522159047fSniklas     obj_header_append (&next_object_file_charP, &headers);
17532159047fSniklas 
1754b55d4692Sfgsch     know ((next_object_file_charP - the_object_file)
1755b55d4692Sfgsch 	  == H_GET_HEADER_SIZE (&headers));
17562159047fSniklas 
1757b55d4692Sfgsch     /* Emit code.  */
17582159047fSniklas     for (fragP = text_frag_root; fragP; fragP = fragP->fr_next)
17592159047fSniklas       {
17602159047fSniklas 	register long count;
17612159047fSniklas 	register char *fill_literal;
17622159047fSniklas 	register long fill_size;
17632159047fSniklas 
17642159047fSniklas 	PROGRESS (1);
17652159047fSniklas 	know (fragP->fr_type == rs_fill);
1766b55d4692Sfgsch 	append (&next_object_file_charP, fragP->fr_literal,
1767b55d4692Sfgsch 		(unsigned long) fragP->fr_fix);
17682159047fSniklas 	fill_literal = fragP->fr_literal + fragP->fr_fix;
17692159047fSniklas 	fill_size = fragP->fr_var;
17702159047fSniklas 	know (fragP->fr_offset >= 0);
17712159047fSniklas 
17722159047fSniklas 	for (count = fragP->fr_offset; count; count--)
1773b55d4692Sfgsch 	  append (&next_object_file_charP, fill_literal,
1774b55d4692Sfgsch 		  (unsigned long) fill_size);
1775b55d4692Sfgsch       }
17762159047fSniklas 
1777b55d4692Sfgsch     know ((next_object_file_charP - the_object_file)
1778b55d4692Sfgsch 	  == (H_GET_HEADER_SIZE (&headers)
1779b55d4692Sfgsch 	      + H_GET_TEXT_SIZE (&headers)
1780b55d4692Sfgsch 	      + H_GET_DATA_SIZE (&headers)));
17812159047fSniklas 
1782b55d4692Sfgsch     /* Emit relocations.  */
1783b55d4692Sfgsch     obj_emit_relocations (&next_object_file_charP, text_fix_root,
1784b55d4692Sfgsch 			  (relax_addressT) 0);
1785b55d4692Sfgsch     know ((next_object_file_charP - the_object_file)
1786b55d4692Sfgsch 	  == (H_GET_HEADER_SIZE (&headers)
1787b55d4692Sfgsch 	      + H_GET_TEXT_SIZE (&headers)
1788b55d4692Sfgsch 	      + H_GET_DATA_SIZE (&headers)
1789b55d4692Sfgsch 	      + H_GET_TEXT_RELOCATION_SIZE (&headers)));
17902159047fSniklas #ifdef TC_I960
17912159047fSniklas     /* Make addresses in data relocation directives relative to beginning of
1792b55d4692Sfgsch        first data fragment, not end of last text fragment:  alignment of the
1793b55d4692Sfgsch        start of the data segment may place a gap between the segments.  */
1794b55d4692Sfgsch     obj_emit_relocations (&next_object_file_charP, data_fix_root,
1795b55d4692Sfgsch 			  data0_frchainP->frch_root->fr_address);
17962159047fSniklas #else /* TC_I960  */
1797b55d4692Sfgsch     obj_emit_relocations (&next_object_file_charP, data_fix_root,
1798b55d4692Sfgsch 			  text_last_frag->fr_address);
17992159047fSniklas #endif /* TC_I960  */
18002159047fSniklas 
1801b55d4692Sfgsch     know ((next_object_file_charP - the_object_file)
1802b55d4692Sfgsch 	  == (H_GET_HEADER_SIZE (&headers)
1803b55d4692Sfgsch 	      + H_GET_TEXT_SIZE (&headers)
1804b55d4692Sfgsch 	      + H_GET_DATA_SIZE (&headers)
1805b55d4692Sfgsch 	      + H_GET_TEXT_RELOCATION_SIZE (&headers)
1806b55d4692Sfgsch 	      + H_GET_DATA_RELOCATION_SIZE (&headers)));
18072159047fSniklas 
1808b55d4692Sfgsch     /* Emit line number entries.  */
18092159047fSniklas     OBJ_EMIT_LINENO (&next_object_file_charP, lineno_rootP, the_object_file);
1810b55d4692Sfgsch     know ((next_object_file_charP - the_object_file)
1811b55d4692Sfgsch 	  == (H_GET_HEADER_SIZE (&headers)
1812b55d4692Sfgsch 	      + H_GET_TEXT_SIZE (&headers)
1813b55d4692Sfgsch 	      + H_GET_DATA_SIZE (&headers)
1814b55d4692Sfgsch 	      + H_GET_TEXT_RELOCATION_SIZE (&headers)
1815b55d4692Sfgsch 	      + H_GET_DATA_RELOCATION_SIZE (&headers)
1816b55d4692Sfgsch 	      + H_GET_LINENO_SIZE (&headers)));
18172159047fSniklas 
1818b55d4692Sfgsch     /* Emit symbols.  */
18192159047fSniklas     obj_emit_symbols (&next_object_file_charP, symbol_rootP);
1820b55d4692Sfgsch     know ((next_object_file_charP - the_object_file)
1821b55d4692Sfgsch 	  == (H_GET_HEADER_SIZE (&headers)
1822b55d4692Sfgsch 	      + H_GET_TEXT_SIZE (&headers)
1823b55d4692Sfgsch 	      + H_GET_DATA_SIZE (&headers)
1824b55d4692Sfgsch 	      + H_GET_TEXT_RELOCATION_SIZE (&headers)
1825b55d4692Sfgsch 	      + H_GET_DATA_RELOCATION_SIZE (&headers)
1826b55d4692Sfgsch 	      + H_GET_LINENO_SIZE (&headers)
1827b55d4692Sfgsch 	      + H_GET_SYMBOL_TABLE_SIZE (&headers)));
18282159047fSniklas 
1829b55d4692Sfgsch     /* Emit strings.  */
18302159047fSniklas     if (string_byte_count > 0)
18312159047fSniklas       obj_emit_strings (&next_object_file_charP);
18322159047fSniklas 
18332159047fSniklas #ifdef BFD_HEADERS
1834c074d1c9Sdrahn     bfd_seek (stdoutput, (file_ptr) 0, 0);
1835c074d1c9Sdrahn     bfd_bwrite (the_object_file, (bfd_size_type) object_file_size, stdoutput);
18362159047fSniklas #else
18372159047fSniklas 
1838b55d4692Sfgsch     /* Write the data to the file.  */
18392159047fSniklas     output_file_append (the_object_file, object_file_size, out_file_name);
18402159047fSniklas     free (the_object_file);
18412159047fSniklas #endif
1842b55d4692Sfgsch   }
18432159047fSniklas #else /* OBJ_VMS  */
1844b55d4692Sfgsch   /* Now do the VMS-dependent part of writing the object file.  */
18452159047fSniklas   vms_write_object_file (H_GET_TEXT_SIZE (&headers),
18462159047fSniklas 			 H_GET_DATA_SIZE (&headers),
18472159047fSniklas 			 H_GET_BSS_SIZE (&headers),
18482159047fSniklas 			 text_frag_root, data_frag_root);
18492159047fSniklas #endif /* OBJ_VMS  */
18502159047fSniklas #else /* BFD_ASSEMBLER  */
18512159047fSniklas 
18522159047fSniklas   /* Resolve symbol values.  This needs to be done before processing
18532159047fSniklas      the relocations.  */
18542159047fSniklas   if (symbol_rootP)
18552159047fSniklas     {
18562159047fSniklas       symbolS *symp;
18572159047fSniklas 
18582159047fSniklas       for (symp = symbol_rootP; symp; symp = symbol_next (symp))
1859c074d1c9Sdrahn 	resolve_symbol_value (symp);
18602159047fSniklas     }
1861b305b0f1Sespie   resolve_local_symbol_values ();
18622159047fSniklas 
18632159047fSniklas   PROGRESS (1);
18642159047fSniklas 
18650c6d0228Sniklas #ifdef tc_frob_file_before_adjust
18660c6d0228Sniklas   tc_frob_file_before_adjust ();
18670c6d0228Sniklas #endif
18680c6d0228Sniklas #ifdef obj_frob_file_before_adjust
18690c6d0228Sniklas   obj_frob_file_before_adjust ();
18700c6d0228Sniklas #endif
18710c6d0228Sniklas 
18722159047fSniklas   bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
18732159047fSniklas 
1874c074d1c9Sdrahn #ifdef tc_frob_file_before_fix
1875c074d1c9Sdrahn   tc_frob_file_before_fix ();
1876c074d1c9Sdrahn #endif
1877c074d1c9Sdrahn #ifdef obj_frob_file_before_fix
1878c074d1c9Sdrahn   obj_frob_file_before_fix ();
1879c074d1c9Sdrahn #endif
1880c074d1c9Sdrahn 
1881c074d1c9Sdrahn   bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
1882c074d1c9Sdrahn 
18832159047fSniklas   /* Set up symbol table, and write it out.  */
18842159047fSniklas   if (symbol_rootP)
18852159047fSniklas     {
18862159047fSniklas       symbolS *symp;
18872159047fSniklas 
18882159047fSniklas       for (symp = symbol_rootP; symp; symp = symbol_next (symp))
18892159047fSniklas 	{
18902159047fSniklas 	  int punt = 0;
18912159047fSniklas 	  const char *name;
18922159047fSniklas 
1893b305b0f1Sespie 	  if (symbol_mri_common_p (symp))
18942159047fSniklas 	    {
18952159047fSniklas 	      if (S_IS_EXTERNAL (symp))
1896b305b0f1Sespie 		as_bad (_("%s: global symbols not supported in common sections"),
18972159047fSniklas 			S_GET_NAME (symp));
18982159047fSniklas 	      symbol_remove (symp, &symbol_rootP, &symbol_lastP);
18992159047fSniklas 	      continue;
19002159047fSniklas 	    }
19012159047fSniklas 
19022159047fSniklas 	  name = S_GET_NAME (symp);
19032159047fSniklas 	  if (name)
19042159047fSniklas 	    {
1905b55d4692Sfgsch 	      const char *name2 =
1906b55d4692Sfgsch 		decode_local_label_name ((char *) S_GET_NAME (symp));
19072159047fSniklas 	      /* They only differ if `name' is a fb or dollar local
19082159047fSniklas 		 label name.  */
19092159047fSniklas 	      if (name2 != name && ! S_IS_DEFINED (symp))
1910c074d1c9Sdrahn 		as_bad (_("local label `%s' is not defined"), name2);
19112159047fSniklas 	    }
19122159047fSniklas 
19132159047fSniklas 	  /* Do it again, because adjust_reloc_syms might introduce
19142159047fSniklas 	     more symbols.  They'll probably only be section symbols,
19152159047fSniklas 	     but they'll still need to have the values computed.  */
1916c074d1c9Sdrahn 	  resolve_symbol_value (symp);
19172159047fSniklas 
1918191aa565Sniklas 	  /* Skip symbols which were equated to undefined or common
1919191aa565Sniklas              symbols.  */
1920c074d1c9Sdrahn 	  if (symbol_equated_reloc_p (symp))
1921191aa565Sniklas 	    {
1922191aa565Sniklas 	      symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1923191aa565Sniklas 	      continue;
1924191aa565Sniklas 	    }
1925191aa565Sniklas 
19262159047fSniklas 	  /* So far, common symbols have been treated like undefined symbols.
19272159047fSniklas 	     Put them in the common section now.  */
19282159047fSniklas 	  if (S_IS_DEFINED (symp) == 0
19292159047fSniklas 	      && S_GET_VALUE (symp) != 0)
19302159047fSniklas 	    S_SET_SEGMENT (symp, bfd_com_section_ptr);
19312159047fSniklas #if 0
19322159047fSniklas 	  printf ("symbol `%s'\n\t@%x: value=%d flags=%x seg=%s\n",
19332159047fSniklas 		  S_GET_NAME (symp), symp,
19342159047fSniklas 		  S_GET_VALUE (symp),
1935b305b0f1Sespie 		  symbol_get_bfdsym (symp)->flags,
1936b305b0f1Sespie 		  segment_name (S_GET_SEGMENT (symp)));
19372159047fSniklas #endif
19382159047fSniklas 
19392159047fSniklas #ifdef obj_frob_symbol
19402159047fSniklas 	  obj_frob_symbol (symp, punt);
19412159047fSniklas #endif
19422159047fSniklas #ifdef tc_frob_symbol
1943b305b0f1Sespie 	  if (! punt || symbol_used_in_reloc_p (symp))
19442159047fSniklas 	    tc_frob_symbol (symp, punt);
19452159047fSniklas #endif
19462159047fSniklas 
19472159047fSniklas 	  /* If we don't want to keep this symbol, splice it out of
19482159047fSniklas 	     the chain now.  If EMIT_SECTION_SYMBOLS is 0, we never
19492159047fSniklas 	     want section symbols.  Otherwise, we skip local symbols
19502159047fSniklas 	     and symbols that the frob_symbol macros told us to punt,
19512159047fSniklas 	     but we keep such symbols if they are used in relocs.  */
1952c074d1c9Sdrahn 	  if (symp == abs_section_sym
1953c074d1c9Sdrahn 	      || (! EMIT_SECTION_SYMBOLS
1954b305b0f1Sespie 		  && symbol_section_p (symp))
19552159047fSniklas 	      /* Note that S_IS_EXTERN and S_IS_LOCAL are not always
19562159047fSniklas 		 opposites.  Sometimes the former checks flags and the
19572159047fSniklas 		 latter examines the name...  */
19582159047fSniklas 	      || (!S_IS_EXTERN (symp)
1959c074d1c9Sdrahn 		  && (punt || S_IS_LOCAL (symp))
1960b305b0f1Sespie 		  && ! symbol_used_in_reloc_p (symp)))
19612159047fSniklas 	    {
19622159047fSniklas 	      symbol_remove (symp, &symbol_rootP, &symbol_lastP);
1963b55d4692Sfgsch 
19642159047fSniklas 	      /* After symbol_remove, symbol_next(symp) still returns
19652159047fSniklas 		 the one that came after it in the chain.  So we don't
19662159047fSniklas 		 need to do any extra cleanup work here.  */
19672159047fSniklas 	      continue;
19682159047fSniklas 	    }
19692159047fSniklas 
19702159047fSniklas 	  /* Make sure we really got a value for the symbol.  */
1971b305b0f1Sespie 	  if (! symbol_resolved_p (symp))
19722159047fSniklas 	    {
1973c074d1c9Sdrahn 	      as_bad (_("can't resolve value for symbol `%s'"),
19742159047fSniklas 		      S_GET_NAME (symp));
1975b305b0f1Sespie 	      symbol_mark_resolved (symp);
19762159047fSniklas 	    }
19772159047fSniklas 
19782159047fSniklas 	  /* Set the value into the BFD symbol.  Up til now the value
19792159047fSniklas 	     has only been kept in the gas symbolS struct.  */
1980b305b0f1Sespie 	  symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
19812159047fSniklas 	}
19822159047fSniklas     }
19832159047fSniklas 
19842159047fSniklas   PROGRESS (1);
19852159047fSniklas 
19862159047fSniklas   /* Now do any format-specific adjustments to the symbol table, such
19872159047fSniklas      as adding file symbols.  */
19882159047fSniklas #ifdef tc_adjust_symtab
19892159047fSniklas   tc_adjust_symtab ();
19902159047fSniklas #endif
19912159047fSniklas #ifdef obj_adjust_symtab
19922159047fSniklas   obj_adjust_symtab ();
19932159047fSniklas #endif
19942159047fSniklas 
19952159047fSniklas   /* Now that all the sizes are known, and contents correct, we can
19962159047fSniklas      start writing to the file.  */
19972159047fSniklas   set_symtab ();
19982159047fSniklas 
19992159047fSniklas   /* If *_frob_file changes the symbol value at this point, it is
20002159047fSniklas      responsible for moving the changed value into symp->bsym->value
20012159047fSniklas      as well.  Hopefully all symbol value changing can be done in
20022159047fSniklas      *_frob_symbol.  */
20032159047fSniklas #ifdef tc_frob_file
20042159047fSniklas   tc_frob_file ();
20052159047fSniklas #endif
20062159047fSniklas #ifdef obj_frob_file
20072159047fSniklas   obj_frob_file ();
20082159047fSniklas #endif
20092159047fSniklas 
20102159047fSniklas   bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
20112159047fSniklas 
2012e93f7393Sniklas #ifdef tc_frob_file_after_relocs
2013e93f7393Sniklas   tc_frob_file_after_relocs ();
2014e93f7393Sniklas #endif
2015e93f7393Sniklas #ifdef obj_frob_file_after_relocs
2016e93f7393Sniklas   obj_frob_file_after_relocs ();
2017e93f7393Sniklas #endif
2018e93f7393Sniklas 
20192159047fSniklas   bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
20202159047fSniklas #endif /* BFD_ASSEMBLER  */
20212159047fSniklas }
20222159047fSniklas #endif /* ! BFD  */
20232159047fSniklas 
20240c6d0228Sniklas #ifdef TC_GENERIC_RELAX_TABLE
20252159047fSniklas 
2026b305b0f1Sespie /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE.  */
2027b305b0f1Sespie 
2028b305b0f1Sespie long
relax_frag(segT segment,fragS * fragP,long stretch)2029*007c2a45Smiod relax_frag (segT segment, fragS *fragP, long stretch)
2030b305b0f1Sespie {
2031b305b0f1Sespie   const relax_typeS *this_type;
2032b305b0f1Sespie   const relax_typeS *start_type;
2033b305b0f1Sespie   relax_substateT next_state;
2034b305b0f1Sespie   relax_substateT this_state;
2035*007c2a45Smiod   offsetT growth;
2036b55d4692Sfgsch   offsetT aim;
2037b55d4692Sfgsch   addressT target;
2038b55d4692Sfgsch   addressT address;
2039b55d4692Sfgsch   symbolS *symbolP;
2040b55d4692Sfgsch   const relax_typeS *table;
2041b305b0f1Sespie 
2042b55d4692Sfgsch   target = fragP->fr_offset;
2043b55d4692Sfgsch   address = fragP->fr_address;
2044b55d4692Sfgsch   table = TC_GENERIC_RELAX_TABLE;
2045b305b0f1Sespie   this_state = fragP->fr_subtype;
2046b305b0f1Sespie   start_type = this_type = table + this_state;
2047b55d4692Sfgsch   symbolP = fragP->fr_symbol;
2048b305b0f1Sespie 
2049b305b0f1Sespie   if (symbolP)
2050b305b0f1Sespie     {
2051b55d4692Sfgsch       fragS *sym_frag;
2052b55d4692Sfgsch 
2053b55d4692Sfgsch       sym_frag = symbol_get_frag (symbolP);
2054b55d4692Sfgsch 
2055b305b0f1Sespie #ifndef DIFF_EXPR_OK
2056b305b0f1Sespie #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
2057b305b0f1Sespie       know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
2058b305b0f1Sespie 	    || (S_GET_SEGMENT (symbolP) == SEG_DATA)
2059b305b0f1Sespie 	    || (S_GET_SEGMENT (symbolP) == SEG_BSS)
2060b305b0f1Sespie 	    || (S_GET_SEGMENT (symbolP) == SEG_TEXT));
2061b305b0f1Sespie #endif
2062b55d4692Sfgsch       know (sym_frag != NULL);
2063b305b0f1Sespie #endif
2064c074d1c9Sdrahn       know (S_GET_SEGMENT (symbolP) != absolute_section
2065b55d4692Sfgsch 	    || sym_frag == &zero_address_frag);
2066c074d1c9Sdrahn       target += S_GET_VALUE (symbolP);
2067b305b0f1Sespie 
2068b305b0f1Sespie       /* If frag has yet to be reached on this pass,
2069b305b0f1Sespie 	 assume it will move by STRETCH just as we did.
2070b305b0f1Sespie 	 If this is not so, it will be because some frag
2071b55d4692Sfgsch 	 between grows, and that will force another pass.  */
2072b305b0f1Sespie 
2073b55d4692Sfgsch       if (stretch != 0
2074b55d4692Sfgsch 	  && sym_frag->relax_marker != fragP->relax_marker
2075b55d4692Sfgsch 	  && S_GET_SEGMENT (symbolP) == segment)
2076b305b0f1Sespie 	{
2077b305b0f1Sespie 	  target += stretch;
2078b305b0f1Sespie 	}
2079b305b0f1Sespie     }
2080b305b0f1Sespie 
2081b305b0f1Sespie   aim = target - address - fragP->fr_fix;
2082b305b0f1Sespie #ifdef TC_PCREL_ADJUST
2083b55d4692Sfgsch   /* Currently only the ns32k family needs this.  */
2084b305b0f1Sespie   aim += TC_PCREL_ADJUST (fragP);
2085b305b0f1Sespie /* #else */
2086b305b0f1Sespie   /* This machine doesn't want to use pcrel_adjust.
2087b305b0f1Sespie      In that case, pcrel_adjust should be zero.  */
2088b55d4692Sfgsch #if 0
2089b55d4692Sfgsch   assert (fragP->fr_targ.ns32k.pcrel_adjust == 0);
2090b55d4692Sfgsch #endif
2091b305b0f1Sespie #endif
2092b305b0f1Sespie #ifdef md_prepare_relax_scan /* formerly called M68K_AIM_KLUDGE  */
2093b305b0f1Sespie   md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
2094b305b0f1Sespie #endif
2095b305b0f1Sespie 
2096b305b0f1Sespie   if (aim < 0)
2097b305b0f1Sespie     {
2098b305b0f1Sespie       /* Look backwards.  */
2099b305b0f1Sespie       for (next_state = this_type->rlx_more; next_state;)
2100b305b0f1Sespie 	if (aim >= this_type->rlx_backward)
2101b305b0f1Sespie 	  next_state = 0;
2102b305b0f1Sespie 	else
2103b305b0f1Sespie 	  {
2104b305b0f1Sespie 	    /* Grow to next state.  */
2105b305b0f1Sespie 	    this_state = next_state;
2106b305b0f1Sespie 	    this_type = table + this_state;
2107b305b0f1Sespie 	    next_state = this_type->rlx_more;
2108b305b0f1Sespie 	  }
2109b305b0f1Sespie     }
2110b305b0f1Sespie   else
2111b305b0f1Sespie     {
2112b305b0f1Sespie       /* Look forwards.  */
2113b305b0f1Sespie       for (next_state = this_type->rlx_more; next_state;)
2114b305b0f1Sespie 	if (aim <= this_type->rlx_forward)
2115b305b0f1Sespie 	  next_state = 0;
2116b305b0f1Sespie 	else
2117b305b0f1Sespie 	  {
2118b305b0f1Sespie 	    /* Grow to next state.  */
2119b305b0f1Sespie 	    this_state = next_state;
2120b305b0f1Sespie 	    this_type = table + this_state;
2121b305b0f1Sespie 	    next_state = this_type->rlx_more;
2122b305b0f1Sespie 	  }
2123b305b0f1Sespie     }
2124b305b0f1Sespie 
2125b305b0f1Sespie   growth = this_type->rlx_length - start_type->rlx_length;
2126b305b0f1Sespie   if (growth != 0)
2127b305b0f1Sespie     fragP->fr_subtype = this_state;
2128b305b0f1Sespie   return growth;
2129b305b0f1Sespie }
2130b305b0f1Sespie 
21310c6d0228Sniklas #endif /* defined (TC_GENERIC_RELAX_TABLE)  */
21322159047fSniklas 
21332159047fSniklas /* Relax_align. Advance location counter to next address that has 'alignment'
21342159047fSniklas    lowest order bits all 0s, return size of adjustment made.  */
21352159047fSniklas static relax_addressT
relax_align(register relax_addressT address,register int alignment)2136*007c2a45Smiod relax_align (register relax_addressT address,	/* Address now.  */
2137*007c2a45Smiod 	     register int alignment	/* Alignment (binary).  */)
21382159047fSniklas {
21392159047fSniklas   relax_addressT mask;
21402159047fSniklas   relax_addressT new_address;
21412159047fSniklas 
21422159047fSniklas   mask = ~((~0) << alignment);
21432159047fSniklas   new_address = (address + mask) & (~mask);
21442159047fSniklas #ifdef LINKER_RELAXING_SHRINKS_ONLY
21452159047fSniklas   if (linkrelax)
21462159047fSniklas     /* We must provide lots of padding, so the linker can discard it
21472159047fSniklas        when needed.  The linker will not add extra space, ever.  */
21482159047fSniklas     new_address += (1 << alignment);
21492159047fSniklas #endif
21502159047fSniklas   return (new_address - address);
21512159047fSniklas }
21522159047fSniklas 
2153b55d4692Sfgsch /* Now we have a segment, not a crowd of sub-segments, we can make
2154b55d4692Sfgsch    fr_address values.
2155b55d4692Sfgsch 
2156b55d4692Sfgsch    Relax the frags.
2157b55d4692Sfgsch 
2158b55d4692Sfgsch    After this, all frags in this segment have addresses that are correct
2159b55d4692Sfgsch    within the segment. Since segments live in different file addresses,
2160b55d4692Sfgsch    these frag addresses may not be the same as final object-file
2161b55d4692Sfgsch    addresses.  */
2162b55d4692Sfgsch 
2163c074d1c9Sdrahn int
relax_segment(struct frag * segment_frag_root,segT segment)2164*007c2a45Smiod relax_segment (struct frag *segment_frag_root, segT segment)
21652159047fSniklas {
21662159047fSniklas   register struct frag *fragP;
21672159047fSniklas   register relax_addressT address;
2168c074d1c9Sdrahn   int ret;
2169c074d1c9Sdrahn 
21702159047fSniklas #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
21712159047fSniklas   know (segment == SEG_DATA || segment == SEG_TEXT || segment == SEG_BSS);
21722159047fSniklas #endif
21732159047fSniklas   /* In case md_estimate_size_before_relax() wants to make fixSs.  */
21742159047fSniklas   subseg_change (segment, 0);
21752159047fSniklas 
21762159047fSniklas   /* For each frag in segment: count and store  (a 1st guess of)
21772159047fSniklas      fr_address.  */
21782159047fSniklas   address = 0;
21792159047fSniklas   for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
21802159047fSniklas     {
2181b55d4692Sfgsch       fragP->relax_marker = 0;
21822159047fSniklas       fragP->fr_address = address;
21832159047fSniklas       address += fragP->fr_fix;
21842159047fSniklas 
21852159047fSniklas       switch (fragP->fr_type)
21862159047fSniklas 	{
21872159047fSniklas 	case rs_fill:
21882159047fSniklas 	  address += fragP->fr_offset * fragP->fr_var;
21892159047fSniklas 	  break;
21902159047fSniklas 
21912159047fSniklas 	case rs_align:
21922159047fSniklas 	case rs_align_code:
2193b55d4692Sfgsch 	case rs_align_test:
21942159047fSniklas 	  {
2195b305b0f1Sespie 	    addressT offset = relax_align (address, (int) fragP->fr_offset);
2196b305b0f1Sespie 
2197b305b0f1Sespie 	    if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
2198b305b0f1Sespie 	      offset = 0;
2199b305b0f1Sespie 
22002159047fSniklas 	    if (offset % fragP->fr_var != 0)
22012159047fSniklas 	      {
2202c074d1c9Sdrahn 		as_bad_where (fragP->fr_file, fragP->fr_line,
2203c074d1c9Sdrahn 			      _("alignment padding (%lu bytes) not a multiple of %ld"),
2204b305b0f1Sespie 			      (unsigned long) offset, (long) fragP->fr_var);
22052159047fSniklas 		offset -= (offset % fragP->fr_var);
22062159047fSniklas 	      }
2207b305b0f1Sespie 
22082159047fSniklas 	    address += offset;
22092159047fSniklas 	  }
22102159047fSniklas 	  break;
22112159047fSniklas 
22122159047fSniklas 	case rs_org:
22132159047fSniklas 	case rs_space:
22142159047fSniklas 	  /* Assume .org is nugatory. It will grow with 1st relax.  */
22152159047fSniklas 	  break;
22162159047fSniklas 
22172159047fSniklas 	case rs_machine_dependent:
2218c074d1c9Sdrahn 	  /* If fr_symbol is an expression, this call to
2219c074d1c9Sdrahn 	     resolve_symbol_value sets up the correct segment, which will
2220c074d1c9Sdrahn 	     likely be needed in md_estimate_size_before_relax.  */
2221c074d1c9Sdrahn 	  if (fragP->fr_symbol)
2222c074d1c9Sdrahn 	    resolve_symbol_value (fragP->fr_symbol);
2223c074d1c9Sdrahn 
22242159047fSniklas 	  address += md_estimate_size_before_relax (fragP, segment);
22252159047fSniklas 	  break;
22262159047fSniklas 
22272159047fSniklas #ifndef WORKING_DOT_WORD
2228b55d4692Sfgsch 	  /* Broken words don't concern us yet.  */
22292159047fSniklas 	case rs_broken_word:
22302159047fSniklas 	  break;
22312159047fSniklas #endif
22322159047fSniklas 
2233b305b0f1Sespie 	case rs_leb128:
2234b305b0f1Sespie 	  /* Initial guess is always 1; doing otherwise can result in
2235b305b0f1Sespie 	     stable solutions that are larger than the minimum.  */
2236b305b0f1Sespie 	  address += fragP->fr_offset = 1;
2237b305b0f1Sespie 	  break;
2238b305b0f1Sespie 
2239b305b0f1Sespie 	case rs_cfa:
2240b305b0f1Sespie 	  address += eh_frame_estimate_size_before_relax (fragP);
2241b305b0f1Sespie 	  break;
2242b305b0f1Sespie 
2243b55d4692Sfgsch 	case rs_dwarf2dbg:
2244b55d4692Sfgsch 	  address += dwarf2dbg_estimate_size_before_relax (fragP);
2245b55d4692Sfgsch 	  break;
2246b55d4692Sfgsch 
22472159047fSniklas 	default:
22482159047fSniklas 	  BAD_CASE (fragP->fr_type);
22492159047fSniklas 	  break;
2250b55d4692Sfgsch 	}
2251b55d4692Sfgsch     }
22522159047fSniklas 
22532159047fSniklas   /* Do relax().  */
22542159047fSniklas   {
2255*007c2a45Smiod     offsetT stretch;	/* May be any size, 0 or negative.  */
2256b55d4692Sfgsch     /* Cumulative number of addresses we have relaxed this pass.
2257b55d4692Sfgsch        We may have relaxed more than one address.  */
2258b55d4692Sfgsch     int stretched;	/* Have we stretched on this pass?  */
22592159047fSniklas     /* This is 'cuz stretch may be zero, when, in fact some piece of code
22602159047fSniklas        grew, and another shrank.  If a branch instruction doesn't fit anymore,
22612159047fSniklas        we could be scrod.  */
22622159047fSniklas 
22632159047fSniklas     do
22642159047fSniklas       {
2265b55d4692Sfgsch 	stretch = 0;
2266b55d4692Sfgsch 	stretched = 0;
2267b55d4692Sfgsch 
22682159047fSniklas 	for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
22692159047fSniklas 	  {
2270*007c2a45Smiod 	    offsetT growth = 0;
2271b305b0f1Sespie 	    addressT was_address;
2272b305b0f1Sespie 	    offsetT offset;
22732159047fSniklas 	    symbolS *symbolP;
22742159047fSniklas 
2275b55d4692Sfgsch 	    fragP->relax_marker ^= 1;
22762159047fSniklas 	    was_address = fragP->fr_address;
22772159047fSniklas 	    address = fragP->fr_address += stretch;
22782159047fSniklas 	    symbolP = fragP->fr_symbol;
22792159047fSniklas 	    offset = fragP->fr_offset;
22802159047fSniklas 
22812159047fSniklas 	    switch (fragP->fr_type)
22822159047fSniklas 	      {
22832159047fSniklas 	      case rs_fill:	/* .fill never relaxes.  */
22842159047fSniklas 		growth = 0;
22852159047fSniklas 		break;
22862159047fSniklas 
22872159047fSniklas #ifndef WORKING_DOT_WORD
22882159047fSniklas 		/* JF:  This is RMS's idea.  I do *NOT* want to be blamed
22892159047fSniklas 		   for it I do not want to write it.  I do not want to have
22902159047fSniklas 		   anything to do with it.  This is not the proper way to
22912159047fSniklas 		   implement this misfeature.  */
22922159047fSniklas 	      case rs_broken_word:
22932159047fSniklas 		{
22942159047fSniklas 		  struct broken_word *lie;
22952159047fSniklas 		  struct broken_word *untruth;
22962159047fSniklas 
22972159047fSniklas 		  /* Yes this is ugly (storing the broken_word pointer
22982159047fSniklas 		     in the symbol slot).  Still, this whole chunk of
22992159047fSniklas 		     code is ugly, and I don't feel like doing anything
23002159047fSniklas 		     about it.  Think of it as stubbornness in action.  */
23012159047fSniklas 		  growth = 0;
23022159047fSniklas 		  for (lie = (struct broken_word *) (fragP->fr_symbol);
23032159047fSniklas 		       lie && lie->dispfrag == fragP;
23042159047fSniklas 		       lie = lie->next_broken_word)
23052159047fSniklas 		    {
23062159047fSniklas 
23072159047fSniklas 		      if (lie->added)
23082159047fSniklas 			continue;
23092159047fSniklas 
2310c074d1c9Sdrahn 		      offset = (S_GET_VALUE (lie->add)
23112159047fSniklas 				+ lie->addnum
2312c074d1c9Sdrahn 				- S_GET_VALUE (lie->sub));
23132159047fSniklas 		      if (offset <= -32768 || offset >= 32767)
23142159047fSniklas 			{
23152159047fSniklas 			  if (flag_warn_displacement)
23162159047fSniklas 			    {
23172159047fSniklas 			      char buf[50];
23182159047fSniklas 			      sprint_value (buf, (addressT) lie->addnum);
2319c074d1c9Sdrahn 			      as_warn_where (fragP->fr_file, fragP->fr_line,
2320c074d1c9Sdrahn 					     _(".word %s-%s+%s didn't fit"),
23212159047fSniklas 					     S_GET_NAME (lie->add),
23222159047fSniklas 					     S_GET_NAME (lie->sub),
23232159047fSniklas 					     buf);
23242159047fSniklas 			    }
23252159047fSniklas 			  lie->added = 1;
23262159047fSniklas 			  if (fragP->fr_subtype == 0)
23272159047fSniklas 			    {
23282159047fSniklas 			      fragP->fr_subtype++;
23292159047fSniklas 			      growth += md_short_jump_size;
23302159047fSniklas 			    }
23312159047fSniklas 			  for (untruth = lie->next_broken_word;
23322159047fSniklas 			       untruth && untruth->dispfrag == lie->dispfrag;
23332159047fSniklas 			       untruth = untruth->next_broken_word)
2334b305b0f1Sespie 			    if ((symbol_get_frag (untruth->add)
2335b305b0f1Sespie 				 == symbol_get_frag (lie->add))
2336b305b0f1Sespie 				&& (S_GET_VALUE (untruth->add)
2337b305b0f1Sespie 				    == S_GET_VALUE (lie->add)))
23382159047fSniklas 			      {
23392159047fSniklas 				untruth->added = 2;
23402159047fSniklas 				untruth->use_jump = lie;
23412159047fSniklas 			      }
23422159047fSniklas 			  growth += md_long_jump_size;
23432159047fSniklas 			}
23442159047fSniklas 		    }
23452159047fSniklas 
23462159047fSniklas 		  break;
23472159047fSniklas 		}		/* case rs_broken_word  */
23482159047fSniklas #endif
23492159047fSniklas 	      case rs_align:
23502159047fSniklas 	      case rs_align_code:
2351b55d4692Sfgsch 	      case rs_align_test:
2352b305b0f1Sespie 		{
2353b305b0f1Sespie 		  addressT oldoff, newoff;
2354b305b0f1Sespie 
2355b305b0f1Sespie 		  oldoff = relax_align (was_address + fragP->fr_fix,
2356b305b0f1Sespie 					(int) offset);
2357b305b0f1Sespie 		  newoff = relax_align (address + fragP->fr_fix,
2358b305b0f1Sespie 					(int) offset);
2359b305b0f1Sespie 
2360b305b0f1Sespie 		  if (fragP->fr_subtype != 0)
2361b305b0f1Sespie 		    {
2362b305b0f1Sespie 		      if (oldoff > fragP->fr_subtype)
2363b305b0f1Sespie 			oldoff = 0;
2364b305b0f1Sespie 		      if (newoff > fragP->fr_subtype)
2365b305b0f1Sespie 			newoff = 0;
2366b305b0f1Sespie 		    }
2367b305b0f1Sespie 
2368b305b0f1Sespie 		  growth = newoff - oldoff;
2369b305b0f1Sespie 		}
23702159047fSniklas 		break;
23712159047fSniklas 
23722159047fSniklas 	      case rs_org:
2373b305b0f1Sespie 		{
2374b55d4692Sfgsch 		  addressT target = offset;
2375b55d4692Sfgsch 		  addressT after;
23762159047fSniklas 
23772159047fSniklas 		  if (symbolP)
23782159047fSniklas 		    {
23792159047fSniklas #if !defined (MANY_SEGMENTS) && !defined (BFD_ASSEMBLER)
23802159047fSniklas 		      know ((S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
23812159047fSniklas 			    || (S_GET_SEGMENT (symbolP) == SEG_DATA)
23822159047fSniklas 			    || (S_GET_SEGMENT (symbolP) == SEG_TEXT)
23832159047fSniklas 			    || S_GET_SEGMENT (symbolP) == SEG_BSS);
23842159047fSniklas 		      know (symbolP->sy_frag);
23852159047fSniklas 		      know (!(S_GET_SEGMENT (symbolP) == SEG_ABSOLUTE)
23862159047fSniklas 			    || (symbolP->sy_frag == &zero_address_frag));
23872159047fSniklas #endif
2388c074d1c9Sdrahn                       /* Convert from an actual address to an octet offset
2389c074d1c9Sdrahn                          into the section.  Here it is assumed that the
2390c074d1c9Sdrahn                          section's VMA is zero, and can omit subtracting it
2391c074d1c9Sdrahn                          from the symbol's value to get the address offset.  */
2392c074d1c9Sdrahn                       know (S_GET_SECTION (symbolP)->vma == 0);
2393c074d1c9Sdrahn 		      target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
2394c074d1c9Sdrahn 		    }
23952159047fSniklas 
23962159047fSniklas 		  know (fragP->fr_next);
23972159047fSniklas 		  after = fragP->fr_next->fr_address;
23982159047fSniklas 		  growth = target - after;
23992159047fSniklas 		  if (growth < 0)
24002159047fSniklas 		    {
24012159047fSniklas 		      /* Growth may be negative, but variable part of frag
24022159047fSniklas 			 cannot have fewer than 0 chars.  That is, we can't
24032159047fSniklas 			 .org backwards.  */
2404b305b0f1Sespie 		      as_bad_where (fragP->fr_file, fragP->fr_line,
2405*007c2a45Smiod 				    _("attempt to move .org backwards"));
2406b305b0f1Sespie 
2407b305b0f1Sespie 		      /* We've issued an error message.  Change the
2408b305b0f1Sespie                          frag to avoid cascading errors.  */
2409b305b0f1Sespie 		      fragP->fr_type = rs_align;
2410b305b0f1Sespie 		      fragP->fr_subtype = 0;
2411b305b0f1Sespie 		      fragP->fr_offset = 0;
2412b305b0f1Sespie 		      fragP->fr_fix = after - address;
2413b305b0f1Sespie 		      growth = stretch;
24142159047fSniklas 		    }
24152159047fSniklas 
2416b55d4692Sfgsch 		  /* This is an absolute growth factor  */
2417b55d4692Sfgsch 		  growth -= stretch;
24182159047fSniklas 		  break;
2419b305b0f1Sespie 		}
24202159047fSniklas 
24212159047fSniklas 	      case rs_space:
2422c074d1c9Sdrahn 		growth = 0;
24232159047fSniklas 		if (symbolP)
24242159047fSniklas 		  {
2425c074d1c9Sdrahn 		    offsetT amount;
2426c074d1c9Sdrahn 
2427c074d1c9Sdrahn 		    amount = S_GET_VALUE (symbolP);
2428c074d1c9Sdrahn 		    if (S_GET_SEGMENT (symbolP) != absolute_section
2429b305b0f1Sespie 			|| S_IS_COMMON (symbolP)
2430b305b0f1Sespie 			|| ! S_IS_DEFINED (symbolP))
2431c074d1c9Sdrahn 		      {
2432b305b0f1Sespie 			as_bad_where (fragP->fr_file, fragP->fr_line,
2433b305b0f1Sespie 				      _(".space specifies non-absolute value"));
2434c074d1c9Sdrahn 			/* Prevent repeat of this error message.  */
24352159047fSniklas 			fragP->fr_symbol = 0;
24362159047fSniklas 		      }
2437c074d1c9Sdrahn 		    else if (amount < 0)
2438c074d1c9Sdrahn 		      {
2439c074d1c9Sdrahn 			as_warn_where (fragP->fr_file, fragP->fr_line,
2440c074d1c9Sdrahn 				       _(".space or .fill with negative value, ignored"));
2441c074d1c9Sdrahn 			fragP->fr_symbol = 0;
24422159047fSniklas 		      }
24432159047fSniklas 		    else
2444c074d1c9Sdrahn 		      growth = (was_address + fragP->fr_fix + amount
2445c074d1c9Sdrahn 				- fragP->fr_next->fr_address);
2446c074d1c9Sdrahn 		  }
24472159047fSniklas 		break;
24482159047fSniklas 
24492159047fSniklas 	      case rs_machine_dependent:
24502159047fSniklas #ifdef md_relax_frag
2451b55d4692Sfgsch 		growth = md_relax_frag (segment, fragP, stretch);
24522159047fSniklas #else
24532159047fSniklas #ifdef TC_GENERIC_RELAX_TABLE
24542159047fSniklas 		/* The default way to relax a frag is to look through
2455b305b0f1Sespie 		   TC_GENERIC_RELAX_TABLE.  */
2456b55d4692Sfgsch 		growth = relax_frag (segment, fragP, stretch);
24572159047fSniklas #endif /* TC_GENERIC_RELAX_TABLE  */
24582159047fSniklas #endif
24592159047fSniklas 		break;
24602159047fSniklas 
2461b305b0f1Sespie 	      case rs_leb128:
2462b305b0f1Sespie 		{
2463b305b0f1Sespie 		  valueT value;
2464*007c2a45Smiod 		  offsetT size;
2465b305b0f1Sespie 
2466c074d1c9Sdrahn 		  value = resolve_symbol_value (fragP->fr_symbol);
2467b305b0f1Sespie 		  size = sizeof_leb128 (value, fragP->fr_subtype);
2468b305b0f1Sespie 		  growth = size - fragP->fr_offset;
2469b305b0f1Sespie 		  fragP->fr_offset = size;
2470b305b0f1Sespie 		}
2471b305b0f1Sespie 		break;
2472b305b0f1Sespie 
2473b305b0f1Sespie 	      case rs_cfa:
2474b305b0f1Sespie 		growth = eh_frame_relax_frag (fragP);
2475b305b0f1Sespie 		break;
2476b305b0f1Sespie 
2477b55d4692Sfgsch 	      case rs_dwarf2dbg:
2478b55d4692Sfgsch 		growth = dwarf2dbg_relax_frag (fragP);
2479b55d4692Sfgsch 		break;
2480b55d4692Sfgsch 
24812159047fSniklas 	      default:
24822159047fSniklas 		BAD_CASE (fragP->fr_type);
24832159047fSniklas 		break;
24842159047fSniklas 	      }
24852159047fSniklas 	    if (growth)
24862159047fSniklas 	      {
24872159047fSniklas 		stretch += growth;
2488b55d4692Sfgsch 		stretched = 1;
24892159047fSniklas 	      }
24902159047fSniklas 	  }			/* For each frag in the segment.  */
24912159047fSniklas       }
24922159047fSniklas     while (stretched);		/* Until nothing further to relax.  */
24932159047fSniklas   }				/* do_relax  */
24942159047fSniklas 
2495c074d1c9Sdrahn   ret = 0;
2496c074d1c9Sdrahn   for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
2497c074d1c9Sdrahn     if (fragP->last_fr_address != fragP->fr_address)
2498c074d1c9Sdrahn       {
2499c074d1c9Sdrahn 	fragP->last_fr_address = fragP->fr_address;
2500c074d1c9Sdrahn 	ret = 1;
2501c074d1c9Sdrahn       }
2502c074d1c9Sdrahn   return ret;
2503b55d4692Sfgsch }
25042159047fSniklas 
25052159047fSniklas #if defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS))
25062159047fSniklas 
25072159047fSniklas /* fixup_segment()
25082159047fSniklas 
25092159047fSniklas    Go through all the fixS's in a segment and see which ones can be
25102159047fSniklas    handled now.  (These consist of fixS where we have since discovered
25112159047fSniklas    the value of a symbol, or the address of the frag involved.)
2512c074d1c9Sdrahn    For each one, call md_apply_fix3 to put the fix into the frag data.
25132159047fSniklas 
25142159047fSniklas    Result is a count of how many relocation structs will be needed to
25152159047fSniklas    handle the remaining fixS's that we couldn't completely handle here.
25162159047fSniklas    These will be output later by emit_relocations().  */
25172159047fSniklas 
25182159047fSniklas static long
fixup_segment(fixS * fixP,segT this_segment)2519*007c2a45Smiod fixup_segment (fixS *fixP, segT this_segment)
25202159047fSniklas {
25212159047fSniklas   long seg_reloc_count = 0;
25222159047fSniklas   valueT add_number;
25232159047fSniklas   fragS *fragP;
25242159047fSniklas   segT add_symbol_segment = absolute_section;
25252159047fSniklas 
2526c074d1c9Sdrahn   if (fixP != NULL && abs_section_sym == NULL)
2527c074d1c9Sdrahn     {
2528c074d1c9Sdrahn #ifndef BFD_ASSEMBLER
2529c074d1c9Sdrahn       abs_section_sym = &abs_symbol;
2530c074d1c9Sdrahn #else
2531c074d1c9Sdrahn       abs_section_sym = section_symbol (absolute_section);
2532c074d1c9Sdrahn #endif
2533c074d1c9Sdrahn     }
2534c074d1c9Sdrahn 
25352159047fSniklas   /* If the linker is doing the relaxing, we must not do any fixups.
25362159047fSniklas 
2537c074d1c9Sdrahn      Well, strictly speaking that's not true -- we could do any that
2538c074d1c9Sdrahn      are PC-relative and don't cross regions that could change size.
2539c074d1c9Sdrahn      And for the i960 we might be able to turn callx/callj into bal
2540c074d1c9Sdrahn      anyways in cases where we know the maximum displacement.  */
2541c074d1c9Sdrahn   if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
25422159047fSniklas     {
25432159047fSniklas       for (; fixP; fixP = fixP->fx_next)
2544c074d1c9Sdrahn 	if (!fixP->fx_done)
2545c074d1c9Sdrahn 	  {
2546c074d1c9Sdrahn 	    if (fixP->fx_addsy == NULL)
2547c074d1c9Sdrahn 	      {
2548c074d1c9Sdrahn 		/* There was no symbol required by this relocation.
2549c074d1c9Sdrahn 		   However, BFD doesn't really handle relocations
2550c074d1c9Sdrahn 		   without symbols well. So fake up a local symbol in
2551c074d1c9Sdrahn 		   the absolute section.  */
2552c074d1c9Sdrahn 		fixP->fx_addsy = abs_section_sym;
2553c074d1c9Sdrahn 	      }
2554c074d1c9Sdrahn 	    symbol_mark_used_in_reloc (fixP->fx_addsy);
2555c074d1c9Sdrahn 	    if (fixP->fx_subsy != NULL)
2556c074d1c9Sdrahn 	      symbol_mark_used_in_reloc (fixP->fx_subsy);
25572159047fSniklas 	    seg_reloc_count++;
2558c074d1c9Sdrahn 	  }
25592159047fSniklas       TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
25602159047fSniklas       return seg_reloc_count;
25612159047fSniklas     }
25622159047fSniklas 
25632159047fSniklas   for (; fixP; fixP = fixP->fx_next)
25642159047fSniklas     {
25652159047fSniklas #ifdef DEBUG5
25662159047fSniklas       fprintf (stderr, "\nprocessing fixup:\n");
25672159047fSniklas       print_fixup (fixP);
25682159047fSniklas #endif
25692159047fSniklas 
25702159047fSniklas       fragP = fixP->fx_frag;
25712159047fSniklas       know (fragP);
25722159047fSniklas #ifdef TC_VALIDATE_FIX
2573c074d1c9Sdrahn       TC_VALIDATE_FIX (fixP, this_segment, skip);
25742159047fSniklas #endif
25752159047fSniklas       add_number = fixP->fx_offset;
25762159047fSniklas 
2577c074d1c9Sdrahn       if (fixP->fx_addsy != NULL
2578c074d1c9Sdrahn 	  && symbol_mri_common_p (fixP->fx_addsy))
25792159047fSniklas 	{
2580c074d1c9Sdrahn 	  know (fixP->fx_addsy->sy_value.X_op == O_symbol);
2581c074d1c9Sdrahn 	  add_number += S_GET_VALUE (fixP->fx_addsy);
25822159047fSniklas 	  fixP->fx_offset = add_number;
2583c074d1c9Sdrahn 	  fixP->fx_addsy
2584c074d1c9Sdrahn 	    = symbol_get_value_expression (fixP->fx_addsy)->X_add_symbol;
25852159047fSniklas 	}
25862159047fSniklas 
2587c074d1c9Sdrahn       if (fixP->fx_addsy != NULL)
2588c074d1c9Sdrahn 	add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
25892159047fSniklas 
2590c074d1c9Sdrahn       if (fixP->fx_subsy != NULL)
25912159047fSniklas 	{
2592c074d1c9Sdrahn 	  segT sub_symbol_segment;
2593c074d1c9Sdrahn 	  resolve_symbol_value (fixP->fx_subsy);
2594c074d1c9Sdrahn 	  sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
2595c074d1c9Sdrahn 	  if (fixP->fx_addsy != NULL
2596c074d1c9Sdrahn 	      && sub_symbol_segment == add_symbol_segment
2597c074d1c9Sdrahn 	      && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
25982159047fSniklas 	    {
2599c074d1c9Sdrahn 	      add_number += S_GET_VALUE (fixP->fx_addsy);
2600c074d1c9Sdrahn 	      add_number -= S_GET_VALUE (fixP->fx_subsy);
2601c074d1c9Sdrahn 	      fixP->fx_offset = add_number;
26022159047fSniklas 	      fixP->fx_addsy = NULL;
26032159047fSniklas 	      fixP->fx_subsy = NULL;
2604*007c2a45Smiod #ifdef TC_M68K
2605*007c2a45Smiod 	      /* See the comment below about 68k weirdness.  */
26062159047fSniklas 	      fixP->fx_pcrel = 0;
2607*007c2a45Smiod #endif
2608c074d1c9Sdrahn 	    }
2609c074d1c9Sdrahn 	  else if (sub_symbol_segment == absolute_section
2610c074d1c9Sdrahn 		   && !TC_FORCE_RELOCATION_SUB_ABS (fixP))
2611c074d1c9Sdrahn 	    {
2612c074d1c9Sdrahn 	      add_number -= S_GET_VALUE (fixP->fx_subsy);
2613c074d1c9Sdrahn 	      fixP->fx_offset = add_number;
26142159047fSniklas 	      fixP->fx_subsy = NULL;
26152159047fSniklas 	    }
2616c074d1c9Sdrahn 	  else if (sub_symbol_segment == this_segment
2617c074d1c9Sdrahn 		   && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP))
26182159047fSniklas 	    {
2619c074d1c9Sdrahn 	      add_number -= S_GET_VALUE (fixP->fx_subsy);
2620c074d1c9Sdrahn 	      fixP->fx_offset = (add_number + fixP->fx_dot_value
2621c074d1c9Sdrahn 				 + fixP->fx_frag->fr_address);
26222159047fSniklas 
2623c074d1c9Sdrahn 	      /* Make it pc-relative.  If the back-end code has not
2624c074d1c9Sdrahn 		 selected a pc-relative reloc, cancel the adjustment
2625c074d1c9Sdrahn 		 we do later on all pc-relative relocs.  */
2626c074d1c9Sdrahn 	      if (0
2627c074d1c9Sdrahn #ifdef TC_M68K
2628c074d1c9Sdrahn 		  /* Do this for m68k even if it's already described
2629c074d1c9Sdrahn 		     as pc-relative.  On the m68k, an operand of
2630c074d1c9Sdrahn 		     "pc@(foo-.-2)" should address "foo" in a
2631c074d1c9Sdrahn 		     pc-relative mode.  */
2632c074d1c9Sdrahn 		  || 1
26332159047fSniklas #endif
2634c074d1c9Sdrahn 		  || !fixP->fx_pcrel)
2635c074d1c9Sdrahn 		add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
2636c074d1c9Sdrahn 	      fixP->fx_subsy = NULL;
26372159047fSniklas 	      fixP->fx_pcrel = 1;
26382159047fSniklas 	    }
2639c074d1c9Sdrahn 	  else if (!TC_VALIDATE_FIX_SUB (fixP))
26402159047fSniklas 	    {
26412159047fSniklas 	      as_bad_where (fixP->fx_file, fixP->fx_line,
2642c074d1c9Sdrahn 			    _("can't resolve `%s' {%s section} - `%s' {%s section}"),
2643c074d1c9Sdrahn 			    fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : "0",
2644c074d1c9Sdrahn 			    segment_name (add_symbol_segment),
2645c074d1c9Sdrahn 			    S_GET_NAME (fixP->fx_subsy),
2646c074d1c9Sdrahn 			    segment_name (sub_symbol_segment));
26472159047fSniklas 	    }
26482159047fSniklas 	}
26492159047fSniklas 
2650c074d1c9Sdrahn       if (fixP->fx_addsy)
26512159047fSniklas 	{
2652c074d1c9Sdrahn 	  if (add_symbol_segment == this_segment
2653c074d1c9Sdrahn 	      && !TC_FORCE_RELOCATION_LOCAL (fixP))
26542159047fSniklas 	    {
2655b55d4692Sfgsch 	      /* This fixup was made when the symbol's segment was
2656b55d4692Sfgsch 		 SEG_UNKNOWN, but it is now in the local segment.
2657b55d4692Sfgsch 		 So we know how to do the address without relocation.  */
2658c074d1c9Sdrahn 	      add_number += S_GET_VALUE (fixP->fx_addsy);
2659c074d1c9Sdrahn 	      fixP->fx_offset = add_number;
2660c074d1c9Sdrahn 	      if (fixP->fx_pcrel)
2661c074d1c9Sdrahn 		add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2662c074d1c9Sdrahn 	      fixP->fx_addsy = NULL;
26632159047fSniklas 	      fixP->fx_pcrel = 0;
2664c074d1c9Sdrahn 	    }
2665c074d1c9Sdrahn 	  else if (add_symbol_segment == absolute_section
2666c074d1c9Sdrahn 		   && !TC_FORCE_RELOCATION_ABS (fixP))
2667c074d1c9Sdrahn 	    {
2668c074d1c9Sdrahn 	      add_number += S_GET_VALUE (fixP->fx_addsy);
2669c074d1c9Sdrahn 	      fixP->fx_offset = add_number;
26702159047fSniklas 	      fixP->fx_addsy = NULL;
26712159047fSniklas 	    }
2672c074d1c9Sdrahn 	  else if (add_symbol_segment != undefined_section
26732159047fSniklas #ifdef BFD_ASSEMBLER
2674c074d1c9Sdrahn 		   && ! bfd_is_com_section (add_symbol_segment)
26752159047fSniklas #endif
2676c074d1c9Sdrahn 		   && MD_APPLY_SYM_VALUE (fixP))
2677c074d1c9Sdrahn 	    add_number += S_GET_VALUE (fixP->fx_addsy);
26782159047fSniklas 	}
26792159047fSniklas 
2680c074d1c9Sdrahn       if (fixP->fx_pcrel)
26812159047fSniklas 	{
2682c074d1c9Sdrahn 	  add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
2683c074d1c9Sdrahn 	  if (!fixP->fx_done && fixP->fx_addsy == NULL)
26842159047fSniklas 	    {
2685c074d1c9Sdrahn 	      /* There was no symbol required by this relocation.
2686c074d1c9Sdrahn 		 However, BFD doesn't really handle relocations
2687c074d1c9Sdrahn 		 without symbols well. So fake up a local symbol in
2688c074d1c9Sdrahn 		 the absolute section.  */
2689c074d1c9Sdrahn 	      fixP->fx_addsy = abs_section_sym;
26902159047fSniklas 	    }
26912159047fSniklas 	}
26922159047fSniklas 
26932159047fSniklas       if (!fixP->fx_done)
2694c074d1c9Sdrahn 	md_apply_fix3 (fixP, &add_number, this_segment);
26952159047fSniklas 
2696c074d1c9Sdrahn       if (!fixP->fx_done)
2697c074d1c9Sdrahn 	{
2698c074d1c9Sdrahn 	  ++seg_reloc_count;
2699c074d1c9Sdrahn 	  if (fixP->fx_addsy == NULL)
2700c074d1c9Sdrahn 	    fixP->fx_addsy = abs_section_sym;
2701c074d1c9Sdrahn 	  symbol_mark_used_in_reloc (fixP->fx_addsy);
2702c074d1c9Sdrahn 	  if (fixP->fx_subsy != NULL)
2703c074d1c9Sdrahn 	    symbol_mark_used_in_reloc (fixP->fx_subsy);
27042159047fSniklas 	}
2705b305b0f1Sespie 
2706c074d1c9Sdrahn       if (!fixP->fx_bit_fixP && !fixP->fx_no_overflow && fixP->fx_size != 0)
2707b305b0f1Sespie 	{
2708c074d1c9Sdrahn 	  if (fixP->fx_size < sizeof (valueT))
2709b305b0f1Sespie 	    {
2710b305b0f1Sespie 	      valueT mask;
2711b305b0f1Sespie 
2712b305b0f1Sespie 	      mask = 0;
2713b55d4692Sfgsch 	      mask--;		/* Set all bits to one.  */
2714c074d1c9Sdrahn 	      mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
2715b305b0f1Sespie 	      if ((add_number & mask) != 0 && (add_number & mask) != mask)
2716b305b0f1Sespie 		{
2717b305b0f1Sespie 		  char buf[50], buf2[50];
2718c074d1c9Sdrahn 		  sprint_value (buf, fragP->fr_address + fixP->fx_where);
2719b305b0f1Sespie 		  if (add_number > 1000)
2720b305b0f1Sespie 		    sprint_value (buf2, add_number);
2721b305b0f1Sespie 		  else
2722b305b0f1Sespie 		    sprintf (buf2, "%ld", (long) add_number);
2723b305b0f1Sespie 		  as_bad_where (fixP->fx_file, fixP->fx_line,
2724c074d1c9Sdrahn 				_("value of %s too large for field of %d bytes at %s"),
2725c074d1c9Sdrahn 				buf2, fixP->fx_size, buf);
2726b55d4692Sfgsch 		} /* Generic error checking.  */
2727b305b0f1Sespie 	    }
2728b305b0f1Sespie #ifdef WARN_SIGNED_OVERFLOW_WORD
2729b305b0f1Sespie 	  /* Warn if a .word value is too large when treated as a signed
2730b305b0f1Sespie 	     number.  We already know it is not too negative.  This is to
2731b305b0f1Sespie 	     catch over-large switches generated by gcc on the 68k.  */
2732b305b0f1Sespie 	  if (!flag_signed_overflow_ok
2733c074d1c9Sdrahn 	      && fixP->fx_size == 2
2734b305b0f1Sespie 	      && add_number > 0x7fff)
2735b305b0f1Sespie 	    as_bad_where (fixP->fx_file, fixP->fx_line,
2736c074d1c9Sdrahn 			  _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
2737b305b0f1Sespie 			  (long) add_number,
2738c074d1c9Sdrahn 			  (long) (fragP->fr_address + fixP->fx_where));
2739b305b0f1Sespie #endif
2740b55d4692Sfgsch 	}			/* Not a bit fix.  */
2741b305b0f1Sespie 
27422159047fSniklas #ifdef TC_VALIDATE_FIX
2743b305b0f1Sespie     skip:  ATTRIBUTE_UNUSED_LABEL
2744b305b0f1Sespie       ;
27452159047fSniklas #endif
27462159047fSniklas #ifdef DEBUG5
27472159047fSniklas       fprintf (stderr, "result:\n");
27482159047fSniklas       print_fixup (fixP);
27492159047fSniklas #endif
27502159047fSniklas     }				/* For each fixS in this segment.  */
27512159047fSniklas 
27522159047fSniklas   TC_ADJUST_RELOC_COUNT (fixP, seg_reloc_count);
27532159047fSniklas   return seg_reloc_count;
27542159047fSniklas }
27552159047fSniklas 
27562159047fSniklas #endif /* defined (BFD_ASSEMBLER) || (!defined (BFD) && !defined (OBJ_VMS)) */
27572159047fSniklas 
27582159047fSniklas void
number_to_chars_bigendian(char * buf,valueT val,int n)2759*007c2a45Smiod number_to_chars_bigendian (char *buf, valueT val, int n)
27602159047fSniklas {
2761c074d1c9Sdrahn   if (n <= 0)
27622159047fSniklas     abort ();
27632159047fSniklas   while (n--)
27642159047fSniklas     {
27652159047fSniklas       buf[n] = val & 0xff;
27662159047fSniklas       val >>= 8;
27672159047fSniklas     }
27682159047fSniklas }
27692159047fSniklas 
27702159047fSniklas void
number_to_chars_littleendian(char * buf,valueT val,int n)2771*007c2a45Smiod number_to_chars_littleendian (char *buf, valueT val, int n)
27722159047fSniklas {
2773c074d1c9Sdrahn   if (n <= 0)
27742159047fSniklas     abort ();
27752159047fSniklas   while (n--)
27762159047fSniklas     {
27772159047fSniklas       *buf++ = val & 0xff;
27782159047fSniklas       val >>= 8;
27792159047fSniklas     }
27802159047fSniklas }
27812159047fSniklas 
2782191aa565Sniklas void
write_print_statistics(FILE * file)2783*007c2a45Smiod write_print_statistics (FILE *file)
2784191aa565Sniklas {
2785b305b0f1Sespie   fprintf (file, "fixups: %d\n", n_fixups);
2786191aa565Sniklas }
2787191aa565Sniklas 
2788b55d4692Sfgsch /* For debugging.  */
27892159047fSniklas extern int indent_level;
27902159047fSniklas 
27912159047fSniklas void
print_fixup(fixS * fixp)2792*007c2a45Smiod print_fixup (fixS *fixp)
27932159047fSniklas {
27942159047fSniklas   indent_level = 1;
27952159047fSniklas   fprintf (stderr, "fix %lx %s:%d", (long) fixp, fixp->fx_file, fixp->fx_line);
27962159047fSniklas   if (fixp->fx_pcrel)
27972159047fSniklas     fprintf (stderr, " pcrel");
27982159047fSniklas   if (fixp->fx_pcrel_adjust)
27992159047fSniklas     fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
28002159047fSniklas   if (fixp->fx_im_disp)
28012159047fSniklas     {
28022159047fSniklas #ifdef TC_NS32K
28032159047fSniklas       fprintf (stderr, " im_disp=%d", fixp->fx_im_disp);
28042159047fSniklas #else
28052159047fSniklas       fprintf (stderr, " im_disp");
28062159047fSniklas #endif
28072159047fSniklas     }
28082159047fSniklas   if (fixp->fx_tcbit)
28092159047fSniklas     fprintf (stderr, " tcbit");
28102159047fSniklas   if (fixp->fx_done)
28112159047fSniklas     fprintf (stderr, " done");
28122159047fSniklas   fprintf (stderr, "\n    size=%d frag=%lx where=%ld offset=%lx addnumber=%lx",
28132159047fSniklas 	   fixp->fx_size, (long) fixp->fx_frag, (long) fixp->fx_where,
28142159047fSniklas 	   (long) fixp->fx_offset, (long) fixp->fx_addnumber);
28152159047fSniklas #ifdef BFD_ASSEMBLER
28162159047fSniklas   fprintf (stderr, "\n    %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
28172159047fSniklas 	   fixp->fx_r_type);
28182159047fSniklas #else
28192159047fSniklas #ifdef NEED_FX_R_TYPE
28202159047fSniklas   fprintf (stderr, " r_type=%d", fixp->fx_r_type);
28212159047fSniklas #endif
28222159047fSniklas #endif
28232159047fSniklas   if (fixp->fx_addsy)
28242159047fSniklas     {
28252159047fSniklas       fprintf (stderr, "\n   +<");
28262159047fSniklas       print_symbol_value_1 (stderr, fixp->fx_addsy);
28272159047fSniklas       fprintf (stderr, ">");
28282159047fSniklas     }
28292159047fSniklas   if (fixp->fx_subsy)
28302159047fSniklas     {
28312159047fSniklas       fprintf (stderr, "\n   -<");
28322159047fSniklas       print_symbol_value_1 (stderr, fixp->fx_subsy);
28332159047fSniklas       fprintf (stderr, ">");
28342159047fSniklas     }
28352159047fSniklas   fprintf (stderr, "\n");
2836b305b0f1Sespie #ifdef TC_FIX_DATA_PRINT
2837b305b0f1Sespie   TC_FIX_DATA_PRINT (stderr, fixp);
2838b305b0f1Sespie #endif
28392159047fSniklas }
2840