Skip to content

Commit

Permalink
Remove ElfOffsetAny
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Chwedczuk committed Dec 19, 2021
1 parent f821a1a commit 3f662e3
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public NativeWordType type() {

@Override
public ElfOffset<Integer> zeroOffset() {
return new ElfOffsetAny<>(0);
return new ElfOffset<>(0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public NativeWordType type() {

@Override
public ElfOffset<Long> zeroOffset() {
return new ElfOffsetAny<>(0L);
return new ElfOffset<>(0L);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import static java.util.Objects.requireNonNull;

public abstract class ElfOffset<
public class ElfOffset<
NATIVE_WORD extends Number & Comparable<NATIVE_WORD>
>
{
Expand All @@ -16,8 +16,6 @@ public ElfOffset(NATIVE_WORD fileOffset) {

public NATIVE_WORD value() { return fileOffset; }

protected abstract ElfOffset<NATIVE_WORD> mkFileOffset(NATIVE_WORD value);

public boolean isNull() {
return (fileOffset.longValue() == 0);
}
Expand All @@ -43,7 +41,7 @@ public boolean isBeforeOrAt(ElfOffset<NATIVE_WORD> fileOffset) {
public ElfOffset<NATIVE_WORD> 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);
Expand All @@ -69,5 +67,7 @@ public int hashCode() {
}

@Override
public abstract String toString();
public String toString() {
return String.format("0x%016x", value().longValue());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -64,7 +63,7 @@ public short readUnsignedShort() {
public ElfOffset<Long> readOffset64() {
byte[] addressBytes = readNext(8);
long address = endianness.toUnsignedLong(addressBytes);
return new ElfOffsetAny<>(address);
return new ElfOffset<>(address);
}

public int readUnsignedInt() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public StructuredFile32(ElfFile<Integer> file, ElfOffset<Integer> offset) {

@Override
protected ElfOffset<Integer> mkOffset(long offset) {
return new ElfOffsetAny<>(Math.toIntExact(offset));
return new ElfOffset<>(Math.toIntExact(offset));
}

@Override
public ElfOffset<Integer> readOffset() {
byte[] addressBytes = readNext(4);
int address = endianness.toUnsignedInt(addressBytes);
return new ElfOffsetAny<>(address);
return new ElfOffset<>(address);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long> {
Expand All @@ -25,14 +24,14 @@ public StructuredFile64(ElfFile<Long> file, ElfOffset<Long> offset) {

@Override
protected ElfOffset<Long> mkOffset(long offset) {
return new ElfOffsetAny<>(offset);
return new ElfOffset<>(offset);
}

@Override
public ElfOffset<Long> readOffset() {
byte[] addressBytes = readNext(8);
long address = endianness.toUnsignedLong(addressBytes);
return new ElfOffsetAny<>(address);
return new ElfOffset<>(address);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 3f662e3

Please sign in to comment.