From 3f662e3757b44e31e6122c0a87e32b0e8f890099 Mon Sep 17 00:00:00 2001 From: Marcin Chwedczuk <0xmarcin+dev@gmail.com> Date: Sun, 19 Dec 2021 13:32:24 +0100 Subject: [PATCH] Remove ElfOffsetAny --- .../elfreader/elf/arch/IntegerNativeWord.java | 2 +- .../elfreader/elf/arch/LongNativeWord.java | 2 +- .../elfreader/elf/shared/ElfOffset.java | 10 ++++----- .../elfreader/elf/shared/ElfOffsetAny.java | 22 ------------------- .../elfreader/io/StructuredFile.java | 3 +-- .../elfreader/io/StructuredFile32.java | 4 ++-- .../elfreader/io/StructuredFile64.java | 5 ++--- .../elf32/ElfReader_32Bits_Test.java | 8 +++---- .../elf32/ElfReader_64Bits_Test.java | 8 +++---- 9 files changed, 20 insertions(+), 44 deletions(-) delete mode 100644 elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/shared/ElfOffsetAny.java diff --git a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/arch/IntegerNativeWord.java b/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/arch/IntegerNativeWord.java index 1f4c7ad..55cf08c 100644 --- a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/arch/IntegerNativeWord.java +++ b/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/arch/IntegerNativeWord.java @@ -13,7 +13,7 @@ public NativeWordType type() { @Override public ElfOffset zeroOffset() { - return new ElfOffsetAny<>(0); + return new ElfOffset<>(0); } @Override diff --git a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/arch/LongNativeWord.java b/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/arch/LongNativeWord.java index 63f9e1e..b83cf6a 100644 --- a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/arch/LongNativeWord.java +++ b/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/arch/LongNativeWord.java @@ -13,7 +13,7 @@ public NativeWordType type() { @Override public ElfOffset zeroOffset() { - return new ElfOffsetAny<>(0L); + return new ElfOffset<>(0L); } @Override diff --git a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/shared/ElfOffset.java b/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/shared/ElfOffset.java index 114e866..a38b442 100644 --- a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/shared/ElfOffset.java +++ b/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/shared/ElfOffset.java @@ -4,7 +4,7 @@ import static java.util.Objects.requireNonNull; -public abstract class ElfOffset< +public class ElfOffset< NATIVE_WORD extends Number & Comparable > { @@ -16,8 +16,6 @@ public ElfOffset(NATIVE_WORD fileOffset) { public NATIVE_WORD value() { return fileOffset; } - protected abstract ElfOffset mkFileOffset(NATIVE_WORD value); - public boolean isNull() { return (fileOffset.longValue() == 0); } @@ -43,7 +41,7 @@ public boolean isBeforeOrAt(ElfOffset fileOffset) { public ElfOffset plus(long bytesCount) { long result = fileOffset.longValue() + bytesCount; // TODO: Check overflow - return mkFileOffset((fileOffset instanceof Integer) + return new ElfOffset<>((fileOffset instanceof Integer) // TODO: Think how to remove type check, maybe use static method? ? (NATIVE_WORD)(Integer)Math.toIntExact(result) : (NATIVE_WORD)(Long)result); @@ -69,5 +67,7 @@ public int hashCode() { } @Override - public abstract String toString(); + public String toString() { + return String.format("0x%016x", value().longValue()); + } } diff --git a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/shared/ElfOffsetAny.java b/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/shared/ElfOffsetAny.java deleted file mode 100644 index 9288091..0000000 --- a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/elf/shared/ElfOffsetAny.java +++ /dev/null @@ -1,22 +0,0 @@ -package pl.marcinchwedczuk.elfviewer.elfreader.elf.shared; - -import java.util.Objects; - -public class ElfOffsetAny< - NATIVE_WORD extends Number & Comparable - > extends ElfOffset { - - public ElfOffsetAny(NATIVE_WORD fileOffset) { - super(fileOffset); - } - - @Override - protected ElfOffset mkFileOffset(NATIVE_WORD value) { - return new ElfOffsetAny<>(value); - } - - @Override - public String toString() { - return Objects.toString(value()); - } -} diff --git a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/io/StructuredFile.java b/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/io/StructuredFile.java index 5ca372c..f92b8f1 100644 --- a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/io/StructuredFile.java +++ b/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/io/StructuredFile.java @@ -3,7 +3,6 @@ import pl.marcinchwedczuk.elfviewer.elfreader.elf.shared.ElfAddress; import pl.marcinchwedczuk.elfviewer.elfreader.elf.shared.ElfFile; import pl.marcinchwedczuk.elfviewer.elfreader.elf.shared.ElfOffset; -import pl.marcinchwedczuk.elfviewer.elfreader.elf.shared.ElfOffsetAny; import pl.marcinchwedczuk.elfviewer.elfreader.endianness.Endianness; import pl.marcinchwedczuk.elfviewer.elfreader.utils.ByteList; @@ -64,7 +63,7 @@ public short readUnsignedShort() { public ElfOffset readOffset64() { byte[] addressBytes = readNext(8); long address = endianness.toUnsignedLong(addressBytes); - return new ElfOffsetAny<>(address); + return new ElfOffset<>(address); } public int readUnsignedInt() { diff --git a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/io/StructuredFile32.java b/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/io/StructuredFile32.java index 1138645..788db48 100644 --- a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/io/StructuredFile32.java +++ b/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/io/StructuredFile32.java @@ -22,14 +22,14 @@ public StructuredFile32(ElfFile file, ElfOffset offset) { @Override protected ElfOffset mkOffset(long offset) { - return new ElfOffsetAny<>(Math.toIntExact(offset)); + return new ElfOffset<>(Math.toIntExact(offset)); } @Override public ElfOffset readOffset() { byte[] addressBytes = readNext(4); int address = endianness.toUnsignedInt(addressBytes); - return new ElfOffsetAny<>(address); + return new ElfOffset<>(address); } @Override diff --git a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/io/StructuredFile64.java b/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/io/StructuredFile64.java index 9f47bf7..00541cf 100644 --- a/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/io/StructuredFile64.java +++ b/elf-reader/src/main/java/pl/marcinchwedczuk/elfviewer/elfreader/io/StructuredFile64.java @@ -3,7 +3,6 @@ import pl.marcinchwedczuk.elfviewer.elfreader.elf.shared.ElfAddress; import pl.marcinchwedczuk.elfviewer.elfreader.elf.shared.ElfFile; import pl.marcinchwedczuk.elfviewer.elfreader.elf.shared.ElfOffset; -import pl.marcinchwedczuk.elfviewer.elfreader.elf.shared.ElfOffsetAny; import pl.marcinchwedczuk.elfviewer.elfreader.endianness.Endianness; public class StructuredFile64 extends StructuredFile { @@ -25,14 +24,14 @@ public StructuredFile64(ElfFile file, ElfOffset offset) { @Override protected ElfOffset mkOffset(long offset) { - return new ElfOffsetAny<>(offset); + return new ElfOffset<>(offset); } @Override public ElfOffset readOffset() { byte[] addressBytes = readNext(8); long address = endianness.toUnsignedLong(addressBytes); - return new ElfOffsetAny<>(address); + return new ElfOffset<>(address); } @Override diff --git a/elf-reader/src/test/java/pl/marcinchwedczuk/elfviewer/elfreader/elf32/ElfReader_32Bits_Test.java b/elf-reader/src/test/java/pl/marcinchwedczuk/elfviewer/elfreader/elf32/ElfReader_32Bits_Test.java index 497c27a..85ef457 100644 --- a/elf-reader/src/test/java/pl/marcinchwedczuk/elfviewer/elfreader/elf32/ElfReader_32Bits_Test.java +++ b/elf-reader/src/test/java/pl/marcinchwedczuk/elfviewer/elfreader/elf32/ElfReader_32Bits_Test.java @@ -92,11 +92,11 @@ public void elf32_header() { // offset into ELF file assertThat(header.programHeaderTableOffset()) - .isEqualTo(new ElfOffsetAny<>(52)); + .isEqualTo(new ElfOffset<>(52)); // offset into ELF file assertThat(header.sectionHeaderTableOffset()) - .isEqualTo(new ElfOffsetAny<>(6116)); + .isEqualTo(new ElfOffset<>(6116)); // flags assertThat(header.flags()) @@ -158,7 +158,7 @@ public void elf32_section_header() { .isEqualTo(new ElfAddress<>(0x08048310)); assertThat(textSection.fileOffset()) - .isEqualTo(new ElfOffsetAny<>(0x00000310)); + .isEqualTo(new ElfOffset<>(0x00000310)); assertThat(textSection.size()) .isEqualTo(0x00000192); @@ -254,7 +254,7 @@ void elf32_segment_header() { assertThat(textSegment.type()) .isEqualTo(ElfSegmentType.LOAD); assertThat(textSegment.fileOffset()) - .isEqualTo(new ElfOffsetAny<>(0x000000)); + .isEqualTo(new ElfOffset<>(0x000000)); assertThat(textSegment.virtualAddress()) .isEqualTo(new ElfAddress<>(0x08048000)); assertThat(textSegment.physicalAddress()) diff --git a/elf-reader/src/test/java/pl/marcinchwedczuk/elfviewer/elfreader/elf32/ElfReader_64Bits_Test.java b/elf-reader/src/test/java/pl/marcinchwedczuk/elfviewer/elfreader/elf32/ElfReader_64Bits_Test.java index a5a0890..6b3ea75 100644 --- a/elf-reader/src/test/java/pl/marcinchwedczuk/elfviewer/elfreader/elf32/ElfReader_64Bits_Test.java +++ b/elf-reader/src/test/java/pl/marcinchwedczuk/elfviewer/elfreader/elf32/ElfReader_64Bits_Test.java @@ -96,11 +96,11 @@ public void elf64_header() { // offset into ELF file assertThat(header.programHeaderTableOffset()) - .isEqualTo(new ElfOffsetAny<>(64L)); + .isEqualTo(new ElfOffset<>(64L)); // offset into ELF file assertThat(header.sectionHeaderTableOffset()) - .isEqualTo(new ElfOffsetAny<>(14744L)); + .isEqualTo(new ElfOffset<>(14744L)); // flags assertThat(header.flags()) @@ -162,7 +162,7 @@ public void elf64_section_header() { .isEqualTo(new ElfAddress<>(0x1050L)); assertThat(textSection.fileOffset()) - .isEqualTo(new ElfOffsetAny<>(0x1050L)); + .isEqualTo(new ElfOffset<>(0x1050L)); assertThat(textSection.size()) .isEqualTo(0x181); @@ -265,7 +265,7 @@ void elf64_segment_header() { .isEqualTo(ElfSegmentType.LOAD); assertThat(textSegment.fileOffset()) - .isEqualTo(new ElfOffsetAny<>(0x0000000000001000L)); + .isEqualTo(new ElfOffset<>(0x0000000000001000L)); assertThat(textSegment.virtualAddress()) .isEqualTo(new ElfAddress<>(0x0000000000001000L)); assertThat(textSegment.physicalAddress())