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, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
28 #include <mach/mach.h>
29 #include <mach-o/loader.h>
30 #include <mach-o/reloc.h>
34 /* Instead of unistd.h, this used to include libc.h.
35 "Nelson H. F. Beebe" <beebe@math.utah.edu> says that doesn't work
36 in system version 3.3. */
42 * Kludge: we don't expect any program data beyond VM_HIGHDATA
43 * What is really needed is a way to find out from malloc() which
44 * pages it vm_allocated and write only those out into the data segment.
46 * This kludge may break when we stop using fixed virtual address
47 * shared libraries. Actually, emacs will probably continue working, but be
48 * much larger on disk than it needs to be (because non-malloced data will
51 static const unsigned VM_HIGHDATA
= 0x2000000;
53 typedef struct region_t
{
57 vm_prot_t max_protection
;
58 vm_inherit_t inheritance
;
67 struct load_command
***the_commands
,
68 unsigned *the_commands_len
71 if (*the_commands
== NULL
) {
72 *the_commands_len
= 1;
73 *the_commands
= malloc(sizeof(*the_commands
));
75 (*the_commands_len
)++;
76 *the_commands
= realloc(*the_commands
,
78 sizeof(**the_commands
)));
85 struct load_command
*command
,
86 struct load_command
***the_commands
,
87 unsigned *the_commands_len
90 struct load_command
**tmp
;
92 grow(the_commands
, the_commands_len
);
93 tmp
= &(*the_commands
)[*the_commands_len
- 1];
94 *tmp
= malloc(command
->cmdsize
);
95 bcopy(command
, *tmp
, command
->cmdsize
);
99 fatal_unexec(char *format
, ...)
103 va_start(ap
, format
);
104 fprintf(stderr
, "unexec: ");
105 vfprintf(stderr
, format
, ap
);
106 fprintf(stderr
, "\n");
113 struct mach_header
*the_header
,
114 struct load_command
***the_commands
,
115 unsigned *the_commands_len
118 struct load_command command
;
119 struct load_command
*buf
;
123 if (read(fd
, the_header
, sizeof(*the_header
)) != sizeof(*the_header
)) {
124 fatal_unexec("cannot read macho header");
127 for (i
= 0; i
< the_header
->ncmds
; i
++) {
128 if (read(fd
, &command
, sizeof(struct load_command
)) !=
129 sizeof(struct load_command
)) {
130 fatal_unexec("cannot read macho load command header");
133 size
= command
.cmdsize
- sizeof(struct load_command
);
135 fatal_unexec("bogus load command size");
138 buf
= malloc(command
.cmdsize
);
139 buf
->cmd
= command
.cmd
;
140 buf
->cmdsize
= command
.cmdsize
;
141 if (read(fd
, ((char *)buf
+
142 sizeof(struct load_command
)),
144 fatal_unexec("cannot read load command data");
147 save_command(buf
, the_commands
, the_commands_len
);
154 vm_address_t start_address
,
156 vm_address_t end_address
159 vm_address_t address
;
162 address
= (start_address
+ *size
);
163 gapsize
= end_address
- address
;
165 if (vm_allocate(task_self(), &address
, gapsize
,
166 FALSE
) != KERN_SUCCESS
) {
167 fatal_unexec("cannot vm_allocate");
175 vm_address_t
*address
,
181 struct section
*sect
;
183 sect
= (struct section
*) getsectbyname(SEG_DATA
, SECT_DATA
);
187 ret
= vm_region(task_self(),
191 ®ion
.max_protection
,
196 if (ret
!= KERN_SUCCESS
|| region
.address
>= VM_HIGHDATA
) {
200 if (region
.address
> *address
+ *size
) {
201 if (!filldatagap(*address
, size
,
206 *size
+= region
.size
;
208 if (region
.address
== sect
->addr
) {
209 *address
= region
.address
;
213 region
.address
+= region
.size
;
223 vm_address_t address
;
225 if (vm_allocate(task_self(), &address
, size
, TRUE
) != KERN_SUCCESS
) {
228 return ((char *)address
);
237 vm_deallocate(task_self(), (vm_address_t
)buf
, size
);
247 struct load_command
**the_commands
= NULL
;
248 unsigned the_commands_len
;
249 struct mach_header the_header
;
256 vm_address_t data_address
;
258 vm_size_t vmaddr_growth
= 0;
259 vm_size_t dataseg_vmaddr
, dataseg_vmend
;
261 struct segment_command
*segment
;
264 unsigned long extreloff
= 0;
265 unsigned long nextrel
= 0;
266 struct dysymtab_command
*dysymtab
;
267 struct relocation_info reloc_info
;
270 if (!read_macho(infd
, &the_header
, &the_commands
, &the_commands_len
)) {
275 malloc_cookie
= malloc_freezedry ();
276 if (!get_data_region(&data_address
, &data_size
)) {
282 * DO NOT USE MALLOC IN THIS SECTION
288 for (i
= 0; i
< the_commands_len
; i
++) {
289 switch (the_commands
[i
]->cmd
) {
291 segment
= ((struct segment_command
*)
293 if (strcmp(segment
->segname
, SEG_DATA
) == 0) {
294 fdatastart
= segment
->fileoff
;
295 fdatasize
= segment
->filesize
;
296 fgrowth
= (data_size
-
298 segment
->vmsize
= data_size
;
299 segment
->filesize
= data_size
;
300 dataseg_vmaddr
= segment
->vmaddr
;
301 dataseg_vmend
= segment
->vmaddr
+ segment
->vmsize
;
302 vmaddr_growth
= segment
->vmaddr
+ segment
->vmsize
;
304 ((struct segment_command
*)the_commands
[i
])->fileoff
+= fgrowth
;
307 if( strcmp( segment
->segname
, SEG_LINKEDIT
) == 0 ) {
308 segment
->vmaddr
= vmaddr_growth
;
313 ((struct symtab_command
*)
314 the_commands
[i
])->symoff
+= fgrowth
;
315 ((struct symtab_command
*)
316 the_commands
[i
])->stroff
+= fgrowth
;
319 ((struct symseg_command
*)
320 the_commands
[i
])->offset
+= fgrowth
;
324 dysymtab
= ((struct dysymtab_command
*)the_commands
[i
]);
325 extreloff
= dysymtab
->extreloff
;
326 nextrel
= dysymtab
->nextrel
;
327 dysymtab
->indirectsymoff
+= fgrowth
;
328 dysymtab
->extreloff
+= fgrowth
;
339 if (write(outfd
, &the_header
,
340 sizeof(the_header
)) != sizeof(the_header
)) {
341 fatal_unexec("cannot write output file");
348 for (i
= 0; i
< the_commands_len
; i
++) {
349 if (write(outfd
, the_commands
[i
],
350 the_commands
[i
]->cmdsize
) !=
351 the_commands
[i
]->cmdsize
) {
352 fatal_unexec("cannot write output file");
358 * Write original text
360 if (lseek(infd
, the_header
.sizeofcmds
+ sizeof(the_header
),
362 fatal_unexec("cannot seek input file");
365 size
= fdatastart
- (sizeof(the_header
) +
366 the_header
.sizeofcmds
);
367 buf
= my_malloc(size
);
368 if (read(infd
, buf
, size
) != size
) {
370 fatal_unexec("cannot read input file");
372 if (write(outfd
, buf
, size
) != size
) {
374 fatal_unexec("cannot write output file");
383 if (write(outfd
, (char *)data_address
,
384 data_size
) != data_size
) {
385 fatal_unexec("cannot write output file");
392 * OKAY TO USE MALLOC NOW
399 if (lseek(infd
, fdatasize
, L_INCR
) < 0) {
400 fatal_unexec("cannot seek input file");
403 size
= st
.st_size
- lseek(infd
, 0, L_INCR
);
406 if (read(infd
, buf
, size
) != size
) {
408 fatal_unexec("cannot read input file");
411 if (write(outfd
, buf
, size
) != size
) {
413 fatal_unexec("cannot write output file");
420 * Fix up relocation entries in the data segment.
423 if (lseek(infd
, extreloff
, L_SET
) < 0) {
424 fatal_unexec("cannot seek input file");
428 for (i
= 0; i
< nextrel
; i
++)
432 if (read(infd
, &reloc_info
, sizeof (reloc_info
)) != sizeof (reloc_info
)) {
433 fatal_unexec("cannot read input file");
436 if (reloc_info
.r_address
>= dataseg_vmaddr
&& reloc_info
.r_address
< dataseg_vmend
)
438 if (lseek (outfd
, fdatastart
+ reloc_info
.r_address
- dataseg_vmaddr
, L_SET
) < 0 ) {
439 fatal_unexec("cannot seek input file");
442 switch (reloc_info
.r_length
) {
444 if (write(outfd
, &zeroval
, 1) != 1) {
445 fatal_unexec("cannot write output file");
450 if (write(outfd
, &zeroval
, 2) != 2) {
451 fatal_unexec("cannot write output file");
456 if (write(outfd
, &zeroval
, 4) != 4) {
457 fatal_unexec("cannot write output file");
477 char tmpbuf
[L_tmpnam
];
480 infd
= open(infile
, O_RDONLY
, 0);
482 fatal_unexec("cannot open input file `%s'", infile
);
487 tmpfile
= rindex(tmpbuf
, '/');
488 if (tmpfile
== NULL
) {
493 outfd
= open(tmpfile
, O_WRONLY
|O_TRUNC
|O_CREAT
, 0755);
496 fatal_unexec("cannot open tmp file `%s'", tmpfile
);
499 if (!unexec_doit(infd
, outfd
)) {
507 if (rename(tmpfile
, outfile
) < 0) {
509 fatal_unexec("cannot rename `%s' to `%s'", tmpfile
, outfile
);