📄 internals.texi
字号:
@end table@cindex frchainS structureA chain of frags is built up for each subsection. The data structuredescribing a chain is called a @code{frchainS}, and contains the followingfields:@table @code@item frch_rootPoints to the first frag in the chain. May be NULL if there are no frags inthis chain.@item frch_lastPoints to the last frag in the chain, or NULL if there are none.@item frch_nextNext in the list of @code{frchainS} structures.@item frch_segIndicates the section this frag chain belongs to.@item frch_subsegSubsection (subsegment) number of this frag chain.@item fix_root, fix_tail(Defined only if @code{BFD_ASSEMBLER} is defined). Point to first and last@code{fixS} structures associated with this subsection.@item frch_obstackNot currently used. Intended to be used for frag allocation for thissubsection. This should reduce frag generation caused by switching sections.@item frch_frag_nowThe current frag for this subsegment.@end tableA @code{frchainS} corresponds to a subsection; each section has a list of@code{frchainS} records associated with it. In most cases, only one subsectionof each section is used, so the list will only be one element long, but anyprocessing of frag chains should be prepared to deal with multiple chains persection.After the input files have been completely processed, and no more frags are tobe generated, the frag chains are joined into one per section for furtherprocessing. After this point, it is safe to operate on one chain per section.The assembler always has a current frag, named @code{frag_now}. More space isallocated for the current frag using the @code{frag_more} function; thisreturns a pointer to the amount of requested space. Relaxing is done usingvariant frags allocated by @code{frag_var} or @code{frag_variant}(@pxref{Relaxation}).@node GAS processing@section What GAS does when it runs@cindex internals, overviewThis is a quick look at what an assembler run looks like.@itemize @bullet@itemThe assembler initializes itself by calling various init routines.@itemFor each source file, the @code{read_a_source_file} function reads in the fileand parses it. The global variable @code{input_line_pointer} points to thecurrent text; it is guaranteed to be correct up to the end of the line, but notfarther.@itemFor each line, the assembler passes labels to the @code{colon} function, andisolates the first word. If it looks like a pseudo-op, the word is looked upin the pseudo-op hash table @code{po_hash} and dispatched to a pseudo-oproutine. Otherwise, the target dependent @code{md_assemble} routine is calledto parse the instruction.@itemWhen pseudo-ops or instructions output data, they add it to a frag, calling@code{frag_more} to get space to store it in.@itemPseudo-ops and instructions can also output fixups created by @code{fix_new} or@code{fix_new_exp}.@itemFor certain targets, instructions can create variant frags which are used tostore relaxation information (@pxref{Relaxation}).@itemWhen the input file is finished, the @code{write_object_file} routine iscalled. It assigns addresses to all the frags (@code{relax_segment}), resolvesall the fixups (@code{fixup_segment}), resolves all the symbol values (using@code{resolve_symbol_value}), and finally writes out the file (in the@code{BFD_ASSEMBLER} case, this is done by simply calling @code{bfd_close}).@end itemize@node Porting GAS@section Porting GAS@cindex portingEach GAS target specifies two main things: the CPU file and the object formatfile. Two main switches in the @file{configure.in} file handle this. Thefirst switches on CPU type to set the shell variable @code{cpu_type}. Thesecond switches on the entire target to set the shell variable @code{fmt}.The configure script uses the value of @code{cpu_type} to select two files inthe @file{config} directory: @file{tc-@var{CPU}.c} and @file{tc-@var{CPU}.h}.The configuration process will create a file named @file{targ-cpu.h} in thebuild directory which includes @file{tc-@var{CPU}.h}.The configure script also uses the value of @code{fmt} to select two files:@file{obj-@var{fmt}.c} and @file{obj-@var{fmt}.h}. The configuration processwill create a file named @file{obj-format.h} in the build directory whichincludes @file{obj-@var{fmt}.h}.You can also set the emulation in the configure script by setting the @code{em}variable. Normally the default value of @samp{generic} is fine. Theconfiguration process will create a file named @file{targ-env.h} in the builddirectory which includes @file{te-@var{em}.h}.There is a special case for COFF. For historical reason, the GNU COFFassembler doesn't follow the documented behavior on certain debug symbols forthe compatibility with other COFF assemblers. A port can define@code{STRICTCOFF} in the configure script to make the GNU COFF assemblerto follow the documented behavior.Porting GAS to a new CPU requires writing the @file{tc-@var{CPU}} files.Porting GAS to a new object file format requires writing the@file{obj-@var{fmt}} files. There is sometimes some interaction between thesetwo files, but it is normally minimal.The best approach is, of course, to copy existing files. The documentationbelow assumes that you are looking at existing files to see usage details.These interfaces have grown over time, and have never been carefully thoughtout or designed. Nothing about the interfaces described here is cast in stone.It is possible that they will change from one version of the assembler to thenext. Also, new macros are added all the time as they are needed.@menu* CPU backend:: Writing a CPU backend* Object format backend:: Writing an object format backend* Emulations:: Writing emulation files@end menu@node CPU backend@subsection Writing a CPU backend@cindex CPU backend@cindex @file{tc-@var{CPU}}The CPU backend files are the heart of the assembler. They are the only partsof the assembler which actually know anything about the instruction set of theprocessor.You must define a reasonably small list of macros and functions in the CPUbackend files. You may define a large number of additional macros in the CPUbackend files, not all of which are documented here. You must, of course,define macros in the @file{.h} file, which is included by every assemblersource file. You may define the functions as macros in the @file{.h} file, oras functions in the @file{.c} file.@table @code@item TC_@var{CPU}@cindex TC_@var{CPU}By convention, you should define this macro in the @file{.h} file. Forexample, @file{tc-m68k.h} defines @code{TC_M68K}. You might have to use thisif it is necessary to add CPU specific code to the object format file.@item TARGET_FORMATThis macro is the BFD target name to use when creating the output file. Thiswill normally depend upon the @code{OBJ_@var{FMT}} macro.@item TARGET_ARCHThis macro is the BFD architecture to pass to @code{bfd_set_arch_mach}.@item TARGET_MACHThis macro is the BFD machine number to pass to @code{bfd_set_arch_mach}. Ifit is not defined, GAS will use 0.@item TARGET_BYTES_BIG_ENDIANYou should define this macro to be non-zero if the target is big endian, andzero if the target is little endian.@item md_shortopts@itemx md_longopts@itemx md_longopts_size@itemx md_parse_option@itemx md_show_usage@cindex md_shortopts@cindex md_longopts@cindex md_longopts_size@cindex md_parse_option@cindex md_show_usageGAS uses these variables and functions during option processing.@code{md_shortopts} is a @code{const char *} which GAS adds to the machineindependent string passed to @code{getopt}. @code{md_longopts} is a@code{struct option []} which GAS adds to the machine independent long optionspassed to @code{getopt}; you may use @code{OPTION_MD_BASE}, defined in@file{as.h}, as the start of a set of long option indices, if necessary.@code{md_longopts_size} is a @code{size_t} holding the size @code{md_longopts}.GAS will call @code{md_parse_option} whenever @code{getopt} returns anunrecognized code, presumably indicating a special code value which appears in@code{md_longopts}. GAS will call @code{md_show_usage} when a usage message isprinted; it should print a description of the machine specific options.@item md_begin@cindex md_beginGAS will call this function at the start of the assembly, after the commandline arguments have been parsed and all the machine independent initializationshave been completed.@item md_cleanup@cindex md_cleanupIf you define this macro, GAS will call it at the end of each input file.@item md_assemble@cindex md_assembleGAS will call this function for each input line which does not contain apseudo-op. The argument is a null terminated string. The function shouldassemble the string as an instruction with operands. Normally@code{md_assemble} will do this by calling @code{frag_more} and writing outsome bytes (@pxref{Frags}). @code{md_assemble} will call @code{fix_new} tocreate fixups as needed (@pxref{Fixups}). Targets which need to do specialpurpose relaxation will call @code{frag_var}.@item md_pseudo_table@cindex md_pseudo_tableThis is a const array of type @code{pseudo_typeS}. It is a mapping frompseudo-op names to functions. You should use this table to implementpseudo-ops which are specific to the CPU.@item tc_conditional_pseudoop@cindex tc_conditional_pseudoopIf this macro is defined, GAS will call it with a @code{pseudo_typeS} argument.It should return non-zero if the pseudo-op is a conditional which controlswhether code is assembled, such as @samp{.if}. GAS knows about the normalconditional pseudo-ops, and you should normally not have to define this macro.@item comment_chars@cindex comment_charsThis is a null terminated @code{const char} array of characters which start acomment.@item tc_comment_chars@cindex tc_comment_charsIf this macro is defined, GAS will use it instead of @code{comment_chars}.@item tc_symbol_chars@cindex tc_symbol_charsIf this macro is defined, it is a pointer to a null terminated list ofcharacters which may appear in an operand. GAS already assumes that allalphanumberic characters, and @samp{$}, @samp{.}, and @samp{_} may appear in anoperand (see @samp{symbol_chars} in @file{app.c}). This macro may be definedto treat additional characters as appearing in an operand. This affects theway in which GAS removes whitespace before passing the string to@samp{md_assemble}.@item line_comment_chars@cindex line_comment_charsThis is a null terminated @code{const char} array of characters which start acomment when they appear at the start of a line.@item line_separator_chars@cindex line_separator_charsThis is a null terminated @code{const char} array of characters which separatelines (null and newline are such characters by default, and need not belisted in this array). Note that line_separator_chars do not separate linesif found in a comment, such as after a character in line_comment_chars orcomment_chars.@item EXP_CHARS@cindex EXP_CHARSThis is a null terminated @code{const char} array of characters which may beused as the exponent character in a floating point number. This is normally@code{"eE"}.@item FLT_CHARS@cindex FLT_CHARSThis is a null terminated @code{const char} array of characters which may beused to indicate a floating point constant. A zero followed by one of thesecharacters is assumed to be followed by a floating point number; thus theyoperate the way that @code{0x} is used to indicate a hexadecimal constant.Usually this includes @samp{r} and @samp{f}.@item LEX_AT@cindex LEX_ATYou may define this macro to the lexical type of the @kbd{@@} character. Thedefault is zero.Lexical types are a combination of @code{LEX_NAME} and @code{LEX_BEGIN_NAME},both defined in @file{read.h}. @code{LEX_NAME} indicates that the charactermay appear in a name. @code{LEX_BEGIN_NAME} indicates that the character mayappear at the beginning of a name.@item LEX_BR@cindex LEX_BRYou may define this macro to the lexical type of the brace characters @kbd{@{},@kbd{@}}, @kbd{[}, and @kbd{]}. The default value is zero.@item LEX_PCT@cindex LEX_PCTYou may define this macro to the lexical type of the @kbd{%} character. Thedefault value is zero.@item LEX_QM@cindex LEX_QMYou may define this macro to the lexical type of the @kbd{?} character. Thedefault value it zero.@item LEX_DOLLAR@cindex LEX_DOLLARYou may define this macro to the lexical type of the @kbd{$} character. Thedefault value is @code{LEX_NAME | LEX_BEGIN_NAME}.@item NUMBERS_WITH_SUFFIX@cindex NUMBERS_WITH_SUFFIXWhen this macro is defined to be non-zero, the parser allows the radix of aconstant to be indicated with a suffix. Valid suffixes are binary (B),octal (Q), and hexadecimal (H). Case is not significant.@item SINGLE_QUOTE_STRINGS@cindex SINGLE_QUOTE_STRINGSIf you define this macro, GAS will treat single quotes as string delimiters.Normally only double quotes are accepted as string delimiters.@item NO_STRING_ESCAPES@cindex NO_STRING_ESCAPESIf you define this macro, GAS will not permit escape sequences in a string.@item ONLY_STANDARD_ESCAPES@cindex ONLY_STANDARD_ESCAPESIf you define this macro, GAS will warn about the use of nonstandard escapesequences in a string.@item md_start_line_hook@cindex md_start_line_hookIf you define this macro, GAS will call it at the start of each line.@item LABELS_WITHOUT_COLONS@cindex LABELS_WITHOUT_COLONSIf you define this macro, GAS will assume that any text at the start of a line
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -