Skip to content

Instantly share code, notes, and snippets.

@nuta
Created December 16, 2019 03:39
Show Gist options
  • Select an option

  • Save nuta/380fecb26dd42a5b170b58b76272da63 to your computer and use it in GitHub Desktop.

Select an option

Save nuta/380fecb26dd42a5b170b58b76272da63 to your computer and use it in GitHub Desktop.
diff --git a/llvm/test/tools/llvm-objcopy/MachO/copy-relocations.test b/llvm/test/tools/llvm-objcopy/MachO/copy-relocations.test
new file mode 100644
index 00000000000..8bfac24ee93
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/MachO/copy-relocations.test
@@ -0,0 +1,111 @@
+## Show that llvm-objcopy copies relocation entries where r_extern=0.
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-objcopy %t %t2
+# RUN: cmp %t %t2
+
+--- !mach-o
+FileHeader:
+ magic: 0xFEEDFACF
+ cputype: 0x01000007
+ cpusubtype: 0x00000003
+ filetype: 0x00000001
+ ncmds: 4
+ sizeofcmds: 440
+ flags: 0x00002000
+ reserved: 0x00000000
+LoadCommands:
+ - cmd: LC_SEGMENT_64
+ cmdsize: 312
+ segname: ''
+ vmaddr: 0
+ vmsize: 120
+ fileoff: 472
+ filesize: 120
+ maxprot: 7
+ initprot: 7
+ nsects: 3
+ flags: 0
+ Sections:
+ - sectname: __text
+ segname: __TEXT
+ addr: 0x0000000000000000
+ size: 18
+ offset: 0x000001D8
+ align: 4
+ reloff: 0x00000000
+ nreloc: 0
+ flags: 0x80000400
+ reserved1: 0x00000000
+ reserved2: 0x00000000
+ reserved3: 0x00000000
+ content: 554889E5C745FC00000000B87B0000005DC3
+ - sectname: __compact_unwind
+ segname: __LD
+ addr: 0x0000000000000018
+ size: 32
+ offset: 0x000001F0
+ align: 3
+ reloff: 0x00000250
+ nreloc: 1
+ flags: 0x02000000
+ reserved1: 0x00000000
+ reserved2: 0x00000000
+ reserved3: 0x00000000
+ content: '0000000000000000120000000000000100000000000000000000000000000000'
+ - sectname: __eh_frame
+ segname: __TEXT
+ addr: 0x0000000000000038
+ size: 64
+ offset: 0x00000210
+ align: 3
+ reloff: 0x00000000
+ nreloc: 0
+ flags: 0x6800000B
+ reserved1: 0x00000000
+ reserved2: 0x00000000
+ reserved3: 0x00000000
+ content: 1400000000000000017A520001781001100C070890010000240000001C000000A8FFFFFFFFFFFFFF120000000000000000410E108602430D0600000000000000
+ - cmd: LC_BUILD_VERSION
+ cmdsize: 24
+ platform: 1
+ minos: 658944
+ sdk: 0
+ ntools: 0
+ - cmd: LC_SYMTAB
+ cmdsize: 24
+ symoff: 600
+ nsyms: 1
+ stroff: 616
+ strsize: 8
+ - cmd: LC_DYSYMTAB
+ cmdsize: 80
+ ilocalsym: 0
+ nlocalsym: 0
+ iextdefsym: 0
+ nextdefsym: 1
+ iundefsym: 1
+ nundefsym: 0
+ tocoff: 0
+ ntoc: 0
+ modtaboff: 0
+ nmodtab: 0
+ extrefsymoff: 0
+ nextrefsyms: 0
+ indirectsymoff: 0
+ nindirectsyms: 0
+ extreloff: 0
+ nextrel: 0
+ locreloff: 0
+ nlocrel: 0
+LinkEditData:
+ NameList:
+ - n_strx: 1
+ n_type: 0x0F
+ n_sect: 1
+ n_desc: 0
+ n_value: 0
+ StringTable:
+ - ''
+ - _main
+ - ''
+...
diff --git a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp b/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
index cf8c93b1cea..db2eabf1eda 100644
--- a/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
+++ b/llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
@@ -103,6 +103,7 @@ extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd,
R.Symbol = nullptr; // We'll fill this field later.
R.Info = MachOObj.getRelocation(RI->getRawDataRefImpl());
R.Scattered = MachOObj.isRelocationScattered(R.Info);
+ R.Extern = R.Scattered ? false : MachOObj.getPlainRelocationExternal(R.Info);
S.Relocations.push_back(R);
}
@@ -208,7 +209,7 @@ void MachOReader::setSymbolInRelocationInfo(Object &O) const {
for (auto &LC : O.LoadCommands)
for (auto &Sec : LC.Sections)
for (auto &Reloc : Sec.Relocations)
- if (!Reloc.Scattered) {
+ if (!Reloc.Scattered && Reloc.Extern) {
auto *Info = reinterpret_cast<MachO::relocation_info *>(&Reloc.Info);
Reloc.Symbol = O.SymTable.getSymbolByIndex(Info->r_symbolnum);
}
diff --git a/llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp b/llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
index 0d9590612ec..5e91cb798aa 100644
--- a/llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
+++ b/llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
@@ -240,7 +240,7 @@ void MachOWriter::writeSections() {
Sec.Content.size());
for (size_t Index = 0; Index < Sec.Relocations.size(); ++Index) {
auto RelocInfo = Sec.Relocations[Index];
- if (!RelocInfo.Scattered) {
+ if (!RelocInfo.Scattered && RelocInfo.Extern) {
auto *Info =
reinterpret_cast<MachO::relocation_info *>(&RelocInfo.Info);
Info->r_symbolnum = RelocInfo.Symbol->Index;
diff --git a/llvm/tools/llvm-objcopy/MachO/Object.h b/llvm/tools/llvm-objcopy/MachO/Object.h
index 1ce488126b3..4b8fd38169e 100644
--- a/llvm/tools/llvm-objcopy/MachO/Object.h
+++ b/llvm/tools/llvm-objcopy/MachO/Object.h
@@ -148,6 +148,8 @@ struct RelocationInfo {
const SymbolEntry *Symbol;
// True if Info is a scattered_relocation_info.
bool Scattered;
+ // True if the r_symbolnum points to a section number (i.e. r_extern=0).
+ bool Extern;
MachO::any_relocation_info Info;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment