0

So memory segmentation was abandoned in x86-64, but when we use assembly we can specify .code and .data sections/segments in our code, and there is also the stack pointer register.

And the stack segment, data segment and code segment registers.

How and where does that division of code/data/stack happens, is it implemented by the CPU or OS?

Because when we debug and see the disassembly view of some C program, the address space is linear with no divisions.

And when they say that the data segment has "parts" for the globals, statics and the heap, this is OS abstraction?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
JBDC
  • 5
  • 2
  • 1
    The parts are implemented using paging. The important things are the writable and executable flags which may be different for the various sections. Also the OS may handle stack specially so that it can grow on demand. The sections in an ELF binary are also treated specially by the loader, such as sections containing relocation information. – Jester Jan 17 '15 at 23:58
  • when compiling/linking, you can pass to the linker a file, usually with a .cmd extension, that will define the desired location for each kind of segment in the .o files. – user3629249 Jan 18 '15 at 00:17
  • in the .cmd file, you will define the location/size for .reloc, .const, .text, .data, .data2, .idata, and several other segment names You can also specify the specific address/size for the .stack, .heap, etc I use it often when there is memory mapped peripherals by giving a unique segment name for the peripheral and setting that peripherals segment address in the .cmd file – user3629249 Jan 18 '15 at 00:21
  • The segmentation wasn't completely abandoned, as either FS or GS (not sure which) is still used for kernel interface stuff and per thread stuff. – rcgldr Jan 18 '15 at 01:09
  • 1
    The section names (.text, .data, ...) are NOT identical to CPU segments addressed using segment registers. In a typical memory layout all segment registers (cs, ds, es and ss) (except fs and gs) address the same memory area. – Martin Rosenau Jan 18 '15 at 06:55

1 Answers1

2

You need to use a different model to think of memory usage. Think of memory using a program sections with attributes like:

  • Code: Executable, readable, no write
  • Static Data: Nonexecutable, readable, nowrite
  • Modifiable Data: Nonexecutable, readable, writeable (You might also add Demand-zero areas)

These attributes can be set at the page level. In a system you could have the pages interleaved

Code-Data-Code-Data-Code-Data

Normally, linkers will put similar sections together but it can lay out the memory in nearly any way with paging. The usual mechanism is that if you give names to sections, linkers will collect and group things with the same section together.

You are no restricted by the segmentation system from Ye Olde 8086.

The parts are then governed by the operating system and the linker.

user3344003
  • 20,574
  • 3
  • 26
  • 62