llvm-ifs.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. //===- llvm-ifs.cpp -------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===-----------------------------------------------------------------------===/
  8. #include "ErrorCollector.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/ADT/StringSwitch.h"
  11. #include "llvm/ADT/Triple.h"
  12. #include "llvm/InterfaceStub/ELFObjHandler.h"
  13. #include "llvm/InterfaceStub/IFSHandler.h"
  14. #include "llvm/InterfaceStub/IFSStub.h"
  15. #include "llvm/ObjectYAML/yaml2obj.h"
  16. #include "llvm/Support/CommandLine.h"
  17. #include "llvm/Support/Debug.h"
  18. #include "llvm/Support/Errc.h"
  19. #include "llvm/Support/Error.h"
  20. #include "llvm/Support/FileOutputBuffer.h"
  21. #include "llvm/Support/MemoryBuffer.h"
  22. #include "llvm/Support/Path.h"
  23. #include "llvm/Support/VersionTuple.h"
  24. #include "llvm/Support/WithColor.h"
  25. #include "llvm/Support/YAMLTraits.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include "llvm/TextAPI/InterfaceFile.h"
  28. #include "llvm/TextAPI/TextAPIReader.h"
  29. #include "llvm/TextAPI/TextAPIWriter.h"
  30. #include <set>
  31. #include <string>
  32. #include <vector>
  33. using namespace llvm;
  34. using namespace llvm::yaml;
  35. using namespace llvm::MachO;
  36. using namespace llvm::ifs;
  37. #define DEBUG_TYPE "llvm-ifs"
  38. namespace {
  39. const VersionTuple IfsVersionCurrent(3, 0);
  40. enum class FileFormat { IFS, ELF, TBD };
  41. } // end anonymous namespace
  42. cl::OptionCategory IfsCategory("Ifs Options");
  43. // TODO: Use OptTable for option parsing in the future.
  44. // Command line flags:
  45. cl::list<std::string> InputFilePaths(cl::Positional, cl::desc("input"),
  46. cl::ZeroOrMore, cl::cat(IfsCategory));
  47. cl::opt<FileFormat> InputFormat(
  48. "input-format", cl::desc("Specify the input file format"),
  49. cl::values(clEnumValN(FileFormat::IFS, "IFS", "Text based ELF stub file"),
  50. clEnumValN(FileFormat::ELF, "ELF", "ELF object file")),
  51. cl::cat(IfsCategory));
  52. cl::opt<FileFormat> OutputFormat(
  53. "output-format", cl::desc("Specify the output file format **DEPRECATED**"),
  54. cl::values(clEnumValN(FileFormat::IFS, "IFS", "Text based ELF stub file"),
  55. clEnumValN(FileFormat::ELF, "ELF", "ELF stub file"),
  56. clEnumValN(FileFormat::TBD, "TBD", "Apple TBD text stub file")),
  57. cl::cat(IfsCategory));
  58. cl::opt<std::string> OptArch("arch",
  59. cl::desc("Specify the architecture, e.g. x86_64"),
  60. cl::cat(IfsCategory));
  61. cl::opt<IFSBitWidthType>
  62. OptBitWidth("bitwidth", cl::desc("Specify the bit width"),
  63. cl::values(clEnumValN(IFSBitWidthType::IFS32, "32", "32 bits"),
  64. clEnumValN(IFSBitWidthType::IFS64, "64", "64 bits")),
  65. cl::cat(IfsCategory));
  66. cl::opt<IFSEndiannessType> OptEndianness(
  67. "endianness", cl::desc("Specify the endianness"),
  68. cl::values(clEnumValN(IFSEndiannessType::Little, "little", "Little Endian"),
  69. clEnumValN(IFSEndiannessType::Big, "big", "Big Endian")),
  70. cl::cat(IfsCategory));
  71. cl::opt<std::string> OptTargetTriple(
  72. "target", cl::desc("Specify the target triple, e.g. x86_64-linux-gnu"),
  73. cl::cat(IfsCategory));
  74. cl::opt<std::string> OptTargetTripleHint(
  75. "hint-ifs-target",
  76. cl::desc("When --output-format is 'IFS', this flag will hint the expected "
  77. "target triple for IFS output"),
  78. cl::cat(IfsCategory));
  79. cl::opt<bool> StripIFSArch(
  80. "strip-ifs-arch",
  81. cl::desc("Strip target architecture information away from IFS output"),
  82. cl::cat(IfsCategory));
  83. cl::opt<bool> StripIFSBitWidth(
  84. "strip-ifs-bitwidth",
  85. cl::desc("Strip target bit width information away from IFS output"),
  86. cl::cat(IfsCategory));
  87. cl::opt<bool> StripIFSEndiannessWidth(
  88. "strip-ifs-endianness",
  89. cl::desc("Strip target endianness information away from IFS output"),
  90. cl::cat(IfsCategory));
  91. cl::opt<bool> StripIFSTarget(
  92. "strip-ifs-target",
  93. cl::desc("Strip all target information away from IFS output"),
  94. cl::cat(IfsCategory));
  95. cl::opt<bool>
  96. StripUndefined("strip-undefined",
  97. cl::desc("Strip undefined symbols from IFS output"),
  98. cl::cat(IfsCategory));
  99. cl::opt<std::string>
  100. SoName("soname",
  101. cl::desc("Manually set the DT_SONAME entry of any emitted files"),
  102. cl::value_desc("name"), cl::cat(IfsCategory));
  103. cl::opt<std::string> OutputFilePath("output",
  104. cl::desc("Output file **DEPRECATED**"),
  105. cl::cat(IfsCategory));
  106. cl::alias OutputFilePathA("o", cl::desc("Alias for --output"),
  107. cl::aliasopt(OutputFilePath), cl::cat(IfsCategory));
  108. cl::opt<std::string> OutputELFFilePath("output-elf",
  109. cl::desc("Output path for ELF file"),
  110. cl::cat(IfsCategory));
  111. cl::opt<std::string> OutputIFSFilePath("output-ifs",
  112. cl::desc("Output path for IFS file"),
  113. cl::cat(IfsCategory));
  114. cl::opt<std::string> OutputTBDFilePath("output-tbd",
  115. cl::desc("Output path for TBD file"),
  116. cl::cat(IfsCategory));
  117. cl::opt<bool> WriteIfChanged(
  118. "write-if-changed",
  119. cl::desc("Write the output file only if it is new or has changed."),
  120. cl::cat(IfsCategory));
  121. static std::string getTypeName(IFSSymbolType Type) {
  122. switch (Type) {
  123. case IFSSymbolType::NoType:
  124. return "NoType";
  125. case IFSSymbolType::Func:
  126. return "Func";
  127. case IFSSymbolType::Object:
  128. return "Object";
  129. case IFSSymbolType::TLS:
  130. return "TLS";
  131. case IFSSymbolType::Unknown:
  132. return "Unknown";
  133. }
  134. llvm_unreachable("Unexpected ifs symbol type.");
  135. }
  136. static Expected<std::unique_ptr<IFSStub>> readInputFile(StringRef FilePath) {
  137. // Read in file.
  138. ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrError =
  139. MemoryBuffer::getFileOrSTDIN(FilePath, /*IsText=*/true);
  140. if (!BufOrError)
  141. return createStringError(BufOrError.getError(), "Could not open `%s`",
  142. FilePath.data());
  143. std::unique_ptr<MemoryBuffer> FileReadBuffer = std::move(*BufOrError);
  144. ErrorCollector EC(/*UseFatalErrors=*/false);
  145. // First try to read as a binary (fails fast if not binary).
  146. if (InputFormat.getNumOccurrences() == 0 || InputFormat == FileFormat::ELF) {
  147. Expected<std::unique_ptr<IFSStub>> StubFromELF =
  148. readELFFile(FileReadBuffer->getMemBufferRef());
  149. if (StubFromELF) {
  150. (*StubFromELF)->IfsVersion = IfsVersionCurrent;
  151. return std::move(*StubFromELF);
  152. }
  153. EC.addError(StubFromELF.takeError(), "BinaryRead");
  154. }
  155. // Fall back to reading as a ifs.
  156. if (InputFormat.getNumOccurrences() == 0 || InputFormat == FileFormat::IFS) {
  157. Expected<std::unique_ptr<IFSStub>> StubFromIFS =
  158. readIFSFromBuffer(FileReadBuffer->getBuffer());
  159. if (StubFromIFS) {
  160. if ((*StubFromIFS)->IfsVersion > IfsVersionCurrent)
  161. EC.addError(
  162. createStringError(errc::not_supported,
  163. "IFS version " +
  164. (*StubFromIFS)->IfsVersion.getAsString() +
  165. " is unsupported."),
  166. "ReadInputFile");
  167. else
  168. return std::move(*StubFromIFS);
  169. } else {
  170. EC.addError(StubFromIFS.takeError(), "YamlParse");
  171. }
  172. }
  173. // If both readers fail, build a new error that includes all information.
  174. EC.addError(createStringError(errc::not_supported,
  175. "No file readers succeeded reading `%s` "
  176. "(unsupported/malformed file?)",
  177. FilePath.data()),
  178. "ReadInputFile");
  179. EC.escalateToFatal();
  180. return EC.makeError();
  181. }
  182. static int writeTbdStub(const Triple &T, const std::vector<IFSSymbol> &Symbols,
  183. const StringRef Format, raw_ostream &Out) {
  184. auto PlatformTypeOrError =
  185. [](const llvm::Triple &T) -> llvm::Expected<llvm::MachO::PlatformType> {
  186. if (T.isMacOSX())
  187. return llvm::MachO::PLATFORM_MACOS;
  188. if (T.isTvOS())
  189. return llvm::MachO::PLATFORM_TVOS;
  190. if (T.isWatchOS())
  191. return llvm::MachO::PLATFORM_WATCHOS;
  192. // Note: put isiOS last because tvOS and watchOS are also iOS according
  193. // to the Triple.
  194. if (T.isiOS())
  195. return llvm::MachO::PLATFORM_IOS;
  196. return createStringError(errc::not_supported, "Invalid Platform.\n");
  197. }(T);
  198. if (!PlatformTypeOrError)
  199. return -1;
  200. PlatformType Plat = PlatformTypeOrError.get();
  201. TargetList Targets({Target(llvm::MachO::mapToArchitecture(T), Plat)});
  202. InterfaceFile File;
  203. File.setFileType(FileType::TBD_V3); // Only supporting v3 for now.
  204. File.addTargets(Targets);
  205. for (const auto &Symbol : Symbols) {
  206. auto Name = Symbol.Name;
  207. auto Kind = SymbolKind::GlobalSymbol;
  208. switch (Symbol.Type) {
  209. default:
  210. case IFSSymbolType::NoType:
  211. Kind = SymbolKind::GlobalSymbol;
  212. break;
  213. case IFSSymbolType::Object:
  214. Kind = SymbolKind::GlobalSymbol;
  215. break;
  216. case IFSSymbolType::Func:
  217. Kind = SymbolKind::GlobalSymbol;
  218. break;
  219. }
  220. if (Symbol.Weak)
  221. File.addSymbol(Kind, Name, Targets, SymbolFlags::WeakDefined);
  222. else
  223. File.addSymbol(Kind, Name, Targets);
  224. }
  225. SmallString<4096> Buffer;
  226. raw_svector_ostream OS(Buffer);
  227. if (Error Result = TextAPIWriter::writeToStream(OS, File))
  228. return -1;
  229. Out << OS.str();
  230. return 0;
  231. }
  232. static void fatalError(Error Err) {
  233. WithColor::defaultErrorHandler(std::move(Err));
  234. exit(1);
  235. }
  236. /// writeIFS() writes a Text-Based ELF stub to a file using the latest version
  237. /// of the YAML parser.
  238. static Error writeIFS(StringRef FilePath, IFSStub &Stub) {
  239. // Write IFS to memory first.
  240. std::string IFSStr;
  241. raw_string_ostream OutStr(IFSStr);
  242. Error YAMLErr = writeIFSToOutputStream(OutStr, Stub);
  243. if (YAMLErr)
  244. return YAMLErr;
  245. OutStr.flush();
  246. if (WriteIfChanged) {
  247. if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrError =
  248. MemoryBuffer::getFile(FilePath)) {
  249. // Compare IFS output with the existing IFS file. If unchanged, avoid changing the file.
  250. if ((*BufOrError)->getBuffer() == IFSStr)
  251. return Error::success();
  252. }
  253. }
  254. // Open IFS file for writing.
  255. std::error_code SysErr;
  256. raw_fd_ostream Out(FilePath, SysErr);
  257. if (SysErr)
  258. return createStringError(SysErr, "Couldn't open `%s` for writing",
  259. FilePath.data());
  260. Out << IFSStr;
  261. return Error::success();
  262. }
  263. int main(int argc, char *argv[]) {
  264. // Parse arguments.
  265. cl::HideUnrelatedOptions({&IfsCategory, &getColorCategory()});
  266. cl::ParseCommandLineOptions(argc, argv);
  267. if (InputFilePaths.empty())
  268. InputFilePaths.push_back("-");
  269. // If input files are more than one, they can only be IFS files.
  270. if (InputFilePaths.size() > 1)
  271. InputFormat.setValue(FileFormat::IFS);
  272. // Attempt to merge input.
  273. IFSStub Stub;
  274. std::map<std::string, IFSSymbol> SymbolMap;
  275. std::string PreviousInputFilePath;
  276. for (const std::string &InputFilePath : InputFilePaths) {
  277. Expected<std::unique_ptr<IFSStub>> StubOrErr = readInputFile(InputFilePath);
  278. if (!StubOrErr)
  279. fatalError(StubOrErr.takeError());
  280. std::unique_ptr<IFSStub> TargetStub = std::move(StubOrErr.get());
  281. if (PreviousInputFilePath.empty()) {
  282. Stub.IfsVersion = TargetStub->IfsVersion;
  283. Stub.Target = TargetStub->Target;
  284. Stub.SoName = TargetStub->SoName;
  285. Stub.NeededLibs = TargetStub->NeededLibs;
  286. } else {
  287. if (Stub.IfsVersion != TargetStub->IfsVersion) {
  288. if (Stub.IfsVersion.getMajor() != IfsVersionCurrent.getMajor()) {
  289. WithColor::error()
  290. << "Interface Stub: IfsVersion Mismatch."
  291. << "\nFilenames: " << PreviousInputFilePath << " "
  292. << InputFilePath << "\nIfsVersion Values: " << Stub.IfsVersion
  293. << " " << TargetStub->IfsVersion << "\n";
  294. return -1;
  295. }
  296. if (TargetStub->IfsVersion > Stub.IfsVersion)
  297. Stub.IfsVersion = TargetStub->IfsVersion;
  298. }
  299. if (Stub.Target != TargetStub->Target && !TargetStub->Target.empty()) {
  300. WithColor::error() << "Interface Stub: Target Mismatch."
  301. << "\nFilenames: " << PreviousInputFilePath << " "
  302. << InputFilePath;
  303. return -1;
  304. }
  305. if (Stub.SoName != TargetStub->SoName) {
  306. WithColor::error() << "Interface Stub: SoName Mismatch."
  307. << "\nFilenames: " << PreviousInputFilePath << " "
  308. << InputFilePath
  309. << "\nSoName Values: " << Stub.SoName << " "
  310. << TargetStub->SoName << "\n";
  311. return -1;
  312. }
  313. if (Stub.NeededLibs != TargetStub->NeededLibs) {
  314. WithColor::error() << "Interface Stub: NeededLibs Mismatch."
  315. << "\nFilenames: " << PreviousInputFilePath << " "
  316. << InputFilePath << "\n";
  317. return -1;
  318. }
  319. }
  320. for (auto Symbol : TargetStub->Symbols) {
  321. auto SI = SymbolMap.find(Symbol.Name);
  322. if (SI == SymbolMap.end()) {
  323. SymbolMap.insert(
  324. std::pair<std::string, IFSSymbol>(Symbol.Name, Symbol));
  325. continue;
  326. }
  327. assert(Symbol.Name == SI->second.Name && "Symbol Names Must Match.");
  328. // Check conflicts:
  329. if (Symbol.Type != SI->second.Type) {
  330. WithColor::error() << "Interface Stub: Type Mismatch for "
  331. << Symbol.Name << ".\nFilename: " << InputFilePath
  332. << "\nType Values: " << getTypeName(SI->second.Type)
  333. << " " << getTypeName(Symbol.Type) << "\n";
  334. return -1;
  335. }
  336. if (Symbol.Size != SI->second.Size) {
  337. WithColor::error() << "Interface Stub: Size Mismatch for "
  338. << Symbol.Name << ".\nFilename: " << InputFilePath
  339. << "\nSize Values: " << SI->second.Size << " "
  340. << Symbol.Size << "\n";
  341. return -1;
  342. }
  343. if (Symbol.Weak != SI->second.Weak) {
  344. Symbol.Weak = false;
  345. continue;
  346. }
  347. // TODO: Not checking Warning. Will be dropped.
  348. }
  349. PreviousInputFilePath = InputFilePath;
  350. }
  351. if (Stub.IfsVersion != IfsVersionCurrent)
  352. if (Stub.IfsVersion.getMajor() != IfsVersionCurrent.getMajor()) {
  353. WithColor::error() << "Interface Stub: Bad IfsVersion: "
  354. << Stub.IfsVersion << ", llvm-ifs supported version: "
  355. << IfsVersionCurrent << ".\n";
  356. return -1;
  357. }
  358. for (auto &Entry : SymbolMap)
  359. Stub.Symbols.push_back(Entry.second);
  360. // Change SoName before emitting stubs.
  361. if (SoName.getNumOccurrences() == 1)
  362. Stub.SoName = SoName;
  363. Optional<IFSArch> OverrideArch;
  364. Optional<IFSEndiannessType> OverrideEndianness;
  365. Optional<IFSBitWidthType> OverrideBitWidth;
  366. Optional<std::string> OverrideTriple;
  367. if (OptArch.getNumOccurrences() == 1)
  368. OverrideArch = ELF::convertArchNameToEMachine(OptArch.getValue());
  369. if (OptEndianness.getNumOccurrences() == 1)
  370. OverrideEndianness = OptEndianness.getValue();
  371. if (OptBitWidth.getNumOccurrences() == 1)
  372. OverrideBitWidth = OptBitWidth.getValue();
  373. if (OptTargetTriple.getNumOccurrences() == 1)
  374. OverrideTriple = OptTargetTriple.getValue();
  375. Error OverrideError = overrideIFSTarget(
  376. Stub, OverrideArch, OverrideEndianness, OverrideBitWidth, OverrideTriple);
  377. if (OverrideError)
  378. fatalError(std::move(OverrideError));
  379. if (OutputELFFilePath.getNumOccurrences() == 0 &&
  380. OutputIFSFilePath.getNumOccurrences() == 0 &&
  381. OutputTBDFilePath.getNumOccurrences() == 0) {
  382. if (OutputFormat.getNumOccurrences() == 0) {
  383. WithColor::error() << "at least one output should be specified.";
  384. return -1;
  385. }
  386. } else if (OutputFormat.getNumOccurrences() == 1) {
  387. WithColor::error() << "'--output-format' cannot be used with "
  388. "'--output-{FILE_FORMAT}' options at the same time";
  389. return -1;
  390. }
  391. if (OutputFormat.getNumOccurrences() == 1) {
  392. // TODO: Remove OutputFormat flag in the next revision.
  393. WithColor::warning() << "--output-format option is deprecated, please use "
  394. "--output-{FILE_FORMAT} options instead\n";
  395. switch (OutputFormat.getValue()) {
  396. case FileFormat::TBD: {
  397. std::error_code SysErr;
  398. raw_fd_ostream Out(OutputFilePath, SysErr);
  399. if (SysErr) {
  400. WithColor::error() << "Couldn't open " << OutputFilePath
  401. << " for writing.\n";
  402. return -1;
  403. }
  404. if (!Stub.Target.Triple) {
  405. WithColor::error()
  406. << "Triple should be defined when output format is TBD";
  407. return -1;
  408. }
  409. return writeTbdStub(llvm::Triple(Stub.Target.Triple.getValue()),
  410. Stub.Symbols, "TBD", Out);
  411. }
  412. case FileFormat::IFS: {
  413. Stub.IfsVersion = IfsVersionCurrent;
  414. if (InputFormat.getValue() == FileFormat::ELF &&
  415. OptTargetTripleHint.getNumOccurrences() == 1) {
  416. std::error_code HintEC(1, std::generic_category());
  417. IFSTarget HintTarget = parseTriple(OptTargetTripleHint);
  418. if (Stub.Target.Arch.getValue() != HintTarget.Arch.getValue())
  419. fatalError(make_error<StringError>(
  420. "Triple hint does not match the actual architecture", HintEC));
  421. if (Stub.Target.Endianness.getValue() !=
  422. HintTarget.Endianness.getValue())
  423. fatalError(make_error<StringError>(
  424. "Triple hint does not match the actual endianness", HintEC));
  425. if (Stub.Target.BitWidth.getValue() != HintTarget.BitWidth.getValue())
  426. fatalError(make_error<StringError>(
  427. "Triple hint does not match the actual bit width", HintEC));
  428. stripIFSTarget(Stub, true, false, false, false);
  429. Stub.Target.Triple = OptTargetTripleHint.getValue();
  430. } else {
  431. stripIFSTarget(Stub, StripIFSTarget, StripIFSArch,
  432. StripIFSEndiannessWidth, StripIFSBitWidth);
  433. }
  434. if (StripUndefined)
  435. stripIFSUndefinedSymbols(Stub);
  436. Error IFSWriteError = writeIFS(OutputFilePath.getValue(), Stub);
  437. if (IFSWriteError)
  438. fatalError(std::move(IFSWriteError));
  439. break;
  440. }
  441. case FileFormat::ELF: {
  442. Error TargetError = validateIFSTarget(Stub, true);
  443. if (TargetError)
  444. fatalError(std::move(TargetError));
  445. Error BinaryWriteError =
  446. writeBinaryStub(OutputFilePath, Stub, WriteIfChanged);
  447. if (BinaryWriteError)
  448. fatalError(std::move(BinaryWriteError));
  449. break;
  450. }
  451. }
  452. } else {
  453. // Check if output path for individual format.
  454. if (OutputELFFilePath.getNumOccurrences() == 1) {
  455. Error TargetError = validateIFSTarget(Stub, true);
  456. if (TargetError)
  457. fatalError(std::move(TargetError));
  458. Error BinaryWriteError =
  459. writeBinaryStub(OutputELFFilePath, Stub, WriteIfChanged);
  460. if (BinaryWriteError)
  461. fatalError(std::move(BinaryWriteError));
  462. }
  463. if (OutputIFSFilePath.getNumOccurrences() == 1) {
  464. Stub.IfsVersion = IfsVersionCurrent;
  465. if (InputFormat.getValue() == FileFormat::ELF &&
  466. OptTargetTripleHint.getNumOccurrences() == 1) {
  467. std::error_code HintEC(1, std::generic_category());
  468. IFSTarget HintTarget = parseTriple(OptTargetTripleHint);
  469. if (Stub.Target.Arch.getValue() != HintTarget.Arch.getValue())
  470. fatalError(make_error<StringError>(
  471. "Triple hint does not match the actual architecture", HintEC));
  472. if (Stub.Target.Endianness.getValue() !=
  473. HintTarget.Endianness.getValue())
  474. fatalError(make_error<StringError>(
  475. "Triple hint does not match the actual endianness", HintEC));
  476. if (Stub.Target.BitWidth.getValue() != HintTarget.BitWidth.getValue())
  477. fatalError(make_error<StringError>(
  478. "Triple hint does not match the actual bit width", HintEC));
  479. stripIFSTarget(Stub, true, false, false, false);
  480. Stub.Target.Triple = OptTargetTripleHint.getValue();
  481. } else {
  482. stripIFSTarget(Stub, StripIFSTarget, StripIFSArch,
  483. StripIFSEndiannessWidth, StripIFSBitWidth);
  484. }
  485. if (StripUndefined)
  486. stripIFSUndefinedSymbols(Stub);
  487. Error IFSWriteError = writeIFS(OutputIFSFilePath.getValue(), Stub);
  488. if (IFSWriteError)
  489. fatalError(std::move(IFSWriteError));
  490. }
  491. if (OutputTBDFilePath.getNumOccurrences() == 1) {
  492. std::error_code SysErr;
  493. raw_fd_ostream Out(OutputTBDFilePath, SysErr);
  494. if (SysErr) {
  495. WithColor::error() << "Couldn't open " << OutputTBDFilePath
  496. << " for writing.\n";
  497. return -1;
  498. }
  499. if (!Stub.Target.Triple) {
  500. WithColor::error()
  501. << "Triple should be defined when output format is TBD";
  502. return -1;
  503. }
  504. return writeTbdStub(llvm::Triple(Stub.Target.Triple.getValue()),
  505. Stub.Symbols, "TBD", Out);
  506. }
  507. }
  508. return 0;
  509. }