In looking at some assembly, I have come across a few instructions using segment prefixes. For example

<address>: ....  mov eax,DWORD PTR gs:0x20

I understand the basics of memory segments, but how could I get the 'absolute' memory address (particularly in GDB) of the data being accessed here? For example, I may want to break on further access, or examine the memory in that area.

Edit: I know how to get the value of $gs, but not sure how to use that information with the given instruction to get the actual address of memory. And yes, this is under Linux.

  • 3
    You should specify what operating system you are targeting because the usage of these register is OS dependent (though I suppose this is a Unix-based system because you refer to GDB).
    – perror
    Commented Jul 11, 2014 at 8:09
  • 1
    Are you ask how you get the value of GS or how to use that value to make absolute address? Commented Jul 11, 2014 at 13:43

1 Answer 1


On Linux, in protected mode, the segment registers aren't standard "segments" anymore, instead, they're called selectors, and include information if the segment is readable/writable/executable. The real address they're pointing to is "hidden" in a table in the kernel and the segment register is used as an index into that table, but the physical address shouldn't matter to you much anyway, since your application deals with virtual memory only, and the virtual address that gs maps to isn't even accessible in your "normal", ds/ss-referenced, virtual address space.

Linux uses the gs register to make some per-cpu-information available to the process. The special offset 0x20, which you're using in your example, is initialized to some random value, which the compiler reads whenever it enters a function and writes on the stack, and checks when the functions terminates, to detect buffer overflows.

Read more about the overflow protection on wikipedia, and more about the percpu structure in the linux kernel source.

  • 1
    On Windows, you can use GetThreadSelectorEntry() in user-mode to get the base address of a segment. But as @guntram-blohm said above, it looks like there's no such user-mode functionality in Linux. Commented Jul 11, 2014 at 18:11
  • In his particular example, as well, it looks like it's just the glibc stack canary code anyways, although thread local storage uses fs or gs typically as well.
    – broadway
    Commented Jul 12, 2014 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.