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)
13531정성태1/16/20242065닷넷: 2204. C# - TCP KeepAlive에 새로 추가된 Retry 옵션파일 다운로드1
13530정성태1/15/20242042닷넷: 2203. C# - Python과의 AES 암호화 연동파일 다운로드1
13529정성태1/15/20241922닷넷: 2202. C# - PublishAot의 glibc에 대한 정적 링킹하는 방법
13528정성태1/14/20242062Linux: 68. busybox 컨테이너에서 실행 가능한 C++, Go 프로그램 빌드
13527정성태1/14/20242012오류 유형: 892. Visual Studio - Failed to launch debug adapter. Additional information may be available in the output window.
13526정성태1/14/20242109닷넷: 2201. C# - Facebook 연동 / 사용자 탈퇴 처리 방법
13525정성태1/13/20242064오류 유형: 891. Visual Studio - Web Application을 실행하지 못하는 IISExpress
13524정성태1/12/20242113오류 유형: 890. 한국투자증권 KIS Developers OpenAPI - GW라우팅 중 오류가 발생했습니다.
13523정성태1/12/20241933오류 유형: 889. Visual Studio - error : A project with that name is already opened in the solution.
13522정성태1/11/20242094닷넷: 2200. C# - HttpClient.PostAsJsonAsync 호출 시 "Transfer-Encoding: chunked" 대신 "Content-Length" 헤더 처리
13521정성태1/11/20242154닷넷: 2199. C# - 한국투자증권 KIS Developers OpenAPI의 WebSocket Ping, Pong 처리
13520정성태1/10/20241892오류 유형: 888. C# - Unable to resolve service for type 'Microsoft.Extensions.ObjectPool.ObjectPool`....'
13519정성태1/10/20241984닷넷: 2198. C# - Reflection을 이용한 ClientWebSocket의 Ping 호출파일 다운로드1
13518정성태1/9/20242254닷넷: 2197. C# - ClientWebSocket의 Ping, Pong 처리
13517정성태1/8/20242100스크립트: 63. Python - 공개 패키지를 이용한 위성 이미지 생성 (pystac_client, odc.stac)
13516정성태1/7/20242194닷넷: 2196. IIS - AppPool의 "Disable Overlapped Recycle" 옵션의 부작용
13515정성태1/6/20242472닷넷: 2195. async 메서드 내에서 C# 7의 discard 구문 활용 사례 [1]
13514정성태1/5/20242115개발 환경 구성: 702. IIS - AppPool의 "Disable Overlapped Recycle" 옵션
13513정성태1/5/20242038닷넷: 2194. C# - WebActivatorEx / System.Web의 PreApplicationStartMethod 특성
13512정성태1/4/20241999개발 환경 구성: 701. IIS - w3wp.exe 프로세스의 ASP.NET 런타임을 항상 Warmup 모드로 유지하는 preload Enabled 설정
13511정성태1/4/20242020닷넷: 2193. C# - ASP.NET Web Application + OpenAPI(Swashbuckle) 스펙 제공
13510정성태1/3/20241955닷넷: 2192. C# - 특정 실행 파일이 있는지 확인하는 방법 (Linux)
13509정성태1/3/20241976오류 유형: 887. .NET Core 2 이하의 프로젝트에서 System.Runtime.CompilerServices.Unsafe doesn't support netcoreapp2.0.
13508정성태1/3/20242029오류 유형: 886. ORA-28000: the account is locked
13507정성태1/2/20242717닷넷: 2191. C# - IPGlobalProperties를 이용해 netstat처럼 사용 중인 Socket 목록 구하는 방법파일 다운로드1
13506정성태12/29/20232210닷넷: 2190. C# - 닷넷 코어/5+에서 달라지는 System.Text.Encoding 지원
1  2  3  [4]  5  6  7  8  9  10  11  12  13  14  15  ...