initfini.pl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 (/i_am_not_a_leaf/) {
  73. next;
  74. }
  75. if (/^_init:/ || /^_fini:/) {
  76. $omitcrtn = 1;
  77. }
  78. if (/PROLOG_PAUSES/) {
  79. $omitcrti = 1;
  80. next;
  81. }
  82. if (/PROLOG_UNPAUSES/) {
  83. $omitcrti = 0;
  84. next;
  85. }
  86. if (/PROLOG_ENDS/) {
  87. $omitcrti = 1;
  88. next;
  89. }
  90. if (/EPILOG_BEGINS/) {
  91. $omitcrtn = 0;
  92. next;
  93. }
  94. if (/EPILOG_ENDS/) {
  95. $omitcrtn = 1;
  96. next;
  97. }
  98. if (/TRAILER_BEGINS/) {
  99. $omitcrti = 0;
  100. $omitcrtn = 0;
  101. next;
  102. }
  103. if (/END_INIT/) {
  104. if ($endp) {
  105. s/END_INIT/.endp _init/;
  106. } else {
  107. if($end) {
  108. s/END_INIT/.end _init/;
  109. } else {
  110. s/END_INIT//;
  111. }
  112. }
  113. }
  114. if (/END_FINI/) {
  115. if ($endp) {
  116. s/END_FINI/.endp _fini/;
  117. } else {
  118. if($end) {
  119. s/END_FINI/.end _fini/;
  120. } else {
  121. s/END_FINI//;
  122. }
  123. }
  124. }
  125. if (/ALIGN/) {
  126. if($alignval) {
  127. s/ALIGN/.align $alignval/;
  128. } else {
  129. s/ALIGN//;
  130. }
  131. }
  132. if (!$omitcrti) {
  133. print CRTI;
  134. }
  135. if (!$omitcrtn) {
  136. print CRTN;
  137. }
  138. }
  139. close(INITFINI);
  140. close(CRTI);
  141. close(CRTN);