summaryrefslogtreecommitdiff
path: root/tools/librel/rellib.py
blob: a9a6539d2f150a929fc0b4f5797a1d57ccca3445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import struct
import os
import libyaz0
import io

from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Optional

R_PPC_NONE = 0
R_PPC_ADDR32 = 1
R_PPC_ADDR24 = 2
R_PPC_ADDR16 = 3
R_PPC_ADDR16_LO = 4
R_PPC_ADDR16_HI = 5
R_PPC_ADDR16_HA = 6
R_PPC_ADDR14 = 7
R_PPC_REL24 = 10
R_PPC_REL14 = 11
R_DOLPHIN_NOP = 201
R_DOLPHIN_SECTION = 202
R_DOLPHIN_END = 203
R_DOLPHIN_MRKREF = 204

RELOCATION_NAMES = {
    R_PPC_NONE: "R_PPC_NONE",
    R_PPC_ADDR32: "R_PPC_ADDR32",
    R_PPC_ADDR24: "R_PPC_ADDR24",
    R_PPC_ADDR16: "R_PPC_ADDR16",
    R_PPC_ADDR16_LO: "R_PPC_ADDR16_LO",
    R_PPC_ADDR16_HI: "R_PPC_ADDR16_HI",
    R_PPC_ADDR16_HA: "R_PPC_ADDR16_HA",
    R_PPC_ADDR14: "R_PPC_ADDR14",
    R_PPC_REL24: "R_PPC_REL24",
    R_PPC_REL14: "R_PPC_REL14",
    R_DOLPHIN_NOP: "R_DOLPHIN_NOP",
    R_DOLPHIN_SECTION: "R_DOLPHIN_SECTION",
    R_DOLPHIN_END: "R_DOLPHIN_END",
    R_DOLPHIN_MRKREF: "R_DOLPHIN_MRKREF"
}


@dataclass
class Relocation:
    module: int  # reference module
    section: int  # reference section
    type: int
    offset: int
    addend: int  # reference offset
    parent: "Section" = field(default=None, repr=False)
    access: "disassembler.Access" = None
    relative_offset: int = None
    rel_offset: int = None

    @property
    def symbol_offset(self):
        return self.addend

    @property
    def replace_addr(self):
        return self.offset  # self.parent.offset +
    """
    @property
    def addr(self):
        shift = 0
        if (self.type == R_PPC_ADDR16 or self.type == R_PPC_ADDR16_LO or
                self.type == R_PPC_ADDR16_HI or self.type == R_PPC_ADDR16_HA):
            shift -= 2
        return self.offset + shift  # self.parent.offset
    """

@dataclass
class Section:
    index: int
    # Offset from the beginning of the REL to the section. If this is zero, the section is an uninitialized section (i.e. .bss)
    offset: int = 0
    unknown_flag: bool = False
    # Executable flag; if this is 1 the section is executable.
    executable_flag: bool = False
    # Length in bytes of the section. If this is zero, this entry is skipped.
    length: int = 0
    alignment: int = 0

    # not part of the section in the rel-file
    addr: int = None
    write_offset: int = None 
    name: Optional[str] = None
    data: Optional[bytes] = field(default=None, repr=False)
    relocations: List[Relocation] = field(default_factory=list, repr=False)


@dataclass
class REL:
    # Arbitrary identification number. Must be unique amongst all RELs used by a game. Must not be 0.
    index: int = 0
    next: int = 0 	# Pointer to next module, filled at runtime.
    prev: int = 0 	# Pointer to previous module, filled at runtime.
    numSections: int = 0  # Number of sections in the file.
    sectionInfoOffset: int = 0  # Offset to the start of the section table.
    # Offset to ASCII string containing name of module. May be NULL. Relative to start of REL file.
    nameOffset: int = 0
    nameSize: int = 0  # Size of the module name in bytes.
    version: int = 0  # Version number of the REL file format.
    bssSize: int = 0  # Size of the '.bss' section.
    relOffset: int = 0  # Offset to the relocation table.
    impOffset: int = 0  # Offset to imp table.
    impSize: int = 0  # Size of imp table in bytes.
    # Index into section table which prolog is relative to. Skip if this field is 0.
    prologSection: int = 0
    # Index into section table which epilog is relative to. Skip if this field is 0.
    epilogSection: int = 0
    # Index into section table which unresolved is relative to. Skip if this field is 0.
    unresolvedSection: int = 0
    bssSection: int = 0  # Index into section table which bss is relative to. Filled at runtime!
    prolog: int = 0  # Offset into specified section of the _prolog function.
    epilog: int = 0  # Offset into specified section of the _epilog function.
    # Offset into specified section of the _unresolved function.
    unresolved: int = 0
    # Version ≥ 2 only. Alignment constraint on all sections, expressed as power of 2.
    align: int = 0
    # Version ≥ 2 only. Alignment constraint on all '.bss' section, expressed as power of 2.
    bssAlign: int = 0
    # Version ≥ 3 only. If REL is linked with OSLinkFixed (instead of OSLink), the space after this address can be used for other purposes (like BSS).
    fixSize: int = 0

    # not part of the rel-file
    sections: List[Section] = field(default_factory=list, repr=False)
    relocations: List[Relocation] = field(default_factory=list, repr=False)
    path: Optional[Path] = field(default=None, repr=False)
    data: bytearray = field(default=None,repr=False)


def read_relocation(module, buffer):
    offset, type, section, addend = struct.unpack('>HBBI', buffer[:0x8])
    return Relocation(module, section, type, offset, addend)


def read_section(index, buffer):
    header = struct.unpack('>II', buffer[:0x8])
    flag0 = ((header[0] >> 1) & 1) != 0
    flag1 = ((header[0] >> 0) & 1) != 0

    offset = header[0] & ~3
    length = header[1]

    return Section(index, offset, flag0, flag1, length)


def read(buffer):
    # check if the rel is compressed
    if struct.unpack('>I', buffer[:4])[0] == 0x59617A30:
        buffer = libyaz0.decompress(io.BytesIO(buffer))

    header_size = 0x40
    header = struct.unpack('>IIIIIIIIIIIIBBBBIII', buffer[:0x40])

    align = 0
    bssAlign = 0
    if header[7] >= 2:
        v2 = struct.unpack('>II', buffer[0x40:0x48])
        align = v2[0]
        bssAlign = v2[1]
        header_size += 0x8

    fixSize = 0
    if header[7] >= 3:
        v3 = struct.unpack('>I', buffer[0x48:0x4C])
        fixSize = v3[0]
        header_size += 0x4

    rel = REL(*header, align, bssAlign, fixSize)
    rel.data = buffer

    rel.sections = []
    section_buffer = buffer[rel.sectionInfoOffset:]
    for i in range(rel.numSections):
        section_offset = 0x8 * i
        section = read_section(i, section_buffer[section_offset:])
        rel.sections.append(section)

    addr = 0
    for section in rel.sections:
        section.offset_padding = 0
        section.first_padding = 0
        section.addr = addr
        section.alignment = align
        addr += section.length

    for section in reversed(rel.sections):
        if section.offset > 0 and section.length > 0:
            offset_aligned = (section.offset + 15) & ~15
            shift = offset_aligned - section.offset
            section.offset_padding += 0  # shift
            addr_aligned = (section.addr + 7) & ~7
            shift = addr_aligned - section.addr
            section.first_padding += 0  # shift
            break

    for section in rel.sections:
        if section.offset > 0 and section.length > 0:
            section.data = buffer[section.offset:][:section.length +
                                                   section.first_padding+section.offset_padding]

    for bss_section in rel.sections[1:]:
        if bss_section.data == None:
            bss_section.addr = (last_end + 7) & ~7
            bss_section.alignment = bssAlign
        last_end = bss_section.addr + bss_section.length

    rel.relocations = []
    imp_buffer = buffer[rel.impOffset:]
    for i in range(rel.impSize // 8):
        imp_offset = 0x8 * i
        module_id, rel_offset = struct.unpack(
            '>II', imp_buffer[imp_offset:][:0x8])

        section = None
        offset = 0
        while True:
            relocation = read_relocation(module_id, buffer[rel_offset:])
            rel_offset += 0x8

            if relocation.type == R_PPC_NONE:
                continue
            if relocation.type == R_DOLPHIN_NOP:
                # NOP is used to simulate have long offset values, this is because 
                # the offset field is limited to 2^16-1 values. Thus, any offsets 
                # above 2^16 will be divided into a NOP + original relocation type.
                offset += relocation.offset
                continue
            if relocation.type == R_DOLPHIN_SECTION:
                section = rel.sections[relocation.section]
                offset = 0
                continue

            assert section
            relocation.parent = section
            relocation.rel_offset = rel_offset - 0x8
            relocation.relative_offset = relocation.offset
            relocation.offset += offset
            offset = relocation.offset

            assert relocation.type != R_DOLPHIN_MRKREF
            if relocation.type == R_DOLPHIN_END:
                break

            section.relocations.append(relocation)
            rel.relocations.append(relocation)

    return rel

def write_header(file, rel):
    header = struct.pack(
        '>IIIIIIIIIIIIBBBBIII', 
        rel.index,
        rel.next,
        rel.prev,
        rel.numSections,
        rel.sectionInfoOffset,
        rel.nameOffset,
        rel.nameSize,
        rel.version,
        rel.bssSize,
        rel.relOffset,
        rel.impOffset,
        rel.impSize,
        rel.prologSection,
        rel.epilogSection,
        rel.unresolvedSection,
        rel.bssSection,
        rel.prolog,
        rel.epilog,
        rel.unresolved)
    file.write(header)

    if rel.version >= 2:
        header = struct.pack(
            '>II', 
            rel.align,
            rel.bssAlign)
        file.write(header)

    if rel.version >= 3:
        header = struct.pack(
            '>I', 
            rel.fixSize)
        file.write(header)

def write_section_header(file, section):
    assert section.offset & 3 == 0
    file.write(struct.pack('>II', 
        section.offset | ((section.unknown_flag & 1) << 1) | ((section.executable_flag & 1) << 0),
        section.length
    ))

def write_section_data(file, section):
    assert len(section.data) == section.length
    file.write(section.data)

def write_relocation(file, offset, type, section, addend):
    assert (offset & 0xFFFF) == offset
    assert (type & 0xFF) == type
    assert (section & 0xFF) == section
    assert (addend & 0xFFFFFFFF) == addend
    file.write(struct.pack('>HBBI', 
        offset,
        type,
        section,
        addend
    ))
  
def write_imp(file, module, offset):
    assert (module & 0xFFFFFFFF) == module
    assert (offset & 0xFFFFFFFF) == offset
    file.write(struct.pack('>II', 
        module,
        offset
    ))   

RELOCATION_SIZES = {
    R_PPC_ADDR32: (0, 4),
    R_PPC_ADDR24: (1, 4),
    R_PPC_ADDR16: (0, 2),
    R_PPC_ADDR16_LO: (0, 2),
    R_PPC_ADDR16_HI: (0, 2),
    R_PPC_ADDR16_HA: (0, 2),
    R_PPC_REL24: (1, 4),
    R_PPC_REL14: (2, 4),
}

class RELRelocationException(Exception):
    ...

def apply_relocation(kind: int, module: int,
                     data: bytearray, data_offset: int,
                     P: int, S: int, A: int) -> bool:
    """

    P = relocation position - the location where the relocation will occur (data[P - data_offset])
    S = symbol address - the address of the symbol we are referencing
    A = append - the value added to the symbol address 

    """

    offset, size = RELOCATION_SIZES.get(kind, (0, 0))
    I = P - data_offset

    if size == 4:
        if len(data[I:]) < 4:
            raise RELRelocationException(f"data length too short. length >= {I+4} (current length: {len(data)})")
        X = struct.unpack('>I', data[I:][:4])[0]
    elif size == 2:
        if len(data[I:]) < 2:
            raise RELRelocationException(f"data length too short. length >= {I+2} (current length: {len(data)})")
        X = struct.unpack('>H', data[I:][:2])[0]
    else:
        return False

    if kind == R_PPC_ADDR32:
        R = (S + A) & 0xFFFFFFFF
        Y = struct.pack('>I', R)
    elif kind == R_PPC_ADDR16 or kind == R_PPC_ADDR16_LO:
        R = (S + A) & 0xFFFF
        Y = struct.pack('>H', R)
    elif kind == R_PPC_ADDR16_HI:
        R = ((S + A) >> 16) & 0xFFFF
        Y = struct.pack('>H', R)     
    elif kind == R_PPC_ADDR16_HA:
        R = (S + A) & 0xFFFFFFFF
        H = (R >> 16) + (1 if (R & 0x8000) else 0)
        Y = struct.pack('>H', H & 0xFFFF)     
    elif kind == R_PPC_REL24:
        R = (S + A - P) & 0xFFFFFFFF
        TM = (0b111111 << 26) & 0xFFFFFFFF
        BM = (0b11)

        assert (R & BM) == 0
        assert (R & TM) == 0 or (R & TM) == TM
        M = TM | BM
        Y = struct.pack(">I", (X & M) | (R & ~M))
    elif kind == R_PPC_REL14:
        R = (S + A - P) & 0xFFFFFFFF
        TM = 0b1111111111111111 << 16
        BM = 0b11
        assert (R & BM) == 0
        assert (R & TM) == 0 or (R & TM) == TM
        M = TM | BM
        Y = struct.pack(">I", (X & M) | (R & ~M))
    else:
        return False
    
    data[I:I+size] = Y
    return True