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