public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Stale entries left in GOT & PLT while discarding sections
@ 2006-04-07 14:15 Rajat Jain
  2006-04-07 14:33 ` Alan Modra
  0 siblings, 1 reply; 3+ messages in thread
From: Rajat Jain @ 2006-04-07 14:15 UTC (permalink / raw)
  To: Fedora-tools-list, binutils, rajat.noida.india

Hi list,

I am trying to remove certain functions from a shared object at LINK time:

=========t.c==========
int i=2;
int j=3;

void fun()  { i++; }
void gun() { j++; }
void sun() { fun(); }
====================

I created my shared object normally with " -shared -fPIC" and the
resultant shared object contains the following dynamic relocations:

$ objdump -R liba.so

DYNAMIC RELOCATION RECORDS
OFFSET   TYPE              VALUE
000016a0 R_386_GLOB_DAT    j
000016a4 R_386_GLOB_DAT    i
000016c0 R_386_JUMP_SLOT   fun

i.e., as expected, there are entries for "i" and "j" in the GOT and an
entry for "fun()" in the PLT. Now at link time, I am trying to
eliminate the symbols "i", "fun()" and "sun()" from the final shared
object. So this time I compiled my file with " -c -ffunction-sections
-fdata-sections " (to place each function / data in a separate
section) and whie linking, I changed the default linker script for the
shared object to /DISCARD/ the following sections:

.text.fun
.text.sun
.data.i

The result is as expected i.e. the symbols are totally removed from
the final shared object (definitions disappear, symbol entries
disappear etc). However, the GOT, the PLT (and associated relocations)
remain unchanged:

$ objdump -R liba.so

DYNAMIC RELOCATION RECORDS
OFFSET   TYPE              VALUE
0000166c R_386_GLOB_DAT    j
00001670 R_386_GLOB_DAT    i
0000168c R_386_JUMP_SLOT   fun

Due to this, when ever I try to link a program with this shared
object, the link goes fine but when I execute the program, the dynamic
linker gives me this error when trying to load the shared object:

symbol lookup error: ./liba.so: undefined symbol: i

AFAIK, this is because the dynamic linker tries to resolve the
R_386_GLOB_DAT relocation in the GOT before handing over the control
to the program:

1) As per my understanding the reason that the (stale) entries for the
removed sections / symbols are left in the GOT / PLT is because "ld"
creates the GOT and PLT at an early stage and later when reading the
linker script discards the sections as requested. It however does not
see (and does not care) that due to the removal of certain sections,
the entries in GOT and PLT have become inconsistent. Is my
understanding correct?

2) Is there any solution to the problem I am facing here? I mean is
there a way to remove the entries from the GOT and PLT as well?

Any pointers are welcome,

Thanks in Advance,

Rajat

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

* Re: Stale entries left in GOT & PLT while discarding sections
  2006-04-07 14:15 Stale entries left in GOT & PLT while discarding sections Rajat Jain
@ 2006-04-07 14:33 ` Alan Modra
  2006-04-10  9:18   ` Rajat Jain
  0 siblings, 1 reply; 3+ messages in thread
From: Alan Modra @ 2006-04-07 14:33 UTC (permalink / raw)
  To: Rajat Jain; +Cc: Fedora-tools-list, binutils

On Fri, Apr 07, 2006 at 07:09:55PM +0530, Rajat Jain wrote:
> 1) As per my understanding the reason that the (stale) entries for the
> removed sections / symbols are left in the GOT / PLT is because "ld"
> creates the GOT and PLT at an early stage and later when reading the
> linker script discards the sections as requested. It however does not
> see (and does not care) that due to the removal of certain sections,
> the entries in GOT and PLT have become inconsistent. Is my
> understanding correct?

Yes.

> 2) Is there any solution to the problem I am facing here? I mean is
> there a way to remove the entries from the GOT and PLT as well?

I think the basic machinery to do this is already in the linker
--gc-sections support.  If I was trying to do this, I would

o Break out the gc_sweep_hook calls in elflink.c:elf_gc_sweep into
  another function.  ie. Set SEC_EXCLUDE in elf_gc_sweep, but don't call
  the hook to decrement plt/got reloc counts.  The new function should
  call gc_sweep_hook on all SEC_EXCLUDE sections that have been
  processed by check_relocs.
o Arrange to call the new function immediately after
  ldlang.c:lang_process calls lang_place_orphans.

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

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

* Re: Stale entries left in GOT & PLT while discarding sections
  2006-04-07 14:33 ` Alan Modra
@ 2006-04-10  9:18   ` Rajat Jain
  0 siblings, 0 replies; 3+ messages in thread
From: Rajat Jain @ 2006-04-10  9:18 UTC (permalink / raw)
  To: Rajat Jain, Fedora-tools-list, binutils

On 4/7/06, Alan Modra <amodra@bigpond.net.au> wrote:
> On Fri, Apr 07, 2006 at 07:09:55PM +0530, Rajat Jain wrote:
> > 1) As per my understanding the reason that the (stale) entries for the
> > removed sections / symbols are left in the GOT / PLT is because "ld"
> > creates the GOT and PLT at an early stage and later when reading the
> > linker script discards the sections as requested. It however does not
> > see (and does not care) that due to the removal of certain sections,
> > the entries in GOT and PLT have become inconsistent. Is my
> > understanding correct?
>
> Yes.
>
> > 2) Is there any solution to the problem I am facing here? I mean is
> > there a way to remove the entries from the GOT and PLT as well?
>
> I think the basic machinery to do this is already in the linker
> --gc-sections support.  If I was trying to do this, I would
>
> o Break out the gc_sweep_hook calls in elflink.c:elf_gc_sweep into
>  another function.  ie. Set SEC_EXCLUDE in elf_gc_sweep, but don't call
>  the hook to decrement plt/got reloc counts.  The new function should
>  call gc_sweep_hook on all SEC_EXCLUDE sections that have been
>  processed by check_relocs.
> o Arrange to call the new function immediately after
>  ldlang.c:lang_process calls lang_place_orphans.
>

Hi Alan,

Thanks for the clue. I now have a new direction to look at :)

Thanks again,

Rajat

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

end of thread, other threads:[~2006-04-10  7:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-04-07 14:15 Stale entries left in GOT & PLT while discarding sections Rajat Jain
2006-04-07 14:33 ` Alan Modra
2006-04-10  9:18   ` Rajat Jain

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