s3c2440裸机-清bss原理及实现

1.清bss的引入(为什么要清bss)

我们先举个例子:
#include "s3c2440_soc.h"
#include "uart.h"

char g_Char = 'A'; //.data
char g_Char3 = 'a';
const char g_Char2 = 'B'; //.rodata
int g_A = 0; //bss
int g_B; //bss

int main(void)
{
	uart0_init();

	puts("

g_A = ");
	printHex(g_A);
	puts("

");

	while (1)
	{
		putchar(g_Char);
		g_Char++;         /* nor启动时, 此代码无效 ,重定位到sdram的baseaddr后有效*/

		putchar(g_Char3);
		g_Char3++;
		delay(1000000);
	}
	return 0;
}

我们把程序烧进去,然后打印g_A,但是发现g_A这个值并不是0,而是一个随机值。为什么呢?

这个时候我们做完了重定位,把代码copy到了sdram上,然后sdram上紧接着的地址就是.bss的基地址了,这时候bss段的这块内存没有经过任何处理,所以是随机的。

那么我们重定位完代码后需要进行清除sdram上.bss段的数据,因为我们知道bss是未初始化和初始值为0的全局变量。

2.怎么清bss

当然只需要往bss段写入全0就ok了,我们编写链接脚本如下:

 SECTIONS
 {
     . = 0x30000000;

     . = ALIGN(4);
     .text      :
     {
       *(.text)
     }

     . = ALIGN(4);
     .rodata : { *(.rodata) }

     . = ALIGN(4);
     .data : { *(.data) }

     . = ALIGN(4);
     __bss_start = .;
     .bss : { *(.bss) *(.COMMON) }
     _end = .;
 }

再编写start.s,清除bss段的代码如下:

/* 清除BSS段 */
	ldr r1, =__bss_start
	ldr r2, =_end
	mov r3, #0
clean:
	strb r3, [r1]
	add r1, r1, #1
	cmp r1, r2
	ble clean

	bl main
halt:
	b halt

我们把程序再烧进去,然后打印g_A,但是发现g_A的值是0了。本质上就是对重定位后的bss段数据清0.

原文地址:https://www.cnblogs.com/fuzidage/p/12044558.html