编译此C++代码时:

#include "../SM64DS_2.h"

//@021026a0, hooks: 0x020bddb8, 0x020bde0c
void CancelPowerups(Player& player)
{
    if(player.isFreezeFramePeach)
    {
        player.isFreezeFramePeach = false;
        player.EndMusic(0x33);
    }
    if(player.isDashyToad)
    {
        player.isDashyToad = false;
        player.EndMusic(0x33);
    }
}

//hook: 0x020b98f8
void EnableFreezeFramePeach(Player& player)
{
    //TODO: Show message on first use
    player.isFreezeFramePeach = true;
    player.flowerPowerTimer = 600;
    player.SetMusic(0x33);
}

//hook: 0x020b9904
void EnableDashyToad(Player& player)
{
    //TODO: Show message on first use
    player.isDashyToad = true;
    player.flowerPowerTimer = 600;
    player.SetMusic(0x33);
}

//hook: 0x020e4f10
void WorkDashyToadPart1(Player& player)
{
    //TODO: Deal with speed of walking animation
    if(player.isDashyToad)
    {
        player.horzSpeed /= 2;
        player.isSpeedHalfed = true;
        if(player.walkOnWaterTimer == 61)
            ++player.walkOnWaterTimer;
    }
}

//hook: 0x020e52e8
void WorkDashyToadPart2(Player& player)
{
    if(player.isSpeedHalfed)
    {
        player.horzSpeed *= 2;
        player.isSpeedHalfed = false;
        player.dashyToadParticleSysID = Particle::System::New(player.dashyToadParticleSysID,
                                                              0x4a,
                                                              player.pos.x,
                                                              player.pos.y + Fix12i(72),
                                                              player.pos.z,
                                                              nullptr,
                                                              nullptr);
    }
}

带有gcc的以下标志
arm-none-eabi-g++ -MMD -MP -MF /c/Users/Joshua/Documents/desmume-0.9.11-win64/SM64DSe/SM64DSe/ASMPatchTemplate/build/PeachToadAbilities.d -g -Wall -O2 -march=armv5te -mtune=arm946e-s -fomit-frame-pointer -ffast-math  -iquote /c/Users/Joshua/Documents/desmume-0.9.11-win64/SM64DSe/SM64DSe/ASMPatchTemplate/include -I/c/devkitPro/libnds/include -I/c/devkitPro/devkitARM/include -I/c/devkitPro/devkitARM/arm-none-eabi/include -I/c/Users/Joshua/Documents/desmume-0.9.11-win64/SM64DSe/SM64DSe/ASMPatchTemplate/build -DARM9 -nodefaultlibs -I. -fno-builtin -c --std=c++14 -fno-rtti -fno-exceptions -Wa,-adl=PeachToadAbilities.lst -c /c/Users/Joshua/Documents/desmume-0.9.11-win64/SM64DSe/SM64DSe/ASMPatchTemplate/source/C_PeachToadAbilities/PeachToadAbilities.cpp -o PeachToadAbilities.o

(特别注意,该体系结构是ARMv5TE,而CPU是ARM946E-S)
为最后一个功能生成以下ASM:
push    {r4, lr}
mov     r4, r0
mov     r1, #0x0
ldrd    r2, [r4, #0x5c]
ldr     ip, [r0, #0x98]
ldr     r0, [r0, #0x280]
lsl     ip, ip, #0x1
str     ip, [r4, #0x98]
ldr     ip, [r4, #0x64]
sub     sp, sp, #0x10
strb    r1, [r4, #0x27d]
add     r3, r3, #0x48000
str     r1, [sp, #0x8]
str     r1, [sp, #0x4]
str     ip, [sp]
mov     r1, #0x4a
bl      _ZN8Particle6System3NewEjj5Fix12IiES2_S2_PK11Vector3_16fPNS_8CallbackE
str     r0, [r4, #0x280]
add     sp, sp, #0x10
pop     {r4, pc}

就上下文而言,这是Nintendo DS游戏的ROM破解。 Player结构是4字节对齐的,并且偏移量0x5C是播放器的位置,它是3 Fix12 的结构(Fix12 是1个4字节整数的结构)。

问题在于gcc生成了一个“ldrd”指令,该指令无法证明可以访问8字节对齐的地址(它不应该知道“player”在哪里,而且0x5C不能是8的倍数)。我看过的ARM文档说,对于ARMv5,加载未对齐的双字是未定义的行为。但是,no $ gba和DeSmuME假定地址是4字节对齐的访问仍然从该地址加载8字节的值。对于ARM946E-S CPU上的ARMv5TE,这是正确的行为吗? (我没有在实际的DS上进行测试,因为我没有闪存卡。)

最佳答案

我还通过ARMv5TE和ARM946E-S以及GCC 6看到了这一点(在5中还可以)。就我而言,代码正在加载数组的两个连续元素,但是编译器无法证明传入的数组指针是8字节对齐的。我将尝试生成一个简单的示例并进行报告。您应该做同样的事情,希望它将得到解决。

07-24 21:21