[llvm] 69937a8 - [llvm-objcopy][MachO] Support ARM64_RELOC_ADDEND
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 29 11:23:36 PDT 2021
Author: Fangrui Song
Date: 2021-06-29T11:23:30-07:00
New Revision: 69937a8080bc4828f0b317cd54a03ed2704b661a
URL: https://github.com/llvm/llvm-project/commit/69937a8080bc4828f0b317cd54a03ed2704b661a
DIFF: https://github.com/llvm/llvm-project/commit/69937a8080bc4828f0b317cd54a03ed2704b661a.diff
LOG: [llvm-objcopy][MachO] Support ARM64_RELOC_ADDEND
An ARM64_RELOC_ADDEND relocation reuses the symbol field for the addend value.
We should pass through such relocations.
Reviewed By: alexander-shaposhnikov
Differential Revision: https://reviews.llvm.org/D104967
Added:
llvm/test/tools/llvm-objcopy/MachO/arm64-relocs.s
Modified:
llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
llvm/tools/llvm-objcopy/MachO/Object.h
Removed:
################################################################################
diff --git a/llvm/test/tools/llvm-objcopy/MachO/arm64-relocs.s b/llvm/test/tools/llvm-objcopy/MachO/arm64-relocs.s
new file mode 100644
index 0000000000000..269926666eb37
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/MachO/arm64-relocs.s
@@ -0,0 +1,16 @@
+# REQUIRES: aarch64-registered-target
+
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t
+# RUN: llvm-objcopy %t %t.copy
+# RUN: cmp %t %t.copy
+
+.text
+.globl _foo, _bar
+_foo:
+ ## ARM64_RELOC_ADDEND and ARM64_RELOC_BRANCH26
+ bl _bar + 123
+
+_bar:
+ ret
+
+.subsections_via_symbols
diff --git a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp b/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
index 050dd976f3f35..d1f87bde12403 100644
--- a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
+++ b/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
@@ -94,6 +94,7 @@ Expected<std::vector<std::unique_ptr<Section>>> static extractSections(
S.Content =
StringRef(reinterpret_cast<const char *>(Data->data()), Data->size());
+ const uint32_t CPUType = MachOObj.getHeader().cputype;
S.Relocations.reserve(S.NReloc);
for (auto RI = MachOObj.section_rel_begin(SecRef->getRawDataRefImpl()),
RE = MachOObj.section_rel_end(SecRef->getRawDataRefImpl());
@@ -102,6 +103,10 @@ Expected<std::vector<std::unique_ptr<Section>>> static extractSections(
R.Symbol = nullptr; // We'll fill this field later.
R.Info = MachOObj.getRelocation(RI->getRawDataRefImpl());
R.Scattered = MachOObj.isRelocationScattered(R.Info);
+ unsigned Type = MachOObj.getAnyRelocationType(R.Info);
+ // TODO Support CPU_TYPE_ARM.
+ R.IsAddend = !R.Scattered && (CPUType == MachO::CPU_TYPE_ARM64 &&
+ Type == MachO::ARM64_RELOC_ADDEND);
R.Extern = !R.Scattered && MachOObj.getPlainRelocationExternal(R.Info);
S.Relocations.push_back(R);
}
@@ -222,7 +227,7 @@ void MachOReader::setSymbolInRelocationInfo(Object &O) const {
for (LoadCommand &LC : O.LoadCommands)
for (std::unique_ptr<Section> &Sec : LC.Sections)
for (auto &Reloc : Sec->Relocations)
- if (!Reloc.Scattered) {
+ if (!Reloc.Scattered && !Reloc.IsAddend) {
const uint32_t SymbolNum =
Reloc.getPlainRelocationSymbolNum(MachOObj.isLittleEndian());
if (Reloc.Extern) {
diff --git a/llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp b/llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
index 483703de352f8..24a9d28dfbd96 100644
--- a/llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
+++ b/llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
@@ -261,7 +261,7 @@ void MachOWriter::writeSections() {
Sec->Content.size());
for (size_t Index = 0; Index < Sec->Relocations.size(); ++Index) {
RelocationInfo RelocInfo = Sec->Relocations[Index];
- if (!RelocInfo.Scattered) {
+ if (!RelocInfo.Scattered && !RelocInfo.IsAddend) {
const uint32_t SymbolNum = RelocInfo.Extern
? (*RelocInfo.Symbol)->Index
: (*RelocInfo.Sec)->Index;
diff --git a/llvm/tools/llvm-objcopy/MachO/Object.h b/llvm/tools/llvm-objcopy/MachO/Object.h
index 0bb4b344b2eb0..978bd80e97b39 100644
--- a/llvm/tools/llvm-objcopy/MachO/Object.h
+++ b/llvm/tools/llvm-objcopy/MachO/Object.h
@@ -180,6 +180,9 @@ struct RelocationInfo {
Optional<const Section *> Sec;
// True if Info is a scattered_relocation_info.
bool Scattered;
+ // True if the type is an ADDEND. r_symbolnum holds the addend instead of a
+ // symbol index.
+ bool IsAddend;
// True if the r_symbolnum points to a section number (i.e. r_extern=0).
bool Extern;
MachO::any_relocation_info Info;
More information about the llvm-commits
mailing list