-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.7][Autolink Extract] Keep a set of seen linker library flags #59034
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
#include <string> | ||
#include <vector> | ||
#include <unordered_set> | ||
|
||
#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<std::string> &SeenLinkerLibraryFlags, | ||
std::vector<std::string> &LinkerFlags, | ||
CompilerInstance &Instance) { | ||
// Search for the section we hold autolink entries in | ||
|
@@ -140,8 +142,15 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile, | |
llvm::SmallVector<llvm::StringRef, 4> 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
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<std::string> &SeenLinkerLibraryFlags, | ||
std::vector<std::string> &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<llvm::StringRef, 4> 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<std::string> &SeenLinkerLibraryFlags, | ||
std::vector<std::string> &LinkerFlags) { | ||
if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) { | ||
return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance); | ||
return extractLinkerFlagsFromObjectFile(ObjectFile, SeenLinkerLibraryFlags, LinkerFlags, Instance); | ||
} else if (auto *ObjectFile = | ||
llvm::dyn_cast<llvm::object::WasmObjectFile>(Bin)) { | ||
return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance); | ||
return extractLinkerFlagsFromObjectFile(ObjectFile, SeenLinkerLibraryFlags, LinkerFlags, Instance); | ||
} else if (auto *Archive = llvm::dyn_cast<llvm::object::Archive>(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<const char *> Args, const char *Argv0, | |
} | ||
|
||
std::vector<std::string> LinkerFlags; | ||
// Flags of the form '-lxxx' seen so far | ||
std::unordered_set<std::string> SeenLinkerLibraryFlags; | ||
|
||
// Extract the linker flags from the objects. | ||
for (const auto &BinaryFileName : Invocation.getInputFilenames()) { | ||
|
@@ -245,7 +265,7 @@ int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0, | |
} | ||
|
||
if (extractLinkerFlags(BinaryOwner->getBinary(), Instance, BinaryFileName, | ||
LinkerFlags)) { | ||
SeenLinkerLibraryFlags, LinkerFlags)) { | ||
return 1; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I worry about this approach because you have to consider the following equivalences:
-l:library.a
-static
-lrary
lib/library.a
and the following:
-l:library.so
-shared
-lrary
lib/liblibrary.so
Furthermore, the order of the libraries is relevant to linking. The conversion to an unordered_set breaks linking semantics - order of library specification on the command line indicates how to resolve symbols. This can change the symbolic resolution.