Sujet : Re: C23 thoughts and opinions
De : nospam (at) *nospam* please.ty (jak)
Groupes : comp.lang.cDate : 01. Jun 2024, 12:59:32
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v3f2f3$2oq04$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
User-Agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0 SeaMonkey/2.53.18.2
bart ha scritto:
On 01/06/2024 02:37, jak wrote:
bart ha scritto:
I can see that the first two can be subtracted to give the sizes of the data, which is 70 or 0x46. 0x46 is the last byte of the address of _size, so what's happening there? What's with the crap in bits 16-47?
>
I can extract the size using:
>
printf("%d\n", (unsigned short)&_binary_hello_c_size);
>
But something is not right. I've also asked what is the point of the -size symbol if you can just do -end - -start, but nobody has explained.
>
typedef unsigned char uchar;
extern uchar _binary_hello_c_size[];
long hello_c_size = _binary_hello_c_size - (uchar *)0;
What result for the size did you get when you ran this?
It seems people are just guessing what might be the right code and posting random fragments!
I wrote it that way precisely because I believed it was the clearest
way. With the extern you can retrive the relative values that in the
case of _start and _end correspond to the initial and final address of
the object, in fact you can get the length of the object by subtracting
the starting address from the final one:
extern char _binary_hello_c_start[];
extern char _binary_hello_c_end[];
long len = _binary_hello_c_end - _binary_hello_c_start;
Unfortunately, _size is provided in the same way as _start and _end addresses, then, since it is not an address but a length and in C:
Address +/- Value = Address
Address +/- Address = Value
so, to retrive this length that in the program it is seen as an address it is sufficient to subtract the starting address which in the case of a length is zero.
extern char _binary_hello_c_size[];
long len = _binary_hello_c_size - (char *)0;
surely you can also recover the value with a cast:
long len = (long)_binary_hello_c_size;
but the example I sent you had seemed more explanatory while the cast
seems to me a blow of hoe.
Here nobody invents anything. I'm sorry you think this.
/*
* example:
* file to embed:
* --- start file.txt ---
* line number 1
* line number 2
* line number 3
* line number 4
* line number 5
* line number 6
* line number 7
* line number 8
* line number 9
* line number 10
* line number 11
* line number 12
* line number 13
* line number 14
* line number 15
* line number 16
* line number 17
* line number 18
* line number 19
* line number 20
* --- end file.txt ---
* objcopy --input-target binary --output-target pe-x86-64 --binary-architecture i386 file.txt file.txt.o
* gcc embed.c file.txt.o -o embed
*/
#include <stdio.h>
int main()
{
typedef unsigned char uchar;
extern uchar _binary_file_txt_start[];
extern uchar _binary_file_txt_size[];
long file_txt_size = _binary_file_txt_size - (uchar *)0;
for(long i = 0; i < file_txt_size; i++)
putchar(_binary_file_txt_start[i]);
return 0;
}
output: show file.txt content