This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: PATCH: ld/2218: Weak undefined symbol doesn't work properly with PIE
On Fri, Jan 27, 2006 at 07:07:27AM -0800, H. J. Lu wrote:
> On Fri, Jan 27, 2006 at 06:28:06AM -0800, H. J. Lu wrote:
> > On Fri, Jan 27, 2006 at 08:34:51PM +1030, Alan Modra wrote:
> > > On Thu, Jan 26, 2006 at 11:03:35PM -0800, H. J. Lu wrote:
> > > > We should treat global symbols in PIE like shared library.
> > >
> > > Perhaps. But please explain why the place you are patching is the
> > > correct place to fix a problem with weak syms. I don't think your patch
> > > is correct.
> >
> > PIE is like DSO when the undefined weak symbols are concerned. They
> > have to be dynamic symbols so that ld.so can handle them properly. I
> > will check in my testcase shortly so that you can check it out.
> >
>
> The patch probably is incorrect. I think we have an issue with dynamic
> symbols in PIE:
>
> bash-3.00$ cat x.c
> main (){ }
> bash-3.00$ gcc -c x.c
> bash-3.00$ gcc x.o
> bash-3.00$ readelf -Ds a.out
>
> Symbol table for image:
> Num Buc: Value Size Type Bind Vis Ndx Name
> 4 0: 00000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
> 3 0: 00000000 0 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses
> 1 1: 00000000 221 FUNC GLOBAL DEFAULT UND __libc_start_main
> 2 2: 08048430 4 OBJECT GLOBAL DEFAULT 14 _IO_stdin_used
> bash-3.00$ gcc x.o -pie
> bash-3.00$ readelf -Ds a.out
>
> Symbol table for image:
> Num Buc: Value Size Type Bind Vis Ndx Name
> 16 1: 000017c0 0 NOTYPE GLOBAL DEFAULT ABS _edata
> 15 1: 00000000 231 FUNC WEAK DEFAULT UND __cxa_finalize
> 19 3: 00000000 0 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses
> 14 7: 00000000 221 FUNC GLOBAL DEFAULT UND __libc_start_main
> 13 10: 0000059c 30 FUNC GLOBAL DEFAULT 12 main
> 12 11: 000017c0 0 NOTYPE GLOBAL DEFAULT ABS __bss_start
> 18 13: 000006ac 4 OBJECT GLOBAL DEFAULT 14 _IO_stdin_used
> 17 13: 000017c4 0 NOTYPE GLOBAL DEFAULT ABS _end
> 20 15: 00000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
> bash-3.00$
>
> We are putting more symbols into dynamic symbol table in PIE without
> marking them hidden. That means we export more symbols in PIE. I will
> see what I can do.
>
Here is the updated patch.
H.J.
----
2006-01-27 H.J. Lu <hongjiu.lu@intel.com>
PR ld/2218
* elflink.c (elf_link_add_object_symbols): Mark global symbol
in shared library dynamic.
(elf_link_output_extsym): Mark a symbol in PIE hidden when
appropriate.
--- bfd/elflink.c.pie 2006-01-27 06:53:51.000000000 -0800
+++ bfd/elflink.c 2006-01-27 09:37:15.000000000 -0800
@@ -4050,7 +4050,12 @@ elf_link_add_object_symbols (bfd *abfd,
}
else
h->def_regular = 1;
- if (! info->executable
+
+ /* The global symbols in DSO and PIE are put in dynamic
+ symbol table by default. We will mark the ones in PIE
+ as hidden in elf_link_output_extsym if they aren't
+ defined in nor referenced by any dynamic object. */
+ if (info->shared
|| h->def_dynamic
|| h->ref_dynamic)
dynsym = TRUE;
@@ -6487,6 +6492,15 @@ elf_link_output_extsym (struct elf_link_
else
sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
+ /* If we don't export all symbols into the dynamic symbol table, a
+ symbol in PIE, which isn't defined in nor referenced by any
+ dynamic object, has the hidden visibility. */
+ if (finfo->info->pie
+ && !finfo->info->export_dynamic
+ && !h->def_dynamic
+ && !h->ref_dynamic)
+ sym.st_other = STV_HIDDEN | (h->other & ~ ELF_ST_VISIBILITY (-1));
+
switch (h->root.type)
{
default: