3232#include " llvm/Object/Archive.h"
3333#include " llvm/Object/ObjectFile.h"
3434#include " llvm/Object/ELFObjectFile.h"
35+ #include " llvm/Object/Wasm.h"
36+ #include " llvm/BinaryFormat/Wasm.h"
3537
3638using namespace swift ;
3739using namespace llvm ::opt;
@@ -145,6 +147,30 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile,
145147 return false ;
146148}
147149
150+ // / Look inside the object file 'WasmObjectFile' and append any linker flags
151+ // / found in its ".swift1_autolink_entries" section to 'LinkerFlags'. Return
152+ // / 'true' if there was an error, and 'false' otherwise.
153+ static bool
154+ extractLinkerFlagsFromObjectFile (const llvm::object::WasmObjectFile *ObjectFile,
155+ std::vector<std::string> &LinkerFlags,
156+ CompilerInstance &Instance) {
157+ // Search for the data segment we hold autolink entries in
158+ for (const llvm::object::WasmSegment &Segment : ObjectFile->dataSegments ()) {
159+ if (Segment.Data .Name == " .swift1_autolink_entries" ) {
160+
161+ StringRef SegmentData = llvm::toStringRef (Segment.Data .Content );
162+ // entries are null-terminated, so extract them and push them into
163+ // the set.
164+ llvm::SmallVector<llvm::StringRef, 4 > SplitFlags;
165+ SegmentData.split (SplitFlags, llvm::StringRef (" \0 " , 1 ), -1 ,
166+ /* KeepEmpty=*/ false );
167+ for (const auto &Flag : SplitFlags)
168+ LinkerFlags.push_back (Flag.str ());
169+ }
170+ }
171+ return false ;
172+ }
173+
148174// / Look inside the binary 'Bin' and append any linker flags found in its
149175// / ".swift1_autolink_entries" section to 'LinkerFlags'. If 'Bin' is an archive,
150176// / recursively look inside all children within the archive. Return 'true' if
@@ -155,6 +181,9 @@ static bool extractLinkerFlags(const llvm::object::Binary *Bin,
155181 std::vector<std::string> &LinkerFlags) {
156182 if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) {
157183 return extractLinkerFlagsFromObjectFile (ObjectFile, LinkerFlags, Instance);
184+ } else if (auto *ObjectFile =
185+ llvm::dyn_cast<llvm::object::WasmObjectFile>(Bin)) {
186+ return extractLinkerFlagsFromObjectFile (ObjectFile, LinkerFlags, Instance);
158187 } else if (auto *Archive = llvm::dyn_cast<llvm::object::Archive>(Bin)) {
159188 llvm::Error Error = llvm::Error::success ();
160189 for (const auto &Child : Archive->children (Error)) {
0 commit comments