header32.asm 2.76 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

[section .header]

%include "elf.inc"

9
ehdr:
PoroCYon's avatar
PoroCYon committed
10
11
12
13
14
15
16
17
18
19
    ; 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
20
    dd (phdr - ehdr)   ; e_phoff
PoroCYon's avatar
PoroCYon committed
21
22
    dd 0               ; e_shoff
    dd 0               ; e_flags
23
24
    dw (phdr - ehdr)   ; e_ehsize
    dw (phdr.load - phdr.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
50
phdr:
PoroCYon's avatar
PoroCYon committed
51
%endif
PoroCYon's avatar
PoroCYon committed
52
%ifdef USE_INTERP
53
phdr.interp:
PoroCYon's avatar
PoroCYon committed
54
    dd PT_INTERP          ; {e_phnum: 2, e_shentsize: 0}, p_type
55
56
57
58
    dd (interp - ehdr)    ; {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
PoroCYon's avatar
PoroCYon committed
59
60
    dd 0,0                ; p_flags, p_align
%endif
61
phdr.dynamic:
PoroCYon's avatar
PoroCYon committed
62
    dd PT_DYNAMIC          ; {e_phnum: 2, e_shentsize: 0}, p_type
63
    dd (dynamic - ehdr)    ; {e_shnum: <junk>, e_shstrnd: <junk>}, p_offset
64
65
66
    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
69
phdr.load:
PoroCYon's avatar
PoroCYon committed
70
71
    dd PT_LOAD          ; p_type: 1 = PT_LOAD
    dd 0                ; p_offset
72
    dd ehdr, 0          ; p_vaddr, p_paddr
PoroCYon's avatar
PoroCYon committed
73
74
75
    ; 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
79
80
phdr.end:

PoroCYon's avatar
PoroCYon committed
81
%ifdef USE_INTERP
82
83
[section .rodata.interp]
interp:
PoroCYon's avatar
PoroCYon committed
84
    db "/lib/ld-linux.so.2",0
85
interp.end:
PoroCYon's avatar
PoroCYon committed
86
%endif
87
88

[section .rodata.dynamic]
PoroCYon's avatar
PoroCYon committed
89
90
global _DYNAMIC
_DYNAMIC:
91
92
dynamic:
dynamic.strtab:
PoroCYon's avatar
PoroCYon committed
93
    dd DT_STRTAB ; d_tag
94
    dd _strtab   ; d_un.d_ptr
95
dynamic.symtab:
PoroCYon's avatar
PoroCYon committed
96
97
98
    ; 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
99
%ifdef USE_DT_DEBUG
100
dynamic.debug:
PoroCYon's avatar
PoroCYon committed
101
102
103
104
    dd DT_DEBUG
_DEBUG:
    dd 0
%endif
PoroCYon's avatar
PoroCYon committed
105