xref: /reactos/sdk/tools/gen_baseaddress.py (revision dc4992ce)
15a9dc5e7SMark Jansen'''
25a9dc5e7SMark JansenPROJECT:     ReactOS baseaddress updater
35a9dc5e7SMark JansenLICENSE:     MIT (https://spdx.org/licenses/MIT)
45a9dc5e7SMark JansenPURPOSE:     Update baseaddresses of all modules
55a9dc5e7SMark JansenCOPYRIGHT:   Copyright 2017,2018 Mark Jansen (mark.jansen@reactos.org)
65a9dc5e7SMark Jansen'''
79f6a1c91SMark Jansen
85e6810d5SThomas Faberfrom __future__ import print_function, absolute_import, division
99f6a1c91SMark Jansen
109f6a1c91SMark JansenUSAGE = """
119f6a1c91SMark JansenThis script will update the baseaddresses of all modules, based on the build output.
129f6a1c91SMark Jansen
139f6a1c91SMark JansenSpecify the build output dir as commandline argument to the script:
149f6a1c91SMark Jansen`python gen_baseaddress.py C:\\Users\\Mark\\reactos\\output-MinGW-i386`
159f6a1c91SMark Jansen
169f6a1c91SMark JansenMultiple directories can be specified:
179f6a1c91SMark Jansen`python gen_baseaddress r:/build/msvc r:/build/gcc`
187377f143STimo Kreuzer
197377f143STimo KreuzerSpecify -64 as first argument to use 64-bit addresses:
207377f143STimo Kreuzer`python gen_baseaddress -64 r:\\build\\msvc-x64`
219f6a1c91SMark Jansen"""
229f6a1c91SMark Jansen
235a9dc5e7SMark Jansenimport os
245a9dc5e7SMark Jansenimport struct
255a9dc5e7SMark Jansenimport sys
266b92dd73SMark Jansen
275a9dc5e7SMark Jansentry:
285a9dc5e7SMark Jansen    import pefile
295a9dc5e7SMark Jansenexcept ImportError:
305e6810d5SThomas Faber    print('# Please install pefile from pip or https://github.com/erocarrera/pefile')
31c5196500SMark Jansen    sys.exit(-1)
325a9dc5e7SMark Jansen
339f6a1c91SMark Jansen
346b92dd73SMark JansenALL_EXTENSIONS = (
35*dc4992ceSKatayama Hirofumi MZ    '.dll', '.acm', '.ax', '.cpl', '.drv', '.ocx', '.ime'
365a9dc5e7SMark Jansen)
375a9dc5e7SMark Jansen
385a9dc5e7SMark JansenPRIORITIES = (
395a9dc5e7SMark Jansen    'ntdll.dll',
405a9dc5e7SMark Jansen    'kernel32.dll',
415a9dc5e7SMark Jansen    'msvcrt.dll',
425a9dc5e7SMark Jansen    'advapi32.dll',
435a9dc5e7SMark Jansen    'gdi32.dll',
445a9dc5e7SMark Jansen    'user32.dll',
455a9dc5e7SMark Jansen    'dhcpcsvc.dll',
465a9dc5e7SMark Jansen    'dnsapi.dll',
475a9dc5e7SMark Jansen    'icmp.dll',
485a9dc5e7SMark Jansen    'iphlpapi.dll',
495a9dc5e7SMark Jansen    'ws2_32.dll',
505a9dc5e7SMark Jansen    'ws2help.dll',
515a9dc5e7SMark Jansen    'shlwapi.dll',
525a9dc5e7SMark Jansen    'rpcrt4.dll',
535a9dc5e7SMark Jansen    'comctl32.dll',
545a9dc5e7SMark Jansen    'ole32.dll',
555a9dc5e7SMark Jansen    'winspool.drv',
565a9dc5e7SMark Jansen    'winmm.dll',
575a9dc5e7SMark Jansen    'comdlg32.dll',
585a9dc5e7SMark Jansen    'shell32.dll',
595a9dc5e7SMark Jansen    'lz32.dll',
605a9dc5e7SMark Jansen    'version.dll',
615a9dc5e7SMark Jansen    'oleaut32.dll',
625a9dc5e7SMark Jansen    'setupapi.dll',
635a9dc5e7SMark Jansen    'mpr.dll',
645a9dc5e7SMark Jansen    'crypt32.dll',
655a9dc5e7SMark Jansen    'wininet.dll',
665a9dc5e7SMark Jansen    'urlmon.dll',
675a9dc5e7SMark Jansen    'psapi.dll',
685a9dc5e7SMark Jansen    'imm32.dll',
695a9dc5e7SMark Jansen    'msvfw32.dll',
705a9dc5e7SMark Jansen    'dbghelp.dll',
715a9dc5e7SMark Jansen    'devmgr.dll',
725a9dc5e7SMark Jansen    'msacm32.dll',
735a9dc5e7SMark Jansen    'netapi32.dll',
745a9dc5e7SMark Jansen    'powrprof.dll',
755a9dc5e7SMark Jansen    'secur32.dll',
765a9dc5e7SMark Jansen    'wintrust.dll',
775a9dc5e7SMark Jansen    'avicap32.dll',
785a9dc5e7SMark Jansen    'cabinet.dll',
795a9dc5e7SMark Jansen    'dsound.dll',
805a9dc5e7SMark Jansen    'glu32.dll',
815a9dc5e7SMark Jansen    'opengl32.dll',
825a9dc5e7SMark Jansen    'riched20.dll',
835a9dc5e7SMark Jansen    'userenv.dll',
845a9dc5e7SMark Jansen    'uxtheme.dll',
855a9dc5e7SMark Jansen    'cryptui.dll',
865a9dc5e7SMark Jansen    'csrsrv.dll',
875a9dc5e7SMark Jansen    'basesrv.dll',
885a9dc5e7SMark Jansen    'winsrv.dll',
895a9dc5e7SMark Jansen    'dplayx.dll',
905a9dc5e7SMark Jansen    'gdiplus.dll',
915a9dc5e7SMark Jansen    'msimg32.dll',
925a9dc5e7SMark Jansen    'mswsock.dll',
935a9dc5e7SMark Jansen    'oledlg.dll',
945a9dc5e7SMark Jansen    'rasapi32.dll',
955a9dc5e7SMark Jansen    'rsaenh.dll',
965a9dc5e7SMark Jansen    'samlib.dll',
975a9dc5e7SMark Jansen    'sensapi.dll',
985a9dc5e7SMark Jansen    'sfc_os.dll',
995a9dc5e7SMark Jansen    'snmpapi.dll',
1005a9dc5e7SMark Jansen    'spoolss.dll',
1015a9dc5e7SMark Jansen    'usp10.dll',
1025a9dc5e7SMark Jansen)
1035a9dc5e7SMark Jansen
1045a9dc5e7SMark JansenEXCLUDE = (
1055a9dc5e7SMark Jansen    'bmfd.dll',
1065a9dc5e7SMark Jansen    'bootvid.dll',
1079efeefd5STimo Kreuzer    'framebuf.dll',
1085a9dc5e7SMark Jansen    'ftfd.dll',
1099efeefd5STimo Kreuzer    'genincdata.dll',
1105a9dc5e7SMark Jansen    'hal.dll',
1115a9dc5e7SMark Jansen    'halaacpi.dll',
1125a9dc5e7SMark Jansen    'halacpi.dll',
1135a9dc5e7SMark Jansen    'halapic.dll',
1145a9dc5e7SMark Jansen    'kbda1.dll',
1155a9dc5e7SMark Jansen    'kbda2.dll',
1165a9dc5e7SMark Jansen    'kbda3.dll',
1175a9dc5e7SMark Jansen    'kbdal.dll',
1185a9dc5e7SMark Jansen    'kbdarme.dll',
1195a9dc5e7SMark Jansen    'kbdarmw.dll',
1205a9dc5e7SMark Jansen    'kbdaze.dll',
1215a9dc5e7SMark Jansen    'kbdazel.dll',
1225a9dc5e7SMark Jansen    'kbdbe.dll',
1235a9dc5e7SMark Jansen    'kbdbga.dll',
1240fb580a8SJoachim Henze    'kbdbgm.dll',
1255a9dc5e7SMark Jansen    'kbdbgt.dll',
1265a9dc5e7SMark Jansen    'kbdblr.dll',
1275a9dc5e7SMark Jansen    'kbdbr.dll',
1282220e566SKatayama Hirofumi MZ    'kbdbu.dll',
1295a9dc5e7SMark Jansen    'kbdbur.dll',
1305a9dc5e7SMark Jansen    'kbdcan.dll',
1315a9dc5e7SMark Jansen    'kbdcr.dll',
1325a9dc5e7SMark Jansen    'kbdcz.dll',
1335a9dc5e7SMark Jansen    'kbdcz1.dll',
1345a9dc5e7SMark Jansen    'kbdda.dll',
1355a9dc5e7SMark Jansen    'kbddv.dll',
1360fb580a8SJoachim Henze    'kbdeo.dll',
1370fb580a8SJoachim Henze    'kbdes.dll',
1385a9dc5e7SMark Jansen    'kbdest.dll',
1395a9dc5e7SMark Jansen    'kbdfc.dll',
1405a9dc5e7SMark Jansen    'kbdfi.dll',
1415a9dc5e7SMark Jansen    'kbdfr.dll',
1425a9dc5e7SMark Jansen    'kbdgeo.dll',
1435a9dc5e7SMark Jansen    'kbdgerg.dll',
1445a9dc5e7SMark Jansen    'kbdgneo.dll',
1455a9dc5e7SMark Jansen    'kbdgr.dll',
1462220e566SKatayama Hirofumi MZ    'kbdgr1.dll',
1470fb580a8SJoachim Henze    'kbdgrist.dll',
1485a9dc5e7SMark Jansen    'kbdhe.dll',
1495a9dc5e7SMark Jansen    'kbdheb.dll',
1505a9dc5e7SMark Jansen    'kbdhu.dll',
1515a9dc5e7SMark Jansen    'kbdic.dll',
1525a9dc5e7SMark Jansen    'kbdinasa.dll',
1535a9dc5e7SMark Jansen    'kbdinben.dll',
1545a9dc5e7SMark Jansen    'kbdindev.dll',
1555a9dc5e7SMark Jansen    'kbdinguj.dll',
1565a9dc5e7SMark Jansen    'kbdinmal.dll',
1575a9dc5e7SMark Jansen    'kbdir.dll',
1585a9dc5e7SMark Jansen    'kbdit.dll',
1590fb580a8SJoachim Henze    'kbdja.dll',
1602220e566SKatayama Hirofumi MZ    'kbdjpn.dll',
1615a9dc5e7SMark Jansen    'kbdkaz.dll',
1620fb580a8SJoachim Henze    'kbdko.dll',
1632220e566SKatayama Hirofumi MZ    'kbdkor.dll',
1645a9dc5e7SMark Jansen    'kbdla.dll',
1655a9dc5e7SMark Jansen    'kbdlt1.dll',
1665a9dc5e7SMark Jansen    'kbdlv.dll',
1675a9dc5e7SMark Jansen    'kbdmac.dll',
1685a9dc5e7SMark Jansen    'kbdne.dll',
1695a9dc5e7SMark Jansen    'kbdno.dll',
1705a9dc5e7SMark Jansen    'kbdpl.dll',
1715a9dc5e7SMark Jansen    'kbdpl1.dll',
1725a9dc5e7SMark Jansen    'kbdpo.dll',
1735a9dc5e7SMark Jansen    'kbdro.dll',
1740fb580a8SJoachim Henze    'kbdrost.dll',
1755a9dc5e7SMark Jansen    'kbdru.dll',
1765a9dc5e7SMark Jansen    'kbdru1.dll',
1770fb580a8SJoachim Henze    'kbdsf.dll',
1785a9dc5e7SMark Jansen    'kbdsg.dll',
1790fb580a8SJoachim Henze    'kbdsk.dll',
1800fb580a8SJoachim Henze    'kbdsk1.dll',
1812220e566SKatayama Hirofumi MZ    'kbdsl.dll',
1822220e566SKatayama Hirofumi MZ    'kbdsl1.dll',
1832220e566SKatayama Hirofumi MZ    'kbdsp.dll',
1845a9dc5e7SMark Jansen    'kbdsw.dll',
1855a9dc5e7SMark Jansen    'kbdtat.dll',
1865a9dc5e7SMark Jansen    'kbdth0.dll',
1875a9dc5e7SMark Jansen    'kbdth1.dll',
1885a9dc5e7SMark Jansen    'kbdth2.dll',
1895a9dc5e7SMark Jansen    'kbdth3.dll',
1905a9dc5e7SMark Jansen    'kbdtuf.dll',
1915a9dc5e7SMark Jansen    'kbdtuq.dll',
1925a9dc5e7SMark Jansen    'kbduk.dll',
1935a9dc5e7SMark Jansen    'kbdur.dll',
1945a9dc5e7SMark Jansen    'kbdurs.dll',
1955a9dc5e7SMark Jansen    'kbdus.dll',
1965a9dc5e7SMark Jansen    'kbdusa.dll',
1975a9dc5e7SMark Jansen    'kbdusl.dll',
1985a9dc5e7SMark Jansen    'kbdusr.dll',
1995a9dc5e7SMark Jansen    'kbdusx.dll',
2005a9dc5e7SMark Jansen    'kbduzb.dll',
2015a9dc5e7SMark Jansen    'kbdvntc.dll',
2025a9dc5e7SMark Jansen    'kbdycc.dll',
2035a9dc5e7SMark Jansen    'kbdycl.dll',
2045a9dc5e7SMark Jansen    'kdcom.dll',
2055a9dc5e7SMark Jansen    'kdvbox.dll',
2065a9dc5e7SMark Jansen    'vgaddi.dll',
2075a9dc5e7SMark Jansen    'dllexport_test_dll1.dll',
2085a9dc5e7SMark Jansen    'dllexport_test_dll2.dll',
2095a9dc5e7SMark Jansen    'dllimport_test.dll',
2102b7246fdSTimo Kreuzer    'localspl_apitest.dll',
2115a9dc5e7SMark Jansen    'MyEventProvider.dll',
2122b7246fdSTimo Kreuzer    'redirtest1.dll',
2132b7246fdSTimo Kreuzer    'redirtest2.dll',
2147377f143STimo Kreuzer    'win32u_2k3sp2.dll',
2157377f143STimo Kreuzer    'win32u_vista.dll',
2167377f143STimo Kreuzer    'win32u_xpsp2.dll',
2177377f143STimo Kreuzer    'testvdd.dll',
2185a9dc5e7SMark Jansen)
2195a9dc5e7SMark Jansen
2201e568ce6SMark JansenIMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b
2211e568ce6SMark JansenIMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b
2221e568ce6SMark Jansen
2231e568ce6SMark JansenIMAGE_TYPES = {
2241e568ce6SMark Jansen    IMAGE_NT_OPTIONAL_HDR32_MAGIC: 0,
2251e568ce6SMark Jansen    IMAGE_NT_OPTIONAL_HDR64_MAGIC: 0
2261e568ce6SMark Jansen}
2271e568ce6SMark Jansen
2287377f143STimo KreuzerIS_64_BIT = False
2295a9dc5e7SMark Jansen
230c5196500SMark Jansendef size_of_image(filename):
2317377f143STimo Kreuzer    if IS_64_BIT:
2327377f143STimo Kreuzer        return 0xFE0000 # This results in 0x1000000 / 16MB space
2335a9dc5e7SMark Jansen    with open(filename, 'rb') as fin:
234599843acSTimo Kreuzer        if fin.read(2) != b'MZ':
2355e6810d5SThomas Faber            print(filename, 'No dos header found!')
2365a9dc5e7SMark Jansen            return 0
2375a9dc5e7SMark Jansen        fin.seek(0x3C)
2385a9dc5e7SMark Jansen        e_lfanew = struct.unpack('i', fin.read(4))[0]
2395a9dc5e7SMark Jansen        fin.seek(e_lfanew)
240599843acSTimo Kreuzer        if fin.read(4) != b'PE\0\0':
2415e6810d5SThomas Faber            print(filename, 'No PE header found!')
2425a9dc5e7SMark Jansen            return 0
2435a9dc5e7SMark Jansen        fin.seek(e_lfanew + 0x18)
2445a9dc5e7SMark Jansen        pe_magic = struct.unpack('h', fin.read(2))[0]
2451e568ce6SMark Jansen        if pe_magic in IMAGE_TYPES.keys():
2461e568ce6SMark Jansen            IMAGE_TYPES[pe_magic] += 1
2475a9dc5e7SMark Jansen            fin.seek(e_lfanew + 0x50)
2485a9dc5e7SMark Jansen            pe_size_of_image = struct.unpack('i', fin.read(4))[0]
2495a9dc5e7SMark Jansen            return pe_size_of_image
2501e568ce6SMark Jansen        print(filename, 'Unknown executable format!')
2511e568ce6SMark Jansen        return 0
2525a9dc5e7SMark Jansen
2535a9dc5e7SMark Jansen
2546b92dd73SMark Jansenclass Module(object):
2551e568ce6SMark Jansen    def __init__(self, name, address, size, filename):
2566b92dd73SMark Jansen        self._name = name
2576b92dd73SMark Jansen        self.address = address
2586b92dd73SMark Jansen        self.size = size
2596b92dd73SMark Jansen        self._reserved = address != 0
2601e568ce6SMark Jansen        self.filename = filename
2616b92dd73SMark Jansen
262c5196500SMark Jansen    def gen_baseaddress(self, output_file):
2636b92dd73SMark Jansen        name, ext = os.path.splitext(self._name)
2645a9dc5e7SMark Jansen        postfix = ''
2656b92dd73SMark Jansen        if ext in('.acm', '.drv') and self._name != 'winspool.drv':
2666b92dd73SMark Jansen            name = self._name
2676b92dd73SMark Jansen        if name == 'ntdll':
2686b92dd73SMark Jansen            postfix = ' # should be above 0x%08x' % self.address
2696b92dd73SMark Jansen        elif self._reserved:
2706b92dd73SMark Jansen            postfix = ' # reserved'
271c5196500SMark Jansen        output_file.write('set(baseaddress_%-30s 0x%08x)%s\n' % (name, self.address, postfix))
2726b92dd73SMark Jansen
2736b92dd73SMark Jansen    def end(self):
2746b92dd73SMark Jansen        return self.address + self.size
2756b92dd73SMark Jansen
2766b92dd73SMark Jansen    def __repr__(self):
2776b92dd73SMark Jansen        return '%s (0x%08x - 0x%08x)' % (self._name, self.address, self.end())
2786b92dd73SMark Jansen
2796b92dd73SMark Jansenclass MemoryLayout(object):
2806b92dd73SMark Jansen    def __init__(self, startaddress):
2816b92dd73SMark Jansen        self.addresses = []
2826b92dd73SMark Jansen        self.found = {}
2836b92dd73SMark Jansen        self.reserved = {}
2846b92dd73SMark Jansen        self.initial = startaddress
2856b92dd73SMark Jansen        self.start_at = 0
2866b92dd73SMark Jansen        self.module_padding = 0x2000
2876b92dd73SMark Jansen
2886b92dd73SMark Jansen    def add_reserved(self, name, address):
2896b92dd73SMark Jansen        self.reserved[name] = (address, 0)
2906b92dd73SMark Jansen
2916b92dd73SMark Jansen    def add(self, filename, name):
292c5196500SMark Jansen        size = size_of_image(filename)
2936b92dd73SMark Jansen        addr = 0
2946b92dd73SMark Jansen        if name in self.found:
2956b92dd73SMark Jansen            return  # Assume duplicate files (rshell, ...) are 1:1 copies
2966b92dd73SMark Jansen        if name in self.reserved:
2976b92dd73SMark Jansen            addr = self.reserved[name][0]
2986b92dd73SMark Jansen            self.reserved[name] = (addr, size)
2991e568ce6SMark Jansen        self.found[name] = Module(name, addr, size, filename)
3006b92dd73SMark Jansen
3016b92dd73SMark Jansen    def _next_address(self, size):
3026b92dd73SMark Jansen        if self.start_at:
3037377f143STimo Kreuzer            addr = (self.start_at - size - self.module_padding - 0xffff) & 0xffffffffffff0000
3046b92dd73SMark Jansen            self.start_at = addr
3055a9dc5e7SMark Jansen        else:
3066b92dd73SMark Jansen            addr = self.start_at = self.initial
3076b92dd73SMark Jansen        return addr
3086b92dd73SMark Jansen
3096b92dd73SMark Jansen    def next_address(self, size):
3106b92dd73SMark Jansen        while True:
3116b92dd73SMark Jansen            current_start = self._next_address(size)
3126b92dd73SMark Jansen            current_end = current_start + size + self.module_padding
3136b92dd73SMark Jansen            # Is there overlap with reserved modules?
3145e6810d5SThomas Faber            for key, reserved in self.reserved.items():
3156b92dd73SMark Jansen                res_start = reserved[0]
3166b92dd73SMark Jansen                res_end = res_start + reserved[1] + self.module_padding
3176b92dd73SMark Jansen                if (res_start <= current_start <= res_end) or \
3186b92dd73SMark Jansen                   (res_start <= current_end <= res_end) or \
3196b92dd73SMark Jansen                   (current_start < res_start and current_end > res_end):
3206b92dd73SMark Jansen                    # We passed this reserved item, we can remove it now
3216b92dd73SMark Jansen                    self.start_at = min(res_start, current_start)
3226b92dd73SMark Jansen                    del self.reserved[key]
3236b92dd73SMark Jansen                    current_start = 0
3246b92dd73SMark Jansen                    break
3256b92dd73SMark Jansen            # No overlap with a reserved module?
3266b92dd73SMark Jansen            if current_start:
3276b92dd73SMark Jansen                return current_start
3286b92dd73SMark Jansen
3296b92dd73SMark Jansen    def update(self, priorities):
3306b92dd73SMark Jansen        # sort addresses, should only contain reserved modules at this point!
3315e6810d5SThomas Faber        for key, reserved in self.reserved.items():
3326b92dd73SMark Jansen            assert reserved[1] != 0, key
3336b92dd73SMark Jansen        for curr in priorities:
3346b92dd73SMark Jansen            if not curr in self.found:
3355e6810d5SThomas Faber                print('# Did not find', curr, '!')
3366b92dd73SMark Jansen            else:
3376b92dd73SMark Jansen                obj = self.found[curr]
3386b92dd73SMark Jansen                del self.found[curr]
3396b92dd73SMark Jansen                if not obj.address:
3406b92dd73SMark Jansen                    obj.address = self.next_address(obj.size)
3416b92dd73SMark Jansen                self.addresses.append(obj)
3426b92dd73SMark Jansen        # We handled all known modules now, run over the rest we found
3436b92dd73SMark Jansen        for key in sorted(self.found):
3446b92dd73SMark Jansen            obj = self.found[key]
3456b92dd73SMark Jansen            obj.address = self.next_address(obj.size)
3466b92dd73SMark Jansen            self.addresses.append(obj)
3476b92dd73SMark Jansen
348c5196500SMark Jansen    def gen_baseaddress(self, output_file):
3496b92dd73SMark Jansen        for obj in self.addresses:
350c5196500SMark Jansen            obj.gen_baseaddress(output_file)
3515a9dc5e7SMark Jansen
352c5196500SMark Jansendef get_target_file(ntdll_path):
3531e568ce6SMark Jansen    if 'pefile' in globals():
3541e568ce6SMark Jansen        ntdll_pe = pefile.PE(ntdll_path, fast_load=True)
355599843acSTimo Kreuzer        names = [sect.Name.strip(b'\0') for sect in ntdll_pe.sections]
356599843acSTimo Kreuzer        count = b'|'.join(names).count(b'/')
3577377f143STimo Kreuzer        if IS_64_BIT:
3587377f143STimo Kreuzer            return 'baseaddress64.cmake'
3597377f143STimo Kreuzer        elif b'.rossym' in names:
360c5196500SMark Jansen            return 'baseaddress.cmake'
3611e568ce6SMark Jansen        elif count == 0:
362c5196500SMark Jansen            return 'baseaddress_msvc.cmake'
3631e568ce6SMark Jansen        elif count > 3:
364c5196500SMark Jansen            return 'baseaddress_dwarf.cmake'
3651e568ce6SMark Jansen        else:
366c5196500SMark Jansen            assert False, "Unknown"
367c5196500SMark Jansen    return None
3681e568ce6SMark Jansen
3695a9dc5e7SMark Jansendef run_dir(target):
3707377f143STimo Kreuzer    if IS_64_BIT:
3717377f143STimo Kreuzer        layout = MemoryLayout(0x7FFB7000000)
3727377f143STimo Kreuzer    else:
3736b92dd73SMark Jansen        layout = MemoryLayout(0x7c920000)
3746b92dd73SMark Jansen        layout.add_reserved('user32.dll', 0x77a20000)
375c5196500SMark Jansen    IMAGE_TYPES[IMAGE_NT_OPTIONAL_HDR64_MAGIC] = 0
376c5196500SMark Jansen    IMAGE_TYPES[IMAGE_NT_OPTIONAL_HDR32_MAGIC] = 0
3775a9dc5e7SMark Jansen    for root, _, files in os.walk(target):
3786b92dd73SMark Jansen        for dll in [filename for filename in files if filename.endswith(ALL_EXTENSIONS)]:
3795a9dc5e7SMark Jansen            if not dll in EXCLUDE and not dll.startswith('api-ms-win-'):
3806b92dd73SMark Jansen                layout.add(os.path.join(root, dll), dll)
3811e568ce6SMark Jansen    ntdll_path = layout.found['ntdll.dll'].filename
382c5196500SMark Jansen    target_file = get_target_file(ntdll_path)
383c5196500SMark Jansen    if target_file:
384c5196500SMark Jansen        target_dir = os.path.realpath(os.path.dirname(os.path.dirname(__file__)))
385c5196500SMark Jansen        target_path = os.path.join(target_dir, 'cmake', target_file)
386c5196500SMark Jansen        output_file = open(target_path, "w")
387c5196500SMark Jansen    else:
388c5196500SMark Jansen        output_file = sys.stdout
389c5196500SMark Jansen    with output_file:
390c5196500SMark Jansen        output_file.write('# Generated from {}\n'.format(target))
391c5196500SMark Jansen        output_file.write('# Generated by sdk/tools/gen_baseaddress.py\n\n')
3926b92dd73SMark Jansen        layout.update(PRIORITIES)
393c5196500SMark Jansen        layout.gen_baseaddress(output_file)
3945a9dc5e7SMark Jansen
3951e568ce6SMark Jansendef main():
3961e568ce6SMark Jansen    dirs = sys.argv[1:]
3977377f143STimo Kreuzer
3987377f143STimo Kreuzer    if len(dirs) > 0 and dirs[0] == '-64':
3997377f143STimo Kreuzer        print('Using 64-bit addresses')
4007377f143STimo Kreuzer        global IS_64_BIT
4017377f143STimo Kreuzer        IS_64_BIT = True
4027377f143STimo Kreuzer        dirs = sys.argv[2:]
4035a9dc5e7SMark Jansen    if len(dirs) < 1:
4045a9dc5e7SMark Jansen        trydir = os.getcwd()
4059f6a1c91SMark Jansen        print(USAGE)
4069f6a1c91SMark Jansen        print('No path specified, trying the working directory: ', trydir)
4075a9dc5e7SMark Jansen        dirs = [trydir]
4085a9dc5e7SMark Jansen    for onedir in dirs:
4099f6a1c91SMark Jansen        if onedir.lower() in ['-help', '/help', '/h', '-h', '/?', '-?']:
4109f6a1c91SMark Jansen            print(USAGE)
4119f6a1c91SMark Jansen        else:
4125a9dc5e7SMark Jansen            run_dir(onedir)
4135a9dc5e7SMark Jansen
4141e568ce6SMark Jansen
4155a9dc5e7SMark Jansenif __name__ == '__main__':
4161e568ce6SMark Jansen    main()
417