summaryrefslogtreecommitdiff
path: root/lib/mipsobj.cpp
blob: 233c5c5f41223d22c1a8d3a7cca4d2c1791bcd08 (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
#include "mipsobj.h"

section::section(const String & _name, int _type) : name(_name), type(_type), datas(0), length(0) { }
section::section() : name(""), type(-1), datas(0), length(0) { }

section::~section() {
    if (datas)
	free(datas);
}

void section::setname(const String & _name) {
    name = _name;
}

void section::settype(int _type) {
    type = _type;
}

void section::putdatas(const Uint8 * _datas, int _length) {
    if (type != BSS) {
	datas = (Uint8 *) realloc(datas, length + _length);
	memcpy(datas + length, _datas, _length);
    }
    length += _length;
}

int section::gettype() {
    return type;
}

int section::getsize() {
    return length;
}

const Uint8 * section::getdatas() {
    return datas;
}

void section::putreloc(const String & _symbol, int _type, Uint32 _offset) {
    struct reloc_t r;

    r.symbol = _symbol;
    r.type = _type;
    r.offset = _offset;
    
    putreloc(r);
}

void section::putreloc(const struct reloc_t & r) {
    relocs.push_back(r);
}

mipsobj::mipsobj() : loaded(false) { }

mipsobj::~mipsobj() { }

void loadELF(Handle * elf) throw (GeneralException) {
}

#define OBJSIG 0x024b4e4c

#define READNAME(_str, _file) { \
    char _name[256]; \
    int _len; \
    \
    _len = _file->readU8(); \
    _file->read(_name, _len); \
    _name[_len] = 0; \
    _str = _name; \
}

void mipsobj::loadOBJ(Handle * obj) throw (GeneralException) {
    int cursec, len, reloctype, relocexpr, id;
    bool eof = false;
    std::map<int, String> secnames;
    std::map<int, String> symbolnames;
    Uint8 * datas;
    struct reloc_t reloc;
    struct symbol_t symbol;
    String name;
    
    while (!eof) {
	int entryid = obj->readU8();
	
	switch (entryid) {
	case 0x00:
	    eof = true;
	    break;
	
	case 0x02:
	    len = obj->readU16();
	    datas = (Uint8 *) malloc(len);
	    
	    obj->read(datas, len);
	    sections[secnames[cursec]].putdatas(datas, len);
	    
	    free(datas);
	    break;
	
	case 0x06:
	    cursec = obj->readU16();
	    break;
	
	case 0x08:
	    len = obj->readU32();
	    
	    sections[secnames[cursec]].putdatas(0, len);
	    break;
	
	case 0x0a:
	    reloctype = obj->readU8();
	    reloc.offset = obj->readU16();
	    
	    switch (reloctype) {
	    case 0x10:
		reloc.type = R_MIPS_32;
		break;
    	    case 0x4a:
		reloc.type = R_MIPS_26;
		break;
	    case 0x52:
		reloc.type = R_MIPS_HI16;
		break;
	    case 0x54:
		reloc.type = R_MIPS_LO16;
		break;
	    case 0x0a:
	    case 0x26:
	    case 0x28:
	    case 0x64:
		printm(M_ERROR, "Relocation type %02x not supported.\n", reloctype);
		exit(-1);
		break;
	    default:
		printm(M_ERROR, "Relocation type %02x UNKNOWN! Please send the object to the author.\n", reloctype);
		exit(-1);
	    }
	    
	    relocexpr = obj->readU8();
	    
	    switch (relocexpr) {
	    case 0x02:
		reloc.symbol = symbolnames[obj->readU16()];
		break;
	    case 0x04:
		reloc.symbol = secnames[obj->readU16()];
		break;
	    case 0x00:
	    case 0x0c:
	    case 0x16:
	    case 0x2c:
	    case 0x2e:
	    case 0x30:
	    case 0x32:
	    case 0x36:
		printm(M_ERROR, "Relocation expression %02x not supported.\n", relocexpr);
		exit(-1);
		break;
	    default:
		printm(M_ERROR, "Relocation expression %02x UNKNOWN! Please mail the author.\n", relocexpr);
		exit(-1);
		break;
	    }
	    
	    sections[secnames[cursec]].relocs.push_back(reloc);
	    
	    break;
	
	case 0x0c:
	    id = obj->readU16();
	    symbol.section = obj->readU16();
	    symbol.offset = obj->readU32();
	    READNAME(symbol.name, obj);
	    symbol.type = GLOBAL;

	    symbolnames[id] = symbol.name;

	    break;
	
	case 0x0e:
	    id = obj->readU16();
	    READNAME(symbol.name, obj);
	    symbol.type = EXTERN;
	    
	    symbolnames[id] = symbol.name;

	    break;
	
	case 0x10:
	    id = obj->readU16();
	    obj->readU8();
	    obj->readU16();
	    READNAME(name, obj);
	    
	    secnames[id] = name;
	    
	    break;

	case 0x12:
	    printm(M_WARNING, "Local symbol not supported.\n");
	    obj->readU16();
	    obj->readU32();
	    READNAME(name, obj);
	    break;
	
	case 0x1c:
	    printm(M_WARNING, "File number and name not supported.\n");
	    obj->readU16();
	    READNAME(name, obj);
	    break;
	
	case 0x2e:
	    if ((id = obj->readU8()) != 7) {
		printm(M_ERROR, "CPU type %i not supported.\n", id);
		exit(-1);
	    }
	    break;
	
	case 0x30:
	    printm(M_ERROR, "Constant not supported.\n");
	    exit(-1);
	    break;
	
	default:
	    printm(M_ERROR, "Object entry type %i UNKNOWN! Please send the object to the author.\n", entryid);
	    exit(-1);
	    break;
	}
    }
}

#define LIBSIG 0x0142494c

void mipsobj::loadLIB(Handle * lib, const String & objname) throw (GeneralException) {
    char _name[9];
    String name;
    int hsize, size, ptr;
    bool found = false;
    
    lib->seek(0);
    
    if (lib->readU32() != LIBSIG) {
	throw GeneralException("Not a Psy-Q lib file");
    }
    
    while (lib->tell() != lib->GetSize()) {
	ptr = lib->tell();
	
	lib->read(_name, 8);
	name = _name;
	lib->seek(4, SEEK_CUR);
	hsize = lib->readU32();
	size = lib->readU32();
	
	if (objname == name.trim()) {
	    lib->seek(ptr + hsize);
	    found = true;
	    break;
	}
	
	lib->seek(ptr + size);
    }
    
    if (!found) {
	throw GeneralException("Object `" + objname + "' not found in archive " + lib->GetName());
    }
    
    loadOBJ(lib);
}