smolemit.py 4.72 KB
Newer Older
PoroCYon's avatar
PoroCYon committed
1
2
3
4
5

import sys

from smolshared import *

6
def output_x86(libraries, nx, outf):
PoroCYon's avatar
PoroCYon committed
7
    outf.write('; vim: set ft=nasm:\n') # be friendly
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

    if nx:
        outf.write('%define USE_NX 1\n')

    usedrelocs = set({})
    for library, symrels in libraries.items():
        for sym, reloc in symrels: usedrelocs.add(reloc)

    if not(nx) and 'R_386_PC32' in usedrelocs and 'R_386_GOT32X' in usedrelocs:
        eprintf("Using a mix of R_386_PC32 and R_386_GOT32X relocations! "+\
                "Please change a few C compiler flags and recompile your code.")
        exit(1)


    use_jmp_bytes = not nx and 'R_386_PC32' in usedrelocs
    if use_jmp_bytes:
        outf.write('%define USE_JMP_BYTES 1\n')

PoroCYon's avatar
PoroCYon committed
26
    outf.write('bits 32\n')
27

PoroCYon's avatar
PoroCYon committed
28
29
    shorts = { l: l.split('.', 1)[0].lower().replace('-', '_') for l in libraries }

PoroCYon's avatar
PoroCYon committed
30
    outf.write('%include "header32.asm"\n')
31
    outf.write('dynamic.needed:\n')
PoroCYon's avatar
PoroCYon committed
32
    for library in libraries:
PoroCYon's avatar
PoroCYon committed
33
        outf.write('dd 1;DT_NEEDED\n')
34
        outf.write('dd (_symbols.{} - _strtab)\n'.format(shorts[library]))
35
36
37
38
39
40
    outf.write("""\
dynamic.end:
%ifndef UNSAFE_DYNAMIC
    dd DT_NULL
%endif
""")
PoroCYon's avatar
PoroCYon committed
41

42
    outf.write('[section .rodata.neededlibs]\n')
43
    outf.write('_strtab:\n')
44
    for library, symrels in libraries.items():
45
        outf.write('\t_symbols.{}: db "{}",0\n'.format(shorts[library], library))
PoroCYon's avatar
PoroCYon committed
46

47
48
49
50
51
52
    outf.write('[section .data.smolgot]\n')
    if not nx:
        outf.write('[section .text.smolplt]\n')

    outf.write('_symbols:\n')
    for library, symrels in libraries.items():
53
54
        for sym, reloc in symrels:
            # meh
55
            if reloc != 'R_386_PC32' and reloc != 'R_386_GOT32X':
56
57
58
                eprintf('Relocation type ' + reloc + ' of symbol ' + sym + ' unsupported!')
                sys.exit(1)

PoroCYon's avatar
PoroCYon committed
59
            hash = hash_djb2(sym)
60
61
62
63
64
            if nx:
                outf.write("\t\t_symbols.{lib}.{name}: dd 0x{hash:x}"\
                    .format(lib=shorts[library],name=sym,hash=hash).lstrip('\n'))
            else:
                outf.write(("""\
PoroCYon's avatar
PoroCYon committed
65
\t\tglobal {name}
66
67
68
\t\t{name}:""" + ("\n\t\t\tdb 0xE9" if use_jmp_bytes else '') + """
\t\t\tdd 0x{hash:x}
""").format(name=sym, hash=hash).lstrip('\n'))
PoroCYon's avatar
PoroCYon committed
69
70

    outf.write('db 0\n')
PoroCYon's avatar
PoroCYon committed
71
72
    outf.write('_symbols.end:\n')

73
74
75
76
77
78
79
80
81
82
83
84
85
    if nx:
        outf.write('_smolplt:\n')
        for library, symrels in libraries.items():
            for sym, reloc in symrels:
                outf.write("""\
[section .text.smolplt.{name}]
global {name}
{name}:
\tjmp [dword _symbols.{lib}.{name}]
""".format(lib=shorts[library],name=sym).lstrip('\n'))

        outf.write('_smolplt.end:\n')

PoroCYon's avatar
PoroCYon committed
86
    outf.write('%include "loader32.asm"\n')
87
88
# end output_x86

PoroCYon's avatar
PoroCYon committed
89

90
def output_amd64(libraries, outf):
PoroCYon's avatar
PoroCYon committed
91
92
    outf.write('; vim: set ft=nasm:\n')
    outf.write('bits 64\n')
93

PoroCYon's avatar
PoroCYon committed
94
95
96
97
98
    shorts = { l: l.split('.', 1)[0].lower().replace('-', '_') for l in libraries }

    outf.write('%include "header64.asm"\n')
    outf.write('dynamic.needed:\n')
    for library in libraries:
99
100
101
102
103
104
105
106
107
108
109
        outf.write('    dq 1;DT_NEEDED\n')
        outf.write('    dq (_symbols.{} - _strtab)\n'.format(shorts[library]))
    outf.write("""\
dynamic.symtab:
    dq DT_SYMTAB        ; d_tag
    dq 0                ; d_un.d_ptr
dynamic.end:
%ifndef UNSAFE_DYNAMIC
    dq DT_NULL
%endif
""")
PoroCYon's avatar
PoroCYon committed
110

111
    outf.write('[section .rodata.neededlibs]\n')
112
113

    outf.write('_strtab:\n')
114
115
    for library, symrels in libraries.items():
        outf.write('\t_symbols.{}: db "{}",0\n'.format(shorts[library], library))
116

117
    outf.write('[section .data.smolgot]\n')
118

PoroCYon's avatar
PoroCYon committed
119
    outf.write('_symbols:\n')
120
121
122
123
124
125
126
127
128
129
130
131
    for library, symrels in libraries.items():
        for sym, reloc in symrels:
            if reloc != 'R_X86_64_PLT32' and reloc != 'R_X86_64_GOTPCRELX':
                eprintf('Relocation type ' + reloc + ' of symbol ' + sym + ' unsupported!')
                sys.exit(1)

            if reloc == 'R_X86_64_GOTPCRELX':
                outf.write("""
global {name}
{name}:
""".format(name=sym).lstrip('\n'))

PoroCYon's avatar
PoroCYon committed
132
133
134
135
136
137
138
139
            hash = hash_djb2(sym)
            outf.write('\t\t_symbols.{lib}.{name}: dq 0x{hash:x}\n'\
                       .format(lib=shorts[library],name=sym,hash=hash))

    outf.write('db 0\n')
    outf.write('_symbols.end:\n')

    outf.write('_smolplt:\n')
140
141
142
    for library, symrels in libraries.items():
        for sym, reloc in symrels:
            if reloc == 'R_X86_64_PLT32':
143
                outf.write("""\
PoroCYon's avatar
PoroCYon committed
144
145
146
[section .text.smolplt.{name}]
global {name}
{name}:
147
\tjmp [rel _symbols.{lib}.{name}]
PoroCYon's avatar
PoroCYon committed
148
""".format(lib=shorts[library],name=sym).lstrip('\n'))
PoroCYon's avatar
PoroCYon committed
149

PoroCYon's avatar
PoroCYon committed
150
151
    outf.write('_smolplt.end:\n')
    outf.write('%include "loader64.asm"\n')
152
153
154
# end output_amd64


155
156
def output(arch, libraries, nx, outf):
    if arch == 'i386': output_x86(libraries, nx, outf)
157
    elif arch == 'x86_64': output_amd64(libraries, outf)
PoroCYon's avatar
PoroCYon committed
158
159
160
161
    else:
        eprintf("E: cannot emit for arch '" + str(arch) + "'")
        sys.exit(1)