1 %************************************************************************
3 \section[Driver-obj-splitting]{Splitting into many \tr{.o} files (for libraries)}
5 %************************************************************************
8 $TargetPlatform = $TARGETPLATFORM;
10 ($Pgm = $0) =~ s|.*/||;
12 $Tmp_prefix = $ARGV[1];
15 &split_asm_file($ifile);
17 open(OUTPUT, "> $Output") || &tidy_up_and_die(1,"$Pgm: failed to open `$Output' (to write)\n");
18 print OUTPUT "$NoOfSplitFiles\n";
27 local($asm_file) = @_;
30 open(TMPI, "< $asm_file") || &tidy_up_and_die(1,"$Pgm: failed to open `$asm_file' (to read)\n");
32 &collectExports_hppa() if $TargetPlatform =~ /^hppa/;
33 &collectExports_mips() if $TargetPlatform =~ /^mips/;
34 &collectDyldStuff_darwin() if $TargetPlatform =~ /-apple-darwin/;
36 $octr = 0; # output file counter
37 $* = 1; # multi-line matches are OK
39 %LocalConstant = (); # we have to subvert C compiler's commoning-up of constants...
41 $s_stuff = &ReadTMPIUpToAMarker( '', $octr );
42 # that first stuff is a prologue for all .s outputs
43 $prologue_stuff = &process_asm_block ( $s_stuff );
44 # $_ already has some of the next stuff in it...
46 # &tidy_up_and_die(1,"$Pgm: no split markers in .s file!\n")
47 # if $prologue_stuff eq $s_stuff;
49 # lie about where this stuff came from
50 # Note the \Q: this ignores regex meta-chars in $Tmp_prefix.
51 $prologue_stuff =~ s/\Q"$Tmp_prefix.c"/"$ifile_root.hc"/g;
53 while ( $_ ne '' ) { # not EOF
56 # grab and de-mangle a section of the .s file...
57 $s_stuff = &ReadTMPIUpToAMarker ( $_, $octr );
58 $pieces[$octr] = &process_asm_block ( $s_stuff );
61 # Make sure that we still have some output when the input file is empty
67 $NoOfSplitFiles = $octr;
69 if ($pieces[$NoOfSplitFiles] =~ /(\n[ \t]*\.section[ \t]+\.note\.GNU-stack,[^\n]*\n)/) {
71 for $octr (1..($NoOfSplitFiles - 1)) {
72 $pieces[$octr] .= $note_gnu_stack;
76 for $octr (1..$NoOfSplitFiles) {
77 # output to a file of its own
78 # open a new output file...
79 $ofname = "${Tmp_prefix}__${octr}.s";
80 open(OUTF, "> $ofname") || die "$Pgm: can't open output file: $ofname\n";
82 print OUTF $prologue_stuff;
83 print OUTF $pieces[$octr];
86 || &tidy_up_and_die(1,"$Pgm:Failed writing ${Tmp_prefix}__${octr}.s\n");
89 close(TMPI) || &tidy_up_and_die(1,"Failed reading $asm_file\n");
92 sub collectExports_hppa { # Note: HP-PA only
94 %LocalExport = (); # NB: global table
97 if (/^\s+\.EXPORT\s+([^,]+),.*\n/) {
99 local($body) = "\t.IMPORT $label";
105 $label =~ s/\$/\\\$/g;
106 $LocalExport{$label} = $body;
113 sub collectExports_mips { # Note: MIPS only
114 # (not really sure this is necessary [WDP 95/05])
116 $UNDEFINED_FUNS = ''; # NB: global table
119 $UNDEFINED_FUNS .= $_ if /^\t\.globl\s+\S+ \.\S+\n/;
126 sub collectDyldStuff_darwin {
127 local($chunk_label,$label,$cur_section,$section,$chunk,$alignment,$cur_alignment);
129 %DyldChunks = (); # NB: global table
130 %DyldChunksDefined = (); # NB: global table
141 if ( $_ eq '' || (/^L(_.+)\$.+:/ && !(/^L(.*)\$stub_binder:/))) {
142 if ( $label ne '' ) {
143 $DyldChunksDefined{$label} .= $section . $alignment . $chunk_label . $ chunk;
144 if( $section =~ s/\.data/\.non_lazy_symbol_pointer/ ) {
145 $chunk = "\t.indirect_symbol $label\n\t.long 0\n";
147 $DyldChunks{$label} .= $section . $alignment . $chunk_label . $chunk;
148 print STDERR "### dyld chunk: $label\n$section$alignment$chunk\n###\n" if $Dump_asm_splitting_info;
155 $section = $cur_section;
156 $alignment = $cur_alignment;
157 print STDERR "label: $label\n" if $Dump_asm_splitting_info;
158 } elsif ( /^\s*\.(symbol_stub|picsymbol_stub|lazy_symbol_pointer|non_lazy_symbol_pointer|data|section __IMPORT,.*|section __DATA, __la_sym_ptr(2|3),lazy_symbol_pointers)/ ) {
160 printf STDERR "section: $cur_section\n" if $Dump_asm_splitting_info;
162 } elsif ( /^\s*\.section\s+__TEXT,__symbol_stub1,symbol_stubs,pure_instructions,\d+/ ) {
164 printf STDERR "section: $cur_section\n" if $Dump_asm_splitting_info;
165 # always make sure we align things
166 $cur_alignment = '\t.align 2'
167 } elsif ( /^\s*\.align.*/ ) {
169 printf STDERR "alignment: $cur_alignment\n" if $Dump_asm_splitting_info;
178 sub ReadTMPIUpToAMarker {
179 local($str, $count) = @_; # already read bits
182 for ( $_ = <TMPI>; $_ ne '' && ! /_?__stg_split_marker/; $_ = <TMPI> ) {
185 # if not EOF, then creep forward until next "real" line
186 # (throwing everything away).
187 # that first "real" line will stay in $_.
189 # This loop is intended to pick up the body of the split_marker function
190 # Note that the assembler mangler will already have eliminated this code
191 # if it's been invoked (which it probably has).
193 while ($_ ne '' && (/_?__stg_split_marker/
203 # || /\t\.end/ NOT! Let the split_marker regexp catch it
204 # || /\t\.ent/ NOT! Let the split_marker regexp catch it
205 || /^\s+(save|retl?|restore|nop)/)) {
209 print STDERR "### BLOCK:$count:\n$str" if $Dump_asm_splitting_info;
212 $str =~ tr/\r//d if $TargetPlatform =~ /-mingw32$/; # in case Perl doesn't convert line endings
217 We must (a)~strip the marker off the block, (b)~record any literal C
218 constants that are defined here, and (c)~inject copies of any C constants
219 that are used-but-not-defined here.
222 sub process_asm_block {
225 return(&process_asm_block_darwin($str))
226 if $TargetPlatform =~ /-apple-darwin/;
227 return(&process_asm_block_m68k($str)) if $TargetPlatform =~ /^m68k-/;
228 return(&process_asm_block_sparc($str)) if $TargetPlatform =~ /^sparc-/;
229 return(&process_asm_block_iX86($str)) if $TargetPlatform =~ /^i[34]86-/;
230 return(&process_asm_block_x86_64($str)) if $TargetPlatform =~ /^x86_64-/;
231 return(&process_asm_block_alpha($str)) if $TargetPlatform =~ /^alpha-/;
232 return(&process_asm_block_hppa($str)) if $TargetPlatform =~ /^hppa/;
233 return(&process_asm_block_mips($str)) if $TargetPlatform =~ /^mips-/;
234 return(&process_asm_block_powerpc_linux($str))
235 if $TargetPlatform =~ /^powerpc-[^-]+-linux/;
238 &tidy_up_and_die(1,"$Pgm: no process_asm_block for $TargetPlatform\n");
241 sub process_asm_block_sparc {
246 $str =~ s/_?__stg_split_marker.*:\n//;
248 $str =~ s/(\.text\n\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/;
249 $str =~ s/(\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/;
252 # make sure the *.hc filename gets saved; not just ghc*.c (temp name)
253 $str =~ s/^\.stabs "(ghc\d+\.c)"/.stabs "$ifile_root.hc"/g; # HACK HACK
255 # remove/record any literal constants defined here
256 while ( $str =~ /(\t\.align .\n\.?(L?LC\d+):\n(\t\.asci[iz].*\n)+)/ ) {
260 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
261 if $LocalConstant{$label};
263 $LocalConstant{$label} = $body;
265 $str =~ s/\t\.align .\n\.?LL?C\d+:\n(\t\.asci[iz].*\n)+//;
268 # inject definitions for any local constants now used herein
269 foreach $k (keys %LocalConstant) {
270 if ( $str =~ /\b$k\b/ ) {
271 $str = $LocalConstant{$k} . $str;
275 print STDERR "### STRIPPED BLOCK (sparc):\n$str" if $Dump_asm_splitting_info;
280 sub process_asm_block_m68k {
285 $str =~ s/(\.text\n\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/;
286 $str =~ s/(\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/;
288 # it seems prudent to stick on one of these:
289 $str = "\.text\n\t.even\n" . $str;
291 # remove/record any literal constants defined here
292 while ( $str =~ /((LC\d+):\n\t\.ascii.*\n)/ ) {
296 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
297 if $LocalConstant{$label};
299 $LocalConstant{$label} = $body;
301 $str =~ s/LC\d+:\n\t\.ascii.*\n//;
304 # inject definitions for any local constants now used herein
305 foreach $k (keys %LocalConstant) {
306 if ( $str =~ /\b$k\b/ ) {
307 $str = $LocalConstant{$k} . $str;
311 print STDERR "### STRIPPED BLOCK (m68k):\n$str" if $Dump_asm_splitting_info;
316 sub process_asm_block_alpha {
321 $str =~ s/_?__stg_split_marker.*:\n//;
323 $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/;
326 # remove/record any literal constants defined here
327 while ( $str =~ /(\.rdata\n\t\.align \d\n)?(\$(C\d+):\n\t\..*\n)/ ) {
331 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
332 if $LocalConstant{$label};
334 $LocalConstant{$label} = ".rdata\n\t.align 3\n" . $body . "\t.text\n";
336 $str =~ s/(\.rdata\n\t\.align \d\n)?\$C\d+:\n\t\..*\n//;
339 # inject definitions for any local constants now used herein
340 foreach $k (keys %LocalConstant) {
341 if ( $str =~ /\$\b$k\b/ ) {
342 $str = $LocalConstant{$k} . $str;
346 # Slide the dummy direct return code into the vtbl .ent/.end block,
347 # to keep the label fixed if it's the last thing in a module, and
348 # to avoid having any anonymous text that the linker will complain about
349 $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/g;
351 print STDERR "### STRIPPED BLOCK (alpha):\n$str" if $Dump_asm_splitting_info;
356 sub process_asm_block_iX86 {
361 $str =~ s/(\.text\n\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/;
362 $str =~ s/(\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/;
364 # it seems prudent to stick on one of these:
365 $str = "\.text\n\t.align 4\n" . $str;
367 # remove/record any literal constants defined here
368 # [perl made uglier to work around the perl 5.7/5.8 bug documented at
369 # http://bugs6.perl.org/rt2/Ticket/Display.html?id=1760 and illustrated
370 # by the seg fault of perl -e '("x\n" x 5000) =~ /(.*\n)+/'
371 # -- ccshan 2002-09-05]
372 while ( ($str =~ /((?:^|\.)(LC\d+):\n(\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/ )) {
375 local($prefix, $suffix, $*) = ($`, $', 0);
377 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
378 if $LocalConstant{$label};
380 while ( $suffix =~ /^((\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/ ) {
384 $LocalConstant{$label} = $body;
385 $str = $prefix . $suffix;
388 # inject definitions for any local constants now used herein
389 foreach $k (keys %LocalConstant) {
390 if ( $str =~ /\b$k\b/ ) {
391 $str = $LocalConstant{$k} . $str;
395 print STDERR "### STRIPPED BLOCK (iX86):\n$str" if $Dump_asm_splitting_info;
402 sub process_asm_block_x86_64 {
405 # remove/record any literal constants defined here
406 # [perl made uglier to work around the perl 5.7/5.8 bug documented at
407 # http://bugs6.perl.org/rt2/Ticket/Display.html?id=1760 and illustrated
408 # by the seg fault of perl -e '("x\n" x 5000) =~ /(.*\n)+/'
409 # -- ccshan 2002-09-05]
410 while ( ($str =~ /((?:^|\.)(LC\d+):\n(\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/ )) {
413 local($prefix, $suffix, $*) = ($`, $', 0);
415 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
416 if $LocalConstant{$label};
418 while ( $suffix =~ /^((\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/ ) {
422 $LocalConstant{$label} = $body;
423 $str = $prefix . $suffix;
426 # inject definitions for any local constants now used herein
427 foreach $k (keys %LocalConstant) {
428 if ( $str =~ /\b$k\b/ ) {
429 $str = $LocalConstant{$k} . $str;
433 print STDERR "### STRIPPED BLOCK (x86_64):\n$str" if $Dump_asm_splitting_info;
440 sub process_asm_block_hppa {
444 $str =~ s/___stg_split_marker.*\n//;
446 # remove/record any imports defined here
447 while ( $str =~ /^(\s+\.IMPORT\s.*\n)/ ) {
450 $str =~ s/^\s+\.IMPORT.*\n//;
453 # remove/record any literal constants defined here
454 while ( $str =~ /^(\s+\.align.*\n(L\$C\d+)\n(\s.*\n)+); end literal\n/ ) {
459 $label =~ s/\$/\\\$/g;
461 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
462 if $LocalConstant{$label};
464 $LocalConstant{$label} = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$LIT\$\n\n" . $body;
466 $str = $prefix . $suffix;
469 # inject definitions for any local constants now used herein
470 foreach $k (keys %LocalConstant) {
471 if ( $str =~ /\b$k\b/ ) {
472 $str = $LocalConstant{$k} . $str;
476 # inject required imports for local exports in other chunks
477 foreach $k (keys %LocalExport) {
478 if ( $str =~ /\b$k\b/ && ! /EXPORT\s+$k\b/ ) {
479 $str = $LocalExport{$k} . $str;
483 # inject collected imports
485 $str = $Imports . $str;
487 print STDERR "### STRIPPED BLOCK (hppa):\n$str" if $Dump_asm_splitting_info;
494 sub process_asm_block_mips {
499 $str =~ s/_?__stg_split_marker.*:\n//;
501 $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/;
504 # remove/record any literal constants defined here
505 while ( $str =~ /(\t\.rdata\n\t\.align \d\n)?^(\$(LC\d+):\n(\t\.byte\t.*\n)+)/ ) {
509 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
510 if $LocalConstant{$label};
512 $LocalConstant{$label} = "\t.rdata\n\t.align 2\n" . $body . "\t.text\n";
514 $str =~ s/(\t\.rdata\n\t\.align \d\n)?\$LC\d+:\n(\t\.byte\t.*\n)+//;
517 # inject definitions for any local constants now used herein
518 foreach $k (keys %LocalConstant) {
519 if ( $str =~ /\$\b$k\b/ ) {
520 $str = $LocalConstant{$k} . $str;
524 # Slide the dummy direct return code into the vtbl .ent/.end block,
525 # to keep the label fixed if it's the last thing in a module, and
526 # to avoid having any anonymous text that the linker will complain about
527 $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/g;
529 $str .= $UNDEFINED_FUNS; # pin on gratuitiously-large amount of info
531 print STDERR "### STRIPPED BLOCK (mips):\n$str" if $Dump_asm_splitting_info;
538 # The logic for both Darwin/PowerPC and Darwin/x86 ends up being the same.
540 sub process_asm_block_darwin {
542 local($dyld_stuff) = '';
545 $str =~ s/___stg_split_marker.*\n//;
547 $str =~ s/L_.*\$.*:\n(.|\n)*//;
549 # remove/record any literal constants defined here
550 while ( $str =~ s/^(\s+.const.*\n\s+\.align.*\n(LC\d+):\n(\s\.(byte|short|long|fill|space|ascii).*\n)+)// ) {
554 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
555 if $LocalConstant{$label};
557 $LocalConstant{$label} = $body;
560 # inject definitions for any local constants now used herein
561 foreach $k (keys %LocalConstant) {
562 if ( $str =~ /\b$k(\b|\[)/ ) {
563 $str = $LocalConstant{$k} . $str;
567 foreach $k (keys %DyldChunks) {
568 if ( $str =~ /\bL$k\$/ ) {
569 if ( $str =~ /^$k:$/ ) {
570 $dyld_stuff .= $DyldChunksDefined{$k};
572 $dyld_stuff .= $DyldChunks{$k};
577 $str .= "\n" . $dyld_stuff;
579 print STDERR "### STRIPPED BLOCK (darwin):\n$str" if $Dump_asm_splitting_info;
586 sub process_asm_block_powerpc_linux {
590 $str =~ s/__stg_split_marker.*\n//;
592 # remove/record any literal constants defined here
593 while ( $str =~ s/^(\s+.section\s+\.rodata\n\s+\.align.*\n(\.LC\d+):\n(\s\.(byte|short|long|quad|2byte|4byte|8byte|fill|space|ascii|string).*\n)+)// ) {
597 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
598 if $LocalConstant{$label};
600 $LocalConstant{$label} = $body;
603 # inject definitions for any local constants now used herein
604 foreach $k (keys %LocalConstant) {
605 if ( $str =~ /[\s,]$k\b/ ) {
606 $str = $LocalConstant{$k} . $str;
610 print STDERR "### STRIPPED BLOCK (powerpc linux):\n$str" if $Dump_asm_splitting_info;
617 sub tidy_up_and_die {
618 local($return_val, $msg) = @_;
620 exit (($return_val == 0) ? 0 : 1);