如何用gcc从libc内联函数? [英] How to inline a function from libc with gcc?

查看:133
本文介绍了如何用gcc从libc内联函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下问题:我的软件正在引起死锁,而关于它的起源的唯一线索是我的芯片组针对这种情况仅提供了8个级别的原始堆栈跟踪.

I'm having the following problem: my software is causing a deadlock and the only clue that I have about its origin is a primitive stack trace with only 8 levels provided by my chipset for situations like this.

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

However, my stack trace is something like:

memset
memset
memset
memset
memset
memset
memset
memset

我想以某种方式删除memset,以查看导致此混乱的函数.如果我要求gcc内联memset,它将抱怨说我没有函数体.

I want to remove memset out of my way to see which function is causing this mess. If I ask gcc to inline memset, it will complain stating that I don't have the function body for it.

那么,有没有办法内联内存集?还有人对调试此问题有其他想法吗?

So, is there a way to inline memset? Does anyone have any other idea about debugging this problem?

编辑

我已经尝试使用 for 将哑剧集替换为愚蠢的实现,但这使我的软件表现异常,并且出现了其他问题,使我无法进入僵局.

I've already tried to replace memset with a dumb implementation using a for, but this made my software behave strangely and other problems appear that prevents me from getting to the deadlock.

编辑

顺便说一句,我正在使用MIPS拱门.我不是MIPS汇编专家,所以我做了愚蠢的尝试,以获取反汇编的memset代码并将其插入C函数的主体中.我从objdump -d和gdb反汇编中获得了不同的指令,因此我尝试了使用它们.这是我创建的功能:

By the way, I'm using the MIPS arch. I'm not an MIPS assembly expert, so I've made a dumb attempt at getting the disassembled code for memset and inserting in the body of a C function. I got different instructions from objdump -d and gdb disassemble, so I've tried with both of them. Here are the functions that I've created:

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

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

Can someone help with those errors?

编辑

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

Tried another version with the source code from :

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函数体中进行内联的答案. /p>

The problem was indeed related to stack corruption, but I'll let this question here to see if anyone knows the answer about how to get the disassembled code from a subroutine and put it back into a C function body for inlining.

推荐答案

gdbobjdump都不生成gcc内联MIPS汇编程序将接受的代码.我不知道有任何工具可以自动修复该语法,因此需要对汇编器语法进行手动更正.您还需要告诉gcc内联asm代码将更改哪些寄存器和/或内存.所有这些都不是一件容易的事.

Neither gdb nor objdump produce code that the gcc inline MIPS assembler will accept. I'm not aware of any tool that automatically fixes the syntax, so manual correction of the assembler syntax will be needed. You would also need to tell gcc which registers and/or memory will get altered by the inline asm code. All of this is not a trivial task.

如果您具有C源代码,则gcc-S选项会生成语法上几乎正确的MIPS汇编程序,但是即使如此,也需要进行一些手动编辑.

If you had C source code,the -S option of gcc produces almost syntactically correct MIPS assembler, but even that requires some manual editing.

这篇关于如何用gcc从libc内联函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆