Make the declarations of fix_new consistent under __STDC__. It might
[platform/upstream/binutils.git] / gas / write.c
1 /* write.c - emit .o file
2
3    Copyright (C) 1986, 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
4    
5    This file is part of GAS, the GNU Assembler.
6    
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11    
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to
19    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 /* This thing should be set up to do byteordering correctly.  But... */
22
23 #include "as.h"
24 #include "subsegs.h"
25 #include "obstack.h"
26 #include "output-file.h"
27
28 /* The NOP_OPCODE is for the alignment fill value.
29  * fill it a nop instruction so that the disassembler does not choke
30  * on it
31  */
32 #ifndef NOP_OPCODE
33 #define NOP_OPCODE 0x00
34 #endif
35
36 #ifndef MANY_SEGMENTS
37 static struct frag *text_frag_root;
38 static struct frag *data_frag_root;
39
40 static struct frag *text_last_frag;     /* Last frag in segment. */
41 static struct frag *data_last_frag;     /* Last frag in segment. */
42 #endif
43
44 static object_headers headers;
45
46 long string_byte_count;
47
48 static char *the_object_file;
49
50 char *next_object_file_charP;   /* Tracks object file bytes. */
51
52 int magic_number_for_object_file = DEFAULT_MAGIC_NUMBER_FOR_OBJECT_FILE;
53
54 /* static long          length; JF unused */    /* String length, including trailing '\0'. */
55
56
57 #if __STDC__ == 1
58
59 static int is_dnrange(struct frag *f1, struct frag *f2);
60 static long fixup_segment(fixS *fixP, segT this_segment_type);
61 static relax_addressT relax_align(relax_addressT address, long alignment);
62 void relax_segment(struct frag *segment_frag_root, segT segment_type);
63
64 #else
65
66 static int is_dnrange();
67 static long fixup_segment();
68 static relax_addressT relax_align();
69 void relax_segment();
70
71 #endif /* not __STDC__ */
72
73 /*
74  *                      fix_new()
75  *
76  * Create a fixS in obstack 'notes'.
77  */
78 fixS *fix_new(frag, where, size, add_symbol, sub_symbol, offset, pcrel
79 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
80               , r_type
81 #endif
82               )
83 fragS *frag;            /* Which frag? */
84 int where;              /* Where in that frag? */
85 short int size;         /* 1, 2, or 4 usually. */
86 symbolS *add_symbol;    /* X_add_symbol. */
87 symbolS *sub_symbol;    /* X_subtract_symbol. */
88 long offset;            /* X_add_number. */
89 int pcrel;              /* TRUE if PC-relative relocation. */
90 #if defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
91 int     r_type; /* Relocation type */
92 #endif
93 {
94         fixS *fixP;
95         
96         fixP = (fixS *) obstack_alloc(&notes, sizeof(fixS));
97         
98         fixP->fx_frag   = frag;
99         fixP->fx_where  = where;
100         fixP->fx_size   = size;
101         fixP->fx_addsy  = add_symbol;
102         fixP->fx_subsy  = sub_symbol;
103         fixP->fx_offset = offset;
104         fixP->fx_pcrel  = pcrel;
105 #if defined(TC_SPARC) || defined(TC_A29K) || defined( NEED_FX_R_TYPE)
106         fixP->fx_r_type = r_type;
107 #endif
108         /* JF these 'cuz of the NS32K stuff */
109         fixP->fx_im_disp = 0;
110         fixP->fx_pcrel_adjust = 0;
111         fixP->fx_bsr = 0;
112         fixP->fx_bit_fixP = 0;
113         
114         /* usually, we want relocs sorted numerically, but while
115            comparing to older versions of gas that have relocs
116            reverse sorted, it is convenient to have this compile
117            time option.  xoxorich. */
118         
119 #ifdef REVERSE_SORT_RELOCS
120         
121         fixP->fx_next = *seg_fix_rootP;
122         *seg_fix_rootP = fixP;
123         
124 #else /* REVERSE_SORT_RELOCS */
125         
126         fixP->fx_next   = NULL;
127         
128         if (*seg_fix_tailP)
129             (*seg_fix_tailP)->fx_next = fixP;
130         else
131             *seg_fix_rootP = fixP;
132         *seg_fix_tailP = fixP;
133         
134 #endif /* REVERSE_SORT_RELOCS */
135         
136         fixP->fx_callj = 0;
137         return(fixP);
138 } /* fix_new() */
139
140 #ifndef BFD
141 void write_object_file() 
142 {
143         register struct frchain *       frchainP; /* Track along all frchains. */
144         register fragS *                fragP;  /* Track along all frags. */
145         register struct frchain *       next_frchainP;
146         register fragS * *              prev_fragPP;
147         /*  register char *             name; */
148         /*  symbolS *symbolP; */
149         /*  register symbolS **         symbolPP; */
150         /* register fixS *              fixP; JF unused */
151         unsigned int data_siz;
152         
153         long object_file_size;
154         
155 #ifdef  VMS
156         /*
157          *      Under VMS we try to be compatible with VAX-11 "C".  Thus, we
158          *      call a routine to check for the definition of the procedure
159          *      "_main", and if so -- fix it up so that it can be program
160          *      entry point.
161          */
162         VMS_Check_For_Main();
163 #endif /* VMS */
164         /*
165          * After every sub-segment, we fake an ".align ...". This conforms to BSD4.2
166          * brane-damage. We then fake ".fill 0" because that is the kind of frag
167          * that requires least thought. ".align" frags like to have a following
168          * frag since that makes calculating their intended length trivial.
169          */
170 #define SUB_SEGMENT_ALIGN (2)
171         for (frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next) {
172 #ifdef  VMS
173                 /*
174                  *      Under VAX/VMS, the linker (and PSECT specifications)
175                  *      take care of correctly aligning the segments.
176                  *      Doing the alignment here (on initialized data) can
177                  *      mess up the calculation of global data PSECT sizes.
178                  */
179 #undef  SUB_SEGMENT_ALIGN
180 #define SUB_SEGMENT_ALIGN ((frchainP->frch_seg != SEG_DATA) ? 2 : 0)
181 #endif  /* VMS */
182                 subseg_new (frchainP->frch_seg, frchainP->frch_subseg);
183                 frag_align (SUB_SEGMENT_ALIGN, NOP_OPCODE);
184                 /* frag_align will have left a new frag. */
185                 /* Use this last frag for an empty ".fill". */
186                 /*
187                  * For this segment ...
188                  * Create a last frag. Do not leave a "being filled in frag".
189                  */
190                 frag_wane (frag_now);
191                 frag_now->fr_fix        = 0;
192                 know( frag_now->fr_next == NULL );
193                 /* know( frags . obstack_c_base == frags . obstack_c_next_free ); */
194                 /* Above shows we haven't left a half-completed object on obstack. */
195         } /* walk the frag chain */
196         
197         /*
198          * From now on, we don't care about sub-segments.
199          * Build one frag chain for each segment. Linked thru fr_next.
200          * We know that there is at least 1 text frchain & at least 1 data frchain.
201          */
202         prev_fragPP = &text_frag_root;
203         for (frchainP = frchain_root; frchainP; frchainP = next_frchainP) {
204                 know( frchainP->frch_root );
205                 * prev_fragPP = frchainP->frch_root;
206                 prev_fragPP = & frchainP->frch_last->fr_next;
207                 
208                 if (((next_frchainP = frchainP->frch_next) == NULL)
209                     || next_frchainP == data0_frchainP) {
210                         prev_fragPP = & data_frag_root;
211                         if (next_frchainP) {
212                                 text_last_frag = frchainP->frch_last;
213                         } else {
214                                 data_last_frag = frchainP->frch_last;
215                         }
216                 }
217         } /* walk the frag chain */
218         
219         /*
220          * We have two segments. If user gave -R flag, then we must put the
221          * data frags into the text segment. Do this before relaxing so
222          * we know to take advantage of -R and make shorter addresses.
223          */
224         if (flagseen[ 'R' ]) {
225                 fixS *tmp;
226                 
227                 text_last_frag->fr_next = data_frag_root;
228                 text_last_frag = data_last_frag;
229                 data_last_frag = NULL;
230                 data_frag_root = NULL;
231                 if (text_fix_root) {
232                         for (tmp = text_fix_root; tmp->fx_next; tmp = tmp->fx_next) ;;
233                         tmp->fx_next=data_fix_root;
234                 } else
235                     text_fix_root=data_fix_root;
236                 data_fix_root=NULL;
237         }
238         
239         relax_segment(text_frag_root, SEG_TEXT);
240         relax_segment(data_frag_root, SEG_DATA);
241         /*
242          * Now the addresses of frags are correct within the segment.
243          */
244         
245         know(text_last_frag->fr_type == rs_fill && text_last_frag->fr_offset == 0);
246         H_SET_TEXT_SIZE(&headers, text_last_frag->fr_address);
247         text_last_frag->fr_address = H_GET_TEXT_SIZE(&headers);
248         
249         /*
250          * Join the 2 segments into 1 huge segment.
251          * To do this, re-compute every rn_address in the SEG_DATA frags.
252          * Then join the data frags after the text frags.
253          *
254          * Determine a_data [length of data segment].
255          */
256         if (data_frag_root) {
257                 register relax_addressT slide;
258                 
259                 know((text_last_frag->fr_type == rs_fill) && (text_last_frag->fr_offset == 0));
260                 
261                 H_SET_DATA_SIZE(&headers, data_last_frag->fr_address);
262                 data_last_frag->fr_address = H_GET_DATA_SIZE(&headers);
263                 slide = H_GET_TEXT_SIZE(&headers); /* & in file of the data segment. */
264                 
265                 for (fragP = data_frag_root; fragP; fragP = fragP->fr_next) {
266                         fragP->fr_address += slide;
267                 } /* for each data frag */
268                 
269                 know(text_last_frag != 0);
270                 text_last_frag->fr_next = data_frag_root;
271         } else {
272                 H_SET_DATA_SIZE(&headers,0);
273                 data_siz = 0;
274         }
275         
276         bss_address_frag.fr_address = (H_GET_TEXT_SIZE(&headers) + 
277                                        H_GET_DATA_SIZE(&headers));
278         
279         H_SET_BSS_SIZE(&headers,local_bss_counter);
280         
281         /*
282          *
283          * Crawl the symbol chain.
284          *
285          * For each symbol whose value depends on a frag, take the address of
286          * that frag and subsume it into the value of the symbol.
287          * After this, there is just one way to lookup a symbol value.
288          * Values are left in their final state for object file emission.
289          * We adjust the values of 'L' local symbols, even if we do
290          * not intend to emit them to the object file, because their values
291          * are needed for fix-ups.
292          *
293          * Unless we saw a -L flag, remove all symbols that begin with 'L'
294          * from the symbol chain.  (They are still pointed to by the fixes.)
295          *
296          * Count the remaining symbols.
297          * Assign a symbol number to each symbol.
298          * Count the number of string-table chars we will emit.
299          * Put this info into the headers as appropriate.
300          *
301          */
302         know(zero_address_frag.fr_address == 0);
303         string_byte_count = sizeof(string_byte_count);
304         
305         obj_crawl_symbol_chain(&headers);
306         
307         if (string_byte_count == sizeof(string_byte_count)) {
308                 string_byte_count = 0;
309         } /* if no strings, then no count. */
310         
311         H_SET_STRING_SIZE(&headers, string_byte_count);
312         
313         /*
314          * Addresses of frags now reflect addresses we use in the object file.
315          * Symbol values are correct.
316          * Scan the frags, converting any ".org"s and ".align"s to ".fill"s.
317          * Also converting any machine-dependent frags using md_convert_frag();
318          */
319         subseg_change(SEG_TEXT, 0);
320         
321         for (fragP = text_frag_root;  fragP;  fragP = fragP->fr_next) {
322                 switch (fragP->fr_type) {
323                 case rs_align:
324                 case rs_org:
325                         fragP->fr_type = rs_fill;
326                         know(fragP->fr_var == 1);
327                         know(fragP->fr_next != NULL);
328                         
329                         fragP->fr_offset = (fragP->fr_next->fr_address
330                                             - fragP->fr_address
331                                             - fragP->fr_fix);
332                         break;
333                         
334                 case rs_fill:
335                         break;
336                         
337                 case rs_machine_dependent:
338                         md_convert_frag(&headers, fragP);
339                         
340                         know((fragP->fr_next == NULL) || ((fragP->fr_next->fr_address - fragP->fr_address) == fragP->fr_fix));
341                         
342                         /*
343                          * After md_convert_frag, we make the frag into a ".space 0".
344                          * Md_convert_frag() should set up any fixSs and constants
345                          * required.
346                          */
347                         frag_wane(fragP);
348                         break;
349                         
350 #ifndef WORKING_DOT_WORD
351                 case rs_broken_word: {
352                         struct broken_word *lie;
353                         extern md_short_jump_size;
354                         extern md_long_jump_size;
355                         
356                         if (fragP->fr_subtype) {
357                                 fragP->fr_fix+=md_short_jump_size;
358                                 for (lie=(struct broken_word *)(fragP->fr_symbol);lie && lie->dispfrag==fragP;lie=lie->next_broken_word)
359                                     if (lie->added==1)
360                                         fragP->fr_fix+=md_long_jump_size;
361                         }
362                         frag_wane(fragP);
363                 }
364                         break;
365 #endif
366                         
367                 default:
368                         BAD_CASE( fragP->fr_type );
369                         break;
370                 } /* switch (fr_type) */
371                 
372                 know((fragP->fr_next == NULL) || ((fragP->fr_next->fr_address - fragP->fr_address) == (fragP->fr_fix + (fragP->fr_offset * fragP->fr_var))));
373         } /* for each frag. */
374         
375 #ifndef WORKING_DOT_WORD
376         {
377                 struct broken_word *lie;
378                 struct broken_word **prevP;
379                 
380                 prevP= &broken_words;
381                 for (lie=broken_words; lie; lie=lie->next_broken_word)
382                     if (!lie->added) {
383 #ifdef TC_NS32K
384                             fix_new_ns32k(lie->frag,
385                                           lie->word_goes_here - lie->frag->fr_literal,
386                                           2,
387                                           lie->add,
388                                           lie->sub,
389                                           lie->addnum,
390                                           0, 0, 2, 0, 0);
391 #elif defined(TC_SPARC) || defined(TC_A29K) || defined(NEED_FX_R_TYPE)
392                             fix_new(    lie->frag,  lie->word_goes_here - lie->frag->fr_literal,
393                                     2,  lie->add,
394                                     lie->sub,  lie->addnum,
395                                     0,  NO_RELOC);
396 #else
397                             fix_new(    lie->frag,  lie->word_goes_here - lie->frag->fr_literal,
398                                     2,  lie->add,
399                                     lie->sub,  lie->addnum,
400                                     0,  0);
401
402 #endif /* TC_NS32K */
403                             /* md_number_to_chars(lie->word_goes_here,
404                                S_GET_VALUE(lie->add)
405                                + lie->addnum
406                                - S_GET_VALUE(lie->sub),
407                                2); */
408                             *prevP=lie->next_broken_word;
409                     } else
410                         prevP= &(lie->next_broken_word);
411                 
412                 for (lie=broken_words;lie;) {
413                         struct broken_word *untruth;
414                         char    *table_ptr;
415                         long    table_addr;
416                         long    from_addr,
417                         to_addr;
418                         int     n,
419                         m;
420                         
421                         extern md_short_jump_size;
422                         extern md_long_jump_size;
423                         
424                         fragP=lie->dispfrag;
425                         
426                         /* Find out how many broken_words go here */
427                         n=0;
428                         for (untruth=lie;untruth && untruth->dispfrag==fragP;untruth=untruth->next_broken_word)
429                             if (untruth->added==1)
430                                 n++;
431                         
432                         table_ptr=lie->dispfrag->fr_opcode;
433                         table_addr=lie->dispfrag->fr_address+(table_ptr - lie->dispfrag->fr_literal);
434                         /* Create the jump around the long jumps */
435                         /* This is a short jump from table_ptr+0 to table_ptr+n*long_jump_size */
436                         from_addr=table_addr;
437                         to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
438                         md_create_short_jump(table_ptr, from_addr, to_addr, lie->dispfrag, lie->add);
439                         table_ptr+=md_short_jump_size;
440                         table_addr+=md_short_jump_size;
441                         
442                         for (m=0;lie && lie->dispfrag==fragP;m++,lie=lie->next_broken_word) {
443                                 if (lie->added==2)
444                                     continue;
445                                 /* Patch the jump table */
446                                 /* This is the offset from ??? to table_ptr+0 */
447                                 to_addr =   table_addr
448                                     - S_GET_VALUE(lie->sub);
449                                 md_number_to_chars(lie->word_goes_here,to_addr,2);
450                                 for (untruth=lie->next_broken_word;untruth && untruth->dispfrag==fragP;untruth=untruth->next_broken_word) {
451                                         if (untruth->use_jump==lie)
452                                             md_number_to_chars(untruth->word_goes_here,to_addr,2);
453                                 }
454                                 
455                                 /* Install the long jump */
456                                 /* this is a long jump from table_ptr+0 to the final target */
457                                 from_addr=table_addr;
458                                 to_addr=S_GET_VALUE(lie->add) + lie->addnum;
459                                 md_create_long_jump(table_ptr,from_addr,to_addr,lie->dispfrag,lie->add);
460                                 table_ptr+=md_long_jump_size;
461                                 table_addr+=md_long_jump_size;
462                         }
463                 }
464         }
465 #endif /* not WORKING_DOT_WORD */
466         
467 #ifndef VMS
468         { /* not vms */
469                 /*
470                  * Scan every FixS performing fixups. We had to wait until now to do
471                  * this because md_convert_frag() may have made some fixSs.
472                  */
473                 
474                 H_SET_RELOCATION_SIZE(&headers,
475                                       md_reloc_size * fixup_segment(text_fix_root, SEG_TEXT),
476                                       md_reloc_size * fixup_segment(data_fix_root, SEG_DATA));
477                 
478                 
479                 /* FIXME move this stuff into the pre-write-hook */
480                 H_SET_MAGIC_NUMBER(&headers, magic_number_for_object_file);
481                 H_SET_ENTRY_POINT(&headers, 0);
482                 
483                 obj_pre_write_hook(&headers); /* extra coff stuff */
484                 if ((had_warnings() && flagseen['Z'])
485                     || had_errors() > 0) {
486                         if (flagseen['Z']) {
487                                 as_warn("%d error%s, %d warning%s, generating bad object file.\n",
488                                         had_errors(), had_errors() == 1 ? "" : "s",
489                                         had_warnings(), had_warnings() == 1 ? "" : "s");
490                         } else {
491                                 as_fatal("%d error%s, %d warning%s, no object file generated.\n",
492                                          had_errors(), had_errors() == 1 ? "" : "s",
493                                          had_warnings(), had_warnings() == 1 ? "" : "s");
494                         } /* on want output */
495                 } /* on error condition */
496                 
497                 object_file_size = H_GET_FILE_SIZE(&headers);
498                 next_object_file_charP = the_object_file = xmalloc(object_file_size);
499                 
500                 output_file_create(out_file_name);
501                 
502                 obj_header_append(&next_object_file_charP, &headers);
503                 
504                 know((next_object_file_charP - the_object_file) == H_GET_HEADER_SIZE(&headers));
505                 
506                 /*
507                  * Emit code.
508                  */
509                 for (fragP = text_frag_root;  fragP;  fragP = fragP->fr_next) {
510                         register long count;
511                         register char *fill_literal;
512                         register long fill_size;
513                         
514                         know(fragP->fr_type == rs_fill);
515                         append(&next_object_file_charP, fragP->fr_literal, (unsigned long) fragP->fr_fix);
516                         fill_literal = fragP->fr_literal + fragP->fr_fix;
517                         fill_size = fragP->fr_var;
518                         know(fragP->fr_offset >= 0);
519                         
520                         for (count = fragP->fr_offset; count; count--) {
521                                 append(&next_object_file_charP, fill_literal, (unsigned long) fill_size);
522                         } /* for each  */
523                         
524                 } /* for each code frag. */
525                 
526                 know((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE(&headers) + H_GET_TEXT_SIZE(&headers) + H_GET_DATA_SIZE(&headers)));
527                 
528                 /*
529                  * Emit relocations.
530                  */
531                 obj_emit_relocations(&next_object_file_charP, text_fix_root, (relax_addressT)0);
532                 know((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE(&headers) + H_GET_TEXT_SIZE(&headers) + H_GET_DATA_SIZE(&headers) + H_GET_TEXT_RELOCATION_SIZE(&headers)));
533 #ifdef TC_I960
534                 /* Make addresses in data relocation directives relative to beginning of
535                  * first data fragment, not end of last text fragment:  alignment of the
536                  * start of the data segment may place a gap between the segments.
537                  */
538                 obj_emit_relocations(&next_object_file_charP, data_fix_root, data0_frchainP->frch_root->fr_address);
539 #else /* TC_I960 */
540                 obj_emit_relocations(&next_object_file_charP, data_fix_root, text_last_frag->fr_address);
541 #endif /* TC_I960 */
542                 
543                 know((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE(&headers) + H_GET_TEXT_SIZE(&headers) + H_GET_DATA_SIZE(&headers) + H_GET_TEXT_RELOCATION_SIZE(&headers) + H_GET_DATA_RELOCATION_SIZE(&headers)));
544                 
545                 /*
546                  * Emit line number entries.
547                  */
548                 OBJ_EMIT_LINENO(&next_object_file_charP, lineno_rootP, the_object_file);
549                 know((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE(&headers) + H_GET_TEXT_SIZE(&headers) + H_GET_DATA_SIZE(&headers) + H_GET_TEXT_RELOCATION_SIZE(&headers) + H_GET_DATA_RELOCATION_SIZE(&headers) + H_GET_LINENO_SIZE(&headers)));
550                 
551                 /*
552                  * Emit symbols.
553                  */
554                 obj_emit_symbols(&next_object_file_charP, symbol_rootP);
555                 know((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE(&headers) + H_GET_TEXT_SIZE(&headers) + H_GET_DATA_SIZE(&headers) + H_GET_TEXT_RELOCATION_SIZE(&headers) + H_GET_DATA_RELOCATION_SIZE(&headers) + H_GET_LINENO_SIZE(&headers) + H_GET_SYMBOL_TABLE_SIZE(&headers)));
556                 
557                 /*
558                  * Emit strings.
559                  */
560                 
561                 if (string_byte_count > 0) {
562                         obj_emit_strings(&next_object_file_charP);
563                 } /* only if we have a string table */
564                 
565                 /*        know((next_object_file_charP - the_object_file) == (H_GET_HEADER_SIZE(&headers) + H_GET_TEXT_SIZE(&headers) + H_GET_DATA_SIZE(&headers) + H_GET_TEXT_RELOCATION_SIZE(&headers) + H_GET_DATA_RELOCATION_SIZE(&headers) + H_GET_LINENO_SIZE(&headers) + H_GET_SYMBOL_TABLE_SIZE(&headers) + H_GET_STRING_SIZE(&headers)));
566                  */
567                 /*        know(next_object_file_charP == the_object_file + object_file_size);*/
568                 
569 #ifdef BFD_HEADERS
570                 bfd_seek(stdoutput, 0, 0);
571                 bfd_write(the_object_file, 1, object_file_size,  stdoutput);
572 #else
573                 
574                 /* Write the data to the file */
575                 output_file_append(the_object_file,object_file_size,out_file_name);
576 #endif
577                 
578                 output_file_close(out_file_name);
579         } /* non vms output */
580 #else   /* VMS */
581         /*
582          *      Now do the VMS-dependent part of writing the object file
583          */
584         VMS_write_object_file(text_siz, data_siz, text_frag_root, data_frag_root);
585 #endif  /* VMS */
586 } /* write_object_file() */
587 #else
588 #endif
589
590 /*
591  *                      relax_segment()
592  *
593  * Now we have a segment, not a crowd of sub-segments, we can make fr_address
594  * values.
595  *
596  * Relax the frags.
597  *
598  * After this, all frags in this segment have addresses that are correct
599  * within the segment. Since segments live in different file addresses,
600  * these frag addresses may not be the same as final object-file addresses.
601  */
602
603
604
605 void relax_segment(segment_frag_root, segment)
606 struct frag *   segment_frag_root;
607 segT            segment; /* SEG_DATA or SEG_TEXT */
608 {
609         register struct frag *  fragP;
610         register relax_addressT address;
611         /* register relax_addressT      old_address; JF unused */
612         /* register relax_addressT      new_address; JF unused */
613 #ifndef MANY_SEGMENTS   
614         know(segment == SEG_DATA || segment == SEG_TEXT);
615 #endif
616         /* In case md_estimate_size_before_relax() wants to make fixSs. */
617         subseg_change(segment, 0);
618         
619         /*
620          * For each frag in segment: count and store  (a 1st guess of) fr_address.
621          */
622         address = 0;
623         for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next) {
624                 fragP->fr_address = address;
625                 address += fragP->fr_fix;
626                 
627                 switch (fragP->fr_type) {
628                 case rs_fill:
629                         address += fragP->fr_offset * fragP->fr_var ;
630                         break;
631                         
632                 case rs_align:
633                         address += relax_align(address, fragP->fr_offset);
634                         break;
635                         
636                 case rs_org:
637                         /*
638                          * Assume .org is nugatory. It will grow with 1st relax.
639                          */
640                         break;
641                         
642                 case rs_machine_dependent:
643                         address += md_estimate_size_before_relax(fragP, segment);
644                         break;
645                         
646 #ifndef WORKING_DOT_WORD
647                         /* Broken words don't concern us yet */
648                 case rs_broken_word:
649                         break;
650 #endif
651                         
652                 default:
653                         BAD_CASE(fragP->fr_type);
654                         break;
655                 } /* switch(fr_type) */
656         } /* for each frag in the segment */
657         
658         /*
659          * Do relax().
660          */
661         {
662                 register long   stretch; /* May be any size, 0 or negative. */
663                 /* Cumulative number of addresses we have */
664                 /* relaxed this pass. */
665                 /* We may have relaxed more than one address. */
666                 register long stretched;  /* Have we stretched on this pass? */
667                 /* This is 'cuz stretch may be zero, when,
668                    in fact some piece of code grew, and
669                    another shrank.  If a branch instruction
670                    doesn't fit anymore, we could be scrod */
671                 
672                 do {
673                         stretch = stretched = 0;
674                         for (fragP = segment_frag_root;  fragP;  fragP = fragP->fr_next) {
675                                 register long growth = 0;
676                                 register unsigned long was_address;
677                                 /* register long var; */
678                                 register long offset;
679                                 register symbolS *symbolP;
680                                 register long target;
681                                 register long after;
682                                 register long aim;
683                                 
684                                 was_address = fragP->fr_address;
685                                 address = fragP->fr_address += stretch;
686                                 symbolP = fragP->fr_symbol;
687                                 offset = fragP->fr_offset;
688                                 /* var = fragP->fr_var; */
689                                 
690                                 switch (fragP->fr_type) {
691                                 case rs_fill:   /* .fill never relaxes. */
692                                         growth = 0;
693                                         break;
694                                         
695 #ifndef WORKING_DOT_WORD
696                                         /* JF:  This is RMS's idea.  I do *NOT* want to be blamed
697                                            for it I do not want to write it.  I do not want to have
698                                            anything to do with it.  This is not the proper way to
699                                            implement this misfeature. */
700                                 case rs_broken_word: {
701                                         struct broken_word *lie;
702                                         struct broken_word *untruth;
703                                         extern int md_short_jump_size;
704                                         extern int md_long_jump_size;
705                                         
706                                         /* Yes this is ugly (storing the broken_word pointer
707                                            in the symbol slot).  Still, this whole chunk of
708                                            code is ugly, and I don't feel like doing anything
709                                            about it.  Think of it as stubbornness in action */
710                                         growth=0;
711                                         for (lie=(struct broken_word *)(fragP->fr_symbol);
712                                              lie && lie->dispfrag==fragP;
713                                              lie=lie->next_broken_word) {
714                                                 
715                                                 if (lie->added)
716                                                     continue;
717                                                 
718                                                 offset=  lie->add->sy_frag->fr_address+ S_GET_VALUE(lie->add) + lie->addnum -
719                                                     (lie->sub->sy_frag->fr_address+ S_GET_VALUE(lie->sub));
720                                                 if (offset<=-32768 || offset>=32767) {
721                                                         if (flagseen['k'])
722                                                             as_warn(".word %s-%s+%ld didn't fit",
723                                                                     S_GET_NAME(lie->add),
724                                                                     S_GET_NAME(lie->sub),
725                                                                     lie->addnum);
726                                                         lie->added=1;
727                                                         if (fragP->fr_subtype==0) {
728                                                                 fragP->fr_subtype++;
729                                                                 growth+=md_short_jump_size;
730                                                         }
731                                                         for (untruth=lie->next_broken_word;untruth && untruth->dispfrag==lie->dispfrag;untruth=untruth->next_broken_word)
732                                                             if ((untruth->add->sy_frag == lie->add->sy_frag)
733                                                                 && S_GET_VALUE(untruth->add) == S_GET_VALUE(lie->add)) {
734                                                                     untruth->added=2;
735                                                                     untruth->use_jump=lie;
736                                                             }
737                                                         growth+=md_long_jump_size;
738                                                 }
739                                         }
740                                         
741                                         break;
742                                 } /* case rs_broken_word */
743 #endif
744                                 case rs_align:
745                                         growth = relax_align((relax_addressT) (address + fragP->fr_fix), offset)
746                                             - relax_align((relax_addressT) (was_address + fragP->fr_fix), offset);
747                                         break;
748                                         
749                                 case rs_org:
750                                         target = offset;
751                                         
752                                         if (symbolP) {
753 #ifdef MANY_SEGMENTS
754 #else
755                                                 know((S_GET_SEGMENT(symbolP) == SEG_ABSOLUTE) || (S_GET_SEGMENT(symbolP) == SEG_DATA) || (S_GET_SEGMENT(symbolP) == SEG_TEXT));
756                                                 know(symbolP->sy_frag);
757                                                 know(!(S_GET_SEGMENT(symbolP) == SEG_ABSOLUTE) || (symbolP->sy_frag == &zero_address_frag));
758 #endif
759                                                 target += S_GET_VALUE(symbolP)
760                                                     + symbolP->sy_frag->fr_address;
761                                         } /* if we have a symbol */
762                                         
763                                         know(fragP->fr_next);
764                                         after = fragP->fr_next->fr_address;
765                                         growth = ((target - after ) > 0) ? (target - after) : 0;
766                                         /* Growth may be -ve, but variable part */
767                                         /* of frag cannot have < 0 chars. */
768                                         /* That is, we can't .org backwards. */
769                                         
770                                         growth -= stretch;      /* This is an absolute growth factor */
771                                         break;
772                                         
773                                 case rs_machine_dependent: {
774                                         register const relax_typeS *    this_type;
775                                         register const relax_typeS *    start_type;
776                                         register relax_substateT        next_state;
777                                         register relax_substateT        this_state;
778                                         
779                                         start_type = this_type = md_relax_table + (this_state = fragP->fr_subtype);
780                                         target = offset;
781                                         
782                                         if (symbolP) {
783 #ifndef MANY_SEGMENTS
784                                                 know((S_GET_SEGMENT(symbolP) == SEG_ABSOLUTE) || (S_GET_SEGMENT(symbolP) == SEG_DATA) || (S_GET_SEGMENT(symbolP) == SEG_TEXT));
785 #endif
786                                                 know(symbolP->sy_frag);
787                                                 know(!(S_GET_SEGMENT(symbolP) == SEG_ABSOLUTE) || symbolP->sy_frag==&zero_address_frag );
788                                                 target +=
789                                                     S_GET_VALUE(symbolP)
790                                                         + symbolP->sy_frag->fr_address;
791                                                 
792                                                 /* If frag has yet to be reached on this pass,
793                                                    assume it will move by STRETCH just as we did.
794                                                    If this is not so, it will be because some frag
795                                                    between grows, and that will force another pass.  */
796                                                 
797                                                 /* JF was just address */
798                                                 /* JF also added is_dnrange hack */
799                                                 /* There's gotta be a better/faster/etc way
800                                                    to do this. . . */
801                                                 /* gnu@cygnus.com:  I changed this from > to >=
802                                                    because I ran into a zero-length frag (fr_fix=0)
803                                                    which was created when the obstack needed a new
804                                                    chunk JUST AFTER the opcode of a branch.  Since
805                                                    fr_fix is zero, fr_address of this frag is the same
806                                                    as fr_address of the next frag.  This
807                                                    zero-length frag was variable and jumped to .+2
808                                                    (in the next frag), but since the > comparison
809                                                    below failed (the two were =, not >), "stretch"
810                                                    was not added to the target.  Stretch was 178, so
811                                                    the offset appeared to be .-176 instead, which did
812                                                    not fit into a byte branch, so the assembler
813                                                    relaxed the branch to a word.  This didn't compare
814                                                    with what happened when the same source file was
815                                                    assembled on other machines, which is how I found it.
816                                                    You might want to think about what other places have
817                                                    trouble with zero length frags... */
818                                                 
819                                                 if (symbolP->sy_frag->fr_address >= was_address
820                                                     && is_dnrange(fragP,symbolP->sy_frag)) {
821                                                         target += stretch;
822                                                 } /*  */
823                                                 
824                                         } /* if there's a symbol attached */
825                                         
826                                         aim = target - address - fragP->fr_fix;
827                                         /* The displacement is affected by the instruction size
828                                          * for the 32k architecture. I think we ought to be able
829                                          * to add fragP->fr_pcrel_adjust in all cases (it should be
830                                          * zero if not used), but just in case it breaks something
831                                          * else we'll put this inside #ifdef NS32K ... #endif
832                                          */
833 #ifdef TC_NS32K
834                                         aim += fragP->fr_pcrel_adjust;
835 #endif /* TC_NS32K */
836                                         
837                                         if (aim < 0) {
838                                                 /* Look backwards. */
839                                                 for (next_state = this_type->rlx_more; next_state; ) {
840                                                         if (aim >= this_type->rlx_backward) {
841                                                                 next_state = 0;
842                                                         } else { /* Grow to next state. */
843                                                                 this_type = md_relax_table + (this_state = next_state);
844                                                                 next_state = this_type->rlx_more;
845                                                         }
846                                                 }
847                                         } else {
848 #ifdef DONTDEF
849                                                 /* JF these next few lines of code are for the mc68020 which can't handle short
850                                                    offsets of zero in branch instructions.  What a kludge! */
851                                                 if (aim==0 && this_state==(1<<2+0)) { /* FOO hard encoded from m.c */
852                                                         aim=this_type->rlx_forward+1; /* Force relaxation into word mode */
853                                                 }
854 #endif
855 #ifdef M68K_AIM_KLUDGE
856                                                 M68K_AIM_KLUDGE(aim, this_state, this_type);
857 #endif
858                                                 /* JF end of 68020 code */
859                                                 /* Look forwards. */
860                                                 for (next_state = this_type->rlx_more; next_state; ) {
861                                                         if (aim <= this_type->rlx_forward) {
862                                                                 next_state = 0;
863                                                         } else { /* Grow to next state. */
864                                                                 this_type = md_relax_table + (this_state = next_state);
865                                                                 next_state = this_type->rlx_more;
866                                                         }
867                                                 }
868                                         }
869                                         
870                                         if ((growth = this_type->rlx_length - start_type->rlx_length) != 0)
871                                             fragP->fr_subtype = this_state;
872                                         
873                                         break;
874                                 } /* case rs_machine_dependent */
875                                         
876                                 default:
877                                         BAD_CASE( fragP->fr_type );
878                                         break;
879                                 }
880                                 if (growth) {
881                                         stretch += growth;
882                                         stretched++;
883                                 }
884                         } /* For each frag in the segment. */
885                 } while (stretched);    /* Until nothing further to relax. */
886         } /* do_relax */
887         
888         /*
889          * We now have valid fr_address'es for each frag.
890          */
891         
892         /*
893          * All fr_address's are correct, relative to their own segment.
894          * We have made all the fixS we will ever make.
895          */
896 } /* relax_segment() */
897
898 /*
899  * Relax_align. Advance location counter to next address that has 'alignment'
900  * lowest order bits all 0s.
901  */
902
903 /* How many addresses does the .align take? */
904 static relax_addressT relax_align(address, alignment)
905 register relax_addressT address; /* Address now. */
906 register long alignment; /* Alignment (binary). */
907 {
908         relax_addressT  mask;
909         relax_addressT  new_address;
910         
911         mask = ~ ( (~0) << alignment );
912         new_address = (address + mask) & (~ mask);
913         return (new_address - address);
914 } /* relax_align() */
915
916 /* fixup_segment()
917    
918    Go through all the fixS's in a segment and see which ones can be
919    handled now.  (These consist of fixS where we have since discovered
920    the value of a symbol, or the address of the frag involved.)
921    For each one, call md_apply_fix to put the fix into the frag data.
922    
923    Result is a count of how many relocation structs will be needed to
924    handle the remaining fixS's that we couldn't completely handle here.
925    These will be output later by emit_relocations().  */
926
927 static long fixup_segment(fixP, this_segment_type)
928 register fixS * fixP;
929 segT            this_segment_type; /* N_TYPE bits for segment. */
930 {
931         register long seg_reloc_count;
932         register symbolS *add_symbolP;
933         register symbolS *sub_symbolP;
934         register long add_number;
935         register int size;
936         register char *place;
937         register long where;
938         register char pcrel;
939         register fragS *fragP;
940         register segT add_symbol_segment = SEG_ABSOLUTE;
941         
942         /* FIXME: remove this line */ /*        fixS *orig = fixP; */
943         seg_reloc_count = 0;
944         
945         for ( ;  fixP;  fixP = fixP->fx_next) {
946                 fragP       = fixP->fx_frag;
947                 know(fragP);
948                 where     = fixP->fx_where;
949                 place       = fragP->fr_literal + where;
950                 size      = fixP->fx_size;
951                 add_symbolP = fixP->fx_addsy;
952 #ifdef TC_I960
953                 if (fixP->fx_callj && TC_S_IS_CALLNAME(add_symbolP)) {
954                         /* Relocation should be done via the
955                            associated 'bal' entry point
956                            symbol. */
957                         
958                         if (!TC_S_IS_BALNAME(tc_get_bal_of_call(add_symbolP))) {
959                                 as_bad("No 'bal' entry point for leafproc %s",
960                                        S_GET_NAME(add_symbolP));
961                                 continue;
962                         }
963                         fixP->fx_addsy = add_symbolP = tc_get_bal_of_call(add_symbolP);
964                 }       /* callj relocation */
965 #endif
966                 sub_symbolP = fixP->fx_subsy;
967                 add_number  = fixP->fx_offset;
968                 pcrel     = fixP->fx_pcrel;
969                 
970                 if (add_symbolP) {
971                         add_symbol_segment = S_GET_SEGMENT(add_symbolP);
972                 }       /* if there is an addend */
973                 
974                 if (sub_symbolP) {
975                         if (!add_symbolP) {
976                                 /* Its just -sym */
977                                 if (S_GET_SEGMENT(sub_symbolP) != SEG_ABSOLUTE) {
978                                         as_bad("Negative of non-absolute symbol %s", S_GET_NAME(sub_symbolP));
979                                 } /* not absolute */
980                                 
981                                 add_number -= S_GET_VALUE(sub_symbolP);
982                                 
983                                 /* if sub_symbol is in the same segment that add_symbol
984                                    and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
985                         } else if ((S_GET_SEGMENT(sub_symbolP) == add_symbol_segment)
986                                    && (SEG_NORMAL(add_symbol_segment)
987                                        || (add_symbol_segment == SEG_ABSOLUTE))) {
988                                 /* Difference of 2 symbols from same segment. */
989                                 /* Can't make difference of 2 undefineds: 'value' means */
990                                 /* something different for N_UNDF. */
991 #ifdef TC_I960
992                                 /* Makes no sense to use the difference of 2 arbitrary symbols
993                                  * as the target of a call instruction.
994                                  */
995                                 if (fixP->fx_callj) {
996                                         as_bad("callj to difference of 2 symbols");
997                                 }
998 #endif  /* TC_I960 */
999                                 add_number += S_GET_VALUE(add_symbolP) - 
1000                                     S_GET_VALUE(sub_symbolP);
1001                                 
1002                                 add_symbolP = NULL;
1003                                 fixP->fx_addsy = NULL;
1004                         } else {
1005                                 /* Different segments in subtraction. */
1006                                 know(!(S_IS_EXTERNAL(sub_symbolP) && (S_GET_SEGMENT(sub_symbolP) == SEG_ABSOLUTE)));
1007                                 
1008                                 if ((S_GET_SEGMENT(sub_symbolP) == SEG_ABSOLUTE)) {
1009                                         add_number -= S_GET_VALUE(sub_symbolP);
1010                                 } else {
1011                                         as_bad("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
1012                                                segment_name(S_GET_SEGMENT(sub_symbolP)),
1013                                                S_GET_NAME(sub_symbolP), fragP->fr_address + where);
1014                                 } /* if absolute */
1015                         }
1016                 } /* if sub_symbolP */
1017                 
1018                 if (add_symbolP) {
1019                         if (add_symbol_segment == this_segment_type && pcrel) {
1020                                 /*
1021                                  * This fixup was made when the symbol's segment was
1022                                  * SEG_UNKNOWN, but it is now in the local segment.
1023                                  * So we know how to do the address without relocation.
1024                                  */
1025 #ifdef TC_I960
1026                                 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
1027                                  * in which cases it modifies *fixP as appropriate.  In the case
1028                                  * of a 'calls', no further work is required, and *fixP has been
1029                                  * set up to make the rest of the code below a no-op.
1030                                  */
1031                                 reloc_callj(fixP);
1032 #endif /* TC_I960 */
1033                                 
1034                                 add_number += S_GET_VALUE(add_symbolP);
1035                                 add_number -= md_pcrel_from (fixP);
1036                                 pcrel = 0;      /* Lie. Don't want further pcrel processing. */
1037                                 fixP->fx_addsy = NULL; /* No relocations please. */
1038                         } else {
1039                                 switch (add_symbol_segment) {
1040                                 case SEG_ABSOLUTE:
1041 #ifdef TC_I960
1042                                         reloc_callj(fixP); /* See comment about reloc_callj() above*/
1043 #endif /* TC_I960 */
1044                                         add_number += S_GET_VALUE(add_symbolP);
1045                                         fixP->fx_addsy = NULL;
1046                                         add_symbolP = NULL;
1047                                         break;
1048                                 default:
1049                                         seg_reloc_count ++;
1050                                         add_number += S_GET_VALUE(add_symbolP);
1051                                         break;
1052                                         
1053                                 case SEG_UNKNOWN:
1054 #ifdef TC_I960
1055                                         if ((int)fixP->fx_bit_fixP == 13) {
1056                                                 /* This is a COBR instruction.  They have only a
1057                                                  * 13-bit displacement and are only to be used
1058                                                  * for local branches: flag as error, don't generate
1059                                                  * relocation.
1060                                                  */
1061                                                 as_bad("can't use COBR format with external label");
1062                                                 fixP->fx_addsy = NULL;  /* No relocations please. */
1063                                                 continue;
1064                                         } /* COBR */
1065 #endif /* TC_I960 */
1066
1067 #ifdef OBJ_COFF
1068 #ifdef TE_I386AIX
1069                                         if (S_IS_COMMON(add_symbolP))
1070                                             add_number += S_GET_VALUE(add_symbolP);
1071 #endif /* TE_I386AIX */
1072 #endif /* OBJ_COFF */
1073                                         ++seg_reloc_count;
1074                                         
1075                                         break;
1076                                         
1077                                         
1078                                 } /* switch on symbol seg */
1079                         } /* if not in local seg */
1080                 } /* if there was a + symbol */
1081                 
1082                 if (pcrel) {
1083                         add_number -= md_pcrel_from(fixP);
1084                         if (add_symbolP == 0) {
1085                                 fixP->fx_addsy = & abs_symbol;
1086                                 ++seg_reloc_count;
1087                         } /* if there's an add_symbol */
1088                 } /* if pcrel */
1089                 
1090                 if (!fixP->fx_bit_fixP) {
1091                         if ((size==1 &&
1092                              (add_number& ~0xFF)   && (add_number&~0xFF!=(-1&~0xFF))) ||
1093                             (size==2 &&
1094                              (add_number& ~0xFFFF) && (add_number&~0xFFFF!=(-1&~0xFFFF)))) {
1095                                 as_bad("Value of %d too large for field of %d bytes at 0x%x",
1096                                        add_number, size, fragP->fr_address + where);
1097                         } /* generic error checking */
1098                 } /* not a bit fix */
1099                 
1100                 md_apply_fix(fixP, add_number);
1101         } /* For each fixS in this segment. */
1102         
1103 #ifdef OBJ_COFF
1104 #ifdef TC_I960
1105         {
1106                 fixS *topP = fixP;
1107                 
1108                 /* two relocs per callj under coff. */
1109                 for (fixP = topP; fixP; fixP = fixP->fx_next) {
1110                         if (fixP->fx_callj && fixP->fx_addsy != 0) {
1111                                 ++seg_reloc_count;
1112                         } /* if callj and not already fixed. */
1113                 } /* for each fix */
1114         }
1115 #endif /* TC_I960 */
1116         
1117 #endif /* OBJ_COFF */
1118         return(seg_reloc_count);
1119 } /* fixup_segment() */
1120
1121
1122 static int is_dnrange(f1,f2)
1123 struct frag *f1;
1124 struct frag *f2;
1125 {
1126         while (f1) {
1127                 if (f1->fr_next==f2)
1128                     return 1;
1129                 f1=f1->fr_next;
1130         }
1131         return 0;
1132 } /* is_dnrange() */
1133
1134 /* Append a string onto another string, bumping the pointer along.  */
1135 void
1136     append (charPP, fromP, length)
1137 char    **charPP;
1138 char    *fromP;
1139 unsigned long length;
1140 {
1141         if (length) {           /* Don't trust memcpy() of 0 chars. */
1142                 memcpy(*charPP, fromP, (int) length);
1143                 *charPP += length;
1144         }
1145 }
1146
1147 int section_alignment[SEG_MAXIMUM_ORDINAL];
1148
1149 /*
1150  * This routine records the largest alignment seen for each segment.
1151  * If the beginning of the segment is aligned on the worst-case
1152  * boundary, all of the other alignments within it will work.  At
1153  * least one object format really uses this info.
1154  */
1155 void record_alignment(seg, align)
1156 segT seg;       /* Segment to which alignment pertains */
1157 int align;      /* Alignment, as a power of 2
1158                  *      (e.g., 1 => 2-byte boundary, 2 => 4-byte boundary, etc.)
1159                  */
1160 {
1161         
1162         if ( align > section_alignment[(int) seg] ){
1163                 section_alignment[(int) seg] = align;
1164         } /* if highest yet */
1165         
1166         return;
1167 } /* record_alignment() */
1168
1169 /*
1170  * Local Variables:
1171  * comment-column: 0
1172  * fill-column: 131
1173  * End:
1174  */
1175
1176 /* end of write.c */