📄 internals.texi
字号:
@item LISTING_HEADERA string to use on the header line of a listing. The default value is simply@code{"GAS LISTING"}.@item LISTING_WORD_SIZEThe number of bytes to put into a word in a listing. This affects the way thebytes are clumped together in the listing. For example, a value of 2 mightprint @samp{1234 5678} where a value of 1 would print @samp{12 34 56 78}. Thedefault value is 4.@item LISTING_LHS_WIDTHThe number of words of data to print on the first line of a listing for aparticular source line, where each word is @code{LISTING_WORD_SIZE} bytes. Thedefault value is 1.@item LISTING_LHS_WIDTH_SECONDLike @code{LISTING_LHS_WIDTH}, but applying to the second and subsequent lineof the data printed for a particular source line. The default value is 1.@item LISTING_LHS_CONT_LINESThe maximum number of continuation lines to print in a listing for a particularsource line. The default value is 4.@item LISTING_RHS_WIDTHThe maximum number of characters to print from one line of the input file. Thedefault value is 100.@item TC_COFF_SECTION_DEFAULT_ATTRIBUTES@cindex TC_COFF_SECTION_DEFAULT_ATTRIBUTESThe COFF @code{.section} directive will use the value of this macro to seta new section's attributes when a directive has no valid flags or when theflag is @code{w}. The default value of the macro is @code{SEC_LOAD | SEC_DATA}.@end table@node Object format backend@subsection Writing an object format backend@cindex object format backend@cindex @file{obj-@var{fmt}}As with the CPU backend, the object format backend must define a few things,and may define some other things. The interface to the object format backendis generally simpler; most of the support for an object file format consists ofdefining a number of pseudo-ops.The object format @file{.h} file must include @file{targ-cpu.h}.This section will only define the @code{BFD_ASSEMBLER} version of GAS. It isimpossible to support a new object file format using any other version anyhow,as the original GAS version only supports a.out, and the @code{MANY_SEGMENTS}GAS version only supports COFF.@table @code@item OBJ_@var{format}@cindex OBJ_@var{format}By convention, you should define this macro in the @file{.h} file. Forexample, @file{obj-elf.h} defines @code{OBJ_ELF}. You might have to use thisif it is necessary to add object file format specific code to the CPU file.@item obj_beginIf you define this macro, GAS will call it at the start of the assembly, afterthe command line arguments have been parsed and all the machine independentinitializations have been completed.@item obj_app_file@cindex obj_app_fileIf you define this macro, GAS will invoke it when it sees a @code{.file}pseudo-op or a @samp{#} line as used by the C preprocessor.@item OBJ_COPY_SYMBOL_ATTRIBUTES@cindex OBJ_COPY_SYMBOL_ATTRIBUTESYou should define this macro to copy object format specific information fromone symbol to another. GAS will call it when one symbol is equated toanother.@item obj_fix_adjustable@cindex obj_fix_adjustableYou may define this macro to indicate whether a fixup against a locally definedsymbol should be adjusted to be against the section symbol. It should return anon-zero value if the adjustment is acceptable.@item obj_sec_sym_ok_for_reloc@cindex obj_sec_sym_ok_for_relocYou may define this macro to indicate that it is OK to use a section symbol ina relocateion entry. If it is not, GAS will define a new symbol at the startof a section.@item EMIT_SECTION_SYMBOLS@cindex EMIT_SECTION_SYMBOLSYou should define this macro with a zero value if you do not want to includesection symbols in the output symbol table. The default value for this macrois one.@item obj_adjust_symtab@cindex obj_adjust_symtabIf you define this macro, GAS will invoke it just before setting the symboltable of the output BFD. For example, the COFF support uses this macro togenerate a @code{.file} symbol if none was generated previously.@item SEPARATE_STAB_SECTIONS@cindex SEPARATE_STAB_SECTIONSYou may define this macro to a nonzero value to indicate that stabs should beplaced in separate sections, as in ELF.@item INIT_STAB_SECTION@cindex INIT_STAB_SECTIONYou may define this macro to initialize the stabs section in the output file.@item OBJ_PROCESS_STAB@cindex OBJ_PROCESS_STABYou may define this macro to do specific processing on a stabs entry.@item obj_frob_section@cindex obj_frob_sectionIf you define this macro, GAS will call it for each section at the end of theassembly.@item obj_frob_file_before_adjust@cindex obj_frob_file_before_adjustIf you define this macro, GAS will call it after the symbol values areresolved, but before the fixups have been changed from local symbols to sectionsymbols.@item obj_frob_symbol@cindex obj_frob_symbolIf you define this macro, GAS will call it for each symbol. You can indicatethat the symbol should not be included in the object file by definining thismacro to set its second argument to a non-zero value.@item obj_frob_file@cindex obj_frob_fileIf you define this macro, GAS will call it after the symbol table has beencompleted, but before the relocations have been generated.@item obj_frob_file_after_relocsIf you define this macro, GAS will call it after the relocs have beengenerated.@item SET_SECTION_RELOCS (@var{sec}, @var{relocs}, @var{n})@cindex SET_SECTION_RELOCSIf you define this, it will be called after the relocations have been set forthe section @var{sec}. The list of relocations is in @var{relocs}, and thenumber of relocations is in @var{n}. This is only used with@code{BFD_ASSEMBLER}.@end table@node Emulations@subsection Writing emulation filesNormally you do not have to write an emulation file. You can just use@file{te-generic.h}.If you do write your own emulation file, it must include @file{obj-format.h}.An emulation file will often define @code{TE_@var{EM}}; this may then be usedin other files to change the output.@node Relaxation@section Relaxation@cindex relaxation@dfn{Relaxation} is a generic term used when the size of some instruction ordata depends upon the value of some symbol or other data.GAS knows to relax a particular type of PC relative relocation using a table.You can also define arbitrarily complex forms of relaxation yourself.@menu* Relaxing with a table:: Relaxing with a table* General relaxing:: General relaxing@end menu@node Relaxing with a table@subsection Relaxing with a tableIf you do not define @code{md_relax_frag}, and you do define@code{TC_GENERIC_RELAX_TABLE}, GAS will relax @code{rs_machine_dependent} fragsbased on the frag subtype and the displacement to some specified targetaddress. The basic idea is that several machines have different addressingmodes for instructions that can specify different ranges of values, withsuccessive modes able to access wider ranges, including the entirety of theprevious range. Smaller ranges are assumed to be more desirable (perhaps theinstruction requires one word instead of two or three); if this is not thecase, don't describe the smaller-range, inferior mode.The @code{fr_subtype} field of a frag is an index into a CPU-specificrelaxation table. That table entry indicates the range of values that can bestored, the number of bytes that will have to be added to the frag toaccomodate the addressing mode, and the index of the next entry to examine ifthe value to be stored is outside the range accessible by the currentaddressing mode. The @code{fr_symbol} field of the frag indicates what symbolis to be accessed; the @code{fr_offset} field is added in.If the @code{TC_PCREL_ADJUST} macro is defined, which currently should only happenfor the NS32k family, the @code{TC_PCREL_ADJUST} macro is called on the frag tocompute an adjustment to be made to the displacement.The value fitted by the relaxation code is always assumed to be a displacementfrom the current frag. (More specifically, from @code{fr_fix} bytes into thefrag.)@ignoreThis seems kinda silly. What about fitting small absolute values? I suppose@code{md_assemble} is supposed to take care of that, but if the operand is adifference between symbols, it might not be able to, if the difference was notcomputable yet.@end ignoreThe end of the relaxation sequence is indicated by a ``next'' value of 0. Thismeans that the first entry in the table can't be used.For some configurations, the linker can do relaxing within a section of anobject file. If call instructions of various sizes exist, the linker candetermine which should be used in each instance, when a symbol's value isresolved. In order for the linker to avoid wasting space and having to insertno-op instructions, it must be able to expand or shrink the section contentswhile still preserving intra-section references and meeting alignmentrequirements.For the i960 using b.out format, no expansion is done; instead, each@samp{.align} directive causes extra space to be allocated, enough that whenthe linker is relaxing a section and removing unneeded space, it can discardsome or all of this extra padding and cause the following data to be correctlyaligned.For the H8/300, I think the linker expands calls that can't reach, and doesn'tworry about alignment issues; the cpu probably never needs any significantalignment beyond the instruction size.The relaxation table type contains these fields:@table @code@item long rlx_forwardForward reach, must be non-negative.@item long rlx_backwardBackward reach, must be zero or negative.@item rlx_lengthLength in bytes of this addressing mode.@item rlx_moreIndex of the next-longer relax state, or zero if there is no next relax state.@end tableThe relaxation is done in @code{relax_segment} in @file{write.c}. Thedifference in the length fields between the original mode and the one finallychosen by the relaxing code is taken as the size by which the current frag willbe increased in size. For example, if the initial relaxing mode has a lengthof 2 bytes, and because of the size of the displacement, it gets upgraded to amode with a size of 6 bytes, it is assumed that the frag will grow by 4 bytes.(The initial two bytes should have been part of the fixed portion of the frag,since it is already known that they will be output.) This growth must beeffected by @code{md_convert_frag}; it should increase the @code{fr_fix} fieldby the appropriate size, and fill in the appropriate bytes of the frag.(Enough space for the maximum growth should have been allocated in the call tofrag_var as the second argument.)If relocation records are needed, they should be emitted by@code{md_estimate_size_before_relax}. This function should examine the targetsymbol of the supplied frag and correct the @code{fr_subtype} of the frag ifneeded. When this function is called, if the symbol has not yet been defined,it will not become defined later; however, its value may still change if thesection it is in gets relaxed.Usually, if the symbol is in the same section as the frag (given by the@var{sec} argument), the narrowest likely relaxation mode is stored in@code{fr_subtype}, and that's that.If the symbol is undefined, or in a different section (and therefore moveableto an arbitrarily large distance), the largest available relaxation mode isspecified, @code{fix_new} is called to produce the relocation record,@code{fr_fix} is increased to include the relocated field (remember, thisstorage was allocated when @code{frag_var} was called), and @code{frag_wane} iscalled to convert the frag to an @code{rs_fill} frag with no variant part.Sometimes changing addressing modes may also require rewriting the instruction.It can be accessed via @code{fr_opcode} or @code{fr_fix}.If you generate frags separately for the basic insn opcode and any relaxableoperands, do not call @code{fix_new} thinking you can emit fixups for theopcode field from the relaxable frag. It is not garanteed to be the same frag.If you need to emit fixups for the opcode field from inspection of therelaxable frag, then you need to generate a common frag for both the basicopcode and relaxable fields, or you need to provide the frag for the opcode topass to @code{fix_new}. The latter can be done for example by defining@code{TC_FRAG_TYPE} to include a pointer to it and defining @code{TC_FRAG_INIT}to set the pointer.Sometimes @code{fr_var} is increased instead, and @code{frag_wane} is notcalled. I'm not sure, but I think this is to keep @code{fr_fix} referring toan earlier byte, and @code{fr_subtype} set to @code{rs_machine_dependent} sothat @code{md_convert_frag} will get called.@node General relaxing@subsection General relaxingIf using a simple table is not suitable, you may implement arbitrarily complexrelaxation semantics yourself. For example, the MIPS backend uses this to emitdifferent instruction sequences depending upon the size of the symbol beingaccessed.When you assemble an instruction that may need relaxation, you should allocatea frag using @code{frag_var} or @code{frag_variant} with a type of@code{rs_machine_dependent}. You should store some sort of information in the@code{fr_subtype} field so that you can figure out what to do with the fraglater.When GAS reaches the end of the input file, it will look through the frags andwork out their final sizes.GAS will first call @code{md_estimate_size_before_relax} on each@code{rs_machine_dependent} frag. This function must return an estimated sizefor the frag.GAS will then loop over the frags, calling @code{md_relax_frag} on each@code{rs_machine_dependent} frag. This function should return the change insize of the frag. GAS will keep looping over the frags until none of the fragschanges size.@node Broken words@section Broken words@cindex internals, broken words@cindex broken wordsSome compilers, including GCC, will sometimes emit switch tables specifying16-bit @code{.word} displacements to branch targets, and branch instructionsthat load entries from that table to compute the target address. If this isdone on a 32-bit machine, there is a chance (at least with really largefunctions) that the displacement will not fit in 16 bits. The assemblerhandles this using a concept called @dfn{broken words}. This idea is wellnamed, since there is an implied promise that the 16-bit field will in facthold the specified displacement.If broken word processing is enabled, and a situation like this is encountered,the assembler will insert a jump ins
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -