SymbolRewriter.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //===-- SymbolRewriter.h - Symbol Rewriting Pass ----------------*- 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. // This file provides the prototypes and definitions related to the Symbol
  11. // Rewriter pass.
  12. //
  13. // The Symbol Rewriter pass takes a set of rewrite descriptors which define
  14. // transformations for symbol names. These can be either single name to name
  15. // trnsformation or more broad regular expression based transformations.
  16. //
  17. // All the functions are re-written at the IR level. The Symbol Rewriter itself
  18. // is exposed as a module level pass. All symbols at the module level are
  19. // iterated. For any matching symbol, the requested transformation is applied,
  20. // updating references to it as well (a la RAUW). The resulting binary will
  21. // only contain the rewritten symbols.
  22. //
  23. // By performing this operation in the compiler, we are able to catch symbols
  24. // that would otherwise not be possible to catch (e.g. inlined symbols).
  25. //
  26. // This makes it possible to cleanly transform symbols without resorting to
  27. // overly-complex macro tricks and the pre-processor. An example of where this
  28. // is useful is the sanitizers where we would like to intercept a well-defined
  29. // set of functions across the module.
  30. //
  31. //===----------------------------------------------------------------------===//
  32. #ifndef LLVM_TRANSFORMS_UTILS_SYMBOL_REWRITER_H
  33. #define LLVM_TRANSFORMS_UTILS_SYMBOL_REWRITER_H
  34. #include "llvm/ADT/ilist.h"
  35. #include "llvm/ADT/ilist_node.h"
  36. #include "llvm/IR/Module.h"
  37. namespace llvm {
  38. class MemoryBuffer;
  39. namespace yaml {
  40. class KeyValueNode;
  41. class MappingNode;
  42. class ScalarNode;
  43. class Stream;
  44. }
  45. namespace SymbolRewriter {
  46. /// The basic entity representing a rewrite operation. It serves as the base
  47. /// class for any rewrite descriptor. It has a certain set of specializations
  48. /// which describe a particular rewrite.
  49. ///
  50. /// The RewriteMapParser can be used to parse a mapping file that provides the
  51. /// mapping for rewriting the symbols. The descriptors individually describe
  52. /// whether to rewrite a function, global variable, or global alias. Each of
  53. /// these can be selected either by explicitly providing a name for the ones to
  54. /// be rewritten or providing a (posix compatible) regular expression that will
  55. /// select the symbols to rewrite. This descriptor list is passed to the
  56. /// SymbolRewriter pass.
  57. class RewriteDescriptor : public ilist_node<RewriteDescriptor> {
  58. RewriteDescriptor(const RewriteDescriptor &) = delete;
  59. const RewriteDescriptor &
  60. operator=(const RewriteDescriptor &) = delete;
  61. public:
  62. enum class Type {
  63. Invalid, /// invalid
  64. Function, /// function - descriptor rewrites a function
  65. GlobalVariable, /// global variable - descriptor rewrites a global variable
  66. NamedAlias, /// named alias - descriptor rewrites a global alias
  67. };
  68. virtual ~RewriteDescriptor() {}
  69. Type getType() const { return Kind; }
  70. virtual bool performOnModule(Module &M) = 0;
  71. protected:
  72. explicit RewriteDescriptor(Type T) : Kind(T) {}
  73. private:
  74. const Type Kind;
  75. };
  76. typedef iplist<RewriteDescriptor> RewriteDescriptorList;
  77. class RewriteMapParser {
  78. public:
  79. bool parse(const std::string &MapFile, RewriteDescriptorList *Descriptors);
  80. private:
  81. bool parse(std::unique_ptr<MemoryBuffer> &MapFile, RewriteDescriptorList *DL);
  82. bool parseEntry(yaml::Stream &Stream, yaml::KeyValueNode &Entry,
  83. RewriteDescriptorList *DL);
  84. bool parseRewriteFunctionDescriptor(yaml::Stream &Stream,
  85. yaml::ScalarNode *Key,
  86. yaml::MappingNode *Value,
  87. RewriteDescriptorList *DL);
  88. bool parseRewriteGlobalVariableDescriptor(yaml::Stream &Stream,
  89. yaml::ScalarNode *Key,
  90. yaml::MappingNode *Value,
  91. RewriteDescriptorList *DL);
  92. bool parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
  93. yaml::MappingNode *V,
  94. RewriteDescriptorList *DL);
  95. };
  96. }
  97. template <>
  98. struct ilist_traits<SymbolRewriter::RewriteDescriptor>
  99. : public ilist_default_traits<SymbolRewriter::RewriteDescriptor> {
  100. mutable ilist_half_node<SymbolRewriter::RewriteDescriptor> Sentinel;
  101. public:
  102. // createSentinel is used to get a reference to a node marking the end of
  103. // the list. Because the sentinel is relative to this instance, use a
  104. // non-static method.
  105. SymbolRewriter::RewriteDescriptor *createSentinel() const {
  106. // since i[p] lists always publicly derive from the corresponding
  107. // traits, placing a data member in this class will augment the
  108. // i[p]list. Since the NodeTy is expected to publicly derive from
  109. // ilist_node<NodeTy>, there is a legal viable downcast from it to
  110. // NodeTy. We use this trick to superpose i[p]list with a "ghostly"
  111. // NodeTy, which becomes the sentinel. Dereferencing the sentinel is
  112. // forbidden (save the ilist_node<NodeTy>) so no one will ever notice
  113. // the superposition.
  114. return static_cast<SymbolRewriter::RewriteDescriptor *>(&Sentinel);
  115. }
  116. void destroySentinel(SymbolRewriter::RewriteDescriptor *) {}
  117. SymbolRewriter::RewriteDescriptor *provideInitialHead() const {
  118. return createSentinel();
  119. }
  120. SymbolRewriter::RewriteDescriptor *
  121. ensureHead(SymbolRewriter::RewriteDescriptor *&) const {
  122. return createSentinel();
  123. }
  124. static void noteHead(SymbolRewriter::RewriteDescriptor *,
  125. SymbolRewriter::RewriteDescriptor *) {}
  126. };
  127. ModulePass *createRewriteSymbolsPass();
  128. ModulePass *createRewriteSymbolsPass(SymbolRewriter::RewriteDescriptorList &);
  129. }
  130. #endif