请选择 进入手机版 | 继续访问电脑版

硬汉嵌入式论坛

 找回密码
 立即注册
查看: 8448|回复: 2
收起左侧

关于MDK中8字节对齐问题

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
105917
QQ
发表于 2014-10-22 21:22:21 | 显示全部楼层 |阅读模式
What is eight-byte stack alignment?
Eight byte stack alignment is a requirement of the Procedure Call Standard for the ARM Architecture [AAPCS]. This specifies that functions must maintain an eight-byte aligned stack address (for example: 0x00, 0x08, 0x10, 0x18, 0x20) on all external interfaces. In practice this requirement is met if:
    [li]At each external interface, the current stack pointer (SP) is a multiple of eight bytes.[/li][li]Your OS maintains eight-byte stack alignment on its external interfaces, for example, on task switches [/li]

Eight-byte stack alignment is of particular benefit to processors supporting LDRD and STRD instructions, for example, processors based on ARM architecture v5TE and later. If the stack is not eight-byte aligned the use of LDRD and STRD might cause an alignment fault, depending on the target and configuration used.Eight-byte stack alignment in RVCT and the ARM Compiler toolchain
In the RVCT v2.0 and above, all generated code and C library code will maintain eight-byte stack alignment on external interfaces. The generated code may not keep the stack eight-byte aligned internally, for example, in leaf functions. However all generated code will keep the stack eight-byte aligned providing that it is already correctly aligned at the entry to the function. The compiler will not generate code to correct a misaligned stack.
Please be aware that eight-byte stack alignment is not new to RVDS. ADS does maintain eight-byte stack alignment, only the alignment of double and long long has changed between ADS and RVDS. In ADS double and long long data types were four-byte aligned ("EBA4"), unless -Oldrd or __align were used. In RVCT 2.0 and later, double and long long data types are now eight-byte aligned ("EBA8"), unless --apcs /adsabi is used.
The RVCT 3.1 compiler and earlier supports the option --apcs /adsabi to compile code that is compatible with the old ADS ABI. However, this option was deprecated and finally removed in RVCT 4.0.
The ARM Compiler toolchain check eight byte stack alignment using two build attributes REQUIRE8 and PRESERVE8, these can either be true or false. These are embedded into the object files and executables generated by the tools. REQUIRE8 is used to indicate that the code requires the stack pointer to be eight-byte aligned. PRESERVE8 is used to indicate that the code preserves eight-byte stack alignment.
The assembler uses the REQUIRE8 and PRESERVE8 directives to indicate whether REQUIRE8 and PRESERVE8 build attributes should be true or false. If you omit PRESERVE8 the assembler will decide whether to set the PRES8 build attribute or not, by examining instructions that modify SP. ARM recommends that you specify PRESERVE8 explicitly. If your assembly requires the stack to be eight byte aligned, for example, an LDRD using SP, you should specify the REQUIRE8 directive.
In RVCT 2.1 and later the assembler (armasm) can generate a warning if it detects a stack access that would leave the stack not eight-byte aligned. This can be enabled by adding --diag_warning 1546 to your assembler command line.
The REQUIRE8 and PRESERVE8 build attributes set by the compiler and assembler are used by the linker to prevent functions that require eight-byte alignment calling functions that do not preserve eight-byte alignment. If this occurs the linker will generate an error, for example:
Error: L6238E: foo.o(.text) contains invalid call from '~PRES8' function to 'REQ8' function
Please see the Errors and Warnings document for your toolchain for further information about this error message.
回复

使用道具 举报

6

主题

70

回帖

88

积分

初级会员

积分
88
发表于 2017-1-7 10:56:01 | 显示全部楼层
是否帮忙翻译一下,英文不是很好。
回复

使用道具 举报

8

主题

17

回帖

8

积分

新手上路

。。

积分
8
发表于 2017-6-22 18:35:58 | 显示全部楼层
什么是八字节堆栈对齐?
八字节堆栈对齐是ARM体系结构[AAPCS]的过程调用标准的要求。这规定了所有外部接口上的功能必须保持八字节对齐的堆栈地址(例如:0x00,0x08,0x10,0x18,0x20)。在实践中,满足以下要求:
在每个外部接口,当前堆栈指针(SP)是八个字节的倍数。
您的操作系统在其外部接口上保持八字节堆栈对齐,例如在任务交换机上

对于支持LDRD和STRD指令的处理器,例如基于ARM架构v5TE和更高版本的处理器,八字节堆栈对齐特别有利。如果堆栈不是八字节对齐,使用LDRD和STRD可能会导致对齐错误,具体取决于所使用的目标和配置。RVCT和ARM编译器工具链中的字节堆栈对齐
在RVCT v2.0及更高版本中,所有生成的代码和C库代码将在外部接口上保持八字节堆栈对齐。生成的代码可能不会将堆栈八字节内部保留,例如,在叶函数中。然而,所有生成的代码将保持堆栈八字节对齐,只要它已经在函数的入口处已经正确对齐。编译器不会生成代码来纠正不对齐的堆栈。
请注意,8字节堆栈对齐对于RVDS来说并不新鲜。 ADS确实保持八字节堆栈对齐,只有ADS和RVDS之间的双长和长整型的对齐方式发生变化。在ADS中,双长和长长数据类型是四字节对齐(“EBA4”),除非使用了-Oldrd或__align。在RVCT 2.0和更高版本中,双长和长长数据类型现在是八字节对齐(“EBA8”),除非使用--apcs / adsabi。
RVCT 3.1编译器和更早版本支持选项--apcs / adsabi来编译与旧的ADS ABI兼容的代码。但是,此选项已被弃用,最终在RVCT 4.0中被删除。
ARM编译器工具链使用两个构建属性REQUIRE8和PRESERVE8来检查八字节堆栈对齐,这些可以是true还是false。这些嵌入到工具生成的对象文件和可执行文件中。 REQUIRE8用于指示代码要求堆栈指针为8字节对齐。 PRESERVE8用于表示代码保留八字节堆栈对齐。
汇编器使用REQUIRE8和PRESERVE8指令来指示REQUIRE8和PRESERVE8构建属性是否为true或false。如果省略PRESERVE8,汇编器将通过检查修改SP的指令来决定是否设置PRES8构建属性。 ARM建议您明确指定PRESERVE8。如果您的程序集需要将堆栈八字节对齐,例如使用SP的LDRD,则应指定REQUIRE8指令。
在RVCT 2.1和更高版本中,如果汇编程序(armasm)检测到堆栈访问将导致堆栈不是八字节对齐,则可以生成警告。这可以通过将--diag_warning 1546添加到汇编器命令行来启用。
链接器使用由编译器和汇编器设置的REQUIRE8和PRESERVE8构建属性来防止需要八字节对齐调用功能的函数,这些函数不保留八字节对齐。如果发生这种情况,链接器将生成错误,例如:
错误:L6238E:foo.o(.text)包含从'&#12316RES8'功能到'REQ8'功能的无效调用
有关此错误消息的更多信息,请参阅工具链的错误和警告文档。
。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-3-29 04:25 , Processed in 0.156691 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表