developer tip

ELF 파일 형식에서 섹션과 세그먼트의 차이점은 무엇입니까

copycodes 2020. 12. 5. 10:00
반응형

ELF 파일 형식에서 섹션과 세그먼트의 차이점은 무엇입니까


위키 실행 가능 및 링크 가능 형식에서 :

세그먼트에는 파일의 런타임 실행에 필요한 정보가 포함되어 있으며 섹션에는 연결 및 재배치에 대한 중요한 데이터가 포함되어 있습니다. 전체 파일의 모든 바이트는 최대 하나의 섹션에서 소유 할 수 있으며 어떤 섹션에서도 소유하지 않은 고아 바이트가있을 수 있습니다.

그러나 섹션과 세그먼트의 차이점은 무엇입니까? 실행 가능한 ELF 파일에서 세그먼트에 하나 이상의 섹션이 포함되어 있습니까?


그러나 섹션과 세그먼트의 차이점은 무엇입니까?

정확히 인용 한 내용 : 세그먼트에는 런타임에 필요한 정보가 포함되고 섹션에는 링크 중에 필요한 정보가 포함됩니다.

세그먼트에 하나 이상의 섹션이 포함되어 있습니까?

세그먼트는 0 개 이상의 섹션을 포함 할 수 있습니다. 예:

readelf -l /bin/date

Elf file type is EXEC (Executable file)
Entry point 0x402000
There are 9 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x00000000000001f8 0x00000000000001f8  R E    8
  INTERP         0x0000000000000238 0x0000000000400238 0x0000000000400238
                 0x000000000000001c 0x000000000000001c  R      1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x000000000000d5ac 0x000000000000d5ac  R E    200000
  LOAD           0x000000000000de10 0x000000000060de10 0x000000000060de10
                 0x0000000000000440 0x0000000000000610  RW     200000
  DYNAMIC        0x000000000000de38 0x000000000060de38 0x000000000060de38
                 0x00000000000001a0 0x00000000000001a0  RW     8
  NOTE           0x0000000000000254 0x0000000000400254 0x0000000000400254
                 0x0000000000000044 0x0000000000000044  R      4
  GNU_EH_FRAME   0x000000000000c700 0x000000000040c700 0x000000000040c700
                 0x00000000000002a4 0x00000000000002a4  R      4
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     8
  GNU_RELRO      0x000000000000de10 0x000000000060de10 0x000000000060de10
                 0x00000000000001f0 0x00000000000001f0  R      1

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp 
   02     .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 
   03     .ctors .dtors .jcr .dynamic .got .got.plt .data .bss 
   04     .dynamic 
   05     .note.ABI-tag .note.gnu.build-id 
   06     .eh_frame_hdr 
   07     
   08     .ctors .dtors .jcr .dynamic .got 

여기서 PHDR세그먼트에는 0 개의 섹션이 포함되고 INTERP세그먼트에는 .interp섹션이 포함 되며 첫 번째 LOAD세그먼트에는 전체 섹션이 포함됩니다.

멋진 일러스트 와 함께 추가 읽기 .


섹션에는 링커에 대한 정적, OS에 대한 세그먼트 동적 데이터가 포함됩니다.

인용문은 정확하지만 실제로 차이점을 이해하려면 섹션 헤더 및 프로그램 헤더 (세그먼트) 항목의 필드와 링커 (섹션) 및 운영 체제 (세그먼트)에서 어떻게 사용되는지 이해해야합니다. .

특히 중요한 정보는 다음과 같습니다 (길이 외에).

  • 섹션 : 섹션이 다음 중 하나인지 링커에 알립니다.

    • 원시 데이터는 메모리 등으로로드되는 .data, .text등등
    • or formatted metadata about other sections, that will be used by the linker, but disappear at runtime e.g. .symtab, .srttab, .rela.text
  • segment: tells the operating system:

    • where should a segment be loaded into virtual memory
    • what permissions the segments have (read, write, execute). Remember that this can be efficiently enforced by the processor: How does x86 paging work?

I have written a tutorial that covers that in more detail at: http://www.cirosantilli.com/elf-hello-world/

Does a segment contain one or more sections?

Yes, and it is the linker that puts sections into segments.

In Binutils, how sections are put into segments by ld is determined by a text file called a linker script. Docs: https://sourceware.org/binutils/docs/ld/Scripts.html

You can get the default one with ld --verbose, and set a custom one with -T.

For example, my default Ubuntu 17.04 linker script contains:

  .text           :                                                                                                                                                             
  {                                                                                                                                                                             
    *(.text.unlikely .text.*_unlikely .text.unlikely.*)                                                                                                                         
    *(.text.exit .text.exit.*)                                                                                                                                                  
    *(.text.startup .text.startup.*)                                                                                                                                            
    *(.text.hot .text.hot.*)                                                                                                                                                    
    *(.text .stub .text.* .gnu.linkonce.t.*)                                                                                                                                                                                                                                                                                               
  } 

which tells the linker to put sections named .text.unlikely, .text.*_unlikely, .text.exit, etc. in the .text segment.

OS development is a case where custom scripts are useful, minimal example: https://github.com/cirosantilli/x86-bare-metal-examples/blob/d217b180be4220a0b4a453f31275d38e697a99e0/linker.ld

Once the executable is linked, it is only possible to know which section went to which segment if the linker stores the optional section header in the executable: Where is the "Section to segment mapping" stored in ELF files?

참고URL : https://stackoverflow.com/questions/14361248/whats-the-difference-of-section-and-segment-in-elf-file-format

반응형