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

import sys

from smolshared import *

6
def output_x86(libraries, nx, h16, outf):
PoroCYon's avatar
PoroCYon committed
7
    outf.write('; vim: set ft=nasm:\n') # be friendly
8

9
10
    if nx:  outf.write('%define USE_NX 1\n')
    if h16: outf.write('%define USE_HASH16 1\n')
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

    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)

59
            hash = hash_bsd2(sym) if h16 else 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
91
92
93
94
95
96
97
def output_amd64(libraries, nx, h16, outf):
    if h16:
        eprintf("--hash16 not supported yet for x86_64 outputs.")
        exit(1)

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

PoroCYon's avatar
PoroCYon committed
98
99
    outf.write('; vim: set ft=nasm:\n')
    outf.write('bits 64\n')
100

PoroCYon's avatar
PoroCYon committed
101
102
103
104
105
    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:
106
107
108
109
110
111
112
113
114
115
116
        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
117

118
    outf.write('[section .rodata.neededlibs]\n')
119
120

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

124
    outf.write('[section .data.smolgot]\n')
125

PoroCYon's avatar
PoroCYon committed
126
    outf.write('_symbols:\n')
127
128
129
130
131
132
133
134
135
136
137
138
    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'))

139
            hash = hash_bsd2(sym) if h16 else hash_djb2(sym)
PoroCYon's avatar
PoroCYon committed
140
141
142
143
144
145
146
            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')
147
148
149
    for library, symrels in libraries.items():
        for sym, reloc in symrels:
            if reloc == 'R_X86_64_PLT32':
150
                outf.write("""\
PoroCYon's avatar
PoroCYon committed
151
152
153
[section .text.smolplt.{name}]
global {name}
{name}:
154
\tjmp [rel _symbols.{lib}.{name}]
PoroCYon's avatar
PoroCYon committed
155
""".format(lib=shorts[library],name=sym).lstrip('\n'))
PoroCYon's avatar
PoroCYon committed
156

PoroCYon's avatar
PoroCYon committed
157
158
    outf.write('_smolplt.end:\n')
    outf.write('%include "loader64.asm"\n')
159
160
161
# end output_amd64


162
163
164
def output(arch, libraries, nx, h16, outf):
    if arch == 'i386': output_x86(libraries, nx, h16, outf)
    elif arch == 'x86_64': output_amd64(libraries, nx, h16, outf)
PoroCYon's avatar
PoroCYon committed
165
166
167
168
    else:
        eprintf("E: cannot emit for arch '" + str(arch) + "'")
        sys.exit(1)