커스텀 eglibc 라이브러리를 사용(테스트)하려는데 문제가 좀 있습니다.
글쓴이: mandugukbap / 작성시간: 금, 2012/12/07 - 11:45오후
Ubuntu 12.04 32bit 환경에서 eglibc-2.15 소스를 컴파일하고 테스트 코드를 작성해 제가 직접 컴파일한 (내용은 전혀 변경하지 않은 상태입니다.) eglib-2.15를 테스트 하고자 하는데 문제가 있습니다.
테스트 코드는 test-codes-1.c 이고 시스템 디폴트 라이브러리는 이상없이 컴파일 되고 실행 됩니다. 코드는 간단한 URL-to-IP 변환 예제이고 다음과 같습니다.
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <string.h> #define PORT 0x1234 #define HOST "google.com" main(argc, argv) int argc; char **argv; { char hostname[100]; int sd; struct sockaddr_in sin; struct sockaddr_in pin; struct hostent *hp; strcpy(hostname, HOST); if (argc > 2) strcpy(hostname, argv[2]); if ((hp = gethostbyname(hostname)) == 0) { perror("gethostbyname"); return -1; } memset(&pin, 0, sizeof(pin)); pin.sin_family = AF_INET; pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; pin.sin_port = htons(PORT); unsigned long host; unsigned char a, b, c, d; char* logstring; host = ntohl(pin.sin_addr.s_addr); a = host >> 24; b = (host >> 16) & 0xff; c = (host >> 8) & 0xff; d = host & 0xff; /* Return the formatted log entry string */ sprintf(logstring, "%d.%d.%d.%d", a, b, c, d); printf("%s\n", logstring); }
우선 테스트 코드를 컴파일한 Makefile은 다음과 같습니다. 직접 컴파일한 eglibc 라이브러리는 /Str/myusr/lib에 있습니다.
TARGET = test-codes-1 OBJ = $(TARGET).o SRC = $(TARGET).c CC = gcc CFLAGS = -g LDFLAGS = -nostdlib -nostartfiles -static GLIBCDIR = /Str/myusr/lib STARTFILES = $(GLIBCDIR)/crt1.o $(GLIBCDIR)/crti.o `gcc --print-file-name=crtbegin.o` ENDFILES = `gcc --print-file-name=crtend.o` $(GLIBCDIR)/crtn.o LIBGROUP = -Wl,--start-group $(GLIBCDIR)/libc.a -lgcc -lgcc_eh -Wl,--end-group $(TARGET): $(OBJ) $(CC) $(LDFLAGS) -o $@ $(STARTFILES) $^ $(LIBGROUP) $(ENDFILES) $(OBJ): $(SRC) $(CC) $(CFLAGS) -c $^ clean: rm -f *.o *.~ $(TARGET)
해당 파일을 컴파일할 때, 다음과 같은 Warning이 뜹니다만 컴파일은 완료 됩니다.
test-codes-1.c:29: warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
Forums:
에고 글 작성을 끝내기도 전에 포스팅 되어
에고 글 작성을 끝내기도 전에 포스팅 되어 버렸습니다. 아무튼, 파일을 실행 시켜 보면 커스텀 eglib으로 컴파일 한 경우 Segmentation fault가 뜹니다. 특별히 debugging을 해 보지 않아도 앞에 뜬 warning이 원인일 거라는 생각이 드는데.
커스텀 라이브러리를 올바르게 테스트할 수 있는 방법을 여쭙고 싶습니다. 부디 답변 부탁 드립니다.
char* logstring; .... .... /*
할당된 메모리를 가리키지 않는 포인터에 뭔가를 쓰고 있습니다.
-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.
. .. ... .... ..... OTL... 감
.
..
...
....
.....
OTL...
감사합니다.
댓글 달기