Vous êtes sur la page 1sur 17

1) Declaration vs.

Definition of a variable

Declaration :
- States the type + name of the variable
- Doesn't automatically allocate storage
=> A Declared Variable is not created/allocated

Definition :
- Causes storage -memory- to be allocated by compiler
- Initializes allocated memory with a value
=> A Defined Variable is created/allocated

Definition != Initialization (part of definition)

a) Internal/Local variable : (declared + defined inside a block of code : {})

-- <type> <varName>;
=> [declaration]
=> no memory allocated

-- <type> <varName> = <value>;


=> [declaration] + [explicit definition]
=> memory allocated explicitly
memory initialized explicitly with value

-- <type> <varName>;
[varName used i.e. varName++];
=> [declaration] + [implicit definition : variable
used]
=> memory allocated implicitly
=> memory initialized implicitly with garbage
(previous content of allocated memory)
[usage requires allocation to manipulate variable]
b) External/Global variable : (defined outside of any block of data)

-- extern <type> <varName>;


=> [external declaration]
Inside block of code (functions)
or
At the top of module
=> Informs that the variable was defined globally
elsewhere
=> no memory allocated

-- <type> <varName>;
=> [definition]
=> memory allocated explicitly
memory initialized implicitly with 0s

-- <type> <varName> = <value>;


=> [definition]
memory allocated explicitly
memory initialized explicitly with value

Internal/Local variables are always declared, and may not be defined


External/Global variables are always defined exactly once, never just declared

2) Variables

a)-- Internal / Local / Automatic Variables (without 'static' attribute)


-> [Always Declared] + [Defined or not, explicitly or implicitly] inside block of code {} (i.e. inside a
function)

* Come into existence when the function is called, and disappear/are destroyed when the function
is exited
Do not retain their value from one function call to another
=> Lifetime = Function execution
* Can be accessed only inside the block where it was declared
=> Scope/Visibility = Inside block (None)
* Are allocated on the Stack [Part of the Process], at execution-time
* Don't have a label

-> In theory, using a Compiler with no optimizations :

* In Assembly Code : (assembly file .s)


-- Compiler instructs the Assembler to allocate memory in the Program Stack Section
for any declared + defined local variable (explicitly or implicitly)
=> i.e. sub sp, <value> : value being total number of bytes
required by the declared + defined
local variables
-- Compiler instructs the Assembler to initialize 'explicitly defined local
variables' with
values in code and 'implicitly defined variables' with garbage
=> i.e. mov DWORD PTR [bp-4], <value> : value being the
initialization value
in code

* In Binary Code : (object file .o)


-- Assembler translates the instructions and instructs CPU to make room in the
Program Stack Section for the (declared + explicitly defined local variables)
and for
(declared + implicitly defined local variables), and to initialize them
(with proper values or with garbage : previous content)

* Finally, at Execution Time : Automatic Allocation


-- When the function is called:
** Necessary amount of memory is allocated in the stack, creating thus the
variables memory locations
** Memory locations either initialiazed with values defined in code if
explicitly defined or with garbage -previous content- if
implicitly defined
-- When the function is terminated:
** The amount of memory allocated in the stack is freed, destroying thus
the variables
-> In practice, using a Compiler with optimizations : (i.e. alignment)

* In Assembly Code : (assembly file .s)


-- Compiler instructs the Assembler to make room in the Program Stack Section for
any
declared + defined local variable (explicitly or implicitly)
=> i.e. sub sp, <value> : value being number of bytes required
by the type of local variables

Actually, if there is at least (1 declared + defined variable),


value = default function stack size multiple (16, 32...)

-- Compiler instructs the Assembler to initialize 'explicitly defined local


variables' with
values in code and 'implicitly defined variables' with garbage
=> i.e. mov DWORD PTR [bp-4], <value> : value being the
initialization value

* In Binary Code : (object file .o)


-- Assembler translates the instructions and instructs CPU to make room in the
Program Stack Section for the (declared + explicitly defined local variables)
and for
(declared + implicitly defined local variables), and to initialize them
(with proper values or with garbage : previous content)

* Finally, at Execution Time : Automatic Allocation


-- When the function is called:
** Necessary amount of memory is allocated in the stack, creating thus the
variables memory locations
** Memory locations either initialiazed with values defined in code if
explicitly defined or with garbage -previous content- if
implicitly defined
-- When the function is terminated:
** The amount of memory allocated in the stack is freed, destroying thus
the variables
b)-- External / Global Variables
-> [Always Defined] + [Explicitly or Implicitly initialized] outside any block of code
By default, an external variable not explicitly initialized is implicitly init. by compiler (init
with 0s)
=> Storage always allocated for a declared External Variabled

* Come into existence from the start of the Program Execution, remain in existence permanently
until
the program exits
Retain their values between multiple functions that access/use them
=> Lifetime = Program execution
* With 'static' attribute : Accessible by name, usable and redefinable by only functions in the
same module
=> Scope/Visibility with static attribute = Internal
* Without 'static' attribute : Accessible by name, usable and redefinable by:
* all function in the same module
* all functions in external modules provided they are declared in them using extern
declaration
=> Scope/Visibility without static attribute = External
* Are allocated and stored in data section [part of the Executable File], at compilation-time
* Have a label

An External/Global Variable must be :

* Defined exactly once outside of any function (extern definition), in a module (source file, not
header)
(either initialized explicitly or implicitly)
-> <type> <varName>; or <type> <varName>= <value>;
=> Storage allocated + initialization + label defined (= varName)

* Declared to be usable by functions of a module :


-- If functions using it are in the same module as where Global Variable was defined
-> Each function can declare it in its body explicitly using extern declaration
statement
=> extern <type> <varName>;
or
-> Each function can declare it implicitly by omitting the extern declaration and
just
using the global variable

-- If functions using it are in another external module


-> Each function can declare it in its body explicitly using extern declaration
statement
=> extern <type> <varName>;
or
-> Each module containing functions that use it can declare it at the top, outside
of all
functions, using extern declaration statement
Functions can therefore use it directly
=> extern <type> <varName>;

Multiple modules need to use the same Global variables

1-Gather all Global Variables Extern declarations in a separate file (called header, .h, .hpp)
2-Define these Global variables once in exactly one module - source file -(init explicitly or
implicitly)
3-Include the header into all modules that need it
4-Use the Global Variables directly in functions (Implicit declaration)

3) Functions
- In C, all functions are external by default (unless static attribute used)
=> Scope/Visibility = External (no static attribute)
(accessible to all modules, provided they use extern declaration
otherwise => Implicit declaration warning)
Internal (static attribute)
(accessible only inside the module)
=> Lifetime = Program Execution
4) Storage Classes in C :

- Define the Scope (Visibility) + Lifetime of variables + functions


- Precede type of variable / return type of function
-> <storage_class> <type> <variable_name>
<storage_class> <return_type> <function_name>
- 4 types :

* auto
- Default storage class for local variables
- Can only be used with Local Variables - within blocks {} : i.e. inside functions
-> Local variable var1 declared + defined (see 1-a) inside func1 using auto
(following is done by compiler)
* is Created/Allocated only when func1 is called
is Destroyed/Deallocated only when func1 exists
=> Lifetime = Function Execution
* is Allocated/Stored in Program's Stack Section/Segment
in Memory (RAM) at Execution-Time : Automatic Allocation
* Has a memory location
=> can be referenced using & to access location
* is Visible only inside func1
=> Scope/Visibility = None, inside block
* If func1 is called multiple times, var1 doesn't keep its value between
the calls : it is recreated and reinitialized at each function
call

* register
- Can only be used with Local Variables - within blocks {} : i.e. inside functions
- Can only be used for variables that have a maximum size = register size
- Usually used for variables that require quick access
-> Local variable var1 declared + defined (see 1-a) inside func1 using register
(following is done by compiler)
* is Created/Allocated only when func1 is called
is Destroyed/Deallocated only when func1 exists
=> Lifetime = Function Execution
* hints the compiler to Allocate/Store/Cache the variable in a register
at Execution-Time (not an obligation) i.e. mov ebx, <value>
* Doesn't have a memory location
=> cannot be referenced using & to access location
* is Visible only inside func1
=> Scope/Visibility = None, inside block
* If func1 is called multiple times, var1 doesn't keep its value
between the calls : it is recreated and reinitialized at each
function call

* static
- Can be used for Local Variables, Global Variables and Functions

* Static Local variable (inside a block of code {} i.e. Function)


-- is Created/Allocated at Program Execution
is Destroyed/Deallocated when all the program terminates
=> Lifetime = Program Execution
-- is Allocated/Stored in Program's Data Section/Segment in Memory (RAM) at
Compilation-Time : Static Allocation
-- is Visible only inside func1
=> Scope/Visibility = None, inside block

* Static Global Variable


-- is Created/Allocated at Program Execution
is Destroyed/Deallocated when all the program terminates
=> Lifetime = Program Execution
-- is Allocated/Stored in Program's Data Section/Segment in Memory (RAM) at
Compilation-Time : Static Allocation
-- is Visible only inside the module (source file) where it was decl.
=> Scope/Visibility = Internal
(inside the module only, invisible to external modules)

Static Allocation : variable occupies the same memory location throughout all
program's execution (in data section/segment),
determined at Compilation
variable keeps its value between multiple function calls : not
reinitialized at each call

* Static Function
-- is Created/Allocated at Program Execution
is Destroyed/Deallocated when all the program terminates
=> Lifetime = Program Execution
-- is Allocated/Stored in Program's Text Section/Segment at Compilation-Time
-- is Visible only inside the module (source file) where it was decl.
=> Scope/Visibility = Internal
(inside the module only, invisible to external modules)
* extern (see 2-b)
- Can be used for Global Variables and Functions
- Used in "extern declaration" syntax

* extern Global Variable :


-- called 'extern declaration'
-- used in a module to declare a global variable that has been defined in
another module
-- see 1-b
-- doesn't allocate memory for the variable
(memory already allocated by the definition of global variable)

* extern Function :
-- called 'extern declaration'
-- used in a module to declare a function that has been defined in another
module
-- informs that the function has been defined elsewhere
-- doesn't allocate memory for the function
(memory already allocated by the definition of function)

Lifetime = Program Execution


Scope/Visibility = External (whole program)

5) Symbols :
= Names of Variables + Functions in a Module (Source File -> Assembly File -> Relocatable Obj. File)
-> Global Symbol => Visible to all modules
-Global Variables without static attribute
-Functions without static attribute
-> Local Symbol => Visible only inside the module itself
-Static { Global Variables + Local Variables }
-Static Functions
3 types of Symbols :

* Defined Global Symbols (without 'static' attribute)

- Declared in a module and can be referenced : used(var), called(func) by external modules

-> C Functions without 'static' attribute are visible to external modules (who use extern
decl.)
=> in .text section or _TEXT SEGMENT
using .globl or PUBLIC directive
Function Name as a Label:

-> C Global Variables without 'static' attribute are visible to external modules (who use
extern de)

---> Without 'const' attribute

** Initialized != 0 => in .data section or _DATA SEGMENT


using .globl or PUBLIC directive
Variable Name as a Label:
** Initialized = 0 => in .bss section or _BSS SEGMENT
using .globl or PUBLIC directive
Variable Name as a Label:
** Uninitialized => in .data section or _DATA SEGMENT
using .comm or COMM directive

---> With 'const' attribute

** Initialized => using .section .rodata or .rdata directive


using .globl directive
Variable Name as a Label:
in CONST SEGMENT
using PUBLIC directive
** Uninitialized => using .section .rodata or .rdata directive
using .comm
in _DATA SEGMENT using COMM directive

* Defined Local Symbols (with 'static' attribute) (see 4-Static Allocation)


- Declared in a module locally => can only be referenced inside the module itself
cannot be referenced by external modules

-> C Functions with 'static' attribute are visible only inside the module itself
=> in .text section or _TEXT SEGMENT
no use of .globl or PUBLIC directives
Function Name as a Label:

-> C Global Variables with 'static' attribute are visible only inside the module itself

---> Without 'const' attribute

** Initialized != 0 => in data section / _DATA SEGMENT


no use of .globl or PUBLIC directives
Variable Name as a Label:
** Initialized = 0 => ?in .text/.data section
using .lcomm / .local + .comm directives
no use of .globl
Variable Name as a Label:
in _BSS SEGMENT
no use of PUBLIC
** Uninitialized => ?in .data section
using .lcomm or .local + .comm directives
no use of .globl or PUBLIC directives
in _BSS SEGMENT section
no use of .globl or PUBLIC directives

---> With 'const' attribute

** Initialized => in .section .rodata or .section .rdata,"dr" section


no use of .globl or PUBLIC directives
Variable Name as a Label:
in CONST SEGMENT
no use of .globl or PUBLIC directives

** Uninitialized => in .section .rodata or .section .rdata,"dr" or


.text section
using .lcom or .local + .comm directives
no use of .globl or PUBLIC directives

-> C Local Variables with 'static' attribute are allocated for the whole program
execution
---> Without 'const' attribute

** Initialized != 0 => in data section / _DATA SEGMENT


Variable Name as a Label:
** Initialized = 0 => using .lcomm / .local + .comm directives
no use of .globl
Variable Name as a Label:
in _BSS SEGMENT
no use of PUBLIC

** Uninitialized => ?in .text/.data section


using .lcomm or .local + .comm directives
or in _BSS SEGMENT section

---> With 'const' attribute

** Initialized => in .section .rodata or .section .rdata,"dr" section


Variable Name as a Label:
or in CONST SEGMENT section

** Uninitialized => ?in .section .rodata or .section .rdata,"dr"


section
using .lcom or .local + .comm directives

N.B : - 'Non-static' Local Variables (to a Static and Non-Static Func) (initialized
and not) are allocated in the Stack and never in data section

* Referenced Global Symbols


- Referenced only : used(vars), called(funcs) in Source File, Declared + Initialized in another
one
-> C Functions + Global Variables defined in other modules, accessible to this module
(defined without 'static' attribute)
A module (Source File / Preprocessed Source File / Assembly File / Relocatable Object File) contains 3 types of
Symbols :

i) Defined Global Symbols


-> Global Non-Static variables
-> Non-Static functions

=> Can be referenced by any other module, provided it uses extern declaration

ii) Defined Local symbols


-> Global Static variables
-> Static functions
-> Local Static variables

=> Can be referenced only by the module itself

iii) Referenced Global Symbols


-> Global Non-Static variables defined in another module, declared in the module using
"extern"
-> Non-Static functions declared + defined in another module, declared in the module using
"extern"

All these symbols will be allocated memory locations by linker and other tools in Text or Data section of the
resulting binary
6) Sections in Assembly File (.s, .asm...)

- Depend on the :
- Compiler Driver in use (i.e. Assembler)
- File Format in use (ELF for Unix-Like and BSD-Like OSes, PE for Windows, Mach-O for Mac-OS X)

- 3 Default / Usual sections :


* Text (.text/_TEXT) section => Contains executable code
.text assembles following code/symbols into Text Section
* Data (.data/_DATA) section => Contains initialized data
.data assembles following into "Initialized data" Data
Section
* BSS (.bss/_BSS) section => Contains uninitialized data
.bss reserves space in "Uninitialized data" BSS Section

Additional sections :
* Symbol Table Section (.symtab) => Contains information about all symbols in the Module
* Common (.comm/COMM) Section => Contains symbols created with .comm or COMM directives
* Named sections => Sections created using .section directive
(i.e. .rodata, CONST SEGMENT and other sections)

- Sections are described in the Section Header Table of Assembly File (.s, .asm)

7) Raw Sections in Relocatable Object File (.o, .obj...)

- Depend on the :
- Compiler Driver in use (i.e. Assembler)
- File Format in use (ELF for Unix-Like and BSD-Like OSes, PE for Windows, Mach-O for Mac-OS X)
- Same sections as in the corresponding assembly file
<=> same content of the Assembly file, but in binary form

Sections : exist before linking, in each module (Assembly File : in assembly language / Relocatable Object
File : in binary)
Each Section has a Section Header
All the Sections Headers form the Section Header Table

Segments : exist after linking, in the finale Executable file


are detailed in the Segment/Program Header Table by the Linker (ELF Header, PE Header)
The Segment/Program Header Table contains information about :
- how each segment should be loaded into the process virtual memory by the OS (location and
permissions)
- which sections of the object files will go into which segments of the executable
- relocations

+-------------------+
| ELF header |---+
+---------> +-------------------+ | e_shoff
| | |<--+
| Section | Section header 0 |
| | |---+ sh_offset
| Header +-------------------+ |
| | Section header 1 |---|--+ sh_offset
| Table +-------------------+ | |
| | Section header 2 |---|--|--+
+---------> +-------------------+ | | |
| Section 0 |<--+ | |
+-------------------+ | | sh_offset
| Section 1 |<-----+ |
+-------------------+ |
| Section 2 |<--------+
+-------------------+

- All sections are organized into 3 loadable segments which make up the Relocatable Object File
-> Text Segment
-> Data Segment
-> BSS Segment
Each segment contains 1 or more sections

- Sections are regrouped into segments


- Relocatable object files actually contain sections
* Code Segment : Text Section + other Names Sections (i.e. .rodata)
* Data Segment : Data Section + BSS Section + other Named Sections (i.e.

* Stack Segment:
* Heap Segment :

Vous aimerez peut-être aussi