ELF파일 포멧에 관한 질문입니다.

dgsquare의 이미지

이궁-_-' 글을 써놓고 미리보기만하고 창을 닫아버렸습니다... 다쉬 적어야져 ^^-
OS를 공부겸해서 만들고 있습니다. kernel을 보통 binary로 만드는데, 저는 bochs에서 gdb로 디버깅을 하기위해 ELF파일포멧으로 만들었습니다. 실행이나 디버깅은 별 문제가 없었는데, 엉뚱한데에서 문제가 발생했습니다. 정적변수와 관련된 문제인데요...
공유라이브러리 사용을 위해서 리눅스 커널의 _ctype 변수를 사용하려하는데 이것이 전역변수로 할당과 동시에 초기화 되어있습니다. 이 변수를 사용하려하는데, 엉뚱한 곳을 access하게 됩니다.
일단 원인은 알고 있습니다. C에서 정적변수를 할당과 동시에 초기화 하면, 나중에 실행파일(ELF)로 로드시 .data영역에 들어간다고 알고 있습니다. 그런데 실제 실행파일을 xxd로 추출해보면, .data의 주소위치에 내용이 담긴것이 아닌, .text의 바로 뒤에 붙어서 나오는군요. 즉 objdump로 해서 본 실제 변수의 offset과 실제 file에 들어가 있는 변수의 offset이 다르다는 것 입니다. 아마도 파일의 공간낭비를 막기위해서 이렇게 하는 것 같습니다. 그냥 공간이 조금 더 들더라도, 실행시 offset과 파일에 들어있는 offset을 일치시킬수 없는지 알고 싶습니다.
ld의 옵션을 찾아보았지만, 내공부족으로 제대로 해석하기도 힘드네요. 아니면 ELF파일 포맷에서 지원안하는 건지도 걱정이구요.
뭐, 선언만하고 나중에 할당하는 것은 주소만 할당되기때문에 이상없이 동작합니다. 정 할수없으면 이렇게 할 생각입니다.
그럼 좋은 답변 부탁드립니다. :o

override의 이미지

ld 의 man 내용 중 발췌해봤습니다.

이걸로는 해결 안되나요?

Quote:

--section-start sectionname=org
Locate a section in the output file at the absolute address given
by org. You may use this option as many times as necessary to
locate multiple sections in the command line. org must be a single
hexadecimal integer; for compatibility with other linkers, you may
omit the leading 0x usually associated with hexadecimal values.
Note: there should be no white space between sectionname, the
equals sign (旬=旬), and org.

-Tbss org
-Tdata org
-Ttext org
Same as --section-start, with ".bss", ".data" or ".text" as the
sectionname.

dgsquare의 이미지

답변감사합니다.
이런 방법으로도 가능하군요. 단지 이럴경우 .data session의 주소를 강제 지정해야 하는데, 이것이 좀 불편하군요.
그냥 최적화만 안 시키는 방법이 없는지 궁금하네여.
Man페이지를 좀더 찾아봐야겠습니다.

dgsquare의 이미지

다른 방법으로 해결했습니다.
--omagic로 아예 paging alignement를 실행 못하게 하니, offset자체가 소스파일 바로 뒤에 붙네여. 그 뒤 base address만 조정하니, 전역변수의 off이 output의 output파일의 offset과 일치되는 군요 :shock:
너무 한쪽으로만 생각 했던것 같습니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.