From 10a187d2ad81df9b3b00e0deaa9ecd5d8776b7c6 Mon Sep 17 00:00:00 2001 From: Artem Chikin Date: Tue, 10 May 2022 14:05:03 -0700 Subject: [PATCH] [Autolink Extract] Keep a set of linker flags instead of vector Otherwise we can duplicate linker flags across input binaries, which can result in very large linkerr invocation commands. Resolves https://github.com/apple/swift/issues/58380 --- lib/DriverTool/autolink_extract_main.cpp | 36 ++++++++++++++++++------ test/AutolinkExtract/import.swift | 8 +++++- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/lib/DriverTool/autolink_extract_main.cpp b/lib/DriverTool/autolink_extract_main.cpp index 8d00c791589cb..37e03d7d45f3b 100644 --- a/lib/DriverTool/autolink_extract_main.cpp +++ b/lib/DriverTool/autolink_extract_main.cpp @@ -18,6 +18,7 @@ #include #include +#include #include "swift/AST/DiagnosticsFrontend.h" #include "swift/Frontend/Frontend.h" @@ -112,6 +113,7 @@ class AutolinkExtractInvocation { /// Return 'true' if there was an error, and 'false' otherwise. static bool extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile, + std::unordered_set &SeenLinkerLibraryFlags, std::vector &LinkerFlags, CompilerInstance &Instance) { // Search for the section we hold autolink entries in @@ -140,8 +142,15 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile, llvm::SmallVector SplitFlags; SectionData->split(SplitFlags, llvm::StringRef("\0", 1), -1, /*KeepEmpty=*/false); - for (const auto &Flag : SplitFlags) - LinkerFlags.push_back(Flag.str()); + for (const auto &Flag : SplitFlags) { + // If this is a library '-lxxx' flag, only add it if we have not seen it before + if (Flag.str().rfind("-l", 0) == 0) { + auto SeenFlagsInsertResult = SeenLinkerLibraryFlags.insert(Flag.str()); + if (SeenFlagsInsertResult.second == true) + LinkerFlags.push_back(Flag.str()); + } else + LinkerFlags.push_back(Flag.str()); + } } } return false; @@ -152,6 +161,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile, /// 'true' if there was an error, and 'false' otherwise. static bool extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile, + std::unordered_set &SeenLinkerLibraryFlags, std::vector &LinkerFlags, CompilerInstance &Instance) { // Search for the data segment we hold autolink entries in @@ -164,8 +174,15 @@ extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile, llvm::SmallVector SplitFlags; SegmentData.split(SplitFlags, llvm::StringRef("\0", 1), -1, /*KeepEmpty=*/false); - for (const auto &Flag : SplitFlags) - LinkerFlags.push_back(Flag.str()); + for (const auto &Flag : SplitFlags) { + // If this is a library '-lxxx' flag, only add it if we have not seen it before + if (Flag.str().rfind("-l", 0) == 0) { + auto SeenFlagsInsertResult = SeenLinkerLibraryFlags.insert(Flag.str()); + if (SeenFlagsInsertResult.second == true) + LinkerFlags.push_back(Flag.str()); + } else + LinkerFlags.push_back(Flag.str()); + } } } return false; @@ -178,12 +195,13 @@ extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile, static bool extractLinkerFlags(const llvm::object::Binary *Bin, CompilerInstance &Instance, StringRef BinaryFileName, + std::unordered_set &SeenLinkerLibraryFlags, std::vector &LinkerFlags) { if (auto *ObjectFile = llvm::dyn_cast(Bin)) { - return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance); + return extractLinkerFlagsFromObjectFile(ObjectFile, SeenLinkerLibraryFlags, LinkerFlags, Instance); } else if (auto *ObjectFile = llvm::dyn_cast(Bin)) { - return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance); + return extractLinkerFlagsFromObjectFile(ObjectFile, SeenLinkerLibraryFlags, LinkerFlags, Instance); } else if (auto *Archive = llvm::dyn_cast(Bin)) { llvm::Error Error = llvm::Error::success(); for (const auto &Child : Archive->children(Error)) { @@ -197,7 +215,7 @@ static bool extractLinkerFlags(const llvm::object::Binary *Bin, return true; } if (extractLinkerFlags(ChildBinary->get(), Instance, BinaryFileName, - LinkerFlags)) { + SeenLinkerLibraryFlags, LinkerFlags)) { return true; } } @@ -228,6 +246,8 @@ int autolink_extract_main(ArrayRef Args, const char *Argv0, } std::vector LinkerFlags; + // Flags of the form '-lxxx' seen so far + std::unordered_set SeenLinkerLibraryFlags; // Extract the linker flags from the objects. for (const auto &BinaryFileName : Invocation.getInputFilenames()) { @@ -245,7 +265,7 @@ int autolink_extract_main(ArrayRef Args, const char *Argv0, } if (extractLinkerFlags(BinaryOwner->getBinary(), Instance, BinaryFileName, - LinkerFlags)) { + SeenLinkerLibraryFlags, LinkerFlags)) { return 1; } } diff --git a/test/AutolinkExtract/import.swift b/test/AutolinkExtract/import.swift index ae8b929c7e132..17966ba9b7446 100644 --- a/test/AutolinkExtract/import.swift +++ b/test/AutolinkExtract/import.swift @@ -1,10 +1,16 @@ // RUN: %empty-directory(%t) // RUN: %target-swiftc_driver -emit-module -emit-module-path %t/empty.swiftmodule -module-name empty -module-link-name empty %S/empty.swift // RUN: %target-swiftc_driver -c %s -I %t -o %t/import_experimental.o -// RUN: %target-swift-autolink-extract %t/import_experimental.o -o - | %FileCheck --check-prefix CHECK-%target-object-format %s +// RUN: %target-swiftc_driver -c %s -I %t -o %t/import_experimental_again.o +// RUN: %target-swift-autolink-extract %t/import_experimental.o %t/import_experimental_again.o -o - | %FileCheck --check-prefix CHECK-%target-object-format %s + +// RUN: %target-swift-autolink-extract %t/import_experimental.o %t/import_experimental_again.o -o - | %FileCheck --check-prefix UNIQUE %s // REQUIRES: autolink-extract +// UNIQUE-COUNT-1: -lempty +// UNIQUE-COUNT-1: -lswiftCore + // CHECK-elf-DAG: -lswiftCore // CHECK-elf-DAG: -lempty