Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -274,6 +274,7 @@ initLLVM(Args); createFiles(Args); checkOptions(Args); + if (HasError) return; @@ -313,9 +314,6 @@ Config->Emulation = S; } - if (Config->EMachine == EM_MIPS && Config->EKind == ELF64LEKind) - Config->Mips64EL = true; - Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition); Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic); Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions); @@ -469,6 +467,17 @@ if (Files.empty() && !HasError) error("no input files."); + + // If -m was not given, infer it from object files. + if (Config->EKind == ELFNoneKind) { + for (std::unique_ptr &F : Files) { + if (F->EKind == ELFNoneKind) + continue; + Config->EKind = F->EKind; + Config->EMachine = F->EMachine; + break; + } + } } // Do actual linking. Note that when this function is called, @@ -483,6 +492,8 @@ Script::X = &LS; Config->Rela = ELFT::Is64Bits; + Config->Mips64EL = + (Config->EMachine == EM_MIPS && Config->EKind == ELF64LEKind); // Add entry symbol. Note that AMDGPU binaries have no entry points. if (Config->Entry.empty() && !Config->Shared && !Config->Relocatable && Index: ELF/InputFiles.h =================================================================== --- ELF/InputFiles.h +++ ELF/InputFiles.h @@ -56,6 +56,11 @@ // string for creating error messages. StringRef ArchiveName; + // If this is an architecture-specific file, the following members + // have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type. + ELFKind EKind = ELFNoneKind; + uint16_t EMachine = llvm::ELF::EM_NONE; + protected: InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {} @@ -79,11 +84,9 @@ return K == ObjectKind || K == SharedKind; } - static ELFKind getELFKind(); const llvm::object::ELFFile &getObj() const { return ELFObj; } llvm::object::ELFFile &getObj() { return ELFObj; } - uint16_t getEMachine() const { return getObj().getHeader()->e_machine; } uint8_t getOSABI() const { return getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI]; } Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -44,18 +44,20 @@ return F; } -template -ELFFileBase::ELFFileBase(Kind K, MemoryBufferRef MB) - : InputFile(K, MB), ELFObj(createELFObj(MB)) {} - -template -ELFKind ELFFileBase::getELFKind() { +template static ELFKind getELFKind() { if (ELFT::TargetEndianness == support::little) return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind; return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind; } template +ELFFileBase::ELFFileBase(Kind K, MemoryBufferRef MB) + : InputFile(K, MB), ELFObj(createELFObj(MB)) { + EKind = getELFKind(); + EMachine = ELFObj.getHeader()->e_machine; +} + +template typename ELFT::SymRange ELFFileBase::getElfSymbols(bool OnlyGlobals) { if (!Symtab) return Elf_Sym_Range(nullptr, nullptr); @@ -644,23 +646,6 @@ Symbols.push_back(createSymbol(KeptComdats, *Obj, Sym)); } -template -static std::unique_ptr createELFFileAux(MemoryBufferRef MB) { - std::unique_ptr Ret = llvm::make_unique(MB); - - if (!Config->FirstElf) - Config->FirstElf = Ret.get(); - - if (Config->EKind == ELFNoneKind) { - Config->EKind = Ret->getELFKind(); - Config->EMachine = Ret->getEMachine(); - if (Config->EMachine == EM_MIPS && Config->EKind == ELF64LEKind) - Config->Mips64EL = true; - } - - return std::move(Ret); -} - template