ELF 파일의 섹션 사이에 불필요한 간격을 생성하는 GNU 링커

ELF 파일의 섹션 사이에 불필요한 간격을 생성하는 GNU 링커

내 의도는 텍스트 섹션을 메모리의 특정 위치(0x00100000)에 배치하는 것입니다.

SECTIONS
{
    . = 0x00100000;
    .text :
    {   
        *(.text*)
    }
}    

비록 링커이지만하다이렇게 하세요(0x01000000 Addr필드 참고).

$ readelf -S file.elf 
There are 12 section headers, starting at offset 0x104edc:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00100000 100000 000e66 00  AX  0   0  4
  [ 2] .eh_frame         PROGBITS        00100e68 100e68 000628 00   A  0   0  4
...

.text또한 ELF 파일의 섹션 앞에 ~1MB의 0을 배치합니다 ( .text섹션의 오프셋은 1MB입니다). 다른 방식으로 표시:

$ hexdump -C file.elf
00000000  7f 45 4c 46 01 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  02 00 03 00 01 00 00 00  0c 00 10 00 34 00 00 00  |............4...|
00000020  dc 4e 10 00 00 00 00 00  34 00 20 00 02 00 28 00  |.N......4. ...(.|
00000030  0c 00 0b 00 01 00 00 00  00 00 00 00 00 00 00 00  |................|
00000040  00 00 00 00 90 14 10 00  96 04 4f 00 07 00 00 00  |..........O.....|
00000050  00 00 20 00 51 e5 74 64  00 00 00 00 00 00 00 00  |.. .Q.td........|
00000060  00 00 00 00 00 00 00 00  00 00 00 00 07 00 00 00  |................|
00000070  10 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000080  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00100000  02 b0 ad 1b 03 00 00 00  fb 4f 52 e4 8b 25 90 04  |.........OR..%..|
00100010  4f 00 50 53 e8 88 00 00  00 fa f4 eb fc 55 89 e5  |O.PS.........U..|
00100020  83 ec 10 c7 45 f8 00 80  0b 00 c7 45 fc 00 00 00  |....E......E....|
00100030  00 eb 24 8b 45 fc 8d 14  00 8b 45 f8 01 d0 8b 4d  |..$.E.....E....M|

이를 어떻게 방지할 수 있나요? 위치 카운터("점" 표기법) 구문을 부적절하게 사용하고 있습니까?

답변1

링커에게 elf_i386을 에뮬레이트하라고 지시하면 내가 찾고 있던 결과가 나왔지만 이유는 모르겠습니다. 즉, 다음을 사용하여 링커를 호출합니다.

$ ld -melf_i386 [...]

포함된 파일과 포함되지 않은 파일은 -melf_i386대부분 유사한 것으로 보입니다.

with.elf:    ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped, with debug_info
without.elf: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped, with debug_info

크기가 크게 다르다는 점을 제외하면:

$ ls -l *.elf
-rwxr-xr-x 1 user user   10948 May 24 11:56 with.elf
-rwxr-xr-x 1 user user 1055428 May 24 11:56 without.elf

내가 알 수 있는 한, 출력 파일은 그렇지 않습니다.정확히똑같다:

$ readelf -S with.elf
There are 12 section headers, starting at offset 0x28e4:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00100000 001000 000205 00  AX  0   0  4
  [ 2] .eh_frame         PROGBITS        00100208 001208 0000b8 00   A  0   0  4
  [ 3] .bss              NOBITS          001002c0 0012c0 3ef000 00  WA  0   0  4
  [ 4] .debug_info       PROGBITS        00000000 0012c0 0007bf 00      0   0  1
  [ 5] .debug_abbrev     PROGBITS        00000000 001a7f 0002c9 00      0   0  1
  [ 6] .debug_aranges    PROGBITS        00000000 001d48 000060 00      0   0  1
  [ 7] .debug_line       PROGBITS        00000000 001da8 00023c 00      0   0  1
  [ 8] .debug_str        PROGBITS        00000000 001fe4 0004bd 01  MS  0   0  1
  [ 9] .symtab           SYMTAB          00000000 0024a4 000280 10     10  22  4
  [10] .strtab           STRTAB          00000000 002724 00014e 00      0   0  1
  [11] .shstrtab         STRTAB          00000000 002872 000070 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  p (processor specific)

"오프셋" 필드가 약간 다릅니다.

$ readelf -S without.elf
There are 12 section headers, starting at offset 0x1018e4:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00100000 100000 000205 00  AX  0   0  4
  [ 2] .eh_frame         PROGBITS        00100208 100208 0000b8 00   A  0   0  4
  [ 3] .bss              NOBITS          001002c0 1002c0 3ef000 00  WA  0   0  4
  [ 4] .debug_info       PROGBITS        00000000 1002c0 0007bf 00      0   0  1
  [ 5] .debug_abbrev     PROGBITS        00000000 100a7f 0002c9 00      0   0  1
  [ 6] .debug_aranges    PROGBITS        00000000 100d48 000060 00      0   0  1
  [ 7] .debug_line       PROGBITS        00000000 100da8 00023c 00      0   0  1
  [ 8] .debug_str        PROGBITS        00000000 100fe4 0004bd 01  MS  0   0  1
  [ 9] .symtab           SYMTAB          00000000 1014a4 000280 10     10  22  4
  [10] .strtab           STRTAB          00000000 101724 00014e 00      0   0  1
  [11] .shstrtab         STRTAB          00000000 101872 000070 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  p (processor specific)

관련 정보