실행 파일은 런타임에 공유 객체를 어디에서 찾습니까? 링크 / 컴파일시 공유 객체 포함을 정의하는

링크 / 컴파일시 공유 객체 포함을 정의하는 방법을 이해합니다. 그러나 여전히 실행 파일에서 실행 파일이 공유 객체 ( *.so라이브러리)를 어떻게 찾는 지 궁금합니다 .

예를 들어, 내 앱 a.outlib.so라이브러리에 정의 된 함수를 호출합니다 . 컴파일 후 lib.so내의 새 디렉토리 로 이동 합니다 $HOME.

a.out거기서 찾아 보라고 어떻게 할 수 있습니까?



답변

공유 라이브러리 HOWTO는 관련된 메커니즘의 대부분을 설명하고, 동적 로더 설명서는 자세히로 들어갑니다. 각 유닉스 변형에는 고유 한 방식이 있지만 대부분 동일한 실행 형식 ( ELF )을 사용하며 유사한 동적 링커 (Solaris에서 파생)가 있습니다. 아래에서는 Linux에 중점을 둔 일반적인 동작을 요약합니다. 전체 스토리는 시스템 설명서를 확인하십시오.

간단히 말해, .so링커는 동적 라이브러리 ( 파일)를 찾을 때 다음을 시도합니다.

  • LD_LIBRARY_PATH환경 변수에 나열된 디렉토리 ( DYLD_LIBRARY_PATHOSX의 경우);
  • 실행 파일의 rpath에 나열된 디렉토리 ;
  • 시스템 검색 경로의 디렉토리 (최소 Linux의 경우)는 /etc/ld.so.confplus /lib및 의 항목으로 구성됩니다 /usr/lib.

rpath는 실행 파일에 저장됩니다 ( DT_RPATH또는 DT_RUNPATH동적 속성). 여기에는 $ORIGIN실행 파일의 위치에 상대적인 경로를 나타 내기 위해 시작하는 절대 경로 또는 경로가 포함될 수 있습니다 (예 : 실행 파일이 /opt/myapp/bin있고 해당 경로가 r $ORIGIN/../lib:$ORIGIN/../plugins인 경우 동적 링커가 /opt/myapp/lib와 를 찾습니다 /opt/myapp/plugins). rpath는 일반적으로 -rpath옵션을 사용하여 실행 파일을 컴파일 할 때 결정 ld되지만 나중에로 변경할 수 있습니다 chrpath.

시나리오에서는 응용 프로그램의 개발자 또는 패키저이라면, 설명하고 그것이 설치 될 수 있도록하고자 …/bin, …/lib구조, 다음으로 링크 -rpath='$ORIGIN/../lib'. 시스템에 사전 빌드 된 바이너리를 설치하는 경우 라이브러리를 검색 경로의 디렉토리 ( /usr/local/lib시스템 관리자 인 경우 추가하는 디렉토리 $LD_LIBRARY_PATH)에 놓거나을 시도하십시오 chrpath.


답변

Linux에서이 동작은 ld(1)매뉴얼 페이지에 명시되어 있습니다

       The linker uses the following search paths to locate required
       shared libraries:

       1.  Any directories specified by -rpath-link options.

       2.  Any directories specified by -rpath options.  The difference
           between -rpath and -rpath-link is that directories specified by
           -rpath options are included in the executable and used at
           runtime, whereas the -rpath-link option is only effective at
           link time. Searching -rpath in this way is only supported by
           native linkers and cross linkers which have been configured
           with the --with-sysroot option.

       3.  On an ELF system, for native linkers, if the -rpath and
           -rpath-link options were not used, search the contents of the
           environment variable "LD_RUN_PATH".

       4.  On SunOS, if the -rpath option was not used, search any
           directories specified using -L options.

       5.  For a native linker, the search the contents of the environment
           variable "LD_LIBRARY_PATH".

       6.  For a native ELF linker, the directories in "DT_RUNPATH" or
           "DT_RPATH" of a shared library are searched for shared
           libraries needed by it. The "DT_RPATH" entries are ignored if
           "DT_RUNPATH" entries exist.

       7.  The default directories, normally /lib and /usr/lib.

       8.  For a native linker on an ELF system, if the file
           /etc/ld.so.conf exists, the list of directories found in that
           file.

       If the required shared library is not found, the linker will issue
       a warning and continue with the link.

답변

나는 대답이 여기 있다고 확신합니다 ldconfig.

ldconfig는 명령 줄에 지정된 디렉토리, /etc/ld.so.conf 파일 및 신뢰할 수있는 디렉토리 (/ lib 및 / usr / lib)에있는 가장 최근의 공유 라이브러리에 필요한 링크와 캐시를 만듭니다. 캐시는 런타임 링커 ld.so 또는 ld-linux.so에서 사용합니다. ldconfig는 링크를 업데이트해야하는 버전을 결정할 때 발견되는 라이브러리의 헤더 및 파일 이름을 확인합니다.

http://linux.die.net/man/8/ldconfig


답변

응용 프로그램을 실행하기 위해 파일 /proc/1234/maps에는 실제로 동적으로 연결된 모든 라이브러리가 포함됩니다.

1234실행중인 실행 파일의 pid는 어디에 있습니까 ?

Gilles가 지적한대로 Linux는 LD_LIBRARY_PATH 및 기타 변수를 따릅니다.


답변