안녕하세요…
컴파일 중… -Wl,-as-needed 옵션에 대한 의문이 생겨서 찾아보니…
당췌 이해가 안되는데… 좀 쉽게 설명 좀 해주실 분 없으신가요?
안녕하세요…
컴파일 중… -Wl,-as-needed 옵션에 대한 의문이 생겨서 찾아보니…
당췌 이해가 안되는데… 좀 쉽게 설명 좀 해주실 분 없으신가요?
이해가 안되는 이유는 gcc를 단순한 컴파일러로 생각하기 때문입니다.
위에 언급된 옵션은 링크옵션이고 ld 관련 문서를 보면 잘 설명이 되어 있습니다.
내용은 대략 이렇습니다.
ELF 파일 포멧에 다이나믹 라이브러리 의존성 태그가 존재합니다.
$ readelf -a /bin/ls | grep NEED
링크할때 혹시나하고 다이나믹 라이브러리들을 옵션으로 마구마구 추가하면
실제로 참조하지 않으면서 그 다이나믹 라이브러리들은 ELF Dynamic section에 기록이 되고
ldd(다이나믹 라이브러리 의존성 표시) 같은 프로그램들이 삽질을 하게 됩니다.
(–no-as-need, default)
–as-need 옵션을 사용하면 참조한 라이브러리만 ELF Dynamic section에 기록합니다.
간단한 테스트
[code:iqyxumbc]
$ cat > hello.c
#include <stdio.h>
int main(void)
{
printf("hello\n");
return 0;
}
CTRL + D
$ gcc -Wall hello.c -o hello
$ readelf -a hello | grep NEED
[ 9] .gnu.version_r VERNEED 08048278 000278 000020 00 A 7 1 4
0x00000001 (NEEDED) Shared library: [libc.so.6]
0x6ffffffe (VERNEED) 0x8048278
0x6fffffff (VERNEEDNUM) 1
$ ldd hello
linux-gate.so.1 => (0xb7855000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb76e3000)
/lib/ld-linux.so.2 (0xb7856000)
$ gcc -Wall hello.c -o hello -lm
$ readelf -a hello | grep NEED
[ 9] .gnu.version_r VERNEED 080482ac 0002ac 000020 00 A 7 1 4
0x00000001 (NEEDED) Shared library: [libm.so.6]
0x00000001 (NEEDED) Shared library: [libc.so.6]
0x6ffffffe (VERNEED) 0x80482ac
0x6fffffff (VERNEEDNUM)
$ ldd hello
linux-gate.so.1 => (0xb77e6000)
libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xb77a8000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb764e000)
/lib/ld-linux.so.2 (0xb77e7000)
$ gcc -Wall hello.c -o hello -Wl,–as-needed,-lm
$ readelf -a hello | grep NEED
[ 9] .gnu.version_r VERNEED 08048278 000278 000020 00 A 7 1 4
0x00000001 (NEEDED) Shared library: [libc.so.6]
0x6ffffffe (VERNEED) 0x8048278
0x6fffffff (VERNEEDNUM) 1
$ ldd hello
linux-gate.so.1 => (0xb78ad000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb773b000)
/lib/ld-linux.so.2 (0xb78ae000)
[/code:iqyxumbc]
도와 주신 분들 감사 감사… 이해가 조금씩 되네요…