initfini.awk 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #! /usr/bin/awk -f
  2. # Contributed by Christian MICHON <christian_michon@yahoo.fr> to
  3. # eliminate the compile time dependancy on perl introduced by
  4. # Erik's older initfini.pl
  5. # vim:ai:sw=2:
  6. BEGIN \
  7. { alignval="";
  8. endp=0;
  9. end=0;
  10. system("touch crt[in].S");
  11. system("/bin/rm -f crt[in].S");
  12. omitcrti=0;
  13. omitcrtn=0;
  14. glb_idx = 0;
  15. while(getline < "initfini.S")
  16. { if(/\.endp/) {endp=1}
  17. if(/\.end/) {end=1}
  18. if(/\.align/) {alignval=$2}
  19. # here comes some special stuff for the SuperH targets
  20. # We search for all labels, which uses the _GLOBAL_OFFSET_TABLE_
  21. # or a call_gmon_start function reference, and store
  22. # them in the glb_label array.
  23. if(/_init_EPILOG_BEGINS/) {glb_idx=1;glb_idx_arr[glb_idx]=0}
  24. if(/_fini_EPILOG_BEGINS/) {glb_idx=2;glb_idx_arr[glb_idx]=0}
  25. if(/EPILOG_ENDS/) {glb_idx=0}
  26. if(/_GLOBAL_OFFSET_TABLE_/||/call_gmon_start/) {
  27. glb_label[glb_idx,glb_idx_arr[glb_idx]] = last;
  28. glb_idx_arr[glb_idx] += 1;
  29. glb_label[glb_idx,glb_idx_arr[glb_idx]] = $0;
  30. glb_idx_arr[glb_idx] += 1;
  31. }
  32. last = $1;
  33. }
  34. close("initfini.S");
  35. }
  36. # special rules for the SuperH targets (They do nothing on other targets)
  37. /SH_GLB_BEGINS/ && glb_idx_arr[1]==0 && glb_idx_arr[2]==0 {omitcrti +=1}
  38. /_init_SH_GLB/ {glb_idx=1}
  39. /_fini_SH_GLB/ {glb_idx=2}
  40. /SH_GLB_ENDS/ {omitcrti -=1}
  41. /SH_GLB/ \
  42. {
  43. if (glb_idx>0)
  44. {
  45. for (i=0;i<glb_idx_arr[glb_idx];i+=1) {
  46. print glb_label[glb_idx,i] >> "crti.S";
  47. }
  48. glb_idx = 0;
  49. }
  50. next;
  51. }
  52. # special rules for H8/300 (sorry quick hack)
  53. /.h8300h/ {end=0}
  54. # rules for all targets
  55. /HEADER_ENDS/{omitcrti=1;omitcrtn=1;getline}
  56. /PROLOG_BEGINS/{omitcrti=0;omitcrtn=0;getline}
  57. /i_am_not_a_leaf/{getline}
  58. /_init:/||/_fini:/{omitcrtn=1}
  59. /PROLOG_PAUSES/{omitcrti=1;getline}
  60. /PROLOG_UNPAUSES/{omitcrti=0;getline}
  61. /PROLOG_ENDS/{omitcrti=1;getline}
  62. /EPILOG_BEGINS/{omitcrtn=0;getline}
  63. /EPILOG_ENDS/{omitcrtn=1;getline}
  64. /TRAILER_BEGINS/{omitcrti=0;omitcrtn=0;getline}
  65. /GMON_STUFF_BEGINS/{omitcrtn=1;getline}
  66. /GMON_STUFF_PAUSES/{omitcrtn=0;getline}
  67. /GMON_STUFF_UNPAUSES/{omitcrtn=1;getline}
  68. /GMON_STUFF_ENDS/{omitcrtn=0;getline}
  69. /_GLOBAL_OFFSET_TABLE_/||/gmon_start/ \
  70. {
  71. if(omitcrti==0) {print >> "crti.S";}
  72. next; # no gmon_start or GLOBAL_OFFSET_TABLE references in crtn.S
  73. }
  74. /END_INIT/ \
  75. { if(endp)
  76. { gsub("END_INIT",".endp _init",$0)
  77. }
  78. else
  79. { if(end)
  80. { gsub("END_INIT",".end _init",$0)
  81. }
  82. else
  83. { gsub("END_INIT","",$0)
  84. }
  85. }
  86. }
  87. /END_FINI/ \
  88. { if(endp)
  89. { gsub("END_FINI",".endp _fini",$0)
  90. }
  91. else
  92. { if(end)
  93. { gsub("END_FINI",".end _fini",$0)
  94. }
  95. else
  96. { gsub("END_FINI","",$0)
  97. }
  98. }
  99. }
  100. /ALIGN/ \
  101. { if(alignval!="")
  102. { gsub("ALIGN",sprintf(".align %s",alignval),$0)
  103. }
  104. else
  105. { gsub("ALIGN","",$0)
  106. }
  107. }
  108. omitcrti==0 {print >> "crti.S"}
  109. omitcrtn==0 {print >> "crtn.S"}
  110. END \
  111. { close("crti.S");
  112. close("crtn.S");
  113. }