aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>2015-04-14 19:49:43 +0100
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2015-04-15 19:17:51 +0100
commit369a63011c2f7fe2b98119b673915e5120d30954 (patch)
tree92a06e361a0576f2ac216e344a0bd48a141a24ea
parentf2e3295132ae23a82cc080e35d1ef7edf640b852 (diff)
ELF/ARM: Ignore R_ARM_V4BX but allow linkinglinaro-arm32
This patch allow the ARM relocation R_ARM_V4BX to be processed by lld, although it is not really handled in the static relocation code. The relocation is in the form: Relocation section '.rel.text' at offset 0x428 contains 4 entries: Offset Info Type Sym.Value Sym. Name 00000014 00000028 R_ARM_V4BX Meaning it does have a direct target, but rather references to an absolute section *ABS* (in this exemple to the .text segment itself). It makes the target Atom after file parse to not have a associated pointer and thus generating a derrefence NULL point in ELFFile<ELFT>::findAtom. Current approach is just ignore and return nullptr in such cases. The problem relies that default GCC configuration for arm-linux-gnueabi{hf} emits the relocation for the asm: -- .syntax unified .arm .p2align 2 .type fn, %function fn: ldr r3, .LGOT ldr r2, .LGOT+4 .LPIC: add r3, pc, r3 ldr r2, [r3, r2] cmp r2, #0 bxeq lr b __start__ .LGOT: .word _GLOBAL_OFFSET_TABLE_-(.LPIC+8) .word __start__(GOT) -- But only with the option -march=armv4 (which is the default GCC configuration). For arm5 and forward the relocation is not created. This a special relocation (defined miscellaneous for ARM) that instruct the linker to replace the bx instruction into a mov. GNU linker has some options related to which substitution it can create for such cases. With this patch I can dynamically link an application against a GLIBC arm-linux-gnueabi system configured with default GCC.
-rw-r--r--lib/ReaderWriter/ELF/ARM/ARMRelocationHandler.cpp5
-rw-r--r--lib/ReaderWriter/ELF/ELFFile.cpp2
-rw-r--r--lib/ReaderWriter/ELF/SectionChunks.cpp5
-rw-r--r--test/elf/ARM/rel-v4bx.test76
4 files changed, 85 insertions, 3 deletions
diff --git a/lib/ReaderWriter/ELF/ARM/ARMRelocationHandler.cpp b/lib/ReaderWriter/ELF/ARM/ARMRelocationHandler.cpp
index ce5f14c9..bacdba56 100644
--- a/lib/ReaderWriter/ELF/ARM/ARMRelocationHandler.cpp
+++ b/lib/ReaderWriter/ELF/ARM/ARMRelocationHandler.cpp
@@ -514,7 +514,7 @@ std::error_code ARMTargetRelocationHandler::applyRelocation(
// Flags that the relocation addresses Thumb instruction
bool thumb = false;
- if (const auto *definedAtom = dyn_cast<DefinedAtom>(ref.target())) {
+ if (const auto *definedAtom = dyn_cast_or_null<DefinedAtom>(ref.target())) {
thumb = isThumbCode(definedAtom);
}
@@ -596,6 +596,9 @@ std::error_code ARMTargetRelocationHandler::applyRelocation(
case R_ARM_IRELATIVE:
// Runtime only relocations. Ignore here.
break;
+ case R_ARM_V4BX:
+ // TODO implement
+ break;
default:
return make_unhandled_reloc_error();
}
diff --git a/lib/ReaderWriter/ELF/ELFFile.cpp b/lib/ReaderWriter/ELF/ELFFile.cpp
index ab2ec605..64c44dca 100644
--- a/lib/ReaderWriter/ELF/ELFFile.cpp
+++ b/lib/ReaderWriter/ELF/ELFFile.cpp
@@ -38,7 +38,7 @@ Atom *ELFFile<ELFT>::findAtom(const Elf_Sym *sourceSym,
const Elf_Sym *targetSym) {
// Return the atom for targetSym if we can do so.
Atom *target = _symbolToAtomMapping.lookup(targetSym);
- if (target->definition() != Atom::definitionRegular)
+ if (!target || target->definition() != Atom::definitionRegular)
return target;
Atom::Scope scope = llvm::cast<DefinedAtom>(target)->scope();
if (scope == DefinedAtom::scopeTranslationUnit)
diff --git a/lib/ReaderWriter/ELF/SectionChunks.cpp b/lib/ReaderWriter/ELF/SectionChunks.cpp
index 0598cda7..e7a12361 100644
--- a/lib/ReaderWriter/ELF/SectionChunks.cpp
+++ b/lib/ReaderWriter/ELF/SectionChunks.cpp
@@ -127,10 +127,13 @@ void AtomSection<ELFT>::printError(const std::string &errorStr,
kindValStr = "unknown";
}
+ StringRef targetName = "undefined";
+ if (ref.target())
+ targetName = ref.target()->name();
std::string errStr =
(Twine(errorStr) + " in file " + atom._atom->file().path() +
": reference from " + atom._atom->name() + "+" +
- Twine(ref.offsetInAtom()) + " to " + ref.target()->name() + "+" +
+ Twine(ref.offsetInAtom()) + " to " + targetName + "+" +
Twine(ref.addend()) + " of type " + Twine(ref.kindValue()) + " (" +
kindValStr + ")\n")
.str();
diff --git a/test/elf/ARM/rel-v4bx.test b/test/elf/ARM/rel-v4bx.test
new file mode 100644
index 00000000..b843d88c
--- /dev/null
+++ b/test/elf/ARM/rel-v4bx.test
@@ -0,0 +1,76 @@
+# REQUIRES: arm
+
+# Check handling of R_ARM_V4BX relocation.
+# RUN: yaml2obj -format=elf %s > %t-o.o
+# RUN: lld -flavor gnu -target arm -m armelf_linux_eabi \
+# RUN: --noinhibit-exec %t-o.o -o %t
+# RUN: llvm-objdump -d -triple=armv4t %t | FileCheck %s
+
+# CHECK: Disassembly of section .text:
+# CHECK: main:
+# CHECK: 400114: 03 30 8f e0 add r3, pc, r3
+# CHECK: 400118: 02 20 93 e7 ldr r2, [r3, r2]
+# CHECK: 40011c: 00 00 52 e3 cmp r2, #0
+# The following instruction creates a RM_ARM_V4BX relocation that
+# allows the linker to replace the bx instruction to a ARMv4
+# compatible one. Currently lld does not change it.
+# CHECK: 400120: 1e ff 2f 01 bxeq lr
+
+---
+FileHeader:
+ Class: ELFCLASS32
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_ARM
+ Flags: [ EF_ARM_EABI_VER5 ]
+Sections:
+ - Name: .text
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ AddressAlign: 0x0000000000000004
+ Content: 03308FE0022093E7000052E31EFF2F01
+ - Name: .rel.text
+ Type: SHT_REL
+ Flags: [ SHF_INFO_LINK ]
+ Link: .symtab
+ AddressAlign: 0x0000000000000004
+ Info: .text
+ Relocations:
+ - Offset: 0x000000000000000C
+ Symbol: ''
+ Type: R_ARM_V4BX
+ - Name: .data
+ Type: SHT_PROGBITS
+ Flags: [ SHF_WRITE, SHF_ALLOC ]
+ AddressAlign: 0x0000000000000001
+ Content: ''
+ - Name: .bss
+ Type: SHT_NOBITS
+ Flags: [ SHF_WRITE, SHF_ALLOC ]
+ AddressAlign: 0x0000000000000001
+ Content: ''
+ - Name: .ARM.attributes
+ Type: SHT_ARM_ATTRIBUTES
+ AddressAlign: 0x0000000000000001
+ Content: 4115000000616561626900010B000000060208010901
+Symbols:
+ Local:
+ - Name: .text
+ Type: STT_SECTION
+ Section: .text
+ - Name: .data
+ Type: STT_SECTION
+ Section: .data
+ - Name: .bss
+ Type: STT_SECTION
+ Section: .bss
+ - Name: '$a'
+ Section: .text
+ - Name: .ARM.attributes
+ Type: STT_SECTION
+ Section: .ARM.attributes
+ Global:
+ - Name: main
+ Type: STT_FUNC
+ Section: .text
+...