1 //===- bolt/RuntimeLibs/RuntimeLibrary.h - Runtime Library ------*- 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 // This file contains the declaration of the RuntimeLibrary class, which
10 // provides all the necessary utilities to link runtime libraries during binary
11 // rewriting, such as the instrumentation runtime library.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef BOLT_RUNTIMELIBS_RUNTIME_LIBRARY_H
16 #define BOLT_RUNTIMELIBS_RUNTIME_LIBRARY_H
17 
18 #include "llvm/ADT/StringRef.h"
19 #include <functional>
20 #include <vector>
21 
22 namespace llvm {
23 
24 class MCStreamer;
25 class RuntimeDyld;
26 
27 namespace bolt {
28 
29 class BinaryContext;
30 
31 class RuntimeLibrary {
32   // vtable anchor.
33   virtual void anchor();
34 
35 public:
36   virtual ~RuntimeLibrary() = default;
37 
getRuntimeFiniAddress()38   uint64_t getRuntimeFiniAddress() const { return RuntimeFiniAddress; }
39 
getRuntimeStartAddress()40   uint64_t getRuntimeStartAddress() const { return RuntimeStartAddress; }
41 
42   /// Add custom sections added by the runtime libraries.
43   virtual void
44   addRuntimeLibSections(std::vector<std::string> &SecNames) const = 0;
45 
46   /// Validity check and modify if necessary all the command line options
47   /// for this runtime library.
48   virtual void adjustCommandLineOptions(const BinaryContext &BC) const = 0;
49 
50   /// Emit data structures that will be necessary during runtime.
51   virtual void emitBinary(BinaryContext &BC, MCStreamer &Streamer) = 0;
52 
53   /// Link with the library code.
54   virtual void link(BinaryContext &BC, StringRef ToolPath, RuntimeDyld &RTDyld,
55                     std::function<void(RuntimeDyld &)> OnLoad) = 0;
56 
57 protected:
58   /// The fini and init address set by the runtime library.
59   uint64_t RuntimeFiniAddress{0};
60   uint64_t RuntimeStartAddress{0};
61 
62   /// Get the full path to a runtime library specified by \p LibFileName.
63   static std::string getLibPath(StringRef ToolPath, StringRef LibFileName);
64 
65   /// Load a static runtime library specified by \p LibPath.
66   static void loadLibrary(StringRef LibPath, RuntimeDyld &RTDyld);
67 };
68 
69 } // namespace bolt
70 } // namespace llvm
71 
72 #endif // BOLT_RUNTIMELIBS_RUNTIME_LIBRARY_H
73