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
38 %LocalConstant = (); # we have to subvert C compiler's commoning-up of constants...
40 $s_stuff = &ReadTMPIUpToAMarker( '', $octr );
41 # that first stuff is a prologue for all .s outputs
42 $prologue_stuff = &process_asm_block ( $s_stuff );
43 # $_ already has some of the next stuff in it...
45 # &tidy_up_and_die(1,"$Pgm: no split markers in .s file!\n")
46 # if $prologue_stuff eq $s_stuff;
48 # lie about where this stuff came from
49 # Note the \Q: this ignores regex meta-chars in $Tmp_prefix.
50 $prologue_stuff =~ s/\Q"$Tmp_prefix.c"/"$ifile_root.hc"/gm;
52 while ( $_ ne '' ) { # not EOF
55 # grab and de-mangle a section of the .s file...
56 $s_stuff = &ReadTMPIUpToAMarker ( $_, $octr );
57 $pieces[$octr] = &process_asm_block ( $s_stuff );
60 # Make sure that we still have some output when the input file is empty
66 $NoOfSplitFiles = $octr;
68 if ($pieces[$NoOfSplitFiles] =~ /(\n[ \t]*\.section[ \t]+\.note\.GNU-stack,[^\n]*\n)/m) {
70 for $octr (1..($NoOfSplitFiles - 1)) {
71 $pieces[$octr] .= $note_gnu_stack;
75 for $octr (1..$NoOfSplitFiles) {
76 # output to a file of its own
77 # open a new output file...
78 $ofname = "${Tmp_prefix}__${octr}.s";
79 open(OUTF, "> $ofname") || die "$Pgm: can't open output file: $ofname\n";
81 print OUTF $prologue_stuff;
82 print OUTF $pieces[$octr];
85 || &tidy_up_and_die(1,"$Pgm:Failed writing ${Tmp_prefix}__${octr}.s\n");
88 close(TMPI) || &tidy_up_and_die(1,"Failed reading $asm_file\n");
91 sub collectExports_hppa { # Note: HP-PA only
93 %LocalExport = (); # NB: global table
96 if (/^\s+\.EXPORT\s+([^,]+),.*\n/m) {
98 local($body) = "\t.IMPORT $label";
104 $label =~ s/\$/\\\$/gm;
105 $LocalExport{$label} = $body;
112 sub collectExports_mips { # Note: MIPS only
113 # (not really sure this is necessary [WDP 95/05])
115 $UNDEFINED_FUNS = ''; # NB: global table
118 $UNDEFINED_FUNS .= $_ if /^\t\.globl\s+\S+ \.\S+\n/m;
125 sub collectDyldStuff_darwin {
126 local($chunk_label,$label,$cur_section,$section,$chunk,$alignment,$cur_alignment);
128 %DyldChunks = (); # NB: global table
129 %DyldChunksDefined = (); # NB: global table
140 if ( $_ eq '' || (/^L(_.+)\$.+:/m && !(/^L(.*)\$stub_binder:/m))) {
141 if ( $label ne '' ) {
142 $DyldChunksDefined{$label} .= $section . $alignment . $chunk_label . $ chunk;
143 if( $section =~ s/\.data/\.non_lazy_symbol_pointer/m ) {
144 $chunk = "\t.indirect_symbol $label\n\t.long 0\n";
146 $DyldChunks{$label} .= $section . $alignment . $chunk_label . $chunk;
147 print STDERR "### dyld chunk: $label\n$section$alignment$chunk\n###\n" if $Dump_asm_splitting_info;
154 $section = $cur_section;
155 $alignment = $cur_alignment;
156 print STDERR "label: $label\n" if $Dump_asm_splitting_info;
157 } 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)/m ) {
159 printf STDERR "section: $cur_section\n" if $Dump_asm_splitting_info;
161 } elsif ( /^\s*\.section\s+__TEXT,__symbol_stub1,symbol_stubs,pure_instructions,\d+/m ) {
163 printf STDERR "section: $cur_section\n" if $Dump_asm_splitting_info;
164 # always make sure we align things
165 $cur_alignment = '\t.align 2'
166 } elsif ( /^\s*\.align.*/m ) {
168 printf STDERR "alignment: $cur_alignment\n" if $Dump_asm_splitting_info;
177 sub ReadTMPIUpToAMarker {
178 local($str, $count) = @_; # already read bits
181 for ( $_ = <TMPI>; $_ ne '' && ! /_?__stg_split_marker/m; $_ = <TMPI> ) {
184 # if not EOF, then creep forward until next "real" line
185 # (throwing everything away).
186 # that first "real" line will stay in $_.
188 # This loop is intended to pick up the body of the split_marker function
189 # Note that the assembler mangler will already have eliminated this code
190 # if it's been invoked (which it probably has).
192 while ($_ ne '' && (/_?__stg_split_marker/m
202 # || /\t\.end/ NOT! Let the split_marker regexp catch it
203 # || /\t\.ent/ NOT! Let the split_marker regexp catch it
204 || /^\s+(save|retl?|restore|nop)/m)) {
208 print STDERR "### BLOCK:$count:\n$str" if $Dump_asm_splitting_info;
211 $str =~ tr/\r//d if $TargetPlatform =~ /-mingw32$/m; # in case Perl doesn't convert line endings
216 We must (a)~strip the marker off the block, (b)~record any literal C
217 constants that are defined here, and (c)~inject copies of any C constants
218 that are used-but-not-defined here.
221 sub process_asm_block {
224 return(&process_asm_block_darwin($str))
225 if $TargetPlatform =~ /-apple-darwin/m;
226 return(&process_asm_block_m68k($str)) if $TargetPlatform =~ /^m68k-/m;
227 return(&process_asm_block_sparc($str)) if $TargetPlatform =~ /^sparc-/m;
228 return(&process_asm_block_iX86($str)) if $TargetPlatform =~ /^i[34]86-/m;
229 return(&process_asm_block_x86_64($str)) if $TargetPlatform =~ /^x86_64-/m;
230 return(&process_asm_block_alpha($str)) if $TargetPlatform =~ /^alpha-/m;
231 return(&process_asm_block_hppa($str)) if $TargetPlatform =~ /^hppa/m;
232 return(&process_asm_block_mips($str)) if $TargetPlatform =~ /^mips-/m;
233 return(&process_asm_block_powerpc_linux($str))
234 if $TargetPlatform =~ /^powerpc-[^-]+-linux/m;
237 &tidy_up_and_die(1,"$Pgm: no process_asm_block for $TargetPlatform\n");
240 sub process_asm_block_sparc {
245 $str =~ s/_?__stg_split_marker.*:\n//m;
247 $str =~ s/(\.text\n\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/m;
248 $str =~ s/(\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/m;
251 # make sure the *.hc filename gets saved; not just ghc*.c (temp name)
252 $str =~ s/^\.stabs "(ghc\d+\.c)"/.stabs "$ifile_root.hc"/gm; # HACK HACK
254 # remove/record any literal constants defined here
255 while ( $str =~ /(\t\.align .\n\.?(L?LC\d+):\n(\t\.asci[iz].*\n)+)/m ) {
259 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
260 if $LocalConstant{$label};
262 $LocalConstant{$label} = $body;
264 $str =~ s/\t\.align .\n\.?LL?C\d+:\n(\t\.asci[iz].*\n)+//m;
267 # inject definitions for any local constants now used herein
268 foreach $k (keys %LocalConstant) {
269 if ( $str =~ /\b$k\b/m ) {
270 $str = $LocalConstant{$k} . $str;
274 print STDERR "### STRIPPED BLOCK (sparc):\n$str" if $Dump_asm_splitting_info;
279 sub process_asm_block_m68k {
284 $str =~ s/(\.text\n\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/m;
285 $str =~ s/(\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/m;
287 # it seems prudent to stick on one of these:
288 $str = "\.text\n\t.even\n" . $str;
290 # remove/record any literal constants defined here
291 while ( $str =~ /((LC\d+):\n\t\.ascii.*\n)/m ) {
295 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
296 if $LocalConstant{$label};
298 $LocalConstant{$label} = $body;
300 $str =~ s/LC\d+:\n\t\.ascii.*\n//m;
303 # inject definitions for any local constants now used herein
304 foreach $k (keys %LocalConstant) {
305 if ( $str =~ /\b$k\b/m ) {
306 $str = $LocalConstant{$k} . $str;
310 print STDERR "### STRIPPED BLOCK (m68k):\n$str" if $Dump_asm_splitting_info;
315 sub process_asm_block_alpha {
320 $str =~ s/_?__stg_split_marker.*:\n//m;
322 $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/m;
325 # remove/record any literal constants defined here
326 while ( $str =~ /(\.rdata\n\t\.align \d\n)?(\$(C\d+):\n\t\..*\n)/m ) {
330 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
331 if $LocalConstant{$label};
333 $LocalConstant{$label} = ".rdata\n\t.align 3\n" . $body . "\t.text\n";
335 $str =~ s/(\.rdata\n\t\.align \d\n)?\$C\d+:\n\t\..*\n//m;
338 # inject definitions for any local constants now used herein
339 foreach $k (keys %LocalConstant) {
340 if ( $str =~ /\$\b$k\b/m ) {
341 $str = $LocalConstant{$k} . $str;
345 # Slide the dummy direct return code into the vtbl .ent/.end block,
346 # to keep the label fixed if it's the last thing in a module, and
347 # to avoid having any anonymous text that the linker will complain about
348 $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/gm;
350 print STDERR "### STRIPPED BLOCK (alpha):\n$str" if $Dump_asm_splitting_info;
355 sub process_asm_block_iX86 {
360 $str =~ s/(\.text\n\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/m;
361 $str =~ s/(\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/m;
363 # it seems prudent to stick on one of these:
364 $str = "\.text\n\t.align 4\n" . $str;
366 # remove/record any literal constants defined here
367 # [perl made uglier to work around the perl 5.7/5.8 bug documented at
368 # http://bugs6.perl.org/rt2/Ticket/Display.html?id=1760 and illustrated
369 # by the seg fault of perl -e '("x\n" x 5000) =~ /(.*\n)+/'
370 # -- ccshan 2002-09-05]
371 while ( ($str =~ /((?:^|\.)(LC\d+):\n(\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/m )) {
374 local($prefix, $suffix) = ($`, $');
376 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
377 if $LocalConstant{$label};
379 while ( $suffix =~ /^((\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/ ) {
383 $LocalConstant{$label} = $body;
384 $str = $prefix . $suffix;
387 # inject definitions for any local constants now used herein
388 foreach $k (keys %LocalConstant) {
389 if ( $str =~ /\b$k\b/m ) {
390 $str = $LocalConstant{$k} . $str;
394 print STDERR "### STRIPPED BLOCK (iX86):\n$str" if $Dump_asm_splitting_info;
401 sub process_asm_block_x86_64 {
404 # remove/record any literal constants defined here
405 # [perl made uglier to work around the perl 5.7/5.8 bug documented at
406 # http://bugs6.perl.org/rt2/Ticket/Display.html?id=1760 and illustrated
407 # by the seg fault of perl -e '("x\n" x 5000) =~ /(.*\n)+/'
408 # -- ccshan 2002-09-05]
409 while ( ($str =~ /((?:^|\.)(LC\d+):\n(\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/m )) {
412 local($prefix, $suffix) = ($`, $');
414 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
415 if $LocalConstant{$label};
417 while ( $suffix =~ /^((\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/ ) {
421 $LocalConstant{$label} = $body;
422 $str = $prefix . $suffix;
425 # inject definitions for any local constants now used herein
426 foreach $k (keys %LocalConstant) {
427 if ( $str =~ /\b$k\b/m ) {
428 $str = $LocalConstant{$k} . $str;
432 print STDERR "### STRIPPED BLOCK (x86_64):\n$str" if $Dump_asm_splitting_info;
439 sub process_asm_block_hppa {
443 $str =~ s/___stg_split_marker.*\n//m;
445 # remove/record any imports defined here
446 while ( $str =~ /^(\s+\.IMPORT\s.*\n)/m ) {
449 $str =~ s/^\s+\.IMPORT.*\n//m;
452 # remove/record any literal constants defined here
453 while ( $str =~ /^(\s+\.align.*\n(L\$C\d+)\n(\s.*\n)+); end literal\n/m ) {
458 $label =~ s/\$/\\\$/gm;
460 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
461 if $LocalConstant{$label};
463 $LocalConstant{$label} = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$LIT\$\n\n" . $body;
465 $str = $prefix . $suffix;
468 # inject definitions for any local constants now used herein
469 foreach $k (keys %LocalConstant) {
470 if ( $str =~ /\b$k\b/m ) {
471 $str = $LocalConstant{$k} . $str;
475 # inject required imports for local exports in other chunks
476 foreach $k (keys %LocalExport) {
477 if ( $str =~ /\b$k\b/m && ! /EXPORT\s+$k\b/m ) {
478 $str = $LocalExport{$k} . $str;
482 # inject collected imports
484 $str = $Imports . $str;
486 print STDERR "### STRIPPED BLOCK (hppa):\n$str" if $Dump_asm_splitting_info;
493 sub process_asm_block_mips {
498 $str =~ s/_?__stg_split_marker.*:\n//m;
500 $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/m;
503 # remove/record any literal constants defined here
504 while ( $str =~ /(\t\.rdata\n\t\.align \d\n)?^(\$(LC\d+):\n(\t\.byte\t.*\n)+)/m ) {
508 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
509 if $LocalConstant{$label};
511 $LocalConstant{$label} = "\t.rdata\n\t.align 2\n" . $body . "\t.text\n";
513 $str =~ s/(\t\.rdata\n\t\.align \d\n)?\$LC\d+:\n(\t\.byte\t.*\n)+//m;
516 # inject definitions for any local constants now used herein
517 foreach $k (keys %LocalConstant) {
518 if ( $str =~ /\$\b$k\b/m ) {
519 $str = $LocalConstant{$k} . $str;
523 # Slide the dummy direct return code into the vtbl .ent/.end block,
524 # to keep the label fixed if it's the last thing in a module, and
525 # to avoid having any anonymous text that the linker will complain about
526 $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/gm;
528 $str .= $UNDEFINED_FUNS; # pin on gratuitiously-large amount of info
530 print STDERR "### STRIPPED BLOCK (mips):\n$str" if $Dump_asm_splitting_info;
537 # The logic for both Darwin/PowerPC and Darwin/x86 ends up being the same.
539 sub process_asm_block_darwin {
541 local($dyld_stuff) = '';
544 $str =~ s/___stg_split_marker.*\n//m;
546 $str =~ s/L_.*\$.*:\n(.|\n)*//m;
548 # remove/record any literal constants defined here
549 while ( $str =~ s/^(\s+.const.*\n\s+\.align.*\n(LC\d+):\n(\s\.(byte|short|long|fill|space|ascii).*\n)+)//m ) {
553 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
554 if $LocalConstant{$label};
556 $LocalConstant{$label} = $body;
559 # inject definitions for any local constants now used herein
560 foreach $k (keys %LocalConstant) {
561 if ( $str =~ /\b$k(\b|\[)/m ) {
562 $str = $LocalConstant{$k} . $str;
566 foreach $k (keys %DyldChunks) {
567 if ( $str =~ /\bL$k\$/m ) {
568 if ( $str =~ /^$k:$/m ) {
569 $dyld_stuff .= $DyldChunksDefined{$k};
571 $dyld_stuff .= $DyldChunks{$k};
576 $str .= "\n" . $dyld_stuff;
578 print STDERR "### STRIPPED BLOCK (darwin):\n$str" if $Dump_asm_splitting_info;
585 sub process_asm_block_powerpc_linux {
589 $str =~ s/__stg_split_marker.*\n//m;
591 # remove/record any literal constants defined here
592 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)+)//m ) {
596 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
597 if $LocalConstant{$label};
599 $LocalConstant{$label} = $body;
602 # inject definitions for any local constants now used herein
603 foreach $k (keys %LocalConstant) {
604 if ( $str =~ /[\s,]$k\b/m ) {
605 $str = $LocalConstant{$k} . $str;
609 print STDERR "### STRIPPED BLOCK (powerpc linux):\n$str" if $Dump_asm_splitting_info;
616 sub tidy_up_and_die {
617 local($return_val, $msg) = @_;
619 exit (($return_val == 0) ? 0 : 1);