782
[darwin-xtools.git] / cctools / as / write_object.c
blobd40b2b5a0c73a1455b59d7f98cfc7edf36c18b4c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <ctype.h>
4 #include <sys/file.h>
5 #include <libc.h>
6 #include <mach/mach.h>
7 #include "arch64_32.h"
8 #include "stuff/openstep_mach.h"
9 #include <mach-o/loader.h>
10 #include <mach-o/reloc.h>
11 #include <mach-o/stab.h>
12 #ifdef I860
13 #include <mach-o/i860/reloc.h>
14 #endif
15 #ifdef M88K
16 #include <mach-o/m88k/reloc.h>
17 #endif
18 #ifdef PPC
19 #include <mach-o/ppc/reloc.h>
20 #endif
21 #ifdef HPPA
22 #include <mach-o/hppa/reloc.h>
23 #include "stuff/hppa.h"
24 #endif
25 #ifdef SPARC
26 #include <mach-o/sparc/reloc.h>
27 #endif
28 #ifdef ARM
29 #include <mach-o/arm/reloc.h>
30 #include "arm_reloc.h"
31 #endif
32 #if defined(I386) && defined(ARCH64)
33 #include <mach-o/x86_64/reloc.h>
34 #endif
35 #include "stuff/rnd.h"
36 #include "stuff/bytesex.h"
37 #include "stuff/errors.h"
38 #include "as.h"
39 #include "struc-symbol.h"
40 #include "symbols.h"
41 #include "frags.h"
42 #include "fixes.h"
43 #include "md.h"
44 #include "sections.h"
45 #include "messages.h"
46 #include "xmalloc.h"
47 #include "input-scrub.h"
48 #if defined(I386) && defined(ARCH64)
49 #include "i386.h"
50 #endif
51 #ifdef I860
52 #define RELOC_SECTDIFF I860_RELOC_SECTDIFF
53 #define RELOC_LOCAL_SECTDIFF I860_RELOC_SECTDIFF
54 #define RELOC_PAIR I860_RELOC_PAIR
55 #endif
56 #ifdef M88K
57 #define RELOC_SECTDIFF M88K_RELOC_SECTDIFF
58 #define RELOC_LOCAL_SECTDIFF M88K_RELOC_SECTDIFF
59 #define RELOC_PAIR M88K_RELOC_PAIR
60 #endif
61 #ifdef PPC
62 #define RELOC_SECTDIFF PPC_RELOC_SECTDIFF
63 #define RELOC_LOCAL_SECTDIFF PPC_RELOC_LOCAL_SECTDIFF
64 #define RELOC_PAIR PPC_RELOC_PAIR
65 #define PPC_RELOC_BR14_predicted (0x10 | PPC_RELOC_BR14)
66 #endif
67 #ifdef HPPA
68 #define RELOC_SECTDIFF HPPA_RELOC_SECTDIFF
69 #define RELOC_LOCAL_SECTDIFF HPPA_RELOC_SECTDIFF
70 #define RELOC_PAIR HPPA_RELOC_PAIR
71 #endif
72 #ifdef SPARC
73 #define RELOC_SECTDIFF SPARC_RELOC_SECTDIFF
74 #define RELOC_LOCAL_SECTDIFF SPARC_RELOC_SECTDIFF
75 #define RELOC_PAIR SPARC_RELOC_PAIR
76 #endif
77 #if defined(M68K) || defined(I386)
78 #define RELOC_SECTDIFF GENERIC_RELOC_SECTDIFF
79 #define RELOC_LOCAL_SECTDIFF GENERIC_RELOC_LOCAL_SECTDIFF
80 #define RELOC_PAIR GENERIC_RELOC_PAIR
81 #endif
82 #ifdef ARM
83 #define RELOC_SECTDIFF ARM_RELOC_SECTDIFF
84 #define RELOC_LOCAL_SECTDIFF ARM_RELOC_SECTDIFF
85 #define RELOC_PAIR ARM_RELOC_PAIR
86 #endif
89 * These variables are set by layout_symbols() to organize the symbol table and
90 * string table in order the dynamic linker expects. They are then used in
91 * write_object() to put out the symbols and strings in that order.
92 * The order of the symbol table is:
93 * local symbols
94 * defined external symbols (sorted by name)
95 * undefined external symbols (sorted by name)
96 * The order of the string table is:
97 * strings for external symbols
98 * strings for local symbols
100 /* index to and number of local symbols */
101 static uint32_t ilocalsym = 0;
102 static uint32_t nlocalsym = 0;
103 /* index to, number of and array of sorted externally defined symbols */
104 static uint32_t iextdefsym = 0;
105 static uint32_t nextdefsym = 0;
106 static symbolS **extdefsyms = NULL;
107 /* index to, number of and array of sorted undefined symbols */
108 static uint32_t iundefsym = 0;
109 static uint32_t nundefsym = 0;
110 static symbolS **undefsyms = NULL;
112 static uint32_t layout_indirect_symbols(
113 void);
114 static void layout_symbols(
115 int32_t *symbol_number,
116 int32_t *string_byte_count);
117 static int qsort_compare(
118 const symbolS **sym1,
119 const symbolS **sym2);
120 static uint32_t nrelocs_for_fix(
121 struct fix *fixP);
122 static uint32_t fix_to_relocation_entries(
123 struct fix *fixP,
124 uint64_t sect_addr,
125 struct relocation_info *riP,
126 uint32_t debug_section);
127 #ifdef I860
128 static void
129 I860_tweeks(void);
130 #endif
133 * write_object() writes a Mach-O object file from the built up data structures.
135 void
136 write_object(
137 char *out_file_name)
139 /* The structures for Mach-O relocatables */
140 mach_header_t header;
141 segment_command_t reloc_segment;
142 struct symtab_command symbol_table;
143 struct dysymtab_command dynamic_symbol_table;
144 uint32_t section_type;
145 uint32_t *indirect_symbols;
146 isymbolS *isymbolP;
147 uint32_t i, j, nsects, nsyms, strsize, nindirectsyms;
149 /* locals to fill in section struct fields */
150 uint32_t offset, zero;
152 /* The GAS data structures */
153 struct frchain *frchainP, *p;
154 struct symbol *symbolP;
155 struct frag *fragP;
156 struct fix *fixP;
158 uint32_t output_size;
159 char *output_addr;
160 kern_return_t r;
162 enum byte_sex host_byte_sex;
163 uint32_t reloff, nrelocs;
164 int32_t count;
165 char *fill_literal;
166 int32_t fill_size;
167 int32_t num_bytes;
168 char *symbol_name;
169 int fd;
170 uint32_t local;
171 struct stat stat_buf;
173 #ifdef I860
174 I860_tweeks();
175 #endif
176 i = 0; /* to shut up a compiler "may be used uninitialized" warning */
179 * The first group of things to do is to set all the fields in the
180 * header structures which includes offsets and determining the final
181 * sizes of things.
185 * Fill in the addr and size fields of each section structure and count
186 * the number of sections.
188 nsects = 0;
189 for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
190 frchainP->frch_section.addr = frchainP->frch_root->fr_address;
191 frchainP->frch_section.size = frchainP->frch_last->fr_address -
192 frchainP->frch_root->fr_address;
193 nsects++;
197 * Setup the indirect symbol tables by looking up or creating symbol
198 * from the indirect symbol names and recording the symbol pointers.
200 nindirectsyms = layout_indirect_symbols();
203 * Setup the symbol table to include only those symbols that will be in
204 * the object file, assign the string table offsets into the symbols
205 * and size the string table.
207 nsyms = 0;
208 strsize = 0;
209 layout_symbols((int32_t *)&nsyms, (int32_t *)&strsize);
211 /* fill in the Mach-O header */
212 header.magic = MH_MAGIC_VALUE;
213 header.cputype = md_cputype;
214 if(archflag_cpusubtype != -1)
215 header.cpusubtype = archflag_cpusubtype;
216 else
217 header.cpusubtype = md_cpusubtype;
219 header.filetype = MH_OBJECT;
220 header.ncmds = 0;
221 header.sizeofcmds = 0;
222 if(nsects != 0){
223 header.ncmds += 1;
224 header.sizeofcmds += sizeof(segment_command_t) +
225 nsects * sizeof(section_t);
227 if(nsyms != 0){
228 header.ncmds += 1;
229 header.sizeofcmds += sizeof(struct symtab_command);
230 if(flagseen['k']){
231 header.ncmds += 1;
232 header.sizeofcmds += sizeof(struct dysymtab_command);
235 else
236 strsize = 0;
237 header.flags = 0;
238 if(subsections_via_symbols == TRUE)
239 header.flags |= MH_SUBSECTIONS_VIA_SYMBOLS;
240 #ifdef ARCH64
241 header.reserved = 0;
242 #endif
244 /* fill in the segment command */
245 memset(&reloc_segment, '\0', sizeof(segment_command_t));
246 reloc_segment.cmd = LC_SEGMENT_VALUE;
247 reloc_segment.cmdsize = sizeof(segment_command_t) +
248 nsects * sizeof(section_t);
249 /* leave reloc_segment.segname full of zeros */
250 reloc_segment.vmaddr = 0;
251 reloc_segment.vmsize = 0;
252 reloc_segment.filesize = 0;
253 offset = header.sizeofcmds + sizeof(mach_header_t);
254 reloc_segment.fileoff = offset;
255 reloc_segment.maxprot = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
256 reloc_segment.initprot= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
257 reloc_segment.nsects = nsects;
258 reloc_segment.flags = 0;
260 * Set the offsets to the contents of the sections (for non-zerofill
261 * sections) and set the filesize and vmsize of the segment. This is
262 * complicated by the fact that all the zerofill sections have addresses
263 * after the non-zerofill sections and that the alignment of sections
264 * produces gaps that are not in any section. For the vmsize we rely on
265 * the fact the the sections start at address 0 so it is just the last
266 * zerofill section or the last not-zerofill section.
268 for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
269 if((frchainP->frch_section.flags & SECTION_TYPE) == S_ZEROFILL)
270 continue;
271 for(p = frchainP->frch_next; p != NULL; p = p->frch_next)
272 if((p->frch_section.flags & SECTION_TYPE) != S_ZEROFILL)
273 break;
274 if(p != NULL)
275 i = p->frch_section.addr - frchainP->frch_section.addr;
276 else
277 i = frchainP->frch_section.size;
278 reloc_segment.filesize += i;
279 frchainP->frch_section.offset = offset;
280 offset += i;
281 reloc_segment.vmsize = frchainP->frch_section.addr +
282 frchainP->frch_section.size;
284 for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
285 if((frchainP->frch_section.flags & SECTION_TYPE) != S_ZEROFILL)
286 continue;
287 reloc_segment.vmsize = frchainP->frch_section.addr +
288 frchainP->frch_section.size;
290 offset = rnd(offset, sizeof(int32_t));
293 * Count the number of relocation entries for each section.
295 for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
296 frchainP->frch_section.nreloc = 0;
297 for(fixP = frchainP->frch_fix_root; fixP; fixP = fixP->fx_next){
298 frchainP->frch_section.nreloc += nrelocs_for_fix(fixP);
303 * Fill in the offset to the relocation entries of the sections.
305 offset = rnd(offset, sizeof(int32_t));
306 reloff = offset;
307 nrelocs = 0;
308 for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
309 if(frchainP->frch_section.nreloc == 0)
310 frchainP->frch_section.reloff = 0;
311 else
312 frchainP->frch_section.reloff = offset;
313 offset += frchainP->frch_section.nreloc *
314 sizeof(struct relocation_info);
315 nrelocs += frchainP->frch_section.nreloc;
318 if(flagseen['k']){
319 /* fill in the fields of the dysymtab_command */
320 dynamic_symbol_table.cmd = LC_DYSYMTAB;
321 dynamic_symbol_table.cmdsize = sizeof(struct dysymtab_command);
323 dynamic_symbol_table.ilocalsym = ilocalsym;
324 dynamic_symbol_table.nlocalsym = nlocalsym;
325 dynamic_symbol_table.iextdefsym = iextdefsym;
326 dynamic_symbol_table.nextdefsym = nextdefsym;
327 dynamic_symbol_table.iundefsym = iundefsym;
328 dynamic_symbol_table.nundefsym = nundefsym;
330 if(nindirectsyms == 0){
331 dynamic_symbol_table.nindirectsyms = 0;
332 dynamic_symbol_table.indirectsymoff = 0;
334 else{
335 dynamic_symbol_table.nindirectsyms = nindirectsyms;
336 dynamic_symbol_table.indirectsymoff = offset;
337 offset += nindirectsyms * sizeof(uint32_t);
340 dynamic_symbol_table.tocoff = 0;
341 dynamic_symbol_table.ntoc = 0;
342 dynamic_symbol_table.modtaboff = 0;
343 dynamic_symbol_table.nmodtab = 0;
344 dynamic_symbol_table.extrefsymoff = 0;
345 dynamic_symbol_table.nextrefsyms = 0;
346 dynamic_symbol_table.extreloff = 0;
347 dynamic_symbol_table.nextrel = 0;
348 dynamic_symbol_table.locreloff = 0;
349 dynamic_symbol_table.nlocrel = 0;
352 /* fill in the fields of the symtab_command (except the string table) */
353 symbol_table.cmd = LC_SYMTAB;
354 symbol_table.cmdsize = sizeof(struct symtab_command);
355 if(nsyms == 0)
356 symbol_table.symoff = 0;
357 else
358 symbol_table.symoff = offset;
359 symbol_table.nsyms = nsyms;
360 offset += symbol_table.nsyms * sizeof(nlist_t);
362 /* fill in the string table fields of the symtab_command */
363 if(strsize == 0)
364 symbol_table.stroff = 0;
365 else
366 symbol_table.stroff = offset;
367 symbol_table.strsize = rnd(strsize, sizeof(uint32_t));
368 offset += rnd(strsize, sizeof(uint32_t));
371 * The second group of things to do is now with the size of everything
372 * known the object file and the offsets set in the various structures
373 * the contents of the object file can be created.
377 * Create the buffer to copy the parts of the output file into.
379 output_size = offset;
380 if((r = vm_allocate(mach_task_self(), (vm_address_t *)&output_addr,
381 output_size, TRUE)) != KERN_SUCCESS)
382 as_fatal("can't vm_allocate() buffer for output file of size %u",
383 output_size);
385 /* put the headers in the output file's buffer */
386 host_byte_sex = get_host_byte_sex();
387 offset = 0;
389 /* put the mach_header in the buffer */
390 memcpy(output_addr + offset, &header, sizeof(mach_header_t));
391 if(host_byte_sex != md_target_byte_sex)
392 swap_mach_header_t((mach_header_t *)(output_addr + offset),
393 md_target_byte_sex);
394 offset += sizeof(mach_header_t);
396 /* put the segment_command in the buffer */
397 if(nsects != 0){
398 memcpy(output_addr + offset, &reloc_segment,
399 sizeof(segment_command_t));
400 if(host_byte_sex != md_target_byte_sex)
401 swap_segment_command_t((segment_command_t *)
402 (output_addr + offset),
403 md_target_byte_sex);
404 offset += sizeof(segment_command_t);
407 /* put the segment_command's section structures in the buffer */
408 for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
409 memcpy(output_addr + offset, &(frchainP->frch_section),
410 sizeof(section_t));
411 if(host_byte_sex != md_target_byte_sex)
412 swap_section_t((section_t *)(output_addr + offset), 1,
413 md_target_byte_sex);
414 offset += sizeof(section_t);
417 /* put the symbol_command in the buffer */
418 if(nsyms != 0){
419 memcpy(output_addr + offset, &symbol_table,
420 sizeof(struct symtab_command));
421 if(host_byte_sex != md_target_byte_sex)
422 swap_symtab_command((struct symtab_command *)
423 (output_addr + offset),
424 md_target_byte_sex);
425 offset += sizeof(struct symtab_command);
428 if(flagseen['k']){
429 /* put the dysymbol_command in the buffer */
430 if(nsyms != 0){
431 memcpy(output_addr + offset, &dynamic_symbol_table,
432 sizeof(struct dysymtab_command));
433 if(host_byte_sex != md_target_byte_sex)
434 swap_dysymtab_command((struct dysymtab_command *)
435 (output_addr + offset),
436 md_target_byte_sex);
437 offset += sizeof(struct dysymtab_command);
441 /* put the section contents (frags) in the buffer */
442 for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
443 offset = frchainP->frch_section.offset;
444 for(fragP = frchainP->frch_root; fragP; fragP = fragP->fr_next){
445 know(fragP->fr_type == rs_fill);
446 /* put the fixed part of the frag in the buffer */
447 memcpy(output_addr + offset, fragP->fr_literal, fragP->fr_fix);
448 offset += fragP->fr_fix;
450 /* put the variable repeated part of the frag in the buffer */
451 fill_literal = fragP->fr_literal + fragP->fr_fix;
452 fill_size = fragP->fr_var;
453 num_bytes = fragP->fr_offset * fragP->fr_var;
454 for(count = 0; count < num_bytes; count += fill_size){
455 memcpy(output_addr + offset, fill_literal, fill_size);
456 offset += fill_size;
462 /* put the symbols in the output file's buffer */
463 offset = symbol_table.symoff;
464 for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
465 if((symbolP->sy_type & N_EXT) == 0){
466 symbol_name = symbolP->sy_name;
467 symbolP->sy_nlist.n_un.n_strx = symbolP->sy_name_offset;
468 if(symbolP->expression != 0) {
469 expressionS *exp;
471 exp = (expressionS *)symbolP->expression;
472 if((exp->X_add_symbol->sy_type & N_TYPE) == N_UNDF)
473 as_fatal("undefined symbol `%s' in operation setting "
474 "`%s'", exp->X_add_symbol->sy_name,
475 symbol_name);
476 if((exp->X_subtract_symbol->sy_type & N_TYPE) == N_UNDF)
477 as_fatal("undefined symbol `%s' in operation setting "
478 "`%s'", exp->X_subtract_symbol->sy_name,
479 symbol_name);
480 if(exp->X_add_symbol->sy_other !=
481 exp->X_subtract_symbol->sy_other)
482 as_fatal("invalid sections for operation on `%s' and "
483 "`%s' setting `%s'",exp->X_add_symbol->sy_name,
484 exp->X_subtract_symbol->sy_name, symbol_name);
485 symbolP->sy_nlist.n_value +=
486 exp->X_add_symbol->sy_value -
487 exp->X_subtract_symbol->sy_value;
489 memcpy(output_addr + offset, (char *)(&symbolP->sy_nlist),
490 sizeof(nlist_t));
491 symbolP->sy_name = symbol_name;
492 offset += sizeof(nlist_t);
495 for(i = 0; i < nextdefsym; i++){
496 symbol_name = extdefsyms[i]->sy_name;
497 extdefsyms[i]->sy_nlist.n_un.n_strx = extdefsyms[i]->sy_name_offset;
498 memcpy(output_addr + offset, (char *)(&extdefsyms[i]->sy_nlist),
499 sizeof(nlist_t));
500 extdefsyms[i]->sy_name = symbol_name;
501 offset += sizeof(nlist_t);
503 for(j = 0; j < nundefsym; j++){
504 symbol_name = undefsyms[j]->sy_name;
505 undefsyms[j]->sy_nlist.n_un.n_strx = undefsyms[j]->sy_name_offset;
506 memcpy(output_addr + offset, (char *)(&undefsyms[j]->sy_nlist),
507 sizeof(nlist_t));
508 undefsyms[j]->sy_name = symbol_name;
509 offset += sizeof(nlist_t);
511 if(host_byte_sex != md_target_byte_sex)
512 swap_nlist_t((nlist_t *)(output_addr + symbol_table.symoff),
513 symbol_table.nsyms, md_target_byte_sex);
516 * Put the relocation entries for each section in the buffer.
518 for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
519 offset = frchainP->frch_section.reloff;
520 for(fixP = frchainP->frch_fix_root; fixP; fixP = fixP->fx_next){
521 offset += fix_to_relocation_entries(
522 fixP,
523 frchainP->frch_section.addr,
524 (struct relocation_info *)(output_addr +
525 offset),
526 frchainP->frch_section.flags &
527 S_ATTR_DEBUG);
530 if(host_byte_sex != md_target_byte_sex)
531 swap_relocation_info((struct relocation_info *)
532 (output_addr + reloff), nrelocs, md_target_byte_sex);
534 if(flagseen['k']){
535 /* put the indirect symbol table in the buffer */
536 offset = dynamic_symbol_table.indirectsymoff;
537 for(frchainP = frchain_root;
538 frchainP != NULL;
539 frchainP = frchainP->frch_next){
540 section_type = frchainP->frch_section.flags & SECTION_TYPE;
541 if(section_type == S_NON_LAZY_SYMBOL_POINTERS ||
542 section_type == S_LAZY_SYMBOL_POINTERS ||
543 section_type == S_SYMBOL_STUBS){
545 * For each indirect symbol put out the symbol number.
547 for(isymbolP = frchainP->frch_isym_root;
548 isymbolP != NULL;
549 isymbolP = isymbolP->isy_next){
551 * If this is a non-lazy pointer symbol section and
552 * if the symbol is a local symbol then put out
553 * INDIRECT_SYMBOL_LOCAL as the indirect symbol table
554 * entry. This is used with code gen for fix-n-continue
555 * where the compiler generates indirection for static
556 * data references. See the comments at the end of
557 * fixup_section() that explains the assembly code used.
559 if(section_type == S_NON_LAZY_SYMBOL_POINTERS &&
560 (isymbolP->isy_symbol->sy_nlist.n_type & N_EXT) !=
561 N_EXT){
562 local = INDIRECT_SYMBOL_LOCAL;
563 if((isymbolP->isy_symbol->sy_nlist.n_type &
564 N_TYPE) == N_ABS)
565 local |= INDIRECT_SYMBOL_ABS;
566 memcpy(output_addr + offset, (char *)(&local),
567 sizeof(uint32_t));
569 else{
570 memcpy(output_addr + offset,
571 (char *)(&isymbolP->isy_symbol->sy_number),
572 sizeof(uint32_t));
574 offset += sizeof(uint32_t);
578 if(host_byte_sex != md_target_byte_sex){
579 indirect_symbols = (uint32_t *)(output_addr +
580 dynamic_symbol_table.indirectsymoff);
581 swap_indirect_symbols(indirect_symbols, nindirectsyms,
582 md_target_byte_sex);
586 /* put the strings in the output file's buffer */
587 offset = symbol_table.stroff;
588 if(symbol_table.strsize != 0){
589 zero = 0;
590 memcpy(output_addr + offset, (char *)&zero, sizeof(char));
591 offset += sizeof(char);
593 for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
594 /* Ordinary case: not .stabd. */
595 if(symbolP->sy_name != NULL){
596 if((symbolP->sy_type & N_EXT) != 0){
597 memcpy(output_addr + offset, symbolP->sy_name,
598 strlen(symbolP->sy_name) + 1);
599 offset += strlen(symbolP->sy_name) + 1;
603 for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
604 /* Ordinary case: not .stabd. */
605 if(symbolP->sy_name != NULL){
606 if((symbolP->sy_type & N_EXT) == 0){
607 memcpy(output_addr + offset, symbolP->sy_name,
608 strlen(symbolP->sy_name) + 1);
609 offset += strlen(symbolP->sy_name) + 1;
614 * Create the output file. The unlink() is done to handle the problem
615 * when the out_file_name is not writable but the directory allows the
616 * file to be removed (since the file may not be there the return code
617 * of the unlink() is ignored).
619 if(bad_error != 0)
620 return;
622 * Avoid doing the unlink() on special files, just unlink regular files
623 * that exist.
625 if(stat(out_file_name, &stat_buf) != -1){
626 if(stat_buf.st_mode & S_IFREG)
627 (void)unlink(out_file_name);
629 if((fd = open(out_file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) == -1)
630 as_fatal("can't create output file: %s", out_file_name);
631 if(write(fd, output_addr, output_size) != (int)output_size)
632 as_fatal("can't write output file");
633 if(close(fd) == -1)
634 as_fatal("can't close output file");
638 * layout_indirect_symbols() setups the indirect symbol tables by looking up or
639 * creating symbol from the indirect symbol names and recording the symbol
640 * pointers. It returns the total count of indirect symbol table entries.
642 static
643 uint32_t
644 layout_indirect_symbols(void)
646 struct frchain *frchainP;
647 uint32_t section_type, total, count, stride;
648 isymbolS *isymbolP;
649 symbolS *symbolP;
652 * Mark symbols that only appear in a lazy section with
653 * REFERENCE_FLAG_UNDEFINED_LAZY. To do this we first make sure a
654 * symbol exists for all non-lazy symbols. Then we make a pass looking
655 * up the lazy symbols and if not there we make the symbol and mark it
656 * with REFERENCE_FLAG_UNDEFINED_LAZY.
658 for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
659 section_type = frchainP->frch_section.flags & SECTION_TYPE;
660 if(section_type == S_NON_LAZY_SYMBOL_POINTERS){
661 for(isymbolP = frchainP->frch_isym_root;
662 isymbolP != NULL;
663 isymbolP = isymbolP->isy_next){
665 (void)symbol_find_or_make(isymbolP->isy_name);
667 symbolP = symbol_find(isymbolP->isy_name);
668 if(symbolP == NULL){
669 symbolP = symbol_new(isymbolP->isy_name, N_UNDF, 0, 0,
670 0, &zero_address_frag);
671 symbol_table_insert(symbolP);
676 for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
677 section_type = frchainP->frch_section.flags & SECTION_TYPE;
678 if(section_type == S_LAZY_SYMBOL_POINTERS ||
679 section_type == S_SYMBOL_STUBS){
680 for(isymbolP = frchainP->frch_isym_root;
681 isymbolP != NULL;
682 isymbolP = isymbolP->isy_next){
684 symbolP = symbol_find(isymbolP->isy_name);
685 if(symbolP == NULL){
686 symbolP = symbol_find_or_make(isymbolP->isy_name);
687 symbolP->sy_desc |= REFERENCE_FLAG_UNDEFINED_LAZY;
693 total = 0;
694 for(frchainP = frchain_root; frchainP; frchainP = frchainP->frch_next){
695 section_type = frchainP->frch_section.flags & SECTION_TYPE;
696 if(section_type == S_LAZY_SYMBOL_POINTERS ||
697 section_type == S_NON_LAZY_SYMBOL_POINTERS ||
698 section_type == S_SYMBOL_STUBS){
699 count = 0;
700 for(isymbolP = frchainP->frch_isym_root;
701 isymbolP != NULL;
702 isymbolP = isymbolP->isy_next){
705 symbolP = symbol_find_or_make(isymbolP->isy_name);
707 symbolP = symbol_find(isymbolP->isy_name);
708 if(symbolP == NULL){
709 symbolP = symbol_new(isymbolP->isy_name, N_UNDF, 0, 0,
710 0, &zero_address_frag);
711 symbol_table_insert(symbolP);
713 isymbolP->isy_symbol = symbolP;
714 count++;
717 * Check for missing indirect symbols.
719 if(section_type == S_SYMBOL_STUBS)
720 stride = frchainP->frch_section.reserved2;
721 else
722 stride = sizeof(signed_target_addr_t);
723 if(frchainP->frch_section.size / stride != count)
724 as_bad("missing indirect symbols for section (%s,%s)",
725 frchainP->frch_section.segname,
726 frchainP->frch_section.sectname);
728 * Set the index into the indirect symbol table for this
729 * section into the reserved1 field.
731 frchainP->frch_section.reserved1 = total;
732 total += count;
735 return(total);
740 * set_BINCL_checksums() walks through all STABS and calculate BINCL checksums. This will improve
741 * linking performance because the linker will not need to touch and sum STABS
742 * strings to do the BINCL/EINCL duplicate removal.
744 * A BINCL checksum is a sum of all stabs strings within a BINCL/EINCL pair.
745 * Since BINCL/EINCL can be nested, a stab string contributes to only the
746 * innermost BINCL/EINCL enclosing it.
748 * The checksum excludes the first number after an open paren.
750 * Some stabs (e.g. SLINE) when found within a BINCL/EINCL disqualify the EXCL
751 * optimization and therefore disable this checksumming.
753 static
754 void
755 set_BINCL_checksums()
757 struct HeaderRange {
758 symbolS* bincl;
759 struct HeaderRange* parentRange;
760 unsigned int sum;
761 int okToChecksum;
763 symbolS *symbolP;
764 struct HeaderRange* curRange = NULL;
766 for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
767 if((symbolP->sy_nlist.n_type & N_STAB) != 0){
768 switch(symbolP->sy_nlist.n_type){
769 case N_BINCL:
771 struct HeaderRange* range =
772 xmalloc(sizeof(struct HeaderRange));
773 range->bincl = symbolP;
774 range->parentRange = curRange;
775 range->sum = 0;
776 range->okToChecksum = (symbolP->sy_nlist.n_value == 0);
777 curRange = range;
779 break;
780 case N_EINCL:
781 if(curRange != NULL){
782 struct HeaderRange* tmp = curRange;
783 if (curRange->okToChecksum)
784 curRange->bincl->sy_nlist.n_value = curRange->sum;
785 curRange = tmp->parentRange;
786 free(tmp);
788 break;
789 case N_FUN:
790 case N_BNSYM:
791 case N_ENSYM:
792 case N_LBRAC:
793 case N_RBRAC:
794 case N_SLINE:
795 case N_STSYM:
796 case N_LCSYM:
797 if(curRange != NULL){
798 curRange->okToChecksum = FALSE;
800 break;
801 case N_EXCL:
802 break;
803 default:
804 if(curRange != NULL){
805 if(curRange->okToChecksum){
806 unsigned int sum = 0;
807 const char* s = symbolP->sy_name;
808 char c;
809 while((c = *s++) != '\0'){
810 sum += c;
812 * Don't checkusm first number (file index)
813 * after open paren in string.
815 if(c == '('){
816 while(isdigit(*s))
817 ++s;
820 curRange->sum += sum;
829 * layout_symbols() removes temporary symbols (symbols that are of the form L1
830 * and 1:) if the -L flag is not seen so the symbol table has only the symbols
831 * it will have in the output file. Then each remaining symbol is given a
832 * symbol number and a string offset for the symbol name which also sizes the
833 * string table.
834 * The order of the symbol table is:
835 * local symbols
836 * defined external symbols (sorted by name)
837 * undefined external symbols (sorted by name)
838 * The order of the string table is:
839 * strings for external symbols
840 * strings for local symbols
842 static
843 void
844 layout_symbols(
845 int32_t *symbol_number,
846 int32_t *string_byte_count)
848 uint32_t i, j;
849 symbolS *symbolP;
850 symbolS **symbolPP;
851 char *name;
852 int seenBINCL = FALSE;
854 *symbol_number = 0;
855 *string_byte_count = sizeof(char);
858 * First pass through the symbols remove temporary symbols that are not
859 * going to be in the output file. Also number the local symbols and
860 * assign string offset to external symbols.
862 symbolPP = &symbol_rootP;
863 while((symbolP = *symbolPP)){
864 name = symbolP->sy_name;
866 * Deal with temporary symbols. Temporary symbols start with 'L'
867 * but are not stabs. It is an error if they are undefined. They
868 * are removed if the -L flag is not seen else they are kept.
870 if(name != NULL &&
871 (symbolP->sy_nlist.n_type & N_STAB) == 0 &&
872 name[0] == 'L'){
874 if((symbolP->sy_nlist.n_type & N_TYPE) == N_UNDF){
875 if(name[1] != '\0' && name[2] == '\001'){
876 as_bad("Undefined local symbol %c (%cf or %cb)",
877 name[1], name[1], name[1]);
879 else{
880 as_bad("Undefined local symbol %s", name);
882 /* don't keep this symbol */
883 *symbolPP = symbolP->sy_next;
885 else if(flagseen['L'] || (symbolP->sy_type & N_EXT) != 0
886 #if defined(I386) && defined(ARCH64)
887 || is_section_cstring_literals(symbolP->sy_other)
888 #endif
890 if((symbolP->sy_type & N_EXT) == 0){
891 nlocalsym++;
892 symbolP->sy_number = *symbol_number;
893 *symbol_number = *symbol_number + 1;
895 else{
896 nextdefsym++;
897 symbolP->sy_name_offset = *string_byte_count;
898 *string_byte_count += strlen(symbolP->sy_name) + 1;
900 symbolPP = &(symbolP->sy_next);
902 else{
903 /* don't keep this symbol */
904 *symbolPP = symbolP->sy_next;
908 * All non-temporary symbols will be the symbol table in the output
909 * file.
911 else{
912 /* Any undefined symbols become N_EXT. */
913 if(symbolP->sy_type == N_UNDF)
914 symbolP->sy_type |= N_EXT;
916 if((symbolP->sy_type & N_EXT) == 0){
917 symbolP->sy_number = *symbol_number;
918 *symbol_number = *symbol_number + 1;
919 nlocalsym++;
921 else{
922 if((symbolP->sy_type & N_TYPE) != N_UNDF)
923 nextdefsym++;
924 else
925 nundefsym++;
927 if(name != NULL){
928 /* the ordinary case (symbol has a name) */
929 symbolP->sy_name_offset = *string_byte_count;
930 *string_byte_count += strlen(symbolP->sy_name) + 1;
932 else{
933 /* the .stabd case (symbol has no name) */
934 symbolP->sy_name_offset = 0;
937 symbolPP = &(symbolP->sy_next);
942 * Check to see that any symbol that is marked as a weak_definition
943 * is a global symbol defined in a coalesced section.
945 for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
946 if((symbolP->sy_nlist.n_type & N_STAB) == 0 &&
947 (symbolP->sy_desc & N_WEAK_DEF) == N_WEAK_DEF){
948 if((symbolP->sy_type & N_EXT) == 0){
949 as_bad("Non-global symbol: %s can't be a weak_definition",
950 symbolP->sy_name);
952 else if((symbolP->sy_type & N_TYPE) == N_UNDF){
953 as_bad("Undefined symbol: %s can't be a weak_definition",
954 symbolP->sy_name);
959 /* Set the indexes for symbol groups into the symbol table */
960 ilocalsym = 0;
961 iextdefsym = nlocalsym;
962 iundefsym = nlocalsym + nextdefsym;
964 /* allocate arrays for sorting externals by name */
965 extdefsyms = xmalloc(nextdefsym * sizeof(symbolS *));
966 undefsyms = xmalloc(nundefsym * sizeof(symbolS *));
968 i = 0;
969 j = 0;
970 for(symbolP = symbol_rootP; symbolP; symbolP = symbolP->sy_next){
971 if((symbolP->sy_type & N_EXT) == 0){
972 if(symbolP->sy_name != NULL){
973 /* the ordinary case (symbol has a name) */
974 symbolP->sy_name_offset = *string_byte_count;
975 *string_byte_count += strlen(symbolP->sy_name) + 1;
976 /* check for existance of BINCL/EINCL */
977 if(symbolP->sy_nlist.n_type == N_BINCL)
978 seenBINCL = TRUE;
980 else{
981 /* the .stabd case (symbol has no name) */
982 symbolP->sy_name_offset = 0;
985 else{
986 if((symbolP->sy_type & N_TYPE) != N_UNDF)
987 extdefsyms[i++] = symbolP;
988 else
989 undefsyms[j++] = symbolP;
992 qsort(extdefsyms, nextdefsym, sizeof(symbolS *),
993 (int (*)(const void *, const void *))qsort_compare);
994 qsort(undefsyms, nundefsym, sizeof(symbolS *),
995 (int (*)(const void *, const void *))qsort_compare);
996 for(i = 0; i < nextdefsym; i++){
997 extdefsyms[i]->sy_number = *symbol_number;
998 *symbol_number = *symbol_number + 1;
1000 for(j = 0; j < nundefsym; j++){
1001 undefsyms[j]->sy_number = *symbol_number;
1002 *symbol_number = *symbol_number + 1;
1005 /* calculate BINCL checksums */
1006 if(seenBINCL)
1007 set_BINCL_checksums();
1011 * Function for qsort to sort symbol structs by their name
1013 static
1015 qsort_compare(
1016 const symbolS **sym1,
1017 const symbolS **sym2)
1019 return(strcmp((*sym1)->sy_name, (*sym2)->sy_name));
1023 * nrelocs_for_fix() returns the number of relocation entries needed for the
1024 * specified fix structure.
1026 static
1027 uint32_t
1028 nrelocs_for_fix(
1029 struct fix *fixP)
1032 * If fx_addsy is NULL then this fix needs no relocation entry.
1034 if(fixP->fx_addsy == NULL)
1035 return(0);
1038 * If this fix has a subtract symbol it is a SECTDIFF relocation which
1039 * takes two relocation entries.
1041 if(fixP->fx_subsy != NULL)
1042 return(2);
1045 * For RISC machines whenever we have a relocation item using the half
1046 * of an address a second a relocation item describing the other
1047 * half of the address is used.
1049 #ifdef I860
1050 if(fixP->fx_r_type == I860_RELOC_HIGH ||
1051 fixP->fx_r_type == I860_RELOC_HIGHADJ)
1052 return(2);
1053 #endif
1054 #ifdef M88K
1055 if(fixP->fx_r_type == M88K_RELOC_HI16 ||
1056 fixP->fx_r_type == M88K_RELOC_LO16)
1057 return(2);
1058 #endif
1059 #ifdef PPC
1060 if(fixP->fx_r_type == PPC_RELOC_HI16 ||
1061 fixP->fx_r_type == PPC_RELOC_LO16 ||
1062 fixP->fx_r_type == PPC_RELOC_HA16 ||
1063 fixP->fx_r_type == PPC_RELOC_LO14 ||
1064 fixP->fx_r_type == PPC_RELOC_JBSR)
1065 return(2);
1066 #endif
1067 #ifdef HPPA
1068 if(fixP->fx_r_type == HPPA_RELOC_HI21 ||
1069 fixP->fx_r_type == HPPA_RELOC_LO14 ||
1070 fixP->fx_r_type == HPPA_RELOC_BR17 ||
1071 fixP->fx_r_type == HPPA_RELOC_JBSR)
1072 return(2);
1073 #endif
1074 #ifdef SPARC
1075 if(fixP->fx_r_type == SPARC_RELOC_HI22 ||
1076 fixP->fx_r_type == SPARC_RELOC_LO10)
1077 return(2);
1078 #endif
1079 #ifdef ARM
1080 if(fixP->fx_r_type == ARM_RELOC_LO16 ||
1081 fixP->fx_r_type == ARM_RELOC_HI16 ||
1082 fixP->fx_r_type == ARM_THUMB_RELOC_LO16 ||
1083 fixP->fx_r_type == ARM_THUMB_RELOC_HI16)
1084 return(2);
1085 #endif
1086 return(1);
1090 * fix_to_relocation_entries() creates the needed relocation entries for the
1091 * specified fix structure that is from a section who's address starts at
1092 * sect_addr. It returns the number of bytes of relocation_info structs it
1093 * placed at riP.
1095 static
1096 uint32_t
1097 fix_to_relocation_entries(
1098 struct fix *fixP,
1099 uint64_t sect_addr,
1100 struct relocation_info *riP,
1101 uint32_t debug_section)
1103 struct symbol *symbolP;
1104 uint32_t count;
1105 struct scattered_relocation_info sri;
1106 uint32_t sectdiff;
1107 #ifdef HPPA
1108 uint32_t left21, right14;
1109 #endif
1112 * If fx_addsy is NULL then this fix needs no relocation entry.
1114 if(fixP->fx_addsy == NULL)
1115 return(0);
1117 #ifdef TC_VALIDATE_FIX
1118 TC_VALIDATE_FIX(fixP, sect_addr, 0);
1119 #endif
1121 memset(riP, '\0', sizeof(struct relocation_info));
1122 symbolP = fixP->fx_addsy;
1124 #ifdef ARM
1125 /* see arm_reloc.h for the encodings in the low 2 bits */
1126 if(fixP->fx_r_type == ARM_RELOC_LO16 ||
1127 fixP->fx_r_type == ARM_RELOC_HI16 ||
1128 fixP->fx_r_type == ARM_THUMB_RELOC_LO16 ||
1129 fixP->fx_r_type == ARM_THUMB_RELOC_HI16){
1130 riP->r_length = fixP->fx_r_type & 0x3;
1132 else
1133 #endif
1134 switch(fixP->fx_size){
1135 case 1:
1136 riP->r_length = 0;
1137 break;
1138 case 2:
1139 riP->r_length = 1;
1140 break;
1141 case 4:
1142 #ifdef PPC
1143 if(fixP->fx_r_type == PPC_RELOC_BR14_predicted)
1144 riP->r_length = 3;
1145 else
1146 #endif
1147 riP->r_length = 2;
1148 break;
1149 #if defined(ARCH64)
1150 case 8:
1151 riP->r_length = 3;
1152 break;
1153 #endif /* defined(ARCH64) */
1154 default:
1155 layout_file = fixP->file;
1156 layout_line = fixP->line;
1157 as_fatal("Bad fx_size (0x%x) in fix_to_relocation_info()\n",
1158 fixP->fx_size);
1160 riP->r_pcrel = fixP->fx_pcrel;
1161 riP->r_address = fixP->fx_frag->fr_address + fixP->fx_where -
1162 sect_addr;
1163 #ifdef ARM
1164 if(fixP->fx_r_type == ARM_RELOC_LO16 ||
1165 fixP->fx_r_type == ARM_RELOC_HI16 ||
1166 fixP->fx_r_type == ARM_THUMB_RELOC_LO16 ||
1167 fixP->fx_r_type == ARM_THUMB_RELOC_HI16){
1168 riP->r_type = ARM_RELOC_HALF;
1170 else
1171 #endif
1172 riP->r_type = fixP->fx_r_type;
1174 * For undefined symbols this will be an external relocation entry.
1175 * Or if this is an external coalesced symbol or weak symbol.
1177 #if defined(I386) && defined(ARCH64)
1178 if(fixP->fx_subsy == NULL &&
1179 (!debug_section || (symbolP->sy_type & N_TYPE) == N_UNDF) &&
1180 (!is_local_symbol(symbolP) ||
1181 ((symbolP->sy_type & N_TYPE) == N_SECT &&
1182 is_section_cstring_literals(symbolP->sy_other)) ) ) {
1183 #else
1184 if((symbolP->sy_type & N_TYPE) == N_UNDF ||
1185 ((symbolP->sy_type & N_EXT) == N_EXT &&
1186 (symbolP->sy_type & N_TYPE) == N_SECT &&
1187 (is_section_coalesced(symbolP->sy_other) ||
1188 (symbolP->sy_desc & N_WEAK_DEF) == N_WEAK_DEF) &&
1189 fixP->fx_subsy == NULL)){
1190 #endif
1191 riP->r_extern = 1;
1192 riP->r_symbolnum = symbolP->sy_number;
1194 else{
1196 * For defined symbols this will be a local relocation entry
1197 * (possibly a section difference or a scattered relocation entry).
1199 riP->r_extern = 0;
1200 riP->r_symbolnum = symbolP->sy_other; /* nsect */
1203 * Determine if this is left as a local relocation entry or
1204 * changed to a SECTDIFF relocation entry. If this comes from a fix
1205 * that has a subtract symbol it is a SECTDIFF relocation. Which is
1206 * "addsy - subsy + constant" where both symbols are defined in
1207 * sections. To encode all this information two scattered
1208 * relocation entries are used. The first has the add symbol value
1209 * and the second has the subtract symbol value.
1211 if(fixP->fx_subsy != NULL){
1212 #if defined(I386) && defined(ARCH64)
1213 /* Encode fixP->fx_subsy (B) first, then symbolP (fixP->fx_addsy) (A). */
1214 if (is_local_symbol(fixP->fx_subsy))
1216 riP->r_extern = 0;
1217 riP->r_symbolnum = fixP->fx_subsy->sy_other;
1219 else
1221 riP->r_extern = 1;
1222 riP->r_symbolnum = fixP->fx_subsy->sy_number;
1224 riP->r_type = X86_64_RELOC_SUBTRACTOR;
1226 /* Now write out the unsigned relocation entry. */
1227 riP++;
1228 *riP = *(riP - 1);
1229 if (is_local_symbol(fixP->fx_addsy))
1231 riP->r_extern = 0;
1232 riP->r_symbolnum = fixP->fx_addsy->sy_other;
1234 else
1236 riP->r_extern = 1;
1237 riP->r_symbolnum = fixP->fx_addsy->sy_number;
1239 riP->r_type = X86_64_RELOC_UNSIGNED;
1240 return(2 * sizeof(struct relocation_info));
1241 #endif
1242 #ifdef PPC
1243 if(fixP->fx_r_type == PPC_RELOC_HI16)
1244 sectdiff = PPC_RELOC_HI16_SECTDIFF;
1245 else if(fixP->fx_r_type == PPC_RELOC_LO16)
1246 sectdiff = PPC_RELOC_LO16_SECTDIFF;
1247 else if(fixP->fx_r_type == PPC_RELOC_HA16)
1248 sectdiff = PPC_RELOC_HA16_SECTDIFF;
1249 else if(fixP->fx_r_type == PPC_RELOC_LO14)
1250 sectdiff = PPC_RELOC_LO14_SECTDIFF;
1251 else
1252 #endif
1253 #ifdef HPPA
1254 if(fixP->fx_r_type == HPPA_RELOC_HI21)
1255 sectdiff = HPPA_RELOC_HI21_SECTDIFF;
1256 else if(fixP->fx_r_type == HPPA_RELOC_LO14)
1257 sectdiff = HPPA_RELOC_LO14_SECTDIFF;
1258 else
1259 #endif
1260 #ifdef SPARC
1261 if(fixP->fx_r_type == SPARC_RELOC_HI22)
1262 sectdiff = SPARC_RELOC_HI22_SECTDIFF;
1263 else if(fixP->fx_r_type == SPARC_RELOC_LO10)
1264 sectdiff = SPARC_RELOC_LO10_SECTDIFF;
1265 else
1266 #endif
1267 #ifdef ARM
1268 if(fixP->fx_r_type == ARM_RELOC_LO16 ||
1269 fixP->fx_r_type == ARM_RELOC_HI16 ||
1270 fixP->fx_r_type == ARM_THUMB_RELOC_LO16 ||
1271 fixP->fx_r_type == ARM_THUMB_RELOC_HI16)
1272 sectdiff = ARM_RELOC_HALF_SECTDIFF;
1273 else
1274 #endif
1276 if(fixP->fx_r_type != 0){
1277 layout_file = fixP->file;
1278 layout_line = fixP->line;
1279 as_fatal("Internal error: incorrect fx_r_type (%u) for "
1280 "fx_subsy != 0 in fix_to_relocation_info()",
1281 fixP->fx_r_type);
1283 if((!(fixP->fx_addsy->sy_type & N_EXT)) && flagseen['k'])
1284 sectdiff = RELOC_LOCAL_SECTDIFF;
1285 else
1286 sectdiff = RELOC_SECTDIFF;
1288 memset(&sri, '\0',sizeof(struct scattered_relocation_info));
1289 sri.r_scattered = 1;
1290 #ifdef ARM
1291 /* see arm_reloc.h for the encodings in the low 2 bits */
1292 if(fixP->fx_r_type == ARM_RELOC_LO16 ||
1293 fixP->fx_r_type == ARM_RELOC_HI16 ||
1294 fixP->fx_r_type == ARM_THUMB_RELOC_LO16 ||
1295 fixP->fx_r_type == ARM_THUMB_RELOC_HI16)
1296 sri.r_length = fixP->fx_r_type & 0x3;
1297 else
1298 #endif
1299 sri.r_length = riP->r_length;
1300 sri.r_pcrel = riP->r_pcrel;
1301 sri.r_address = riP->r_address;
1302 sri.r_type = sectdiff;
1303 sri.r_value = symbolP->sy_value;
1304 *riP = *((struct relocation_info *)&sri);
1305 riP++;
1307 sri.r_type = RELOC_PAIR;
1308 sri.r_value = fixP->fx_subsy->sy_value;
1309 if(sectdiff == RELOC_SECTDIFF ||
1310 sectdiff == RELOC_LOCAL_SECTDIFF)
1311 sri.r_address = 0;
1312 #ifdef PPC
1313 else if(sectdiff == PPC_RELOC_HI16_SECTDIFF ||
1314 sectdiff == PPC_RELOC_HA16_SECTDIFF){
1315 sri.r_address = (symbolP->sy_value -
1316 fixP->fx_subsy->sy_value
1317 + fixP->fx_offset) & 0xffff;
1319 else if(sectdiff == PPC_RELOC_LO16_SECTDIFF ||
1320 sectdiff == PPC_RELOC_LO14_SECTDIFF){
1321 sri.r_address = ((symbolP->sy_value -
1322 fixP->fx_subsy->sy_value +
1323 fixP->fx_offset) >> 16) & 0xffff;
1325 #endif
1326 #ifdef HPPA
1327 else if(sectdiff == HPPA_RELOC_HI21_SECTDIFF){
1328 calc_hppa_HILO(symbolP->sy_value - fixP->fx_subsy->sy_value,
1329 fixP->fx_offset, &left21, &right14);
1330 sri.r_address = right14 & 0x3fff;
1332 else if(sectdiff == HPPA_RELOC_LO14_SECTDIFF){
1333 calc_hppa_HILO(symbolP->sy_value - fixP->fx_subsy->sy_value,
1334 fixP->fx_offset, &left21, &right14);
1335 sri.r_address = left21 >> 11;
1337 #endif
1338 #ifdef SPARC
1339 else if(sectdiff == SPARC_RELOC_HI22_SECTDIFF){
1340 sri.r_address = (symbolP->sy_value -
1341 fixP->fx_subsy->sy_value
1342 + fixP->fx_offset) & 0x3ff;
1344 else if(sectdiff == SPARC_RELOC_LO10_SECTDIFF){
1345 sri.r_address = ((symbolP->sy_value -
1346 fixP->fx_subsy->sy_value +
1347 fixP->fx_offset) >> 10) & 0x3fffff;
1349 #endif
1350 #ifdef ARM
1351 else if(sectdiff == ARM_RELOC_HALF_SECTDIFF){
1352 if((sri.r_length & 0x1) == 0x1)
1353 sri.r_address = (symbolP->sy_value -
1354 fixP->fx_subsy->sy_value
1355 + fixP->fx_offset) & 0xffff;
1356 else
1357 sri.r_address = ((symbolP->sy_value -
1358 fixP->fx_subsy->sy_value +
1359 fixP->fx_offset) >> 16) & 0xffff;
1361 #endif
1362 *riP = *((struct relocation_info *)&sri);
1363 return(2 * sizeof(struct relocation_info));
1366 * Determine if this is left as a local relocation entry or must be
1367 * changed to a scattered relocation entry. These entries allow
1368 * the link editor to scatter the contents of a section and a local
1369 * relocation can't be used when an offset is added to the symbol's
1370 * value (symbol + offset). This is because the relocation must be
1371 * based on the value of the symbol not the value of the expression.
1372 * Thus a scattered relocation entry that encodes the address of the
1373 * symbol is used when the offset is non-zero. Unfortunately this
1374 * encoding only allows for 24 bits in the r_address field and can
1375 * overflow. So it if it would overflow we don't create a
1376 * scattered relocation entry and hope the offset does not reach
1377 * out of the block or the linker will not be doing scattered
1378 * loading on this symbol in this object file.
1380 #if !defined(I860) && !(defined(I386) && defined(ARCH64))
1382 * For processors that don't have all references as unique 32 bits
1383 * wide references scattered relocation entries are not generated.
1384 * This is so that the link editor does not get stuck not being able
1385 * to do the relocation if the high half of the reference is shared
1386 * by two references to two different symbols.
1388 if(fixP->fx_offset != 0 &&
1389 (riP->r_address & 0xff000000) == 0 &&
1390 ((symbolP->sy_type & N_TYPE) & ~N_EXT) != N_ABS
1391 #ifdef M68K
1393 * Since the m68k's pc relative branch instructions use the
1394 * address of the beginning of the displacement (except for
1395 * byte) the code in m68k.c when generating fixes adds to the
1396 * offset 2 for word and 4 for long displacements.
1398 && !(fixP->fx_pcrel &&
1399 ((fixP->fx_size == 2 && fixP->fx_offset == 2) ||
1400 (fixP->fx_size == 4 && fixP->fx_offset == 4)) )
1401 #endif /* M68K */
1403 memset(&sri, '\0',sizeof(struct scattered_relocation_info));
1404 sri.r_scattered = 1;
1405 sri.r_length = riP->r_length;
1406 sri.r_pcrel = riP->r_pcrel;
1407 sri.r_address = riP->r_address;
1408 sri.r_type = riP->r_type;
1409 sri.r_value = symbolP->sy_value;
1410 *riP = *((struct relocation_info *)&sri);
1412 #endif /* !defined(I860) && !(defined(I386) && defined(ARCH64)) */
1414 count = 1;
1415 riP++;
1417 #if !defined(M68K) && !defined(I386)
1419 * For RISC machines whenever we have a relocation item using the half
1420 * of an address we also emit a relocation item describing the other
1421 * half of the address so the linker can reconstruct the address to do
1422 * the relocation.
1424 #ifdef I860
1425 if(fixP->fx_r_type == I860_RELOC_HIGH ||
1426 fixP->fx_r_type == I860_RELOC_HIGHADJ)
1427 #endif
1428 #ifdef M88K
1429 if(fixP->fx_r_type == M88K_RELOC_HI16 ||
1430 fixP->fx_r_type == M88K_RELOC_LO16)
1431 #endif
1432 #ifdef PPC
1433 if(fixP->fx_r_type == PPC_RELOC_HI16 ||
1434 fixP->fx_r_type == PPC_RELOC_LO16 ||
1435 fixP->fx_r_type == PPC_RELOC_HA16 ||
1436 fixP->fx_r_type == PPC_RELOC_LO14 ||
1437 fixP->fx_r_type == PPC_RELOC_JBSR)
1438 #endif
1439 #ifdef HPPA
1440 if(fixP->fx_r_type == HPPA_RELOC_HI21 ||
1441 fixP->fx_r_type == HPPA_RELOC_LO14 ||
1442 fixP->fx_r_type == HPPA_RELOC_BR17 ||
1443 fixP->fx_r_type == HPPA_RELOC_JBSR)
1444 #endif
1445 #ifdef SPARC
1446 if(fixP->fx_r_type == SPARC_RELOC_HI22 ||
1447 fixP->fx_r_type == SPARC_RELOC_LO10)
1448 #endif
1449 #ifdef ARM
1450 if(fixP->fx_r_type == ARM_RELOC_LO16 ||
1451 fixP->fx_r_type == ARM_RELOC_HI16 ||
1452 fixP->fx_r_type == ARM_THUMB_RELOC_LO16 ||
1453 fixP->fx_r_type == ARM_THUMB_RELOC_HI16)
1454 #endif
1456 memset(riP, '\0', sizeof(struct relocation_info));
1457 #ifdef ARM
1458 /* see arm_reloc.h for the encodings in the low 2 bits */
1459 if(fixP->fx_r_type == ARM_RELOC_LO16 ||
1460 fixP->fx_r_type == ARM_RELOC_HI16 ||
1461 fixP->fx_r_type == ARM_THUMB_RELOC_LO16 ||
1462 fixP->fx_r_type == ARM_THUMB_RELOC_HI16){
1463 riP->r_length = fixP->fx_r_type & 0x3;
1465 else
1466 #endif
1467 switch(fixP->fx_size){
1468 case 1:
1469 riP->r_length = 0;
1470 break;
1471 case 2:
1472 riP->r_length = 1;
1473 break;
1474 case 4:
1475 riP->r_length = 2;
1476 break;
1477 #if defined(ARCH64)
1478 case 8:
1479 riP->r_length = 3;
1480 break;
1481 #endif /* defined(ARCH64) */
1482 default:
1483 as_fatal("Bad fx_size (0x%x) in fix_to_relocation_info()\n",
1484 fixP->fx_size);
1486 riP->r_pcrel = fixP->fx_pcrel;
1488 * We set r_extern to 0, so other apps won't try to use r_symbolnum
1489 * as a symbol table indice. We set all the bits of r_symbolnum so
1490 * it is all but guaranteed to be outside the range we use for non-
1491 * external types to denote what section the relocation is in.
1493 riP->r_extern = 0;
1494 riP->r_symbolnum = 0x00ffffff;
1495 #ifdef I860
1496 riP->r_type = I860_RELOC_PAIR;
1497 riP->r_address = 0xffff & fixP->fx_value;
1498 #endif
1499 #ifdef M88K
1500 riP->r_type = M88K_RELOC_PAIR;
1501 if(fixP->fx_r_type == M88K_RELOC_HI16)
1502 riP->r_address = 0xffff & fixP->fx_value;
1503 else if(fixP->fx_r_type == M88K_RELOC_LO16)
1504 riP->r_address = 0xffff & (fixP->fx_value >> 16);
1505 #endif
1506 #ifdef PPC
1507 riP->r_type = PPC_RELOC_PAIR;
1508 if(fixP->fx_r_type == PPC_RELOC_HI16 ||
1509 fixP->fx_r_type == PPC_RELOC_HA16)
1510 riP->r_address = 0xffff & fixP->fx_value;
1511 else if(fixP->fx_r_type == PPC_RELOC_LO16 ||
1512 fixP->fx_r_type == PPC_RELOC_LO14)
1513 riP->r_address = 0xffff & (fixP->fx_value >> 16);
1514 else if (fixP->fx_r_type == PPC_RELOC_JBSR){
1516 * To allow the "true target address" to use the full 32 bits
1517 * we convert this PAIR relocation entry to a scattered
1518 * relocation entry if the true target address has the
1519 * high bit (R_SCATTERED) set and store the "true target
1520 * address" in the r_value field. Or for an external relocation
1521 * entry if the "offset" to the symbol has the high bit set
1522 * we also use a scattered relocation entry.
1524 if((fixP->fx_value & R_SCATTERED) == 0){
1525 riP->r_address = fixP->fx_value;
1527 else{
1528 memset(&sri, '\0',sizeof(struct scattered_relocation_info));
1529 sri.r_scattered = 1;
1530 sri.r_pcrel = riP->r_pcrel;
1531 sri.r_length = riP->r_length;
1532 sri.r_type = riP->r_type;
1533 sri.r_address = 0;
1534 sri.r_value = fixP->fx_value;
1535 *riP = *((struct relocation_info *)&sri);
1538 #endif
1539 #ifdef HPPA
1540 riP->r_type = HPPA_RELOC_PAIR;
1541 calc_hppa_HILO(fixP->fx_value - fixP->fx_offset,
1542 fixP->fx_offset, &left21, &right14);
1543 if (fixP->fx_r_type == HPPA_RELOC_LO14 ||
1544 fixP->fx_r_type == HPPA_RELOC_BR17)
1545 riP->r_address = left21 >> 11;
1546 else if (fixP->fx_r_type == HPPA_RELOC_HI21)
1547 riP->r_address = right14 & 0x3fff;
1548 else if (fixP->fx_r_type == HPPA_RELOC_JBSR){
1549 if((symbolP->sy_type & N_TYPE) == N_UNDF)
1550 riP->r_address = fixP->fx_value & 0xffffff;
1551 else
1552 riP->r_address = (fixP->fx_value - sect_addr) & 0xffffff;
1554 #endif
1555 #ifdef SPARC
1556 riP->r_type = SPARC_RELOC_PAIR;
1557 if (fixP->fx_r_type == SPARC_RELOC_HI22)
1558 riP->r_address = fixP->fx_value & 0x3ff;
1559 else if (fixP->fx_r_type == SPARC_RELOC_LO10)
1560 riP->r_address = (fixP->fx_value >> 10) & 0x3fffff;
1561 #endif
1562 #ifdef ARM
1563 riP->r_type = ARM_RELOC_PAIR;
1564 if(fixP->fx_r_type == ARM_RELOC_HI16 ||
1565 fixP->fx_r_type == ARM_THUMB_RELOC_HI16)
1566 riP->r_address = 0xffff & fixP->fx_value;
1567 else if(fixP->fx_r_type == ARM_RELOC_LO16 ||
1568 fixP->fx_r_type == ARM_THUMB_RELOC_LO16)
1569 riP->r_address = 0xffff & (fixP->fx_value >> 16);
1570 #endif
1571 count = 2;
1573 #endif /* !defined(M68K) && !defined(I386) */
1574 return(count * sizeof(struct relocation_info));
1577 #ifdef I860
1579 * set_default_section_align() is used to set a default minimum section
1580 * alignment if the section exist.
1582 static
1583 void
1584 set_default_section_align(
1585 char *segname,
1586 char *sectname,
1587 uint32_t align)
1589 frchainS *frcP;
1591 for(frcP = frchain_root; frcP != NULL; frcP = frcP->frch_next){
1592 if(strncmp(frcP->frch_section.segname, segname,
1593 sizeof(frcP->frch_section.segname)) == 0 &&
1594 strncmp(frcP->frch_section.sectname, sectname,
1595 sizeof(frcP->frch_section.sectname)) == 0){
1596 if(align > frcP->frch_section.align)
1597 frcP->frch_section.align = align;
1598 return;
1604 * clear_section_flags() clears the section types for literals from the section
1605 * flags field. This is needed for processors that don't have all references
1606 * to sections as unique 32 bits wide references. In this case the literal
1607 * flags are not set. This is so that the link editor does not merge them and
1608 * get stuck not being able to fit the relocated address in the item to be
1609 * relocated or if the high half of the reference is shared by two references
1610 * to different symbols (which can also stick the link editor).
1612 static
1613 void
1614 clear_section_flags(void)
1616 frchainS *frcP;
1618 for(frcP = frchain_root; frcP != NULL; frcP = frcP->frch_next)
1619 if(frcP->frch_section.flags != S_ZEROFILL)
1620 frcP->frch_section.flags = 0;
1624 * I860_tweeks() preforms the tweeks needed by the I860 processor to get minimum
1625 * section alignments and no merging of literals by the link editor.
1627 static
1628 void
1629 I860_tweeks(void)
1631 set_default_section_align("__TEXT", "__text", 5);
1632 set_default_section_align("__DATA", "__data", 4);
1633 set_default_section_align("__DATA", "__bss", 4);
1635 clear_section_flags();
1637 #endif
1639 /* FROM write.c line 2764 */
1640 void
1641 number_to_chars_bigendian (char *buf, signed_expr_t val, int n)
1643 if (n <= 0)
1644 abort ();
1645 while (n--)
1647 buf[n] = val & 0xff;
1648 val >>= 8;
1652 void
1653 number_to_chars_littleendian (char *buf, signed_expr_t val, int n)
1655 if (n <= 0)
1656 abort ();
1657 while (n--)
1659 *buf++ = val & 0xff;
1660 val >>= 8;