Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1125,7 +1125,7 @@ // Add the first PT_LOAD segment for regular output sections. uintX_t Flags = computeFlags(PF_R); Phdr *Load = AddHdr(PT_LOAD, Flags); - if (!ScriptConfig->HasSections) { + if (!Config->OFormatBinary && !ScriptConfig->HasSections) { Load->add(Out::ElfHeader); Load->add(Out::ProgramHeaders); } @@ -1234,8 +1234,9 @@ // The first section of each PT_LOAD and the first section after PT_GNU_RELRO // have to be page aligned so that the dynamic linker can set the permissions. template void Writer::fixSectionAlignments() { + // Load can be empty if we produce binary output. for (const Phdr &P : Phdrs) - if (P.H.p_type == PT_LOAD) + if (P.H.p_type == PT_LOAD && P.First) P.First->PageAlign = true; for (const Phdr &P : Phdrs) { Index: test/ELF/oformat-binary-ttext.s =================================================================== --- test/ELF/oformat-binary-ttext.s +++ test/ELF/oformat-binary-ttext.s @@ -0,0 +1,18 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t + +# RUN: ld.lld -N -Ttext 0x100 -o %t.out %t --oformat binary +# RUN: od -t x1 -v %t.out | FileCheck %s --check-prefix=BIN + +# BIN: 0000000 90 00 00 00 00 00 00 00 +# BIN-NEXT: 0000010 +# BIN-NOT: 0000020 + +## The same but without OMAGIC. +# RUN: ld.lld -Ttext 0x100 -o %t.out %t --oformat binary +# RUN: od -t x1 -v %t.out | FileCheck %s --check-prefix=BIN + +.text +.globl _start +_start: + nop