IFSHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //===- IFSHandler.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 "llvm/InterfaceStub/IFSHandler.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. #include "llvm/ADT/StringRef.h"
  11. #include "llvm/ADT/StringSwitch.h"
  12. #include "llvm/ADT/Triple.h"
  13. #include "llvm/BinaryFormat/ELF.h"
  14. #include "llvm/InterfaceStub/IFSStub.h"
  15. #include "llvm/Support/Error.h"
  16. #include "llvm/Support/GlobPattern.h"
  17. #include "llvm/Support/LineIterator.h"
  18. #include "llvm/Support/YAMLTraits.h"
  19. #include <functional>
  20. #include <optional>
  21. using namespace llvm;
  22. using namespace llvm::ifs;
  23. LLVM_YAML_IS_SEQUENCE_VECTOR(IFSSymbol)
  24. namespace llvm {
  25. namespace yaml {
  26. /// YAML traits for ELFSymbolType.
  27. template <> struct ScalarEnumerationTraits<IFSSymbolType> {
  28. static void enumeration(IO &IO, IFSSymbolType &SymbolType) {
  29. IO.enumCase(SymbolType, "NoType", IFSSymbolType::NoType);
  30. IO.enumCase(SymbolType, "Func", IFSSymbolType::Func);
  31. IO.enumCase(SymbolType, "Object", IFSSymbolType::Object);
  32. IO.enumCase(SymbolType, "TLS", IFSSymbolType::TLS);
  33. IO.enumCase(SymbolType, "Unknown", IFSSymbolType::Unknown);
  34. // Treat other symbol types as noise, and map to Unknown.
  35. if (!IO.outputting() && IO.matchEnumFallback())
  36. SymbolType = IFSSymbolType::Unknown;
  37. }
  38. };
  39. template <> struct ScalarTraits<IFSEndiannessType> {
  40. static void output(const IFSEndiannessType &Value, void *,
  41. llvm::raw_ostream &Out) {
  42. switch (Value) {
  43. case IFSEndiannessType::Big:
  44. Out << "big";
  45. break;
  46. case IFSEndiannessType::Little:
  47. Out << "little";
  48. break;
  49. default:
  50. llvm_unreachable("Unsupported endianness");
  51. }
  52. }
  53. static StringRef input(StringRef Scalar, void *, IFSEndiannessType &Value) {
  54. Value = StringSwitch<IFSEndiannessType>(Scalar)
  55. .Case("big", IFSEndiannessType::Big)
  56. .Case("little", IFSEndiannessType::Little)
  57. .Default(IFSEndiannessType::Unknown);
  58. if (Value == IFSEndiannessType::Unknown) {
  59. return "Unsupported endianness";
  60. }
  61. return StringRef();
  62. }
  63. static QuotingType mustQuote(StringRef) { return QuotingType::None; }
  64. };
  65. template <> struct ScalarTraits<IFSBitWidthType> {
  66. static void output(const IFSBitWidthType &Value, void *,
  67. llvm::raw_ostream &Out) {
  68. switch (Value) {
  69. case IFSBitWidthType::IFS32:
  70. Out << "32";
  71. break;
  72. case IFSBitWidthType::IFS64:
  73. Out << "64";
  74. break;
  75. default:
  76. llvm_unreachable("Unsupported bit width");
  77. }
  78. }
  79. static StringRef input(StringRef Scalar, void *, IFSBitWidthType &Value) {
  80. Value = StringSwitch<IFSBitWidthType>(Scalar)
  81. .Case("32", IFSBitWidthType::IFS32)
  82. .Case("64", IFSBitWidthType::IFS64)
  83. .Default(IFSBitWidthType::Unknown);
  84. if (Value == IFSBitWidthType::Unknown) {
  85. return "Unsupported bit width";
  86. }
  87. return StringRef();
  88. }
  89. static QuotingType mustQuote(StringRef) { return QuotingType::None; }
  90. };
  91. template <> struct MappingTraits<IFSTarget> {
  92. static void mapping(IO &IO, IFSTarget &Target) {
  93. IO.mapOptional("ObjectFormat", Target.ObjectFormat);
  94. IO.mapOptional("Arch", Target.ArchString);
  95. IO.mapOptional("Endianness", Target.Endianness);
  96. IO.mapOptional("BitWidth", Target.BitWidth);
  97. }
  98. // Compacts symbol information into a single line.
  99. static const bool flow = true; // NOLINT(readability-identifier-naming)
  100. };
  101. /// YAML traits for ELFSymbol.
  102. template <> struct MappingTraits<IFSSymbol> {
  103. static void mapping(IO &IO, IFSSymbol &Symbol) {
  104. IO.mapRequired("Name", Symbol.Name);
  105. IO.mapRequired("Type", Symbol.Type);
  106. // The need for symbol size depends on the symbol type.
  107. if (Symbol.Type == IFSSymbolType::NoType) {
  108. // Size is None, so we are reading it in, or it is non 0 so we
  109. // should emit it.
  110. if (!Symbol.Size || *Symbol.Size)
  111. IO.mapOptional("Size", Symbol.Size);
  112. } else if (Symbol.Type != IFSSymbolType::Func) {
  113. IO.mapOptional("Size", Symbol.Size);
  114. }
  115. IO.mapOptional("Undefined", Symbol.Undefined, false);
  116. IO.mapOptional("Weak", Symbol.Weak, false);
  117. IO.mapOptional("Warning", Symbol.Warning);
  118. }
  119. // Compacts symbol information into a single line.
  120. static const bool flow = true; // NOLINT(readability-identifier-naming)
  121. };
  122. /// YAML traits for ELFStub objects.
  123. template <> struct MappingTraits<IFSStub> {
  124. static void mapping(IO &IO, IFSStub &Stub) {
  125. if (!IO.mapTag("!ifs-v1", true))
  126. IO.setError("Not a .tbe YAML file.");
  127. IO.mapRequired("IfsVersion", Stub.IfsVersion);
  128. IO.mapOptional("SoName", Stub.SoName);
  129. IO.mapOptional("Target", Stub.Target);
  130. IO.mapOptional("NeededLibs", Stub.NeededLibs);
  131. IO.mapRequired("Symbols", Stub.Symbols);
  132. }
  133. };
  134. /// YAML traits for ELFStubTriple objects.
  135. template <> struct MappingTraits<IFSStubTriple> {
  136. static void mapping(IO &IO, IFSStubTriple &Stub) {
  137. if (!IO.mapTag("!ifs-v1", true))
  138. IO.setError("Not a .tbe YAML file.");
  139. IO.mapRequired("IfsVersion", Stub.IfsVersion);
  140. IO.mapOptional("SoName", Stub.SoName);
  141. IO.mapOptional("Target", Stub.Target.Triple);
  142. IO.mapOptional("NeededLibs", Stub.NeededLibs);
  143. IO.mapRequired("Symbols", Stub.Symbols);
  144. }
  145. };
  146. } // end namespace yaml
  147. } // end namespace llvm
  148. /// Attempt to determine if a Text stub uses target triple.
  149. bool usesTriple(StringRef Buf) {
  150. for (line_iterator I(MemoryBufferRef(Buf, "ELFStub")); !I.is_at_eof(); ++I) {
  151. StringRef Line = (*I).trim();
  152. if (Line.startswith("Target:")) {
  153. if (Line == "Target:" || Line.contains("{")) {
  154. return false;
  155. }
  156. }
  157. }
  158. return true;
  159. }
  160. Expected<std::unique_ptr<IFSStub>> ifs::readIFSFromBuffer(StringRef Buf) {
  161. yaml::Input YamlIn(Buf);
  162. std::unique_ptr<IFSStubTriple> Stub(new IFSStubTriple());
  163. if (usesTriple(Buf)) {
  164. YamlIn >> *Stub;
  165. } else {
  166. YamlIn >> *static_cast<IFSStub *>(Stub.get());
  167. }
  168. if (std::error_code Err = YamlIn.error()) {
  169. return createStringError(Err, "YAML failed reading as IFS");
  170. }
  171. if (Stub->IfsVersion > IFSVersionCurrent)
  172. return make_error<StringError>(
  173. "IFS version " + Stub->IfsVersion.getAsString() + " is unsupported.",
  174. std::make_error_code(std::errc::invalid_argument));
  175. if (Stub->Target.ArchString) {
  176. Stub->Target.Arch =
  177. ELF::convertArchNameToEMachine(*Stub->Target.ArchString);
  178. }
  179. return std::move(Stub);
  180. }
  181. Error ifs::writeIFSToOutputStream(raw_ostream &OS, const IFSStub &Stub) {
  182. yaml::Output YamlOut(OS, nullptr, /*WrapColumn =*/0);
  183. std::unique_ptr<IFSStubTriple> CopyStub(new IFSStubTriple(Stub));
  184. if (Stub.Target.Arch) {
  185. CopyStub->Target.ArchString =
  186. std::string(ELF::convertEMachineToArchName(*Stub.Target.Arch));
  187. }
  188. IFSTarget Target = Stub.Target;
  189. if (CopyStub->Target.Triple ||
  190. (!CopyStub->Target.ArchString && !CopyStub->Target.Endianness &&
  191. !CopyStub->Target.BitWidth))
  192. YamlOut << *CopyStub;
  193. else
  194. YamlOut << *static_cast<IFSStub *>(CopyStub.get());
  195. return Error::success();
  196. }
  197. Error ifs::overrideIFSTarget(
  198. IFSStub &Stub, std::optional<IFSArch> OverrideArch,
  199. std::optional<IFSEndiannessType> OverrideEndianness,
  200. std::optional<IFSBitWidthType> OverrideBitWidth,
  201. std::optional<std::string> OverrideTriple) {
  202. std::error_code OverrideEC(1, std::generic_category());
  203. if (OverrideArch) {
  204. if (Stub.Target.Arch && *Stub.Target.Arch != *OverrideArch) {
  205. return make_error<StringError>(
  206. "Supplied Arch conflicts with the text stub", OverrideEC);
  207. }
  208. Stub.Target.Arch = *OverrideArch;
  209. }
  210. if (OverrideEndianness) {
  211. if (Stub.Target.Endianness &&
  212. *Stub.Target.Endianness != *OverrideEndianness) {
  213. return make_error<StringError>(
  214. "Supplied Endianness conflicts with the text stub", OverrideEC);
  215. }
  216. Stub.Target.Endianness = *OverrideEndianness;
  217. }
  218. if (OverrideBitWidth) {
  219. if (Stub.Target.BitWidth && *Stub.Target.BitWidth != *OverrideBitWidth) {
  220. return make_error<StringError>(
  221. "Supplied BitWidth conflicts with the text stub", OverrideEC);
  222. }
  223. Stub.Target.BitWidth = *OverrideBitWidth;
  224. }
  225. if (OverrideTriple) {
  226. if (Stub.Target.Triple && *Stub.Target.Triple != *OverrideTriple) {
  227. return make_error<StringError>(
  228. "Supplied Triple conflicts with the text stub", OverrideEC);
  229. }
  230. Stub.Target.Triple = *OverrideTriple;
  231. }
  232. return Error::success();
  233. }
  234. Error ifs::validateIFSTarget(IFSStub &Stub, bool ParseTriple) {
  235. std::error_code ValidationEC(1, std::generic_category());
  236. if (Stub.Target.Triple) {
  237. if (Stub.Target.Arch || Stub.Target.BitWidth || Stub.Target.Endianness ||
  238. Stub.Target.ObjectFormat) {
  239. return make_error<StringError>(
  240. "Target triple cannot be used simultaneously with ELF target format",
  241. ValidationEC);
  242. }
  243. if (ParseTriple) {
  244. IFSTarget TargetFromTriple = parseTriple(*Stub.Target.Triple);
  245. Stub.Target.Arch = TargetFromTriple.Arch;
  246. Stub.Target.BitWidth = TargetFromTriple.BitWidth;
  247. Stub.Target.Endianness = TargetFromTriple.Endianness;
  248. }
  249. return Error::success();
  250. }
  251. if (!Stub.Target.Arch || !Stub.Target.BitWidth || !Stub.Target.Endianness) {
  252. // TODO: unify the error message.
  253. if (!Stub.Target.Arch) {
  254. return make_error<StringError>("Arch is not defined in the text stub",
  255. ValidationEC);
  256. }
  257. if (!Stub.Target.BitWidth) {
  258. return make_error<StringError>("BitWidth is not defined in the text stub",
  259. ValidationEC);
  260. }
  261. if (!Stub.Target.Endianness) {
  262. return make_error<StringError>(
  263. "Endianness is not defined in the text stub", ValidationEC);
  264. }
  265. }
  266. return Error::success();
  267. }
  268. IFSTarget ifs::parseTriple(StringRef TripleStr) {
  269. Triple IFSTriple(TripleStr);
  270. IFSTarget RetTarget;
  271. // TODO: Implement a Triple Arch enum to e_machine map.
  272. switch (IFSTriple.getArch()) {
  273. case Triple::ArchType::aarch64:
  274. RetTarget.Arch = (IFSArch)ELF::EM_AARCH64;
  275. break;
  276. case Triple::ArchType::x86_64:
  277. RetTarget.Arch = (IFSArch)ELF::EM_X86_64;
  278. break;
  279. default:
  280. RetTarget.Arch = (IFSArch)ELF::EM_NONE;
  281. }
  282. RetTarget.Endianness = IFSTriple.isLittleEndian() ? IFSEndiannessType::Little
  283. : IFSEndiannessType::Big;
  284. RetTarget.BitWidth =
  285. IFSTriple.isArch64Bit() ? IFSBitWidthType::IFS64 : IFSBitWidthType::IFS32;
  286. return RetTarget;
  287. }
  288. void ifs::stripIFSTarget(IFSStub &Stub, bool StripTriple, bool StripArch,
  289. bool StripEndianness, bool StripBitWidth) {
  290. if (StripTriple || StripArch) {
  291. Stub.Target.Arch.reset();
  292. Stub.Target.ArchString.reset();
  293. }
  294. if (StripTriple || StripEndianness) {
  295. Stub.Target.Endianness.reset();
  296. }
  297. if (StripTriple || StripBitWidth) {
  298. Stub.Target.BitWidth.reset();
  299. }
  300. if (StripTriple) {
  301. Stub.Target.Triple.reset();
  302. }
  303. if (!Stub.Target.Arch && !Stub.Target.BitWidth && !Stub.Target.Endianness) {
  304. Stub.Target.ObjectFormat.reset();
  305. }
  306. }
  307. Error ifs::filterIFSSyms(IFSStub &Stub, bool StripUndefined,
  308. const std::vector<std::string> &Exclude) {
  309. std::function<bool(const IFSSymbol &)> Filter = [](const IFSSymbol &) {
  310. return false;
  311. };
  312. if (StripUndefined) {
  313. Filter = [Filter](const IFSSymbol &Sym) {
  314. return Sym.Undefined || Filter(Sym);
  315. };
  316. }
  317. for (StringRef Glob : Exclude) {
  318. Expected<llvm::GlobPattern> PatternOrErr = llvm::GlobPattern::create(Glob);
  319. if (!PatternOrErr)
  320. return PatternOrErr.takeError();
  321. Filter = [Pattern = *PatternOrErr, Filter](const IFSSymbol &Sym) {
  322. return Pattern.match(Sym.Name) || Filter(Sym);
  323. };
  324. }
  325. llvm::erase_if(Stub.Symbols, Filter);
  326. return Error::success();
  327. }