3

I am trying to understand the implementation of process segments in 64-bit architectures. I came across these 2 discussions:

64 bit application: layout in memory

Process segments(stack,heap,data and code) are contiguous in memory?

However, I am still unclear. Segmentation was introduced to overcome the limits of using 64K memory with 16-bit addresses, in the Intel 80286/80386 days. After that 32-bit Intel machines still continued it for compatibility reasons.

Now moving onto 64-bit: The manuals say that segmentation is rarely implemented here (ref: http://en.wikipedia.org/wiki/X86_memory_segmentation). The virtual memory and paging can provde access to the entire address space alongwith protection.

So my question is: How is a 64-bit program compiled by 64-bit compilers? Do they still use the concept of "segments" (because I still see the mentions of data segment, stack segment, etc) as was before, but with higher # bit segment pointers? Or, is the word "segment" referring to something completely different for 64-bit architectures?

Any help is appreciated.

Community
  • 1
  • 1
Sujay Phadke
  • 2,145
  • 1
  • 22
  • 41
  • I haven't re-read the links you provided, but if I recall correctly, this is a primary difference between compiling/running your code in `real mode` or `protect mode`. If not specifically setup to take advantage of `protect mode` you default to `real mode` with all the traditional limitations. – David C. Rankin Apr 20 '15 at 08:07
  • Sure but the segmentation is orthogonal to real and protected mode correct? – Sujay Phadke Apr 20 '15 at 09:55

1 Answers1

0

There are no segments in 64-bit mode (THANK GOD!).

Segmentation in Intel land has always been a kludge.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • well then could you explain where the different parts of a 64-bit compiled code are stored? Where does the code go? how about stack variables? and the heap? I came across this thread too: http://reverseengineering.stackexchange.com/questions/2006/how-are-the-segment-registers-fs-gs-cs-ss-ds-es-used-in-linux It says that windows x64 still uses the GS register for thread specific tasks. If the entire memory is treated as "flat", can code be interleaved with data and stack variables in theory? – Sujay Phadke Apr 20 '15 at 20:30
  • The operating system sets the boundaries. The linker usually makes the final decision. Remember that virtually every system other than Intel since the 1970's had has a flat memory model. – user3344003 Apr 20 '15 at 22:21
  • Ok. So if I understand correctly, these boundaries define the various segments for the various parts of the vote right? So in essence isn't it still using segmentation even though they may not call it that? If it's a completely flat model, shouldn't I be able to mix code, stack data and heap data if I wanted to? – Sujay Phadke Apr 20 '15 at 22:24
  • You might want to take a different perspective. A program segment typically is a combination of these attributes: executable/readable/writeable. A linker is going to group the segments of a program according to those terms. For the static areas of a program, linkers generally to to keep similar areas together. In the pre-thread days, the stack was usually located at the highest area of the user address space so that it could grow downward. With threads, a process can have multiple stacks so they have to be spaced apart. The heap is usually between the linker's static data and the stack. – user3344003 Apr 20 '15 at 22:31
  • Thanks for the explanation. But I am still not clear as to the difference. If I understand what you're saying is a picture like this: (static data) .... ( heap) .... (Stacks). Is that correct. Now I understand that you may have multiple of these portions and they could be allocated anywhere really. So isn't is still segmentation? The boundaries could be dynamic and there may be multiple spaces, but how this it fundamentally different from segmentation? Can i still run 'size' on a 64-bit binary? – Sujay Phadke Apr 20 '15 at 22:34
  • 1
    In the segment model, the attributes were at on a segment level. In a flat model, they are done on a per page level. Linkers usually group similar pages together into program sections. Page protection is a more flexible system than segment protection. – user3344003 Apr 20 '15 at 22:39
  • Thank that help.s. Do you have any reference for that? I've tried searching but couldn't find what you've mentioned. so about the 'size' command: can that be used directly to get a list of the static data, or is there an equivalent command? – Sujay Phadke Apr 20 '15 at 23:29
  • The easiest, if you can find one, is a VAX Architecture manual. You might be able to find one of those. That is the easiest system to understand (though dated). – user3344003 Apr 21 '15 at 01:58