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 deal with 8 byte literals sections.
28 * A literal in this section must beable to me moved freely with respect to
29 * other literals.  This means relocation must not reach outside the size of
30 * the literal.  The size of this this type of section must be a multiple of
31 * 8 bytes in all input files.
32 */
33#include <stdlib.h>
34#if !(defined(KLD) && defined(__STATIC__))
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 <mach-o/loader.h>
43#include "stuff/bool.h"
44#include "stuff/bytesex.h"
45
46#include "ld.h"
47#include "live_refs.h"
48#include "objects.h"
49#include "sections.h"
50#include "8byte_literals.h"
51#include "pass2.h"
52
53/*
54 * literal8_merge() merges 8 byte literals from the specified section in the
55 * current object file (cur_obj). When redo_live is FALSE it allocates a fine
56 * relocation map and sets the fine_relocs field in the section_map to it (as
57 * well as the count).  When redo_live is TRUE it re-merges only the live
58 * cstrings based on the live bit in the previouly allocated fine_relocs.
59 */
60__private_extern__
61void
62literal8_merge(
63struct literal8_data *data,
64struct merged_section *ms,
65struct section *s,
66struct section_map *section_map,
67enum bool redo_live)
68{
69    unsigned long nliteral8s, i;
70    struct literal8 *literal8s;
71    struct fine_reloc *fine_relocs;
72
73	if(s->size == 0){
74	    if(redo_live == FALSE){
75		section_map->fine_relocs = NULL;
76		section_map->nfine_relocs = 0;
77	    }
78	    return;
79	}
80	/*
81	 * Calcualte the number of literals so the size of the fine relocation
82	 * structures can be allocated.
83	 */
84	if(s->size % 8 != 0){
85	    error_with_cur_obj("8 byte literal section (%.16s,%.16s) size is "
86			       "not a multiple of 8 bytes", ms->s.segname,
87			       ms->s.sectname);
88	    return;
89	}
90	nliteral8s = s->size / 8;
91#ifdef DEBUG
92	if(redo_live == FALSE){
93	    data->nfiles++;
94	    data->nliterals += nliteral8s;
95	}
96#endif /* DEBUG */
97
98	/*
99	 * We will be called the first time with redo_live == FALSE and will
100	 * just merge the cstrings from the input file and create the
101	 * fine_relocs.
102	 */
103	if(redo_live == FALSE){
104	    fine_relocs = allocate(nliteral8s * sizeof(struct fine_reloc));
105	    memset(fine_relocs, '\0', nliteral8s * sizeof(struct fine_reloc));
106
107	    /*
108	     * lookup and enter each 8 byte literal in the section and record
109	     * the offsets in the input file and in the output file.
110	     */
111	    literal8s = (struct literal8 *)(cur_obj->obj_addr + s->offset);
112	    for(i = 0; i < nliteral8s; i++){
113		fine_relocs[i].input_offset = i * 8;
114		fine_relocs[i].output_offset =
115		    lookup_literal8(literal8s[i], data, ms);
116	    }
117	    section_map->fine_relocs = fine_relocs;
118	    section_map->nfine_relocs = nliteral8s;
119	}
120	else{
121	    /*
122	     * redo_live == TRUE and this is being called a second time after
123	     * all the literals were previouly merged when -dead_strip is
124	     * specified.  So now we walk the fine_relocs and only re-merge the
125	     * live literals.
126	     */
127	    fine_relocs = section_map->fine_relocs;
128	    nliteral8s = section_map->nfine_relocs;
129	    literal8s = (struct literal8 *)(cur_obj->obj_addr + s->offset);
130	    for(i = 0; i < nliteral8s; i++){
131		if(fine_relocs[i].live == TRUE){
132		    fine_relocs[i].output_offset =
133			lookup_literal8(literal8s[i], data, ms);
134		}
135		else{
136		    fine_relocs[i].output_offset = 0;
137		}
138	    }
139	}
140}
141
142/*
143 * literal8_order() enters 8 byte literals from the order_file from the merged
144 * section structure.  Since this is called before any call to literal8_merge
145 * and it enters the literals in the order of the file it causes the section
146 * to be ordered.
147 */
148__private_extern__
149void
150literal8_order(
151struct literal8_data *data,
152struct merged_section *ms)
153{
154#ifndef RLD
155    unsigned long i, line_number, output_offset, nliteral8_order_lines;
156    struct literal8 literal8;
157    struct literal8_order_line *literal8_order_lines;
158
159	/*
160	 * If -dead_strip is specified allocate the needed structures so that
161	 * the order of the live literals can be recreated later by
162	 * literal8_reset_live().  Allocate a literal8_order_line for each
163	 * line as the maximum that will needed.
164	 */
165	literal8_order_lines = NULL;
166	if(dead_strip == TRUE){
167	    line_number = 1;
168	    i = 0;
169	    while(i < ms->order_size){
170		while(i < ms->order_size && ms->order_addr[i] != '\n')
171		    i++;
172		if(i < ms->order_size && ms->order_addr[i] == '\n')
173		    i++;
174		line_number++;
175	    }
176	    data->literal8_load_order_data =
177		allocate(sizeof(struct literal8_load_order_data));
178	    literal8_order_lines = allocate(sizeof(struct literal8_order_line) *
179					   (line_number - 1));
180	    data->literal8_load_order_data->literal8_order_lines =
181		literal8_order_lines;
182	}
183
184	line_number = 1;
185	i = 0;
186	nliteral8_order_lines = 0;
187	while(i < ms->order_size){
188	    if(get_hex_from_sectorder(ms, &i, &(literal8.long0),
189				      line_number) == TRUE){
190		if(get_hex_from_sectorder(ms, &i, &(literal8.long1),
191					  line_number) == TRUE){
192		    output_offset = lookup_literal8(literal8, data, ms);
193		    if(dead_strip == TRUE){
194			literal8_order_lines[nliteral8_order_lines].
195			    literal8 = literal8;
196			literal8_order_lines[nliteral8_order_lines].
197			    line_number = line_number;
198			literal8_order_lines[nliteral8_order_lines].
199			    output_offset = output_offset;
200			nliteral8_order_lines++;
201		    }
202		}
203		else
204		    error("format error in -sectorder file: %s line %lu for "
205			  "section (%.16s,%.16s) (missing second hex number)",
206			  ms->order_filename, line_number, ms->s.segname,
207			  ms->s.sectname);
208	    }
209	    while(i < ms->order_size && ms->order_addr[i] != '\n')
210		i++;
211	    if(i < ms->order_size && ms->order_addr[i] == '\n')
212		i++;
213	    line_number++;
214	}
215
216	if(dead_strip == TRUE)
217	    data->literal8_load_order_data->nliteral8_order_lines =
218		nliteral8_order_lines;
219#endif /* !defined(RLD) */
220}
221
222/*
223 * literal8_reset_live() is called when -dead_strip is specified after all the
224 * literals from the input objects are merged.  It clears out the literal8_data
225 * so the live literals can be re-merged (by later calling literal8_merge() with
226 * redo_live == TRUE.  In here we first merge in the live literals from the
227 * order file if any.
228 */
229__private_extern__
230void
231literal8_reset_live(
232struct literal8_data *data,
233struct merged_section *ms)
234{
235#ifndef RLD
236    unsigned long i, nliteral8_order_lines, line_number;
237    struct literal8_order_line *literal8_order_lines;
238    enum bool live;
239
240	/* reset the merge section size back to zero */
241	ms->s.size = 0;
242
243	/* clear out the previously merged data */
244	literal8_free(data);
245
246	/*
247	 * If this merged section has an order file we need to re-merged only
248	 * the live literal8s from that order file.
249	 */
250	if(ms->order_filename != NULL){
251	    literal8_order_lines =
252		data->literal8_load_order_data->literal8_order_lines;
253	    nliteral8_order_lines =
254		data->literal8_load_order_data->nliteral8_order_lines;
255	    for(i = 0; i < nliteral8_order_lines; i++){
256		/*
257		 * Figure out if this literal8 order line's output_index is live
258		 * and if so re-merge the literal8 literal.
259		 */
260		live = is_literal_output_offset_live(
261			ms, literal8_order_lines[i].output_offset);
262		line_number = literal8_order_lines[i].line_number;
263		if(live){
264		    (void)lookup_literal8(literal8_order_lines[i].literal8,
265					  data, ms);
266		}
267		else{
268		    if(sectorder_detail == TRUE)
269			warning("specification of 8-byte literal in -sectorder "
270				"file: %s on line %lu for section (%.16s,%.16s)"
271				" not used (dead stripped)", ms->order_filename,
272				line_number, ms->s.segname, ms->s.sectname);
273		}
274	    }
275
276	    /* deallocate the various data structures no longer needed */
277	    free(data->literal8_load_order_data->literal8_order_lines);
278	    free(data->literal8_load_order_data);
279	    data->literal8_load_order_data = NULL;
280	}
281#endif /* !defined(RLD) */
282}
283
284/*
285 * get_hex_from_sectorder() gets a hex number of the form 0x<hex digits> for a
286 * 32 bit value from a sectorder file.  The sectorder file is for the merged
287 * section passed to it (ms).  The index to start scaning from is in *index and
288 * is set to the index after the hex number on return.  If a hex number is found
289 * it is returned indirectly through *value and TRUE is returned (if a hex
290 * number is not found FALSE is returned).  If an error is incountered then an
291 * error message is printed stating the order file and the section it is for
292 * and the line_number (passed in) it occured on.
293 */
294__private_extern__
295enum bool
296get_hex_from_sectorder(
297struct merged_section *ms,
298unsigned long *index,
299unsigned long *value,
300unsigned long line_number)
301{
302    unsigned long i, j;
303    char hex[9];
304
305	i = *index;
306	/* trim leading white space */
307	while(i < ms->order_size &&
308	      (ms->order_addr[i] == ' ' ||
309	       ms->order_addr[i] == '\t'))
310	    i++;
311
312	/*
313	 * If after skipping leading white space we are at the end of the file
314	 * then just return FALSE but print no error.
315	 */
316	if(i > ms->order_size){
317	    *index = i;
318	    return(FALSE);
319	}
320
321	/* look for a leading 0x */
322	if(i > ms->order_size || ms->order_addr[i] != '0'){
323	    error("format error in -sectorder file: %s line %lu for section "
324		  "(%.16s,%.16s) (missing hex number, no leading 0x found)",
325		  ms->order_filename,line_number,ms->s.segname,ms->s.sectname);
326	    *index = i;
327	    return(FALSE);
328	}
329	i++;
330	if(i > ms->order_size || ms->order_addr[i] != 'x'){
331	    error("format error in -sectorder file: %s line %lu for section "
332		  "(%.16s,%.16s) (missing hex number, no leading 0x found)",
333		  ms->order_filename,line_number,ms->s.segname,ms->s.sectname);
334	    *index = i;
335	    return(FALSE);
336	}
337	i++;
338
339	/* pick-up all hex digits and save the first 8 */
340	j = 0;
341	while(i < ms->order_size &&
342	      ((ms->order_addr[i] >= '0' && ms->order_addr[i] <= '9') ||
343	       (ms->order_addr[i] >= 'a' && ms->order_addr[i] <= 'f') ||
344	       (ms->order_addr[i] >= 'A' && ms->order_addr[i] <= 'F')) ){
345	    if(j <= 8)
346		hex[j++] = ms->order_addr[i++];
347	    else
348		i++;
349	}
350	if(j > 8){
351	    error("format error in -sectorder file: %s line %lu for section "
352		  "(%.16s,%.16s) (too many hex digits for 32 bit value)",
353		  ms->order_filename,line_number,ms->s.segname,ms->s.sectname);
354	    *index = i;
355	    return(FALSE);
356	}
357	hex[j] = '\0';
358	*value = strtoul(hex, NULL, 16);
359	*index = i;
360	return(TRUE);
361}
362
363/*
364 * lookup_literal8() looks up the 8 byte literal passed to it in the
365 * literal8_data passed to it and returns the offset the 8 byte literal will
366 * have in the output file.  It creates the blocks to store the literals and
367 * attaches them to the literal8_data passed to it.  The total size of the
368 * section is accumulated in ms->s.size which is the merged section for this
369 * literal section.  The literal is aligned to the alignment in the merged
370 * section (ms->s.align).
371 */
372__private_extern__
373unsigned long
374lookup_literal8(
375struct literal8 literal8,
376struct literal8_data *data,
377struct merged_section *ms)
378{
379    struct literal8_block **p, *literal8_block;
380    unsigned long align_multiplier, output_offset, i;
381
382	align_multiplier = 1;
383 	if((1 << ms->s.align) > 8)
384	    align_multiplier = (1 << ms->s.align) / 8;
385
386	output_offset = 0;
387	for(p = &(data->literal8_blocks); *p ; p = &(literal8_block->next)){
388	    literal8_block = *p;
389	    for(i = 0; i < literal8_block->used; i++){
390		if(literal8.long0 == literal8_block->literal8s[i].long0 &&
391		   literal8.long1 == literal8_block->literal8s[i].long1)
392		    return(output_offset + i * 8 * align_multiplier);
393	    }
394	    if(literal8_block->used != LITERAL8_BLOCK_SIZE){
395		literal8_block->literal8s[i].long0 = literal8.long0;
396		literal8_block->literal8s[i].long1 = literal8.long1;
397		literal8_block->used++;
398		ms->s.size += 8 * align_multiplier;
399		return(output_offset + i * 8 * align_multiplier);
400	    }
401	    output_offset += literal8_block->used * 8 * align_multiplier;
402	}
403	*p = allocate(sizeof(struct literal8_block));
404	literal8_block = *p;
405	literal8_block->used = 1;
406	literal8_block->literal8s[0].long0 = literal8.long0;
407	literal8_block->literal8s[0].long1 = literal8.long1;
408	literal8_block->next = NULL;
409
410	ms->s.size += 8 * align_multiplier;
411	return(output_offset);
412}
413
414/*
415 * literal8_output() copies the 8 byte literals for the data passed to it into
416 * the output file's buffer.  The pointer to the merged section passed to it is
417 * used to tell where in the output file this section goes.  Then this routine
418 * calls literal8_free() to free up all space used by the data block except the
419 * data block itself.
420 */
421__private_extern__
422void
423literal8_output(
424struct literal8_data *data,
425struct merged_section *ms)
426{
427    unsigned long align_multiplier, i, offset;
428    struct literal8_block **p, *literal8_block;
429
430	align_multiplier = 1;
431 	if((1 << ms->s.align) > 8)
432	    align_multiplier = (1 << ms->s.align) / 8;
433
434	/*
435	 * Copy the literals into the output file.
436	 */
437	offset = ms->s.offset;
438	for(p = &(data->literal8_blocks); *p ;){
439	    literal8_block = *p;
440	    for(i = 0; i < literal8_block->used; i++){
441		memcpy(output_addr + offset,
442		       literal8_block->literal8s + i,
443		       sizeof(struct literal8));
444		offset += 8 * align_multiplier;
445	    }
446	    p = &(literal8_block->next);
447	}
448#ifndef RLD
449	output_flush(ms->s.offset, offset - ms->s.offset);
450#endif /* !defined(RLD) */
451	literal8_free(data);
452}
453
454/*
455 * literal8_free() free()'s up all space used by the data block except the
456 * data block itself.
457 */
458__private_extern__
459void
460literal8_free(
461struct literal8_data *data)
462{
463    struct literal8_block *literal8_block, *next_literal8_block;
464
465	/*
466	 * Free all data for this block.
467	 */
468	for(literal8_block = data->literal8_blocks; literal8_block ;){
469	    next_literal8_block = literal8_block->next;
470	    free(literal8_block);
471	    literal8_block = next_literal8_block;
472	}
473	data->literal8_blocks = NULL;
474}
475
476#ifdef DEBUG
477/*
478 * print_literal8_data() prints a literal8_data.  Used for debugging.
479 */
480__private_extern__
481void
482print_literal8_data(
483struct literal8_data *data,
484char *indent)
485{
486    unsigned long i;
487    struct literal8_block **p, *literal8_block;
488
489	print("%s8 byte literal data at 0x%x\n", indent, (unsigned int)data);
490	if(data == NULL)
491	    return;
492	print("%s   literal8_blocks 0x%x\n", indent,
493	      (unsigned int)(data->literal8_blocks));
494	for(p = &(data->literal8_blocks); *p ; p = &(literal8_block->next)){
495	    literal8_block = *p;
496	    print("%s\tused %lu\n", indent, literal8_block->used);
497	    print("%s\tnext 0x%x\n", indent,
498		  (unsigned int)(literal8_block->next));
499	    print("%s\tliteral8s\n", indent);
500	    for(i = 0; i < literal8_block->used; i++){
501		print("%s\t    0x%08x 0x%08x\n", indent,
502		      (unsigned int)(literal8_block->literal8s[i].long0),
503		      (unsigned int)(literal8_block->literal8s[i].long1));
504	    }
505	}
506}
507
508/*
509 * literal8_data_stats() prints the literal8_data stats.  Used for tuning.
510 */
511__private_extern__
512void
513literal8_data_stats(
514struct literal8_data *data,
515struct merged_section *ms)
516{
517	if(data == NULL)
518	    return;
519	print("literal8 section (%.16s,%.16s) contains:\n",
520	      ms->s.segname, ms->s.sectname);
521	print("    %u merged literals \n", ms->s.size / 8);
522	print("    from %lu files and %lu total literals from those "
523	      "files\n", data->nfiles, data->nliterals);
524	print("    average number of literals per file %g\n",
525	      (double)((double)data->nliterals / (double)(data->nfiles)));
526}
527#endif /* DEBUG */
528