Dynamic linker (LD) 정리

steadycode·2023년 1월 31일
0

C, C++, 심지어 python 에서도 shared library 를 사용함. 특히 python 의 경우 여러 dependency 가 여러 디렉토리, i.e, /usr, /usr/local ~/.local 에 분산되어 있기 때문에 사이즈가 큰 코드일수록 shared library 관리하기가 어려움. 본 포스팅은 shared library 를 관리하고 runtime 에 load 하는 dynamic linker 에 대해 다룸.


Dynamic linker (LD) 의 정의

Dynamic linker (LD) 란, OS 의 일부 요소 중 하나로, shared library 의 symbol 을 resolve (look-up) 하며 프로그램의 runtime 에 link 하는 역할을 함.

여기서 말하는 runtime 이란, 프로그램이 실행되고, LD 가 프로그램이 요구하는 shared library 를 로드할 때의 시간을 지칭함.

LD 가 shared library 를 로드하고, symbol 을 look-up 하는 것을 dynamic linking 이라고 지칭하며, 이러한 dynamic linking 은 recomplie 을 줄이고, 프로그램의 크기, 유지, 그리고 공유성을 가지고 있다는 장점이 있기 때문에 자주 사용됨.


Resolving the symbols

LD 가 shared library 의 symbol 을 look-up 하기 위해서는 shared library (.so or .dll) 의 위치를 특정해야 함. 위치를 특정하고 찾는 순서는 다음과 같으며 다음의 링크를 참조하면 알 수 있음.

  • Cache: The dynamic linker maintains a cache of the shared libraries that have been loaded and the symbols they contain. If a symbol is found in the cache, the dynamic linker will use the cached information to resolve the symbol.

  • LD_LIBRARY_PATH: If the symbol is not found in the cache, the dynamic linker will search for the shared library in the directories specified in the LD_LIBRARY_PATH environment variable, in the order they are listed.

  • Trusted Directories: If the symbol is not found in the LD_LIBRARY_PATH, the dynamic linker will search for the shared library in a set of trusted directories, such as /usr/lib or /lib.

  • Global Configuration: If the symbol is still not found, the dynamic linker will check the global configuration file, usually located at /etc/ld.so.conf, which lists the directories where shared libraries are located.

  • Default Directories: If the symbol is still not found, the dynamic linker will search for the shared library in a set of default directories, such as /usr/local/lib.

cache 는 종종 out-dated information 을 가지고 있기 때문에 shared library 를 설치하거나, 혹은 LD_LIBRARY_PATH 를 설정했으면 sudo ldconfig 명령어를 통해 cache 를 refresh 해줘야 함.


profile
steadycode

0개의 댓글