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
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
///
|
|
|
|
|
/// 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
|
|
|
|
|
/// within (the ProDOS file type is supposed to do that).
|
|
|
|
|
///
|
2020-06-24 00:21:18 +00:00
|
|
|
|
/// References:
|
2020-06-26 00:30:44 +00:00
|
|
|
|
/// - (OMF "v0" is documented in an Orca/M manual?)
|
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,
|
|
|
|
|
/// http://nparker.llx.com/a2/loader.html . Among other things it documents ExpressLoad
|
|
|
|
|
/// segments, something Apple apparently never did.
|
|
|
|
|
/// - 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
|
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
|
// - has an overall file type (load, object, RTL)
|
|
|
|
|
// - determine with a prioritized series of "could this be ____" checks
|
|
|
|
|
// - holds list of OmfSegment
|
|
|
|
|
// - has a list of warnings and errors that arose during parsing
|
|
|
|
|
// - holds on to byte[] with data
|
|
|
|
|
// OmfSegment:
|
|
|
|
|
// - header (common data, plus name/value dict with version-specific fields for display)
|
|
|
|
|
// - ref back to OmfFile for byte[] access?
|
|
|
|
|
// - list of OmfRecord
|
|
|
|
|
// - file-type-specific stuff can be generated and cached in second pass, e.g.
|
|
|
|
|
// generate a full relocation dictionary for load files (can't do this until we
|
|
|
|
|
// know the overall file type, which we can't know until all segments have been
|
|
|
|
|
// processed a bit)
|
2020-06-26 00:30:44 +00:00
|
|
|
|
|
|
|
|
|
private byte[] mFileData;
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
|
Foreign // not OMF, or not IIgs OMF
|
|
|
|
|
}
|
|
|
|
|
public FileKind OmfFileKind { get; private set; }
|
|
|
|
|
|
|
|
|
|
private bool mIsDamaged;
|
|
|
|
|
|
|
|
|
|
private string mDamageMsg = string.Empty;
|
|
|
|
|
|
|
|
|
|
private List<OmfSegment> mSegmentList = new List<OmfSegment>();
|
|
|
|
|
public List<OmfSegment> SegmentList {
|
|
|
|
|
get { return mSegmentList; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
|
|
|
|
OmfFileKind = FileKind.Unknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Analyze() {
|
|
|
|
|
OmfSegment.ParseResult result = DoAnalyze(false);
|
|
|
|
|
if (result == OmfSegment.ParseResult.IsLibrary ||
|
|
|
|
|
result == OmfSegment.ParseResult.Failure) {
|
|
|
|
|
DoAnalyze(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OmfSegment.ParseResult DoAnalyze(bool parseAsLibrary) {
|
|
|
|
|
bool first = true;
|
|
|
|
|
int offset = 0;
|
|
|
|
|
int len = mFileData.Length;
|
|
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
|
OmfSegment.ParseResult result =
|
|
|
|
|
OmfSegment.ParseSegment(mFileData, offset, parseAsLibrary, out OmfSegment seg);
|
|
|
|
|
if (result == OmfSegment.ParseResult.Failure) {
|
|
|
|
|
// parsing failed; reject file or stop early
|
|
|
|
|
if (first) {
|
|
|
|
|
OmfFileKind = FileKind.Foreign;
|
|
|
|
|
} else {
|
|
|
|
|
mIsDamaged = true;
|
|
|
|
|
mDamageMsg = string.Format("File may be damaged; ignoring last {0} bytes",
|
|
|
|
|
mFileData.Length - offset);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
} else if (result == OmfSegment.ParseResult.IsLibrary) {
|
|
|
|
|
// Need to start over in library mode.
|
|
|
|
|
Debug.WriteLine("Restarting in library mode");
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug.Assert(seg.FileLength > 0);
|
|
|
|
|
mSegmentList.Add(seg);
|
|
|
|
|
offset += seg.FileLength;
|
|
|
|
|
len -= seg.FileLength;
|
|
|
|
|
Debug.Assert(len >= 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug.WriteLine("Num segments = " + mSegmentList.Count);
|
|
|
|
|
return OmfSegment.ParseResult.Success;
|
|
|
|
|
}
|
2020-06-24 00:21:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|