<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>编译点滴 &#187; objdump</title>
	<atom:link href="http://www.lingcc.com/tag/objdump/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lingcc.com</link>
	<description>编译器、虚拟机、程序设计语言、体系结构、软件调试、操作系统等等</description>
	<lastBuildDate>Sat, 04 Feb 2012 06:56:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>GNU工具链学习笔记</title>
		<link>http://www.lingcc.com/2010/03/08/10709/</link>
		<comments>http://www.lingcc.com/2010/03/08/10709/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 12:43:40 +0000</pubDate>
		<dc:creator>erlv</dc:creator>
				<category><![CDATA[GCC]]></category>
		<category><![CDATA[编译技术]]></category>
		<category><![CDATA[编译理论实践和应用]]></category>
		<category><![CDATA[elf]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[gentoo]]></category>
		<category><![CDATA[glibc]]></category>
		<category><![CDATA[ld]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[objdump]]></category>
		<category><![CDATA[工具链]]></category>
		<category><![CDATA[链接]]></category>

		<guid isPermaLink="false">http://www.lingcc.com/?p=10709</guid>
		<description><![CDATA[1..so为动态链接库，.a为静态连接库。他们在Linux下按照ELF格式存储。ELF有四种文件类型。可重定位文件(Relocatable file,*.o，*.a),包含代码和数据，可用来连接成可执行文件或共享目标文件;可执行文件(Executable File),ELF可执行文件；共享目标文件(Shared Object File,*.so),包含代码和数据；核心转储文件(Core Dump File),进程意外终止时，系统将该进程的地址空间内容和其他信息保存到该文件中。 file命令可以查看。 2，Objdump程序，来自binutils，可以查看Object file内的结构。-h：打印各个段基本信息；-s所有段内容以16进制打印;-d:将所有包含指令的段反汇编。 3, ar -t libc.a；查看libc.a压缩了那些.o文件。 4, collect2 是ld连接器的包装，它调用ld链接目标文件，并对结果做处理，包括收集所有与程序初始化相关的信息并构造初始化结构。 5，readelf：查看ELF文件。 -s:打印符号信息； 6, size *.o : 用于查看ELF文件代码段，数据段和BSS(Block Started by Symbol,为未初始化的全局变量和局部静态变量预留位置)段的长度。 7, nm：查看目标文件的符号信息。 8，C运行时库GlibC,由两部分组成，头文件和二进制库，前者多在/usr/include下，后者主要是C语言的标准库，有静态(/usr/lib/libc.a)和动态(/lib/libc.so.6)两个.另外还有/usr/lib/crt1.o(含程序入口函数_start,负责初始化libc和调用main),/usr/lib/crti.o(负责main函数执行前全局和静态对象的构造)和/usr/lib/crtn.o(负责main函数执行后，全局和静态对象的析构)三个辅助程序运行的库。链接器会收集所有输入目标文件中对全局和静态对象的构造/析构操作，合并到输出文件的构造和析构操作中。 9.GCC中的几个库文件，这些库文件都在GCC的安装目录下，Gentoo系统中的目录为/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/。crtbeginT.o和crtend.o,负责C++全局构造和析构的目标文件，因为glibc只是C语言运行库，GCC实现C++语言，所以C++中构造和析构需要glibc中的C语言构造/析构和GCC中相应的构造/析构。libgcc.a用来对付GCC在多平台上运行时的差异问题；libgcc_eh.a包含了支持C++异常处理的平台相关函数。libgcc_s.so是libgcc.a的动态链接版本。 10.ldd 打印共享库依赖关系。 以上内容摘抄自《程序员的自我修养 &#8211;链接，装载与库》 相关文章： GCC初窥 GCC中的pie和fpie选项 Gentoo中的交叉编译利器&#8211;crossdev zz Linux下的段错误的原因及调试 使用crossdev安装龙芯的O32 gnu交叉工具链 用crossdev装龙芯的n32 gnu交叉工具链<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F08%2F10609%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F03%2F08%2F10709%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">GCC中的pie和fpie选项</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F03%2F08%2F10709%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">zz Linux下的段错误的原因及调试</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F05%2F25%2F10951%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F03%2F08%2F10709%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">认识静态链接库</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2009%2F11%2F20%2F10065%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F03%2F08%2F10709%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Gcc和Open64中的-fPIC选项</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F05%2F19%2F10892%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F03%2F08%2F10709%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">走进Debian和Gentoo中的系统初始化过程</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>
<h3>相关文章：</h3><ul>
<li><a href='http://www.lingcc.com/2009/12/29/10503/' rel='bookmark' title='GCC初窥'>GCC初窥</a></li>
<li><a href='http://www.lingcc.com/2010/01/08/10609/' rel='bookmark' title='GCC中的pie和fpie选项'>GCC中的pie和fpie选项</a></li>
<li><a href='http://www.lingcc.com/2009/12/31/10534/' rel='bookmark' title='Gentoo中的交叉编译利器&#8211;crossdev'>Gentoo中的交叉编译利器&#8211;crossdev</a></li>
<li><a href='http://www.lingcc.com/2008/08/27/9982/' rel='bookmark' title='zz Linux下的段错误的原因及调试'>zz Linux下的段错误的原因及调试</a></li>
<li><a href='http://www.lingcc.com/2010/01/07/10594/' rel='bookmark' title='使用crossdev安装龙芯的O32 gnu交叉工具链'>使用crossdev安装龙芯的O32 gnu交叉工具链</a></li>
<li><a href='http://www.lingcc.com/2010/01/05/10588/' rel='bookmark' title='用crossdev装龙芯的n32 gnu交叉工具链'>用crossdev装龙芯的n32 gnu交叉工具链</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<p>1..so为动态链接库，.a为静态连接库。他们在Linux下按照ELF格式存储。ELF有四种文件类型。可重定位文件(Relocatable file,*.o，*.a),包含代码和数据，可用来连接成可执行文件或共享目标文件;可执行文件(Executable File),ELF可执行文件；共享目标文件(Shared Object File,*.so),包含代码和数据；核心转储文件(Core Dump File),进程意外终止时，系统将该进程的地址空间内容和其他信息保存到该文件中。<br />
file命令可以查看。<br />
2，Objdump程序，来自binutils，可以查看Object file内的结构。-h：打印各个段基本信息；-s所有段内容以16进制打印;-d:将所有包含指令的段反汇编。<br />
3, ar -t libc.a；查看libc.a压缩了那些.o文件。<br />
4, collect2 是ld连接器的包装，它调用ld链接目标文件，并对结果做处理，包括收集所有与程序初始化相关的信息并构造初始化结构。<br />
5，readelf：查看ELF文件。 -s:打印符号信息；<br />
<span id="more-10709"></span><br />
6, size *.o : 用于查看ELF文件代码段，数据段和BSS(Block Started by Symbol,为未初始化的全局变量和局部静态变量预留位置)段的长度。<br />
7, nm：查看目标文件的符号信息。<br />
8，C运行时库GlibC,由两部分组成，头文件和二进制库，前者多在/usr/include下，后者主要是C语言的标准库，有静态(/usr/lib/libc.a)和动态(/lib/libc.so.6)两个.另外还有/usr/lib/crt1.o(含程序入口函数_start,负责初始化libc和调用main),/usr/lib/crti.o(负责main函数执行前全局和静态对象的构造)和/usr/lib/crtn.o(负责main函数执行后，全局和静态对象的析构)三个辅助程序运行的库。链接器会收集所有输入目标文件中对全局和静态对象的构造/析构操作，合并到输出文件的构造和析构操作中。<br />
9.GCC中的几个库文件，这些库文件都在GCC的安装目录下，Gentoo系统中的目录为/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/。crtbeginT.o和crtend.o,负责C++全局构造和析构的目标文件，因为glibc只是C语言运行库，GCC实现C++语言，所以C++中构造和析构需要glibc中的C语言构造/析构和GCC中相应的构造/析构。libgcc.a用来对付GCC在多平台上运行时的差异问题；libgcc_eh.a包含了支持C++异常处理的平台相关函数。libgcc_s.so是libgcc.a的动态链接版本。<br />
10.ldd 打印共享库依赖关系。</p>
<p>以上内容摘抄自《程序员的自我修养 &#8211;链接，装载与库》</p>
<div style=float:left><!-- JiaThis Button BEGIN -->
<div id="jiathis_style_32x32">
	<a class="jiathis_button_qzone"></a>
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_tqq"></a>
	<a class="jiathis_button_renren"></a>
	<a class="jiathis_button_kaixin001"></a>
	<a href="http://www.jiathis.com/share/" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
	<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" src="http://v2.jiathis.com/code/jia.js" charset="utf-8"></script>
<!-- JiaThis Button END --></div><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F08%2F10609%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F03%2F08%2F10709%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">GCC中的pie和fpie选项</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F03%2F08%2F10709%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">zz Linux下的段错误的原因及调试</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F05%2F25%2F10951%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F03%2F08%2F10709%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">认识静态链接库</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2009%2F11%2F20%2F10065%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F03%2F08%2F10709%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">Gcc和Open64中的-fPIC选项</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F05%2F19%2F10892%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F03%2F08%2F10709%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">走进Debian和Gentoo中的系统初始化过程</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table><p><h3>相关文章：</h3><ul>
<li><a href='http://www.lingcc.com/2009/12/29/10503/' rel='bookmark' title='GCC初窥'>GCC初窥</a></li>
<li><a href='http://www.lingcc.com/2010/01/08/10609/' rel='bookmark' title='GCC中的pie和fpie选项'>GCC中的pie和fpie选项</a></li>
<li><a href='http://www.lingcc.com/2009/12/31/10534/' rel='bookmark' title='Gentoo中的交叉编译利器&#8211;crossdev'>Gentoo中的交叉编译利器&#8211;crossdev</a></li>
<li><a href='http://www.lingcc.com/2008/08/27/9982/' rel='bookmark' title='zz Linux下的段错误的原因及调试'>zz Linux下的段错误的原因及调试</a></li>
<li><a href='http://www.lingcc.com/2010/01/07/10594/' rel='bookmark' title='使用crossdev安装龙芯的O32 gnu交叉工具链'>使用crossdev安装龙芯的O32 gnu交叉工具链</a></li>
<li><a href='http://www.lingcc.com/2010/01/05/10588/' rel='bookmark' title='用crossdev装龙芯的n32 gnu交叉工具链'>用crossdev装龙芯的n32 gnu交叉工具链</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://www.lingcc.com/2010/03/08/10709/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>反汇编器和汇编器</title>
		<link>http://www.lingcc.com/2010/01/09/10623/</link>
		<comments>http://www.lingcc.com/2010/01/09/10623/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 00:30:48 +0000</pubDate>
		<dc:creator>erlv</dc:creator>
				<category><![CDATA[GCC]]></category>
		<category><![CDATA[编译技术]]></category>
		<category><![CDATA[编译理论实践和应用]]></category>
		<category><![CDATA[Assembler 汇编器]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[disassemble 反汇编器]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[ld]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[objdump]]></category>
		<category><![CDATA[sse]]></category>
		<category><![CDATA[后端]]></category>
		<category><![CDATA[编译器]]></category>

		<guid isPermaLink="false">http://www.lingcc.com/?p=10623</guid>
		<description><![CDATA[反汇编器和汇编器是两个互逆的过程，后者将汇编源文件转换为机器码，前者将机器码转换成汇编语言。 汇编器通过将汇编指令符号转换为opcode和解析符号名为存储位置的形式创建目标代码，该目标代码就是机器码.其中符号解析是汇编器的关键部分.很多汇编器还提供宏支持来方便文本替换.相对于高层语言的编译器,汇编器简单很多.最早的汇编器出现在20世纪50年代,很多现代汇编其器都支持指令调度优化.Linux下使用最频繁的汇编器是gas,Gnu Assembler,因为它是GCC的默认后端，是Binutils的一部分。 反汇编器的输出通常是可读的汇编码格式，因此它时反向工程的重要工具。通常汇编语言允许常数和注释，但是在生成机器码时这些信息通常被删除，所以，在机器码上的反汇编操作只能产生无常数和注释的汇编码，所以程序员很难读懂，也很难转换为原来的高级代码。现在也有些编译器通过使用符号调试信息和交互式允许用户为特定区域的代码和值使用符号代替的方式来增强可读性。现在常见的反汇编器有：各种调试器都包含反汇编器，如GNU Binutils中的objdump就是gdb的交互式反汇编器，其他的还有PVDasm，一个支持多CPU反汇编器，OllyDbg&#8211;32位汇编级分析调试器，ILDASM&#8211;.NET Framework SDK中，用来反汇编PE文件等等 参考： http://en.wikipedia.org/wiki/Assembly_language#Assembler http://en.wikipedia.org/wiki/GNU_Assembler http://en.wikipedia.org/wiki/Disassembler 相关文章： GCC初窥 前瞻-主流处理器中的数据并行支持(SIMD) 小例子&#8211;回眸一笑百媚生 认识静态链接库 GNU工具链学习笔记 GCC中的pie和fpie选项<table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F09%2F10623%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">zz Linux下的段错误的原因及调试</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F08%2F10609%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F09%2F10623%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">GCC中的pie和fpie选项</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F20%2F9981%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F09%2F10623%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">zz  Linux内核 编程风格</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F04%2F30%2F10867%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F09%2F10623%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">风河北京研发中心招编译器研发工程师</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2009%2F12%2F22%2F10404%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F09%2F10623%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">有写编译器的冲动？来看看这些网站</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>
<h3>相关文章：</h3><ul>
<li><a href='http://www.lingcc.com/2009/12/29/10503/' rel='bookmark' title='GCC初窥'>GCC初窥</a></li>
<li><a href='http://www.lingcc.com/2010/05/04/10878/' rel='bookmark' title='前瞻-主流处理器中的数据并行支持(SIMD)'>前瞻-主流处理器中的数据并行支持(SIMD)</a></li>
<li><a href='http://www.lingcc.com/2010/01/21/10663/' rel='bookmark' title='小例子&#8211;回眸一笑百媚生'>小例子&#8211;回眸一笑百媚生</a></li>
<li><a href='http://www.lingcc.com/2010/05/25/10951/' rel='bookmark' title='认识静态链接库'>认识静态链接库</a></li>
<li><a href='http://www.lingcc.com/2010/03/08/10709/' rel='bookmark' title='GNU工具链学习笔记'>GNU工具链学习笔记</a></li>
<li><a href='http://www.lingcc.com/2010/01/08/10609/' rel='bookmark' title='GCC中的pie和fpie选项'>GCC中的pie和fpie选项</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<p>反汇编器和汇编器是两个互逆的过程，后者将汇编源文件转换为机器码，前者将机器码转换成汇编语言。</p>
<p>汇编器通过将汇编指令符号转换为opcode和解析符号名为存储位置的形式创建目标代码，该目标代码就是机器码.其中符号解析是汇编器的关键部分.很多汇编器还提供宏支持来方便文本替换.相对于高层语言的编译器,汇编器简单很多.最早的汇编器出现在20世纪50年代,很多现代汇编其器都支持指令调度优化.Linux下使用最频繁的汇编器是gas,Gnu Assembler,因为它是GCC的默认后端，是Binutils的一部分。</p>
<p>反汇编器的输出通常是可读的汇编码格式，因此它时反向工程的重要工具。通常汇编语言允许常数和注释，但是在生成机器码时这些信息通常被删除，所以，在机器码上的反汇编操作只能产生无常数和注释的汇编码，所以程序员很难读懂，也很难转换为原来的高级代码。现在也有些编译器通过使用符号调试信息和交互式允许用户为特定区域的代码和值使用符号代替的方式来增强可读性。现在常见的反汇编器有：各种调试器都包含反汇编器，如GNU Binutils中的objdump就是gdb的交互式反汇编器，其他的还有PVDasm，一个支持多CPU反汇编器，OllyDbg&#8211;32位汇编级分析调试器，ILDASM&#8211;.NET Framework SDK中，用来反汇编PE文件等等</p>
<p><a href=" http://en.wikipedia.org/wiki/Assembly_language#Assembler">参考：</p>
<p>http://en.wikipedia.org/wiki/Assembly_language#Assembler</a></p>
<p><a href="http://en.wikipedia.org/wiki/GNU_Assembler">http://en.wikipedia.org/wiki/GNU_Assembler</a><br />
<a href="http://en.wikipedia.org/wiki/Disassembler">http://en.wikipedia.org/wiki/Disassembler</a></p>
<div style=float:left><!-- JiaThis Button BEGIN -->
<div id="jiathis_style_32x32">
	<a class="jiathis_button_qzone"></a>
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_tqq"></a>
	<a class="jiathis_button_renren"></a>
	<a class="jiathis_button_kaixin001"></a>
	<a href="http://www.jiathis.com/share/" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
	<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" src="http://v2.jiathis.com/code/jia.js" charset="utf-8"></script>
<!-- JiaThis Button END --></div><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F09%2F10623%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">zz Linux下的段错误的原因及调试</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F08%2F10609%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2010%2F<p><h3>相关文章：</h3><ul>
<li><a href='http://www.lingcc.com/2009/12/29/10503/' rel='bookmark' title='GCC初窥'>GCC初窥</a></li>
<li><a href='http://www.lingcc.com/2010/05/04/10878/' rel='bookmark' title='前瞻-主流处理器中的数据并行支持(SIMD)'>前瞻-主流处理器中的数据并行支持(SIMD)</a></li>
<li><a href='http://www.lingcc.com/2010/01/21/10663/' rel='bookmark' title='小例子&#8211;回眸一笑百媚生'>小例子&#8211;回眸一笑百媚生</a></li>
<li><a href='http://www.lingcc.com/2010/05/25/10951/' rel='bookmark' title='认识静态链接库'>认识静态链接库</a></li>
<li><a href='http://www.lingcc.com/2010/03/08/10709/' rel='bookmark' title='GNU工具链学习笔记'>GNU工具链学习笔记</a></li>
<li><a href='http://www.lingcc.com/2010/01/08/10609/' rel='bookmark' title='GCC中的pie和fpie选项'>GCC中的pie和fpie选项</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://www.lingcc.com/2010/01/09/10623/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>zz Linux下的段错误的原因及调试</title>
		<link>http://www.lingcc.com/2008/08/27/9982/</link>
		<comments>http://www.lingcc.com/2008/08/27/9982/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 14:57:00 +0000</pubDate>
		<dc:creator>erlv</dc:creator>
				<category><![CDATA[Linux系统]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[gentoo]]></category>
		<category><![CDATA[glibc]]></category>
		<category><![CDATA[ld]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mit]]></category>
		<category><![CDATA[objdump]]></category>
		<category><![CDATA[USE]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[编程技术]]></category>
		<category><![CDATA[编译器]]></category>

		<guid isPermaLink="false">http://lingcc.net/blog/?p=9982</guid>
		<description><![CDATA[简而言之,产生段错误就是访问了错误的内存段，一般是你没有权限，或者根本就不存在对应的物理内存,尤其常见的是访问0地址. 一 般来说, 段错误就是指访问的内存超出了系统所给这个程序的内存空间，通常这个值是由gdtr来保存的，他是一个48位的寄存器，其中的32位是保存由它指向的 gdt表，后13位保存相应于gdt的下标，最后3位包括了程序是否在内存中以及程序的在cpu中的运行级别,指向的gdt是由以64位为一个单位的表， 在这张表中就保存着程序运行的代码段以及数据段的起始地址以及与此相应的段限和页面交换还有程序运行级别还有内存粒度等等的信息。一旦一个程序发生了越界 访问，cpu就会产生相应的异常保护，于是segmentation fault就出现了. 在编程中以下几类做法容易导致段错误,基本是是错误地使用指针引起的 1)访问系统数据区，尤其是往 系统保护的内存地址写数据 &#160;&#160; 最常见就是给一个指针以0地址 2)内存越界(数组越界，变量类型不一致等) 访问到不属于你的内存区域 解决方法 我 们在用C/C++语言写程序的时侯，内存管理的绝大部分工作都是需要我们来做的。实际上，内存管理是一个比较繁琐的工作，无论你多高明，经验多丰富，难免 会在此处犯些小错误，而通常这些错误又是那么的浅显而易于消除。但是手工&#8220;除虫&#8221;（debug），往往是效率低下且让人厌烦的，本文将就&#34;段错误&#34;这个内 存访问越界的错误谈谈如何快速定位这些&#34;段错误&#34;的语句。 下面将就以下的一个存在段错误的程序介绍几种调试方法： &#160;&#160;&#160;&#160; 1 dummy_function (void) &#160;&#160;&#160;&#160; 2 { &#160;&#160;&#160;&#160; 3&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; unsigned char *ptr = 0&#215;00; &#160;&#160;&#160;&#160; 4&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; *ptr = 0&#215;00; &#160;&#160;&#160;&#160; 5 } &#160;&#160;&#160;&#160; 6 &#160;&#160;&#160;&#160; 7 int main (void) &#160;&#160;&#160;&#160; 8 { &#160;&#160;&#160;&#160; 9&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <a href='http://www.lingcc.com/2008/08/27/9982/'>[...]</a><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F08%2F10609%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">GCC中的pie和fpie选项</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F20%2F9981%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">zz  Linux内核 编程风格</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F03%2F08%2F10709%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">GNU工具链学习笔记</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F09%2F10623%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">反汇编器和汇编器</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2008%2F02%2F27%2F9904%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">gentoo的相关配置-make.conf</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>
<h3>相关文章：</h3><ul>
<li><a href='http://www.lingcc.com/2010/01/21/10663/' rel='bookmark' title='小例子&#8211;回眸一笑百媚生'>小例子&#8211;回眸一笑百媚生</a></li>
<li><a href='http://www.lingcc.com/2009/12/31/10534/' rel='bookmark' title='Gentoo中的交叉编译利器&#8211;crossdev'>Gentoo中的交叉编译利器&#8211;crossdev</a></li>
<li><a href='http://www.lingcc.com/2010/03/08/10709/' rel='bookmark' title='GNU工具链学习笔记'>GNU工具链学习笔记</a></li>
<li><a href='http://www.lingcc.com/2010/10/12/11305/' rel='bookmark' title='如何让程序屏蔽CTRL+C和CTRL+Z'>如何让程序屏蔽CTRL+C和CTRL+Z</a></li>
<li><a href='http://www.lingcc.com/2010/01/05/10588/' rel='bookmark' title='用crossdev装龙芯的n32 gnu交叉工具链'>用crossdev装龙芯的n32 gnu交叉工具链</a></li>
<li><a href='http://www.lingcc.com/2010/01/07/10594/' rel='bookmark' title='使用crossdev安装龙芯的O32 gnu交叉工具链'>使用crossdev安装龙芯的O32 gnu交叉工具链</a></li>
</ul>]]></description>
			<content:encoded><![CDATA[<h2> </h2>
<h2><font size="2">简而言之,产生段错误就是访问了错误的内存段，一般是你没有权限，或者根本就不存在对应的物理内存,尤其常见的是访问0地址.</font></h2>
<div class="content">
<div style="margin: 15px;">
一 般来说, 段错误就是指访问的内存超出了系统所给这个程序的内存空间，通常这个值是由gdtr来保存的，他是一个48位的寄存器，其中的32位是保存由它指向的 gdt表，后13位保存相应于gdt的下标，最后3位包括了程序是否在内存中以及程序的在cpu中的运行级别,指向的gdt是由以64位为一个单位的表， 在这张表中就保存着程序运行的代码段以及数据段的起始地址以及与此相应的段限和页面交换还有程序运行级别还有内存粒度等等的信息。一旦一个程序发生了越界 访问，cpu就会产生相应的异常保护，于是segmentation fault就出现了.</p>
<p>在编程中以下几类做法容易导致段错误,基本是是错误地使用指针引起的</p>
<p>1)访问系统数据区，尤其是往  系统保护的内存地址写数据<br />
&nbsp;&nbsp;   最常见就是给一个指针以0地址<br />
2)内存越界(数组越界，变量类型不一致等) 访问到不属于你的内存区域</p>
<p>解决方法</p>
<p>我 们在用C/C++语言写程序的时侯，内存管理的绝大部分工作都是需要我们来做的。实际上，内存管理是一个比较繁琐的工作，无论你多高明，经验多丰富，难免 会在此处犯些小错误，而通常这些错误又是那么的浅显而易于消除。但是手工&ldquo;除虫&rdquo;（debug），往往是效率低下且让人厌烦的，本文将就&quot;段错误&quot;这个内 存访问越界的错误谈谈如何快速定位这些&quot;段错误&quot;的语句。<br />
下面将就以下的一个存在段错误的程序介绍几种调试方法：</p>
<table align="center" style="border: 1px solid rgb(153, 153, 153); font-size: 12px; width: 80%;">
<tbody>
<tr>
<td>&nbsp;&nbsp;&nbsp;&nbsp;   1  dummy_function (void)<br />
            &nbsp;&nbsp;&nbsp;&nbsp;   2  {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;   3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   unsigned char *ptr = 0&#215;00;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;   4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   *ptr = 0&#215;00;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;   5  }<br />
            &nbsp;&nbsp;&nbsp;&nbsp;   6<br />
            &nbsp;&nbsp;&nbsp;&nbsp;   7  int main (void)<br />
            &nbsp;&nbsp;&nbsp;&nbsp;   8  {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;   9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   dummy_function ();<br />
            &nbsp;&nbsp;&nbsp;   10<br />
            &nbsp;&nbsp;&nbsp;   11&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   return 0;<br />
            &nbsp;&nbsp;&nbsp;   12  }</td>
</tr>
</tbody>
</table>
<p>作为一个熟练的C/C++程序员，以上代码的bug应该是很清楚的，因为它尝试操作地址为0的内存区域，而这个内存区域通常是不可访问的禁区，当然就会出错了。我们尝试编译运行它:</p>
<table align="center" style="border: 1px solid rgb(153, 153, 153); font-size: 12px; width: 80%;">
<tbody>
<tr>
<td>xiaosuo@gentux test $ ./a.out<br />
            段错误</td>
</tr>
</tbody>
</table>
<p>果然不出所料，它出错并退出了。<br />
<span style="font-weight: bold;">1.利用gdb逐步查找段错误:</span><br />
这种方法也是被大众所熟知并广泛采用的方法，首先我们需要一个带有调试信息的可执行程序，所以我们加上&ldquo;-g -rdynamic&quot;的参数进行编译，然后用gdb调试运行这个新编译的程序,具体步骤如下:</p>
<table align="center" style="border: 1px solid rgb(153, 153, 153); font-size: 12px; width: 80%;">
<tbody>
<tr>
<td>xiaosuo@gentux test $ gcc -g -rdynamic d.c<br />
            xiaosuo@gentux test $ gdb ./a.out<br />
            GNU gdb 6.5<br />
            Copyright (C) 2006 Free Software Foundation, Inc.<br />
            GDB is free software, covered by the GNU General Public License, and you are<br />
            welcome to change it and/or distribute copies of it under certain conditions.<br />
            Type &quot;show copying&quot; to see the conditions.<br />
            There is absolutely no warranty for GDB.  Type &quot;show warranty&quot; for details.<br />
            This GDB was configured as &quot;i686-pc-linux-gnu&quot;&#8230;Using host libthread_db library &quot;/lib/libthread_db.so.1&quot;.</p>
<p>            (gdb) r<br />
            Starting program: /home/xiaosuo/test/a.out</p>
<p>            Program received signal SIGSEGV, Segmentation fault.<br />
            0&#215;08048524 in dummy_function () at d.c:4<br />
            4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   *ptr = 0&#215;00;<br />
            (gdb)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>
</tbody>
</table>
<p>哦？！好像不用一步步调试我们就找到了出错位置d.c文件的第4行，其实就是如此的简单。<br />
从这里我们还发现进程是由于收到了SIGSEGV信号而结束的。通过进一步的查阅文档(man 7 signal)，我们知道SIGSEGV默认handler的动作是打印&rdquo;段错误&quot;的出错信息，并产生Core文件，由此我们又产生了方法二。<br />
<span style="font-weight: bold;">2.分析Core文件：</span><br />
Core文件是什么呢？</p>
<table align="center" style="border: 1px solid rgb(153, 153, 153); font-size: 12px; width: 80%;">
<tbody>
<tr>
<td>The  default action of certain signals is to cause a process to terminate and produce a core dump file, a disk file containing an image of the process&#8217;s memory  at the time of termination.  A list of the signals which cause a process to dump core can be found in signal(7).</td>
</tr>
</tbody>
</table>
<p>以 上资料摘自man page(man 5 core)。不过奇怪了，我的系统上并没有找到core文件。后来，忆起为了渐少系统上的拉圾文件的数量（本人有些洁癖，这也是我喜欢Gentoo的原因 之一），禁止了core文件的生成，查看了以下果真如此，将系统的core文件的大小限制在512K大小，再试:</p>
<table align="center" style="border: 1px solid rgb(153, 153, 153); font-size: 12px; width: 80%;">
<tbody>
<tr>
<td>xiaosuo@gentux test $ ulimit -c<br />
            0<br />
            xiaosuo@gentux test $ ulimit -c 1000<br />
            xiaosuo@gentux test $ ulimit -c<br />
            1000<br />
            xiaosuo@gentux test $ ./a.out<br />
            段错误 (core dumped)<br />
            xiaosuo@gentux test $ ls<br />
            a.out  core  d.c  f.c  g.c  pango.c  test_iconv.c  test_regex.c</td>
</tr>
</tbody>
</table>
<p>core文件终于产生了，用gdb调试一下看看吧:</p>
<table align="center" style="border: 1px solid rgb(153, 153, 153); font-size: 12px; width: 80%;">
<tbody>
<tr>
<td>xiaosuo@gentux test $ gdb ./a.out core<br />
            GNU gdb 6.5<br />
            Copyright (C) 2006 Free Software Foundation, Inc.<br />
            GDB is free software, covered by the GNU General Public License, and you are<br />
            welcome to change it and/or distribute copies of it under certain conditions.<br />
            Type &quot;show copying&quot; to see the conditions.<br />
            There is absolutely no warranty for GDB.  Type &quot;show warranty&quot; for details.<br />
            This GDB was configured as &quot;i686-pc-linux-gnu&quot;&#8230;Using host libthread_db library &quot;/lib/libthread_db.so.1&quot;.</p>
<p>
            warning: Can&#8217;t read pathname for load map: 输入/输出错误.<br />
            Reading symbols from /lib/libc.so.6&#8230;done.<br />
            Loaded symbols for /lib/libc.so.6<br />
            Reading symbols from /lib/ld-linux.so.2&#8230;done.<br />
            Loaded symbols for /lib/ld-linux.so.2<br />
            Core was generated by `./a.out&#8217;.<br />
            Program terminated with signal 11, Segmentation fault.<br />
            #0  0&#215;08048524 in dummy_function () at d.c:4<br />
            4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   *ptr = 0&#215;00;</td>
</tr>
</tbody>
</table>
<p>哇，好历害，还是一步就定位到了错误所在地，佩服一下Linux/Unix系统的此类设计。<br />
接着考虑下去，以前用windows系统下的ie的时侯，有时打开某些网页，会出现&ldquo;运行时错误&rdquo;，这个时侯如果恰好你的机器上又装有windows的编译器的话，他会弹出来一个对话框，问你是否进行调试，如果你选择是，编译器将被打开，并进入调试状态，开始调试。<br />
Linux下如何做到这些呢？我的大脑飞速地旋转着，有了，让它在SIGSEGV的handler中调用gdb，于是第三个方法又诞生了:<br />
<span style="font-weight: bold;">3.段错误时启动调试:</span></p>
<table align="center" style="border: 1px solid rgb(153, 153, 153); font-size: 12px; width: 80%;">
<tbody>
<tr>
<td>#include &lt;stdio.h&gt;<br />
            #include &lt;stdlib.h&gt;<br />
            #include &lt;signal.h&gt;<br />
            #include &lt;string.h&gt;</p>
<p>            void dump(int signo)<br />
            {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   char buf[1024];<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   char cmd[1024];<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   FILE *fh;</p>
<p>            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   snprintf(buf, sizeof(buf), &quot;/proc/%d/cmdline&quot;, getpid());<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   if(!(fh = fopen(buf, &quot;r&quot;)))<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   exit(0);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   if(!fgets(buf, sizeof(buf), fh))<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   exit(0);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   fclose(fh);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   if(buf[strlen(buf) - 1] == &#8216;\n&#8217;)<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   buf[strlen(buf) - 1] = &#8216;\0&#8242;;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   snprintf(cmd, sizeof(cmd), &quot;gdb %s %d&quot;, buf, getpid());<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   system(cmd);</p>
<p>            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   exit(0);<br />
            }</p>
<p>            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   void<br />
            dummy_function (void)<br />
            {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   unsigned char *ptr = 0&#215;00;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   *ptr = 0&#215;00;<br />
            }</p>
<p>            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   int<br />
            main (void)<br />
            {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   signal(SIGSEGV, &amp;dump);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   dummy_function ();</p>
<p>            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   return 0;<br />
            }</td>
</tr>
</tbody>
</table>
<p>编译运行效果如下:</p>
<table align="center" style="border: 1px solid rgb(153, 153, 153); font-size: 12px; width: 80%;">
<tbody>
<tr>
<td>xiaosuo@gentux test $ gcc -g -rdynamic f.c<br />
            xiaosuo@gentux test $ ./a.out<br />
            GNU gdb 6.5<br />
            Copyright (C) 2006 Free Software Foundation, Inc.<br />
            GDB is free software, covered by the GNU General Public License, and you are<br />
            welcome to change it and/or distribute copies of it under certain conditions.<br />
            Type &quot;show copying&quot; to see the conditions.<br />
            There is absolutely no warranty for GDB.  Type &quot;show warranty&quot; for details.<br />
            This GDB was configured as &quot;i686-pc-linux-gnu&quot;&#8230;Using host libthread_db library &quot;/lib/libthread_db.so.1&quot;.</p>
<p>            Attaching to program: /home/xiaosuo/test/a.out, process 9563<br />
            Reading symbols from /lib/libc.so.6&#8230;done.<br />
            Loaded symbols for /lib/libc.so.6<br />
            Reading symbols from /lib/ld-linux.so.2&#8230;done.<br />
            Loaded symbols for /lib/ld-linux.so.2<br />
            0xffffe410 in __kernel_vsyscall ()<br />
            (gdb) bt<br />
            #0  0xffffe410 in __kernel_vsyscall ()<br />
            #1  0xb7ee4b53 in waitpid () from /lib/libc.so.6<br />
            #2  0xb7e925c9 in strtold_l () from /lib/libc.so.6<br />
            #3  0&#215;08048830 in dump (signo=11) at f.c:22<br />
            #4  &lt;signal handler called&gt;<br />
            #5  0x0804884c in dummy_function () at f.c:31<br />
            #6  0&#215;08048886 in main () at f.c:38</td>
</tr>
</tbody>
</table>
<p>怎么样？是不是依旧很酷？<br />
以上方法都是在系统上有gdb的前提下进行的，如果没有呢？其实glibc为我们提供了此类能够dump栈内容的函数簇，详见/usr/include/execinfo.h（这些函数都没有提供man page，难怪我们找不到），另外你也可以通过<a href="http://www.gnu.org/software/libc/manual/html_node/Backtraces.html" target="_blank"><u>gnu的手册</u></a>进行学习。<br />
<span style="font-weight: bold;">4.利用backtrace和objdump进行分析:</span><br />
重写的代码如下:</p>
<table align="center" style="border: 1px solid rgb(153, 153, 153); font-size: 12px; width: 80%;">
<tbody>
<tr>
<td>#include &lt;execinfo.h&gt;<br />
            #include &lt;stdio.h&gt;<br />
            #include &lt;stdlib.h&gt;<br />
            #include &lt;signal.h&gt;</p>
<p>            /* A dummy function to make the backtrace more interesting. */<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   void<br />
            dummy_function (void)<br />
            {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   unsigned char *ptr = 0&#215;00;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   *ptr = 0&#215;00;<br />
            }</p>
<p>            void dump(int signo)<br />
            {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   void *array[10];<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   size_t size;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   char **strings;<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   size_t i;</p>
<p>            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   size = backtrace (array, 10);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   strings = backtrace_symbols (array, size);</p>
<p>            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   printf (&quot;Obtained %zd stack frames.\n&quot;, size);</p>
<p>            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   for (i = 0; i &lt; size; i++)<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   printf (&quot;%s\n&quot;, strings[i]);</p>
<p>            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   free (strings);</p>
<p>            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   exit(0);<br />
            }</p>
<p>            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   int<br />
            main (void)<br />
            {<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   signal(SIGSEGV, &amp;dump);<br />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   dummy_function ();</p>
<p>            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   return 0;<br />
            }</td>
</tr>
</tbody>
</table>
<p>编译运行结果如下：</p>
<table align="center" style="border: 1px solid rgb(153, 153, 153); font-size: 12px; width: 80%;">
<tbody>
<tr>
<td>xiaosuo@gentux test $ gcc -g -rdynamic g.c<br />
            xiaosuo@gentux test $ ./a.out<br />
            Obtained 5 stack frames.<br />
            ./a.out(dump+0&#215;19) [0x80486c2]<br />
            [0xffffe420]<br />
            ./a.out(main+0&#215;35) [0x804876f]<br />
            /lib/libc.so.6(__libc_start_main+0xe6) [0xb7e02866]<br />
            ./a.out [0x8048601]</td>
</tr>
</tbody>
</table>
<p>这次你可能有些失望,似乎没能给出足够的信息来标示错误,不急,先看看能分析出来什么吧,用objdump反汇编程序,找到地址0x804876f对应的代码位置:</p>
<table align="center" style="border: 1px solid rgb(153, 153, 153); font-size: 12px; width: 80%;">
<tbody>
<tr>
<td>xiaosuo@gentux test $ objdump -d a.out</td>
</tr>
</tbody>
</table>
<p></p>
<table align="center" style="border: 1px solid rgb(153, 153, 153); font-size: 12px; width: 80%;">
<tbody>
<tr>
<td>8048765:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   e8 02 fe ff ff&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   call&nbsp;&nbsp;   804856c &lt;signal@plt&gt;<br />
            804876a:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   e8 25 ff ff ff&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   call&nbsp;&nbsp;   8048694 &lt;dummy_function&gt;<br />
<span style="color: rgb(255, 1, 2);">804876f</span>:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   b8 00 00 00 00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   mov&nbsp;&nbsp;&nbsp;   $0&#215;0,%eax<br />
            8048774:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   c9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   leave</td>
</tr>
</tbody>
</table>
<p>我们还是找到了在哪个函数(dummy_function)中出错的,信息已然不是很完整,不过有总比没有好的啊!<br />
<span style="font-weight: bold;">后记:</span><br />
本文给出了分析&quot;段错误&quot;的几种方法,不要认为这是与孔乙己先生的&quot;回&quot;字四种写法一样的哦,因为每种方法都有其自身的适用范围和适用环境,请酌情使用,或遵医嘱。</div>
</div>
<div class="content">
<p align="right">责任编辑 webmaster</p>
<p align="right">
<p align="right">&#8212;&#8212;&#8211;from  http://www.yuanma.org/data/2008/0818/article_3139.htm</p>
<p align="right">
<p align="right">
</div>
<div style=float:left><!-- JiaThis Button BEGIN -->
<div id="jiathis_style_32x32">
	<a class="jiathis_button_qzone"></a>
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_tqq"></a>
	<a class="jiathis_button_renren"></a>
	<a class="jiathis_button_kaixin001"></a>
	<a href="http://www.jiathis.com/share/" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
	<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" src="http://v2.jiathis.com/code/jia.js" charset="utf-8"></script>
<!-- JiaThis Button END --></div><table class="wumii-related-items" cellspacing="0" cellpadding="2" border="0" width="100%" style="clear: both;">
    
    <tr>
        <td ><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F08%2F10609%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">GCC中的pie和fpie选项</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F20%2F9981%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">zz  Linux内核 编程风格</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F03%2F08%2F10709%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">GNU工具链学习笔记</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2010%2F01%2F09%2F10623%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">反汇编器和汇编器</font>
                    </a>
                </td>
            </tr>
            <tr>
                <td style="margin: 0 !important; padding: 0 !important; line-height: 20px !important;">
                    <img border="0" src="http://static.wumii.com/images/widget/widget_solidPoint.gif">
                    <a target="_blank" style="text-decoration: none !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.lingcc.com%2F2008%2F02%2F27%2F9904%2F&from=http%3A%2F%2Fwww.lingcc.com%2F2008%2F08%2F27%2F9982%2F">
                        <font size="-1" color="#333333" style="line-height: 1.65em; font-size: 12px !important;">gentoo的相关配置-make.conf</font>
                    </a>
                </td>
            </tr>
    
    <tr>
        <td  align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table><p><h3>相关文章：</h3><ul>
<li><a href='http://www.lingcc.com/2010/01/21/10663/' rel='bookmark' title='小例子&#8211;回眸一笑百媚生'>小例子&#8211;回眸一笑百媚生</a></li>
<li><a href='http://www.lingcc.com/2009/12/31/10534/' rel='bookmark' title='Gentoo中的交叉编译利器&#8211;crossdev'>Gentoo中的交叉编译利器&#8211;crossdev</a></li>
<li><a href='http://www.lingcc.com/2010/03/08/10709/' rel='bookmark' title='GNU工具链学习笔记'>GNU工具链学习笔记</a></li>
<li><a href='http://www.lingcc.com/2010/10/12/11305/' rel='bookmark' title='如何让程序屏蔽CTRL+C和CTRL+Z'>如何让程序屏蔽CTRL+C和CTRL+Z</a></li>
<li><a href='http://www.lingcc.com/2010/01/05/10588/' rel='bookmark' title='用crossdev装龙芯的n32 gnu交叉工具链'>用crossdev装龙芯的n32 gnu交叉工具链</a></li>
<li><a href='http://www.lingcc.com/2010/01/07/10594/' rel='bookmark' title='使用crossdev安装龙芯的O32 gnu交叉工具链'>使用crossdev安装龙芯的O32 gnu交叉工具链</a></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://www.lingcc.com/2008/08/27/9982/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

