summaryrefslogtreecommitdiff
path: root/ZAPD/Overlays/ZOverlay.cpp
blob: df4ca1ea55cd6c4fc527a02f061b4811c79f729a (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
#if 0
#include "ZOverlay.h"

#include <cassert>
#include <unordered_set>
#include "Globals.h"
#include "Utils/Directory.h"
#include <Utils/DiskFile.h>
#include "Utils/Path.h"
#include <ship/utils/StringHelper.h>
#include "WarningHandler.h"

using namespace ELFIO;

const char* RelocationEntry::GetSectionName() const
{
	switch (sectionType)
	{
	case SectionType::Text:
		return ".text";
	case SectionType::Data:
		return ".data";
	case SectionType::RoData:
		return ".rodata";
	case SectionType::Bss:
		return ".bss";
	case SectionType::ERROR:
		return ".ERROR";
	}
	assert(!"Oh no :c");
}

const char* RelocationEntry::GetRelocTypeName() const
{
	switch (relocationType)
	{
	case RelocationType::R_MIPS_32:
		return "R_MIPS_32";
	case RelocationType::R_MIPS_26:
		return "R_MIPS_26";
	case RelocationType::R_MIPS_HI16:
		return "R_MIPS_HI16";
	case RelocationType::R_MIPS_LO16:
		return "R_MIPS_LO16";
	}
	assert(!"Oh no :c");
}

ZOverlay::ZOverlay()
{
	name = "";
	entries = std::vector<RelocationEntry*>();
}

ZOverlay::ZOverlay(const std::string& nName) : ZOverlay()
{
	name = nName;
}

ZOverlay::~ZOverlay()
{
	for (auto entry : entries)
		if (entry)
			delete entry;
	entries.clear();
}

static const std::unordered_set<std::string> sRelSections = {
	".rel.text",
	".rel.data",
	".rel.rodata",
};
static const std::unordered_set<std::string> sSections = {
	".text", ".data", ".symtab", ".rodata", ".rodata.str1.4", ".rodata.cst4", ".rodata.cst8",
};

ZOverlay* ZOverlay::FromBuild(fs::path buildPath, fs::path cfgFolderPath)
{
	std::string cfgText = DiskFile::ReadAllText(cfgFolderPath / "overlay.cfg");
	std::vector<std::string> cfgLines = StringHelper::Split(cfgText, "\n");

	ZOverlay* ovl = new ZOverlay(StringHelper::Strip(cfgLines[0], "\r"));
	ovl->cfgLines = cfgLines;

	int32_t sectionOffs[5] = {0};
	std::vector<RelocationEntry*> textRelocs;
	std::vector<RelocationEntry*> dataRelocs;
	std::vector<RelocationEntry*> rodataRelocs;

	// get the elf files
	std::vector<elfio*> readers;
	for (size_t i = 1; i < cfgLines.size(); i++)
	{
		std::string elfPath =
			(buildPath / (cfgLines[i].substr(0, cfgLines[i].size() - 2) + ".o")).string();
		elfio* reader = new elfio();

		if (!reader->load(elfPath))
		{
			// not all files were compiled
			for (auto r : readers)
				delete r;
			readers.clear();

			delete ovl;
			return nullptr;
		}

		readers.push_back(reader);
	}

	for (size_t curReaderId = 0; curReaderId < readers.size(); curReaderId++)
	{
		auto& curReader = readers[curReaderId];

		Elf_Half sec_num = curReader->sections.size();
		for (int32_t i = 0; i < sec_num; i++)
		{
			section* pSec = curReader->sections[i];

			if (pSec->get_type() != SHT_REL ||
			    sRelSections.find(pSec->get_name()) == sRelSections.end())
			{
				continue;
			}

			symbol_section_accessor currentSymbols(*curReader,
			                                       curReader->sections[(Elf_Half)pSec->get_link()]);

			SectionType sectionType = GetSectionTypeFromStr(pSec->get_name());

			if (sectionType == SectionType::ERROR)
			{
				HANDLE_WARNING(WarningType::Always, "one of the section types returned ERROR", "");
			}

			relocation_section_accessor relocs(*curReader, pSec);
			for (Elf_Xword j = 0; j < relocs.get_entries_num(); j++)
			{
				Elf64_Addr offset = 0;
				Elf_Word symbol = 0;
				Elf_Word type = 0;
				{
					Elf_Sxword addend = 0;
					relocs.get_entry(j, offset, symbol, type, addend);
				}

				std::string curSymName;
				Elf_Half curSymShndx = SHN_UNDEF;
				{
					Elf64_Addr value;
					Elf_Xword size;
					unsigned char bind;
					unsigned char type;
					unsigned char other;
					currentSymbols.get_symbol(symbol, curSymName, value, size, bind, type,
					                          curSymShndx, other);
				}

				// check symbols outside the elf but within the overlay
				if (curSymShndx == SHN_UNDEF)
				{
					if (Globals::Instance->verbosity >= VerbosityLevel::VERBOSITY_DEBUG)
					{
						printf("Symbol '%s' doesn't exist in current .o file (%s). Searching...\n",
						       curSymName.c_str(), cfgLines[curReaderId + 1].c_str());
					}

					for (size_t readerId = 0; readerId < readers.size(); readerId++)
					{
						auto& reader = readers[readerId];

						if (curSymShndx != SHN_UNDEF)
							break;

						if (reader == curReader)
							continue;

						auto sectionData = reader->sections[(Elf_Half)pSec->get_link()];
						curSymShndx =
							ovl->FindSymbolInSection(curSymName, sectionData, *reader, readerId);
						if (curSymShndx != SHN_UNDEF)
							break;

						if (Globals::Instance->gccCompat)
						{
							// Symbol wasn't found, try checking every section
							Elf_Half sec_num = reader->sections.size();
							for (int32_t otherSectionIdx = 0; otherSectionIdx < sec_num;
							     otherSectionIdx++)
							{
								if (curSymShndx != SHN_UNDEF)
								{
									break;
								}

								auto sectionDataIter = reader->sections[otherSectionIdx];

								curSymShndx = ovl->FindSymbolInSection(curSymName, sectionDataIter,
								                                       *reader, readerId);
							}
						}
					}
				}

				if (curSymShndx != SHN_UNDEF)
				{
					RelocationType typeConverted = (RelocationType)type;
					offset += sectionOffs[static_cast<size_t>(sectionType)];

					RelocationEntry* reloc =
						new RelocationEntry(sectionType, typeConverted, offset);

					if (Globals::Instance->verbosity >= VerbosityLevel::VERBOSITY_DEBUG)
					{
						printf(".word 0x%08X  # %s %s 0x%04X\n", reloc->CalcRelocationWord(),
						       reloc->GetSectionName(), reloc->GetRelocTypeName(), reloc->offset);
					}

					// this is to keep the correct reloc entry order
					if (sectionType == SectionType::Text)
						textRelocs.push_back(reloc);
					if (sectionType == SectionType::Data)
						dataRelocs.push_back(reloc);
					if (sectionType == SectionType::RoData)
						rodataRelocs.push_back(reloc);
				}
			}
		}

		// increase section offsets
		for (int32_t i = 0; i < sec_num; i++)
		{
			section* pSec = curReader->sections[i];
			if (pSec->get_type() == SHT_PROGBITS &&
			    sSections.find(pSec->get_name()) != sSections.end())
			{
				SectionType sectionType = GetSectionTypeFromStr(pSec->get_name());
				sectionOffs[static_cast<size_t>(sectionType)] += pSec->get_size();
			}
		}
	}

	ovl->entries.reserve(textRelocs.size() + dataRelocs.size() + rodataRelocs.size());
	ovl->entries.insert(ovl->entries.end(), textRelocs.begin(), textRelocs.end());
	ovl->entries.insert(ovl->entries.end(), dataRelocs.begin(), dataRelocs.end());
	ovl->entries.insert(ovl->entries.end(), rodataRelocs.begin(), rodataRelocs.end());

	for (auto r : readers)
		delete r;
	readers.clear();

	return ovl;
}

std::string ZOverlay::GetSourceOutputCode([[maybe_unused]] const std::string& prefix)
{
	std::string output;

	output += ".section .ovl\n";

	output += StringHelper::Sprintf("# %sOverlayInfo\n", name.c_str());
	output += StringHelper::Sprintf(".word _%sSegmentTextSize\n", name.c_str());
	output += StringHelper::Sprintf(".word _%sSegmentDataSize\n", name.c_str());
	output += StringHelper::Sprintf(".word _%sSegmentRoDataSize\n", name.c_str());
	output += StringHelper::Sprintf(".word _%sSegmentBssSize\n", name.c_str());
	output += "\n";

	output += StringHelper::Sprintf(".word %i # reloc_count\n", entries.size());

	for (size_t i = 0; i < entries.size(); i++)
	{
		RelocationEntry* reloc = entries[i];
		output += StringHelper::Sprintf(".word 0x%08X  # %s %s 0x%04X\n",
		                                reloc->CalcRelocationWord(), reloc->GetSectionName(),
		                                reloc->GetRelocTypeName(), reloc->offset);
	}

	size_t offset = (entries.size() * 4) + 20;

	while (offset % 16 != 12)
	{
		output += ".word 0\n";
		offset += 4;
	}

	output += "\n";
	output +=
		StringHelper::Sprintf(".word 0x%08X # %sOverlayInfoOffset\n", offset + 4, name.c_str());
	return output;
}

SectionType ZOverlay::GetSectionTypeFromStr(const std::string& sectionName)
{
	if (sectionName == ".rel.text" || sectionName == ".text")
		return SectionType::Text;
	else if (sectionName == ".rel.data" || sectionName == ".data")
		return SectionType::Data;
	else if (sectionName == ".rel.rodata" || sectionName == ".rodata" ||
	         sectionName == ".rodata.str1.4" || sectionName == ".rodata.cst4")
		return SectionType::RoData;
	else if (sectionName == ".rel.bss" || sectionName == ".bss")
		return SectionType::Bss;

	return SectionType::ERROR;
}

ELFIO::Elf_Half ZOverlay::FindSymbolInSection(const std::string& curSymName,
                                              ELFIO::section* sectionData, ELFIO::elfio& reader,
                                              size_t readerId)
{
	if (sectionData == nullptr)
		return SHN_UNDEF;

	auto sectionDataName = sectionData->get_name();
	if (sSections.find(sectionDataName) == sSections.end())
		return SHN_UNDEF;

#ifdef DEVELOPMENT
	if (Globals::Instance->verbosity >= VerbosityLevel::VERBOSITY_DEBUG)
	{
		printf("\t File '%s' section: %s \n", cfgLines[readerId + 1].c_str(),
		       sectionDataName.c_str());
	}
#endif

	symbol_section_accessor symbols(reader, sectionData);

	Elf_Xword symbolNum = symbols.get_symbols_num();
	for (Elf_Xword symIdx = 0; symIdx < symbolNum; symIdx++)
	{
		Elf_Half shndx = SHN_UNDEF;
		Elf64_Addr value;
		std::string name;
		Elf_Xword size;
		unsigned char bind;
		unsigned char type;
		unsigned char other;

		symbols.get_symbol(symIdx, name, value, size, bind, type, shndx, other);

		if (name == curSymName)
		{
			if (Globals::Instance->verbosity >= VerbosityLevel::VERBOSITY_DEBUG)
			{
				printf("\t Symbol '%s' found in '%s' '%s' \n", curSymName.c_str(),
				       cfgLines[readerId + 1].c_str(), sectionDataName.c_str());
			}
			return shndx;
		}
	}
	return SHN_UNDEF;
}
#endif