summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorFangrui Song <maskray@google.com>2019-09-09 16:45:17 +0000
committerFangrui Song <maskray@google.com>2019-09-09 16:45:17 +0000
commitc28f3e6e2c3ef1323ed18d4c485681bb4ff72ced (patch)
tree61ae67185fae95bd78b5bcfc29f558f0a0587c31 /llvm/lib
parent2b7089949eda508203eb23c835d6a295eb00b46b (diff)
downloadbcm5719-llvm-c28f3e6e2c3ef1323ed18d4c485681bb4ff72ced.tar.gz
bcm5719-llvm-c28f3e6e2c3ef1323ed18d4c485681bb4ff72ced.zip
[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 llvm-svn: 371420
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/ObjectYAML/ELFEmitter.cpp42
1 files changed, 15 insertions, 27 deletions
diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index e53e8cb8512..531f0d4f1d8 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -627,35 +627,23 @@ void ELFState<ELFT>::setProgramHeaderLayout(std::vector<Elf_Phdr> &PHeaders,
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 (SHeader->sh_type != llvm::ELF::SHT_NOBITS)
+ FileOffset = std::max(FileOffset, 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;
- }
+ // 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
OpenPOWER on IntegriCloud