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
22using namespace lldb_private;
23using namespace lldb;
24
25class SectionSizeTest : public testing::Test {
26 SubsystemRAII<FileSystem, ObjectFilePECOFF> subsystems;
27};
28
29TEST_F(SectionSizeTest, NoAlignmentPadding) {
30 llvm::Expected<TestFile> ExpectedFile = TestFile::fromYaml(
31 Yaml: R"(
32--- !COFF
33OptionalHeader:
34 SectionAlignment: 4096
35 FileAlignment: 512
36header:
37 Machine: IMAGE_FILE_MACHINE_AMD64
38 Characteristics: [ IMAGE_FILE_EXECUTABLE_IMAGE, IMAGE_FILE_LARGE_ADDRESS_AWARE ]
39sections:
40 - Name: swiftast
41 VirtualSize: 496
42 SizeOfRawData: 512
43 Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
44 SectionData: 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110000
45
46symbols: []
47...
48)");
49 ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());
50
51 ModuleSP module_sp = std::make_shared<Module>(args: 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(depth: 0);
60 for (size_t i = 0; i < section_count; ++i) {
61 SectionSP section_sp = section_list->GetSectionAtIndex(idx: 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 (size_t)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(), (uint64_t)496);
77}
78
79

source code of lldb/unittests/ObjectFile/PECOFF/TestSectionSize.cpp