Makefile Study (1) arm-linux-ld arm-linux-objcopy arm-linux-objdump

Source: Internet
Author: User

Record what you have learned O (∩_∩) o haha ~

Makefile

LINK.BIN:START.O MAIN.O
Arm-linux-ld-tlink.lds-o link.elf $^
Arm-linux-objcopy-o binary link.elf Link.bin
arm-linux-objdump-d link.elf > Link_elf.dis
GCC Mkv210_image.c-o mkmini210
./mkmini210 Link.bin 210.bin

%.O:%. S
Arm-linux-gcc-o [email protected] $<-C

%.O:%.c
Arm-linux-gcc-o [email protected] $<-C

Clean
RM *.o *.elf *.bin *.dis mkmini210-f

Explain:


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.

Full link Script

Arm-linux-ld is a connector that combines some target and archive files, repositions data, and connects symbolic references. Typically, the final step in building a new compiler is to call LD.

ARM-LINUX-GCC-WALL-O2-C-o [email protected] $<
-O only activates preprocessing, compilation, and assembly, that is, he only makes the program obj file
-wall Specifies that all warning messages are generated
The-O2 compiler provides compiler-optimized options for the program, which can be used at compile time to improve the execution efficiency of the resulting execution file.
-C means that only the compiler is asked to compile, not link, to generate the file name of the source file, but the suffix from. C or. cc into the. O Target file
-S only activates preprocessing and compilation, which means compiling the files into assembly code
ARM-LINUX-LD directly specify the code snippet, data segment, and start address of the BSS segment
-TBSS address Set address of. BSS section

-tdata address Set address of. Data section

-ttext address Set address of. Text section
Example:
${cross}ld-ttext=0x33000000 Led.o-o led.elf
To set an address using a connection script:
Arm-linux-ld-tbeep.lds START.O Beep.o-o beep.elf
Where Beep.lds is the connection script as follows:
Arm-linux-objcopy is used to copy the contents of one destination file into another file, which can be used for format conversion between different source files
Example:
Arm-linux-objcopy–o binary–s Elf_file Bin_file
Common options:
Input-file, Outflie
Input and output files, if not outfile, the output file name is input file name
2.-l Bfdname or-input-target=bfdname
Used to indicate the format of the source file, Bfdname is the standard format name described in the BFD library, and if not specified, arm-linux-objcopy analyze It Yourself
3.-o the format of the Bfdname output
4.-f Bfdname also indicate the source file, the format of the destination file
5.-r sectionname Remove all segments named SectionName from the output file
6.-s does not copy relocation information and symbolic information from the source file into the destination file
7.-g do not copy the debug symbols from the source file to the destination file

Arm-linux-objdump
View the target file (. o file) and library file (. a file) information
arm-linux-objdump-d-M arm beep.elf > Beep.dis
-D displays all assembly information in the file
-M machine

Makefile Study (1) arm-linux-ld arm-linux-objcopy arm-linux-objdump

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.