diff options
| author | Fangrui Song <i@maskray.me> | 2021-01-09 00:29:09 -0800 |
|---|---|---|
| committer | Fangrui Song <i@maskray.me> | 2021-01-09 00:32:01 -0800 |
| commit | 1d3ebbf537832f80be97739abc4f6962caad1dab (patch) | |
| tree | 04d566482273a71acb857fb93d24f83ad0873c80 /clang/lib/CodeGen/CodeGenModule.cpp | |
| parent | 9724c3cff46fb9e333df7d27b44758b4aba07168 (diff) | |
Add -f[no-]direct-access-external-data to supersede -mpie-copy-relocations
GCC r218397 "x86-64: Optimize access to globals in PIE with copy reloc" made
-fpie code emit R_X86_64_PC32 to reference external data symbols by default.
Clang adopted -mpie-copy-relocations D19996 as a flexible alternative.
The name -mpie-copy-relocations can be improved [1] and does not capture the
idea that this option can apply to -fno-pic and -fpic [2], so this patch
introduces -f[no-]direct-access-external-data and makes -mpie-copy-relocations
their aliases for compatibility.
[1]
For
```
extern int var;
int get() { return var; }
```
if var is defined in another translation unit in the link unit, there is no copy
relocation.
[2]
-fno-pic -fno-direct-access-external-data is useful to avoid copy relocations.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65888
If a shared object is linked with -Bsymbolic or --dynamic-list and exports a
data symbol, normally the data symbol cannot be accessed by -fno-pic code
(because by default an absolute relocation is produced which will lead to a copy
relocation). -fno-direct-access-external-data can prevent copy relocations.
-fpic -fdirect-access-external-data can avoid GOT indirection. This is like the
undefined counterpart of -fno-semantic-interposition. However, the user should
define var in another translation unit and link with -Bsymbolic or
--dynamic-list, otherwise the linker will error in a -shared link. Generally
the user has better tools for their goal but I want to mention that this
combination is valid.
On COFF, the behavior is like always -fdirect-access-external-data.
`__declspec(dllimport)` is needed to enable indirect access.
There is currently no plan to affect non-ELF behaviors or -fpic behaviors.
-fno-pic -fno-direct-access-external-data will be implemented in the subsequent patch.
GCC feature request https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98112
Reviewed By: tmsriram
Differential Revision: https://reviews.llvm.org/D92633
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
| -rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index cfc0f732748c..959f149667d9 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -986,7 +986,7 @@ static bool shouldAssumeDSOLocal(const CodeGenModule &CGM, // If we can use copy relocations we can assume it is local. if (auto *Var = dyn_cast<llvm::GlobalVariable>(GV)) if (!Var->isThreadLocal() && - (RM == llvm::Reloc::Static || CGOpts.PIECopyRelocations)) + (RM == llvm::Reloc::Static || CGOpts.DirectAccessExternalData)) return true; // If we can use a plt entry as the symbol address we can assume it |
