4

I can't make sense of what's stored in the text segment. What I found so far says that it contains executable code (in 0s and 1s) of my program. But does not the whole memory of the program contain executable code(ie written in 0s and 1s)? I mean, a program is first compiled, then assembled, and finally linked, and thus, once we run it, it's all in binary. So, once a program runs, stack frames are made in the stack for current executing functions, and in these stack frames all the variables are in 0s and 1s, but what's inside the text segment?

Root149
  • 389
  • 1
  • 3
  • 11
  • Well, the "0's and 1's" part is already not true. Some of my programs contain 2s, 3s, and so on -- all the way up to 255. – Jongware Dec 12 '14 at 22:17
  • clcto that doesn't answer my question – Root149 Dec 12 '14 at 22:18
  • Wait, does that area contain all the functions' prototypes? – Root149 Dec 12 '14 at 22:20
  • In *compiled* code? Of course not! Function prototypes are a compiler construction only. – Jongware Dec 12 '14 at 22:23
  • 1
    @Root149: The answer to the suggested duplicate question does answer your question: _The 'text' segment of a program on Unix systems is the code — the machine code, the functions that make up the program (including, in particular, `main()` if the program is written in C or C++). It can also include read-only data._ That's what you wanted to know. And yes, all the code and data in the executable can be viewed as binary data. Some parts of the binary data can be interpreted as textual data -- character strings, for example, are stored in binary but can be viewed as text. – Jonathan Leffler Dec 12 '14 at 22:47
  • You may also find [How are the different segments like heap, stack, text related to the physical memory?](http://stackoverflow.com/questions/9226088/how-the-diff-segments-like-heap-stack-text-section-are-related-to-the-physical) useful. – Jonathan Leffler Dec 12 '14 at 23:05
  • 1
    "text" is a old naming convention used for the code section (sometimes called segment) of a program. "data" is used for data usually initialized data. "stack" is used for the stack. "bss" stands for block started by symbol, and is used for uninitialized data (which may be zeroed). Another term, mostly for mainframes, is "bes" which stands for block ended by symbol, which was an alternative for stack or stack like data. – rcgldr Dec 12 '14 at 23:06

2 Answers2

4

The (somewhat oddly named) text section (segment) usually contains the compiled binary code, as you suspect.

An executable often contains other types of data besides the code, like initialized variable values, resources, debug information, relocation data, and so on, which are placed in sections with other names.

  • 1
    So, in the text segment are contained the actual function definitions written in binary? – Root149 Dec 12 '14 at 22:43
  • 1
    @Root149: Yes, the text segment contains the compiled bodies of the functions that make up the program. It may also contain other material: if you didn't use shared libraries, then it may contain the code from library functions too; it may also contain read-only data, such as string literals, because the text segment is made read-only (no self-modifying code). – Jonathan Leffler Dec 12 '14 at 22:45
2

In the '.text' segment the compiler is putting the '1's and '0's generated from the program instructions and encoding these instructions, and it is typically read-only and executable. As opposed to the 'data' segment, which contains the ones and zeros encoding your variable values, and it is typically RW and not executable. The stack is commonly residing in the data segment, because you are not pushing there any instructions, only data. In the physical memory the text segment, and the data segment (and some other sections) may reside in totally different locations, that are typically not even contiguous. Even though they are contiguous in your binary. It means, that your 'text' segment can reside in the address 0x100, but the data in 0x1000000. But it does'n mean, that your executable will be 0x1000000 large.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61