diff options
| -rw-r--r-- | lld/ELF/Arch/ARM.cpp | 12 | ||||
| -rw-r--r-- | lld/ELF/Arch/Mips.cpp | 2 | ||||
| -rw-r--r-- | lld/ELF/Arch/PPC64.cpp | 3 | ||||
| -rw-r--r-- | lld/ELF/Arch/X86.cpp | 2 | ||||
| -rw-r--r-- | lld/ELF/Arch/X86_64.cpp | 2 | ||||
| -rw-r--r-- | lld/ELF/OutputSections.cpp | 19 | ||||
| -rw-r--r-- | lld/ELF/OutputSections.h | 5 | ||||
| -rw-r--r-- | lld/ELF/ScriptParser.cpp | 14 | ||||
| -rw-r--r-- | lld/ELF/Target.h | 3 |
9 files changed, 33 insertions, 29 deletions
diff --git a/lld/ELF/Arch/ARM.cpp b/lld/ELF/Arch/ARM.cpp index 25dde85875b..ab0b8f8c4af 100644 --- a/lld/ELF/Arch/ARM.cpp +++ b/lld/ELF/Arch/ARM.cpp @@ -61,7 +61,7 @@ ARM::ARM() { GotPltEntrySize = 4; PltEntrySize = 16; PltHeaderSize = 32; - TrapInstr = 0xd4d4d4d4; + TrapInstr = {0xd4, 0xd4, 0xd4, 0xd4}; NeedsThunks = true; } @@ -196,10 +196,10 @@ void ARM::writePltHeader(uint8_t *Buf) const { write32le(Buf + 4, PltData[1] | ((Offset >> 20) & 0xff)); write32le(Buf + 8, PltData[2] | ((Offset >> 12) & 0xff)); write32le(Buf + 12, PltData[3] | (Offset & 0xfff)); - write32le(Buf + 16, TrapInstr); // Pad to 32-byte boundary - write32le(Buf + 20, TrapInstr); - write32le(Buf + 24, TrapInstr); - write32le(Buf + 28, TrapInstr); + memcpy(Buf + 16, TrapInstr.data(), 4); // Pad to 32-byte boundary + memcpy(Buf + 20, TrapInstr.data(), 4); + memcpy(Buf + 24, TrapInstr.data(), 4); + memcpy(Buf + 28, TrapInstr.data(), 4); } void ARM::addPltHeaderSymbols(InputSection &IS) const { @@ -248,7 +248,7 @@ void ARM::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, write32le(Buf + 0, PltData[0] | ((Offset >> 20) & 0xff)); write32le(Buf + 4, PltData[1] | ((Offset >> 12) & 0xff)); write32le(Buf + 8, PltData[2] | (Offset & 0xfff)); - write32le(Buf + 12, TrapInstr); // Pad to 16-byte boundary + memcpy(Buf + 12, TrapInstr.data(), 4); // Pad to 16-byte boundary } void ARM::addPltSymbols(InputSection &IS, uint64_t Off) const { diff --git a/lld/ELF/Arch/Mips.cpp b/lld/ELF/Arch/Mips.cpp index f6402f80a5b..f52019f0684 100644 --- a/lld/ELF/Arch/Mips.cpp +++ b/lld/ELF/Arch/Mips.cpp @@ -56,7 +56,7 @@ template <class ELFT> MIPS<ELFT>::MIPS() { NoneRel = R_MIPS_NONE; PltRel = R_MIPS_JUMP_SLOT; NeedsThunks = true; - TrapInstr = 0xefefefef; + TrapInstr = {0xef, 0xef, 0xef, 0xef}; if (ELFT::Is64Bits) { RelativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32; diff --git a/lld/ELF/Arch/PPC64.cpp b/lld/ELF/Arch/PPC64.cpp index 884f1764e6f..6562eebf3a0 100644 --- a/lld/ELF/Arch/PPC64.cpp +++ b/lld/ELF/Arch/PPC64.cpp @@ -230,8 +230,7 @@ PPC64::PPC64() { // use 0x10000000 as the starting address. DefaultImageBase = 0x10000000; - TrapInstr = - (Config->IsLE == sys::IsLittleEndianHost) ? 0x7fe00008 : 0x0800e07f; + write32(TrapInstr.data(), 0x7fe00008); } static uint32_t getEFlags(InputFile *File) { diff --git a/lld/ELF/Arch/X86.cpp b/lld/ELF/Arch/X86.cpp index 82246e68966..e910375d2fc 100644 --- a/lld/ELF/Arch/X86.cpp +++ b/lld/ELF/Arch/X86.cpp @@ -60,7 +60,7 @@ X86::X86() { PltEntrySize = 16; PltHeaderSize = 16; TlsGdRelaxSkip = 2; - TrapInstr = 0xcccccccc; // 0xcc = INT3 + TrapInstr = {0xcc, 0xcc, 0xcc, 0xcc}; // 0xcc = INT3 // Align to the non-PAE large page size (known as a superpage or huge page). // FreeBSD automatically promotes large, superpage-aligned allocations. diff --git a/lld/ELF/Arch/X86_64.cpp b/lld/ELF/Arch/X86_64.cpp index 6421ec4e0c3..9b86f228d70 100644 --- a/lld/ELF/Arch/X86_64.cpp +++ b/lld/ELF/Arch/X86_64.cpp @@ -67,7 +67,7 @@ template <class ELFT> X86_64<ELFT>::X86_64() { PltEntrySize = 16; PltHeaderSize = 16; TlsGdRelaxSkip = 2; - TrapInstr = 0xcccccccc; // 0xcc = INT3 + TrapInstr = {0xcc, 0xcc, 0xcc, 0xcc}; // 0xcc = INT3 // Align to the large page size (known as a superpage or huge page). // FreeBSD automatically promotes large, superpage-aligned allocations. diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index c2c7f2c0320..8ad01e34654 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -25,6 +25,7 @@ using namespace llvm; using namespace llvm::dwarf; using namespace llvm::object; +using namespace llvm::support::endian; using namespace llvm::ELF; using namespace lld; @@ -170,11 +171,12 @@ void OutputSection::sort(llvm::function_ref<int(InputSectionBase *S)> Order) { // Fill [Buf, Buf + Size) with Filler. // This is used for linker script "=fillexp" command. -static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) { +static void fill(uint8_t *Buf, size_t Size, + const std::array<uint8_t, 4> &Filler) { size_t I = 0; for (; I + 4 < Size; I += 4) - memcpy(Buf + I, &Filler, 4); - memcpy(Buf + I, &Filler, Size - I); + memcpy(Buf + I, Filler.data(), 4); + memcpy(Buf + I, Filler.data(), Size - I); } // Compress section contents if this section contains debug info. @@ -235,8 +237,9 @@ template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) { // Write leading padding. std::vector<InputSection *> Sections = getInputSections(this); - uint32_t Filler = getFiller(); - if (Filler) + std::array<uint8_t, 4> Filler = getFiller(); + bool NonZeroFiller = read32(Filler.data()) != 0; + if (NonZeroFiller) fill(Buf, Sections.empty() ? Size : Sections[0]->OutSecOff, Filler); parallelForEachN(0, Sections.size(), [&](size_t I) { @@ -244,7 +247,7 @@ template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) { IS->writeTo<ELFT>(Buf); // Fill gaps between sections. - if (Filler) { + if (NonZeroFiller) { uint8_t *Start = Buf + IS->OutSecOff + IS->getSize(); uint8_t *End; if (I + 1 == Sections.size()) @@ -405,12 +408,12 @@ void OutputSection::sortInitFini() { sort([](InputSectionBase *S) { return getPriority(S->Name); }); } -uint32_t OutputSection::getFiller() { +std::array<uint8_t, 4> OutputSection::getFiller() { if (Filler) return *Filler; if (Flags & SHF_EXECINSTR) return Target->TrapInstr; - return 0; + return {0, 0, 0, 0}; } template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr); diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h index c004c1f19bf..113bf683692 100644 --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -17,6 +17,7 @@ #include "lld/Common/LLVM.h" #include "llvm/MC/StringTableBuilder.h" #include "llvm/Object/ELF.h" +#include <array> namespace lld { namespace elf { @@ -94,7 +95,7 @@ public: Expr SubalignExpr; std::vector<BaseCommand *> SectionCommands; std::vector<StringRef> Phdrs; - llvm::Optional<uint32_t> Filler; + llvm::Optional<std::array<uint8_t, 4>> Filler; ConstraintKind Constraint = ConstraintKind::NoConstraint; std::string Location; std::string MemoryRegionName; @@ -117,7 +118,7 @@ private: std::vector<uint8_t> ZDebugHeader; llvm::SmallVector<char, 1> CompressedData; - uint32_t getFiller(); + std::array<uint8_t, 4> getFiller(); }; int getPriority(StringRef S); diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp index b1802111eaf..6aed439cf4f 100644 --- a/lld/ELF/ScriptParser.cpp +++ b/lld/ELF/ScriptParser.cpp @@ -78,8 +78,8 @@ private: SymbolAssignment *readSymbolAssignment(StringRef Name); ByteCommand *readByteCommand(StringRef Tok); - uint32_t readFill(); - uint32_t parseFill(StringRef Tok); + std::array<uint8_t, 4> readFill(); + std::array<uint8_t, 4> parseFill(StringRef Tok); bool readSectionDirective(OutputSection *Cmd, StringRef Tok1, StringRef Tok2); void readSectionAddressType(OutputSection *Cmd); OutputSection *readOverlaySectionDescription(); @@ -727,9 +727,9 @@ Expr ScriptParser::readAssert() { // alias for =fillexp section attribute, which is different from // what GNU linkers do. // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html -uint32_t ScriptParser::readFill() { +std::array<uint8_t, 4> ScriptParser::readFill() { expect("("); - uint32_t V = parseFill(next()); + std::array<uint8_t, 4> V = parseFill(next()); expect(")"); return V; } @@ -879,13 +879,13 @@ OutputSection *ScriptParser::readOutputSectionDescription(StringRef OutSec) { // When reading a hexstring, ld.bfd handles it as a blob of arbitrary // size, while ld.gold always handles it as a 32-bit big-endian number. // We are compatible with ld.gold because it's easier to implement. -uint32_t ScriptParser::parseFill(StringRef Tok) { +std::array<uint8_t, 4> ScriptParser::parseFill(StringRef Tok) { uint32_t V = 0; if (!to_integer(Tok, V)) setError("invalid filler expression: " + Tok); - uint32_t Buf; - write32be(&Buf, V); + std::array<uint8_t, 4> Buf; + write32be(Buf.data(), V); return Buf; } diff --git a/lld/ELF/Target.h b/lld/ELF/Target.h index e1e33de3d26..6f2b7473085 100644 --- a/lld/ELF/Target.h +++ b/lld/ELF/Target.h @@ -14,6 +14,7 @@ #include "lld/Common/ErrorHandler.h" #include "llvm/Object/ELF.h" #include "llvm/Support/MathExtras.h" +#include <array> namespace lld { std::string toString(elf::RelType Type); @@ -121,7 +122,7 @@ public: // A 4-byte field corresponding to one or more trap instructions, used to pad // executable OutputSections. - uint32_t TrapInstr = 0; + std::array<uint8_t, 4> TrapInstr = {0, 0, 0, 0}; // If a target needs to rewrite calls to __morestack to instead call // __morestack_non_split when a split-stack enabled caller calls a |

