Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -15,6 +15,7 @@ #include "llvm/ADT/StringSet.h" #include "llvm/Support/CodeGen.h" #include "llvm/Support/ELF.h" +#include "llvm/Support/Endian.h" #include @@ -204,6 +205,12 @@ bool isMips64EL() const { return EMachine == llvm::ELF::EM_MIPS && EKind == ELF64LEKind; } + + llvm::support::endianness getEndian() const { + return (EKind == ELF32LEKind || EKind == ELF64LEKind) + ? llvm::support::endianness::little + : llvm::support::endianness::big; + } }; // The only instance of Configuration struct. Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -267,6 +267,7 @@ uint64_t getDot() { return Dot; } OutputSection *getOutputSection(const Twine &Loc, StringRef S); uint64_t getOutputSectionSize(StringRef S); + void writeDataBytes(StringRef Name, uint8_t *Buf); virtual uint64_t getSymbolValue(const Twine &Loc, StringRef S) = 0; virtual bool isDefined(StringRef S) = 0; @@ -297,7 +298,6 @@ LinkerScript(); ~LinkerScript(); - void writeDataBytes(StringRef Name, uint8_t *Buf); void addSymbol(SymbolAssignment *Cmd); void discard(ArrayRef V); void processCommands(OutputSectionFactory &Factory); Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -863,30 +863,28 @@ return 0; } -template static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) { - const endianness E = ELFT::TargetEndianness; + const endianness E = Config->getEndian(); switch (Size) { case 1: *Buf = (uint8_t)Data; break; case 2: - write16(Buf, Data); + write16(Buf, Data, E); break; case 4: - write32(Buf, Data); + write32(Buf, Data, E); break; case 8: - write64(Buf, Data); + write64(Buf, Data, E); break; default: llvm_unreachable("unsupported Size argument"); } } -template -void LinkerScript::writeDataBytes(StringRef Name, uint8_t *Buf) { +void LinkerScriptBase::writeDataBytes(StringRef Name, uint8_t *Buf) { int I = getSectionIndex(Name); if (I == INT_MAX) return; @@ -894,7 +892,7 @@ auto *Cmd = dyn_cast(Opt.Commands[I].get()); for (const std::unique_ptr &Base : Cmd->Commands) if (auto *Data = dyn_cast(Base.get())) - writeInt(Buf + Data->Offset, Data->Expression(), Data->Size); + writeInt(Buf + Data->Offset, Data->Expression(), Data->Size); } bool LinkerScriptBase::hasLMA(StringRef Name) {