1

我遇到了以下问题:我的软件导致了死锁,我对它的起源的唯一线索是一个原始堆栈跟踪,我的芯片组只为这种情况提供了 8 个级别。

但是,我的堆栈跟踪类似于:

memset
memset
memset
memset
memset
memset
memset
memset

我想删除 memset 以查看是哪个函数导致了这种混乱。如果我要求 gcc 内联 memset,它会抱怨说我没有它的函数体。

那么,有没有办法内联 memset 呢?有没有人对调试这个问题有任何其他想法?

编辑

我已经尝试使用for将 memset 替换为愚蠢的实现,但这使我的软件行为异常,并且出现了其他问题,使我无法陷入僵局。

编辑

顺便说一句,我正在使用 MIPS 拱门。我不是 MIPS 汇编专家,所以我做了一个愚蠢的尝试来获取 memset 的反汇编代码并插入 C 函数的主体。我从 objdump -d 和 gdb disassemble 得到了不同的指令,所以我对它们都进行了尝试。以下是我创建的函数:

void * memset(void * str_a, int ch, size_t count) {
    asm("mov    #12,r0");
    asm("cmp/gt r6,r0");
    asm("mov    r4,r0");
    asm("bt.s   30 <.L_dup_bytes+0x18>");
    asm("add    r4,r6");
    asm("tst    #3,r0");
    asm("bt.s   18 <.L_dup_bytes>");
    asm("extu.b r5,r5");
    asm("mov.b  r5,@r0");
    asm("add    #1,r0");
    asm("tst    #3,r0");
    asm("bf 10 <_memset+0x10>");
}

void * memset(void * str_a, int ch, size_t count) {
    asm("mov    #12,r0");
    asm("cmp/gt r6,r0");
    asm("mov    r4,r0");
    asm("bt.s   0x8163682c <memset+48>");
    asm("add    r4,r6");
    asm("tst    #3,r0");
    asm("bt.s   0x81636814 <memset+24>");
    asm("extu.b r5,r5");
    asm("mov.b  r5,@r0");
    asm("add    #1,r0");
    asm("tst    #3,r0");
    asm("bf 0x8163680c <memset+16>");
    asm("swap.b r5,r2");
    asm("or r2,r5");
    asm("swap.w r5,r2");
    asm("or r2,r5");
    asm("add    #-16,r6");
    asm("nop    ");
    asm("mov.l  r5,@r0");
    asm("cmp/hs r6,r0");
    asm("mov.l  r5,@(4,r0)");
    asm("bf.s   0x81636820 <memset+36>");
    asm("add    #8,r0");
    asm("add    #16,r6");
    asm("cmp/eq r6,r0");
    asm("bt 0x81636838 <memset+60>");
    asm("mov.b  r5,@r0");
    asm("add    #1,r0");
    asm("cmp/eq r6,r0");
    asm("bf 0x81636830 <memset+52>");
    asm("rts    ");
    asm("mov    r4,r0");
}

这给了我以下错误:

第一个内存集:

/tmp/ccAuFFap.s: Assembler messages:
/tmp/ccAuFFap.s:3457: Warning: missing operand; zero assumed
/tmp/ccAuFFap.s:3469: Warning: missing operand; zero assumed
/tmp/ccAuFFap.s:3489: Warning: missing operand; zero assumed
/tmp/ccAuFFap.s:3457: Error: displacement to defined symbol .L0 overflows 8-bit field
/tmp/ccAuFFap.s:3469: Error: displacement to undefined symbol .L0 overflows 8-bit field 
/tmp/ccAuFFap.s:3489: Error: displacement to defined symbol .L0 overflows 8-bit field

第二个内存集:

/tmp/cctCDgi5.s: Assembler messages:
/tmp/cctCDgi5.s:3457: Warning: missing operand; zero assumed
/tmp/cctCDgi5.s:3469: Warning: missing operand; zero assumed
/tmp/cctCDgi5.s:3489: Warning: missing operand; zero assumed
/tmp/cctCDgi5.s:3529: Warning: missing operand; zero assumed
/tmp/cctCDgi5.s:3545: Warning: missing operand; zero assumed
/tmp/cctCDgi5.s:3561: Warning: missing operand; zero assumed
/tmp/cctCDgi5.s:3457: Error: displacement to defined symbol .L0 overflows 8-bit field
/tmp/cctCDgi5.s:3469: Error: displacement to defined symbol .L0 overflows 8-bit field
/tmp/cctCDgi5.s:3489: Error: displacement to defined symbol .L0 overflows 8-bit field
/tmp/cctCDgi5.s:3529: Error: displacement to defined symbol .L0 overflows 8-bit field
/tmp/cctCDgi5.s:3545: Error: displacement to defined symbol .L0 overflows 8-bit field
/tmp/cctCDgi5.s:3561: Error: displacement to defined symbol .L0 overflows 8-bit field

有人可以帮助解决这些错误吗?

编辑

使用以下源代码尝试了另一个版本:

void * memset(void * str_a, int ch, size_t count) {    
    asm("slti    t1, a2, 8               ");
    asm("bne     t1, zero, L(last8)");
    asm("move    v0, a0                  ");
    asm("beq     a1, zero, L(ueven)      ");
    asm("andi    a1, 0xff                ");
    asm("sll     t0, a1, 8");
    asm("or      a1, t0");
    asm("sll     t0, a1, 16");
    asm("or      a1, t0                  ");
    asm("L(ueven):       ");
    asm("subu    t0, zero, a0            ");
    asm("andi    t0, 0x3");
    asm("beq     t0, zero, L(chkw)");
    asm("subu    a2, t0");
    asm("SWHI    a1, 0(a0)               ");
    asm("addu    a0, t0                  ");
    asm("L(chkw):        ");
    asm("andi    t0, a2, 0x7             ");
    asm("beq     t0, a2, L(chkl)");
    asm("subu    a3, a2, t0");
    asm("addu    a3, a0                  ");
    asm("move    a2, t0                  ");
    asm("L(loopw):       ");
    asm("addiu   a0, 8                   ");
    asm("sw      a1, -8(a0)");
    asm("bne     a0, a3, L(loopw)");
    asm("sw      a1, -4(a0)");
    asm("L(chkl):        ");
    asm("andi    t0, a2, 0x4             ");
    asm("beq     t0, zero, L(last8)      ");
    asm("subu    a2, t0");
    asm("sw      a1, 0(a0)               ");
    asm("addiu   a0, 4");
    asm("L(last8):       ");
    asm("blez    a2, L(exit)             ");
    asm("addu    a3, a2, a0              ");
    asm("L(lst8l):       ");
    asm("addiu   a0, 1");
    asm("bne     a0, a3, L(lst8l)");
    asm("sb      a1, -1(a0)");
    asm("L(exit):        ");
    asm("j       ra                      ");
    asm("nop");
}

这次的错误如下:

/tmp/ccuyfGW5.s: Assembler messages:
/tmp/ccuyfGW5.s:5131: Error: unknown opcode
/tmp/ccuyfGW5.s:5135: Error: unknown opcode
/tmp/ccuyfGW5.s:5139: Error: unknown opcode
/tmp/ccuyfGW5.s:5143: Error: unknown opcode
/tmp/ccuyfGW5.s:5147: Error: unknown opcode
/tmp/ccuyfGW5.s:5151: Error: unknown opcode
/tmp/ccuyfGW5.s:5155: Error: invalid operands for opcode
/tmp/ccuyfGW5.s:5159: Error: unknown opcode
/tmp/ccuyfGW5.s:5163: Error: invalid operands for opcode
/tmp/ccuyfGW5.s:5167: Error: unknown opcode
/tmp/ccuyfGW5.s:5171: Error: unknown opcode
/tmp/ccuyfGW5.s:5175: Error: unknown opcode
/tmp/ccuyfGW5.s:5179: Error: unknown opcode
/tmp/ccuyfGW5.s:5183: Error: unknown opcode
/tmp/ccuyfGW5.s:5187: Error: unknown opcode
/tmp/ccuyfGW5.s:5191: Error: unknown opcode
/tmp/ccuyfGW5.s:5195: Error: unknown opcode
/tmp/ccuyfGW5.s:5199: Error: unknown opcode
/tmp/ccuyfGW5.s:5203: Error: unknown opcode
/tmp/ccuyfGW5.s:5207: Error: unknown opcode
/tmp/ccuyfGW5.s:5211: Error: unknown opcode
/tmp/ccuyfGW5.s:5215: Error: unknown opcode
/tmp/ccuyfGW5.s:5219: Error: unknown opcode
/tmp/ccuyfGW5.s:5223: Error: unknown opcode
/tmp/ccuyfGW5.s:5227: Error: unknown opcode
/tmp/ccuyfGW5.s:5231: Error: unknown opcode
/tmp/ccuyfGW5.s:5235: Error: unknown opcode
/tmp/ccuyfGW5.s:5239: Error: unknown opcode
/tmp/ccuyfGW5.s:5243: Error: unknown opcode
/tmp/ccuyfGW5.s:5247: Error: unknown opcode
/tmp/ccuyfGW5.s:5251: Error: unknown opcode
/tmp/ccuyfGW5.s:5255: Error: unknown opcode
/tmp/ccuyfGW5.s:5259: Error: unknown opcode
/tmp/ccuyfGW5.s:5263: Error: unknown opcode
/tmp/ccuyfGW5.s:5267: Error: unknown opcode
/tmp/ccuyfGW5.s:5271: Error: unknown opcode
/tmp/ccuyfGW5.s:5275: Error: unknown opcode
/tmp/ccuyfGW5.s:5279: Error: unknown opcode
/tmp/ccuyfGW5.s:5283: Error: unknown opcode
/tmp/ccuyfGW5.s:5287: Error: unknown opcode
/tmp/ccuyfGW5.s:5291: Error: unknown opcode
/tmp/ccuyfGW5.s:5295: Error: unknown opcode

编辑

这个问题确实与堆栈损坏有关,但我会在这里提出这个问题,看看是否有人知道如何从子例程中获取反汇编代码并将其放回 C 函数体进行内联的答案。

4

1 回答 1

2

既不gdb也不objdump产生gcc内联 MIPS 汇编器将接受的代码。我不知道有任何工具可以自动修复语法,因此需要手动更正汇编器语法。您还需要知道gcc哪些寄存器和/或内存将被内联 asm 代码更改。所有这一切都不是一项微不足道的任务。

如果您C有源代码,则-S可以选择gcc生成几乎语法正确的 MIPS 汇编程序,但即使这样也需要一些手动编辑。

于 2013-10-29T20:43:01.013 回答