Skip to content

Commit 69937a8

Browse files
committed
[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
1 parent 7b639f5 commit 69937a8

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# REQUIRES: aarch64-registered-target
2+
3+
# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t
4+
# RUN: llvm-objcopy %t %t.copy
5+
# RUN: cmp %t %t.copy
6+
7+
.text
8+
.globl _foo, _bar
9+
_foo:
10+
## ARM64_RELOC_ADDEND and ARM64_RELOC_BRANCH26
11+
bl _bar + 123
12+
13+
_bar:
14+
ret
15+
16+
.subsections_via_symbols

llvm/tools/llvm-objcopy/MachO/MachOReader.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Expected<std::vector<std::unique_ptr<Section>>> static extractSections(
9494
S.Content =
9595
StringRef(reinterpret_cast<const char *>(Data->data()), Data->size());
9696

97+
const uint32_t CPUType = MachOObj.getHeader().cputype;
9798
S.Relocations.reserve(S.NReloc);
9899
for (auto RI = MachOObj.section_rel_begin(SecRef->getRawDataRefImpl()),
99100
RE = MachOObj.section_rel_end(SecRef->getRawDataRefImpl());
@@ -102,6 +103,10 @@ Expected<std::vector<std::unique_ptr<Section>>> static extractSections(
102103
R.Symbol = nullptr; // We'll fill this field later.
103104
R.Info = MachOObj.getRelocation(RI->getRawDataRefImpl());
104105
R.Scattered = MachOObj.isRelocationScattered(R.Info);
106+
unsigned Type = MachOObj.getAnyRelocationType(R.Info);
107+
// TODO Support CPU_TYPE_ARM.
108+
R.IsAddend = !R.Scattered && (CPUType == MachO::CPU_TYPE_ARM64 &&
109+
Type == MachO::ARM64_RELOC_ADDEND);
105110
R.Extern = !R.Scattered && MachOObj.getPlainRelocationExternal(R.Info);
106111
S.Relocations.push_back(R);
107112
}
@@ -222,7 +227,7 @@ void MachOReader::setSymbolInRelocationInfo(Object &O) const {
222227
for (LoadCommand &LC : O.LoadCommands)
223228
for (std::unique_ptr<Section> &Sec : LC.Sections)
224229
for (auto &Reloc : Sec->Relocations)
225-
if (!Reloc.Scattered) {
230+
if (!Reloc.Scattered && !Reloc.IsAddend) {
226231
const uint32_t SymbolNum =
227232
Reloc.getPlainRelocationSymbolNum(MachOObj.isLittleEndian());
228233
if (Reloc.Extern) {

llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ void MachOWriter::writeSections() {
261261
Sec->Content.size());
262262
for (size_t Index = 0; Index < Sec->Relocations.size(); ++Index) {
263263
RelocationInfo RelocInfo = Sec->Relocations[Index];
264-
if (!RelocInfo.Scattered) {
264+
if (!RelocInfo.Scattered && !RelocInfo.IsAddend) {
265265
const uint32_t SymbolNum = RelocInfo.Extern
266266
? (*RelocInfo.Symbol)->Index
267267
: (*RelocInfo.Sec)->Index;

llvm/tools/llvm-objcopy/MachO/Object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ struct RelocationInfo {
180180
Optional<const Section *> Sec;
181181
// True if Info is a scattered_relocation_info.
182182
bool Scattered;
183+
// True if the type is an ADDEND. r_symbolnum holds the addend instead of a
184+
// symbol index.
185+
bool IsAddend;
183186
// True if the r_symbolnum points to a section number (i.e. r_extern=0).
184187
bool Extern;
185188
MachO::any_relocation_info Info;

0 commit comments

Comments
 (0)