固然书本上讲明jiffies是jiffies_64的低32位,但是我还是自己测试了下,重点在于链接脚本的写法。
此处只是为了测试,因此简化链接脚本。
/* link.lds */
1 ENTRY(_start) 2 3 jiffies = jiffies_64; 4 5 SECTIONS 6 { 7 8 . = 0x0; 9 10 . = ALIGN(4); 11 .text : 12 { 13 test.o (.text) 14 *(.text) 15 } 16 17 . = ALIGN(4); 18 .rodata : { *(.rodata) } 19 20 . = ALIGN(4); 21 .data : { *(.data) } 22 23 . = ALIGN(4); 24 .got : { *(.got) } 25 26 . = ALIGN(4); 27 __bss_start = .; 28 .bss : { *(.bss) } 29 _end = .; 30 }
#Makefile
1 CROPROSS_COMPILER = arm-none-linux-gnueabi- 2 LD := ${CROPROSS_COMPILER}ld 3 AS := ${CROPROSS_COMPILER}as 4 GCC := ${CROPROSS_COMPILER}gcc 5 6 mytest: test.o main.o 7 ${LD} -T link.lds $^ -o $@ 8 9 test.o: test.S 10 ${AS} $^ -o $@ 11 12 main.o : main.c 13 ${GCC} -c $^ -o $@ 14 15 .PHONY: clean 16 17 clean: 18 @rm -f -r mytest test.o main.o
#test.S
1 .text 2 .global _start 3 _start: 4 b 1f 5 1: 6 mov r0, r0 7 2: 8 mov r0, r0 9 3: 10 mov r0, r0 11 1: 12 b main
//main.c
1 unsigned long long jiffies_64 = -300 * 250; 2 3 extern int jiffies; 4 5 int main(void) 6 { 7 8 jiffies_64 += 1; 9 jiffies += 3; 10 11 return 0; 12 }
#反汇编,省略了一部分
1 00000000 <_start>: 2 0: b 4 <_start+0x4> 3 4: nop 4 8: nop 5 c: nop 6 10: b 14 <main> 7 8 00000014 <main>: 9 10 1c: ldr r3, [pc, #64] ; 取jiffies_64地址 11 12 20: ldm r3, {r1, r2} 13 24: mov r3, #1 ; 0x1 ; jiffies_64低位加1 14 28: mov r4, #0 ; 0x0 ; jiffies_64高位加0 15 2c: adds r3, r3, r1 16 30: adc r4, r4, r2 17 34: ldr r2, [pc, #40] ; 取jiffies_64地址 18 38: stm r2, {r3, r4} ; 将jiffies_64 + 1 存入jiffies_64 19 20 3c: ldr r3, [pc, #36] ; 取jiffies地址 21 22 40: ldr r3, [r3] 23 44: add r2, r3, #3 ; jiffies低位加3 24 48: ldr r3, [pc, #24] ; 取jiffies地址 25 4c: str r2, [r3] ; 将jiffies + 3 存入jiffies 26 27 28 64: .word 0x00000070 ; jiffies_64地址 29 68: .word 0x00000070 ; jiffies地址 30 Disassembly of section .data: 31 32 00000070 <jiffies>: 33 70: .word 0xfffedb08 34 74: .word 0xffffffff