Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-2045 update COFF section header structure datatypes for binary markup use.

ghidra132e75f4f1b4a
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader.java
index 243f4153d1..457584b4fc 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader.java
@@ -65,8 +65,8 @@ public class CoffSectionHeader implements StructConverter {
s_scnptr = reader.readNextInt();
s_relptr = reader.readNextInt();
s_lnnoptr = reader.readNextInt();
- s_nreloc = reader.readNextShort() & 0xffff;
- s_nlnno = reader.readNextShort() & 0xffff;
+ s_nreloc = reader.readNextUnsignedShort();
+ s_nlnno = reader.readNextUnsignedShort();
s_flags = reader.readNextInt();
s_reserved = 0;
s_page = 0;
@@ -306,14 +306,6 @@ public class CoffSectionHeader implements StructConverter {
struct.add(WORD, "s_nreloc", null);
struct.add(WORD, "s_nlnno", null);
struct.add(DWORD, "s_flags", null);
- if (_header.getMagic() == CoffMachineType.TICOFF1MAGIC) {
- struct.add(BYTE, "s_reserved", null);
- struct.add(BYTE, "s_page", null);
- }
- else if (_header.getMagic() == CoffMachineType.TICOFF2MAGIC) {
- struct.add(WORD, "s_reserved", null);
- struct.add(WORD, "s_page", null);
- }
return struct;
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader1.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader1.java
index f9de1260c4..2952591cfa 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader1.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader1.java
@@ -18,6 +18,8 @@ package ghidra.app.util.bin.format.coff;
import java.io.IOException;
import ghidra.app.util.bin.BinaryReader;
+import ghidra.app.util.bin.StructConverterUtil;
+import ghidra.program.model.data.*;
/**
* A 0x28 byte COFF section header
@@ -37,11 +39,30 @@ class CoffSectionHeader1 extends CoffSectionHeader {
s_scnptr = reader.readNextInt();
s_relptr = reader.readNextInt();
s_lnnoptr = reader.readNextInt();
- s_nreloc = reader.readNextShort() & 0xffff;
- s_nlnno = reader.readNextShort() & 0xffff;
- s_flags = reader.readNextShort() & 0xffff;
- s_reserved = reader.readNextByte();
- s_page = reader.readNextByte();
+ s_nreloc = reader.readNextUnsignedShort();
+ s_nlnno = reader.readNextUnsignedShort();
+ s_flags = reader.readNextUnsignedShort();
+ s_reserved = (short) (reader.readNextByte() & 0xff);
+ s_page = (short) (reader.readNextByte() & 0xff);
+ }
+
+ @Override
+ public DataType toDataType() throws IOException {
+ Structure struct = new StructureDataType(StructConverterUtil.parseName(getClass()), 0);
+ struct.add(new ArrayDataType(ASCII, CoffConstants.SECTION_NAME_LENGTH, ASCII.getLength()),
+ "s_name", null);
+ struct.add(DWORD, "s_paddr", null);
+ struct.add(DWORD, "s_vaddr", null);
+ struct.add(DWORD, "s_size", null);
+ struct.add(DWORD, "s_scnptr", null);
+ struct.add(DWORD, "s_relptr", null);
+ struct.add(DWORD, "s_lnnoptr", null);
+ struct.add(WORD, "s_nreloc", null);
+ struct.add(WORD, "s_nlnno", null);
+ struct.add(WORD, "s_flags", null);
+ struct.add(BYTE, "s_reserved", null);
+ struct.add(BYTE, "s_page", null);
+ return struct;
}
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader2.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader2.java
index 45a6cdf6e8..b7e5a07698 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader2.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader2.java
@@ -18,6 +18,8 @@ package ghidra.app.util.bin.format.coff;
import java.io.IOException;
import ghidra.app.util.bin.BinaryReader;
+import ghidra.app.util.bin.StructConverterUtil;
+import ghidra.program.model.data.*;
/**
* A 0x30 byte COFF section header
@@ -44,4 +46,23 @@ class CoffSectionHeader2 extends CoffSectionHeader {
s_page = reader.readNextShort();
}
+ @Override
+ public DataType toDataType() throws IOException {
+ Structure struct = new StructureDataType(StructConverterUtil.parseName(getClass()), 0);
+ struct.add(new ArrayDataType(ASCII, CoffConstants.SECTION_NAME_LENGTH, ASCII.getLength()),
+ "s_name", null);
+ struct.add(DWORD, "s_paddr", null);
+ struct.add(DWORD, "s_vaddr", null);
+ struct.add(DWORD, "s_size", null);
+ struct.add(DWORD, "s_scnptr", null);
+ struct.add(DWORD, "s_relptr", null);
+ struct.add(DWORD, "s_lnnoptr", null);
+ struct.add(DWORD, "s_nreloc", null);
+ struct.add(DWORD, "s_nlnno", null);
+ struct.add(DWORD, "s_flags", null);
+ struct.add(WORD, "s_reserved", null);
+ struct.add(WORD, "s_page", null);
+ return struct;
+ }
+
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader3.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader3.java
index d6de3fd962..b5983edd04 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader3.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/coff/CoffSectionHeader3.java
@@ -18,6 +18,8 @@ package ghidra.app.util.bin.format.coff;
import java.io.IOException;
import ghidra.app.util.bin.BinaryReader;
+import ghidra.app.util.bin.StructConverterUtil;
+import ghidra.program.model.data.*;
/**
* A 0x2c byte COFF section header
@@ -37,14 +39,32 @@ class CoffSectionHeader3 extends CoffSectionHeader {
s_scnptr = reader.readNextInt();
s_relptr = reader.readNextInt();
s_lnnoptr = reader.readNextInt();
- s_nreloc = reader.readNextShort() & 0xffff;
- s_nlnno = reader.readNextShort() & 0xffff;
+ s_nreloc = reader.readNextUnsignedShort();
+ s_nlnno = reader.readNextUnsignedShort();
s_flags = reader.readNextInt();
- reader.readNextInt(); // section alignment, currently unused
+ reader.readNextUnsignedInt(); // s_align: section alignment, currently unused
s_reserved = 0;
- s_page = 0;
+ s_page = 0; // TODO: (short) (reader.readNextUnsignedByte() & 0xff) ?
+ }
+
+ @Override
+ public DataType toDataType() throws IOException {
+ Structure struct = new StructureDataType(StructConverterUtil.parseName(getClass()), 0);
+ struct.add(new ArrayDataType(ASCII, CoffConstants.SECTION_NAME_LENGTH, ASCII.getLength()),
+ "s_name", null);
+ struct.add(DWORD, "s_paddr", null);
+ struct.add(DWORD, "s_vaddr", null);
+ struct.add(DWORD, "s_size", null);
+ struct.add(DWORD, "s_scnptr", null);
+ struct.add(DWORD, "s_relptr", null);
+ struct.add(DWORD, "s_lnnoptr", null);
+ struct.add(WORD, "s_nreloc", null);
+ struct.add(WORD, "s_nlnno", null);
+ struct.add(DWORD, "s_flags", null);
+ struct.add(DWORD, "s_align", null);
+ return struct;
}
}