SlideShare a Scribd company logo
Prepared by
Mohammed Sikander
Technical Lead
Cranes Software International Limited
int g_var = 5;
int main( )
{
int l_var = 8;
}
mohammed.sikander@cranessoftware.com 2
00000000 D g_var
00000000 T main
• Symbol table does not contain the
entries of local/automatic variables.
• gl is stored in Initialized Data Segment
• main is stored inText / Code Segment
int global = 5;
static int stglobal = 9;
int main( )
{
int local = 8;
static int stlocal = 3;
}
mohammed.sikander@cranessoftware.com 3
00000000 D global
00000000 T main
00000004 d stglobal
00000008 d stlocal.0
• d stands for Internal Linkage
• External variables will not have any
name mangling.
• Internal Linkage variables might have
name mangling.
1. Can we have two static
variables with same name
but different functions.
2. What is the memory
segment of x.
3. How does the compiler
avoid name clashes.
mohammed.sikander@cranessoftware.com 4
void print( )
{
static int x = 5;
}
void display( )
{
static int x = 8;
}
int main( )
{
}
00000005 T display
0000000a T main
00000000 T print
00000000 d x.0
00000004 d x.1
Compiler uses different names for x.
int x = 5;
int main( )
{
int x = 10;
printf(“x = %d n “ , x);
}
mohammed.sikander@cranessoftware.com 5
static int x = 5;
int main( )
{
static int x = 10;
{
static int x = 12;
printf(“x = %d n “ , x);
}
}
FILE1.C
int x = 5;
void func( );
int main( )
{
func( );
}
FILE2.C
int x = 8;
void func( )
{
printf(" x = %d n" , x);
}
mohammed.sikander@cranessoftware.com 6
• Compile file1.c
• Compile file2.c
• Link file1.o and file2.o
• Observe the output
mohammed.sikander@cranessoftware.com 7
• [sikander@localhost nm]$ gcc file1.c -c
• [sikander@localhost nm]$ gcc file2.c –c
• [sikander@localhost nm]$ gcc file1.o file2.o
• file2.o(.data+0x0): multiple definition of `x'
• file1.o(.data+0x0): first defined here
• collect2: ld returned 1 exit status
[sikander@localhost nm]$ nm file1.o
U func
00000000 T main
00000000 D x
[sikander@localhost nm]$ nm file2.o
00000000 T func
U printf
00000000 D x
FILE1.C
static int x = 5;
void func( );
int main( )
{
printf(__func__ );
printf(" x = %d n",x);
func( );
}
FILE2.C
static int x = 8;
void func( )
{
printf("%s x = %d n" ,__func__ , x);
}
mohammed.sikander@cranessoftware.com 8
• Compile file1.c
• Compile file2.c
• Link file1.o and file2.o
• Observe the output
mohammed.sikander@cranessoftware.com 9
• $ gcc -c file1.c
• $ gcc -c file2.c
• $ gcc file1.o file2.o
• $ ./a.out
• main x = 5
• func x = 8
$ nm file1.o
U func
00000000r __func__.0
00000000T main
U printf
00000000 d x
$ nm file2.o
00000000T func
00000000r __func__.0
U printf
00000000 d x
080495dc d x
080495e0 d x
FILE1.C
int x = 5;
void func( );
int main( )
{
printf(__func__ );
printf(" x = %d n",x);
func( );
}
FILE2.C
extern int x;
void func( )
{
printf("%s x = %d n" ,__func__ , x);
}
mohammed.sikander@cranessoftware.com 10
• Compile file1.c
• Compile file2.c
• Link file1.o and file2.o
• Observe the output
FILE1.C
int x = 5;
void func( );
int main( )
{
printf(__func__ );
printf(" x = %d n",x);
func( );
}
FILE2.C
extern int x;
void func( )
{
printf("%s x = %d n" ,__func__ , x);
}
mohammed.sikander@cranessoftware.com 11
• Compile file1.c
• Compile file2.c
• Link file1.o and file2.o
• Observe the output
mohammed.sikander@cranessoftware.com 12
• $ gcc -c file1.c
• $ gcc -c file2.c
• $ gcc file1.o file2.o
• $ ./a.out
 main x = 5
 &x = 0x8049618
 func x = 5
 &x = 0x8049618
]$ nm file1.o
U func
00000000 r __func__.0
00000000 T main
U printf
00000000 D x
]$ nm file2.o
00000000 T func
00000000 r __func__.0
U printf
U x
$nm a.out
08049618 D x
FILE1.C
static int x = 5;
void func( );
int main( )
{
printf(__func__ );
printf(" x = %d n",x);
func( );
}
FILE2.C
extern int x;
void func( )
{
printf("%s x = %d n" ,__func__ , x);
}
mohammed.sikander@cranessoftware.com 13
• Compile file1.c
• Compile file2.c
• Link file1.o and file2.o
• Observe the output
mohammed.sikander@cranessoftware.com 14
• $ gcc -c file1.c
• $ gcc -c file2.c
• $ gcc file1.o file2.o
• file2.o(.text+0xb): In function `func':
• : undefined reference to `x‘
• collect2: ld returned 1 exit status
]$ nm file1.o
U func
00000000 r __func__.0
00000000 T main
U printf
00000000 d x
]$ nm file2.o
00000000 T func
00000000 r __func__.0
U printf
U x
static int x = 5;
int main( )
{
int x;
printf(“x = %d n”,x);
}
static int x = 5;
int main( )
{
extern int x;
printf(“x = %d n”,x);
}
int a = 5;
int b;
static int c = 7;
static int d;
int main( )
{
}
• Mention the memory segments of variables after
compiling (.o file) and after linking (a.out)
• What is the difference between a and c.
• Use nm utility to view the memory segments
$gcc –c file.c
$nm file.o
$gcc file.c
$nm ./a.out
 $ gcc file.c -c
 $ nm file.o
 00000000 D a
 00000004 C b
 00000004 d c
 00000000 b d
 00000000 T main
mohammed.sikander@cranessoftware.com 18
•“C” The symbol is common.
• Common symbols are uninitialized
data.
• When linking, multiple common
symbols may appear with the same
name.
• If the symbol is defined anywhere, the
common symbols are treated as
undefined references.$ nm a.out
08049534 D a
08049544 B b
08049538 d c
08049540 b d
int x ;
int main( )
{
printf(“Main &x= %p” , &x);
printf(“x = %d n”, x);
func( );
}
int x ;
void func( )
{
printf(“Func &x= %p” , &x);
printf(“x = %d n”, x);
}
$ nm file1.o
U func
00000000 T main
U printf
00000004 C x
]$ nm file2.o
00000000 T func
U printf
00000004 C x
$nm a.out
08049600 B x
int x ;
int main( )
{
printf(“Main &x= %p” , &x);
printf(“x = %d n”, x);
func( );
}
int x = 5 ;
void func( )
{
printf(“Func &x= %p” , &x);
printf(“x = %d n”, x);
}
$ nm file2.o
00000000T func
U printf
00000000 D x
$ nm file1.o
U func
00000000 T main
U printf
00000004 C x
$nm a.out
08049600 D x
C C
B
C D
D
D D
Multiple Definition
d d
NoConflict, two different variables
b b
NoConflict, two different variables
d b
NoConflict, two different variables
b d
NoConflict, two different variables
b D
NoConflict, two different variables
void func( );
int x = 5; //D
int main( )
{
printf(“Main &x= %p” , &x);
printf(“x = %d n”, x);
func( );
}
int x = 10; //D
void func( )
{
printf(“Func &x= %p” , &x);
printf(“x = %d n”, x);
}
void func( );
static int x = 5; //d
int main( )
{
printf(“Main &x= %p” , &x);
printf(“x = %d n”, x);
func( );
}
int x = 10; //D
void func( )
{
printf(“Func &x= %p” , &x);
printf(“x = %d n”, x);
}
const int gc = 10;
static const int gsc = 12;
int main( )
{
const int lc = 5;
static const int lsc = 8;
}
00000000 R gc
00000004 r gsc
00000008 r lsc.0
00000000 T main
const int gc = 15; //ReadOnly
int main( )
{
const int lc = 5; //Stack
printf(“Enter the value for local const variable : “);
scanf(“ %d”,&lc);
printf(“LocalConst = %d n” , lc);
printf(“Enter the value for global const variable : “);
scanf(“ %d”,&gc);
printf(“GlobalConst = %d n”,gc);
}
[sikander@localhost ~]$ ./a.out
Enter the value for local const variable : 89
LocalConst = 89
Enter the value for global const variable : 6
Segmentation fault
 [sikander@localhost ~]$ nm f1.o
 00000000 t display
 0000000a T main
 00000005 T print
static void display()
{
}
void print()
{
}
int main( )
{
display( );
print( );
}
 For any queries and feedback
 Mail to
 Mohammed.sikander@cranessoftware.com
 sikander1248@gmail.com
mohammed.sikander@cranessoftware.com 26

More Related Content

What's hot

The Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignThe Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable Design
Victor Rentea
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
Udaya Kumar
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
Mohammed Arif
 
Learn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type ConversionLearn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type Conversion
Eng Teong Cheah
 
Html Study Guide
Html Study GuideHtml Study Guide
Html Study Guide
vergetec
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
SudhanshuVijay3
 
Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8
Victor Rentea
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
Dhani Ahmad
 
presentation in html,css,javascript
presentation in html,css,javascriptpresentation in html,css,javascript
presentation in html,css,javascript
FaysalAhammed5
 
Modern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptxModern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptx
Mohammed Sikander
 
Regular Expressions in Java
Regular Expressions in JavaRegular Expressions in Java
Regular Expressions in Java
OblivionWalker
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
Jayfee Ramos
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Nikhil Pandit
 
C operator and expression
C operator and expressionC operator and expression
C operator and expression
LavanyaManokaran
 
basics dart.pdf
basics dart.pdfbasics dart.pdf
basics dart.pdf
ssuser0ca68e
 
How-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptx
How-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptxHow-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptx
How-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptx
ishawrb
 
Python book
Python bookPython book
Python book
Victor Rabinovich
 
Operator Overloading in C++
Operator Overloading in C++Operator Overloading in C++
Operator Overloading in C++
Mohammed Sikander
 
C string
C stringC string

What's hot (20)

The Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignThe Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable Design
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
 
JavaScript & Dom Manipulation
JavaScript & Dom ManipulationJavaScript & Dom Manipulation
JavaScript & Dom Manipulation
 
Learn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type ConversionLearn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type Conversion
 
Html Study Guide
Html Study GuideHtml Study Guide
Html Study Guide
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
presentation in html,css,javascript
presentation in html,css,javascriptpresentation in html,css,javascript
presentation in html,css,javascript
 
Modern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptxModern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptx
 
Regular Expressions in Java
Regular Expressions in JavaRegular Expressions in Java
Regular Expressions in Java
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C operator and expression
C operator and expressionC operator and expression
C operator and expression
 
basics dart.pdf
basics dart.pdfbasics dart.pdf
basics dart.pdf
 
How-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptx
How-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptxHow-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptx
How-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptx
 
Python book
Python bookPython book
Python book
 
Operator Overloading in C++
Operator Overloading in C++Operator Overloading in C++
Operator Overloading in C++
 
C string
C stringC string
C string
 

Viewers also liked

Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3
Ilio Catallo
 
UPPS in health 20151021
UPPS in health 20151021UPPS in health 20151021
UPPS in health 20151021
CLICKNL
 
Pubblicazioni Chiave sull'Unione Europea
Pubblicazioni Chiave sull'Unione EuropeaPubblicazioni Chiave sull'Unione Europea
Pubblicazioni Chiave sull'Unione Europea
Luigi A. Dell'Aquila
 
Coaching Clinic Rusun Karang Anyar 6 Oct 2015
Coaching Clinic Rusun Karang Anyar 6 Oct 2015Coaching Clinic Rusun Karang Anyar 6 Oct 2015
Coaching Clinic Rusun Karang Anyar 6 Oct 2015
Uni Papua Football
 
Citizenship stage 2 health
Citizenship stage 2 healthCitizenship stage 2 health
Citizenship stage 2 healthJustin Chua
 
Anno Europeo dello Sviluppo
Anno Europeo dello SviluppoAnno Europeo dello Sviluppo
Anno Europeo dello Sviluppo
Luigi A. Dell'Aquila
 
C++ programming Unit 5 flow of control
C++ programming Unit 5 flow of controlC++ programming Unit 5 flow of control
C++ programming Unit 5 flow of control
AAKASH KUMAR
 
Química nos veículos automotores 3°4
Química nos veículos automotores 3°4Química nos veículos automotores 3°4
Química nos veículos automotores 3°4
Química Cool
 
Adfactors
Adfactors Adfactors
Adfactors
Priyadarshani Jain
 
Konsep dasar asuhan kehamilan poltekkes sby
Konsep dasar asuhan kehamilan poltekkes sbyKonsep dasar asuhan kehamilan poltekkes sby
Konsep dasar asuhan kehamilan poltekkes sby
Triana Septianti
 
[대학생 연합 해커톤 UNITHON 3RD] Mingginyu_ppt
[대학생 연합 해커톤 UNITHON 3RD] Mingginyu_ppt[대학생 연합 해커톤 UNITHON 3RD] Mingginyu_ppt
[대학생 연합 해커톤 UNITHON 3RD] Mingginyu_ppt
Junyoung Jung
 
Standar asuhan keperawatan
Standar asuhan keperawatanStandar asuhan keperawatan
Standar asuhan keperawatan
Sulistia Rini
 
askeb Bayi sehat dengan imunisasi campak
askeb Bayi sehat dengan imunisasi campakaskeb Bayi sehat dengan imunisasi campak
askeb Bayi sehat dengan imunisasi campak
Ratna Imas Indriyani (Ratna Fadhilah Al-mumtazah)
 
Arrays
ArraysArrays
UDI. CURSO Intef. Competencias Clave. el desarrollo y el impacto de la tecnol...
UDI. CURSO Intef. Competencias Clave. el desarrollo y el impacto de la tecnol...UDI. CURSO Intef. Competencias Clave. el desarrollo y el impacto de la tecnol...
UDI. CURSO Intef. Competencias Clave. el desarrollo y el impacto de la tecnol...
Juana Colomo
 

Viewers also liked (18)

Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3
 
UPPS in health 20151021
UPPS in health 20151021UPPS in health 20151021
UPPS in health 20151021
 
Corinna_Tutor_Resume_10_2
Corinna_Tutor_Resume_10_2Corinna_Tutor_Resume_10_2
Corinna_Tutor_Resume_10_2
 
Pubblicazioni Chiave sull'Unione Europea
Pubblicazioni Chiave sull'Unione EuropeaPubblicazioni Chiave sull'Unione Europea
Pubblicazioni Chiave sull'Unione Europea
 
Coaching Clinic Rusun Karang Anyar 6 Oct 2015
Coaching Clinic Rusun Karang Anyar 6 Oct 2015Coaching Clinic Rusun Karang Anyar 6 Oct 2015
Coaching Clinic Rusun Karang Anyar 6 Oct 2015
 
Citizenship stage 2 health
Citizenship stage 2 healthCitizenship stage 2 health
Citizenship stage 2 health
 
Anno Europeo dello Sviluppo
Anno Europeo dello SviluppoAnno Europeo dello Sviluppo
Anno Europeo dello Sviluppo
 
C++ programming Unit 5 flow of control
C++ programming Unit 5 flow of controlC++ programming Unit 5 flow of control
C++ programming Unit 5 flow of control
 
Química nos veículos automotores 3°4
Química nos veículos automotores 3°4Química nos veículos automotores 3°4
Química nos veículos automotores 3°4
 
Adfactors
Adfactors Adfactors
Adfactors
 
Konsep dasar asuhan kehamilan poltekkes sby
Konsep dasar asuhan kehamilan poltekkes sbyKonsep dasar asuhan kehamilan poltekkes sby
Konsep dasar asuhan kehamilan poltekkes sby
 
FMD 492 E Final Evaluation
FMD 492 E Final EvaluationFMD 492 E Final Evaluation
FMD 492 E Final Evaluation
 
[대학생 연합 해커톤 UNITHON 3RD] Mingginyu_ppt
[대학생 연합 해커톤 UNITHON 3RD] Mingginyu_ppt[대학생 연합 해커톤 UNITHON 3RD] Mingginyu_ppt
[대학생 연합 해커톤 UNITHON 3RD] Mingginyu_ppt
 
Standar asuhan keperawatan
Standar asuhan keperawatanStandar asuhan keperawatan
Standar asuhan keperawatan
 
askeb Bayi sehat dengan imunisasi campak
askeb Bayi sehat dengan imunisasi campakaskeb Bayi sehat dengan imunisasi campak
askeb Bayi sehat dengan imunisasi campak
 
Arrays
ArraysArrays
Arrays
 
Legno - 6
Legno - 6Legno - 6
Legno - 6
 
UDI. CURSO Intef. Competencias Clave. el desarrollo y el impacto de la tecnol...
UDI. CURSO Intef. Competencias Clave. el desarrollo y el impacto de la tecnol...UDI. CURSO Intef. Competencias Clave. el desarrollo y el impacto de la tecnol...
UDI. CURSO Intef. Competencias Clave. el desarrollo y el impacto de la tecnol...
 

Similar to Understanding storage class using nm

Interesting facts on c
Interesting facts on cInteresting facts on c
Interesting facts on c
Durgadevi palani
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
Janani Satheshkumar
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
2017eee0459
 
Python-GTK
Python-GTKPython-GTK
Python-GTKYuren Ju
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
Vikas Sharma
 
11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class웅식 전
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
EasyStudy3
 
Functions
FunctionsFunctions
Functions
Swarup Boro
 
Python GTK (Hacking Camp)
Python GTK (Hacking Camp)Python GTK (Hacking Camp)
Python GTK (Hacking Camp)Yuren Ju
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
Sami Said
 
Boosting Developer Productivity with Clang
Boosting Developer Productivity with ClangBoosting Developer Productivity with Clang
Boosting Developer Productivity with Clang
Samsung Open Source Group
 
C# 6.0 Preview
C# 6.0 PreviewC# 6.0 Preview
C# 6.0 Preview
Fujio Kojima
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manualSami Said
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
Bharat Kalia
 
Cbasic
CbasicCbasic
Cbasic
rohitladdu
 
Cquestions
Cquestions Cquestions
Cquestions
mohamed sikander
 
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1Little Tukta Lita
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
CharuJain396881
 

Similar to Understanding storage class using nm (20)

Interesting facts on c
Interesting facts on cInteresting facts on c
Interesting facts on c
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
Python-GTK
Python-GTKPython-GTK
Python-GTK
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Vcs15
Vcs15Vcs15
Vcs15
 
functions
functionsfunctions
functions
 
11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Functions
FunctionsFunctions
Functions
 
Python GTK (Hacking Camp)
Python GTK (Hacking Camp)Python GTK (Hacking Camp)
Python GTK (Hacking Camp)
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
 
Boosting Developer Productivity with Clang
Boosting Developer Productivity with ClangBoosting Developer Productivity with Clang
Boosting Developer Productivity with Clang
 
C# 6.0 Preview
C# 6.0 PreviewC# 6.0 Preview
C# 6.0 Preview
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 
Cbasic
CbasicCbasic
Cbasic
 
Cquestions
Cquestions Cquestions
Cquestions
 
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
 

More from mohamed sikander

C++ 11 range-based for loop
C++ 11   range-based for loopC++ 11   range-based for loop
C++ 11 range-based for loop
mohamed sikander
 
Pointer level 2
Pointer   level 2Pointer   level 2
Pointer level 2
mohamed sikander
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
mohamed sikander
 
Implementation of c string functions
Implementation of c string functionsImplementation of c string functions
Implementation of c string functions
mohamed sikander
 
Arrays
ArraysArrays
Function basics
Function basicsFunction basics
Function basics
mohamed sikander
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
mohamed sikander
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
mohamed sikander
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
mohamed sikander
 
C questions
C questionsC questions
C questions
mohamed sikander
 
Container adapters
Container adaptersContainer adapters
Container adapters
mohamed sikander
 
Implementing string
Implementing stringImplementing string
Implementing string
mohamed sikander
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
mohamed sikander
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
mohamed sikander
 
Static and const members
Static and const membersStatic and const members
Static and const members
mohamed sikander
 

More from mohamed sikander (15)

C++ 11 range-based for loop
C++ 11   range-based for loopC++ 11   range-based for loop
C++ 11 range-based for loop
 
Pointer level 2
Pointer   level 2Pointer   level 2
Pointer level 2
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 
Implementation of c string functions
Implementation of c string functionsImplementation of c string functions
Implementation of c string functions
 
Arrays
ArraysArrays
Arrays
 
Function basics
Function basicsFunction basics
Function basics
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
C questions
C questionsC questions
C questions
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
Implementing string
Implementing stringImplementing string
Implementing string
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
Static and const members
Static and const membersStatic and const members
Static and const members
 

Recently uploaded

Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 

Recently uploaded (20)

Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 

Understanding storage class using nm

  • 1. Prepared by Mohammed Sikander Technical Lead Cranes Software International Limited
  • 2. int g_var = 5; int main( ) { int l_var = 8; } mohammed.sikander@cranessoftware.com 2 00000000 D g_var 00000000 T main • Symbol table does not contain the entries of local/automatic variables. • gl is stored in Initialized Data Segment • main is stored inText / Code Segment
  • 3. int global = 5; static int stglobal = 9; int main( ) { int local = 8; static int stlocal = 3; } mohammed.sikander@cranessoftware.com 3 00000000 D global 00000000 T main 00000004 d stglobal 00000008 d stlocal.0 • d stands for Internal Linkage • External variables will not have any name mangling. • Internal Linkage variables might have name mangling.
  • 4. 1. Can we have two static variables with same name but different functions. 2. What is the memory segment of x. 3. How does the compiler avoid name clashes. mohammed.sikander@cranessoftware.com 4 void print( ) { static int x = 5; } void display( ) { static int x = 8; } int main( ) { } 00000005 T display 0000000a T main 00000000 T print 00000000 d x.0 00000004 d x.1 Compiler uses different names for x.
  • 5. int x = 5; int main( ) { int x = 10; printf(“x = %d n “ , x); } mohammed.sikander@cranessoftware.com 5 static int x = 5; int main( ) { static int x = 10; { static int x = 12; printf(“x = %d n “ , x); } }
  • 6. FILE1.C int x = 5; void func( ); int main( ) { func( ); } FILE2.C int x = 8; void func( ) { printf(" x = %d n" , x); } mohammed.sikander@cranessoftware.com 6 • Compile file1.c • Compile file2.c • Link file1.o and file2.o • Observe the output
  • 7. mohammed.sikander@cranessoftware.com 7 • [sikander@localhost nm]$ gcc file1.c -c • [sikander@localhost nm]$ gcc file2.c –c • [sikander@localhost nm]$ gcc file1.o file2.o • file2.o(.data+0x0): multiple definition of `x' • file1.o(.data+0x0): first defined here • collect2: ld returned 1 exit status [sikander@localhost nm]$ nm file1.o U func 00000000 T main 00000000 D x [sikander@localhost nm]$ nm file2.o 00000000 T func U printf 00000000 D x
  • 8. FILE1.C static int x = 5; void func( ); int main( ) { printf(__func__ ); printf(" x = %d n",x); func( ); } FILE2.C static int x = 8; void func( ) { printf("%s x = %d n" ,__func__ , x); } mohammed.sikander@cranessoftware.com 8 • Compile file1.c • Compile file2.c • Link file1.o and file2.o • Observe the output
  • 9. mohammed.sikander@cranessoftware.com 9 • $ gcc -c file1.c • $ gcc -c file2.c • $ gcc file1.o file2.o • $ ./a.out • main x = 5 • func x = 8 $ nm file1.o U func 00000000r __func__.0 00000000T main U printf 00000000 d x $ nm file2.o 00000000T func 00000000r __func__.0 U printf 00000000 d x 080495dc d x 080495e0 d x
  • 10. FILE1.C int x = 5; void func( ); int main( ) { printf(__func__ ); printf(" x = %d n",x); func( ); } FILE2.C extern int x; void func( ) { printf("%s x = %d n" ,__func__ , x); } mohammed.sikander@cranessoftware.com 10 • Compile file1.c • Compile file2.c • Link file1.o and file2.o • Observe the output
  • 11. FILE1.C int x = 5; void func( ); int main( ) { printf(__func__ ); printf(" x = %d n",x); func( ); } FILE2.C extern int x; void func( ) { printf("%s x = %d n" ,__func__ , x); } mohammed.sikander@cranessoftware.com 11 • Compile file1.c • Compile file2.c • Link file1.o and file2.o • Observe the output
  • 12. mohammed.sikander@cranessoftware.com 12 • $ gcc -c file1.c • $ gcc -c file2.c • $ gcc file1.o file2.o • $ ./a.out  main x = 5  &x = 0x8049618  func x = 5  &x = 0x8049618 ]$ nm file1.o U func 00000000 r __func__.0 00000000 T main U printf 00000000 D x ]$ nm file2.o 00000000 T func 00000000 r __func__.0 U printf U x $nm a.out 08049618 D x
  • 13. FILE1.C static int x = 5; void func( ); int main( ) { printf(__func__ ); printf(" x = %d n",x); func( ); } FILE2.C extern int x; void func( ) { printf("%s x = %d n" ,__func__ , x); } mohammed.sikander@cranessoftware.com 13 • Compile file1.c • Compile file2.c • Link file1.o and file2.o • Observe the output
  • 14. mohammed.sikander@cranessoftware.com 14 • $ gcc -c file1.c • $ gcc -c file2.c • $ gcc file1.o file2.o • file2.o(.text+0xb): In function `func': • : undefined reference to `x‘ • collect2: ld returned 1 exit status ]$ nm file1.o U func 00000000 r __func__.0 00000000 T main U printf 00000000 d x ]$ nm file2.o 00000000 T func 00000000 r __func__.0 U printf U x
  • 15. static int x = 5; int main( ) { int x; printf(“x = %d n”,x); }
  • 16. static int x = 5; int main( ) { extern int x; printf(“x = %d n”,x); }
  • 17. int a = 5; int b; static int c = 7; static int d; int main( ) { } • Mention the memory segments of variables after compiling (.o file) and after linking (a.out) • What is the difference between a and c. • Use nm utility to view the memory segments $gcc –c file.c $nm file.o $gcc file.c $nm ./a.out
  • 18.  $ gcc file.c -c  $ nm file.o  00000000 D a  00000004 C b  00000004 d c  00000000 b d  00000000 T main mohammed.sikander@cranessoftware.com 18 •“C” The symbol is common. • Common symbols are uninitialized data. • When linking, multiple common symbols may appear with the same name. • If the symbol is defined anywhere, the common symbols are treated as undefined references.$ nm a.out 08049534 D a 08049544 B b 08049538 d c 08049540 b d
  • 19. int x ; int main( ) { printf(“Main &x= %p” , &x); printf(“x = %d n”, x); func( ); } int x ; void func( ) { printf(“Func &x= %p” , &x); printf(“x = %d n”, x); } $ nm file1.o U func 00000000 T main U printf 00000004 C x ]$ nm file2.o 00000000 T func U printf 00000004 C x $nm a.out 08049600 B x
  • 20. int x ; int main( ) { printf(“Main &x= %p” , &x); printf(“x = %d n”, x); func( ); } int x = 5 ; void func( ) { printf(“Func &x= %p” , &x); printf(“x = %d n”, x); } $ nm file2.o 00000000T func U printf 00000000 D x $ nm file1.o U func 00000000 T main U printf 00000004 C x $nm a.out 08049600 D x
  • 21. C C B C D D D D Multiple Definition d d NoConflict, two different variables b b NoConflict, two different variables d b NoConflict, two different variables b d NoConflict, two different variables b D NoConflict, two different variables
  • 22. void func( ); int x = 5; //D int main( ) { printf(“Main &x= %p” , &x); printf(“x = %d n”, x); func( ); } int x = 10; //D void func( ) { printf(“Func &x= %p” , &x); printf(“x = %d n”, x); } void func( ); static int x = 5; //d int main( ) { printf(“Main &x= %p” , &x); printf(“x = %d n”, x); func( ); } int x = 10; //D void func( ) { printf(“Func &x= %p” , &x); printf(“x = %d n”, x); }
  • 23. const int gc = 10; static const int gsc = 12; int main( ) { const int lc = 5; static const int lsc = 8; } 00000000 R gc 00000004 r gsc 00000008 r lsc.0 00000000 T main
  • 24. const int gc = 15; //ReadOnly int main( ) { const int lc = 5; //Stack printf(“Enter the value for local const variable : “); scanf(“ %d”,&lc); printf(“LocalConst = %d n” , lc); printf(“Enter the value for global const variable : “); scanf(“ %d”,&gc); printf(“GlobalConst = %d n”,gc); } [sikander@localhost ~]$ ./a.out Enter the value for local const variable : 89 LocalConst = 89 Enter the value for global const variable : 6 Segmentation fault
  • 25.  [sikander@localhost ~]$ nm f1.o  00000000 t display  0000000a T main  00000005 T print static void display() { } void print() { } int main( ) { display( ); print( ); }
  • 26.  For any queries and feedback  Mail to  Mohammed.sikander@cranessoftware.com  sikander1248@gmail.com mohammed.sikander@cranessoftware.com 26