blob: ef53f7ed06632a74a2f87bd9346e08048eaf473e [file] [log] [blame]
Louis Mayencourt3dea1a52019-10-14 14:09:40 +01001#!/usr/bin/env python3
2#
Yann Gautier522acd22022-04-04 18:22:45 +02003# Copyright (c) 2019-2022, Arm Limited and Contributors. All rights reserved.
Louis Mayencourt3dea1a52019-10-14 14:09:40 +01004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8import re
9import os
10import sys
11import operator
12
13# List of folder/map to parse
14bl_images = ['bl1', 'bl2', 'bl31']
15
16# List of symbols to search for
17blx_symbols = ['__BL1_RAM_START__', '__BL1_RAM_END__',
18 '__BL2_END__',
19 '__BL31_END__',
Yann Gautier522acd22022-04-04 18:22:45 +020020 '__RO_START__', '__RO_END_UNALIGNED__', '__RO_END__',
Louis Mayencourt3dea1a52019-10-14 14:09:40 +010021 '__TEXT_START__', '__TEXT_END__',
Yann Gautier522acd22022-04-04 18:22:45 +020022 '__TEXT_RESIDENT_START__', '__TEXT_RESIDENT_END__',
Louis Mayencourt3dea1a52019-10-14 14:09:40 +010023 '__RODATA_START__', '__RODATA_END__',
24 '__DATA_START__', '__DATA_END__',
25 '__STACKS_START__', '__STACKS_END__',
Yann Gautier522acd22022-04-04 18:22:45 +020026 '__BSS_START__', '__BSS_END__',
Louis Mayencourtc1c2bf72020-02-13 08:21:34 +000027 '__COHERENT_RAM_START__', '__COHERENT_RAM_END__',
Yann Gautier522acd22022-04-04 18:22:45 +020028 '__CPU_OPS_START__', '__CPU_OPS_END__',
29 '__FCONF_POPULATOR_START__', '__FCONF_POPULATOR_END__',
30 '__GOT_START__', '__GOT_END__',
31 '__PARSER_LIB_DESCS_START__', '__PARSER_LIB_DESCS_END__',
32 '__PMF_TIMESTAMP_START__', '__PMF_TIMESTAMP_END__',
33 '__PMF_SVC_DESCS_START__', '__PMF_SVC_DESCS_END__',
34 '__RELA_START__', '__RELA_END__',
35 '__RT_SVC_DESCS_START__', '__RT_SVC_DESCS_END__',
36 '__BASE_XLAT_TABLE_START__', '__BASE_XLAT_TABLE_END__',
37 '__XLAT_TABLE_START__', '__XLAT_TABLE_END__',
Louis Mayencourt3dea1a52019-10-14 14:09:40 +010038 ]
39
40# Regex to extract address from map file
41address_pattern = re.compile(r"\b0x\w*")
42
43# List of found element: [address, symbol, file]
44address_list = []
45
46# Get the directory from command line or use a default one
Louis Mayencourtc1c2bf72020-02-13 08:21:34 +000047inverted_print = True
Louis Mayencourt3dea1a52019-10-14 14:09:40 +010048if len(sys.argv) >= 2:
49 build_dir = sys.argv[1]
Louis Mayencourtc1c2bf72020-02-13 08:21:34 +000050 if len(sys.argv) >= 3:
51 inverted_print = sys.argv[2] == '0'
Louis Mayencourt3dea1a52019-10-14 14:09:40 +010052else:
53 build_dir = 'build/fvp/debug'
54
Yann Gautier522acd22022-04-04 18:22:45 +020055max_len = max(len(word) for word in blx_symbols) + 2
56if (max_len % 2) != 0:
57 max_len += 1
58
Louis Mayencourt3dea1a52019-10-14 14:09:40 +010059# Extract all the required symbols from the map files
60for image in bl_images:
61 file_path = os.path.join(build_dir, image, '{}.map'.format(image))
62 if os.path.isfile(file_path):
63 with open (file_path, 'rt') as mapfile:
64 for line in mapfile:
65 for symbol in blx_symbols:
Yann Gautier522acd22022-04-04 18:22:45 +020066 skip_symbol = 0
Louis Mayencourtc1c2bf72020-02-13 08:21:34 +000067 # Regex to find symbol definition
68 line_pattern = re.compile(r"\b0x\w*\s*" + symbol + "\s= .")
69 match = line_pattern.search(line)
70 if match:
Louis Mayencourt3dea1a52019-10-14 14:09:40 +010071 # Extract address from line
72 match = address_pattern.search(line)
73 if match:
Yann Gautier522acd22022-04-04 18:22:45 +020074 if '_END__' in symbol:
75 sym_start = symbol.replace('_END__', '_START__')
76 if [match.group(0), sym_start, image] in address_list:
77 address_list.remove([match.group(0), sym_start, image])
78 skip_symbol = 1
79 if skip_symbol == 0:
80 address_list.append([match.group(0), symbol, image])
Louis Mayencourt3dea1a52019-10-14 14:09:40 +010081
82# Sort by address
83address_list.sort(key=operator.itemgetter(0))
84
Louis Mayencourtc1c2bf72020-02-13 08:21:34 +000085# Invert list for lower address at bottom
86if inverted_print:
87 address_list = reversed(address_list)
88
Louis Mayencourt3dea1a52019-10-14 14:09:40 +010089# Generate memory view
Yann Gautier522acd22022-04-04 18:22:45 +020090print(('{:-^%d}' % (max_len * 3 + 20 + 7)).format('Memory Map from: ' + build_dir))
Louis Mayencourtc1c2bf72020-02-13 08:21:34 +000091for address in address_list:
Louis Mayencourt3dea1a52019-10-14 14:09:40 +010092 if "bl1" in address[2]:
Yann Gautier522acd22022-04-04 18:22:45 +020093 print(address[0], ('+{:-^%d}+ |{:^%d}| |{:^%d}|' % (max_len, max_len, max_len)).format(address[1], '', ''))
Louis Mayencourt3dea1a52019-10-14 14:09:40 +010094 elif "bl2" in address[2]:
Yann Gautier522acd22022-04-04 18:22:45 +020095 print(address[0], ('|{:^%d}| +{:-^%d}+ |{:^%d}|' % (max_len, max_len, max_len)).format('', address[1], ''))
Louis Mayencourt3dea1a52019-10-14 14:09:40 +010096 elif "bl31" in address[2]:
Yann Gautier522acd22022-04-04 18:22:45 +020097 print(address[0], ('|{:^%d}| |{:^%d}| +{:-^%d}+' % (max_len, max_len, max_len)).format('', '', address[1]))
Louis Mayencourt3dea1a52019-10-14 14:09:40 +010098 else:
Yann Gautier522acd22022-04-04 18:22:45 +020099 print(address[0], ('|{:^%d}| |{:^%d}| +{:-^%d}+' % (max_len, max_len, max_len)).format('', '', address[1]))
Louis Mayencourt3dea1a52019-10-14 14:09:40 +0100100
Yann Gautier522acd22022-04-04 18:22:45 +0200101print(('{:^20}{:_^%d} {:_^%d} {:_^%d}' % (max_len, max_len, max_len)).format('', '', '', ''))
102print(('{:^20}{:^%d} {:^%d} {:^%d}' % (max_len, max_len, max_len)).format('address', 'bl1', 'bl2', 'bl31'))