1

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

4
  • I have done an entire search of the project files, there isnt an definition for bootstacktop. It is generated by the .space directive I think, but I cant figure it out if it is. Commented Sep 26, 2015 at 1:11
  • I've added the linker script Commented Sep 26, 2015 at 1:11
  • Oh You are working with JOS (Noticed that in your Linker file). There will be an include with those things defined. Probably inc/memlayout.h Commented Sep 26, 2015 at 1:17
  • Your linker script tells me you are using GNU Assembler and are targeting elf-i386. SO I don't need that info now Commented Sep 26, 2015 at 1:22

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.