[ELF][PPC64] Fix potentially corrupted section content with empty .got
authorFangrui Song <i@maskray.me>
Fri, 5 Aug 2022 22:22:57 +0000 (15:22 -0700)
committerTom Stellard <tstellar@redhat.com>
Mon, 8 Aug 2022 19:53:26 +0000 (12:53 -0700)
D91426 makes .got possibly empty while needed. If .got and .data have the same
address, and .got's content is written after .data, the first word of .data will
be corrupted.

The bug is not testable without D131247.

(cherry picked from commit 28d05d672300e51f53c73fe9a4bd053e73844247)

lld/ELF/SyntheticSections.cpp

index 919afc7a6e0e29bd55e997101ccbb2c60fd524f3..aa5e8675dd3135314412307cdc204ed2a4bbb7d8 100644 (file)
@@ -712,6 +712,9 @@ bool GotSection::isNeeded() const {
 }
 
 void GotSection::writeTo(uint8_t *buf) {
+  // On PPC64 .got may be needed but empty. Skip the write.
+  if (size == 0)
+    return;
   target->writeGotHeader(buf);
   relocateAlloc(buf, buf + size);
 }