u-boot移植随笔(0):System.map文件格式

背景:
正在努力看u-boot代码,已经研究了两三天,内容太多,一时难理清头绪。不过有一定的linux基础,也懂点gnu扩展,elf格式,连接器还没有深入研究。
Linux编译内核后会生成System.map,u-boot编译后也生成System.map——还有u-boot.map,后者比前者多一些内容。

.map文件格式为:

1
地址     类型     符号  

比如u-boot生成的System.map文件开始的几个:

1
2
3
4
5
6
7
8
9
10
11
12
13
33f80000 T _start
33f80020 t _undefined_instruction
33f80024 t _software_interrupt
33f80028 t _prefetch_abort
33f8002c t _data_abort
33f80030 t _not_used
33f80034 t _irq
33f80038 t _fiq
33f80040 t _TEXT_BASE
33f80044 T _armboot_start
33f80048 T _bss_start
33f8004c T _bss_end
33f80050 t start_code

它们的地址以及符号名称不是本文重点,此处不提。
下面写一下符号类型的解释,大写表示global,相应的小写表示是local。前面几个是经常使用到的:

A

The symbol’s value is absolute, and will not be changed by further linking.
绝对地址,不会再被连接(重定向?)

B

The symbol is in the uninitialized data section (known as BSS).
未初始化数据段,常以BSS表示

D

The symbol is in the initialized data section.
数据段

T

The symbol is in the text (code) section.
代码段

U

The symbol is undefined.
未定义符号

R

The symbol is in a read only data section.
只读数据

W

The symbol is a weak symbol that has not been specifically tagged as a weak object symbol. When a weak defined symbol is linked with a normal defined symbol, the normal defined symbol is used with no error. When a weak undefined symbol is linked and the symbol is not defined, the value of the weak symbol becomes zero with no error.
这个是weak连接,后面简单提一下

C

The symbol is common. Common symbols are uninitialized data. When linking, multiple common symbols may appear with the same name. If the symbol is defined anywhere, the common symbols are treated as undefined references. For more details on common symbols, see the discussion of -warn-common in Linker options.

G

The symbol is in an initialized data section for small objects. Some object file formats permit more efficient access to small data objects, such as a global int variable as opposed to a large global array.

I

The symbol is an indirect reference to another symbol. This is a GNU extension to the a.out object file format which is rarely used.

N

The symbol is a debugging symbol.

S

The symbol is in an uninitialized data section for small objects.

V

The symbol is a weak object. When a weak defined symbol is linked with a normal defined symbol, the normal defined symbol is used with no error. When a weak undefined symbol is linked and the symbol is not defined, the value of the weak symbol becomes zero with no error.

-

The symbol is a stabs symbol in an a.out object file. In this case, the next values printed are the stabs other field, the stabs desc field, and the stab type. Stabs symbols are used to hold debugging information. For more information, see Stabs.

?

The symbol type is unknown, or object file format specific.
(上述信息来自http://forum.eepw.com.cn/thread/128712/1,自己再添加点内容

现在提一下W那个类型,在u-boot生成的System.map文件也存在一些这种类型的,举个例子:

1
33f80664 W coloured_LED_init

这个函数在board.c文件中是这样的:

1
2
void inline __coloured_LED_init (void) {}
void inline coloured_LED_init (void) __attribute__((weak, alias("__coloured_LED_init")));

看到没有?第二行后面的__attribute__((weak, alias("__coloured_LED_init")))就是将它声明为weak连接,其中alias同linux shell的那个alias。这种连接大约是说,如果定义了某个函数就使用这个函数,如果没有,它就是一个空函数,啥也不做。
(__attribute__太复杂,很难一下子讲清楚)
《linux性能详解》一书中讲到了nm的使用方法,也提到一下上面那些类型,其实,使用nm foo的结果就是.map的格式。
后话:那本书是电子版,出版后的书名不是这个,叫《嵌入式Linux内存使用与性能优化》,书很好,不过内容有点深,看过一次只是有点大概印象,惭愧。