Index: lld/MachO/Driver.h =================================================================== --- lld/MachO/Driver.h +++ lld/MachO/Driver.h @@ -52,7 +52,7 @@ std::string createResponseFile(const llvm::opt::InputArgList &args); // Check for both libfoo.dylib and libfoo.tbd (in that order). -llvm::Optional resolveDylibPath(llvm::StringRef path); +llvm::Optional resolveDylibPath(llvm::StringRef path); DylibFile *loadDylib(llvm::MemoryBufferRef mbref, DylibFile *umbrella = nullptr, bool isBundleLoader = false); Index: lld/MachO/Driver.cpp =================================================================== --- lld/MachO/Driver.cpp +++ lld/MachO/Driver.cpp @@ -92,7 +92,7 @@ {".tbd", ".dylib", ".a"}); } -static Optional findFramework(StringRef name) { +static Optional findFramework(StringRef name) { SmallString<260> symlink; StringRef suffix; std::tie(name, suffix) = name.split(","); @@ -108,12 +108,12 @@ // only append suffix if realpath() succeeds Twine suffixed = location + suffix; if (fs::exists(suffixed)) - return suffixed.str(); + return saver.save(suffixed.str()); } // Suffix lookup failed, fall through to the no-suffix case. } - if (Optional path = resolveDylibPath(symlink)) + if (Optional path = resolveDylibPath(symlink)) return path; } return {}; @@ -351,7 +351,7 @@ static void addFramework(StringRef name, bool isNeeded, bool isWeak, bool isReexport, bool isExplicit) { - if (Optional path = findFramework(name)) { + if (Optional path = findFramework(name)) { if (auto *dylibFile = dyn_cast_or_null( addFile(*path, /*forceLoadArchive=*/false, isExplicit))) { if (isNeeded) Index: lld/MachO/DriverUtils.cpp =================================================================== --- lld/MachO/DriverUtils.cpp +++ lld/MachO/DriverUtils.cpp @@ -183,7 +183,7 @@ depTracker->logFileNotFound(path); } -Optional macho::resolveDylibPath(StringRef dylibPath) { +Optional macho::resolveDylibPath(StringRef dylibPath) { // TODO: if a tbd and dylib are both present, we should check to make sure // they are consistent. SmallString<261> tbdPath = dylibPath; @@ -191,12 +191,12 @@ bool tbdExists = fs::exists(tbdPath); searchedDylib(tbdPath, tbdExists); if (tbdExists) - return std::string(tbdPath); + return saver.save(tbdPath.str()); bool dylibExists = fs::exists(dylibPath); searchedDylib(dylibPath, dylibExists); if (dylibExists) - return std::string(dylibPath); + return saver.save(dylibPath); return {}; } Index: lld/MachO/InputFiles.cpp =================================================================== --- lld/MachO/InputFiles.cpp +++ lld/MachO/InputFiles.cpp @@ -902,7 +902,7 @@ for (StringRef dir : config->frameworkSearchPaths) { SmallString<128> candidate = dir; path::append(candidate, frameworkName); - if (Optional dylibPath = resolveDylibPath(candidate)) + if (Optional dylibPath = resolveDylibPath(candidate)) return loadDylib(*dylibPath, umbrella); } } else if (Optional dylibPath = findPathCombination( @@ -913,7 +913,7 @@ // 2. As absolute path. if (path::is_absolute(path, path::Style::posix)) for (StringRef root : config->systemLibraryRoots) - if (Optional dylibPath = + if (Optional dylibPath = resolveDylibPath((root + path).str())) return loadDylib(*dylibPath, umbrella); @@ -943,7 +943,7 @@ path::remove_filename(newPath); } path::append(newPath, rpath, path.drop_front(strlen("@rpath/"))); - if (Optional dylibPath = resolveDylibPath(newPath)) + if (Optional dylibPath = resolveDylibPath(newPath)) return loadDylib(*dylibPath, umbrella); } } @@ -961,7 +961,7 @@ } } - if (Optional dylibPath = resolveDylibPath(path)) + if (Optional dylibPath = resolveDylibPath(path)) return loadDylib(*dylibPath, umbrella); return nullptr; Index: lld/test/MachO/Inputs/MacOSX.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation.tbd =================================================================== --- lld/test/MachO/Inputs/MacOSX.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation.tbd +++ lld/test/MachO/Inputs/MacOSX.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation.tbd @@ -7,7 +7,7 @@ compatibility-version: 150 exports: - archs: [ 'x86_64' ] - symbols: [ __CFBigNumGetInt128, ___CFConstantStringClassReference ] + symbols: [ __CFBigNumGetInt128, ___CFConstantStringClassReference, __objc_empty_cache ] objc-classes: [ NSObject ] objc-ivars: [ NSConstantArray._count ] objc-eh-types: [ NSException ] Index: lld/test/MachO/multi-loaded-archive.yaml =================================================================== --- /dev/null +++ lld/test/MachO/multi-loaded-archive.yaml @@ -0,0 +1,407 @@ +# REQUIRES: x86 +# RUN: rm -rf %t; split-file %s %t +# RUN: yaml2obj %t/objFile1.yaml > %t/objFile1.o +# RUN: yaml2obj %t/objFile2.yaml > %t/objFile2.o +# RUN: yaml2obj %t/foo.yaml > %t/foo.o +# RUN: yaml2obj %t/main.yaml > %t/main.o +# RUN: mkdir -p %t/Foo.framework/Versions/A +# RUN: llvm-ar rcs %t/Foo.framework/Versions/A/Foo %t/foo.o +# RUN: ln -sf A %t/Foo.framework/Versions/Current +# RUN: ln -sf Versions/Current/Foo %t/Foo.framework/Foo +# RUN: %lld -demangle -ObjC -syslibroot %S/Inputs/MacOSX.sdk -o %t/main.out %t/main.o %t/objFile1.o %t/objFile2.o -F%t -framework CoreFoundation + +#--- objFile1.yaml +--- !mach-o +FileHeader: + magic: 0xFEEDFACF + cputype: 0x1000007 + cpusubtype: 0x3 + filetype: 0x1 + ncmds: 2 + sizeofcmds: 280 + flags: 0x2000 + reserved: 0x0 +LoadCommands: + - cmd: LC_SEGMENT_64 + cmdsize: 152 + segname: '' + vmaddr: 0 + vmsize: 21 + fileoff: 312 + filesize: 21 + maxprot: 7 + initprot: 7 + nsects: 0 + flags: 0 + - cmd: LC_LINKER_OPTION + cmdsize: 32 + count: 2 + PayloadBytes: [ 0x2D, 0x66, 0x72, 0x61, 0x6D, 0x65, 0x77, 0x6F, + 0x72, 0x6B, 0x0, 0x66, 0x6f, 0x6f, 0x0 ] +... + +#--- objFile2.yaml +--- !mach-o +FileHeader: + magic: 0xFEEDFACF + cputype: 0x1000007 + cpusubtype: 0x3 + filetype: 0x1 + ncmds: 2 + sizeofcmds: 280 + flags: 0x2000 + reserved: 0x0 +LoadCommands: + - cmd: LC_SEGMENT_64 + cmdsize: 152 + segname: '' + vmaddr: 0 + vmsize: 21 + fileoff: 312 + filesize: 21 + maxprot: 7 + initprot: 7 + nsects: 0 + flags: 0 + - cmd: LC_LINKER_OPTION + cmdsize: 32 + count: 2 + PayloadBytes: [ 0x2D, 0x66, 0x72, 0x61, 0x6D, 0x65, 0x77, 0x6F, + 0x72, 0x6B, 0x0, 0x66, 0x6f, 0x6f, 0x0 ] +... + +#--- main.yaml +--- !mach-o +FileHeader: + magic: 0xFEEDFACF + cputype: 0x1000007 + cpusubtype: 0x3 + filetype: 0x1 + ncmds: 2 + sizeofcmds: 976 + flags: 0x2000 + reserved: 0x0 +LoadCommands: + - cmd: LC_SEGMENT_64 + cmdsize: 392 + segname: '' + vmaddr: 0 + vmsize: 112 + fileoff: 1008 + filesize: 112 + maxprot: 7 + initprot: 7 + nsects: 4 + flags: 0 + Sections: + - sectname: __text + segname: __TEXT + addr: 0x0 + size: 8 + offset: 0x3F0 + align: 0 + reloff: 0x0 + nreloc: 0 + flags: 0x80000400 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: 554889E531C05DC3 + - sectname: __objc_imageinfo + segname: __DATA + addr: 0x8 + size: 8 + offset: 0x3F8 + align: 0 + reloff: 0x0 + nreloc: 0 + flags: 0x10000000 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: '0000000040000000' + - sectname: __compact_unwind + segname: __LD + addr: 0x10 + size: 32 + offset: 0x400 + align: 3 + reloff: 0x460 + nreloc: 1 + flags: 0x2000000 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: '0000000000000000080000000000000100000000000000000000000000000000' + relocations: + - address: 0x0 + symbolnum: 1 + pcrel: false + length: 3 + extern: false + type: 0 + scattered: false + value: 0 + - sectname: __eh_frame + segname: __TEXT + addr: 0x30 + size: 64 + offset: 0x420 + align: 3 + reloff: 0x0 + nreloc: 0 + flags: 0x6800000B + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: 1400000000000000017A520001781001100C070890010000240000001C000000B0FFFFFFFFFFFFFF080000000000000000410E108602430D0600000000000000 + - cmd: LC_SYMTAB + cmdsize: 24 + symoff: 1128 + nsyms: 1 + stroff: 1144 + strsize: 8 +LinkEditData: + NameList: + - n_strx: 1 + n_type: 0x1F + n_sect: 1 + n_desc: 0 + n_value: 0 + StringTable: + - '' + - _main + - '' +... + + +#--- foo.yaml +--- !mach-o +FileHeader: + magic: 0xFEEDFACF + cputype: 0x1000007 + cpusubtype: 0x3 + filetype: 0x1 + ncmds: 2 + sizeofcmds: 680 + flags: 0x2000 + reserved: 0x0 +LoadCommands: + - cmd: LC_SEGMENT_64 + cmdsize: 552 + segname: '' + vmaddr: 0 + vmsize: 256 + fileoff: 712 + filesize: 256 + maxprot: 7 + initprot: 7 + nsects: 6 + flags: 0 + Sections: + - sectname: __text + segname: __TEXT + addr: 0x0 + size: 0 + offset: 0x2C8 + align: 0 + reloff: 0x0 + nreloc: 0 + flags: 0x80000000 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: '' + - sectname: __objc_data + segname: __DATA + addr: 0x0 + size: 80 + offset: 0x2C8 + align: 3 + reloff: 0x3C8 + nreloc: 7 + flags: 0x0 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' + relocations: + - address: 0x48 + symbolnum: 1 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - address: 0x38 + symbolnum: 4 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - address: 0x30 + symbolnum: 2 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - address: 0x28 + symbolnum: 3 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - address: 0x20 + symbolnum: 0 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - address: 0x10 + symbolnum: 4 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - address: 0x0 + symbolnum: 3 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - sectname: __objc_classname + segname: __TEXT + addr: 0x50 + size: 10 + offset: 0x318 + align: 0 + reloff: 0x0 + nreloc: 0 + flags: 0x2 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: 54657374436C61737300 + - sectname: __objc_const + segname: __DATA + addr: 0x60 + size: 144 + offset: 0x328 + align: 3 + reloff: 0x400 + nreloc: 2 + flags: 0x0 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: '030000002800000028000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' + relocations: + - address: 0x60 + symbolnum: 3 + pcrel: false + length: 3 + extern: false + type: 0 + scattered: false + value: 0 + - address: 0x18 + symbolnum: 3 + pcrel: false + length: 3 + extern: false + type: 0 + scattered: false + value: 0 + - sectname: __objc_classlist + segname: __DATA + addr: 0xF0 + size: 8 + offset: 0x3B8 + align: 3 + reloff: 0x410 + nreloc: 1 + flags: 0x10000000 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: '0000000000000000' + relocations: + - address: 0x0 + symbolnum: 2 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - sectname: __objc_imageinfo + segname: __DATA + addr: 0xF8 + size: 8 + offset: 0x3C0 + align: 0 + reloff: 0x0 + nreloc: 0 + flags: 0x10000000 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: '0000000040000000' + - cmd: LC_SYMTAB + cmdsize: 24 + symoff: 1048 + nsyms: 5 + stroff: 1128 + strsize: 136 +LinkEditData: + NameList: + - n_strx: 53 + n_type: 0xE + n_sect: 4 + n_desc: 0 + n_value: 168 + - n_strx: 81 + n_type: 0xE + n_sect: 4 + n_desc: 0 + n_value: 96 + - n_strx: 1 + n_type: 0xF + n_sect: 2 + n_desc: 0 + n_value: 0 + - n_strx: 25 + n_type: 0xF + n_sect: 2 + n_desc: 0 + n_value: 40 + - n_strx: 113 + n_type: 0x1 + n_sect: 0 + n_desc: 0 + n_value: 0 + StringTable: + - '' + - '_OBJC_CLASS_$_TestClass' + - '_OBJC_METACLASS_$_TestClass' + - '__OBJC_CLASS_RO_$_TestClass' + - '__OBJC_METACLASS_RO_$_TestClass' + - __objc_empty_cache + - '' + - '' + - '' + - '' +...