1 //===- SymbolRewriter.cpp - Symbol Rewriter -------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
13 // The YAML configuration file format is as follows:
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]+
25 // Currently, the following descriptor types are supported:
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)
41 // Note that source and exactly one of [Target, Transform] must be provided
43 // New rewrite descriptors can be created. Addding a new rewrite descriptor
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)
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`
57 //===----------------------------------------------------------------------===//
59 #include "llvm/Transforms/Utils/SymbolRewriter.h"
60 #include "llvm/ADT/STLExtras.h"
61 #include "llvm/ADT/SmallString.h"
62 #include "llvm/ADT/StringRef.h"
63 #include "llvm/ADT/ilist.h"
64 #include "llvm/ADT/iterator_range.h"
65 #include "llvm/IR/Comdat.h"
66 #include "llvm/IR/Function.h"
67 #include "llvm/IR/GlobalAlias.h"
68 #include "llvm/IR/GlobalObject.h"
69 #include "llvm/IR/GlobalVariable.h"
70 #include "llvm/IR/Module.h"
71 #include "llvm/IR/Value.h"
72 #include "llvm/InitializePasses.h"
73 #include "llvm/Pass.h"
74 #include "llvm/Support/Casting.h"
75 #include "llvm/Support/CommandLine.h"
76 #include "llvm/Support/ErrorHandling.h"
77 #include "llvm/Support/ErrorOr.h"
78 #include "llvm/Support/MemoryBuffer.h"
79 #include "llvm/Support/Regex.h"
80 #include "llvm/Support/SourceMgr.h"
81 #include "llvm/Support/YAMLParser.h"
87 using namespace SymbolRewriter;
89 #define DEBUG_TYPE "symbol-rewriter"
91 static cl::list<std::string> RewriteMapFiles("rewrite-map-file",
92 cl::desc("Symbol Rewrite Map"),
93 cl::value_desc("filename"),
96 static void rewriteComdat(Module &M, GlobalObject *GO,
97 const std::string &Source,
98 const std::string &Target) {
99 if (Comdat *CD = GO->getComdat()) {
100 auto &Comdats = M.getComdatSymbolTable();
102 Comdat *C = M.getOrInsertComdat(Target);
103 C->setSelectionKind(CD->getSelectionKind());
106 Comdats.erase(Comdats.find(Source));
112 template <RewriteDescriptor::Type DT, typename ValueType,
113 ValueType *(Module::*Get)(StringRef) const>
114 class ExplicitRewriteDescriptor : public RewriteDescriptor {
116 const std::string Source;
117 const std::string Target;
119 ExplicitRewriteDescriptor(StringRef S, StringRef T, const bool Naked)
120 : RewriteDescriptor(DT),
121 Source(std::string(Naked ? StringRef("\01" + S.str()) : S)),
122 Target(std::string(T)) {}
124 bool performOnModule(Module &M) override;
126 static bool classof(const RewriteDescriptor *RD) {
127 return RD->getType() == DT;
131 } // end anonymous namespace
133 template <RewriteDescriptor::Type DT, typename ValueType,
134 ValueType *(Module::*Get)(StringRef) const>
135 bool ExplicitRewriteDescriptor<DT, ValueType, Get>::performOnModule(Module &M) {
136 bool Changed = false;
137 if (ValueType *S = (M.*Get)(Source)) {
138 if (GlobalObject *GO = dyn_cast<GlobalObject>(S))
139 rewriteComdat(M, GO, Source, Target);
141 if (Value *T = (M.*Get)(Target))
142 S->setValueName(T->getValueName());
153 template <RewriteDescriptor::Type DT, typename ValueType,
154 ValueType *(Module::*Get)(StringRef) const,
155 iterator_range<typename iplist<ValueType>::iterator>
156 (Module::*Iterator)()>
157 class PatternRewriteDescriptor : public RewriteDescriptor {
159 const std::string Pattern;
160 const std::string Transform;
162 PatternRewriteDescriptor(StringRef P, StringRef T)
163 : RewriteDescriptor(DT), Pattern(std::string(P)),
164 Transform(std::string(T)) {}
166 bool performOnModule(Module &M) override;
168 static bool classof(const RewriteDescriptor *RD) {
169 return RD->getType() == DT;
173 } // end anonymous namespace
175 template <RewriteDescriptor::Type DT, typename ValueType,
176 ValueType *(Module::*Get)(StringRef) const,
177 iterator_range<typename iplist<ValueType>::iterator>
178 (Module::*Iterator)()>
179 bool PatternRewriteDescriptor<DT, ValueType, Get, Iterator>::
180 performOnModule(Module &M) {
181 bool Changed = false;
182 for (auto &C : (M.*Iterator)()) {
185 std::string Name = Regex(Pattern).sub(Transform, C.getName(), &Error);
187 report_fatal_error("unable to transforn " + C.getName() + " in " +
188 M.getModuleIdentifier() + ": " + Error);
190 if (C.getName() == Name)
193 if (GlobalObject *GO = dyn_cast<GlobalObject>(&C))
194 rewriteComdat(M, GO, std::string(C.getName()), Name);
196 if (Value *V = (M.*Get)(Name))
197 C.setValueName(V->getValueName());
208 /// Represents a rewrite for an explicitly named (function) symbol. Both the
209 /// source function name and target function name of the transformation are
210 /// explicitly spelt out.
211 using ExplicitRewriteFunctionDescriptor =
212 ExplicitRewriteDescriptor<RewriteDescriptor::Type::Function, Function,
213 &Module::getFunction>;
215 /// Represents a rewrite for an explicitly named (global variable) symbol. Both
216 /// the source variable name and target variable name are spelt out. This
217 /// applies only to module level variables.
218 using ExplicitRewriteGlobalVariableDescriptor =
219 ExplicitRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
220 GlobalVariable, &Module::getGlobalVariable>;
222 /// Represents a rewrite for an explicitly named global alias. Both the source
223 /// and target name are explicitly spelt out.
224 using ExplicitRewriteNamedAliasDescriptor =
225 ExplicitRewriteDescriptor<RewriteDescriptor::Type::NamedAlias, GlobalAlias,
226 &Module::getNamedAlias>;
228 /// Represents a rewrite for a regular expression based pattern for functions.
229 /// A pattern for the function name is provided and a transformation for that
230 /// pattern to determine the target function name create the rewrite rule.
231 using PatternRewriteFunctionDescriptor =
232 PatternRewriteDescriptor<RewriteDescriptor::Type::Function, Function,
233 &Module::getFunction, &Module::functions>;
235 /// Represents a rewrite for a global variable based upon a matching pattern.
236 /// Each global variable matching the provided pattern will be transformed as
237 /// described in the transformation pattern for the target. Applies only to
238 /// module level variables.
239 using PatternRewriteGlobalVariableDescriptor =
240 PatternRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
241 GlobalVariable, &Module::getGlobalVariable,
244 /// PatternRewriteNamedAliasDescriptor - represents a rewrite for global
245 /// aliases which match a given pattern. The provided transformation will be
246 /// applied to each of the matching names.
247 using PatternRewriteNamedAliasDescriptor =
248 PatternRewriteDescriptor<RewriteDescriptor::Type::NamedAlias, GlobalAlias,
249 &Module::getNamedAlias, &Module::aliases>;
251 } // end anonymous namespace
253 bool RewriteMapParser::parse(const std::string &MapFile,
254 RewriteDescriptorList *DL) {
255 ErrorOr<std::unique_ptr<MemoryBuffer>> Mapping =
256 MemoryBuffer::getFile(MapFile);
259 report_fatal_error("unable to read rewrite map '" + MapFile + "': " +
260 Mapping.getError().message());
262 if (!parse(*Mapping, DL))
263 report_fatal_error("unable to parse rewrite map '" + MapFile + "'");
268 bool RewriteMapParser::parse(std::unique_ptr<MemoryBuffer> &MapFile,
269 RewriteDescriptorList *DL) {
271 yaml::Stream YS(MapFile->getBuffer(), SM);
273 for (auto &Document : YS) {
274 yaml::MappingNode *DescriptorList;
276 // ignore empty documents
277 if (isa<yaml::NullNode>(Document.getRoot()))
280 DescriptorList = dyn_cast<yaml::MappingNode>(Document.getRoot());
281 if (!DescriptorList) {
282 YS.printError(Document.getRoot(), "DescriptorList node must be a map");
286 for (auto &Descriptor : *DescriptorList)
287 if (!parseEntry(YS, Descriptor, DL))
294 bool RewriteMapParser::parseEntry(yaml::Stream &YS, yaml::KeyValueNode &Entry,
295 RewriteDescriptorList *DL) {
296 yaml::ScalarNode *Key;
297 yaml::MappingNode *Value;
298 SmallString<32> KeyStorage;
299 StringRef RewriteType;
301 Key = dyn_cast<yaml::ScalarNode>(Entry.getKey());
303 YS.printError(Entry.getKey(), "rewrite type must be a scalar");
307 Value = dyn_cast<yaml::MappingNode>(Entry.getValue());
309 YS.printError(Entry.getValue(), "rewrite descriptor must be a map");
313 RewriteType = Key->getValue(KeyStorage);
314 if (RewriteType.equals("function"))
315 return parseRewriteFunctionDescriptor(YS, Key, Value, DL);
316 else if (RewriteType.equals("global variable"))
317 return parseRewriteGlobalVariableDescriptor(YS, Key, Value, DL);
318 else if (RewriteType.equals("global alias"))
319 return parseRewriteGlobalAliasDescriptor(YS, Key, Value, DL);
321 YS.printError(Entry.getKey(), "unknown rewrite type");
325 bool RewriteMapParser::
326 parseRewriteFunctionDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
327 yaml::MappingNode *Descriptor,
328 RewriteDescriptorList *DL) {
332 std::string Transform;
334 for (auto &Field : *Descriptor) {
335 yaml::ScalarNode *Key;
336 yaml::ScalarNode *Value;
337 SmallString<32> KeyStorage;
338 SmallString<32> ValueStorage;
341 Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
343 YS.printError(Field.getKey(), "descriptor key must be a scalar");
347 Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
349 YS.printError(Field.getValue(), "descriptor value must be a scalar");
353 KeyValue = Key->getValue(KeyStorage);
354 if (KeyValue.equals("source")) {
357 Source = std::string(Value->getValue(ValueStorage));
358 if (!Regex(Source).isValid(Error)) {
359 YS.printError(Field.getKey(), "invalid regex: " + Error);
362 } else if (KeyValue.equals("target")) {
363 Target = std::string(Value->getValue(ValueStorage));
364 } else if (KeyValue.equals("transform")) {
365 Transform = std::string(Value->getValue(ValueStorage));
366 } else if (KeyValue.equals("naked")) {
367 std::string Undecorated;
369 Undecorated = std::string(Value->getValue(ValueStorage));
370 Naked = StringRef(Undecorated).lower() == "true" || Undecorated == "1";
372 YS.printError(Field.getKey(), "unknown key for function");
377 if (Transform.empty() == Target.empty()) {
378 YS.printError(Descriptor,
379 "exactly one of transform or target must be specified");
383 // TODO see if there is a more elegant solution to selecting the rewrite
386 DL->push_back(std::make_unique<ExplicitRewriteFunctionDescriptor>(
387 Source, Target, Naked));
390 std::make_unique<PatternRewriteFunctionDescriptor>(Source, Transform));
395 bool RewriteMapParser::
396 parseRewriteGlobalVariableDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
397 yaml::MappingNode *Descriptor,
398 RewriteDescriptorList *DL) {
401 std::string Transform;
403 for (auto &Field : *Descriptor) {
404 yaml::ScalarNode *Key;
405 yaml::ScalarNode *Value;
406 SmallString<32> KeyStorage;
407 SmallString<32> ValueStorage;
410 Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
412 YS.printError(Field.getKey(), "descriptor Key must be a scalar");
416 Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
418 YS.printError(Field.getValue(), "descriptor value must be a scalar");
422 KeyValue = Key->getValue(KeyStorage);
423 if (KeyValue.equals("source")) {
426 Source = std::string(Value->getValue(ValueStorage));
427 if (!Regex(Source).isValid(Error)) {
428 YS.printError(Field.getKey(), "invalid regex: " + Error);
431 } else if (KeyValue.equals("target")) {
432 Target = std::string(Value->getValue(ValueStorage));
433 } else if (KeyValue.equals("transform")) {
434 Transform = std::string(Value->getValue(ValueStorage));
436 YS.printError(Field.getKey(), "unknown Key for Global Variable");
441 if (Transform.empty() == Target.empty()) {
442 YS.printError(Descriptor,
443 "exactly one of transform or target must be specified");
448 DL->push_back(std::make_unique<ExplicitRewriteGlobalVariableDescriptor>(
452 DL->push_back(std::make_unique<PatternRewriteGlobalVariableDescriptor>(
458 bool RewriteMapParser::
459 parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
460 yaml::MappingNode *Descriptor,
461 RewriteDescriptorList *DL) {
464 std::string Transform;
466 for (auto &Field : *Descriptor) {
467 yaml::ScalarNode *Key;
468 yaml::ScalarNode *Value;
469 SmallString<32> KeyStorage;
470 SmallString<32> ValueStorage;
473 Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
475 YS.printError(Field.getKey(), "descriptor key must be a scalar");
479 Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
481 YS.printError(Field.getValue(), "descriptor value must be a scalar");
485 KeyValue = Key->getValue(KeyStorage);
486 if (KeyValue.equals("source")) {
489 Source = std::string(Value->getValue(ValueStorage));
490 if (!Regex(Source).isValid(Error)) {
491 YS.printError(Field.getKey(), "invalid regex: " + Error);
494 } else if (KeyValue.equals("target")) {
495 Target = std::string(Value->getValue(ValueStorage));
496 } else if (KeyValue.equals("transform")) {
497 Transform = std::string(Value->getValue(ValueStorage));
499 YS.printError(Field.getKey(), "unknown key for Global Alias");
504 if (Transform.empty() == Target.empty()) {
505 YS.printError(Descriptor,
506 "exactly one of transform or target must be specified");
511 DL->push_back(std::make_unique<ExplicitRewriteNamedAliasDescriptor>(
515 DL->push_back(std::make_unique<PatternRewriteNamedAliasDescriptor>(
523 class RewriteSymbolsLegacyPass : public ModulePass {
525 static char ID; // Pass identification, replacement for typeid
527 RewriteSymbolsLegacyPass();
528 RewriteSymbolsLegacyPass(SymbolRewriter::RewriteDescriptorList &DL);
530 bool runOnModule(Module &M) override;
533 RewriteSymbolPass Impl;
536 } // end anonymous namespace
538 char RewriteSymbolsLegacyPass::ID = 0;
540 RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass() : ModulePass(ID) {
541 initializeRewriteSymbolsLegacyPassPass(*PassRegistry::getPassRegistry());
544 RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass(
545 SymbolRewriter::RewriteDescriptorList &DL)
546 : ModulePass(ID), Impl(DL) {}
548 bool RewriteSymbolsLegacyPass::runOnModule(Module &M) {
549 return Impl.runImpl(M);
552 PreservedAnalyses RewriteSymbolPass::run(Module &M, ModuleAnalysisManager &AM) {
554 return PreservedAnalyses::all();
556 return PreservedAnalyses::none();
559 bool RewriteSymbolPass::runImpl(Module &M) {
563 for (auto &Descriptor : Descriptors)
564 Changed |= Descriptor->performOnModule(M);
569 void RewriteSymbolPass::loadAndParseMapFiles() {
570 const std::vector<std::string> MapFiles(RewriteMapFiles);
571 SymbolRewriter::RewriteMapParser Parser;
573 for (const auto &MapFile : MapFiles)
574 Parser.parse(MapFile, &Descriptors);
577 INITIALIZE_PASS(RewriteSymbolsLegacyPass, "rewrite-symbols", "Rewrite Symbols",
580 ModulePass *llvm::createRewriteSymbolsPass() {
581 return new RewriteSymbolsLegacyPass();
585 llvm::createRewriteSymbolsPass(SymbolRewriter::RewriteDescriptorList &DL) {
586 return new RewriteSymbolsLegacyPass(DL);