header32.asm 2.75 KB
Newer Older
PoroCYon's avatar
PoroCYon committed
1
2
; vim: set ft=nasm:

PoroCYon's avatar
PoroCYon committed
3
%include "linkscr.inc"
PoroCYon's avatar
PoroCYon committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

[section .header]

%include "elf.inc"

header:
    ; e_ident
    db 0x7F, "ELF"
    db EI_CLASS, EI_DATA, EI_VERSION, EI_OSABI
    db EI_OSABIVERSION
    times 7 db 0       ; EI_PAD, ld.so is a busta and won't let us use our leet
                       ; group tags for padding bytes :(
    dw ELF_TYPE        ; e_type: 2 = executable
    dw ELF_MACHINE     ; e_machine: 3 = x86
    dd EI_VERSION      ; e_version
    dd _smol_start     ; e_entry
    dd (.segments - header) ; e_phoff
    dd 0               ; e_shoff
    dd 0               ; e_flags
    dw (.segments - header) ; e_ehsize
    dw (.segments.load - .segments.dynamic) ; e_phentsize
PoroCYon's avatar
PoroCYon committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
%ifdef USE_NX
%error "USE_NX not supported yet on i386 ('GOT' still needs RWX, and alignment has to be fixed)"
;%ifdef USE_INTERP
;    dw 4, 0            ; e_phnum, e_shentsize
;%else
;    dw 3, 0
;%endif
;.segments:
;.segments.load.text:
;    dd PT_LOAD
;    dd _smol_origin
;    dd _smol_text_start, 0
;    dd _smol_textandheader_size
;    dd _smol_textandheader_size
;    dd (PHDR_R | PHDR_X)
;    dd 0x1000
;.segments.load.data:
;    dd PT_LOAD
;    dd _smol_data_off
;    dd _smol_data_start, 0
;    dd _smol_data_size
;    dd _smol_dataandbss_size
;    dd (PHDR_R | PHDR_W)
;    dd 0x1;000
%else
PoroCYon's avatar
PoroCYon committed
50
.segments:
PoroCYon's avatar
PoroCYon committed
51
%endif
PoroCYon's avatar
PoroCYon committed
52
53
54
55
56
57
58
59
60
61
62
%ifdef USE_INTERP
.segments.interp:
    dd PT_INTERP          ; {e_phnum: 2, e_shentsize: 0}, p_type
    dd (.interp - header) ; {e_shnum: <junk>, e_shstrnd: <junk>}, p_offset
    dd .interp, .interp   ; p_vaddr, p_paddr
    dd (.interp.end-.interp) ; p_filesz
    dd (.interp.end-.interp) ; p_memsz
    dd 0,0                ; p_flags, p_align
%endif
.segments.dynamic:
    dd PT_DYNAMIC          ; {e_phnum: 2, e_shentsize: 0}, p_type
63
64
65
66
    dd (dynamic - header)  ; {e_shnum: <junk>, e_shstrnd: <junk>}, p_offset
    dd  dynamic, 0         ; p_vaddr, p_paddr
    dd (dynamic.end - dynamic) ; p_filesz
    dd (dynamic.end - dynamic) ; p_memsz
PoroCYon's avatar
PoroCYon committed
67
    dd 0, 0                ; p_flags, p_align
PoroCYon's avatar
PoroCYon committed
68
%ifndef USE_NX
PoroCYon's avatar
PoroCYon committed
69
70
71
.segments.load:
    dd PT_LOAD          ; p_type: 1 = PT_LOAD
    dd 0                ; p_offset
PoroCYon's avatar
PoroCYon committed
72
73
74
75
    dd _smol_origin, 0  ; p_vaddr, p_paddr
    ; use memsize twice here, linux doesn't care and it compresses better
    dd _smol_total_memsize ; p_filesz
    dd _smol_total_memsize ; p_memsz
PoroCYon's avatar
PoroCYon committed
76
77
    dd (PHDR_R | PHDR_W | PHDR_X) ; p_flags
    dd 0x1000           ; p_align
PoroCYon's avatar
PoroCYon committed
78
%endif
PoroCYon's avatar
PoroCYon committed
79
80
81
82
83
84
.segments.end:
%ifdef USE_INTERP
.interp:
    db "/lib/ld-linux.so.2",0
.interp.end:
%endif
PoroCYon's avatar
PoroCYon committed
85
86
global _DYNAMIC
_DYNAMIC:
87
88
dynamic:
dynamic.strtab:
PoroCYon's avatar
PoroCYon committed
89
90
    dd DT_STRTAB ; d_tag
    dd _symbols  ; d_un.d_ptr
91
dynamic.symtab:
PoroCYon's avatar
PoroCYon committed
92
93
94
    ; this is required to be present or ld.so will crash, but it can be bogus
    dd DT_SYMTAB ; d_tag: 6 = DT_SYMTAB
    dd 0         ; d_un.d_ptr
PoroCYon's avatar
PoroCYon committed
95
96
97
98
99
%ifdef USE_DT_DEBUG
    dd DT_DEBUG
_DEBUG:
    dd 0
%endif
PoroCYon's avatar
PoroCYon committed
100