1/* layout.c (was part of write.c in original GAS version)
2   Copyright (C) 1986,1987 Free Software Foundation, Inc.
3
4This file is part of GAS, the GNU Assembler.
5
6GAS is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GAS is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GAS; see the file COPYING.  If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20#include <stdlib.h>
21#include <string.h>
22#include "stuff/rnd.h"
23#include "as.h"
24#include "sections.h"
25#include "frags.h"
26#include "symbols.h"
27#include "fixes.h"
28#include "messages.h"
29#include "expr.h"
30#include "md.h"
31#include "obstack.h"
32#include "input-scrub.h"
33#include "dwarf2dbg.h"
34#if I386
35#include "i386.h"
36#endif
37
38#ifdef SPARC
39/* internal relocation types not to be emitted */
40#define SPARC_RELOC_13 (127)
41#define SPARC_RELOC_22 (126)
42#endif
43
44#ifdef ARM
45/* FROM tc-arm.h line 82 */
46int
47arm_force_relocation (struct fix * fixp);
48
49#define TC_FORCE_RELOCATION(FIX) arm_force_relocation (FIX)
50
51/* FROM tc-arm.h line 133 */
52/* This expression evaluates to true if the relocation is for a local
53   object for which we still want to do the relocation at runtime.
54   False if we are willing to perform this relocation while building
55   the .o file.  GOTOFF does not need to be checked here because it is
56   not pcrel.  I am not sure if some of the others are ever used with
57   pcrel, but it is easier to be safe than sorry.  */
58
59#define TC_FORCE_RELOCATION_LOCAL(FIX)			\
60  (!(FIX)->fx_pcrel					\
61   || TC_FORCE_RELOCATION (FIX))
62
63/* FROM write.c line 35 */
64#ifndef TC_FORCE_RELOCATION
65#define TC_FORCE_RELOCATION(FIX)		\
66  (0)
67#endif
68
69extern int arm_relax_frag (int nsect, fragS *fragp, int32_t stretch);
70
71#endif /* ARM */
72
73/* FROM write.c line 96 */
74#ifndef	MD_PCREL_FROM_SECTION
75extern int32_t md_pcrel_from_section(fixS * fixP);
76#define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from_section (FIX)
77#endif
78
79static void fixup_section(
80    fixS *fixP,
81    int nsect);
82#ifndef SPARC
83static int is_assembly_time_constant_subtraction_expression(
84    symbolS *add_symbolP,
85    int add_symbol_nsect,
86    symbolS *sub_symbolP,
87    int sub_symbol_nsect);
88#endif /* !defined(SPARC) */
89static int relax_section(
90    struct frag *section_frag_root,
91    int nsect);
92static relax_addressT relax_align(
93    relax_addressT address,
94    uint32_t alignment);
95#ifndef ARM
96static int is_down_range(
97    struct frag *f1,
98    struct frag *f2);
99#endif /* !defined(ARM) */
100
101/*
102 * add_last_frags_to_sections() does what layout_addresses() does below about
103 * adding a last ".fill 0" frag to each section.  This is called by
104 * dwarf2_finish() allow get_frag_fix() in dwarf2dbg.c to work for the last
105 * fragment in a section.
106 */
107void
108add_last_frags_to_sections(
109void)
110{
111    struct frchain *frchainP;
112
113	if(frchain_root == NULL)
114	    return;
115
116	/*
117	 * If there is any current frag close it off.
118	 */
119	if(frag_now != NULL && frag_now->fr_fix == 0){
120	    frag_now->fr_fix = obstack_next_free(&frags) -
121			       frag_now->fr_literal;
122	    frag_wane(frag_now);
123	}
124
125	/*
126	 * For every section, add a last ".fill 0" frag that will later be used
127	 * as the ending address of that section.
128	 */
129	for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
130	    /*
131	     * We must do the obstack_finish(), so the next object we put on
132	     * obstack frags will not appear to start at the fr_literal of the
133	     * current frag.  Also, it ensures that the next object will begin
134	     * on a address that is aligned correctly for the engine that runs
135	     * the assembler.
136	     */
137	    (void)obstack_finish(&frags);
138
139	    /*
140	     * Make a fresh frag for the last frag.
141	     */
142	    frag_now = (fragS *)obstack_alloc(&frags, SIZEOF_STRUCT_FRAG);
143	    memset(frag_now, '\0', SIZEOF_STRUCT_FRAG);
144	    frag_now->fr_next = NULL;
145	    (void)obstack_finish(&frags);
146
147	    /*
148	     * Append the new frag to current frchain.
149	     */
150	    frchainP->frch_last->fr_next = frag_now;
151	    frchainP->frch_last = frag_now;
152	    frag_wane(frag_now);
153	}
154}
155
156/*
157 * layout_addresses() is called after all the assembly code has been read and
158 * fragments, symbols and fixups have been created.  This routine sets the
159 * address of the fragments and symbols.  Then it does the fixups of the frags
160 * and prepares the fixes so relocation entries can be created from them.
161 */
162void
163layout_addresses(
164void)
165{
166    struct frchain *frchainP;
167    fragS *fragP;
168    relax_addressT slide, tmp;
169    symbolS *symbolP;
170    uint32_t nbytes, fill_size, repeat_expression, partial_bytes, layout_pass;
171    uint32_t section_type;
172    relax_stateT old_fr_type;
173    int changed;
174
175	if(frchain_root == NULL)
176	    return;
177
178	/*
179	 * If there is any current frag close it off.
180	 */
181	if(frag_now != NULL && frag_now->fr_fix == 0){
182	    frag_now->fr_fix = obstack_next_free(&frags) -
183			       frag_now->fr_literal;
184	    frag_wane(frag_now);
185	}
186
187	/*
188	 * For every section, add a last ".fill 0" frag that will later be used
189	 * as the ending address of that section.
190	 */
191	for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
192	    /*
193	     * We must do the obstack_finish(), so the next object we put on
194	     * obstack frags will not appear to start at the fr_literal of the
195	     * current frag.  Also, it ensures that the next object will begin
196	     * on a address that is aligned correctly for the engine that runs
197	     * the assembler.
198	     */
199	    (void)obstack_finish(&frags);
200
201	    /*
202	     * Make a fresh frag for the last frag.
203	     */
204	    frag_now = (fragS *)obstack_alloc(&frags, SIZEOF_STRUCT_FRAG);
205	    memset(frag_now, '\0', SIZEOF_STRUCT_FRAG);
206	    frag_now->fr_next = NULL;
207	    (void)obstack_finish(&frags);
208
209	    /*
210	     * Append the new frag to current frchain.
211	     */
212	    frchainP->frch_last->fr_next = frag_now;
213	    frchainP->frch_last = frag_now;
214	    frag_wane(frag_now);
215
216	}
217
218	/*
219	 * Now set the relative addresses of frags within the section by
220	 * relaxing each section.  That is all sections will start at address
221	 * zero and addresses of the frags in that section will increase from
222	 * there.
223	 *
224	 * The debug sections are done last as other section are needed to be
225	 * done first becase debug sections may have line numbers with .loc
226	 * directives in them and their sizes need to be set before processing
227	 * the line number sections.  We also do sections that have rs_leb128s
228	 * in them before debug sections but after other sections since they
229	 * are used for things like exception tables and they may be refering to
230	 * sections such that their sizes too must be known first.
231	 */
232	for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
233	    if((frchainP->frch_section.flags & S_ATTR_DEBUG) == S_ATTR_DEBUG)
234		frchainP->layout_pass = 2;
235	    else if(frchainP->has_rs_leb128s == TRUE)
236		frchainP->layout_pass = 1;
237	    else
238		frchainP->layout_pass = 0;
239	}
240	for(layout_pass = 0; layout_pass < 3; layout_pass++){
241	    do{
242		changed = 0;
243		for(frchainP = frchain_root;
244		    frchainP;
245		    frchainP = frchainP->frch_next){
246		    if(frchainP->layout_pass != layout_pass)
247			continue;
248		    section_type = frchainP->frch_section.flags & SECTION_TYPE;
249		    if(section_type == S_ZEROFILL ||
250		       section_type == S_THREAD_LOCAL_ZEROFILL)
251			continue;
252		    /*
253		     * This is done so in case md_estimate_size_before_relax()
254		     * (called by relax_section) wants to make fixSs they are
255		     * for this section.
256		     */
257		    frchain_now = frchainP;
258
259		    changed += relax_section(frchainP->frch_root,
260					    frchainP->frch_nsect);
261		}
262	    }
263	    while(changed != 0);
264	}
265
266	/*
267	 * Now set the absolute addresses of all frags by sliding the frags in
268	 * each non-zerofill section by the address ranges taken up by the
269	 * sections before it.
270	 */
271	slide = 0;
272	for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
273	    section_type = frchainP->frch_section.flags & SECTION_TYPE;
274	    if(section_type == S_ZEROFILL ||
275	       section_type == S_THREAD_LOCAL_ZEROFILL)
276		continue;
277	    slide = rnd(slide, 1 << frchainP->frch_section.align);
278	    tmp = frchainP->frch_last->fr_address;
279	    if(slide != 0){
280		for(fragP = frchainP->frch_root; fragP; fragP = fragP->fr_next){
281		    fragP->fr_address += slide;
282		}
283	    }
284	    slide += tmp;
285	}
286	/*
287	 * Now with the non-zerofill section addresses set set all of the
288	 * addresses of the zerofill sections.  Comming in the fr_address is
289	 * the size of the section and going out it is the start address.  This
290	 * will make layout_symbols() work out naturally.  The only funky thing
291	 * is that section numbers do not end up in address order.
292	 */
293	for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
294	    section_type = frchainP->frch_section.flags & SECTION_TYPE;
295	    if(section_type != S_ZEROFILL &&
296	       section_type != S_THREAD_LOCAL_ZEROFILL)
297		continue;
298	    slide = rnd(slide, 1 << frchainP->frch_section.align);
299
300	    tmp = frchainP->frch_root->fr_address;
301	    frchainP->frch_root->fr_address = slide;
302	    frchainP->frch_last->fr_address = tmp + slide;
303	    slide += tmp;
304	}
305
306	/*
307	 * Set the symbol addresses based on their frag's address.
308	 * First forward references are handled.
309	 */
310	for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
311	    if(symbolP->sy_forward != NULL){
312		if(symbolP->sy_nlist.n_type & N_STAB)
313		    symbolP->sy_other = symbolP->sy_forward->sy_other;
314		symbolP->sy_value += symbolP->sy_forward->sy_value +
315				     symbolP->sy_forward->sy_frag->fr_address;
316		symbolP->sy_forward = 0;
317	    }
318	}
319	for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
320	    symbolP->sy_value += symbolP->sy_frag->fr_address;
321	}
322
323	/*
324	 * At this point the addresses of frags now reflect addresses we use in
325	 * the object file and the symbol values are correct.
326	 * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
327	 * Also converting any machine-dependent frags using md_convert_frag();
328	 */
329	for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
330	    /*
331	     * This is done so any fixes created by md_convert_frag() are for
332	     * this section.
333	     */
334	    frchain_now = frchainP;
335
336	    for(fragP = frchainP->frch_root; fragP; fragP = fragP->fr_next){
337		switch(fragP->fr_type){
338		case rs_align:
339		case rs_org:
340		    old_fr_type = fragP->fr_type;
341		    /* convert this frag to an rs_fill type */
342		    fragP->fr_type = rs_fill;
343		    /*
344		     * Calculate the number of bytes the variable part of the
345		     * the rs_fill frag will need to fill.  Then calculate this
346		     * as the fill_size * repeat_expression + partial_bytes.
347		     */
348		    know(fragP->fr_next != NULL);
349		    nbytes = fragP->fr_next->fr_address -
350			     fragP->fr_address -
351			     fragP->fr_fix;
352		    if((int)nbytes < 0){
353			as_warn("rs_org invalid, dot past value by %d bytes",
354				nbytes);
355			nbytes = 0;
356		    }
357		    fill_size = fragP->fr_var;
358		    repeat_expression = nbytes / fill_size;
359#ifdef I386
360		    /*
361		     * For x86 architecures in sections containing only
362		     * instuctions being padded with nops that are aligned to 16
363		     * bytes or less and are assembled with -dynamic we will
364		     * actually end up padding with the optimal nop sequence.
365		     * Previously there has been the maximum number of bytes
366		     * allocated in the frag to use for this.
367		     */
368		    if(old_fr_type == rs_align &&
369		       (frchain_now->frch_section.flags &
370			S_ATTR_PURE_INSTRUCTIONS) != 0 &&
371			 fill_size == 1 &&
372			 fragP->fr_literal[fragP->fr_fix] == (char)0x90 &&
373			 nbytes > 0 && nbytes < 16 &&
374			 flagseen['k'] == TRUE){
375			i386_align_code(fragP, nbytes);
376			/*
377			 * The call to i386_align_code() has set the fill_size
378			 * in fragP->fr_var to nbytes. So we set the fr_offset
379			 * to the fill repeat_expression to 1 to match for this
380			 * now an rs_fill type frag.
381			 */
382			fragP->fr_offset = 1;
383			break;
384		    }
385#endif /* I386 */
386		    partial_bytes = nbytes - (repeat_expression * fill_size);
387		    /*
388		     * Now set the fr_offset to the fill repeat_expression
389		     * since this is now an rs_fill type.  The fr_var is still
390		     * the fill_size.
391		     */
392		    fragP->fr_offset = repeat_expression;
393		    /*
394		     * For rs_align frags there may be partial_bytes to fill
395		     * with zeros before we can fill with the fill_expression
396		     * of fill_size.  When the rs_align frag was created it was
397		     * created with fill_size-1 extra bytes in the fixed part.
398		     */
399		    if(partial_bytes != 0){
400			/* moved the fill_expression bytes foward */
401			memmove(fragP->fr_literal +fragP->fr_fix +partial_bytes,
402				fragP->fr_literal +fragP->fr_fix,
403				fragP->fr_var);
404    			/* zero out the partial_bytes */
405    			memset(fragP->fr_literal + fragP->fr_fix,
406			       '\0',
407			       partial_bytes);
408			/* adjust the fixed part of the frag */
409    			fragP->fr_fix += partial_bytes;
410		    }
411		    break;
412
413		case rs_fill:
414		    break;
415
416		case rs_machine_dependent:
417		    md_convert_frag(fragP);
418		    /*
419		     * After md_convert_frag, we make the frag into a ".fill 0"
420		     * md_convert_frag() should set up any fixSs and constants
421		     * required.
422		     */
423		    frag_wane(fragP);
424		    break;
425
426		case rs_dwarf2dbg:
427		    dwarf2dbg_convert_frag(fragP);
428		    break;
429
430		case rs_leb128:
431		  {
432		    int size;
433#ifdef OLD
434		    valueT value = S_GET_VALUE (fragP->fr_symbol);
435#else
436		    valueT value;
437  		      expressionS *expression;
438
439		      if(fragP->fr_symbol->expression != NULL){
440			expression =
441			  (expressionS *)fragP->fr_symbol->expression;
442			value = 0;
443			if(expression->X_add_symbol != NULL)
444			    value += expression->X_add_symbol->sy_nlist.n_value;
445			if(expression->X_subtract_symbol != NULL)
446			   value -=
447			     expression->X_subtract_symbol->sy_nlist.n_value;
448			value += expression->X_add_number;
449		      }
450		      else{
451			value = fragP->fr_symbol->sy_nlist.n_value +
452				fragP->fr_address;
453		      }
454#endif
455
456		    size = output_leb128 (fragP->fr_literal + fragP->fr_fix,
457					  value,
458					  fragP->fr_subtype);
459
460		    fragP->fr_fix += size;
461		    fragP->fr_type = rs_fill;
462		    fragP->fr_var = 0;
463		    fragP->fr_offset = 0;
464		    fragP->fr_symbol = NULL;
465		  }
466		  break;
467
468
469		default:
470		    BAD_CASE(fragP->fr_type);
471		    break;
472		}
473	    }
474	}
475
476	/*
477	 * For each section do the fixups for the frags.
478	 */
479	for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
480	    now_seg = frchainP->frch_nsect;
481	    fixup_section(frchainP->frch_fix_root, frchainP->frch_nsect);
482	}
483}
484
485/*
486 * fixup_section() does the fixups of the frags and prepares the fixes so
487 * relocation entries can be created from them.  The fixups cause the contents
488 * of the frag to have the value for the fixup expression.  A fix structure that
489 * ends up with a non-NULL fx_addsy will have a relocation entry created for it.
490 */
491static
492void
493fixup_section(
494fixS *fixP,
495int nsect)
496{
497    symbolS *add_symbolP;
498    symbolS *sub_symbolP;
499    signed_expr_t value;
500    int size;
501    char *place;
502    int32_t where;
503    char pcrel;
504    fragS *fragP;
505    int	add_symbol_N_TYPE;
506    int	add_symbol_nsect;
507#ifndef SPARC
508    int sub_symbol_nsect;
509#endif
510
511	/*
512	 * The general fix expression is "fx_addsy - fx_subsy + fx_offset".
513	 * The goal is to put the result of this expression into the frag at
514	 * "place" for size "size".  The value of the expression is calculated
515	 * in the variable "value" and starts with just the fx_offset.
516	 */
517	for( ; fixP != NULL; fixP = fixP->fx_next){
518	    fragP       = fixP->fx_frag;
519	    know(fragP);
520	    where	= fixP->fx_where;
521	    place       = fragP->fr_literal + where;
522	    size	= fixP->fx_size;
523#ifdef TC_FIXUP_SYMBOL
524		fixP->fx_offset += TC_FIXUP_SYMBOL(fixP, nsect, &fixP->fx_addsy);
525		fixP->fx_offset -= TC_FIXUP_SYMBOL(fixP, nsect, &fixP->fx_subsy);
526#endif
527#if defined(I386) && defined(ARCH64)
528		if(fixP->fx_addsy == fixP->fx_subsy){
529			/*
530			 * If we've fixed up both symbols to the same location,
531			 * we don't need a relocation entry.
532			 */
533			fixP->fx_addsy = NULL;
534			fixP->fx_subsy = NULL;
535		}
536#endif
537	    add_symbolP = fixP->fx_addsy;
538	    sub_symbolP = fixP->fx_subsy;
539	    value  	= fixP->fx_offset;
540	    pcrel       = fixP->fx_pcrel;
541
542#if ARM
543	    /* If the symbol is defined in this file, the linker won't set the
544	       low-order bit for a Thumb symbol, so we have to do it here.  */
545	    if(add_symbolP != NULL && add_symbolP->sy_desc & N_ARM_THUMB_DEF &&
546	       !(sub_symbolP != NULL && sub_symbolP->sy_desc & N_ARM_THUMB_DEF) &&
547	       !pcrel){
548	        value |= 1;
549	    }
550#endif
551
552	    add_symbol_N_TYPE = 0;
553	    add_symbol_nsect = 0;
554
555	    if(add_symbolP != NULL){
556		add_symbol_N_TYPE = add_symbolP->sy_type & N_TYPE;
557		if(add_symbol_N_TYPE == N_SECT)
558		    add_symbol_nsect = add_symbolP->sy_other;
559	    }
560
561	    /*
562	     * Is there a subtract symbol?
563	     */
564	    if(sub_symbolP){
565		/* is it just -sym ? */
566		if(add_symbolP == NULL){
567		    if(sub_symbolP->sy_type != N_ABS)
568			as_warn("Negative of non-absolute symbol %s",
569				sub_symbolP->sy_name);
570#if !(defined(I386) && defined(ARCH64))
571			/* Symbol offsets are not part of fixups for x86_64. */
572		    value -= sub_symbolP->sy_value;
573#endif
574		    fixP->fx_subsy = NULL;
575		}
576		/*
577		 * There are both an add symbol and a subtract symbol at this
578		 * point.
579		 *
580		 * If both symbols are absolute then just calculate the
581		 * value of the fix expression and no relocation entry will be
582		 * needed.
583		 */
584		else if((sub_symbolP->sy_type & N_TYPE) == N_ABS &&
585		        (add_symbolP->sy_type & N_TYPE) == N_ABS){
586		    value += add_symbolP->sy_value - sub_symbolP->sy_value;
587		    add_symbolP = NULL;
588		    fixP->fx_addsy = NULL; /* no relocation entry */
589		    fixP->fx_subsy = NULL;
590		}
591		/*
592		 * If both symbols are defined in a section then calculate the
593		 * value of the fix expression and let a section difference
594		 * relocation entry be created.
595		 */
596		else if((sub_symbolP->sy_type & N_TYPE) == N_SECT &&
597		        (add_symbolP->sy_type & N_TYPE) == N_SECT){
598#if defined(I386) && !defined(ARCH64)
599		    /*
600		     * For 'symbol@TLVP - subtract_symbol' type relocations the
601		     * subtract_symbol value is stored in the contents of the
602		     * item to be relocated.
603		     */
604		    if(fixP->fx_r_type == GENERIC_RELOC_TLV){
605			value += fixP->fx_frag->fr_address + where +
606				 fixP->fx_size - sub_symbolP->sy_value;
607			fixP->fx_subsy = NULL; /* no SECTDIFF reloc entry */
608			fixP->fx_pcrel = TRUE; /* force pcrel */
609			goto down;
610		    }
611#endif
612		    /*
613		     * We are use the new features that are incompatible with
614		     * 3.2 then just calculate the value and let this create a
615		     * SECTDIFF relocation type.
616		     */
617#ifdef SPARC
618		    /*
619		     * Special case dealing with assembler internal relocation
620		     * entries SPARC_RELOC_13 and RELOC_22. The can not be
621		     * output and must be resolved.
622		     */
623		    if((fixP->fx_r_type == SPARC_RELOC_13) ||
624		       (fixP->fx_r_type == SPARC_RELOC_22)){
625			if(sub_symbolP->sy_other == add_symbolP->sy_other){
626			    value += add_symbolP->sy_value -
627			    sub_symbolP->sy_value;
628			    add_symbolP = NULL;
629			    fixP->fx_addsy = NULL; /* no relocation entry */
630			    fixP->fx_subsy = NULL;
631			}
632			else{
633			    as_warn("Can't emit reloc type %u {-symbol \"%s\"} "
634			            "@ file address %llu (mode?).",
635				    fixP->fx_r_type, sub_symbolP->sy_name,
636				    fragP->fr_address + where);
637			}
638		    }
639		    else
640			value += add_symbolP->sy_value - sub_symbolP->sy_value;
641#else
642#if !(defined(I386) && defined(ARCH64))
643			/*
644			 * Special case for x86_64.  'value' doesn't include
645			 * the difference between the two symbols because
646			 * that's handled by the subtractor/vanilla reloc pair.
647			 */
648		    value += add_symbolP->sy_value;
649		    value -= sub_symbolP->sy_value;
650#else
651		    /*
652		     * But for x86_64 expressions in the debug section must
653		     * be the actual value of the expression.
654		     */
655		    if(is_section_debug(nsect)){
656			value += add_symbolP->sy_value;
657			value -= sub_symbolP->sy_value;
658		    }
659#endif
660		    sub_symbol_nsect = sub_symbolP->sy_other;
661		    /*
662		     * If we have the special assembly time constant expression
663		     * of the difference of two symbols defined in the same
664		     * section then divided by exactly 2 adjust the value and
665		     * make sure these symbols will produce an assembly time
666		     * constant.
667		     */
668		    if(fixP->fx_sectdiff_divide_by_two == 1){
669			value = value / 2;
670			if(is_assembly_time_constant_subtraction_expression(
671				add_symbolP, add_symbol_nsect,
672				sub_symbolP, sub_symbol_nsect) == TRUE){
673			    fixP->fx_addsy = NULL; /* no relocation entry */
674			    goto down;
675			}
676			else{
677			    layout_line = fixP->line;
678			    layout_file = fixP->file;
679			    as_warn("section difference divide by two "
680				    "expression, \"%s\" minus \"%s\" divide by "
681				    "2 will not produce an assembly time "
682				    "constant", add_symbolP->sy_name,
683				    sub_symbolP->sy_name);
684			}
685		    }
686		    if(is_end_section_address(add_symbol_nsect,
687					      add_symbolP->sy_value) ||
688		       is_end_section_address(sub_symbol_nsect,
689					      sub_symbolP->sy_value)){
690			if(is_assembly_time_constant_subtraction_expression(
691				add_symbolP, add_symbol_nsect,
692				sub_symbolP, sub_symbol_nsect) == TRUE){
693			    fixP->fx_addsy = NULL; /* no relocation entry */
694			    goto down;
695			}
696			if(is_section_debug(nsect) &&
697	   		   strcmp(add_symbolP->sy_name, FAKE_LABEL_NAME) == 0 &&
698	   		   strcmp(sub_symbolP->sy_name, FAKE_LABEL_NAME) == 0){
699			    fixP->fx_addsy = NULL; /* no relocation entry */
700			    goto down;
701			}
702			layout_line = fixP->line;
703			layout_file = fixP->file;
704			as_warn("section difference relocatable subtraction "
705				"expression, \"%s\" minus \"%s\" using a "
706				"symbol at the end of section will not "
707				"produce an assembly time constant",
708				add_symbolP->sy_name, sub_symbolP->sy_name);
709			as_warn("use a symbol with a constant value created "
710				"with an assignment instead of the expression, "
711				"L_const_sym = %s - %s", add_symbolP->sy_name,
712				sub_symbolP->sy_name);
713			layout_line = 0;
714			layout_file = NULL;
715		    }
716#endif
717		    goto down;
718		}
719		/*
720		 * If the subtract symbol is absolute subtract it's value from
721		 * the fix expression and let a relocation entry get created
722		 * that is not a section difference type.
723		 */
724		else if(sub_symbolP->sy_type == N_ABS){
725		    value -= sub_symbolP->sy_value;
726		    fixP->fx_subsy = NULL; /* no SECTDIFF relocation entry */
727		}
728#if defined(I386) && !defined(ARCH64)
729		/*
730		 * For 'symbol@TLVP - subtract_symbol' type relocations the
731		 * subtract_symbol value is stored in the contents of the item
732		 * to be relocated.
733		 */
734		else if(fixP->fx_r_type == GENERIC_RELOC_TLV){
735		    value += fixP->fx_frag->fr_address + where + fixP->fx_size -
736			     sub_symbolP->sy_value;
737		    fixP->fx_subsy = NULL; /* no SECTDIFF relocation entry */
738		    fixP->fx_pcrel = TRUE; /* force pcrel */
739		}
740#endif
741		/*
742		 * At this point we have something we can't generate a
743		 * relocation entry for (two undefined symbols, etc.).
744		 */
745	        else{
746		     layout_line = fixP->line;
747		     layout_file = fixP->file;
748		     as_bad("non-relocatable subtraction expression, \"%s\" "
749			     "minus \"%s\"", add_symbolP->sy_name,
750			     sub_symbolP->sy_name);
751		     if((add_symbolP->sy_type & N_TYPE) == N_UNDF)
752			as_bad("symbol: \"%s\" can't be undefined in a "
753				"subtraction expression", add_symbolP->sy_name);
754		     if((sub_symbolP->sy_type & N_TYPE) == N_UNDF)
755			as_bad("symbol: \"%s\" can't be undefined in a "
756				"subtraction expression", sub_symbolP->sy_name);
757		     layout_line = 0;
758		     layout_file = NULL;
759		}
760	    }
761
762	    /*
763	     * If a there is an add symbol in the fixup expression then add
764	     * the symbol value into the fixup expression's value.
765	     */
766	    if(add_symbolP){
767		/*
768		 * If this symbol is in this section and is pc-relative and we
769		 * do not want to force a pc-relative relocation entry (to
770		 * support scattered loading) then just calculate the value.
771		 */
772		if(add_symbol_nsect == nsect
773		   /* FROM write.c line 2659 */
774#ifdef ARM
775		   && !TC_FORCE_RELOCATION_LOCAL (fixP)
776#else
777		   && pcrel
778#endif
779		   && !(fixP->fx_pcrel_reloc)){
780		    /*
781		     * This fixup was made when the symbol's section was
782		     * unknown, but it is now in this section. So we know how
783		     * to do the address without relocation.
784		     */
785		    value += add_symbolP->sy_value;
786#ifdef ARM
787		    /* FROM write.c line 2667 */
788		    value -= MD_PCREL_FROM_SECTION (fixP, nsect);
789#else
790		    value -= size + where + fragP->fr_address;
791#endif
792		    pcrel = 0;	/* Lie. Don't want further pcrel processing. */
793		    fixP->fx_addsy = NULL; /* No relocations please. */
794		    /*
795		     * It would be nice to check that the address does not
796		     * overflow.
797		     * I didn't do this check because:
798		     * +  It is machine dependent in the general case (eg 32032)
799		     * +  Compiler output will never need this checking, so why
800		     *    slow down the usual case?
801		     */
802		}
803		else{
804		    switch(add_symbol_N_TYPE){
805		    case N_ABS:
806			/*
807			 * If the value of the symbol was an expression then
808			 * now evaluate the expression now.  This can happen
809			 * when symbols like:
810			 *	.set x,a-b
811			 * are used and the value of x is not known till all
812			 * of the symbols are seen and had their values set.
813			 */
814			if(add_symbolP->expression != NULL){
815			    expressionS *exp;
816
817			    exp = (expressionS *)add_symbolP->expression;
818			    value +=
819				exp->X_add_symbol->sy_value +
820				exp->X_add_number -
821				exp->X_subtract_symbol->sy_value;
822			}
823			else
824			{
825			    value += add_symbolP->sy_value;
826			}
827			fixP->fx_addsy = NULL; /* no relocation entry */
828			add_symbolP = NULL;
829			break;
830
831		    case N_SECT:
832#if (defined(I386) && defined(ARCH64))
833			/*
834			 * Symbol offsets are not part of fixups for external
835			 * symbols for x86_64.
836			 */
837			if((is_section_debug(nsect) &&
838			    add_symbol_N_TYPE != N_UNDF) ||
839			   (add_symbol_N_TYPE == N_SECT &&
840			    is_local_symbol(add_symbolP) &&
841			    !is_section_cstring_literals(add_symbol_nsect)) )
842#else
843			if(((add_symbolP->sy_type & N_EXT) != N_EXT ||
844			    add_symbol_N_TYPE != N_SECT ||
845			    !is_section_coalesced(add_symbol_nsect)) &&
846			   (add_symbolP->sy_desc & N_WEAK_DEF) != N_WEAK_DEF
847#if defined(I386) && !defined(ARCH64)
848			   &&
849			   fixP->fx_r_type != GENERIC_RELOC_TLV
850#endif
851			  )
852#endif
853			    value += add_symbolP->sy_value;
854			break;
855
856		    case N_UNDF:
857			break;
858
859		    default:
860			BAD_CASE(add_symbol_N_TYPE);
861			break;
862		    }
863		}
864	    }
865down:
866	    /*
867	     * If the fixup expression is pc-relative then the value of the pc
868	     * will be added to the expression when the machine executes the
869	     * the instruction so we adjust the fixup expression's value by
870	     * subtracting off the pc value (where) and adjust for insn size.
871	     */
872	    if(pcrel){
873#ifdef ARM
874	        /* This should work for both */
875	        /* FROM write.c line 2688 */
876		value -= MD_PCREL_FROM_SECTION (fixP, nsect);
877#elif !(defined(I386) && defined(ARCH64))
878		/* Symbol offsets are not part of fixups for x86_64. */
879		value -= size + where + fragP->fr_address;
880#endif
881		if(add_symbolP == NULL){
882		    fixP->fx_addsy = &abs_symbol; /* force relocation entry */
883		}
884	    }
885
886	    if((size == 1 && (value & 0xffffff00) &&
887			    ((value & 0xffffff80) != 0xffffff80)) ||
888	       (size == 2 && (value & 0xffff0000) &&
889			    ((value & 0xffff8000) != 0xffff8000))){
890		layout_line = fixP->line;
891		layout_file = fixP->file;
892		as_bad("Fixup of %lld too large for field width of %d",
893			value, size);
894		layout_line = 0;
895		layout_file = NULL;
896	    }
897
898	    /*
899	     * Now place the fix expression's value in the place for the size.
900	     * And save the fix expression's value to be used when creating
901	     * a relocation entry if required.
902	     */
903	    md_number_to_imm((unsigned char *)place, value, size, fixP, nsect);
904	    fixP->fx_value = value;
905
906	    /*
907	     * If this is a non-lazy pointer section and this fix is for a
908	     * local symbol without an subtract symbol then cause this not to
909	     * generate a relocation entry.  This is used with code gen for
910	     * fix-n-continue where the compiler generates indirection for
911	     * static data references.  So the assembly looks like this:
912	     *
913	     * 	.non_lazy_symbol_pointer
914	     * 	L_i$non_lazy_ptr:
915       	     * 	.indirect_symbol _i
916       	     * 	.long   _i
917	     *
918	     * this allows the value of the symbol to be set into the pointer
919	     * but not cause the relocation entry to be created.  The code in
920	     * write_object() then changes the indirect symbol table entry to
921	     * INDIRECT_SYMBOL_LOCAL when the symbol is local.  This is what
922	     * the static and dynamic linkers expect and will then cause the
923	     * pointer to be correctly relocated.
924	     */
925	    if(is_section_non_lazy_symbol_pointers(nsect) &&
926	       (add_symbolP->sy_type & N_EXT) != N_EXT &&
927	       sub_symbolP == NULL){
928		fixP->fx_addsy = NULL; /* no relocation entry */
929	    }
930	}
931}
932
933#ifndef SPARC
934/*
935 * is_assembly_time_constant_subtraction_expression() is passed the symbols and
936 * section numbers of a subtraction expression invloving symbols both defined in
937 * some section.  If the subtraction expression is an assembly time constant
938 * value then this returns 1 (TRUE) else this returns 0 (FALSE).
939 *
940 * Since the static link editor can break apart a section this routine can only
941 * return TRUE when it is known for sure these symbols will not be moved apart
942 * from each other.  So this is an assembly time constant subtraction expression
943 * if the following are all true:
944 * - the expression's symbols are assembly temporary symbols (starting with 'L')
945 * - assembly temporary symbol are not being saved (no -L flag)
946 * - the two symbols are in the same section
947 * - the section is a regular section or coalesced section (non-literal section)
948 * - there are no non-assembly temporary symbols defined between two symbols of
949 *   the expression.  For example if the assembly code is:
950 *	L1: nop
951 *	foo: nop
952 *	L2: nop
953 *   the expression is L1-L2 is not an assembly time constant because the block
954 *   of code after foo (including the address of L2) could be link edited away
955 *   from the block of code with L1.
956 */
957static
958int
959is_assembly_time_constant_subtraction_expression(
960symbolS *add_symbolP,
961int add_symbol_nsect,
962symbolS *sub_symbolP,
963int sub_symbol_nsect)
964{
965    struct frchain *frchainP;
966    uint32_t section_type, section_attributes;
967    symbolS *prev_symbol;
968    int non_assembly_temporary_symbol;
969
970	/* see if both symbols are assembly temporary symbols */
971	if(add_symbolP->sy_name == NULL || add_symbolP->sy_name[0] != 'L' ||
972	   sub_symbolP->sy_name == NULL || sub_symbolP->sy_name[0] != 'L')
973	    return(0);
974
975	/* make sure we are not saving assembly temporary symbols */
976	if(flagseen[(int)'L'])
977	    return(0);
978
979	/* make sure the two symbols are in the same section */
980	if(add_symbol_nsect != sub_symbol_nsect)
981	    return(0);
982
983	/* make sure the section is a regular or coalesced section */
984	section_attributes = 0;
985	for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
986	    if(frchainP->frch_nsect == add_symbol_nsect){
987		section_type = frchainP->frch_section.flags & SECTION_TYPE;
988		section_attributes = frchainP->frch_section.flags &
989				     SECTION_ATTRIBUTES;
990		if(section_type == S_REGULAR || section_type == S_COALESCED)
991		    break;
992		else
993		    return(0);
994	    }
995	}
996
997	/*
998	 * See if we can find the chain of symbols from the add_symbolP through
999	 * its previous symbols to the sub_symbolP.  And check for non assembler
1000	 * temporary symbols along that chain.
1001	 */
1002	non_assembly_temporary_symbol = 0;
1003	for(prev_symbol = add_symbolP->sy_prev_by_index;
1004	    prev_symbol != NULL;
1005	    prev_symbol = prev_symbol->sy_prev_by_index){
1006	    if((prev_symbol->sy_type & N_SECT) == N_SECT &&
1007	       (prev_symbol->sy_type & N_STAB) == 0 &&
1008		prev_symbol->sy_other == add_symbol_nsect){
1009		if(prev_symbol == sub_symbolP){
1010		    if(non_assembly_temporary_symbol == 0)
1011			return(1);
1012		    else
1013			return(0);
1014		}
1015		if(prev_symbol->sy_name != NULL &&
1016		   prev_symbol->sy_name[0] != 'L')
1017		    non_assembly_temporary_symbol = 1;
1018	    }
1019	}
1020
1021	/*
1022	 * Couldn't find the chain above, so now try we can find the chain of
1023	 * symbols from the sub_symbolP through its previous symbols to the
1024	 * add_symbolP.  And check for non assembler temporary symbols along
1025	 * that chain.
1026	 */
1027	non_assembly_temporary_symbol = 0;
1028	for(prev_symbol = sub_symbolP->sy_prev_by_index;
1029	    prev_symbol != NULL;
1030	    prev_symbol = prev_symbol->sy_prev_by_index){
1031	    if((prev_symbol->sy_type & N_SECT) == N_SECT &&
1032	       (prev_symbol->sy_type & N_STAB) == 0 &&
1033		prev_symbol->sy_other == sub_symbol_nsect){
1034		if(prev_symbol == add_symbolP){
1035		    if(non_assembly_temporary_symbol == 0)
1036			return(1);
1037		    else
1038			return(0);
1039		}
1040		if(prev_symbol->sy_name != NULL &&
1041		   prev_symbol->sy_name[0] != 'L')
1042		    non_assembly_temporary_symbol = 1;
1043	    }
1044	}
1045
1046	/*
1047	 * It is possible that this expression is coming from a dwarf section
1048	 * made from .file and .loc directives.  If so both symbols would have
1049	 * the FAKE_LABEL_NAME and the section_type would be
1050	 * and in this case the then the expression is an assembly time
1051	 * constant.
1052	 */
1053	if((section_attributes & S_ATTR_DEBUG) == S_ATTR_DEBUG &&
1054	   strcmp(add_symbolP->sy_name, FAKE_LABEL_NAME) == 0 &&
1055	   strcmp(sub_symbolP->sy_name, FAKE_LABEL_NAME) == 0)
1056	    return(1);
1057
1058	return(0);
1059}
1060#endif /* !defined(SPARC) */
1061
1062/*
1063 * relax_section() here we set the fr_address values in the frags.
1064 * After this, all frags in this segment have addresses that are correct
1065 * relative to the section (that is the section starts at address zero).
1066 * After all of the sections have been processed by this call and their sizes
1067 * are know then they can be slid to their final address.
1068 */
1069static
1070int
1071relax_section(
1072struct frag *frag_root,
1073int nsect)
1074{
1075    struct frag *fragP;
1076    relax_addressT address;
1077
1078    int32_t stretch; /* May be any size, 0 or negative. */
1079		     /* Cumulative number of addresses we have */
1080		     /* relaxed this pass. */
1081		     /* We may have relaxed more than one address. */
1082    int32_t stretched;  /* Have we stretched on this pass? */
1083		    /* This is 'cuz stretch may be zero, when,
1084		       in fact some piece of code grew, and
1085		       another shrank.  If a branch instruction
1086		       doesn't fit anymore, we need another pass */
1087
1088#ifndef ARM
1089    const relax_typeS *this_type;
1090    const relax_typeS *start_type;
1091    relax_substateT next_state;
1092    relax_substateT this_state;
1093    int32_t aim;
1094#endif /* !defined(ARM) */
1095
1096    int32_t growth;
1097    uint32_t was_address;
1098    int32_t offset;
1099    symbolS *symbolP;
1100    int32_t target;
1101    int32_t after;
1102    uint32_t oldoff, newoff;
1103    int ret;
1104
1105	ret = 0;
1106	growth = 0;
1107
1108	/*
1109	 * For each frag in segment count and store (a 1st guess of) fr_address.
1110	 */
1111	address = 0;
1112	for(fragP = frag_root; fragP != NULL; fragP = fragP->fr_next){
1113#ifdef ARM
1114            fragP->relax_marker = 0;
1115#endif /* ARM */
1116	    fragP->fr_address = address;
1117	    address += fragP->fr_fix;
1118	    switch(fragP->fr_type){
1119	    case rs_fill:
1120		address += fragP->fr_offset * fragP->fr_var;
1121		break;
1122
1123	    case rs_align:
1124		offset = relax_align (address, (int) fragP->fr_offset);
1125		/*
1126		 * If a maximum number of bytes to fill was specified for this
1127		 * align (stored in fr_subtype) then check to see if this align
1128		 * can be done.  If not ignore it.  If so and this alignment is
1129		 * larger than any previous alignment then this becomes the
1130		 * section's alignment.
1131		 */
1132		if(fragP->fr_subtype != 0){
1133		    if(offset > (int32_t)fragP->fr_subtype){
1134			offset = 0;
1135		    }
1136		    else{
1137			if(frchain_now->frch_section.align <
1138			   (uint32_t)fragP->fr_offset)
1139			    frchain_now->frch_section.align = fragP->fr_offset;
1140		    }
1141		}
1142		address += offset;
1143		break;
1144
1145	    case rs_org:
1146		/*
1147		 * Assume .org is nugatory. It will grow with 1st relax.
1148		 */
1149		break;
1150
1151	    case rs_machine_dependent:
1152		address += md_estimate_size_before_relax(fragP, nsect);
1153		break;
1154
1155	    case rs_dwarf2dbg:
1156		address += dwarf2dbg_estimate_size_before_relax(fragP);
1157		break;
1158
1159	    case rs_leb128:
1160	      /* Initial guess is always 1; doing otherwise can result in
1161		 stable solutions that are larger than the minimum.  */
1162	      address += fragP->fr_offset = 1;
1163	      break;
1164
1165	    default:
1166		BAD_CASE(fragP->fr_type);
1167		break;
1168	    }
1169	}
1170
1171	/*
1172	 * Do relax().
1173	 * Make repeated passes over the chain of frags allowing each frag to
1174	 * grow if needed.  On each pass each frag's address is incremented by
1175	 * the accumulated growth, kept in stretched.  Passes are continued
1176	 * until there is no stretch on the previous pass.
1177	 */
1178	do{
1179	    stretch = 0;
1180	    stretched = 0;
1181	    for(fragP = frag_root; fragP != NULL; fragP = fragP->fr_next){
1182#ifdef ARM
1183                fragP->relax_marker ^= 1;
1184#endif /* ARM */
1185		was_address = fragP->fr_address;
1186		fragP->fr_address += stretch;
1187		address = fragP->fr_address;
1188		symbolP = fragP->fr_symbol;
1189		offset = fragP->fr_offset;
1190		switch(fragP->fr_type){
1191		case rs_fill:	/* .fill never relaxes. */
1192		    growth = 0;
1193		    break;
1194
1195		case rs_align:
1196		    oldoff = relax_align(was_address + fragP->fr_fix, offset);
1197		    newoff = relax_align(address + fragP->fr_fix, offset);
1198		    /*
1199		     * Check if a maximum number of bytes to fill was specified
1200		     * for this align (stored in fr_subtype).
1201		     */
1202		    if(fragP->fr_subtype != 0){
1203			if(oldoff > fragP->fr_subtype)
1204			    oldoff = 0;
1205			if(newoff > fragP->fr_subtype)
1206			    newoff = 0;
1207		    }
1208		    growth = newoff - oldoff;
1209		    break;
1210
1211		case rs_org:
1212		    target = offset;
1213		    if(symbolP != NULL){
1214			know(((symbolP->sy_type & N_TYPE) == N_ABS) ||
1215			     ((symbolP->sy_type & N_TYPE) == N_SECT));
1216			know(symbolP->sy_frag);
1217			know((symbolP->sy_type & N_TYPE) != N_ABS ||
1218			     symbolP->sy_frag == &zero_address_frag );
1219			target += symbolP->sy_value +
1220				  symbolP->sy_frag->fr_address;
1221		    }
1222		    know(fragP->fr_next);
1223		    after = fragP->fr_next->fr_address;
1224		    /*
1225		     * Growth may be negative, but variable part of frag cannot
1226		     * have < 0 chars. That is, we can't .org backwards.
1227		     */
1228		    growth = ((target - after ) > 0) ? (target - after) : 0;
1229
1230		    growth -= stretch;	/* This is an absolute growth factor */
1231		    break;
1232
1233		case rs_machine_dependent:
1234#ifdef ARM
1235		    growth = arm_relax_frag(nsect, fragP, stretch);
1236#else /* !defined(ARM) */
1237		    this_state = fragP->fr_subtype;
1238		    this_type = md_relax_table + this_state;
1239		    start_type = this_type;
1240
1241		    target = offset;
1242		    if(symbolP){
1243			know(((symbolP->sy_type & N_TYPE) == N_ABS) ||
1244			     ((symbolP->sy_type & N_TYPE) == N_SECT));
1245			know(symbolP->sy_frag);
1246			know((symbolP->sy_type & N_TYPE) != N_ABS ||
1247			     symbolP->sy_frag == &zero_address_frag);
1248
1249			target += symbolP->sy_value +
1250				  symbolP->sy_frag->fr_address;
1251			/*
1252			 * If frag has yet to be reached on this pass,
1253			 * assume it will move by STRETCH just as we did.
1254			 * If this is not so, it will be because some frag
1255			 * between grows, and that will force another pass.
1256			 */
1257			if(symbolP->sy_frag->fr_address >= was_address &&
1258			   is_down_range(fragP, symbolP->sy_frag))
1259			    target += stretch;
1260		    }
1261		    aim = target - address - fragP->fr_fix;
1262		    if(aim < 0){
1263			/* Look backwards. */
1264			for(next_state = this_type->rlx_more; next_state; ){
1265			    if(aim >= this_type->rlx_backward)
1266				next_state = 0;
1267			    else{	/* Grow to next state. */
1268				this_state = next_state;
1269				this_type = md_relax_table + this_state;
1270				next_state = this_type->rlx_more;
1271			    }
1272			}
1273		    }
1274		    else{
1275			/* Look forwards. */
1276			for(next_state = this_type->rlx_more; next_state; ){
1277			    if(aim <= this_type->rlx_forward)
1278				next_state = 0;
1279			    else{	/* Grow to next state. */
1280				this_state = next_state;
1281				this_type = md_relax_table + this_state;
1282				next_state = this_type->rlx_more;
1283			    }
1284			}
1285		    }
1286		    if((growth = this_type->rlx_length -start_type->rlx_length))
1287			  fragP->fr_subtype = this_state;
1288#endif /* !defined(ARM) */
1289		    break;
1290		  case rs_dwarf2dbg:
1291		      growth = dwarf2dbg_relax_frag(fragP);
1292		      break;
1293
1294		  case rs_leb128:
1295		    {
1296		      valueT value;
1297		      offsetT size;
1298#ifdef OLD
1299		      value = resolve_symbol_value (fragP->fr_symbol);
1300#else
1301  		      expressionS *expression;
1302
1303		      if(fragP->fr_symbol->expression != NULL){
1304			expression =
1305			  (expressionS *)fragP->fr_symbol->expression;
1306			value = 0;
1307			if(expression->X_add_symbol != NULL)
1308			    value +=
1309			     (expression->X_add_symbol->sy_nlist.n_value +
1310			      expression->X_add_symbol->sy_frag->fr_address);
1311			if(expression->X_subtract_symbol != NULL)
1312			   value -=
1313			     (expression->X_subtract_symbol->sy_nlist.n_value +
1314			      expression->X_subtract_symbol->
1315							   sy_frag->fr_address);
1316			value += expression->X_add_number;
1317		      }
1318		      else{
1319			value = fragP->fr_symbol->sy_nlist.n_value +
1320				fragP->fr_address;
1321		      }
1322#endif
1323		      size = sizeof_leb128 (value, fragP->fr_subtype);
1324		      growth = size - fragP->fr_offset;
1325		      fragP->fr_offset = size;
1326		    }
1327		    break;
1328
1329		  default:
1330		      BAD_CASE(fragP->fr_type);
1331		      break;
1332		}
1333		if(growth) {
1334		    stretch += growth;
1335		    stretched++;
1336		}
1337	    }			/* For each frag in the segment. */
1338	}while(stretched);	/* Until nothing further to relax. */
1339
1340	/*
1341	 * We now have valid fr_address'es for each frag.  All fr_address's
1342	 * are correct, relative to their own section.  We have made all the
1343	 * fixS for this section that will be made.
1344	 */
1345
1346	for(fragP = frag_root; fragP != NULL; fragP = fragP->fr_next){
1347	    if(fragP->last_fr_address != fragP->fr_address){
1348		fragP->last_fr_address = fragP->fr_address;
1349		ret = 1;
1350	    }
1351	}
1352	return(ret);
1353}
1354
1355/*
1356 * Relax_align. Advance location counter to next address that has 'alignment'
1357 * lowest order bits all 0s.
1358 */
1359static
1360relax_addressT		/* How many addresses does the .align take? */
1361relax_align(
1362relax_addressT address, /* Address now. */
1363uint32_t alignment)		/* Alignment (binary). */
1364{
1365    relax_addressT mask;
1366    relax_addressT new_address;
1367
1368	mask = ~ ( (~0) << alignment );
1369	new_address = (address + mask) & (~ mask);
1370	return(new_address - address);
1371}
1372
1373#ifndef ARM
1374/*
1375 * is_down_range() is used in relax_section() to determine it one fragment is
1376 * after another to know if it will also be moved if the first is moved.
1377 */
1378static
1379int
1380is_down_range(
1381struct frag *f1,
1382struct frag *f2)
1383{
1384	while(f1){
1385	    if(f1->fr_next == f2)
1386		return(1);
1387	    f1 = f1->fr_next;
1388	}
1389	return(0);
1390}
1391#endif /* !defined(ARM) */
1392