File: nescc-diff.in

package info (click to toggle)
nescc 1.3.5-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 11,604 kB
  • sloc: ansic: 72,471; sh: 4,474; yacc: 2,171; perl: 2,109; java: 1,699; makefile: 1,535; lisp: 693; xml: 182; lex: 95; cpp: 28
file content (326 lines) | stat: -rw-r--r-- 7,576 bytes parent folder | download | duplicates (2)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!@pathperl@
# -*- perl -*-

# This file is part of the nesC compiler.
#    Copyright (C) 2004 Intel Corporation
# 
# The attached "nesC" software is provided to you under the terms and
# conditions of the GNU General Public License Version 2 as published by the
# Free Software Foundation.
# 
# nesC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with nesC; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

# Configuration
$prefix = "@prefix@";
$exec_prefix = "@exec_prefix@";
$nescc = "$exec_prefix/bin/nescc";
$NCDIR = "@libdir@/ncc";

# Have fun with the arguments

for ($i = 0; $i <= $#ARGV; $i++) {
    $strip = 0;
    $_ = $ARGV[$i];
    if (/^-/) {
	if (/^-fnesc-diff=(.*),(.*)$/) {
	    $diff_from = $1;
	    $diff_to = $2;
	}
	elsif (/^-fnesc-diff=(.*)$/) {
	    $diff_to = $1;
	}
	elsif (/^-nescc=(.*)$/) {
	    $nescc = $1;
	    $strip = 1;
	}
	elsif (/^-fnesc-cfile/ || /^-o/ || /^-c/ || /^-Wl,/) {
	    print STDERR "$_ option not allowed\n";
	    exit 2;
	}
	elsif (/^-v$/) {
	    $verbose = 1;
	}
	push @ncc_args, $_ if !$strip;
    }
    push @new_args, $_ if !$strip;
}

if (!$diff_to) {
    print STDERR "no -fnesc-diff directive specified\n";
    exit 2;
}

if (system("mkdir -p $diff_to")) {
    print STDERR "Couldn't create destination directory $diff_to\n";
    exit 1;
}

unshift @new_args, 
  $nescc, "-c", "-o$diff_to/app.o", "-fnesc-cfile=$diff_to/app.c";

print STDERR join(' ', @new_args), "\n" if $verbose;

if (system @new_args) {
    print STDERR "ncc failed\n";
    exit 1;
}

#%sizes = &get_sizes("$diff_to/app.o");
%kinds = &get_kinds("$diff_to/symbols");

#foreach $sym (keys %kinds) {
#    print "$sym: $kinds{$sym}\n";
#}

open(SCRIPT, ">$diff_to/scr.x") || die;
$textsec = &print_section("FN");
$datasec = &print_section("DATA");
$bsssec = &print_section("BSS");
&gen_script("$NCDIR/first-avr5.x");
close SCRIPT || die;

@exe1_args = @ncc_args;
push @exe1_args, "$diff_to/app.o";
unshift @exe1_args, $nescc, "-Wl,--script=$diff_to/scr.x", "-Wl,-Map=$diff_to/map1", 
    "-o", "$diff_to/exe1";

print STDERR join(' ', @exe1_args), "\n" if $verbose;
system(@exe1_args) && die;

#foreach $sym (keys %sizes) {
#    print "$sym: $sizes{$sym} $output{$sym}\n";
#}

#foreach $sec (keys %sections) {
#    print "$sec: " . join(' ', @{$sections{$sec}}) . "\n";
#}

#foreach $sym (keys %symbols) {
#    print "$sym = $symbols{$sym}\n";
#}

if (!$diff_from) {
    &parse_map("$diff_to/map1");
}
else {
    &parse_map("$diff_from/map2");
    foreach $sym (keys %address) {
	$new = $sym;
	$new =~ s/^\Q$diff_from\E/\Q$diff_to\E/;
	$oldaddress{$new} = $address{$sym} - $secaddress{$output{$sym}};
    }

    &parse_map("$diff_to/map1");
}

$offset = &layout(".progmem", $symbols{"__ebase"} + 1024, 16);
$osize{".progmem"} = $offset;
$offset = &layout(".text", ($offset + 128 + 1) & ~1, 128);
$osize{".text"} = $offset + 4096;

$offset = &layout(".data", 0, 0);
$osize{".data"} = $offset + 16;

$offset = &layout(".bss", 0, 0);
$osize{".bss"} = $offset;

open(SCRIPT, ">$diff_to/fscr.x") || die;
$textsec = &print_final_section(".text");
$progmemsec = &print_final_section(".progmem");
$datasec = &print_final_section(".data");
$bsssec = &print_final_section(".bss");
&gen_script("$NCDIR/second-avr5.x");
close SCRIPT || die;

@exe2_args = @ncc_args;
push @exe2_args, "$diff_to/app.o";
unshift @exe2_args, $nescc, "-Wl,--script=$diff_to/fscr.x", "-Wl,-Map=$diff_to/map2", 
    "-o", "$diff_to/exe2";

print STDERR join(' ', @exe2_args), "\n" if $verbose;
system(@exe2_args) && die;


sub layout {
    my ($section, $base, $spacing) = @_;
    my (@names, $isec, @order);

    @names = @{$sections{$section}};

    #open(LAYOUT, "|$NCDIR/smalldiff >$diff_to/layout$section") || die;
    open(LAYOUT, ">$diff_to/ilayout$section") || die;
    $count = $#names + 1;
    print LAYOUT "(original $count $base $spacing)\n";
    foreach $isec (@names) {
	$oldaddress = $oldaddress{$isec};
	$oldaddress = "null" if !defined($oldaddress);
	print LAYOUT "(\"$isec\" $oldaddress $sizes{$isec})\n";
    }
    close LAYOUT || die;
    system("$NCDIR/smalldiff <$diff_to/ilayout$section >$diff_to/layout$section") && die;
    #die if $?;

    open LAYOUT, "$diff_to/layout$section";
    while (<LAYOUT>) {
	next if /^;/;
	die unless /\(\"(.*)\" ([0-9]+)\)/;
	$offset = $offset{$1} = $2;
	push @order, $1 if $1 ne "";
    }
    $order{$section} = \ @order;
    return $offset;
}

sub print_final_section {
    my ($section) = @_;
    my (@names, $isec, $inp);

    @names = @{$order{$section}};
    foreach $isec (@names) {
	$inp .= "    . = $offset{$isec};\n";
	$inp .= "    $isec\n";
    }
    $inp .= "    . = $osize{$section};\n";

    return $inp;
}

sub print_section {
    my ($kindre) = @_;
    my ($inp);

    foreach $sym (keys %kinds) {
	$inp .= "    *(.nesc.$sym)\n" if $kinds{$sym} =~ /$kindre/;
    }
    return $inp;
}

sub get_sizes {
    my ($f) = @_;
    my (%sizes);

    if (!open(OBJINFO, "objdump -h $f |")) {
	print STDERR "error obtaining symbol sizes\n";
	exit 1;
    }

    while (<OBJINFO>) {
	if (/\.nesc\.([A-Za-z0-9_\$]+)\s+([0-9a-fA-F]+)/) {
	    $sizes{$1} = hex $2;
	}
    }
    close OBJINFO;
    return %sizes;
}

sub parse_map {
    my ($f) = @_;
    my ($name, $size, $file, $ilistref, $output, $aoutput, $address);

    if (!open(MAP, "$f")) {
	print STDERR "error reading $f\n";
	exit 1;
    }

    undef %secaddress;
    undef %symbols;
    undef %output;
    undef %address;
    undef %sizes;
    undef %sections;

    while (<MAP>) {
	last if /^Linker script/;
    }
    while (<MAP>) {
	chomp;
	$name = 0;
	if (/^([^ ()*]+)\s+([0-9a-fx]+)/) {
	    $secaddress{$1} = hex $2;
	    $output = $1;
	}
	elsif (/^ ([^ ()*]+)\s+([0-9a-fx]+)\s+([0-9a-fx]+)\s+(.*)/) {
	    die unless $output;
	    $name = $1;
	    $address = $2;
	    $size = $3;
	    $file = $4;
	}
	elsif (/^ ([^ ()*]+)$/) {
	    # Split into two lines, sometimes.
	    $name = $1;
	    $_ = <MAP>;
	    if (/^\s+([0-9a-fx]+)\s+([0-9a-fx]+)\s+(.*)/) {
		$address = $1;
		$size = $2;
		$file = $3;
	    }
	    else {
		$name = 0;
	    }
	}
	elsif (/^\s+([0-9a-fx]+)\s+(.*) =/) {
	    $symbols{$2} = hex $1;
	}
	elsif (/^\s+([0-9a-fx]+)\s+PROVIDE \((.*),/) {
	    $symbols{$2} = hex $1;
	}
	#if ($name && !($name =~ /^\.(vectors|init|fini)/) &&
	if ($name && ($name =~ /^\.(nesc|progmem)/) &&
	    ($output =~ /^\.(bss|text|data)$/)) {
	    if ($name =~ /^\.(progmem)/) {
		$aoutput = ".progmem";
	    }
	    else {
		$aoutput = $output;
	    }
	    $name = "$file($name)";
	    $output{$name} = $aoutput;
	    $address{$name} = hex $address;
	    $sizes{$name} = hex $size;

	    $ilistref = $sections{$aoutput};
	    if (!$ilistref) {
		$ilistref = [ ];
		$sections{$aoutput} = $ilistref;
	    }
	    push @{$ilistref}, $name;
	}
    }
    close MAP || die;
}

sub get_kinds {
    my ($f) = @_;
    my (%kinds);

    if (!open(OBJINFO, $f)) {
	print STDERR "error obtaining symbol information\n";
	exit 1;
    }

    while (<OBJINFO>) {
	die unless /^([A-Za-z0-9_\$]+) ([A-Z]+)$/;
	$kinds{$1} = $2;
    }
    close OBJINFO || die;
    return %kinds;
}

sub gen_script {
    my ($template) = @_;

    open(TEMPLATE, $template) || die;
    while (<TEMPLATE>) {
	print SCRIPT eval "qq[$_]";
    }
    close TEMPLATE || die;
}