This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: PATCH: PR ld/15149: Weak reference leads to DT_NEEDED entry
On Thu, Feb 14, 2013 at 5:46 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Thu, Feb 14, 2013 at 3:16 PM, H.J. Lu <hongjiu.lu@intel.com> wrote:
>> We shouldn't add DT_NEEDED for weak references unless asked. This
>> patch implements it. OK to install?
>>
>> Thanks.
>>
>>
>> H.J.
>> ---
>> bfd/
>>
>> 2013-02-14 H.J. Lu <hongjiu.lu@intel.com>
>>
>> PR ld/15149
>> * elflink.c (elf_link_add_object_symbols): Don't add DT_NEEDED for
>> weak references, unless the dynamic object is specified on the
>> command line.
>>
>> ld/testsuite/
>>
>> 2013-02-14 H.J. Lu <hongjiu.lu@intel.com>
>>
>> PR ld/15149
>> * ld/testsuite/ld-elf/dummy.s: New file.
>> * ld/testsuite/ld-elf/pr15149.rd: Likewise.
>> * ld/testsuite/ld-elf/pr15149a.s: Likewise.
>> * ld/testsuite/ld-elf/pr15149b.s: Likewise.
>>
>> * ld-elf/elf.exp: Add PR ld/15149 test.
>>
>> diff --git a/bfd/elflink.c b/bfd/elflink.c
>> index d423ca4..05d5746 100644
>> --- a/bfd/elflink.c
>> +++ b/bfd/elflink.c
>> @@ -4477,10 +4477,14 @@ error_free_dyn:
>> break;
>> }
>>
>> + /* Don't add DT_NEEDED for weak references, unless the dynamic
>> + object is specified on the command line. */
>> if (!add_needed
>> && definition
>> && ((dynsym
>> - && h->ref_regular)
>> + && (h->ref_regular_nonweak
>> + || (h->ref_regular
>> + && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) == 0)))
>> || (h->ref_dynamic
>> && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
>> && !on_needed_list (elf_dt_name (abfd), htab->needed))))
>
> This isn't consistent with bfd ld behavior. --no-copy-dt-needed-entries
> is ignored when creating executable. We try to emulate run-time linker.
> In this case, we should issue an error, instead of silently adding DT_NEEDED.
>
This patch also tracks weak references. Now we issue an
error:
gcc -Wl,--no-copy-dt-needed-entries -B./ -Wl,--no-as-needed -o x foo.o
libyyy.so -Wl,-rpath-link,.
./ld: foo.o: undefined reference to symbol 'xxx'
./ld: note: 'xxx' is defined in DSO ./libxxx.so so try adding it to
the linker command line
./libxxx.so: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
OK to install?
Thanks.
--
H.J.
---
2013-02-14 H.J. Lu <hongjiu.lu@intel.com>
PR ld/15149
* elflink.c (elf_link_add_object_symbols): Also track weak
references.
diff --git a/bfd/elflink.c b/bfd/elflink.c
index a92ff60..617c505 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -4002,15 +4002,17 @@ error_free_dyn:
bfd_boolean skip;
/* If this is a definition of a symbol which was previously
- referenced in a non-weak manner then make a note of the bfd
- that contained the reference. This is used if we need to
- refer to the source of the reference later on. */
+ referenced, then make a note of the bfd that contained the
+ reference. This is used if we need to refer to the source
+ of the reference later on. */
if (! bfd_is_und_section (sec))
{
- h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE,
FALSE, FALSE);
+ h = elf_link_hash_lookup (elf_hash_table (info), name,
+ FALSE, FALSE, FALSE);
if (h != NULL
- && h->root.type == bfd_link_hash_undefined
+ && (h->root.type == bfd_link_hash_undefined
+ || h->root.type == bfd_link_hash_undefweak)
&& h->root.u.undef.abfd)
undef_bfd = h->root.u.undef.abfd;
}
@@ -4123,14 +4125,15 @@ error_free_dyn:
}
/* If necessary, make a second attempt to locate the bfd
- containing an unresolved, non-weak reference to the
- current symbol. */
+ containing an unresolved reference to the current symbol. */
if (! bfd_is_und_section (sec) && undef_bfd == NULL)
{
- h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE,
FALSE, FALSE);
+ h = elf_link_hash_lookup (elf_hash_table (info), name,
+ FALSE, FALSE, FALSE);
if (h != NULL
- && h->root.type == bfd_link_hash_undefined
+ && (h->root.type == bfd_link_hash_undefined
+ || h->root.type == bfd_link_hash_undefweak)
&& h->root.u.undef.abfd)
undef_bfd = h->root.u.undef.abfd;
}