Index: ELF/Target.h =================================================================== --- ELF/Target.h +++ ELF/Target.h @@ -41,6 +41,7 @@ bool supportsLazyRelocations() const { return LazyRelocations; } unsigned getGotHeaderEntriesNum() const { return GotHeaderEntriesNum; } unsigned getGotPltHeaderEntriesNum() const { return GotPltHeaderEntriesNum; } + unsigned getELFFlags() const { return ELFFlags; } virtual unsigned getDynReloc(unsigned Type) const { return Type; } virtual bool isTlsDynReloc(unsigned Type) const { return false; } virtual unsigned getPltRefReloc(unsigned Type) const; @@ -91,6 +92,7 @@ unsigned PltZeroEntrySize = 0; unsigned GotHeaderEntriesNum = 0; unsigned GotPltHeaderEntriesNum = 3; + unsigned ELFFlags = 0; bool LazyRelocations = false; }; Index: ELF/Target.cpp =================================================================== --- ELF/Target.cpp +++ ELF/Target.cpp @@ -1254,6 +1254,11 @@ template MipsTargetInfo::MipsTargetInfo() { PageSize = 65536; GotHeaderEntriesNum = 2; + // FIXME: In fact ELF flags depends on ELF flags of input object files + // and selected emulation. + ELFFlags = EF_MIPS_ABI_O32 | EF_MIPS_CPIC | EF_MIPS_ARCH_32R2; + if (Config->Shared) + ELFFlags |= EF_MIPS_PIC; } template Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1021,6 +1021,7 @@ EHdr->e_entry = getEntryAddr(); EHdr->e_phoff = sizeof(Elf_Ehdr); EHdr->e_shoff = SectionHeaderOff; + EHdr->e_flags = Target->getELFFlags(); EHdr->e_ehsize = sizeof(Elf_Ehdr); EHdr->e_phentsize = sizeof(Elf_Phdr); EHdr->e_phnum = Phdrs.size(); Index: test/ELF/basic-mips.s =================================================================== --- test/ELF/basic-mips.s +++ test/ELF/basic-mips.s @@ -28,7 +28,10 @@ # CHECK-NEXT: Entry: 0x20000 # CHECK-NEXT: ProgramHeaderOffset: 0x34 # CHECK-NEXT: SectionHeaderOffset: 0x20084 -# CHECK-NEXT: Flags [ (0x0) +# CHECK-NEXT: Flags [ +# CHECK-NEXT: EF_MIPS_ABI_O32 +# CHECK-NEXT: EF_MIPS_ARCH_32R2 +# CHECK-NEXT: EF_MIPS_CPIC # CHECK-NEXT: ] # CHECK-NEXT: HeaderSize: 52 # CHECK-NEXT: ProgramHeaderEntrySize: 32 Index: test/ELF/emulation.s =================================================================== --- test/ELF/emulation.s +++ test/ELF/emulation.s @@ -112,7 +112,10 @@ # MIPS-NEXT: Entry: # MIPS-NEXT: ProgramHeaderOffset: 0x34 # MIPS-NEXT: SectionHeaderOffset: -# MIPS-NEXT: Flags [ (0x0) +# MIPS-NEXT: Flags [ +# MIPS-NEXT: EF_MIPS_ABI_O32 +# MIPS-NEXT: EF_MIPS_ARCH_32R2 +# MIPS-NEXT: EF_MIPS_CPIC # MIPS-NEXT: ] # RUN: llvm-mc -filetype=obj -triple=mipsel-unknown-linux %s -o %tmipsel @@ -138,7 +141,10 @@ # MIPSEL-NEXT: Entry: # MIPSEL-NEXT: ProgramHeaderOffset: 0x34 # MIPSEL-NEXT: SectionHeaderOffset: -# MIPSEL-NEXT: Flags [ (0x0) +# MIPSEL-NEXT: Flags [ +# MIPSEL-NEXT: EF_MIPS_ABI_O32 +# MIPSEL-NEXT: EF_MIPS_ARCH_32R2 +# MIPSEL-NEXT: EF_MIPS_CPIC # MIPSEL-NEXT: ] # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-linux %s -o %taarch64 Index: test/ELF/mips-elf-flags.s =================================================================== --- /dev/null +++ test/ELF/mips-elf-flags.s @@ -0,0 +1,27 @@ +# Check generation of MIPS specific ELF header flags. + +# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o +# RUN: ld.lld %t.o -shared -o %t.so +# RUN: llvm-readobj -h %t.so | FileCheck -check-prefix=SO %s +# RUN: ld.lld %t.o -o %t.exe +# RUN: llvm-readobj -h %t.exe | FileCheck -check-prefix=EXE %s + +# REQUIRES: mips + + .text + .globl __start +__start: + nop + +# SO: Flags [ +# SO-NEXT: EF_MIPS_ABI_O32 +# SO-NEXT: EF_MIPS_ARCH_32R2 +# SO-NEXT: EF_MIPS_CPIC +# SO-NEXT: EF_MIPS_PIC +# SO-NEXT: ] + +# EXE: Flags [ +# EXE-NEXT: EF_MIPS_ABI_O32 +# EXE-NEXT: EF_MIPS_ARCH_32R2 +# EXE-NEXT: EF_MIPS_CPIC +# EXE-NEXT: ]