[PATCH] D66280: [llvm-objcopy][MachO] Support indirect symbol table

Seiya Nuta via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 15 02:40:25 PDT 2019


seiya created this revision.
Herald added subscribers: llvm-commits, abrachet, jakehehrlich.
Herald added a reviewer: alexshap.
Herald added a reviewer: rupprecht.
Herald added a reviewer: jhenderson.
Herald added a project: LLVM.

Parse the indirect symbol table and update the indexes of
symbol entries in the table in the writer in case they have
been changed.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66280

Files:
  llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
  llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
  llvm/tools/llvm-objcopy/MachO/Object.h


Index: llvm/tools/llvm-objcopy/MachO/Object.h
===================================================================
--- llvm/tools/llvm-objcopy/MachO/Object.h
+++ llvm/tools/llvm-objcopy/MachO/Object.h
@@ -112,8 +112,19 @@
   const SymbolEntry *getSymbolByIndex(uint32_t Index) const;
 };
 
+struct IndirectSymbolEntry {
+  uint32_t OriginalIndex;
+  /// The Symbol referenced by this entry. It's None if the index is
+  /// INDIRECT_SYMBOL_LOCAL or INDIRECT_SYMBOL_ABS.
+  Optional<const SymbolEntry *> Symbol;
+
+  IndirectSymbolEntry(uint32_t OriginalIndex,
+                      Optional<const SymbolEntry *> Symbol)
+      : OriginalIndex(OriginalIndex), Symbol(Symbol) {}
+};
+
 struct IndirectSymbolTable {
-  std::vector<uint32_t> Symbols;
+  std::vector<IndirectSymbolEntry> Symbols;
 };
 
 /// The location of the string table inside the binary is described by LC_SYMTAB
Index: llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
===================================================================
--- llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
+++ llvm/tools/llvm-objcopy/MachO/MachOWriter.cpp
@@ -369,11 +369,14 @@
       O.LoadCommands[*O.DySymTabCommandIndex]
           .MachOLoadCommand.dysymtab_command_data;
 
-  char *Out = (char *)B.getBufferStart() + DySymTabCommand.indirectsymoff;
-  assert((DySymTabCommand.nindirectsyms == O.IndirectSymTable.Symbols.size()) &&
-         "Incorrect indirect symbol table size");
-  memcpy(Out, O.IndirectSymTable.Symbols.data(),
-         sizeof(uint32_t) * O.IndirectSymTable.Symbols.size());
+  uint32_t *Out =
+      (uint32_t *)(B.getBufferStart() + DySymTabCommand.indirectsymoff);
+  for (const IndirectSymbolEntry &Sym : O.IndirectSymTable.Symbols) {
+    uint32_t Entry = (Sym.Symbol) ? (*Sym.Symbol)->Index : Sym.OriginalIndex;
+    if (IsLittleEndian != sys::IsLittleEndianHost)
+      sys::swapByteOrder(Entry);
+    *Out++ = Entry;
+  }
 }
 
 void MachOWriter::writeDataInCodeData() {
Index: llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
===================================================================
--- llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
+++ llvm/tools/llvm-objcopy/MachO/MachOReader.cpp
@@ -256,9 +256,16 @@
 
 void MachOReader::readIndirectSymbolTable(Object &O) const {
   MachO::dysymtab_command DySymTab = MachOObj.getDysymtabLoadCommand();
-  for (uint32_t i = 0; i < DySymTab.nindirectsyms; ++i)
-    O.IndirectSymTable.Symbols.push_back(
-        MachOObj.getIndirectSymbolTableEntry(DySymTab, i));
+  for (uint32_t i = 0; i < DySymTab.nindirectsyms; ++i) {
+    uint32_t Index = MachOObj.getIndirectSymbolTableEntry(DySymTab, i);
+    if (Index == MachO::INDIRECT_SYMBOL_LOCAL ||
+        Index == MachO::INDIRECT_SYMBOL_ABS ||
+        Index == (MachO::INDIRECT_SYMBOL_LOCAL | MachO::INDIRECT_SYMBOL_ABS))
+      O.IndirectSymTable.Symbols.emplace_back(Index, None);
+    else
+      O.IndirectSymTable.Symbols.emplace_back(
+          Index, O.SymTable.getSymbolByIndex(Index));
+  }
 }
 
 std::unique_ptr<Object> MachOReader::create() const {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66280.215351.patch
Type: text/x-patch
Size: 3039 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190815/03c97698/attachment-0001.bin>


More information about the llvm-commits mailing list