This is the mail archive of the
mailing list for the binutils project.
Re: Relinker util?
- From: Ian Lance Taylor <ian at airs dot com>
- To: Mike Perry <mikepery at fscked dot org>
- Cc: binutils at sources dot redhat dot com
- Date: 28 Feb 2002 07:51:26 -0800
- Subject: Re: Relinker util?
- References: <20020228081708.GB10819@fscked.org>
Mike Perry <firstname.lastname@example.org> writes:
> Hey guys. Is there a way to add new object files into a pre-exisiting
> statically linked binary? Or a way add a new .so file to the library
> list of a dynamic library (by actually PERMANENTLY modifying the binary,
> ie not LD_PRELOAD)?
> What would be the best way to approach this? I was thinking of just
> modifying the ELF program header by hand to expand like the last segment
> in the file and throwing some code down there. Would using something like
> libBFD be a better choice?
Simply modifying the ELF program header is an interesting approach.
Since the last LOAD segment normally includes the BSS (i.e., the memsz
is larger than the filesz) you will have to write out some zeroes for
the BSS. The last LOAD segment is also normally not executable, so
technically you can not add instructions there. However, very few
processors--perhaps none--enforce that restriction.
However, you didn't mention why you want to do this. Simply adding
new code in the text section is not going to cause it to be called by
anything. To get that code to be called by anything within the
program, you would have to ensure that the relocations were retained,
and you have to adjust them. To get that code to be called by
anything outside the program (e.g., using dlopen), you would have to
frob the dynamic symbol table. Neither operation is nearly as simple
as just dumping new text into the binary.