ELF

EEEFFEE·2023년 11월 30일
0

Linux 환경

목록 보기
6/9

23.11.30 최초 작성

1. ELF (Excutable and Linkable Format)

  • 리눅스 환경에서 구동되는 프로그램의 바이너리 파일의 형식

1.1 ELF 코드 구조

  • section : 컴파일러에 의해 링크된 데이터의 모음
  • segment : section의 모음으로 메모리에 로드되는 단위
  • Elf header : ELF 파일의 메타데이터 저장 (/usr/include/elf.h에서 구조 확인 가능)

typedef struct {
  unsigned char	e_ident[EI_NIDENT];  /* Magic number and other info */
  Elf64_Half	e_type;         /* Object file type */
  Elf64_Half	e_machine;      /* Architecture */
  Elf64_Word	e_version;      /* Object file version */
  Elf64_Addr	e_entry;        /* Entry point virtual address */
  Elf64_Off	  e_phoff;        /* Program header table file offset */
  Elf64_Off	  e_shoff;        /* Section header table file offset */
  Elf64_Word	e_flags;        /* Processor-specific flags */
  Elf64_Half	e_ehsize;       /* ELF header size in bytes */
  Elf64_Half	e_phentsize;    /* Program header table entry size */
  Elf64_Half	e_phnum;        /* Program header table entry count */
  Elf64_Half	e_shentsize;    /* Section header table entry size */
  Elf64_Half	e_shnum;        /* Section header table entry count */
  Elf64_Half	e_shstrndx;     /* Section header string table index */
} Elf64_Ehdr;

  • Segment(Program) header table : segment들에 대한 정보와 그 segment들을 메모리에 어떻게 로드해야 하는지에 대한 정보

typedef struct elf64_phdr {
  Elf64_Word p_type;      /* Segment type */
  Elf64_Word p_flags;     /* Segment flags */
  Elf64_Off p_offset;     /* Segment file offset */
  Elf64_Addr p_vaddr;     /* Segment virtual address */
  Elf64_Addr p_paddr;     /* Segment physical address */
  Elf64_Xword p_filesz;   /* Segment size in file */
  Elf64_Xword p_memsz;    /* Segment size in memory */
  Elf64_Xword p_align;    /* Segment alignment, file & memory */
} Elf64_Phdr;

  • Section header table : 각 section & segment 영역의 범위에 대한 정보

typedef struct {
  Elf64_Word	sh_name;        /* Section name (string tbl index) */
  Elf64_Word	sh_type;        /* Section type */
  Elf64_Xword	sh_flags;       /* Section flags */
  Elf64_Addr	sh_addr;        /* Section virtual addr at execution */
  Elf64_Off	sh_offset;        /* Section file offset */
  Elf64_Xword	sh_size;        /* Section size in bytes */
  Elf64_Word	sh_link;        /* Link to another section */
  Elf64_Word	sh_info;        /* Additional section information */
  Elf64_Xword	sh_addralign;   /* Section alignment */
  Elf64_Xword	sh_entsize;     /* Entry size if section holds table */
} Elf64_Shdr;

  • .text : 메인 함수 코드
  • .rodata : 읽기 전용 데이터 (상수)
  • .data : 초기화된 변수의 기본값이 저장
  • .bss : 초기화되지 않은 변수들을 위해 예약된 공간
  • .symtab : 심볼명을 함수나 변수와 같이 코드나 데이터와 연관시키는 심볼 테이블저장
  • .rel.text : 현재 파일에 정의되어있지 않고 link시 참조되는 함수에 대한 정보
  • .rel.data : 현재 파일에 정의되어있지 않고 link시 참조되는 전역변수에 대한 정보
  • .debug : -g컴파일 옵션 사용 시 디버깅 정보가 저장되는 영역

0개의 댓글

관련 채용 정보