advarx21ic 发表于 2017-11-4 13:34:18

请教:关于IAR中,将变量定位到指定位置的问题

大家好。

我遇到一个问题,在IAR编译器环境下,我想将某个文件中的所有变量指定到某段内存中。

我参考网上资料用如下代码却得不到正确的结果。

place in RAM_region2 {readwrite data object aa.o};
place in RAM_region2 {readwrite data object bb.o};

上面的代码就是希望连接器将aa.o和bb.o中的readwrite data 放到内存段 RAM_region2 中。

但链接出来后却不是这样的。

请教大家问题出再哪里?

shangdawei 发表于 2017-11-4 13:43:14

https://www.iar.com/support/tech-notes/linker/how-do-i-place-a-group-of-functions-or-variables-in-a-specific-section/

shangdawei 发表于 2017-11-4 13:44:03

/* Place following data in section MY_DATA */
#pragma default_variable_attributes = @ "MY_DATA"

int data1;
int data2;

/* Stop placing data in section MY_DATA */
#pragma default_variable_attributes =

int data3;

int main()
{
    data1 = fun1(5);
    data2 = fun2(5);
    data3 = fun3(5);

    return data1 + data2 + data3;
}

define region DATA_region = mem:;
place in DATA_region { readwrite section MY_DATA };

advarx21ic 发表于 2017-11-4 13:55:22

shangdawei 发表于 2017-11-4 13:44
/* Place following data in section MY_DATA */
#pragma default_variable_attributes = @ "MY_DATA"




感谢!

不过,有没有简单一点的办法,可以整个OBJ文件一起处理。KEIL是可以的,只要放入文件名就可以了。

如果用您的方式,就必须每个文件都要处理以下才行。

advarx21ic 发表于 2017-11-4 13:59:28


下面是网上找到的,不过好像不好用

place in ROM { readonly }; /* all readonly sections */ place in RAM { readwrite }; /* all readwrite sections */

place in RAM { block HEAP, block CSTACK, block IRQ_STACK };
place in ROM { section .text object myfile.o }; /* the .text section of myfile.o */
place in ROM { readonly object myfile.o }; /* all read-only sections of myfile.o */
place in ROM { readonly data object myfile.o }; /* all read-only data sections myfile.o */

shangdawei 发表于 2017-11-4 17:52:01




define block objectx { rw object object*.o }; <== 支持通配符
place in IRAM2_region{ block objectx };

==================================================================================
uint8_t object1_var_uint8;
uint16_t object1_var_uint16;
uint32_t object1_var_uint32;

==================================================================================
uint8_t object2_var_uint8;
uint16_t object2_var_uint16;
uint32_t object2_var_uint32;

==================================================================================
__root uint32_t main_var_result;
__root uint8_t main_var_uint8_zi = 0;
__root uint8_t main_var_uint8 = 8;
__root uint16_t main_var_uint16 = 16;
__root uint32_t main_var_uint32 = 32;

==================================================================================
main_var_result         0x20000004   0x4DataGbmain.o
main_var_uint16         0x20000008   0x2DataGbmain.o
main_var_uint32         0x20000000   0x4DataGbmain.o
main_var_uint8          0x2000000a   0x1DataGbmain.o
main_var_uint8_zi       0x2000000b   0x1DataGbmain.o

==================================================================================
object1_var_uint16      0x20010008   0x2DataGbobject1.o
object1_var_uint32      0x20010000   0x4DataGbobject1.o
object1_var_uint8       0x2001000c   0x1DataGbobject1.o

object2_var_uint16      0x2001000a   0x2DataGbobject2.o
object2_var_uint32      0x20010004   0x4DataGbobject2.o
object2_var_uint8       0x2001000d   0x1DataGbobject2.o

==================================================================================
objectx$Base         0x20010000         --   Gb- Linker created -
objectx$Limit          0x20010010         --   Gb- Linker created -


advarx21ic 发表于 2017-11-4 17:54:03

shangdawei 发表于 2017-11-4 17:52
define block objectx { rw object object*.o };

非常感谢您!

advarx21ic 发表于 2017-11-4 18:26:32

shangdawei 发表于 2017-11-4 17:52
define block objectx { rw object object*.o };

压缩文件下载不了,谢谢

shangdawei 发表于 2017-11-4 18:51:05

advarx21ic 发表于 2017-11-4 18:26
压缩文件下载不了,谢谢

正常啊,你试试其他办法下载。

advarx21ic 发表于 2017-11-4 19:15:56

shangdawei 发表于 2017-11-4 18:51
正常啊,你试试其他办法下载。

嗯,能下载,不能解压,解压报错.

advarx21ic 发表于 2017-11-4 21:20:35


试了大半个晚上,发现用#pragma default_variable_attributes 的方式来做是可以正确分配到指定的内存段的.

但是,下面的方式就不行.但有个例外,如果变量用_no_init来定义又可以.

define block objectx { rw object object*.o }; <== 支持通配符
place in IRAM2_region{ block objectx };

shangdawei 发表于 2017-11-4 21:53:30

advarx21ic 发表于 2017-11-4 19:15
嗯,能下载,不能解压,解压报错.

advarx21ic 发表于 2017-11-4 22:30:41

shangdawei 发表于 2017-11-4 21:53



下载了2345压缩软件就可以解压了

3s.

Gorgon_Meducer 发表于 2017-11-5 19:02:15

本帖最后由 Gorgon_Meducer 于 2017-11-5 19:03 编辑

横跨四编译器的常见编译器扩展:


//! \note for IAR
#ifdef __IS_COMPILER_IAR__
    #undef __IS_COMPILER_IAR__
#endif
#define __IS_COMPILER_IAR__ defined(__IAR_SYSTEMS_ICC__)

//! \note for gcc
#ifdef __IS_COMPILER_GCC__
    #undef __IS_COMPILER_GCC__
#endif
#define __IS_COMPILER_GCC__ defined(__GNUC__)
//! @}

//! \note for arm compiler 5
#ifdef __IS_COMPILER_ARM_COMPILER_5__
    #undef __IS_COMPILER_ARM_COMPILER_5__
#endif
#define __IS_COMPILER_ARM_COMPILER_5__ ((__ARMCC_VERSION >= 5000000) && (__ARMCC_VERSION < 6000000))
//! @}

//! \note for arm compiler 6
#ifdef __IS_COMPILER_ARM_COMPILER_6__
    #undef __IS_COMPILER_ARM_COMPILER_6__
#endif
#define __IS_COMPILER_ARM_COMPILER_6__ ((__ARMCC_VERSION >= 6000000) && (__ARMCC_VERSION < 7000000))
//! @}



//! \brief none standard memory types
#if __IS_COMPILER_IAR__
#   define FLASH                const
#   define EEPROM               const
#   define NO_INIT            __no_init
#   define ROOT               __root
#   define INLINE               inline
#   define ALWAYS_IN_LINE       __attribute__((always_inline))
#   define WEAK               __weak
#   define RAMFUNC            __ramfunc
#   define __asm__            __asm
#   define __ALIGN(__N)         _Pragma(__STR(data_alignment=__N))
#   define __AT_ADDR(__ADDR)    @ __ADDR
#   define __SECTION(__SEC)   _Pragma(__STR(location=__SEC))

#   define PACKED               __packed
#   define UNALIGNED            __packed
#   define TRANSPARENT_UNION    __attribute__((transparent_union))

#elif __IS_COMPILER_GCC__
#   define FLASH                const
#   define EEPROM               const
#   define NO_INIT            __attribute__(( section( ".bss.noinit"))
#   define ROOT               __attribute__((used))   
#   define INLINE            inline
#   define ALWAYS_IN_LINE       __attribute__((always_inline))
#   define WEAK               __attribute__((weak))
#   define RAMFUNC            __attribute__((section (".textrw")))
#   define __asm__            __asm
#   define __ALIGN(__N)         __attribute__((aligned (__N)))
#   define __AT_ADDR(__ADDR)    __attribute__((at(__ADDR)))
#   define __SECTION(__SEC)   __attribute__((section (__SEC)))

#   define PACKED               __attribute__((packed))
#   define UNALIGNED            __attribute__((packed))
#   define TRANSPARENT_UNION    __attribute__((transparent_union))

#elif __IS_COMPILER_ARM_COMPILER_5__
#   define FLASH                const
#   define EEPROM               const
#   define NO_INIT            __attribute__( ( section( ".bss.noinit"),zero_init) )
#   define ROOT               __attribute__((used))   
#   define INLINE            __inline
#   define ALWAYS_IN_LINE       __attribute__((always_inline))
#   define WEAK               __attribute__((weak))
#   define RAMFUNC            __attribute__((section (".textrw")))
#   define __asm__            __asm
#   define __ALIGN(__N)         __attribute__((aligned (__N)))
#   define __AT_ADDR(__ADDR)    __attribute__((at(__ADDR)))
#   define __SECTION(__SEC)   __attribute__((section (__SEC)))


#   define PACKED               __packed
#   define UNALIGNED            __packed
#   define TRANSPARENT_UNION    __attribute__((transparent_union))

#elif __IS_COMPILER_ARM_COMPILER_6__
#   define FLASH                const
#   define EEPROM               const
#   define NO_INIT            __attribute__( ( section( ".bss.noinit")) )
#   define ROOT               __attribute__((used))   
#   define INLINE            __inline
#   define ALWAYS_IN_LINE       __attribute__((always_inline))
#   define WEAK               __attribute__((weak))
#   define RAMFUNC            __attribute__((section (".textrw")))
#   define __asm__            __asm
#   define __ALIGN(__N)         __attribute__((aligned (__N)))
#   define __AT_ADDR(__ADDR)    __attribute__((section (".ARM.__at_" #__ADDR)))
#   define __SECTION(__SEC)   __attribute__((section (__SEC)))

#   define PACKED               __attribute__((packed))
#   define UNALIGNED            __unaligned
#   define TRANSPARENT_UNION    __attribute__((transparent_union))

#endif


#define AT_ADDR(__ADDR)   __AT_ADDR(__ADDR)
#define ALIGN(__N)          __ALIGN(__N)
#define SECTION(__SEC)      __SECTION(__SEC)

advarx21ic 发表于 2017-11-6 11:54:35

Gorgon_Meducer 发表于 2017-11-5 19:02
横跨四编译器的常见编译器扩展:

很不错,学习一下,谢谢!

shangdawei 发表于 2017-11-6 12:03:28

Cmsis_compiler.h 这个文件也有好多定义可以参考。
页: [1]
查看完整版本: 请教:关于IAR中,将变量定位到指定位置的问题