Index: lib/ReaderWriter/ELF/ELFFile.h =================================================================== --- lib/ReaderWriter/ELF/ELFFile.h +++ lib/ReaderWriter/ELF/ELFFile.h @@ -201,7 +201,11 @@ /// symbol references. bool handleSectionWithNoSymbols(const Elf_Shdr *shdr, std::vector &syms) const { - return shdr && (shdr->sh_type == llvm::ELF::SHT_PROGBITS) && syms.empty(); + return shdr && + (shdr->sh_type == llvm::ELF::SHT_PROGBITS || + shdr->sh_type == llvm::ELF::SHT_INIT_ARRAY || + shdr->sh_type == llvm::ELF::SHT_FINI_ARRAY) && + syms.empty(); } /// Handle creation of atoms for .gnu.linkonce sections. Index: test/elf/init_array.test =================================================================== --- test/elf/init_array.test +++ test/elf/init_array.test @@ -10,6 +10,11 @@ # # int # main() { return (0); } +# +# Note: both STT_OBJECT and STT_SECTION for .init_array are commented in yaml +# declaration to check if lld correct adds the object's .init_array when the +# section has no symbol (some compilers may create object with this behavior, +# specially for C++ global constructors). #RUN: yaml2obj -format=elf %s -o=%t.o #RUN: lld -flavor gnu -target x86_64 %t.o -o %t -e=main @@ -90,10 +95,10 @@ Section: .text Value: 0x0000000000000020 Size: 0x0000000000000006 - - Name: init_array - Type: STT_OBJECT - Section: .init_array - Size: 0x0000000000000008 +# - Name: init_array +# Type: STT_OBJECT +# Section: .init_array +# Size: 0x0000000000000008 - Name: .text Type: STT_SECTION Section: .text @@ -103,9 +108,9 @@ - Name: .bss Type: STT_SECTION Section: .bss - - Name: .init_array - Type: STT_SECTION - Section: .init_array +# - Name: .init_array +# Type: STT_SECTION +# Section: .init_array - Name: .comment Type: STT_SECTION Section: .comment