2 PROJECT: ReactOS baseaddress updater
3 LICENSE: MIT (https://spdx.org/licenses/MIT)
4 PURPOSE: Update baseaddresses of all modules
5 COPYRIGHT: Copyright 2017,2018 Mark Jansen (mark.jansen@reactos.org)
8 # FIXME: user32 always at 0x77a20000
16 print '# Please install pefile from pip or https://github.com/erocarrera/pefile'
17 print '# Using fallback'
25 '.acm', '.ax', '.cpl', '.drv', '.ocx'
189 'dllexport_test_dll1.dll',
190 'dllexport_test_dll2.dll',
191 'dllimport_test.dll',
192 'MyEventProvider.dll',
193 'w32kdll_2k3sp2.dll',
199 def size_of_image_fallback(filename
):
200 with
open(filename
, 'rb') as fin
:
201 if fin
.read(2) != 'MZ':
202 print filename
, 'No dos header found!'
205 e_lfanew
= struct
.unpack('i', fin
.read(4))[0]
207 if fin
.read(4) != 'PE\0\0':
208 print filename
, 'No PE header found!'
210 fin
.seek(e_lfanew
+ 0x18)
211 pe_magic
= struct
.unpack('h', fin
.read(2))[0]
212 if pe_magic
!= 0x10b:
213 print filename
, 'is not a 32 bit exe!'
215 fin
.seek(e_lfanew
+ 0x50)
216 pe_size_of_image
= struct
.unpack('i', fin
.read(4))[0]
217 return pe_size_of_image
219 def size_of_image(filename
):
220 if 'pefile' in globals():
221 return pefile
.PE(filename
, fast_load
=True).OPTIONAL_HEADER
.SizeOfImage
222 return size_of_image_fallback(filename
)
224 def onefile(current_address
, filename
, size
):
225 name
, ext
= os
.path
.splitext(filename
)
227 if ext
in('.acm', '.drv') and filename
!= 'winspool.drv':
229 if current_address
== 0:
230 current_address
= 0x7c920000
231 postfix
= ' # should be above 0x%08x' % current_address
233 current_address
= (current_address
- size
- 0x2000 - 0xffff) & 0xffff0000
234 print 'set(baseaddress_%-30s 0x%08x)%s' % (name
, current_address
, postfix
)
235 return current_address
238 print '# Generated from', target
239 print '# Generated by sdk/tools/gen_baseaddress.py'
242 for root
, _
, files
in os
.walk(target
):
243 for dll
in [filename
for filename
in files
if filename
.endswith(DLL_EXTENSIONS
)]:
244 if not dll
in EXCLUDE
and not dll
.startswith('api-ms-win-'):
245 found_dlls
[dll
] = size_of_image(os
.path
.join(root
, dll
))
246 extrafiles
= [filename
for filename
in files
if filename
.endswith(OTHER_EXTENSIONS
)]
247 for extrafile
in extrafiles
:
248 if not extrafile
in EXCLUDE
:
249 found_files
[extrafile
] = size_of_image(os
.path
.join(root
, extrafile
))
252 for curr
in PRIORITIES
:
253 if curr
in found_dlls
:
254 current_address
= onefile(current_address
, curr
, found_dlls
[curr
])
256 elif curr
in found_files
:
257 current_address
= onefile(current_address
, curr
, found_files
[curr
])
258 del found_files
[curr
]
260 print '# Did not find', curr
, '!'
263 for curr
in sorted(found_dlls
):
264 current_address
= onefile(current_address
, curr
, found_dlls
[curr
])
265 print '# Extra files'
266 for curr
in sorted(found_files
):
267 current_address
= onefile(current_address
, curr
, found_files
[curr
])
272 print '# No path specified, trying', trydir
277 if __name__
== '__main__':