llvm.org GIT mirror llvm / ea59150
[lto] add getLinkerOpts() Summary: Some compilers, including MSVC and Clang, allow linker options to be specified in source files. In the legacy LTO API, there is a getLinkerOpts() method that returns linker options for the bitcode module being processed. This change adds that method to the new API, so that the COFF linker can get the right linker options when using the new LTO API. Reviewers: pcc, ruiu, mehdi_amini, tejohnson Reviewed By: pcc Differential Revision: https://reviews.llvm.org/D29207 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@293950 91177308-0d34-0410-b5e6-96231b3b80d8 Bob Haarman 3 years ago
4 changed file(s) with 70 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
170170 const GlobalValue *GV) const override;
171171 };
172172
173 void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
174 const Triple &TT, Mangler &Mangler);
175
173176 } // end namespace llvm
174177
175178 #endif
2424 #include "llvm/LTO/Config.h"
2525 #include "llvm/Linker/IRMover.h"
2626 #include "llvm/Object/IRObjectFile.h"
27 #include "llvm/Support/Error.h"
2728 #include "llvm/Support/thread.h"
2829 #include "llvm/Target/TargetOptions.h"
2930 #include "llvm/Transforms/IPO/FunctionImport.h"
224225 symbol_iterator(SymTab.symbols().begin(), SymTab, this),
225226 symbol_iterator(SymTab.symbols().end(), SymTab, this));
226227 }
228
229 /// Returns linker options specified in the input file.
230 Expected getLinkerOpts();
227231
228232 /// Returns the path to the InputFile.
229233 StringRef getName() const;
918918 return 0;
919919 }
920920
921 void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
922 const Triple &TT, Mangler &Mangler) {
923 if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
924 return;
925
926 if (TT.isKnownWindowsMSVCEnvironment())
927 OS << " /EXPORT:";
928 else
929 OS << " -export:";
930
931 if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
932 std::string Flag;
933 raw_string_ostream FlagOS(Flag);
934 Mangler.getNameWithPrefix(FlagOS, GV, false);
935 FlagOS.flush();
936 if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
937 OS << Flag.substr(1);
938 else
939 OS << Flag;
940 } else {
941 Mangler.getNameWithPrefix(OS, GV, false);
942 }
943
944 if (!GV->getValueType()->isFunctionTy()) {
945 if (TT.isKnownWindowsMSVCEnvironment())
946 OS << ",DATA";
947 else
948 OS << ",data";
949 }
950 }
951
921952 MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
922953 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
923954 int Selection = 0;
11211152
11221153 void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
11231154 raw_ostream &OS, const GlobalValue *GV) const {
1124 if (!GV->hasDLLExportStorageClass() || GV->isDeclaration())
1125 return;
1126
1127 const Triple &TT = getTargetTriple();
1128
1129 if (TT.isKnownWindowsMSVCEnvironment())
1130 OS << " /EXPORT:";
1131 else
1132 OS << " -export:";
1133
1134 if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) {
1135 std::string Flag;
1136 raw_string_ostream FlagOS(Flag);
1137 getMangler().getNameWithPrefix(FlagOS, GV, false);
1138 FlagOS.flush();
1139 if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
1140 OS << Flag.substr(1);
1141 else
1142 OS << Flag;
1143 } else {
1144 getMangler().getNameWithPrefix(OS, GV, false);
1145 }
1146
1147 if (!GV->getValueType()->isFunctionTy()) {
1148 if (TT.isKnownWindowsMSVCEnvironment())
1149 OS << ",DATA";
1150 else
1151 OS << ",data";
1152 }
1153 }
1155 emitLinkerFlagsForGlobalCOFF(OS, GV, getTargetTriple(), getMangler());
1156 }
1616 #include "llvm/Bitcode/BitcodeReader.h"
1717 #include "llvm/Bitcode/BitcodeWriter.h"
1818 #include "llvm/CodeGen/Analysis.h"
19 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
1920 #include "llvm/IR/AutoUpgrade.h"
2021 #include "llvm/IR/DiagnosticPrinter.h"
2122 #include "llvm/IR/LegacyPassManager.h"
23 #include "llvm/IR/Mangler.h"
24 #include "llvm/IR/Metadata.h"
2225 #include "llvm/LTO/LTOBackend.h"
2326 #include "llvm/Linker/IRMover.h"
2427 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
28 #include "llvm/Support/Error.h"
2529 #include "llvm/Support/ManagedStatic.h"
2630 #include "llvm/Support/MemoryBuffer.h"
2731 #include "llvm/Support/Path.h"
288292 return I->second;
289293 }
290294 return -1;
295 }
296
297 Expected InputFile::getLinkerOpts() {
298 std::string LinkerOpts;
299 raw_string_ostream LOS(LinkerOpts);
300 // Extract linker options from module metadata.
301 for (InputModule &Mod : Mods) {
302 std::unique_ptr &M = Mod.Mod;
303 if (auto E = M->materializeMetadata())
304 return std::move(E);
305 if (Metadata *Val = M->getModuleFlag("Linker Options")) {
306 MDNode *LinkerOptions = cast(Val);
307 for (const MDOperand &MDOptions : LinkerOptions->operands())
308 for (const MDOperand &MDOption : cast(MDOptions)->operands())
309 LOS << " " << cast(MDOption)->getString();
310 }
311 }
312
313 // Synthesize export flags for symbols with dllexport storage.
314 const Triple TT(Mods[0].Mod->getTargetTriple());
315 Mangler M;
316 for (const ModuleSymbolTable::Symbol &Sym : SymTab.symbols())
317 if (auto *GV = Sym.dyn_cast())
318 emitLinkerFlagsForGlobalCOFF(LOS, GV, TT, M);
319 LOS.flush();
320 return LinkerOpts;
291321 }
292322
293323 StringRef InputFile::getName() const {