This is the mail archive of the mailing list for the binutils project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Relinker util?

Mike Perry <> 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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]