Stop using Apple's PEFBinaryFormat, use MultiversalInterfaces instead!

This commit is contained in:
Wolfgang Thaller 2019-09-08 20:54:09 +02:00
parent 8b7c52d993
commit fcf9548bff
6 changed files with 301 additions and 75 deletions

View File

@ -1,11 +1,9 @@
find_package(Boost COMPONENTS filesystem system REQUIRED)
add_executable(MakePEF MakePEF.cc rs6000.h PEF.h)
target_include_directories(MakePEF PRIVATE ${CMAKE_INSTALL_PREFIX}/include)
add_executable(MakeImport MakeImport.cc PEF.h)
target_link_libraries(MakeImport ResourceFiles ${Boost_LIBRARIES})
target_include_directories(MakeImport PRIVATE ${Boost_INCLUDE_DIR})
target_include_directories(MakeImport PRIVATE ${CMAKE_INSTALL_PREFIX}/include)
install(TARGETS MakePEF MakeImport RUNTIME DESTINATION bin)

View File

@ -99,8 +99,8 @@ void MakeImportLibrary(char *pefptr, size_t pefsize, fs::path dest, fs::path tmp
= (PEFSectionHeader*) (pefptr + kPEFFirstSectionHeaderOffset);
PEFSectionHeader *loaderHeader = NULL;
UInt16 n = containerHeader->sectionCount;
for(UInt16 i=0; i < n; i++)
uint16_t n = containerHeader->sectionCount;
for(uint16_t i=0; i < n; i++)
{
eswap(&sectionHeaders[i]);
if(sectionHeaders[i].sectionKind == kPEFLoaderSection)
@ -111,12 +111,12 @@ void MakeImportLibrary(char *pefptr, size_t pefsize, fs::path dest, fs::path tmp
= (PEFLoaderInfoHeader*) (pefptr + loaderHeader->containerOffset);
eswap(loaderInfoHeader);
UInt32 hashTableSize = 1;
UInt32 hashTablePower = loaderInfoHeader->exportHashTablePower;
uint32_t hashTableSize = 1;
uint32_t hashTablePower = loaderInfoHeader->exportHashTablePower;
while(hashTablePower--)
hashTableSize *= 2;
UInt32 nSymbols = loaderInfoHeader->exportedSymbolCount;
uint32_t nSymbols = loaderInfoHeader->exportedSymbolCount;
const char *symbols /* use char pointer to avoid packing issues */
= (pefptr + loaderHeader->containerOffset
@ -130,15 +130,15 @@ void MakeImportLibrary(char *pefptr, size_t pefsize, fs::path dest, fs::path tmp
/*const char *stringTableEnd = pefptr
+ loaderHeader->containerOffset
+ loaderInfoHeader->exportHashOffset;*/
vector< pair< const char *, UInt8 > > classesAndNamePtrs;
vector< pair< const char *, uint8_t > > classesAndNamePtrs;
for(UInt32 i=0; i < nSymbols; i++)
for(uint32_t i=0; i < nSymbols; i++)
{
PEFExportedSymbol *sym = (PEFExportedSymbol*) (symbols + 10*i);
eswap(sym);
UInt8 symclass = PEFExportedSymbolClass(sym->classAndName);
UInt32 nameoffset = PEFExportedSymbolNameOffset(sym->classAndName);
uint8_t symclass = PEFExportedSymbolClass(sym->classAndName);
uint32_t nameoffset = PEFExportedSymbolNameOffset(sym->classAndName);
const char *nameptr
= stringTable + nameoffset;
@ -147,10 +147,10 @@ void MakeImportLibrary(char *pefptr, size_t pefsize, fs::path dest, fs::path tmp
}
std::sort(classesAndNamePtrs.begin(), classesAndNamePtrs.end());
vector< pair<string, UInt8> > exportedSymbols;
for(UInt32 i=0; i < nSymbols; i++)
vector< pair<string, uint8_t> > exportedSymbols;
for(uint32_t i=0; i < nSymbols; i++)
{
UInt8 symclass = classesAndNamePtrs[i].second;
uint8_t symclass = classesAndNamePtrs[i].second;
const char *namestart = classesAndNamePtrs[i].first;
string name;
@ -174,7 +174,7 @@ void MakeImportLibrary(char *pefptr, size_t pefsize, fs::path dest, fs::path tmp
fs::ofstream expFile(stub_exp);
fs::ofstream sFile(stub_s);
sFile << "\t.toc\n";
for(UInt32 i=0; i< nSymbols; i++)
for(uint32_t i=0; i< nSymbols; i++)
{
string& sym = exportedSymbols[i].first;
if(exportedSymbols[i].second == kPEFTVectorSymbol)
@ -242,7 +242,7 @@ bool MakeImportLibraryMulti(fs::path path, fs::path libname)
CFragResourceMember *member = &(cfrg -> firstMember);
for(UInt16 i = 0; i < cfrg->memberCount; i++)
for(uint16_t i = 0; i < cfrg->memberCount; i++)
{
eswap(member);
string membername =

View File

@ -1,60 +1,13 @@
#ifndef PEF_H
#define PEF_H
/* Provide minimal definitions from ConfitionalMacros.h.
* ConditionalMacros.h can't deal with our host C compiler.
*/
#define __CONDITIONALMACROS__
#define TYPE_BOOL 1
#define EXTERN_API(x) x
#define EXTERN_API_C(x) x
#define CALLBACK_API(ret,name) ret (*name)
#define CALLBACK_API_C(ret,name) ret (*name)
#define ONEWORDINLINE(x)
#define TWOWORDINLINE(x,y)
#define THREEWORDINLINE(x,y,z)
#define FOUR_CHAR_CODE(x) (x)
#define PRAGMA_STRUCT_PACKPUSH 1
#include <stdint.h>
/* Can't use MacTypes.h (or Types.h) either,
* as older versions are hardcoded to 32 bit platforms.
*/
#define __MACTYPES__
#define __TYPES__
typedef uint8_t UInt8;
typedef int8_t SInt8;
typedef uint16_t UInt16;
typedef int16_t SInt16;
typedef uint32_t UInt32;
typedef int32_t SInt32;
typedef uint32_t OSType;
typedef int16_t OSErr;
typedef const unsigned char ConstStr63Param[64];
typedef const unsigned char ConstStr255Param[256];
typedef unsigned char Str255[256];
typedef unsigned char *StringPtr;
typedef char* Ptr;
typedef int16_t Boolean;
typedef void *LogicalAddress;
#define __FILES__
typedef struct {} FSSpec, *FSSpecPtr;
/* Definitions for PEF, from Apple's Universal Interfaces */
/* Definitions for PEF, from the Multiversal Interfaces */
#include "PEFBinaryFormat.h"
/* Deal with differences between versions of PEFBinaryFormat.h */
#ifndef PEFRelocComposeSetPosition_1st
#define PEFRelocComposeSetPosition_1st(fullOffset) \
( 0xA000 | ((UInt16) (((UInt32)(fullOffset)) >> 16) ) )
#define PEFRelocComposeSetPosition_2nd(fullOffset) \
( (UInt16) ((UInt32)(fullOffset) & 0xFFFF) )
#define PEFRelocComposeLgByImport_1st(fullIndex) \
( 0xA400 | ((UInt16) (((UInt32)(fullIndex)) >> 16) ) )
#define PEFRelocComposeLgByImport_2nd(fullIndex) \
( (UInt16) ((UInt32)(fullIndex) & 0xFFFF) )
#endif
template <typename T>
void eswap(T *data, const char * format)

285
PEFTools/PEFBinaryFormat.h Normal file
View File

@ -0,0 +1,285 @@
// PEFBinaryFormat.h from the Multiversal Interfaces
// generated from executor source code
/*****************************************************************************
* *
* PEFBinaryFormat.h *
* ================= *
* *
* Needs: *
* MacTypes.h OSType *
* *
* *
*****************************************************************************/
#pragma pack(push, 2)
/*****************************************************************************/
enum
{
kPEFTag1 = 'Joy!',
kPEFTag2 = 'peff',
kPEFVersion = 1,
};
/*****************************************************************************/
enum
{
kPEFProcessShare = 1,
kPEFGlobalShare = 4,
kPEFProtectedShare = 5,
};
/*****************************************************************************/
enum
{
kPEFCodeSection = 0,
kPEFUnpackedDataSection,
kPEFPatternDataSection,
kPEFConstantSection,
kPEFLoaderSection,
kPEFDebugSection,
kPEFExecutableDataSection,
kPEFExceptionSection,
kPEFTracebackSection,
};
/*****************************************************************************/
enum
{
kPEFCodeSymbol,
kPEFDataSymbol,
kPEFTVectorSymbol,
kPEFTOCSymbol,
kPEFGlueSymbol,
};
/*****************************************************************************
* flags for PEFImportedLibrary::options *
*****************************************************************************/
enum
{
kPEFWeakImportLibMask = 64,
kPEFInitLibBeforeMask = 128,
};
/*****************************************************************************
* PEFContainerHeader *
*****************************************************************************/
typedef struct PEFContainerHeader PEFContainerHeader;
struct PEFContainerHeader
{
OSType tag1;
OSType tag2;
OSType architecture;
uint32_t formatVersion;
uint32_t dateTimeStamp;
uint32_t oldDefVersion;
uint32_t oldImpVersion;
uint32_t currentVersion;
uint16_t sectionCount;
uint16_t instSectionCount;
uint32_t reservedA;
};
/*****************************************************************************/
enum
{
kPEFFirstSectionHeaderOffset = sizeof(PEFContainerHeader),
};
/*****************************************************************************
* PEFSectionHeader *
*****************************************************************************/
typedef struct PEFSectionHeader PEFSectionHeader;
struct PEFSectionHeader
{
int32_t nameOffset;
uint32_t defaultAddress;
uint32_t totalLength;
uint32_t unpackedLength;
uint32_t containerLength;
uint32_t containerOffset;
uint8_t sectionKind;
uint8_t shareKind;
uint8_t alignment;
uint8_t reservedA;
};
/*****************************************************************************
* PEFLoaderInfoHeader *
*****************************************************************************/
typedef struct PEFLoaderInfoHeader PEFLoaderInfoHeader;
struct PEFLoaderInfoHeader
{
int32_t mainSection;
uint32_t mainOffset;
int32_t initSection;
uint32_t initOffset;
int32_t termSection;
uint32_t termOffset;
uint32_t importedLibraryCount;
uint32_t totalImportedSymbolCount;
uint32_t relocSectionCount;
uint32_t relocInstrOffset;
uint32_t loaderStringsOffset;
uint32_t exportHashOffset;
uint32_t exportHashTablePower;
uint32_t exportedSymbolCount;
};
/*****************************************************************************
* PEFImportedLibrary *
*****************************************************************************/
typedef struct PEFImportedLibrary PEFImportedLibrary;
struct PEFImportedLibrary
{
uint32_t nameOffset;
uint32_t oldImpVersion;
uint32_t currentVersion;
uint32_t importedSymbolCount;
uint32_t firstImportedSymbol;
uint8_t options;
uint8_t reservedA;
uint16_t reservedB;
};
/*****************************************************************************
* PEFLoaderRelocationHeader *
*****************************************************************************/
typedef struct PEFLoaderRelocationHeader PEFLoaderRelocationHeader;
struct PEFLoaderRelocationHeader
{
uint16_t sectionIndex;
uint16_t reservedA;
uint32_t relocCount;
uint32_t firstRelocOffset;
};
/*****************************************************************************/
enum
{
kExponentLimit = 16,
kAverageChainLimit = 10,
};
/*****************************************************************************/
enum
{
kPEFHashLengthShift = 16,
kPEFHashValueMask = 65535,
};
/*****************************************************************************/
enum
{
FIRST_INDEX_SHIFT = 0,
FIRST_INDEX_MASK = 262143,
CHAIN_COUNT_SHIFT = 18,
CHAIN_COUNT_MASK = 16383,
};
/*****************************************************************************
* PEFExportedSymbol *
*****************************************************************************/
typedef struct PEFExportedSymbol PEFExportedSymbol;
struct PEFExportedSymbol
{
uint32_t classAndName;
uint32_t symbolValue;
int16_t sectionIndex;
};
/*****************************************************************************
* PEFImportedSymbol *
*****************************************************************************/
typedef struct PEFImportedSymbol PEFImportedSymbol;
struct PEFImportedSymbol
{
uint32_t classAndName;
};
/*****************************************************************************
* PEFExportedSymbolKey *
*****************************************************************************/
typedef uint32_t PEFExportedSymbolKey;
/*****************************************************************************
* PEFExportedSymbolHashSlot *
*****************************************************************************/
typedef uint32_t PEFExportedSymbolHashSlot;
/*****************************************************************************/
enum
{
NAME_MASK = 16777215,
};
/*****************************************************************************
* PEFExportedSymbolClass *
*****************************************************************************/
#define PEFExportedSymbolClass(classAndName) (((classAndName) >> 24) & 0xFF)
/*****************************************************************************
* PEFExportedSymbolNameOffset *
*****************************************************************************/
#define PEFExportedSymbolNameOffset(classAndName) ((classAndName)&0xFFFFFF)
/*****************************************************************************
* PEFComposeImportedSymbol *
*****************************************************************************/
#define PEFComposeImportedSymbol(cls, nameOffset) \
((((uint32_t)cls) << 24) | (uint32_t)(nameOffset))
/*****************************************************************************
* PEFRelocComposeBySectC *
*****************************************************************************/
#define PEFRelocComposeBySectC(runLength) (0x4000 | ((uint16_t)(runLength)-1))
/*****************************************************************************
* PEFRelocComposeBySectD *
*****************************************************************************/
#define PEFRelocComposeBySectD(runLength) (0x4200 | ((uint16_t)(runLength)-1))
/*****************************************************************************
* PEFRelocComposeSetPosition_1st *
*****************************************************************************/
#define PEFRelocComposeSetPosition_1st(fullOffset) \
(0xA000 | (uint16_t)((uint32_t)(fullOffset) >> 16))
/*****************************************************************************
* PEFRelocComposeSetPosition_2nd *
*****************************************************************************/
#define PEFRelocComposeSetPosition_2nd(fullOffset) \
((uint16_t)((uint32_t)(fullOffset)))
/*****************************************************************************
* PEFRelocComposeLgByImport_1st *
*****************************************************************************/
#define PEFRelocComposeLgByImport_1st(fullIndex) \
(0xA400 | (uint16_t)((uint32_t)(fullIndex) >> 16))
/*****************************************************************************
* PEFRelocComposeLgByImport_2nd *
*****************************************************************************/
#define PEFRelocComposeLgByImport_2nd(fullIndex) \
((uint16_t)((uint32_t)(fullIndex)))
#pragma pack(pop)

View File

@ -319,9 +319,6 @@ fi # SKIP_THIRDPARTY
echo "Building host-based tools..."
# Copy PEFBinaryFormat.h from Universal Interfaces, needed by MakePEF & MakeImport
setupPEFBinaryFormat
mkdir -p build-host
cd build-host
cmake ${SRC} -DCMAKE_INSTALL_PREFIX=$PREFIX -DCMAKE_BUILD_TYPE=Debug "${HOST_CMAKE_FLAGS[@]}" ${CMAKE_GENERATOR}

View File

@ -157,11 +157,6 @@ function unlinkHeaders()
done
}
function setupPEFBinaryFormat()
{
(export LC_ALL=C; sed 's/\r$//' < "$CINCLUDES/PEFBinaryFormat.h" | tr '\r' '\n' > "$PREFIX/include/PEFBinaryFormat.h")
}
function setUpInterfacesAndLibraries()
{
echo "Preparing CIncludes..."
@ -268,7 +263,6 @@ function removeInterfacesAndLibraries()
rm -rf "$PREFIX/CIncludes"
find "$PREFIX/RIncludes" ! -name 'Retro*.r' -type f -exec rm -f {} \;
rm -f "$FILE_LIST"
rm -f "$PREFIX/include/PEFBinaryFormat.h"
fi
}
@ -297,7 +291,6 @@ else
else
locateAndCheckInterfacesAndLibraries
removeInterfacesAndLibraries
setupPEFBinaryFormat
setUpInterfacesAndLibraries
fi
fi