cosmopolitan/libc/elf/struct/shdr.h
Justine Tunney dd53f31147
Introduce post-linker that converts ELF to PE
If you build a static ELF executable in `ld -q` mode (which leaves rela
sections inside the binary) then you can run it through the elf2pe.com
program afterwards, which will turn it into a PE executable. We have a
new trick for defining WIN32 DLL imports in C without any assembly code.
This also achieves the optimally tiny and perfect PE binary structure.

We need this because it isn't possible to have a GNU ld linker script
generate a PE file where the virtual pointer and the file pointer can
drift apart. This post-linker can do that. One cool benefit is we can
now use a smaller 512-byte alignment in the file, and an even bigger
64kb alignment for the segment virtual addresses, and the executable
ends up being smaller.

Another program introduced by this change is pecheck.com which can do
extensive linting of PE static executables to help explain why Windows
won't load it.
2023-08-09 18:46:06 -07:00

52 lines
1.3 KiB
C

#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_SHDR_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_SHDR_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
/**
* Section header.
* @see https://docs.oracle.com/cd/E19683-01/816-1386/chapter6-94076/index.html
*/
typedef struct Elf64_Shdr {
Elf64_Word sh_name;
Elf64_Word sh_type; /* SHT_{PROGBITS,NOBITS,STRTAB,SYMTAB,RELA,...} */
Elf64_Xword sh_flags; /* SHF_{WRITE,ALLOC,EXECINSTR,TLS,MERGE,STRINGS,,...} */
Elf64_Addr sh_addr;
Elf64_Off sh_offset;
Elf64_Xword sh_size;
/*
* Index of linked section header.
*
* If `sh_type` is `SHT_RELA` then `sh_link` holds the section header
* index of the associated symbol table.
*
* If `sh_type` is `SHT_SYMTAB` then `sh_link` holds the section
* header index of the associated string table.
*/
Elf64_Word sh_link;
/*
* If `sh_type` is `SHT_RELA` then `sh_info` contains the index of the
* section to which relocations apply.
*
* If `sh_type` is `SHT_SYMTAB` or `SHT_DYNSYM` then `sh_info`
* contains an index that's one greater than symbol table index of
* last `STB_LOCAL` symbol.
*/
Elf64_Word sh_info;
Elf64_Xword sh_addralign;
Elf64_Xword sh_entsize;
} Elf64_Shdr;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_SHDR_H_ */