1 /* Dump Emacs in macho format.
2 Copyright (C) 1990, 1993, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 Written by Bradley Taylor (btaylor@next.com).
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
27 #include <mach/mach.h>
28 #include <mach-o/loader.h>
29 #include <mach-o/reloc.h>
33 /* Instead of unistd.h, this used to include libc.h.
34 "Nelson H. F. Beebe" <beebe@math.utah.edu> says that doesn't work
35 in system version 3.3. */
41 * Kludge: we don't expect any program data beyond VM_HIGHDATA
42 * What is really needed is a way to find out from malloc() which
43 * pages it vm_allocated and write only those out into the data segment.
45 * This kludge may break when we stop using fixed virtual address
46 * shared libraries. Actually, emacs will probably continue working, but be
47 * much larger on disk than it needs to be (because non-malloced data will
50 static const unsigned VM_HIGHDATA
= 0x2000000;
52 typedef struct region_t
{
56 vm_prot_t max_protection
;
57 vm_inherit_t inheritance
;
66 struct load_command
***the_commands
,
67 unsigned *the_commands_len
70 if (*the_commands
== NULL
) {
71 *the_commands_len
= 1;
72 *the_commands
= malloc(sizeof(*the_commands
));
74 (*the_commands_len
)++;
75 *the_commands
= realloc(*the_commands
,
77 sizeof(**the_commands
)));
84 struct load_command
*command
,
85 struct load_command
***the_commands
,
86 unsigned *the_commands_len
89 struct load_command
**tmp
;
91 grow(the_commands
, the_commands_len
);
92 tmp
= &(*the_commands
)[*the_commands_len
- 1];
93 *tmp
= malloc(command
->cmdsize
);
94 bcopy(command
, *tmp
, command
->cmdsize
);
98 fatal_unexec(char *format
, ...)
102 va_start(ap
, format
);
103 fprintf(stderr
, "unexec: ");
104 vfprintf(stderr
, format
, ap
);
105 fprintf(stderr
, "\n");
112 struct mach_header
*the_header
,
113 struct load_command
***the_commands
,
114 unsigned *the_commands_len
117 struct load_command command
;
118 struct load_command
*buf
;
122 if (read(fd
, the_header
, sizeof(*the_header
)) != sizeof(*the_header
)) {
123 fatal_unexec("cannot read macho header");
126 for (i
= 0; i
< the_header
->ncmds
; i
++) {
127 if (read(fd
, &command
, sizeof(struct load_command
)) !=
128 sizeof(struct load_command
)) {
129 fatal_unexec("cannot read macho load command header");
132 size
= command
.cmdsize
- sizeof(struct load_command
);
134 fatal_unexec("bogus load command size");
137 buf
= malloc(command
.cmdsize
);
138 buf
->cmd
= command
.cmd
;
139 buf
->cmdsize
= command
.cmdsize
;
140 if (read(fd
, ((char *)buf
+
141 sizeof(struct load_command
)),
143 fatal_unexec("cannot read load command data");
146 save_command(buf
, the_commands
, the_commands_len
);
153 vm_address_t start_address
,
155 vm_address_t end_address
158 vm_address_t address
;
161 address
= (start_address
+ *size
);
162 gapsize
= end_address
- address
;
164 if (vm_allocate(task_self(), &address
, gapsize
,
165 FALSE
) != KERN_SUCCESS
) {
166 fatal_unexec("cannot vm_allocate");
174 vm_address_t
*address
,
180 struct section
*sect
;
182 sect
= (struct section
*) getsectbyname(SEG_DATA
, SECT_DATA
);
186 ret
= vm_region(task_self(),
190 ®ion
.max_protection
,
195 if (ret
!= KERN_SUCCESS
|| region
.address
>= VM_HIGHDATA
) {
199 if (region
.address
> *address
+ *size
) {
200 if (!filldatagap(*address
, size
,
205 *size
+= region
.size
;
207 if (region
.address
== sect
->addr
) {
208 *address
= region
.address
;
212 region
.address
+= region
.size
;
222 vm_address_t address
;
224 if (vm_allocate(task_self(), &address
, size
, TRUE
) != KERN_SUCCESS
) {
227 return ((char *)address
);
236 vm_deallocate(task_self(), (vm_address_t
)buf
, size
);
246 struct load_command
**the_commands
= NULL
;
247 unsigned the_commands_len
;
248 struct mach_header the_header
;
255 vm_address_t data_address
;
257 vm_size_t vmaddr_growth
= 0;
258 vm_size_t dataseg_vmaddr
, dataseg_vmend
;
260 struct segment_command
*segment
;
263 unsigned long extreloff
= 0;
264 unsigned long nextrel
= 0;
265 struct dysymtab_command
*dysymtab
;
266 struct relocation_info reloc_info
;
269 if (!read_macho(infd
, &the_header
, &the_commands
, &the_commands_len
)) {
274 malloc_cookie
= malloc_freezedry ();
275 if (!get_data_region(&data_address
, &data_size
)) {
281 * DO NOT USE MALLOC IN THIS SECTION
287 for (i
= 0; i
< the_commands_len
; i
++) {
288 switch (the_commands
[i
]->cmd
) {
290 segment
= ((struct segment_command
*)
292 if (strcmp(segment
->segname
, SEG_DATA
) == 0) {
293 fdatastart
= segment
->fileoff
;
294 fdatasize
= segment
->filesize
;
295 fgrowth
= (data_size
-
297 segment
->vmsize
= data_size
;
298 segment
->filesize
= data_size
;
299 dataseg_vmaddr
= segment
->vmaddr
;
300 dataseg_vmend
= segment
->vmaddr
+ segment
->vmsize
;
301 vmaddr_growth
= segment
->vmaddr
+ segment
->vmsize
;
303 ((struct segment_command
*)the_commands
[i
])->fileoff
+= fgrowth
;
306 if( strcmp( segment
->segname
, SEG_LINKEDIT
) == 0 ) {
307 segment
->vmaddr
= vmaddr_growth
;
312 ((struct symtab_command
*)
313 the_commands
[i
])->symoff
+= fgrowth
;
314 ((struct symtab_command
*)
315 the_commands
[i
])->stroff
+= fgrowth
;
318 ((struct symseg_command
*)
319 the_commands
[i
])->offset
+= fgrowth
;
323 dysymtab
= ((struct dysymtab_command
*)the_commands
[i
]);
324 extreloff
= dysymtab
->extreloff
;
325 nextrel
= dysymtab
->nextrel
;
326 dysymtab
->indirectsymoff
+= fgrowth
;
327 dysymtab
->extreloff
+= fgrowth
;
338 if (write(outfd
, &the_header
,
339 sizeof(the_header
)) != sizeof(the_header
)) {
340 fatal_unexec("cannot write output file");
347 for (i
= 0; i
< the_commands_len
; i
++) {
348 if (write(outfd
, the_commands
[i
],
349 the_commands
[i
]->cmdsize
) !=
350 the_commands
[i
]->cmdsize
) {
351 fatal_unexec("cannot write output file");
357 * Write original text
359 if (lseek(infd
, the_header
.sizeofcmds
+ sizeof(the_header
),
361 fatal_unexec("cannot seek input file");
364 size
= fdatastart
- (sizeof(the_header
) +
365 the_header
.sizeofcmds
);
366 buf
= my_malloc(size
);
367 if (read(infd
, buf
, size
) != size
) {
369 fatal_unexec("cannot read input file");
371 if (write(outfd
, buf
, size
) != size
) {
373 fatal_unexec("cannot write output file");
382 if (write(outfd
, (char *)data_address
,
383 data_size
) != data_size
) {
384 fatal_unexec("cannot write output file");
391 * OKAY TO USE MALLOC NOW
398 if (lseek(infd
, fdatasize
, L_INCR
) < 0) {
399 fatal_unexec("cannot seek input file");
402 size
= st
.st_size
- lseek(infd
, 0, L_INCR
);
405 if (read(infd
, buf
, size
) != size
) {
407 fatal_unexec("cannot read input file");
410 if (write(outfd
, buf
, size
) != size
) {
412 fatal_unexec("cannot write output file");
419 * Fix up relocation entries in the data segment.
422 if (lseek(infd
, extreloff
, L_SET
) < 0) {
423 fatal_unexec("cannot seek input file");
427 for (i
= 0; i
< nextrel
; i
++)
431 if (read(infd
, &reloc_info
, sizeof (reloc_info
)) != sizeof (reloc_info
)) {
432 fatal_unexec("cannot read input file");
435 if (reloc_info
.r_address
>= dataseg_vmaddr
&& reloc_info
.r_address
< dataseg_vmend
)
437 if (lseek (outfd
, fdatastart
+ reloc_info
.r_address
- dataseg_vmaddr
, L_SET
) < 0 ) {
438 fatal_unexec("cannot seek input file");
441 switch (reloc_info
.r_length
) {
443 if (write(outfd
, &zeroval
, 1) != 1) {
444 fatal_unexec("cannot write output file");
449 if (write(outfd
, &zeroval
, 2) != 2) {
450 fatal_unexec("cannot write output file");
455 if (write(outfd
, &zeroval
, 4) != 4) {
456 fatal_unexec("cannot write output file");
476 char tmpbuf
[L_tmpnam
];
479 infd
= open(infile
, O_RDONLY
, 0);
481 fatal_unexec("cannot open input file `%s'", infile
);
486 tmpfile
= rindex(tmpbuf
, '/');
487 if (tmpfile
== NULL
) {
492 outfd
= open(tmpfile
, O_WRONLY
|O_TRUNC
|O_CREAT
, 0755);
495 fatal_unexec("cannot open tmp file `%s'", tmpfile
);
498 if (!unexec_doit(infd
, outfd
)) {
506 if (rename(tmpfile
, outfile
) < 0) {
508 fatal_unexec("cannot rename `%s' to `%s'", tmpfile
, outfile
);
513 /* arch-tag: 9796bdc3-c050-417a-b2f5-4cfd31032634
514 (do not change this comment) */