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 #ifndef BACKENDS_PLUGINS_ELF_LOADER_H
24 #define BACKENDS_PLUGINS_ELF_LOADER_H
25 
26 #include "common/scummsys.h"
27 
28 #if defined(DYNAMIC_MODULES) && defined(USE_ELF_LOADER)
29 
30 #include <stddef.h>
31 
32 #include "backends/plugins/elf/elf32.h"
33 #include "backends/plugins/dynamic-plugin.h"
34 
35 #include "common/stream.h"
36 
37 /**
38  * DLObject
39  *
40  * Class that most directly handles operations on a plugin file
41  * (opening it for reading, loading/unloading it in memory, finding a specific symbol in the file, etc.)
42  * Subclasses have the same functionality, but implementations specific to different processors/platforms.
43  */
44 class DLObject {
45 protected:
46 	Common::SeekableReadStream *_file;
47 
48 	byte *_segment;
49 	Elf32_Sym *_symtab;
50 	char *_strtab;
51 
52 	uint32 _segmentSize;
53 	ptrdiff_t _segmentOffset;
54 	uint32 _segmentVMA;
55 
56 	uint32 _symbol_cnt;
57 	int32 _symtab_sect;
58 	void *_dtors_start, *_dtors_end;
59 
60 	virtual void unload();
61 	bool load();
62 
63 	bool readElfHeader(Elf32_Ehdr *ehdr);
64 	bool readProgramHeaders(Elf32_Ehdr *ehdr, Elf32_Phdr *phdr, Elf32_Half num);
65 	virtual bool loadSegment(Elf32_Phdr *phdr);
66 	Elf32_Shdr *loadSectionHeaders(Elf32_Ehdr *ehdr);
67 	int findSymbolTableSection(Elf32_Ehdr *ehdr, Elf32_Shdr *shdr);
68 	int loadSymbolTable(Elf32_Ehdr *ehdr, Elf32_Shdr *shdr);
69 	bool loadStringTable(Elf32_Shdr *shdr);
70 	virtual void relocateSymbols(ptrdiff_t offset);
71 	void discardSegment();
72 
73 	// architecture specific
74 
75 	/**
76 	 * Follow the instruction of a relocation section.
77 	 *
78 	 * @param fileOffset	Offset into the File
79 	 * @param size			Size of relocation section
80 	 * @param relSegment	Base address of relocated segment in memory (memory offset)
81 	 */
82 	virtual bool relocate(Elf32_Off offset, Elf32_Word size, byte *relSegment) = 0;
83 	virtual bool relocateRels(Elf32_Ehdr *ehdr, Elf32_Shdr *shdr) = 0;
84 
85 	// platform specific
86 	virtual void flushDataCache(void *ptr, uint32 len) const = 0;
87 	virtual void *allocateMemory(uint32 align, uint32 size);
88 	virtual void deallocateMemory(void *ptr, uint32 size);
protectMemory(void * ptr,uint32 len,int prot)89 	virtual void protectMemory(void *ptr, uint32 len, int prot) const {};
90 
91 public:
92 	DLObject();
93 	virtual ~DLObject();
94 
95 	/**
96 	 * Test the size of the plugin in memory using the memory manager.
97 	 * @param path			Path of file
98 	 */
99 	void trackSize(const char *path);
100 	bool open(const char *path);
101 	bool close();
102 	void *symbol(const char *name);
103 	void discardSymtab();
104 };
105 
106 #endif /* defined(DYNAMIC_MODULES) && defined(USE_ELF_LOADER) */
107 
108 #endif /* BACKENDS_PLUGINS_ELF_LOADER_H */
109