diff options
-rw-r--r-- | lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp | 14 | ||||
-rw-r--r-- | lld/test/mach-o/empty-sections.yaml | 9 |
2 files changed, 17 insertions, 6 deletions
diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp index da27c7cadf9..ab7ea7e07f2 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp @@ -626,17 +626,19 @@ llvm::Error MachOFileLayout::writeSingleSegmentLoadCommand(uint8_t *&lc) { + _file.sections.size() * sizeof(typename T::section); uint8_t *next = lc + seg->cmdsize; memset(seg->segname, 0, 16); + seg->flags = 0; seg->vmaddr = 0; - seg->vmsize = _file.sections.back().address - + _file.sections.back().content.size(); seg->fileoff = _endOfLoadCommands; - seg->filesize = _sectInfo[&_file.sections.back()].fileOffset + - _file.sections.back().content.size() - - _sectInfo[&_file.sections.front()].fileOffset; seg->maxprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE; seg->initprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE; seg->nsects = _file.sections.size(); - seg->flags = 0; + if (seg->nsects) { + seg->vmsize = _file.sections.back().address + + _file.sections.back().content.size(); + seg->filesize = _sectInfo[&_file.sections.back()].fileOffset + + _file.sections.back().content.size() - + _sectInfo[&_file.sections.front()].fileOffset; + } if (_swap) swapStruct(*seg); typename T::section *sout = reinterpret_cast<typename T::section*> diff --git a/lld/test/mach-o/empty-sections.yaml b/lld/test/mach-o/empty-sections.yaml new file mode 100644 index 00000000000..ca6b150b1a4 --- /dev/null +++ b/lld/test/mach-o/empty-sections.yaml @@ -0,0 +1,9 @@ +# RUN: ld64.lld -arch x86_64 -r %s -o %t +# +# Test that writing empty mach-o sections does not segfault the linker. +# + +--- !mach-o +arch: x86_64 +file-type: MH_OBJECT +... |