1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef sw_DLL_hpp
16 #define sw_DLL_hpp
17 
18 #include <windows.h>
19 #include <vector>
20 #include <map>
21 
22 namespace sw
23 {
24 	class DLL
25 	{
26 	public:
27 		DLL(const char *name, const void *constants = 0, int constSize = 0);
28 
29 		~DLL();
30 
31 		void addFunction(const void *function, const void *entry, int size);
32 		void addRelocation(const void *function, const void *address, bool ripRelative);
33 		void emit();
34 
35 	private:
pageAlign(int address)36 		int pageAlign(int address)   // Align to 4 kB virtual page size
37 		{
38 			return (address + 0xFFF) & -0x1000;
39 		}
40 
fileAlign(int address)41 		int fileAlign(int address)   // Align to 512 byte file sections
42 		{
43 			return (address + 0x1FF) & -0x200;
44 		}
45 
46 		char *dllName;
47 
48 		IMAGE_DOS_HEADER DOSheader;
49 		IMAGE_NT_HEADERS32 COFFheader32;
50 		IMAGE_NT_HEADERS64 COFFheader64;
51 		IMAGE_SECTION_HEADER textSection;
52 		IMAGE_SECTION_HEADER exportsSection;
53 		IMAGE_SECTION_HEADER relocSection;
54 		IMAGE_SECTION_HEADER constSection;
55 
56 		IMAGE_EXPORT_DIRECTORY exportDirectory;
57 
58 		struct Function
59 		{
Functionsw::DLL::Function60 			Function() {};
Functionsw::DLL::Function61 			Function(unsigned int location, const void *function, const void *entry, int size) : location(location), entry(entry), size(size)
62 			{
63 				buffer = new unsigned char[size];
64 
65 				memcpy(buffer, function, size);
66 			}
67 
~Functionsw::DLL::Function68 			~Function()
69 			{
70 				delete[] buffer;
71 			}
72 
73 			void *buffer;
74 
75 			unsigned int location;
76 			const void *entry;
77 			int size;
78 		};
79 
80 		std::vector<const void*> functionOrder;
81 		typedef std::map<const void*, Function*> FunctionList;
82 		FunctionList functionList;
83 		int codeSize;
84 
85 		const void *constants;
86 		int constSize;
87 
88 		struct Relocation
89 		{
Relocationsw::DLL::Relocation90 			Relocation(unsigned int offset, bool ripRelative) : offset(offset), ripRelative(ripRelative)
91 			{
92 			}
93 
94 			unsigned int offset;
95 			bool ripRelative;
96 		};
97 
98 		typedef std::map<const void*, std::vector<Relocation> > GlobalRelocations;
99 		GlobalRelocations globalRelocations;
100 		typedef std::map<unsigned int, std::vector<unsigned short> > PageRelocations;
101 		PageRelocations pageRelocations;
102 	};
103 }
104 
105 #endif   // sw_DLL_hpp
106