[llvm] r371420 - [yaml2obj] Simplify p_filesz/p_memsz computing
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 9 09:45:17 PDT 2019
Author: maskray
Date: Mon Sep 9 09:45:17 2019
New Revision: 371420
URL: http://llvm.org/viewvc/llvm-project?rev=371420&view=rev
Log:
[yaml2obj] Simplify p_filesz/p_memsz computing
This fixes a bug as well. When "FileSize:" (p_filesz) is specified and
different from the actual value, the following code probably should not
use PHeader.p_filesz:
if (SHeader->sh_offset == PHeader.p_offset + PHeader.p_filesz)
PHeader.p_memsz += SHeader->sh_size;
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D67256
Modified:
llvm/trunk/lib/ObjectYAML/ELFEmitter.cpp
llvm/trunk/test/tools/yaml2obj/program-header-size-offset.yaml
Modified: llvm/trunk/lib/ObjectYAML/ELFEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ObjectYAML/ELFEmitter.cpp?rev=371420&r1=371419&r2=371420&view=diff
==============================================================================
--- llvm/trunk/lib/ObjectYAML/ELFEmitter.cpp (original)
+++ llvm/trunk/lib/ObjectYAML/ELFEmitter.cpp Mon Sep 9 09:45:17 2019
@@ -627,36 +627,24 @@ void ELFState<ELFT>::setProgramHeaderLay
PHeader.p_offset = std::min(PHeader.p_offset, SHeader->sh_offset);
}
- // Find the maximum offset of the end of a section in order to set p_filesz,
- // if not set explicitly.
- if (YamlPhdr.FileSize) {
- PHeader.p_filesz = *YamlPhdr.FileSize;
- } else {
- PHeader.p_filesz = 0;
- for (Elf_Shdr *SHeader : Sections) {
- uint64_t EndOfSection;
- if (SHeader->sh_type == llvm::ELF::SHT_NOBITS)
- EndOfSection = SHeader->sh_offset;
- else
- EndOfSection = SHeader->sh_offset + SHeader->sh_size;
- uint64_t EndOfSegment = PHeader.p_offset + PHeader.p_filesz;
- EndOfSegment = std::max(EndOfSegment, EndOfSection);
- PHeader.p_filesz = EndOfSegment - PHeader.p_offset;
- }
- }
+ // Find the maximum offset of the end of a section in order to set p_filesz
+ // and p_memsz. When setting p_filesz, trailing SHT_NOBITS sections are not
+ // counted.
+ uint64_t FileOffset = PHeader.p_offset, MemOffset = PHeader.p_offset;
+ for (Elf_Shdr *SHeader : Sections) {
+ uint64_t End = SHeader->sh_offset + SHeader->sh_size;
+ MemOffset = std::max(MemOffset, End);
- // If not set explicitly, find the memory size by adding the size of
- // sections at the end of the segment. These should be empty (size of zero)
- // and NOBITS sections.
- if (YamlPhdr.MemSize) {
- PHeader.p_memsz = *YamlPhdr.MemSize;
- } else {
- PHeader.p_memsz = PHeader.p_filesz;
- for (Elf_Shdr *SHeader : Sections)
- if (SHeader->sh_offset == PHeader.p_offset + PHeader.p_filesz)
- PHeader.p_memsz += SHeader->sh_size;
+ if (SHeader->sh_type != llvm::ELF::SHT_NOBITS)
+ FileOffset = std::max(FileOffset, End);
}
+ // Set the file size and the memory size if not set explicitly.
+ PHeader.p_filesz = YamlPhdr.FileSize ? uint64_t(*YamlPhdr.FileSize)
+ : FileOffset - PHeader.p_offset;
+ PHeader.p_memsz = YamlPhdr.MemSize ? uint64_t(*YamlPhdr.MemSize)
+ : MemOffset - PHeader.p_offset;
+
// Set the alignment of the segment to be the same as the maximum alignment
// of the sections with the same offset so that by default the segment
// has a valid and sensible alignment.
Modified: llvm/trunk/test/tools/yaml2obj/program-header-size-offset.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/yaml2obj/program-header-size-offset.yaml?rev=371420&r1=371419&r2=371420&view=diff
==============================================================================
--- llvm/trunk/test/tools/yaml2obj/program-header-size-offset.yaml (original)
+++ llvm/trunk/test/tools/yaml2obj/program-header-size-offset.yaml Mon Sep 9 09:45:17 2019
@@ -11,7 +11,7 @@
# CHECK: Offset: 0x2000
# CHECK: FileSize: 6
-# CHECK: MemSize: 6
+# CHECK: MemSize: 4
# CHECK: Offset: 0x2000
# CHECK: FileSize: 4
@@ -28,6 +28,10 @@
# CHECK: Offset: 0x3000
# CHECK: FileSize: 3
# CHECK: MemSize: 2
+
+# CHECK: Offset: 0x2004
+# CHECK: FileSize: 4
+# CHECK: MemSize: 6
# CHECK: ]
!ELF
@@ -40,14 +44,26 @@ Sections:
- Name: .text
Type: SHT_PROGBITS
Size: 4
+ ShOffset: 0x1000
AddressAlign: 0x1000
- Name: .rodata
Type: SHT_PROGBITS
Size: 4
+ ShOffset: 0x2000
AddressAlign: 0x1000
- Name: .data
Type: SHT_PROGBITS
+ ShOffset: 0x2004
Size: 4
+ - Name: .nobits1
+ Type: SHT_NOBITS
+ ShOffset: 0x2008
+ Size: 1
+ - Name: .nobits2
+ Type: SHT_NOBITS
+ # Intentionally set to 0x2009 though the previous section is SHT_NOBITS.
+ ShOffset: 0x2009
+ Size: 1
ProgramHeaders:
# Program header with no sections.
- Type: 0x6abcdef0 # arbitrary type
@@ -83,3 +99,10 @@ ProgramHeaders:
MemSize: 2
Sections:
- Section: .data
+ # Program header with 2 SHT_NOBITS sections.
+ - Type: 0x6abcdef0
+ Offset: 0x2004
+ Sections:
+ - Section: .data
+ - Section: .nobits1
+ - Section: .nobits2
More information about the llvm-commits
mailing list