The linker trick has a problem. It doesn't set the non-executable stack bit, so as a result your whole binary will have an executable stack, and therefore be insecure against various stack smashing exploits.
Try running 'readelf -S blob.o' and you won't see any '.note.GNU-stack' in the output.
If you go via a C source file compiled with gcc (or clang), then the compiler sets the bit properly.
Edit: Also I tried to add a comment to the original posting to warn them, but blogger just eats my comment.
If you are right then this can be mitigated by two additional options passed to objcopy, namely --add-section to create an empty section and --set-section-flags to adjust the flags of this empty section. I'm just recreating the section/flags that I see in a normally compiled file.
Looks good. The reason I know a bit about this is I had the same "bin2o" hacky script that used objcopy. It broke every time someone found a new architecture or platform (ie. having to choose the correct -O and -B flags is non-trivial if you want to support every architecture).
The solution (which is not really much better than yours) is a script that creates some assembler and assembles it:
No binary object file "has a stack" since it's created ephemerally for the executable at runtime. However the linker checks all objects and determines if any of them contain code that requires an executable stack. For backwards compat reasons, that check will say "executable stack needed" for objects that don't contain the special section flag.
In other words, the check is a logical && operation across all the objects, and therefore somewhat fragile. It's for this reason that in RHEL we have a separate check for all the RPMs we ship to make sure that none of them contain executable stack binaries (well, except in some rather exceptional circumstances).
I think you can argue this is a bug in the linker command that creates the binary blob object.
Try running 'readelf -S blob.o' and you won't see any '.note.GNU-stack' in the output.
If you go via a C source file compiled with gcc (or clang), then the compiler sets the bit properly.
Edit: Also I tried to add a comment to the original posting to warn them, but blogger just eats my comment.