1#!/usr/bin/env python
2#
3# Copyright 2020 Espressif Systems (Shanghai) PTE LTD
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18# Check placements in this test app for main
19# specified in main/linker.lf
20
21import argparse
22import subprocess
23
24from pyparsing import LineEnd, LineStart, Literal, Optional, Word, alphanums, hexnums
25
26argparser = argparse.ArgumentParser()
27
28argparser.add_argument('objdump')
29argparser.add_argument('elf')
30
31args = argparser.parse_args()
32
33contents = subprocess.check_output([args.objdump, '-t', args.elf]).decode()
34
35
36def check_location(symbol, expected):
37    pattern = (LineStart() + Word(hexnums).setResultsName('address')
38               + Optional(Word(alphanums, exact=1))
39               + Optional(Word(alphanums,exact=1))
40               + Word(alphanums + '._*').setResultsName('actual')
41               + Word(hexnums)
42               + Literal(symbol)
43               + LineEnd())
44
45    try:
46        results = pattern.searchString(contents)[0]
47    except IndexError:
48        raise Exception("check placement fail: '%s' was not found" % (symbol))
49
50    if results.actual != expected:
51        raise Exception("check placement fail: '%s' was placed in '%s', not in '%s'" % (symbol, results.actual, expected))
52
53    print("check placement pass: '%s' was successfully placed in '%s'" % (symbol, results.actual))
54    return int(results.address, 16)
55
56
57# src1:func1 (noflash) - explicit mapping for func2 using 'rtc' scheme
58# should have been dropped since it is unreferenced.
59func1 = check_location('func1', '.iram0.text')
60
61sym1_start = check_location('_sym1_start', '*ABS*')
62sym1_end = check_location('_sym1_end', '*ABS*')
63
64assert func1 >= sym1_start, 'check placement fail: func1 comes before __sym1_start'
65assert func1 < sym1_end, 'check placement fail: func1 comes after __sym1_end'
66assert sym1_start % 9 == 0, '_sym1_start is not aligned as specified in linker fragment'
67assert sym1_end % 12 == 0, '_sym1_end is not aligned as specified in linker fragment'
68print('check placement pass: _sym1_start < func1 < __sym1_end and alignments checked')
69
70# src1:func2 (rtc) - explicit mapping for func2 using 'rtc' scheme
71check_location('func2', '.rtc.text')
72
73# src1 (default) - only func3 in src1 remains that has not been
74# mapped using a different scheme
75check_location('func3', '.flash.text')
76
77check_location('func4', '.iram0.text')
78