32
32
#include " llvm/Object/Archive.h"
33
33
#include " llvm/Object/ObjectFile.h"
34
34
#include " llvm/Object/ELFObjectFile.h"
35
+ #include " llvm/Object/Wasm.h"
36
+ #include " llvm/BinaryFormat/Wasm.h"
35
37
36
38
using namespace swift ;
37
39
using namespace llvm ::opt;
@@ -145,6 +147,30 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile,
145
147
return false ;
146
148
}
147
149
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
+
148
174
// / Look inside the binary 'Bin' and append any linker flags found in its
149
175
// / ".swift1_autolink_entries" section to 'LinkerFlags'. If 'Bin' is an archive,
150
176
// / recursively look inside all children within the archive. Return 'true' if
@@ -155,6 +181,9 @@ static bool extractLinkerFlags(const llvm::object::Binary *Bin,
155
181
std::vector<std::string> &LinkerFlags) {
156
182
if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) {
157
183
return extractLinkerFlagsFromObjectFile (ObjectFile, LinkerFlags, Instance);
184
+ } else if (auto *ObjectFile =
185
+ llvm::dyn_cast<llvm::object::WasmObjectFile>(Bin)) {
186
+ return extractLinkerFlagsFromObjectFile (ObjectFile, LinkerFlags, Instance);
158
187
} else if (auto *Archive = llvm::dyn_cast<llvm::object::Archive>(Bin)) {
159
188
llvm::Error Error = llvm::Error::success ();
160
189
for (const auto &Child : Archive->children (Error)) {
0 commit comments