1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24    File:        GetSymbolFromPEF.h
25
26    Contains:    xxx put contents here xxx
27
28    Written by:    Jeffrey Robbin
29
30    Copyright:    � 1994 by Apple Computer, Inc., all rights reserved.
31
32    Change History (most recent first):
33
34         <2>     9/20/94    TS        Dump the globals!
35         <1>      9/6/94    TS        first checked in
36
37*/
38
39#ifndef __GETSYMBOLFROMPEF__
40#define __GETSYMBOLFROMPEF__
41
42//#include <Types.h>
43
44// #pragma options align=mac68k
45
46#define NewPtrSys(a)    malloc(a)
47#define DisposePtr(a)    free(a)
48
49/*
50    Container information
51*/
52
53struct ContainerHeader {               // File/container header layout:
54    uint32_t    magicCookie;      // PEF container magic cookie
55    uint32_t    containerID;      // 'peff'
56    uint32_t    architectureID;   // 'pwpc' | 'm68k'
57    uint32_t    versionNumber;    // format version number
58    uint32_t    dateTimeStamp;    // date/time stamp (Mac format)
59    uint32_t    oldDefVersion;    // old definition version number
60    uint32_t    oldImpVersion;    // old implementation version number
61    uint32_t    currentVersion;   // current version number
62    uint16_t    nbrOfSections;    // nbr of sections (rel. 1)
63    uint16_t    loadableSections; // nbr of loadable sectionsfor execution
64                                       //   (= nbr of 1st non-loadable section)
65    LogicalAddress    memoryAddress;   // location this container was last loaded
66};
67typedef struct ContainerHeader ContainerHeader, *ContainerHeaderPtr;
68
69#define  kMagic1  'joy!'
70#define  kMagic2  'peff'
71
72
73/*
74    Section information
75*/
76
77struct SectionHeader {               // Section header layout:
78    int32_t   sectionName;     // section name (str tbl container offset)
79    uint32_t  sectionAddress;  // preferred base address for the section
80    int32_t   execSize;        // execution (byte) size including 0 init)
81    int32_t   initSize;        // init (byte) size before 0 init
82    int32_t   rawSize;         // raw data size (bytes)
83    int32_t   containerOffset; // container offest to section's raw data
84    uint8_t   regionKind;      // section/region classification
85    uint8_t   shareKind;       // sharing classification
86    uint8_t   alignment;       // log 2 alignment
87    uint8_t   reserved;        // <reserved>
88};
89typedef struct SectionHeader SectionHeader, *SectionHeaderPtr;
90
91/* regionKind section classification: */
92/* loadable sections */
93#define kCodeSection      0U  /* code section */
94#define kDataSection      1U  /* data section */
95#define kPIDataSection    2U  /* "pattern" initialized data */
96#define kConstantSection  3U  /* read-only data */
97#define kExecDataSection  6U  /* "self modifying" code (!?) */
98/* non-loadable sections */
99#define kLoaderSection    4U  /* loader */
100#define kDebugSection     5U  /* debugging info */
101#define kExceptionSection 7U  /* exception data */
102#define kTracebackSection 8U  /* traceback data */
103
104
105/*
106    Loader Information
107*/
108
109struct LoaderHeader {     // Loader raw data section header layout:
110    int32_t entrySection;    // entry point section number
111    int32_t entryOffset;     // entry point descr. ldr section offset
112    int32_t initSection;     // init routine section number
113    int32_t initOffset;      // init routine descr. ldr section offset
114    int32_t termSection;     // term routine section number
115    int32_t termOffset;      // term routine descr. ldr section offset
116    int32_t nbrImportIDs;    // nbr of import container id entries
117    int32_t nbrImportSyms;   // nbr of import symbol table entries
118    int32_t nbrRelocSects;   // nbr of relocation sections (headers)
119    int32_t relocsOffset;    // reloc. instructions ldr section offset
120    int32_t strTblOffset;    // string table ldr section offset
121    int32_t slotTblOffset; // hash slot table ldr section offset
122    int32_t hashSlotTblSz;   // log 2 of nbr of hash slot entries
123    int32_t nbrExportSyms;   // nbr of export symbol table entries
124};
125typedef struct LoaderHeader LoaderHeader, *LoaderHeaderPtr;
126
127struct LoaderHashSlotEntry { // Loader export hash slot table entry layout:
128    uint32_t  slotEntry; // chain count (0:13), chain index (14:31)
129};
130typedef struct LoaderHashSlotEntry LoaderHashSlotEntry, *LoaderHashSlotEntryPtr;
131
132struct LoaderExportChainEntry { // Loader export hash chain tbl entry layout:
133    union {
134        uint32_t  _hashWord; // name length and hash value
135        struct {
136            unsigned short _nameLength; // name length is top half of hash word
137            unsigned short doNotUseThis; // this field should never be accessed!
138        } _h_h;
139    } _h;
140};
141typedef struct LoaderExportChainEntry LoaderExportChainEntry, *LoaderExportChainEntryPtr;
142
143struct ExportSymbolEntry {        // Loader export symbol table entry layout:
144    uint32_t  class_and_name; // symClass (0:7), nameOffset (8:31)
145    int32_t address;                 // ldr section offset to exported symbol
146    short sectionNumber;          // section nbr that this export belongs to
147};
148typedef struct ExportSymbolEntry ExportSymbolEntry, *ExportSymbolEntryPtr;
149
150
151
152/*
153    Unpacking Information
154*/
155
156 /* "pattern" initialized data opcodes: */
157#define kZero              0U     /* zero (clear) bytes */
158#define kBlock             1U     /* block transfer bytes */
159#define kRepeat            2U     /* repeat block xfer bytes */
160#define kRepeatBlock       3U     /* repeat block xfer with contant prefix */
161#define kRepeatZero        4U     /* repeat block xfer with contant prefix 0 */
162#define kOpcodeShift       0x05U  /* shift to access opcode */
163#define kFirstOperandMask  0x1FU  /* mask to access 1st operand (count) */
164
165#define PIOP(x)  (unsigned char)((x) >> kOpcodeShift)  /* extract opcode */
166#define PICNT(x)  (int)((x) & kFirstOperandMask) /* extract 1st operand (count) */
167
168/* The following macros are used for extracting count value operands from pidata...            */
169
170#define kCountShift  0x07UL  /* value shift to concat count bytes */
171#define kCountMask   0x7FUL  /* mask to extract count bits from a byte */
172#define IS_LAST_PICNT_BYTE(x) (((x) & 0x80U) == 0) /* true if last count byte */
173#define CONCAT_PICNT(value, x) (((value)<<kCountShift) | ((x) & kCountMask))
174
175
176
177/*
178    Function Prototypes
179*/
180
181static OSStatus GetSymbolFromPEF(
182    char *theSymbolName,
183    LogicalAddress thePEFPtr,
184    LogicalAddress theSymbolPtr,
185    ByteCount      theSymbolSize);
186
187static Boolean SymbolCompare(
188    StringPtr     theLookedForSymbol,
189    StringPtr     theExportSymbol,
190    uint32_t  theExportSymbolLength);
191
192static OSErr UnpackPiData(
193    LogicalAddress   thePEFPtr,
194    SectionHeaderPtr sectionHeaderPtr,
195    LogicalAddress * theData);
196
197static unsigned char PEFGetNextByte(
198    unsigned char ** rawBuffer,
199    int32_t * rawBufferRemaining);
200
201static uint32_t  PEFGetCount(
202    unsigned char ** rawBuffer,
203    int * rawBufferRemaining);
204
205
206// #pragma options align=reset
207
208#endif
209