On most PC architectures (all modern ones for sure), the global variables are stored in a region in memory marked as "non-executable", which means it crashes if you try to run code directly from it (this is a security measure, see "NX bit")
It’s worth noting though on some it’s generic/customisable, so memory regions can be set to have whatever access rights. So on some we can still make that program executable by specifying the correct access rights (some you can just set the executable bit, others you might need to clear the write bit) on the section that will contain that variable at link time (or after on the binary).
Another possible option can be to make it const. As some systems will place some (or all) readonly sections in the same segment that may be marked as a group readonly and executable. So while it’s no longer the smallest program, it may still result in a program that successfully executes without specifying it in the linker (or modifying the binary).
As you mention though what this will then do will then depend on what instruction zeroes are decoded as. You could initialise with data (up to the size of an int on that platform) that may be a valid instruction(s). Of course now the program is even longer.
e.g. On x86 on some mainstream OS’s the following may work:
main=195; // which will return, unfortunately we’re one extra byte over `main(){}`
But we can make the smallest program with an infinite loop.
main=65259; // will run indefinitely
Again this is assuming we’re changing it in the linker (on MacOS the flags may look something like this -Xlinker -segprot -Xlinker __DATA -Xlinker rwx -Xlinker rwx) or modifying the binary. If not doing that you can try making it const (const main=x;).
2
u/ScrimpyCat Aug 02 '22
It’s worth noting though on some it’s generic/customisable, so memory regions can be set to have whatever access rights. So on some we can still make that program executable by specifying the correct access rights (some you can just set the executable bit, others you might need to clear the write bit) on the section that will contain that variable at link time (or after on the binary).
Another possible option can be to make it const. As some systems will place some (or all) readonly sections in the same segment that may be marked as a group readonly and executable. So while it’s no longer the smallest program, it may still result in a program that successfully executes without specifying it in the linker (or modifying the binary).
As you mention though what this will then do will then depend on what instruction zeroes are decoded as. You could initialise with data (up to the size of an int on that platform) that may be a valid instruction(s). Of course now the program is even longer.
e.g. On x86 on some mainstream OS’s the following may work:
But we can make the smallest program with an infinite loop.
Again this is assuming we’re changing it in the linker (on MacOS the flags may look something like this
-Xlinker -segprot -Xlinker __DATA -Xlinker rwx -Xlinker rwx) or modifying the binary. If not doing that you can try making it const (const main=x;).