1/*
2 * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23#ifdef SHLIB
24#include "shlib.h"
25#endif /* SHLIB */
26/*
27 * This file contains the routines that drives pass2 of the link-editor.  In
28 * pass2 the output is created and written.  The sections from the input files
29 * are copied into the output and relocated.  The headers, relocation entries,
30 * symbol table and string table are all copied into the output file.
31 */
32#include <stdlib.h>
33#if !(defined(KLD) && defined(__STATIC__))
34#include <libc.h>
35#include <stdio.h>
36#include <mach/mach.h>
37#else /* defined(KLD) && defined(__STATIC__) */
38#include <mach/kern_return.h>
39#endif /* !(defined(KLD) && defined(__STATIC__)) */
40#include <stdarg.h>
41#include <string.h>
42#include <sys/file.h>
43#include <sys/types.h>
44#include <sys/stat.h>
45#include "stuff/openstep_mach.h"
46#include <mach-o/loader.h>
47#include <mach-o/nlist.h>
48#include <mach-o/reloc.h>
49#include "stuff/bool.h"
50#include "stuff/bytesex.h"
51#include "stuff/macosx_deployment_target.h"
52#include "stuff/unix_standard_mode.h"
53
54#include "ld.h"
55#include "live_refs.h"
56#include "objects.h"
57#include "fvmlibs.h"
58#include "dylibs.h"
59#include "sections.h"
60#include "pass1.h"
61#include "symbols.h"
62#include "layout.h"
63#include "pass2.h"
64#include "sets.h"
65#include "indirect_sections.h"
66
67/*
68 * The total size of the output file and the memory buffer for the output file.
69 */
70__private_extern__ unsigned long output_size = 0;
71__private_extern__ char *output_addr = NULL;
72
73/*
74 * This is used to setting the SG_NORELOC flag in the segment flags correctly.
75 * This is an array of pointers to the merged sections in the output file that
76 * is used by the relocation routines to set the field 'referenced' in the
77 * merged section structure by indexing this array (directly without subtracting
78 * one from the section number) with the section number of a merged symbol that
79 * is refered to in a relocation entry.  The array is created by the routine
80 * create_output_sections_array() in here. Then after the 'referenced' field is
81 * set by the relocation routines (like generic_reloc() in generic_reloc.c) and
82 * the 'relocated' field is set by output_section() in sections.c then the
83 * routine set_SG_NORELOC_flags() in here can use these two fields to set the
84 * SG_NORELOC flag in the segments that have no relocation to or for them.
85 */
86__private_extern__ struct merged_section **output_sections = NULL;
87
88#ifndef RLD
89/* the file descriptor of the output file */
90static int fd = 0;
91
92/*
93 * This structure is used to describe blocks of the output file that are flushed
94 * to the disk file with output_flush.  It is kept in an ordered list starting
95 * with output_blocks.
96 */
97static struct block {
98    unsigned long offset;	/* starting offset of this block */
99    unsigned long size;		/* size of this block */
100    unsigned long written_offset;/* first page offset after starting offset */
101    unsigned long written_size;	/* size of written area from written_offset */
102    struct block *next; /* next block in the list */
103} *output_blocks;
104
105static void setup_output_flush(void);
106static void final_output_flush(void);
107#ifdef DEBUG
108static void print_block_list(void);
109#endif /* DEBUG */
110static struct block *get_block(void);
111static void remove_block(struct block *block);
112static unsigned long trnc(unsigned long v, unsigned long r);
113#endif /* !defined(RLD) */
114static void create_output_sections_array(void);
115static void set_SG_NORELOC_flags(void);
116static void output_headers(void);
117
118/*
119 * pass2() creates the output file and the memory buffer to create the file
120 * into.  It drives the process to get everything copied into the buffer for
121 * the output file.  It then writes the output file and deallocates the buffer.
122 */
123__private_extern__
124void
125pass2(void)
126{
127    unsigned long i, j, section_type;
128    struct object_list *object_list, **p;
129#ifndef RLD
130    int mode;
131    struct stat stat_buf;
132    kern_return_t r;
133
134	/*
135	 * In UNIX standard conformance mode we are not allowed to replace
136	 * a file that is not writeable.
137	 */
138	if(get_unix_standard_mode() == TRUE &&
139	   access(outputfile, F_OK) == 0 &&
140	   access(outputfile, W_OK) == -1)
141	    system_fatal("can't write output file: %s", outputfile);
142
143	/*
144	 * Create the output file.  The unlink() is done to handle the problem
145	 * when the outputfile is not writable but the directory allows the
146	 * file to be removed (since the file may not be there the return code
147	 * of the unlink() is ignored).
148	 */
149	(void)unlink(outputfile);
150	if((fd = open(outputfile, O_WRONLY | O_CREAT | O_TRUNC, 0777)) == -1)
151	    system_fatal("can't create output file: %s", outputfile);
152#ifdef F_NOCACHE
153        /* tell filesystem to NOT cache the file when reading or writing */
154	(void)fcntl(fd, F_NOCACHE, 1);
155#endif
156	if(fstat(fd, &stat_buf) == -1)
157	    system_fatal("can't stat file: %s", outputfile);
158	/*
159	 * Turn the execute bits on or off depending if there are any undefined
160	 * symbols in the output file.  If the file existed before the above
161	 * open() call the creation mode in that call would have been ignored
162	 * so it has to be set explicitly in any case.
163	 */
164	if(output_mach_header.flags & MH_NOUNDEFS ||
165	   (has_dynamic_linker_command && output_for_dyld))
166	    mode = (stat_buf.st_mode & 0777) | (0111 & ~umask(0));
167	else
168	    mode = (stat_buf.st_mode & 0777) & ~0111;
169	if(fchmod(fd, mode) == -1)
170	    system_fatal("can't set execution permissions output file: %s",
171			 outputfile);
172
173	/*
174	 * Create the buffer to copy the parts of the output file into.
175	 */
176	if((r = vm_allocate(mach_task_self(), (vm_address_t *)&output_addr,
177			    output_size, TRUE)) != KERN_SUCCESS)
178	    mach_fatal(r, "can't vm_allocate() buffer for output file of size "
179		       "%lu", output_size);
180
181	/*
182	 * Set up for flushing pages to the output file as they fill up.
183	 */
184	if(flush)
185	    setup_output_flush();
186
187	/*
188	 * Make sure pure_instruction sections are padded with nop's.
189	 */
190	nop_pure_instruction_scattered_sections();
191
192#endif /* !defined(RLD) */
193
194	/*
195	 * The strings indexes for the merged string blocks need to be set
196	 * before the dylib tables are output because the module names are in
197	 * them as well as the merged symbol names.
198	 */
199	set_merged_string_block_indexes();
200
201#ifndef RLD
202	/*
203	 * Copy the dylib tables into the output file.  This is done before the
204	 * sections are outputted so that the indexes to the local and external
205	 * relocation entries for each object can be used as running indexes as
206	 * each section in the object is outputted.
207	 */
208	if(filetype == MH_DYLIB)
209	    output_dylib_tables();
210#endif /* !defined(RLD) */
211
212	/*
213	 * Create the array of pointers to merged sections in the output file
214	 * so the relocation routines can use it to set the 'referenced' fields
215	 * in the merged section structures.
216	 */
217	create_output_sections_array();
218
219	/*
220	 * Copy the merged literal sections and the sections created from files
221	 * into the output object file.
222	 */
223	output_literal_sections();
224#ifndef RLD
225	output_sections_from_files();
226#endif /* !defined(RLD) */
227
228	/*
229	 * For each non-literal content section in each object file loaded
230	 * relocate it into the output file (along with the relocation entries).
231	 * Then relocate local symbols into the output file for the loaded
232	 * objects.
233	 */
234	for(p = &objects; *p; p = &(object_list->next)){
235	    object_list = *p;
236	    for(i = 0; i < object_list->used; i++){
237		cur_obj = &(object_list->object_files[i]);
238		/* print the object file name if tracing */
239		if(trace){
240		    print_obj_name(cur_obj);
241		    print("\n");
242		}
243		if(cur_obj->dylib)
244		    continue;
245		if(cur_obj->bundle_loader)
246		    continue;
247		if(cur_obj->dylinker)
248		    continue;
249		if(cur_obj != base_obj){
250		    for(j = 0; j < cur_obj->nsection_maps; j++){
251			if(cur_obj->section_maps[j].s->flags & S_ATTR_DEBUG)
252			    continue;
253#ifdef RLD
254			if(cur_obj->set_num == cur_set)
255#endif /* RLD */
256			{
257			    section_type = (cur_obj->section_maps[j].s->flags &
258                                                   SECTION_TYPE);
259			    if(section_type == S_REGULAR ||
260			       section_type == S_SYMBOL_STUBS ||
261			       section_type == S_NON_LAZY_SYMBOL_POINTERS ||
262			       section_type == S_LAZY_SYMBOL_POINTERS ||
263			       section_type == S_COALESCED ||
264			       section_type == S_MOD_INIT_FUNC_POINTERS ||
265			       section_type == S_MOD_TERM_FUNC_POINTERS){
266				output_section(&(cur_obj->section_maps[j]));
267			    }
268			}
269		    }
270		}
271		output_local_symbols();
272#ifdef VM_SYNC_DEACTIVATE
273		vm_msync(mach_task_self(), (vm_address_t)cur_obj->obj_addr,
274			 (vm_size_t)cur_obj->obj_size, VM_SYNC_DEACTIVATE);
275#endif /* VM_SYNC_DEACTIVATE */
276	    }
277	}
278	/*
279	 * If there were errors in output_section() then return as so not
280	 * to cause later internal errors.
281	 */
282	if(errors != 0)
283	    return;
284
285#ifdef RLD
286	/*
287	 * For each content section clean up the data structures not needed
288	 * after rld is run.  This must be done after ALL the sections are
289	 * output'ed because the fine relocation entries could be used by any
290	 * of the sections.
291	 */
292	for(p = &objects; *p; p = &(object_list->next)){
293	    object_list = *p;
294	    for(i = 0; i < object_list->used; i++){
295		cur_obj = &(object_list->object_files[i]);
296		for(j = 0; j < cur_obj->nsection_maps; j++){
297		    if(cur_obj->section_maps[j].nfine_relocs != 0){
298			free(cur_obj->section_maps[j].fine_relocs);
299			cur_obj->section_maps[j].fine_relocs = NULL;
300			cur_obj->section_maps[j].nfine_relocs = 0;
301		    }
302		}
303		if(cur_obj->nundefineds != 0){
304		    free(cur_obj->undefined_maps);
305		    cur_obj->undefined_maps = NULL;
306		    cur_obj->nundefineds = 0;
307		}
308	    }
309	}
310#endif /* RLD */
311
312	/*
313	 * Set the SG_NORELOC flag in the segments that had no relocation to
314	 * or for them.
315	 */
316	set_SG_NORELOC_flags();
317
318#ifndef SA_RLD
319	/*
320	 * Copy the indirect symbol table into the output file.
321	 */
322	output_indirect_symbols();
323#endif /* SA_RLD */
324
325	/*
326	 * Copy the merged symbol table into the output file.
327	 */
328	output_merged_symbols();
329
330	/*
331	 * Copy the headers into the output file.
332	 */
333	output_headers();
334
335#ifndef RLD
336	if(flush){
337	    /*
338	     * Flush the sections that have been scatter loaded.
339	     */
340	    flush_scatter_copied_sections();
341	    /*
342	     * flush the remaining part of the object file that is not a full
343	     * page.
344	     */
345	    final_output_flush();
346	}
347	else{
348	    /*
349	     * Write the entire object file.
350	     */
351	    if(write(fd, output_addr, output_size) != (int)output_size)
352		system_fatal("can't write output file");
353
354	    if((r = vm_deallocate(mach_task_self(), (vm_address_t)output_addr,
355				  output_size)) != KERN_SUCCESS)
356		mach_fatal(r, "can't vm_deallocate() buffer for output file");
357	}
358#ifdef F_NOCACHE
359	/* re-enable caching of file reads/writes */
360	(void)fcntl(fd, F_NOCACHE, 0);
361#endif
362	if(close(fd) == -1)
363	    system_fatal("can't close output file");
364#endif /* RLD */
365}
366
367#if defined(RLD) && !defined(SA_RLD)
368/*
369 * pass2_rld_symfile() drives the process to get everything copied into the
370 * buffer for the output file.
371 */
372__private_extern__
373void
374pass2_rld_symfile(void)
375{
376	/*
377	 * Copy the merged symbol table into the output file.
378	 */
379	output_rld_symfile_merged_symbols();
380
381	/*
382	 * Copy the headers into the output file.
383	 */
384	/* first the mach header */
385	memcpy(output_addr, &output_mach_header, sizeof(struct mach_header));
386
387	/* next the symbol table load command */
388	memcpy(output_addr + sizeof(struct mach_header),
389	       &(output_symtab_info.symtab_command),
390	       output_symtab_info.symtab_command.cmdsize);
391}
392#endif /* defined(RLD) && !defined(SA_RLD) */
393
394/*
395 * create_output_sections_array() creates the output_sections array and fills
396 * it in with the pointers to the merged sections in the output file.  This
397 * is used by the relocation routines to set the field 'referenced' in the
398 * merged section structure by indexing this array (directly without subtracting
399 * one from the section number) with the section number of a merged symbol that
400 * is refered to in a relocation entry.
401 */
402__private_extern__
403void
404create_output_sections_array(void)
405{
406    unsigned long i, nsects;
407    struct merged_segment **p, *msg;
408    struct merged_section **content, **zerofill, *ms;
409
410	nsects = 1;
411	p = &merged_segments;
412	while(*p){
413	    msg = *p;
414	    nsects += msg->sg.nsects;
415	    p = &(msg->next);
416	}
417
418	output_sections = (struct merged_section **)
419			  allocate(nsects * sizeof(struct merged_section *));
420
421	i = 1;
422	p = &merged_segments;
423	while(*p){
424	    msg = *p;
425	    content = &(msg->content_sections);
426	    while(*content){
427		ms = *content;
428		output_sections[i++] = ms;
429		content = &(ms->next);
430	    }
431	    zerofill = &(msg->zerofill_sections);
432	    while(*zerofill){
433		ms = *zerofill;
434		output_sections[i++] = ms;
435		zerofill = &(ms->next);
436	    }
437	    p = &(msg->next);
438	}
439}
440
441/*
442 * set_SG_NORELOC_flags() sets the SG_NORELOC flag in the segment that have no
443 * relocation to or from them.  This uses the fields 'referenced' and
444 * 'relocated' in the merged section structures.  The array that was created
445 * by the routine create_output_sections_array() to help set the above
446 * 'referenced' field is deallocated in here.
447 */
448static
449void
450set_SG_NORELOC_flags(void)
451{
452    struct merged_segment **p, *msg;
453    struct merged_section **content, **zerofill, *ms;
454    enum bool relocated, referenced;
455
456	free(output_sections);
457	output_sections = NULL;
458
459	p = &merged_segments;
460	while(*p){
461	    relocated = FALSE;
462	    referenced = FALSE;
463	    msg = *p;
464	    content = &(msg->content_sections);
465	    while(*content){
466		ms = *content;
467		if(ms->relocated == TRUE)
468		    relocated = TRUE;
469		if(ms->referenced == TRUE)
470		    referenced = TRUE;
471		content = &(ms->next);
472	    }
473	    zerofill = &(msg->zerofill_sections);
474	    while(*zerofill){
475		ms = *zerofill;
476		/* a zero fill section can't be relocated */
477		if(ms->referenced == TRUE)
478		    referenced = TRUE;
479		zerofill = &(ms->next);
480	    }
481	    if(relocated == FALSE && referenced == FALSE)
482		msg->sg.flags |= SG_NORELOC;
483	    p = &(msg->next);
484	}
485}
486
487#ifndef RLD
488/*
489 * setup_output_flush() flushes the gaps between things in the file that are
490 * holes created by alignment.  This must stay in lock step with the layout
491 * routine that lays out the file (layout_segments() in layout.c).
492 */
493static
494void
495setup_output_flush(void)
496{
497    unsigned long offset;
498    struct merged_segment **p, *msg;
499    struct merged_section **content, *ms;
500
501	offset = sizeof(struct mach_header) + output_mach_header.sizeofcmds;
502
503	/* the offsets to the contents of the sections */
504	p = &merged_segments;
505	while(*p){
506	    msg = *p;
507	    content = &(msg->content_sections);
508	    while(*content){
509		ms = *content;
510		if(ms->s.size != 0){
511		    if(ms->s.offset != offset)
512			output_flush(offset, ms->s.offset - offset);
513		    offset = ms->s.offset + ms->s.size;
514		}
515		content = &(ms->next);
516	    }
517	    p = &(msg->next);
518	}
519
520	/* the offsets to the relocation entries */
521	p = &merged_segments;
522	while(*p){
523	    msg = *p;
524	    content = &(msg->content_sections);
525	    while(*content){
526		ms = *content;
527		if(ms->s.nreloc != 0){
528		    if(ms->s.reloff != offset)
529			output_flush(offset, ms->s.reloff - offset);
530		    offset = ms->s.reloff +
531			     ms->s.nreloc * sizeof(struct relocation_info);
532		}
533		content = &(ms->next);
534	    }
535	    p = &(msg->next);
536	}
537	if(output_dysymtab_info.dysymtab_command.nlocrel != 0){
538	    output_flush(offset,
539			 output_dysymtab_info.dysymtab_command.locreloff -
540			 offset);
541	    offset = output_dysymtab_info.dysymtab_command.locreloff +
542		     output_dysymtab_info.dysymtab_command.nlocrel *
543		     sizeof(struct relocation_info);
544	}
545	if(output_for_dyld){
546	    if(strip_level != STRIP_ALL){
547		/* the offset to the symbol table */
548		if(output_symtab_info.symtab_command.symoff != offset)
549		    output_flush(offset,
550			output_symtab_info.symtab_command.symoff - offset);
551		offset = output_symtab_info.symtab_command.symoff +
552			 output_symtab_info.symtab_command.nsyms *
553							sizeof(struct nlist);
554	    }
555	}
556	if(output_for_dyld && twolevel_namespace == TRUE &&
557	   twolevel_namespace_hints == TRUE){
558	    output_flush(offset,
559			 output_hints_info.twolevel_hints_command.offset -
560			 offset);
561	    offset = output_hints_info.twolevel_hints_command.offset +
562		     output_hints_info.twolevel_hints_command.nhints *
563		     sizeof(struct twolevel_hint);
564	}
565	if(output_dysymtab_info.dysymtab_command.nextrel != 0){
566	    output_flush(offset,
567			 output_dysymtab_info.dysymtab_command.extreloff -
568			 offset);
569	    offset = output_dysymtab_info.dysymtab_command.extreloff +
570		     output_dysymtab_info.dysymtab_command.nextrel *
571		     sizeof(struct relocation_info);
572	}
573	/* the offset to the indirect symbol table */
574	if(output_dysymtab_info.dysymtab_command.nindirectsyms != 0){
575	    if(output_dysymtab_info.dysymtab_command.indirectsymoff != offset)
576		output_flush(offset, output_dysymtab_info.
577			     dysymtab_command.indirectsymoff - offset);
578	    offset = output_dysymtab_info.dysymtab_command.indirectsymoff +
579		     output_dysymtab_info.dysymtab_command.nindirectsyms *
580							sizeof(unsigned long);
581	}
582#ifndef RLD
583	/* the offset to the dylib table of contents */
584	if(output_dysymtab_info.dysymtab_command.ntoc != 0){
585	    if(output_dysymtab_info.dysymtab_command.tocoff != offset)
586		output_flush(offset, output_dysymtab_info.
587			     dysymtab_command.tocoff - offset);
588	    offset = output_dysymtab_info.dysymtab_command.tocoff +
589		     output_dysymtab_info.dysymtab_command.ntoc *
590					sizeof(struct dylib_table_of_contents);
591	}
592
593	/* the offset to the dylib module table */
594	if(output_dysymtab_info.dysymtab_command.nmodtab != 0){
595	    if(output_dysymtab_info.dysymtab_command.modtaboff != offset)
596		output_flush(offset, output_dysymtab_info.
597			     dysymtab_command.modtaboff - offset);
598	    offset = output_dysymtab_info.dysymtab_command.modtaboff +
599		     output_dysymtab_info.dysymtab_command.nmodtab *
600					sizeof(struct dylib_module);
601	}
602
603	/* the offset to the dylib reference table */
604	if(output_dysymtab_info.dysymtab_command.nextrefsyms != 0){
605	    if(output_dysymtab_info.dysymtab_command.extrefsymoff != offset)
606		output_flush(offset, output_dysymtab_info.
607			     dysymtab_command.extrefsymoff - offset);
608	    offset = output_dysymtab_info.dysymtab_command.extrefsymoff +
609		     output_dysymtab_info.dysymtab_command.nextrefsyms *
610					sizeof(struct dylib_reference);
611	}
612#endif /* !defined(RLD) */
613
614	if(output_for_dyld == FALSE){
615	    if(strip_level != STRIP_ALL){
616		/* the offset to the symbol table */
617		if(output_symtab_info.symtab_command.symoff != offset)
618		    output_flush(offset,
619			output_symtab_info.symtab_command.symoff - offset);
620		offset = output_symtab_info.symtab_command.symoff +
621			 output_symtab_info.symtab_command.nsyms *
622							sizeof(struct nlist);
623	    }
624	}
625
626	if(strip_level != STRIP_ALL){
627	    /* the offset to the string table */
628	    /*
629	     * This is flushed to output_symtab_info.symtab_command.stroff plus
630	     * output_symtab_info.output_merged_strsize and not just to
631	     * output_symtab_info.symtab_command.stroff because the first byte
632	     * can't be used to store a string because a symbol with a string
633	     * offset of zero (nlist.n_un.n_strx == 0) is defined to be a symbol
634	     * with a null name "".  So this byte(s) have to be flushed.
635	     */
636	    if(output_symtab_info.symtab_command.stroff +
637	       output_symtab_info.output_merged_strsize != offset)
638		output_flush(offset, output_symtab_info.symtab_command.stroff +
639			     output_symtab_info.output_merged_strsize - offset);
640	    /* flush the string table pad if any */
641	    if(output_symtab_info.output_strpad != 0){
642		output_flush(output_symtab_info.symtab_command.stroff +
643			     output_symtab_info.symtab_command.strsize -
644			     output_symtab_info.output_strpad,
645			     output_symtab_info.output_strpad);
646	    }
647	    offset = output_symtab_info.symtab_command.stroff +
648		     output_symtab_info.symtab_command.strsize;
649	}
650
651	/* the offset to the end of the file */
652	if(offset != output_size)
653	    output_flush(offset, output_size - offset);
654}
655
656/*
657 * output_flush() takes an offset and a size of part of the output file, known
658 * in the comments as the new area, and causes any fully flushed pages to be
659 * written to the output file the new area in combination with previous areas
660 * creates.  The data structure output_blocks has ordered blocks of areas that
661 * have been flushed which are maintained by this routine.  Any area can only
662 * be flushed once and an error will result is the new area overlaps with a
663 * previously flushed area.
664 *
665 * The goal of this is to again minimize the number of dirty pages the link
666 * editor has and hopfully improve performance in a memory starved system and
667 * to prevent these pages to be written to the swap area when they could just be
668 * written to the output file (if only external pagers worked well ...).
669 */
670__private_extern__
671void
672output_flush(
673unsigned long offset,
674unsigned long size)
675{
676    unsigned long write_offset, write_size;
677    struct block **p, *block, *before, *after;
678    kern_return_t r;
679
680	if(flush == FALSE)
681	    return;
682
683/*
684if(offset == 588824 && size != 0)
685printf("in output_flush() offset = %lu size = %lu\n", offset, size);
686*/
687
688	if(offset + size > output_size)
689	    fatal("internal error: output_flush(offset = %lu, size = %lu) out "
690		  "of range for output_size = %lu", offset, size, output_size);
691
692#ifdef DEBUG
693	if(debug & (1 << 12))
694	    print_block_list();
695	if(debug & (1 << 11))
696	    print("output_flush(offset = %lu, size %lu)", offset, size);
697#endif /* DEBUG */
698
699	if(size == 0){
700#ifdef DEBUG
701	if(debug & (1 << 11))
702	    print("\n");
703#endif /* DEBUG */
704	    return;
705	}
706
707	/*
708	 * Search through the ordered output blocks to find the block before the
709	 * new area and after the new area if any exist.
710	 */
711	before = NULL;
712	after = NULL;
713	p = &(output_blocks);
714	while(*p){
715	    block = *p;
716	    if(offset < block->offset){
717		after = block;
718		break;
719	    }
720	    else{
721		before = block;
722	    }
723	    p = &(block->next);
724	}
725
726	/*
727	 * Check for overlap of the new area with the block before and after the
728	 * new area if there are such blocks.
729	 */
730	if(before != NULL){
731	    if(before->offset + before->size > offset){
732		warning("internal error: output_flush(offset = %lu, size = %lu) "
733		      "overlaps with flushed block(offset = %lu, size = %lu)",
734		      offset, size, before->offset, before->size);
735		printf("calling abort()\n");
736		abort();
737	    }
738	}
739	if(after != NULL){
740	    if(offset + size > after->offset){
741		warning("internal error: output_flush(offset = %lu, size = %lu) "
742		      "overlaps with flushed block(offset = %lu, size = %lu)",
743		      offset, size, after->offset, after->size);
744		printf("calling abort()\n");
745		abort();
746	    }
747	}
748
749	/*
750	 * Now see how the new area fits in with the blocks before and after it
751	 * (that is does it touch both, one or the other or neither blocks).
752	 * For each case first the offset and size to write (write_offset and
753	 * write_size) are set for the area of full pages that can now be
754	 * written from the block.  Then the area written in the block
755	 * (->written_offset and ->written_size) are set to reflect the total
756	 * area in the block now written.  Then offset and size the block
757	 * refers to (->offset and ->size) are set to total area of the block.
758	 * Finally the links to others blocks in the list are adjusted if a
759	 * block is added or removed.
760	 *
761	 * See if there is a block before the new area and the new area
762	 * starts at the end of that block.
763	 */
764	if(before != NULL && before->offset + before->size == offset){
765	    /*
766	     * See if there is also a block after the new area and the new area
767	     * ends at the start of that block.
768	     */
769	    if(after != NULL && offset + size == after->offset){
770		/*
771		 * This is the case where the new area exactly fill the area
772		 * between two existing blocks.  The total area is folded into
773		 * the block before the new area and the block after the new
774		 * area is removed from the list.
775		 */
776		if(before->offset == 0 && before->written_size == 0){
777		    write_offset = 0;
778		    before->written_offset = 0;
779		}
780		else
781		    write_offset =before->written_offset + before->written_size;
782		if(after->written_size == 0)
783		    write_size = trnc(after->offset + after->size -
784				      write_offset, host_pagesize);
785		else
786		    write_size = trnc(after->written_offset - write_offset,
787				      host_pagesize);
788		if(write_size != 0){
789		    before->written_size += write_size;
790		}
791		if(after->written_size != 0)
792		    before->written_size += after->written_size;
793		before->size += size + after->size;
794
795		/* remove the block after the new area */
796		before->next = after->next;
797		remove_block(after);
798	    }
799	    else{
800		/*
801		 * This is the case where the new area starts at the end of the
802		 * block just before it but does not end where the block after
803		 * it (if any) starts.  The new area is folded into the block
804		 * before the new area.
805		 */
806		write_offset = before->written_offset + before->written_size;
807		write_size = trnc(offset + size - write_offset, host_pagesize);
808		if(write_size != 0)
809		    before->written_size += write_size;
810		before->size += size;
811	    }
812	}
813	/*
814	 * See if the new area and the new area ends at the start of the block
815	 * after it (if any).
816	 */
817	else if(after != NULL && offset + size == after->offset){
818	    /*
819	     * This is the case where the new area ends at the begining of the
820	     * block just after it but does not start where the block before it.
821	     * (if any) ends.  The new area is folded into this block after the
822	     * new area.
823	     */
824	    write_offset = rnd(offset, host_pagesize);
825	    if(after->written_size == 0)
826		write_size = trnc(after->offset + after->size - write_offset,
827				  host_pagesize);
828	    else
829		write_size = trnc(after->written_offset - write_offset,
830				  host_pagesize);
831	    if(write_size != 0){
832		after->written_offset = write_offset;
833		after->written_size += write_size;
834	    }
835	    else if(write_offset != after->written_offset){
836		after->written_offset = write_offset;
837	    }
838	    after->offset = offset;
839	    after->size += size;
840	}
841	else{
842	    /*
843	     * This is the case where the new area neither starts at the end of
844	     * the block just before it (if any) or ends where the block after
845	     * it (if any) starts.  A new block is created and the new area is
846	     * is placed in it.
847	     */
848	    write_offset = rnd(offset, host_pagesize);
849	    write_size = trnc(offset + size - write_offset, host_pagesize);
850	    block = get_block();
851	    block->offset = offset;
852	    block->size = size;
853	    block->written_offset = write_offset;
854	    block->written_size = write_size;
855	    /*
856	     * Insert this block in the ordered list in the correct place.
857	     */
858	    if(before != NULL){
859		block->next = before->next;
860		before->next = block;
861	    }
862	    else{
863		block->next = output_blocks;
864		output_blocks = block;
865	    }
866	}
867
868	/*
869	 * Now if there are full pages to write write them to the output file.
870	 */
871	if(write_size != 0){
872#ifdef DEBUG
873	if((debug & (1 << 11)) || (debug & (1 << 10)))
874	    print(" writing (write_offset = %lu write_size = %lu)\n",
875		   write_offset, write_size);
876#endif /* DEBUG */
877	    lseek(fd, write_offset, L_SET);
878	    if(write(fd, output_addr + write_offset, write_size) !=
879	       (int)write_size)
880		system_fatal("can't write to output file");
881	    if((r = vm_deallocate(mach_task_self(), (vm_address_t)(output_addr +
882				  write_offset), write_size)) != KERN_SUCCESS)
883		mach_fatal(r, "can't vm_deallocate() buffer for output file");
884	}
885#ifdef DEBUG
886	else{
887	    if(debug & (1 << 11))
888		print(" no write\n");
889	}
890#endif /* DEBUG */
891}
892
893/*
894 * final_output_flush() flushes the last part of the last page of the object
895 * file if it does not round out to exactly a page.
896 */
897static
898void
899final_output_flush(void)
900{
901    struct block *block;
902    unsigned long write_offset, write_size;
903    kern_return_t r;
904
905#ifdef DEBUG
906	/* The compiler "warning: `write_offset' may be used uninitialized in */
907	/* this function" can safely be ignored */
908	write_offset = 0;
909	if((debug & (1 << 11)) || (debug & (1 << 10))){
910	    print("final_output_flush block_list:\n");
911	    print_block_list();
912	}
913#endif /* DEBUG */
914
915	write_size = 0;
916	block = output_blocks;
917	if(block != NULL){
918	    if(block->offset != 0)
919		fatal("internal error: first block not at offset 0");
920	    if(block->written_size != 0){
921		if(block->written_offset != 0)
922		    fatal("internal error: first block written_offset not 0");
923		write_offset = block->written_size;
924		write_size = block->size - block->written_size;
925	    }
926	    else{
927		write_offset = block->offset;
928		write_size = block->size;
929	    }
930	    if(block->next != NULL)
931		fatal("internal error: more than one block in final list");
932	}
933	if(write_size != 0){
934#ifdef DEBUG
935	    if((debug & (1 << 11)) || (debug & (1 << 10)))
936		print(" writing (write_offset = %lu write_size = %lu)\n",
937		       write_offset, write_size);
938#endif /* DEBUG */
939	    lseek(fd, write_offset, L_SET);
940	    if(write(fd, output_addr + write_offset, write_size) !=
941	       (int)write_size)
942		system_fatal("can't write to output file");
943	    if((r = vm_deallocate(mach_task_self(), (vm_address_t)(output_addr +
944				  write_offset), write_size)) != KERN_SUCCESS)
945		mach_fatal(r, "can't vm_deallocate() buffer for output file");
946	}
947}
948
949#ifdef DEBUG
950/*
951 * print_block_list() prints the list of blocks.  Used for debugging.
952 */
953static
954void
955print_block_list(void)
956{
957    struct block **p, *block;
958
959	p = &(output_blocks);
960	if(*p == NULL)
961	    print("Empty block list\n");
962	while(*p){
963	    block = *p;
964	    print("block 0x%x\n", (unsigned int)block);
965	    print("    offset %lu\n", block->offset);
966	    print("    size %lu\n", block->size);
967	    print("    written_offset %lu\n", block->written_offset);
968	    print("    written_size %lu\n", block->written_size);
969	    print("    next 0x%x\n", (unsigned int)(block->next));
970	    p = &(block->next);
971	}
972}
973#endif /* DEBUG */
974
975/*
976 * get_block() returns a pointer to a new block.  This could be done by
977 * allocating block of these placing them on a free list and and handing them
978 * out.  The maximum number of blocks needed would be one for each content
979 * section, one for each section that has relocation entries (if saving them)
980 * and one for the symbol and string table.  For the initial release of this
981 * code this number is typicly around 8 it is not a big win so each block is
982 * just allocated and free'ed.
983 */
984static
985struct block *
986get_block(void)
987{
988    struct block *block;
989
990	block = allocate(sizeof(struct block));
991	return(block);
992}
993
994/*
995 * remove_block() throws away the block specified.  See comments in get_block().
996 */
997static
998void
999remove_block(
1000struct block *block)
1001{
1002	free(block);
1003}
1004
1005/*
1006 * trnc() truncates the value 'v' to the power of two value 'r'.  If v is
1007 * less than zero it returns zero.
1008 */
1009static
1010unsigned long
1011trnc(
1012unsigned long v,
1013unsigned long r)
1014{
1015	if(((long)v) < 0)
1016	    return(0);
1017	return(v & ~(r - 1));
1018}
1019#endif /* !defined(RLD) */
1020
1021/*
1022 * output_headers() copys the headers of the object file into the buffer for
1023 * the output file.
1024 */
1025static
1026void
1027output_headers(void)
1028{
1029    unsigned long header_offset;
1030    struct merged_segment **p, *msg;
1031    struct merged_section **content, **zerofill, *ms;
1032#ifndef RLD
1033    unsigned long i;
1034    struct merged_fvmlib **q, *mfl;
1035    struct dylinker_command *dyld;
1036    struct merged_dylib *mdl;
1037    struct dylib_command *dl;
1038    struct dynamic_library *dp;
1039    enum bool some_symbols_referenced, some_non_weak_refs;
1040#endif /* !defined(RLD) */
1041    struct mach_header *mh;
1042    struct load_command *lc;
1043
1044	header_offset = 0;
1045
1046	/* first the mach header */
1047	mh = (struct mach_header *)output_addr;
1048	memcpy(mh, &output_mach_header, sizeof(struct mach_header));
1049	header_offset += sizeof(struct mach_header);
1050	lc = (struct load_command *)(output_addr + header_offset);
1051
1052	/* next the segment load commands (and section structures) */
1053	p = &merged_segments;
1054	while(*p){
1055	    msg = *p;
1056	    memcpy(output_addr + header_offset, &(msg->sg),
1057		   sizeof(struct segment_command));
1058	    header_offset += sizeof(struct segment_command);
1059	    content = &(msg->content_sections);
1060	    while(*content){
1061		ms = *content;
1062		memcpy(output_addr + header_offset, &(ms->s),
1063		       sizeof(struct section));
1064		header_offset += sizeof(struct section);
1065		content = &(ms->next);
1066	    }
1067	    zerofill = &(msg->zerofill_sections);
1068	    while(*zerofill){
1069		ms = *zerofill;
1070		memcpy(output_addr + header_offset, &(ms->s),
1071		       sizeof(struct section));
1072		header_offset += sizeof(struct section);
1073		zerofill = &(ms->next);
1074	    }
1075	    p = &(msg->next);
1076	}
1077
1078#ifndef RLD
1079	/* next the fixed VM shared library load commands */
1080	q = &merged_fvmlibs;
1081	while(*q){
1082	    mfl = *q;
1083	    memcpy(output_addr + header_offset, mfl->fl, mfl->fl->cmdsize);
1084	    header_offset += mfl->fl->cmdsize;
1085	    q = &(mfl->next);
1086	}
1087
1088	/* next the dynamic linker load command */
1089	if(merged_dylinker != NULL){
1090	    memcpy(output_addr + header_offset, merged_dylinker->dyld,
1091		   merged_dylinker->dyld->cmdsize);
1092	    if(filetype != MH_DYLINKER){
1093		dyld = (struct dylinker_command *)(output_addr + header_offset);
1094		dyld->cmd = LC_LOAD_DYLINKER;
1095	    }
1096	    header_offset += merged_dylinker->dyld->cmdsize;
1097	}
1098
1099	/* next the dynamicly linked shared library load commands */
1100	mdl = merged_dylibs;
1101	while(mdl != NULL){
1102	    memcpy(output_addr + header_offset, mdl->dl, mdl->dl->cmdsize);
1103	    if(mdl->output_id == FALSE){
1104		dl = (struct dylib_command *)(output_addr + header_offset);
1105		/*
1106		 * Propagate the some_non_weak_refs and some_non_weak_refs
1107		 * booleans up through the sub images for this dylib.
1108		 */
1109		some_symbols_referenced =
1110		    mdl->dynamic_library->some_symbols_referenced;
1111		some_non_weak_refs =
1112		    mdl->dynamic_library->some_non_weak_refs;
1113		for(i = 0; i < mdl->dynamic_library->nsub_images; i++){
1114		    if(mdl->dynamic_library->sub_images[i]->
1115		       some_symbols_referenced == TRUE){
1116			some_symbols_referenced = TRUE;
1117			if(mdl->dynamic_library->sub_images[i]->
1118			   some_non_weak_refs == TRUE)
1119			    some_non_weak_refs = TRUE;
1120		    }
1121		}
1122		if((some_symbols_referenced == TRUE &&
1123		    some_non_weak_refs == FALSE) ||
1124		    mdl->dynamic_library->force_weak_dylib == TRUE){
1125		    if(macosx_deployment_target.major >= 2){
1126			dl->cmd = LC_LOAD_WEAK_DYLIB;
1127		    }
1128		    else{
1129			warning("dynamic shared library: %s not made a weak "
1130				"library in output with "
1131				"MACOSX_DEPLOYMENT_TARGET environment variable "
1132				"set to: %s", mdl->definition_object->file_name,
1133				macosx_deployment_target.name);
1134			dl->cmd = LC_LOAD_DYLIB;
1135		    }
1136		}
1137		else
1138		    dl->cmd = LC_LOAD_DYLIB;
1139	    }
1140	    header_offset += mdl->dl->cmdsize;
1141	    mdl = mdl->next;
1142	}
1143
1144	/* next the sub framework load command */
1145	if(merged_sub_framework != NULL){
1146	    memcpy(output_addr + header_offset, merged_sub_framework->sub,
1147		   merged_sub_framework->sub->cmdsize);
1148	    header_offset += merged_sub_framework->sub->cmdsize;
1149	}
1150
1151	/* next the sub umbrella load commands */
1152	for(i = 0; i < nsub_umbrellas ; i++){
1153	    memcpy(output_addr + header_offset, merged_sub_umbrellas[i].sub,
1154		   merged_sub_umbrellas[i].sub->cmdsize);
1155	    header_offset += merged_sub_umbrellas[i].sub->cmdsize;
1156	}
1157
1158	/* next the sub library load commands */
1159	for(i = 0; i < nsub_librarys ; i++){
1160	    memcpy(output_addr + header_offset, merged_sub_librarys[i].sub,
1161		   merged_sub_librarys[i].sub->cmdsize);
1162	    header_offset += merged_sub_librarys[i].sub->cmdsize;
1163	}
1164
1165	/* next the sub client load commands */
1166	for(i = 0; i < nallowable_clients ; i++){
1167	    memcpy(output_addr + header_offset, merged_sub_clients[i].sub,
1168		   merged_sub_clients[i].sub->cmdsize);
1169	    header_offset += merged_sub_clients[i].sub->cmdsize;
1170	}
1171
1172	/* next the prebound dynamic libraries load commands */
1173	if(filetype == MH_EXECUTE){
1174	    for(dp = dynamic_libs; dp != NULL; dp = dp->next){
1175		if(dp->type == DYLIB){
1176		    if(dp->pbdylib != NULL){
1177			memcpy(output_addr + header_offset, dp->pbdylib,
1178			       dp->pbdylib->cmdsize);
1179			header_offset += dp->pbdylib->cmdsize;
1180		    }
1181		}
1182	    }
1183	}
1184#endif /* !defined(RLD) */
1185
1186	/* next the symbol table load command */
1187	memcpy(output_addr + header_offset,
1188	       &(output_symtab_info.symtab_command),
1189	       output_symtab_info.symtab_command.cmdsize);
1190	header_offset += output_symtab_info.symtab_command.cmdsize;
1191
1192	/* next the dysymbol table load command */
1193	if(nindirectsyms != 0 || output_for_dyld){
1194	    memcpy(output_addr + header_offset,
1195		   &(output_dysymtab_info.dysymtab_command),
1196		   output_dysymtab_info.dysymtab_command.cmdsize);
1197	    header_offset += output_dysymtab_info.dysymtab_command.cmdsize;
1198	}
1199
1200	/* next the two-level namespace hints load command */
1201	if(output_for_dyld && twolevel_namespace == TRUE &&
1202	   twolevel_namespace_hints == TRUE){
1203	    memcpy(output_addr + header_offset,
1204	    	   &(output_hints_info.twolevel_hints_command),
1205		   output_hints_info.twolevel_hints_command.cmdsize);
1206	    header_offset += output_hints_info.twolevel_hints_command.cmdsize;
1207	}
1208
1209	/* next the prebind cksum load command */
1210	if(output_cksum_info.prebind_cksum_command.cmdsize != 0){
1211	    memcpy(output_addr + header_offset,
1212	    	   &(output_cksum_info.prebind_cksum_command),
1213		   output_cksum_info.prebind_cksum_command.cmdsize);
1214	    header_offset += output_cksum_info.prebind_cksum_command.cmdsize;
1215	}
1216
1217	/* next the uuid load command */
1218	if(output_uuid_info.uuid_command.cmdsize != 0){
1219	    memcpy(output_addr + header_offset,
1220	    	   &(output_uuid_info.uuid_command),
1221		   output_uuid_info.uuid_command.cmdsize);
1222	    header_offset += output_uuid_info.uuid_command.cmdsize;
1223	}
1224
1225	/* next the thread command if the output file has one */
1226	if(output_thread_info.thread_in_output == TRUE){
1227	    /* the thread command itself */
1228	    memcpy(output_addr + header_offset,
1229		   &(output_thread_info.thread_command),
1230		   sizeof(struct thread_command));
1231	    header_offset += sizeof(struct thread_command);
1232	    /* the flavor of the thread state */
1233	    memcpy(output_addr + header_offset, &(output_thread_info.flavor),
1234		   sizeof(long));
1235	    header_offset += sizeof(long);
1236	    /* the count of longs of the thread state */
1237	    memcpy(output_addr + header_offset, &(output_thread_info.count),
1238		   sizeof(long));
1239	    header_offset += sizeof(long);
1240	    /* the thread state */
1241	    memcpy(output_addr + header_offset, output_thread_info.state,
1242		   output_thread_info.count * sizeof(long));
1243	    header_offset += output_thread_info.count * sizeof(long);
1244	    /* the second thread state if any */
1245	    if(output_thread_info.second_count != 0){
1246		/* the flavor of the second thread state */
1247		memcpy(output_addr + header_offset,
1248		       &(output_thread_info.second_flavor),
1249		       sizeof(long));
1250		header_offset += sizeof(long);
1251		/* the count of longs of the second thread state */
1252		memcpy(output_addr + header_offset,
1253		       &(output_thread_info.second_count),
1254		       sizeof(long));
1255		header_offset += sizeof(long);
1256		/* the second thread state */
1257		memcpy(output_addr + header_offset,
1258		       output_thread_info.second_state,
1259		       output_thread_info.second_count * sizeof(long));
1260		header_offset += output_thread_info.second_count * sizeof(long);
1261	    }
1262	}
1263	/* next the routines command if the output file has one */
1264	if(output_routines_info.routines_in_output == TRUE){
1265	    memcpy(output_addr + header_offset,
1266		   &(output_routines_info.routines_command),
1267		   sizeof(struct routines_command));
1268	    header_offset += sizeof(struct routines_command);
1269	}
1270	if(host_byte_sex != target_byte_sex)
1271	    if(swap_object_headers(mh, lc) == FALSE)
1272		fatal("internal error: swap_object_headers() failed in "
1273		      "output_headers()");
1274#ifndef RLD
1275	output_flush(0, sizeof(struct mach_header) +
1276			output_mach_header.sizeofcmds);
1277#endif /* !defined(RLD) */
1278}
1279