John Yang
John Yang

Reputation: 1370

Question about the explanation of data segment on Wikipedia?

Following yesterday's question, I did some research and I thought I had a more clear picture of linux process memory map.

I think one reason of my initial confusion is the incorrect explanation on Wikipedia, which claims heap is a part of data segment, which is obviously wrong. Also, it claims that data segment is not read-only and thus different from Rodata.

However, my understanding is that data segment contains rodata, BSS, and data, can anybody confirm my understanding? (It'd be even better if an expert could rewrite the Wikipedia article.)

Upvotes: 0

Views: 264

Answers (2)

Mike Seymour
Mike Seymour

Reputation: 254771

"Segments" are a rather old-fashioned concept, dating back to when modern paged-memory architectures weren't widely used. Segmented architectures forced quite a rigid memory layout, whereas paged memory allows the process to have many separate regions of virtual memory, each with its own access restrictions.

A Linux process has a text (or code) region containing the executable's code (initialised from the text section of the executable), and a data region containing runtime data (initialised from the data, bss and (perhaps) rodata sections of the executable). These regions correspond (more or less) to the old-fashioned text and data segments. It will also have a stack, and may also have access to other regions of memory, for example memory-mapped files and code from dynamic libraries.

[the article] claims heap is a part of data segment, which is obviously wrong

It's not necessarily wrong. The heap can be created either by extending the data segment (using the brk() system call), or by creating new memory regions (using mmap() to create anonymous mappings), or a combination of both. Heap space created by the first method is part of the data segment, although in that case the article is incorrect in stating that the segment has fixed size.

Also, it claims that data segment is not read-only and thus different from Rodata. However, my understanding is that data segment contains rodata, BSS, and data.

The article is slightly confused here; you cannot compare a segment (a process's memory region) with a section (part of an executable file). Read-only data can be protected by putting it in a separate, write-protected region rather than the writable data region. Modern desktop/server operating systems will do this (typically by mapping the rodata section of the file directly into memory); simpler systems may not have a mechanism for write-protecting memory, and so will be more likely to place it in the data segment.

A good way to see how memory is laid out in a Linux process is to look at the /proc/<PID>/maps file. This will show the virtual address range, access restrictions, and mapped file (if there is one) for each region available to the process.

Upvotes: 2

Puppy
Puppy

Reputation: 147054

There's no segmentation in modern desktop operating systems at all. The memory has a flat model. Anything you find discussing sections is referring to the binary executable format- not the process or operating system at all.

Upvotes: 0

Related Questions