Skip to content

Commit 9f0abea

Browse files
committed
[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 #58380
1 parent d34d88d commit 9f0abea

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

lib/DriverTool/autolink_extract_main.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <string>
2020
#include <vector>
21+
#include <set>
2122

2223
#include "swift/AST/DiagnosticsFrontend.h"
2324
#include "swift/Frontend/Frontend.h"
@@ -112,7 +113,7 @@ class AutolinkExtractInvocation {
112113
/// Return 'true' if there was an error, and 'false' otherwise.
113114
static bool
114115
extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile,
115-
std::vector<std::string> &LinkerFlags,
116+
llvm::SetVector<std::string> &LinkerFlags,
116117
CompilerInstance &Instance) {
117118
// Search for the section we hold autolink entries in
118119
for (auto &Section : ObjectFile->sections()) {
@@ -141,7 +142,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile,
141142
SectionData->split(SplitFlags, llvm::StringRef("\0", 1), -1,
142143
/*KeepEmpty=*/false);
143144
for (const auto &Flag : SplitFlags)
144-
LinkerFlags.push_back(Flag.str());
145+
LinkerFlags.insert(Flag.str());
145146
}
146147
}
147148
return false;
@@ -152,7 +153,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile,
152153
/// 'true' if there was an error, and 'false' otherwise.
153154
static bool
154155
extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile,
155-
std::vector<std::string> &LinkerFlags,
156+
llvm::SetVector<std::string> &LinkerFlags,
156157
CompilerInstance &Instance) {
157158
// Search for the data segment we hold autolink entries in
158159
for (const llvm::object::WasmSegment &Segment : ObjectFile->dataSegments()) {
@@ -165,7 +166,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile,
165166
SegmentData.split(SplitFlags, llvm::StringRef("\0", 1), -1,
166167
/*KeepEmpty=*/false);
167168
for (const auto &Flag : SplitFlags)
168-
LinkerFlags.push_back(Flag.str());
169+
LinkerFlags.insert(Flag.str());
169170
}
170171
}
171172
return false;
@@ -178,7 +179,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile,
178179
static bool extractLinkerFlags(const llvm::object::Binary *Bin,
179180
CompilerInstance &Instance,
180181
StringRef BinaryFileName,
181-
std::vector<std::string> &LinkerFlags) {
182+
llvm::SetVector<std::string> &LinkerFlags) {
182183
if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) {
183184
return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance);
184185
} else if (auto *ObjectFile =
@@ -227,7 +228,7 @@ int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0,
227228
return 1;
228229
}
229230

230-
std::vector<std::string> LinkerFlags;
231+
llvm::SetVector<std::string> LinkerFlags;
231232

232233
// Extract the linker flags from the objects.
233234
for (const auto &BinaryFileName : Invocation.getInputFilenames()) {

test/AutolinkExtract/import.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-swiftc_driver -emit-module -emit-module-path %t/empty.swiftmodule -module-name empty -module-link-name empty %S/empty.swift
33
// RUN: %target-swiftc_driver -c %s -I %t -o %t/import_experimental.o
4-
// RUN: %target-swift-autolink-extract %t/import_experimental.o -o - | %FileCheck --check-prefix CHECK-%target-object-format %s
4+
// RUN: %target-swiftc_driver -c %s -I %t -o %t/import_experimental_again.o
5+
// RUN: %target-swift-autolink-extract %t/import_experimental.o %t/import_experimental_again.o -o - | %FileCheck --check-prefix CHECK-%target-object-format %s
6+
7+
// RUN: %target-swift-autolink-extract %t/import_experimental.o %t/import_experimental_again.o -o - | %FileCheck --check-prefix UNIQUE %s
58

69
// REQUIRES: autolink-extract
710

11+
// UNIQUE-COUNT-1: -lswiftCore
12+
// UNIQUE-COUNT-1: -lempty
13+
814
// CHECK-elf-DAG: -lswiftCore
915
// CHECK-elf-DAG: -lempty
1016

0 commit comments

Comments
 (0)