print_memory_map.py 2.66 KB
Newer Older
1
2
#!/usr/bin/env python3
#
3
# Copyright (c) 2019-2020, Arm Limited. All rights reserved.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#
# SPDX-License-Identifier: BSD-3-Clause
#

import re
import os
import sys
import operator

# List of folder/map to parse
bl_images = ['bl1', 'bl2', 'bl31']

# List of symbols to search for
blx_symbols = ['__BL1_RAM_START__', '__BL1_RAM_END__',
                '__BL2_END__',
                '__BL31_END__',
                '__TEXT_START__', '__TEXT_END__',
                '__RODATA_START__', '__RODATA_END__',
                '__DATA_START__', '__DATA_END__',
                '__STACKS_START__', '__STACKS_END__',
                '__BSS_END',
25
                '__COHERENT_RAM_START__', '__COHERENT_RAM_END__',
26
27
28
29
30
31
32
33
34
               ]

# Regex to extract address from map file
address_pattern = re.compile(r"\b0x\w*")

# List of found element: [address, symbol, file]
address_list = []

# Get the directory from command line or use a default one
35
inverted_print = True
36
37
if len(sys.argv) >= 2:
    build_dir = sys.argv[1]
38
39
    if len(sys.argv) >= 3:
        inverted_print = sys.argv[2] == '0'
40
41
42
43
44
45
46
47
48
49
else:
    build_dir = 'build/fvp/debug'

# Extract all the required symbols from the map files
for image in bl_images:
    file_path = os.path.join(build_dir, image, '{}.map'.format(image))
    if os.path.isfile(file_path):
        with open (file_path, 'rt') as mapfile:
            for line in mapfile:
                for symbol in blx_symbols:
50
51
52
53
                    # Regex to find symbol definition
                    line_pattern = re.compile(r"\b0x\w*\s*" + symbol + "\s= .")
                    match = line_pattern.search(line)
                    if match:
54
55
56
57
58
59
60
61
                        # Extract address from line
                        match = address_pattern.search(line)
                        if match:
                            address_list.append([match.group(0), symbol, image])

# Sort by address
address_list.sort(key=operator.itemgetter(0))

62
63
64
65
# Invert list for lower address at bottom
if inverted_print:
    address_list = reversed(address_list)

66
# Generate memory view
67
68
print('{:-^93}'.format('Memory Map from: ' + build_dir))
for address in address_list:
69
    if "bl1" in address[2]:
70
        print(address[0], '+{:-^22}+ |{:^22}| |{:^22}|'.format(address[1], '', ''))
71
    elif "bl2" in address[2]:
72
        print(address[0], '|{:^22}| +{:-^22}+ |{:^22}|'.format('', address[1], ''))
73
    elif "bl31" in address[2]:
74
        print(address[0], '|{:^22}| |{:^22}| +{:-^22}+'.format('', '', address[1]))
75
    else:
76
        print(address[0], '|{:^22}| |{:^22}| +{:-^22}+'.format('', '', address[1]))
77

78
79
print('{:^20}{:_^22}   {:_^22}   {:_^22}'.format('', '', '', ''))
print('{:^20}{:^22}   {:^22}   {:^22}'.format('address', 'bl1', 'bl2', 'bl31'))