Skip to content

DriverTool: add support for WebAssembly autolink entries #39502

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

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/DriverTool/autolink_extract_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "llvm/Object/Archive.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Object/Wasm.h"
#include "llvm/BinaryFormat/Wasm.h"

using namespace swift;
using namespace llvm::opt;
Expand Down Expand Up @@ -145,6 +147,30 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile,
return false;
}

/// Look inside the object file 'WasmObjectFile' and append any linker flags
/// found in its ".swift1_autolink_entries" section to 'LinkerFlags'. Return
/// 'true' if there was an error, and 'false' otherwise.
static bool
extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile,
std::vector<std::string> &LinkerFlags,
CompilerInstance &Instance) {
// Search for the data segment we hold autolink entries in
for (const llvm::object::WasmSegment &Segment : ObjectFile->dataSegments()) {
if (Segment.Data.Name == ".swift1_autolink_entries") {

StringRef SegmentData = llvm::toStringRef(Segment.Data.Content);
// entries are null-terminated, so extract them and push them into
// the set.
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());
}
}
return false;
}

/// 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
Expand All @@ -155,6 +181,9 @@ static bool extractLinkerFlags(const llvm::object::Binary *Bin,
std::vector<std::string> &LinkerFlags) {
if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) {
return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance);
} else if (auto *ObjectFile =
llvm::dyn_cast<llvm::object::WasmObjectFile>(Bin)) {
return extractLinkerFlagsFromObjectFile(ObjectFile, 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)) {
Expand Down