Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1114,8 +1114,17 @@ // 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 = nullptr; + if (!Config->OFormatBinary) + Load = AddHdr(PT_LOAD, Flags); + // We do not place headers when have SECTIONS command here, because it is + // possible to have no place for them. We do it later when script assign + // sections addressed. But we still need to create this load for them for case + // when first section uses AT command and requires own PT_LOAD. + if (Load && !ScriptConfig->HasSections) { Load->add(Out::ElfHeader); Load->add(Out::ProgramHeaders); } @@ -1142,7 +1151,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