[MIPS][ELF] Use PC-relative relocations in .eh_frame when possible

When compiling position-independent executables, we now use
DW_EH_PE_pcrel | DW_EH_PE_sdata4. However, the MIPS ABI does not define a
64-bit PC-relative ELF relocation so we cannot use sdata8 for the large
code model case. When using the large code model, we fall back to the
previous behaviour of generating absolute relocations.

With this change clang-generated .o files can be linked by LLD without
having to pass -Wl,-z,notext (which creates text relocations).
This is simpler than the approach used by ld.bfd, which rewrites the
.eh_frame section to convert absolute relocations into relative references.

I saw in D13104 that apparently ld.bfd did not accept pc-relative relocations
for MIPS ouput at some point. However, I also checked that recent ld.bfd
can process the clang-generated .o files so this no longer seems true.

Reviewed By: atanasyan
Differential Revision: https://reviews.llvm.org/D72228
This commit is contained in:
Alex Richardson 2020-01-12 10:55:15 +00:00
parent 03ef20d9a0
commit 53f757e913
4 changed files with 90 additions and 41 deletions

View File

@ -303,9 +303,14 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
case Triple::mipsel: case Triple::mipsel:
case Triple::mips64: case Triple::mips64:
case Triple::mips64el: case Triple::mips64el:
FDECFIEncoding = Ctx->getAsmInfo()->getCodePointerSize() == 4 // We cannot use DW_EH_PE_sdata8 for the large PositionIndependent case
? dwarf::DW_EH_PE_sdata4 // since there is no R_MIPS_PC64 relocation (only a 32-bit version).
: dwarf::DW_EH_PE_sdata8; if (PositionIndependent && !Large)
FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
else
FDECFIEncoding = Ctx->getAsmInfo()->getCodePointerSize() == 4
? dwarf::DW_EH_PE_sdata4
: dwarf::DW_EH_PE_sdata8;
break; break;
case Triple::ppc64: case Triple::ppc64:
case Triple::ppc64le: case Triple::ppc64le:

View File

@ -105,6 +105,7 @@ static bool supportsMips64(uint64_t Type) {
case ELF::R_MIPS_32: case ELF::R_MIPS_32:
case ELF::R_MIPS_64: case ELF::R_MIPS_64:
case ELF::R_MIPS_TLS_DTPREL64: case ELF::R_MIPS_TLS_DTPREL64:
case ELF::R_MIPS_PC32:
return true; return true;
default: default:
return false; return false;
@ -119,6 +120,8 @@ static uint64_t resolveMips64(RelocationRef R, uint64_t S, uint64_t A) {
return S + getELFAddend(R); return S + getELFAddend(R);
case ELF::R_MIPS_TLS_DTPREL64: case ELF::R_MIPS_TLS_DTPREL64:
return S + getELFAddend(R) - 0x8000; return S + getELFAddend(R) - 0x8000;
case ELF::R_MIPS_PC32:
return S + getELFAddend(R) - R.getOffset();
default: default:
llvm_unreachable("Invalid relocation type"); llvm_unreachable("Invalid relocation type");
} }

View File

@ -1,7 +1,7 @@
; RUN: llc -mtriple mips-unknown-linux-gnu -mattr=+micromips -relocation-model=static -O3 -filetype=obj -o - %s | \ ; RUN: llc -mtriple mips-unknown-linux-gnu -mattr=+micromips -relocation-model=static -O3 -filetype=obj -o - %s | \
; RUN: llvm-readelf -r | FileCheck %s --check-prefix=CHECK-READELF ; RUN: llvm-readelf -r | FileCheck %s --check-prefixes=CHECK-READELF,CHECK-READELF-STATIC
; RUN: llc -mtriple mips-unknown-linux-gnu -mattr=+micromips -relocation-model=pic -O3 -filetype=obj -o - %s | \ ; RUN: llc -mtriple mips-unknown-linux-gnu -mattr=+micromips -relocation-model=pic -O3 -filetype=obj -o - %s | \
; RUN: llvm-readelf -r | FileCheck %s --check-prefix=CHECK-READELF ; RUN: llvm-readelf -r | FileCheck %s --check-prefixes=CHECK-READELF,CHECK-READELF-PIC
; RUN: llc -mtriple mips-unknown-linux-gnu -mattr=+micromips -relocation-model=static -O3 -filetype=obj -o - %s | \ ; RUN: llc -mtriple mips-unknown-linux-gnu -mattr=+micromips -relocation-model=static -O3 -filetype=obj -o - %s | \
; RUN: llvm-objdump -s -j .gcc_except_table - | FileCheck %s --check-prefix=CHECK-EXCEPT-TABLE-STATIC ; RUN: llvm-objdump -s -j .gcc_except_table - | FileCheck %s --check-prefix=CHECK-EXCEPT-TABLE-STATIC
; RUN: llc -mtriple mips-unknown-linux-gnu -mattr=+micromips -relocation-model=pic -O3 -filetype=obj -o - %s | \ ; RUN: llc -mtriple mips-unknown-linux-gnu -mattr=+micromips -relocation-model=pic -O3 -filetype=obj -o - %s | \
@ -9,7 +9,8 @@
; CHECK-READELF: .rel.eh_frame ; CHECK-READELF: .rel.eh_frame
; CHECK-READELF: DW.ref.__gxx_personality_v0 ; CHECK-READELF: DW.ref.__gxx_personality_v0
; CHECK-READELF-NEXT: .text ; CHECK-READELF-STATIC-NEXT: R_MIPS_32 00000000 .text
; CHECK-READELF-PIC-NEXT: R_MIPS_PC32
; CHECK-READELF-NEXT: .gcc_except_table ; CHECK-READELF-NEXT: .gcc_except_table
; CHECK-EXCEPT-TABLE-STATIC: 0000 ff9b1501 0c011500 00150e23 01231e00 ...........#.#.. ; CHECK-EXCEPT-TABLE-STATIC: 0000 ff9b1501 0c011500 00150e23 01231e00 ...........#.#..

View File

@ -1,56 +1,96 @@
// Test the bits of .eh_frame on mips that are already implemented correctly. // Test the bits of .eh_frame on mips that are already implemented correctly.
// RUN: llvm-mc -filetype=obj %s -o %t.o -triple mips-unknown-linux-gnu // RUN: llvm-mc -filetype=obj %s -o %t.o -triple mips-unknown-linux-gnu
// RUN: llvm-objdump -r -section=.rel.eh_frame %t.o | FileCheck --check-prefix=REL32 %s // RUN: llvm-readobj -r %t.o | FileCheck --check-prefixes=RELOCS,ABS32 %s
// RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefix=DWARF32 %s // RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefixes=DWARF32,DWARF32_ABS %s
// RUN: llvm-mc -filetype=obj %s -o %t.o -triple mipsel-unknown-linux-gnu // RUN: llvm-mc -filetype=obj %s -o %t.o -triple mipsel-unknown-linux-gnu
// RUN: llvm-objdump -r -section=.rel.eh_frame %t.o | FileCheck --check-prefix=REL32 %s // RUN: llvm-readobj -r %t.o | FileCheck --check-prefixes=RELOCS,ABS32 %s
// RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefix=DWARF32 %s // RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefixes=DWARF32,DWARF32_ABS %s
// RUN: llvm-mc -filetype=obj %s -o %t.o -triple mips64-unknown-linux-gnu // RUN: llvm-mc -filetype=obj %s -o %t.o -triple mips64-unknown-linux-gnu
// RUN: llvm-objdump -r -section=.rela.eh_frame %t.o | FileCheck --check-prefix=REL64 %s // RUN: llvm-readobj -r %t.o | FileCheck --check-prefixes=RELOCS,ABS64 %s
// RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefix=DWARF64 %s // RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefixes=DWARF64,DWARF64_ABS %s
// RUN: llvm-mc -filetype=obj %s -o %t.o -triple mips64el-unknown-linux-gnu // RUN: llvm-mc -filetype=obj %s -o %t.o -triple mips64el-unknown-linux-gnu
// RUN: llvm-objdump -r -section=.rela.eh_frame %t.o | FileCheck --check-prefix=REL64 %s // RUN: llvm-readobj -r %t.o | FileCheck --check-prefixes=RELOCS,ABS64 %s
// RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefix=DWARF64 %s // RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefixes=DWARF64,DWARF64_ABS %s
/// Check that position-indenpendent code use PC-relative relocations:
// RUN: llvm-mc -filetype=obj %s -o %t.o -triple mips-unknown-linux-gnu --position-independent
// RUN: llvm-readobj -r %t.o | FileCheck --check-prefixes=RELOCS,PIC32 %s
// RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefixes=DWARF32,DWARF32_PIC %s
// RUN: llvm-mc -filetype=obj %s -o %t.o -triple mipsel-unknown-linux-gnu --position-independent
// RUN: llvm-readobj -r %t.o | FileCheck --check-prefixes=RELOCS,PIC32 %s
// RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefixes=DWARF32,DWARF32_PIC %s
// RUN: llvm-mc -filetype=obj %s -o %t.o -triple mips64-unknown-linux-gnu --position-independent
// RUN: llvm-readobj -r %t.o | FileCheck --check-prefixes=RELOCS,PIC64 %s
// RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefixes=DWARF64,DWARF64_PIC %s
// RUN: llvm-mc -filetype=obj %s -o %t.o -triple mips64el-unknown-linux-gnu --position-independent
// RUN: llvm-readobj -r %t.o | FileCheck --check-prefixes=RELOCS,PIC64 %s
// RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefixes=DWARF64,DWARF64_PIC %s
/// However using the large code model forces R_MIPS_64 since there is no R_MIPS_PC64 relocation:
// RUN: llvm-mc -filetype=obj %s -o %t.o -triple mips64-unknown-linux-gnu --position-independent --large-code-model
// RUN: llvm-readobj -r %t.o | FileCheck --check-prefixes=RELOCS,ABS64 %s
// RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefixes=DWARF64,DWARF64_ABS %s
// RUN: llvm-mc -filetype=obj %s -o %t.o -triple mips64el-unknown-linux-gnu --position-independent --large-code-model
// RUN: llvm-readobj -r %t.o | FileCheck --check-prefixes=RELOCS,ABS64 %s
// RUN: llvm-dwarfdump -eh-frame %t.o | FileCheck --check-prefixes=DWARF64,DWARF64_ABS %s
func: func:
.cfi_startproc .cfi_startproc
.cfi_endproc .cfi_endproc
// REL32: R_MIPS_32 // RELOCS: Relocations [
// REL64: R_MIPS_64/R_MIPS_NONE/R_MIPS_NONE // RELOCS: Section ({{.+}}) .rel{{a?}}.eh_frame {
// ABS32-NEXT: R_MIPS_32
// ABS64-NEXT: R_MIPS_64/R_MIPS_NONE/R_MIPS_NONE
// PIC32-NEXT: R_MIPS_PC32
// PIC64-NEXT: R_MIPS_PC32/R_MIPS_NONE/R_MIPS_NONE
// RELOCS-NEXT: }
// DWARF32: 00000000 00000010 ffffffff CIE // DWARF32: 00000000 00000010 ffffffff CIE
// DWARF32: Version: 1 // DWARF32-NEXT: Version: 1
// DWARF32: Augmentation: "zR" // DWARF32-NEXT: Augmentation: "zR"
// DWARF32: Code alignment factor: 1 // DWARF32-NEXT: Code alignment factor: 1
// DWARF32: Data alignment factor: -4 // DWARF32-NEXT: Data alignment factor: -4
// DWARF32: Return address column: 31 // DWARF32-NEXT: Return address column: 31
// DWARF32: Augmentation data: 0B // DWARF32_ABS-NEXT: Augmentation data: 0B
// ^^ fde pointer encoding: DW_EH_PE_sdata4 // ^^ fde pointer encoding: DW_EH_PE_sdata4
// DWARF32: DW_CFA_def_cfa_register: reg29 // DWARF32_PIC-NEXT: Augmentation data: 1B
// ^^ fde pointer encoding: DW_EH_PE_pcrel | DW_EH_PE_sdata4
// DWARF32-EMPTY:
// DWARF32-NEXT: DW_CFA_def_cfa_register: reg29
// //
// DWARF32: 00000014 00000010 00000018 FDE cie=00000018 pc=00000000...00000000 // DWARF32: 00000014 00000010 00000018 FDE cie=00000018 pc=00000000...00000000
// DWARF32: DW_CFA_nop: // DWARF32-NEXT: DW_CFA_nop:
// DWARF32: DW_CFA_nop: // DWARF32-NEXT: DW_CFA_nop:
// DWARF32: DW_CFA_nop: // DWARF32-NEXT: DW_CFA_nop:
// DWARF64: 00000000 00000010 ffffffff CIE // DWARF64: 00000000 00000010 ffffffff CIE
// DWARF64: Version: 1 // DWARF64-NEXT: Version: 1
// DWARF64: Augmentation: "zR" // DWARF64-NEXT: Augmentation: "zR"
// DWARF64: Code alignment factor: 1 // DWARF64-NEXT: Code alignment factor: 1
// DWARF64: Data alignment factor: -8 // DWARF64-NEXT: Data alignment factor: -8
// ^^ GAS uses -4. Should be ok as long as // ^^ GAS uses -4. Should be ok as long as
// all offsets we need are a multiple of 8. // all offsets we need are a multiple of 8.
// DWARF64: Return address column: 31 // DWARF64-NEXT: Return address column: 31
// DWARF64: Augmentation data: 0C // DWARF64_ABS-NEXT: Augmentation data: 0C
// ^^ fde pointer encoding: DW_EH_PE_sdata8 // ^^ fde pointer encoding: DW_EH_PE_sdata8
// DWARF64: DW_CFA_def_cfa_register: reg29 // DWARF64_PIC: Augmentation data: 1B
// ^^ fde pointer encoding: DW_EH_PE_pcrel | DW_EH_PE_sdata4
// DWARF64-EMPTY:
// DWARF64-NEXT: DW_CFA_def_cfa_register: reg29
// DWARF64_PIC-NEXT: DW_CFA_nop:
// //
// DWARF64: 00000014 00000018 00000018 FDE cie=00000018 pc=00000000...00000000 // DWARF64_ABS: 00000014 00000018 00000018 FDE cie=00000018 pc=00000000...00000000
// DWARF64: DW_CFA_nop: // DWARF64_PIC: 00000014 00000010 00000018 FDE cie=00000018 pc=00000000...00000000
// DWARF64: DW_CFA_nop: // DWARF64-NEXT: DW_CFA_nop:
// DWARF64: DW_CFA_nop: // DWARF64-NEXT: DW_CFA_nop:
// DWARF64-NEXT: DW_CFA_nop: