[COFF] Fix .bss section size bug in obj2yaml / yaml2obj

We need to serialize SizeOfRawData through even when there is no data,
as in a .bss section.

Fixes PR41836

llvm-svn: 360473
This commit is contained in:
Reid Kleckner 2019-05-10 21:53:44 +00:00
parent 114f763f37
commit 7eb6b5ffc3
3 changed files with 23 additions and 2 deletions

View File

@ -578,6 +578,12 @@ void MappingTraits<COFFYAML::Section>::mapping(IO &IO, COFFYAML::Section &Sec) {
else if (Sec.Name == ".debug$H")
IO.mapOptional("GlobalHashes", Sec.DebugH);
// Uninitialized sections, such as .bss, typically have no data, but the size
// is carried in SizeOfRawData, even though PointerToRawData is zero.
if (Sec.SectionData.binary_size() == 0 &&
NC->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
IO.mapOptional("SizeOfRawData", Sec.Header.SizeOfRawData);
IO.mapOptional("Relocations", Sec.Relocations);
}

View File

@ -0,0 +1,14 @@
# RUN: llvm-mc -filetype=obj -triple=x86_64-windows-msvc %s -o %t.obj
# RUN: llvm-objdump -h %t.obj | FileCheck %s
# RUN: obj2yaml %t.obj | yaml2obj -o %t.2.obj
# RUN: llvm-objdump -h %t.2.obj | FileCheck %s
# CHECK: Idx Name Size VMA Type
# CHECK: .bss 00000004 0000000000000000 BSS
# Before PR41836, Size would be 0 after yaml conversion.
.bss
.global gv_bss
gv_bss:
.long 0

View File

@ -259,7 +259,8 @@ static bool layoutCOFF(COFFParser &CP) {
S.Header.NumberOfRelocations * COFF::RelocationSize;
}
} else {
S.Header.SizeOfRawData = 0;
// Leave SizeOfRawData unaltered. For .bss sections in object files, it
// carries the section size.
S.Header.PointerToRawData = 0;
}
}
@ -496,7 +497,7 @@ static bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
// Output section data.
for (const COFFYAML::Section &S : CP.Obj.Sections) {
if (!S.Header.SizeOfRawData)
if (S.Header.SizeOfRawData == 0 || S.Header.PointerToRawData == 0)
continue;
assert(S.Header.PointerToRawData >= OS.tell());
OS.write_zeros(S.Header.PointerToRawData - OS.tell());