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

... 31  32  33  [34]  35  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12770정성태8/11/20218420.NET Framework: 1086. C# - Windows Forms 응용 프로그램의 자식 컨트롤 부하파일 다운로드1
12769정성태8/11/20216409오류 유형: 752. Python - ImportError: No module named pip._internal.cli.main 두 번째 이야기
12768정성태8/10/20217433.NET Framework: 1085. .NET 6에 포함된 신규 BCL API [1]파일 다운로드1
12767정성태8/10/20218480오류 유형: 752. Python - ImportError: No module named pip._internal.cli.main
12766정성태8/9/20217015Java: 32. closing inbound before receiving peer's close_notify
12765정성태8/9/20216332Java: 31. Cannot load JDBC driver class 'org.mysql.jdbc.Driver'
12764정성태8/9/202144790Java: 30. XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid
12763정성태8/9/20217790Java: 29. java.lang.NullPointerException - com.mysql.jdbc.ConnectionImpl.getServerCharset
12762정성태8/8/202111311Java: 28. IntelliJ - Unable to open debugger port 오류
12761정성태8/8/20218531Java: 27. IntelliJ - java: package javax.inject does not exist [2]
12760정성태8/8/20215942개발 환경 구성: 594. 전용 "Command Prompt for ..." 단축 아이콘 만들기
12759정성태8/8/20219079Java: 26. IntelliJ + Spring Framework + 새로운 Controller 추가 [2]파일 다운로드1
12758정성태8/7/20218413오류 유형: 751. Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
12757정성태8/7/20219104Java: 25. IntelliJ + Spring Framework 프로젝트 생성
12756정성태8/6/20217921.NET Framework: 1084. C# - .NET Core Web API 단위 테스트 방법 [1]파일 다운로드1
12755정성태8/5/20217055개발 환경 구성: 593. MSTest - 단위 테스트에 static/instance 유형의 private 멤버 접근 방법파일 다운로드1
12754정성태8/5/20217990오류 유형: 750. manage.py - Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
12753정성태8/5/20218204오류 유형: 749. PyCharm - Error: Django is not importable in this environment
12752정성태8/4/20216312개발 환경 구성: 592. JetBrains의 IDE(예를 들어, PyCharm)에서 Visual Studio 키보드 매핑 적용
12751정성태8/4/20219351개발 환경 구성: 591. Windows 10 WSL2 환경에서 docker-compose 빌드하는 방법
12750정성태8/3/20216154디버깅 기술: 181. windbg - 콜 스택의 "Call Site" 오프셋 값이 가리키는 위치
12749정성태8/2/20215572개발 환경 구성: 590. Visual Studio 2017부터 단위 테스트에 DataRow 특성 지원
12748정성태8/2/20216186개발 환경 구성: 589. Azure Active Directory - tenant의 관리자(admin) 계정 로그인 방법
12747정성태8/1/20216788오류 유형: 748. 오류 기록 - MICROSOFT GRAPH – HOW TO IMPLEMENT IAUTHENTICATIONPROVIDER파일 다운로드1
12746정성태7/31/20218774개발 환경 구성: 588. 네트워크 장비 환경을 시뮬레이션하는 Packet Tracer 프로그램 소개
12745정성태7/31/20216625개발 환경 구성: 587. Azure Active Directory - tenant의 관리자 계정 로그인 방법
... 31  32  33  [34]  35  36  37  38  39  40  41  42  43  44  45  ...