From 6e24e500db3247dc0a733b0ee9264f42552f404c Mon Sep 17 00:00:00 2001 From: Christian Speckner Date: Mon, 15 Jul 2024 22:50:23 +0200 Subject: [PATCH] More relocation tests. --- src/emucore/elf/LinkerTest.cxx | 74 ++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/emucore/elf/LinkerTest.cxx b/src/emucore/elf/LinkerTest.cxx index c899bf7f9..24d64dfcc 100644 --- a/src/emucore/elf/LinkerTest.cxx +++ b/src/emucore/elf/LinkerTest.cxx @@ -736,6 +736,20 @@ TEST(ElfLinker, RodataSectionsGoToRodata) { EXPECT_EQ(segmentRead32(linker, SegmentType::text, 0x08), 0xf803f000); } + TEST(ElfLinker, R_ARM_THM_CALL_ThrowsIfTargetIsOutOfBounds) { + ElfFixture fixture(1000); + ElfLinker linker(0x00100000, 0x00200000, 0x00300000, fixture); + + fixture + .addSection(".text.1", ElfFile::SHT_PROGBITS, 0, 0x10) + .addSection(".text.2", ElfFile::SHT_PROGBITS, 0x10, 0x20) + .addSymbol("foo", 0x04, 2) + .addRelocation(1, 0, 0x08, ElfFile::R_ARM_THM_CALL, 0x7fffff00) + .write32(0x08, 0xfffff7ff); + + EXPECT_THROW(linker.link({}), ElfLinker::ElfLinkError); + } + TEST(ElfLinker, R_ARM_THM_JUMP24L_PatchesOffset) { ElfFixture fixture(1000); ElfLinker linker(0x00100000, 0x00200000, 0x00300000, fixture); @@ -784,4 +798,64 @@ TEST(ElfLinker, RodataSectionsGoToRodata) { EXPECT_EQ(segmentRead32(linker, SegmentType::text, 0x08), 0xb803f000); } + TEST(ElfLinker, R_ARM_THM_JUMP24_ThrowsIfTargetIsOutOfBounds) { + ElfFixture fixture(1000); + ElfLinker linker(0x00100000, 0x00200000, 0x00300000, fixture); + + fixture + .addSection(".text.1", ElfFile::SHT_PROGBITS, 0, 0x10) + .addSection(".text.2", ElfFile::SHT_PROGBITS, 0x10, 0x20) + .addSymbol("foo", 0x04, 2) + .addRelocation(1, 0, 0x08, ElfFile::R_ARM_THM_JUMP24, 0x7fffff00) + .write32(0x08, 0xbffff7ff); + + EXPECT_THROW(linker.link({}), ElfLinker::ElfLinkError); + } + + class RelocationExceptionTest: public testing::TestWithParam {}; + + TEST_P(RelocationExceptionTest, RelocationThrowsIfRelocatedSymbolIsNotResolved) { + ElfFixture fixture(1000); + ElfLinker linker(0x00100000, 0x00200000, 0x00300000, fixture); + + fixture + .addSection(".text.1", ElfFile::SHT_PROGBITS, 0, 0x10) + .addSection(".text.2", ElfFile::SHT_PROGBITS, 0x10, 0x10) + .addSymbol("foo", 0x1234, ElfFile::SHN_UND) + .addRelocation(2, 0, 0x04, GetParam()); + + EXPECT_THROW(linker.link({}), ElfLinker::ElfLinkError); + } + + TEST_P(RelocationExceptionTest, RelocationThrowsIfTargetIsBeyondSectionLimits) { + ElfFixture fixture(1000); + ElfLinker linker(0x00100000, 0x00200000, 0x00300000, fixture); + + fixture + .addSection(".text.1", ElfFile::SHT_PROGBITS, 0, 0x10) + .addSection(".text.2", ElfFile::SHT_PROGBITS, 0x10, 0x10) + .addSymbol("foo", 0x1234, ElfFile::SHN_ABS) + .addRelocation(2, 0, 0xd, GetParam()); + + EXPECT_THROW(linker.link({}), ElfLinker::ElfLinkError); + } + + TEST_P(RelocationExceptionTest, RelocationDoesNotThrowIfTargetIsExactlyOnSectionLimit) { + ElfFixture fixture(1000); + ElfLinker linker(0x00100000, 0x00200000, 0x00300000, fixture); + + fixture + .addSection(".text.1", ElfFile::SHT_PROGBITS, 0, 0x10) + .addSection(".text.2", ElfFile::SHT_PROGBITS, 0x10, 0x10) + .addSymbol("foo", 0x1234, ElfFile::SHN_ABS) + .addRelocation(2, 0, 0xc, GetParam()); + + EXPECT_NO_THROW(linker.link({})); + } + + INSTANTIATE_TEST_SUITE_P(RelocationExceptionSuite, RelocationExceptionTest, testing::Values( + ElfFile::R_ARM_ABS32, ElfFile::R_ARM_TARGET1, + ElfFile::R_ARM_THM_CALL, ElfFile::R_ARM_THM_JUMP24 + )); + }