[PATCH] D42839: [MC] Allow overriding whether to output Elf_Rel or Elf_Rela relocations
Alexander Richardson via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 2 02:52:28 PST 2018
arichardson created this revision.
arichardson added reviewers: rafael, ruiu, atanasyan, grimar.
Herald added subscribers: llvm-commits, javed.absar.
This makes it possible to test that tools correctly deal with REL vs RELA
differences in input files. In https://reviews.llvm.org/D42790 I am trying to fix LLD not writing
addends if the default output format uses Elf_Rel but an input contains
relocations in the Elf_Rela format. In order to test this I need to create
object files that use a different relocation format from the output.
Repository:
rL LLVM
https://reviews.llvm.org/D42839
Files:
lib/MC/MCELFObjectTargetWriter.cpp
test/MC/AArch64/elf-override-rela-and-rel.s
Index: test/MC/AArch64/elf-override-rela-and-rel.s
===================================================================
--- /dev/null
+++ test/MC/AArch64/elf-override-rela-and-rel.s
@@ -0,0 +1,45 @@
+// Check that we can override whether to ouput Elf_Rel vs Elf_Rela relocations
+
+// By default we use Elf_Rela relocations:
+// RUN: llvm-mc -filetype=obj -triple aarch64-none-linux-gnu %s -o - | \
+// RUN: llvm-readobj -r -s -section-data - | FileCheck %s -check-prefix RELA
+
+// RELA: Section {
+// RELA: Name: .data
+// RELA: SectionData (
+// RELA-NEXT: 0000: 00000000 00000000 |........|
+// RELA-NEXT: )
+// RELA: Relocations [
+// RELA-NEXT: Section (4) .rela.data {
+// RELA-NEXT: 0x0 R_AARCH64_ABS64 foo 0x123456
+// RELA-NEXT: }
+// RELA-NEXT: ]
+
+// RUN: llvm-mc -filetype=obj -triple aarch64-none-linux-gnu %s -o - -elf-relocation-format=rel | \
+// RUN: llvm-readobj -r -s -section-data - | FileCheck %s -check-prefix REL
+// REL: Section {
+// REL: Name: .data
+// REL: SectionData (
+// REL-NEXT: 0000: 56341200 00000000 |V4......|
+// ^---- Addend 0x123456 for the relocation below
+// REL-NEXT: )
+// REL: Relocations [
+// REL-NEXT: Section (4) .rel.data {
+// The real addend is not printed for REL as it is in .data
+// REL-NEXT: 0x0 R_AARCH64_ABS64 foo 0x0
+// REL-NEXT: }
+// REL-NEXT: ]
+
+// RUN: llvm-mc -filetype=obj -triple aarch64-none-linux-gnu %s -o - -elf-relocation-format=rela | \
+// RUN: llvm-readobj -r -s -section-data - | FileCheck %s -check-prefix RELA
+// RUN: llvm-mc -filetype=obj -triple aarch64-none-linux-gnu %s -o - -elf-relocation-format=default | \
+// RUN: llvm-readobj -r -s -section-data - | FileCheck %s -check-prefix RELA
+
+// Check that we only accept rela, rel, and default for the -elf-relocation-format flag
+// RUN: not llvm-mc -filetype=obj -triple aarch64-none-linux-gnu %s -o - \
+// RUN: -elf-relocation-format=none 2>&1 | FileCheck %s -check-prefix ERR
+// ERR: llvm-mc: for the -elf-relocation-format option: Cannot find option named 'none'
+.extern foo
+
+.data
+.quad foo + 0x123456
\ No newline at end of file
Index: lib/MC/MCELFObjectTargetWriter.cpp
===================================================================
--- lib/MC/MCELFObjectTargetWriter.cpp
+++ lib/MC/MCELFObjectTargetWriter.cpp
@@ -8,14 +8,40 @@
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCELFObjectWriter.h"
+#include "llvm/Support/CommandLine.h"
using namespace llvm;
+enum class ELFRelocationFormat { Default, Rel, Rela };
+static cl::opt<ELFRelocationFormat> RelocationFormat(
+ "elf-relocation-format",
+ cl::desc("Select whether to use REL or "
+ "RELA for relocations in the output object"),
+ cl::values(clEnumValN(ELFRelocationFormat::Default, "default",
+ "Use the default kind for the current target"),
+ clEnumValN(ELFRelocationFormat::Rel, "rel",
+ "Always emit REL relocations"),
+ clEnumValN(ELFRelocationFormat::Rela, "rela",
+ "Always emit RELA relocations")),
+ cl::init(ELFRelocationFormat::Default), cl::Hidden);
+
+static bool shouldUseRelocationAddend(bool Default) {
+ switch (RelocationFormat) {
+ case ELFRelocationFormat::Rel:
+ return false;
+ case ELFRelocationFormat::Rela:
+ return true;
+ default:
+ return Default;
+ }
+}
+
MCELFObjectTargetWriter::MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
uint16_t EMachine_,
bool HasRelocationAddend_)
: OSABI(OSABI_), EMachine(EMachine_),
- HasRelocationAddend(HasRelocationAddend_), Is64Bit(Is64Bit_) {}
+ HasRelocationAddend(shouldUseRelocationAddend(HasRelocationAddend_)),
+ Is64Bit(Is64Bit_) {}
bool MCELFObjectTargetWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
unsigned Type) const {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42839.132556.patch
Type: text/x-patch
Size: 4128 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180202/d5f20cc0/attachment.bin>
More information about the llvm-commits
mailing list