📄 internals.texi
字号:
\input texinfo@c Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,@c 2001@c Free Software Foundation, Inc.@setfilename internals.info@node Top@top Assembler Internals@raisesections@cindex internalsThis chapter describes the internals of the assembler. It is incomplete, butit may help a bit.This chapter is not updated regularly, and it may be out of date.@menu* GAS versions:: GAS versions* Data types:: Data types* GAS processing:: What GAS does when it runs* Porting GAS:: Porting GAS* Relaxation:: Relaxation* Broken words:: Broken words* Internal functions:: Internal functions* Test suite:: Test suite@end menu@node GAS versions@section GAS versionsGAS has acquired layers of code over time. The original GAS only supported thea.out object file format, with three sections. Support for multiple sectionshas been added in two different ways.The preferred approach is to use the version of GAS created when the symbol@code{BFD_ASSEMBLER} is defined. The other versions of GAS are documented forhistorical purposes, and to help anybody who has to debug code written forthem.The type @code{segT} is used to represent a section in code which must workwith all versions of GAS.@menu* Original GAS:: Original GAS version* MANY_SEGMENTS:: MANY_SEGMENTS gas version* BFD_ASSEMBLER:: BFD_ASSEMBLER gas version@end menu@node Original GAS@subsection Original GASThe original GAS only supported the a.out object file format with threesections: @samp{.text}, @samp{.data}, and @samp{.bss}. This is the version ofGAS that is compiled if neither @code{BFD_ASSEMBLER} nor @code{MANY_SEGMENTS}is defined. This version of GAS is still used for the m68k-aout target, andperhaps others.This version of GAS should not be used for any new development.There is still code that is specific to this version of GAS, notably in@file{write.c}. There is no way for this code to loop through all thesections; it simply looks at global variables like @code{text_frag_root} and@code{data_frag_root}.The type @code{segT} is an enum.@node MANY_SEGMENTS@subsection MANY_SEGMENTS gas version@cindex MANY_SEGMENTSThe @code{MANY_SEGMENTS} version of gas is only used for COFF. It uses the BFDlibrary, but it writes out all the data itself using @code{bfd_write}. Thisversion of gas supports up to 40 normal sections. The section names are storedin the @code{seg_name} array. Other information is stored in the@code{segment_info} array.The type @code{segT} is an enum. Code that wants to examine all the sectionscan use a @code{segT} variable as loop index from @code{SEG_E0} up to but notincluding @code{SEG_UNKNOWN}.Most of the code specific to this version of GAS is in the file@file{config/obj-coff.c}, in the portion of that file that is compiled when@code{BFD_ASSEMBLER} is not defined.This version of GAS is still used for several COFF targets.@node BFD_ASSEMBLER@subsection BFD_ASSEMBLER gas version@cindex BFD_ASSEMBLERThe preferred version of GAS is the @code{BFD_ASSEMBLER} version. In thisversion of GAS, the output file is a normal BFD, and the BFD routines are usedto generate the output.@code{BFD_ASSEMBLER} will automatically be used for certain targets, includingthose that use the ELF, ECOFF, and SOM object file formats, and also all Alpha,MIPS, PowerPC, and SPARC targets. You can force the use of@code{BFD_ASSEMBLER} for other targets with the configure option@samp{--enable-bfd-assembler}; however, it has not been tested for manytargets, and can not be assumed to work.@node Data types@section Data types@cindex internals, data typesThis section describes some fundamental GAS data types.@menu* Symbols:: The symbolS structure* Expressions:: The expressionS structure* Fixups:: The fixS structure* Frags:: The fragS structure@end menu@node Symbols@subsection Symbols@cindex internals, symbols@cindex symbols, internal@cindex symbolS structureThe definition for the symbol structure, @code{symbolS}, is located in@file{struc-symbol.h}.In general, the fields of this structure may not be referred to directly.Instead, you must use one of the accessor functions defined in @file{symbol.h}.These accessor functions should work for any GAS version.Symbol structures contain the following fields:@table @code@item sy_valueThis is an @code{expressionS} that describes the value of the symbol. It mightrefer to one or more other symbols; if so, its true value may not be knownuntil @code{resolve_symbol_value} is called in @code{write_object_file}.The expression is often simply a constant. Before @code{resolve_symbol_value}is called, the value is the offset from the frag (@pxref{Frags}). Afterward,the frag address has been added in.@item sy_resolvedThis field is non-zero if the symbol's value has been completely resolved. Itis used during the final pass over the symbol table.@item sy_resolvingThis field is used to detect loops while resolving the symbol's value.@item sy_used_in_relocThis field is non-zero if the symbol is used by a relocation entry. If a localsymbol is used in a relocation entry, it must be possible to redirect thoserelocations to other symbols, or this symbol cannot be removed from the finalsymbol list.@item sy_next@itemx sy_previousThese pointers to other @code{symbolS} structures describe a singly or doublylinked list. (If @code{SYMBOLS_NEED_BACKPOINTERS} is not defined, the@code{sy_previous} field will be omitted; @code{SYMBOLS_NEED_BACKPOINTERS} isalways defined if @code{BFD_ASSEMBLER}.) These fields should be accessed withthe @code{symbol_next} and @code{symbol_previous} macros.@item sy_fragThis points to the frag (@pxref{Frags}) that this symbol is attached to.@item sy_usedWhether the symbol is used as an operand or in an expression. Note: Not all ofthe backends keep this information accurate; backends which use this bit areresponsible for setting it when a symbol is used in backend routines.@item sy_mri_commonWhether the symbol is an MRI common symbol created by the @code{COMMON}pseudo-op when assembling in MRI mode.@item bsymIf @code{BFD_ASSEMBLER} is defined, this points to the BFD @code{asymbol} thatwill be used in writing the object file.@item sy_name_offset(Only used if @code{BFD_ASSEMBLER} is not defined.) This is the position ofthe symbol's name in the string table of the object file. On some formats,this will start at position 4, with position 0 reserved for unnamed symbols.This field is not used until @code{write_object_file} is called.@item sy_symbol(Only used if @code{BFD_ASSEMBLER} is not defined.) This is theformat-specific symbol structure, as it would be written into the object file.@item sy_number(Only used if @code{BFD_ASSEMBLER} is not defined.) This is a 24-bit symbolnumber, for use in constructing relocation table entries.@item sy_objThis format-specific data is of type @code{OBJ_SYMFIELD_TYPE}. If no macro bythat name is defined in @file{obj-format.h}, this field is not defined.@item sy_tcThis processor-specific data is of type @code{TC_SYMFIELD_TYPE}. If no macroby that name is defined in @file{targ-cpu.h}, this field is not defined.@end tableHere is a description of the accessor functions. These should be used ratherthan referring to the fields of @code{symbolS} directly.@table @code@item S_SET_VALUE@cindex S_SET_VALUESet the symbol's value.@item S_GET_VALUE@cindex S_GET_VALUEGet the symbol's value. This will cause @code{resolve_symbol_value} to becalled if necessary, so @code{S_GET_VALUE} should only be called when it issafe to resolve symbols (i.e., after the entire input file has been read andall symbols have been defined).@item S_SET_SEGMENT@cindex S_SET_SEGMENTSet the section of the symbol.@item S_GET_SEGMENT@cindex S_GET_SEGMENTGet the symbol's section.@item S_GET_NAME@cindex S_GET_NAMEGet the name of the symbol.@item S_SET_NAME@cindex S_SET_NAMESet the name of the symbol.@item S_IS_EXTERNAL@cindex S_IS_EXTERNALReturn non-zero if the symbol is externally visible.@item S_IS_EXTERN@cindex S_IS_EXTERNA synonym for @code{S_IS_EXTERNAL}. Don't use it.@item S_IS_WEAK@cindex S_IS_WEAKReturn non-zero if the symbol is weak.@item S_IS_COMMON@cindex S_IS_COMMONReturn non-zero if this is a common symbol. Common symbols are sometimesrepresented as undefined symbols with a value, in which case this function willnot be reliable.@item S_IS_DEFINED@cindex S_IS_DEFINEDReturn non-zero if this symbol is defined. This function is not reliable whencalled on a common symbol.@item S_IS_DEBUG@cindex S_IS_DEBUGReturn non-zero if this is a debugging symbol.@item S_IS_LOCAL@cindex S_IS_LOCALReturn non-zero if this is a local assembler symbol which should not beincluded in the final symbol table. Note that this is not the opposite of@code{S_IS_EXTERNAL}. The @samp{-L} assembler option affects the return valueof this function.@item S_SET_EXTERNAL@cindex S_SET_EXTERNALMark the symbol as externally visible.@item S_CLEAR_EXTERNAL@cindex S_CLEAR_EXTERNALMark the symbol as not externally visible.@item S_SET_WEAK@cindex S_SET_WEAKMark the symbol as weak.@item S_GET_TYPE@item S_GET_DESC@item S_GET_OTHER@cindex S_GET_TYPE@cindex S_GET_DESC@cindex S_GET_OTHERGet the @code{type}, @code{desc}, and @code{other} fields of the symbol. Theseare only defined for object file formats for which they make sense (primarilya.out).@item S_SET_TYPE@item S_SET_DESC@item S_SET_OTHER@cindex S_SET_TYPE@cindex S_SET_DESC@cindex S_SET_OTHERSet the @code{type}, @code{desc}, and @code{other} fields of the symbol. Theseare only defined for object file formats for which they make sense (primarilya.out).@item S_GET_SIZE@cindex S_GET_SIZEGet the size of a symbol. This is only defined for object file formats forwhich it makes sense (primarily ELF).@item S_SET_SIZE@cindex S_SET_SIZESet the size of a symbol. This is only defined for object file formats forwhich it makes sense (primarily ELF).@item symbol_get_value_expression@cindex symbol_get_value_expressionGet a pointer to an @code{expressionS} structure which represents the value ofthe symbol as an expression.@item symbol_set_value_expression@cindex symbol_set_value_expressionSet the value of a symbol to an expression.@item symbol_set_frag@cindex symbol_set_fragSet the frag where a symbol is defined.@item symbol_get_frag@cindex symbol_get_fragGet the frag where a symbol is defined.@item symbol_mark_used@cindex symbol_mark_usedMark a symbol as having been used in an expression.@item symbol_clear_used@cindex symbol_clear_usedClear the mark indicating that a symbol was used in an expression.@item symbol_used_p@cindex symbol_used_p
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -