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