Initial import

This commit is contained in:
Justine Tunney 2020-06-15 07:18:57 -07:00
commit c91b3c5006
14915 changed files with 590219 additions and 0 deletions

520
libc/elf/def.h Normal file
View file

@ -0,0 +1,520 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_DEF_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_DEF_H_
#if 0
/**
* @fileoverview Executable and Linkable Format Definitions.
*/
#endif
#define EI_MAG0 0
#define EI_MAG1 1
#define EI_MAG2 2
#define EI_MAG3 3
#define ELFMAG "\177ELF"
#define ELFMAG0 0x7f
#define ELFMAG1 'E'
#define ELFMAG2 'L'
#define ELFMAG3 'F'
#define SELFMAG 4
#define EI_CLASS 4
#define ELFCLASSNONE 0
#define ELFCLASS32 1
#define ELFCLASS64 2
#define ELFCLASSNUM 3
#define EI_DATA 5
#define ELFDATANONE 0
#define ELFDATA2LSB 1
#define ELFDATA2MSB 2
#define ELFDATANUM 3
#define EI_VERSION 6
#define EI_OSABI 7
#define ELFOSABI_NONE 0
#define ELFOSABI_SYSV 0
#define ELFOSABI_HPUX 1
#define ELFOSABI_NETBSD 2
#define ELFOSABI_LINUX 3
#define ELFOSABI_GNU 3
#define ELFOSABI_SOLARIS 6
#define ELFOSABI_AIX 7
#define ELFOSABI_IRIX 8
#define ELFOSABI_FREEBSD 9
#define ELFOSABI_TRU64 10
#define ELFOSABI_MODESTO 11
#define ELFOSABI_OPENBSD 12
#define ELFOSABI_ARM 97
#define ELFOSABI_STANDALONE 255
#define EI_ABIVERSION 8
#define EI_PAD 9
#define ET_NONE 0
#define ET_REL 1
#define ET_EXEC 2
#define ET_DYN 3
#define ET_CORE 4
#define ET_NUM 5
#define ET_LOOS 0xfe00
#define ET_HIOS 0xfeff
#define ET_LOPROC 0xff00
#define ET_HIPROC 0xffff
#define EM_NONE 0
#define EM_M32 1
#define EM_386 3
#define EM_S390 22
#define EM_ARM 40
#define EM_NEXGEN32E 62
#define EM_X86_64 EM_NEXGEN32E
#define EM_IA32E EM_NEXGEN32E
#define EM_AMD64 EM_NEXGEN32E
#define EM_PDP11 65
#define EM_CRAYNV2 172
#define EM_L10M 180
#define EM_K10M 181
#define EM_AARCH64 183
#define EM_CUDA 190
#define EM_Z80 220
#define EM_RISCV 243
#define EM_BPF 247
#define GRP_COMDAT 0x1
#define STN_UNDEF 0
#define EV_NONE 0
#define EV_CURRENT 1
#define EV_NUM 2
#define SYMINFO_NONE 0
#define SYMINFO_CURRENT 1
#define SYMINFO_NUM 2
#define SYMINFO_BT_SELF 0xffff
#define SYMINFO_BT_PARENT 0xfffe
#define SYMINFO_BT_LOWRESERVE 0xff00
#define SYMINFO_FLG_DIRECT 0x0001
#define SYMINFO_FLG_PASSTHRU 0x0002
#define SYMINFO_FLG_COPY 0x0004
#define SYMINFO_FLG_LAZYLOAD 0x0008
#define PT_NULL 0
#define PT_LOAD 1
#define PT_DYNAMIC 2
#define PT_INTERP 3
#define PT_NOTE 4
#define PT_SHLIB 5
#define PT_PHDR 6
#define PT_TLS 7
#define PT_NUM 8
#define PT_LOOS 0x60000000
#define PT_GNU_EH_FRAME 0x6474e550
#define PT_GNU_STACK 0x6474e551
#define PT_GNU_RELRO 0x6474e552
#define PT_LOSUNW 0x6ffffffa
#define PT_SUNWBSS 0x6ffffffa
#define PT_SUNWSTACK 0x6ffffffb
#define PT_HISUNW 0x6fffffff
#define PT_HIOS 0x6fffffff
#define PT_LOPROC 0x70000000
#define PT_HIPROC 0x7fffffff
#define PN_XNUM 0xffff
#define PF_X (1 << 0)
#define PF_W (1 << 1)
#define PF_R (1 << 2)
#define PF_MASKOS 0x0ff00000
#define PF_MASKPROC 0xf0000000
#define R_X86_64_NONE 0
#define R_X86_64_64 1
#define R_X86_64_PC32 2
#define R_X86_64_GOT32 3
#define R_X86_64_PLT32 4
#define R_X86_64_COPY 5
#define R_X86_64_GLOB_DAT 6
#define R_X86_64_JUMP_SLOT 7
#define R_X86_64_RELATIVE 8
#define R_X86_64_GOTPCREL 9
#define R_X86_64_32 10
#define R_X86_64_32S 11
#define R_X86_64_16 12
#define R_X86_64_PC16 13
#define R_X86_64_8 14
#define R_X86_64_PC8 15
#define R_X86_64_DTPMOD64 16
#define R_X86_64_DTPOFF64 17
#define R_X86_64_TPOFF64 18
#define R_X86_64_TLSGD 19
#define R_X86_64_TLSLD 20
#define R_X86_64_DTPOFF32 21
#define R_X86_64_GOTTPOFF 22
#define R_X86_64_TPOFF32 23
#define R_X86_64_PC64 24
#define R_X86_64_GOTOFF64 25
#define R_X86_64_GOTPC32 26
#define R_X86_64_GOT64 27
#define R_X86_64_GOTPCREL64 28
#define R_X86_64_GOTPC64 29
#define R_X86_64_GOTPLT64 30
#define R_X86_64_PLTOFF64 31
#define R_X86_64_SIZE32 32
#define R_X86_64_SIZE64 33
#define R_X86_64_GOTPC32_TLSDESC 34
#define R_X86_64_TLSDESC_CALL 35
#define R_X86_64_TLSDESC 36
#define R_X86_64_IRELATIVE 37
#define R_X86_64_RELATIVE64 38
#define R_X86_64_GOTPCRELX 41 /* 6 bytes */
#define R_X86_64_REX_GOTPCRELX 42 /* 7 bytes */
#define R_X86_64_NUM 43
#define STB_LOCAL 0
#define STB_GLOBAL 1
#define STB_WEAK 2
#define STB_NUM 3
#define STB_LOOS 10
#define STB_GNU_UNIQUE 10
#define STB_HIOS 12
#define STB_LOPROC 13
#define STB_HIPROC 15
#define STT_NOTYPE 0
#define STT_OBJECT 1
#define STT_FUNC 2
#define STT_SECTION 3
#define STT_FILE 4
#define STT_COMMON 5
#define STT_TLS 6
#define STT_NUM 7
#define STT_LOOS 10
#define STT_GNU_IFUNC 10
#define STT_HIOS 12
#define STT_LOPROC 13
#define STT_HIPROC 15
#define STV_DEFAULT 0
#define STV_INTERNAL 1
#define STV_HIDDEN 2
#define STV_PROTECTED 3
#define SHN_UNDEF 0
#define SHN_LORESERVE 0xff00
#define SHN_LOPROC 0xff00
#define SHN_BEFORE 0xff00
#define SHN_AFTER 0xff01
#define SHN_HIPROC 0xff1f
#define SHN_LOOS 0xff20
#define SHN_HIOS 0xff3f
#define SHN_ABS 0xfff1
#define SHN_COMMON 0xfff2
#define SHN_XINDEX 0xffff
#define SHN_HIRESERVE 0xffff
#define SHF_WRITE (1 << 0)
#define SHF_ALLOC (1 << 1)
#define SHF_EXECINSTR (1 << 2)
#define SHF_MERGE (1 << 4)
#define SHF_STRINGS (1 << 5)
#define SHF_INFO_LINK (1 << 6)
#define SHF_LINK_ORDER (1 << 7)
#define SHF_OS_NONCONFORMING (1 << 8)
#define SHF_GROUP (1 << 9)
#define SHF_TLS (1 << 10)
#define SHF_COMPRESSED (1 << 11)
#define SHF_MASKOS 0x0ff00000
#define SHF_MASKPROC 0xf0000000
#define SHF_ORDERED (1 << 30)
#define SHF_EXCLUDE (1U << 31)
#define ELFCOMPRESS_ZLIB 1
#define ELFCOMPRESS_LOOS 0x60000000
#define ELFCOMPRESS_HIOS 0x6fffffff
#define ELFCOMPRESS_LOPROC 0x70000000
#define ELFCOMPRESS_HIPROC 0x7fffffff
#define SHT_NULL 0
#define SHT_PROGBITS 1
#define SHT_SYMTAB 2
#define SHT_STRTAB 3
#define SHT_RELA 4
#define SHT_HASH 5
#define SHT_DYNAMIC 6
#define SHT_NOTE 7
#define SHT_NOBITS 8
#define SHT_REL 9
#define SHT_SHLIB 10
#define SHT_DYNSYM 11
#define SHT_INIT_ARRAY 14
#define SHT_FINI_ARRAY 15
#define SHT_PREINIT_ARRAY 16
#define SHT_GROUP 17
#define SHT_SYMTAB_SHNDX 18
#define SHT_NUM 19
#define SHT_LOOS 0x60000000
#define SHT_GNU_ATTRIBUTES 0x6ffffff5
#define SHT_GNU_HASH 0x6ffffff6
#define SHT_GNU_LIBLIST 0x6ffffff7
#define SHT_CHECKSUM 0x6ffffff8
#define SHT_LOSUNW 0x6ffffffa
#define SHT_SUNW_move 0x6ffffffa
#define SHT_SUNW_COMDAT 0x6ffffffb
#define SHT_SUNW_syminfo 0x6ffffffc
#define SHT_GNU_verdef 0x6ffffffd
#define SHT_GNU_verneed 0x6ffffffe
#define SHT_GNU_versym 0x6fffffff
#define SHT_HISUNW 0x6fffffff
#define SHT_HIOS 0x6fffffff
#define SHT_LOPROC 0x70000000
#define SHT_HIPROC 0x7fffffff
#define SHT_LOUSER 0x80000000
#define SHT_HIUSER 0x8fffffff
#define DT_NULL 0
#define DT_NEEDED 1
#define DT_PLTRELSZ 2
#define DT_PLTGOT 3
#define DT_HASH 4
#define DT_STRTAB 5
#define DT_SYMTAB 6
#define DT_RELA 7
#define DT_RELASZ 8
#define DT_RELAENT 9
#define DT_STRSZ 10
#define DT_SYMENT 11
#define DT_INIT 12
#define DT_FINI 13
#define DT_SONAME 14
#define DT_RPATH 15
#define DT_SYMBOLIC 16
#define DT_REL 17
#define DT_RELSZ 18
#define DT_RELENT 19
#define DT_PLTREL 20
#define DT_DEBUG 21
#define DT_TEXTREL 22
#define DT_JMPREL 23
#define DT_BIND_NOW 24
#define DT_INIT_ARRAY 25
#define DT_FINI_ARRAY 26
#define DT_INIT_ARRAYSZ 27
#define DT_FINI_ARRAYSZ 28
#define DT_RUNPATH 29
#define DT_FLAGS 30
#define DT_ENCODING 32
#define DT_PREINIT_ARRAY 32
#define DT_PREINIT_ARRAYSZ 33
#define DT_SYMTAB_SHNDX 34
#define DT_NUM 35
#define DT_LOOS 0x6000000d
#define DT_HIOS 0x6ffff000
#define DT_LOPROC 0x70000000
#define DT_HIPROC 0x7fffffff
#define DT_VALRNGLO 0x6ffffd00
#define DT_GNU_PRELINKED 0x6ffffdf5
#define DT_GNU_CONFLICTSZ 0x6ffffdf6
#define DT_GNU_LIBLISTSZ 0x6ffffdf7
#define DT_CHECKSUM 0x6ffffdf8
#define DT_PLTPADSZ 0x6ffffdf9
#define DT_MOVEENT 0x6ffffdfa
#define DT_MOVESZ 0x6ffffdfb
#define DT_FEATURE_1 0x6ffffdfc
#define DT_POSFLAG_1 0x6ffffdfd
#define DT_SYMINSZ 0x6ffffdfe
#define DT_SYMINENT 0x6ffffdff
#define DT_VALRNGHI 0x6ffffdff
#define DT_VALTAGIDX(tag) (DT_VALRNGHI - (tag))
#define DT_VALNUM 12
#define DT_ADDRRNGLO 0x6ffffe00
#define DT_GNU_HASH 0x6ffffef5
#define DT_TLSDESC_PLT 0x6ffffef6
#define DT_TLSDESC_GOT 0x6ffffef7
#define DT_GNU_CONFLICT 0x6ffffef8
#define DT_GNU_LIBLIST 0x6ffffef9
#define DT_CONFIG 0x6ffffefa
#define DT_DEPAUDIT 0x6ffffefb
#define DT_AUDIT 0x6ffffefc
#define DT_PLTPAD 0x6ffffefd
#define DT_MOVETAB 0x6ffffefe
#define DT_SYMINFO 0x6ffffeff
#define DT_ADDRRNGHI 0x6ffffeff
#define DT_ADDRTAGIDX(tag) (DT_ADDRRNGHI - (tag))
#define DT_ADDRNUM 11
#define DT_VERSYM 0x6ffffff0
#define DT_RELACOUNT 0x6ffffff9
#define DT_RELCOUNT 0x6ffffffa
#define DT_FLAGS_1 0x6ffffffb
#define DT_VERDEF 0x6ffffffc
#define DT_VERDEFNUM 0x6ffffffd
#define DT_VERNEED 0x6ffffffe
#define DT_VERNEEDNUM 0x6fffffff
#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag))
#define DT_VERSIONTAGNUM 16
#define DT_AUXILIARY 0x7ffffffd
#define DT_FILTER 0x7fffffff
#define DT_EXTRATAGIDX(tag) ((Elf32_Word) - ((Elf32_Sword)(tag) << 1 >> 1) - 1)
#define DT_EXTRANUM 3
#define VER_NEED_NONE 0
#define VER_NEED_CURRENT 1
#define VER_NEED_NUM 2
#define VER_FLG_WEAK 0x2
#define ELF_NOTE_SOLARIS "SUNW Solaris"
#define ELF_NOTE_GNU "GNU"
#define ELF_NOTE_PAGESIZE_HINT 1
#define ELF_NOTE_ABI NT_GNU_ABI_TAG
#define ELF_NOTE_OS_LINUX 0
#define ELF_NOTE_OS_GNU 1
#define ELF_NOTE_OS_SOLARIS2 2
#define ELF_NOTE_OS_FREEBSD 3
#define NT_GNU_ABI_TAG 1
#define NT_GNU_BUILD_ID 3
#define NT_GNU_GOLD_VERSION 4
#define EF_CPU32 0x00810000
#define DF_ORIGIN 0x00000001
#define DF_SYMBOLIC 0x00000002
#define DF_TEXTREL 0x00000004
#define DF_BIND_NOW 0x00000008
#define DF_STATIC_TLS 0x00000010
#define DF_1_NOW 0x00000001
#define DF_1_GLOBAL 0x00000002
#define DF_1_GROUP 0x00000004
#define DF_1_NODELETE 0x00000008
#define DF_1_LOADFLTR 0x00000010
#define DF_1_INITFIRST 0x00000020
#define DF_1_NOOPEN 0x00000040
#define DF_1_ORIGIN 0x00000080
#define DF_1_DIRECT 0x00000100
#define DF_1_TRANS 0x00000200
#define DF_1_INTERPOSE 0x00000400
#define DF_1_NODEFLIB 0x00000800
#define DF_1_NODUMP 0x00001000
#define DF_1_CONFALT 0x00002000
#define DF_1_ENDFILTEE 0x00004000
#define DF_1_DISPRELDNE 0x00008000
#define DF_1_DISPRELPND 0x00010000
#define DF_1_NODIRECT 0x00020000
#define DF_1_IGNMULDEF 0x00040000
#define DF_1_NOKSYMS 0x00080000
#define DF_1_NOHDR 0x00100000
#define DF_1_EDITED 0x00200000
#define DF_1_NORELOC 0x00400000
#define DF_1_SYMINTPOSE 0x00800000
#define DF_1_GLOBAUDIT 0x01000000
#define DF_1_SINGLETON 0x02000000
#define DF_1_STUB 0x04000000
#define DF_1_PIE 0x08000000
#define DTF_1_PARINIT 0x00000001
#define DTF_1_CONFEXP 0x00000002
#define DF_P1_LAZYLOAD 0x00000001
#define DF_P1_GROUPPERM 0x00000002
#define ELF64_ST_BIND(val) (((unsigned char)(val)) >> 4)
#define ELF64_ST_TYPE(val) ((val)&0xf)
#define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type)&0xf))
#define ELF64_ST_VISIBILITY(o) ((o)&0x03)
#define ELF64_R_SYM(i) ((i) >> 32)
#define ELF64_R_TYPE(i) ((i)&0xffffffff)
#define ELF64_R_INFO(sym, type) ((((Elf64_Xword)(sym)) << 32) + (type))
#define ELF64_M_SYM(info) ((info) >> 8)
#define ELF64_M_SIZE(info) ((unsigned char)(info))
#define ELF64_M_INFO(sym, size) (((sym) << 8) + (unsigned char)(size))
#define NT_PRSTATUS 1
#define NT_PRFPREG 2
#define NT_FPREGSET 2
#define NT_PRPSINFO 3
#define NT_PRXREG 4
#define NT_TASKSTRUCT 4
#define NT_PLATFORM 5
#define NT_AUXV 6
#define NT_GWINDOWS 7
#define NT_ASRS 8
#define NT_PSTATUS 10
#define NT_PSINFO 13
#define NT_PRCRED 14
#define NT_UTSNAME 15
#define NT_LWPSTATUS 16
#define NT_LWPSINFO 17
#define NT_PRFPXREG 20
#define NT_SIGINFO 0x53494749
#define NT_FILE 0x46494c45
#define NT_PRXFPREG 0x46e62b7f
#define NT_PPC_VMX 0x100
#define NT_PPC_SPE 0x101
#define NT_PPC_VSX 0x102
#define NT_PPC_TAR 0x103
#define NT_PPC_PPR 0x104
#define NT_PPC_DSCR 0x105
#define NT_PPC_EBB 0x106
#define NT_PPC_PMU 0x107
#define NT_PPC_TM_CGPR 0x108
#define NT_PPC_TM_CFPR 0x109
#define NT_PPC_TM_CVMX 0x10a
#define NT_PPC_TM_CVSX 0x10b
#define NT_PPC_TM_SPR 0x10c
#define NT_PPC_TM_CTAR 0x10d
#define NT_PPC_TM_CPPR 0x10e
#define NT_PPC_TM_CDSCR 0x10f
#define NT_X86_XSTATE 0x202
#define NT_S390_HIGH_GPRS 0x300
#define NT_S390_TIMER 0x301
#define NT_S390_TODCMP 0x302
#define NT_S390_TODPREG 0x303
#define NT_S390_CTRS 0x304
#define NT_S390_PREFIX 0x305
#define NT_S390_LAST_BREAK 0x306
#define NT_S390_SYSTEM_CALL 0x307
#define NT_S390_TDB 0x308
#define NT_S390_VXRS_LOW 0x309
#define NT_S390_VXRS_HIGH 0x30a
#define NT_S390_GS_CB 0x30b
#define NT_S390_GS_BC 0x30c
#define NT_S390_RI_CB 0x30d
#define NT_ARM_VFP 0x400
#define NT_ARM_TLS 0x401
#define NT_ARM_HW_BREAK 0x402
#define NT_ARM_HW_WATCH 0x403
#define NT_ARM_SYSTEM_CALL 0x404
#define NT_ARM_SVE 0x405
#define NT_ARM_PAC_MASK 0x406
#define NT_METAG_CBUF 0x500
#define NT_METAG_RPIPE 0x501
#define NT_METAG_TLS 0x502
#define NT_ARC_V2 0x600
#define NT_VMCOREDD 0x700
#define NT_VERSION 1
#define VER_DEF_NONE 0
#define VER_DEF_CURRENT 1
#define VER_DEF_NUM 2
#define VER_FLG_BASE 0x1
#define VER_FLG_WEAK 0x2
#define VER_NDX_LOCAL 0
#define VER_NDX_GLOBAL 1
#define VER_NDX_LORESERVE 0xff00
#define VER_NDX_ELIMINATE 0xff01
#define LL_NONE 0
#define LL_EXACT_MATCH (1 << 0)
#define LL_IGNORE_INT_VER (1 << 1)
#define LL_REQUIRE_MINOR (1 << 2)
#define LL_EXPORTS (1 << 3)
#define LL_DELAY_LOAD (1 << 4)
#define LL_DELTA (1 << 5)
#define R_BPF_NONE 0
#define R_BPF_MAP_FD 1
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_DEF_H_ */

120
libc/elf/elf.h Normal file
View file

@ -0,0 +1,120 @@
#ifndef COSMOPOLITAN_LIBC_ELF_H_
#define COSMOPOLITAN_LIBC_ELF_H_
#include "libc/bits/safemacros.h"
#include "libc/elf/def.h"
#include "libc/elf/struct/ehdr.h"
#include "libc/elf/struct/phdr.h"
#include "libc/elf/struct/shdr.h"
#include "libc/elf/struct/sym.h"
#include "libc/limits.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § executable & linkable format
*/
struct MappedFile;
Elf64_Ehdr *mapelfread(const char *filename, struct MappedFile *mf);
char *getelfstringtable(const Elf64_Ehdr *elf, size_t mapsize);
Elf64_Sym *getelfsymboltable(const Elf64_Ehdr *elf, size_t mapsize,
Elf64_Xword *out_count);
Elf64_Shdr *getelfsectionbyaddress(const Elf64_Ehdr *elf, size_t mapsize,
void *addr);
forceinline void checkelfaddress(const Elf64_Ehdr *elf, size_t mapsize,
intptr_t addr, size_t addrsize) {
#if !(TRUSTWORTHY + ELF_TRUSTWORTHY + 0)
if (addr < (intptr_t)elf || addr + addrsize > (intptr_t)elf + mapsize) {
abort();
}
#endif
}
static inline bool iself64binary(const Elf64_Ehdr *elf, size_t mapsize) {
return mapsize >= sizeof(Elf64_Ehdr) &&
memcmp(elf->e_ident, ELFMAG, 4) == 0 &&
(elf->e_ident[EI_CLASS] == ELFCLASSNONE ||
elf->e_ident[EI_CLASS] == ELFCLASS64);
}
static inline bool iselfsymbolcontent(const Elf64_Sym *sym) {
return sym->st_size > 0 && (ELF64_ST_TYPE(sym->st_info) == STT_FUNC ||
ELF64_ST_TYPE(sym->st_info) == STT_OBJECT);
}
static inline Elf64_Phdr *getelfsegmentheaderaddress(const Elf64_Ehdr *elf,
size_t mapsize,
unsigned i) {
intptr_t addr =
((intptr_t)elf + (intptr_t)elf->e_phoff + (intptr_t)elf->e_phentsize * i);
checkelfaddress(elf, mapsize, addr, elf->e_phentsize);
return (Elf64_Phdr *)addr;
}
static inline Elf64_Shdr *getelfsectionheaderaddress(const Elf64_Ehdr *elf,
size_t mapsize,
Elf64_Half i) {
intptr_t addr =
((intptr_t)elf + (intptr_t)elf->e_shoff + (intptr_t)elf->e_shentsize * i);
checkelfaddress(elf, mapsize, addr, elf->e_shentsize);
return (Elf64_Shdr *)addr;
}
static inline void *getelfsectionaddress(const Elf64_Ehdr *elf, size_t mapsize,
const Elf64_Shdr *shdr) {
intptr_t addr = (intptr_t)elf + (intptr_t)shdr->sh_offset;
intptr_t size = (intptr_t)shdr->sh_size;
checkelfaddress(elf, mapsize, addr, size);
return (void *)addr;
}
static inline char *getelfsectionnamestringtable(const Elf64_Ehdr *elf,
size_t mapsize) {
if (!elf->e_shoff || !elf->e_shentsize) return NULL;
return getelfsectionaddress(
elf, mapsize, getelfsectionheaderaddress(elf, mapsize, elf->e_shstrndx));
}
static inline void getelfvirtualaddressrange(const Elf64_Ehdr *elf,
size_t elfsize,
intptr_t *out_start,
intptr_t *out_end) {
intptr_t start = INTPTR_MAX;
intptr_t end = 0;
for (unsigned i = 0; i < elf->e_phnum; ++i) {
Elf64_Phdr *phdr = getelfsegmentheaderaddress(elf, elfsize, i);
if (phdr->p_type != PT_LOAD) continue;
intptr_t pstart = phdr->p_vaddr;
intptr_t pend = phdr->p_vaddr + phdr->p_memsz;
if (pstart < start) start = pstart;
if (pend > end) end = pend;
}
if (out_start) *out_start = start;
if (out_end) *out_end = end;
}
static inline char *getelfstring(const Elf64_Ehdr *elf, size_t mapsize,
const char *strtab, Elf64_Word rva) {
intptr_t addr = (intptr_t)strtab + rva;
#if !(TRUSTWORTHY + ELF_TRUSTWORTHY + 0)
checkelfaddress(elf, mapsize, addr, 0);
checkelfaddress(elf, mapsize, addr,
strnlen((char *)addr, (intptr_t)elf + mapsize - addr) + 1);
#endif
return (char *)addr;
}
static inline const char *getelfsectionname(const Elf64_Ehdr *elf,
size_t mapsize, Elf64_Shdr *shdr) {
if (!elf || !shdr) return NULL;
return getelfstring(elf, mapsize, getelfsectionnamestringtable(elf, mapsize),
shdr->sh_name);
}
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_H_ */

210
libc/elf/elf.lds Normal file
View file

@ -0,0 +1,210 @@
/*-*- mode: ld-script; indent-tabs-mode: nil; tab-width: 2; coding: utf-8 -*-│
│vi: set et sts=2 tw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
│ This program is free software; you can redistribute it and/or modify │
│ it under the terms of the GNU General Public License as published by │
│ the Free Software Foundation; version 2 of the License. │
│ │
│ This program is distributed in the hope that it will be useful, but │
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
│ General Public License for more details. │
│ │
│ You should have received a copy of the GNU General Public License │
│ along with this program; if not, write to the Free Software │
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
│ 02110-1301 USA │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "ape/relocations.h"
/**
* @fileoverview Cosmopolitan ELF-Normative Linker Script.
*
* This linker script is an alternative to αcτµαlly pδrταblε εxεcµταblε.
* It's intended for host-only binaries that don't need to be able to run
* without an operating system, and don't need to be distributed. These
* are still static executables, and they start at roughly 1kb in size.
*
* @see ape/ape.lds
*/
ENTRY(_start)
SECTIONS {
. = SEGMENT_START("text-segment", IMAGE_BASE_VIRTUAL) + SIZEOF_HEADERS;
.real : {
HIDDEN(_base = .);
*(.text.real)
HIDDEN(_ereal = .);
}
.start : {
*(.start)
KEEP(*(.initprologue))
KEEP(*(SORT_BY_NAME(.init.*)))
KEEP(*(SORT_NONE(.init)))
KEEP(*(.initepilogue))
*(.text.startup)
}
.exit : {
*(.text.exit)
}
.cold : {
*(.text.unlikely)
}
.windows : {
*(.textwindowsprologue)
*(.text.windows) /* ← you can flip the windows bit in -DSUPPORT_VECTOR */
*(.textwindowsepilogue)
}
.text : {
*(SORT_BY_ALIGNMENT(.text.hot))
KEEP(*(.keep.text))
*(.text .text.*)
KEEP(*(SORT_BY_NAME(.sort.text.*)))
}
.test : {
HIDDEN(__test_start = .);
*(.test.unlikely)
*(.test .test.*)
}
.privileged : {
. = ALIGN(DEFINED(ftrace_init) ? PAGESIZE : 1);
HIDDEN(__privileged_start = .);
*(.privileged)
}
.rodata : {
HIDDEN(__ro = .); /* ←for gdb readibility */
/* Read-Only Data */
*(.rodata .rodata.*)
KEEP(*(SORT_BY_NAME(.sort.rodata.*)))
/* DSO stuff */
PROVIDE_HIDDEN(__got_plt = .);
*(.got.plt)
/* Windows DLL Import Directory */
HIDDEN(idata.ro = .);
KEEP(*(SORT_BY_NAME(.idata.ro.*)))
}
.initro : {
/* Encoded Data Structures */
KEEP(*(.initroprologue))
KEEP(*(SORT_BY_NAME(.initro.*)))
KEEP(*(.initroepilogue))
}
.ubsan : {
/* Undefined Behavior Sanitizer Types */
PROVIDE_HIDDEN(__ubsan_types_start = .);
*(.ubsan.types)
PROVIDE_HIDDEN(__ubsan_types_end = .);
PROVIDE_HIDDEN(__ubsan_data_start = .);
*(.ubsan.data)
PROVIDE_HIDDEN(__ubsan_data_end = .);
}
HIDDEN(_etext = .);
PROVIDE_HIDDEN(etext = .);
. = DATA_SEGMENT_ALIGN(CONSTANT(MAXPAGESIZE), CONSTANT(COMMONPAGESIZE));
.data : {
*(.data .data.*)
KEEP(*(SORT_BY_NAME(.sort.data.*)))
SORT(CONSTRUCTORS)
KEEP(*(.edata))
. = ALIGN(__SIZEOF_POINTER__);
HIDDEN(__piro_start = .);
QUAD(IMAGE_BASE_VIRTUAL);
PROVIDE_HIDDEN(__init_array_start = .);
KEEP(*(.preinit_array))
KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*)
SORT_BY_INIT_PRIORITY(.ctors.*)))
KEEP(*(SORT_NONE(.init_array)))
KEEP(*(SORT_NONE(.ctors)))
PROVIDE_HIDDEN(__init_array_end = .);
PROVIDE_HIDDEN(__fini_array_start = .);
KEEP(*(SORT_BY_INIT_PRIORITY(.fini_array.*)
SORT_BY_INIT_PRIORITY(.dtors.*)))
KEEP(*(SORT_NONE(.fini_array)))
KEEP(*(SORT_NONE(.dtors)))
PROVIDE_HIDDEN(__fini_array_end = .);
KEEP(*(SORT_BY_NAME(.piro.relo.sort.*)))
*(.data.rel.ro .data.rel.ro.*)
PROVIDE_HIDDEN(__relo_end = .);
KEEP(*(SORT_BY_NAME(.piro.data.sort.*)))
}
HIDDEN(_edata = .);
PROVIDE_HIDDEN(edata = .);
.bss : {
KEEP(*(SORT_BY_NAME(.piro.bss.init.*)))
*(.piro.bss)
KEEP(*(SORT_BY_NAME(.piro.bss.sort.*)))
HIDDEN(__piro_end = .);
*(.bss .bss.*)
*(COMMON)
KEEP(*(SORT_BY_NAME(.sort.bss.*)))
. = ALIGN(ABSOLUTE(.) - IMAGE_BASE_VIRTUAL > 0x80000000 ? 0x80000000 :
ABSOLUTE(.) - IMAGE_BASE_VIRTUAL > 0x200000 ? 0x200000 : 0x1000);
*(.xlm)
. = ALIGN(ABSOLUTE(.) - IMAGE_BASE_VIRTUAL > 0x80000000 ? 0x80000000 :
ABSOLUTE(.) - IMAGE_BASE_VIRTUAL > 0x200000 ? 0x200000 : 0x1000);
. = ALIGN(. != 0 ? __SIZEOF_POINTER__ : 1);
}
HIDDEN(_end = .);
PROVIDE_HIDDEN(end = .);
.gnu_debuglink 0 : { *(.gnu_debuglink) }
.stab 0 : { *(.stab) }
.stabstr 0 : { *(.stabstr) }
.stab.excl 0 : { *(.stab.excl) }
.stab.exclstr 0 : { *(.stab.exclstr) }
.stab.index 0 : { *(.stab.index) }
.stab.indexstr 0 : { *(.stab.indexstr) }
.debug 0 : { *(.debug) }
.line 0 : { *(.line) }
.debug_srcinfo 0 : { *(.debug_srcinfo) }
.debug_sfnames 0 : { *(.debug_sfnames) }
.debug_aranges 0 : { *(.debug_aranges) }
.debug_pubnames 0 : { *(.debug_pubnames) }
.debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }
.debug_abbrev 0 : { *(.debug_abbrev) }
.debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end ) }
.debug_frame 0 : { *(.debug_frame) }
.debug_str 0 : { *(.debug_str) }
.debug_loc 0 : { *(.debug_loc) }
.debug_macinfo 0 : { *(.debug_macinfo) }
.debug_weaknames 0 : { *(.debug_weaknames) }
.debug_funcnames 0 : { *(.debug_funcnames) }
.debug_typenames 0 : { *(.debug_typenames) }
.debug_varnames 0 : { *(.debug_varnames) }
.debug_pubtypes 0 : { *(.debug_pubtypes) }
.debug_ranges 0 : { *(.debug_ranges) }
.debug_macro 0 : { *(.debug_macro) }
.debug_addr 0 : { *(.debug_addr) }
.gnu.attributes 0 : { KEEP(*(.gnu.attributes)) }
.GCC.command.line 0 : { *(.GCC.command.line) }
/DISCARD/ : {
*(.comment)
*(.discard)
*(.yoink)
*(.*)
}
}

54
libc/elf/elf.mk Normal file
View file

@ -0,0 +1,54 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
PKGS += LIBC_ELF
LIBC_ELF_ARTIFACTS += LIBC_ELF_A
LIBC_ELF = $(LIBC_ELF_A_DEPS) $(LIBC_ELF_A)
LIBC_ELF_A = o/$(MODE)/libc/elf/elf.a
LIBC_ELF_A_FILES := $(wildcard libc/elf/*) $(wildcard libc/elf/struct/*)
LIBC_ELF_A_HDRS = $(filter %.h,$(LIBC_ELF_A_FILES))
LIBC_ELF_A_SRCS_S = $(filter %.S,$(LIBC_ELF_A_FILES))
LIBC_ELF_A_SRCS_C = $(filter %.c,$(LIBC_ELF_A_FILES))
LIBC_ELF_A_SRCS = \
$(LIBC_ELF_A_SRCS_S) \
$(LIBC_ELF_A_SRCS_C)
LIBC_ELF_A_OBJS = \
$(LIBC_ELF_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(LIBC_ELF_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_ELF_A_SRCS_C:%.c=o/$(MODE)/%.o)
LIBC_ELF_A_CHECKS = \
$(LIBC_ELF_A).pkg \
$(LIBC_ELF_A_HDRS:%=o/$(MODE)/%.ok)
LIBC_ELF_A_DIRECTDEPS = \
LIBC_NEXGEN32E \
LIBC_STUBS
LIBC_ELF_A_DEPS := \
$(call uniq,$(foreach x,$(LIBC_ELF_A_DIRECTDEPS),$($(x))))
$(LIBC_ELF_A): libc/elf/ \
$(LIBC_ELF_A).pkg \
$(LIBC_ELF_A_OBJS)
$(LIBC_ELF_A).pkg: \
$(LIBC_ELF_A_OBJS) \
$(foreach x,$(LIBC_ELF_A_DIRECTDEPS),$($(x)_A).pkg)
o/libc/elf/elf.lds: \
libc/elf/elf.lds \
ape/relocations.h
LIBC_ELF_LIBS = $(foreach x,$(LIBC_ELF_ARTIFACTS),$($(x)))
LIBC_ELF_SRCS = $(foreach x,$(LIBC_ELF_ARTIFACTS),$($(x)_SRCS))
LIBC_ELF_HDRS = $(foreach x,$(LIBC_ELF_ARTIFACTS),$($(x)_HDRS))
LIBC_ELF_CHECKS = $(foreach x,$(LIBC_ELF_ARTIFACTS),$($(x)_CHECKS))
LIBC_ELF_OBJS = $(foreach x,$(LIBC_ELF_ARTIFACTS),$($(x)_OBJS))
$(LIBC_ELF_OBJS): $(BUILD_FILES) libc/elf/elf.mk
.PHONY: o/$(MODE)/libc/elf
o/$(MODE)/libc/elf: $(LIBC_ELF_CHECKS)

View file

@ -0,0 +1,36 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/elf/def.h"
#include "libc/elf/elf.h"
Elf64_Shdr *getelfsectionbyaddress(const Elf64_Ehdr *elf, size_t mapsize,
void *addr) {
Elf64_Half i;
if (elf) {
for (i = elf->e_shnum; i > 0; --i) {
Elf64_Shdr *shdr = getelfsectionheaderaddress(elf, mapsize, i - 1);
if ((intptr_t)addr >= shdr->sh_addr &&
(intptr_t)addr < shdr->sh_addr + shdr->sh_size) {
return shdr;
}
}
}
return NULL;
}

View file

@ -0,0 +1,34 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/elf/def.h"
#include "libc/elf/elf.h"
char *getelfstringtable(const Elf64_Ehdr *elf, size_t mapsize) {
Elf64_Half i;
Elf64_Shdr *shdr;
for (i = elf->e_shnum; i > 0; --i) {
if (i - 1 == elf->e_shstrndx) continue;
shdr = getelfsectionheaderaddress(elf, mapsize, i - 1);
if (shdr->sh_type == SHT_STRTAB) {
return getelfsectionaddress(elf, mapsize, shdr);
}
}
return NULL;
}

View file

@ -0,0 +1,36 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/elf/def.h"
#include "libc/elf/elf.h"
Elf64_Sym *getelfsymboltable(const Elf64_Ehdr *elf, size_t mapsize,
Elf64_Xword *out_count) {
Elf64_Half i;
Elf64_Shdr *shdr;
for (i = elf->e_shnum; i > 0; --i) {
shdr = getelfsectionheaderaddress(elf, mapsize, i - 1);
if (shdr->sh_type == SHT_SYMTAB) {
if (shdr->sh_entsize != sizeof(Elf64_Sym)) continue;
if (out_count) *out_count = shdr->sh_size / shdr->sh_entsize;
return getelfsectionaddress(elf, mapsize, shdr);
}
}
return NULL;
}

16
libc/elf/scalar.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef COSMOPOLITAN_LIBC_ELF_SCALAR_H_
#define COSMOPOLITAN_LIBC_ELF_SCALAR_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#define Elf64_Half uint16_t
#define Elf64_Word uint32_t
#define Elf64_Sword int32_t
#define Elf64_Xword uint64_t
#define Elf64_Sxword int64_t
#define Elf64_Addr uint64_t
#define Elf64_Off uint64_t
#define Elf64_Section uint16_t
#define Elf64_Versym Elf64_Half
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_SCALAR_H_ */

13
libc/elf/struct/auxv.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_AUXV_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_AUXV_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_auxv_t {
uint64_t a_type;
union {
uint64_t a_val;
} a_un;
} Elf64_auxv_t;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_AUXV_H_ */

14
libc/elf/struct/chdr.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_CHDR_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_CHDR_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Chdr {
Elf64_Word ch_type;
Elf64_Word ch_reserved;
Elf64_Xword ch_size;
Elf64_Xword ch_addralign;
} Elf64_Chdr;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_CHDR_H_ */

15
libc/elf/struct/dyn.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_DYN_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_DYN_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Dyn {
Elf64_Sxword d_tag;
union {
Elf64_Xword d_val;
Elf64_Addr d_ptr;
} d_un;
} Elf64_Dyn;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_DYN_H_ */

27
libc/elf/struct/ehdr.h Normal file
View file

@ -0,0 +1,27 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_EHDR_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_EHDR_H_
#include "libc/elf/def.h"
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#define EI_NIDENT 16
typedef struct Elf64_Ehdr {
unsigned char e_ident[EI_NIDENT];
Elf64_Half e_type;
Elf64_Half e_machine;
Elf64_Word e_version;
Elf64_Addr e_entry;
Elf64_Off e_phoff;
Elf64_Off e_shoff;
Elf64_Word e_flags;
Elf64_Half e_ehsize;
Elf64_Half e_phentsize;
Elf64_Half e_phnum;
Elf64_Half e_shentsize;
Elf64_Half e_shnum;
Elf64_Half e_shstrndx;
} Elf64_Ehdr;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_EHDR_H_ */

15
libc/elf/struct/lib.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_LIB_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_LIB_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Lib {
Elf64_Word l_name;
Elf64_Word l_time_stamp;
Elf64_Word l_checksum;
Elf64_Word l_version;
Elf64_Word l_flags;
} Elf64_Lib;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_LIB_H_ */

15
libc/elf/struct/move.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_MOVE_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_MOVE_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Move {
Elf64_Xword m_value;
Elf64_Xword m_info;
Elf64_Xword m_poffset;
Elf64_Half m_repeat;
Elf64_Half m_stride;
} Elf64_Move;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_MOVE_H_ */

13
libc/elf/struct/nhdr.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_NHDR_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_NHDR_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Nhdr {
Elf64_Word n_namesz;
Elf64_Word n_descsz;
Elf64_Word n_type;
} Elf64_Nhdr;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_NHDR_H_ */

18
libc/elf/struct/phdr.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_PHDR_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_PHDR_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Phdr {
Elf64_Word p_type;
Elf64_Word p_flags;
Elf64_Off p_offset;
Elf64_Addr p_vaddr;
Elf64_Addr p_paddr;
Elf64_Xword p_filesz;
Elf64_Xword p_memsz;
Elf64_Xword p_align;
} Elf64_Phdr;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_PHDR_H_ */

12
libc/elf/struct/rel.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_REL_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_REL_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Rel {
Elf64_Addr r_offset;
Elf64_Xword r_info; /** @see ELF64_R_{SYM,SIZE,INFO} */
} Elf64_Rel;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_REL_H_ */

18
libc/elf/struct/rela.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_RELA_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_RELA_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Rela {
/*u64*/ Elf64_Addr r_offset;
/*
* ELF64_R_SYM(r_info) sym
* ELF64_R_TYPE(r_info) R_X86_64_{64,PC32,GOTPCRELX,...}
* ELF64_R_INFO(sym, type) r_info
*/
/*u64*/ Elf64_Xword r_info; /** @see ELF64_R_{SYM,SIZE,INFO} */
/*i64*/ Elf64_Sxword r_addend;
} Elf64_Rela;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_RELA_H_ */

32
libc/elf/struct/shdr.h Normal file
View file

@ -0,0 +1,32 @@
#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,MERGE,STRINGS,...} */
Elf64_Addr sh_addr;
Elf64_Off sh_offset;
Elf64_Xword sh_size;
/*
* If SHT_RELA: Index of section of associated symbol table.
* If SHT_SYMTAB: Index of section of associated string table.
*/
Elf64_Word sh_link;
/*
* If SHT_RELA: Index of section to which relocations apply.
* If SHT_SYMTAB: One greater than symbol table index of last 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_ */

18
libc/elf/struct/sym.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_SYM_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_SYM_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Sym {
Elf64_Word st_name;
/* ELF64_ST_TYPE(st_info) → STT_{NOTYPE,OBJECT,FUNC,SECTION,FILE,COMMON,...}
* ELF64_ST_BIND(st_info) STB_{LOCAL,GLOBAL,WEAK,...} */
unsigned char st_info;
unsigned char st_other;
Elf64_Section st_shndx;
Elf64_Addr st_value;
Elf64_Xword st_size;
} Elf64_Sym;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_SYM_H_ */

12
libc/elf/struct/syminfo.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_SYMINFO_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_SYMINFO_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Syminfo {
Elf64_Half si_boundto;
Elf64_Half si_flags;
} Elf64_Syminfo;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_SYMINFO_H_ */

12
libc/elf/struct/verdaux.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_VERDAUX_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_VERDAUX_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Verdaux {
Elf64_Word vda_name;
Elf64_Word vda_next;
} Elf64_Verdaux;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_VERDAUX_H_ */

17
libc/elf/struct/verdef.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_VERDEF_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_VERDEF_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Verdef {
Elf64_Half vd_version;
Elf64_Half vd_flags;
Elf64_Half vd_ndx;
Elf64_Half vd_cnt;
Elf64_Word vd_hash;
Elf64_Word vd_aux;
Elf64_Word vd_next;
} Elf64_Verdef;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_VERDEF_H_ */

15
libc/elf/struct/vernaux.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_VERNAUX_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_VERNAUX_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Vernaux {
Elf64_Word vna_hash;
Elf64_Half vna_flags;
Elf64_Half vna_other;
Elf64_Word vna_name;
Elf64_Word vna_next;
} Elf64_Vernaux;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_VERNAUX_H_ */

15
libc/elf/struct/verneed.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef COSMOPOLITAN_LIBC_ELF_STRUCT_VERNEED_H_
#define COSMOPOLITAN_LIBC_ELF_STRUCT_VERNEED_H_
#include "libc/elf/scalar.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
typedef struct Elf64_Verneed {
Elf64_Half vn_version;
Elf64_Half vn_cnt;
Elf64_Word vn_file;
Elf64_Word vn_aux;
Elf64_Word vn_next;
} Elf64_Verneed;
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_STRUCT_VERNEED_H_ */