快乐虾
http://blog.csdn.net/lights_joy/
lights@hb165.com
本文适用于
bfinutils-2.19
vs2008
欢迎转载,但请保留作者信息
在bfd读取静态库文件时,有一个内存泄漏,以下是简单的测试代码:
bfd* f = ::bfd_openr("e://etools//debug//libcommon.a", NULL);
::bfd_check_format(f, ::bfd_archive);
::bfd_close(f);
这个时候有一堆的内存块没有释放,用_CrtMemDifference检查可以发现:
0 bytes in 0 Free Blocks.
28800 bytes in 9 Normal Blocks.
48 bytes in 2 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 52500 bytes.
Total allocations: 52604 bytes.
也就是有28800个字节的内存泄漏!查一下bfd读取静态库的代码,在bfd_generic_archive_p函数中有这样的代码段:
const bfd_target *
bfd_generic_archive_p (bfd *abfd)
{
………………………………..
if (bfd_has_map (abfd))
{
bfd *first;
/* This archive has a map, so we may presume that the contents
are object files. Make sure that if the first file in the
archive can be recognized as an object file, it is for this
target. If not, assume that this is the wrong format. If
the first file is not an object file, somebody is doing
something weird, and we permit it so that ar -t will work.
This is done because any normal format will recognize any
normal archive, regardless of the format of the object files.
We do accept an empty archive. */
first = bfd_openr_next_archived_file (abfd, NULL);
if (first != NULL)
{
first->target_defaulted = FALSE;
if (bfd_check_format (first, bfd_object)
&& first->xvec != abfd->xvec)
{
bfd_set_error (bfd_error_wrong_object_format);
bfd_ardata (abfd) = tdata_hold;
return NULL;
}
/* And we ought to close `first' here too. */
}
}
return abfd->xvec;
}
在这里,bfd先从文件头判断是否一个静态库文件,在得到肯定的结果后它将读取库中的第一个文件,用first这个变量来保存结果,但是在这里bfd并没有释放这个读取结果,这也是造成内存泄漏的原因!
按它的注释,在这里加上
bfd_close(first);
内存泄漏检测结果变成了:
0 bytes in 0 Free Blocks.
184 bytes in 2 Normal Blocks.
48 bytes in 2 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 52500 bytes.
Total allocations: 52604 bytes.
还有184个字节的内存泄漏!估计是bfd_openr_next_archived_file这个函数仍然有问题!
呵呵,代码决定一切,还是别迷信的好!
参考资料
bfd对多目标的支持( 2008-9-25 )
bfd对elf32格式的支持( 2008-11-7 )
objdump与readelf的区别( 2008-11-10 )
objdump代码分析( 2008-11-10 )
bfd的文件格式识别( 2008-11-10 )
bfd的后端数据表示( 2008-11-10 )