.rel文件格式相关
sdas\doc\format.txt 原文
2.5.1 Object Module Format
The first line of an object module contains the [XDQ][HL]
format specifier (i.e. XH indicates a hexidecimal file with
most significant byte first) for the following designators.
2.5.2 Header Line
H aa areas gg global symbols
The header line specifies the number of areas(aa) and the
number of global symbols(gg) defined or referenced in this ob-
ject module segment.
2.5.3 Module Line
M name
The module line specifies the module name from which this
header segment was assembled. The module line will not appear
if the .module directive was not used in the source program.
2.5.4 Symbol Line
S string Defnnnn
or
S string Refnnnn
The symbol line defines (Def) or references (Ref) the symbol
'string' with the value nnnn. The defined value is relative to
the current area base address. References to constants and
external global symbols will always appear before the first area
definition. References to external symbols will have a value of
zero.
2.5.5 Area Line
A label size ss flags ff
The area line defines the area label, the size (ss) of the
area in bytes, and the area flags (ff). The area flags specify
the ABS, REL, CON, OVR, and PAG parameters:
OVR/CON (0x04/0x00 i.e. bit position 2)
ABS/REL (0x08/0x00 i.e. bit position 3)
PAG (0x10 i.e. bit position 4)
2.5.6 T Line
T xx xx nn nn nn nn nn ...
The T line contains the assembled code output by the assem-
bler with xx xx being the offset address from the current area
base address and nn being the assembled instructions and data in
byte format.
2.5.7 R Line
R 0 0 nn nn n1 [n1x] n2 xx xx ...
The R line provides the relocation information to the linker.
The nn nn value is the current area index, i.e. which area the
current values were assembled. Relocation information is en-
coded in groups of 4 (possibly 5) bytes:
1. n1 (and optionally n1x) is the relocation mode and object
format:
1. bit 0 word(0x00)/byte(0x01)
2. bit 1 relocatable area(0x00)/symbol(0x02)
3. bit 2 normal(0x00)/PC relative(0x04) relocation
4. bit 3 1-byte(0x00)/2-byte(0x08) object format for
byte data
5. bit 4 signed(0x00)/unsigned(0x10) byte data
6. bit 5 normal(0x00)/page '0'(0x20) reference
7. bit 6 normal(0x00)/page 'nnn'(0x40) reference
8. bit 7 LSB byte(0x00)/MSB byte(0x80) with 2-byte
mode
9. bit 8 1 or 2 (0x00)/3-byte (0x100) object format
for byte data.
10. bit 9 LSB or MSB (middle byte) (0x00) or byte 3
(real MSB) (0x200) for 3-byte mode.
If the upper four bits of n1 are set (i.e.
(n1 & 0xf0) == 0xf0), it is taken as an escape character,
and the relocation mode will consist of the lower four bits
of n1 left shifted 8 bits or'ed with the value of n1x. If
the upper four bits of n1 are not all set, then it is not an
escape character, and the n1x byte is not present.
This escape mechanism allows a 12-bit relocation mode value.
Note that in byte mode, when 3-byte mode is used (bits 0
and 8 are both set), the MSB bit (bit 7) really refers to
the 16 bit MSB (the middle byte of the 24-bit value) while
the "byte 3" bit (bit 9) refers to the 24-bit MSB.
2. n2 is a byte index into the corresponding (i.e. pre-
ceeding) T line data (i.e. a pointer to the data to be
updated by the relocation). The T line data may be
1-byte or 2-byte byte data format or 2-byte word
format.
3. xx xx is the area/symbol index for the area/symbol be-
ing referenced. the corresponding area/symbol is found
in the header area/symbol lists.
The groups of 4 bytes are repeated for each item requiring relo-
cation in the preceeding T line.
2.5.8 P Line
P 0 0 nn nn n1 n2 xx xx
The P line provides the paging information to the linker as
specified by a .setdp directive. The format of the relocation
information is identical to that of the R line. The correspond-
ing T line has the following information:
T xx xx aa aa bb bb
Where aa aa is the area reference number which specifies the
selected page area and bb bb is the base address of the page.
bb bb will require relocation processing if the 'n1 n2 xx xx' is
specified in the P line. The linker will verify that the base
address is on a 256 byte boundary and that the page length of an
area defined with the PAG type is not larger than 256 bytes.
The linker defaults any direct page references to the first
area defined in the input REL file. All ASxxxx assemblers will
specify the _CODE area first, making this the default page area.
ASxxxx 目标文件(.rel)格式详解
概述
.rel 文件是 ASxxxx 汇编器生成的可重定位目标文件格式,SDCC 使用这个格式作为中间文件格式,官方格式说明文件路径为sdas/doc/format.txt。本文档详细解释 .rel 文件的完整格式规范。
1. 文件格式标识符 (Format Specifier)
1.1 格式说明
文件的第一行是格式标识符:
XH3
格式:[XDQ][HL]
含义:
| 标识符 |
含义 |
X |
扩展格式(Extended format) |
H |
十六进制文件(Hexadecimal file) |
D |
十进制文件(Decimal file) |
Q |
八进制文件(Octal file) |
L |
高位字节在先(Little-endian / Most significant byte first) |
H |
高位字节在先(Most significant byte first) |
最常见标识符:
| 标识符 |
含义 |
XH3 |
扩展格式,十六进制,高位字节在前,版本3 |
QH3 |
扩展格式,八进制,高位字节在前,版本3 |
SD |
标准格式,十进制 |
在我们的例子中:
XH3
表示这是扩展格式的十六进制文件,高位字节在前,版本3。
2. 头部行 (Header Line)
2.1 格式定义
H aa areas gg global symbols
2.2 字段说明
| 字段 |
含义 |
示例 |
H |
头部行标识符 |
H |
aa |
段数量(十六进制,2位) |
1A = 26个段 |
gg |
全局符号数量(十六进制,2位) |
0B = 11个符号 |
2.3 实际示例
H 1A areas 0B global symbols
解析:
1A = 26(十进制):本模块定义了26个内存段
0B = 11(十进制):本模块定义了11个全局符号
2.4 在 hello51.rel 中的应用
H 1A areas 0B global symbols
表示:
3. 模块行 (Module Line)
3.1 格式定义
M name
3.2 字段说明
| 字段 |
含义 |
说明 |
M |
模块行标识符 |
M |
name |
模块名称 |
源文件名(不含扩展名) |
3.3 说明
- 如果源程序中使用了
.module 伪指令,则会生成此行
- 模块名通常是源文件名(不含扩展名)
3.4 实际示例
M hello51
解析:模块名称为 hello51,表示这个目标文件是从 hello51.asm 汇编生成的。
4. 符号行 (Symbol Line)
4.1 格式定义
S string Defnnnn
或
S string Refnnnn
4.2 字段说明
| 字段 |
含义 |
示例 |
S |
符号行标识符 |
S |
string |
符号名称 |
_main, _P2 |
Defnnnn |
定义符号(相对地址) |
Def000000, Def0000A0 |
Refnnnn |
引用符号(外部引用) |
Ref000000 |
4.3 定义符号 (Def)
定义符号表示在本模块中定义的符号,地址值是相对于当前段基址的偏移。
格式:Def + 6位十六进制地址
示例:
S _main Def000000 ← _main 函数定义在偏移 0x0000
S _P2 Def0000A0 ← _P2 端口定义在偏移 0x00A0
4.4 引用符号 (Ref)
引用符号表示在其他模块中定义的符号,本模块只引用,不定义。
格式:Ref + 6位十六进制地址(通常为000000)
示例:
S __mcs51_genRAMCLEAR Ref000000 ← 引用外部函数 __mcs51_genRAMCLEAR
4.5 重要规则
引用符号优先规则:对常量和外部全局符号的引用必须出现在第一个段定义之前。
4.6 实际示例
S __mcs51_genRAMCLEAR Ref000000
S _P2M0 Def000096
S .__.ABS. Def000000
S _P2M1 Def000095
S _P2 Def0000A0
S __sdcc_gsinit_startup Ref000000
S __start__stack Def000000
S __sdcc_program_startup Def000003
S _main Def000000
符号表解析:
| 序号 |
符号名 |
类型 |
地址/偏移 |
说明 |
| 0 |
__mcs51_genRAMCLEAR |
Ref |
- |
RAM清零函数(外部引用) |
| 1 |
_P2M0 |
Def |
0x96 |
P2M0寄存器(相对地址) |
| 2 |
.__.ABS. |
Def |
0x00 |
绝对地址段 |
| 3 |
_P2M1 |
Def |
0x95 |
P2M1寄存器 |
| 4 |
_P2 |
Def |
0xA0 |
P2端口 |
| 5 |
__sdcc_gsinit_startup |
Ref |
- |
启动函数(外部引用) |
| 6 |
__start__stack |
Def |
0x00 |
栈起始位置 |
| 7 |
__sdcc_program_startup |
Def |
0x03 |
程序启动入口 |
| 8 |
_main |
Def |
0x00 |
main函数入口 |
5. 段行 (Area Line)
5.1 格式定义
A label size ss flags ff
5.2 字段说明
| 字段 |
含义 |
示例 |
A |
段行标识符 |
A |
label |
段名称 |
_CODE, CSEG, HOME |
size |
段大小关键字 |
size |
ss |
段大小(十六进制,4位) |
0017, 0000 |
flags |
段标志关键字 |
flags |
ff |
段标志值(十六进制) |
00, 20, 04, 08 |
5.3 段标志位 (Area Flags)
段标志用于描述段的属性,使用位掩码编码:
| 位位置 |
标志名 |
值 |
说明 |
| bit 0 |
- |
- |
未使用 |
| bit 1 |
- |
- |
未使用 |
| bit 2 |
OVR/CON |
0x04 / 0x00 |
覆盖/连接属性 |
| bit 3 |
ABS/REL |
0x08 / 0x00 |
绝对/相对地址 |
| bit 4 |
PAG |
0x10 |
分页属性 |
| bit 5-7 |
- |
- |
未使用 |
5.4 标志位详解
5.4.1 ABS/REL (bit 3)
| 值 |
属性 |
说明 |
0x00 |
REL |
可重定位段(Relocatable) |
0x08 |
ABS |
绝对地址段(Absolute) |
5.4.2 OVR/CON (bit 2)
| 值 |
属性 |
说明 |
0x00 |
CON |
连接属性(Contents in consecutive locations) |
0x04 |
OVR |
覆盖属性(Overlay) |
5.4.3 PAG (bit 4)
| 值 |
属性 |
说明 |
0x00 |
- |
非分页段 |
0x10 |
PAG |
分页段(需要页面边界检查) |
5.5 常用标志组合
| flags 值 |
含义 |
用途 |
00 |
REL + CON |
普通代码/数据段 |
04 |
REL + OVR |
覆盖段 |
08 |
ABS + CON |
绝对地址段(如SFR) |
20 |
ABS + CON + 初始化 |
已初始化段 |
5.6 实际示例
A _CODE size 0 flags 0 addr 0
A RSEG size 0 flags 8 addr 0
A REG_BANK_0 size 8 flags 4 addr 0
A SSEG size 1 flags 0 addr 0
A HOME size 6 flags 20 addr 0
A CSEG size 17 flags 20 addr 0
A GSFINAL size 3 flags 20 addr 0
段解析表:
| 段名 |
大小 |
flags |
addr |
说明 |
_CODE |
0 |
0x00 (REL) |
0 |
代码段 |
RSEG |
0 |
0x08 (ABS) |
0 |
寄存器段(SFR) |
REG_BANK_0 |
8 |
0x04 (OVR) |
0 |
工作寄存器组0 |
SSEG |
1 |
0x00 (REL) |
0 |
栈段 |
HOME |
6 |
0x20 |
0 |
中断向量区 |
CSEG |
23 |
0x20 |
0 |
主代码段 |
GSFINAL |
3 |
0x20 |
0 |
全局初始化结束段 |
5.7 SDCC 常用段名
| 段名 |
用途 |
类型 |
HOME |
中断向量表(0x0000-0x003F) |
CODE |
CSEG |
代码段 |
CODE |
DSEG |
数据段(内部RAM) |
DATA |
ISEG |
间接寻址段 |
IDATA |
XSEG |
外部数据段 |
XDATA |
BSEG |
位寻址段 |
BIT |
GSINIT |
全局初始化段 |
CODE |
GSFINAL |
全局初始化结束段 |
CODE |
CONST |
常量段 |
CODE |
XINIT |
外部数据初始化 |
CODE |
SSEG |
栈段 |
DATA |
RSEG |
寄存器段(SFR) |
ABS |
6. T行 - 代码数据行 (Text Line)
6.1 格式定义
T xx xx nn nn nn nn nn ...
6.2 字段说明
| 字段 |
含义 |
说明 |
T |
T行标识符 |
T |
xx xx |
偏移地址 |
相对于当前段基址的偏移(4位十六进制) |
nn nn nn nn nn... |
机器码 |
实际的指令和数据字节 |
6.3 说明
- T行包含汇编后的实际机器码
- 地址是段内偏移,不是绝对地址
- 字节数据使用十六进制表示
6.4 实际示例
T 00 00 00 02 00 01 75 95 00
解析:
| 部分 |
值 |
说明 |
T |
- |
T行标识符 |
00 00 |
0x0000 |
段内偏移地址 |
00 |
0x00 |
第一个数据字节 |
02 00 01 |
0x020001 |
指令字节(可能是LJMP或数据) |
75 95 00 |
0x75 0x95 0x00 |
mov _P2M1,#0x00 的机器码 |
6.5 机器码反汇编参考
51单片机指令编码:
| 操作码 |
指令 |
说明 |
75 |
MOV direct,#data |
2字节操作数指令 |
02 ll hh |
LJMP addr |
长跳转(3字节) |
12 ll hh |
LCALL addr |
长调用(3字节) |
80 hh |
SJMP offset |
短跳转(2字节,相对地址) |
A3 |
INC DPTR |
DPTR加1 |
6.6 完整示例
T 00 00 00 75 95 00 75 96 00 75 A0 FF
对应的汇编代码:
.org 0x0000 ; 地址 0x0000
; 空字节
.org 0x0003 ; 地址 0x0003
mov _P2M1,#0x00 ; 75 95 00
mov _P2M0,#0x00 ; 75 96 00
mov _P2,#0xFF ; 75 A0 FF
7. R行 - 重定位行 (Relocation Line)
7.1 格式定义
R 0 0 nn nn n1 [n1x] n2 xx xx ...
7.2 字段说明
| 字段 |
含义 |
说明 |
R |
R行标识符 |
R |
0 0 |
固定值 |
固定为两个0 |
nn nn |
段索引 |
当前段索引(用于定位) |
n1 [n1x] |
重定位模式 |
编码重定位类型 |
n2 |
T行索引 |
指向T行中需要重定位的字节位置 |
xx xx |
符号/段索引 |
引用的符号或段在符号表中的索引 |
7.3 重定位模式位字段 (n1)
n1 是一个位掩码,编码了10种不同的重定位属性:
| 位 |
属性 |
值=0 |
值=1 |
说明 |
| bit 0 |
数据类型 |
字(0x00) |
字节(0x01) |
操作数是字还是字节 |
| bit 1 |
引用类型 |
段(0x00) |
符号(0x02) |
引用的是段还是符号 |
| bit 2 |
寻址模式 |
普通(0x00) |
PC相对(0x04) |
普通重定位还是PC相对 |
| bit 3 |
字节宽度 |
1字节(0x00) |
2字节(0x08) |
操作数的字节宽度 |
| bit 4 |
符号性质 |
有符号(0x00) |
无符号(0x10) |
字节数据是否有符号 |
| bit 5 |
页模式 |
普通(0x00) |
页0(0x20) |
普通页还是特定页 |
| bit 6 |
页模式 |
普通(0x00) |
页编号(0x40) |
页模式变体 |
| bit 7 |
字节选择 |
LSB(0x00) |
MSB(0x80) |
2字节模式下的字节选择 |
| bit 8 |
3字节模式 |
1-2字节(0x00) |
3字节(0x100) |
3字节数据格式 |
| bit 9 |
3字节MSB |
中间字节(0x00) |
字节3(0x200) |
3字节模式下的MSB |
7.4 转义机制 (Escape Mechanism)
如果 n1 的高4位全部为1(即 n1 & 0xF0 == 0xF0),则 n1 作为转义字符:
实际模式值 = (n1 & 0x0F) << 8 | n1x
这允许12位的重定位模式值。
7.5 常用重定位模式
| n1 值 |
含义 |
说明 |
00 |
普通绝对地址 |
普通重定位,字数据 |
01 |
普通绝对地址 |
普通重定位,字节数据 |
02 |
符号引用 |
引用符号,字数据 |
03 |
符号引用 |
引用符号,字节数据 |
08 |
2字节绝对 |
2字节绝对地址 |
09 |
2字节引用 |
2字节符号引用 |
7.6 n2 - T行字节索引
n2 是一个字节索引,指向对应的T行数据中需要重定位的位置。
T行数据可以是:
7.7 xx xx - 符号/段索引
这是一个索引值,指向:
- 符号表中的符号(如果引用符号)
- 段表中的段(如果引用段)
索引从0开始,对应符号表或段表中的顺序位置。
7.8 实际示例
R 00 00 00 18
解析:
| 字段 |
值 |
说明 |
R |
- |
R行标识符 |
00 00 |
0, 0 |
固定值 |
00 |
0 |
重定位模式 = 0(普通绝对地址重定位) |
18 |
24(十进制) |
T行字节索引 = 24 |
含义:在当前段的偏移24字节处,有一个需要重定位的绝对地址,引用的是符号表中的第24个符号。
7.9 完整解析示例
T 00 00 00 02 00 00 02 00 01 75 95 00 75 96 00 75 A0 FF 00 00
R 00 00 00 17
R 00 00 00 18
对应汇编代码:
.org 0x0000
00 00 ; 填充字节
02 00 00 ; LJMP 目标地址(占位符)
02 00 01 ; LJMP 目标地址(占位符)
75 95 00 ; mov _P2M1,#0x00
75 96 00 ; mov _P2M0,#0x00
75 A0 FF ; mov _P2,#0xFF
00 00 ; 填充字节
重定位信息:
| R行 |
偏移 |
引用符号 |
R 00 00 00 17 |
3 |
符号17(可能是__sdcc_gsinit_startup) |
R 00 00 00 18 |
6 |
符号18(可能是__sdcc_program_startup) |
8. P行 - 分页信息行 (Paging Line)
8.1 格式定义
P 0 0 nn nn n1 n2 xx xx
8.2 字段说明
| 字段 |
含义 |
说明 |
P |
P行标识符 |
P |
0 0 |
固定值 |
固定为两个0 |
nn nn |
段索引 |
段索引 |
n1 |
重定位模式 |
与R行相同的重定位模式编码 |
n2 |
T行索引 |
指向T行中需要重定位的位置 |
xx xx |
符号/段索引 |
引用的符号或段索引 |
8.3 对应的T行格式
T xx xx aa aa bb bb
| 字段 |
含义 |
xx xx |
页区域内偏移 |
aa aa |
页区域引用编号 |
bb bb |
页基址(可能需要重定位) |
8.4 说明
- P行提供分页信息给链接器
- 由
.setdp 伪指令指定
- 与R行使用相同的重定位编码
- 链接器会验证页基址是否在256字节边界上
8.5 默认分页区域
链接器默认将直接页引用指向输入REL文件中定义的第一个段。所有ASxxxx汇编器都会首先指定_CODE段,使其成为默认页区域。
9. O行 - 选项行 (Options Line)
9.1 格式定义
O options
9.2 字段说明
| 字段 |
含义 |
O |
O行标识符 |
options |
汇编器选项字符串 |
9.3 说明
9.4 实际示例
O -mmcs51 --model-small
含义:这个目标文件使用mcs51架构和小内存模式编译。
10. 综合示例解析
10.1 hello51.rel 完整结构
XH3
H 1A areas 0B global symbols
M hello51
O -mmcs51 --model-small
S __mcs51_genRAMCLEAR Ref000000
S _P2M0 Def000096
S .__.ABS. Def000000
S _P2M1 Def000095
S _P2 Def0000A0
S __sdcc_gsinit_startup Ref000000
S __start__stack Def000000
S __sdcc_program_startup Def000003
S _main Def000000
A _CODE size 0 flags 0 addr 0
A RSEG size 0 flags 8 addr 0
A REG_BANK_0 size 8 flags 4 addr 0
A SSEG size 1 flags 0 addr 0
A HOME size 6 flags 20 addr 0
A CSEG size 17 flags 20 addr 0
A GSFINAL size 3 flags 20 addr 0
T 00 00 00 02 00 01 02 00 03 75 95 00 75 96 00 75 A0 FF 00 00
R 00 00 00 07
R 00 00 00 08
R 00 00 00 0D
10.2 详细解析
文件头
- 格式:
XH3 - 扩展格式,十六进制,高位字节在前,版本3
- 头部: 26个段,11个全局符号
- 模块:
hello51
- 选项:
-mmcs51 --model-small
符号表(9个符号)
__mcs51_genRAMCLEAR - 引用(外部函数)
_P2M0 - 定义(地址0x96)
.__.ABS. - 定义(绝对段)
_P2M1 - 定义(地址0x95)
_P2 - 定义(地址0xA0)
__sdcc_gsinit_startup - 引用(启动函数)
__start__stack - 定义(栈起始)
__sdcc_program_startup - 定义(入口点0x03)
_main - 定义(入口点0x00)
段定义(7个段)
_CODE - 代码段,0字节,可重定位
RSEG - SFR段,0字节,绝对地址
REG_BANK_0 - 寄存器组,8字节,可覆盖
SSEG - 栈段,1字节,可重定位
HOME - 向量区,6字节,初始化
CSEG - 主代码,23字节,初始化
GSFINAL - 初始化结束,3字节,初始化
代码数据
T 00 00 00 02 00 01 02 00 03 75 95 00 75 96 00 75 A0 FF 00 00
反汇编:
02 00 01 - LJMP 0x0001(重定位:符号7)
02 00 03 - LJMP 0x0003(重定位:符号8)
75 95 00 - MOV 0x95,#0x00
75 96 00 - MOV 0x96,#0x00
75 A0 FF - MOV 0xA0,#0xFF
00 00 - NOP
重定位信息
| R行 |
T行偏移 |
引用符号 |
说明 |
R 00 00 00 07 |
3 |
符号7 (__start__stack) |
第一个LJMP目标 |
R 00 00 00 08 |
6 |
符号8 (__sdcc_program_startup) |
第二个LJMP目标 |
R 00 00 00 0D |
- |
符号13 |
未知引用 |
11. 链接器处理过程
11.1 链接器任务
- 合并段: 将多个REL文件的同名段合并
- 分配地址: 为每个段分配绝对地址
- 解析符号: 将符号引用与定义匹配
- 处理重定位: 根据R行信息修正地址
- 生成输出: 生成最终的HEX或BIN文件
11.2 重定位过程示例
链接前(REL文件):
T 00 00 02 00 01 ; LJMP 0x0001(占位符)
R 00 00 00 07 ; 重定位到符号7
符号表:
符号7: __start__stack = 0x0080(在链接后的地址)
链接后(HEX文件):
地址 0x0000: 02 00 80 ; LJMP 0x0080(实际地址)
12. 附录
12.1 行类型速查表
| 行标识 |
名称 |
格式 |
用途 |
XH3 |
格式标识 |
XH3 |
文件格式版本 |
H |
头部行 |
H aa areas gg symbols |
段数和符号数 |
M |
模块行 |
M name |
模块名称 |
O |
选项行 |
O options |
汇编选项 |
S |
符号行 |
S name Defnnnn 或 S name Refnnnn |
符号定义或引用 |
A |
段行 |
A name size ss flags ff |
段定义 |
T |
数据行 |
T offset data... |
机器码数据 |
R |
重定位行 |
R mode idx n2 sym... |
重定位信息 |
P |
分页行 |
P mode idx n2 sym... |
分页信息 |
12.2 标志位速查
| flags |
二进制 |
ABS/REL |
OVR/CON |
PAG |
说明 |
00 |
00000000 |
REL |
CON |
- |
可重定位段 |
04 |
00000100 |
REL |
OVR |
- |
可覆盖段 |
08 |
00001000 |
ABS |
CON |
- |
绝对地址段 |
0C |
00001100 |
ABS |
OVR |
- |
绝对覆盖段 |
10 |
00010000 |
REL |
CON |
PAG |
分页段 |
20 |
00100000 |
ABS |
CON |
- |
初始化段 |