[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Specify how undefined weak symbol should be resolved in executable



On Mon, Feb 22, 2016 at 9:45 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Mon, Feb 22, 2016 at 8:25 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
>> Currently when building executable, undefined weak symbol is resolved
>> differently, depending on if the relocatable input is compiled with PIC or
>> not:
>>
>> hjl@gnu-tools-1 weak-1]$ cat x.c
>> extern void __attribute__ ((weak)) fun (void);
>> int main (void)
>> {
>>   if (&fun != 0)
>>     fun ();
>>   return 0;
>> }
>> [hjl@gnu-tools-1 weak-1]$ make
>> gcc -B./ -O2 -o x x.c
>> gcc -B./ -O2 -o y x.c -fPIC
>> readelf -r x
>>
>> Relocation section '.rela.dyn' at offset 0x328 contains 1 entries:
>>   Offset          Info           Type           Sym. Value    Sym. Name + Addend
>> 000000600870  000200000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ + 0
>>
>> Relocation section '.rela.plt' at offset 0x340 contains 1 entries:
>>   Offset          Info           Type           Sym. Value    Sym. Name + Addend
>> 000000600890  000100000007 R_X86_64_JUMP_SLO 0000000000000000
>> __libc_start_main@GLIBC_2.2.5 + 0
>>
>> There is no dynamic relocation against fun, whose value is
>> resolved to zero at link-time.
>>
>> readelf -r y
>>
>> Relocation section '.rela.dyn' at offset 0x348 contains 2 entries:
>>   Offset          Info           Type           Sym. Value    Sym. Name + Addend
>> 0000006008a0  000100000006 R_X86_64_GLOB_DAT 0000000000000000 fun + 0
>> 0000006008a8  000300000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ + 0
>>
>> Relocation section '.rela.plt' at offset 0x378 contains 1 entries:
>>   Offset          Info           Type           Sym. Value    Sym. Name + Addend
>> 0000006008c8  000200000007 R_X86_64_JUMP_SLO 0000000000000000
>> __libc_start_main@GLIBC_2.2.5 + 0
>> [hjl@gnu-tools-1 weak-1]$
>>
>> There is dynamic relocation against fun, whose value is resolved
>> at runt-time. We should make it consistent, regardless if input
>> relocatable file is compiled with PIC or not.
>>
>> I raised the issue at gABI:
>>
>> https://groups.google.com/d/msg/generic-abi/Ckq19PfLxyk/eCbXPofkAQAJ
>>
>> But if gABI change is incompatible with other OSes, we need to
>> specify it in GNU ABI.
>
> We should extend GNU ABI to:
>
> When creating executable, if dynamic relocation is available at
> run-time, the link
> editor should generate dynamic relocations against unresolved weak symbols so
> that their values will be resolved at run-time.
>

I opened:

https://sourceware.org/bugzilla/show_bug.cgi?id=19704

-- 
H.J.