Skip to content

Commit

Permalink
[Autolink Extract] Keep a set of linker flags instead of vector
Browse files Browse the repository at this point in the history
Otherwise we can duplicate linker flags across input binaries, which can result in very large linkerr invocation commands.

Resolves swiftlang#58380
  • Loading branch information
artemcm committed May 13, 2022
1 parent d34d88d commit 9f0abea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
13 changes: 7 additions & 6 deletions lib/DriverTool/autolink_extract_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <string>
#include <vector>
#include <set>

#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/Frontend/Frontend.h"
Expand Down Expand Up @@ -112,7 +113,7 @@ class AutolinkExtractInvocation {
/// Return 'true' if there was an error, and 'false' otherwise.
static bool
extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile,
std::vector<std::string> &LinkerFlags,
llvm::SetVector<std::string> &LinkerFlags,
CompilerInstance &Instance) {
// Search for the section we hold autolink entries in
for (auto &Section : ObjectFile->sections()) {
Expand Down Expand Up @@ -141,7 +142,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile,
SectionData->split(SplitFlags, llvm::StringRef("\0", 1), -1,
/*KeepEmpty=*/false);
for (const auto &Flag : SplitFlags)
LinkerFlags.push_back(Flag.str());
LinkerFlags.insert(Flag.str());
}
}
return false;
Expand All @@ -152,7 +153,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::vector<std::string> &LinkerFlags,
llvm::SetVector<std::string> &LinkerFlags,
CompilerInstance &Instance) {
// Search for the data segment we hold autolink entries in
for (const llvm::object::WasmSegment &Segment : ObjectFile->dataSegments()) {
Expand All @@ -165,7 +166,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile,
SegmentData.split(SplitFlags, llvm::StringRef("\0", 1), -1,
/*KeepEmpty=*/false);
for (const auto &Flag : SplitFlags)
LinkerFlags.push_back(Flag.str());
LinkerFlags.insert(Flag.str());
}
}
return false;
Expand All @@ -178,7 +179,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile,
static bool extractLinkerFlags(const llvm::object::Binary *Bin,
CompilerInstance &Instance,
StringRef BinaryFileName,
std::vector<std::string> &LinkerFlags) {
llvm::SetVector<std::string> &LinkerFlags) {
if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) {
return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance);
} else if (auto *ObjectFile =
Expand Down Expand Up @@ -227,7 +228,7 @@ int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0,
return 1;
}

std::vector<std::string> LinkerFlags;
llvm::SetVector<std::string> LinkerFlags;

// Extract the linker flags from the objects.
for (const auto &BinaryFileName : Invocation.getInputFilenames()) {
Expand Down
8 changes: 7 additions & 1 deletion test/AutolinkExtract/import.swift
Original file line number Diff line number Diff line change
@@ -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: -lswiftCore
// UNIQUE-COUNT-1: -lempty

// CHECK-elf-DAG: -lswiftCore
// CHECK-elf-DAG: -lempty

Expand Down

0 comments on commit 9f0abea

Please sign in to comment.