Linux xlanchenustc edu cn 3606864 83421 Autumn 2007
- Slides: 96
Linux操作系统分析 主讲:陈香兰 助教:贾永泉、毛熠璐 xlanchen@ustc. edu. cn 3606864 -83(西区电三421) Autumn 2007 xlanchen@2007. 5. 22 Embedded Operating Systems
“ 欲善其事, 必先利其器” --《论语》 xlanchen@2007. 5. 22 Embedded Operating Systems 2
相关 具介绍 l l 开发 具 分析 具 xlanchen@2007. 5. 22 Embedded Operating Systems 3
GNU Tools简介 xlanchen@2007. 5. 22 Embedded Operating Systems
一、GCC l l 很多人认为GCC只是一个C编译器, 其实GCC = GNU Compiler Collection 目前,GCC可以支持多种高级语言,如 l l l C、C++ ADA Object C JAVA Fortran PASCAL xlanchen@2007. 5. 22 Embedded Operating Systems 6
gcc使用举例(1) 源程序 xlanchen@2007. 5. 22 Embedded Operating Systems 9
gcc使用举例(2) 编译和运行 编译 运行 xlanchen@2007. 5. 22 Embedded Operating Systems 10
gcc的 作过程(1) l 如果使用-v选项,则可以看到许多被隐藏的信 息 xlanchen@2007. 5. 22 Embedded Operating Systems 11
xlanchen@2007. 5. 22 Embedded Operating Systems 12
预编译 行数 单词数 字节数 xlanchen@2007. 5. 22 Embedded Operating Systems 15
预处理文件 汇编代码 直接编译到汇编代码 xlanchen@2007. 5. 22 Embedded Operating Systems 17
3、编译成目标代码 l 汇编代码 目标代码 gcc –x assembler –c gcctest. s l 直接编译成目标代码 gcc –c gcctest. c l 使用汇编器生成目标代码 as –o gcctest. s xlanchen@2007. 5. 22 Embedded Operating Systems 18
汇编代码 目标代码 直接编译成目标代码 使用汇编器 xlanchen@2007. 5. 22 Embedded Operating Systems 19
4、编译成执行代码 l 目标代码 执行代码 gcc –o gcctest. o l 直接生成执行代码 gcc –o gcctest. c xlanchen@2007. 5. 22 Embedded Operating Systems 20
目标代码 执行代码 直接生成执行代码 xlanchen@2007. 5. 22 Embedded Operating Systems 21
gcc的高级选项 l -Wall:打开所有的警告信息 xlanchen@2007. 5. 22 Embedded Operating Systems 22
修改源程序 xlanchen@2007. 5. 22 Embedded Operating Systems 24
gcc的优化编译举例(1) 考虑如下的源代码 不同的优化 编译选项 xlanchen@2007. 5. 22 Embedded Operating Systems 26
gcc的优化编译举例(2) 使用time命令统计程序的运行 xlanchen@2007. 5. 22 Embedded Operating Systems 27
15. 16. 17. libiberty 包含许多GNU程序都会用到的函数,这 些程序有: getopt, obstack, strerror, strtol 和 strtoul. libbfd 二进制文件描述库. libopcodes 用来处理opcodes的库, 在生成一些 应用程序的时候也会用到它, 比如 objdump. Opcodes是文本格式可读的处理器操作 指令. xlanchen@2007. 5. 22 Embedded Operating Systems 32
binutils开发 具使用举例 l l l ar nm Objcopy Objdump readelf xlanchen@2007. 5. 22 Embedded Operating Systems 33
使用ar建立库文件(1) l 源程序add. c和minus. c xlanchen@2007. 5. 22 Embedded Operating Systems 35
库文件使用举例 在代码中使用Add和Minus函数 xlanchen@2007. 5. 22 Embedded Operating Systems 37
在编译时指定库文件 指明将libtest. a链接进来 运行结果 xlanchen@2007. 5. 22 Embedded Operating Systems 38
nm使用举例 如果对test可执行文件使用nm, 会有什么结果呢?(作业) xlanchen@2007. 5. 22 Embedded Operating Systems 41
Objcopy使用举例 使用file命令查看文件类型 生成srec格式的目标文件 使用file命令查看新文件的类型 xlanchen@2007. 5. 22 Embedded Operating Systems 43
文件格式 l l l a. out: assembler and link editor output 汇编器和链接编辑器的输出 coff common object file format 一种通用的对象文件格式 ELF excutive linked file Linux系统所采用的一种通用文件格式,支持动态连接。 ELF格式可以比COFF格式包含更多的调试信息 xlanchen@2007. 5. 22 Embedded Operating Systems 44
文件格式 l l l Flat elf格式有很大的文件头,flat文件对文件头和 一些段信息做了简化 u. Clinux系统使用flat可执行文件格式 SREC MOTOROLA S-Recoder格式(S记录格式文 件) 等等 xlanchen@2007. 5. 22 Embedded Operating Systems 45
Objdump使用举例(1) -f选项:显示文件头中的内容 xlanchen@2007. 5. 22 Embedded Operating Systems 47
Objdump使用举例(2) -d选项进行反汇编 xlanchen@2007. 5. 22 Embedded Operating Systems 48
5、readelf l readelf: 显示一个或多个ELF格式的目标文件信息。 xlanchen@2007. 5. 22 Embedded Operating Systems 49
Readelf使用举例 xlanchen@2007. 5. 22 Embedded Operating Systems 50
三、其他GNU 具 l l Gdb—调试器 GNU make--软件 程 具 diff,patch--补丁 具 CVS--版本控制系统 xlanchen@2007. 5. 22 Embedded Operating Systems 51
1、GNU Toolchain—gdb l l Gdb = GNU debuger GNU tools中的调试器,功能强大 l l l 设置断点 监视、修改变量 单步执行 显示/修改寄存器的值 堆栈查看 远程调试 xlanchen@2007. 5. 22 Embedded Operating Systems 52
gdb使用举例 l 源代码如下 编译: gcc –o bug. c xlanchen@2007. 5. 22 Embedded Operating Systems 53
编译并运行 编译 ???? xlanchen@2007. 5. 22 Embedded Operating Systems 54
使用gdb调试bug 运行bug 输入字符串hello 显示出错位置 xlanchen@2007. 5. 22 Embedded Operating Systems 能不能看到源代码呢? 55
使用gcc的-g参数 l l gcc –g –o bug. c 重新调试 列出源代码 xlanchen@2007. 5. 22 Embedded Operating Systems 56
设置断点 ?怎么修改前面的源代码呢? xlanchen@2007. 5. 22 Embedded Operating Systems 57
l 规则一般形式 target. . . : prerequisites. . . <tab>command <tab>. . . l 例如 test: test. c; gcc –O –o test. c xlanchen@2007. 5. 22 Embedded Operating Systems 63
一个简单的makefile edit : main. o kbd. o command. o display. o insert. o search. o files. o utils. o cc -o edit main. o kbd. o command. o display. o insert. o search. o files. o utils. o main. o : main. c defs. h cc -c main. c kbd. o : kbd. c defs. h command. h cc -c kbd. c command. o : command. c defs. h command. h cc -c command. c display. o : display. c defs. h buffer. h cc -c display. c insert. o : insert. c defs. h buffer. h cc -c insert. c search. o : search. c defs. h buffer. h cc -c search. c files. o : files. c defs. h buffer. h command. h cc -c files. c utils. o : utils. c defs. h cc -c utils. c clean : xlanchen@2007. 5. 22 Embeddeddisplay. o Operating Systems rm edit main. o kbd. o command. o insert. o search. o files. o utils. o 64
Make的 作过程 l l default goal 在缺省的情况下,make从makefile中的第一个 目标开始执行 Make的 作过程类似一次深度优先遍历过程 xlanchen@2007. 5. 22 Embedded Operating Systems 65
Makefile 中的变量 l 使用变量可以 l l l 降低错误风险 简化makefile 例:objects变量($(objects)) objects = main. o kbd. o command. o display. o insert. o search. o files. o utils. o edit : $(objects) cc -o edit $(objects) xlanchen@2007. 5. 22 Embedded Operating Systems 66
预定义变量 l Make使用了许多预定义的变量,如 l l l l AR AS CC CXX CFLAGS CPPFLAGS 等等 xlanchen@2007. 5. 22 Embedded Operating Systems 68
简化后的makefile文件 objects = main. o kbd. o command. o display. o insert. o search. o files. o utils. o edit : $(objects) cc -o edit $(objects) main. o : defs. h kbd. o : defs. h command. h display. o : defs. h buffer. h insert. o : defs. h buffer. h search. o : defs. h buffer. h files. o : defs. h buffer. h command. h utils. o : defs. h. PHONY : clean : rm edit $(objects) xlanchen@2007. 5. 22 Embedded Operating Systems 69
Makefile中的函数 (Functions) l l 用来计算出要操作的文件、目标或者要执行的 命令 使用方法: l l $(function arguments) 典型的函数 l $(subst from, to, text) l l $(subst ee, EE, feet on the street) 相当于`f. EEt on the str. EEt' xlanchen@2007. 5. 22 Embedded Operating Systems 74
l $(patsubst pattern, replacement, text) l l l $(patsubst %. c, %. o, x. c. c bar. c) 相当于`x. c. o bar. o‘ $(wildcard pattern) l l $(wildcard *. c) objects : = $(wildcard *. o) xlanchen@2007. 5. 22 Embedded Operating Systems 75
makefile中的条件语句 conditional-directive text-if-true endif or conditional-directive text-if-true else text-if-false endif xlanchen@2007. 5. 22 Embedded Operating Systems 76
四种条件语句 l l ifeq. . . else. . . endif ifneq…else…endif ifndef. . . else…endif xlanchen@2007. 5. 22 Embedded Operating Systems 77
实际项目中的makefile l 找到Linux源代码中所有的makefile,分析它们 的功能、相互关系。 xlanchen@2007. 5. 22 Embedded Operating Systems 78
ld使用举例(1) l 源程序 l 编译hello. c到hello. o l 命令:gcc -c hello. c xlanchen@2007. 5. 22 Embedded Operating Systems 80
ld使用举例(2) l 链接 l l 命令:ld –dynamic-linker /lib/ld-linux. so. 2 /usr/lib/crt 1. o /usr/lib/crti. o /usr/lib/crtn. o hello. o –lc –o hello 运行. /hello xlanchen@2007. 5. 22 Embedded Operating Systems 81
l 使用objdump查看目标文件的信息 l l objdump –h hello. o 比较hello 看看有什么不同? xlanchen@2007. 5. 22 Embedded Operating Systems 83
常用的命令 l INCULDE filename 包含其他filename的链接描述文件 l INPUT(file, …) 指定多个输入文件名 xlanchen@2007. 5. 22 Embedded Operating Systems 88
常用的命令 l OUTPUT_FORMAT(bfdname) 指定输出文件的格式 l OUTPUT_ARCH ( bfdname ) l 指定目标机器体系结构,例如: OUTPUT_ARCH(arm) xlanchen@2007. 5. 22 Embedded Operating Systems 89
Memory举例 xlanchen@2007. 5. 22 Embedded Operating Systems 91
定义输出节 § l SECTIONS {. . . secname : { contents }. . . } 例如: xlanchen@2007. 5. 22 Embedded Operating Systems 93
定位计数器 l 定位计数器,The Location Counter l l l 一个特殊的ld变量,使用“. ”表示 总是在SECTIONS中使用 例如: xlanchen@2007. 5. 22 Embedded Operating Systems 94
一个简单例子 l 下面是一个简单的例子: 例中,输出文件包含text,data,bss三个节,而输入文件 也只包含这 3个节: SECTIONS {. =0 x 01000000; . text: {*(. text)}; . =0 x 08000000; . data: {*(. data)}; . bss: {*(. bss)}; } xlanchen@2007. 5. 22 Embedded Operating Systems 95
SECTIONS举例(对应于上面的 MEMORY例子) xlanchen@2007. 5. 22 Embedded Operating Systems 96
- Linux kernel linux security module m1
- Embedded linux vs desktop linux
- Edu.sharif.edu
- Effect of assonance
- Aquarius water bearer meaning
- Autumn spring season
- Tongue twisters winter
- Jackral
- Autumn rhythm (number 30) 1950
- Autumn rhythm (number 30)
- Action painting vs color field
- Autumn summer
- Uttam
- The happy autumn fields
- Matt monro autumn leaves
- Words that rhyme with autumn
- What happens in autum
- Taylor paxton
- "israeli autumn"
- Lovevery weather station
- First day of autumn 2014
- Every autumn comes the bear
- Louis comfort tiffany autumn landscape
- Goodbye summer, hello autumn poem
- Thanksgiving fact or opinion
- Sometimes whoever seeks abroad
- Us digital millennium copyright act ethics
- "israeli autumn"
- Fall spring summer winter
- The wild swans at coole imagery
- Wild swans at coole techniques
- Autumn unscramble
- Envelope rhyme scheme
- Spring is green summer is bright song
- Metaphor for autumn leaves
- Macam-macam ikon dan fungsinya
- Biosafety act 2007
- 28 september 2007 hari apa
- 19 februari 2007 hari apa
- Design of gantry girder as per is 800 2007
- Visio professional 2007
- Scaling bridge
- Ogle 2007 blg 368 lb
- 24 april 2007
- 2007-1989
- 学乐xuele
- Who building blocks framework 2007
- Kdnf 2007
- T. trimpe 2007 http //sciencespot.net/
- Google sky 2007
- Ms project 2007
- Powerpivot excel 2007
- Solver excel 2007
- Great coastal gale of 2007
- Lois du 5 mars 2007
- Hisd 2007 bond
- Bdd 2007
- Solveur excel
- Micro office word 2007
- Boardworks ltd 2007
- Matlab 2007
- Foretaste of spring 2007
- Inpc 2013
- Pi i excel
- Static electricity summary
- Riskesdas 2007
- Iajr
- Excel變異數分析
- Ods to excel
- 2007
- 16 febbraio 2007
- Bir sayının çeyreği
- Office 2007 training
- How to make histogram in excel 2007
- Who building blocks framework 2007
- Daiva vaitkevičiūtė
- Delhi fire service act
- 2007
- Microsoft office 2007 wikipedia
- Ramadhan 2007
- Twilight spoiler kenning
- Intitle:"index of" the messengers 2007
- Sinionimi
- Cursus word en excel
- Kirjanik jaan 1920-2007
- Wena naty 2007
- National policy on the voluntary sector 2007
- 23082007
- Moa-2007-blg-192 lb
- Solver excel 2007
- Exchange 2007 signatures
- Sommerville 2007
- Pp bil 5 tahun 2007
- Ms publisher 2007
- Sap business one 2007
- Middle school 2008 fashion
- How to create multiple choice questions in word 2007