initfini.pl 2.5 KB

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