annotate lld/MachO/InputFiles.h @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 0572611fdcc8
children 5f17cb93ff66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- InputFiles.h ---------------------------------------------*- C++ -*-===//
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #ifndef LLD_MACHO_INPUT_FILES_H
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #define LLD_MACHO_INPUT_FILES_H
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
12 #include "MachOStructs.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
13 #include "Target.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
14
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 #include "lld/Common/LLVM.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
16 #include "lld/Common/Memory.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include "llvm/ADT/DenseSet.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
18 #include "llvm/ADT/SetVector.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 #include "llvm/BinaryFormat/MachO.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
20 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #include "llvm/Object/Archive.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 #include "llvm/Support/MemoryBuffer.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
23 #include "llvm/TextAPI/TextAPIReader.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 #include <map>
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 #include <vector>
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
28 namespace llvm {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
29 namespace lto {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
30 class InputFile;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
31 } // namespace lto
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
32 namespace MachO {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
33 class InterfaceFile;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
34 } // namespace MachO
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
35 class TarWriter;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
36 } // namespace llvm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
37
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 namespace lld {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 namespace macho {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
41 struct PlatformInfo;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 class InputSection;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 class Symbol;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 struct Reloc;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
45 enum class RefState : uint8_t;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
46
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
47 // If --reproduce option is given, all input files are written
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
48 // to this tar archive.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
49 extern std::unique_ptr<llvm::TarWriter> tar;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 // If .subsections_via_symbols is set, each InputSection will be split along
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
52 // symbol boundaries. The field offset represents the offset of the subsection
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
53 // from the start of the original pre-split InputSection.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
54 struct SubsectionEntry {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
55 uint64_t offset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
56 InputSection *isec;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
57 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
58 using SubsectionMap = std::vector<SubsectionEntry>;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 class InputFile {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 public:
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 enum Kind {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 ObjKind,
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
64 OpaqueKind,
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 DylibKind,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 ArchiveKind,
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
67 BitcodeKind,
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 };
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 virtual ~InputFile() = default;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 Kind kind() const { return fileKind; }
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
72 StringRef getName() const { return name; }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 MemoryBufferRef mb;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
75
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 std::vector<Symbol *> symbols;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 std::vector<SubsectionMap> subsections;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
78 // Provides an easy way to sort InputFiles deterministically.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
79 const int id;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
80
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
81 // If not empty, this stores the name of the archive containing this file.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
82 // We use this string for creating error messages.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
83 std::string archiveName;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 protected:
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
86 InputFile(Kind kind, MemoryBufferRef mb)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
87 : mb(mb), id(idCount++), fileKind(kind), name(mb.getBufferIdentifier()) {}
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
89 InputFile(Kind, const llvm::MachO::InterfaceFile &);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 private:
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 const Kind fileKind;
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
93 const StringRef name;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
94
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
95 static int idCount;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 };
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 // .o file
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 class ObjFile : public InputFile {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 public:
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
101 ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 static bool classof(const InputFile *f) { return f->kind() == ObjKind; }
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
103
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
104 llvm::DWARFUnit *compileUnit = nullptr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
105 const uint32_t modTime;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
106 std::vector<InputSection *> debugSections;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
107
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
108 private:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
109 template <class LP> void parse();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
110 template <class Section> void parseSections(ArrayRef<Section>);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
111 template <class LP>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
112 void parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
113 ArrayRef<typename LP::nlist> nList, const char *strtab,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
114 bool subsectionsViaSymbols);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
115 template <class NList>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
116 Symbol *parseNonSectionSymbol(const NList &sym, StringRef name);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
117 template <class Section>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
118 void parseRelocations(ArrayRef<Section> sectionHeaders, const Section &,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
119 SubsectionMap &);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
120 void parseDebugInfo();
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 };
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
123 // command-line -sectcreate file
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
124 class OpaqueFile : public InputFile {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
125 public:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
126 OpaqueFile(MemoryBufferRef mb, StringRef segName, StringRef sectName);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
127 static bool classof(const InputFile *f) { return f->kind() == OpaqueKind; }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
128 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
129
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
130 // .dylib or .tbd file
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 class DylibFile : public InputFile {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 public:
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 // Mach-O dylibs can re-export other dylibs as sub-libraries, meaning that the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 // symbols in those sub-libraries will be available under the umbrella
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 // library's namespace. Those sub-libraries can also have their own
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 // re-exports. When loading a re-exported dylib, `umbrella` should be set to
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 // the root dylib to ensure symbols in the child library are correctly bound
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 // to the root. On the other hand, if a dylib is being directly loaded
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 // (through an -lfoo flag), then `umbrella` should be a nullptr.
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
140 explicit DylibFile(MemoryBufferRef mb, DylibFile *umbrella,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
141 bool isBundleLoader = false);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
142 explicit DylibFile(const llvm::MachO::InterfaceFile &interface,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
143 DylibFile *umbrella = nullptr,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
144 bool isBundleLoader = false);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
145
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
146 void parseLoadCommands(MemoryBufferRef mb);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
147 void parseReexports(const llvm::MachO::InterfaceFile &interface);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
148
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 static bool classof(const InputFile *f) { return f->kind() == DylibKind; }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
151 StringRef installName;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
152 DylibFile *exportingFile = nullptr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
153 DylibFile *umbrella;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
154 SmallVector<StringRef, 2> rpaths;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
155 uint32_t compatibilityVersion = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
156 uint32_t currentVersion = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
157 int64_t ordinal = 0; // Ordinal numbering starts from 1, so 0 is a sentinel
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
158 RefState refState;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
159 bool reexport = false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
160 bool forceNeeded = false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
161 bool forceWeakImport = false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
162 bool deadStrippable = false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
163 bool explicitlyLinked = false;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
164
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
165 unsigned numReferencedSymbols = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
166
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
167 bool isReferenced() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
168 return numReferencedSymbols > 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
169 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
170
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
171 // An executable can be used as a bundle loader that will load the output
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
172 // file being linked, and that contains symbols referenced, but not
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
173 // implemented in the bundle. When used like this, it is very similar
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
174 // to a Dylib, so we re-used the same class to represent it.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
175 bool isBundleLoader;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
176
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
177 private:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
178 bool handleLDSymbol(StringRef originalName);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
179 void handleLDPreviousSymbol(StringRef name, StringRef originalName);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
180 void handleLDInstallNameSymbol(StringRef name, StringRef originalName);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 };
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 // .a file
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 class ArchiveFile : public InputFile {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 public:
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 explicit ArchiveFile(std::unique_ptr<llvm::object::Archive> &&file);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
187 static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 void fetch(const llvm::object::Archive::Symbol &sym);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
189
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 private:
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 std::unique_ptr<llvm::object::Archive> file;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 // Keep track of children fetched from the archive by tracking
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 // which address offsets have been fetched already.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 llvm::DenseSet<uint64_t> seen;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 };
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
197 class BitcodeFile : public InputFile {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
198 public:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
199 explicit BitcodeFile(MemoryBufferRef mb);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
200 static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
201
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
202 std::unique_ptr<llvm::lto::InputFile> obj;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
203 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
204
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
205 extern llvm::SetVector<InputFile *> inputFiles;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
206
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 llvm::Optional<MemoryBufferRef> readFile(StringRef path);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
209 namespace detail {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
210
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
211 template <class CommandType, class... Types>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
212 std::vector<const CommandType *>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
213 findCommands(const void *anyHdr, size_t maxCommands, Types... types) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
214 std::vector<const CommandType *> cmds;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
215 std::initializer_list<uint32_t> typesList{types...};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
216 const auto *hdr = reinterpret_cast<const llvm::MachO::mach_header *>(anyHdr);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
217 const uint8_t *p =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
218 reinterpret_cast<const uint8_t *>(hdr) + target->headerSize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
219 for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
220 auto *cmd = reinterpret_cast<const CommandType *>(p);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
221 if (llvm::is_contained(typesList, cmd->cmd)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
222 cmds.push_back(cmd);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
223 if (cmds.size() == maxCommands)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
224 return cmds;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
225 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
226 p += cmd->cmdsize;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
227 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
228 return cmds;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
229 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
230
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
231 } // namespace detail
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
232
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
233 // anyHdr should be a pointer to either mach_header or mach_header_64
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
234 template <class CommandType = llvm::MachO::load_command, class... Types>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
235 const CommandType *findCommand(const void *anyHdr, Types... types) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
236 std::vector<const CommandType *> cmds =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
237 detail::findCommands<CommandType>(anyHdr, 1, types...);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
238 return cmds.size() ? cmds[0] : nullptr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
239 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
240
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
241 template <class CommandType = llvm::MachO::load_command, class... Types>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
242 std::vector<const CommandType *> findCommands(const void *anyHdr,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
243 Types... types) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
244 return detail::findCommands<CommandType>(anyHdr, 0, types...);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
245 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
246
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 } // namespace macho
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
248
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 std::string toString(const macho::InputFile *file);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 } // namespace lld
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
251
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 #endif