Author Topic: linux hello world segfaults  (Read 6683 times)

nobody

  • Guest
linux hello world segfaults
« on: June 28, 2005, 12:51:43 AM »
I am trying to get TLDP's "Hello World" program to compile (http://www.tldp.org/HOWTO/Assembly-HOWTO/hello.html). I followed the instructions to build it:

$ nasm -f elf hello.asm
$ ld -s -o hello hello.o

When I try to run the program with "./hello", I get a segmentation fault. Does anybody know what the problem could be? I tried the gas example following the instructions in the howto, and it seemed to work. Could somebody tell me what I'm doing wrong?

Thanks.

nobody

  • Guest
Re: linux hello world segfaults
« Reply #1 on: June 28, 2005, 01:04:44 AM »
I am using nasm 0.98.39-r1 btw.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: linux hello world segfaults
« Reply #2 on: June 28, 2005, 06:42:21 AM »
What kernel are you running, nobody? :)

> I'm running a 2.6.11 kernel.

Ah, Ha! 2.6.11 (or so) seems to have quietly introduced a "new requirement" beyond the existing ELF spec. The last section in your binary must be writable!

This means that "tricks" like putting your constant "hello world" data in the code section (to save a section and shorten the executable), is no longer going to work!

The "elfexe" example that ships with Fasm segfaults on this kernel, too. (moving the data section after the code fixes it)

It's a bit of a puzzle why the code you cite has a problem, since it *looks* perfectly okay. I think I tested something "almost exactly" the same, and it worked. Try moving the ".data" section to the end, or adding a ".bss" section whether you need one or not...

Something that will screw us up, that we might have gotten away with before, is misspelling section names. Nasm "knows" the names ".text", ".data", ".rodata", and ".bss". "User defined" names get the same properties as ".text", by default. So if we type "section data" - leaving out the dot - we get a "nowrite" section, which will segfault on this kernel. Newsreader software is prone to messing with dots... the Nasm forum is known to eliminate "extra" spaces for us, turning "section .data" into "section.data" - which is *not* the same thing to Nasm! Possible that something like this happened to your code?

The reason why this is happening isn't an accident. The elf loader in previous kernels wasn't checking return values from some functions that *could* have been failing to zero memory - a breach of security. So it's a Good Thing the changes were made, but it puts us in the position that "valid" (according to elf spec) binaries won't run. I really think it would be a better kernel if it would issue a diagnostic and exit ("scream and die") instead of just sending SIG_SEGV... To that extent, it's a "bug", IMHO. But we can find workarounds, I think - just make sure you've got a ".data" or ".bss" section (I think ld should put them after ".text", regardless of where they appear in the source). I don't know where this leaves you with your code - since you do that. All I can suggest is "make sure '.data' is spelled wright".

More info on this in news:alt.lang.asm under the odd subject "Blueflops ate my X"... I'll try to post more on the subject if/when I get a better grip on it. Since the kernel's been released (and 2.6.12 is the same), about all we can do is "get the word out"... Strictly speaking, it's not a "Nasm problem". (Good! :)

Best,
Frank