1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "common/scummsys.h"
24 
25 #if defined(DYNAMIC_MODULES) && defined(USE_ELF_LOADER) && defined(MIPS_TARGET)
26 
27 #include "backends/plugins/elf/shorts-segment-manager.h"
28 
29 #include "common/debug.h"
30 #include "common/textconsole.h"
31 
32 extern char __plugin_hole_start;	// Indicates start of hole in program file for shorts
33 extern char __plugin_hole_end;		// Indicates end of hole in program file
34 extern char _gp[];					// Value of gp register
35 
36 namespace Common {
37 DECLARE_SINGLETON(ShortSegmentManager);	// For singleton
38 }
39 
ShortSegmentManager()40 ShortSegmentManager::ShortSegmentManager() {
41 	_shortsStart = &__plugin_hole_start ;	//shorts segment begins at the plugin hole we made when linking
42 	_shortsEnd = &__plugin_hole_end;		//and ends at the end of that hole.
43 }
44 
newSegment(uint32 size,char * origAddr)45 ShortSegmentManager::Segment *ShortSegmentManager::newSegment(uint32 size, char *origAddr) {
46 	char *lastAddress = origAddr;
47 	Common::List<Segment *>::iterator i;
48 
49 	// Find a block that fits, starting from the beginning
50 	for (i = _list.begin(); i != _list.end(); ++i) {
51 		char *currAddress = (*i)->getStart();
52 
53 		if (uint32(currAddress) - uint32(lastAddress) >= size)
54 			break;
55 
56 		lastAddress = (*i)->getEnd();
57 	}
58 
59 	if ((Elf32_Addr)lastAddress & 3)
60 		lastAddress += 4 - ((Elf32_Addr)lastAddress & 3);	// Round up to multiple of 4
61 
62 	if (lastAddress + size > _shortsEnd) {
63 		warning("elfloader: No space in shorts segment for %x bytes. Last address is %p, max address is %p.",
64 				size, lastAddress, _shortsEnd);
65 		return 0;
66 	}
67 
68 	Segment *seg = new Segment(lastAddress, size, origAddr);	// Create a new segment
69 
70 	if (lastAddress + size > _highestAddress)
71 		_highestAddress = lastAddress + size;	// Keep track of maximum
72 
73 	_list.insert(i, seg);
74 
75 	debug(2, "elfloader: Shorts segment size %x allocated. End = %p. Remaining space = %x. Highest so far is %p.",
76 			size, lastAddress + size, _shortsEnd - _list.back()->getEnd(), _highestAddress);
77 
78 	return seg;
79 }
80 
deleteSegment(ShortSegmentManager::Segment * seg)81 void ShortSegmentManager::deleteSegment(ShortSegmentManager::Segment *seg) {
82 	debug(2, "elfloader: Deleting shorts segment from %p to %p.", seg->getStart(), seg->getEnd());
83 	_list.remove(seg);
84 	delete seg;
85 }
86 
87 #endif // defined(DYNAMIC_MODULES) && defined(USE_ELF_LOADER) && defined(MIPS_TARGET)
88