[llvm-objcopy][MachO] Handle relocation entries where r_extern is zero
Fix handling of relocations with r_extern == 0.
If r_extern == 0 then r_symbolnum is an index of a section rather than a symbol index.
Patch by Seiya Nuta and Alexander Shaposhnikov.
Test plan: make check-all
Differential revision: https://reviews.llvm.org/D78946
diff --git a/llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp b/llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
index db31ef5..4a8d1ae 100644
--- a/llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
+++ b/llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
@@ -239,11 +239,13 @@
memcpy(B.getBufferStart() + Sec->Offset, Sec->Content.data(),
Sec->Content.size());
for (size_t Index = 0; Index < Sec->Relocations.size(); ++Index) {
- auto RelocInfo = Sec->Relocations[Index];
- if (!RelocInfo.Scattered)
- RelocInfo.setPlainRelocationSymbolNum(RelocInfo.Symbol->Index,
- IsLittleEndian);
-
+ RelocationInfo RelocInfo = Sec->Relocations[Index];
+ if (!RelocInfo.Scattered) {
+ const uint32_t SymbolNum = RelocInfo.Extern
+ ? (*RelocInfo.Symbol)->Index
+ : (*RelocInfo.Section)->Index;
+ RelocInfo.setPlainRelocationSymbolNum(SymbolNum, IsLittleEndian);
+ }
if (IsLittleEndian != sys::IsLittleEndianHost)
MachO::swapStruct(
reinterpret_cast<MachO::any_relocation_info &>(RelocInfo.Info));