0

I read a book about segmentations under x86, and I know gcc compiler also use segments. I'm new to this topic. I just wondering if segmentations in compiler will be mapped to segmentations in x86 cpu.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
Raizo
  • 13
  • 3

1 Answers1

0

I just wondering if segmentations in compiler will be mapped to segmentations in x86 cpu.

The 32 bits x86 (so i386 to i686 and compatible) had segment registers. The 64 bits x86-64 (used in most laptops or desktops in 2020), no more (that is, they have a different role and are used differently in long mode). Look into y86 and simulators. For ARM (i.e. tablets or RaspBerryPi), things are different.

On Linux/x86-64 segment registers are nearly unused in user land (except for thread local storage).

If you use GCC, compile a C program foo.c with gcc -fverbose-asm -O2 -S foo.c -o foo.s then look inside the generated foo.s assembler file.

So in practice, not.

For more read the Dragon book, and wikipage about x86 calling conventions and application binary interfaces.

I know gcc compiler also use segments

no, but ELF files have segments and sections.

Then refer to elf(5), syscalls(2) and this question and Advanced Linux Programming. Read the ABI specification and the Linux Assembler HowTo. Consider also using libgccjit or SBCL if you want to generate machine code at runtime. Or generate C code at runtime (see my manydl.c as an example), compile it as a plugin or shared object, and use dlopen(3) and dlsym(3).

You might look into the source code of simple open source C compilers such as tinycc or nwcc (and later inside GCC and Frama-C).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 2
    The segment registers DS, ES, etc, do still exist in long mode, but their functionality is much reduced and they're rarely used explicitly, except that FS and GS are often used for things like thread-local storage. – Nate Eldredge Aug 23 '20 at 02:45
  • The question was tagged Linux – Basile Starynkevitch Aug 23 '20 at 07:34
  • Yes, and GNU/Linux uses segment registers the same way in both i386 and x86-64: flat memory model plus FS as a TLS base. (Or GS for 32-bit code). So the fact that x86-64 neutered segmentation and enforces a flat memory model had no effect on how Linux used them. And more importantly, it's just plain wrong to say they're gone. You can still `mov ds, eax` in user-space and it will still segfault if you do that with a selector value outside the GDT or LDT. And it's *necessary* for CS to have a selector value that selects a 64-bit code segment descriptor in the GDT (or LDT) for x86-64 to work. – Peter Cordes Aug 23 '20 at 20:02
  • TL:DR: I think your first paragraph should probably be removed because it doesn't aid understanding of your later points. It also contradicts your 2nd paragraph (stating that a segment reg is used for TLS, despite earlier saying they don't exist.) – Peter Cordes Aug 23 '20 at 20:04