Graphics Programs Reference
In-Depth Information
With the addresses of the variables displayed, it is apparent that the
static_var in main() is different than the one found in function() , since they are
located at different memory addresses ( 0x804968c and 0x8049688 , respectively).
You may have noticed that the addresses of the local variables all have very
high addresses, like 0xbffff814 , while the global and static variables all have
very low memory addresses, like 0x0804968c and 0x8049688 . That's very astute
of you—noticing details like this and asking why is one of the cornerstones of
hacking. Read on for your answers.
0x270
Memory Segmentation
A compiled program's memory is divided into five segments: text, data, bss,
heap, and stack. Each segment represents a special portion of memory that is
set aside for a certain purpose.
The text segment is also sometimes called the code segment . This is where
the assembled machine language instructions of the program are located.
The execution of instructions in this segment is nonlinear, thanks to the
aforementioned high-level control structures and functions, which compile
into branch, jump, and call instructions in assembly language. As a program
executes, the EIP is set to the first instruction in the text segment. The
processor then follows an execution loop that does the following:
1.
Reads the instruction that EIP is pointing to
2.
Adds the byte length of the instruction to EIP
3.
Executes the instruction that was read in step 1
4.
Goes back to step 1
Sometimes the instruction will be a jump or a call instruction, which
changes the EIP to a different address of memory. The processor doesn't
care about the change, because it's expecting the execution to be nonlinear
anyway. If EIP is changed in step 3, the processor will just go back to step 1
and read the instruction found at the address of whatever EIP was changed to.
Write permission is disabled in the text segment, as it is not used to store
variables, only code. This prevents people from actually modifying the pro-
gram code; any attempt to write to this segment of memory will cause the
program to alert the user that something bad happened, and the program
will be killed. Another advantage of this segment being read-only is that it
can be shared among different copies of the program, allowing multiple
executions of the program at the same time without any problems. It should
also be noted that this memory segment has a fixed size, since nothing ever
changes in it.
The data and bss segments are used to store global and static program
variables. The data segment is filled with the initialized global and static variables,
while the bss segment is filled with their uninitialized counterparts. Although
these segments are writable, they also have a fixed size. Remember that global
variables persist, despite the functional context (like the variable j in the
previous examples). Both global and static variables are able to persist
because they are stored in their own memory segments.
Search WWH ::




Custom Search