0

I am trying to assemble a simple Hello World program with the GNU assembler (as) on a Raspberry Pi 3 B+ running NetBSD 9.1
What flags do I need to add to as or ld to make them assemble the code correctly for the architecture I am using?

$ as -o hllwrld.o hllwrld.s
$ ld -o hllwrld hllwrld.o
$ ./hllwrld
-sh: Cannot execute ELF binary ./hllwrld

$ uname -a
NetBSD rpi 9.1 NetBSD 9.1 (RPI) #0: Sun Oct 18 19:24:30 UTC 2020  mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/evbarm/compile/RPI evbarm

Is this aarch64 or arm64?

I know there are man pages but I am just learning assembly so I have no idea what configurations/flags/arguments I even need to be looking for.

Thanks for any help.

DX0
  • 51
  • 6
  • aarch64 and arm64 are synonyms. It might be a good idea to compare `file ./hllwrld` against `file /bin/ls` (or any other known-working executable) to see what kind of executables your NetBSD system wants. (Some differences are to be expected since you linked a static executable, but showing the differences might let someone help you even if they don't also use NetBSD on ARM) – Peter Cordes May 17 '21 at 06:09
  • Could be a wrongly set up ELF interpreter path. – fuz May 17 '21 at 07:44
  • @fuz: Possible, but on Linux that results in `execve` returning `-ENOENT`, so the shell would print "No such file or directory". BSD *could* be different, though. Of course that's only possible if `ld` without any library options defaults to dynamic linking on this system. – Peter Cordes May 17 '21 at 18:38

2 Answers2

2

This may be failing for a number of reasons, all of which come down to missing arguments and/or flags to ld and may include runtime linker information, startup code, and libraries.

Give this a try for a hint and see what's happing behind the scenes:

$ cc -v -o hllwrld hllwrld.o
1

The answer by Klaus should give enough clues about specifics for your system, but you might also want to look at a working example for NetBSD/i386 and amd64:

github.com/robohack/experiments: thello.s

If you open the file in emacs and approve the local variables settings then the compile-command will be set to a command line that will assemble, link, and run, the example program on a compatible NetBSD system.

Greg A. Woods
  • 2,663
  • 29
  • 26
  • 1
    this would be a much better answer if you actually included the command, instead of just linking to it. In emacs LISP with `fn` as the basename, it's doing this string concatenation: `"as -o " fn ".o " fn ".s && ld -Bstatic -o "fn " /usr/lib/sysident.o " fn ".o && ./" fn`. IDK why it's necessary to link a `sysident.o` file – Peter Cordes May 18 '21 at 02:45
  • nobody learns by cut&paste -- not even me! sysident.o provides the ELF header definition – Greg A. Woods May 19 '21 at 20:46
  • Right, that's why good answers provide the actual answer (not a link to an answer), *and* an explanation. Or are you trying to teach people how to google for examples? – Peter Cordes May 19 '21 at 21:09
  • I think we need a little more of both in this place (i.e. teaching how to find answers on one's own as well as sometimes giving blatantly complete answers). After all this isn't wikipedia (though I really wish it were a lot more like wikipedia). Also I don't really think I can improve on the answer Klaus gave without being sure I'm not missing something specific to ARM as I don't currently have a running ARM machine to test a blatantly complete answer on. – Greg A. Woods May 20 '21 at 05:13