Linux Link Script Learning--lds (GO)

Source: Internet
Author: User

Linux Link Scripting Learning--lds

I. INTRODUCTION

Ld:

GNU's linker.

Used to link a quantitative target file with the archive file and reposition their data, link symbol references.

When compiling a program, the final step is to run the LD to link

Each link is controlled by a link script that is written in the language of the link command.

Second, link script

One of the main purposes of a linked script is to describe how each segment (data segment, code snippet, heap, Stack, BSS) in the input file is mapped to the output file and controls the memory layout of the output file.

The linker always uses a link script, and if you do not provide it, the linker will use a default script that is compiled into the linker executable file.

You can use the--verbose command line to display the contents of the default linker script.

You can use the-t command line to provide your own link script to replace the default link script.

Three, simple link script example.

Many of the scripts are fairly straightforward.

Perhaps the simplest script contains only one command: ' SECTIONS '.

You can use ' SECTIONS ' to describe the memory layout of the output file.

' SECTIONS ' is a powerful command.

Suppose your program has only code snippets, initialized data segments, and uninitialized data segments. These will exist in '. Text ', ' Data ', ' BSS ' segments.

For this example, suppose the code should be loaded into the address 0x1000, and the data should start at 0x8000000, as follows: The script that implements this function

SECTIONS

{

. =0x1000;

. text:{* (. Text)}

. =0x8000000;

. data:{* (. Data)}

. bss:{* (. BSS)}

}

Specific analysis:

The keyword ' SECTIONS ' starts with this configuration. followed by a string of symbols placed in curly braces and the output description of the content.

The first line is to a special symbol '. ' Assignment, which is a positional identifier. If you do not otherwise make the address of the output segment, the address value is set to the existing value of the locator identifier, which is 0x1000.

The second line defines an output segment, '. Text '. Colon ': ' is the syntax required and can now be ignored. In the curly brackets after the segment, you should list all the names of the inputs that should be placed in the output segment. ' * ' is a wildcard character that matches all file names. The. Text fields in all input files are saved in this paragraph.

The rest is. Data and. BSS segments, in the same vein, the linker places all of the. Data segments from the beginning of the address 0x8000000.

Finally, the value of the locator identifier changes to 0x8000000 plus the address of all. Data segments. At this point the linker places all the. BSS addresses at the beginning.

Iv. Simple Link script commands

Set entry point

When you run a program, the first instruction to be executed becomes the entry point. You can use the "ENTRY" link script command to set the entry point. The parameter is a symbolic name, as follows:

ENTRY (SYMBOL)

There are many different ways to set the entry point. The linker sets the entry point by trying the method in order, and if it succeeds, it stops.

1, '-E ' entry command-line options

2, the entry (SYMBOL) command in the link script

3, if start is defined, the value of start is used

4, if present, use the '. Text ' segment's first address

5, address ' 0 '

Five, the command line set the link address

The LD is used to link multiple obj or so (library) files into an executable file.

Use the-t option to specify the data segment, the code snippet, and the starting position of the BSS segment. (-T is only used to link bootloader, kernels, and other software not supported by the underlying software.) When linking applications running on the operating system, the default link is generally used.

1, directly specify the code snippet, data segment, BSS segment start address

As follows:

-ttext startaddr

-tdata startaddr

-TBSS startaddr

For example:

Ld–ttext 0x00000000–g Led_on.o–o led_on_elf

2. Set the start address directly using the link script

Ld–ttimer.lds–o timer_elf A.O B.O

The link script timer.lds content is as follows:

sections{

. =0x30000000;

. Text: {* (. text)}

. Rodata ALIGN (4): {* (. rodata)}

. Data ALIGN (4): {* (. data)}

. BSS ALIGN (4): {*. (. BSS) * (COMMON)}

}

A sections command contains one or more segments, which is the basic unit of a connection script that represents how a part of the input file is placed.

Transferred from: http://www.cnblogs.com/joseph-linux/archive/2013/09/12/3317394.html

Linux Link Script Learning--lds (GO)

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.