Allow pie links to create PLT entries

Sriraman Tallam tmsriram@google.com
Sat Feb 7 00:19:00 GMT 2015


Hi Cary,

   Like we discussed off-line, I have made this patch x86_64 specific
to allow R_X86_64_PC32 relocations to function symbols to create PLT
entries.

Thanks
Sri

On Fri, Jan 30, 2015 at 1:04 PM, Magnus Granberg <zorry@gentoo.org> wrote:
> fredag 30 januari 2015 12.15.29 skrev  H.J. Lu:
>> >>>>>
>> >>>>> movslq   0x1655(%rip),%rax  # 401b80 <i>
>> >>>>> mov    0x401b30(,%rax,4),%esi # a[i]
>> >>
>> >> If you link it with -pie, you will have TEXTREL in executable.
>> >> Do you want relocations in text sections in PIE?
>> >
>> > I have been told TEXTRELs are not preferred though I never understood why.
>> >
>> > Just to make sure I understand, are you saying that the absolute
>> > address in the case of -pie will be a text relocation?   I think that
>>
>> It is not about absolute address.  It is about symbol address.  Since
>> the address of symbol, a,  in
>>
>> movl a(,%rdi,4), %eax
>>
>> is unknown at link-time, linker has to generate relocation in text
>> section to resolve it at run-time.
>
> The sections need to be writeble and you can do change in that section and
> that is not good from a security piont of view.  You may even get performance
> penalties. I see no point to use no -fPIE objects in executable when you can
> get gcc to handel it with some patches. Gentoo Hardened have been building
> executable with -fPIE and linke with -pie for ages and it even a tread on gcc-
> patches ml to get support to Gcc 5.0
>
> /Magnus G.
>
>>
>> > is not true because this mov instruction
>> >
>> > mov    0x401b30(,%rax,4),%esi
>> >
>> > does not allow a 64-bit absolute value which is needed for -pie.  What
>> > I was instead suggesting is to  make that PC-relative like:
>> >
>> > mov    0xabcd(%rip,%rax,4),%esi
>> >
>> > which would not need a text relocation.  However, I do not think such
>> > an insn is supported yet, thought it would be useful.
>>
>> That will be useful.
>
-------------- next part --------------
	* x86_64.cc (Scan::get_reference_flags): Mark PC relative relocations
	as possibly created from function calls too.
	(Scan::global): Allow PLT entry for PC32 relocation of a function symbol
	for pie links.  Set the address of the dynamic symbol table entry for
	this function to that of the PLT entry.

diff --git a/gold/x86_64.cc b/gold/x86_64.cc
index 4543c8a..26943fb 100644
--- a/gold/x86_64.cc
+++ b/gold/x86_64.cc
@@ -2157,6 +2157,7 @@ Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
 
     case elfcpp::R_X86_64_PC64:
     case elfcpp::R_X86_64_PC32:
+      return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
     case elfcpp::R_X86_64_PC32_BND:
     case elfcpp::R_X86_64_PC16:
     case elfcpp::R_X86_64_PC8:
@@ -2835,9 +2836,20 @@ Target_x86_64<size>::Scan::global(Symbol_table* symtab,
     case elfcpp::R_X86_64_PC16:
     case elfcpp::R_X86_64_PC8:
       {
-	// Make a PLT entry if necessary.
-	if (gsym->needs_plt_entry())
-	  target->make_plt_entry(symtab, layout, gsym);
+	// Make a PLT entry if necessary. Allow elfcpp::R_X86_64_PC32
+	// relocations on function symbols to create PLT entries for pie links.
+	if (gsym->needs_plt_entry()
+	    || (r_type == elfcpp::R_X86_64_PC32 && gsym->is_func()
+		&& parameters->options().pie()))
+	  {
+	    target->make_plt_entry(symtab, layout, gsym);
+	    // For PIE links, a R_X86_64_PC32 relocation may be used in
+	    // function calls.  In that case we need to set the entry in
+	    // the dynamic symbol table to the address of the PLT entry.
+	    if (gsym->is_from_dynobj() &&r_type == elfcpp::R_X86_64_PC32
+		&& gsym->is_func() && parameters->options().pie())
+	      gsym->set_needs_dynsym_value();
+	  }
 	// Make a dynamic relocation if necessary.
 	if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
 	  {


More information about the Binutils mailing list