-2

When we write a particular code in C, that code gets allocated to either data memory or code memory. When are those memory initialised, at run time or compile time. Any possible explanation of why they are initialised that way?

  • this is way too general question. But in very general manner, yes of course everything is allocated somewhere, about your why, you better read in wiki or do a course\degree, I don't know even where to begin – Green Dec 12 '17 at 22:26
  • "Code", by its definition, *never* gets stored in a .data segment. Data segments are usually prevented against execution. Also, pure data may be stored in a .code segment – string literals, but also jump tables. The very code itself can also be data in itself. Think of a disassembler disassembling its own code. – Jongware Dec 12 '17 at 22:43

1 Answers1

0

Most of .code comes from the binary image (as you might expect), the loader does make some changes before the program runs (writing the actual addresses of imported functions to an import table for example). On a system with hardware level memory control the pages that .code is loaded to will be marked read-only (and executable if the hardware gives that level of control).

.data also comes from the program binary, but those pages are marked read-write and non-executable (except for .rodata which is read-only)

.bss does not come from the binary, it simply gets allocated and initialized to 0 (this is the stack lives.

After finishing the load the real program entry point runs (not main), this sets up the environment, runs initializers calls main runs destructors and any final tear-down code the platform needs.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23