public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Relinker util?
@ 2002-02-28  3:14 Mike Perry
  2002-02-28  8:03 ` Ian Lance Taylor
  0 siblings, 1 reply; 3+ messages in thread
From: Mike Perry @ 2002-02-28  3:14 UTC (permalink / raw)
  To: binutils

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)?

I was thinking of hacking together a util to expand the .text segment of
a program and try to throw some code in there, but I don't wanna waste
my time if there's some obscure ld option to do this (I've looked and
there doesn't appear to be any - the closest I got was trying to rerun
collect2 without crt0, but I still ran into a redefined _DYNAMIC symbol).

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?

-- 
Mike Perry
Mad Computer Scientist
fscked.org enterprises, inc

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Relinker util?
  2002-02-28  3:14 Relinker util? Mike Perry
@ 2002-02-28  8:03 ` Ian Lance Taylor
  0 siblings, 0 replies; 3+ messages in thread
From: Ian Lance Taylor @ 2002-02-28  8:03 UTC (permalink / raw)
  To: Mike Perry; +Cc: binutils

Mike Perry <mikepery@fscked.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)?

No.

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

Ian

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Relinker util?
@ 2002-02-28 15:33 John Reiser
  0 siblings, 0 replies; 3+ messages in thread
From: John Reiser @ 2002-02-28 15:33 UTC (permalink / raw)
  To: binutils

Mike Perry <mikepery@fscked.org> asked:
> Is there a way to add new object files into a pre-exisiting
> statically linked binary?

In most Elf ET_EXEC, you can add bytes from the end of the first
PT_LOAD up to a page boundary, and from the begnning of the second
PT_LOAD down to a page boundary, by adding one page in the middle
of the file, and adjusting .p_offset etc.  In rare cases (1/4096)
this fails because the PT_LOADs abut exactly in the address space.

Another way to get space is to steal it from DT_HASH by reducing
the number of bucket headers: you get 8 bytes per bucket.  See
http://www.BitWagon.com/elfvector.html for some code that does this.

But I have not figured out how to get ld [bfd] to _input_ an ET_EXEC
file (elf32-i386 format) and obey the obvious placement restrictions
on the ensuing elf32-i386 output.

-- 
John Reiser, jreiser@BitWagon.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-02-28 23:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-28  3:14 Relinker util? Mike Perry
2002-02-28  8:03 ` Ian Lance Taylor
2002-02-28 15:33 John Reiser

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).