summaryrefslogtreecommitdiff
path: root/extra/scripts/initfini.pl
blob: 41add5d8832fe3f0f71dd66459a0dfcbb1dd0d5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/perl

use strict;
use Getopt::Long;

my($initfini)	    = "initfini.s";
my($crti)	    = "crti.S";
my($crtn)	    = "crtn.S";
my($alignval)	    = "";
my($endp)	    = 0;
my($end)	    = 0;
my($omitcrti)	    = 0;
my($omitcrtn)	    = 0;
my($discard)	    = 0;
my($line);

# Get commandline parameters
Getopt::Long::Configure("no_ignore_case", "bundling");
&GetOptions(	"initfini=s" => \$initfini,
		"crti=s" => \$crti,
		"crtn=s" => \$crtn,
		);

chomp($initfini);
chomp($crti);
chomp($crtn);


if ($initfini) {
	open(INITFINI,"<$initfini") or
		    die "(fatal) Can't open $initfini$!";
} else {
    die "(fatal) Please give me an --initfini argument$!";
}
while(<INITFINI>) {
    if (/\.endp/) {
	$endp=1;
	next;
    }
    if (/\.end/) {
	$end=1;
	next;
    }
    if (/\.align(.*)/) {
	$alignval=$1;
	next;
    }

}
close(INITFINI);





if ($initfini) {
	open(INITFINI,"<$initfini") or
		    die "(fatal) Can't open $initfini$!";
} else {
    die "(fatal) Please give me an --initfini argument$!";
}

if ($crti) {
	open(CRTI,">$crti") or
		    die "(fatal) Can't open $crti$!";
} else {
    die "(fatal) Please give me a --asm argument$!";
}
if ($crtn) {
	open(CRTN,">$crtn") or
		    die "(fatal) Can't open $crtn$!";
} else {
    die "(fatal) Please give me a --asm argument$!";
}

while(<INITFINI>) {
    if (/HEADER_ENDS/) {
	$omitcrti = 1;
	$omitcrtn = 1;
	next;
    }
    if (/PROLOG_BEGINS/) {
	$omitcrti = 0;
	$omitcrtn = 0;
	next;
    }
    if (/i_am_not_a_leaf/) {
	$discard = 1;
	next;
    }
    if (/^_init:/ || /^_fini:/) {
	$omitcrtn = 1;
    }
    if (/PROLOG_PAUSES/) {
	$omitcrti = 1;
	next;
    }
    if (/PROLOG_UNPAUSES/) {
	$omitcrti = 0;
	next;
    }
    if (/PROLOG_ENDS/) {
	$omitcrti = 1;
	next;
    }
    if (/EPILOG_BEGINS/) {
	$omitcrtn = 0;
	next;
    }
    if (/EPILOG_ENDS/) {
	$omitcrtn = 1;
	next;
    }
    if (/TRAILER_BEGINS/) {
	$omitcrti = 0;
	$omitcrtn = 0;
	next;
    }
    if (/END_INIT/) {
	if ($endp) {
	    s/END_INIT/.endp _init/;
	} else {
	    if($end) {
		s/END_INIT/.end _init/;
	    } else {
		s/END_INIT//;
	    }
	}
    }
    if (/END_FINI/) {
	if ($endp) {
	    s/END_FINI/.endp _fini/;
	} else {
	    if($end) {
		s/END_FINI/.end _fini/;
	    } else {
		s/END_FINI//;
	    }
	}
    }
    if (/ALIGN/) {
	if($alignval) {
	    s/ALIGN/.align $alignval/;
	} else {
	    s/ALIGN//;
	}
    }
    if (!$omitcrti && !$discard) {
	print CRTI;
    }
    if (!$omitcrtn && !$discard) {
	print CRTN;
    }
    $discard = 0;
}
close(INITFINI);
close(CRTI);
close(CRTN);