您的位置:首页 > 其它

symbols are defined in a linker script file(etext, edata, end)

2016-12-01 06:42 489 查看
##以下是man page 里面的内容,源码里面使用的三个外部变量(  extern char etext, edata, end)并未定义在任何头文件里面,而是定义在了linker script file中,具体解释见最后 ##  解释

NAME

       etext, edata, end - end of program segments

SYNOPSIS 

       extern etext;

       extern edata;

       extern end;

DESCRIPTION 

       The addresses of these symbols indicate the end of various program

       segments:

       etext  This is the first address past the end of the text segment

              (the program code).

       edata  This is the first address past the end of the initialized data

              segment.

       end    This is the first address past the end of the uninitialized

              data segment (also known as the BSS segment).

CONFORMING TO 

       Although these symbols have long been provided on most UNIX systems,

       they are not standardized; use with caution.

NOTES

       The program must explicitly declare these symbols; they are not

       defined in any header file.

       On some systems the names of these symbols are preceded by

       underscores, thus: _etext, _edata, and _end.  These symbols are also

       defined for programs compiled on Linux.

       At the start of program execution, the program break will be

       somewhere near &end (perhaps at the start of the following page).

       However, the break will change as memory is allocated via brk(2) or

       malloc(3).  Use sbrk(2) with an argument of zero to find the current

       value of the program break.

EXAMPLE

       When run, the program below produces output such as the following:

           $ ./a.out

           First address past:

               program text (etext)       0x8048568

               initialized data (edata)   0x804a01c

               uninitialized data (end)   0x804a024

   Program source

       #include <stdio.h>

       #include <stdlib.h>

       extern char etext, edata, end; /* The symbols must have some type,

                                          or "gcc -Wall" complains */

       int

       main(int argc, char *argv[])

       {

           printf("First address past:\n");

           printf("    program text (etext)      %10p\n", &etext);

           printf("    initialized data (edata)  %10p\n", &edata);

           printf("    uninitialized data (end)  %10p\n", &end);

           exit(EXIT_SUCCESS);
       }

##解释


Chapter 4. Linker Scripts

Every link is controlled by a linker script. This script is written in the linker command language.

The main purpose of the linker script is to describe how the sections in the input files should be mapped into the output file, and to control the memory layout of the output file. Most linker scripts do nothing more than this. However, when necessary, the
linker script can also direct the linker to perform many other operations, using the commands described below.

The linker always uses a linker script. If you do not supply one yourself, the linker will use a default script that is compiled into the linker executable. You can use the -verbosecommand
line option to display the default linker script. Certain command line options, such as -r or -N,
will affect the default linker script.

You may supply your own linker script by using the -T command line option. When you do this, your linker script will replace the default linker script.

You may also use linker scripts implicitly by naming them as input files to the linker, as though they were files to be linked. Refer to Section
4.11 Implicit Linker Scripts.

4.1. Basic Linker Script Concepts

We need to define some basic concepts and vocabulary in order to describe the linker script language.
The linker combines input files into a single output file. The output file and each input file are in a special data format known as an object file
format. Each file is called an object file. The output file is often called an executable, but for our purposes we will also call it an
object file. Each object file has, among other things, a list of sections. We sometimes refer to a section in an input file as an input section;
similarly, a section in the output file is an output section.
Each section in an object file has a name and a size. Most sections also have an associated block of data, known as the section contents.
A section may be marked as loadable, which mean that the contents should be loaded into memory when the output file is run. A section with no contents may be allocatable,
which means that an area in memory should be set aside, but nothing in particular should be loaded there (in some cases this memory must be zeroed out). A section which is neither loadable nor allocatable typically contains some sort of debugging information.
Every loadable or allocatable output section has two addresses. The first is the VMA, or virtual memory address. This is the address the
section will have when the output file is run. The second is the LMA, or load memory address. This is the address at which the section will be loaded. In most cases the two addresses will be the same.
An example of when they might be different is when a data section is loaded into ROM, and then copied into RAM when the program starts up (this technique is often used to initialize global variables in a ROM based system). In this case the ROM address would
be the LMA, and the RAM address would be the VMA.
You can see the sections in an object file by using the objdump program with the -h option.
Every object file also has a list of symbols, known as the symbol table. A symbol
may be defined or undefined. Each symbol has a name, and each defined symbol has an address, among other information. If you compile a C or C++ program into an object file, you will get a defined symbol for every defined function and global or static variable.
Every undefined function or global variable which is referenced in the input file will become an undefined symbol.
You can see the symbols in an object file by using the nm program, or by using the objdump program
with the -t option.

##博客仅作个人记录##
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐