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 저도 그 부분은 모르겠습니다. ^^
정성태

... 76  77  78  79  80  81  82  83  [84]  85  86  87  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
11833정성태3/4/201920996개발 환경 구성: 431. Visual Studio 2019 - CMake를 이용한 공유/실행(so/out) 리눅스 프로젝트 설정파일 다운로드1
11832정성태3/4/201916946오류 유형: 524. Visual Studio CMake - rsync: connection unexpectedly closed
11831정성태3/4/201916751오류 유형: 523. Visual Studio 2019 - 새 창으로 뜬 윈도우를 닫을 때 비정상 종료
11830정성태2/26/201916453오류 유형: 522. 이벤트 로그 - Error opening event log file State. Log will not be processed. Return code from OpenEventLog is 87.
11829정성태2/26/201918175개발 환경 구성: 430. 마이크로소프트의 CoreCLR 프로파일러 예제 빌드 방법 - 리눅스 환경 [1]
11828정성태2/26/201926066개발 환경 구성: 429. Component Services 관리자의 RuntimeBroker 설정이 2개 있는 경우 [8]
11827정성태2/26/201919022오류 유형: 521. Visual Studio - Could not start the 'rsync' command on the remote host, please install it using your system package manager.
11826정성태2/26/201919212오류 유형: 520. 우분투에 .NET Core SDK 설치 시 패키지 의존성 오류
11825정성태2/25/201924402개발 환경 구성: 428. Visual Studio 2019 - CMake를 이용한 리눅스 빌드 환경 설정 [1]
11824정성태2/25/201918837오류 유형: 519. The SNMP Service encountered an error while accessing the registry key SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration. [1]
11823정성태2/21/201920606오류 유형: 518. IIS 관리 콘솔이 뜨지 않는 문제
11822정성태2/20/201918853오류 유형: 517. docker에 설치한 MongoDB 서버로 연결이 안 되는 경우
11821정성태2/20/201919622오류 유형: 516. Visual Studio 2019 - This extension uses deprecated APIs and is at risk of not functioning in a future VS update. [1]
11820정성태2/20/201922679오류 유형: 515. 윈도우 10 1809 업데이트 후 "User Profiles Service" 1534 경고 발생
11819정성태2/20/201921965Windows: 158. 컴퓨터와 사용자의 SID(security identifier) 확인 방법
11818정성태2/20/201920003VS.NET IDE: 131. Visual Studio 2019 Preview의 닷넷 프로젝트 빌드가 20초 이상 걸리는 경우 [2]
11817정성태2/17/201916382오류 유형: 514. WinDbg Preview 실행 오류 - Error : DbgX.dll : WindowsDebugger.WindowsDebuggerException: Could not load dbgeng.dll
11816정성태2/17/201919791Windows: 157. 윈도우 스토어 앱(Microsoft Store App)을 명령행에서 직접 실행하는 방법
11815정성태2/14/201918031오류 유형: 513. Visual Studio 2019 - VSIX 설치 시 "The extension cannot be installed to this product due to prerequisites that cannot be resolved." 오류 발생
11814정성태2/12/201916865오류 유형: 512. VM(가상 머신)의 NT 서비스들이 자동 시작되지 않는 문제
11813정성태2/12/201918278.NET Framework: 809. C# - ("Save File Dialog" 등의) 대화 창에 확장 속성을 보이는 방법
11812정성태2/11/201915546오류 유형: 511. Windows Server 2003 VM 부팅 후 로그인 시점에 0xC0000005 BSOD 발생
11811정성태2/11/201920693오류 유형: 510. 서버 운영체제에 NVIDIA GeForce Experience 실행 시 wlanapi.dll 누락 문제
11810정성태2/11/201918455.NET Framework: 808. .NET Profiler - GAC 모듈에서 GAC 비-등록 모듈을 참조하는 경우의 문제
11809정성태2/11/201920541.NET Framework: 807. ClrMD를 이용해 메모리 덤프 파일로부터 특정 인스턴스를 참조하고 있는 소유자 확인
11808정성태2/8/201921866디버깅 기술: 123. windbg - 닷넷 응용 프로그램의 메모리 누수 분석
... 76  77  78  79  80  81  82  83  [84]  85  86  87  88  89  90  ...