diff --git a/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp b/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp index 5b3ea762b7f8..80795e1d558c 100644 --- a/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp +++ b/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp @@ -127,6 +127,27 @@ uint64_t MipsTargetHandler::getGPDispSymAddr() const { return _gpDispSymAtom ? _gpDispSymAtom->_virtualAddr : 0; } +bool MipsTargetHandler::doesOverrideELFHeader() { return true; } + +void MipsTargetHandler::setELFHeader(ELFHeader *elfHeader) { + elfHeader->e_version(1); + + elfHeader->e_ident(llvm::ELF::EI_VERSION, llvm::ELF::EV_CURRENT); + elfHeader->e_ident(llvm::ELF::EI_OSABI, llvm::ELF::ELFOSABI_NONE); + if (_targetLayout.findOutputSection(".got.plt")) + elfHeader->e_ident(llvm::ELF::EI_ABIVERSION, 1); + else + elfHeader->e_ident(llvm::ELF::EI_ABIVERSION, 0); + + // FIXME (simon): Read elf flags from all inputs, check compatibility, + // merge them and write result here. + uint32_t flags = llvm::ELF::EF_MIPS_NOREORDER | llvm::ELF::EF_MIPS_ABI_O32 | + llvm::ELF::EF_MIPS_CPIC | llvm::ELF::EF_MIPS_ARCH_32R2; + if (_context.getOutputELFType() == llvm::ELF::ET_DYN) + flags |= EF_MIPS_PIC; + elfHeader->e_flags(flags); +} + MipsTargetLayout &MipsTargetHandler::targetLayout() { return _targetLayout; } diff --git a/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h b/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h index d6e83372254f..e5bf725966b6 100644 --- a/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h +++ b/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h @@ -54,6 +54,8 @@ public: uint64_t getGPDispSymAddr() const; + virtual bool doesOverrideELFHeader(); + virtual void setELFHeader(ELFHeader *elfHeader); virtual MipsTargetLayout &targetLayout(); virtual const MipsTargetRelocationHandler &getRelocationHandler() const; virtual LLD_UNIQUE_BUMP_PTR(DynamicTable) diff --git a/lld/test/elf/Mips/dynlib-fileheader.test b/lld/test/elf/Mips/dynlib-fileheader.test index 532196fd167a..0ecc293d1dfb 100644 --- a/lld/test/elf/Mips/dynlib-fileheader.test +++ b/lld/test/elf/Mips/dynlib-fileheader.test @@ -22,7 +22,13 @@ CHECK: Version: 1 CHECK: Entry: 0x170 CHECK: ProgramHeaderOffset: 0x34 CHECK: SectionHeaderOffset: 0x21D8 -CHECK: Flags [ (0x0) +CHECK: Flags [ (0x70001007) +CHECK: 0x1 +CHECK: 0x2 +CHECK: 0x1000 +CHECK: 0x10000000 +CHECK: 0x20000000 +CHECK: 0x40000000 CHECK: ] CHECK: HeaderSize: 52 CHECK: ProgramHeaderEntrySize: 32 diff --git a/lld/test/elf/Mips/exe-fileheader.test b/lld/test/elf/Mips/exe-fileheader.test new file mode 100644 index 000000000000..f0919bbbbb2b --- /dev/null +++ b/lld/test/elf/Mips/exe-fileheader.test @@ -0,0 +1,54 @@ +# Check ELF Header for non-pic executable file. + +# Build shared library +# RUN: llvm-mc -triple=mipsel -filetype=obj -relocation-model=pic \ +# RUN: -o=%t-obj %p/Inputs/ext.s +# RUN: lld -flavor gnu -target mipsel -shared -o %t-so %t-obj + +# Build executable +# RUN: llvm-mc -triple=mipsel -filetype=obj -o=%t-obj %s +# RUN: lld -flavor gnu -target mipsel -e glob -o %t-exe %t-obj %t-so +# RUN: llvm-readobj -file-headers %t-exe | FileCheck %s + +# CHECK: Format: ELF32-mips +# CHECK: Arch: mipsel +# CHECK: AddressSize: 32bit +# CHECK: LoadName: +# CHECK: ElfHeader { +# CHECK: Ident { +# CHECK: Magic: (7F 45 4C 46) +# CHECK: Class: 32-bit (0x1) +# CHECK: DataEncoding: LittleEndian (0x1) +# CHECK: FileVersion: 1 +# CHECK: OS/ABI: SystemV (0x0) +# CHECK: ABIVersion: 1 +# CHECK: Unused: (00 00 00 00 00 00 00) +# CHECK: } +# CHECK: Type: Executable (0x2) +# CHECK: Machine: EM_MIPS (0x8) +# CHECK: Version: 1 +# CHECK: Entry: 0x4001B0 +# CHECK: ProgramHeaderOffset: 0x34 +# CHECK: SectionHeaderOffset: 0x2268 +# CHECK: Flags [ (0x70001005) +# CHECK: 0x1 +# CHECK: 0x4 +# CHECK: 0x1000 +# CHECK: 0x10000000 +# CHECK: 0x20000000 +# CHECK: 0x40000000 +# CHECK: ] +# CHECK: HeaderSize: 52 +# CHECK: ProgramHeaderEntrySize: 32 +# CHECK: ProgramHeaderCount: 6 +# CHECK: SectionHeaderEntrySize: 40 +# CHECK: SectionHeaderCount: 16 +# CHECK: StringTableSectionIndex: 13 +# CHECK: } + + .abicalls + .global glob + .ent glob +glob: + jal ext1 + .end glob