[PATCH] D15610: [ELF] - Fixed handling relocations against zero sized .eh_frame section.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 17 07:38:09 PST 2015


grimar created this revision.
grimar added reviewers: ruiu, rafael.
grimar added subscribers: grimar, llvm-commits.

Relocations refering zero sized .eh_frame sections can happen when linking against crtbeginT.o.

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
...
  [ 4] .bss              NOBITS           0000000000000000  00000140
       0000000000000050  0000000000000000  WA       0     0     32
  [ 5] .eh_frame         PROGBITS         0000000000000000  00000140
       0000000000000000  0000000000000000   A       0     0     4
  [ 6] .jcr              PROGBITS         0000000000000000  00000140
       0000000000000000  0000000000000000  WA       0     0     8
...
0000000000000080 <__do_global_dtors_aux>:
  80:	80 3d 00 00 00 00 00 	cmpb   $0x0,0x0(%rip)        # 87 <__do_global_dtors_aux+0x7>
  87:	75 22                	jne    ab <__do_global_dtors_aux+0x2b>
  89:	55                   	push   %rbp
  8a:	48 89 e5             	mov    %rsp,%rbp
  8d:	e8 6e ff ff ff       	callq  0 <deregister_tm_clones>
  92:	b8 00 00 00 00       	mov    $0x0,%eax
  97:	48 85 c0             	test   %rax,%rax
  9a:	74 07                	je     a3 <__do_global_dtors_aux+0x23>
  9c:	bf 00 00 00 00       	mov    $0x0,%edi
  a1:	ff d0                	callq  *%rax
  a3:	5d                   	pop    %rbp
  a4:	c6 05 00 00 00 00 01 	movb   $0x1,0x0(%rip)        # ab <__do_global_dtors_aux+0x2b>
  ab:	f3 c3                	repz retq 
  ad:	0f 1f 00             	nopl   (%rax)
...
00000000009d  00050000000a R_X86_64_32       0000000000000000 .eh_frame + 0
...
0000000000c4  00050000000a R_X86_64_32       0000000000000000 .eh_frame + 0

The result of linking without this patch is assertion fail, details can be found in https://llvm.org/bugs/show_bug.cgi?id=25762&list_id=89776.
With this patch behavior seems to be consistent with gold.


http://reviews.llvm.org/D15610

Files:
  ELF/InputSection.cpp
  ELF/OutputSections.cpp
  test/ELF/ehframe-relocation.s

Index: test/ELF/ehframe-relocation.s
===================================================================
--- test/ELF/ehframe-relocation.s
+++ test/ELF/ehframe-relocation.s
@@ -0,0 +1,35 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+// RUN: ld.lld %t.o -o %t
+// RUN: llvm-readobj -s -section-data %t | FileCheck %s
+// RUN: llvm-objdump -d %t | FileCheck --check-prefix=DISASM %s
+
+// CHECK:      Name: .eh_frame
+// CHECK-NEXT: Type: SHT_X86_64_UNWIND
+// CHECK-NEXT: Flags [
+// CHECK-NEXT:   SHF_ALLOC
+// CHECK-NEXT: ]
+// CHECK-NEXT: Address: 0x10120
+// CHECK-NEXT: Offset:
+// CHECK-NEXT: Size:
+// CHECK-NEXT: Link: 0
+// CHECK-NEXT: Info: 0
+// CHECK-NEXT: AddressAlignment:
+// CHECK-NEXT: EntrySize: 0
+// CHECK-NEXT: SectionData (
+// CHECK-NEXT: )
+
+// 0x10120 = 65824
+// 0x10120 + 5 = 65829
+// DISASM:      Disassembly of section .text:
+// DISASM-NEXT: _start:
+// DISASM-NEXT:    11000: 48 8b 04 25 20 01 01 00 movq 65824, %rax
+// DISASM-NEXT:    11008: 48 8b 04 25 25 01 01 00 movq 65829, %rax
+
+.section .eh_frame,"ax", at unwind
+
+.section .text
+.globl _start
+_start:
+ movq .eh_frame, %rax
+ movq .eh_frame + 5, %rax
Index: ELF/OutputSections.cpp
===================================================================
--- ELF/OutputSections.cpp
+++ ELF/OutputSections.cpp
@@ -853,8 +853,13 @@
     Offset += Addend;
     Addend = 0;
   }
-  return VA + cast<MergeInputSection<ELFT>>(Section)->getOffset(Offset) +
-         Addend;
+  uintX_t SecOff;
+  if (isa<EHInputSection<ELFT>>(Section))
+    SecOff = cast<EHInputSection<ELFT>>(Section)->getOffset(Offset);
+  else
+    SecOff = cast<MergeInputSection<ELFT>>(Section)->getOffset(Offset);
+
+  return VA + SecOff + Addend;
 }
 
 // Returns true if a symbol can be replaced at load-time by a symbol
Index: ELF/InputSection.cpp
===================================================================
--- ELF/InputSection.cpp
+++ ELF/InputSection.cpp
@@ -255,6 +255,11 @@
 template <class ELFT>
 typename EHInputSection<ELFT>::uintX_t
 EHInputSection<ELFT>::getOffset(uintX_t Offset) {
+  // Relocations can be against .eh_frame section
+  // which has zero size. For example crtbeginT.o
+  // has some.
+  if (this->getSectionHdr()->sh_size == 0)
+    return Offset;
   std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first;
   uintX_t Base = I->second;
   if (Base == uintX_t(-1))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15610.43136.patch
Type: text/x-patch
Size: 2412 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151217/a2eee63d/attachment.bin>


More information about the llvm-commits mailing list