xref: /llvm-project/bolt/include/bolt/Core/Linker.h (revision 475a93a07a6a5d152a2f90ad5510a068f6773357)
1 //===- bolt/Core/Linker.h - BOLTLinker interface ----------------*- 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 interface BOLT uses for linking.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef BOLT_CORE_LINKER_H
14 #define BOLT_CORE_LINKER_H
15 
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/MemoryBufferRef.h"
18 
19 #include <cstdint>
20 #include <functional>
21 #include <optional>
22 
23 namespace llvm {
24 namespace bolt {
25 
26 class BinarySection;
27 
28 class BOLTLinker {
29 public:
30   using SectionMapper =
31       std::function<void(const BinarySection &Section, uint64_t Address)>;
32   using SectionsMapper = std::function<void(SectionMapper)>;
33 
34   struct SymbolInfo {
35     uint64_t Address;
36     uint64_t Size;
37   };
38 
39   virtual ~BOLTLinker() = default;
40 
41   /// Load and link \p Obj. \p MapSections will be called before the object is
42   /// linked to allow section addresses to be remapped. When called, the address
43   /// of a section can be changed by calling the passed SectionMapper.
44   virtual void loadObject(MemoryBufferRef Obj, SectionsMapper MapSections) = 0;
45 
46   /// Return the address and size of a symbol or std::nullopt if it cannot be
47   /// found.
48   virtual std::optional<SymbolInfo> lookupSymbolInfo(StringRef Name) const = 0;
49 
50   /// Return the address of a symbol or std::nullopt if it cannot be found.
lookupSymbol(StringRef Name)51   std::optional<uint64_t> lookupSymbol(StringRef Name) const {
52     if (const auto Info = lookupSymbolInfo(Name))
53       return Info->Address;
54     return std::nullopt;
55   }
56 };
57 
58 } // namespace bolt
59 } // namespace llvm
60 
61 #endif // BOLT_CORE_LINKER_H
62