C language refers to the symbol in the connection script LDS--Clears the BSS segment, C implementation mode

Source: Internet
Author: User
Tags volatile

Before our startup file clears BSS and copies are implemented by the way of assembly, however, we are able to use the C language without using the assembler:

First look at the connection script:

 sections{.     = 0x30000000  ;    __code_start  =.; .     = ALIGN (4   * (. Text)}.     = ALIGN (4   * (. Rodata)}.     = ALIGN (4   * = ALIGN (4   =.; . BSS: { * (. BSS) * (COMMON)} _end  =;}  

Now we write the copy and clean functions in C, but in our C program we need to access the symbols in the connection script.

Look at the code first and explain later:

voidCopy2sdram (void){    /*to get __code_start from the LDS file, __bss_start * then copy the data from the 0 address to the __code_start*/    extern int__code_start, __bss_start; volatileUnsignedint*dest = (volatileUnsignedint*) &__code_start; volatileUnsignedint*end = (volatileUnsignedint*) &__bss_start; volatileUnsignedint*SRC = (volatileUnsignedint*)0;  while(Dest <end) {        *dest++ = *src++; }}voidCLEAN_BSS (void){    /*to obtain __bss_start from the LDS file, _end*/    extern int_end, __bss_start; volatileUnsignedint*start = (volatileUnsignedint*) &__bss_start; volatileUnsignedint*end = (volatileUnsignedint*) &_end;  while(Start <=end) {        *start++ =0; }}

Start. S part shows:

First, regardless of the connection script reference, we are now discussing a problem, we use BL here, is relative jump, we must recognize a problem, at this time our SDRAM is initialized, but at the moment we have no program in SDRAM, we need to first reposition, the NOR Flash content copy to SDRAM, so at this time (no copy) Copy2sdram is in nor flash inside the execution, then use BL can understand. But CLEAN_BSS this function, in the end should not use BL? It seems as if we have completed the relocation, the text section, Rodata section, the data section is relocated to the SDRAM, but, SDRAM this above a few paragraphs after the beginning to store the BSS segment, and the BSS section of the variable is worth 0, will not be placed in the bin file, if you do not use BL clean _BSS, while using Ldr PC,=CLEAN_BSS absolute Jump, the program will die.

Back to the C language method of referencing the connection script:

Reference connection (please click)

In the compilation phase, there will be something called the symbol table,

The variables in LDS are not saved in the C program, but in case the C program needs to use the LDS file, it can be referenced by compiling the symbol table at the time of the program.

Already shown, c is the variable name, then the variable address, and the connection script LDS, is the symbol name, and then its value.

Now, we compare, we in C language to take a variable address, is to use the & symbol, we get the corresponding address in the symbol table,

In the same way, in C reference to the symbol of the connection script, we need to be the value of the LDS symbol, but also through the & symbol, but at this time take the address character & +lds symbol, get the symbol value, not the address of the symbol itself, and the value of this symbol, but it happens to be an address. (Please feel the contrast carefully.)

So, we take the external declaration of variables in the way.

How does the C function use the variable ABC in the LDS file?
A. declare the change amount in the C function to be an extern type, for example:
extern int ABC;

B. when using, to be taken, such as:
int *p = &abc; The value of P is the value of ABC in the LDS file

The type of the declaration can be either int or char. The type is not important at this time, but it is best to declare it as the type you want to use.

This is the official reference, so the specific statement into what type, although not affected, but it is best to declare the type you want to use. Because in the symbol table, the symbol table of the connection script is a symbol and a value, stored directly, no more address, before the C language using the extern declaration, the symbol in LDS has been defined in the symbol table, also means that char or int type decoration has no effect on the value of the symbol, This value has long been determined in the symbol table, and the reason we are declaring a type in C is to make it easier to use this symbol later in LDS, and the C syntax requires that the identifier must be type-decorated.

Take the address of the symbol in LDS, get the value of this symbol, and this value just represents an address.

C language refers to the symbol in the connection script LDS--Clears the BSS segment, C implementation mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.