Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -45,6 +45,9 @@ // For --sort-section and linkerscript sorting rules. enum class SortSectionPolicy { Default, None, Alignment, Name, Priority }; +// For --target2 +enum class Target2Policy { Abs, Rel, GotRel }; + struct SymbolVersion { llvm::StringRef Name; bool IsExternCpp; @@ -131,6 +134,7 @@ SortSectionPolicy SortSection; StripPolicy Strip = StripPolicy::None; UnresolvedPolicy UnresolvedSymbols; + Target2Policy Target2 = Target2Policy::GotRel; BuildIdKind BuildId = BuildIdKind::None; ELFKind EKind = ELFNoneKind; uint16_t DefaultSymbolVersion = llvm::ELF::VER_NDX_GLOBAL; Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -345,6 +345,20 @@ return UnresolvedPolicy::ReportError; } +static Target2Policy getTarget2Option(opt::InputArgList &Args) { + if (auto* Arg = Args.getLastArg(OPT_target2)) { + StringRef S = Arg->getValue(); + if (S == "rel") + return Target2Policy::Rel; + if (S == "abs") + return Target2Policy::Abs; + if (S == "got-rel") + return Target2Policy::GotRel; + error("unknown --target2 option: " + S); + } + return Target2Policy::GotRel; +} + static bool isOutputFormatBinary(opt::InputArgList &Args) { if (auto *Arg = Args.getLastArg(OPT_oformat)) { StringRef S = Arg->getValue(); @@ -456,7 +470,7 @@ Config->NoGnuUnique = Args.hasArg(OPT_no_gnu_unique); Config->NoUndefinedVersion = Args.hasArg(OPT_no_undefined_version); Config->Nostdlib = Args.hasArg(OPT_nostdlib); - Config->Pie = Args.hasArg(OPT_pie); + Config->Pie = getArg(Args, OPT_pie, OPT_nopie, false); Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections); Config->Relocatable = Args.hasArg(OPT_relocatable); Config->SaveTemps = Args.hasArg(OPT_save_temps); @@ -550,6 +564,8 @@ Config->UnresolvedSymbols = getUnresolvedSymbolOption(Args); + Config->Target2 = getTarget2Option(Args); + if (auto *Arg = Args.getLastArg(OPT_dynamic_list)) if (Optional Buffer = readFile(Arg->getValue())) parseDynamicList(*Buffer); Index: ELF/Options.td =================================================================== --- ELF/Options.td +++ ELF/Options.td @@ -127,6 +127,8 @@ def noinhibit_exec: F<"noinhibit-exec">, HelpText<"Retain the executable output file whenever it is still usable">; +def nopie: F<"nopie">, HelpText<"Do not create a position independent executable">; + def no_undefined: F<"no-undefined">, HelpText<"Report unresolved symbols even if the linker is creating a shared library">; @@ -175,6 +177,8 @@ def target1_abs: F<"target1-abs">, HelpText<"Interpret R_ARM_TARGET1 as R_ARM_ABS32">; +def target2: J<"target2=">, MetaVarName<"">, HelpText<"Interpret R_ARM_TARGET2 as , where is one of rel, abs, or got-rel.">; + def threads: F<"threads">, HelpText<"Enable use of threads">; def trace: F<"trace">, HelpText<"Print the names of the input files">; Index: ELF/Target.cpp =================================================================== --- ELF/Target.cpp +++ ELF/Target.cpp @@ -1528,6 +1528,14 @@ NeedsThunks = true; } +RelExpr getTarget2Expr() { + if (Config->Target2 == Target2Policy::Rel) + return R_PC; + if (Config->Target2 == Target2Policy::Abs) + return R_ABS; + return R_GOT_PC; +} + RelExpr ARMTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const { switch (Type) { default: @@ -1554,6 +1562,8 @@ return R_GOT_PC; case R_ARM_TARGET1: return Config->Target1Rel ? R_PC : R_ABS; + case R_ARM_TARGET2: + return getTarget2Expr(); case R_ARM_TLS_GD32: return R_TLSGD_PC; case R_ARM_TLS_LDM32: @@ -1660,6 +1670,7 @@ case R_ARM_GOT_PREL: case R_ARM_REL32: case R_ARM_TARGET1: + case R_ARM_TARGET2: case R_ARM_TLS_GD32: case R_ARM_TLS_IE32: case R_ARM_TLS_LDM32: @@ -1789,6 +1800,7 @@ case R_ARM_GOT_PREL: case R_ARM_REL32: case R_ARM_TARGET1: + case R_ARM_TARGET2: case R_ARM_TLS_GD32: case R_ARM_TLS_LDM32: case R_ARM_TLS_LDO32: Index: test/ELF/arm-target2.s =================================================================== --- /dev/null +++ test/ELF/arm-target2.s @@ -0,0 +1,60 @@ +// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o +// RUN: ld.lld %t.o -o %t 2>&1 +// RUN: llvm-objdump -s -triple=armv7a-none-linux-gnueabi %t | FileCheck %s +// RUN: ld.lld %t.o --target2=got-rel -o %t2 2>&1 +// RUN: llvm-objdump -s -triple=armv7a-none-linux-gnueabi %t2 | FileCheck %s +// RUN: ld.lld %t.o --target2=abs -o %t3 2>&1 +// RUN: llvm-objdump -s -triple=armv7a-none-linux-gnueabi %t3 | FileCheck -check-prefix=CHECK-ABS %s +// RUN: ld.lld %t.o --target2=rel -o %t4 2>&1 +// RUN: llvm-objdump -s -triple=armv7a-none-linux-gnueabi %t4 | FileCheck -check-prefix=CHECK-REL %s +// REQUIRES: arm + +// The R_ARM_TARGET2 is present in .ARM.extab sections. It can be handled as +// either R_ARM_ABS32, R_ARM_REL32 or R_ARM_GOT_PREL. For ARM linux the default +// is R_ARM_GOT_PREL. The other two options are primarily used for bare-metal, +// they can be selected with the --target2=abs or --target2=rel option. + .syntax unified + .text + .globl _start + .align 2 +_start: + .type function, %function + .fnstart + bx lr + .personality __gxx_personality_v0 + .handlerdata + .word _ZTIi(TARGET2) + .text + .fnend + .global __gxx_personality_v0 + .type function, %function +__gxx_personality_v0: + bx lr + + .rodata +_ZTIi: .word 0 + +// CHECK: Contents of section .ARM.extab: +// 1011c + 1ee4 = 12000 = .got +// CHECK-NEXT: 10114 f00e0000 b0b0b000 e41e0000 + +// CHECK-ABS: Contents of section .ARM.extab: +// 100e8 = .rodata +// CHECK-ABS-NEXT: 100d4 300f0000 b0b0b000 e8000100 + +// CHECK-REL: Contents of section .ARM.extab: +// 100dc + c = 100e8 = .rodata +// CHECK-REL-NEXT: 100d4 300f0000 b0b0b000 0c000000 + +// CHECK: Contents of section .rodata: +// CHECK-NEXT: 10128 00000000 + +// CHECK-ABS: Contents of section .rodata: +// CHECK-ABS-NEXT: 100e8 00000000 + +// CHECK-REL: Contents of section .rodata: +// CHECK-REL-NEXT: 100e8 00000000 + +// CHECK: Contents of section .got: +// 10128 = _ZTIi +// CHECK-NEXT: 12000 28010100 Index: test/ELF/pie.s =================================================================== --- test/ELF/pie.s +++ test/ELF/pie.s @@ -1,5 +1,12 @@ # REQUIRES: x86 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o + +## Default is no PIE. +# RUN: ld.lld %t1.o -o %t +# RUN: llvm-readobj -file-headers -sections -program-headers -symbols -r %t \ +# RUN: | FileCheck %s --check-prefix=NOPIE + +## Check -pie. # RUN: ld.lld -pie %t1.o -o %t # RUN: llvm-readobj -file-headers -sections -program-headers -symbols -r %t | FileCheck %s @@ -98,5 +105,10 @@ # CHECK-NEXT: Alignment: 8 # CHECK-NEXT: } +## Check -nopie +# RUN: ld.lld -nopie %t1.o -o %t2 +# RUN: llvm-readobj -file-headers -r %t2 | FileCheck %s --check-prefix=NOPIE +# NOPIE-NOT: Type: SharedObject + .globl _start _start: