1
1
/*
2
- * Copyright (c) 2016, Intel Corporation
2
+ * Copyright (c) 2016-2020 , Intel Corporation
3
3
* All rights reserved.
4
4
*
5
5
* Author: Jérémy Compostella <[email protected] >
29
29
* OF THE POSSIBILITY OF SUCH DAMAGE.
30
30
*/
31
31
32
- #include <stdlib .h>
32
+ #include <stddef .h>
33
33
#include <stdint.h>
34
34
#include <ewlog.h>
35
35
#include <ewlib.h>
36
+
37
+ #if defined(HOST )
36
38
#include <sys/mman.h>
39
+ #endif
37
40
41
+ #include "lib.h"
38
42
#include "pe.h"
39
43
40
44
/* The following structure definitions come from:
@@ -99,7 +103,7 @@ typedef struct {
99
103
} std_coff_t ;
100
104
101
105
typedef struct {
102
- UINTN image_base ;
106
+ UINT32 image_base ;
103
107
UINT32 section_alignment ;
104
108
UINT32 file_alignment ;
105
109
UINT16 major_operating_system_version ;
@@ -114,14 +118,16 @@ typedef struct {
114
118
UINT32 checksum ;
115
119
UINT16 subsystem ;
116
120
UINT16 dll_characteristics ;
117
- UINTN size_of_stack_reserve ;
118
- UINTN size_of_stack_commit ;
119
- UINTN size_of_heap_reserve ;
120
- UINTN size_of_heap_commit ;
121
+ UINT32 size_of_stack_reserve ;
122
+ UINT32 size_of_stack_commit ;
123
+ UINT32 size_of_heap_reserve ;
124
+ UINT32 size_of_heap_commit ;
121
125
UINT32 loader_flags ;
122
126
UINT32 number_of_rva_and_sizes ;
123
127
} win_t ;
124
128
129
+ #define BASERELOC_DIRECTORY_ENTRY 5
130
+
125
131
typedef struct {
126
132
coff_header_t hdr ;
127
133
struct {
@@ -165,14 +171,17 @@ void get_section_boundaries(section_header_t *section, UINT16 nb,
165
171
* end = 0 ;
166
172
167
173
for (i = 0 ; i < nb ; i ++ ) {
174
+ UINT32 size = section [i ].virtual_size ;
175
+ if (section [i ].size_of_raw_data )
176
+ size = section [i ].size_of_raw_data ;
168
177
* start = min (* start , section [i ].virtual_address );
169
- * end = max (* end , section [i ].virtual_address +
170
- section [i ].virtual_size );
178
+ * end = max (* end , section [i ].virtual_address + size );
171
179
}
172
180
}
173
181
174
- EFI_STATUS load_sections (section_header_t * section , UINT16 nb ,
175
- void * base , image_t * image )
182
+ static EFI_STATUS load_sections (section_header_t * section , UINT16 nb ,
183
+ void * base , image_t * image ,
184
+ size_t alignment )
176
185
{
177
186
loaded_section_t * data ;
178
187
UINT16 i ;
@@ -188,9 +197,13 @@ EFI_STATUS load_sections(section_header_t *section, UINT16 nb,
188
197
get_section_boundaries (section , nb , & data -> start , & data -> end );
189
198
190
199
data -> size = data -> end - data -> start ;
200
+ #if defined(HOST )
191
201
data -> addr = mmap (NULL , data -> size , PROT_EXEC | PROT_READ | PROT_WRITE ,
192
202
MAP_ANONYMOUS | MAP_SHARED , -1 , 0 );
193
- if (!data -> addr ) {
203
+ #else
204
+ data -> addr = memalign (alignment , data -> size );
205
+ #endif
206
+ if (data -> addr == (void * ) -1 || data -> addr == NULL ) {
194
207
ewerr ("Failed to allocate sections memory" );
195
208
free (data );
196
209
return EFI_OUT_OF_RESOURCES ;
@@ -205,7 +218,7 @@ EFI_STATUS load_sections(section_header_t *section, UINT16 nb,
205
218
data -> start ;
206
219
size = section [i ].virtual_size ;
207
220
208
- if (! size && section [i ].size_of_raw_data )
221
+ if (section [i ].size_of_raw_data )
209
222
size = section [i ].size_of_raw_data ;
210
223
memcpy (dst , src , size );
211
224
@@ -248,7 +261,7 @@ EFI_STATUS pe_load(void *data, UINTN size, image_t *image)
248
261
249
262
ret = load_sections ((section_header_t * )(pe + 1 ),
250
263
pe -> hdr .number_of_sections ,
251
- data , image );
264
+ data , image , pe -> opt . win . section_alignment );
252
265
if (EFI_ERROR (ret ))
253
266
return ret ;
254
267
@@ -268,7 +281,11 @@ EFI_STATUS pe_unload(image_t *image)
268
281
return EFI_INVALID_PARAMETER ;
269
282
270
283
data = image -> data ;
284
+ #if defined(HOST )
271
285
munmap (data -> addr , data -> size );
286
+ #else
287
+ free (data -> addr );
288
+ #endif
272
289
free (data );
273
290
image -> data = NULL ;
274
291
0 commit comments