Download Dynamic linker and debugging/tracing interface for HelenOS
Transcript
5.4
GNU Linker
The GNU Linker basically takes several object files and merges all the sections of
the same name in different object files to a single section. During this process, the
references between object files that are being linked can be resolved, which means
the relocation targets are determined and the relocations are applied.
If the linker is instructed to produce an object file or if dynamic linking is used,
there can still be unresolved references and the output file can contain a non-empty
relocation table.
In reality, the operation of the GNU Linker is controlled by a linker script. The
linker script describes the sections that the output file should have, their contents
and alignment. The output sections can contain fixed values defined in the linker
script. In most cases, however, the linker script simply specifies which input sections
(sections in input files) should go into which output section.
To specify this patterns similar to shell patterns are used in the form filename(section-name). Take the following fragment, for example:
.text : {
*(.text);
*(.rodata*);
}
This linker script fragment specifies that sections from all files (‘*’) having either
the name ‘.text’ or having a name beginning with ‘.rodata’ should be inserted into
the output section ‘.text’.
The linker script can also specify other metadata that should be written into the
output file, such as the entry point, the program headers and it also specifies which
section belongs into which segment.
The linker script to use can be specified with the ‘-T’ option. If no script is
provided as an argument, the linker uses a default one. Linker scripts are highly
architecture-specific. HelenOS provides a linker script for each architecture, one for
the kernel and another one for user-space applications.
5.5
ELF Thread-Local Storage
The document ELF Handling for Thread-Local Storage ([EHfTLS]) defines how
thread-local storage is realized in ELF and how it interacts with dynamic linking.
We will refer to shared libraries and executable files that use dynamic linking
collectively as modules. Dynamic modules are categorized into initial modules that
are loaded at the same time as the executable itself and dynamically-loaded modules
that are loaded at run-time using the dlopen() function.
The compiler puts thread-local uninitialized and initialized variables data to
the .tbss and .tdata sections, respectively. (Instead of putting them in the .bss
and .data sections). When a dynamic module is produced, it contains one .tdata
section and one .tbss section adjacent to it. Together these form the TLS block of
the dynamic module.
One CPU register, the thread register is reserved to hold the pointer tp to the
thread-specific data. It points to a structure called thread control block. The contents of this block are mostly undefined (i.e. the dynamic linker can use it to store
69