Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1124,8 +1124,11 @@ // 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) { + + // Do not create PT_LOAD segment for binary output, it does not + // have file or program headers. + Phdr *Load = Config->OFormatBinary ? nullptr : AddHdr(PT_LOAD, Flags); + if (!ScriptConfig->HasSections && !Config->OFormatBinary) { Load->add(Out::ElfHeader); Load->add(Out::ProgramHeaders); } @@ -1152,7 +1155,7 @@ // different flags or is loaded at a discontiguous address using AT linker // script command. uintX_t NewFlags = computeFlags(Sec->getPhdrFlags()); - if (Script::X->hasLMA(Sec->getName()) || Flags != NewFlags) { + if (!Load || Script::X->hasLMA(Sec->getName()) || Flags != NewFlags) { Load = AddHdr(PT_LOAD, NewFlags); Flags = NewFlags; } 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