-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Autolink] Autolinking on COFF for Cygwin/MinGW #3887
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
// REQUIRES: autolink-extract | ||
|
||
// CHECK-elf: -lswiftCore | ||
// CHECK-coff: -lswiftCore |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
// REQUIRES: autolink-extract | ||
|
||
// CHECK-elf: -lswiftCore | ||
// CHECK-coff: -lswiftCore |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
#include "llvm/Option/Option.h" | ||
#include "llvm/Object/Archive.h" | ||
#include "llvm/Object/ObjectFile.h" | ||
#include "llvm/Object/COFF.h" | ||
#include "llvm/Object/ELFObjectFile.h" | ||
|
||
using namespace swift; | ||
|
@@ -106,32 +107,44 @@ class AutolinkExtractInvocation { | |
} | ||
}; | ||
|
||
// Look inside the binary 'Bin' and append any linker flags found in its | ||
// ".swift1_autolink_entries" section to 'LinkerFlags'. If 'Bin' is an archive, | ||
// recursively look inside all children within the archive. Return 'true' if | ||
// there was an error, and 'false' otherwise. | ||
/// Look inside the object file 'ObjectFile' and append any linker flags found in | ||
/// its ".swift1_autolink_entries" section to 'LinkerFlags'. | ||
static void | ||
extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile, | ||
std::vector<std::string> &LinkerFlags) { | ||
// Search for the section we hold autolink entries in | ||
for (auto &Section : ObjectFile->sections()) { | ||
llvm::StringRef SectionName; | ||
Section.getName(SectionName); | ||
if (SectionName == ".swift1_autolink_entries") { | ||
llvm::StringRef SectionData; | ||
Section.getContents(SectionData); | ||
|
||
// entries are null-terminated, so extract them and push them into | ||
// the set. | ||
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); | ||
} | ||
} | ||
} | ||
|
||
/// Look inside the binary 'Bin' and append any linker flags found in its | ||
/// ".swift1_autolink_entries" section to 'LinkerFlags'. If 'Bin' is an archive, | ||
/// recursively look inside all children within the archive. Return 'true' if | ||
/// there was an error, and 'false' otherwise. | ||
static bool extractLinkerFlags(const llvm::object::Binary *Bin, | ||
CompilerInstance &Instance, | ||
StringRef BinaryFileName, | ||
std::vector<std::string> &LinkerFlags) { | ||
if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) { | ||
// Search for the section we hold autolink entries in | ||
for (auto &Section : ObjectFile->sections()) { | ||
llvm::StringRef SectionName; | ||
Section.getName(SectionName); | ||
if (SectionName == ".swift1_autolink_entries") { | ||
llvm::StringRef SectionData; | ||
Section.getContents(SectionData); | ||
|
||
// entries are null-terminated, so extract them and push them into | ||
// the set. | ||
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); | ||
} | ||
} | ||
extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags); | ||
return false; | ||
} else if (auto *ObjectFile = | ||
llvm::dyn_cast<llvm::object::COFFObjectFile>(Bin)) { | ||
extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags); | ||
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 duplicate the same logic? It seems that you should do:
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. That's a good point. I considered the side effect that the MachO object files would not generate the error (of message "Don't know how to extract from object file"). If we don't care for the handling of MachO object files, the patch can be more simpler. 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. I think we don't care about MachO files being passed to this utility. This utility is not user-facing, it is only invoked by the driver. |
||
return false; | ||
} else if (auto *Archive = llvm::dyn_cast<llvm::object::Archive>(Bin)) { | ||
for (const auto &Child : Archive->children()) { | ||
|
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.
This isn't valid for COFF. COFF has a 8-character limit on section names.
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 quoted the description of the name field in
3. Section Table (Section Headers)
from MS PECOFF.Because the section '.swift1_autolink_entries' will not be emitted to an executable file, we can use the long name for autolink feature. But we can not use the long names '.swift2_protocol_conformances' and '.swift2_type_metadata' instead of '.sw2prtc' or '.sw2tymd', because they are emitted to an executable and loaded into the system memory.