-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, 24 Jun 2019, Paul Flint wrote:

Greetings List Lurkers,

Marvelous Marco submits this for our consternation:

<snip>
I was recently messing around with embedding files into ELF executables and came across this strange behavior:

#include <stdio.h>

// extern unsigned char *_binary_data_txt_start; /* doesn't work */
extern unsigned char _binary_data_txt_start[]; /* does work */

int main() {
   printf("%p\n", _binary_data_txt_start);
}

This file segfaults when using the pointer (*) notation, but runs correctly with the array notation ([]).

I'm compiling with the command:

gcc test.c -o test -Wl,--format=binary,data.txt,--format=default -g

With the above in a file called test.c and some data in a file called data.txt.

I asked for some explanation but I kept getting back "That makes sense" with no explanation....

        I'm not 100% certain, but I think that what's happening is that
using the * notation only declares the pointer, while using the [] notation
declares the pointer _and initializes it to point at a (zero-size) array_.

        I don't get segfaults when I try *_binary_data_txt_start; it runs
correctly and reports the pointer as (nil), where _binary_data_txt_start[]
reports an actual address, which I believe supports this.

- -- John Campbell
[email protected]

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iEYEARECAAYFAl0RjmkACgkQPu/PJk2ePZ2tawCeKlB2TtqnlaYP4Di0VJe0W2BC
cgcAoJouFwtHfIIz6R0S32QTAomCv0hB
=JJJB
-----END PGP SIGNATURE-----

Reply via email to