summaryrefslogtreecommitdiffstats
path: root/llvm/tools/gold/gold-plugin.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/tools/gold/gold-plugin.cpp')
-rw-r--r--llvm/tools/gold/gold-plugin.cpp51
1 files changed, 31 insertions, 20 deletions
diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp
index 7f38a021a0a..fd4107eeb18 100644
--- a/llvm/tools/gold/gold-plugin.cpp
+++ b/llvm/tools/gold/gold-plugin.cpp
@@ -647,16 +647,14 @@ static void recordFile(std::string Filename, bool TempOutFile) {
Cleanup.push_back(Filename.c_str());
}
-/// Open a file and return the new file descriptor given a base input
-/// file name, a flag indicating whether a temp file should be generated,
-/// and an optional task id. The new filename generated is
-/// returned in \p NewFilename.
-static int openOutputFile(SmallString<128> InFilename, bool TempOutFile,
- SmallString<128> &NewFilename, int TaskID = -1) {
- int FD;
+/// Return the desired output filename given a base input name, a flag
+/// indicating whether a temp file should be generated, and an optional task id.
+/// The new filename generated is returned in \p NewFilename.
+static void getOutputFileName(SmallString<128> InFilename, bool TempOutFile,
+ SmallString<128> &NewFilename, int TaskID = -1) {
if (TempOutFile) {
std::error_code EC =
- sys::fs::createTemporaryFile("lto-llvm", "o", FD, NewFilename);
+ sys::fs::createTemporaryFile("lto-llvm", "o", NewFilename);
if (EC)
message(LDPL_FATAL, "Could not create temporary file: %s",
EC.message().c_str());
@@ -664,12 +662,7 @@ static int openOutputFile(SmallString<128> InFilename, bool TempOutFile,
NewFilename = InFilename;
if (TaskID >= 0)
NewFilename += utostr(TaskID);
- std::error_code EC =
- sys::fs::openFileForWrite(NewFilename, FD, sys::fs::F_None);
- if (EC)
- message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str());
}
- return FD;
}
/// Add all required common symbols to M, which is expected to be the first
@@ -723,6 +716,24 @@ static void getThinLTOOldAndNewPrefix(std::string &OldPrefix,
NewPrefix = Split.second.str();
}
+namespace {
+// Define the LTOOutput handling
+class LTOOutput : public lto::NativeObjectOutput {
+ StringRef Path;
+
+public:
+ LTOOutput(StringRef Path) : Path(Path) {}
+ // Open the filename \p Path and allocate a stream.
+ std::unique_ptr<raw_pwrite_stream> getStream() override {
+ int FD;
+ std::error_code EC = sys::fs::openFileForWrite(Path, FD, sys::fs::F_None);
+ if (EC)
+ message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str());
+ return llvm::make_unique<llvm::raw_fd_ostream>(FD, true);
+ }
+};
+}
+
static std::unique_ptr<LTO> createLTO() {
Config Conf;
ThinBackend Backend;
@@ -814,7 +825,7 @@ static ld_plugin_status allSymbolsReadHook() {
}
SmallString<128> Filename;
- // Note that openOutputFile will append a unique ID for each task
+ // Note that getOutputFileName will append a unique ID for each task
if (!options::obj_path.empty())
Filename = options::obj_path;
else if (options::TheOutputType == options::OT_SAVE_TEMPS)
@@ -825,15 +836,15 @@ static ld_plugin_status allSymbolsReadHook() {
std::vector<uintptr_t> IsTemporary(MaxTasks);
std::vector<SmallString<128>> Filenames(MaxTasks);
- auto AddStream = [&](size_t Task) {
- int FD = openOutputFile(Filename, /*TempOutFile=*/!SaveTemps,
- Filenames[Task], MaxTasks > 1 ? Task : -1);
+ auto AddOutput = [&](size_t Task) {
+ auto &OutputName = Filenames[Task];
+ getOutputFileName(Filename, /*TempOutFile=*/!SaveTemps, OutputName,
+ MaxTasks > 1 ? Task : -1);
IsTemporary[Task] = !SaveTemps;
-
- return llvm::make_unique<llvm::raw_fd_ostream>(FD, true);
+ return llvm::make_unique<LTOOutput>(OutputName);
};
- check(Lto->run(AddStream));
+ check(Lto->run(AddOutput));
if (options::TheOutputType == options::OT_DISABLE ||
options::TheOutputType == options::OT_BC_ONLY)
OpenPOWER on IntegriCloud