xref: /llvm-project/lldb/test/API/python_api/absolute_symbol/TestAbsoluteSymbol.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Test absolute symbols in ELF files to make sure they don't create sections and
3to verify that symbol values and size can still be accessed via SBSymbol APIs.
4"""
5
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9import os
10
11
12class TestAbsoluteSymbol(TestBase):
13    @no_debug_info_test
14    def test_absolute_symbol(self):
15        """
16        Load an ELF file that contains two symbols:
17        - "absolute" which is a symbol with the section SHN_ABS
18        - "main" which is a code symbol in .text
19
20        Index   st_name    st_value           st_size            st_info                             st_other st_shndx Name
21        ======= ---------- ------------------ ------------------ ----------------------------------- -------- -------- ===========================
22        [    0] 0x00000000 0x0000000000000000 0x0000000000000000 0x00 (STB_LOCAL      STT_NOTYPE   ) 0x00            0
23        [    1] 0x00000001 0x0000000000001000 0x0000000000000004 0x12 (STB_GLOBAL     STT_FUNC     ) 0x00            1 main
24        [    2] 0x00000006 0xffffffff80000000 0x0000000000000009 0x10 (STB_GLOBAL     STT_NOTYPE   ) 0x00      SHN_ABS absolute
25
26        We used to create sections for symbols whose section ID was SHN_ABS
27        and this caused problems as the new sections could interfere with
28        with address resolution. Absolute symbols' values are not addresses
29        and should not be treated this way.
30
31        New APIs were added to SBSymbol to allow access to the raw integer
32        value and size of symbols so symbols whose value was not an address
33        could be accessed. Prior to this commit, you could only call:
34
35        SBAddress SBSymbol::GetStartAddress()
36        SBAddress SBSymbol::GetEndAddress()
37
38        If the symbol's value was not an address, you couldn't access the
39        raw value because the above accessors would return invalid SBAddress
40        objects if the value wasn't an address. New APIs were added for this:
41
42        uint64_t SBSymbol::GetValue()
43        uint64_t SBSymbol::GetSize();
44        """
45        src_dir = self.getSourceDir()
46        yaml_path = os.path.join(src_dir, "absolute.yaml")
47        yaml_base, ext = os.path.splitext(yaml_path)
48        obj_path = self.getBuildArtifact("a.out")
49        self.yaml2obj(yaml_path, obj_path)
50
51        # Create a target with the object file we just created from YAML
52        target = self.dbg.CreateTarget(obj_path)
53        self.assertTrue(target, VALID_TARGET)
54
55        module = target.modules[0]
56
57        # Make sure the 'main' symbol is valid and has address values. Also make
58        # sure we can access the raw file address and size via the new APIS.
59        symbol = module.FindSymbol("main")
60        self.assertTrue(symbol.IsValid())
61        self.assertTrue(symbol.GetStartAddress().IsValid())
62        self.assertTrue(symbol.GetEndAddress().IsValid())
63        self.assertEqual(symbol.GetValue(), 0x1000)
64        self.assertEqual(symbol.GetSize(), 0x4)
65
66        # Make sure the 'absolute' symbol is valid and has no address values.
67        # Also make sure we can access the raw file address and size via the new
68        # APIS.
69        symbol = module.FindSymbol("absolute")
70        self.assertTrue(symbol.IsValid())
71        self.assertFalse(symbol.GetStartAddress().IsValid())
72        self.assertFalse(symbol.GetEndAddress().IsValid())
73        self.assertEqual(symbol.GetValue(), 0xFFFFFFFF80000000)
74        self.assertEqual(symbol.GetSize(), 9)
75
76        # Make sure no sections were created for the absolute symbol with a
77        # prefix of ".absolute." followed by the symbol name as they interfere
78        # with address lookups if they are treated like real sections.
79        for section in module.sections:
80            self.assertNotEqual(section.GetName(), ".absolute.absolute")
81