链接时已编译的gcc .s代码存在问题 [英] Problems with compiled gcc .s Code when linking
问题描述
第一次在这里,我运行的是Kali linux 64bits,我是linux新秀,也是ASM的新手....于是我用C语言编写了一个代码,它工作得非常好....这里是代码:
First time here, Im running Kali linux 64bits ,Im a linux rookie and a new to ASM aswell.... So I pulled a code in C ,the wich works perfectly fine..... here is the code:
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
int main(int argc , char *argv[])
{
int socket_desc;
struct sockaddr_in server;
char *message , server_reply[2000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 2000 );
//Connect to remote server
if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) <0)
{
puts("connect error");
return 1;
}
puts("Connected\n");
//Send some data
message = "Hola!!!!\n\r\n";
if( send(socket_desc , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
puts("Data Send\n");
//Receive a reply from the server
if( recv(socket_desc, server_reply , 2000 , 0) < 0)
{
puts("recv failed");
}
puts("Reply received\n");
puts(server_reply);
return 0;
}
所以...我使用gcc -S -o example.s example.c来获取ASM代码...这是:
So ... I use gcc -S -o example.s example.c , to get the ASM code... wich is:
.file "test.c"
.section .rodata
.LC0:
.string "Could not create socket"
.LC1:
.string "127.0.0.1"
.LC2:
.string "connect error"
.LC3:
.string "Connected\n"
.align 8
.LC4:
.string "Hola!! , \n\r\n"
.LC5:
.string "Send failed"
.LC6:
.string "Data Send\n"
.LC7:
.string "recv failed"
.LC8:
.string "Reply received\n"
.text
.globl main
.type main, @function
main:
.LFB2:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $2048, %rsp
movl %edi, -2036(%rbp)
movq %rsi, -2048(%rbp)
movl $0, %edx
movl $1, %esi
movl $2, %edi
call socket
movl %eax, -4(%rbp)
cmpl $-1, -4(%rbp)
jne .L2
movl $.LC0, %edi
movl $0, %eax
call printf
.L2:
movl $.LC1, %edi
call inet_addr
movl %eax, -28(%rbp)
movw $2, -32(%rbp)
movl $2000, %edi
call htons
movw %ax, -30(%rbp)
leaq -32(%rbp), %rcx
movl -4(%rbp), %eax
movl $16, %edx
movq %rcx, %rsi
movl %eax, %edi
call connect
testl %eax, %eax
jns .L3
movl $.LC2, %edi
call puts
movl $1, %eax
jmp .L7
.L3:
movl $.LC3, %edi
call puts
movq $.LC4, -16(%rbp)
movq -16(%rbp), %rax
movq %rax, %rdi
call strlen
movq %rax, %rdx
movq -16(%rbp), %rsi
movl -4(%rbp), %eax
movl $0, %ecx
movl %eax, %edi
call send
testq %rax, %rax
jns .L5
movl $.LC5, %edi
call puts
movl $1, %eax
jmp .L7
.L5:
movl $.LC6, %edi
call puts
leaq -2032(%rbp), %rsi
movl -4(%rbp), %eax
movl $0, %ecx
movl $2000, %edx
movl %eax, %edi
call recv
testq %rax, %rax
jns .L6
movl $.LC7, %edi
call puts
.L6:
movl $.LC8, %edi
call puts
leaq -2032(%rbp), %rax
movq %rax, %rdi
call puts
movl $0, %eax
.L7:
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2:
.size main, .-main
.ident "GCC: (Debian 4.9.2-10) 4.9.2"
因此,在将example.s -o example.o用作示例之后,我使用ld example.o -o example,多数民众赞成在其中得到以下错误:
So after using as example.s -o example.o, I use ld example.o -o example, and thats where I get these following errors:
ld:警告:找不到入口符号 _start;默认为00000000004000b0test.o:在功能
main'中:test.c :(.text + 0x28):未定义对
套接字的引用test.c :(.text + 0x40):对printf'的未定义引用test.c :(.text + 0x4a):对
inet_addr的未定义引用test.c :(.text + 0x5d):未定义对htons'的引用test.c :(.text + 0x77):未定义对
connect的引用test.c :(.text + 0x85):对puts的未定义引用test.c :(.text + 0x99):对
puts的未定义引用test.c :(.text + 0xad):对strlen'的未定义引用test.c :(.text + 0xc3):未定义引用
发送test.c :(.text + 0xd2):对puts的未定义引用test.c :(.text + 0xe3):对
puts的未定义引用test.c :(.text + 0xfe):对recv'的未定义引用test.c :(.text + 0x10d):未定义对
puts的引用test.c :(.text + 0x117):对puts的未定义引用test.c :(.text + 0x126):对
puts'
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0 test.o: In function
main': test.c:(.text+0x28): undefined reference to
socket' test.c:(.text+0x40): undefined reference toprintf' test.c:(.text+0x4a): undefined reference to
inet_addr' test.c:(.text+0x5d): undefined reference tohtons' test.c:(.text+0x77): undefined reference to
connect' test.c:(.text+0x85): undefined reference toputs' test.c:(.text+0x99): undefined reference to
puts' test.c:(.text+0xad): undefined reference tostrlen' test.c:(.text+0xc3): undefined reference to
send' test.c:(.text+0xd2): undefined reference toputs' test.c:(.text+0xe3): undefined reference to
puts' test.c:(.text+0xfe): undefined reference torecv' test.c:(.text+0x10d): undefined reference to
puts' test.c:(.text+0x117): undefined reference toputs' test.c:(.text+0x126): undefined reference to
puts'
在我看来,gcc没有正确使用.start,global main等.但是老实说,我不知道如何解决它.如果这是正确的,那为什么呢?
it seems to me that gcc is not usingn correctly .start, global main, etc. but to be honest I wouldnt know how to fix it., if this is correct then why?
任何帮助将不胜感激.
谢谢.
推荐答案
尝试一下
gcc -Wall -O -fverbose-asm -S example.c
gcc -c example.s -o example.o
gcc example.o -o example
这篇关于链接时已编译的gcc .s代码存在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!