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