initfini.pl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/perl
  2. use strict;
  3. use Getopt::Long;
  4. my($initfini) = "initfini.s";
  5. my($crti) = "crti.S";
  6. my($crtn) = "crtn.S";
  7. my($alignval) = "";
  8. my($endp) = 0;
  9. my($end) = 0;
  10. my($omitcrti) = 0;
  11. my($omitcrtn) = 0;
  12. my($line);
  13. # Get commandline parameters
  14. Getopt::Long::Configure("no_ignore_case", "bundling");
  15. &GetOptions( "initfini=s" => \$initfini,
  16. "crti=s" => \$crti,
  17. "crtn=s" => \$crtn,
  18. );
  19. chomp($initfini);
  20. chomp($crti);
  21. chomp($crtn);
  22. if ($initfini) {
  23. open(INITFINI,"<$initfini") or
  24. die "(fatal) Can't open $initfini$!";
  25. } else {
  26. die "(fatal) Please give me an --initfini argument$!";
  27. }
  28. while(<INITFINI>) {
  29. if (/\.endp/) {
  30. $endp=1;
  31. next;
  32. }
  33. if (/\.end/) {
  34. $end=1;
  35. next;
  36. }
  37. if (/\.align(.*)/) {
  38. $alignval=$1;
  39. next;
  40. }
  41. }
  42. close(INITFINI);
  43. if ($initfini) {
  44. open(INITFINI,"<$initfini") or
  45. die "(fatal) Can't open $initfini$!";
  46. } else {
  47. die "(fatal) Please give me an --initfini argument$!";
  48. }
  49. if ($crti) {
  50. open(CRTI,">$crti") or
  51. die "(fatal) Can't open $crti$!";
  52. } else {
  53. die "(fatal) Please give me a --asm argument$!";
  54. }
  55. if ($crtn) {
  56. open(CRTN,">$crtn") or
  57. die "(fatal) Can't open $crtn$!";
  58. } else {
  59. die "(fatal) Please give me a --asm argument$!";
  60. }
  61. while(<INITFINI>) {
  62. if (/HEADER_ENDS/) {
  63. $omitcrti = 1;
  64. $omitcrtn = 1;
  65. next;
  66. }
  67. if (/PROLOG_BEGINS/) {
  68. $omitcrti = 0;
  69. $omitcrtn = 0;
  70. next;
  71. }
  72. if (/^_init:/ || /^_fini:/) {
  73. $omitcrtn = 1;
  74. }
  75. if (/PROLOG_PAUSES/) {
  76. $omitcrti = 1;
  77. next;
  78. }
  79. if (/PROLOG_UNPAUSES/) {
  80. $omitcrti = 0;
  81. next;
  82. }
  83. if (/PROLOG_ENDS/) {
  84. $omitcrti = 1;
  85. next;
  86. }
  87. if (/EPILOG_BEGINS/) {
  88. $omitcrtn = 0;
  89. next;
  90. }
  91. if (/EPILOG_ENDS/) {
  92. $omitcrtn = 1;
  93. next;
  94. }
  95. if (/TRAILER_BEGINS/) {
  96. $omitcrti = 0;
  97. $omitcrtn = 0;
  98. next;
  99. }
  100. if (/END_INIT/) {
  101. if ($endp) {
  102. s/END_INIT/.endp _init/;
  103. } else {
  104. if($end) {
  105. s/END_INIT/.end _init/;
  106. } else {
  107. s/END_INIT//;
  108. }
  109. }
  110. }
  111. if (/END_FINI/) {
  112. if ($endp) {
  113. s/END_FINI/.endp _fini/;
  114. } else {
  115. if($end) {
  116. s/END_FINI/.end _fini/;
  117. } else {
  118. s/END_FINI//;
  119. }
  120. }
  121. }
  122. if (/ALIGN/) {
  123. if($alignval) {
  124. s/ALIGN/.align $alignval/;
  125. } else {
  126. s/ALIGN//;
  127. }
  128. }
  129. if (!$omitcrti) {
  130. print CRTI;
  131. }
  132. if (!$omitcrtn) {
  133. print CRTN;
  134. }
  135. }
  136. close(INITFINI);
  137. close(CRTI);
  138. close(CRTN);