Linux linked program predefined Variables

Source: Internet
Author: User
Article Title: Linux linked program predefined variables. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
During the link process, the linker ld and ld86 use variables to record the logical addresses of each segment in the execution program. Therefore, you can access these external variables in the program to obtain the middle part of the program. The preset external variables of the linker generally include at least etext, _ etext, edata, _ edata, end, and _ end.
  
The address of variable name _ etext and etext is the 1st address after the end of the program body segment; the address of _ edata and edata is the 1st address after the initialization data zone; the _ end and end addresses are the 1st address locations after the data zone (bss) is not initialized. The names prefixed with underscores (_) are equivalent to those without underscores (_). The only difference between them is that the etext, edata, and end symbols are not defined in ANSI, POSIX, and other standards.
  
When the program is started, its brk is in the same position as _ end. However, the system calls sys_brk (), memory allocation function malloc (), and standard input/output operations to change the location. Therefore, the current brk position of the program needs to be obtained using sbrk. Note that these variable names must be considered addresses. Therefore, you need to use the address prefix '&' when accessing them, such as & end. For example:
  
Extern int _ etext;
Int et;
  
(Int *) et = & _ etext; // at this time, et contains the address after the end of the body segment.
  
The following program predef. c can be used to display the addresses of these variables. It can be seen that the address value with and without the underscore '_' is the same.
  
/*
Print the symbols predefined by linker.
*/
Extern int end, etext, edata;
Extern int _ etext, _ edata, _ end;
Int main ()
{
Printf ("& etext = % p, & edata = % p, & end = % p \ n ",
& Etext, & edata, & end );
Printf ("& _ etext = % p, & _ edata = % p, & _ end = % p \ n ",
& _ Etext, & _ edata, & _ end );
Return 0;
}
  
Run this program in Linux 0.1X and you will get the following results. Note that these addresses are the logical addresses in the program address space, that is, the addresses starting from when the execution program is loaded to the memory location.
  
[/Usr/root] # gcc-o predef. c
[/Usr/root] #./predef
& Amp; etext = 4000, & amp; edata = 44c0, & amp; end = 48d8
& _ Etext = 4000, & _ edata = 44c0, & _ end = 48d8
[/Usr/root] #
  
If you run this program in the current Linux system (such as RedHat 9), you can get the following results. We know that the program code in Linux is stored from its logical address 0x08048000. Therefore, we can see that the code segment length of this program is 0x41b bytes.
  
[Root @ plinux] #./predef
& Etext = 0x804841b, & edata = 0x80495a8, & end = 0x80495ac
& _ Etext = 0x804841b, & _ edata = 0x80495a8, & _ end = 0x80495ac
[Root @ plinux] #
  
Linux 0.1x kernel (fs/buffer. c) The variable name_end is used to obtain the position of the kernel Image file after the end of the memory, and set the high-speed buffer from this position.
Related Article

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.