Page 1 of 1

Nasm: issues with writing to .text section

Posted: Tue Oct 09, 2007 5:04 am
by JamesM
I'm compiling my OS on a different computer that has a later version of NASM than I'm used to.

I have a variable, initialEsp that I use to record the stack pointer as GRUB gives it (So I can move the stack elsewhere later in remapStack). Before, I had something like:

Code: Select all

initialEsp:
dd 0

--code here--
And that worked fine. I declared initialEsp as extern in my .cc files and everything was happy. However, the latest version of NASM doesn't like programs writing to the .text section. (It screams fatal at me)

So I decided to try putting it in the .bss or .data sections - no joy, they get linked to some random location (0xf004 or something equally troublesome). So I tried declaring initialEsp in one of my .cc files and extern'ing it in my NASM boot file. No joy - ended up trying to write to 0x0 (I was observing the effects of these in objdump, as well as by trying to run the program).

In the end I opted for a major hack-o-rama:

Code: Select all

start: ;This is where all my multiboot stuff goes
dd 0 ; I added this line to reserve some space at 0x100000
dd MULTIBOOT BLEH
dd MULTIBOOT_MORE_BLEH
...

absolute 0x100000
initialEsp resw 2  ; initialEsp = 2 words (4 bytes) at 0x100000.

[SECTION .text] ; back to text section
--code--
Which works, but I don't like hacks and this is a mother of one!

anyone got any ideas? I assume FASM will work the same way.

Cheers

James

Posted: Tue Oct 09, 2007 6:47 am
by os64dev
i think the new version of NASM does it the right way. Putting data in a .text segment is bad, because it allows for self modifying code and all the security issues that come with that. Also if you look at the ELF definition of .text segment is specifies (A)ccess and e(X)ecutable and thus is read-only. In conclusion NASM does it correctly.

Why don't you push the 'initialEsp' on the stack. Then you can pop it or reference it just before you remap the stack.