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

1  2  3  4  5  6  7  [8]  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13430정성태10/30/20232665닷넷: 2153. C# - 사용자가 빌드한 ICU dll 파일을 사용하는 방법
13429정성태10/27/20232940닷넷: 2152. Win32 Interop - C/C++ DLL로부터 이중 포인터 버퍼를 C#으로 받는 예제파일 다운로드1
13428정성태10/25/20233011닷넷: 2151. C# 12 - ref readonly 매개변수
13427정성태10/18/20233197닷넷: 2150. C# 12 - 정적 문맥에서 인스턴스 멤버에 대한 nameof 접근 허용(Allow nameof to always access instance members from static context)
13426정성태10/13/20233359스크립트: 59. 파이썬 - 비동기 호출 함수(run_until_complete, run_in_executor, create_task, run_in_threadpool)
13425정성태10/11/20233169닷넷: 2149. C# - PLinq의 Partitioner<T>를 이용한 사용자 정의 분할파일 다운로드1
13423정성태10/6/20233147스크립트: 58. 파이썬 - async/await 기본 사용법
13422정성태10/5/20233284닷넷: 2148. C# - async 유무에 따른 awaitable 메서드의 병렬 및 예외 처리
13421정성태10/4/20233334닷넷: 2147. C# - 비동기 메서드의 async 예약어 유무에 따른 차이
13420정성태9/26/20235496스크립트: 57. 파이썬 - UnboundLocalError: cannot access local variable '...' where it is not associated with a value
13419정성태9/25/20233163스크립트: 56. 파이썬 - RuntimeError: dictionary changed size during iteration
13418정성태9/25/20233854닷넷: 2146. C# - ConcurrentDictionary 자료 구조의 동기화 방식
13417정성태9/19/20233406닷넷: 2145. C# - 제네릭의 형식 매개변수에 속한 (매개변수를 가진) 생성자를 호출하는 방법
13416정성태9/19/20233202오류 유형: 877. redis-py - MISCONF Redis is configured to save RDB snapshots, ...
13415정성태9/18/20233689닷넷: 2144. C# 12 - 컬렉션 식(Collection Expressions)
13414정성태9/16/20233464디버깅 기술: 193. Windbg - ThreadStatic 필드 값을 조사하는 방법
13413정성태9/14/20233659닷넷: 2143. C# - 시스템 Time Zone 변경 시 이벤트 알림을 받는 방법
13412정성태9/14/20236944닷넷: 2142. C# 12 - 인라인 배열(Inline Arrays) [1]
13411정성태9/12/20233445Windows: 252. 권한 상승 전/후 따로 관리되는 공유 네트워크 드라이브 정보
13410정성태9/11/20234963닷넷: 2141. C# 12 - Interceptor (컴파일 시에 메서드 호출 재작성) [1]
13409정성태9/8/20233818닷넷: 2140. C# - Win32 API를 이용한 모니터 전원 끄기
13408정성태9/5/20233800Windows: 251. 임의로 만든 EXE 파일을 포함한 ZIP 파일의 압축을 해제할 때 Windows Defender에 의해 삭제되는 경우
13407정성태9/4/20233556닷넷: 2139. C# - ParallelEnumerable을 이용한 IEnumerable에 대한 병렬 처리
13406정성태9/4/20233520VS.NET IDE: 186. Visual Studio Community 버전의 라이선스
13405정성태9/3/20233967닷넷: 2138. C# - async 메서드 호출 원칙
13404정성태8/29/20233476오류 유형: 876. Windows - 키보드의 등호(=, Equals sign) 키가 눌리지 않는 경우
1  2  3  4  5  6  7  [8]  9  10  11  12  13  14  15  ...