1 //===- bolt/Rewrite/MetadataRewriter.h --------------------------*- C++ -*-===// 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 // Interface for reading and updating metadata in a file. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef BOLT_REWRITE_METADATA_REWRITER_H 14 #define BOLT_REWRITE_METADATA_REWRITER_H 15 16 #include "bolt/Core/BinaryContext.h" 17 #include "llvm/Support/Error.h" 18 19 namespace llvm { 20 namespace bolt { 21 22 /// Base class for handling file sections with metadata. In this context, 23 /// metadata encompasses a wide range of data that references code and other 24 /// data. Such metadata may or may not have an impact on program execution. 25 /// Examples include: debug information, unwind information, exception handling 26 /// tables, etc. 27 // 28 /// The metadata can occupy a section (e.g. .note.stapsdt), span a number of 29 /// sections (e.g., DWARF debug info), or exist as subsection of another 30 /// section in the binary (e.g., static-key jump tables embedded in .rodata 31 /// section in the Linux Kernel). 32 class MetadataRewriter { 33 /// The name of the data type handled by an instance of this class. 34 StringRef Name; 35 36 protected: 37 /// Provides access to the binary context. 38 BinaryContext &BC; 39 MetadataRewriter(StringRef Name,BinaryContext & BC)40 MetadataRewriter(StringRef Name, BinaryContext &BC) : Name(Name), BC(BC) {} 41 42 public: 43 virtual ~MetadataRewriter() = default; 44 45 /// Return name for the rewriter. getName()46 StringRef getName() const { return Name; } 47 48 /// Run initialization after the binary is read and sections are identified, 49 /// but before functions are discovered. sectionInitializer()50 virtual Error sectionInitializer() { return Error::success(); } 51 52 /// Interface for modifying/annotating functions in the binary based on the 53 /// contents of the section. Functions are in pre-cfg state. preCFGInitializer()54 virtual Error preCFGInitializer() { return Error::success(); } 55 56 /// Run the rewriter once the functions are in CFG state. postCFGInitializer()57 virtual Error postCFGInitializer() { return Error::success(); } 58 59 /// Run the pass before the binary is emitted. preEmitFinalizer()60 virtual Error preEmitFinalizer() { return Error::success(); } 61 62 /// Finalize section contents based on the new context after the new code is 63 /// emitted. postEmitFinalizer()64 virtual Error postEmitFinalizer() { return Error::success(); } 65 }; 66 67 } // namespace bolt 68 } // namespace llvm 69 70 #endif // BOLT_REWRITE_METADATA_REWRITER_H 71