📄 bfdint.texi
字号:
The new linker, when using the same object file format for all inputfiles and the output file, does not convert relocations into@samp{arelent} structures, so it can not use@samp{bfd_perform_relocation} at all. Instead, users of the new linkerare expected to write a @samp{relocate_section} function which willhandle relocations in a target specific fashion.There are two helper functions for target specific relocation:@samp{_bfd_final_link_relocate} and @samp{_bfd_relocate_contents}.These functions use a howto structure, but they @emph{do not} use the@samp{special_function} field. Since the functions are normally calledfrom target specific code, the @samp{special_function} field addslittle; any relocations which require special handling can be handledwithout calling those functions.So, if you want to add a new target, or add a new relocation to anexisting target, you need to do the following:@itemize @bullet@itemMake sure you clearly understand what the contents of the section shouldlook like after assembly, after a relocatable link, and after a finallink. Make sure you clearly understand the operations the linker mustperform during a relocatable link and during a final link.@itemWrite a howto structure for the relocation. The howto structure isflexible enough to represent any relocation which should be handled bysetting a contiguous bitfield in the destination to the value of asymbol, possibly with an addend, possibly adding the symbol value to thevalue already present in the destination.@itemChange the assembler to generate your relocation. The assembler willcall @samp{bfd_install_relocation}, so your howto structure has to beable to handle that. You may need to set the @samp{special_function}field to handle assembly correctly. Be careful to ensure that any codeyou write to handle the assembler will also work correctly when doing arelocatable link. For example, see @samp{bfd_elf_generic_reloc}.@itemTest the assembler. Consider the cases of relocation against anundefined symbol, a common symbol, a symbol defined in the object filein the same section, and a symbol defined in the object file in adifferent section. These cases may not all be applicable for yourreloc.@itemIf your target uses the new linker, which is recommended, add anyrequired handling to the target specific relocation function. In simplecases this will just involve a call to @samp{_bfd_final_link_relocate}or @samp{_bfd_relocate_contents}, depending upon the definition of therelocation and whether the link is relocatable or not.@itemTest the linker. Test the case of a final link. If the relocation canoverflow, use a linker script to force an overflow and make sure theerror is reported correctly. Test a relocatable link, whether thesymbol is defined or undefined in the relocatable output. For both thefinal and relocatable link, test the case when the symbol is a commonsymbol, when the symbol looked like a common symbol but became a definedsymbol, when the symbol is defined in a different object file, and whenthe symbol is defined in the same object file.@itemIn order for linking to another object file format, such as S-records,to work correctly, @samp{bfd_perform_relocation} has to do the rightthing for the relocation. You may need to set the@samp{special_function} field to handle this correctly. Test this bydoing a link in which the output object file format is S-records.@itemUsing the linker to generate relocatable output in a different objectfile format is impossible in the general case, so you generally don'thave to worry about that. The GNU linker makes sure to stop that fromhappening when an input file in a different format has relocations.Linking input files of different object file formats together is quiteunusual, but if you're really dedicated you may want to consider testingthis case, both when the output object file format is the same as yourformat, and when it is different.@end itemize@node BFD relocation codes@subsection BFD relocation codesBFD has another way of describing relocations besides the howtostructures described above: the enum @samp{bfd_reloc_code_real_type}.Every known relocation type can be described as a value in thisenumeration. The enumeration contains many target specific relocations,but where two or more targets have the same relocation, a single code isused. For example, the single value @samp{BFD_RELOC_32} is used for allsimple 32 bit relocation types.The main purpose of this relocation code is to give the assembler somemechanism to create @samp{arelent} structures. In order for theassembler to create an @samp{arelent} structure, it has to be able toobtain a howto structure. The function @samp{bfd_reloc_type_lookup},which simply calls the target vector entry point@samp{reloc_type_lookup}, takes a relocation code and returns a howtostructure.The function @samp{bfd_get_reloc_code_name} returns the name of arelocation code. This is mainly used in error messages.Using both howto structures and relocation codes can be somewhatconfusing. There are many processor specific relocation codes.However, the relocation is only fully defined by the howto structure.The same relocation code will map to different howto structures indifferent object file formats. For example, the addend handling may bedifferent.Most of the relocation codes are not really general. The assembler cannot use them without already understanding what sorts of relocations canbe used for a particular target. It might be possible to replace therelocation codes with something simpler.@node BFD relocation future@subsection BFD relocation futureClearly the current BFD relocation support is in bad shape. Awholescale rewrite would be very difficult, because it would requirethorough testing of every BFD target. So some sort of incrementalchange is required.My vague thoughts on this would involve defining a new, clearly defined,howto structure. Some mechanism would be used to determine which typeof howto structure was being used by a particular format.The new howto structure would clearly define the relocation behaviour inthe case of an assembly, a relocatable link, and a final link. Atleast one special function would be defined as an escape, and it mightmake sense to define more.One or more generic functions similar to @samp{bfd_perform_relocation}would be written to handle the new howto structure.This should make it possible to write a generic version of the relocatesection functions used by the new linker. The target specific codewould provide some mechanism (a function pointer or an initialconversion) to convert target specific relocations into howtostructures.Ideally it would be possible to use this generic relocate sectionfunction for the generic linker as well. That is, it would replace the@samp{bfd_generic_get_relocated_section_contents} function which iscurrently normally used.For the special case of ELF dynamic linking, more consideration needs tobe given to writing ELF specific but ELF target generic code to handlespecial relocation types such as GOT and PLT.@node BFD ELF support@section BFD ELF support@cindex elf support in bfd@cindex bfd elf supportThe ELF object file format is defined in two parts: a generic ABI and aprocessor specific supplement. The ELF support in BFD is split in asimilar fashion. The processor specific support is largely kept withina single file. The generic support is provided by several other files.The processor specific support provides a set of function pointers andconstants used by the generic support.@menu* BFD ELF sections and segments:: ELF sections and segments* BFD ELF generic support:: BFD ELF generic support* BFD ELF processor specific support:: BFD ELF processor specific support* BFD ELF core files:: BFD ELF core files* BFD ELF future:: BFD ELF future@end menu@node BFD ELF sections and segments@subsection ELF sections and segmentsThe ELF ABI permits a file to have either sections or segments or both.Relocateable object files conventionally have only sections.Executables conventionally have both. Core files conventionally haveonly program segments.ELF sections are similar to sections in other object file formats: theyhave a name, a VMA, file contents, flags, and other miscellaneousinformation. ELF relocations are stored in sections of a particulartype; BFD automatically converts these sections into internal relocationinformation.ELF program segments are intended for fast interpretation by a systemloader. They have a type, a VMA, an LMA, file contents, and a couple ofother fields. When an ELF executable is run on a Unix system, thesystem loader will examine the program segments to decide how to loadit. The loader will ignore the section information. Loadable programsegments (type @samp{PT_LOAD}) are directly loaded into memory. Otherprogram segments are interpreted by the loader, and generally providedynamic linking information.When an ELF file has both program segments and sections, an ELF programsegment may encompass one or more ELF sections, in the sense that theportion of the file which corresponds to the program segment may includethe portions of the file corresponding to one or more sections. Whenthere is more than one section in a loadable program segment, therelative positions of the section contents in the file must correspondto the relative positions they should hold when the program segment isloaded. This requirement should be obvious if you consider that thesystem loader will load an entire program segment at a time.On a system which supports dynamic paging, such as any native Unixsystem, the contents of a loadable program segment must be at the sameoffset in the file as in memory, modulo the memory page size used on thesystem. This is because the system loader will map the file into memorystarting at the start of a page. The system loader can easily remapentire pages to the correct load address. However, if the contents ofthe file were not correctly aligned within the page, the system loaderwould have to shift the contents around within the page, which is tooexpensive. For example, if the LMA of a loadable program segment is@samp{0x40080} and the page size is @samp{0x1000}, then the position ofthe segment contents within the file must equal @samp{0x80} modulo@samp{0x1000}.BFD has only a single set of sections. It does not provide any genericway to examine both sections and segments. When BFD is used to open anobject file or executable, the BFD sections will represent ELF sections.When BFD is used to open a core file, the BFD sections will representELF program segments.When BFD is used to examine an object file or executable, any programsegments will be read to set the LMA of the sections. This is becauseELF sections only have a VMA, while ELF program segments have both a VMAand an LMA. Any program segments will be copied by the@samp{copy_private} entry points. They will be printed by the@samp{print_private} entry point. Otherwise, the program segments areignored. In particular, programs which use BFD currently have no directaccess to the program segments.When BFD is used to create an executable, the program segments will becreated automatically based on the section information. This is done inthe function @samp{assign_file_positions_for_segments} in @file{elf.c}.This function has been tweaked many times, and probably still hasproblems that arise in particular cases.There is a hook which may be used to explicitly define the programsegments when creating an executable: the @samp{bfd_record_phdr}function in @file{bfd.c}. If this function is called, BFD will notcreate program segments itself, but will only create the programsegments specified by the caller. The linker uses this function toimplement the @samp{PHDRS} linker script command.@node BFD ELF generic support@subsection BFD ELF generic supportIn general, functions which do not read external data from the ELF fileare found in @file{elf.c}. They operate on the internal forms of theELF structures, which are defined in @file{include/elf/internal.h}. Theinternal structures are defined in terms of @samp{bfd_vma}, and so maybe used for both 32 bit and 64 bit ELF targets.The file @file{elfcode.h} contains functions which operate on theexternal data. @file{elfcode.h} is compiled twice, once via@file{elf32.c} with @samp{ARCH_SIZE} defined as @samp{32}, and once via@file{elf64.c} with @samp{ARCH_SIZE} defined as @samp{64}.@file{elfcode.h} includes functions to swap the ELF structures in andout of external form, as well as a few more complex functions.Linker support is found in @file{elflink.c}. Thelinker support is only used if the processor specific file defines@samp{elf_backend_relocate_section}, which is required to relocate thesection contents. If that macro is not defined, the generic linker codeis used, and relocations are handled via @samp{bfd_perform_relocation}.The core file support is in @file{elfcore.h}, which is compiled twice,for both 32 and 64 bit support. The more interesting cases of core filesupport only work on a native system which has the @file{sys/procfs.h}header file. Without that file, the core file support does little morethan read the ELF program segments as BFD sections.The BFD internal header file @file{elf-bfd.h} is used for communicationamong these files and the processor specific files.The default entries for the BFD ELF target vector are found mainly in@file{elf.c}. Some functions are found in @file{elfcode.h}.The processor specific files may override particular entries in thetarget vector, but most do not, with one exception: the@samp{bfd_reloc_type_lookup} entry point is always processor specific.@node BFD ELF processor specific support@subsection BFD ELF processor specific supportBy convention, the processor specific support for a particular processorwill be found in @file{elf@var{nn}-@var{cpu}.c}, where @var{nn} iseither 32 or 64, and @var{cpu} is the name of the processor.@menu* BFD ELF processor required:: Required processor specific support* BFD ELF processor linker:: Processor specific linker support* BFD ELF processor other:: Other processor specific support options@end menu@node BFD ELF processor required@subsubsection Required processor specific supportWhen writing a @fil
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -