Skip to content

Commit

Permalink
[lldb] add SBSection.alignment to python bindings
Browse files Browse the repository at this point in the history
This commit adds SBSection.GetAlignment(), and SBSection.alignment as a python property to lldb.

Reviewed By: clayborg, JDevlieghere, labath

Differential Revision: https://reviews.llvm.org/D128069
  • Loading branch information
dmlary authored and labath committed Jul 12, 2022
1 parent 918b1e7 commit 1e3ee76
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lldb/bindings/interface/SBSection.i
Expand Up @@ -105,6 +105,9 @@ public:
uint32_t
GetTargetByteSize ();

uint32_t
GetAlignment ();

bool
GetDescription (lldb::SBStream &description);

Expand Down Expand Up @@ -138,6 +141,7 @@ public:
data = property(GetSectionData, None, doc='''A read only property that returns an lldb object that represents the bytes for this section (lldb.SBData) for this section.''')
type = property(GetSectionType, None, doc='''A read only property that returns an lldb enumeration value (see enumerations that start with "lldb.eSectionType") that represents the type of this section (code, data, etc.).''')
target_byte_size = property(GetTargetByteSize, None, doc='''A read only property that returns the size of a target byte represented by this section as a number of host bytes.''')
alignment = property(GetAlignment, None, doc='''A read only property that returns the alignment of this section as a number of host bytes.''')
%}
#endif

Expand Down
6 changes: 6 additions & 0 deletions lldb/include/lldb/API/SBSection.h
Expand Up @@ -76,6 +76,12 @@ class LLDB_API SBSection {
/// The number of host (8-bit) bytes needed to hold a target byte
uint32_t GetTargetByteSize();

/// Return the alignment of the section in bytes
///
/// \return
/// The alignment of the section in bytes
uint32_t GetAlignment();

bool operator==(const lldb::SBSection &rhs);

bool operator!=(const lldb::SBSection &rhs);
Expand Down
9 changes: 9 additions & 0 deletions lldb/source/API/SBSection.cpp
Expand Up @@ -242,6 +242,15 @@ uint32_t SBSection::GetTargetByteSize() {
return 0;
}

uint32_t SBSection::GetAlignment() {
LLDB_INSTRUMENT_VA(this);

SectionSP section_sp(GetSP());
if (section_sp.get())
return (1 << section_sp->GetLog2Align());
return 0;
}

bool SBSection::operator==(const SBSection &rhs) {
LLDB_INSTRUMENT_VA(this, rhs);

Expand Down
11 changes: 11 additions & 0 deletions lldb/test/API/python_api/section/TestSectionAPI.py
Expand Up @@ -37,3 +37,14 @@ def test_get_target_byte_size(self):

self.assertIsNotNone(data_section)
self.assertEqual(data_section.target_byte_size, 1)

def test_get_alignment(self):
exe = self.getBuildArtifact("aligned.out")
self.yaml2obj("aligned.yaml", exe)
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)

# exe contains a single section aligned to 0x1000
section = target.modules[0].sections[0]
self.assertEqual(section.GetAlignment(), 0x1000)
self.assertEqual(section.alignment, 0x1000)
14 changes: 14 additions & 0 deletions lldb/test/API/python_api/section/aligned.yaml
@@ -0,0 +1,14 @@
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_X86_64
Entry: 0x0000000000400000
Sections:
- Name: .text1
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Address: 0x0000000000400000
AddressAlign: 0x0000000000001000
Size: 0xb0

0 comments on commit 1e3ee76

Please sign in to comment.