SymbolRewriter.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. //===- SymbolRewriter.cpp - Symbol Rewriter -------------------------------===//
  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. //
  9. // SymbolRewriter is a LLVM pass which can rewrite symbols transparently within
  10. // existing code. It is implemented as a compiler pass and is configured via a
  11. // YAML configuration file.
  12. //
  13. // The YAML configuration file format is as follows:
  14. //
  15. // RewriteMapFile := RewriteDescriptors
  16. // RewriteDescriptors := RewriteDescriptor | RewriteDescriptors
  17. // RewriteDescriptor := RewriteDescriptorType ':' '{' RewriteDescriptorFields '}'
  18. // RewriteDescriptorFields := RewriteDescriptorField | RewriteDescriptorFields
  19. // RewriteDescriptorField := FieldIdentifier ':' FieldValue ','
  20. // RewriteDescriptorType := Identifier
  21. // FieldIdentifier := Identifier
  22. // FieldValue := Identifier
  23. // Identifier := [0-9a-zA-Z]+
  24. //
  25. // Currently, the following descriptor types are supported:
  26. //
  27. // - function: (function rewriting)
  28. // + Source (original name of the function)
  29. // + Target (explicit transformation)
  30. // + Transform (pattern transformation)
  31. // + Naked (boolean, whether the function is undecorated)
  32. // - global variable: (external linkage global variable rewriting)
  33. // + Source (original name of externally visible variable)
  34. // + Target (explicit transformation)
  35. // + Transform (pattern transformation)
  36. // - global alias: (global alias rewriting)
  37. // + Source (original name of the aliased name)
  38. // + Target (explicit transformation)
  39. // + Transform (pattern transformation)
  40. //
  41. // Note that source and exactly one of [Target, Transform] must be provided
  42. //
  43. // New rewrite descriptors can be created. Addding a new rewrite descriptor
  44. // involves:
  45. //
  46. // a) extended the rewrite descriptor kind enumeration
  47. // (<anonymous>::RewriteDescriptor::RewriteDescriptorType)
  48. // b) implementing the new descriptor
  49. // (c.f. <anonymous>::ExplicitRewriteFunctionDescriptor)
  50. // c) extending the rewrite map parser
  51. // (<anonymous>::RewriteMapParser::parseEntry)
  52. //
  53. // Specify to rewrite the symbols using the `-rewrite-symbols` option, and
  54. // specify the map file to use for the rewriting via the `-rewrite-map-file`
  55. // option.
  56. //
  57. //===----------------------------------------------------------------------===//
  58. #include "llvm/Transforms/Utils/SymbolRewriter.h"
  59. #include "llvm/ADT/STLExtras.h"
  60. #include "llvm/ADT/SmallString.h"
  61. #include "llvm/ADT/StringRef.h"
  62. #include "llvm/ADT/ilist.h"
  63. #include "llvm/ADT/iterator_range.h"
  64. #include "llvm/IR/Comdat.h"
  65. #include "llvm/IR/Function.h"
  66. #include "llvm/IR/GlobalAlias.h"
  67. #include "llvm/IR/GlobalObject.h"
  68. #include "llvm/IR/GlobalVariable.h"
  69. #include "llvm/IR/Module.h"
  70. #include "llvm/IR/Value.h"
  71. #include "llvm/InitializePasses.h"
  72. #include "llvm/Pass.h"
  73. #include "llvm/Support/Casting.h"
  74. #include "llvm/Support/CommandLine.h"
  75. #include "llvm/Support/ErrorHandling.h"
  76. #include "llvm/Support/ErrorOr.h"
  77. #include "llvm/Support/MemoryBuffer.h"
  78. #include "llvm/Support/Regex.h"
  79. #include "llvm/Support/SourceMgr.h"
  80. #include "llvm/Support/YAMLParser.h"
  81. #include <memory>
  82. #include <string>
  83. #include <vector>
  84. using namespace llvm;
  85. using namespace SymbolRewriter;
  86. #define DEBUG_TYPE "symbol-rewriter"
  87. static cl::list<std::string> RewriteMapFiles("rewrite-map-file",
  88. cl::desc("Symbol Rewrite Map"),
  89. cl::value_desc("filename"),
  90. cl::Hidden);
  91. static void rewriteComdat(Module &M, GlobalObject *GO,
  92. const std::string &Source,
  93. const std::string &Target) {
  94. if (Comdat *CD = GO->getComdat()) {
  95. auto &Comdats = M.getComdatSymbolTable();
  96. Comdat *C = M.getOrInsertComdat(Target);
  97. C->setSelectionKind(CD->getSelectionKind());
  98. GO->setComdat(C);
  99. Comdats.erase(Comdats.find(Source));
  100. }
  101. }
  102. namespace {
  103. template <RewriteDescriptor::Type DT, typename ValueType,
  104. ValueType *(Module::*Get)(StringRef) const>
  105. class ExplicitRewriteDescriptor : public RewriteDescriptor {
  106. public:
  107. const std::string Source;
  108. const std::string Target;
  109. ExplicitRewriteDescriptor(StringRef S, StringRef T, const bool Naked)
  110. : RewriteDescriptor(DT),
  111. Source(std::string(Naked ? StringRef("\01" + S.str()) : S)),
  112. Target(std::string(T)) {}
  113. bool performOnModule(Module &M) override;
  114. static bool classof(const RewriteDescriptor *RD) {
  115. return RD->getType() == DT;
  116. }
  117. };
  118. } // end anonymous namespace
  119. template <RewriteDescriptor::Type DT, typename ValueType,
  120. ValueType *(Module::*Get)(StringRef) const>
  121. bool ExplicitRewriteDescriptor<DT, ValueType, Get>::performOnModule(Module &M) {
  122. bool Changed = false;
  123. if (ValueType *S = (M.*Get)(Source)) {
  124. if (GlobalObject *GO = dyn_cast<GlobalObject>(S))
  125. rewriteComdat(M, GO, Source, Target);
  126. if (Value *T = (M.*Get)(Target))
  127. S->setValueName(T->getValueName());
  128. else
  129. S->setName(Target);
  130. Changed = true;
  131. }
  132. return Changed;
  133. }
  134. namespace {
  135. template <RewriteDescriptor::Type DT, typename ValueType,
  136. ValueType *(Module::*Get)(StringRef) const,
  137. iterator_range<typename iplist<ValueType>::iterator>
  138. (Module::*Iterator)()>
  139. class PatternRewriteDescriptor : public RewriteDescriptor {
  140. public:
  141. const std::string Pattern;
  142. const std::string Transform;
  143. PatternRewriteDescriptor(StringRef P, StringRef T)
  144. : RewriteDescriptor(DT), Pattern(std::string(P)),
  145. Transform(std::string(T)) {}
  146. bool performOnModule(Module &M) override;
  147. static bool classof(const RewriteDescriptor *RD) {
  148. return RD->getType() == DT;
  149. }
  150. };
  151. } // end anonymous namespace
  152. template <RewriteDescriptor::Type DT, typename ValueType,
  153. ValueType *(Module::*Get)(StringRef) const,
  154. iterator_range<typename iplist<ValueType>::iterator>
  155. (Module::*Iterator)()>
  156. bool PatternRewriteDescriptor<DT, ValueType, Get, Iterator>::
  157. performOnModule(Module &M) {
  158. bool Changed = false;
  159. for (auto &C : (M.*Iterator)()) {
  160. std::string Error;
  161. std::string Name = Regex(Pattern).sub(Transform, C.getName(), &Error);
  162. if (!Error.empty())
  163. report_fatal_error(Twine("unable to transforn ") + C.getName() + " in " +
  164. M.getModuleIdentifier() + ": " + Error);
  165. if (C.getName() == Name)
  166. continue;
  167. if (GlobalObject *GO = dyn_cast<GlobalObject>(&C))
  168. rewriteComdat(M, GO, std::string(C.getName()), Name);
  169. if (Value *V = (M.*Get)(Name))
  170. C.setValueName(V->getValueName());
  171. else
  172. C.setName(Name);
  173. Changed = true;
  174. }
  175. return Changed;
  176. }
  177. namespace {
  178. /// Represents a rewrite for an explicitly named (function) symbol. Both the
  179. /// source function name and target function name of the transformation are
  180. /// explicitly spelt out.
  181. using ExplicitRewriteFunctionDescriptor =
  182. ExplicitRewriteDescriptor<RewriteDescriptor::Type::Function, Function,
  183. &Module::getFunction>;
  184. /// Represents a rewrite for an explicitly named (global variable) symbol. Both
  185. /// the source variable name and target variable name are spelt out. This
  186. /// applies only to module level variables.
  187. using ExplicitRewriteGlobalVariableDescriptor =
  188. ExplicitRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
  189. GlobalVariable, &Module::getGlobalVariable>;
  190. /// Represents a rewrite for an explicitly named global alias. Both the source
  191. /// and target name are explicitly spelt out.
  192. using ExplicitRewriteNamedAliasDescriptor =
  193. ExplicitRewriteDescriptor<RewriteDescriptor::Type::NamedAlias, GlobalAlias,
  194. &Module::getNamedAlias>;
  195. /// Represents a rewrite for a regular expression based pattern for functions.
  196. /// A pattern for the function name is provided and a transformation for that
  197. /// pattern to determine the target function name create the rewrite rule.
  198. using PatternRewriteFunctionDescriptor =
  199. PatternRewriteDescriptor<RewriteDescriptor::Type::Function, Function,
  200. &Module::getFunction, &Module::functions>;
  201. /// Represents a rewrite for a global variable based upon a matching pattern.
  202. /// Each global variable matching the provided pattern will be transformed as
  203. /// described in the transformation pattern for the target. Applies only to
  204. /// module level variables.
  205. using PatternRewriteGlobalVariableDescriptor =
  206. PatternRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
  207. GlobalVariable, &Module::getGlobalVariable,
  208. &Module::globals>;
  209. /// PatternRewriteNamedAliasDescriptor - represents a rewrite for global
  210. /// aliases which match a given pattern. The provided transformation will be
  211. /// applied to each of the matching names.
  212. using PatternRewriteNamedAliasDescriptor =
  213. PatternRewriteDescriptor<RewriteDescriptor::Type::NamedAlias, GlobalAlias,
  214. &Module::getNamedAlias, &Module::aliases>;
  215. } // end anonymous namespace
  216. bool RewriteMapParser::parse(const std::string &MapFile,
  217. RewriteDescriptorList *DL) {
  218. ErrorOr<std::unique_ptr<MemoryBuffer>> Mapping =
  219. MemoryBuffer::getFile(MapFile);
  220. if (!Mapping)
  221. report_fatal_error(Twine("unable to read rewrite map '") + MapFile +
  222. "': " + Mapping.getError().message());
  223. if (!parse(*Mapping, DL))
  224. report_fatal_error(Twine("unable to parse rewrite map '") + MapFile + "'");
  225. return true;
  226. }
  227. bool RewriteMapParser::parse(std::unique_ptr<MemoryBuffer> &MapFile,
  228. RewriteDescriptorList *DL) {
  229. SourceMgr SM;
  230. yaml::Stream YS(MapFile->getBuffer(), SM);
  231. for (auto &Document : YS) {
  232. yaml::MappingNode *DescriptorList;
  233. // ignore empty documents
  234. if (isa<yaml::NullNode>(Document.getRoot()))
  235. continue;
  236. DescriptorList = dyn_cast<yaml::MappingNode>(Document.getRoot());
  237. if (!DescriptorList) {
  238. YS.printError(Document.getRoot(), "DescriptorList node must be a map");
  239. return false;
  240. }
  241. for (auto &Descriptor : *DescriptorList)
  242. if (!parseEntry(YS, Descriptor, DL))
  243. return false;
  244. }
  245. return true;
  246. }
  247. bool RewriteMapParser::parseEntry(yaml::Stream &YS, yaml::KeyValueNode &Entry,
  248. RewriteDescriptorList *DL) {
  249. yaml::ScalarNode *Key;
  250. yaml::MappingNode *Value;
  251. SmallString<32> KeyStorage;
  252. StringRef RewriteType;
  253. Key = dyn_cast<yaml::ScalarNode>(Entry.getKey());
  254. if (!Key) {
  255. YS.printError(Entry.getKey(), "rewrite type must be a scalar");
  256. return false;
  257. }
  258. Value = dyn_cast<yaml::MappingNode>(Entry.getValue());
  259. if (!Value) {
  260. YS.printError(Entry.getValue(), "rewrite descriptor must be a map");
  261. return false;
  262. }
  263. RewriteType = Key->getValue(KeyStorage);
  264. if (RewriteType.equals("function"))
  265. return parseRewriteFunctionDescriptor(YS, Key, Value, DL);
  266. else if (RewriteType.equals("global variable"))
  267. return parseRewriteGlobalVariableDescriptor(YS, Key, Value, DL);
  268. else if (RewriteType.equals("global alias"))
  269. return parseRewriteGlobalAliasDescriptor(YS, Key, Value, DL);
  270. YS.printError(Entry.getKey(), "unknown rewrite type");
  271. return false;
  272. }
  273. bool RewriteMapParser::
  274. parseRewriteFunctionDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
  275. yaml::MappingNode *Descriptor,
  276. RewriteDescriptorList *DL) {
  277. bool Naked = false;
  278. std::string Source;
  279. std::string Target;
  280. std::string Transform;
  281. for (auto &Field : *Descriptor) {
  282. yaml::ScalarNode *Key;
  283. yaml::ScalarNode *Value;
  284. SmallString<32> KeyStorage;
  285. SmallString<32> ValueStorage;
  286. StringRef KeyValue;
  287. Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
  288. if (!Key) {
  289. YS.printError(Field.getKey(), "descriptor key must be a scalar");
  290. return false;
  291. }
  292. Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
  293. if (!Value) {
  294. YS.printError(Field.getValue(), "descriptor value must be a scalar");
  295. return false;
  296. }
  297. KeyValue = Key->getValue(KeyStorage);
  298. if (KeyValue.equals("source")) {
  299. std::string Error;
  300. Source = std::string(Value->getValue(ValueStorage));
  301. if (!Regex(Source).isValid(Error)) {
  302. YS.printError(Field.getKey(), "invalid regex: " + Error);
  303. return false;
  304. }
  305. } else if (KeyValue.equals("target")) {
  306. Target = std::string(Value->getValue(ValueStorage));
  307. } else if (KeyValue.equals("transform")) {
  308. Transform = std::string(Value->getValue(ValueStorage));
  309. } else if (KeyValue.equals("naked")) {
  310. std::string Undecorated;
  311. Undecorated = std::string(Value->getValue(ValueStorage));
  312. Naked = StringRef(Undecorated).lower() == "true" || Undecorated == "1";
  313. } else {
  314. YS.printError(Field.getKey(), "unknown key for function");
  315. return false;
  316. }
  317. }
  318. if (Transform.empty() == Target.empty()) {
  319. YS.printError(Descriptor,
  320. "exactly one of transform or target must be specified");
  321. return false;
  322. }
  323. // TODO see if there is a more elegant solution to selecting the rewrite
  324. // descriptor type
  325. if (!Target.empty())
  326. DL->push_back(std::make_unique<ExplicitRewriteFunctionDescriptor>(
  327. Source, Target, Naked));
  328. else
  329. DL->push_back(
  330. std::make_unique<PatternRewriteFunctionDescriptor>(Source, Transform));
  331. return true;
  332. }
  333. bool RewriteMapParser::
  334. parseRewriteGlobalVariableDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
  335. yaml::MappingNode *Descriptor,
  336. RewriteDescriptorList *DL) {
  337. std::string Source;
  338. std::string Target;
  339. std::string Transform;
  340. for (auto &Field : *Descriptor) {
  341. yaml::ScalarNode *Key;
  342. yaml::ScalarNode *Value;
  343. SmallString<32> KeyStorage;
  344. SmallString<32> ValueStorage;
  345. StringRef KeyValue;
  346. Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
  347. if (!Key) {
  348. YS.printError(Field.getKey(), "descriptor Key must be a scalar");
  349. return false;
  350. }
  351. Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
  352. if (!Value) {
  353. YS.printError(Field.getValue(), "descriptor value must be a scalar");
  354. return false;
  355. }
  356. KeyValue = Key->getValue(KeyStorage);
  357. if (KeyValue.equals("source")) {
  358. std::string Error;
  359. Source = std::string(Value->getValue(ValueStorage));
  360. if (!Regex(Source).isValid(Error)) {
  361. YS.printError(Field.getKey(), "invalid regex: " + Error);
  362. return false;
  363. }
  364. } else if (KeyValue.equals("target")) {
  365. Target = std::string(Value->getValue(ValueStorage));
  366. } else if (KeyValue.equals("transform")) {
  367. Transform = std::string(Value->getValue(ValueStorage));
  368. } else {
  369. YS.printError(Field.getKey(), "unknown Key for Global Variable");
  370. return false;
  371. }
  372. }
  373. if (Transform.empty() == Target.empty()) {
  374. YS.printError(Descriptor,
  375. "exactly one of transform or target must be specified");
  376. return false;
  377. }
  378. if (!Target.empty())
  379. DL->push_back(std::make_unique<ExplicitRewriteGlobalVariableDescriptor>(
  380. Source, Target,
  381. /*Naked*/ false));
  382. else
  383. DL->push_back(std::make_unique<PatternRewriteGlobalVariableDescriptor>(
  384. Source, Transform));
  385. return true;
  386. }
  387. bool RewriteMapParser::
  388. parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
  389. yaml::MappingNode *Descriptor,
  390. RewriteDescriptorList *DL) {
  391. std::string Source;
  392. std::string Target;
  393. std::string Transform;
  394. for (auto &Field : *Descriptor) {
  395. yaml::ScalarNode *Key;
  396. yaml::ScalarNode *Value;
  397. SmallString<32> KeyStorage;
  398. SmallString<32> ValueStorage;
  399. StringRef KeyValue;
  400. Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
  401. if (!Key) {
  402. YS.printError(Field.getKey(), "descriptor key must be a scalar");
  403. return false;
  404. }
  405. Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
  406. if (!Value) {
  407. YS.printError(Field.getValue(), "descriptor value must be a scalar");
  408. return false;
  409. }
  410. KeyValue = Key->getValue(KeyStorage);
  411. if (KeyValue.equals("source")) {
  412. std::string Error;
  413. Source = std::string(Value->getValue(ValueStorage));
  414. if (!Regex(Source).isValid(Error)) {
  415. YS.printError(Field.getKey(), "invalid regex: " + Error);
  416. return false;
  417. }
  418. } else if (KeyValue.equals("target")) {
  419. Target = std::string(Value->getValue(ValueStorage));
  420. } else if (KeyValue.equals("transform")) {
  421. Transform = std::string(Value->getValue(ValueStorage));
  422. } else {
  423. YS.printError(Field.getKey(), "unknown key for Global Alias");
  424. return false;
  425. }
  426. }
  427. if (Transform.empty() == Target.empty()) {
  428. YS.printError(Descriptor,
  429. "exactly one of transform or target must be specified");
  430. return false;
  431. }
  432. if (!Target.empty())
  433. DL->push_back(std::make_unique<ExplicitRewriteNamedAliasDescriptor>(
  434. Source, Target,
  435. /*Naked*/ false));
  436. else
  437. DL->push_back(std::make_unique<PatternRewriteNamedAliasDescriptor>(
  438. Source, Transform));
  439. return true;
  440. }
  441. namespace {
  442. class RewriteSymbolsLegacyPass : public ModulePass {
  443. public:
  444. static char ID; // Pass identification, replacement for typeid
  445. RewriteSymbolsLegacyPass();
  446. RewriteSymbolsLegacyPass(SymbolRewriter::RewriteDescriptorList &DL);
  447. bool runOnModule(Module &M) override;
  448. private:
  449. RewriteSymbolPass Impl;
  450. };
  451. } // end anonymous namespace
  452. char RewriteSymbolsLegacyPass::ID = 0;
  453. RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass() : ModulePass(ID) {
  454. initializeRewriteSymbolsLegacyPassPass(*PassRegistry::getPassRegistry());
  455. }
  456. RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass(
  457. SymbolRewriter::RewriteDescriptorList &DL)
  458. : ModulePass(ID), Impl(DL) {}
  459. bool RewriteSymbolsLegacyPass::runOnModule(Module &M) {
  460. return Impl.runImpl(M);
  461. }
  462. PreservedAnalyses RewriteSymbolPass::run(Module &M, ModuleAnalysisManager &AM) {
  463. if (!runImpl(M))
  464. return PreservedAnalyses::all();
  465. return PreservedAnalyses::none();
  466. }
  467. bool RewriteSymbolPass::runImpl(Module &M) {
  468. bool Changed;
  469. Changed = false;
  470. for (auto &Descriptor : Descriptors)
  471. Changed |= Descriptor->performOnModule(M);
  472. return Changed;
  473. }
  474. void RewriteSymbolPass::loadAndParseMapFiles() {
  475. const std::vector<std::string> MapFiles(RewriteMapFiles);
  476. SymbolRewriter::RewriteMapParser Parser;
  477. for (const auto &MapFile : MapFiles)
  478. Parser.parse(MapFile, &Descriptors);
  479. }
  480. INITIALIZE_PASS(RewriteSymbolsLegacyPass, "rewrite-symbols", "Rewrite Symbols",
  481. false, false)
  482. ModulePass *llvm::createRewriteSymbolsPass() {
  483. return new RewriteSymbolsLegacyPass();
  484. }
  485. ModulePass *
  486. llvm::createRewriteSymbolsPass(SymbolRewriter::RewriteDescriptorList &DL) {
  487. return new RewriteSymbolsLegacyPass(DL);
  488. }