This is an archive of the discontinued LLVM Phabricator instance.

Make CanUseCopyRelocWithPIE a llc option, -pie-copy-relocations
ClosedPublic

Authored by tmsriram on Sep 22 2016, 3:30 PM.

Details

Diff Detail

Repository
rL LLVM

Event Timeline

tmsriram updated this revision to Diff 72219.Sep 22 2016, 3:30 PM
tmsriram retitled this revision from to Make CanUseCopyRelocWithPIE a llvm option, -pie-copy-relocations.
tmsriram updated this object.
tmsriram added reviewers: davide, rnk, davidxl.
tmsriram added a subscriber: llvm-commits.
davide edited edge metadata.Sep 23 2016, 12:41 PM

Can you give a little bit more context? How do you plan to use this option?

Can you give a little bit more context? How do you plan to use this option?

https://reviews.llvm.org/D19995 discusses this in detail but here is the quick summary:

  • When linking executables in non-PIE mode, references to external globals do not use the GOT. They are treated as if the globals were defined in the executable.
  • If it so happens that the global is not defined in the executable, a copy relocation is used to duplicate the definition in the executable.
  • Currently, in PIE mode, all accesses to external globals use an extra load from the GOT to get its address which is then dereferenced.
  • This is particularly inefficient if it turns out at link time that the global ends up being defined in the executable from a different compilation unit.
  • Using GOT accesses for globals that end up defined in the executable regresses some of our benchmarks by a few percent.
  • It turns out that copy relocations can be used here just like non-PIE mode to always avoid the extra load from the GOT and directly access the global.
  • Gold and GNU ld started supporting copy relocations for pie only recently. Gold supports via this patch : https://sourceware.org/ml/binutils/2014-05/msg00092.html
  • This option is meant to be used when the linker allows copy relocations for PIE. The default is off.
  • GCC already does this, patch discussing this : https://gcc.gnu.org/ml/gcc-patches/2014-05/msg01215.html

This was discussed quite a bit and Rafael wanted something in the IR to tag globals as "extern local" is needed. For "extern local" globals compiler would not use the GOT and instead the linker would emit a warning if the global ends up undefined in the executable. For globals that are extern and not marked local, GOT would be used if copy relocations support is not available.

rnk added inline comments.Sep 30 2016, 1:07 PM
lib/Target/TargetMachine.cpp
118–121 ↗(On Diff #72219)

This should really live in MCTargetOptions, and not a global. We're trying to move away from cl::opt in library code.

davide added inline comments.Sep 30 2016, 4:30 PM
lib/Target/TargetMachine.cpp
118–121 ↗(On Diff #72219)

Strongly agree. Other than that the logic seems right to me. Please upload another version.

tmsriram updated this revision to Diff 73313.Oct 3 2016, 11:41 AM
tmsriram retitled this revision from Make CanUseCopyRelocWithPIE a llvm option, -pie-copy-relocations to Make CanUseCopyRelocWithPIE a llc option, -pie-copy-relocations.
tmsriram edited edge metadata.
  • Removed cl::opt from library code
  • Created MCTargetOptions member MCPIECopyRelocations instead.
  • New llc command-line option -pie-copy-relocations to enable it.
  • I will add a clang option separately to enable this.

This is probably OK. Reid?

rnk added inline comments.Oct 11 2016, 4:26 PM
lib/Target/TargetMachine.cpp
152 ↗(On Diff #73313)

GV could be a GlobalAlias to a function, so I think this would be better as isa<GlobalVariable>(GV) instead of not a function. That's worth adding as a test case too, if you could

tmsriram updated this revision to Diff 74447.Oct 12 2016, 3:10 PM
  • Check for isa<GlobalVariable> rather than !isa<Function>
  • Test case for Global Alias is not required as alias implies the aliased object is defined and is hence DSO local.
rnk accepted this revision.Oct 12 2016, 3:18 PM
rnk edited edge metadata.

lgtm, thanks!

This revision is now accepted and ready to land.Oct 12 2016, 3:18 PM
tmsriram updated this revision to Diff 74577.Oct 13 2016, 1:48 PM
tmsriram edited edge metadata.

Check if GV not null before checking its type.

This revision was automatically updated to reflect the committed changes.