This commit was generated by cvs2svn to track changes on a CVS vendor
[deliverable/binutils-gdb.git] / gas / doc / internals.texi
1 \input texinfo
2 @setfilename internals.info
3 @node Top
4 @top Assembler Internals
5 @raisesections
6 @cindex internals
7
8 This chapter describes the internals of the assembler. It is incomplete, but
9 it may help a bit.
10
11 This chapter was last modified on $Date$. It is not updated regularly, and it
12 may be out of date.
13
14 @menu
15 * GAS versions:: GAS versions
16 * Data types:: Data types
17 * GAS processing:: What GAS does when it runs
18 * Porting GAS:: Porting GAS
19 * Relaxation:: Relaxation
20 * Broken words:: Broken words
21 * Internal functions:: Internal functions
22 * Test suite:: Test suite
23 @end menu
24
25 @node GAS versions
26 @section GAS versions
27
28 GAS has acquired layers of code over time. The original GAS only supported the
29 a.out object file format, with three sections. Support for multiple sections
30 has been added in two different ways.
31
32 The preferred approach is to use the version of GAS created when the symbol
33 @code{BFD_ASSEMBLER} is defined. The other versions of GAS are documented for
34 historical purposes, and to help anybody who has to debug code written for
35 them.
36
37 The type @code{segT} is used to represent a section in code which must work
38 with all versions of GAS.
39
40 @menu
41 * Original GAS:: Original GAS version
42 * MANY_SEGMENTS:: MANY_SEGMENTS gas version
43 * BFD_ASSEMBLER:: BFD_ASSEMBLER gas version
44 @end menu
45
46 @node Original GAS
47 @subsection Original GAS
48
49 The original GAS only supported the a.out object file format with three
50 sections: @samp{.text}, @samp{.data}, and @samp{.bss}. This is the version of
51 GAS that is compiled if neither @code{BFD_ASSEMBLER} nor @code{MANY_SEGMENTS}
52 is defined. This version of GAS is still used for the m68k-aout target, and
53 perhaps others.
54
55 This version of GAS should not be used for any new development.
56
57 There is still code that is specific to this version of GAS, notably in
58 @file{write.c}. There is no way for this code to loop through all the
59 sections; it simply looks at global variables like @code{text_frag_root} and
60 @code{data_frag_root}.
61
62 The type @code{segT} is an enum.
63
64 @node MANY_SEGMENTS
65 @subsection MANY_SEGMENTS gas version
66 @cindex MANY_SEGMENTS
67
68 The @code{MANY_SEGMENTS} version of gas is only used for COFF. It uses the BFD
69 library, but it writes out all the data itself using @code{bfd_write}. This
70 version of gas supports up to 40 normal sections. The section names are stored
71 in the @code{seg_name} array. Other information is stored in the
72 @code{segment_info} array.
73
74 The type @code{segT} is an enum. Code that wants to examine all the sections
75 can use a @code{segT} variable as loop index from @code{SEG_E0} up to but not
76 including @code{SEG_UNKNOWN}.
77
78 Most of the code specific to this version of GAS is in the file
79 @file{config/obj-coff.c}, in the portion of that file that is compiled when
80 @code{BFD_ASSEMBLER} is not defined.
81
82 This version of GAS is still used for several COFF targets.
83
84 @node BFD_ASSEMBLER
85 @subsection BFD_ASSEMBLER gas version
86 @cindex BFD_ASSEMBLER
87
88 The preferred version of GAS is the @code{BFD_ASSEMBLER} version. In this
89 version of GAS, the output file is a normal BFD, and the BFD routines are used
90 to generate the output.
91
92 @code{BFD_ASSEMBLER} will automatically be used for certain targets, including
93 those that use the ELF, ECOFF, and SOM object file formats, and also all Alpha,
94 MIPS, PowerPC, and SPARC targets. You can force the use of
95 @code{BFD_ASSEMBLER} for other targets with the configure option
96 @samp{--enable-bfd-assembler}; however, it has not been tested for many
97 targets, and can not be assumed to work.
98
99 @node Data types
100 @section Data types
101 @cindex internals, data types
102
103 This section describes some fundamental GAS data types.
104
105 @menu
106 * Symbols:: The symbolS structure
107 * Expressions:: The expressionS structure
108 * Fixups:: The fixS structure
109 * Frags:: The fragS structure
110 @end menu
111
112 @node Symbols
113 @subsection Symbols
114 @cindex internals, symbols
115 @cindex symbols, internal
116 @cindex symbolS structure
117
118 The definition for @code{struct symbol}, also known as @code{symbolS}, is
119 located in @file{struc-symbol.h}. Symbol structures contain the following
120 fields:
121
122 @table @code
123 @item sy_value
124 This is an @code{expressionS} that describes the value of the symbol. It might
125 refer to one or more other symbols; if so, its true value may not be known
126 until @code{resolve_symbol_value} is called in @code{write_object_file}.
127
128 The expression is often simply a constant. Before @code{resolve_symbol_value}
129 is called, the value is the offset from the frag (@pxref{Frags}). Afterward,
130 the frag address has been added in.
131
132 @item sy_resolved
133 This field is non-zero if the symbol's value has been completely resolved. It
134 is used during the final pass over the symbol table.
135
136 @item sy_resolving
137 This field is used to detect loops while resolving the symbol's value.
138
139 @item sy_used_in_reloc
140 This field is non-zero if the symbol is used by a relocation entry. If a local
141 symbol is used in a relocation entry, it must be possible to redirect those
142 relocations to other symbols, or this symbol cannot be removed from the final
143 symbol list.
144
145 @item sy_next
146 @itemx sy_previous
147 These pointers to other @code{symbolS} structures describe a singly or doubly
148 linked list. (If @code{SYMBOLS_NEED_BACKPOINTERS} is not defined, the
149 @code{sy_previous} field will be omitted; @code{SYMBOLS_NEED_BACKPOINTERS} is
150 always defined if @code{BFD_ASSEMBLER}.) These fields should be accessed with
151 the @code{symbol_next} and @code{symbol_previous} macros.
152
153 @item sy_frag
154 This points to the frag (@pxref{Frags}) that this symbol is attached to.
155
156 @item sy_used
157 Whether the symbol is used as an operand or in an expression. Note: Not all of
158 the backends keep this information accurate; backends which use this bit are
159 responsible for setting it when a symbol is used in backend routines.
160
161 @item sy_mri_common
162 Whether the symbol is an MRI common symbol created by the @code{COMMON}
163 pseudo-op when assembling in MRI mode.
164
165 @item bsym
166 If @code{BFD_ASSEMBLER} is defined, this points to the BFD @code{asymbol} that
167 will be used in writing the object file.
168
169 @item sy_name_offset
170 (Only used if @code{BFD_ASSEMBLER} is not defined.) This is the position of
171 the symbol's name in the string table of the object file. On some formats,
172 this will start at position 4, with position 0 reserved for unnamed symbols.
173 This field is not used until @code{write_object_file} is called.
174
175 @item sy_symbol
176 (Only used if @code{BFD_ASSEMBLER} is not defined.) This is the
177 format-specific symbol structure, as it would be written into the object file.
178
179 @item sy_number
180 (Only used if @code{BFD_ASSEMBLER} is not defined.) This is a 24-bit symbol
181 number, for use in constructing relocation table entries.
182
183 @item sy_obj
184 This format-specific data is of type @code{OBJ_SYMFIELD_TYPE}. If no macro by
185 that name is defined in @file{obj-format.h}, this field is not defined.
186
187 @item sy_tc
188 This processor-specific data is of type @code{TC_SYMFIELD_TYPE}. If no macro
189 by that name is defined in @file{targ-cpu.h}, this field is not defined.
190
191 @item TARGET_SYMBOL_FIELDS
192 If this macro is defined, it defines additional fields in the symbol structure.
193 This macro is obsolete, and should be replaced when possible by uses of
194 @code{OBJ_SYMFIELD_TYPE} and @code{TC_SYMFIELD_TYPE}.
195 @end table
196
197 There are a number of access routines used to extract the fields of a
198 @code{symbolS} structure. When possible, these routines should be used rather
199 than referring to the fields directly. These routines will work for any GAS
200 version.
201
202 @table @code
203 @item S_SET_VALUE
204 @cindex S_SET_VALUE
205 Set the symbol's value.
206
207 @item S_GET_VALUE
208 @cindex S_GET_VALUE
209 Get the symbol's value. This will cause @code{resolve_symbol_value} to be
210 called if necessary, so @code{S_GET_VALUE} should only be called when it is
211 safe to resolve symbols (i.e., after the entire input file has been read and
212 all symbols have been defined).
213
214 @item S_SET_SEGMENT
215 @cindex S_SET_SEGMENT
216 Set the section of the symbol.
217
218 @item S_GET_SEGMENT
219 @cindex S_GET_SEGMENT
220 Get the symbol's section.
221
222 @item S_GET_NAME
223 @cindex S_GET_NAME
224 Get the name of the symbol.
225
226 @item S_SET_NAME
227 @cindex S_SET_NAME
228 Set the name of the symbol.
229
230 @item S_IS_EXTERNAL
231 @cindex S_IS_EXTERNAL
232 Return non-zero if the symbol is externally visible.
233
234 @item S_IS_EXTERN
235 @cindex S_IS_EXTERN
236 A synonym for @code{S_IS_EXTERNAL}. Don't use it.
237
238 @item S_IS_WEAK
239 @cindex S_IS_WEAK
240 Return non-zero if the symbol is weak.
241
242 @item S_IS_COMMON
243 @cindex S_IS_COMMON
244 Return non-zero if this is a common symbol. Common symbols are sometimes
245 represented as undefined symbols with a value, in which case this function will
246 not be reliable.
247
248 @item S_IS_DEFINED
249 @cindex S_IS_DEFINED
250 Return non-zero if this symbol is defined. This function is not reliable when
251 called on a common symbol.
252
253 @item S_IS_DEBUG
254 @cindex S_IS_DEBUG
255 Return non-zero if this is a debugging symbol.
256
257 @item S_IS_LOCAL
258 @cindex S_IS_LOCAL
259 Return non-zero if this is a local assembler symbol which should not be
260 included in the final symbol table. Note that this is not the opposite of
261 @code{S_IS_EXTERNAL}. The @samp{-L} assembler option affects the return value
262 of this function.
263
264 @item S_SET_EXTERNAL
265 @cindex S_SET_EXTERNAL
266 Mark the symbol as externally visible.
267
268 @item S_CLEAR_EXTERNAL
269 @cindex S_CLEAR_EXTERNAL
270 Mark the symbol as not externally visible.
271
272 @item S_SET_WEAK
273 @cindex S_SET_WEAK
274 Mark the symbol as weak.
275
276 @item S_GET_TYPE
277 @item S_GET_DESC
278 @item S_GET_OTHER
279 @cindex S_GET_TYPE
280 @cindex S_GET_DESC
281 @cindex S_GET_OTHER
282 Get the @code{type}, @code{desc}, and @code{other} fields of the symbol. These
283 are only defined for object file formats for which they make sense (primarily
284 a.out).
285
286 @item S_SET_TYPE
287 @item S_SET_DESC
288 @item S_SET_OTHER
289 @cindex S_SET_TYPE
290 @cindex S_SET_DESC
291 @cindex S_SET_OTHER
292 Set the @code{type}, @code{desc}, and @code{other} fields of the symbol. These
293 are only defined for object file formats for which they make sense (primarily
294 a.out).
295
296 @item S_GET_SIZE
297 @cindex S_GET_SIZE
298 Get the size of a symbol. This is only defined for object file formats for
299 which it makes sense (primarily ELF).
300
301 @item S_SET_SIZE
302 @cindex S_SET_SIZE
303 Set the size of a symbol. This is only defined for object file formats for
304 which it makes sense (primarily ELF).
305 @end table
306
307 @node Expressions
308 @subsection Expressions
309 @cindex internals, expressions
310 @cindex expressions, internal
311 @cindex expressionS structure
312
313 Expressions are stored in an @code{expressionS} structure. The structure is
314 defined in @file{expr.h}.
315
316 @cindex expression
317 The macro @code{expression} will create an @code{expressionS} structure based
318 on the text found at the global variable @code{input_line_pointer}.
319
320 @cindex make_expr_symbol
321 @cindex expr_symbol_where
322 A single @code{expressionS} structure can represent a single operation.
323 Complex expressions are formed by creating @dfn{expression symbols} and
324 combining them in @code{expressionS} structures. An expression symbol is
325 created by calling @code{make_expr_symbol}. An expression symbol should
326 naturally never appear in a symbol table, and the implementation of
327 @code{S_IS_LOCAL} (@pxref{Symbols}) reflects that. The function
328 @code{expr_symbol_where} returns non-zero if a symbol is an expression symbol,
329 and also returns the file and line for the expression which caused it to be
330 created.
331
332 The @code{expressionS} structure has two symbol fields, a number field, an
333 operator field, and a field indicating whether the number is unsigned.
334
335 The operator field is of type @code{operatorT}, and describes how to interpret
336 the other fields; see the definition in @file{expr.h} for the possibilities.
337
338 An @code{operatorT} value of @code{O_big} indicates either a floating point
339 number, stored in the global variable @code{generic_floating_point_number}, or
340 an integer to large to store in an @code{offsetT} type, stored in the global
341 array @code{generic_bignum}. This rather inflexible approach makes it
342 impossible to use floating point numbers or large expressions in complex
343 expressions.
344
345 @node Fixups
346 @subsection Fixups
347 @cindex internals, fixups
348 @cindex fixups
349 @cindex fixS structure
350
351 A @dfn{fixup} is basically anything which can not be resolved in the first
352 pass. Sometimes a fixup can be resolved by the end of the assembly; if not,
353 the fixup becomes a relocation entry in the object file.
354
355 @cindex fix_new
356 @cindex fix_new_exp
357 A fixup is created by a call to @code{fix_new} or @code{fix_new_exp}. Both
358 take a frag (@pxref{Frags}), a position within the frag, a size, an indication
359 of whether the fixup is PC relative, and a type. In a @code{BFD_ASSEMBLER}
360 GAS, the type is nominally a @code{bfd_reloc_code_real_type}, but several
361 targets use other type codes to represent fixups that can not be described as
362 relocations.
363
364 The @code{fixS} structure has a number of fields, several of which are obsolete
365 or are only used by a particular target. The important fields are:
366
367 @table @code
368 @item fx_frag
369 The frag (@pxref{Frags}) this fixup is in.
370
371 @item fx_where
372 The location within the frag where the fixup occurs.
373
374 @item fx_addsy
375 The symbol this fixup is against. Typically, the value of this symbol is added
376 into the object contents. This may be NULL.
377
378 @item fx_subsy
379 The value of this symbol is subtracted from the object contents. This is
380 normally NULL.
381
382 @item fx_offset
383 A number which is added into the fixup.
384
385 @item fx_addnumber
386 Some CPU backends use this field to convey information between
387 @code{md_apply_fix} and @code{tc_gen_reloc}. The machine independent code does
388 not use it.
389
390 @item fx_next
391 The next fixup in the section.
392
393 @item fx_r_type
394 The type of the fixup. This field is only defined if @code{BFD_ASSEMBLER}, or
395 if the target defines @code{NEED_FX_R_TYPE}.
396
397 @item fx_size
398 The size of the fixup. This is mostly used for error checking.
399
400 @item fx_pcrel
401 Whether the fixup is PC relative.
402
403 @item fx_done
404 Non-zero if the fixup has been applied, and no relocation entry needs to be
405 generated.
406
407 @item fx_file
408 @itemx fx_line
409 The file and line where the fixup was created.
410
411 @item tc_fix_data
412 This has the type @code{TC_FIX_TYPE}, and is only defined if the target defines
413 that macro.
414 @end table
415
416 @node Frags
417 @subsection Frags
418 @cindex internals, frags
419 @cindex frags
420 @cindex fragS structure.
421
422 The @code{fragS} structure is defined in @file{as.h}. Each frag represents a
423 portion of the final object file. As GAS reads the source file, it creates
424 frags to hold the data that it reads. At the end of the assembly the frags and
425 fixups are processed to produce the final contents.
426
427 @table @code
428 @item fr_address
429 The address of the frag. This is not set until the assembler rescans the list
430 of all frags after the entire input file is parsed. The function
431 @code{relax_segment} fills in this field.
432
433 @item fr_next
434 Pointer to the next frag in this (sub)section.
435
436 @item fr_fix
437 Fixed number of characters we know we're going to emit to the output file. May
438 be zero.
439
440 @item fr_var
441 Variable number of characters we may output, after the initial @code{fr_fix}
442 characters. May be zero.
443
444 @item fr_offset
445 The interpretation of this field is controlled by @code{fr_type}. Generally,
446 if @code{fr_var} is non-zero, this is a repeat count: the @code{fr_var}
447 characters are output @code{fr_offset} times.
448
449 @item line
450 Holds line number info when an assembler listing was requested.
451
452 @item fr_type
453 Relaxation state. This field indicates the interpretation of @code{fr_offset},
454 @code{fr_symbol} and the variable-length tail of the frag, as well as the
455 treatment it gets in various phases of processing. It does not affect the
456 initial @code{fr_fix} characters; they are always supposed to be output
457 verbatim (fixups aside). See below for specific values this field can have.
458
459 @item fr_subtype
460 Relaxation substate. If the macro @code{md_relax_frag} isn't defined, this is
461 assumed to be an index into @code{TC_GENERIC_RELAX_TABLE} for the generic
462 relaxation code to process (@pxref{Relaxation}). If @code{md_relax_frag} is
463 defined, this field is available for any use by the CPU-specific code.
464
465 @item fr_symbol
466 This normally indicates the symbol to use when relaxing the frag according to
467 @code{fr_type}.
468
469 @item fr_opcode
470 Points to the lowest-addressed byte of the opcode, for use in relaxation.
471
472 @item tc_frag_data
473 Target specific fragment data of type TC_FRAG_TYPE.
474 Only present if @code{TC_FRAG_TYPE} is defined.
475
476 @item fr_file
477 @itemx fr_line
478 The file and line where this frag was last modified.
479
480 @item fr_literal
481 Declared as a one-character array, this last field grows arbitrarily large to
482 hold the actual contents of the frag.
483 @end table
484
485 These are the possible relaxation states, provided in the enumeration type
486 @code{relax_stateT}, and the interpretations they represent for the other
487 fields:
488
489 @table @code
490 @item rs_align
491 @itemx rs_align_code
492 The start of the following frag should be aligned on some boundary. In this
493 frag, @code{fr_offset} is the logarithm (base 2) of the alignment in bytes.
494 (For example, if alignment on an 8-byte boundary were desired, @code{fr_offset}
495 would have a value of 3.) The variable characters indicate the fill pattern to
496 be used. The @code{fr_subtype} field holds the maximum number of bytes to skip
497 when doing this alignment. If more bytes are needed, the alignment is not
498 done. An @code{fr_subtype} value of 0 means no maximum, which is the normal
499 case. Target backends can use @code{rs_align_code} to handle certain types of
500 alignment differently.
501
502 @item rs_broken_word
503 This indicates that ``broken word'' processing should be done (@pxref{Broken
504 words}). If broken word processing is not necessary on the target machine,
505 this enumerator value will not be defined.
506
507 @item rs_fill
508 The variable characters are to be repeated @code{fr_offset} times. If
509 @code{fr_offset} is 0, this frag has a length of @code{fr_fix}. Most frags
510 have this type.
511
512 @item rs_leb128
513 This state is used to implement the DWARF ``little endian base 128''
514 variable length number format. The @code{fr_symbol} is always an expression
515 symbol, as constant expressions are emitted directly. The @code{fr_offset}
516 field is used during relaxation to hold the previous size of the number so
517 that we can determine if the fragment changed size.
518
519 @item rs_machine_dependent
520 Displacement relaxation is to be done on this frag. The target is indicated by
521 @code{fr_symbol} and @code{fr_offset}, and @code{fr_subtype} indicates the
522 particular machine-specific addressing mode desired. @xref{Relaxation}.
523
524 @item rs_org
525 The start of the following frag should be pushed back to some specific offset
526 within the section. (Some assemblers use the value as an absolute address; GAS
527 does not handle final absolute addresses, but rather requires that the linker
528 set them.) The offset is given by @code{fr_symbol} and @code{fr_offset}; one
529 character from the variable-length tail is used as the fill character.
530 @end table
531
532 @cindex frchainS structure
533 A chain of frags is built up for each subsection. The data structure
534 describing a chain is called a @code{frchainS}, and contains the following
535 fields:
536
537 @table @code
538 @item frch_root
539 Points to the first frag in the chain. May be NULL if there are no frags in
540 this chain.
541 @item frch_last
542 Points to the last frag in the chain, or NULL if there are none.
543 @item frch_next
544 Next in the list of @code{frchainS} structures.
545 @item frch_seg
546 Indicates the section this frag chain belongs to.
547 @item frch_subseg
548 Subsection (subsegment) number of this frag chain.
549 @item fix_root, fix_tail
550 (Defined only if @code{BFD_ASSEMBLER} is defined). Point to first and last
551 @code{fixS} structures associated with this subsection.
552 @item frch_obstack
553 Not currently used. Intended to be used for frag allocation for this
554 subsection. This should reduce frag generation caused by switching sections.
555 @item frch_frag_now
556 The current frag for this subsegment.
557 @end table
558
559 A @code{frchainS} corresponds to a subsection; each section has a list of
560 @code{frchainS} records associated with it. In most cases, only one subsection
561 of each section is used, so the list will only be one element long, but any
562 processing of frag chains should be prepared to deal with multiple chains per
563 section.
564
565 After the input files have been completely processed, and no more frags are to
566 be generated, the frag chains are joined into one per section for further
567 processing. After this point, it is safe to operate on one chain per section.
568
569 The assembler always has a current frag, named @code{frag_now}. More space is
570 allocated for the current frag using the @code{frag_more} function; this
571 returns a pointer to the amount of requested space. Relaxing is done using
572 variant frags allocated by @code{frag_var} or @code{frag_variant}
573 (@pxref{Relaxation}).
574
575 @node GAS processing
576 @section What GAS does when it runs
577 @cindex internals, overview
578
579 This is a quick look at what an assembler run looks like.
580
581 @itemize @bullet
582 @item
583 The assembler initializes itself by calling various init routines.
584
585 @item
586 For each source file, the @code{read_a_source_file} function reads in the file
587 and parses it. The global variable @code{input_line_pointer} points to the
588 current text; it is guaranteed to be correct up to the end of the line, but not
589 farther.
590
591 @item
592 For each line, the assembler passes labels to the @code{colon} function, and
593 isolates the first word. If it looks like a pseudo-op, the word is looked up
594 in the pseudo-op hash table @code{po_hash} and dispatched to a pseudo-op
595 routine. Otherwise, the target dependent @code{md_assemble} routine is called
596 to parse the instruction.
597
598 @item
599 When pseudo-ops or instructions output data, they add it to a frag, calling
600 @code{frag_more} to get space to store it in.
601
602 @item
603 Pseudo-ops and instructions can also output fixups created by @code{fix_new} or
604 @code{fix_new_exp}.
605
606 @item
607 For certain targets, instructions can create variant frags which are used to
608 store relaxation information (@pxref{Relaxation}).
609
610 @item
611 When the input file is finished, the @code{write_object_file} routine is
612 called. It assigns addresses to all the frags (@code{relax_segment}), resolves
613 all the fixups (@code{fixup_segment}), resolves all the symbol values (using
614 @code{resolve_symbol_value}), and finally writes out the file (in the
615 @code{BFD_ASSEMBLER} case, this is done by simply calling @code{bfd_close}).
616 @end itemize
617
618 @node Porting GAS
619 @section Porting GAS
620 @cindex porting
621
622 Each GAS target specifies two main things: the CPU file and the object format
623 file. Two main switches in the @file{configure.in} file handle this. The
624 first switches on CPU type to set the shell variable @code{cpu_type}. The
625 second switches on the entire target to set the shell variable @code{fmt}.
626
627 The configure script uses the value of @code{cpu_type} to select two files in
628 the @file{config} directory: @file{tc-@var{CPU}.c} and @file{tc-@var{CPU}.h}.
629 The configuration process will create a file named @file{targ-cpu.h} in the
630 build directory which includes @file{tc-@var{CPU}.h}.
631
632 The configure script also uses the value of @code{fmt} to select two files:
633 @file{obj-@var{fmt}.c} and @file{obj-@var{fmt}.h}. The configuration process
634 will create a file named @file{obj-format.h} in the build directory which
635 includes @file{obj-@var{fmt}.h}.
636
637 You can also set the emulation in the configure script by setting the @code{em}
638 variable. Normally the default value of @samp{generic} is fine. The
639 configuration process will create a file named @file{targ-env.h} in the build
640 directory which includes @file{te-@var{em}.h}.
641
642 Porting GAS to a new CPU requires writing the @file{tc-@var{CPU}} files.
643 Porting GAS to a new object file format requires writing the
644 @file{obj-@var{fmt}} files. There is sometimes some interaction between these
645 two files, but it is normally minimal.
646
647 The best approach is, of course, to copy existing files. The documentation
648 below assumes that you are looking at existing files to see usage details.
649
650 These interfaces have grown over time, and have never been carefully thought
651 out or designed. Nothing about the interfaces described here is cast in stone.
652 It is possible that they will change from one version of the assembler to the
653 next. Also, new macros are added all the time as they are needed.
654
655 @menu
656 * CPU backend:: Writing a CPU backend
657 * Object format backend:: Writing an object format backend
658 * Emulations:: Writing emulation files
659 @end menu
660
661 @node CPU backend
662 @subsection Writing a CPU backend
663 @cindex CPU backend
664 @cindex @file{tc-@var{CPU}}
665
666 The CPU backend files are the heart of the assembler. They are the only parts
667 of the assembler which actually know anything about the instruction set of the
668 processor.
669
670 You must define a reasonably small list of macros and functions in the CPU
671 backend files. You may define a large number of additional macros in the CPU
672 backend files, not all of which are documented here. You must, of course,
673 define macros in the @file{.h} file, which is included by every assembler
674 source file. You may define the functions as macros in the @file{.h} file, or
675 as functions in the @file{.c} file.
676
677 @table @code
678 @item TC_@var{CPU}
679 @cindex TC_@var{CPU}
680 By convention, you should define this macro in the @file{.h} file. For
681 example, @file{tc-m68k.h} defines @code{TC_M68K}. You might have to use this
682 if it is necessary to add CPU specific code to the object format file.
683
684 @item TARGET_FORMAT
685 This macro is the BFD target name to use when creating the output file. This
686 will normally depend upon the @code{OBJ_@var{FMT}} macro.
687
688 @item TARGET_ARCH
689 This macro is the BFD architecture to pass to @code{bfd_set_arch_mach}.
690
691 @item TARGET_MACH
692 This macro is the BFD machine number to pass to @code{bfd_set_arch_mach}. If
693 it is not defined, GAS will use 0.
694
695 @item TARGET_BYTES_BIG_ENDIAN
696 You should define this macro to be non-zero if the target is big endian, and
697 zero if the target is little endian.
698
699 @item md_shortopts
700 @itemx md_longopts
701 @itemx md_longopts_size
702 @itemx md_parse_option
703 @itemx md_show_usage
704 @cindex md_shortopts
705 @cindex md_longopts
706 @cindex md_longopts_size
707 @cindex md_parse_option
708 @cindex md_show_usage
709 GAS uses these variables and functions during option processing.
710 @code{md_shortopts} is a @code{const char *} which GAS adds to the machine
711 independent string passed to @code{getopt}. @code{md_longopts} is a
712 @code{struct option []} which GAS adds to the machine independent long options
713 passed to @code{getopt}; you may use @code{OPTION_MD_BASE}, defined in
714 @file{as.h}, as the start of a set of long option indices, if necessary.
715 @code{md_longopts_size} is a @code{size_t} holding the size @code{md_longopts}.
716 GAS will call @code{md_parse_option} whenever @code{getopt} returns an
717 unrecognized code, presumably indicating a special code value which appears in
718 @code{md_longopts}. GAS will call @code{md_show_usage} when a usage message is
719 printed; it should print a description of the machine specific options.
720
721 @item md_begin
722 @cindex md_begin
723 GAS will call this function at the start of the assembly, after the command
724 line arguments have been parsed and all the machine independent initializations
725 have been completed.
726
727 @item md_cleanup
728 @cindex md_cleanup
729 If you define this macro, GAS will call it at the end of each input file.
730
731 @item md_assemble
732 @cindex md_assemble
733 GAS will call this function for each input line which does not contain a
734 pseudo-op. The argument is a null terminated string. The function should
735 assemble the string as an instruction with operands. Normally
736 @code{md_assemble} will do this by calling @code{frag_more} and writing out
737 some bytes (@pxref{Frags}). @code{md_assemble} will call @code{fix_new} to
738 create fixups as needed (@pxref{Fixups}). Targets which need to do special
739 purpose relaxation will call @code{frag_var}.
740
741 @item md_pseudo_table
742 @cindex md_pseudo_table
743 This is a const array of type @code{pseudo_typeS}. It is a mapping from
744 pseudo-op names to functions. You should use this table to implement
745 pseudo-ops which are specific to the CPU.
746
747 @item tc_conditional_pseudoop
748 @cindex tc_conditional_pseudoop
749 If this macro is defined, GAS will call it with a @code{pseudo_typeS} argument.
750 It should return non-zero if the pseudo-op is a conditional which controls
751 whether code is assembled, such as @samp{.if}. GAS knows about the normal
752 conditional pseudo-ops,and you should normally not have to define this macro.
753
754 @item comment_chars
755 @cindex comment_chars
756 This is a null terminated @code{const char} array of characters which start a
757 comment.
758
759 @item tc_comment_chars
760 @cindex tc_comment_chars
761 If this macro is defined, GAS will use it instead of @code{comment_chars}.
762
763 @item line_comment_chars
764 @cindex line_comment_chars
765 This is a null terminated @code{const char} array of characters which start a
766 comment when they appear at the start of a line.
767
768 @item line_separator_chars
769 @cindex line_separator_chars
770 This is a null terminated @code{const char} array of characters which separate
771 lines (the semicolon is such a character by default, and need not be listed in
772 this array).
773
774 @item EXP_CHARS
775 @cindex EXP_CHARS
776 This is a null terminated @code{const char} array of characters which may be
777 used as the exponent character in a floating point number. This is normally
778 @code{"eE"}.
779
780 @item FLT_CHARS
781 @cindex FLT_CHARS
782 This is a null terminated @code{const char} array of characters which may be
783 used to indicate a floating point constant. A zero followed by one of these
784 characters is assumed to be followed by a floating point number; thus they
785 operate the way that @code{0x} is used to indicate a hexadecimal constant.
786 Usually this includes @samp{r} and @samp{f}.
787
788 @item LEX_AT
789 @cindex LEX_AT
790 You may define this macro to the lexical type of the @kbd{@}} character. The
791 default is zero.
792
793 Lexical types are a combination of @code{LEX_NAME} and @code{LEX_BEGIN_NAME},
794 both defined in @file{read.h}. @code{LEX_NAME} indicates that the character
795 may appear in a name. @code{LEX_BEGIN_NAME} indicates that the character may
796 appear at the beginning of a nem.
797
798 @item LEX_BR
799 @cindex LEX_BR
800 You may define this macro to the lexical type of the brace characters @kbd{@{},
801 @kbd{@}}, @kbd{[}, and @kbd{]}. The default value is zero.
802
803 @item LEX_PCT
804 @cindex LEX_PCT
805 You may define this macro to the lexical type of the @kbd{%} character. The
806 default value is zero.
807
808 @item LEX_QM
809 @cindex LEX_QM
810 You may define this macro to the lexical type of the @kbd{?} character. The
811 default value it zero.
812
813 @item LEX_DOLLAR
814 @cindex LEX_DOLLAR
815 You may define this macro to the lexical type of the @kbd{$} character. The
816 default value is @code{LEX_NAME | LEX_BEGIN_NAME}.
817
818 @item SINGLE_QUOTE_STRINGS
819 @cindex SINGLE_QUOTE_STRINGS
820 If you define this macro, GAS will treat single quotes as string delimiters.
821 Normally only double quotes are accepted as string delimiters.
822
823 @item NO_STRING_ESCAPES
824 @cindex NO_STRING_ESCAPES
825 If you define this macro, GAS will not permit escape sequences in a string.
826
827 @item ONLY_STANDARD_ESCAPES
828 @cindex ONLY_STANDARD_ESCAPES
829 If you define this macro, GAS will warn about the use of nonstandard escape
830 sequences in a string.
831
832 @item md_start_line_hook
833 @cindex md_start_line_hook
834 If you define this macro, GAS will call it at the start of each line.
835
836 @item LABELS_WITHOUT_COLONS
837 @cindex LABELS_WITHOUT_COLONS
838 If you define this macro, GAS will assume that any text at the start of a line
839 is a label, even if it does not have a colon.
840
841 @item TC_START_LABEL
842 @cindex TC_START_LABEL
843 You may define this macro to control what GAS considers to be a label. The
844 default definition is to accept any name followed by a colon character.
845
846 @item NO_PSEUDO_DOT
847 @cindex NO_PSEUDO_DOT
848 If you define this macro, GAS will not require pseudo-ops to start with a
849 @kbd{.} character.
850
851 @item TC_EQUAL_IN_INSN
852 @cindex TC_EQUAL_IN_INSN
853 If you define this macro, it should return nonzero if the instruction is
854 permitted to contain an @kbd{=} character. GAS will use this to decide if a
855 @kbd{=} is an assignment or an instruction.
856
857 @item TC_EOL_IN_INSN
858 @cindex TC_EOL_IN_INSN
859 If you define this macro, it should return nonzero if the current input line
860 pointer should be treated as the end of a line.
861
862 @item md_parse_name
863 @cindex md_parse_name
864 If this macro is defined, GAS will call it for any symbol found in an
865 expression. You can define this to handle special symbols in a special way.
866 If a symbol always has a certain value, you should normally enter it in the
867 symbol table, perhaps using @code{reg_section}.
868
869 @item md_undefined_symbol
870 @cindex md_undefined_symbol
871 GAS will call this function when a symbol table lookup fails, before it
872 creates a new symbol. Typically this would be used to supply symbols whose
873 name or value changes dynamically, possibly in a context sensitive way.
874 Predefined symbols with fixed values, such as register names or condition
875 codes, are typically entered directly into the symbol table when @code{md_begin}
876 is called.
877
878 @item md_operand
879 @cindex md_operand
880 GAS will call this function for any expression that can not be recognized.
881 When the function is called, @code{input_line_pointer} will point to the start
882 of the expression.
883
884 @item tc_unrecognized_line
885 @cindex tc_unrecognized_line
886 If you define this macro, GAS will call it when it finds a line that it can not
887 parse.
888
889 @item md_do_align
890 @cindex md_do_align
891 You may define this macro to handle an alignment directive. GAS will call it
892 when the directive is seen in the input file. For example, the i386 backend
893 uses this to generate efficient nop instructions of varying lengths, depending
894 upon the number of bytes that the alignment will skip.
895
896 @item HANDLE_ALIGN
897 @cindex HANDLE_ALIGN
898 You may define this macro to do special handling for an alignment directive.
899 GAS will call it at the end of the assembly.
900
901 @item md_flush_pending_output
902 @cindex md_flush_pending_output
903 If you define this macro, GAS will call it each time it skips any space because of a
904 space filling or alignment or data allocation pseudo-op.
905
906 @item TC_PARSE_CONS_EXPRESSION
907 @cindex TC_PARSE_CONS_EXPRESSION
908 You may define this macro to parse an expression used in a data allocation
909 pseudo-op such as @code{.word}. You can use this to recognize relocation
910 directives that may appear in such directives.
911
912 @item BITFIELD_CONS_EXPRESSION
913 @cindex BITFIELD_CONS_EXPRESSION
914 If you define this macro, GAS will recognize bitfield instructions in data
915 allocation pseudo-ops, as used on the i960.
916
917 @item REPEAT_CONS_EXPRESSION
918 @cindex REPEAT_CONS_EXPRESSION
919 If you define this macro, GAS will recognize repeat counts in data allocation
920 pseudo-ops, as used on the MIPS.
921
922 @item md_cons_align
923 @cindex md_cons_align
924 You may define this macro to do any special alignment before a data allocation
925 pseudo-op.
926
927 @item TC_CONS_FIX_NEW
928 @cindex TC_CONS_FIX_NEW
929 You may define this macro to generate a fixup for a data allocation pseudo-op.
930
931 @item TC_INIT_FIX_DATA (@var{fixp})
932 @cindex TC_INIT_FIX_DATA
933 A C statement to initialize the target specific fields of fixup @var{fixp}.
934
935 @item TC_FIX_DATA_PRINT (@var{stream}, @var{fixp})
936 @cindex TC_FIX_DATA_PRINT
937 A C statement to output target specific debugging information for
938 fixup @var{fixp} to @var{stream}. This macro is called by @code{print_fixup}.
939
940 @item md_number_to_chars
941 @cindex md_number_to_chars
942 This should just call either @code{number_to_chars_bigendian} or
943 @code{number_to_chars_littleendian}, whichever is appropriate. On targets like
944 the MIPS which support options to change the endianness, which function to call
945 is a runtime decision. On other targets, @code{md_number_to_chars} can be a
946 simple macro.
947
948 @item md_reloc_size
949 @cindex md_reloc_size
950 This variable is only used in the original version of gas (not
951 @code{BFD_ASSEMBLER} and not @code{MANY_SEGMENTS}). It holds the size of a
952 relocation entry.
953
954 @item WORKING_DOT_WORD
955 @itemx md_short_jump_size
956 @itemx md_long_jump_size
957 @itemx md_create_short_jump
958 @itemx md_create_long_jump
959 @cindex WORKING_DOT_WORD
960 @cindex md_short_jump_size
961 @cindex md_long_jump_size
962 @cindex md_create_short_jump
963 @cindex md_create_long_jump
964 If @code{WORKING_DOT_WORD} is defined, GAS will not do broken word processing
965 (@pxref{Broken words}). Otherwise, you should set @code{md_short_jump_size} to
966 the size of a short jump (a jump that is just long enough to jump around a long
967 jmp) and @code{md_long_jump_size} to the size of a long jump (a jump that can
968 go anywhere in the function), You should define @code{md_create_short_jump} to
969 create a short jump around a long jump, and define @code{md_create_long_jump}
970 to create a long jump.
971
972 @item md_estimate_size_before_relax
973 @cindex md_estimate_size_before_relax
974 This function returns an estimate of the size of a @code{rs_machine_dependent}
975 frag before any relaxing is done. It may also create any necessary
976 relocations.
977
978 @item md_relax_frag
979 @cindex md_relax_frag
980 This macro may be defined to relax a frag. GAS will call this with the frag
981 and the change in size of all previous frags; @code{md_relax_frag} should
982 return the change in size of the frag. @xref{Relaxation}.
983
984 @item TC_GENERIC_RELAX_TABLE
985 @cindex TC_GENERIC_RELAX_TABLE
986 If you do not define @code{md_relax_frag}, you may define
987 @code{TC_GENERIC_RELAX_TABLE} as a table of @code{relax_typeS} structures. The
988 machine independent code knows how to use such a table to relax PC relative
989 references. See @file{tc-m68k.c} for an example. @xref{Relaxation}.
990
991 @item md_prepare_relax_scan
992 @cindex md_prepare_relax_scan
993 If defined, it is a C statement that is invoked prior to scanning
994 the relax table.
995
996 @item LINKER_RELAXING_SHRINKS_ONLY
997 @cindex LINKER_RELAXING_SHRINKS_ONLY
998 If you define this macro, and the global variable @samp{linkrelax} is set
999 (because of a command line option, or unconditionally in @code{md_begin}), a
1000 @samp{.align} directive will cause extra space to be allocated. The linker can
1001 then discard this space when relaxing the section.
1002
1003 @item md_convert_frag
1004 @cindex md_convert_frag
1005 GAS will call this for each rs_machine_dependent fragment.
1006 The instruction is completed using the data from the relaxation pass.
1007 It may also create any necessary relocations.
1008 @xref{Relaxation}.
1009
1010 @item md_apply_fix
1011 @cindex md_apply_fix
1012 GAS will call this for each fixup. It should store the correct value in the
1013 object file.
1014
1015 @item TC_HANDLES_FX_DONE
1016 @cindex TC_HANDLES_FX_DONE
1017 If this macro is defined, it means that @code{md_apply_fix} correctly sets the
1018 @code{fx_done} field in the fixup.
1019
1020 @item tc_gen_reloc
1021 @cindex tc_gen_reloc
1022 A @code{BFD_ASSEMBLER} GAS will call this to generate a reloc. GAS will pass
1023 the resulting reloc to @code{bfd_install_relocation}. This currently works
1024 poorly, as @code{bfd_install_relocation} often does the wrong thing, and
1025 instances of @code{tc_gen_reloc} have been written to work around the problems,
1026 which in turns makes it difficult to fix @code{bfd_install_relocation}.
1027
1028 @item RELOC_EXPANSION_POSSIBLE
1029 @cindex RELOC_EXPANSION_POSSIBLE
1030 If you define this macro, it means that @code{tc_gen_reloc} may return multiple
1031 relocation entries for a single fixup. In this case, the return value of
1032 @code{tc_gen_reloc} is a pointer to a null terminated array.
1033
1034 @item MAX_RELOC_EXPANSION
1035 @cindex MAX_RELOC_EXPANSION
1036 You must define this if @code{RELOC_EXPANSION_POSSIBLE} is defined; it
1037 indicates the largest number of relocs which @code{tc_gen_reloc} may return for
1038 a single fixup.
1039
1040 @item tc_fix_adjustable
1041 @cindex tc_fix_adjustable
1042 You may define this macro to indicate whether a fixup against a locally defined
1043 symbol should be adjusted to be against the section symbol. It should return a
1044 non-zero value if the adjustment is acceptable.
1045
1046 @item MD_PCREL_FROM_SECTION
1047 @cindex MD_PCREL_FROM_SECTION
1048 If you define this macro, it should return the offset between the address of a
1049 PC relative fixup and the position from which the PC relative adjustment should
1050 be made. On many processors, the base of a PC relative instruction is the next
1051 instruction, so this macro would return the length of an instruction.
1052
1053 @item md_pcrel_from
1054 @cindex md_pcrel_from
1055 This is the default value of @code{MD_PCREL_FROM_SECTION}. The difference is
1056 that @code{md_pcrel_from} does not take a section argument.
1057
1058 @item tc_frob_label
1059 @cindex tc_frob_label
1060 If you define this macro, GAS will call it each time a label is defined.
1061
1062 @item md_section_align
1063 @cindex md_section_align
1064 GAS will call this function for each section at the end of the assembly, to
1065 permit the CPU backend to adjust the alignment of a section.
1066
1067 @item tc_frob_section
1068 @cindex tc_frob_section
1069 If you define this macro, a @code{BFD_ASSEMBLER} GAS will call it for each
1070 section at the end of the assembly.
1071
1072 @item tc_frob_file_before_adjust
1073 @cindex tc_frob_file_before_adjust
1074 If you define this macro, GAS will call it after the symbol values are
1075 resolved, but before the fixups have been changed from local symbols to section
1076 symbols.
1077
1078 @item tc_frob_symbol
1079 @cindex tc_frob_symbol
1080 If you define this macro, GAS will call it for each symbol. You can indicate
1081 that the symbol should not be included in the object file by definining this
1082 macro to set its second argument to a non-zero value.
1083
1084 @item tc_frob_file
1085 @cindex tc_frob_file
1086 If you define this macro, GAS will call it after the symbol table has been
1087 completed, but before the relocations have been generated.
1088
1089 @item tc_frob_file_after_relocs
1090 If you define this macro, GAS will call it after the relocs have been
1091 generated.
1092
1093 @item LISTING_HEADER
1094 A string to use on the header line of a listing. The default value is simply
1095 @code{"GAS LISTING"}.
1096
1097 @item LISTING_WORD_SIZE
1098 The number of bytes to put into a word in a listing. This affects the way the
1099 bytes are clumped together in the listing. For example, a value of 2 might
1100 print @samp{1234 5678} where a value of 1 would print @samp{12 34 56 78}. The
1101 default value is 4.
1102
1103 @item LISTING_LHS_WIDTH
1104 The number of words of data to print on the first line of a listing for a
1105 particular source line, where each word is @code{LISTING_WORD_SIZE} bytes. The
1106 default value is 1.
1107
1108 @item LISTING_LHS_WIDTH_SECOND
1109 Like @code{LISTING_LHS_WIDTH}, but applying to the second and subsequent line
1110 of the data printed for a particular source line. The default value is 1.
1111
1112 @item LISTING_LHS_CONT_LINES
1113 The maximum number of continuation lines to print in a listing for a particular
1114 source line. The default value is 4.
1115
1116 @item LISTING_RHS_WIDTH
1117 The maximum number of characters to print from one line of the input file. The
1118 default value is 100.
1119 @end table
1120
1121 @node Object format backend
1122 @subsection Writing an object format backend
1123 @cindex object format backend
1124 @cindex @file{obj-@var{fmt}}
1125
1126 As with the CPU backend, the object format backend must define a few things,
1127 and may define some other things. The interface to the object format backend
1128 is generally simpler; most of the support for an object file format consists of
1129 defining a number of pseudo-ops.
1130
1131 The object format @file{.h} file must include @file{targ-cpu.h}.
1132
1133 This section will only define the @code{BFD_ASSEMBLER} version of GAS. It is
1134 impossible to support a new object file format using any other version anyhow,
1135 as the original GAS version only supports a.out, and the @code{MANY_SEGMENTS}
1136 GAS version only supports COFF.
1137
1138 @table @code
1139 @item OBJ_@var{format}
1140 @cindex OBJ_@var{format}
1141 By convention, you should define this macro in the @file{.h} file. For
1142 example, @file{obj-elf.h} defines @code{OBJ_ELF}. You might have to use this
1143 if it is necessary to add object file format specific code to the CPU file.
1144
1145 @item obj_begin
1146 If you define this macro, GAS will call it at the start of the assembly, after
1147 the command line arguments have been parsed and all the machine independent
1148 initializations have been completed.
1149
1150 @item obj_app_file
1151 @cindex obj_app_file
1152 If you define this macro, GAS will invoke it when it sees a @code{.file}
1153 pseudo-op or a @samp{#} line as used by the C preprocessor.
1154
1155 @item OBJ_COPY_SYMBOL_ATTRIBUTES
1156 @cindex OBJ_COPY_SYMBOL_ATTRIBUTES
1157 You should define this macro to copy object format specific information from
1158 one symbol to another. GAS will call it when one symbol is equated to
1159 another.
1160
1161 @item obj_fix_adjustable
1162 @cindex obj_fix_adjustable
1163 You may define this macro to indicate whether a fixup against a locally defined
1164 symbol should be adjusted to be against the section symbol. It should return a
1165 non-zero value if the adjustment is acceptable.
1166
1167 @item obj_sec_sym_ok_for_reloc
1168 @cindex obj_sec_sym_ok_for_reloc
1169 You may define this macro to indicate that it is OK to use a section symbol in
1170 a relocateion entry. If it is not, GAS will define a new symbol at the start
1171 of a section.
1172
1173 @item EMIT_SECTION_SYMBOLS
1174 @cindex EMIT_SECTION_SYMBOLS
1175 You should define this macro with a zero value if you do not want to include
1176 section symbols in the output symbol table. The default value for this macro
1177 is one.
1178
1179 @item obj_adjust_symtab
1180 @cindex obj_adjust_symtab
1181 If you define this macro, GAS will invoke it just before setting the symbol
1182 table of the output BFD. For example, the COFF support uses this macro to
1183 generate a @code{.file} symbol if none was generated previously.
1184
1185 @item SEPARATE_STAB_SECTIONS
1186 @cindex SEPARATE_STAB_SECTIONS
1187 You may define this macro to indicate that stabs should be placed in separate
1188 sections, as in ELF.
1189
1190 @item INIT_STAB_SECTION
1191 @cindex INIT_STAB_SECTION
1192 You may define this macro to initialize the stabs section in the output file.
1193
1194 @item OBJ_PROCESS_STAB
1195 @cindex OBJ_PROCESS_STAB
1196 You may define this macro to do specific processing on a stabs entry.
1197
1198 @item obj_frob_section
1199 @cindex obj_frob_section
1200 If you define this macro, GAS will call it for each section at the end of the
1201 assembly.
1202
1203 @item obj_frob_file_before_adjust
1204 @cindex obj_frob_file_before_adjust
1205 If you define this macro, GAS will call it after the symbol values are
1206 resolved, but before the fixups have been changed from local symbols to section
1207 symbols.
1208
1209 @item obj_frob_symbol
1210 @cindex obj_frob_symbol
1211 If you define this macro, GAS will call it for each symbol. You can indicate
1212 that the symbol should not be included in the object file by definining this
1213 macro to set its second argument to a non-zero value.
1214
1215 @item obj_frob_file
1216 @cindex obj_frob_file
1217 If you define this macro, GAS will call it after the symbol table has been
1218 completed, but before the relocations have been generated.
1219
1220 @item obj_frob_file_after_relocs
1221 If you define this macro, GAS will call it after the relocs have been
1222 generated.
1223 @end table
1224
1225 @node Emulations
1226 @subsection Writing emulation files
1227
1228 Normally you do not have to write an emulation file. You can just use
1229 @file{te-generic.h}.
1230
1231 If you do write your own emulation file, it must include @file{obj-format.h}.
1232
1233 An emulation file will often define @code{TE_@var{EM}}; this may then be used
1234 in other files to change the output.
1235
1236 @node Relaxation
1237 @section Relaxation
1238 @cindex relaxation
1239
1240 @dfn{Relaxation} is a generic term used when the size of some instruction or
1241 data depends upon the value of some symbol or other data.
1242
1243 GAS knows to relax a particular type of PC relative relocation using a table.
1244 You can also define arbitrarily complex forms of relaxation yourself.
1245
1246 @menu
1247 * Relaxing with a table:: Relaxing with a table
1248 * General relaxing:: General relaxing
1249 @end menu
1250
1251 @node Relaxing with a table
1252 @subsection Relaxing with a table
1253
1254 If you do not define @code{md_relax_frag}, and you do define
1255 @code{TC_GENERIC_RELAX_TABLE}, GAS will relax @code{rs_machine_dependent} frags
1256 based on the frag subtype and the displacement to some specified target
1257 address. The basic idea is that several machines have different addressing
1258 modes for instructions that can specify different ranges of values, with
1259 successive modes able to access wider ranges, including the entirety of the
1260 previous range. Smaller ranges are assumed to be more desirable (perhaps the
1261 instruction requires one word instead of two or three); if this is not the
1262 case, don't describe the smaller-range, inferior mode.
1263
1264 The @code{fr_subtype} field of a frag is an index into a CPU-specific
1265 relaxation table. That table entry indicates the range of values that can be
1266 stored, the number of bytes that will have to be added to the frag to
1267 accomodate the addressing mode, and the index of the next entry to examine if
1268 the value to be stored is outside the range accessible by the current
1269 addressing mode. The @code{fr_symbol} field of the frag indicates what symbol
1270 is to be accessed; the @code{fr_offset} field is added in.
1271
1272 If the @code{TC_PCREL_ADJUST} macro is defined, which currently should only happen
1273 for the NS32k family, the @code{TC_PCREL_ADJUST} macro is called on the frag to
1274 compute an adjustment to be made to the displacement.
1275
1276 The value fitted by the relaxation code is always assumed to be a displacement
1277 from the current frag. (More specifically, from @code{fr_fix} bytes into the
1278 frag.)
1279 @ignore
1280 This seems kinda silly. What about fitting small absolute values? I suppose
1281 @code{md_assemble} is supposed to take care of that, but if the operand is a
1282 difference between symbols, it might not be able to, if the difference was not
1283 computable yet.
1284 @end ignore
1285
1286 The end of the relaxation sequence is indicated by a ``next'' value of 0. This
1287 means that the first entry in the table can't be used.
1288
1289 For some configurations, the linker can do relaxing within a section of an
1290 object file. If call instructions of various sizes exist, the linker can
1291 determine which should be used in each instance, when a symbol's value is
1292 resolved. In order for the linker to avoid wasting space and having to insert
1293 no-op instructions, it must be able to expand or shrink the section contents
1294 while still preserving intra-section references and meeting alignment
1295 requirements.
1296
1297 For the i960 using b.out format, no expansion is done; instead, each
1298 @samp{.align} directive causes extra space to be allocated, enough that when
1299 the linker is relaxing a section and removing unneeded space, it can discard
1300 some or all of this extra padding and cause the following data to be correctly
1301 aligned.
1302
1303 For the H8/300, I think the linker expands calls that can't reach, and doesn't
1304 worry about alignment issues; the cpu probably never needs any significant
1305 alignment beyond the instruction size.
1306
1307 The relaxation table type contains these fields:
1308
1309 @table @code
1310 @item long rlx_forward
1311 Forward reach, must be non-negative.
1312 @item long rlx_backward
1313 Backward reach, must be zero or negative.
1314 @item rlx_length
1315 Length in bytes of this addressing mode.
1316 @item rlx_more
1317 Index of the next-longer relax state, or zero if there is no next relax state.
1318 @end table
1319
1320 The relaxation is done in @code{relax_segment} in @file{write.c}. The
1321 difference in the length fields between the original mode and the one finally
1322 chosen by the relaxing code is taken as the size by which the current frag will
1323 be increased in size. For example, if the initial relaxing mode has a length
1324 of 2 bytes, and because of the size of the displacement, it gets upgraded to a
1325 mode with a size of 6 bytes, it is assumed that the frag will grow by 4 bytes.
1326 (The initial two bytes should have been part of the fixed portion of the frag,
1327 since it is already known that they will be output.) This growth must be
1328 effected by @code{md_convert_frag}; it should increase the @code{fr_fix} field
1329 by the appropriate size, and fill in the appropriate bytes of the frag.
1330 (Enough space for the maximum growth should have been allocated in the call to
1331 frag_var as the second argument.)
1332
1333 If relocation records are needed, they should be emitted by
1334 @code{md_estimate_size_before_relax}. This function should examine the target
1335 symbol of the supplied frag and correct the @code{fr_subtype} of the frag if
1336 needed. When this function is called, if the symbol has not yet been defined,
1337 it will not become defined later; however, its value may still change if the
1338 section it is in gets relaxed.
1339
1340 Usually, if the symbol is in the same section as the frag (given by the
1341 @var{sec} argument), the narrowest likely relaxation mode is stored in
1342 @code{fr_subtype}, and that's that.
1343
1344 If the symbol is undefined, or in a different section (and therefore moveable
1345 to an arbitrarily large distance), the largest available relaxation mode is
1346 specified, @code{fix_new} is called to produce the relocation record,
1347 @code{fr_fix} is increased to include the relocated field (remember, this
1348 storage was allocated when @code{frag_var} was called), and @code{frag_wane} is
1349 called to convert the frag to an @code{rs_fill} frag with no variant part.
1350 Sometimes changing addressing modes may also require rewriting the instruction.
1351 It can be accessed via @code{fr_opcode} or @code{fr_fix}.
1352
1353 Sometimes @code{fr_var} is increased instead, and @code{frag_wane} is not
1354 called. I'm not sure, but I think this is to keep @code{fr_fix} referring to
1355 an earlier byte, and @code{fr_subtype} set to @code{rs_machine_dependent} so
1356 that @code{md_convert_frag} will get called.
1357
1358 @node General relaxing
1359 @subsection General relaxing
1360
1361 If using a simple table is not suitable, you may implement arbitrarily complex
1362 relaxation semantics yourself. For example, the MIPS backend uses this to emit
1363 different instruction sequences depending upon the size of the symbol being
1364 accessed.
1365
1366 When you assemble an instruction that may need relaxation, you should allocate
1367 a frag using @code{frag_var} or @code{frag_variant} with a type of
1368 @code{rs_machine_dependent}. You should store some sort of information in the
1369 @code{fr_subtype} field so that you can figure out what to do with the frag
1370 later.
1371
1372 When GAS reaches the end of the input file, it will look through the frags and
1373 work out their final sizes.
1374
1375 GAS will first call @code{md_estimate_size_before_relax} on each
1376 @code{rs_machine_dependent} frag. This function must return an estimated size
1377 for the frag.
1378
1379 GAS will then loop over the frags, calling @code{md_relax_frag} on each
1380 @code{rs_machine_dependent} frag. This function should return the change in
1381 size of the frag. GAS will keep looping over the frags until none of the frags
1382 changes size.
1383
1384 @node Broken words
1385 @section Broken words
1386 @cindex internals, broken words
1387 @cindex broken words
1388
1389 Some compilers, including GCC, will sometimes emit switch tables specifying
1390 16-bit @code{.word} displacements to branch targets, and branch instructions
1391 that load entries from that table to compute the target address. If this is
1392 done on a 32-bit machine, there is a chance (at least with really large
1393 functions) that the displacement will not fit in 16 bits. The assembler
1394 handles this using a concept called @dfn{broken words}. This idea is well
1395 named, since there is an implied promise that the 16-bit field will in fact
1396 hold the specified displacement.
1397
1398 If broken word processing is enabled, and a situation like this is encountered,
1399 the assembler will insert a jump instruction into the instruction stream, close
1400 enough to be reached with the 16-bit displacement. This jump instruction will
1401 transfer to the real desired target address. Thus, as long as the @code{.word}
1402 value really is used as a displacement to compute an address to jump to, the
1403 net effect will be correct (minus a very small efficiency cost). If
1404 @code{.word} directives with label differences for values are used for other
1405 purposes, however, things may not work properly. For targets which use broken
1406 words, the @samp{-K} option will warn when a broken word is discovered.
1407
1408 The broken word code is turned off by the @code{WORKING_DOT_WORD} macro. It
1409 isn't needed if @code{.word} emits a value large enough to contain an address
1410 (or, more correctly, any possible difference between two addresses).
1411
1412 @node Internal functions
1413 @section Internal functions
1414
1415 This section describes basic internal functions used by GAS.
1416
1417 @menu
1418 * Warning and error messages:: Warning and error messages
1419 * Hash tables:: Hash tables
1420 @end menu
1421
1422 @node Warning and error messages
1423 @subsection Warning and error messages
1424
1425 @deftypefun @{@} int had_warnings (void)
1426 @deftypefunx @{@} int had_errors (void)
1427 Returns non-zero if any warnings or errors, respectively, have been printed
1428 during this invocation.
1429 @end deftypefun
1430
1431 @deftypefun @{@} void as_perror (const char *@var{gripe}, const char *@var{filename})
1432 Displays a BFD or system error, then clears the error status.
1433 @end deftypefun
1434
1435 @deftypefun @{@} void as_tsktsk (const char *@var{format}, ...)
1436 @deftypefunx @{@} void as_warn (const char *@var{format}, ...)
1437 @deftypefunx @{@} void as_bad (const char *@var{format}, ...)
1438 @deftypefunx @{@} void as_fatal (const char *@var{format}, ...)
1439 These functions display messages about something amiss with the input file, or
1440 internal problems in the assembler itself. The current file name and line
1441 number are printed, followed by the supplied message, formatted using
1442 @code{vfprintf}, and a final newline.
1443
1444 An error indicated by @code{as_bad} will result in a non-zero exit status when
1445 the assembler has finished. Calling @code{as_fatal} will result in immediate
1446 termination of the assembler process.
1447 @end deftypefun
1448
1449 @deftypefun @{@} void as_warn_where (char *@var{file}, unsigned int @var{line}, const char *@var{format}, ...)
1450 @deftypefunx @{@} void as_bad_where (char *@var{file}, unsigned int @var{line}, const char *@var{format}, ...)
1451 These variants permit specification of the file name and line number, and are
1452 used when problems are detected when reprocessing information saved away when
1453 processing some earlier part of the file. For example, fixups are processed
1454 after all input has been read, but messages about fixups should refer to the
1455 original filename and line number that they are applicable to.
1456 @end deftypefun
1457
1458 @deftypefun @{@} void fprint_value (FILE *@var{file}, valueT @var{val})
1459 @deftypefunx @{@} void sprint_value (char *@var{buf}, valueT @var{val})
1460 These functions are helpful for converting a @code{valueT} value into printable
1461 format, in case it's wider than modes that @code{*printf} can handle. If the
1462 type is narrow enough, a decimal number will be produced; otherwise, it will be
1463 in hexadecimal. The value itself is not examined to make this determination.
1464 @end deftypefun
1465
1466 @node Hash tables
1467 @subsection Hash tables
1468 @cindex hash tables
1469
1470 @deftypefun @{@} @{struct hash_control *@} hash_new (void)
1471 Creates the hash table control structure.
1472 @end deftypefun
1473
1474 @deftypefun @{@} void hash_die (struct hash_control *)
1475 Destroy a hash table.
1476 @end deftypefun
1477
1478 @deftypefun @{@} PTR hash_delete (struct hash_control *, const char *)
1479 Deletes entry from the hash table, returns the value it had.
1480 @end deftypefun
1481
1482 @deftypefun @{@} PTR hash_replace (struct hash_control *, const char *, PTR)
1483 Updates the value for an entry already in the table, returning the old value.
1484 If no entry was found, just returns NULL.
1485 @end deftypefun
1486
1487 @deftypefun @{@} @{const char *@} hash_insert (struct hash_control *, const char *, PTR)
1488 Inserting a value already in the table is an error.
1489 Returns an error message or NULL.
1490 @end deftypefun
1491
1492 @deftypefun @{@} @{const char *@} hash_jam (struct hash_control *, const char *, PTR)
1493 Inserts if the value isn't already present, updates it if it is.
1494 @end deftypefun
1495
1496 @node Test suite
1497 @section Test suite
1498 @cindex test suite
1499
1500 The test suite is kind of lame for most processors. Often it only checks to
1501 see if a couple of files can be assembled without the assembler reporting any
1502 errors. For more complete testing, write a test which either examines the
1503 assembler listing, or runs @code{objdump} and examines its output. For the
1504 latter, the TCL procedure @code{run_dump_test} may come in handy. It takes the
1505 base name of a file, and looks for @file{@var{file}.d}. This file should
1506 contain as its initial lines a set of variable settings in @samp{#} comments,
1507 in the form:
1508
1509 @example
1510 #@var{varname}: @var{value}
1511 @end example
1512
1513 The @var{varname} may be @code{objdump}, @code{nm}, or @code{as}, in which case
1514 it specifies the options to be passed to the specified programs. Exactly one
1515 of @code{objdump} or @code{nm} must be specified, as that also specifies which
1516 program to run after the assembler has finished. If @var{varname} is
1517 @code{source}, it specifies the name of the source file; otherwise,
1518 @file{@var{file}.s} is used. If @var{varname} is @code{name}, it specifies the
1519 name of the test to be used in the @code{pass} or @code{fail} messages.
1520
1521 The non-commented parts of the file are interpreted as regular expressions, one
1522 per line. Blank lines in the @code{objdump} or @code{nm} output are skipped,
1523 as are blank lines in the @code{.d} file; the other lines are tested to see if
1524 the regular expression matches the program output. If it does not, the test
1525 fails.
1526
1527 Note that this means the tests must be modified if the @code{objdump} output
1528 style is changed.
1529
1530 @bye
1531 @c Local Variables:
1532 @c fill-column: 79
1533 @c End:
This page took 0.063543 seconds and 5 git commands to generate.