LLVM 23.0.0git
ArchiveLinker.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 declares shared functionality for linking static libraries
10// (archives) in offloading tools. It provides a symbol-driven fixed-point
11// archive member selection algorithm used by both clang-nvlink-wrapper and
12// clang-sycl-linker.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_FRONTEND_OFFLOADING_ARCHIVELINKER_H
17#define LLVM_FRONTEND_OFFLOADING_ARCHIVELINKER_H
18
19#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/StringMap.h"
22#include "llvm/ADT/StringRef.h"
25#include "llvm/Support/Error.h"
28#include <cstdint>
29#include <memory>
30
31namespace llvm {
32class MemoryBuffer;
33
34namespace object {
35class SymbolRef;
36} // namespace object
37
38namespace offloading {
39
40/// A minimum symbol interface that provides the necessary information to
41/// extract archive members and resolve LTO symbols.
42struct Symbol {
43 enum Flags {
44 None = 0,
45 Undefined = 1 << 0,
46 Weak = 1 << 1,
47 };
48
51
59
60 /// Create a Symbol from an object file symbol reference.
61 /// Returns an error if symbol flags cannot be retrieved.
63 const object::SymbolRef &Sym);
64
65 bool isWeak() const { return SymFlags & Weak; }
66 bool isUndefined() const { return SymFlags & Undefined; }
67
71};
72
73/// Description of a single input (file or library).
74struct InputDesc {
75 enum class Kind { File, Library };
76
77 StringRef Value; // File path, or library name for -l (the value after -l).
79 bool WholeArchive = false; // --whole-archive state in effect at this input.
80};
81
82/// Result of archive member resolution.
85 Buffers; // Members to link, in order.
86 StringMap<Symbol> SymTab; // Symbol table (for LTO resolution).
87};
88
89/// Resolve archive members from the given inputs using a symbol-driven
90/// fixed-point algorithm. For each input:
91/// - If it's a Library, search for lib<name>.a or :<name> in SearchPaths
92/// - If it's a File, use the path directly
93/// - Archives are expanded and members are lazily extracted based on symbol
94/// references unless WholeArchive is true
95/// - Non-archive inputs (bitcode, ELF objects) are always included
96///
97/// Returns the buffers to link and the symbol table for LTO resolution.
98///
99/// \param Order Positional inputs + -l libraries in order.
100/// \param SearchPaths -L paths for library search.
101/// \param ForcedUndefs -u symbols (may be empty).
102/// \param Root Sysroot for "=" prefixed paths ("" if none).
103/// \param DeviceArchs Architectures of the device code being linked. When
104/// non-empty, any ELF input whose architecture is not in this list (or
105/// which cannot be parsed as an object) is treated as a "fat binary"
106/// and passed through without symbol scanning (e.g., nvlink's cubin
107/// detection). When empty, all inputs are scanned normally.
109 ArrayRef<InputDesc> Order, ArrayRef<StringRef> SearchPaths,
110 ArrayRef<StringRef> ForcedUndefs = {}, StringRef Root = "",
111 ArrayRef<Triple::ArchType> DeviceArchs = {});
112
113} // namespace offloading
114} // namespace llvm
115
116#endif // LLVM_FRONTEND_OFFLOADING_ARCHIVELINKER_H
This file defines the StringMap class.
#define F(x, y, z)
Definition MD5.cpp:54
if(PassOpts->AAPipeline)
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Tagged union holding either a T or a Error.
Definition Error.h:485
This interface provides simple read-only access to a block of memory, and provides simple methods for...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Ephemeral symbols produced by Reader::symbols() and Reader::module_symbols().
Definition IRSymtab.h:316
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition ObjectFile.h:170
Expected< ResolvedInputs > resolveArchiveMembers(ArrayRef< InputDesc > Order, ArrayRef< StringRef > SearchPaths, ArrayRef< StringRef > ForcedUndefs={}, StringRef Root="", ArrayRef< Triple::ArchType > DeviceArchs={})
Resolve archive members from the given inputs using a symbol-driven fixed-point algorithm.
This is an optimization pass for GlobalISel generic memory operations.
bool isWeak() const
Definition IRSymtab.h:202
bool isUndefined() const
Definition IRSymtab.h:201
Description of a single input (file or library).
Result of archive member resolution.
SmallVector< std::unique_ptr< MemoryBuffer > > Buffers
Symbol(Symbol::Flags F)
Symbol(MemoryBufferRef File, const irsymtab::Reader::SymbolRef Sym)
static Expected< Symbol > createFromObject(MemoryBufferRef File, const object::SymbolRef &Sym)
Create a Symbol from an object file symbol reference.