sysprog2 part2

13
C for Linux Session 2 BY: Eng.Ahmed Teirelbar Software Engineer Shuja' Consulting

Upload: ahmed-mekkawy

Post on 03-Jul-2015

535 views

Category:

Technology


1 download

DESCRIPTION

second part of the second session in phase I of system programming sessions in eglug

TRANSCRIPT

Page 1: sysprog2 Part2

C for Linux

Session 2

BY:Eng.Ahmed TeirelbarSoftware EngineerShuja' Consulting

Page 2: sysprog2 Part2

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

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

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

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

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

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

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

Makefile Basics

● variables● rules● dependencies● variable defaults● wild characters

Page 10: sysprog2 Part2

Makefile Example

● Same program from earlier● Add the shared object example

Page 11: sysprog2 Part2

gdb Basics● -g● In execution

– run

– continue

– step

– breakpoint

– variables

– directives

– execute code

Page 12: sysprog2 Part2

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

Pointers

● SEGV ● Memory overwrites● null termination issues● Core dump● More gdb

– dumping memory

– casting