一. 安装bfd库

libbfd(Binary File Descriptor library是binutils中附带的一个C库。

从 http://ftp.gnu.org/gnu/binutils 下载binutils。

我们这里下载binutils-2.23.2.tar.gz

下载后,解压安装libbfd。方法如下:

tar -xzf binutils-2.23.2.tar.gz

cd binutils-2.23.2/bfd

./configure

make

make install

安装完成后,可以看到有如下文件生成。

/usr/local/include/bfd.h

/usr/local/lib/libbfd.a

二. 使用bfd库需要注意的地方

1. 头文件包含

程序使用bfd,需要包含bfd.h头文件。

但是,在包含bfd.h之前,还需要包含config.h。

即代码中需要有如下形式的文件包含:

#include "config.h"

#include

那么这个config.h是什么东东呢?他不是系统的头文件,也不是bfd库的头文件,而是应用程序自己的头文件。

然而用户可能会感到奇怪,我的应用程序源码中并不存在这个config.h文件。那是因为你开发应用时,没有采用GNU autotools。

采用GNU autotools的项目,在编译前一般都会执行一下configure脚本,生成Makefile及config.h文件。

那么,对于没有使用GNU autotools的应用,怎么解决呢?本文这里提供一个简单的config.h文件。

这个文件的内容,相当于是使用GNU autotools开发一个hello world项目而得到的config.h。

/* config.h.  Generated from config.h.in by configure.  */

/* config.h.in.  Generated from configure.ac by autoheader.  */

/* Name of package */

#define PACKAGE "hello"

/* Define to the address where bug reports for this package should be sent. */

#define PACKAGE_BUGREPORT "bug-report@address"

/* Define to the full name of this package. */

#define PACKAGE_NAME "hello"

/* Define to the full name and version of this package. */

#define PACKAGE_STRING "hello 1.0"

/* Define to the one symbol short name of this package. */

#define PACKAGE_TARNAME "hello"

/* Define to the home page for this package. */

#define PACKAGE_URL ""

/* Define to the version of this package. */

#define PACKAGE_VERSION "1.0"

/* Version number of package */

#define VERSION "1.0"

2. 链接

需要带上这几个库:bfd iberty  dl z

例如,假设hello.c是一个完整的使用bfd库的程序,则他的编译方法如下。

gcc hello.c -lbfd -liberty  -ldl -lz

三. 应用实例

#include

#include

#include "config.h"

#include

#include

#include

/*

这里定义3个static变量,并把他们放到一个单独的section中。

后面,我们通过bfd找出这个section,并得到这3个变量的内容。

同时,我们还通过符号查找操作,找到a_haha这个static变量的信息。

*/

static uint64_t  a_haha   __attribute__((section ("my_test_sec"))) =3;

static uint64_t  b        __attribute__((section ("my_test_sec"))) =7;

static uint64_t  c        __attribute__((section ("my_test_sec"))) =8;

/* 获取当前进程自己的elf文件路径 */

int get_self_path(char *buf, int buf_len)

{

int ret = readlink("/proc/self/exe", buf, buf_len);

buf[ret]='\0';

return ret;

}

void section_proc(bfd *abfd,  asection *sect, PTR obj)

{

if (strcmp(sect->name, "my_test_sec")==0)

printf("section %s exists\n", sect->name);

}

void search_a_given_symbol(bfd *ibfd, const char *name)

{

long storage_needed;

asymbol **symbol_table;

long number_of_symbols;

long i;

symbol_info symbolinfo ;

storage_needed = bfd_get_symtab_upper_bound(ibfd);

symbol_table =  (void *)(unsigned long)xmalloc(storage_needed);

number_of_symbols =  bfd_canonicalize_symtab (ibfd, symbol_table);

printf("Scanning %i symbols\n", number_of_symbols);

for(i=0;i

{

if (symbol_table[i]->section==NULL) continue;

bfd_symbol_info(symbol_table[i],&symbolinfo);

if (strcmp(name, symbolinfo.name))  continue;

printf("Section %s  ",symbol_table[i]->section->name);

printf("Symbol \"%s\"  value 0x%x\n",

symbolinfo.name, symbolinfo.value);

}

}

int main()

{

char our_self_path[1024];

bfd *ibfd;

char **matching;

asection *psection;

bfd_init();

get_self_path(our_self_path, sizeof(our_self_path));

printf("our elf file path:%s\n", our_self_path);

ibfd = bfd_openr(our_self_path, NULL);

bfd_check_format_matches(ibfd, bfd_object, &matching);

printf("number of sections = %d\n", bfd_count_sections(ibfd));

/* 遍历所有section,让section_proc对每一个section进行处理 */

bfd_map_over_sections(ibfd,  section_proc,  NULL);

/* 查找特定名称的section,打印出其信息 */

psection = bfd_get_section_by_name(ibfd, "my_test_sec");

printf("section name=%s; start_address=0x%llx; size=%d\n"

, psection->name

, psection->vma

, psection->size);

/* 打印出my_test_sec section中的3个uint64_t变量的值 */

{

uint64_t *pu64 = (void *) psection->vma;

printf("%lu %lu %lu \n", pu64[0], pu64[1], pu64[2]);

}

printf("address of a_haha=%p\n", &a_haha);

/* 遍历所有符号,以找出名称为a_haha的符号 */

search_a_given_symbol(ibfd, "a_haha");

return 0;

}

最后,程序运行效果如下:

[root@smb test]# gcc hello.c -lbfd -liberty  -ldl -lz

[root@smb test]# ./a.out

our elf file path:/home/shared/sunmingbao/test/a.out

number of sections = 34

section my_test_sec exists

section name=my_test_sec; start_address=0x6c9a28; size=24

3 7 8

address of a_haha=0x6c9a28

Scanning 1905 symbols

Section my_test_sec  Symbol "a_haha"  value 0x6c9a28

[root@smb test]#

elf 取路径_利用libbfd获取elf可执行文件的section(节)及symbol(符号)信息相关推荐

  1. elf 取路径_PatchELF 修改linux下elf文件library搜索路径

    http://nixos.org/patchelf.html wget http://nixos.org/releases/patchelf/patchelf-0.8/patchelf-0.8.tar ...

  2. igs无法分配驱动器映射表_利用VBA获取驱动器的信息

    大家好,我们今日讲解"VBA信息获取与处理"教程中第十七个专题"文件及文件夹信息的获取及操作"的第三节"利用VBA获取文件的信息和属性",这 ...

  3. vba monthview控件64位_利用VBA获取文件的信息和属性

    大家好,我们今日讲解"VBA信息获取与处理"教程中第十七个专题"文件及文件夹信息的获取及操作"的第三节"利用VBA获取文件的信息和属性",这 ...

  4. python过去日期_利用python获取当前日期前后N天或N月日期的方法示例

    前言 最近因为工作原因,发现一个Python的时间组件,很好用分享出来!(忘记作者名字了,在这里先感谢了),下面话不多说,来一起看看详细的介绍吧. 示例代码: # -*- coding: utf-8 ...

  5. python获取window共享目录列表_利用Python获取DICOM RTstructure勾画列表

    在<利用Python打开DICOM CT文件>一文中,我们利用pydicom.dcmread()读取了CT图像.本文中我们将修改load_scan()函数来读取RTstructure文件并 ...

  6. python自动保存ping结果_利用python获取Ping结果示例代码

    前言 本文主要跟大家分享了关于利用python获取Ping结果的相关内容,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍吧. 示例代码: # -*- coding: utf-8 -*- i ...

  7. python读取sas数据集_利用Python获取SAS和R自带数据集

    图:北京-奥森公园-2018年4月 无论是SAS.R还是Python,本身都自带一些数据集,对于初学者来说,可以通过这些自带的小数据集进行编程练习,无疑是非常方便的.SAS.R作为统计分析软件,本身自 ...

  8. python百度地图显示路径_百度地图获取规划路径信息

    本文意在解决通过制定两点坐标获取百度搜索的路径结果信息,用途是重现路径(比如在mapv上绘制轨迹显示效果图等). 实现思路: 加载百度地图所需的js引用: html布局用于显示结果: 初始化百度地图并 ...

  9. js获取classname值_利用js获取元素class值的两种方法

    我们有时为了达到某种效果,需要以元素的class值为条件做判断. 我们如何利用JavaScript获取元素class的值?我们先看下面代码: x=document.getElementsByTagNa ...

最新文章

  1. Firewalld共享上网及本地yum仓库搭建步骤
  2. format函数使用matlab,Matlab基本函数-format函数
  3. bzoj5108 数据_成都day3t3
  4. ckEditor使用JS代码调用的方法
  5. 第三篇:C++ 中的几种初始化
  6. [WCF编程]10.操作:请求/应答操作
  7. 如何修改远程桌面连接端口 ,历史记录,支持多用户
  8. gorm软删除_删除 |《GORM 中文文档 v1》| Go 技术论坛
  9. PCWorld:谷歌成下一个微软式企业10大理由
  10. 电子表格计算机操作题及素材,2015计算机应用基础-excel电子表格题目.doc
  11. DB2入门(5)——DB2配置文件
  12. U盘大容量存储设备 感叹号,错误10
  13. 【ionic】无法识别ionic
  14. 如何做一个基于JAVA房产中介预约看房系统毕业设计毕设作品(springboot框架)
  15. 3-maven学习-学习创建maven的依赖属性
  16. 谢国忠-高房价伤害老百姓,破坏社会稳定
  17. [转载]使用J2WTK2.2编译和运行第一个J2ME的HelloWorld程序
  18. B2C电子商务网站运营推广的优化技巧
  19. 计算机毕设 SpringBoot+Vue幼儿园管理系统 幼儿园信息管理系统 智慧幼儿园管理系统Java Vue MySQL数据库 远程调试 代码讲解
  20. linux冷备份oracle数据库,Linux余Windows系统Oracle数据库简单冷备同步

热门文章

  1. 【SpringCloud】Hystrix断路器【五】
  2. 【云原生】DevOps(八):Jenkins集成Kubernetes
  3. 视频帧率和显示器刷新率的关系
  4. 香肠派对显示服务器断开怎么解决,《香肠派对》这些bug都会解决的 匹配自动取消如何快速解决...
  5. 录录(高清录屏) - Video321 提高开会效率
  6. Android Studio中logcat没有工具栏
  7. C 输入一行字符,输出其中字母的个数。
  8. 大脑笔记:古诗词思维导图涂鸦记忆法
  9. python将想要打印的数据输出到txt文件中,打印省略号里面的内容
  10. Android Bitmap详解