xref: /llvm-project/bolt/include/bolt/RuntimeLibs/RuntimeLibrary.h (revision abc2eae68290c453e1899a94eccc4ed5ea3b69c1)
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 "bolt/Core/Linker.h"
19 #include "llvm/ADT/StringRef.h"
20 #include <vector>
21 
22 namespace llvm {
23 
24 class MCStreamer;
25 
26 namespace bolt {
27 
28 class BinaryContext;
29 
30 class RuntimeLibrary {
31   // vtable anchor.
32   virtual void anchor();
33 
34 public:
35   virtual ~RuntimeLibrary() = default;
36 
37   uint64_t getRuntimeFiniAddress() const { return RuntimeFiniAddress; }
38 
39   uint64_t getRuntimeStartAddress() const { return RuntimeStartAddress; }
40 
41   /// Add custom sections added by the runtime libraries.
42   virtual void
43   addRuntimeLibSections(std::vector<std::string> &SecNames) const = 0;
44 
45   /// Validity check and modify if necessary all the command line options
46   /// for this runtime library.
47   virtual void adjustCommandLineOptions(const BinaryContext &BC) const = 0;
48 
49   /// Emit data structures that will be necessary during runtime.
50   virtual void emitBinary(BinaryContext &BC, MCStreamer &Streamer) = 0;
51 
52   /// Link with the library code.
53   virtual void link(BinaryContext &BC, StringRef ToolPath, BOLTLinker &Linker,
54                     BOLTLinker::SectionsMapper MapSections) = 0;
55 
56 protected:
57   /// The fini and init address set by the runtime library.
58   uint64_t RuntimeFiniAddress{0};
59   uint64_t RuntimeStartAddress{0};
60 
61   /// Get the full path to a runtime library specified by \p LibFileName and \p
62   /// ToolPath.
63   static std::string getLibPathByToolPath(StringRef ToolPath,
64                                           StringRef LibFileName);
65 
66   /// Get the full path to a runtime library by the install directory.
67   static std::string getLibPathByInstalled(StringRef LibFileName);
68 
69   /// Gets the full path to a runtime library based on whether it exists
70   /// in the install libdir or runtime libdir.
71   static std::string getLibPath(StringRef ToolPath, StringRef LibFileName);
72 
73   /// Load a static runtime library specified by \p LibPath.
74   static void loadLibrary(StringRef LibPath, BOLTLinker &Linker,
75                           BOLTLinker::SectionsMapper MapSections);
76 };
77 
78 } // namespace bolt
79 } // namespace llvm
80 
81 #endif // BOLT_RUNTIMELIBS_RUNTIME_LIBRARY_H
82