From f463709d7d66239b2221c48ee907b826136f84b5 Mon Sep 17 00:00:00 2001 From: Simon Atanasyan Date: Wed, 20 Jul 2016 20:15:24 +0000 Subject: [PATCH] [ELF][MIPS] Pick arch flag from the first input file. LLD still does not produce a correct combination of MIPS ELF flags if input files have different sets of ELF flags (i.e. EF_MIPS_ARCH_32 and EF_MIPS_ARCH_32R2). But now we do not stick to "R2" ABI version and can emit EF_MIPS_ARCH_32R6 for example. llvm-svn: 276172 --- lld/ELF/Writer.cpp | 26 ++++++++++++++++---------- lld/test/ELF/basic-mips.s | 2 +- lld/test/ELF/emulation.s | 8 ++++---- lld/test/ELF/mips-elf-flags.s | 24 ++++++++++++++++++++++-- 4 files changed, 43 insertions(+), 17 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 764699f17e20..ceff38c4e68b 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1166,16 +1166,22 @@ template void Writer::setPhdrs() { } } -static uint32_t getMipsEFlags(bool Is64Bits) { - // FIXME: In fact ELF flags depends on ELF flags of input object files - // and selected emulation. For now just use hard coded values. +template +static uint32_t getMipsEFlags(bool Is64Bits, + const ELFFileBase &FirstElf) { + // FIXME: ELF flags depends on ELF flags of all input object files and + // selected emulation. For now pick the arch flag from the fisrt input file + // and use hard coded values for other flags. + uint32_t FirstElfFlags = FirstElf.getObj().getHeader()->e_flags; + uint32_t ElfFlags = FirstElfFlags & EF_MIPS_ARCH; if (Is64Bits) - return EF_MIPS_CPIC | EF_MIPS_PIC | EF_MIPS_ARCH_64R2; - - uint32_t V = EF_MIPS_CPIC | EF_MIPS_ABI_O32 | EF_MIPS_ARCH_32R2; - if (Config->Shared) - V |= EF_MIPS_PIC; - return V; + ElfFlags |= EF_MIPS_CPIC | EF_MIPS_PIC; + else { + ElfFlags |= EF_MIPS_CPIC | EF_MIPS_ABI_O32; + if (Config->Shared) + ElfFlags |= EF_MIPS_PIC; + } + return ElfFlags; } template static typename ELFT::uint getEntryAddr() { @@ -1252,7 +1258,7 @@ template void Writer::writeHeader() { EHdr->e_shstrndx = Out::ShStrTab->SectionIndex; if (Config->EMachine == EM_MIPS) - EHdr->e_flags = getMipsEFlags(ELFT::Is64Bits); + EHdr->e_flags = getMipsEFlags(ELFT::Is64Bits, FirstObj); if (!Config->Relocatable) { EHdr->e_phoff = sizeof(Elf_Ehdr); diff --git a/lld/test/ELF/basic-mips.s b/lld/test/ELF/basic-mips.s index c598c7b5f2f7..86dd04200533 100644 --- a/lld/test/ELF/basic-mips.s +++ b/lld/test/ELF/basic-mips.s @@ -30,7 +30,7 @@ __start: # CHECK-NEXT: SectionHeaderOffset: 0x30088 # CHECK-NEXT: Flags [ # CHECK-NEXT: EF_MIPS_ABI_O32 -# CHECK-NEXT: EF_MIPS_ARCH_32R2 +# CHECK-NEXT: EF_MIPS_ARCH_32 # CHECK-NEXT: EF_MIPS_CPIC # CHECK-NEXT: ] # CHECK-NEXT: HeaderSize: 52 diff --git a/lld/test/ELF/emulation.s b/lld/test/ELF/emulation.s index 7cc764fc6c18..1d95b56d1a45 100644 --- a/lld/test/ELF/emulation.s +++ b/lld/test/ELF/emulation.s @@ -176,7 +176,7 @@ # MIPS-NEXT: SectionHeaderOffset: # MIPS-NEXT: Flags [ # MIPS-NEXT: EF_MIPS_ABI_O32 -# MIPS-NEXT: EF_MIPS_ARCH_32R2 +# MIPS-NEXT: EF_MIPS_ARCH_32 # MIPS-NEXT: EF_MIPS_CPIC # MIPS-NEXT: ] @@ -205,7 +205,7 @@ # MIPSEL-NEXT: SectionHeaderOffset: # MIPSEL-NEXT: Flags [ # MIPSEL-NEXT: EF_MIPS_ABI_O32 -# MIPSEL-NEXT: EF_MIPS_ARCH_32R2 +# MIPSEL-NEXT: EF_MIPS_ARCH_32 # MIPSEL-NEXT: EF_MIPS_CPIC # MIPSEL-NEXT: ] @@ -231,7 +231,7 @@ # MIPS64-NEXT: ProgramHeaderOffset: 0x40 # MIPS64-NEXT: SectionHeaderOffset: # MIPS64-NEXT: Flags [ -# MIPS64-NEXT: EF_MIPS_ARCH_64R2 +# MIPS64-NEXT: EF_MIPS_ARCH_64 # MIPS64-NEXT: EF_MIPS_CPIC # MIPS64-NEXT: EF_MIPS_PIC # MIPS64-NEXT: ] @@ -258,7 +258,7 @@ # MIPS64EL-NEXT: ProgramHeaderOffset: 0x40 # MIPS64EL-NEXT: SectionHeaderOffset: # MIPS64EL-NEXT: Flags [ -# MIPS64EL-NEXT: EF_MIPS_ARCH_64R2 +# MIPS64EL-NEXT: EF_MIPS_ARCH_64 # MIPS64EL-NEXT: EF_MIPS_CPIC # MIPS64EL-NEXT: EF_MIPS_PIC # MIPS64EL-NEXT: ] diff --git a/lld/test/ELF/mips-elf-flags.s b/lld/test/ELF/mips-elf-flags.s index 7817e58b5383..c71e2b181cc8 100644 --- a/lld/test/ELF/mips-elf-flags.s +++ b/lld/test/ELF/mips-elf-flags.s @@ -5,6 +5,14 @@ # 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 +# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \ +# RUN: -mcpu=mips32r2 %s -o %t-r2.o +# RUN: ld.lld %t-r2.o -o %t-r2.exe +# RUN: llvm-readobj -h %t-r2.exe | FileCheck -check-prefix=EXE-R2 %s +# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \ +# RUN: -mcpu=mips32r6 %s -o %t-r6.o +# RUN: ld.lld %t-r6.o -o %t-r6.exe +# RUN: llvm-readobj -h %t-r6.exe | FileCheck -check-prefix=EXE-R6 %s # REQUIRES: mips @@ -15,13 +23,25 @@ __start: # SO: Flags [ # SO-NEXT: EF_MIPS_ABI_O32 -# SO-NEXT: EF_MIPS_ARCH_32R2 +# SO-NEXT: EF_MIPS_ARCH_32 # 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_ARCH_32 # EXE-NEXT: EF_MIPS_CPIC # EXE-NEXT: ] + +# EXE-R2: Flags [ +# EXE-R2-NEXT: EF_MIPS_ABI_O32 +# EXE-R2-NEXT: EF_MIPS_ARCH_32R2 +# EXE-R2-NEXT: EF_MIPS_CPIC +# EXE-R2-NEXT: ] + +# EXE-R6: Flags [ +# EXE-R6-NEXT: EF_MIPS_ABI_O32 +# EXE-R6-NEXT: EF_MIPS_ARCH_32R6 +# EXE-R6-NEXT: EF_MIPS_CPIC +# EXE-R6-NEXT: ] -- GitLab