Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -10,14 +10,23 @@ #ifndef LLD_ELF_CONFIG_H #define LLD_ELF_CONFIG_H +#include "llvm/ADT/Optional.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Support/ELF.h" +#include #include namespace lld { namespace elf2 { +enum ELFKind { ELF32LEKind, ELF32BEKind, ELF64LEKind, ELF64BEKind }; + struct Configuration { + bool is64Bits() const { + return *ElfKind == ELF64BEKind || *ElfKind == ELF64LEKind; + } + llvm::StringRef OutputFile; llvm::StringRef DynamicLinker; std::string RPath; @@ -28,6 +37,9 @@ bool DiscardNone = false; bool ExportDynamic = false; bool NoInhibitExec = false; + + llvm::Optional ElfKind; + uint16_t EMachine = llvm::ELF::EM_NONE; }; extern Configuration *Config; Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -15,6 +15,7 @@ #include "Writer.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/Object/ELF.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" @@ -47,17 +48,53 @@ return MBRef; } -static std::unique_ptr createFile(MemoryBufferRef MB) { +static uint16_t getElfMachineType(StringRef Object) { + using namespace object; + + // First three fields of 32-bit and 64-bit ELF headers are of equal + // size, so no matter which one to use, but care about endianness. + if (Object.size() < (ELF::EI_NIDENT + 2 * sizeof(ELF::Elf32_Half))) + return ELF::EM_NONE; + if ((uint8_t)Object[ELF::EI_DATA] == ELF::ELFDATA2MSB) + return ((const Elf_Ehdr_Impl *)Object.data())->e_machine; + return ((const Elf_Ehdr_Impl *)Object.data())->e_machine; +} + +static void configureTarget(ELFKind ElfKind, uint16_t EMachine) { + Config->ElfKind = ElfKind; + Config->EMachine = EMachine; +} + +template