Data and Program Representation Flashcards
How is memory seen by the program?
As an array of bytes.
What is the address range of memory in bytes?
0 - (2^64 - 1). * this is for 64 bit architecture
Is all of the address space of memory used?
No
What are the sections in memory called?
memory mappings
What are the memory mappings, in order form lowest to highest in address range.
Text, RoData, Data, BSS, Heap - Shared libs - Stack
What does the text memory section hold?
Instructions that the program runs
What does the data memory section hold?
Initialized global variables.
What does the Bss memory section hold?
Uninitialized global variables. They are
initialized to zeroes.
What does the Heap memory section hold?
Memory returned when calling malloc/new. It grows upwards.
What does the Stack memory section hold?
It stores local variables and return
addresses. It grows downwards.
What does the RoData memory section hold?
Read Only Data. String constants
Which way does the Stack grow?
The stack grows downwards.
Which was does the heap grow?
The heap grows upwards
What are Dynamic Libraries?
They are libraries shared with other processes.
What does each dynamic library have its own of?
text, data, and bss.
True or False, Each program (process) has its own view of the memory that is independent of each other.
True
True or False, If a process modifies a byte in its own address space, it will modify the address space of another process.
False
Below, where is line x stored?
Program hello.c
int a = 5; // Line x
int b[20];
int main() {
int x;
int p;
p =(int) malloc(sizeof(int));
*p = 5;
printf(“Hello cs250\n”);
}
data section
Below, where is line x stored?
Program hello.c
int a = 5;
int b[20]; // Line x
int main() {
int x;
int p;
p =(int) malloc(sizeof(int));
*p = 5;
printf(“Hello cs250\n”);
}
BSS
Below, where is line x stored?
Program hello.c
int a = 5;
int b[20];
int main() { // Line x
int x;
int p;
p =(int) malloc(sizeof(int));
*p = 5;
printf(“Hello cs250\n”);
}
Text
Below, where is line x stored?
Program hello.c
int a = 5;
int b[20];
int main() {
int x; //Line x
int p;
p =(int) malloc(sizeof(int));
*p = 5;
printf(“Hello cs250\n”);
}
Stack
Below, where is line x stored?
Program hello.c
int a = 5;
int b[20];
int main() {
int x;
int p;
p =(int) malloc(sizeof(int)); //Line x
*p = 5;
printf(“Hello cs250\n”);
}
Heap
What is a memory gap?
A space between memory sections where there is no memory mapping
What happens if a program tries to access a memory gap?
The OS will send a SEGV signal that by default kills the program and dumps a core file.
What does the “core file” contain?
The core file contains the value of the variables global and local at the time of the SEGV.
What file can be used for “post mortem” debugging?
The core file.
What is a program?
a file in a special format that contains
all the necessary information to load an application into memory and make it run
What does a program file include?
● machine instructions
● initialized data
● List of library dependencies
● List of memory sections that the program will use
● List of undefined values in the executable that will be
known when the program is loaded into memory.
True or false, there are different executable file formats?
True
What is ELF – Executable Link File used in?
It is an executable file format used in most UNIX systems (Solaris, Linux).
What is COFF – Common Object File Format used in?
It is used in Windows systems
What is a.out used in?
Used in BSD (Berkeley Standard Distribution) and early UNIX
It was very restrictive. It is not used anymore.
What are the steps to building a program? (extensive maybe split up flashcard)
● The programmer writes a program hello.c
● The preprocessor expands #define, #include,
#ifdef etc preprocessor statements and generates a
hello.i file.
● The compiler compiles hello.i, optimizes it and
generates an assembly instruction listing hello.s
● The assembler (as) assembles hello.s and
generates an object file hello.o
● The compiler (cc or gcc) by default hides all these
intermediate steps. You can use compiler options to
run each step independently.
Building a program
● The linker puts together all object files as well as
the object files in static libraries.
● The linker also takes the definitions in shared
libraries and verifies that the symbols (functions
and variables) needed by the program are
completely satisfied.
● If there is symbol that is not defined in either the
executable or shared libraries, the linker will give
an error.
● Static libraries (.a files) are added to the
executable. shared libraries (.so files) are not
added to the executable file.
What line generates a hello executable?
“gcc –o hello hello.c”
What are the steps to loading a program? (extensive maybe split up flashcard)
● The loader is a program that is used to run an
executable file in a process.
● Before the program starts running, the loader
allocates space for all the sections of the
executable file (text, data, bss etc)
● It loads into memory the executable and
shared libraries (if not loaded yet)
Loading a Program
● It also writes (resolves) any values in the executable
to point to the functions/variables in the shared
libraries.(E.g. calls to printf in hello.c)
● Once memory image is ready, the loader jumps to
the _start entry point that calls init() of all libraries
and initializes static constructors. Then it calls
main() and the program begins.
● _start also calls exit() when main() returns.
● The loader is also called “runtime linker” or “dynamic
linker”.
What is the difference between static and shared libraries?
●Shared libraries are shared across different
processes.
● There is only one instance of each shared
library for the entire system.
● Static libraries are not shared.
● There is an instance of a static library for
each process.
Do Static or Dynamic events happen during program building?
Static – Events that happen during
program building.
Example: Static linker,
Static type checking.
Do Static or Dynamic events happen while the program is running?
Dynamic – Events that happen while
program is running.
Also called “Runtime”.
Example: Dynamic linker or
Runtime linker,
Dynamic Type checking