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