1 /* Dump Emacs in macho format.
2 Copyright (C) 1990, 1993 Free Software Foundation, Inc.
3 Written by Bradley Taylor (btaylor@next.com).
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #include <mach/mach.h>
28 #include <mach-o/loader.h>
37 * Kludge: we don't expect any program data beyond VM_HIGHDATA
38 * What is really needed is a way to find out from malloc() which
39 * pages it vm_allocated and write only those out into the data segment.
41 * This kludge may break when we stop using fixed virtual address
42 * shared libraries. Actually, emacs will probably continue working, but be
43 * much larger on disk than it needs to be (because non-malloced data will
46 static const unsigned VM_HIGHDATA
= 0x2000000;
48 typedef struct region_t
{
52 vm_prot_t max_protection
;
53 vm_inherit_t inheritance
;
62 struct load_command
***the_commands
,
63 unsigned *the_commands_len
66 if (*the_commands
== NULL
) {
67 *the_commands_len
= 1;
68 *the_commands
= malloc(sizeof(*the_commands
));
70 (*the_commands_len
)++;
71 *the_commands
= realloc(*the_commands
,
73 sizeof(**the_commands
)));
80 struct load_command
*command
,
81 struct load_command
***the_commands
,
82 unsigned *the_commands_len
85 struct load_command
**tmp
;
87 grow(the_commands
, the_commands_len
);
88 tmp
= &(*the_commands
)[*the_commands_len
- 1];
89 *tmp
= malloc(command
->cmdsize
);
90 bcopy(command
, *tmp
, command
->cmdsize
);
94 fatal_unexec(char *format
, ...)
99 fprintf(stderr
, "unexec: ");
100 vfprintf(stderr
, format
, ap
);
101 fprintf(stderr
, "\n");
108 struct mach_header
*the_header
,
109 struct load_command
***the_commands
,
110 unsigned *the_commands_len
113 struct load_command command
;
114 struct load_command
*buf
;
118 if (read(fd
, the_header
, sizeof(*the_header
)) != sizeof(*the_header
)) {
119 fatal_unexec("cannot read macho header");
122 for (i
= 0; i
< the_header
->ncmds
; i
++) {
123 if (read(fd
, &command
, sizeof(struct load_command
)) !=
124 sizeof(struct load_command
)) {
125 fatal_unexec("cannot read macho load command header");
128 size
= command
.cmdsize
- sizeof(struct load_command
);
130 fatal_unexec("bogus load command size");
133 buf
= malloc(command
.cmdsize
);
134 buf
->cmd
= command
.cmd
;
135 buf
->cmdsize
= command
.cmdsize
;
136 if (read(fd
, ((char *)buf
+
137 sizeof(struct load_command
)),
139 fatal_unexec("cannot read load command data");
142 save_command(buf
, the_commands
, the_commands_len
);
149 vm_address_t start_address
,
151 vm_address_t end_address
154 vm_address_t address
;
157 address
= (start_address
+ *size
);
158 gapsize
= end_address
- address
;
160 if (vm_allocate(task_self(), &address
, gapsize
,
161 FALSE
) != KERN_SUCCESS
) {
162 fatal_unexec("cannot vm_allocate");
170 vm_address_t
*address
,
176 struct section
*sect
;
178 sect
= (struct section
*) getsectbyname(SEG_DATA
, SECT_DATA
);
182 ret
= vm_region(task_self(),
186 ®ion
.max_protection
,
191 if (ret
!= KERN_SUCCESS
|| region
.address
>= VM_HIGHDATA
) {
195 if (region
.address
> *address
+ *size
) {
196 if (!filldatagap(*address
, size
,
201 *size
+= region
.size
;
203 if (region
.address
== sect
->addr
) {
204 *address
= region
.address
;
208 region
.address
+= region
.size
;
218 vm_address_t address
;
220 if (vm_allocate(task_self(), &address
, size
, TRUE
) != KERN_SUCCESS
) {
223 return ((char *)address
);
232 vm_deallocate(task_self(), (vm_address_t
)buf
, size
);
242 struct load_command
**the_commands
= NULL
;
243 unsigned the_commands_len
;
244 struct mach_header the_header
;
251 vm_address_t data_address
;
254 struct segment_command
*segment
;
256 if (!read_macho(infd
, &the_header
, &the_commands
, &the_commands_len
)) {
261 malloc_cookie
= malloc_freezedry ();
262 if (!get_data_region(&data_address
, &data_size
)) {
268 * DO NOT USE MALLOC IN THIS SECTION
274 for (i
= 0; i
< the_commands_len
; i
++) {
275 switch (the_commands
[i
]->cmd
) {
277 segment
= ((struct segment_command
*)
279 if (strcmp(segment
->segname
, SEG_DATA
) == 0) {
280 fdatastart
= segment
->fileoff
;
281 fdatasize
= segment
->filesize
;
282 fgrowth
= (data_size
-
284 segment
->vmsize
= data_size
;
285 segment
->filesize
= data_size
;
289 ((struct symtab_command
*)
290 the_commands
[i
])->symoff
+= fgrowth
;
291 ((struct symtab_command
*)
292 the_commands
[i
])->stroff
+= fgrowth
;
295 ((struct symseg_command
*)
296 the_commands
[i
])->offset
+= fgrowth
;
306 if (write(outfd
, &the_header
,
307 sizeof(the_header
)) != sizeof(the_header
)) {
308 fatal_unexec("cannot write output file");
315 for (i
= 0; i
< the_commands_len
; i
++) {
316 if (write(outfd
, the_commands
[i
],
317 the_commands
[i
]->cmdsize
) !=
318 the_commands
[i
]->cmdsize
) {
319 fatal_unexec("cannot write output file");
325 * Write original text
327 if (lseek(infd
, the_header
.sizeofcmds
+ sizeof(the_header
),
329 fatal_unexec("cannot seek input file");
332 size
= fdatastart
- (sizeof(the_header
) +
333 the_header
.sizeofcmds
);
334 buf
= my_malloc(size
);
335 if (read(infd
, buf
, size
) != size
) {
337 fatal_unexec("cannot read input file");
339 if (write(outfd
, buf
, size
) != size
) {
341 fatal_unexec("cannot write output file");
350 if (write(outfd
, (char *)data_address
,
351 data_size
) != data_size
) {
352 fatal_unexec("cannot write output file");
359 * OKAY TO USE MALLOC NOW
366 if (lseek(infd
, fdatasize
, L_INCR
) < 0) {
367 fatal_unexec("cannot seek input file");
370 size
= st
.st_size
- lseek(infd
, 0, L_INCR
);
373 if (read(infd
, buf
, size
) != size
) {
375 fatal_unexec("cannot read input file");
378 if (write(outfd
, buf
, size
) != size
) {
380 fatal_unexec("cannot write output file");
395 char tmpbuf
[L_tmpnam
];
398 infd
= open(infile
, O_RDONLY
, 0);
400 fatal_unexec("cannot open input file `%s'", infile
);
405 tmpfile
= rindex(tmpbuf
, '/');
406 if (tmpfile
== NULL
) {
411 outfd
= open(tmpfile
, O_WRONLY
|O_TRUNC
|O_CREAT
, 0755);
414 fatal_unexec("cannot open tmp file `%s'", tmpfile
);
417 if (!unexec_doit(infd
, outfd
)) {
425 if (rename(tmpfile
, outfile
) < 0) {
427 fatal_unexec("cannot rename `%s' to `%s'", tmpfile
, outfile
);