Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -2242,8 +2242,6 @@ EHdr->e_ehsize = sizeof(Elf_Ehdr); EHdr->e_phnum = Phdrs.size(); EHdr->e_shentsize = sizeof(Elf_Shdr); - EHdr->e_shnum = OutputSections.size() + 1; - EHdr->e_shstrndx = InX::ShStrTab->getParent()->SectionIndex; if (!Config->Relocatable) { EHdr->e_phoff = sizeof(Elf_Ehdr); @@ -2264,8 +2262,28 @@ ++HBuf; } - // Write the section header table. Note that the first table entry is null. + // Write the section header table. + // + // When ELF object has SHN_LORESERVE or more of sections, it's header's + // e_shnum field should be zero and sh_size of the section header at index 0 + // is used to store the value. auto *SHdrs = reinterpret_cast(Buf + EHdr->e_shoff); + if (OutputSections.size() + 1 >= SHN_LORESERVE) + SHdrs->sh_size = OutputSections.size() + 1; + else + EHdr->e_shnum = OutputSections.size() + 1; + + // If section name string table section index is greater than or equal to + // SHN_LORESERVE, then e_shstrndx field should have the value of SHN_XINDEX + // and sh_link of the section header at index 0 used to store the value. + uint32_t StrTabIndex = InX::ShStrTab->getParent()->SectionIndex; + if (StrTabIndex >= SHN_LORESERVE) { + SHdrs->sh_link = StrTabIndex; + EHdr->e_shstrndx = SHN_XINDEX; + } else { + EHdr->e_shstrndx = StrTabIndex; + } + for (OutputSection *Sec : OutputSections) Sec->writeHeaderTo(++SHdrs); } Index: test/ELF/relocatable-many-sections.s =================================================================== --- test/ELF/relocatable-many-sections.s +++ test/ELF/relocatable-many-sections.s @@ -0,0 +1,85 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o %t.o +# RUN: ld.lld -r %t.o -o %t +# RUN: llvm-readobj -file-headers %t | FileCheck %s + +## Check we are able to emit a valid ELF header when +## sections amount is greater than SHN_LORESERVE. +# CHECK: ElfHeader { +# CHECK: SectionHeaderCount: 0 (65541) +# CHECK-NEXT: StringTableSectionIndex: 65535 (65539) + +.macro gen_sections4 x + .section a\x + .section b\x + .section c\x + .section d\x +.endm + +.macro gen_sections8 x + gen_sections4 a\x + gen_sections4 b\x +.endm + +.macro gen_sections16 x + gen_sections8 a\x + gen_sections8 b\x +.endm + +.macro gen_sections32 x + gen_sections16 a\x + gen_sections16 b\x +.endm + +.macro gen_sections64 x + gen_sections32 a\x + gen_sections32 b\x +.endm + +.macro gen_sections128 x + gen_sections64 a\x + gen_sections64 b\x +.endm + +.macro gen_sections256 x + gen_sections128 a\x + gen_sections128 b\x +.endm + +.macro gen_sections512 x + gen_sections256 a\x + gen_sections256 b\x +.endm + +.macro gen_sections1024 x + gen_sections512 a\x + gen_sections512 b\x +.endm + +.macro gen_sections2048 x + gen_sections1024 a\x + gen_sections1024 b\x +.endm + +.macro gen_sections4096 x + gen_sections2048 a\x + gen_sections2048 b\x +.endm + +.macro gen_sections8192 x + gen_sections4096 a\x + gen_sections4096 b\x +.endm + +.macro gen_sections16384 x + gen_sections8192 a\x + gen_sections8192 b\x +.endm + +gen_sections16384 a +gen_sections16384 b +gen_sections16384 c +gen_sections16384 d + +.global _start +_start: