Sujet : Re: C23 thoughts and opinions
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 01. Jun 2024, 19:59:19
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v3fr28$2tmmv$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
User-Agent : Mozilla Thunderbird
On 01/06/2024 11:24, bart wrote:
On 01/06/2024 02:25, Scott Lurndal wrote:
[objcopy]
Nope, same thing. This doesn't inspire much confidence. With values shown, the actual size IS contained within the _size value, but only as the last 16 bits of the value.
gcc versions were 10.3.0 and 9.4.0 respectively; the latter is what is provided by Windows 11.
You also brought up the fact that the size is not known to the compiler anyway, which means a few things are not possible, like using the size in a static context.
I thought I'd dash off my own version of 'objcopy' to see if I could do any better. This version does binary/text to COFF only. The input file here was a .wav file:
C:\qapps>qq objcopy test.wav # running my objcopy
Compiling test.m to test.obj
Written test.obj # haven't settled on naming schemes yet
char[] name is: test_wav
u64 size name is: test_wav_len
C:\qapps>gcc demo.c test.obj
C:\qapps>a
Size = 14355
Data = 52 49 46 ...
The demo.c file is this:
#include <stdio.h>
extern char test_wav[];
extern long long test_wav_len;
int main(void) {
printf("Size = %lld\n", test_wav_len);
printf("Data = %02x %02x %02x ...\n", test_wav[0], test_wav[1], test_wav[2]);
}
And this is info about the binary to show the right data has got into the C program:
C:\qapps>dir test.wav
01/11/1996 04:05 14,354 test.wav
C:\qapps>dump test.wav
Dump of test.wav; Size = 14354 bytes
0000: 52 49 46 46 0A 38 00 00 57 41 56 45 66 6D 74 20 RIFF.8..WAVEfmt
There is one slight discrepancy: the size from the C file is one byte bigger; that's because I'm using 'strinclude' (in the code compiled during the process, which adds a terminator. I can fix that easily, or allow the option.
The script used to implement my 'objcopy' is shown below. It writes out a 2-line program which is compiled by my systems language into an object file.
------------------------------------------------
proc main=
if ncmdparams<1 then
println "Usage:"
println " qq objcopy filename [name]"
stop
fi
infile:=cmdparams[1]
basename:=extractbasefile(infile)
mfile:=basename+".m"
objfile:=basename+".obj"
if infile in (mfile, objfile) then abort("Name clash") fi
name:=basename+"_"+extractext(infile)
if ncmdparams>1 then
name:=cmdparams[2]
fi
writetextfile(mfile, (
sfprint("export []byte # = strinclude(""#"")",name, infile),
sfprint("export int #_len = #.len", name, name)
)
)
if system("mm -obj "+mfile)<>0 then
abort("Compile error on "+mfile)
else
println "Written", objfile
println "char[] name is:",name
println "u64 size name is:",name+"_len"
fi
end