如何从C/ c++程序调用__strlen_sse2 ?(Linux)

时间:2021-10-14 16:03:44

I'll state off the bat that I'm not a programmer, and am probably in over my head.

我马上声明我不是一个程序员,而且我可能在我的头脑中。

I'm trying to track down a bug in in the __strlen_sse2 (assembly) function installed in Debian as part of libc6-i686.

我正在跟踪__strlen_sse2 (assembly)函数中的一个bug,该函数作为libc6-i686的一部分安装在Debian中。

I already have a copy of the assembly code (.S file) and I need to figure out a way to call it from a C/C++ program. How can I achieve this?

我已经有一个汇编代码的副本(。我需要找到一种从C/ c++程序调用它的方法。我如何做到这一点?

edit:

编辑:

Tried this code, but I get an error from gcc about undefined reference to '__strlen_sse2'

尝试了这段代码,但是我从gcc获得了一个关于'__strlen_sse2'的未定义引用的错误

edit 2: It's my understanding that this is the proper answer to the question, but I lack the proper knowledge to carry it to completion. Thanks for the help everyone.

编辑2:我的理解是这是问题的正确答案,但我缺乏正确的知识来完成它。谢谢大家的帮助。

#include <stdio.h>
#include <string.h>

size_t __strlen_sse2(const char *);

void main()
{
    char buffer[255];

    printf("Standby.. ");
    gets(buffer);
    __strlen_sse2("CRASH!");
    printf("OK!\n");
}

Like I said... not a programmer.

就像我说的…不是一个程序员。

I hope my question makes sense. Please let me know if you need any more information.

我希望我的问题有意义。如果你需要更多的信息,请告诉我。

2 个解决方案

#1


2  

You can't directly call the copy of __strlen_sse2 inside the usual, dynamically-linked /lib/libc.so.6 because it is a "hidden symbol" -- accessible to code inside libc itself, but not available for external linking.

您不能直接调用__strlen_sse2的副本在通常的动态链接/lib/libc. so。因为它是一个“隐藏的符号”——可以访问libc内部的代码,但不能用于外部链接。

You say you have the .S file that defines __strlen_sse2, from glibc's source code, but you're going to need to modify it to be buildable outside glibc. I found what I think is the right file, and was able to modify it pretty easily. Delete everything up to but not including the line that reads just

您说您有一个定义了__strlen_sse2的. s文件,它来自glibc的源代码,但是您需要修改它,使其在glibc之外是可构建的。我找到了我认为正确的文件,并且能够很容易地修改它。删除所有内容,但不包括只读的行。

        .text

and replace it with this:

用这个来代替

#define PUSH(REG)       pushl REG
#define POP(REG)        popl REG
#define PARMS           4
#define STR             PARMS
#define ENTRANCE
#define RETURN          ret
#define L(x)            .L##x
#define ENTRY(x)        .globl x; .type x,@function; x:
#define END(x)          .size x, .-x

Also delete the #endif line at the very end of the file. Then compile like so:

还要删除文件末尾的#endif行。然后编译如下:

gcc -m32 -c strlen-sse2.S
gcc -m32 -c test.c
gcc -m32 test.o strlen-sse2.o
./a.out

You may not need the -m32s.

您可能不需要-m32。

You might be able to get some help with your larger problem from superuser.com -- provide the contents of /proc/cpuinfo in your question.

你可能可以从superuser.com得到一些帮助来解决你的大问题——在你的问题中提供/proc/cpuinfo的内容。

#2


1  

Just declare the function, preferably with a full prototype, and call it. This is probably the right prototype:

只需声明函数,最好是完整的原型,然后调用它。这可能是正确的原型:

size_t __strlen_sse2(const char *);

I'm a bit skeptical of your claim that you're trying to track down a bug in this function. It's more likely there's a bug in your program that's calling strlen with an invalid argument (either an invalid pointer or a pointer to something other than a null-terminated string).

我有点怀疑你的说法,你试图追踪这个函数中的一个错误。你的程序中很可能有一个错误,它调用strlen的参数是无效的(一个无效的指针或者一个指向其他东西的指针,而不是一个空终止的字符串)。

#1


2  

You can't directly call the copy of __strlen_sse2 inside the usual, dynamically-linked /lib/libc.so.6 because it is a "hidden symbol" -- accessible to code inside libc itself, but not available for external linking.

您不能直接调用__strlen_sse2的副本在通常的动态链接/lib/libc. so。因为它是一个“隐藏的符号”——可以访问libc内部的代码,但不能用于外部链接。

You say you have the .S file that defines __strlen_sse2, from glibc's source code, but you're going to need to modify it to be buildable outside glibc. I found what I think is the right file, and was able to modify it pretty easily. Delete everything up to but not including the line that reads just

您说您有一个定义了__strlen_sse2的. s文件,它来自glibc的源代码,但是您需要修改它,使其在glibc之外是可构建的。我找到了我认为正确的文件,并且能够很容易地修改它。删除所有内容,但不包括只读的行。

        .text

and replace it with this:

用这个来代替

#define PUSH(REG)       pushl REG
#define POP(REG)        popl REG
#define PARMS           4
#define STR             PARMS
#define ENTRANCE
#define RETURN          ret
#define L(x)            .L##x
#define ENTRY(x)        .globl x; .type x,@function; x:
#define END(x)          .size x, .-x

Also delete the #endif line at the very end of the file. Then compile like so:

还要删除文件末尾的#endif行。然后编译如下:

gcc -m32 -c strlen-sse2.S
gcc -m32 -c test.c
gcc -m32 test.o strlen-sse2.o
./a.out

You may not need the -m32s.

您可能不需要-m32。

You might be able to get some help with your larger problem from superuser.com -- provide the contents of /proc/cpuinfo in your question.

你可能可以从superuser.com得到一些帮助来解决你的大问题——在你的问题中提供/proc/cpuinfo的内容。

#2


1  

Just declare the function, preferably with a full prototype, and call it. This is probably the right prototype:

只需声明函数,最好是完整的原型,然后调用它。这可能是正确的原型:

size_t __strlen_sse2(const char *);

I'm a bit skeptical of your claim that you're trying to track down a bug in this function. It's more likely there's a bug in your program that's calling strlen with an invalid argument (either an invalid pointer or a pointer to something other than a null-terminated string).

我有点怀疑你的说法,你试图追踪这个函数中的一个错误。你的程序中很可能有一个错误,它调用strlen的参数是无效的(一个无效的指针或者一个指向其他东西的指针,而不是一个空终止的字符串)。