load time relocation 궁금한 점이 있습니다.

porawon의 이미지

http://eli.thegreenplace.net/2011/08/25/load-time-relocation-of-shared-libraries/

이 블로그 글에 따르면 load time relocation을 사용할 시

실행 파일에서 공유 라이브러리의 전역 변수를 참조하려면 자신의 데이터 섹션에 값을 카피해서 그것을 참조한다고 하는데요.

그 이유가 실행파일은 재배치(relocation)가 불가능하기 때문이라고 하네요.
Recall that the program/executable is not relocatable, and thus its data addresses have to bound at link time. Therefore, the linker has to create a copy of the variable in the program’s address space, and the dynamic loader will use that as the relocation address.

- 링크 글의 Extra credit #2: Referencing shared library data from the executable 중에서 -

제가 이해하기로는 링크할 시에 모든 재배치가 끝나야 하는데 공유 라이브러리의 경우에는 로드 시에 주소가 결정되므로 링크 시에 주소가 결정되는 자신의 데이터 섹션으로 값을 카피해서 그것을 참조한다는 것 같습니다.

그럼 실행파일에서 공유 라이브러리의 함수를 콜할 때는 어떻게 되는 건가요?

직접 해보니까 공유 라이브러리는 load-time relocation이 되어서 직접 주소값으로 jump하는데 실행 파일에서는 plt를 이용해서 콜만 하더라구요.

plt는 pic 옵션이 주어졌을 때 사용하는 것으로 알고 있는데 꼭 그런 것만은 아닌가요?

--------------------------------------------------------------------------------------

다른 분께서 저자에게 댓글로 같은 질문을 해서 저자가 답한 것이 있긴 한데요.

Addresses of functions in shared libraries are already bound at link-time in the executable (unless these libraries are loaded with dlopen), since the linker knows where in the process’s address space each library will be loaded. Code in the shared library is relocated (or PIC), so there’s no problem with internal code references there. Accessing the same functions from the executable can just use relative calls, no relocation required. Data can’t be accessed in this way (on x86), hence this trick.

이 말에 따르면 또 링크 시에 주소가 결정된다고 하는 것 같아서 헷갈리네요.

위 링크 글에는 로드 시에 공유 라이브러리의 주소가 결정되고 로드 될 때마다 address space layout randomization(ASLR)에 의해서 로드되는 주소가 바뀐다고도 나와있어서요.(한 사람이 서로 모순되는 말을 하고 있는 것 같네요 ㅜㅜ)

mirheekl의 이미지

그냥 실행파일 입장에선 펑션콜 위치가 정해져 있고 라이브러리 입장에선 매번 실주소가 바뀐단 얘기로 보입니다. 직접 펑션콜 과정을 확인해보셨다는 부분(테이블 처리)도 여기에 부합하고요.

--

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.