在看汇编器源码的过程中,对汇编器的一些个人理解:
binutils工具链包括很多的工具,下面仅列出常用的几个工具:
ld链接器 将多个目标文件,链接成一个可执行文件(或目标库文件)。
as汇编器 将汇编源代码,编译为(目标)机器代码
readelf 显示ELF格式的(目标)文件的信息
objdump 显示目标文件中的信息(反汇编)
objcopy 拷贝并翻译(转换)文件,可用于不同格式的二进制文件的转换
gprof 显示配置信息
nm 列出目标文件中的符号
ar 用来操作(.a)档案文件,比如创建,修改,提取内容等
addr2line 将地址转换为(文件名和)行号的工具,一般主要用于反汇编(带确认此点)
gold 一个新的,速度更快的,只针对于ELF的链接器,当前出于测试中,还不是很成熟稳定
在binutils中有一个BFD文件夹,BFD主要用来隔离可执行的二进制文件和具体的操作,它将各种工具和具体的文件格式分离开来,binutils中的所有工具都是通过BFD来操作二进制代码的,如果修改或者添加一种可执行文件的格式,我们只需要修改BFD的后端代码即可,不涉及到其他的工具的修改。
汇编过程中使用的一些主要函数:
1 int md_parse_option(optc,optarg) 机器相关对命令行选项处理(default处理)
2 void md_show_usage(stdout) 输出机器相关选项帮助文件
3 void md_pop_insert() 在po_hash中插入伪指令符号
4 oid md_begin() 开始第一pass前的准备工作
5 void md_assemble(s) 对s(行指令)进行汇编处理
6 int md_estimate_size_before_relax(fragP,segment) 返回frag需要增长的长度值
7 void md_convert_frag(stdoutput,sec,fragP) 调整rs_machine_dependent类型fragP的大小,满足sec的要求
8 valueT md_section_align(seg,size) 调整seg的大小,使之对齐边间
9 void md_apply_fix (fixP, &add_number, this_segment); 将add_number添加到对应的fixP中
10 arelent* tc_gen_reloc (sec, fixp) 生成重定位信息
11 void md_number_to_chars(ptr,use,nbytes) 根据大端还是小端,将数字转化为字符串
12 void md_operand(expression) 处理操作数,判断表达式
13 void md_atof(type,litP,sizeP) 字符串转换为浮点
14 symbolS * md_undefined_symbol (char *name) 处理未定义的符号
汇编器处理section时使用的主要函数:
1, add a new section {bfd_make_section}
2, Read data from section in BFD abfd into memory starting at location {bfd_get_section_contents}
3, Data is written to sections {bfd_set_section_contents}
4, section structure {typedef struct bfd_section}
5, Check whether the BFD is of a particular format {bfd_check_format}
6, Set the format of a BFD which was created for output. {bfd_set_format}
7, Write out the contents of the BFD in the given format {bfd_write_contents}
8, allocate all target specific information {bfd_alloc}