[MachO] Add basic support for local symbols.

llvm-svn: 199155
diff --git a/lld/lib/ReaderWriter/MachO/Atoms.h b/lld/lib/ReaderWriter/MachO/Atoms.h
index 7733b2a..d6e0cf1 100644
--- a/lld/lib/ReaderWriter/MachO/Atoms.h
+++ b/lld/lib/ReaderWriter/MachO/Atoms.h
@@ -18,8 +18,8 @@
 public:
   // FIXME: This constructor should also take the ContentType.
   MachODefinedAtom(const File &f, const StringRef name,
-                   const ArrayRef<uint8_t> content)
-      : SimpleDefinedAtom(f), _name(name), _content(content) {}
+                   const ArrayRef<uint8_t> content, Scope scope)
+      : SimpleDefinedAtom(f), _name(name), _content(content), _scope(scope) {}
 
   virtual uint64_t size() const { return rawContent().size(); }
 
@@ -27,13 +27,14 @@
 
   virtual StringRef name() const { return _name; }
 
-  virtual Scope scope() const { return scopeGlobal; }
+  virtual Scope scope() const { return _scope; }
 
   virtual ArrayRef<uint8_t> rawContent() const { return _content; }
 
 private:
   const StringRef _name;
   const ArrayRef<uint8_t> _content;
+  const Scope _scope;
 };
 } // mach_o
 } // lld
diff --git a/lld/lib/ReaderWriter/MachO/File.h b/lld/lib/ReaderWriter/MachO/File.h
index dad5462..697c2e4 100644
--- a/lld/lib/ReaderWriter/MachO/File.h
+++ b/lld/lib/ReaderWriter/MachO/File.h
@@ -21,8 +21,9 @@
 public:
   MachOFile(StringRef path) : SimpleFile(path) {}
 
-  void addDefinedAtom(StringRef name, ArrayRef<uint8_t> content, bool cpyRefs) {
-    if (cpyRefs) {
+  void addDefinedAtom(StringRef name, ArrayRef<uint8_t> content,
+                      Atom::Scope scope, bool copyRefs) {
+    if (copyRefs) {
       // Make a copy of the atom's name and content that is owned by this file.
       char *s = _allocator.Allocate<char>(name.size());
       memcpy(s, name.data(), name.size());
@@ -32,7 +33,7 @@
       content = llvm::makeArrayRef(bytes, content.size());
     }
     MachODefinedAtom *atom =
-        new (_allocator) MachODefinedAtom(*this, name, content);
+        new (_allocator) MachODefinedAtom(*this, name, content, scope);
     addAtom(*atom);
   }
 
diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
index b53d0de..b91e3a8 100644
--- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
+++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp
@@ -50,9 +50,30 @@
     if (sValue < closestAddr)
       closestAddr = s.value;
   }
+  for (const Symbol &s : normalizedFile.localSymbols) {
+    if (s.sect != symbolSectionIndex)
+      continue;
+    uint64_t sValue = s.value;
+    if (sValue <= symbolAddr)
+      continue;
+    if (sValue < closestAddr)
+      closestAddr = s.value;
+  }
   return closestAddr;
 }
 
+static Atom::Scope atomScope(uint8_t scope) {
+  switch (scope) {
+  case N_EXT:
+    return Atom::scopeGlobal;
+  case N_PEXT | N_EXT:
+    return Atom::scopeLinkageUnit;
+  case 0:
+    return Atom::scopeTranslationUnit;
+  }
+  llvm_unreachable("unknown scope value!");
+}
+
 static void processSymbol(const NormalizedFile &normalizedFile, MachOFile &file,
                           const Symbol &sym, bool copyRefs) {
   // Mach-O symbol table does have size in it, so need to scan ahead
@@ -61,7 +82,7 @@
   uint64_t offset = sym.value - section.address;
   uint64_t size = nextSymbolAddress(normalizedFile, sym) - sym.value;
   ArrayRef<uint8_t> atomContent = section.content.slice(offset, size);
-  file.addDefinedAtom(sym.name, atomContent, copyRefs);
+  file.addDefinedAtom(sym.name, atomContent, atomScope(sym.scope), copyRefs);
 }
 
 static ErrorOr<std::unique_ptr<lld::File>>
@@ -73,8 +94,10 @@
     processSymbol(normalizedFile, *file, sym, copyRefs);
   }
 
-  assert(normalizedFile.localSymbols.empty() &&
-         "local symbols not supported yet!");
+  for (const Symbol &sym : normalizedFile.localSymbols) {
+    processSymbol(normalizedFile, *file, sym, copyRefs);
+  }
+
   assert(normalizedFile.undefinedSymbols.empty() &&
          "undefined symbols not supported yet!");