libc是Linux下的ANSI C的函數庫。
ANSI C是基本的C語言函數庫,包含了C語言最基本的庫函數。這個庫可以根據 標頭檔案劃分爲 15 個部分,其中包括:字元型別 ()、錯誤碼()、 浮點常數 ()、數學常數 ()、標準定義 ()、 標準 I/O ()、工具函數 ()、字串操作 ()、 時間和日期 ()、可變參數表 ()、信號 ()、 非區域性跳轉 ()、本地資訊 ()、程式斷言 () 等等。這在其他的C語言的IDE中都是有的。
libc.a在ARM平臺中一般在交叉編譯對應的目錄中
/usr/local/arm/4.5.1/arm-none-linux-gnueabi/sys-root/usr/lib/libc.a
test.c檔案
#include <string.h>
int ar[100];
int (*print)(const char *format, ...) = (void*)0x43e11a2c;
int sum = 0;
extern unsigned int __bss_start;
extern unsigned int __bss_end;
int main(void)
{
int i = 0;
//memset((void*)&__bss_start, 0, (int)&__bss_end - (int)&__bss_start);
for (i = 0; i < 10; i++)
{
print("ar[%d] = %d\n", i, ar[i]);
}
print("sum = %d\n", sum);
return 0;
}
start.lds檔案
ENTRY(_start);
OUTPUT("test");
OUTPUT_ARCH(arm);
SECTIONS
{
. = 0x40000000;
.text : {
start.o(.text);
*(.text);
}
.rodata : {
*(.rodata);
}
. = ALIGN(4);
.data : {
*(.data);
}
. = ALIGN(4);
__bss_start = .;
.bss : {
*(.bss);
}
__bss_end = .;
}
Makefile檔案
CROSS_COMPILE = arm-linux-
CC = $(CROSS_COMPILE)gcc
CC_FLAGS = -c -I ./
LD = $(CROSS_COMPILE)ld
LD_FLAGS = -T start.lds -Ttext 0x40008000
OBJCOPY = $(CROSS_COMPILE)objcopy
COPY_FLAGS = -O binary
OBJDUMP=$(CROSS_COMPILE)objdump
DUMP_FLAGS=-D
TARGET=test
LIBS=lib/libc.a
OBJS=start.o led.o lib.o test.o bell.o key.o
all: $(OBJS)
$(LD) $(LD_FLAGS) $(OBJS) $(LIBS) -o $(TARGET)
$(OBJCOPY) $(COPY_FLAGS) $(TARGET) $(TARGET).bin
$(OBJDUMP) $(DUMP_FLAGS) $(TARGET) > $(TARGET).dis
rm -rf *.o
%.o:%.s
$(CC) $(CC_FLAGS) $<
%.o:%.S
$(CC) $(CC_FLAGS) $<
%.o:%.c
$(CC) $(CC_FLAGS) $<
.PHONY:clean
clean:
rm -rf *.o $(TARGET).bin $(TARGET).dis $(TARGET)
測試結果
## Starting application at 0x40008000 ...
ar[0] = 0
ar[1] = -509595648
ar[2] = -481165304
ar[3] = 973078545
ar[4] = -485294077
ar[5] = 348327937
ar[6] = 306323457
ar[7] = 452984827
ar[8] = -503246593
ar[9] = -511634431
sum = -202655882
## Application terminated, rc = 0x0
test.c檔案內容
#include <string.h>
int ar[100];
int (*print)(const char *format, ...) = (void*)0x43e11a2c;
int sum = 0;
extern unsigned int __bss_start;
extern unsigned int __bss_end;
int main(void)
{
int i = 0;
memset((void*)&__bss_start, 0, (int)&__bss_end - (int)&__bss_start);
for (i = 0; i < 10; i++)
{
print("ar[%d] = %d\n", i, ar[i]);
}
print("sum = %d\n", sum);
return 0;
}
執行結果
## Starting application at 0x40008000 ...
ar[0] = 0
ar[1] = 0
ar[2] = 0
ar[3] = 0
ar[4] = 0
ar[5] = 0
ar[6] = 0
ar[7] = 0
ar[8] = 0
ar[9] = 0
sum = 0
## Application terminated, rc = 0x0
DengJin #
6.1 Exynos 4412 SCP_Users Manual_Ver.0.10.00_Preliminary0.pdf