2020-06-24 00:21:18 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2020 faddenSoft
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-06-26 00:30:44 +00:00
|
|
|
|
using System.Diagnostics;
|
2020-06-24 00:21:18 +00:00
|
|
|
|
|
2020-06-29 03:50:41 +00:00
|
|
|
|
using Asm65;
|
|
|
|
|
|
2020-06-24 00:21:18 +00:00
|
|
|
|
namespace SourceGen.Tools.Omf {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Apple IIgs OMF file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
2020-06-26 00:30:44 +00:00
|
|
|
|
/// OMF files are a series of segments. There is no file header or identifying information.
|
|
|
|
|
/// In some cases the length is expected to be a multiple of 512 bytes, in others it isn't.
|
2020-06-28 01:06:26 +00:00
|
|
|
|
/// Each segment is comprised of a header, followed by a series of records.
|
2020-06-26 00:30:44 +00:00
|
|
|
|
///
|
|
|
|
|
/// There's no structural limitation on mixing and matching segments, whether different
|
|
|
|
|
/// versions or different types. The file format provides a structure in which various
|
|
|
|
|
/// things may be stored, but does not provide a way to tell an observer what is contained
|
2020-06-28 01:06:26 +00:00
|
|
|
|
/// within (the ProDOS file type is supposed to do that). A given file may be a Load
|
|
|
|
|
/// file (handled by the System Loader), Object file (fed to a linker), Library file
|
|
|
|
|
/// (also fed to a linker), or Run-Time Library (used by both the linker and the loader).
|
2020-06-26 00:30:44 +00:00
|
|
|
|
///
|
2020-06-24 00:21:18 +00:00
|
|
|
|
/// References:
|
2020-06-28 01:06:26 +00:00
|
|
|
|
/// - IIgs Orca/M 2.0 manual. Appendix B documents OMF v0, v1, and v2.1 Load files.
|
|
|
|
|
/// (This is included with Opus ][.)
|
2020-06-24 00:21:18 +00:00
|
|
|
|
/// - "Apple IIgs Programmer's Workshop Reference". Chapter 7, page 228, describes
|
|
|
|
|
/// OMF v1.0 and v2.0.
|
|
|
|
|
/// - "Apple IIgs GS/OS Reference, for GS/OS System Software Version 5.0 and later".
|
|
|
|
|
/// Appendix F describes OMF v2.1, and Chapter 8 has some useful information about
|
2020-06-26 00:30:44 +00:00
|
|
|
|
/// how the loader works (e.g. page 205).
|
2020-06-24 00:21:18 +00:00
|
|
|
|
/// - "Undocumented Secrets of the Apple IIGS System Loader" by Neil Parker,
|
2020-06-26 23:58:42 +00:00
|
|
|
|
/// http://nparker.llx.com/a2/loader.html . Among other things it documents the
|
|
|
|
|
/// contents of ExpressLoad segments, which I haven't found in an official reference.
|
2020-06-24 00:21:18 +00:00
|
|
|
|
/// - Apple IIgs Tech Note #66, "ExpressLoad Philosophy".
|
|
|
|
|
///
|
|
|
|
|
/// Related:
|
|
|
|
|
/// - https://www.brutaldeluxe.fr/products/crossdevtools/omfanalyzer/
|
|
|
|
|
/// - https://github.com/fadden/ciderpress/blob/master/reformat/Disasm.cpp
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public class OmfFile {
|
2020-06-26 00:30:44 +00:00
|
|
|
|
public const int MIN_FILE_SIZE = OmfSegment.MIN_HEADER_V0;
|
|
|
|
|
public const int MAX_FILE_SIZE = (1 << 24) - 1; // cap at 16MB
|
2020-06-24 00:21:18 +00:00
|
|
|
|
|
2020-06-26 00:30:44 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Overall file contents, determined by analysis.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public enum FileKind {
|
|
|
|
|
Unknown = 0,
|
|
|
|
|
Load, // loadable files
|
|
|
|
|
Object, // output of assembler/compiler, before linking
|
|
|
|
|
Library, // static code library
|
|
|
|
|
RunTimeLibrary, // dynamic shared library
|
2020-06-26 23:58:42 +00:00
|
|
|
|
Indeterminate, // valid OMF, but type is indeterminate
|
2020-06-26 00:30:44 +00:00
|
|
|
|
Foreign // not OMF, or not IIgs OMF
|
|
|
|
|
}
|
|
|
|
|
public FileKind OmfFileKind { get; private set; }
|
|
|
|
|
|
2020-06-29 03:50:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// File data. Files must be loaded completely into memory.
|
|
|
|
|
/// </summary>
|
2020-06-28 01:06:26 +00:00
|
|
|
|
private byte[] mFileData;
|
2020-06-26 00:30:44 +00:00
|
|
|
|
|
2020-06-29 03:50:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// List of segments.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<OmfSegment> SegmentList { get; private set; } = new List<OmfSegment>();
|
2020-06-26 00:30:44 +00:00
|
|
|
|
|
2020-06-29 03:50:41 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// List of strings to show in the message window.
|
|
|
|
|
/// </summary>
|
2020-06-26 23:58:42 +00:00
|
|
|
|
public List<string> MessageList { get; private set; } = new List<string>();
|
|
|
|
|
|
2020-06-26 00:30:44 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="fileData">File to analyze.</param>
|
|
|
|
|
public OmfFile(byte[] fileData) {
|
|
|
|
|
Debug.Assert(fileData.Length >= MIN_FILE_SIZE && fileData.Length <= MAX_FILE_SIZE);
|
|
|
|
|
mFileData = fileData;
|
|
|
|
|
|
2020-06-30 01:29:18 +00:00
|
|
|
|
// Set to Unknown until analysis completes.
|
2020-06-26 00:30:44 +00:00
|
|
|
|
OmfFileKind = FileKind.Unknown;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 18:56:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Analyzes the contents of an OMF file.
|
|
|
|
|
/// </summary>
|
2020-06-28 01:06:26 +00:00
|
|
|
|
public void Analyze(Formatter formatter) {
|
|
|
|
|
OmfSegment.ParseResult result = DoAnalyze(formatter, false);
|
2020-06-26 00:30:44 +00:00
|
|
|
|
if (result == OmfSegment.ParseResult.IsLibrary ||
|
|
|
|
|
result == OmfSegment.ParseResult.Failure) {
|
2020-06-26 23:58:42 +00:00
|
|
|
|
// Failed; try again as a library.
|
|
|
|
|
List<string> firstFail = new List<string>(MessageList);
|
2020-06-28 01:06:26 +00:00
|
|
|
|
result = DoAnalyze(formatter, true);
|
2020-06-26 23:58:42 +00:00
|
|
|
|
if (result == OmfSegment.ParseResult.Failure) {
|
|
|
|
|
// Failed both ways. Report the failures from the non-library attempt.
|
|
|
|
|
MessageList = firstFail;
|
|
|
|
|
}
|
2020-06-26 00:30:44 +00:00
|
|
|
|
}
|
2020-06-28 18:56:51 +00:00
|
|
|
|
|
|
|
|
|
OmfFileKind = DetermineFileKind();
|
|
|
|
|
Debug.WriteLine("File kind is " + OmfFileKind);
|
2020-06-29 03:50:41 +00:00
|
|
|
|
|
|
|
|
|
if (OmfFileKind == FileKind.Load) {
|
|
|
|
|
GenerateRelocDicts();
|
|
|
|
|
}
|
2020-06-26 00:30:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 18:56:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Analyzes the contents of an OMF file as a library or non-library.
|
|
|
|
|
/// </summary>
|
2020-06-28 01:06:26 +00:00
|
|
|
|
private OmfSegment.ParseResult DoAnalyze(Formatter formatter, bool parseAsLibrary) {
|
2020-06-26 00:30:44 +00:00
|
|
|
|
bool first = true;
|
|
|
|
|
int offset = 0;
|
|
|
|
|
int len = mFileData.Length;
|
|
|
|
|
|
2020-06-26 23:58:42 +00:00
|
|
|
|
List<string> msgs = new List<string>();
|
|
|
|
|
|
2020-06-26 00:30:44 +00:00
|
|
|
|
while (len > 0) {
|
2020-06-28 01:06:26 +00:00
|
|
|
|
OmfSegment.ParseResult result = OmfSegment.ParseHeader(mFileData, offset,
|
2020-06-26 23:58:42 +00:00
|
|
|
|
parseAsLibrary, msgs, out OmfSegment seg);
|
2020-06-28 01:06:26 +00:00
|
|
|
|
if (result == OmfSegment.ParseResult.Success) {
|
|
|
|
|
if (!seg.ParseBody(formatter, msgs)) {
|
|
|
|
|
OmfSegment.AddErrorMsg(msgs, offset, "parsing of segment " +
|
|
|
|
|
seg.SegNum + " '" + seg.SegName + "' incomplete");
|
|
|
|
|
//result = OmfSegment.ParseResult.Failure;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-26 23:58:42 +00:00
|
|
|
|
|
|
|
|
|
MessageList.Clear();
|
|
|
|
|
foreach (string str in msgs) {
|
|
|
|
|
MessageList.Add(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result == OmfSegment.ParseResult.IsLibrary) {
|
2020-06-26 00:30:44 +00:00
|
|
|
|
// Need to start over in library mode.
|
|
|
|
|
Debug.WriteLine("Restarting in library mode");
|
|
|
|
|
return result;
|
2020-06-26 23:58:42 +00:00
|
|
|
|
} else if (result == OmfSegment.ParseResult.Failure) {
|
|
|
|
|
// Could be a library we failed to parse, could be a totally bad file.
|
|
|
|
|
// If we were on the first segment, fail immediately so we can retry as
|
|
|
|
|
// library. If not, it's probably not a library (assuming the Library
|
|
|
|
|
// Dictionary segment appears first), but rather a partially-bad OMF.
|
|
|
|
|
if (first) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2020-06-26 00:30:44 +00:00
|
|
|
|
}
|
2020-06-26 23:58:42 +00:00
|
|
|
|
first = false;
|
2020-06-26 00:30:44 +00:00
|
|
|
|
|
|
|
|
|
Debug.Assert(seg.FileLength > 0);
|
2020-06-28 01:06:26 +00:00
|
|
|
|
|
2020-06-29 03:50:41 +00:00
|
|
|
|
SegmentList.Add(seg);
|
2020-06-26 00:30:44 +00:00
|
|
|
|
offset += seg.FileLength;
|
|
|
|
|
len -= seg.FileLength;
|
|
|
|
|
Debug.Assert(len >= 0);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 03:50:41 +00:00
|
|
|
|
Debug.WriteLine("Num segments = " + SegmentList.Count);
|
2020-06-26 00:30:44 +00:00
|
|
|
|
return OmfSegment.ParseResult.Success;
|
|
|
|
|
}
|
2020-06-28 18:56:51 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Analyzes the file to determine the file kind.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private FileKind DetermineFileKind() {
|
2020-06-29 03:50:41 +00:00
|
|
|
|
if (SegmentList.Count == 0) {
|
2020-06-28 18:56:51 +00:00
|
|
|
|
// Couldn't find a single valid segment, this is not OMF.
|
|
|
|
|
return FileKind.Foreign;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The rules:
|
|
|
|
|
// * Load files may contain any kind of segment except LibraryDict. Their
|
|
|
|
|
// segments must be LCONST/DS followed by relocation records
|
|
|
|
|
// (INTERSEG, cINTERSEG, RELOC, cRELOC, SUPER).
|
2020-06-29 03:50:41 +00:00
|
|
|
|
// * Object files have Code, Data, and DP/Stack segments, and may not contain
|
|
|
|
|
// relocation records or ENTRY.
|
2020-06-28 18:56:51 +00:00
|
|
|
|
// * Library files are like Object files, but start with a LibraryDict segment.
|
|
|
|
|
// * Run-Time Library files have an initial segment with ENTRY records. (These
|
|
|
|
|
// are not supported because I've never actually seen one... the few files I've
|
|
|
|
|
// found with the RTL filetype ($B4) did not have any ENTRY records.)
|
|
|
|
|
|
|
|
|
|
bool maybeLoad = true;
|
|
|
|
|
bool maybeObject = true;
|
|
|
|
|
bool maybeLibrary = true;
|
|
|
|
|
|
2020-06-29 03:50:41 +00:00
|
|
|
|
foreach (OmfSegment omfSeg in SegmentList) {
|
2020-06-28 18:56:51 +00:00
|
|
|
|
switch (omfSeg.Kind) {
|
|
|
|
|
case OmfSegment.SegmentKind.Code:
|
|
|
|
|
case OmfSegment.SegmentKind.Data:
|
2020-06-29 03:50:41 +00:00
|
|
|
|
case OmfSegment.SegmentKind.DpStack:
|
2020-06-28 18:56:51 +00:00
|
|
|
|
// Allowed anywhere.
|
|
|
|
|
break;
|
|
|
|
|
case OmfSegment.SegmentKind.JumpTable:
|
|
|
|
|
case OmfSegment.SegmentKind.PathName:
|
|
|
|
|
case OmfSegment.SegmentKind.Init:
|
|
|
|
|
case OmfSegment.SegmentKind.AbsoluteBank:
|
2020-06-29 03:50:41 +00:00
|
|
|
|
// Only allowed in Load.
|
2020-06-28 18:56:51 +00:00
|
|
|
|
maybeObject = maybeLibrary = false;
|
|
|
|
|
break;
|
|
|
|
|
case OmfSegment.SegmentKind.LibraryDict:
|
|
|
|
|
maybeLoad = false;
|
|
|
|
|
maybeObject = false;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Debug.Assert(false, "Unexpected segment kind " + omfSeg.Kind);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Initial screening complete. Dig into the segment records to see if they're
|
|
|
|
|
// compatible.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
if (maybeLoad) {
|
2020-06-29 03:50:41 +00:00
|
|
|
|
foreach (OmfSegment omfSeg in SegmentList) {
|
2020-06-28 18:56:51 +00:00
|
|
|
|
if (!omfSeg.CheckRecords_LoadFile()) {
|
|
|
|
|
maybeLoad = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (maybeLoad) {
|
|
|
|
|
return FileKind.Load;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (maybeLibrary || maybeObject) {
|
2020-06-29 03:50:41 +00:00
|
|
|
|
foreach (OmfSegment omfSeg in SegmentList) {
|
2020-06-28 18:56:51 +00:00
|
|
|
|
if (!omfSeg.CheckRecords_ObjectOrLib()) {
|
|
|
|
|
maybeLibrary = maybeObject = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (maybeObject) {
|
|
|
|
|
return FileKind.Object;
|
|
|
|
|
} else {
|
|
|
|
|
return FileKind.Library;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FileKind.Indeterminate;
|
|
|
|
|
}
|
2020-06-29 03:50:41 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generates relocation dictionaries for all code/data/init segments.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void GenerateRelocDicts() {
|
|
|
|
|
Debug.Assert(OmfFileKind == FileKind.Load);
|
|
|
|
|
|
|
|
|
|
foreach (OmfSegment omfSeg in SegmentList) {
|
|
|
|
|
if (omfSeg.Kind == OmfSegment.SegmentKind.Code ||
|
|
|
|
|
omfSeg.Kind == OmfSegment.SegmentKind.Data ||
|
|
|
|
|
omfSeg.Kind == OmfSegment.SegmentKind.Init) {
|
|
|
|
|
omfSeg.GenerateRelocDict();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-24 00:21:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|