[ELF] Explicitly write null bytes in string tables
authorJames Henderson <jh7370@my.bristol.ac.uk>
Fri, 4 Aug 2017 09:07:55 +0000 (09:07 +0000)
committerJames Henderson <jh7370@my.bristol.ac.uk>
Fri, 4 Aug 2017 09:07:55 +0000 (09:07 +0000)
Following r309829, if a string table appears in an executable segment, the strings
will not be null terminated. This is a problem, for example, for the .dynstr
section when using -no-rosegment. The strings end up being terminated with 0xcc
because prior to this patch, LLD did not explicitly write the null terminators.
This change fixes that by always writing the null terminators.

Reviewers: rafael

Differential Revision: https://reviews.llvm.org/D36267

llvm-svn: 310042

lld/ELF/SyntheticSections.cpp
lld/test/ELF/dynstr-no-rosegment.s [new file with mode: 0644]

index 301062c..85daa64 100644 (file)
@@ -990,6 +990,7 @@ unsigned StringTableSection::addString(StringRef S, bool HashIt) {
 void StringTableSection::writeTo(uint8_t *Buf) {
   for (StringRef S : Strings) {
     memcpy(Buf, S.data(), S.size());
+    Buf[S.size()] = '\0';
     Buf += S.size() + 1;
   }
 }
diff --git a/lld/test/ELF/dynstr-no-rosegment.s b/lld/test/ELF/dynstr-no-rosegment.s
new file mode 100644 (file)
index 0000000..ad8103e
--- /dev/null
@@ -0,0 +1,12 @@
+# Verify that a .dynstr in the .text segment has null byte terminators\r
+\r
+# REQUIRES: x86\r
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o\r
+# RUN: ld.lld %t.o -no-rosegment -o %t.so -shared\r
+# RUN: llvm-objdump %t.so -s -j .dynstr | FileCheck %s\r
+\r
+# CHECK: 00666f6f 00 .foo.\r
+\r
+.globl foo\r
+foo:\r
+    ret\r