[llvm-objcopy] Avoid invalid Sec.Offset after D79229

To avoid undefined behavior caught by -fsanitize=undefined on binary-paddr.test

  void SectionWriter::visit(const Section &Sec) {
    if (Sec.Type != SHT_NOBITS)
      // Sec.Contents is empty while Sec.Offset may be out of bound
      llvm::copy(Sec.Contents, Out.getBufferStart() + Sec.Offset);
  }
This commit is contained in:
Fangrui Song 2020-05-03 21:54:28 -07:00
parent 14cb0bdf2b
commit 762fb1c40e
1 changed files with 4 additions and 4 deletions

View File

@ -2234,11 +2234,11 @@ Error BinaryWriter::finalize() {
// layoutSections, because we want to truncate the last segment to the end of
// its last non-empty section, to match GNU objcopy's behaviour.
TotalSize = 0;
for (SectionBase &Sec : Obj.allocSections()) {
Sec.Offset = Sec.Addr - MinAddr;
if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
for (SectionBase &Sec : Obj.allocSections())
if (Sec.Type != SHT_NOBITS && Sec.Size > 0) {
Sec.Offset = Sec.Addr - MinAddr;
TotalSize = std::max(TotalSize, Sec.Offset + Sec.Size);
}
}
if (Error E = Buf.allocate(TotalSize))
return E;