SymbolRewriter.cpp 18 KB

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