【问题标题】:Linking the Linker script file to source code将链接器脚本文件链接到源代码
【发布时间】:2010-03-29 10:02:41
【问题描述】:

我是 GNU 编译器的新手。

我有一个 C 源代码文件,其中包含一些结构和变量,我需要在其中将某些变量放置在特定位置。

所以,我编写了一个链接器脚本文件,并在 C 源代码中的变量声明中使用了 __ attribute__("SECTION")。

我正在使用 GNU 编译器 (cygwin) 编译源代码并使用 -objcopy 选项创建一个 .hex 文件,但我不知道如何在编译时链接我的链接器脚本文件以相应地重新定位变量。

我附上链接器脚本文件和 C 源文件以供参考。

请帮助我将链接器脚本文件链接到我的源代码,同时使用 GNU 创建 .hex 文件。

/*linker script file*/
/*defining memory regions*/

      MEMORY
      {
         base_table_ram     : org = 0x00700000, len = 0x00000100  /*base table area for BASE table*/ 
         mem2               : org =0x00800200,  len = 0x00000300 /* other structure variables*/
      }
/*Sections directive definitions*/ 

      SECTIONS
      {
        BASE_TABLE    : { }  > base_table_ram

        GROUP                  :
           {
                   .text        : { } { *(SEG_HEADER)  }
                   .data        : { } { *(SEG_HEADER)  }
                   .bss         : { } { *(SEG_HEADER)  }

           } > mem2

      }

C源代码:

const UINT8 un8_Offset_1 __attribute__((section("BASE_TABLE")))  = 0x1A; 
const UINT8 un8_Offset_2 __attribute__((section("BASE_TABLE")))  = 0x2A; 
const UINT8 un8_Offset_3 __attribute__((section("BASE_TABLE")))  = 0x3A; 
const UINT8 un8_Offset_4 __attribute__((section("BASE_TABLE")))  = 0x4A; 
const UINT8 un8_Offset_5 __attribute__((section("BASE_TABLE")))  = 0x5A;     
const UINT8 un8_Offset_6 __attribute__((section("SEG_HEADER")))  = 0x6A; 

我的意图是将“BASE_TABLE”部分的变量放置在链接描述文件中定义的地址中,并将其余变量放置在上面链接描述文件中定义的“SEG_HEADER”中。

但是编译后,当我查看 .hex 文件时,不同的部分变量位于不同的十六进制记录中,位于地址 0x00,而不是链接描述文件中给出的那个。

请帮助我将链接器脚本文件链接到源代码。

是否有任何命令行选项来链接链接器脚本文件,如果有的话,请向我提供如何使用这些选项的信息。

提前致谢,

SureshDN。

【问题讨论】:

    标签: c embedded gnu


    【解决方案1】:

    试试gcc -Xlinker -T (linker script name) (c sources files)

    【讨论】:

      【解决方案2】:

      我首先将我所有的 c 文件编译为目标文件,然后将它们链接到:

      gcc -Xlinker -T"xxx.lds" (all object files)

      来自 gcc 文档:

      `-Xlinker OPTION'
          Pass OPTION as an option to the linker.  You can use this to
          supply system-specific linker options which GNU CC does not know
          how to recognize.
      

      【讨论】:

        【解决方案3】:

        感谢回复,

        我在 GCC 中找到了另一个链接器选项“ld”和选项 -T 来将这些部分链接到源代码。

        ld -T (linker scriptname) -o (final objfile) (objectfile of source file)

        谢谢 苏雷什

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-22
          • 2021-11-27
          • 1970-01-01
          • 2023-04-01
          • 1970-01-01
          • 2011-10-26
          • 2012-05-09
          • 1970-01-01
          相关资源
          最近更新 更多