关于 ld 命令中 -Ttext 参数 的测试
文件sramboot.S
#include"regdef.h"
.text
.globl main
main:
.set noreorder
.set noat
la at, value
lw a0, 0(at)
lw a1, 4(at)
jal my_add
add a2, a0, a1
sw a2, 8(at)
文件#include"regdef.h"
.text
.globl my_add,value
my_add:
.set noreorder
add a0,a0,3
j ra
add a1,a1,4
.data
value: .word 10, 20, 0
Makefile文件
CC = mipsel-linux-gcc
AS = mipsel-linux-as
LD = mipsel-linux-ld
OBJDUMP = mipsel-linux-objdump
OBJCOPY = mipsel-linux-objcopy
TEXTBASE =
LDFLAGS = $(TEXTBASE) -T my.ld
test.bin:test.exe
$(OBJCOPY) -O binary test.exe test.bin
test.exe: call.o sramboot.o
$(LD) call.o sramboot.o $(LDFLAGS) -o test.exe
$(OBJDUMP) -D test.exe > test.dump
rm *.s
sramboot.o:sramboot.S
$(CC) -E sramboot.S -o sramboot.s
$(AS) sramboot.s -o sramboot.o
call.o:call.S
$(CC) -E call.S -o call.s
$(AS) call.s -o call.o
clean:
rm *.o *.exe *.bin *.dump
my.ld文件
OUTPUT_FORMAT("elf32-tradlittlemips","elf32-tradbigmips","elf32-tradlittlemips")
OUTPUT_ARCH(mips)
SECTIONS
{
.init :
{
sramboot.o
}
.text :
{
*(.text)
}
.data :
{
*(.data)
}
.bss :
{
*(.bss)
}
}
test.sh文件
#! /bin/sh
mipsel-linux-objdump -D -m mips -b binary -EL -M no-aliases -z test.bin >my.dump
产看原生机器码,可以运行test.sh
下只列出test.dump
测试1:
没有指定Ttext .=0
test.exe: file format elf32-tradlittlemips
Disassembly of section .init:
00000000 <main>:
0: 3c010000 lui at,0x0
4: 24210050 addiu at,at,80
8: 8c240000 lw a0,0(at)
c: 8c250004 lw a1,4(at)
10: 0c000008 jal 20 <my_add>
14: 00000000 nop
18: 00853020 add a2,a0,a1
1c: ac260008 sw a2,8(at)
Disassembly of section .text:
00000020 <my_add>:
20: 20840003 addi a0,a0,3
24: 03e00008 jr ra
28: 20a50004 addi a1,a1,4
2c: 00000000 nop
Disassembly of section .reginfo:
00000030 <.reginfo>:
30: 80000030 lb zero,48(zero)
...
Disassembly of section .data:
00000050 <value>:
50: 0000000a 0xa
54: 00000014 0x14
测试2:
指定 –Ttext = 0x50 .=0
test.exe: file format elf32-tradlittlemips
Disassembly of section .text:
00000050 <my_add>:
50: 20840003 addi a0,a0,3
54: 03e00008 jr ra
58: 20a50004 addi a1,a1,4
5c: 00000000 nop
Disassembly of section .reginfo:
00000060 <.reginfo>:
60: 80000030 lb zero,48(zero)
...
Disassembly of section .init:
00000000 <main>:
0: 3c010000 lui at,0x0
4: 24210080 addiu at,at,128
8: 8c240000 lw a0,0(at)
c: 8c250004 lw a1,4(at)
10: 0c000014 jal 50 <my_add>
14: 00000000 nop
18: 00853020 add a2,a0,a1
1c: ac260008 sw a2,8(at)
Disassembly of section .data:
00000080 <value>:
80: 0000000a 0xa
84: 00000014 0x14
测试3:
指定 –Ttext = 0x50 .=0x80000000 (在.text输出section前加)
结果说明 –Ttext的优先级高,且影响后面的.data输出 section的虚拟地址
test.exe: file format elf32-tradlittlemips
Disassembly of section .text:
00000050 <my_add>:
50: 20840003 addi a0,a0,3
54: 03e00008 jr ra
58: 20a50004 addi a1,a1,4
5c: 00000000 nop
Disassembly of section .reginfo:
00000060 <.reginfo>:
60: 80000030 lb zero,48(zero)
...
Disassembly of section .init:
00000000 <main>:
0: 3c010000 lui at,0x0
4: 24210080 addiu at,at,128
8: 8c240000 lw a0,0(at)
c: 8c250004 lw a1,4(at)
10: 0c000014 jal 50 <my_add>
14: 00000000 nop
18: 00853020 add a2,a0,a1
1c: ac260008 sw a2,8(at)
Disassembly of section .data:
00000080 <value>:
80: 0000000a 0xa
84: 00000014 0x14
...、
通过以上测试可以看出,-Ttext ( -Tdata -Tbss)优先级高于.计数器,
由于我们并未指定LMA,所以LMA=VMA,造成当我们指定-Ttext时,原生机器码内充斥着大量的无效数据,占用存储空间,
通过指定LMA,我们可以有效的减少被占用的存储空间。