Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: jak Newsgroups: comp.lang.c Subject: Re: C23 thoughts and opinions Date: Fri, 31 May 2024 21:42:35 +0200 Organization: A noiseless patient Spider Lines: 120 Message-ID: References: <87y18047jk.fsf@nosuchdomain.example.com> <87msoe1xxo.fsf@nosuchdomain.example.com> <87ikz11osy.fsf@nosuchdomain.example.com> <20240530170836.00005fa0@yahoo.com> <20240530180345.00003d9f@yahoo.com> <20240531161937.000063af@yahoo.com> <20240531162811.00006719@yahoo.com> <20240531164835.00007128@yahoo.com> <20240531173437.00003bee@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 31 May 2024 21:42:36 +0200 (CEST) Injection-Info: dont-email.me; posting-host="26ef99e7b718683a3d5a35623baed532"; logging-data="2496970"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18WNPukFSiutz8kTL5Z6hau" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0 SeaMonkey/2.53.18.2 Cancel-Lock: sha1:YJrbpaaGMEFW6KnOMqoo6QAmGqU= In-Reply-To: Bytes: 6226 bart ha scritto: > On 31/05/2024 15:34, Michael S wrote: >> On Fri, 31 May 2024 15:04:46 +0100 >> bart wrote: > >>> Can you show the full program and the full process? >> >> test_objcopy.c: >> #include >> >> int data1[42] = { 1,2,3 ,4,5}; >> extern unsigned char _binary_test_bi_start[]; >> extern unsigned char _binary_test_bi_end[]; >> extern unsigned char _binary_test_bi_size[]; >> >> extern unsigned char _binary_bin_to_list_c_start[]; >> extern unsigned char _binary_bin_to_list_c_end[]; >> extern unsigned char _binary_bin_to_list_c_size[]; >> >> int main() >> { >>    printf("%-40s %p %zd\n", "_binary_test_bi_start", >>      _binary_test_bi_start, (size_t)_binary_test_bi_start); >>    printf("%-40s %p %zd\n", "_binary_test_bi_end", >>      _binary_test_bi_end, (size_t)_binary_test_bi_end); >>    printf("%-40s %p %zd\n", "_binary_test_bi_size", >>      _binary_test_bi_size, (size_t)_binary_test_bi_size); >>    printf("%-40s %p %zd\n", "_binary_bin_to_list_c_start", >>      _binary_bin_to_list_c_start, (size_t)_binary_bin_to_list_c_start); >>    printf("%-40s %p %zd\n", "_binary_bin_to_list_c_end", >>      _binary_bin_to_list_c_end, (size_t)_binary_bin_to_list_c_end); >>    printf("%-40s %p %zd\n", "_binary_bin_to_list_c_size", >>      _binary_bin_to_list_c_size, (size_t)_binary_bin_to_list_c_size); >>    return 0; >> } >> >> Test files: test.bi and bin_to_list_c. >> Conversion to ojects: >> objcopy -I binary -O elf64-x86-64 test.bi test_bi.o >> objcopy -I binary -O elf64-x86-64 bin_to_list.c test_c.o >> >> Compilation: >> gcc -s -Wall -Oz test_objcopy.c test_bi.o test_c.o > > OK, thanks. But I forget to ask what results you got from running the > program. Because if I try your code, using hello.c and hello.exe as test > binary/source data, I get this output: > > _binary_test_bi_start                    00007ff6497620e0 140695771160800 > _binary_test_bi_end                      00007ff649762ae0 140695771163360 > _binary_test_bi_size                     00007ff509750a00 140690402380288 > _binary_bin_to_list_c_start              00007ff649762ae0 140695771163360 > _binary_bin_to_list_c_end                00007ff649762b26 140695771163430 > _binary_bin_to_list_c_size               00007ff509750046 140690402377798 > > The sizes should have been 2560 and 70 respectively; those values are > bit bigger than that. > > However I see that you also have start and end addresses, which sounds a > much better way of determining the size. (In that case, what are those > *size symbols for?). > > So I can put together a working test: > > --------------------------------- > #include > #include > #include > > extern unsigned char _binary_hello_c_start[]; > extern unsigned char _binary_hello_c_end[]; > > char* makestr(char* start, char* end) { >     int length = end-start; >     char* s = malloc(length+1); >     memcpy(s, start, length); >     *(s+length) = 0; >     return s; > } > > int main() { >     char* str = makestr(_binary_hello_c_start, _binary_hello_c_end); > >     printf("Hello = \n%s", str); > } > --------------------------------- > > I can build it like this: > > --------------------------------- > C:\c>mcc -c c > Compiling c.c to c.obj > > C:\c>objcopy -I binary -O elf64-x86-64 hello.c hello.obj > > C:\c>gcc c.c hello.obj > --------------------------------- > > And run it like this: > --------------------------------- > C:\c>a > Hello = > #include "stdio.h" > > int main(void) { >     printf("Hello, World!\n"); > } > --------------------------------- > > Instead of one compiler, here I used two compilers, a tool 'objcopy' > (which bizarrely needs to generate ELF format files) and lots of extra > ugly code. I also need to disregard whatever the hell _binary_..._size > does. > > But it works. > > > You could use the pe-x86-64 format instead of the elf64-x86-64 to reduce the size of the object.