Skip to content

Commit 7c478db

Browse files
authored
Merge pull request #7160 from hjyamauchi/lldb
[lldb][PECOFF] Exclude alignment padding when reading section data
2 parents 059b5f5 + 7c7ff10 commit 7c478db

File tree

7 files changed

+99
-2
lines changed

7 files changed

+99
-2
lines changed

lldb/include/lldb/Symbol/ObjectFile.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,12 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
680680
const char *GetCStrFromSection(Section *section,
681681
lldb::offset_t section_offset) const;
682682

683+
// Returns the section data size. This is special-cased for PECOFF
684+
// due to file alignment.
685+
virtual size_t GetSectionDataSize(Section *section) {
686+
return section->GetFileSize();
687+
}
688+
683689
/// Returns true if the object file exists only in memory.
684690
bool IsInMemory() const { return m_memory_addr != LLDB_INVALID_ADDRESS; }
685691

lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,16 @@ SectionType ObjectFilePECOFF::GetSectionType(llvm::StringRef sect_name,
10331033
return eSectionTypeOther;
10341034
}
10351035

1036+
size_t ObjectFilePECOFF::GetSectionDataSize(Section *section) {
1037+
// For executables, SizeOfRawData (getFileSize()) is aligned by
1038+
// FileAlignment and the actual section size is in VirtualSize
1039+
// (getByteSize()). See the comment on
1040+
// llvm::object::COFFObjectFile::getSectionSize().
1041+
if (m_binary->getPE32Header() || m_binary->getPE32PlusHeader())
1042+
return std::min(section->GetByteSize(), section->GetFileSize());
1043+
return section->GetFileSize();
1044+
}
1045+
10361046
void ObjectFilePECOFF::CreateSections(SectionList &unified_section_list) {
10371047
if (m_sections_up)
10381048
return;

lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ class ObjectFilePECOFF : public lldb_private::ObjectFile {
261261
llvm::StringRef GetSectionName(const section_header_t &sect);
262262
static lldb::SectionType GetSectionType(llvm::StringRef sect_name,
263263
const section_header_t &sect);
264+
size_t GetSectionDataSize(lldb_private::Section *section) override;
264265

265266
llvm::StringRef
266267
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section) override;

lldb/source/Symbol/ObjectFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,8 @@ size_t ObjectFile::ReadSectionData(Section *section,
552552

553553
// The object file now contains a full mmap'ed copy of the object file
554554
// data, so just use this
555-
return GetData(section->GetFileOffset(), section->GetFileSize(),
556-
section_data);
555+
return GetData(section->GetFileOffset(), GetSectionDataSize(section),
556+
section_data);
557557
}
558558

559559
const char *

lldb/unittests/ObjectFile/PECOFF/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_lldb_unittest(ObjectFilePECOFFTests
22
TestPECallFrameInfo.cpp
3+
TestSectionSize.cpp
34

45
LINK_LIBS
56
lldbUtilityHelpers
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===-- TestSectionFileSize.cpp -------------------------------------------===//
2+
//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "gtest/gtest.h"
11+
12+
#include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h"
13+
#include "Plugins/Process/Utility/lldb-x86-register-enums.h"
14+
#include "TestingSupport/SubsystemRAII.h"
15+
#include "TestingSupport/TestUtilities.h"
16+
17+
#include "lldb/Core/Module.h"
18+
#include "lldb/Symbol/CallFrameInfo.h"
19+
#include "lldb/Symbol/UnwindPlan.h"
20+
#include "llvm/Testing/Support/Error.h"
21+
22+
using namespace lldb_private;
23+
using namespace lldb;
24+
25+
class SectionSizeTest : public testing::Test {
26+
SubsystemRAII<FileSystem, ObjectFilePECOFF> subsystems;
27+
};
28+
29+
TEST_F(SectionSizeTest, NoAlignmentPadding) {
30+
llvm::Expected<TestFile> ExpectedFile = TestFile::fromYaml(
31+
R"(
32+
--- !COFF
33+
OptionalHeader:
34+
SectionAlignment: 4096
35+
FileAlignment: 512
36+
header:
37+
Machine: IMAGE_FILE_MACHINE_AMD64
38+
Characteristics: [ IMAGE_FILE_EXECUTABLE_IMAGE, IMAGE_FILE_LARGE_ADDRESS_AWARE ]
39+
sections:
40+
- Name: swiftast
41+
VirtualSize: 496
42+
SizeOfRawData: 512
43+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
44+
SectionData: 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110000
45+
46+
symbols: []
47+
...
48+
)");
49+
ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());
50+
51+
ModuleSP module_sp = std::make_shared<Module>(ExpectedFile->moduleSpec());
52+
ObjectFile *object_file = module_sp->GetObjectFile();
53+
ASSERT_NE(object_file, nullptr);
54+
55+
SectionList *section_list = object_file->GetSectionList();
56+
ASSERT_NE(section_list, nullptr);
57+
58+
SectionSP swiftast_section;
59+
size_t section_count = section_list->GetNumSections(0);
60+
for (size_t i = 0; i < section_count; ++i) {
61+
SectionSP section_sp = section_list->GetSectionAtIndex(i);
62+
if (section_sp->GetName() == "swiftast") {
63+
swiftast_section = section_sp;
64+
break;
65+
}
66+
}
67+
ASSERT_NE(swiftast_section.get(), nullptr);
68+
69+
DataExtractor section_data;
70+
ASSERT_NE(object_file->ReadSectionData(swiftast_section.get(),
71+
section_data),
72+
0);
73+
74+
// Check that the section data size is equal to VirtualSize (496)
75+
// without the zero padding, instead of SizeOfRawData (512).
76+
EXPECT_EQ(section_data.GetByteSize(), 496);
77+
}
78+

llvm/test/tools/yaml2obj/COFF/xrelocs.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
# CHECK-YAML-NEXT: Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_NRELOC_OVFL, IMAGE_SCN_MEM_READ ]
3131
# CHECK-YAML-NEXT: Alignment: 16
3232
# CHECK-YAML-NEXT: SectionData: '00000000000000000000000000000000'
33+
# CHECK-YAML-NEXT: SizeOfRawData: 16
3334
# CHECK-YAML-NEXT: Relocations:
3435
# CHECK-YAML-NEXT: - VirtualAddress: 0
3536
# CHECK-YAML-NEXT: SymbolName: foo

0 commit comments

Comments
 (0)