How to Use the variables defined in the Link script in C code to link the script

Source: Internet
Author: User

How to Use the variables defined in the Link script in C code to link the script

Original content: http://www.100ask.org/bbs/forum.php? Mod = viewthread & tid = 16231 & page = 1 & authorid = 2

Bytes --------------------------------------------------------------------------------------------------------------------------

Reference: https://sourceware.org/ml/binutils/2007-07/msg00154.html


Such code is often found in link scripts:
  1. SECTIONS
  2. {
  3. .....
  4. . = ALIGN (4 );
  5. . Rodata: {* (. rodata )}

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

  8. . = ALIGN (4 );
  9. . Got: {* (. got )}

  10. . = ALIGN (4 );
  11. _ Bss_start = .;
  12. . Bss: {* (. bss )}
  13. _ End = .;
  14. }
Copy code
_ Bss_start and _ end indicate the start and end addresses of BSS segments.
We want to clear this space to zero,
1. In assembly code, you can directly reference _ bss_start and _ end, for example:
  1. Ldr r0, =__ bss_start
  2. Ldr r1, = _ end
Copy code
2. In C code, we cannot directly reference them. We need to do this:
  1. Void clean_bss (void)
  2. {
  3. Extern int _ bss_start, _ end;
  4. Int * p = & __ bss_start;

  5. For (; p <& _ end; p ++)
  6. * P = 0;
  7. }
Copy code
_ Bss_start, _ end does not indicate a value? Why do I need to use the address-removing symbol in the C code &?

Cause:
I.
In C code, such a statement:
  1. Int foo = 1000;
Copying the code will result in two events:
1. Set aside 4 bytes of space in the code to save the value 1000
2. In the symbole talbe in C language, that is, in the symbol table, there is an item named foo, which contains the address of the 4-byte space.

When we execute foo = 1, we will first find the address corresponding to foo in the symbol table, and then fill in the value 1 to the memory corresponding to that address;
When int * a = & foo is executed, the foo address in the symbol table is directly written to.

II.
In the link script, assume that
  1. _ Bss_start = 1000
Copy code
_ Bss_start is not a variable. It is just a value and does not need to be stored in memory;
In C, the symbol table contains an item named _ bss_start. The value (address value) of this item is 1000;
Note that this 1000 does not actually have memory.

III.
Therefore, when using the value defined in the Link script in C language, you should do the following:
  1. Extern int _ bss_start;
  2. Int val = & __ bss_start;
Copy code

Obtain the value in the symbol table by using the accessors.


Note that this value is only the value defined in the Link script and does not represent the address of a variable.
View comments

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.