mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
DriverTool: add support for WebAssembly autolink entries (#39502)
This change adds support for `.swift1_autolink_entries` in WebAssembly object files to `lib/DriverTool/autolink_extract_main.cpp`. This is implemented in a similar way to existing ELF autolink entries handling.
This commit is contained in:
@@ -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;
|
||||
@@ -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
|
||||
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user