Reputation: 1229
I want to know how the values of "bootstacktop" and "bootstack" are calculated by the assembler, when the code sets the value for %esp:
# Set the stack pointer
movl $(bootstacktop),%esp
At the end of the same assembly file, is the "definition" of "bootstacktop" is given:
###################################################################
# boot stack
###################################################################
.p2align PGSHIFT # force page alignment
.globl bootstack
bootstack:
.space KSTKSIZE
.globl bootstacktop
bootstacktop:
I found the value, looking at the deassebly, for 'bootstacktop', here is the part of the deassembly for the above 'mov' instruction:
# Set the stack pointer
movl $(bootstacktop),%esp
f0100034: bc 00 40 11 f0 mov $0xf0114000,%esp
Value of KSTKSIZE is 8*4096, PGSHIFT is 12. How did the value of 'bootsacktop' become '0xf0114000'? And whats the value of 'bootstack'?
Here is the linker script: http://pastebin.com/9DPakfgx
Upvotes: 1
Views: 532
Reputation: 47613
Since you posted your linker file I know you are working with JOS OS. Somewhere at the top of your assembler file with the code snippet you are showing will be a line including the file memlayout.h
. That file defines values for PGSHIFT and KSTKSIZE. This code:
###################################################################
# boot stack
###################################################################
.p2align PGSHIFT # force page alignment
.globl bootstack
bootstack:
.space KSTKSIZE
.globl bootstacktop
bootstacktop:
Will align the page with the bootstack to whatever value is defined in PGSHIFT. bootstack is a label (memory address) that happens to have space allocated after it with .space KSTKSIZE
(amount of space allocated = KSTKSIZE). KSTKSIZE will be defined in memlayout.h
. The .globl bootstacktop
directive simply says that this label will be made global (like a variable declared extern
in C). bootstackstop is another label (memory address) that will be the address just after the last byte in bootstack
. It is also declared globally for other objects to use. bootstacktop - bootstack = KSTKSIZE
The layout of items in an image or executable will be determined by where the linker placed these objects in the final image. Often a linker script drives more complex image layouts. If you have a linker script you may wish to consult it to see how the final image/executable is laid out.
You don't say whether you disassembled an image file on disk or whether this disassembly was done after the program was loaded in memory, but based on the value 0xf0100034
I would guess this is a virtual or physical address of some sort that was determined by the kernel when it loaded the file into memory (likely an ELF object or something equivalent). It would then be a combination of the memory location where the image is loaded into memory by the kernel and the offsets of objects within the image file that the linker generated.
Your question doesn't supply enough information to say definitively how this particular value was arrived at because we don't have the image(executable) that was used, what type of layout the image was in (was it ELF/PE etc) and what memory location was used by the OS to load the image.
Upvotes: 5