|
31 | 31 | #include "llvm/Option/Option.h" |
32 | 32 | #include "llvm/Object/Archive.h" |
33 | 33 | #include "llvm/Object/ObjectFile.h" |
| 34 | +#include "llvm/Object/COFF.h" |
34 | 35 | #include "llvm/Object/ELFObjectFile.h" |
35 | 36 |
|
36 | 37 | using namespace swift; |
@@ -106,32 +107,44 @@ class AutolinkExtractInvocation { |
106 | 107 | } |
107 | 108 | }; |
108 | 109 |
|
109 | | -// Look inside the binary 'Bin' and append any linker flags found in its |
110 | | -// ".swift1_autolink_entries" section to 'LinkerFlags'. If 'Bin' is an archive, |
111 | | -// recursively look inside all children within the archive. Return 'true' if |
112 | | -// there was an error, and 'false' otherwise. |
| 110 | +/// Look inside the object file 'ObjectFile' and append any linker flags found in |
| 111 | +/// its ".swift1_autolink_entries" section to 'LinkerFlags'. |
| 112 | +static void |
| 113 | +extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile, |
| 114 | + std::vector<std::string> &LinkerFlags) { |
| 115 | + // Search for the section we hold autolink entries in |
| 116 | + for (auto &Section : ObjectFile->sections()) { |
| 117 | + llvm::StringRef SectionName; |
| 118 | + Section.getName(SectionName); |
| 119 | + if (SectionName == ".swift1_autolink_entries") { |
| 120 | + llvm::StringRef SectionData; |
| 121 | + Section.getContents(SectionData); |
| 122 | + |
| 123 | + // entries are null-terminated, so extract them and push them into |
| 124 | + // the set. |
| 125 | + llvm::SmallVector<llvm::StringRef, 4> SplitFlags; |
| 126 | + SectionData.split(SplitFlags, llvm::StringRef("\0", 1), -1, |
| 127 | + /*KeepEmpty=*/false); |
| 128 | + for (const auto &Flag : SplitFlags) |
| 129 | + LinkerFlags.push_back(Flag); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/// Look inside the binary 'Bin' and append any linker flags found in its |
| 135 | +/// ".swift1_autolink_entries" section to 'LinkerFlags'. If 'Bin' is an archive, |
| 136 | +/// recursively look inside all children within the archive. Return 'true' if |
| 137 | +/// there was an error, and 'false' otherwise. |
113 | 138 | static bool extractLinkerFlags(const llvm::object::Binary *Bin, |
114 | 139 | CompilerInstance &Instance, |
115 | 140 | StringRef BinaryFileName, |
116 | 141 | std::vector<std::string> &LinkerFlags) { |
117 | 142 | if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) { |
118 | | - // Search for the section we hold autolink entries in |
119 | | - for (auto &Section : ObjectFile->sections()) { |
120 | | - llvm::StringRef SectionName; |
121 | | - Section.getName(SectionName); |
122 | | - if (SectionName == ".swift1_autolink_entries") { |
123 | | - llvm::StringRef SectionData; |
124 | | - Section.getContents(SectionData); |
125 | | - |
126 | | - // entries are null-terminated, so extract them and push them into |
127 | | - // the set. |
128 | | - llvm::SmallVector<llvm::StringRef, 4> SplitFlags; |
129 | | - SectionData.split(SplitFlags, llvm::StringRef("\0", 1), -1, |
130 | | - /*KeepEmpty=*/false); |
131 | | - for (const auto &Flag : SplitFlags) |
132 | | - LinkerFlags.push_back(Flag); |
133 | | - } |
134 | | - } |
| 143 | + extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags); |
| 144 | + return false; |
| 145 | + } else if (auto *ObjectFile = |
| 146 | + llvm::dyn_cast<llvm::object::COFFObjectFile>(Bin)) { |
| 147 | + extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags); |
135 | 148 | return false; |
136 | 149 | } else if (auto *Archive = llvm::dyn_cast<llvm::object::Archive>(Bin)) { |
137 | 150 | for (const auto &Child : Archive->children()) { |
|
0 commit comments