1

I'm trying to reconcile a few concepts.

I know of virtual memory is shared (mapped) between the kernel and all user processes, which I read here. I also know that when the compiler generates addresses for code + data, the kernel must load them at the correct virtual addresses for that process.

To constrain the scope of the question, I'll just mean gcc when I mention 'the compiler'. So does the compiler need to be compliant each new release of an OS, to know not to place code or data at the high memory addresses reserved for the kernel? As in, someone writing that piece of the compiler must know those details of how the kernel plans to load the program (lest the compiler put executable code in high memory)?

Or am I confusing different concepts? I got a bit confused when going through this tutorial, especially at the very bottom where it has OS code in low memory addresses, because I thought Linux uses high memory for the kernel.

Community
  • 1
  • 1
Mike Lui
  • 1,301
  • 10
  • 23

2 Answers2

2

The compiler doesn't determine the address ranges in memory at which things are placed. That's handled by the OS.

When the program is first executed, the loader places the various portions of the program and its libraries in memory. For memory that's allocated dynamically, large chunks are allocated from the OS and then sometimes divided into smaller chunks.

The OS loader knows where to load things. And the OS's virtual memory allocation logic how to find safe, empty spaces in the address space the process uses.

I'm not sure what you mean by the "high memory addresses reserved for the kernel". If you're talking about a 2G/2G or 3G/1G split on a 32-bit operating system, that is a fundamental design element of those OSes that use it. It doesn't change with versions.

If you're talking about high physical memory, then no. Compilers don't care about physical memory.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 1
    There's often some invisible glue in between also, many compilers (or rather the linker) specify the memory layout via a linker script, which is tailored to the OS - where in memory to place the various parts of an executable. – nos Jun 23 '16 at 20:28
  • For Linux, it is actually simple: GCC places the code at virtual address 0 on most architectures. – tofro Jun 23 '16 at 20:51
  • You're correct. I seem to have gotten it in my head when I looked at the disassembly that there were direct (virtual) addresses being used and got confused. – Mike Lui Jun 23 '16 at 21:34
1

Linux gives each application its own memory space, distinct from the kernel. The page table contains the translations between this memory space and physical RAM, and the kernel sets up the page table so there's no interference.

That said, the compiler usually doesn't even care where the program is loaded in memory. Why would it?

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • I was thinking if it put in a jmp instruction to a static address, or how to know where static/global data is located. – Mike Lui Jun 23 '16 at 20:50
  • Ah on second look I see global data is addressed relatively, and any direct jumps are by definition also relative. – Mike Lui Jun 23 '16 at 21:32