This is the mail archive of the
mailing list for the GCC project.
Re: ELF executable & relocatable
- From: Michael Meissner <gcc-mail at the-meissners dot org>
- To: gcc at gcc dot gnu dot org
- Date: Fri, 20 Jun 2003 11:48:11 -0400
- Subject: Re: ELF executable & relocatable
- References: <Law14-F93NLXiEzCS4J00029b8b@hotmail.com>
On Fri, Jun 20, 2003 at 05:56:56PM +0530, S kris wrote:
> I have a very strange query? please clarify.
> I understand that the ELF executable binary in Linux is usually not
> relocatable. So how to make or produce an ELF executable binary to be
> relocatable? Eventually the image should be both executable and
> relocatable, so that if binary in not loaded at its preferred load address
> then it should be able to fixup the addresses using .reloc section.
This isn't a compiler question. You should ask this question on the binutils
mailing lists where they talk about the assembler and linker. Some
instantiations of ELF do allow for this, some don't. It depends on the
individual machine and whether the people doing the port did the work.
In general, this will be easier on machines that use RELA relocations (where
the addend is a field in the relocation structure, and it doesn't look at whats
in the instruction field), than machines that use REL relocations (where the
addend is retrieved from the instruction, and the value is modified in place).
Note, the x86 and m32r use REL relocations, so if you are talking about the
common case of Linux/*BSD/etc. on an x86 platform, it may be somewhat harder.
While I haven't looked at it in detail, Red Hat 9 ships a prelink facility.
Here is the man page:
prelink - prelink ELF shared libraries and binaries to speed up startup
prelink [OPTION...] [FILES]
prelink is a program which modifies ELF shared libraries and ELF dynam-
ically linked binaries, so that the time which dynamic linker needs for
their relocation at startup significantly decreases and also due to
fewer relocations the run-time memory consumption decreases too (espe-
cially number of unshareable pages). Such prelinking information is
only used if all its dependant libraries have not changed since pre-
linking, otherwise programs are relocated normally.
prelink first collects ELF binaries which should be prelinked and all
the ELF shared libraries they depend on. Then it assigns a unique vir-
tual address space slot for each library and relinks the shared library
to that base address. When the dynamic linker attempts to load such a
library, unless that virtual address space slot is already occupied, it
will map it into the given slot. After this is done, prelink with the
help of dynamic linker resolves all relocations in the binary or
library against its dependant libraries and stores the relocations into
the ELF object. It also stores a list of all dependant libraries
together with their checksums into the binary or library. For bina-
ries, it also computes a list of conflicts (relocations which resolve
differently in the binary's symbol search scope than in the smaller
search scope in which the dependant library was resolved) and stores it
into a special ELF section.
At runtime, the dynamic linker first checks whether all dependant
libraries were successfully mapped into their designated address space
slots and whether they have not changed since the prelinking was done.
If all checks are successful, the dynamic linker just replays the list
of conflicts (which is usually significantly shorter than total number
of relocations) instead of relocating each library.
Verbose mode. Print the virtual address slot assignment to
libraries and print what binary or library is currently being
Don't actually prelink anything, just collect the bina-
ries/libraries, assign them addresses and with -v print what
would be prelinked.
Prelink all binaries and dependant libraries found in directory
hierarchies specified in /etc/prelink.conf. Normally only bina-
ries specified from command line and their dependant libraries
When assigning addresses to libraries, allow overlap of address
space slots provided that the two libraries are not present
together in any of the binaries or libraries. This results in
smaller virtual address space range used for libraries, on the
other side if during incremental prelinking prelink sees a
binary which puts together two libraries which were not present
together in any other binary and were given the same virtual
address space slots, then the binary cannot be prelinked. Nor-
mally each library is assigned a unique virtual address space
When assigning addresses to libraries, start with random address
within architecture dependant virtual address space range. This
can make some buffer overflow attacks slightly harder to
exploit, because libraries are not present on the same addresses
accross different machines. Normally, assigning virtual
addresses starts at the bottom of architecture dependant range.
Instead of prelinking, just relink given shared libraries to the
specified base address.
Don't save cache file after prelinking. Normally, list of
libraries (and with -m binaries also) is stored into /etc/pre-
link.cache file together with their given address space slots
and dependencies, so it can be used during incremental prelink-
ing (prelinking without -a option).
Specify alternate config file instead of default /etc/pre-
Specify alternate cache file instead of default /etc/pre-
Force re-prelinking even for already prelinked objects for which
no dependencies changed. This option causes new virtual address
space slots to be assigned to all libraries. Normally, only
binaries or libraries which are either not prelinked yet, or
some of their dependencies changed, are prelinked.
Print the content of the cache file (normally /etc/prelink.conf)
Specify alternate dynamic linker instead of the default.
Specify special LD_LIBRARY_PATH to be used when prelink queries
dynamic linker about symbol resolution details.
Only prelink ELF shared libraries, don't prelink any binaries.
When processing command line directory arguments, follow sym-
bolic links when walking directory hierarchies.
When processing command line directory arguments, limit direc-
tory tree walk to a single filesystem.
Revert binaries and libraries to their original content before
they were prelinked. Without -a option this causes only the
binaries and libraries specified on the command line to be
reverted to their original state (and e.g. not their
dependencies). If used together with -a option all binaries and
libraries from command line, all their dependencies, all bina-
ries found in directories specified on command line and in con-
fig file and all their dependencies will be undone.
Verifies a prelinked binary or library. This option can be used
only on a single binary or library. It first applies an --undo
operation on the file, then prelinks just that file again and
compares this with the original file. If both are identical, it
prints the file after --undo operation on standard output and
exit with zero status. Otherwise it exits with error status.
Thus if --verify operation returns zero exit status and its
standard output is equal to the content of the binary or library
before prelinking, you can be sure that nobody modified the
binaries or libraries after prelinking. Similarly with message
digests and checksums (unless you trigger the unprobable case of
modified file and original file having the same digest or check-
Print version and exit.
Print short help and exit.
Command line arguments should be either directory hierarchies (in which
case -l and -h options apply), or particular ELF binaries or shared
libraries. Unlike when walking directory hierarchies, specifying a
shared library explicitely on the command line causes it to be pre-
linked even if no binary is linked against it. Normally, only binaries
are collected together with all libraries they depend on.
# /usr/sbin/prelink -avmR
will prelink all binaries found in directories specified in /etc/pre-
link.conf and all their dependant libraries, assigning libraries unique
virtual address space slots only if they ever appear together and will
start assigning at random address.
# /usr/sbin/prelink -vm ~/bin/progx
will prelink ~/bin/progx program and all its dependant libraries
(unless they were prelinked already e.g. during prelink -a invocation).
# /usr/sbin/prelink -au
will revert all binaries and libraries to their original content.
# /usr/sbin/prelink -y /bin/prelinked_prog > /tmp/original_prog;
echo $? will verify whether /bin/prelinked_prog hasn't been
/etc/prelink.cache Binary file containing list of prelinked libraries
and/or binaries together with their assigned vir-
tual address space slots and dependencies. You can
run /usr/sbin/prelink -p to see what is stored in
/etc/prelink.conf Configuration file containing a list of directory
hierarchies which can contain ELF shared libraries
or binaries which should be prelinked. This con-
figuration file is used in -a mode to find binaries
which should be prelinked and also no matter
whether -a is given or not to limit which dependant
shared libraries should be prelinked. If prelink
finds a dependant library of some binary or other
library which is not present in any of the directo-
ries specified in /etc/prelink.conf and neither in
any of the directories specified on the command
line, then it cannot be prelinked. Each line of
the config file should be either comment starting
with #, or directory name. It can be prefixed by -l
switch, meaning tree walk of the given directory
will be only limited to one filesystem, or -h
switch, meaning tree walk of the given directory
will follow symbolic links.
prelink Some architectures, including IA-64, HPPA and MIPS are not yet
Jakub Jelinek <firstname.lastname@example.org>.
14 July 2002 prelink(8)