Skip to content

Commit 7b596ce

Browse files
authored
[lldb] Fix ObjectFileJSON to section addresses. (#129648)
ObjectFileJSON sections didn't work, they were set to zero all of the time. Fixed the bug and fixed the test to ensure it was testing real values.
1 parent ed5cd8d commit 7b596ce

File tree

3 files changed

+65
-23
lines changed

3 files changed

+65
-23
lines changed

lldb/source/Core/Section.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ bool fromJSON(const llvm::json::Value &value,
690690
lldb_private::JSONSection &section, llvm::json::Path path) {
691691
llvm::json::ObjectMapper o(value, path);
692692
return o && o.map("name", section.name) && o.map("type", section.type) &&
693-
o.map("size", section.address) && o.map("size", section.size);
693+
o.map("address", section.address) && o.map("size", section.size);
694694
}
695695

696696
bool fromJSON(const llvm::json::Value &value, lldb::SectionType &type,

lldb/source/Plugins/ObjectFile/JSON/ObjectFileJSON.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,16 @@ void ObjectFileJSON::CreateSections(SectionList &unified_section_list) {
182182
lldb::user_id_t id = 1;
183183
for (const auto &section : m_sections) {
184184
auto section_sp = std::make_shared<Section>(
185-
GetModule(), this, id++, ConstString(section.name),
186-
section.type.value_or(eSectionTypeCode), 0, section.size.value_or(0), 0,
187-
section.size.value_or(0), /*log2align*/ 0, /*flags*/ 0);
185+
GetModule(), this,
186+
/*sect_id=*/id++,
187+
/*name=*/ConstString(section.name),
188+
/*sect_type=*/section.type.value_or(eSectionTypeCode),
189+
/*file_vm_addr=*/section.address.value_or(0),
190+
/*vm_size=*/section.size.value_or(0),
191+
/*file_offset=*/0,
192+
/*file_size=*/0,
193+
/*log2align=*/0,
194+
/*flags=*/0);
188195
m_sections_up->AddSection(section_sp);
189196
unified_section_list.AddSection(section_sp);
190197
}

lldb/test/API/functionalities/json/object-file/TestObjectFileJSON.py

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ def test_module(self):
5959

6060
module = target.AddModule(self.toModuleSpec(json_object_file_b))
6161
self.assertFalse(module.IsValid())
62-
62+
TEXT_file_addr = 0x100000000
63+
DATA_file_addr = 0x100001000
64+
foo_file_addr = TEXT_file_addr + 0x100
65+
bar_file_addr = DATA_file_addr + 0x10
66+
TEXT_size = 0x222
67+
DATA_size = 0x333
68+
foo_size = 0x11
69+
bar_size = 0x22
70+
slide = 0x100000000
6371
data = {
6472
"triple": target.GetTriple(),
6573
"uuid": str(uuid.uuid4()),
@@ -68,16 +76,29 @@ def test_module(self):
6876
{
6977
"name": "__TEXT",
7078
"type": "code",
71-
"address": 0,
72-
"size": 0x222,
79+
"address": TEXT_file_addr,
80+
"size": TEXT_size,
81+
},
82+
{
83+
"name": "__DATA",
84+
"type": "code",
85+
"address": DATA_file_addr,
86+
"size": DATA_size,
7387
}
7488
],
7589
"symbols": [
7690
{
7791
"name": "foo",
78-
"address": 0x100,
79-
"size": 0x11,
80-
}
92+
"type": "code",
93+
"address": foo_file_addr,
94+
"size": foo_size,
95+
},
96+
{
97+
"name": "bar",
98+
"type": "data",
99+
"address": bar_file_addr,
100+
"size": bar_size,
101+
},
81102
],
82103
}
83104

@@ -87,17 +108,31 @@ def test_module(self):
87108
module = target.AddModule(self.toModuleSpec(json_object_file_c))
88109
self.assertTrue(module.IsValid())
89110

90-
section = module.GetSectionAtIndex(0)
91-
self.assertTrue(section.IsValid())
92-
self.assertEqual(section.GetName(), "__TEXT")
93-
self.assertEqual(section.file_addr, 0x0)
94-
self.assertEqual(section.size, 0x222)
95-
96-
symbol = module.FindSymbol("foo")
97-
self.assertTrue(symbol.IsValid())
98-
self.assertEqual(symbol.addr.GetFileAddress(), 0x100)
99-
self.assertEqual(symbol.GetSize(), 0x11)
100-
101-
error = target.SetSectionLoadAddress(section, 0x1000)
111+
text_section = module.GetSectionAtIndex(0)
112+
self.assertTrue(text_section.IsValid())
113+
self.assertEqual(text_section.GetName(), "__TEXT")
114+
self.assertEqual(text_section.file_addr, TEXT_file_addr)
115+
self.assertEqual(text_section.size, TEXT_size)
116+
117+
data_section = module.GetSectionAtIndex(1)
118+
self.assertTrue(data_section.IsValid())
119+
self.assertEqual(data_section.GetName(), "__DATA")
120+
self.assertEqual(data_section.file_addr, DATA_file_addr)
121+
self.assertEqual(data_section.size, DATA_size)
122+
123+
foo_symbol = module.FindSymbol("foo")
124+
self.assertTrue(foo_symbol.IsValid())
125+
self.assertEqual(foo_symbol.addr.GetFileAddress(), foo_file_addr)
126+
self.assertEqual(foo_symbol.GetSize(), foo_size)
127+
128+
bar_symbol = module.FindSymbol("bar")
129+
self.assertTrue(bar_symbol.IsValid())
130+
self.assertEqual(bar_symbol.addr.GetFileAddress(), bar_file_addr)
131+
self.assertEqual(bar_symbol.GetSize(), bar_size)
132+
133+
error = target.SetSectionLoadAddress(text_section, TEXT_file_addr + slide)
134+
self.assertSuccess(error)
135+
error = target.SetSectionLoadAddress(data_section, DATA_file_addr + slide)
102136
self.assertSuccess(error)
103-
self.assertEqual(symbol.addr.GetLoadAddress(target), 0x1100)
137+
self.assertEqual(foo_symbol.addr.GetLoadAddress(target), foo_file_addr + slide)
138+
self.assertEqual(bar_symbol.addr.GetLoadAddress(target), bar_file_addr + slide)

0 commit comments

Comments
 (0)