8 #include "stuff/openstep_mach.h"
9 #include <mach-o/loader.h>
10 #include <mach-o/reloc.h>
11 #include <mach-o/stab.h>
13 #include <mach-o/i860/reloc.h>
16 #include <mach-o/m88k/reloc.h>
19 #include <mach-o/ppc/reloc.h>
22 #include <mach-o/hppa/reloc.h>
23 #include "stuff/hppa.h"
26 #include <mach-o/sparc/reloc.h>
29 #include <mach-o/arm/reloc.h>
30 #include "arm_reloc.h"
32 #if defined(I386) && defined(ARCH64)
33 #include <mach-o/x86_64/reloc.h>
35 #include "stuff/rnd.h"
36 #include "stuff/bytesex.h"
37 #include "stuff/errors.h"
39 #include "struc-symbol.h"
47 #include "input-scrub.h"
48 #if defined(I386) && defined(ARCH64)
52 #define RELOC_SECTDIFF I860_RELOC_SECTDIFF
53 #define RELOC_LOCAL_SECTDIFF I860_RELOC_SECTDIFF
54 #define RELOC_PAIR I860_RELOC_PAIR
57 #define RELOC_SECTDIFF M88K_RELOC_SECTDIFF
58 #define RELOC_LOCAL_SECTDIFF M88K_RELOC_SECTDIFF
59 #define RELOC_PAIR M88K_RELOC_PAIR
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)
68 #define RELOC_SECTDIFF HPPA_RELOC_SECTDIFF
69 #define RELOC_LOCAL_SECTDIFF HPPA_RELOC_SECTDIFF
70 #define RELOC_PAIR HPPA_RELOC_PAIR
73 #define RELOC_SECTDIFF SPARC_RELOC_SECTDIFF
74 #define RELOC_LOCAL_SECTDIFF SPARC_RELOC_SECTDIFF
75 #define RELOC_PAIR SPARC_RELOC_PAIR
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
83 #define RELOC_SECTDIFF ARM_RELOC_SECTDIFF
84 #define RELOC_LOCAL_SECTDIFF ARM_RELOC_SECTDIFF
85 #define RELOC_PAIR ARM_RELOC_PAIR
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:
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(
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(
122 static uint32_t fix_to_relocation_entries(
125 struct relocation_info
*riP
,
126 uint32_t debug_section
);
133 * write_object() writes a Mach-O object file from the built up data structures.
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
;
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
;
158 uint32_t output_size
;
162 enum byte_sex host_byte_sex
;
163 uint32_t reloff
, nrelocs
;
171 struct stat stat_buf
;
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
185 * Fill in the addr and size fields of each section structure and count
186 * the number of sections.
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
;
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.
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
;
217 header
.cpusubtype
= md_cpusubtype
;
219 header
.filetype
= MH_OBJECT
;
221 header
.sizeofcmds
= 0;
224 header
.sizeofcmds
+= sizeof(segment_command_t
) +
225 nsects
* sizeof(section_t
);
229 header
.sizeofcmds
+= sizeof(struct symtab_command
);
232 header
.sizeofcmds
+= sizeof(struct dysymtab_command
);
238 if(subsections_via_symbols
== TRUE
)
239 header
.flags
|= MH_SUBSECTIONS_VIA_SYMBOLS
;
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
)
271 for(p
= frchainP
->frch_next
; p
!= NULL
; p
= p
->frch_next
)
272 if((p
->frch_section
.flags
& SECTION_TYPE
) != S_ZEROFILL
)
275 i
= p
->frch_section
.addr
- frchainP
->frch_section
.addr
;
277 i
= frchainP
->frch_section
.size
;
278 reloc_segment
.filesize
+= i
;
279 frchainP
->frch_section
.offset
= offset
;
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
)
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));
308 for(frchainP
= frchain_root
; frchainP
; frchainP
= frchainP
->frch_next
){
309 if(frchainP
->frch_section
.nreloc
== 0)
310 frchainP
->frch_section
.reloff
= 0;
312 frchainP
->frch_section
.reloff
= offset
;
313 offset
+= frchainP
->frch_section
.nreloc
*
314 sizeof(struct relocation_info
);
315 nrelocs
+= frchainP
->frch_section
.nreloc
;
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;
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
);
356 symbol_table
.symoff
= 0;
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 */
364 symbol_table
.stroff
= 0;
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",
385 /* put the headers in the output file's buffer */
386 host_byte_sex
= get_host_byte_sex();
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
),
394 offset
+= sizeof(mach_header_t
);
396 /* put the segment_command in the buffer */
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
),
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
),
411 if(host_byte_sex
!= md_target_byte_sex
)
412 swap_section_t((section_t
*)(output_addr
+ offset
), 1,
414 offset
+= sizeof(section_t
);
417 /* put the symbol_command in the buffer */
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
),
425 offset
+= sizeof(struct symtab_command
);
429 /* put the dysymbol_command in the buffer */
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
),
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
);
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) {
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
,
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
,
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
),
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
),
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
),
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(
523 frchainP
->frch_section
.addr
,
524 (struct relocation_info
*)(output_addr
+
526 frchainP
->frch_section
.flags
&
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
);
535 /* put the indirect symbol table in the buffer */
536 offset
= dynamic_symbol_table
.indirectsymoff
;
537 for(frchainP
= frchain_root
;
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
;
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
) !=
562 local
= INDIRECT_SYMBOL_LOCAL
;
563 if((isymbolP
->isy_symbol
->sy_nlist
.n_type
&
565 local
|= INDIRECT_SYMBOL_ABS
;
566 memcpy(output_addr
+ offset
, (char *)(&local
),
570 memcpy(output_addr
+ offset
,
571 (char *)(&isymbolP
->isy_symbol
->sy_number
),
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
,
586 /* put the strings in the output file's buffer */
587 offset
= symbol_table
.stroff
;
588 if(symbol_table
.strsize
!= 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).
622 * Avoid doing the unlink() on special files, just unlink regular files
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");
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.
644 layout_indirect_symbols(void)
646 struct frchain
*frchainP
;
647 uint32_t section_type
, total
, count
, stride
;
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
;
663 isymbolP
= isymbolP
->isy_next
){
665 (void)symbol_find_or_make(isymbolP->isy_name);
667 symbolP
= symbol_find(isymbolP
->isy_name
);
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
;
682 isymbolP
= isymbolP
->isy_next
){
684 symbolP
= symbol_find(isymbolP
->isy_name
);
686 symbolP
= symbol_find_or_make(isymbolP
->isy_name
);
687 symbolP
->sy_desc
|= REFERENCE_FLAG_UNDEFINED_LAZY
;
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
){
700 for(isymbolP
= frchainP
->frch_isym_root
;
702 isymbolP
= isymbolP
->isy_next
){
705 symbolP = symbol_find_or_make(isymbolP->isy_name);
707 symbolP
= symbol_find(isymbolP
->isy_name
);
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
;
717 * Check for missing indirect symbols.
719 if(section_type
== S_SYMBOL_STUBS
)
720 stride
= frchainP
->frch_section
.reserved2
;
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
;
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.
755 set_BINCL_checksums()
759 struct HeaderRange
* parentRange
;
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
){
771 struct HeaderRange
* range
=
772 xmalloc(sizeof(struct HeaderRange
));
773 range
->bincl
= symbolP
;
774 range
->parentRange
= curRange
;
776 range
->okToChecksum
= (symbolP
->sy_nlist
.n_value
== 0);
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
;
797 if(curRange
!= NULL
){
798 curRange
->okToChecksum
= FALSE
;
804 if(curRange
!= NULL
){
805 if(curRange
->okToChecksum
){
806 unsigned int sum
= 0;
807 const char* s
= symbolP
->sy_name
;
809 while((c
= *s
++) != '\0'){
812 * Don't checkusm first number (file index)
813 * after open paren in string.
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
834 * The order of the symbol table is:
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
845 int32_t *symbol_number
,
846 int32_t *string_byte_count
)
852 int seenBINCL
= FALSE
;
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.
871 (symbolP
->sy_nlist
.n_type
& N_STAB
) == 0 &&
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]);
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
)
890 if((symbolP
->sy_type
& N_EXT
) == 0){
892 symbolP
->sy_number
= *symbol_number
;
893 *symbol_number
= *symbol_number
+ 1;
897 symbolP
->sy_name_offset
= *string_byte_count
;
898 *string_byte_count
+= strlen(symbolP
->sy_name
) + 1;
900 symbolPP
= &(symbolP
->sy_next
);
903 /* don't keep this symbol */
904 *symbolPP
= symbolP
->sy_next
;
908 * All non-temporary symbols will be the symbol table in the output
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;
922 if((symbolP
->sy_type
& N_TYPE
) != N_UNDF
)
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;
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",
952 else if((symbolP
->sy_type
& N_TYPE
) == N_UNDF
){
953 as_bad("Undefined symbol: %s can't be a weak_definition",
959 /* Set the indexes for symbol groups into the symbol table */
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
*));
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
)
981 /* the .stabd case (symbol has no name) */
982 symbolP
->sy_name_offset
= 0;
986 if((symbolP
->sy_type
& N_TYPE
) != N_UNDF
)
987 extdefsyms
[i
++] = symbolP
;
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 */
1007 set_BINCL_checksums();
1011 * Function for qsort to sort symbol structs by their name
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.
1032 * If fx_addsy is NULL then this fix needs no relocation entry.
1034 if(fixP
->fx_addsy
== NULL
)
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
)
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.
1050 if(fixP
->fx_r_type
== I860_RELOC_HIGH
||
1051 fixP
->fx_r_type
== I860_RELOC_HIGHADJ
)
1055 if(fixP
->fx_r_type
== M88K_RELOC_HI16
||
1056 fixP
->fx_r_type
== M88K_RELOC_LO16
)
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
)
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
)
1075 if(fixP
->fx_r_type
== SPARC_RELOC_HI22
||
1076 fixP
->fx_r_type
== SPARC_RELOC_LO10
)
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
)
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
1097 fix_to_relocation_entries(
1100 struct relocation_info
*riP
,
1101 uint32_t debug_section
)
1103 struct symbol
*symbolP
;
1105 struct scattered_relocation_info sri
;
1108 uint32_t left21
, right14
;
1112 * If fx_addsy is NULL then this fix needs no relocation entry.
1114 if(fixP
->fx_addsy
== NULL
)
1117 #ifdef TC_VALIDATE_FIX
1118 TC_VALIDATE_FIX(fixP
, sect_addr
, 0);
1121 memset(riP
, '\0', sizeof(struct relocation_info
));
1122 symbolP
= fixP
->fx_addsy
;
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;
1134 switch(fixP
->fx_size
){
1143 if(fixP
->fx_r_type
== PPC_RELOC_BR14_predicted
)
1153 #endif /* defined(ARCH64) */
1155 layout_file
= fixP
->file
;
1156 layout_line
= fixP
->line
;
1157 as_fatal("Bad fx_size (0x%x) in fix_to_relocation_info()\n",
1160 riP
->r_pcrel
= fixP
->fx_pcrel
;
1161 riP
->r_address
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
-
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
;
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
)) ) ) {
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
)){
1192 riP
->r_symbolnum
= symbolP
->sy_number
;
1196 * For defined symbols this will be a local relocation entry
1197 * (possibly a section difference or a scattered relocation entry).
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
))
1217 riP
->r_symbolnum
= fixP
->fx_subsy
->sy_other
;
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. */
1229 if (is_local_symbol(fixP
->fx_addsy
))
1232 riP
->r_symbolnum
= fixP
->fx_addsy
->sy_other
;
1237 riP
->r_symbolnum
= fixP
->fx_addsy
->sy_number
;
1239 riP
->r_type
= X86_64_RELOC_UNSIGNED
;
1240 return(2 * sizeof(struct relocation_info
));
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
;
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
;
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
;
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
;
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()",
1283 if((!(fixP
->fx_addsy
->sy_type
& N_EXT
)) && flagseen
['k'])
1284 sectdiff
= RELOC_LOCAL_SECTDIFF
;
1286 sectdiff
= RELOC_SECTDIFF
;
1288 memset(&sri
, '\0',sizeof(struct scattered_relocation_info
));
1289 sri
.r_scattered
= 1;
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;
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
);
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
)
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;
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;
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;
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;
1357 sri
.r_address
= ((symbolP
->sy_value
-
1358 fixP
->fx_subsy
->sy_value
+
1359 fixP
->fx_offset
) >> 16) & 0xffff;
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
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)) )
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)) */
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
1425 if(fixP
->fx_r_type
== I860_RELOC_HIGH
||
1426 fixP
->fx_r_type
== I860_RELOC_HIGHADJ
)
1429 if(fixP
->fx_r_type
== M88K_RELOC_HI16
||
1430 fixP
->fx_r_type
== M88K_RELOC_LO16
)
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
)
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
)
1446 if(fixP
->fx_r_type
== SPARC_RELOC_HI22
||
1447 fixP
->fx_r_type
== SPARC_RELOC_LO10
)
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
)
1456 memset(riP
, '\0', sizeof(struct relocation_info
));
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;
1467 switch(fixP
->fx_size
){
1481 #endif /* defined(ARCH64) */
1483 as_fatal("Bad fx_size (0x%x) in fix_to_relocation_info()\n",
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.
1494 riP
->r_symbolnum
= 0x00ffffff;
1496 riP
->r_type
= I860_RELOC_PAIR
;
1497 riP
->r_address
= 0xffff & fixP
->fx_value
;
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);
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
;
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
;
1534 sri
.r_value
= fixP
->fx_value
;
1535 *riP
= *((struct relocation_info
*)&sri
);
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;
1552 riP
->r_address
= (fixP
->fx_value
- sect_addr
) & 0xffffff;
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;
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);
1573 #endif /* !defined(M68K) && !defined(I386) */
1574 return(count
* sizeof(struct relocation_info
));
1579 * set_default_section_align() is used to set a default minimum section
1580 * alignment if the section exist.
1584 set_default_section_align(
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
;
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).
1614 clear_section_flags(void)
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.
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();
1639 /* FROM write.c line 2764 */
1641 number_to_chars_bigendian (char *buf
, signed_expr_t val
, int n
)
1647 buf
[n
] = val
& 0xff;
1653 number_to_chars_littleendian (char *buf
, signed_expr_t val
, int n
)
1659 *buf
++ = val
& 0xff;