Solutions for using Macros in link script (linker ld-eaten files)

Source: Internet
Author: User

Problem: There is a requirement in work: you need to make some judgments and substitutions in LD script using macro definitions like C-Define.

Experiment:

1: Theoretically *.c can be used, whether GCC/LD also support the direct use of the macro in LD script, the result:

Arm-linux-ld:xxx.lds:2: Ignoring invalid character ' # ' in Expressionarm-linux-ld:xxx.lds:2: syntax error
People say Shui, it seems that LD and GCC are not face ah. Here is the cross-compilation of the ld,x86 is the same conclusion, this is the same root. Looks blocked.

2:*.C Why can use define and other macros, this is done in the pre-compilation phase. Can we take the GCC pre-compilation and get it over with first? With doubt, we go on.

First put a copy of the original LD script: Old.lds, there are some define and comments oh

/* * Comments like C style * If comments line was less than 2 lines like this, maybe generate some strange result * */#ifde F __arm__output_format ("Elf32-littlearm", "Elf32-littlearm", "Elf32-littlearm") Output_arch (ARM) #elseOUTPUT_FORMAT ("Elf32-littlends", "Elf32-littlends", "Elf32-littlends")        Output_arch (NDS) #endifENTRY (_start) sections{//c++ comments. = 0x00000000; .        = ALIGN (__align__);                . Text:/*c style comment*/{#if defined (__arm__) cpu/arm920t/start.o (. Text) #endif        BOARD/XXX/LOWLEVEL_INIT.O (. Text) board/xxx/nand_read.o (. Text) * (. Text) }        .        = ALIGN (__align__); . Rodata: {* (Sort_by_alignment (Sort_by_name (. rodata*))}.        = ALIGN (__align__); . Data: {* (. data)}.        = ALIGN (__align__);        __bss_start =.;        . BSS (NOLOAD): {* (. bss). = ALIGN (__align__);} _end =.;}

We'll ask GCC to help. Only Precompile: (mostly- e and-p parameters, man gcc or gcc--help not known)

[Email protected]:~/linux_all/project$ arm-linux-gcc-e-d__arm__-d__align__=4-p old.lds-o New.ldsarm-none-linux-gnueabi-gcc:old.lds:linker input file unused because linking not done

Continue to be beaten, see GCC also recognize the file extension ah. Then let's fool it:

[Email protected]:~/linux_all/project$ cp old.lds Old.c[email protected]:~/linux_all/project$ arm-linux-gcc-e-D__ARM __  -d__align__=4-p Old.c-o New.lds
Wow, actually successfully fooled past, quickly see if New.lds is expected:

Output_format ("Elf32-littlearm", "Elf32-littlearm", "Elf32-littlearm") Output_arch (ARM) ENTRY (_start) SECTIONS{. = 0x00000000;. = ALIGN (4);. Text: {  cpu/arm920t/start.o (. Text)  board/xxx/lowlevel_init.o (. Text)  Board/xxx/nand_ READ.O (. Text)  * (. Text)}. = ALIGN (4);. Rodata: {* (Sort_by_alignment (Sort_by_name (. rodata*))}. = ALIGN (4);. Data: {* (. data)}. = ALIGN (4); __bss_start =.; . BSS (NOLOAD): {* (. bss). = ALIGN (4);} _end =.;}
Too cool, gcc seems to be more good cheat, O (∩_∩) o haha ~

If the sir you feel is not too good to be renamed *.C, then you can use the following line of command to fix it: (note the use of a separate "-" in the command OH)

[Email protected]:~/linux_all/project$ cat Old.lds | ARM-LINUX-GCC-E-d__arm__  -d__align__=4-p-o new.lds[email protected]:~/linux_all/project$ arm-linux-gcc-e-D__ arm__  -d__align__=4-p-<old.lds-o new.lds[email protected]:~/linux_all/project$ arm-linux-gcc-e-D__ARM__  -d__align__=4-p-<old.lds > New.lds
The above three orders, grab one of them (the line unexpectedly did not show, will look at it)
Next, LD eats the already processed new.lds to be possible. Needless to say, clever Sir you must have known.

Solutions for using Macros in link script (linker ld-eaten files)

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.