0
.code32 .globl var var: .long 0 

Let's assume elf binary format. This is a piece of assembly ( I have no idea what exactly assembly it is, I am familiar with nasm). I cannot understand what does it mean.

How is it interpreted? . It looks like global variable, but where it in an elf format file? In global section or in data section? When will be known address of var? After compilation or after linking?

Actually, the problem is with understanding Pintos's start.S file. https://github.com/abhinav-upadhyay/Pintos/blob/master/threads/start.S

Please note, that in line 202 there is the same issue. But, I cannot understand why in the line 48: addr32 movl %eax, init_ram_pages - LOADER_PHYS_BASE - 0x20000 It looks like init_ram_pages was virutal address. But, please note that in the line 48 the CPU is in real mode still!

1
  • Just generic comment (too lazy to dig deeper into your particular question): section is just symbolic name for linker script, which will then map all object files with their sections to addresses (or whatever is the linking process producing), also based on the target binary format it may put different sections into different parts of the file. So the address (symbol table) is known after linking. Commented Sep 5, 2016 at 14:30

1 Answer 1

1

It's AT&T syntax.. Google gas at&t syntax and the front page is full of links that look useful.

The official manual for GNU as (aka gas) is https://sourceware.org/binutils/docs/as/. See also the tag wiki.

The NASM equivalent is

BITS 32 ;; .code32 ;section .text ;; This is the default for gas, same as for NASM global var ;; .globl var var: ;; var: dd 0 ;; .long 0 (GAS's terminology comes from before AMD64. .quad is a qword) 

where it in an elf format file? In global section or in data section?

IIRC, the default section is .text. There is no "global" section.

When will be known address of var? After compilation or after linking?

Same as for NASM: symbol addresses are link-time constants, but the difference between two addresses in the same section is an assemble-time constant.


addr32 movl %eax, init_ram_pages - LOADER_PHYS_BASE - 0x20000 

addr32 means use an address-size prefix if necessary, and encode the addressing mode using the 32-bit machine encoding.

Michael Petch's comment may shed some light on what address is filled in by the linker vs. how you should use it before paging is enabled, to answer the last part of your multi-part question.

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

7 Comments

Paging isn't enabled when the mov with init_ram_pages - LOADER_PHYS_BASE - 0x20000 is done. The linker script used VMA (Virtual Memory Addresses) starting at 0xC0020000 (LOADER_PHYS_BAS+LOADER_KERN_BASE.). This code needs to adjust the VMA of init_ram_pages to its physical memory address (Load Memory Address) in lower memory since paging isn't enabled and the linear address in high memory hasn't been mapped in yet. Prior to enabling paging this kind of adjustment to addresses is the norm for higher half kernel bootstrap designs.
@Gilgamesz The linker was given the value by the OS developer in the linker script kernel.lds.S (same directory as loader.S). In particular this line set the Virtual Memory Address to 0xC0020000: _start = LOADER_PHYS_BASE + LOADER_KERN_BASE; If you look at loader.h (same directory) you'll find that LOADER_PHYS_BASE = 0xC0000000 and LOADER_KERN_BASE=0x20000. Add those together and you get a VMA of 0xC0020000 . The linker script is what determines how the ELF object is laid out and what what virtual memory addresses should be used.
@Gilgamesz: why did you post this question with a dummy account, instead of your normal one that you commented with?
@PeterCordes : Since it is silly season for school starting it might be possible that J. Doe and Gilgamesz are different people with the same OS project. I noticed this too, since I commented on a related question yesterday. They might be two different people (or they could be the same). OSDev questions pick up right about this time of year. PintOS is one of a few academic kernels that are used to teach and we do get PintOS questions on SO.
@Gilgamesz: I think you can merge accounts somehow. Search for it on meta. IDK how, I never made multiple accounts in the first place.
|