sysprog2 Part2
-
Upload
ahmed-mekkawy -
Category
Technology
-
view
535 -
download
1
description
Transcript of sysprog2 Part2
![Page 1: sysprog2 Part2](https://reader035.fdocuments.in/reader035/viewer/2022080212/55961c0a1a28ab80018b45db/html5/thumbnails/1.jpg)
C for Linux
Session 2
BY:Eng.Ahmed TeirelbarSoftware EngineerShuja' Consulting
![Page 2: sysprog2 Part2](https://reader035.fdocuments.in/reader035/viewer/2022080212/55961c0a1a28ab80018b45db/html5/thumbnails/2.jpg)
Examples
● cpp example● gcc example with a -o and -c● objdump of a .o and an ELF
Preprocessing, Compilation and Linking
![Page 3: sysprog2 Part2](https://reader035.fdocuments.in/reader035/viewer/2022080212/55961c0a1a28ab80018b45db/html5/thumbnails/3.jpg)
Your program as a process
● Every executing piece of code (outside the kernel) is running in or as a process– including yours
● ps and top example
![Page 4: sysprog2 Part2](https://reader035.fdocuments.in/reader035/viewer/2022080212/55961c0a1a28ab80018b45db/html5/thumbnails/4.jpg)
Process Address Space
● Concept of address space– why per process?
– why segmented?
– A very simplified view
– what does this have to do with C?Code Segment
Data Segment
Stack
![Page 5: sysprog2 Part2](https://reader035.fdocuments.in/reader035/viewer/2022080212/55961c0a1a28ab80018b45db/html5/thumbnails/5.jpg)
Stack and Scope again
● What's a stack?– SP, push and pop
● Scoping in– SP stored
● Scoping out – stack unwinds, SP retrieved
● What does this mean for declarations
![Page 6: sysprog2 Part2](https://reader035.fdocuments.in/reader035/viewer/2022080212/55961c0a1a28ab80018b45db/html5/thumbnails/6.jpg)
Libraries
● A piece of code that encapsulates some functionality - Modularity
● Dynamic and Static– run-time vs build-time -- functional scalability
– Linking
● Sharing– One copy in memory – size
● In Linux: Static libraries and Shared Objects
![Page 7: sysprog2 Part2](https://reader035.fdocuments.in/reader035/viewer/2022080212/55961c0a1a28ab80018b45db/html5/thumbnails/7.jpg)
Using a shared object
● lib<x>.so● gcc example with libm.so
– gcc -l<m> ....
● The library search path● objdump of this new program● ldd
![Page 8: sysprog2 Part2](https://reader035.fdocuments.in/reader035/viewer/2022080212/55961c0a1a28ab80018b45db/html5/thumbnails/8.jpg)
Makefiles
● Builds get complex:– Files
– Flags and directives
– Stages
● make utility● Reference
– http://www.linuxtopia.org/online_books/programming_tool_guides/gnu_make_user_guide/make.html
![Page 9: sysprog2 Part2](https://reader035.fdocuments.in/reader035/viewer/2022080212/55961c0a1a28ab80018b45db/html5/thumbnails/9.jpg)
Makefile Basics
● variables● rules● dependencies● variable defaults● wild characters
![Page 10: sysprog2 Part2](https://reader035.fdocuments.in/reader035/viewer/2022080212/55961c0a1a28ab80018b45db/html5/thumbnails/10.jpg)
Makefile Example
● Same program from earlier● Add the shared object example
![Page 11: sysprog2 Part2](https://reader035.fdocuments.in/reader035/viewer/2022080212/55961c0a1a28ab80018b45db/html5/thumbnails/11.jpg)
gdb Basics● -g● In execution
– run
– continue
– step
– breakpoint
– variables
– directives
– execute code
![Page 12: sysprog2 Part2](https://reader035.fdocuments.in/reader035/viewer/2022080212/55961c0a1a28ab80018b45db/html5/thumbnails/12.jpg)
Pointer Concepts
● An array is a pointer to the first element● Pointer subscript access● Scope issues – Back to the stack picture● NULL pointer
![Page 13: sysprog2 Part2](https://reader035.fdocuments.in/reader035/viewer/2022080212/55961c0a1a28ab80018b45db/html5/thumbnails/13.jpg)
Pointers
● SEGV ● Memory overwrites● null termination issues● Core dump● More gdb
– dumping memory
– casting