Sujet : Re: C23 thoughts and opinions
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 31. May 2024, 19:03:10
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v3d3ct$2b5sl$1@dont-email.me>
References : 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
User-Agent : Mozilla Thunderbird
On 31/05/2024 15:34, Michael S wrote:
On Fri, 31 May 2024 15:04:46 +0100
bart <bc@freeuk.com> wrote:
Can you show the full program and the full process?
test_objcopy.c:
#include <stdio.h>
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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.