3 sub StabLine
($ $ $ $ $ $) {
4 local ($comment,$n_strx,$type,$other,$desc,$value) = @_;
10 print "","dd","0" . $value . "h";
13 sub RStabLine
($ $ $ $ $) {
14 local ($comment,$offset,$info,$type,$symbol) = @_;
16 print "","dd",$offset;
18 print "","db",$symbol;
22 #this sub exists because i've no idea how to print non-ascii numbers in perl
25 local ($offset, $shnum) = @_;
26 seek(FINAL
,$offset,0);
27 if ( $shnum == 2 ) { printf FINAL
"\x02" } ;
28 if ( $shnum == 3 ) { printf FINAL
"\x03" } ;
29 if ( $shnum == 4 ) { printf FINAL
"\x04" } ;
30 if ( $shnum == 5 ) { printf FINAL
"\x05" } ;
31 if ( $shnum == 6 ) { printf FINAL
"\x06" } ;
32 if ( $shnum == 7 ) { printf FINAL
"\x07" } ;
33 if ( $shnum == 8 ) { printf FINAL
"\x08" } ;
34 if ( $shnum == 9 ) { printf FINAL
"\x09" } ;
35 if ( $shnum == 10 ) { printf FINAL
"\x0a" } ;
36 if ( $shnum == 11 ) { printf FINAL
"\x0b" } ;
37 if ( $shnum == 12 ) { printf FINAL
"\x0c" } ;
38 if ( $shnum == 13 ) { printf FINAL
"\x0d" } ;
39 if ( $shnum == 14 ) { printf FINAL
"\x0e" } ;
40 if ( $shnum == 15 ) { printf FINAL
"\x0f" } ;
46 print "\t-f,--input-file";
47 print "\t\tThe input file name (only required option)";
48 print "\t-o,--output-file";
49 print "\t\tThe output file name (if not specified, *.asm becomes *.o";
50 print "\t\tand anything else becomes a.out)";
51 print "\t-l,--list-file";
52 print "\t\tThe listing file's name (default: trailing .asm is
54 print "\t\tif there and .lst is appended)";
55 print "\t-s,--second-asm-file";
56 print "\t\tThe second asm file's name (default: trailing .asm is";
57 print "\t\tremoved if there and .nasm is appended)";
62 if ( $ARGV[0] eq "" ) { $ARGV[0] = "-h" };
68 while ( $ARGV[$i] ne "" ) {
71 if ( m/^-f$/ ) { $filename = $ARGV[++$i] };
72 if ( m/^-o$/ ) { $outname = $ARGV[++$i] };
73 if ( m/^-l$/ ) { $listname = $ARGV[++$i] };
74 if ( m/^-s$/ ) { $asmname = $ARGV[++$i] };
75 if ( m/^-h$/ ) { DispHelp
};
76 } elsif ( m/^--\w+/ ) {
77 if ( m/^--input-file$/ ) { $filename = $ARGV[++$i] };
78 if ( m/^--output-file$/ ) { $outname = $ARGV[++$i] };
79 if ( m/^--list-file$/ ) { $listname = $ARGV[++$i] };
80 if ( m/^--second-asm-file$/ ) { $asmname = $ARGV[++$i] };
81 if ( m/^--help/ ) { DispHelp
};
83 while ( $ARGV[++$i] ) {
93 if ( $filename eq "" ) { DispHelp
() };
95 if ( $outname eq "" ) {
97 $outname =~ s/\.asm/.o/;
98 if ( $outname eq $filename ) { $outname = "a.out" };
101 if ( $listname eq "" ) {
102 $listname = $filename;
103 $listname =~ s/\.asm//;
107 if ( $asmname eq "" ) {
108 $asmname = $filename;
109 $asmname =~ s/\.asm//;
113 $err = `nasm -f elf ${filename} -l ${listname} -o ${outname} `;
115 if ( $err ) { die "\n$err\n"};
117 open(LISTFILE
,"${listname}") or die "\n $0: Could not reopen list file!\n";
118 open(ASMFILE
,">${asmname}") or die "\n $0: Could not open asm file!\n";
122 open(OLDASM
,$filename) or die "\n$0: Cannot open file $filename\n";
124 while ( $x = <OLDASM
> ) {
128 @stab = ("n_desc", "value");
129 @rel_stab = ("offset");
131 $current_section = "";
133 $midst_of_macro = 'FALSE';
136 while ( $x = <LISTFILE
> ) {
137 if ( $x =~ m/[^;]*%include/ ) {
139 while ( $x =~ m/\s+\d+\s+\<\d+\>\s+/ ) {
144 if ( $current_section eq ".text" ) {
145 if ( $x =~ m/^\s+(\S+)\s+(\S+)\s+(\S+)\s+[^<]+$/ ) {
146 $stab[$i++] = $1-$line_dec; #linenum
147 $stab[$i++] = $2; #offset
153 $midst_of_macro = 'FALSE';
154 } elsif ( $x =~ m/^\s+(\S+)\s+(\S+)\s+(\S+)\s+<\d+>/ ) {
155 if ( $midst_of_macro eq 'TRUE' ) {
156 $stab[$i] = $stab[$i-2]; #same linenum
159 $stab[$i] = $1 - ++$line_dec;
160 $midst_of_macro = 'TRUE';
172 } elsif ( $x =~ m/\s+\S+\s+\S+\s+\S+\s+<\d+>/ ) { # is it a macro?
175 if ( $x =~ s/(section|segment)\s+([^\s]+)/$2/ ) {
176 $current_section = $2;
182 unless ( $has_text eq "TRUE" ) {
183 $err = `nasm -f elf ${asmname} -o ${outname}`;
189 $, = "\t"; #output field separator
190 $\
= "\n"; #output record separator
192 print "section .stab noalloc";
193 StabLine
(";header",1,0,0,$count+1,length($filename)*2+3);
194 StabLine
(";so",length($asmname)+2,"064h",0,0,0);
199 $rel_stab[$j++] = $offset + 8;
201 while ( $stab[$i] ) {
202 StabLine
(";N_SLINE" . " " . ( ($i+2) / 2 ), 0, "044h", 0,
203 $stab[$i++], $stab[$i++]);
205 $rel_stab[$j++] = $offset + 8;
208 #Write .rel.stab section
209 print "\n\nsection .rel.stab noalloc";
211 open (READELF
,"readelf -s ${outname} |") or die "\n$0: Could not run readelf\n";
213 while ( $x = <READELF
> ) {
214 if ( $x =~ m/\s+(\d+):\s+00000000\s+\d+\s+SECTION\s+\w+\s+\w+\s+1\s+/){ $textsymnum = $1;
221 while ( $rel_stab[$i] ne "" ) {
222 RStabLine
(";relocation for N_SLINE " . ($i), $rel_stab[$i], 0, 1, $textsymnum);
226 #Write .stabstr section
228 print "\n\nsection .stabstr noalloc";
231 print "","db",'"' . $asmname . '"';
233 print "","db",'"' . $asmname . '"' ;
238 $err = `nasm -f elf ${asmname} -o ${outname}`;
240 if ( $err ) { die "\n$err\n" } ;
242 open (READELF
,"readelf -h -S ${outname} |") or die "\n$0: Could not run readelf\n";
245 while ( $x = <READELF
> ) {
246 if ( $x =~ m/Start\s+of\s+section\s+headers:\s+(\d+)\s+/ ) {
249 if ( $x =~ m/Size\s+of\s+section\s+headers:\s+(\d+)\s+/ ) {
252 if ( $x =~ m/\[\s*(\d+)\]\s+.rel.stab\s+/ ) {
255 if ( $x =~ m/\[\s*(\d+)\]\s+.stab\s+/ ) {
258 if ( $x =~ m/\[\s*(\d+)\]\s+.stabstr\s+/ ) {
261 if ( $x =~ m/\[\s*(\d+)\]\s+.symtab\s+/ ) {
267 sysopen (FINAL
,"${outname}",2,0) or die "\n$0: Could not open ${outname}";
268 $, = ""; #output field separator
269 $\
= ""; #output record separator
271 #set .rel.stab->type to rel
272 OutBin
($shoff + ($shentsize * $relnum) + 4,9);
274 #set .rel.stab->link to .symtab
275 OutBin
($shoff + ($shentsize * $relnum) + 24,$symtabnum);
277 #set .rel.stab->info to .stab
278 OutBin
($shoff + ($shentsize * $relnum) + 28,$stabnum);
280 #set .rel.stab->entsize to 8
281 OutBin
($shoff + ($shentsize * $relnum) + 36,8);
283 #set .stab->link to .stabstr
284 OutBin
($shoff + ($shentsize * $stabnum) + 24,$stabstrnum);
286 #set .stab->entsize to 12
287 OutBin
($shoff + ($shentsize * $stabnum) + 36,12);
289 #set .stabstr->type to strtab
290 OutBin
($shoff + ($shentsize * $stabstrnum) + 4,3);
294 #Date: 17 Mar 2002 15:51:20 -0800
295 #From: kitsred@hotmail.com (kired)
296 #Newsgroups: alt.lang.asm