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>
39 * Kludge: we don't expect any program data beyond VM_HIGHDATA
40 * What is really needed is a way to find out from malloc() which
41 * pages it vm_allocated and write only those out into the data segment.
43 * This kludge may break when we stop using fixed virtual address
44 * shared libraries. Actually, emacs will probably continue working, but be
45 * much larger on disk than it needs to be (because non-malloced data will
48 static const unsigned VM_HIGHDATA
= 0x2000000;
50 typedef struct region_t
{
54 vm_prot_t max_protection
;
55 vm_inherit_t inheritance
;
64 struct load_command
***the_commands
,
65 unsigned *the_commands_len
68 if (*the_commands
== NULL
) {
69 *the_commands_len
= 1;
70 *the_commands
= malloc(sizeof(*the_commands
));
72 (*the_commands_len
)++;
73 *the_commands
= realloc(*the_commands
,
75 sizeof(**the_commands
)));
82 struct load_command
*command
,
83 struct load_command
***the_commands
,
84 unsigned *the_commands_len
87 struct load_command
**tmp
;
89 grow(the_commands
, the_commands_len
);
90 tmp
= &(*the_commands
)[*the_commands_len
- 1];
91 *tmp
= malloc(command
->cmdsize
);
92 bcopy(command
, *tmp
, command
->cmdsize
);
96 fatal_unexec(char *format
, ...)
100 va_start(ap
, format
);
101 fprintf(stderr
, "unexec: ");
102 vfprintf(stderr
, format
, ap
);
103 fprintf(stderr
, "\n");
110 struct mach_header
*the_header
,
111 struct load_command
***the_commands
,
112 unsigned *the_commands_len
115 struct load_command command
;
116 struct load_command
*buf
;
120 if (read(fd
, the_header
, sizeof(*the_header
)) != sizeof(*the_header
)) {
121 fatal_unexec("cannot read macho header");
124 for (i
= 0; i
< the_header
->ncmds
; i
++) {
125 if (read(fd
, &command
, sizeof(struct load_command
)) !=
126 sizeof(struct load_command
)) {
127 fatal_unexec("cannot read macho load command header");
130 size
= command
.cmdsize
- sizeof(struct load_command
);
132 fatal_unexec("bogus load command size");
135 buf
= malloc(command
.cmdsize
);
136 buf
->cmd
= command
.cmd
;
137 buf
->cmdsize
= command
.cmdsize
;
138 if (read(fd
, ((char *)buf
+
139 sizeof(struct load_command
)),
141 fatal_unexec("cannot read load command data");
144 save_command(buf
, the_commands
, the_commands_len
);
151 vm_address_t start_address
,
153 vm_address_t end_address
156 vm_address_t address
;
159 address
= (start_address
+ *size
);
160 gapsize
= end_address
- address
;
162 if (vm_allocate(task_self(), &address
, gapsize
,
163 FALSE
) != KERN_SUCCESS
) {
164 fatal_unexec("cannot vm_allocate");
172 vm_address_t
*address
,
178 struct section
*sect
;
180 sect
= (struct section
*) getsectbyname(SEG_DATA
, SECT_DATA
);
184 ret
= vm_region(task_self(),
188 ®ion
.max_protection
,
193 if (ret
!= KERN_SUCCESS
|| region
.address
>= VM_HIGHDATA
) {
197 if (region
.address
> *address
+ *size
) {
198 if (!filldatagap(*address
, size
,
203 *size
+= region
.size
;
205 if (region
.address
== sect
->addr
) {
206 *address
= region
.address
;
210 region
.address
+= region
.size
;
220 vm_address_t address
;
222 if (vm_allocate(task_self(), &address
, size
, TRUE
) != KERN_SUCCESS
) {
225 return ((char *)address
);
234 vm_deallocate(task_self(), (vm_address_t
)buf
, size
);
244 struct load_command
**the_commands
= NULL
;
245 unsigned the_commands_len
;
246 struct mach_header the_header
;
253 vm_address_t data_address
;
255 vm_size_t vmaddr_growth
= 0;
256 vm_size_t dataseg_vmaddr
, dataseg_vmend
;
258 struct segment_command
*segment
;
261 unsigned long extreloff
= 0;
262 unsigned long nextrel
= 0;
263 struct dysymtab_command
*dysymtab
;
264 struct relocation_info reloc_info
;
267 if (!read_macho(infd
, &the_header
, &the_commands
, &the_commands_len
)) {
272 malloc_cookie
= malloc_freezedry ();
273 if (!get_data_region(&data_address
, &data_size
)) {
279 * DO NOT USE MALLOC IN THIS SECTION
285 for (i
= 0; i
< the_commands_len
; i
++) {
286 switch (the_commands
[i
]->cmd
) {
288 segment
= ((struct segment_command
*)
290 if (strcmp(segment
->segname
, SEG_DATA
) == 0) {
291 fdatastart
= segment
->fileoff
;
292 fdatasize
= segment
->filesize
;
293 fgrowth
= (data_size
-
295 segment
->vmsize
= data_size
;
296 segment
->filesize
= data_size
;
297 dataseg_vmaddr
= segment
->vmaddr
;
298 dataseg_vmend
= segment
->vmaddr
+ segment
->vmsize
;
299 vmaddr_growth
= segment
->vmaddr
+ segment
->vmsize
;
301 ((struct segment_command
*)the_commands
[i
])->fileoff
+= fgrowth
;
304 if( strcmp( segment
->segname
, SEG_LINKEDIT
) == 0 ) {
305 segment
->vmaddr
= vmaddr_growth
;
310 ((struct symtab_command
*)
311 the_commands
[i
])->symoff
+= fgrowth
;
312 ((struct symtab_command
*)
313 the_commands
[i
])->stroff
+= fgrowth
;
316 ((struct symseg_command
*)
317 the_commands
[i
])->offset
+= fgrowth
;
321 dysymtab
= ((struct dysymtab_command
*)the_commands
[i
]);
322 extreloff
= dysymtab
->extreloff
;
323 nextrel
= dysymtab
->nextrel
;
324 dysymtab
->indirectsymoff
+= fgrowth
;
325 dysymtab
->extreloff
+= fgrowth
;
336 if (write(outfd
, &the_header
,
337 sizeof(the_header
)) != sizeof(the_header
)) {
338 fatal_unexec("cannot write output file");
345 for (i
= 0; i
< the_commands_len
; i
++) {
346 if (write(outfd
, the_commands
[i
],
347 the_commands
[i
]->cmdsize
) !=
348 the_commands
[i
]->cmdsize
) {
349 fatal_unexec("cannot write output file");
355 * Write original text
357 if (lseek(infd
, the_header
.sizeofcmds
+ sizeof(the_header
),
359 fatal_unexec("cannot seek input file");
362 size
= fdatastart
- (sizeof(the_header
) +
363 the_header
.sizeofcmds
);
364 buf
= my_malloc(size
);
365 if (read(infd
, buf
, size
) != size
) {
367 fatal_unexec("cannot read input file");
369 if (write(outfd
, buf
, size
) != size
) {
371 fatal_unexec("cannot write output file");
380 if (write(outfd
, (char *)data_address
,
381 data_size
) != data_size
) {
382 fatal_unexec("cannot write output file");
389 * OKAY TO USE MALLOC NOW
396 if (lseek(infd
, fdatasize
, L_INCR
) < 0) {
397 fatal_unexec("cannot seek input file");
400 size
= st
.st_size
- lseek(infd
, 0, L_INCR
);
403 if (read(infd
, buf
, size
) != size
) {
405 fatal_unexec("cannot read input file");
408 if (write(outfd
, buf
, size
) != size
) {
410 fatal_unexec("cannot write output file");
417 * Fix up relocation entries in the data segment.
420 if (lseek(infd
, extreloff
, L_SET
) < 0) {
421 fatal_unexec("cannot seek input file");
425 for (i
= 0; i
< nextrel
; i
++)
429 if (read(infd
, &reloc_info
, sizeof (reloc_info
)) != sizeof (reloc_info
)) {
430 fatal_unexec("cannot read input file");
433 if (reloc_info
.r_address
>= dataseg_vmaddr
&& reloc_info
.r_address
< dataseg_vmend
)
435 if (lseek (outfd
, fdatastart
+ reloc_info
.r_address
- dataseg_vmaddr
, L_SET
) < 0 ) {
436 fatal_unexec("cannot seek input file");
439 switch (reloc_info
.r_length
) {
441 if (write(outfd
, &zeroval
, 1) != 1) {
442 fatal_unexec("cannot write output file");
447 if (write(outfd
, &zeroval
, 2) != 2) {
448 fatal_unexec("cannot write output file");
453 if (write(outfd
, &zeroval
, 4) != 4) {
454 fatal_unexec("cannot write output file");
474 char tmpbuf
[L_tmpnam
];
477 infd
= open(infile
, O_RDONLY
, 0);
479 fatal_unexec("cannot open input file `%s'", infile
);
484 tmpfile
= rindex(tmpbuf
, '/');
485 if (tmpfile
== NULL
) {
490 outfd
= open(tmpfile
, O_WRONLY
|O_TRUNC
|O_CREAT
, 0755);
493 fatal_unexec("cannot open tmp file `%s'", tmpfile
);
496 if (!unexec_doit(infd
, outfd
)) {
504 if (rename(tmpfile
, outfile
) < 0) {
506 fatal_unexec("cannot rename `%s' to `%s'", tmpfile
, outfile
);