173
|
1 //===- InputFiles.cpp -----------------------------------------------------===//
|
|
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 functions to parse Mach-O object files. In this comment,
|
|
10 // we describe the Mach-O file structure and how we parse it.
|
|
11 //
|
|
12 // Mach-O is not very different from ELF or COFF. The notion of symbols,
|
|
13 // sections and relocations exists in Mach-O as it does in ELF and COFF.
|
|
14 //
|
|
15 // Perhaps the notion that is new to those who know ELF/COFF is "subsections".
|
|
16 // In ELF/COFF, sections are an atomic unit of data copied from input files to
|
|
17 // output files. When we merge or garbage-collect sections, we treat each
|
|
18 // section as an atomic unit. In Mach-O, that's not the case. Sections can
|
|
19 // consist of multiple subsections, and subsections are a unit of merging and
|
|
20 // garbage-collecting. Therefore, Mach-O's subsections are more similar to
|
|
21 // ELF/COFF's sections than Mach-O's sections are.
|
|
22 //
|
|
23 // A section can have multiple symbols. A symbol that does not have the
|
|
24 // N_ALT_ENTRY attribute indicates a beginning of a subsection. Therefore, by
|
|
25 // definition, a symbol is always present at the beginning of each subsection. A
|
|
26 // symbol with N_ALT_ENTRY attribute does not start a new subsection and can
|
|
27 // point to a middle of a subsection.
|
|
28 //
|
|
29 // The notion of subsections also affects how relocations are represented in
|
|
30 // Mach-O. All references within a section need to be explicitly represented as
|
|
31 // relocations if they refer to different subsections, because we obviously need
|
|
32 // to fix up addresses if subsections are laid out in an output file differently
|
|
33 // than they were in object files. To represent that, Mach-O relocations can
|
|
34 // refer to an unnamed location via its address. Scattered relocations (those
|
|
35 // with the R_SCATTERED bit set) always refer to unnamed locations.
|
|
36 // Non-scattered relocations refer to an unnamed location if r_extern is not set
|
|
37 // and r_symbolnum is zero.
|
|
38 //
|
|
39 // Without the above differences, I think you can use your knowledge about ELF
|
|
40 // and COFF for Mach-O.
|
|
41 //
|
|
42 //===----------------------------------------------------------------------===//
|
|
43
|
|
44 #include "InputFiles.h"
|
|
45 #include "Config.h"
|
207
|
46 #include "Driver.h"
|
|
47 #include "Dwarf.h"
|
173
|
48 #include "ExportTrie.h"
|
|
49 #include "InputSection.h"
|
207
|
50 #include "MachOStructs.h"
|
|
51 #include "ObjC.h"
|
173
|
52 #include "OutputSection.h"
|
207
|
53 #include "OutputSegment.h"
|
173
|
54 #include "SymbolTable.h"
|
|
55 #include "Symbols.h"
|
|
56 #include "Target.h"
|
|
57
|
207
|
58 #include "lld/Common/DWARF.h"
|
173
|
59 #include "lld/Common/ErrorHandler.h"
|
|
60 #include "lld/Common/Memory.h"
|
207
|
61 #include "lld/Common/Reproduce.h"
|
|
62 #include "llvm/ADT/iterator.h"
|
173
|
63 #include "llvm/BinaryFormat/MachO.h"
|
207
|
64 #include "llvm/LTO/LTO.h"
|
173
|
65 #include "llvm/Support/Endian.h"
|
|
66 #include "llvm/Support/MemoryBuffer.h"
|
|
67 #include "llvm/Support/Path.h"
|
207
|
68 #include "llvm/Support/TarWriter.h"
|
|
69 #include "llvm/TextAPI/Architecture.h"
|
|
70 #include "llvm/TextAPI/InterfaceFile.h"
|
173
|
71
|
|
72 using namespace llvm;
|
|
73 using namespace llvm::MachO;
|
|
74 using namespace llvm::support::endian;
|
|
75 using namespace llvm::sys;
|
|
76 using namespace lld;
|
|
77 using namespace lld::macho;
|
|
78
|
207
|
79 // Returns "<internal>", "foo.a(bar.o)", or "baz.o".
|
|
80 std::string lld::toString(const InputFile *f) {
|
|
81 if (!f)
|
|
82 return "<internal>";
|
|
83
|
|
84 // Multiple dylibs can be defined in one .tbd file.
|
|
85 if (auto dylibFile = dyn_cast<DylibFile>(f))
|
|
86 if (f->getName().endswith(".tbd"))
|
|
87 return (f->getName() + "(" + dylibFile->installName + ")").str();
|
|
88
|
|
89 if (f->archiveName.empty())
|
|
90 return std::string(f->getName());
|
|
91 return (f->archiveName + "(" + path::filename(f->getName()) + ")").str();
|
|
92 }
|
|
93
|
|
94 SetVector<InputFile *> macho::inputFiles;
|
|
95 std::unique_ptr<TarWriter> macho::tar;
|
|
96 int InputFile::idCount = 0;
|
|
97
|
|
98 static VersionTuple decodeVersion(uint32_t version) {
|
|
99 unsigned major = version >> 16;
|
|
100 unsigned minor = (version >> 8) & 0xffu;
|
|
101 unsigned subMinor = version & 0xffu;
|
|
102 return VersionTuple(major, minor, subMinor);
|
|
103 }
|
|
104
|
|
105 static std::vector<PlatformInfo> getPlatformInfos(const InputFile *input) {
|
|
106 if (!isa<ObjFile>(input) && !isa<DylibFile>(input))
|
|
107 return {};
|
|
108
|
|
109 const char *hdr = input->mb.getBufferStart();
|
|
110
|
|
111 std::vector<PlatformInfo> platformInfos;
|
|
112 for (auto *cmd : findCommands<build_version_command>(hdr, LC_BUILD_VERSION)) {
|
|
113 PlatformInfo info;
|
|
114 info.target.Platform = static_cast<PlatformKind>(cmd->platform);
|
|
115 info.minimum = decodeVersion(cmd->minos);
|
|
116 platformInfos.emplace_back(std::move(info));
|
|
117 }
|
|
118 for (auto *cmd : findCommands<version_min_command>(
|
|
119 hdr, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS,
|
|
120 LC_VERSION_MIN_TVOS, LC_VERSION_MIN_WATCHOS)) {
|
|
121 PlatformInfo info;
|
|
122 switch (cmd->cmd) {
|
|
123 case LC_VERSION_MIN_MACOSX:
|
|
124 info.target.Platform = PlatformKind::macOS;
|
|
125 break;
|
|
126 case LC_VERSION_MIN_IPHONEOS:
|
|
127 info.target.Platform = PlatformKind::iOS;
|
|
128 break;
|
|
129 case LC_VERSION_MIN_TVOS:
|
|
130 info.target.Platform = PlatformKind::tvOS;
|
|
131 break;
|
|
132 case LC_VERSION_MIN_WATCHOS:
|
|
133 info.target.Platform = PlatformKind::watchOS;
|
|
134 break;
|
|
135 }
|
|
136 info.minimum = decodeVersion(cmd->version);
|
|
137 platformInfos.emplace_back(std::move(info));
|
|
138 }
|
|
139
|
|
140 return platformInfos;
|
|
141 }
|
|
142
|
|
143 static PlatformKind removeSimulator(PlatformKind platform) {
|
|
144 // Mapping of platform to simulator and vice-versa.
|
|
145 static const std::map<PlatformKind, PlatformKind> platformMap = {
|
|
146 {PlatformKind::iOSSimulator, PlatformKind::iOS},
|
|
147 {PlatformKind::tvOSSimulator, PlatformKind::tvOS},
|
|
148 {PlatformKind::watchOSSimulator, PlatformKind::watchOS}};
|
|
149
|
|
150 auto iter = platformMap.find(platform);
|
|
151 if (iter == platformMap.end())
|
|
152 return platform;
|
|
153 return iter->second;
|
|
154 }
|
|
155
|
|
156 static bool checkCompatibility(const InputFile *input) {
|
|
157 std::vector<PlatformInfo> platformInfos = getPlatformInfos(input);
|
|
158 if (platformInfos.empty())
|
|
159 return true;
|
|
160
|
|
161 auto it = find_if(platformInfos, [&](const PlatformInfo &info) {
|
|
162 return removeSimulator(info.target.Platform) ==
|
|
163 removeSimulator(config->platform());
|
|
164 });
|
|
165 if (it == platformInfos.end()) {
|
|
166 std::string platformNames;
|
|
167 raw_string_ostream os(platformNames);
|
|
168 interleave(
|
|
169 platformInfos, os,
|
|
170 [&](const PlatformInfo &info) {
|
|
171 os << getPlatformName(info.target.Platform);
|
|
172 },
|
|
173 "/");
|
|
174 error(toString(input) + " has platform " + platformNames +
|
|
175 Twine(", which is different from target platform ") +
|
|
176 getPlatformName(config->platform()));
|
|
177 return false;
|
|
178 }
|
|
179
|
|
180 if (it->minimum <= config->platformInfo.minimum)
|
|
181 return true;
|
|
182
|
|
183 error(toString(input) + " has version " + it->minimum.getAsString() +
|
|
184 ", which is newer than target minimum of " +
|
|
185 config->platformInfo.minimum.getAsString());
|
|
186 return false;
|
|
187 }
|
173
|
188
|
|
189 // Open a given file path and return it as a memory-mapped file.
|
|
190 Optional<MemoryBufferRef> macho::readFile(StringRef path) {
|
207
|
191 ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = MemoryBuffer::getFile(path);
|
|
192 if (std::error_code ec = mbOrErr.getError()) {
|
173
|
193 error("cannot open " + path + ": " + ec.message());
|
|
194 return None;
|
|
195 }
|
|
196
|
|
197 std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
|
|
198 MemoryBufferRef mbref = mb->getMemBufferRef();
|
|
199 make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take mb ownership
|
|
200
|
|
201 // If this is a regular non-fat file, return it.
|
|
202 const char *buf = mbref.getBufferStart();
|
207
|
203 const auto *hdr = reinterpret_cast<const fat_header *>(buf);
|
|
204 if (mbref.getBufferSize() < sizeof(uint32_t) ||
|
|
205 read32be(&hdr->magic) != FAT_MAGIC) {
|
|
206 if (tar)
|
|
207 tar->append(relativeToRoot(path), mbref.getBuffer());
|
173
|
208 return mbref;
|
207
|
209 }
|
173
|
210
|
207
|
211 // Object files and archive files may be fat files, which contain multiple
|
|
212 // real files for different CPU ISAs. Here, we search for a file that matches
|
|
213 // with the current link target and returns it as a MemoryBufferRef.
|
|
214 const auto *arch = reinterpret_cast<const fat_arch *>(buf + sizeof(*hdr));
|
173
|
215
|
|
216 for (uint32_t i = 0, n = read32be(&hdr->nfat_arch); i < n; ++i) {
|
|
217 if (reinterpret_cast<const char *>(arch + i + 1) >
|
|
218 buf + mbref.getBufferSize()) {
|
|
219 error(path + ": fat_arch struct extends beyond end of file");
|
|
220 return None;
|
|
221 }
|
|
222
|
207
|
223 if (read32be(&arch[i].cputype) != static_cast<uint32_t>(target->cpuType) ||
|
173
|
224 read32be(&arch[i].cpusubtype) != target->cpuSubtype)
|
|
225 continue;
|
|
226
|
|
227 uint32_t offset = read32be(&arch[i].offset);
|
|
228 uint32_t size = read32be(&arch[i].size);
|
|
229 if (offset + size > mbref.getBufferSize())
|
|
230 error(path + ": slice extends beyond end of file");
|
207
|
231 if (tar)
|
|
232 tar->append(relativeToRoot(path), mbref.getBuffer());
|
173
|
233 return MemoryBufferRef(StringRef(buf + offset, size), path.copy(bAlloc));
|
|
234 }
|
|
235
|
|
236 error("unable to find matching architecture in " + path);
|
|
237 return None;
|
|
238 }
|
|
239
|
207
|
240 InputFile::InputFile(Kind kind, const InterfaceFile &interface)
|
|
241 : id(idCount++), fileKind(kind), name(saver.save(interface.getPath())) {}
|
173
|
242
|
207
|
243 template <class Section>
|
|
244 void ObjFile::parseSections(ArrayRef<Section> sections) {
|
173
|
245 subsections.reserve(sections.size());
|
|
246 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
|
|
247
|
207
|
248 for (const Section &sec : sections) {
|
173
|
249 InputSection *isec = make<InputSection>();
|
|
250 isec->file = this;
|
207
|
251 isec->name =
|
|
252 StringRef(sec.sectname, strnlen(sec.sectname, sizeof(sec.sectname)));
|
|
253 isec->segname =
|
|
254 StringRef(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
|
|
255 isec->data = {isZeroFill(sec.flags) ? nullptr : buf + sec.offset,
|
|
256 static_cast<size_t>(sec.size)};
|
173
|
257 if (sec.align >= 32)
|
|
258 error("alignment " + std::to_string(sec.align) + " of section " +
|
|
259 isec->name + " is too large");
|
|
260 else
|
|
261 isec->align = 1 << sec.align;
|
|
262 isec->flags = sec.flags;
|
207
|
263
|
|
264 if (!(isDebugSection(isec->flags) &&
|
|
265 isec->segname == segment_names::dwarf)) {
|
|
266 subsections.push_back({{0, isec}});
|
|
267 } else {
|
|
268 // Instead of emitting DWARF sections, we emit STABS symbols to the
|
|
269 // object files that contain them. We filter them out early to avoid
|
|
270 // parsing their relocations unnecessarily. But we must still push an
|
|
271 // empty map to ensure the indices line up for the remaining sections.
|
|
272 subsections.push_back({});
|
|
273 debugSections.push_back(isec);
|
|
274 }
|
173
|
275 }
|
|
276 }
|
|
277
|
|
278 // Find the subsection corresponding to the greatest section offset that is <=
|
|
279 // that of the given offset.
|
|
280 //
|
|
281 // offset: an offset relative to the start of the original InputSection (before
|
|
282 // any subsection splitting has occurred). It will be updated to represent the
|
|
283 // same location as an offset relative to the start of the containing
|
|
284 // subsection.
|
|
285 static InputSection *findContainingSubsection(SubsectionMap &map,
|
207
|
286 uint64_t *offset) {
|
|
287 auto it = std::prev(llvm::upper_bound(
|
|
288 map, *offset, [](uint64_t value, SubsectionEntry subsecEntry) {
|
|
289 return value < subsecEntry.offset;
|
|
290 }));
|
|
291 *offset -= it->offset;
|
|
292 return it->isec;
|
|
293 }
|
|
294
|
|
295 template <class Section>
|
|
296 static bool validateRelocationInfo(InputFile *file, const Section &sec,
|
|
297 relocation_info rel) {
|
|
298 const RelocAttrs &relocAttrs = target->getRelocAttrs(rel.r_type);
|
|
299 bool valid = true;
|
|
300 auto message = [relocAttrs, file, sec, rel, &valid](const Twine &diagnostic) {
|
|
301 valid = false;
|
|
302 return (relocAttrs.name + " relocation " + diagnostic + " at offset " +
|
|
303 std::to_string(rel.r_address) + " of " + sec.segname + "," +
|
|
304 sec.sectname + " in " + toString(file))
|
|
305 .str();
|
|
306 };
|
|
307
|
|
308 if (!relocAttrs.hasAttr(RelocAttrBits::LOCAL) && !rel.r_extern)
|
|
309 error(message("must be extern"));
|
|
310 if (relocAttrs.hasAttr(RelocAttrBits::PCREL) != rel.r_pcrel)
|
|
311 error(message(Twine("must ") + (rel.r_pcrel ? "not " : "") +
|
|
312 "be PC-relative"));
|
|
313 if (isThreadLocalVariables(sec.flags) &&
|
|
314 !relocAttrs.hasAttr(RelocAttrBits::UNSIGNED))
|
|
315 error(message("not allowed in thread-local section, must be UNSIGNED"));
|
|
316 if (rel.r_length < 2 || rel.r_length > 3 ||
|
|
317 !relocAttrs.hasAttr(static_cast<RelocAttrBits>(1 << rel.r_length))) {
|
|
318 static SmallVector<StringRef, 4> widths{"0", "4", "8", "4 or 8"};
|
|
319 error(message("has width " + std::to_string(1 << rel.r_length) +
|
|
320 " bytes, but must be " +
|
|
321 widths[(static_cast<int>(relocAttrs.bits) >> 2) & 3] +
|
|
322 " bytes"));
|
|
323 }
|
|
324 return valid;
|
173
|
325 }
|
|
326
|
207
|
327 template <class Section>
|
|
328 void ObjFile::parseRelocations(ArrayRef<Section> sectionHeaders,
|
|
329 const Section &sec, SubsectionMap &subsecMap) {
|
173
|
330 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
|
207
|
331 ArrayRef<relocation_info> relInfos(
|
|
332 reinterpret_cast<const relocation_info *>(buf + sec.reloff), sec.nreloc);
|
173
|
333
|
207
|
334 for (size_t i = 0; i < relInfos.size(); i++) {
|
|
335 // Paired relocations serve as Mach-O's method for attaching a
|
|
336 // supplemental datum to a primary relocation record. ELF does not
|
|
337 // need them because the *_RELOC_RELA records contain the extra
|
|
338 // addend field, vs. *_RELOC_REL which omit the addend.
|
|
339 //
|
|
340 // The {X86_64,ARM64}_RELOC_SUBTRACTOR record holds the subtrahend,
|
|
341 // and the paired *_RELOC_UNSIGNED record holds the minuend. The
|
|
342 // datum for each is a symbolic address. The result is the offset
|
|
343 // between two addresses.
|
|
344 //
|
|
345 // The ARM64_RELOC_ADDEND record holds the addend, and the paired
|
|
346 // ARM64_RELOC_BRANCH26 or ARM64_RELOC_PAGE21/PAGEOFF12 holds the
|
|
347 // base symbolic address.
|
|
348 //
|
|
349 // Note: X86 does not use *_RELOC_ADDEND because it can embed an
|
|
350 // addend into the instruction stream. On X86, a relocatable address
|
|
351 // field always occupies an entire contiguous sequence of byte(s),
|
|
352 // so there is no need to merge opcode bits with address
|
|
353 // bits. Therefore, it's easy and convenient to store addends in the
|
|
354 // instruction-stream bytes that would otherwise contain zeroes. By
|
|
355 // contrast, RISC ISAs such as ARM64 mix opcode bits with with
|
|
356 // address bits so that bitwise arithmetic is necessary to extract
|
|
357 // and insert them. Storing addends in the instruction stream is
|
|
358 // possible, but inconvenient and more costly at link time.
|
|
359
|
|
360 int64_t pairedAddend = 0;
|
|
361 relocation_info relInfo = relInfos[i];
|
|
362 if (target->hasAttr(relInfo.r_type, RelocAttrBits::ADDEND)) {
|
|
363 pairedAddend = SignExtend64<24>(relInfo.r_symbolnum);
|
|
364 relInfo = relInfos[++i];
|
|
365 }
|
|
366 assert(i < relInfos.size());
|
|
367 if (!validateRelocationInfo(this, sec, relInfo))
|
|
368 continue;
|
|
369 if (relInfo.r_address & R_SCATTERED)
|
173
|
370 fatal("TODO: Scattered relocations not supported");
|
|
371
|
207
|
372 bool isSubtrahend =
|
|
373 target->hasAttr(relInfo.r_type, RelocAttrBits::SUBTRAHEND);
|
|
374 int64_t embeddedAddend = target->getEmbeddedAddend(mb, sec.offset, relInfo);
|
|
375 assert(!(embeddedAddend && pairedAddend));
|
|
376 int64_t totalAddend = pairedAddend + embeddedAddend;
|
173
|
377 Reloc r;
|
207
|
378 r.type = relInfo.r_type;
|
|
379 r.pcrel = relInfo.r_pcrel;
|
|
380 r.length = relInfo.r_length;
|
|
381 r.offset = relInfo.r_address;
|
|
382 if (relInfo.r_extern) {
|
|
383 r.referent = symbols[relInfo.r_symbolnum];
|
|
384 r.addend = isSubtrahend ? 0 : totalAddend;
|
173
|
385 } else {
|
207
|
386 assert(!isSubtrahend);
|
|
387 const Section &referentSec = sectionHeaders[relInfo.r_symbolnum - 1];
|
|
388 uint64_t referentOffset;
|
|
389 if (relInfo.r_pcrel) {
|
|
390 // The implicit addend for pcrel section relocations is the pcrel offset
|
|
391 // in terms of the addresses in the input file. Here we adjust it so
|
|
392 // that it describes the offset from the start of the referent section.
|
|
393 // FIXME This logic was written around x86_64 behavior -- ARM64 doesn't
|
|
394 // have pcrel section relocations. We may want to factor this out into
|
|
395 // the arch-specific .cpp file.
|
|
396 assert(target->hasAttr(r.type, RelocAttrBits::BYTE4));
|
|
397 referentOffset =
|
|
398 sec.addr + relInfo.r_address + 4 + totalAddend - referentSec.addr;
|
|
399 } else {
|
|
400 // The addend for a non-pcrel relocation is its absolute address.
|
|
401 referentOffset = totalAddend - referentSec.addr;
|
|
402 }
|
|
403 SubsectionMap &referentSubsecMap = subsections[relInfo.r_symbolnum - 1];
|
|
404 r.referent = findContainingSubsection(referentSubsecMap, &referentOffset);
|
|
405 r.addend = referentOffset;
|
173
|
406 }
|
|
407
|
207
|
408 InputSection *subsec = findContainingSubsection(subsecMap, &r.offset);
|
173
|
409 subsec->relocs.push_back(r);
|
207
|
410
|
|
411 if (isSubtrahend) {
|
|
412 relocation_info minuendInfo = relInfos[++i];
|
|
413 // SUBTRACTOR relocations should always be followed by an UNSIGNED one
|
|
414 // attached to the same address.
|
|
415 assert(target->hasAttr(minuendInfo.r_type, RelocAttrBits::UNSIGNED) &&
|
|
416 relInfo.r_address == minuendInfo.r_address);
|
|
417 Reloc p;
|
|
418 p.type = minuendInfo.r_type;
|
|
419 if (minuendInfo.r_extern) {
|
|
420 p.referent = symbols[minuendInfo.r_symbolnum];
|
|
421 p.addend = totalAddend;
|
|
422 } else {
|
|
423 uint64_t referentOffset =
|
|
424 totalAddend - sectionHeaders[minuendInfo.r_symbolnum - 1].addr;
|
|
425 SubsectionMap &referentSubsecMap =
|
|
426 subsections[minuendInfo.r_symbolnum - 1];
|
|
427 p.referent =
|
|
428 findContainingSubsection(referentSubsecMap, &referentOffset);
|
|
429 p.addend = referentOffset;
|
|
430 }
|
|
431 subsec->relocs.push_back(p);
|
|
432 }
|
173
|
433 }
|
|
434 }
|
|
435
|
207
|
436 template <class NList>
|
|
437 static macho::Symbol *createDefined(const NList &sym, StringRef name,
|
|
438 InputSection *isec, uint64_t value,
|
|
439 uint64_t size) {
|
|
440 // Symbol scope is determined by sym.n_type & (N_EXT | N_PEXT):
|
|
441 // N_EXT: Global symbols. These go in the symbol table during the link,
|
|
442 // and also in the export table of the output so that the dynamic
|
|
443 // linker sees them.
|
|
444 // N_EXT | N_PEXT: Linkage unit (think: dylib) scoped. These go in the
|
|
445 // symbol table during the link so that duplicates are
|
|
446 // either reported (for non-weak symbols) or merged
|
|
447 // (for weak symbols), but they do not go in the export
|
|
448 // table of the output.
|
|
449 // N_PEXT: Does not occur in input files in practice,
|
|
450 // a private extern must be external.
|
|
451 // 0: Translation-unit scoped. These are not in the symbol table during
|
|
452 // link, and not in the export table of the output either.
|
173
|
453
|
207
|
454 bool isWeakDefCanBeHidden =
|
|
455 (sym.n_desc & (N_WEAK_DEF | N_WEAK_REF)) == (N_WEAK_DEF | N_WEAK_REF);
|
173
|
456
|
207
|
457 if (sym.n_type & (N_EXT | N_PEXT)) {
|
|
458 assert((sym.n_type & N_EXT) && "invalid input");
|
|
459 bool isPrivateExtern = sym.n_type & N_PEXT;
|
173
|
460
|
207
|
461 // lld's behavior for merging symbols is slightly different from ld64:
|
|
462 // ld64 picks the winning symbol based on several criteria (see
|
|
463 // pickBetweenRegularAtoms() in ld64's SymbolTable.cpp), while lld
|
|
464 // just merges metadata and keeps the contents of the first symbol
|
|
465 // with that name (see SymbolTable::addDefined). For:
|
|
466 // * inline function F in a TU built with -fvisibility-inlines-hidden
|
|
467 // * and inline function F in another TU built without that flag
|
|
468 // ld64 will pick the one from the file built without
|
|
469 // -fvisibility-inlines-hidden.
|
|
470 // lld will instead pick the one listed first on the link command line and
|
|
471 // give it visibility as if the function was built without
|
|
472 // -fvisibility-inlines-hidden.
|
|
473 // If both functions have the same contents, this will have the same
|
|
474 // behavior. If not, it won't, but the input had an ODR violation in
|
|
475 // that case.
|
|
476 //
|
|
477 // Similarly, merging a symbol
|
|
478 // that's isPrivateExtern and not isWeakDefCanBeHidden with one
|
|
479 // that's not isPrivateExtern but isWeakDefCanBeHidden technically
|
|
480 // should produce one
|
|
481 // that's not isPrivateExtern but isWeakDefCanBeHidden. That matters
|
|
482 // with ld64's semantics, because it means the non-private-extern
|
|
483 // definition will continue to take priority if more private extern
|
|
484 // definitions are encountered. With lld's semantics there's no observable
|
|
485 // difference between a symbol that's isWeakDefCanBeHidden or one that's
|
|
486 // privateExtern -- neither makes it into the dynamic symbol table. So just
|
|
487 // promote isWeakDefCanBeHidden to isPrivateExtern here.
|
|
488 if (isWeakDefCanBeHidden)
|
|
489 isPrivateExtern = true;
|
173
|
490
|
207
|
491 return symtab->addDefined(
|
|
492 name, isec->file, isec, value, size, sym.n_desc & N_WEAK_DEF,
|
|
493 isPrivateExtern, sym.n_desc & N_ARM_THUMB_DEF,
|
|
494 sym.n_desc & REFERENCED_DYNAMICALLY, sym.n_desc & N_NO_DEAD_STRIP);
|
173
|
495 }
|
|
496
|
207
|
497 assert(!isWeakDefCanBeHidden &&
|
|
498 "weak_def_can_be_hidden on already-hidden symbol?");
|
|
499 return make<Defined>(
|
|
500 name, isec->file, isec, value, size, sym.n_desc & N_WEAK_DEF,
|
|
501 /*isExternal=*/false, /*isPrivateExtern=*/false,
|
|
502 sym.n_desc & N_ARM_THUMB_DEF, sym.n_desc & REFERENCED_DYNAMICALLY,
|
|
503 sym.n_desc & N_NO_DEAD_STRIP);
|
|
504 }
|
|
505
|
|
506 // Absolute symbols are defined symbols that do not have an associated
|
|
507 // InputSection. They cannot be weak.
|
|
508 template <class NList>
|
|
509 static macho::Symbol *createAbsolute(const NList &sym, InputFile *file,
|
|
510 StringRef name) {
|
|
511 if (sym.n_type & (N_EXT | N_PEXT)) {
|
|
512 assert((sym.n_type & N_EXT) && "invalid input");
|
|
513 return symtab->addDefined(name, file, nullptr, sym.n_value, /*size=*/0,
|
|
514 /*isWeakDef=*/false, sym.n_type & N_PEXT,
|
|
515 sym.n_desc & N_ARM_THUMB_DEF,
|
|
516 /*isReferencedDynamically=*/false,
|
|
517 sym.n_desc & N_NO_DEAD_STRIP);
|
|
518 }
|
|
519 return make<Defined>(name, file, nullptr, sym.n_value, /*size=*/0,
|
|
520 /*isWeakDef=*/false,
|
|
521 /*isExternal=*/false, /*isPrivateExtern=*/false,
|
|
522 sym.n_desc & N_ARM_THUMB_DEF,
|
|
523 /*isReferencedDynamically=*/false,
|
|
524 sym.n_desc & N_NO_DEAD_STRIP);
|
|
525 }
|
|
526
|
|
527 template <class NList>
|
|
528 macho::Symbol *ObjFile::parseNonSectionSymbol(const NList &sym,
|
|
529 StringRef name) {
|
|
530 uint8_t type = sym.n_type & N_TYPE;
|
|
531 switch (type) {
|
|
532 case N_UNDF:
|
|
533 return sym.n_value == 0
|
|
534 ? symtab->addUndefined(name, this, sym.n_desc & N_WEAK_REF)
|
|
535 : symtab->addCommon(name, this, sym.n_value,
|
|
536 1 << GET_COMM_ALIGN(sym.n_desc),
|
|
537 sym.n_type & N_PEXT);
|
|
538 case N_ABS:
|
|
539 return createAbsolute(sym, this, name);
|
|
540 case N_PBUD:
|
|
541 case N_INDR:
|
|
542 error("TODO: support symbols of type " + std::to_string(type));
|
|
543 return nullptr;
|
|
544 case N_SECT:
|
|
545 llvm_unreachable(
|
|
546 "N_SECT symbols should not be passed to parseNonSectionSymbol");
|
|
547 default:
|
|
548 llvm_unreachable("invalid symbol type");
|
173
|
549 }
|
|
550 }
|
|
551
|
207
|
552 template <class LP>
|
|
553 void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
|
|
554 ArrayRef<typename LP::nlist> nList,
|
|
555 const char *strtab, bool subsectionsViaSymbols) {
|
|
556 using NList = typename LP::nlist;
|
|
557
|
|
558 // Groups indices of the symbols by the sections that contain them.
|
|
559 std::vector<std::vector<uint32_t>> symbolsBySection(subsections.size());
|
|
560 symbols.resize(nList.size());
|
|
561 for (uint32_t i = 0; i < nList.size(); ++i) {
|
|
562 const NList &sym = nList[i];
|
|
563 StringRef name = strtab + sym.n_strx;
|
|
564 if ((sym.n_type & N_TYPE) == N_SECT) {
|
|
565 SubsectionMap &subsecMap = subsections[sym.n_sect - 1];
|
|
566 // parseSections() may have chosen not to parse this section.
|
|
567 if (subsecMap.empty())
|
|
568 continue;
|
|
569 symbolsBySection[sym.n_sect - 1].push_back(i);
|
|
570 } else {
|
|
571 symbols[i] = parseNonSectionSymbol(sym, name);
|
|
572 }
|
|
573 }
|
|
574
|
|
575 // Calculate symbol sizes and create subsections by splitting the sections
|
|
576 // along symbol boundaries.
|
|
577 for (size_t i = 0; i < subsections.size(); ++i) {
|
|
578 SubsectionMap &subsecMap = subsections[i];
|
|
579 if (subsecMap.empty())
|
|
580 continue;
|
|
581
|
|
582 std::vector<uint32_t> &symbolIndices = symbolsBySection[i];
|
|
583 llvm::sort(symbolIndices, [&](uint32_t lhs, uint32_t rhs) {
|
|
584 return nList[lhs].n_value < nList[rhs].n_value;
|
|
585 });
|
|
586 uint64_t sectionAddr = sectionHeaders[i].addr;
|
|
587 uint32_t sectionAlign = 1u << sectionHeaders[i].align;
|
|
588
|
|
589 // We populate subsecMap by repeatedly splitting the last (highest address)
|
|
590 // subsection.
|
|
591 SubsectionEntry subsecEntry = subsecMap.back();
|
|
592 for (size_t j = 0; j < symbolIndices.size(); ++j) {
|
|
593 uint32_t symIndex = symbolIndices[j];
|
|
594 const NList &sym = nList[symIndex];
|
|
595 StringRef name = strtab + sym.n_strx;
|
|
596 InputSection *isec = subsecEntry.isec;
|
|
597
|
|
598 uint64_t subsecAddr = sectionAddr + subsecEntry.offset;
|
|
599 uint64_t symbolOffset = sym.n_value - subsecAddr;
|
|
600 uint64_t symbolSize =
|
|
601 j + 1 < symbolIndices.size()
|
|
602 ? nList[symbolIndices[j + 1]].n_value - sym.n_value
|
|
603 : isec->data.size() - symbolOffset;
|
|
604 // There are 3 cases where we do not need to create a new subsection:
|
|
605 // 1. If the input file does not use subsections-via-symbols.
|
|
606 // 2. Multiple symbols at the same address only induce one subsection.
|
|
607 // (The symbolOffset == 0 check covers both this case as well as
|
|
608 // the first loop iteration.)
|
|
609 // 3. Alternative entry points do not induce new subsections.
|
|
610 if (!subsectionsViaSymbols || symbolOffset == 0 ||
|
|
611 sym.n_desc & N_ALT_ENTRY) {
|
|
612 symbols[symIndex] =
|
|
613 createDefined(sym, name, isec, symbolOffset, symbolSize);
|
|
614 continue;
|
|
615 }
|
173
|
616
|
207
|
617 auto *nextIsec = make<InputSection>(*isec);
|
|
618 nextIsec->data = isec->data.slice(symbolOffset);
|
|
619 nextIsec->numRefs = 0;
|
|
620 nextIsec->wasCoalesced = false;
|
|
621 isec->data = isec->data.slice(0, symbolOffset);
|
|
622
|
|
623 // By construction, the symbol will be at offset zero in the new
|
|
624 // subsection.
|
|
625 symbols[symIndex] =
|
|
626 createDefined(sym, name, nextIsec, /*value=*/0, symbolSize);
|
|
627 // TODO: ld64 appears to preserve the original alignment as well as each
|
|
628 // subsection's offset from the last aligned address. We should consider
|
|
629 // emulating that behavior.
|
|
630 nextIsec->align = MinAlign(sectionAlign, sym.n_value);
|
|
631 subsecMap.push_back({sym.n_value - sectionAddr, nextIsec});
|
|
632 subsecEntry = subsecMap.back();
|
|
633 }
|
|
634 }
|
|
635 }
|
|
636
|
|
637 OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName,
|
|
638 StringRef sectName)
|
|
639 : InputFile(OpaqueKind, mb) {
|
|
640 InputSection *isec = make<InputSection>();
|
|
641 isec->file = this;
|
|
642 isec->name = sectName.take_front(16);
|
|
643 isec->segname = segName.take_front(16);
|
|
644 const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
|
|
645 isec->data = {buf, mb.getBufferSize()};
|
|
646 isec->live = true;
|
|
647 subsections.push_back({{0, isec}});
|
|
648 }
|
|
649
|
|
650 ObjFile::ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName)
|
|
651 : InputFile(ObjKind, mb), modTime(modTime) {
|
|
652 this->archiveName = std::string(archiveName);
|
|
653 if (target->wordSize == 8)
|
|
654 parse<LP64>();
|
|
655 else
|
|
656 parse<ILP32>();
|
|
657 }
|
|
658
|
|
659 template <class LP> void ObjFile::parse() {
|
|
660 using Header = typename LP::mach_header;
|
|
661 using SegmentCommand = typename LP::segment_command;
|
|
662 using Section = typename LP::section;
|
|
663 using NList = typename LP::nlist;
|
|
664
|
|
665 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
|
|
666 auto *hdr = reinterpret_cast<const Header *>(mb.getBufferStart());
|
|
667
|
|
668 Architecture arch = getArchitectureFromCpuType(hdr->cputype, hdr->cpusubtype);
|
|
669 if (arch != config->arch()) {
|
|
670 error(toString(this) + " has architecture " + getArchitectureName(arch) +
|
|
671 " which is incompatible with target architecture " +
|
|
672 getArchitectureName(config->arch()));
|
|
673 return;
|
|
674 }
|
|
675
|
|
676 if (!checkCompatibility(this))
|
|
677 return;
|
|
678
|
|
679 if (const load_command *cmd = findCommand(hdr, LC_LINKER_OPTION)) {
|
|
680 auto *c = reinterpret_cast<const linker_option_command *>(cmd);
|
|
681 StringRef data{reinterpret_cast<const char *>(c + 1),
|
|
682 c->cmdsize - sizeof(linker_option_command)};
|
|
683 parseLCLinkerOption(this, c->count, data);
|
|
684 }
|
|
685
|
|
686 ArrayRef<Section> sectionHeaders;
|
|
687 if (const load_command *cmd = findCommand(hdr, LP::segmentLCType)) {
|
|
688 auto *c = reinterpret_cast<const SegmentCommand *>(cmd);
|
|
689 sectionHeaders =
|
|
690 ArrayRef<Section>{reinterpret_cast<const Section *>(c + 1), c->nsects};
|
173
|
691 parseSections(sectionHeaders);
|
|
692 }
|
|
693
|
|
694 // TODO: Error on missing LC_SYMTAB?
|
|
695 if (const load_command *cmd = findCommand(hdr, LC_SYMTAB)) {
|
|
696 auto *c = reinterpret_cast<const symtab_command *>(cmd);
|
207
|
697 ArrayRef<NList> nList(reinterpret_cast<const NList *>(buf + c->symoff),
|
|
698 c->nsyms);
|
173
|
699 const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;
|
|
700 bool subsectionsViaSymbols = hdr->flags & MH_SUBSECTIONS_VIA_SYMBOLS;
|
207
|
701 parseSymbols<LP>(sectionHeaders, nList, strtab, subsectionsViaSymbols);
|
173
|
702 }
|
|
703
|
|
704 // The relocations may refer to the symbols, so we parse them after we have
|
|
705 // parsed all the symbols.
|
|
706 for (size_t i = 0, n = subsections.size(); i < n; ++i)
|
207
|
707 if (!subsections[i].empty())
|
|
708 parseRelocations(sectionHeaders, sectionHeaders[i], subsections[i]);
|
|
709
|
|
710 parseDebugInfo();
|
|
711 }
|
|
712
|
|
713 void ObjFile::parseDebugInfo() {
|
|
714 std::unique_ptr<DwarfObject> dObj = DwarfObject::create(this);
|
|
715 if (!dObj)
|
|
716 return;
|
|
717
|
|
718 auto *ctx = make<DWARFContext>(
|
|
719 std::move(dObj), "",
|
|
720 [&](Error err) {
|
|
721 warn(toString(this) + ": " + toString(std::move(err)));
|
|
722 },
|
|
723 [&](Error warning) {
|
|
724 warn(toString(this) + ": " + toString(std::move(warning)));
|
|
725 });
|
|
726
|
|
727 // TODO: Since object files can contain a lot of DWARF info, we should verify
|
|
728 // that we are parsing just the info we need
|
|
729 const DWARFContext::compile_unit_range &units = ctx->compile_units();
|
|
730 // FIXME: There can be more than one compile unit per object file. See
|
|
731 // PR48637.
|
|
732 auto it = units.begin();
|
|
733 compileUnit = it->get();
|
|
734 }
|
|
735
|
|
736 // The path can point to either a dylib or a .tbd file.
|
|
737 static DylibFile *loadDylib(StringRef path, DylibFile *umbrella) {
|
|
738 Optional<MemoryBufferRef> mbref = readFile(path);
|
|
739 if (!mbref) {
|
|
740 error("could not read dylib file at " + path);
|
|
741 return nullptr;
|
|
742 }
|
|
743 return loadDylib(*mbref, umbrella);
|
173
|
744 }
|
|
745
|
207
|
746 // TBD files are parsed into a series of TAPI documents (InterfaceFiles), with
|
|
747 // the first document storing child pointers to the rest of them. When we are
|
|
748 // processing a given TBD file, we store that top-level document in
|
|
749 // currentTopLevelTapi. When processing re-exports, we search its children for
|
|
750 // potentially matching documents in the same TBD file. Note that the children
|
|
751 // themselves don't point to further documents, i.e. this is a two-level tree.
|
|
752 //
|
|
753 // Re-exports can either refer to on-disk files, or to documents within .tbd
|
|
754 // files.
|
|
755 static DylibFile *findDylib(StringRef path, DylibFile *umbrella,
|
|
756 const InterfaceFile *currentTopLevelTapi) {
|
|
757 if (path::is_absolute(path, path::Style::posix))
|
|
758 for (StringRef root : config->systemLibraryRoots)
|
|
759 if (Optional<std::string> dylibPath =
|
|
760 resolveDylibPath((root + path).str()))
|
|
761 return loadDylib(*dylibPath, umbrella);
|
|
762
|
|
763 // TODO: Handle -dylib_file
|
|
764
|
|
765 SmallString<128> newPath;
|
|
766 if (config->outputType == MH_EXECUTE &&
|
|
767 path.consume_front("@executable_path/")) {
|
|
768 // ld64 allows overriding this with the undocumented flag -executable_path.
|
|
769 // lld doesn't currently implement that flag.
|
|
770 path::append(newPath, sys::path::parent_path(config->outputFile), path);
|
|
771 path = newPath;
|
|
772 } else if (path.consume_front("@loader_path/")) {
|
|
773 path::append(newPath, sys::path::parent_path(umbrella->getName()), path);
|
|
774 path = newPath;
|
|
775 } else if (path.startswith("@rpath/")) {
|
|
776 for (StringRef rpath : umbrella->rpaths) {
|
|
777 newPath.clear();
|
|
778 if (rpath.consume_front("@loader_path/"))
|
|
779 path::append(newPath, sys::path::parent_path(umbrella->getName()));
|
|
780 path::append(newPath, rpath, path.drop_front(strlen("@rpath/")));
|
|
781 if (Optional<std::string> dylibPath = resolveDylibPath(newPath))
|
|
782 return loadDylib(*dylibPath, umbrella);
|
|
783 }
|
|
784 }
|
|
785
|
|
786 if (currentTopLevelTapi) {
|
|
787 for (InterfaceFile &child :
|
|
788 make_pointee_range(currentTopLevelTapi->documents())) {
|
|
789 assert(child.documents().empty());
|
|
790 if (path == child.getInstallName()) {
|
|
791 auto file = make<DylibFile>(child, umbrella);
|
|
792 file->parseReexports(child);
|
|
793 return file;
|
|
794 }
|
|
795 }
|
|
796 }
|
|
797
|
|
798 if (Optional<std::string> dylibPath = resolveDylibPath(path))
|
|
799 return loadDylib(*dylibPath, umbrella);
|
|
800
|
|
801 return nullptr;
|
|
802 }
|
|
803
|
|
804 // If a re-exported dylib is public (lives in /usr/lib or
|
|
805 // /System/Library/Frameworks), then it is considered implicitly linked: we
|
|
806 // should bind to its symbols directly instead of via the re-exporting umbrella
|
|
807 // library.
|
|
808 static bool isImplicitlyLinked(StringRef path) {
|
|
809 if (!config->implicitDylibs)
|
|
810 return false;
|
|
811
|
|
812 if (path::parent_path(path) == "/usr/lib")
|
|
813 return true;
|
|
814
|
|
815 // Match /System/Library/Frameworks/$FOO.framework/**/$FOO
|
|
816 if (path.consume_front("/System/Library/Frameworks/")) {
|
|
817 StringRef frameworkName = path.take_until([](char c) { return c == '.'; });
|
|
818 return path::filename(path) == frameworkName;
|
|
819 }
|
|
820
|
|
821 return false;
|
|
822 }
|
|
823
|
|
824 static void loadReexport(StringRef path, DylibFile *umbrella,
|
|
825 const InterfaceFile *currentTopLevelTapi) {
|
|
826 DylibFile *reexport = findDylib(path, umbrella, currentTopLevelTapi);
|
|
827 if (!reexport)
|
|
828 error("unable to locate re-export with install name " + path);
|
|
829 else if (isImplicitlyLinked(path))
|
|
830 inputFiles.insert(reexport);
|
|
831 }
|
|
832
|
|
833 DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella,
|
|
834 bool isBundleLoader)
|
|
835 : InputFile(DylibKind, mb), refState(RefState::Unreferenced),
|
|
836 isBundleLoader(isBundleLoader) {
|
|
837 assert(!isBundleLoader || !umbrella);
|
173
|
838 if (umbrella == nullptr)
|
|
839 umbrella = this;
|
207
|
840 this->umbrella = umbrella;
|
173
|
841
|
|
842 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
|
207
|
843 auto *hdr = reinterpret_cast<const mach_header *>(mb.getBufferStart());
|
173
|
844
|
207
|
845 // Initialize installName.
|
173
|
846 if (const load_command *cmd = findCommand(hdr, LC_ID_DYLIB)) {
|
|
847 auto *c = reinterpret_cast<const dylib_command *>(cmd);
|
207
|
848 currentVersion = read32le(&c->dylib.current_version);
|
|
849 compatibilityVersion = read32le(&c->dylib.compatibility_version);
|
|
850 installName =
|
|
851 reinterpret_cast<const char *>(cmd) + read32le(&c->dylib.name);
|
|
852 } else if (!isBundleLoader) {
|
|
853 // macho_executable and macho_bundle don't have LC_ID_DYLIB,
|
|
854 // so it's OK.
|
|
855 error("dylib " + toString(this) + " missing LC_ID_DYLIB load command");
|
173
|
856 return;
|
|
857 }
|
|
858
|
207
|
859 if (config->printEachFile)
|
|
860 message(toString(this));
|
|
861
|
|
862 deadStrippable = hdr->flags & MH_DEAD_STRIPPABLE_DYLIB;
|
|
863
|
|
864 if (!checkCompatibility(this))
|
|
865 return;
|
|
866
|
|
867 for (auto *cmd : findCommands<rpath_command>(hdr, LC_RPATH)) {
|
|
868 StringRef rpath{reinterpret_cast<const char *>(cmd) + cmd->path};
|
|
869 rpaths.push_back(rpath);
|
|
870 }
|
|
871
|
173
|
872 // Initialize symbols.
|
207
|
873 exportingFile = isImplicitlyLinked(installName) ? this : this->umbrella;
|
173
|
874 if (const load_command *cmd = findCommand(hdr, LC_DYLD_INFO_ONLY)) {
|
|
875 auto *c = reinterpret_cast<const dyld_info_command *>(cmd);
|
|
876 parseTrie(buf + c->export_off, c->export_size,
|
|
877 [&](const Twine &name, uint64_t flags) {
|
207
|
878 StringRef savedName = saver.save(name);
|
|
879 if (handleLDSymbol(savedName))
|
|
880 return;
|
|
881 bool isWeakDef = flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;
|
|
882 bool isTlv = flags & EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL;
|
|
883 symbols.push_back(symtab->addDylib(savedName, exportingFile,
|
|
884 isWeakDef, isTlv));
|
173
|
885 });
|
|
886 } else {
|
207
|
887 error("LC_DYLD_INFO_ONLY not found in " + toString(this));
|
173
|
888 return;
|
|
889 }
|
207
|
890 }
|
173
|
891
|
207
|
892 void DylibFile::parseLoadCommands(MemoryBufferRef mb) {
|
|
893 auto *hdr = reinterpret_cast<const mach_header *>(mb.getBufferStart());
|
|
894 const uint8_t *p = reinterpret_cast<const uint8_t *>(mb.getBufferStart()) +
|
|
895 target->headerSize;
|
173
|
896 for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {
|
|
897 auto *cmd = reinterpret_cast<const load_command *>(p);
|
|
898 p += cmd->cmdsize;
|
207
|
899
|
|
900 if (!(hdr->flags & MH_NO_REEXPORTED_DYLIBS) &&
|
|
901 cmd->cmd == LC_REEXPORT_DYLIB) {
|
|
902 const auto *c = reinterpret_cast<const dylib_command *>(cmd);
|
|
903 StringRef reexportPath =
|
|
904 reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);
|
|
905 loadReexport(reexportPath, exportingFile, nullptr);
|
|
906 }
|
|
907
|
|
908 // FIXME: What about LC_LOAD_UPWARD_DYLIB, LC_LAZY_LOAD_DYLIB,
|
|
909 // LC_LOAD_WEAK_DYLIB, LC_REEXPORT_DYLIB (..are reexports from dylibs with
|
|
910 // MH_NO_REEXPORTED_DYLIBS loaded for -flat_namespace)?
|
|
911 if (config->namespaceKind == NamespaceKind::flat &&
|
|
912 cmd->cmd == LC_LOAD_DYLIB) {
|
|
913 const auto *c = reinterpret_cast<const dylib_command *>(cmd);
|
|
914 StringRef dylibPath =
|
|
915 reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);
|
|
916 DylibFile *dylib = findDylib(dylibPath, umbrella, nullptr);
|
|
917 if (!dylib)
|
|
918 error(Twine("unable to locate library '") + dylibPath +
|
|
919 "' loaded from '" + toString(this) + "' for -flat_namespace");
|
|
920 }
|
|
921 }
|
|
922 }
|
|
923
|
|
924 // Some versions of XCode ship with .tbd files that don't have the right
|
|
925 // platform settings.
|
|
926 static constexpr std::array<StringRef, 3> skipPlatformChecks{
|
|
927 "/usr/lib/system/libsystem_kernel.dylib",
|
|
928 "/usr/lib/system/libsystem_platform.dylib",
|
|
929 "/usr/lib/system/libsystem_pthread.dylib"};
|
|
930
|
|
931 DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella,
|
|
932 bool isBundleLoader)
|
|
933 : InputFile(DylibKind, interface), refState(RefState::Unreferenced),
|
|
934 isBundleLoader(isBundleLoader) {
|
|
935 // FIXME: Add test for the missing TBD code path.
|
|
936
|
|
937 if (umbrella == nullptr)
|
|
938 umbrella = this;
|
|
939 this->umbrella = umbrella;
|
|
940
|
|
941 installName = saver.save(interface.getInstallName());
|
|
942 compatibilityVersion = interface.getCompatibilityVersion().rawValue();
|
|
943 currentVersion = interface.getCurrentVersion().rawValue();
|
|
944
|
|
945 if (config->printEachFile)
|
|
946 message(toString(this));
|
|
947
|
|
948 if (!is_contained(skipPlatformChecks, installName) &&
|
|
949 !is_contained(interface.targets(), config->platformInfo.target)) {
|
|
950 error(toString(this) + " is incompatible with " +
|
|
951 std::string(config->platformInfo.target));
|
|
952 return;
|
|
953 }
|
|
954
|
|
955 exportingFile = isImplicitlyLinked(installName) ? this : umbrella;
|
|
956 auto addSymbol = [&](const Twine &name) -> void {
|
|
957 symbols.push_back(symtab->addDylib(saver.save(name), exportingFile,
|
|
958 /*isWeakDef=*/false,
|
|
959 /*isTlv=*/false));
|
|
960 };
|
|
961 // TODO(compnerd) filter out symbols based on the target platform
|
|
962 // TODO: handle weak defs, thread locals
|
|
963 for (const auto *symbol : interface.symbols()) {
|
|
964 if (!symbol->getArchitectures().has(config->arch()))
|
173
|
965 continue;
|
|
966
|
207
|
967 if (handleLDSymbol(symbol->getName()))
|
|
968 continue;
|
|
969
|
|
970 switch (symbol->getKind()) {
|
|
971 case SymbolKind::GlobalSymbol:
|
|
972 addSymbol(symbol->getName());
|
|
973 break;
|
|
974 case SymbolKind::ObjectiveCClass:
|
|
975 // XXX ld64 only creates these symbols when -ObjC is passed in. We may
|
|
976 // want to emulate that.
|
|
977 addSymbol(objc::klass + symbol->getName());
|
|
978 addSymbol(objc::metaclass + symbol->getName());
|
|
979 break;
|
|
980 case SymbolKind::ObjectiveCClassEHType:
|
|
981 addSymbol(objc::ehtype + symbol->getName());
|
|
982 break;
|
|
983 case SymbolKind::ObjectiveCInstanceVariable:
|
|
984 addSymbol(objc::ivar + symbol->getName());
|
|
985 break;
|
173
|
986 }
|
|
987 }
|
|
988 }
|
|
989
|
207
|
990 void DylibFile::parseReexports(const InterfaceFile &interface) {
|
|
991 const InterfaceFile *topLevel =
|
|
992 interface.getParent() == nullptr ? &interface : interface.getParent();
|
|
993 for (InterfaceFileRef intfRef : interface.reexportedLibraries()) {
|
|
994 InterfaceFile::const_target_range targets = intfRef.targets();
|
|
995 if (is_contained(skipPlatformChecks, intfRef.getInstallName()) ||
|
|
996 is_contained(targets, config->platformInfo.target))
|
|
997 loadReexport(intfRef.getInstallName(), exportingFile, topLevel);
|
|
998 }
|
|
999 }
|
173
|
1000
|
207
|
1001 // $ld$ symbols modify the properties/behavior of the library (e.g. its install
|
|
1002 // name, compatibility version or hide/add symbols) for specific target
|
|
1003 // versions.
|
|
1004 bool DylibFile::handleLDSymbol(StringRef originalName) {
|
|
1005 if (!originalName.startswith("$ld$"))
|
|
1006 return false;
|
|
1007
|
|
1008 StringRef action;
|
|
1009 StringRef name;
|
|
1010 std::tie(action, name) = originalName.drop_front(strlen("$ld$")).split('$');
|
|
1011 if (action == "previous")
|
|
1012 handleLDPreviousSymbol(name, originalName);
|
|
1013 else if (action == "install_name")
|
|
1014 handleLDInstallNameSymbol(name, originalName);
|
|
1015 return true;
|
173
|
1016 }
|
|
1017
|
207
|
1018 void DylibFile::handleLDPreviousSymbol(StringRef name, StringRef originalName) {
|
|
1019 // originalName: $ld$ previous $ <installname> $ <compatversion> $
|
|
1020 // <platformstr> $ <startversion> $ <endversion> $ <symbol-name> $
|
|
1021 StringRef installName;
|
|
1022 StringRef compatVersion;
|
|
1023 StringRef platformStr;
|
|
1024 StringRef startVersion;
|
|
1025 StringRef endVersion;
|
|
1026 StringRef symbolName;
|
|
1027 StringRef rest;
|
|
1028
|
|
1029 std::tie(installName, name) = name.split('$');
|
|
1030 std::tie(compatVersion, name) = name.split('$');
|
|
1031 std::tie(platformStr, name) = name.split('$');
|
|
1032 std::tie(startVersion, name) = name.split('$');
|
|
1033 std::tie(endVersion, name) = name.split('$');
|
|
1034 std::tie(symbolName, rest) = name.split('$');
|
|
1035 // TODO: ld64 contains some logic for non-empty symbolName as well.
|
|
1036 if (!symbolName.empty())
|
|
1037 return;
|
|
1038 unsigned platform;
|
|
1039 if (platformStr.getAsInteger(10, platform) ||
|
|
1040 platform != static_cast<unsigned>(config->platform()))
|
|
1041 return;
|
|
1042
|
|
1043 VersionTuple start;
|
|
1044 if (start.tryParse(startVersion)) {
|
|
1045 warn("failed to parse start version, symbol '" + originalName +
|
|
1046 "' ignored");
|
|
1047 return;
|
|
1048 }
|
|
1049 VersionTuple end;
|
|
1050 if (end.tryParse(endVersion)) {
|
|
1051 warn("failed to parse end version, symbol '" + originalName + "' ignored");
|
|
1052 return;
|
|
1053 }
|
|
1054 if (config->platformInfo.minimum < start ||
|
|
1055 config->platformInfo.minimum >= end)
|
|
1056 return;
|
|
1057
|
|
1058 this->installName = saver.save(installName);
|
|
1059
|
|
1060 if (!compatVersion.empty()) {
|
|
1061 VersionTuple cVersion;
|
|
1062 if (cVersion.tryParse(compatVersion)) {
|
|
1063 warn("failed to parse compatibility version, symbol '" + originalName +
|
|
1064 "' ignored");
|
|
1065 return;
|
|
1066 }
|
|
1067 compatibilityVersion = encodeVersion(cVersion);
|
|
1068 }
|
|
1069 }
|
|
1070
|
|
1071 void DylibFile::handleLDInstallNameSymbol(StringRef name,
|
|
1072 StringRef originalName) {
|
|
1073 // originalName: $ld$ install_name $ os<version> $ install_name
|
|
1074 StringRef condition, installName;
|
|
1075 std::tie(condition, installName) = name.split('$');
|
|
1076 VersionTuple version;
|
|
1077 if (!condition.consume_front("os") || version.tryParse(condition))
|
|
1078 warn("failed to parse os version, symbol '" + originalName + "' ignored");
|
|
1079 else if (version == config->platformInfo.minimum)
|
|
1080 this->installName = saver.save(installName);
|
|
1081 }
|
|
1082
|
|
1083 ArchiveFile::ArchiveFile(std::unique_ptr<object::Archive> &&f)
|
173
|
1084 : InputFile(ArchiveKind, f->getMemoryBufferRef()), file(std::move(f)) {
|
|
1085 for (const object::Archive::Symbol &sym : file->symbols())
|
|
1086 symtab->addLazy(sym.getName(), this, sym);
|
|
1087 }
|
|
1088
|
|
1089 void ArchiveFile::fetch(const object::Archive::Symbol &sym) {
|
|
1090 object::Archive::Child c =
|
|
1091 CHECK(sym.getMember(), toString(this) +
|
|
1092 ": could not get the member for symbol " +
|
207
|
1093 toMachOString(sym));
|
173
|
1094
|
|
1095 if (!seen.insert(c.getChildOffset()).second)
|
|
1096 return;
|
|
1097
|
|
1098 MemoryBufferRef mb =
|
|
1099 CHECK(c.getMemoryBufferRef(),
|
|
1100 toString(this) +
|
|
1101 ": could not get the buffer for the member defining symbol " +
|
207
|
1102 toMachOString(sym));
|
|
1103
|
|
1104 if (tar && c.getParent()->isThin())
|
|
1105 tar->append(relativeToRoot(CHECK(c.getFullName(), this)), mb.getBuffer());
|
|
1106
|
|
1107 uint32_t modTime = toTimeT(
|
|
1108 CHECK(c.getLastModified(), toString(this) +
|
|
1109 ": could not get the modification time "
|
|
1110 "for the member defining symbol " +
|
|
1111 toMachOString(sym)));
|
|
1112
|
|
1113 // `sym` is owned by a LazySym, which will be replace<>()d by make<ObjFile>
|
|
1114 // and become invalid after that call. Copy it to the stack so we can refer
|
|
1115 // to it later.
|
|
1116 const object::Archive::Symbol symCopy = sym;
|
|
1117
|
|
1118 if (Optional<InputFile *> file =
|
|
1119 loadArchiveMember(mb, modTime, getName(), /*objCOnly=*/false)) {
|
|
1120 inputFiles.insert(*file);
|
|
1121 // ld64 doesn't demangle sym here even with -demangle.
|
|
1122 // Match that: intentionally don't call toMachOString().
|
|
1123 printArchiveMemberLoad(symCopy.getName(), *file);
|
|
1124 }
|
173
|
1125 }
|
|
1126
|
207
|
1127 static macho::Symbol *createBitcodeSymbol(const lto::InputFile::Symbol &objSym,
|
|
1128 BitcodeFile &file) {
|
|
1129 StringRef name = saver.save(objSym.getName());
|
|
1130
|
|
1131 // TODO: support weak references
|
|
1132 if (objSym.isUndefined())
|
|
1133 return symtab->addUndefined(name, &file, /*isWeakRef=*/false);
|
|
1134
|
|
1135 assert(!objSym.isCommon() && "TODO: support common symbols in LTO");
|
|
1136
|
|
1137 // TODO: Write a test demonstrating why computing isPrivateExtern before
|
|
1138 // LTO compilation is important.
|
|
1139 bool isPrivateExtern = false;
|
|
1140 switch (objSym.getVisibility()) {
|
|
1141 case GlobalValue::HiddenVisibility:
|
|
1142 isPrivateExtern = true;
|
|
1143 break;
|
|
1144 case GlobalValue::ProtectedVisibility:
|
|
1145 error(name + " has protected visibility, which is not supported by Mach-O");
|
|
1146 break;
|
|
1147 case GlobalValue::DefaultVisibility:
|
|
1148 break;
|
|
1149 }
|
|
1150
|
|
1151 return symtab->addDefined(name, &file, /*isec=*/nullptr, /*value=*/0,
|
|
1152 /*size=*/0, objSym.isWeak(), isPrivateExtern,
|
|
1153 /*isThumb=*/false,
|
|
1154 /*isReferencedDynamically=*/false,
|
|
1155 /*noDeadStrip=*/false);
|
173
|
1156 }
|
207
|
1157
|
|
1158 BitcodeFile::BitcodeFile(MemoryBufferRef mbref)
|
|
1159 : InputFile(BitcodeKind, mbref) {
|
|
1160 obj = check(lto::InputFile::create(mbref));
|
|
1161
|
|
1162 // Convert LTO Symbols to LLD Symbols in order to perform resolution. The
|
|
1163 // "winning" symbol will then be marked as Prevailing at LTO compilation
|
|
1164 // time.
|
|
1165 for (const lto::InputFile::Symbol &objSym : obj->symbols())
|
|
1166 symbols.push_back(createBitcodeSymbol(objSym, *this));
|
|
1167 }
|
|
1168
|
|
1169 template void ObjFile::parse<LP64>();
|