RFC: generating a header using the linker (ASCII, ASCIZ commands)
Nick Clifton
nickc@redhat.com
Tue Feb 14 16:06:47 GMT 2023
Hi Ulf,
>>> Note that if the linker can call an external tool to compute the CRC
>>> based on the extracted text segment, and insert that into the resulting ELF file.
>>> then this is superior to running a utility afterwards.
>>
>> Actually -- there might be a way to do this: A linker plugin. The linker already
>> has the architecture to support plugins, and you could create a new one which would
>> scan the text section, compute a CRC and then insert the value into a header in the
>> linked image...
>>
> OK, tell me more!
Sure. The linker has the ability to run plugins via the use of the "-plugin <name>"
command line option. Currently there are two known plugins for the linker. One is
used to process the dependencies of static archives (ld/libdep_plugin.c in the
binutils sources) and one is used to pass LTO enabled object files back to the compiler
for recompilation (lto-plugin/ in the gcc sources).
The big problem with linker plugins however is the total lack of documentation
on how to write them. Until the day comes when somebody writes a manual the
only documentation is the source code itself. The place to start is the
include/plugin-api.h file which defines the interface between plugins and the
linker. Then take a look at the plugin sources mentioned above and the ld/plugin.c
source file which is the linker's side of the coin.
In essence what a plugin does is provide a selection of callback functions to
the linker. These functions are called at various stages of the link, and can do
pretty much anything. Since plugins are shared objects, they have full access to
the linker's run-time memory space and can muck about as they please. Of course
plugins are expected to behave nicely.
Cheers
Nick
More information about the Binutils
mailing list