diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -1472,14 +1472,15 @@ gatherInputSections(); - if (config->deadStrip) - markLive(); - // ICF assumes that all literals have been folded already, so we must run // foldIdenticalLiterals before foldIdenticalSections. foldIdenticalLiterals(); if (config->icfLevel != ICFLevel::none) foldIdenticalSections(); + registerCompactUnwindSections(); + + if (config->deadStrip) + markLive(); // Write to an output file. if (target->wordSize == 8) diff --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h --- a/lld/MachO/InputFiles.h +++ b/lld/MachO/InputFiles.h @@ -105,6 +105,7 @@ const uint32_t modTime; std::vector debugSections; ArrayRef dataInCodeEntries; + void registerCompactUnwind(); private: template void parse(); @@ -120,7 +121,9 @@ SubsectionMap &); void parseDebugInfo(); void parseDataInCode(); - void registerCompactUnwind(); + + // FIXME: not thread-safe? + bool doneCUReg = false; }; // command-line -sectcreate file @@ -212,6 +215,8 @@ extern llvm::SetVector inputFiles; +void registerCompactUnwindSections(); + llvm::Optional readFile(StringRef path); namespace detail { diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp --- a/lld/MachO/InputFiles.cpp +++ b/lld/MachO/InputFiles.cpp @@ -65,6 +65,7 @@ #include "llvm/LTO/LTO.h" #include "llvm/Support/Endian.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Parallel.h" #include "llvm/Support/Path.h" #include "llvm/Support/TarWriter.h" #include "llvm/TextAPI/Architecture.h" @@ -93,6 +94,7 @@ } SetVector macho::inputFiles; + std::unique_ptr macho::tar; int InputFile::idCount = 0; @@ -173,6 +175,14 @@ return true; } +void macho::registerCompactUnwindSections() { + parallelForEach(inputFiles, [](InputFile *file) { + if (ObjFile *objFile = dyn_cast_or_null(file)) { + objFile->registerCompactUnwind(); + } + }); +} + // Open a given file path and return it as a memory-mapped file. Optional macho::readFile(StringRef path) { ErrorOr> mbOrErr = MemoryBuffer::getFile(path); @@ -832,7 +842,6 @@ parseDebugInfo(); if (config->emitDataInCodeInfo) parseDataInCode(); - registerCompactUnwind(); } void ObjFile::parseDebugInfo() { @@ -875,6 +884,7 @@ // Create pointers from symbols to their associated compact unwind entries. void ObjFile::registerCompactUnwind() { + assert(!doneCUReg); // First, locate the __compact_unwind section. SubsectionMap *cuSubsecMap = nullptr; for (SubsectionMap &map : subsections) { @@ -914,12 +924,14 @@ // The relocation should point at the exact address of a symbol (with no // addend). if (it == referentIsec->symbols.end() || (*it)->value != add) { - assert(referentIsec->wasCoalesced); + assert(config->icfLevel == ICFLevel::none || + referentIsec->wasCoalesced); continue; } (*it)->compactUnwind = isec; } } + doneCUReg = true; } // The path can point to either a dylib or a .tbd file. diff --git a/lld/test/MachO/weak-sym.s b/lld/test/MachO/weak-sym.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/weak-sym.s @@ -0,0 +1,43 @@ +# REQUIRES: x86 +# RUN: rm -rf %t; split-file %s %t + +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-iossimulator %t/lib_def.s -o %t/lib_def.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-iossimulator %t/lib_weak.s -o %t/lib_weak.o +# RUN: %lld -dylib -lSystem -dynamic -platform_version ios-simulator 13.0.0 14.5 %t/lib_def.o %t/lib_weak.o -o %t/out.dylib + +#--- lib_def.s +.section __TEXT,__text,regular,pure_instructions + +.globl _uprv_getICUData_core + +_uprv_getICUData_core: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset %rbp, -16 + movq %rsp, %rbp + .cfi_def_cfa_register %rbp + popq %rbp + retq + .cfi_endproc + +.subsections_via_symbols + +#--- lib_weak.s +.section __TEXT,__text,regular,pure_instructions + +.globl _uprv_getICUData_core +.weak_definition _uprv_getICUData_core + +_uprv_getICUData_core: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset %rbp, -16 + movq %rsp, %rbp + .cfi_def_cfa_register %rbp + popq %rbp + retq + .cfi_endproc + +.subsections_via_symbols