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