Chameleon

Chameleon Commit Details

Date:2011-08-03 07:21:48 (12 years 8 months ago)
Author:Evan Lojewski
Commit:1262
Parents: 1261
Message:Makefile update. add macho-to-xcoff util
Changes:
A/branches/xZen/src/util/macho-to-xcoff.c
M/branches/xZen/src/arch/i386/Makefile
M/branches/xZen/Makefile
M/branches/xZen/src/util/Makefile
M/branches/xZen/src/arch/i386/libsa/Makefile
M/branches/xZen/Make.rules

File differences

branches/xZen/Make.rules
11
2
3
24
3
4
5
6
7
8
95
106
117
include ${SRCROOT}/Make.rules
MKDIRS = /bin/mkdir -p
ifneq "" "$(wildcard /bin/mkdirs)"
MKDIRS = /bin/mkdirs
else
MKDIRS = /bin/mkdir -p
endif
NATIVE_AS = as
NATIVE_LD = ld
NATIVE_CC = cc
branches/xZen/src/arch/i386/libsa/Makefile
2020
2121
2222
23
2423
25
26
2724
2825
2926
LIBS = libsa.a
LIBS := $(addprefix $(SYMROOT)/, $(LIBS))
include ${SRCROOT}/i386/Make.rules
DIRS_NEEDED = $(OBJROOT) $(SYMROOT)
all embedtheme: $(DIRS_NEEDED) $(LIBS)
branches/xZen/src/arch/i386/Makefile
33
44
55
6
6
77
88
99
#
# these paths are only valid in subdirectories of this directory
#
ROOT = $(shell pwd)/../../
ROOT = $(shell pwd)/../../../
SRCROOT = ${ROOT}/src
OBJROOT = $(ROOT)/obj/i386
SYMROOT = $(ROOT)/sym/i386
branches/xZen/src/util/macho-to-xcoff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
* macho-to-xcoff.c - Converts a Mach-O file an XCOFF.
*
* Copyright (c) 1998-2002 Apple Computer, Inc.
*
* DRI: Josh de Cesare
*/
#define __srr0 srr0// works with or without conformance
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <mach-o/loader.h>
#include <mach/thread_status.h>
#include <mach/ppc/thread_status.h>
char *progname;
static FILE *machoF;
static FILE *xcoffF;
static struct mach_header mhead;
typedef unsigned char UInt8;
typedef unsigned short UInt16;
typedef unsigned long UInt32;
#ifdef __i386__
#define SWAPL(value) ( (((value) >> 24) & 0xff) | \
(((value) >> 8) & 0xff00) | \
(((value) << 8) & 0xff0000) | \
(((value) << 24) & 0xff000000) )
#define SWAPS(value) ( (((value) >> 8) & 0xff) | \
(((value) << 8) & 0xff00) )
#else
#define SWAPS(W)(W)
#define SWAPL(L)(L)
#endif
typedef struct {
/* File header */
UInt16 magic;
#define kFileMagic0x1DF
UInt16 nSections;
UInt32 timeAndDate;
UInt32 symPtr;
UInt32 nSyms;
UInt16 optHeaderSize;
UInt16 flags;
} XFileHeader;
typedef struct {
/* Optional header */
UInt16 magic;
#define kOptHeaderMagic0x10B
UInt16 version;
UInt32 textSize;
UInt32 dataSize;
UInt32 BSSSize;
UInt32 entryPoint;
UInt32 textStart;
UInt32 dataStart;
UInt32 toc;
UInt16 snEntry;
UInt16 snText;
UInt16 snData;
UInt16 snTOC;
UInt16 snLoader;
UInt16 snBSS;
UInt8 filler[28];
} XOptHeader;
typedef struct {
char name[8];
UInt32 pAddr;
UInt32 vAddr;
UInt32 size;
UInt32 sectionFileOffset;
UInt32 relocationsFileOffset;
UInt32 lineNumbersFileOffset;
UInt16 nRelocations;
UInt16 nLineNumbers;
UInt32 flags;
} XSection;
enum SectionNumbers {
kTextSN = 1,
kDataSN,
kBSSSN
};
#define kTextName".text"
#define kDataName".data"
#define kBSSName".bss"
static struct {
XFileHeader file;
XOptHeader opt;
XSection text;
XSection data;
XSection BSS;
} xHead = {
{/* file */
SWAPS(kFileMagic),/* magic */
SWAPS(3),/* nSections */
0,/* timeAndDate */
0,/* symPtr */
0,/* nSyms */
SWAPS(sizeof (XOptHeader)),/* optHeaderSize */
0
},
{/* opt */
SWAPS(kOptHeaderMagic),/* magic */
0,/* version */
0,/* textSize */
0,/* dataSize */
0,/* BSSSize */
0,/* entryPoint */
0,/* textStart */
0,/* dataStart */
0,/* toc */
SWAPS(kTextSN),/* snEntry */
SWAPS(kTextSN),/* snText */
SWAPS(kDataSN),/* snData */
SWAPS(0),/* snTOC */
SWAPS(0),/* snLoader */
SWAPS(kBSSSN),/* snBSS */
},
{/* text section */
kTextName
},
{/* data section */
kDataName
},
{/* BSS section */
kBSSName
}
};
static UInt32 textFileOffset;
static UInt32 textSize;
static UInt32 dataFileOffset;
static UInt32 dataSize;
static UInt32 bssSize;
static void usage (char *msg)
{
printf ("Usage: %s mach-o-file-name xcoff-file-name\n\n%s\n",
progname, msg);
exit (1);
}
static void copyMachOSectionToXCOFF (UInt32 machoOffset, UInt32 sectionSize)
{
static char buf[65536];
fseek (machoF, machoOffset, SEEK_SET);
while (sectionSize) {
long readSize = sectionSize > sizeof (buf) ? sizeof (buf) : sectionSize;
long writeSize;
long actualSize;
actualSize = fread (buf, 1, readSize, machoF);
if (actualSize < 0) perror ("read error for section");
writeSize = actualSize;
actualSize = fwrite (buf, 1, writeSize, xcoffF);
if (actualSize < 0) perror ("write error for section");
sectionSize -= actualSize;
}
}
int main (int argc, char **argv)
{
int n;
char *cmdsP, *cp;
#define LCP((struct load_command *) cp)
#define SCP((struct segment_command *) cp)
progname = argv[0];
if (argc < 3) usage ("wrong number of parameters");
machoF = fopen (argv[1], "rb");
if (machoF == 0) perror ("Can't open mach-o file");
xcoffF = fopen (argv[2], "wb");
if (xcoffF == 0) perror ("Can't create and/or open XCOFF file");
n = fread (&mhead, sizeof (mhead), 1, machoF);
if (n != 1) perror ("error reading mach-o file header");
if (SWAPL(mhead.magic) != MH_MAGIC
|| SWAPL(mhead.filetype) != MH_EXECUTE)
usage ("bad mach-o file header");
cmdsP = malloc (SWAPL(mhead.sizeofcmds));
if (cmdsP == 0) usage ("cmdsP allocation failed");
n = fread (cmdsP, SWAPL(mhead.sizeofcmds), 1, machoF);
if (n != 1) perror ("error reading mach-o commands");
printf("Mach-o file has magic=0x%08lX, %ld commands\n", (long unsigned int)SWAPL(mhead.magic), (long int)SWAPL(mhead.ncmds));
for (n = 0, cp = cmdsP; n < SWAPL(mhead.ncmds); ++n, cp += SWAPL(LCP->cmdsize)) {
switch (SWAPL(LCP->cmd)) {
case LC_SEGMENT:
printf ("segment: %s: 0x%08lX of 0x%08lX bytes\n",
SCP->segname, (long unsigned int)SWAPL(SCP->vmaddr), (long unsigned int)SWAPL(SCP->vmsize));
if (strncmp (SCP->segname, SEG_TEXT, sizeof (SCP->segname)) == 0) {
textFileOffset = SWAPL(SCP->fileoff);
textSize = SWAPL(SCP->filesize);
printf ("__TEXT size = 0x%08lX\n", textSize);
xHead.text.pAddr = xHead.text.vAddr = SCP->vmaddr;
xHead.text.size = SCP->vmsize;
} else
if (strncmp (SCP->segname, SEG_DATA, sizeof (SCP->segname)) == 0) {
dataFileOffset = SWAPL(SCP->fileoff);
dataSize = SWAPL(SCP->filesize);
printf ("__DATA size = 0x%08lX\n", dataSize);
bssSize = SWAPL(SCP->vmsize) - SWAPL(SCP->filesize);
printf ("__BSS size = 0x%08lX\n", bssSize);
xHead.data.pAddr = xHead.data.vAddr = SCP->vmaddr;
/* Use just FILE part -- rest is BSS */
xHead.data.size = SCP->filesize;
} else {
printf ("ignoring mach-o section \"%s\"\n", SCP->segname);
}
break;
case LC_THREAD:
case LC_UNIXTHREAD:
xHead.opt.entryPoint = ((ppc_thread_state_t *)
(cp + sizeof(struct thread_command)
+ 2 * sizeof(unsigned long)) )->srr0;
printf("Entry point %lx\n\n", SWAPL(xHead.opt.entryPoint));
break;
}
}
/* Construct BSS out of thin air: the part of the data section
that is NOT file mapped */
xHead.BSS.pAddr = xHead.BSS.vAddr = SWAPL(SWAPL(xHead.data.pAddr) + SWAPL(xHead.data.size));
xHead.BSS.size = SWAPL(bssSize);
/* Calculate the section file offsets in the resulting XCOFF file */
xHead.text.sectionFileOffset = SWAPL(sizeof (xHead.file) + sizeof (xHead.opt)
+ sizeof (xHead.text) + sizeof (xHead.data) + sizeof (xHead.BSS));
xHead.data.sectionFileOffset = SWAPL(SWAPL(xHead.text.sectionFileOffset) + SWAPL(xHead.text.size));
/* MT - write opt header */
xHead.opt.textSize = xHead.text.size;
xHead.opt.dataSize = xHead.data.size;
xHead.opt.BSSSize = xHead.BSS.size;
if (argc == 4) sscanf(argv[3],"%lx",&xHead.opt.entryPoint);
/* Write out the XCOFF file, copying the relevant mach-o file sections */
fwrite (&xHead, sizeof (xHead), 1, xcoffF);
copyMachOSectionToXCOFF (textFileOffset, textSize);
copyMachOSectionToXCOFF (dataFileOffset, dataSize);
fclose (machoF);
fclose (xcoffF);
return 0;
}
branches/xZen/src/util/Makefile
1313
1414
1515
16
17
16
17
1818
1919
2020
......
2929
3030
3131
32
32
3333
34
34
3535
3636
3737
3838
39
39
4040
4141
42
42
43
4344
4445
4546
DIR = util
PROGRAMS = machOconv dyldsymboltool
OBJECTS = machOconv dyldsymboltool
PROGRAMS = machOconv macho-to-xcoff dyldsymboltool
OBJECTS = machOconv macho-to-xcoff dyldsymboltool
ifeq (${CONFIG_BDMESG}, y)
PROGRAMS += bdmesg
LDFLAGS := $(LDFALGS) -framework IOKit -framework CoreFoundation -mmacosx-version-min=10.4
SYMPROG = $(addpostfix $(addprefix $(SYMROOT)/, $(PROGRAMS)), ${ARCH})
SYMPROG = $(addsuffix $(addprefix $(SYMROOT)/, $(PROGRAMS)), .${ARCH})
DIRS_NEEDED = $(OBJROOT) $(SYMROOT)
DIRS_NEEDED =
include ${ROOT}/Make.rules
all: $(DIRS_NEEDED) $(SYMPROG) fdisk440
all: $(OBJROOT) $(SYMROOT) $(SYMPROG) fdisk440
$(SYMPROG).${ARCH}: ${ACTUAL_OBJECTS}
$(SYMPROG): ${ACTUAL_OBJECTS}
@echo "\t[LD:${ARCH}] $(@F)"
@$(TARGET_CC) $(CFLAGS) $(LDFLAGS) $(DEFINES) -arch ${ARCH} -o $(SYMROOT)/$(@F).${ARCH} $(OBJROOT)/$(@F).${ARCH}o
branches/xZen/Makefile
2525
2626
2727
28
29
28
29
3030
3131
3232
do \
if [ -d $$i ]; then \
echo "================= make $@ for $$i arch $$a================="; \
( ROOT=$(ROOT); \
cd $$i; ${MAKE}ARCH=$$a \
( \
cd $$i; ${MAKE}ROOT=$(ROOT) ARCH=$$a \
$@ \
) || exit $$?; \
else \

Archive Download the corresponding diff file

Revision: 1262