Merge branch 'GP-0_ryanmkurtz_PR-5154_Gravelbones_omf_select'

This commit is contained in:
Ryan Kurtz
2023-04-03 06:37:03 -04:00
5 changed files with 119 additions and 91 deletions
@@ -27,6 +27,10 @@ public class OmfCommentRecord extends OmfRecord {
public static final byte COMMENT_CLASS_LIBMOD = (byte) 0xA3;
// Default library cmd
public static final byte COMMENT_CLASS_DEFAULT_LIBRARY = (byte) 0x9F;
// Watcom compile parameters
public static final byte COMMENT_CLASS_WATCOM_SETTINGS = (byte) 0x9b;
// Microsoft compile parameters
public static final byte COMMENT_CLASS_MICROSOFT_SETTINGS = (byte) 0x9d;
private byte commentType;
private byte commentClass;
@@ -37,6 +37,7 @@ public class OmfFileHeader extends OmfRecord {
private ArrayList<OmfFixupRecord> fixup = new ArrayList<OmfFixupRecord>();
private ArrayList<OmfSegmentHeader> extraSeg = null; // Holds implied segments that don't have official header record
// private OmfModuleEnd endModule = null;
private boolean format16bit;
public OmfFileHeader(BinaryReader reader) throws IOException {
readRecordHeader(reader);
@@ -65,7 +66,7 @@ public class OmfFileHeader extends OmfRecord {
* @return the string identifying the architecture this object was compiled for
*/
public String getMachineName() {
return "i386"; // This is the only possibility
return format16bit ? "16bit" : "32bit";
}
/**
@@ -250,13 +251,13 @@ public class OmfFileHeader extends OmfRecord {
* Scan the object file, for the main header and comment records. Other records are parsed but not saved
* @param reader is the byte stream
* @param monitor is checked for cancellation
* @param initialCommentsOnly is true if we only want to scan the header and the initial comments,
* @param fastscan is true if we only want to scan the header until first seghead,
* @return the header record
* @throws IOException for problems reading program data
* @throws OmfException for malformed records
*/
public static OmfFileHeader scan(BinaryReader reader, TaskMonitor monitor,
boolean initialCommentsOnly) throws IOException, OmfException {
public static OmfFileHeader scan(BinaryReader reader, TaskMonitor monitor, boolean fastscan)
throws IOException, OmfException {
OmfRecord record = OmfRecord.readRecord(reader);
if (!(record instanceof OmfFileHeader)) {
throw new OmfException("Object file does not start with proper header");
@@ -274,16 +275,26 @@ public class OmfFileHeader extends OmfRecord {
}
if (record instanceof OmfCommentRecord comment) {
byte commentClass = comment.getCommentClass();
if (commentClass == OmfCommentRecord.COMMENT_CLASS_TRANSLATOR) {
header.translator = comment.getValue();
}
else if (commentClass == OmfCommentRecord.COMMENT_CLASS_LIBMOD) {
header.libModuleName = comment.getValue();
switch (comment.getCommentClass()) {
case OmfCommentRecord.COMMENT_CLASS_TRANSLATOR:
header.translator = comment.getValue();
break;
case OmfCommentRecord.COMMENT_CLASS_LIBMOD:
header.libModuleName = comment.getValue();
break;
case OmfCommentRecord.COMMENT_CLASS_WATCOM_SETTINGS:
header.translator = "Watcom";
break;
case OmfCommentRecord.COMMENT_CLASS_MICROSOFT_SETTINGS:
header.translator = "Microsoft";
break;
}
}
else if (initialCommentsOnly) {
break;
else if (record instanceof OmfSegmentHeader head) {
header.format16bit = head.is16Bit();
if (fastscan) {
break;
}
}
}
return header;
@@ -206,6 +206,13 @@ public class OmfSegmentHeader extends OmfRecord {
return (segAttr >> 2) & 0x7;
}
/**
* @return if 16 or 32 bit segments are used
*/
public boolean is16Bit() {
return (segAttr & 1) == 0;
}
/**
* @return true if this block uses filler other than zero bytes
*/
@@ -70,6 +70,12 @@ public class OmfLoader extends AbstractProgramWrapperLoader {
if (record.startsWith("CodeGear")) {
return "codegearcpp";
}
if (record.equals("MS C")) {
return "windows";
}
if (record.startsWith("Watcom")) {
return "watcom";
}
return null;
}
@@ -156,7 +162,7 @@ public class OmfLoader extends AbstractProgramWrapperLoader {
message = "Unable to process relocation at " + addr + " with type 0x" +
Integer.toHexString(type);
program.getBookmarkManager()
.setBookmark(addr, BookmarkType.ERROR, "Relocations", message);
.setBookmark(addr, BookmarkType.ERROR, "Relocations", message);
}
else {
message = "Badly broken relocation";
@@ -238,10 +244,9 @@ public class OmfLoader extends AbstractProgramWrapperLoader {
targetAddr += subrec.getTargetDisplacement();
locationType = subrec.getLocationType();
OmfSegmentHeader seg =
header.resolveSegment(fixup.getDataBlock().getSegmentIndex());
header.resolveSegment(fixup.getDataBlock().getSegmentIndex());
locAddress = seg.getAddress(language)
.add(
fixup.getDataBlock().getDataOffset() +
.add(fixup.getDataBlock().getDataOffset() +
subrec.getDataRecordOffset());
if (locAddress == null) {
log.appendMsg("Couldn't find address for fixup");
@@ -249,74 +254,74 @@ public class OmfLoader extends AbstractProgramWrapperLoader {
}
finalvalue = targetAddr;
switch (locationType) {
case 0: // Low-order byte
origbytes = new byte[1];
memory.getBytes(locAddress, origbytes);
if (subrec.isSegmentRelative()) {
finalvalue += origbytes[0];
}
else {
finalvalue -= (locAddress.getOffset() + 1);
}
memory.setByte(locAddress, (byte) finalvalue);
break;
case 1: // 16-bit offset
case 5: // 16-bit loader-resolved offset (treated same as 1)
origbytes = new byte[2];
memory.getBytes(locAddress, origbytes);
if (subrec.isSegmentRelative()) {
finalvalue += converter.getShort(origbytes);
}
else {
finalvalue -= (locAddress.getOffset() + 2);
}
memory.setShort(locAddress, (short) finalvalue);
break;
case 2: // 16-bit base -- logical segment base (selector)
if (!subrec.isSegmentRelative()) {
// Segment can't be self relative
relocationError(program, log, locAddress, locationType);
continue;
}
origbytes = new byte[2];
memory.getBytes(locAddress, origbytes);
finalvalue += converter.getShort(origbytes) << 4;
finalvalue >>= 4; // Convert address to segment
memory.setShort(locAddress, (short) finalvalue);
break;
case 3: // 32-bit far pointer (16-bit segment:16-bit offset)
if (!subrec.isSegmentRelative()) {
// Far can't be self relative
relocationError(program, log, locAddress, locationType);
continue;
}
origbytes = new byte[4];
memory.getBytes(locAddress, origbytes);
finalvalue += converter.getInt(origbytes);
// Convert to segment:offset in 64K blocks
finalvalue =
((finalvalue & 0xffff0000L) << 12) | (finalvalue & 0xffff);
memory.setInt(locAddress, (int) finalvalue);
break;
// case 11: // 48-bit far pointer (16-bit segment:32-bit offset)
case 4: // High-order byte (high byte of 16-bit offset)
case 9: // 32-bit offset
case 13: // 32-bit loader-resolved offset (treated same as 9)
origbytes = new byte[4];
memory.getBytes(locAddress, origbytes);
if (subrec.isSegmentRelative()) {
case 0: // Low-order byte
origbytes = new byte[1];
memory.getBytes(locAddress, origbytes);
if (subrec.isSegmentRelative()) {
finalvalue += origbytes[0];
}
else {
finalvalue -= (locAddress.getOffset() + 1);
}
memory.setByte(locAddress, (byte) finalvalue);
break;
case 1: // 16-bit offset
case 5: // 16-bit loader-resolved offset (treated same as 1)
origbytes = new byte[2];
memory.getBytes(locAddress, origbytes);
if (subrec.isSegmentRelative()) {
finalvalue += converter.getShort(origbytes);
}
else {
finalvalue -= (locAddress.getOffset() + 2);
}
memory.setShort(locAddress, (short) finalvalue);
break;
case 2: // 16-bit base -- logical segment base (selector)
if (!subrec.isSegmentRelative()) {
// Segment can't be self relative
relocationError(program, log, locAddress, locationType);
continue;
}
origbytes = new byte[2];
memory.getBytes(locAddress, origbytes);
finalvalue += converter.getShort(origbytes) << 4;
finalvalue >>= 4; // Convert address to segment
memory.setShort(locAddress, (short) finalvalue);
break;
case 3: // 32-bit far pointer (16-bit segment:16-bit offset)
if (!subrec.isSegmentRelative()) {
// Far can't be self relative
relocationError(program, log, locAddress, locationType);
continue;
}
origbytes = new byte[4];
memory.getBytes(locAddress, origbytes);
finalvalue += converter.getInt(origbytes);
}
else {
finalvalue -= (locAddress.getOffset() + 4);
}
memory.setInt(locAddress, (int) finalvalue);
break;
default:
log.appendMsg("Unsupported relocation type " +
Integer.toString(locationType) + " at 0x" +
Long.toHexString(locAddress.getOffset()));
break;
// Convert to segment:offset in 64K blocks
finalvalue =
((finalvalue & 0xffff0000L) << 12) | (finalvalue & 0xffff);
memory.setInt(locAddress, (int) finalvalue);
break;
// case 11: // 48-bit far pointer (16-bit segment:32-bit offset)
case 4: // High-order byte (high byte of 16-bit offset)
case 9: // 32-bit offset
case 13: // 32-bit loader-resolved offset (treated same as 9)
origbytes = new byte[4];
memory.getBytes(locAddress, origbytes);
if (subrec.isSegmentRelative()) {
finalvalue += converter.getInt(origbytes);
}
else {
finalvalue -= (locAddress.getOffset() + 4);
}
memory.setInt(locAddress, (int) finalvalue);
break;
default:
log.appendMsg("Unsupported relocation type " +
Integer.toString(locationType) + " at 0x" +
Long.toHexString(locAddress.getOffset()));
break;
}
}
catch (MemoryAccessException e) {
@@ -334,7 +339,7 @@ public class OmfLoader extends AbstractProgramWrapperLoader {
long[] values = new long[1];
values[0] = finalvalue;
program.getRelocationTable()
.add(locAddress, Status.APPLIED, locationType, values, origbytes, null);
.add(locAddress, Status.APPLIED, locationType, values, origbytes, null);
}
}
}
@@ -371,7 +376,7 @@ public class OmfLoader extends AbstractProgramWrapperLoader {
if (segmentSize == 0) {
continue;
}
if (segment.hasNonZeroData()) {
MemoryBlockUtils.createInitializedBlock(program, false, segment.getName(),
segmentAddr, segment.getRawDataStream(reader, log), segmentSize, "", "",
@@ -548,7 +553,8 @@ public class OmfLoader extends AbstractProgramWrapperLoader {
Map<String, OmfSymbol> publicSymbols = header.getPublicSymbols()
.stream()
.flatMap(symbolRec -> symbolRec.getSymbols().stream())
.collect(Collectors.toMap(sym -> sym.getName(), java.util.function.Function.identity()));
.collect(
Collectors.toMap(sym -> sym.getName(), java.util.function.Function.identity()));
monitor.setMessage("Creating External Symbols");