[ELF] - Stop silently producing a broken .eh_frame_hdr.
authorGeorge Rimar <grimar@accesssoftek.com>
Wed, 18 Jul 2018 11:56:53 +0000 (11:56 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Wed, 18 Jul 2018 11:56:53 +0000 (11:56 +0000)
Currently, getFdePC() returns uint64_t. Its because the following
encodings might use 8 bytes: DW_EH_PE_absptr and DW_EH_PE_udata8.

But caller assigns returned value to uint32_t field:
https://github.com/llvm-mirror/lld/blob/master/ELF/SyntheticSections.cpp#L508

Value is used for building .eh_frame_hdr section.
We use DW_EH_PE_sdata4 encoding for building it at this moment:
https://github.com/llvm-mirror/lld/blob/master/ELF/SyntheticSections.cpp#L2545

And that means that an overflow issue might happen if
DW_EH_PE_absptr/DW_EH_PE_udata8 address encodings are present
in .eh_frame. In that case, before this patch, we silently would
truncate the address and produced broken .eh_frame_hdr section.

It would be not hard to support real 64-bit values for
DW_EH_PE_absptr/DW_EH_PE_udata8 encodings, but it is
unclear if it is usefull and if we should do it.

Since nobody faced/reported it, int this patch I only implement
a check to stop producing broken output silently for now.

llvm-svn: 337382

lld/ELF/SyntheticSections.cpp
lld/test/ELF/eh-frame-pcaddr-overflow.s [new file with mode: 0644]

index 0d20894..d51c288 100644 (file)
@@ -505,9 +505,11 @@ std::vector<EhFrameSection::FdeData> EhFrameSection::getFdeData() const {
   for (CieRecord *Rec : CieRecords) {
     uint8_t Enc = getFdeEncoding(Rec->Cie);
     for (EhSectionPiece *Fde : Rec->Fdes) {
-      uint32_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
+      uint64_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
+      if (Pc > UINT32_MAX)
+        fatal(toString(Fde->Sec) + ": PC address is too large: " + Twine(Pc));
       uint32_t FdeVA = getParent()->Addr + Fde->OutputOff;
-      Ret.push_back({Pc, FdeVA});
+      Ret.push_back({(uint32_t)Pc, FdeVA});
     }
   }
   return Ret;
diff --git a/lld/test/ELF/eh-frame-pcaddr-overflow.s b/lld/test/ELF/eh-frame-pcaddr-overflow.s
new file mode 100644 (file)
index 0000000..3cc5ac8
--- /dev/null
@@ -0,0 +1,32 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: not ld.lld --eh-frame-hdr --section-start .text=0x1000000000000000 \
+# RUN:   %t.o -o /dev/null 2>&1 | FileCheck %s
+# CHECK: error: {{.*}}.o:(.eh_frame): PC address is too large: 2387527121043355528
+
+.text
+.global foo
+foo:
+ nop
+
+.section .eh_frame, "a"
+  .long 12   # Size
+  .long 0x00 # ID
+  .byte 0x01 # Version.
+  
+  .byte 0x52 # Augmentation string: 'R','\0'
+  .byte 0x00
+  
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x00 # DW_EH_PE_absptr
+
+  .byte 0xFF
+  .long 12  # Size
+  .long 0x14 # ID
+  .quad foo + 0x1122334455667788