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"
46 #include "Driver.h"
47 #include "ExportTrie.h"
48 #include "InputSection.h"
49 #include "MachOStructs.h"
50 #include "ObjC.h"
51 #include "OutputSection.h"
52 #include "OutputSegment.h"
53 #include "SymbolTable.h"
54 #include "Symbols.h"
55 #include "Target.h"
56 
57 #include "lld/Common/ErrorHandler.h"
58 #include "lld/Common/Memory.h"
59 #include "lld/Common/Reproduce.h"
60 #include "llvm/ADT/iterator.h"
61 #include "llvm/BinaryFormat/MachO.h"
62 #include "llvm/LTO/LTO.h"
63 #include "llvm/Support/Endian.h"
64 #include "llvm/Support/MemoryBuffer.h"
65 #include "llvm/Support/Path.h"
66 #include "llvm/Support/TarWriter.h"
67 
68 using namespace llvm;
69 using namespace llvm::MachO;
70 using namespace llvm::support::endian;
71 using namespace llvm::sys;
72 using namespace lld;
73 using namespace lld::macho;
74 
75 std::vector<InputFile *> macho::inputFiles;
76 std::unique_ptr<TarWriter> macho::tar;
77 
78 // Open a given file path and return it as a memory-mapped file.
79 Optional<MemoryBufferRef> macho::readFile(StringRef path) {
80   // Open a file.
81   auto mbOrErr = MemoryBuffer::getFile(path);
82   if (auto ec = mbOrErr.getError()) {
83     error("cannot open " + path + ": " + ec.message());
84     return None;
85   }
86 
87   std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
88   MemoryBufferRef mbref = mb->getMemBufferRef();
89   make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take mb ownership
90 
91   // If this is a regular non-fat file, return it.
92   const char *buf = mbref.getBufferStart();
93   auto *hdr = reinterpret_cast<const MachO::fat_header *>(buf);
94   if (read32be(&hdr->magic) != MachO::FAT_MAGIC) {
95     if (tar)
96       tar->append(relativeToRoot(path), mbref.getBuffer());
97     return mbref;
98   }
99 
100   // Object files and archive files may be fat files, which contains
101   // multiple real files for different CPU ISAs. Here, we search for a
102   // file that matches with the current link target and returns it as
103   // a MemoryBufferRef.
104   auto *arch = reinterpret_cast<const MachO::fat_arch *>(buf + sizeof(*hdr));
105 
106   for (uint32_t i = 0, n = read32be(&hdr->nfat_arch); i < n; ++i) {
107     if (reinterpret_cast<const char *>(arch + i + 1) >
108         buf + mbref.getBufferSize()) {
109       error(path + ": fat_arch struct extends beyond end of file");
110       return None;
111     }
112 
113     if (read32be(&arch[i].cputype) != target->cpuType ||
114         read32be(&arch[i].cpusubtype) != target->cpuSubtype)
115       continue;
116 
117     uint32_t offset = read32be(&arch[i].offset);
118     uint32_t size = read32be(&arch[i].size);
119     if (offset + size > mbref.getBufferSize())
120       error(path + ": slice extends beyond end of file");
121     if (tar)
122       tar->append(relativeToRoot(path), mbref.getBuffer());
123     return MemoryBufferRef(StringRef(buf + offset, size), path.copy(bAlloc));
124   }
125 
126   error("unable to find matching architecture in " + path);
127   return None;
128 }
129 
130 const load_command *macho::findCommand(const mach_header_64 *hdr,
131                                        uint32_t type) {
132   const uint8_t *p =
133       reinterpret_cast<const uint8_t *>(hdr) + sizeof(mach_header_64);
134 
135   for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {
136     auto *cmd = reinterpret_cast<const load_command *>(p);
137     if (cmd->cmd == type)
138       return cmd;
139     p += cmd->cmdsize;
140   }
141   return nullptr;
142 }
143 
144 void InputFile::parseSections(ArrayRef<section_64> sections) {
145   subsections.reserve(sections.size());
146   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
147 
148   for (const section_64 &sec : sections) {
149     InputSection *isec = make<InputSection>();
150     isec->file = this;
151     isec->name =
152         StringRef(sec.sectname, strnlen(sec.sectname, sizeof(sec.sectname)));
153     isec->segname =
154         StringRef(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
155     isec->data = {isZeroFill(sec.flags) ? nullptr : buf + sec.offset,
156                   static_cast<size_t>(sec.size)};
157     if (sec.align >= 32)
158       error("alignment " + std::to_string(sec.align) + " of section " +
159             isec->name + " is too large");
160     else
161       isec->align = 1 << sec.align;
162     isec->flags = sec.flags;
163     subsections.push_back({{0, isec}});
164   }
165 }
166 
167 // Find the subsection corresponding to the greatest section offset that is <=
168 // that of the given offset.
169 //
170 // offset: an offset relative to the start of the original InputSection (before
171 // any subsection splitting has occurred). It will be updated to represent the
172 // same location as an offset relative to the start of the containing
173 // subsection.
174 static InputSection *findContainingSubsection(SubsectionMap &map,
175                                               uint32_t *offset) {
176   auto it = std::prev(map.upper_bound(*offset));
177   *offset -= it->first;
178   return it->second;
179 }
180 
181 void InputFile::parseRelocations(const section_64 &sec,
182                                  SubsectionMap &subsecMap) {
183   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
184   ArrayRef<any_relocation_info> anyRelInfos(
185       reinterpret_cast<const any_relocation_info *>(buf + sec.reloff),
186       sec.nreloc);
187 
188   for (const any_relocation_info &anyRelInfo : anyRelInfos) {
189     if (anyRelInfo.r_word0 & R_SCATTERED)
190       fatal("TODO: Scattered relocations not supported");
191 
192     auto relInfo = reinterpret_cast<const relocation_info &>(anyRelInfo);
193 
194     Reloc r;
195     r.type = relInfo.r_type;
196     r.pcrel = relInfo.r_pcrel;
197     r.length = relInfo.r_length;
198     uint64_t rawAddend = target->getImplicitAddend(mb, sec, relInfo);
199 
200     if (relInfo.r_extern) {
201       r.referent = symbols[relInfo.r_symbolnum];
202       r.addend = rawAddend;
203     } else {
204       if (relInfo.r_symbolnum == 0 || relInfo.r_symbolnum > subsections.size())
205         fatal("invalid section index in relocation for offset " +
206               std::to_string(r.offset) + " in section " + sec.sectname +
207               " of " + getName());
208 
209       SubsectionMap &referentSubsecMap = subsections[relInfo.r_symbolnum - 1];
210       const section_64 &referentSec = sectionHeaders[relInfo.r_symbolnum - 1];
211       uint32_t referentOffset;
212       if (relInfo.r_pcrel) {
213         // The implicit addend for pcrel section relocations is the pcrel offset
214         // in terms of the addresses in the input file. Here we adjust it so
215         // that it describes the offset from the start of the referent section.
216         // TODO: The offset of 4 is probably not right for ARM64, nor for
217         //       relocations with r_length != 2.
218         referentOffset =
219             sec.addr + relInfo.r_address + 4 + rawAddend - referentSec.addr;
220       } else {
221         // The addend for a non-pcrel relocation is its absolute address.
222         referentOffset = rawAddend - referentSec.addr;
223       }
224       r.referent = findContainingSubsection(referentSubsecMap, &referentOffset);
225       r.addend = referentOffset;
226     }
227 
228     r.offset = relInfo.r_address;
229     InputSection *subsec = findContainingSubsection(subsecMap, &r.offset);
230     subsec->relocs.push_back(r);
231   }
232 }
233 
234 static macho::Symbol *createDefined(const structs::nlist_64 &sym,
235                                     StringRef name, InputSection *isec,
236                                     uint32_t value) {
237   if (sym.n_type & N_EXT)
238     // Global defined symbol
239     return symtab->addDefined(name, isec, value, sym.n_desc & N_WEAK_DEF);
240   // Local defined symbol
241   return make<Defined>(name, isec, value, sym.n_desc & N_WEAK_DEF,
242                        /*isExternal=*/false);
243 }
244 
245 // Absolute symbols are defined symbols that do not have an associated
246 // InputSection. They cannot be weak.
247 static macho::Symbol *createAbsolute(const structs::nlist_64 &sym,
248                                      StringRef name) {
249   if (sym.n_type & N_EXT)
250     return symtab->addDefined(name, nullptr, sym.n_value, /*isWeakDef=*/false);
251   return make<Defined>(name, nullptr, sym.n_value, /*isWeakDef=*/false,
252                        /*isExternal=*/false);
253 }
254 
255 macho::Symbol *InputFile::parseNonSectionSymbol(const structs::nlist_64 &sym,
256                                                 StringRef name) {
257   uint8_t type = sym.n_type & N_TYPE;
258   switch (type) {
259   case N_UNDF:
260     return sym.n_value == 0
261                ? symtab->addUndefined(name)
262                : symtab->addCommon(name, this, sym.n_value,
263                                    1 << GET_COMM_ALIGN(sym.n_desc));
264   case N_ABS:
265     return createAbsolute(sym, name);
266   case N_PBUD:
267   case N_INDR:
268     error("TODO: support symbols of type " + std::to_string(type));
269     return nullptr;
270   case N_SECT:
271     llvm_unreachable(
272         "N_SECT symbols should not be passed to parseNonSectionSymbol");
273   default:
274     llvm_unreachable("invalid symbol type");
275   }
276 }
277 
278 void InputFile::parseSymbols(ArrayRef<structs::nlist_64> nList,
279                              const char *strtab, bool subsectionsViaSymbols) {
280   // resize(), not reserve(), because we are going to create N_ALT_ENTRY symbols
281   // out-of-sequence.
282   symbols.resize(nList.size());
283   std::vector<size_t> altEntrySymIdxs;
284 
285   for (size_t i = 0, n = nList.size(); i < n; ++i) {
286     const structs::nlist_64 &sym = nList[i];
287     StringRef name = strtab + sym.n_strx;
288 
289     if ((sym.n_type & N_TYPE) != N_SECT) {
290       symbols[i] = parseNonSectionSymbol(sym, name);
291       continue;
292     }
293 
294     const section_64 &sec = sectionHeaders[sym.n_sect - 1];
295     SubsectionMap &subsecMap = subsections[sym.n_sect - 1];
296     uint64_t offset = sym.n_value - sec.addr;
297 
298     // If the input file does not use subsections-via-symbols, all symbols can
299     // use the same subsection. Otherwise, we must split the sections along
300     // symbol boundaries.
301     if (!subsectionsViaSymbols) {
302       symbols[i] = createDefined(sym, name, subsecMap[0], offset);
303       continue;
304     }
305 
306     // nList entries aren't necessarily arranged in address order. Therefore,
307     // we can't create alt-entry symbols at this point because a later symbol
308     // may split its section, which may affect which subsection the alt-entry
309     // symbol is assigned to. So we need to handle them in a second pass below.
310     if (sym.n_desc & N_ALT_ENTRY) {
311       altEntrySymIdxs.push_back(i);
312       continue;
313     }
314 
315     // Find the subsection corresponding to the greatest section offset that is
316     // <= that of the current symbol. The subsection that we find either needs
317     // to be used directly or split in two.
318     uint32_t firstSize = offset;
319     InputSection *firstIsec = findContainingSubsection(subsecMap, &firstSize);
320 
321     if (firstSize == 0) {
322       // Alias of an existing symbol, or the first symbol in the section. These
323       // are handled by reusing the existing section.
324       symbols[i] = createDefined(sym, name, firstIsec, 0);
325       continue;
326     }
327 
328     // We saw a symbol definition at a new offset. Split the section into two
329     // subsections. The new symbol uses the second subsection.
330     auto *secondIsec = make<InputSection>(*firstIsec);
331     secondIsec->data = firstIsec->data.slice(firstSize);
332     firstIsec->data = firstIsec->data.slice(0, firstSize);
333     // TODO: ld64 appears to preserve the original alignment as well as each
334     // subsection's offset from the last aligned address. We should consider
335     // emulating that behavior.
336     secondIsec->align = MinAlign(firstIsec->align, offset);
337 
338     subsecMap[offset] = secondIsec;
339     // By construction, the symbol will be at offset zero in the new section.
340     symbols[i] = createDefined(sym, name, secondIsec, 0);
341   }
342 
343   for (size_t idx : altEntrySymIdxs) {
344     const structs::nlist_64 &sym = nList[idx];
345     StringRef name = strtab + sym.n_strx;
346     SubsectionMap &subsecMap = subsections[sym.n_sect - 1];
347     uint32_t off = sym.n_value - sectionHeaders[sym.n_sect - 1].addr;
348     InputSection *subsec = findContainingSubsection(subsecMap, &off);
349     symbols[idx] = createDefined(sym, name, subsec, off);
350   }
351 }
352 
353 OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName,
354                        StringRef sectName)
355     : InputFile(OpaqueKind, mb) {
356   InputSection *isec = make<InputSection>();
357   isec->file = this;
358   isec->name = sectName.take_front(16);
359   isec->segname = segName.take_front(16);
360   const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
361   isec->data = {buf, mb.getBufferSize()};
362   subsections.push_back({{0, isec}});
363 }
364 
365 ObjFile::ObjFile(MemoryBufferRef mb) : InputFile(ObjKind, mb) {
366   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
367   auto *hdr = reinterpret_cast<const mach_header_64 *>(mb.getBufferStart());
368 
369   if (const load_command *cmd = findCommand(hdr, LC_SEGMENT_64)) {
370     auto *c = reinterpret_cast<const segment_command_64 *>(cmd);
371     sectionHeaders = ArrayRef<section_64>{
372         reinterpret_cast<const section_64 *>(c + 1), c->nsects};
373     parseSections(sectionHeaders);
374   }
375 
376   // TODO: Error on missing LC_SYMTAB?
377   if (const load_command *cmd = findCommand(hdr, LC_SYMTAB)) {
378     auto *c = reinterpret_cast<const symtab_command *>(cmd);
379     ArrayRef<structs::nlist_64> nList(
380         reinterpret_cast<const structs::nlist_64 *>(buf + c->symoff), c->nsyms);
381     const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;
382     bool subsectionsViaSymbols = hdr->flags & MH_SUBSECTIONS_VIA_SYMBOLS;
383     parseSymbols(nList, strtab, subsectionsViaSymbols);
384   }
385 
386   // The relocations may refer to the symbols, so we parse them after we have
387   // parsed all the symbols.
388   for (size_t i = 0, n = subsections.size(); i < n; ++i)
389     parseRelocations(sectionHeaders[i], subsections[i]);
390 }
391 
392 // The path can point to either a dylib or a .tbd file.
393 static Optional<DylibFile *> loadDylib(StringRef path, DylibFile *umbrella) {
394   Optional<MemoryBufferRef> mbref = readFile(path);
395   if (!mbref) {
396     error("could not read dylib file at " + path);
397     return {};
398   }
399 
400   file_magic magic = identify_magic(mbref->getBuffer());
401   if (magic == file_magic::tapi_file)
402     return makeDylibFromTAPI(*mbref, umbrella);
403   assert(magic == file_magic::macho_dynamically_linked_shared_lib);
404   return make<DylibFile>(*mbref, umbrella);
405 }
406 
407 // TBD files are parsed into a series of TAPI documents (InterfaceFiles), with
408 // the first document storing child pointers to the rest of them. When we are
409 // processing a given TBD file, we store that top-level document here. When
410 // processing re-exports, we search its children for potentially matching
411 // documents in the same TBD file. Note that the children themselves don't
412 // point to further documents, i.e. this is a two-level tree.
413 //
414 // ld64 allows a TAPI re-export to reference documents nested within other TBD
415 // files, but that seems like a strange design, so this is an intentional
416 // deviation.
417 const InterfaceFile *currentTopLevelTapi = nullptr;
418 
419 // Re-exports can either refer to on-disk files, or to documents within .tbd
420 // files.
421 static Optional<DylibFile *> loadReexport(StringRef path, DylibFile *umbrella) {
422   if (path::is_absolute(path, path::Style::posix))
423     for (StringRef root : config->systemLibraryRoots)
424       if (Optional<std::string> dylibPath =
425               resolveDylibPath((root + path).str()))
426         return loadDylib(*dylibPath, umbrella);
427 
428   // TODO: Expand @loader_path, @executable_path etc
429 
430   if (currentTopLevelTapi) {
431     for (InterfaceFile &child :
432          make_pointee_range(currentTopLevelTapi->documents())) {
433       if (path == child.getInstallName())
434         return make<DylibFile>(child, umbrella);
435       assert(child.documents().empty());
436     }
437   }
438 
439   if (Optional<std::string> dylibPath = resolveDylibPath(path))
440     return loadDylib(*dylibPath, umbrella);
441 
442   error("unable to locate re-export with install name " + path);
443   return {};
444 }
445 
446 DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella)
447     : InputFile(DylibKind, mb) {
448   if (umbrella == nullptr)
449     umbrella = this;
450 
451   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
452   auto *hdr = reinterpret_cast<const mach_header_64 *>(mb.getBufferStart());
453 
454   // Initialize dylibName.
455   if (const load_command *cmd = findCommand(hdr, LC_ID_DYLIB)) {
456     auto *c = reinterpret_cast<const dylib_command *>(cmd);
457     dylibName = reinterpret_cast<const char *>(cmd) + read32le(&c->dylib.name);
458   } else {
459     error("dylib " + getName() + " missing LC_ID_DYLIB load command");
460     return;
461   }
462 
463   // Initialize symbols.
464   // TODO: if a re-exported dylib is public (lives in /usr/lib or
465   // /System/Library/Frameworks), we should bind to its symbols directly
466   // instead of the re-exporting umbrella library.
467   if (const load_command *cmd = findCommand(hdr, LC_DYLD_INFO_ONLY)) {
468     auto *c = reinterpret_cast<const dyld_info_command *>(cmd);
469     parseTrie(buf + c->export_off, c->export_size,
470               [&](const Twine &name, uint64_t flags) {
471                 bool isWeakDef = flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;
472                 bool isTlv = flags & EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL;
473                 symbols.push_back(symtab->addDylib(saver.save(name), umbrella,
474                                                    isWeakDef, isTlv));
475               });
476   } else {
477     error("LC_DYLD_INFO_ONLY not found in " + getName());
478     return;
479   }
480 
481   if (hdr->flags & MH_NO_REEXPORTED_DYLIBS)
482     return;
483 
484   const uint8_t *p =
485       reinterpret_cast<const uint8_t *>(hdr) + sizeof(mach_header_64);
486   for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {
487     auto *cmd = reinterpret_cast<const load_command *>(p);
488     p += cmd->cmdsize;
489     if (cmd->cmd != LC_REEXPORT_DYLIB)
490       continue;
491 
492     auto *c = reinterpret_cast<const dylib_command *>(cmd);
493     StringRef reexportPath =
494         reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);
495     if (Optional<DylibFile *> reexport = loadReexport(reexportPath, umbrella))
496       reexported.push_back(*reexport);
497   }
498 }
499 
500 DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella)
501     : InputFile(DylibKind, interface) {
502   if (umbrella == nullptr)
503     umbrella = this;
504 
505   dylibName = saver.save(interface.getInstallName());
506   auto addSymbol = [&](const Twine &name) -> void {
507     symbols.push_back(symtab->addDylib(saver.save(name), umbrella,
508                                        /*isWeakDef=*/false,
509                                        /*isTlv=*/false));
510   };
511   // TODO(compnerd) filter out symbols based on the target platform
512   // TODO: handle weak defs, thread locals
513   for (const auto symbol : interface.symbols()) {
514     if (!symbol->getArchitectures().has(config->arch))
515       continue;
516 
517     switch (symbol->getKind()) {
518     case SymbolKind::GlobalSymbol:
519       addSymbol(symbol->getName());
520       break;
521     case SymbolKind::ObjectiveCClass:
522       // XXX ld64 only creates these symbols when -ObjC is passed in. We may
523       // want to emulate that.
524       addSymbol(objc::klass + symbol->getName());
525       addSymbol(objc::metaclass + symbol->getName());
526       break;
527     case SymbolKind::ObjectiveCClassEHType:
528       addSymbol(objc::ehtype + symbol->getName());
529       break;
530     case SymbolKind::ObjectiveCInstanceVariable:
531       addSymbol(objc::ivar + symbol->getName());
532       break;
533     }
534   }
535 
536   bool isTopLevelTapi = false;
537   if (currentTopLevelTapi == nullptr) {
538     currentTopLevelTapi = &interface;
539     isTopLevelTapi = true;
540   }
541 
542   for (InterfaceFileRef intfRef : interface.reexportedLibraries())
543     if (Optional<DylibFile *> reexport =
544             loadReexport(intfRef.getInstallName(), umbrella))
545       reexported.push_back(*reexport);
546 
547   if (isTopLevelTapi)
548     currentTopLevelTapi = nullptr;
549 }
550 
551 ArchiveFile::ArchiveFile(std::unique_ptr<llvm::object::Archive> &&f)
552     : InputFile(ArchiveKind, f->getMemoryBufferRef()), file(std::move(f)) {
553   for (const object::Archive::Symbol &sym : file->symbols())
554     symtab->addLazy(sym.getName(), this, sym);
555 }
556 
557 void ArchiveFile::fetch(const object::Archive::Symbol &sym) {
558   object::Archive::Child c =
559       CHECK(sym.getMember(), toString(this) +
560                                  ": could not get the member for symbol " +
561                                  sym.getName());
562 
563   if (!seen.insert(c.getChildOffset()).second)
564     return;
565 
566   MemoryBufferRef mb =
567       CHECK(c.getMemoryBufferRef(),
568             toString(this) +
569                 ": could not get the buffer for the member defining symbol " +
570                 sym.getName());
571   auto file = make<ObjFile>(mb);
572   symbols.insert(symbols.end(), file->symbols.begin(), file->symbols.end());
573   subsections.insert(subsections.end(), file->subsections.begin(),
574                      file->subsections.end());
575 }
576 
577 BitcodeFile::BitcodeFile(MemoryBufferRef mbref)
578     : InputFile(BitcodeKind, mbref) {
579   obj = check(lto::InputFile::create(mbref));
580 }
581 
582 // Returns "<internal>" or "baz.o".
583 std::string lld::toString(const InputFile *file) {
584   return file ? std::string(file->getName()) : "<internal>";
585 }
586