Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 4개 있습니다.)

Synology NAS(DS216+II)에서 실행한 gcc의 Segmentation fault

지난 글에서,

Synology NAS 장비(DS216+II)에 gcc 컴파일러 설치
; https://www.sysnet.pe.kr/2/0/11705

gcc(와 g++) 수행 시 "Segmentation fault"가 발생한다고 했습니다.

$ gcc --verbose test.c -o hello.o
Using built-in specs.
Target: i686-linux-gnu
Configured with: ../gcc-4.2.1/configure --build=i386-pc-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu --prefix=/opt --disable-nls --disable-static --with-as=/home/slug/optware/syno-i686/toolchain/gcc-4.2.1-glibc-2.3.6/i686-linux-gnu/bin/i686-linux-gnu-as --with-ld=/home/slug/optware/syno-i686/toolchain/gcc-4.2.1-glibc-2.3.6/i686-linux-gnu/bin/i686-linux-gnu-ld --enable-languages=c,c++ --disable-multilib
Thread model: posix
gcc version 4.2.1
 /opt/libexec/gcc/i686-linux-gnu/4.2.1/cc1 -quiet -v test.c -quiet -dumpbase test.c -mtune=generic -auxbase test -version -o /tmp/cchaTgY0.s
Segmentation fault

$ g++ test.c -o hello.o
Segmentation fault

이때의 dmesg 시스템 로그를 보면 다음과 같이 오류가 남습니다.

$ dmesg
...[생략]...
[ 6497.759313] traps: gcc[29467] general protection ip:23 sp:2b error:244 in gcc[8048000+2b000]

strace로 봐도 저 같은 리눅스 초보자 수준에서는 딱히 해결책이 안 보입니다. ^^;

~# strace gcc -nostdlib -g -c test.c
execve("/opt/bin/gcc", ["gcc", "-nostdlib", "-g", "-c", "test.c"], [/* 21 vars */]) = 0
brk(0)                                  = 0x826d000
brk(0x828e000)                          = 0x828e000
rt_sigaction(SIGINT, {SIG_IGN, [INT], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGINT, {0x804f7ff, [INT], SA_RESTART}, {SIG_IGN, [INT], SA_RESTART}, 8) = 0
rt_sigaction(SIGHUP, {SIG_IGN, [HUP], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGHUP, {0x804f7ff, [HUP], SA_RESTART}, {SIG_IGN, [HUP], SA_RESTART}, 8) = 0
rt_sigaction(SIGTERM, {SIG_IGN, [TERM], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGTERM, {0x804f7ff, [TERM], SA_RESTART}, {SIG_IGN, [TERM], SA_RESTART}, 8) = 0
rt_sigaction(SIGPIPE, {SIG_IGN, [PIPE], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
rt_sigaction(SIGPIPE, {0x804f7ff, [PIPE], SA_RESTART}, {SIG_IGN, [PIPE], SA_RESTART}, 8) = 0
rt_sigaction(SIGCHLD, {SIG_DFL, [CHLD], SA_RESTART}, {SIG_DFL, [], 0}, 8) = 0
access("/opt/bin/gcc", X_OK)            = 0
lstat64("/opt", {st_mode=S_IFDIR|0755, st_size=200, ...}) = 0
lstat64("/opt/bin", {st_mode=S_IFDIR|0755, st_size=6998, ...}) = 0
lstat64("/opt/bin/gcc", {st_mode=S_IFREG|0755, st_size=179372, ...}) = 0
access("/opt/bin/gcc", X_OK)            = 0
lstat64("/opt", {st_mode=S_IFDIR|0755, st_size=200, ...}) = 0
lstat64("/opt/bin", {st_mode=S_IFDIR|0755, st_size=6998, ...}) = 0
lstat64("/opt/bin/gcc", {st_mode=S_IFREG|0755, st_size=179372, ...}) = 0
access("/opt/lib/gcc/i686-linux-gnu/4.2.1/", X_OK) = 0
access("/opt/lib/gcc/i686-linux-gnu/4.2.1/", X_OK) = 0
access("test.c", F_OK)                  = 0
access("/opt/lib/gcc/i686-linux-gnu/4.2.1/specs", R_OK) = -1 ENOENT (No such file or directory)
access("/usr/lib/gcc/i686-linux-gnu/4.2.1/specs", R_OK) = -1 ENOENT (No such file or directory)
access("/opt/lib/gcc/i686-linux-gnu/4.2.1/../../../../i686-linux-gnu/lib/i686-linux-gnu/4.2.1/specs", R_OK) = -1 ENOENT (No such file or directory)
access("/opt/lib/gcc/i686-linux-gnu/4.2.1/../../../../i686-linux-gnu/lib/specs", R_OK) = -1 ENOENT (No such file or directory)
access("/opt/lib/gcc/i686-linux-gnu/specs", R_OK) = -1 ENOENT (No such file or directory)
access("/opt/lib/gcc/i686-linux-gnu/4.2.1/", X_OK) = 0
access("/tmp", R_OK|W_OK|X_OK)          = 0
gettimeofday({1538117841, 562587}, NULL) = 0
getpid()                                = 13801
open("/tmp/cc2NGCrP.s", O_RDWR|O_CREAT|O_EXCL, 0600) = 3
close(3)                                = 0
stat64("/opt/libexec/gcc/i686-linux-gnu/4.2.1/cc1", {st_mode=S_IFREG|0755, st_size=4913668, ...}) = 0
access("/opt/libexec/gcc/i686-linux-gnu/4.2.1/cc1", X_OK) = 0
+++ killed by SIGSEGV +++
Segmentation fault (core dumped)

재미있는 것은 cc1로 직접 빌드한 경우 .c 파일을 .asm으로는 잘 빌드가 됩니다.

root@jst_nas:~# /opt/libexec/gcc/i686-linux-gnu/4.2.1/cc1 test.c -o test.s
 main
Execution times (seconds)
 preprocessing         :   0.00 ( 0%) usr   0.00 ( 0%) sys   0.16 (84%) wall      68 kB ( 7%) ggc
 parser                :   0.01 (100%) usr   0.01 (100%) sys   0.02 (11%) wall     121 kB (12%) ggc
 TOTAL                 :   0.01             0.01             0.19               1012 kB

그런데 collect2의 실행은 그냥 --help만 출력해도 Segmentation fault 오류가 발생합니다.

~# /opt/libexec/gcc/i686-linux-gnu/4.2.1/collect2 --help
Segmentation fault

음... 제 수준에서는 더 이상의 추적이 불가능합니다. ^^




문제를 우회하기 위해 검색한 결과 Synology NAS 용의 패키지 관리자로 Entware의 opkg가 있다는 것을 알게 되었습니다.

Entware/Entware-ng - Install on Synology NAS
; https://github.com/Entware/Entware-ng/wiki/Install-on-Synology-NAS

설치해 볼까요? ^^

우선, 기존 /opt 폴더를 대체하기 위해 임의 폴더를 하나 만들고,

mkdir -p /volume1/@entware-ng/opt

기존 /opt 폴더를 제거 후 위에서 만들어 둔 폴더와 링크를 겁니다.

rm -rf /opt                             # 이제부터는 /opt/bin에서 제공하던 모든 실행 명령어가 오류가 발생합니다.
ln -sf /volume1/@entware-ng/opt /opt

그다음 자신의 환경에 맞는,

~# uname -a
Linux 3.10.105 #23739 SMP Tue Jul 10 00:16:03 CST 2018 x86_64 GNU/Linux synology_braswell_216+II

entware 설치 스크립트를 결정해 다운로드 및 실행합니다.

wget -O - http://pkg.entware.net/binaries/x86-64/installer/entware_install.sh | /bin/sh

마지막으로 부팅 시마다 /opt 링크를 걸어줄 스크립트를 실행하는데, DSM 6.0부터는 /etc/rc.local이 사용되지 않으므로 다음의 스크립트를 생성해야 한다고 합니다.

~# sudo nano /usr/local/etc/rc.d/entware-startup.sh

entware-startup.sh 파일의 내용은 다음과 같습니다.

#!/bin/sh

case $1 in
    start)
    mkdir -p /opt
    mount -o bind /volume1/@entware-ng/opt /opt
    /opt/etc/init.d/rc.unslung start
    ;;
    stop)
    ;;
esac

재부팅하고 다음과 같이 gcc 구성요소를 설치합니다.

# opkg install gcc

그럼 (2018-09-29 기준) 6.3.0-1a 버전이 설치되고, 지난 글에서 "Segmentation fault"가 발생하던 gcc 실행이 이제는 정상적으로 컴파일을 합니다.

~# gcc test.c -o test.out

~# ./test.out
Hello, world!




gcc 실행을 정리해 보면, 우선 cc1로 C 언어 코드를 assembly 코드로 변환합니다. 다음은 entware 환경에서의 빌드 방법입니다.

# /opt/lib/gcc/x86_64-openwrt-linux-gnu/6.3.0/cc1 test.c -o test.s

# 또는 g++인 경우,

# /opt/lib/gcc/x86_64-openwrt-linux-gnu/6.3.0/cc1plus test.c -o test.s

그다음 어셈블리 코드로 번역된 것을 assembler로 컴파일합니다.

# as test.s -o test.o

기계어 코드로 변환된 object 파일을 마지막으로 링커에 태워 실행 파일로 만들어 주는 것이 collect2입니다.

# /opt/lib/gcc/x86_64-openwrt-linux-gnu/6.3.0/collect2 -plugin /opt/lib/gcc/x86_64-openwrt-linux-gnu/6.3.0/liblto_plugin.so -plugin-opt=/opt/lib/gcc/x86_64-openwrt-linux-gnu/6.3.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccnk5tF5.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o test.out /opt/lib/gcc/x86_64-openwrt-linux-gnu/6.3.0/crt1.o /opt/lib/gcc/x86_64-openwrt-linux-gnu/6.3.0/crti.o /opt/lib/gcc/x86_64-openwrt-linux-gnu/6.3.0/crtbegin.o -L/opt/lib/gcc/x86_64-openwrt-linux-gnu/6.3.0 -L/lib/../lib64 -L/usr/lib/../lib64 -L/opt/lib/gcc/x86_64-openwrt-linux-gnu/6.3.0/../../.. test.o -rpath=/opt/lib --dynamic-linker=/opt/lib/ld-linux-x86-64.so.2 -lgcc_s -lc -lgcc_s /opt/lib/gcc/x86_64-openwrt-linux-gnu/6.3.0/crtend.o /opt/lib/gcc/x86_64-openwrt-linux-gnu/6.3.0/crtn.o


참고로 위의 단계들은 "gcc -v" 옵션으로 실행하면 확인할 수 있습니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 9/29/2018]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2020-06-10 07시40분
[123] 이렇게 설정하고나니까 ls명령어같은 명령어가 안먹히는데 혹시 opkg를 삭제하고 전으로 되돌리는 방법이 있을까요
[guest]
2020-06-10 08시41분
@123 저도 그 부분은 모르겠습니다. ^^
정성태

... [61]  62  63  64  65  66  67  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
12110정성태1/11/202011919디버깅 기술: 154. Patch Guard로 인해 블루 스크린(BSOD)가 발생하는 사례 [5]파일 다운로드1
12109정성태1/10/20209814오류 유형: 588. Driver 프로젝트 빌드 오류 - Inf2Cat error -2: "Inf2Cat, signability test failed."
12108정성태1/10/20209834오류 유형: 587. Kernel Driver 시작 시 127(The specified procedure could not be found.) 오류 메시지 발생
12107정성태1/10/202010790.NET Framework: 877. C# - 프로세스의 모든 핸들을 열람 - 두 번째 이야기
12106정성태1/8/202012191VC++: 136. C++ - OSR Driver Loader와 같은 Legacy 커널 드라이버 설치 프로그램 제작 [1]
12105정성태1/8/202010865디버깅 기술: 153. C# - PEB를 조작해 로드된 DLL을 숨기는 방법
12104정성태1/7/202011543DDK: 9. 커널 메모리를 읽고 쓰는 NT Legacy driver와 C# 클라이언트 프로그램 [4]
12103정성태1/7/202014263DDK: 8. Visual Studio 2019 + WDK Legacy Driver 제작- Hello World 예제 [1]파일 다운로드2
12102정성태1/6/202011872디버깅 기술: 152. User 권한(Ring 3)의 프로그램에서 _ETHREAD 주소(및 커널 메모리를 읽을 수 있다면 _EPROCESS 주소) 구하는 방법
12101정성태1/5/202011205.NET Framework: 876. C# - PEB(Process Environment Block)를 통해 로드된 모듈 목록 열람
12100정성태1/3/20209241.NET Framework: 875. .NET 3.5 이하에서 IntPtr.Add 사용
12099정성태1/3/202011560디버깅 기술: 151. Windows 10 - Process Explorer로 확인한 Handle 정보를 windbg에서 조회 [1]
12098정성태1/2/202011130.NET Framework: 874. C# - 커널 구조체의 Offset 값을 하드 코딩하지 않고 사용하는 방법 [3]
12097정성태1/2/20209699디버깅 기술: 150. windbg - Wow64, x86, x64에서의 커널 구조체(예: TEB) 구조체 확인
12096정성태12/30/201911680디버깅 기술: 149. C# - DbgEng.dll을 이용한 간단한 디버거 제작 [1]
12095정성태12/27/201913099VC++: 135. C++ - string_view의 동작 방식
12094정성태12/26/201911273.NET Framework: 873. C# - 코드를 통해 PDB 심벌 파일 다운로드 방법
12093정성태12/26/201911286.NET Framework: 872. C# - 로딩된 Native DLL의 export 함수 목록 출력파일 다운로드1
12092정성태12/25/201910723디버깅 기술: 148. cdb.exe를 이용해 (ntdll.dll 등에 정의된) 커널 구조체 출력하는 방법
12091정성태12/25/201912211디버깅 기술: 147. pdb 파일을 다운로드하기 위한 symchk.exe 실행에 필요한 최소 파일 [1]
12090정성태12/24/201910884.NET Framework: 871. .NET AnyCPU로 빌드된 PE 헤더의 로딩 전/후 차이점 [1]파일 다운로드1
12089정성태12/23/201911572디버깅 기술: 146. gflags와 _CrtIsMemoryBlock을 이용한 Heap 메모리 손상 여부 체크
12088정성태12/23/201910550Linux: 28. Linux - 윈도우의 "Run as different user" 기능을 shell에서 실행하는 방법
12087정성태12/21/201911017디버깅 기술: 145. windbg/sos - Dictionary의 entries 배열 내용을 모두 덤프하는 방법 (do_hashtable.py) [1]
12086정성태12/20/201913066디버깅 기술: 144. windbg - Marshal.FreeHGlobal에서 발생한 덤프 분석 사례
12085정성태12/20/201910805오류 유형: 586. iisreset - The data is invalid. (2147942413, 8007000d) 오류 발생 - 두 번째 이야기 [1]
... [61]  62  63  64  65  66  67  68  69  70  71  72  73  74  75  ...