Make sure we correctly merge all N_FUN symbols with non-stab entries even if there are multiple symbols with the same name.

<rdar://problem/15831292>

llvm-svn: 199344
diff --git a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
index 7a34515..e1972a2 100644
--- a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
+++ b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
@@ -1987,7 +1987,7 @@
         std::vector<uint32_t> N_INCL_indexes;
         std::vector<uint32_t> N_BRAC_indexes;
         std::vector<uint32_t> N_COMM_indexes;
-        typedef std::map <uint64_t, uint32_t> ValueToSymbolIndexMap;
+        typedef std::multimap <uint64_t, uint32_t> ValueToSymbolIndexMap;
         typedef std::map <uint32_t, uint32_t> NListIndexToSymbolIndexMap;
         typedef std::map <const char *, uint32_t> ConstNameToSymbolIndexMap;
         ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
@@ -2322,7 +2322,7 @@
                                                             type = eSymbolTypeCode;
                                                             symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
 
-                                                            N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
+                                                            N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
                                                             // We use the current number of symbols in the symbol table in lieu of
                                                             // using nlist_idx in case we ever start trimming entries out
                                                             N_FUN_indexes.push_back(sym_idx);
@@ -2347,7 +2347,7 @@
 
                                                     case N_STSYM:
                                                         // static symbol: name,,n_sect,type,address
-                                                        N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
+                                                        N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
                                                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
                                                         type = eSymbolTypeData;
                                                         break;
@@ -2855,25 +2855,33 @@
                                                         // If we do find a match, and the name matches, then we
                                                         // can merge the two into just the function symbol to avoid
                                                         // duplicate entries in the symbol table
-                                                        ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
-                                                        if (pos != N_FUN_addr_to_sym_idx.end())
+                                                        std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
+                                                        range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
+                                                        if (range.first != range.second)
                                                         {
-                                                            if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
+                                                            bool found_it = false;
+                                                            for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
                                                             {
-                                                                m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
-                                                                // We just need the flags from the linker symbol, so put these flags
-                                                                // into the N_FUN flags to avoid duplicate symbols in the symbol table
-                                                                sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
-                                                                sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
-                                                                if (resolver_addresses.find(nlist.n_value) != resolver_adresses.end())
-                                                                    sym[pos->second].SetType (eSymbolTypeResolver);
-                                                                sym[sym_idx].Clear();
-                                                                continue;
+                                                                if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
+                                                                {
+                                                                    m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
+                                                                    // We just need the flags from the linker symbol, so put these flags
+                                                                    // into the N_FUN flags to avoid duplicate symbols in the symbol table
+                                                                    sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
+                                                                    sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
+                                                                    if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
+                                                                        sym[pos->second].SetType (eSymbolTypeResolver);
+                                                                    sym[sym_idx].Clear();
+                                                                    found_it = true;
+                                                                    break;
+                                                                }
                                                             }
+                                                            if (found_it)
+                                                                continue;
                                                         }
                                                         else
                                                         {
-                                                            if (resolver_addresses.find(nlist.n_value) != resolver_adresses.end())
+                                                            if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
                                                                 sym[sym_idx].SetType (eSymbolTypeResolver);
                                                         }
                                                     }
@@ -2883,19 +2891,27 @@
                                                         // If we do find a match, and the name matches, then we
                                                         // can merge the two into just the Static symbol to avoid
                                                         // duplicate entries in the symbol table
-                                                        ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
-                                                        if (pos != N_STSYM_addr_to_sym_idx.end())
+                                                        std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
+                                                        range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
+                                                        if (range.first != range.second)
                                                         {
-                                                            if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
+                                                            bool found_it = false;
+                                                            for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
                                                             {
-                                                                m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
-                                                                // We just need the flags from the linker symbol, so put these flags
-                                                                // into the N_STSYM flags to avoid duplicate symbols in the symbol table
-                                                                sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
-                                                                sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
-                                                                sym[sym_idx].Clear();
-                                                                continue;
+                                                                if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
+                                                                {
+                                                                    m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
+                                                                    // We just need the flags from the linker symbol, so put these flags
+                                                                    // into the N_STSYM flags to avoid duplicate symbols in the symbol table
+                                                                    sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
+                                                                    sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
+                                                                    sym[sym_idx].Clear();
+                                                                    found_it = true;
+                                                                    break;
+                                                                }
                                                             }
+                                                            if (found_it)
+                                                                continue;
                                                         }
                                                         else
                                                         {
@@ -3071,7 +3087,7 @@
                             type = eSymbolTypeCode;
                             symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
 
-                            N_FUN_addr_to_sym_idx[nlist.n_value] = sym_idx;
+                            N_FUN_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
                             // We use the current number of symbols in the symbol table in lieu of
                             // using nlist_idx in case we ever start trimming entries out
                             N_FUN_indexes.push_back(sym_idx);
@@ -3096,7 +3112,7 @@
 
                     case N_STSYM:
                         // static symbol: name,,n_sect,type,address
-                        N_STSYM_addr_to_sym_idx[nlist.n_value] = sym_idx;
+                        N_STSYM_addr_to_sym_idx.insert(std::make_pair(nlist.n_value, sym_idx));
                         symbol_section = section_info.GetSection (nlist.n_sect, nlist.n_value);
                         type = eSymbolTypeData;
                         break;
@@ -3607,21 +3623,29 @@
                             // If we do find a match, and the name matches, then we
                             // can merge the two into just the function symbol to avoid
                             // duplicate entries in the symbol table
-                            ValueToSymbolIndexMap::const_iterator pos = N_FUN_addr_to_sym_idx.find (nlist.n_value);
-                            if (pos != N_FUN_addr_to_sym_idx.end())
+                            std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
+                            range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
+                            if (range.first != range.second)
                             {
-                                if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
+                                bool found_it = false;
+                                for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
                                 {
-                                    m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
-                                    // We just need the flags from the linker symbol, so put these flags
-                                    // into the N_FUN flags to avoid duplicate symbols in the symbol table
-                                    sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
-                                    sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
-                                    if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
-                                        sym[pos->second].SetType (eSymbolTypeResolver);
-                                    sym[sym_idx].Clear();
-                                    continue;
+                                    if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
+                                    {
+                                        m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
+                                        // We just need the flags from the linker symbol, so put these flags
+                                        // into the N_FUN flags to avoid duplicate symbols in the symbol table
+                                        sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
+                                        sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
+                                        if (resolver_addresses.find(nlist.n_value) != resolver_addresses.end())
+                                            sym[pos->second].SetType (eSymbolTypeResolver);
+                                        sym[sym_idx].Clear();
+                                        found_it = true;
+                                        break;
+                                    }
                                 }
+                                if (found_it)
+                                    continue;
                             }
                             else
                             {
@@ -3635,19 +3659,27 @@
                             // If we do find a match, and the name matches, then we
                             // can merge the two into just the Static symbol to avoid
                             // duplicate entries in the symbol table
-                            ValueToSymbolIndexMap::const_iterator pos = N_STSYM_addr_to_sym_idx.find (nlist.n_value);
-                            if (pos != N_STSYM_addr_to_sym_idx.end())
+                            std::pair<ValueToSymbolIndexMap::const_iterator, ValueToSymbolIndexMap::const_iterator> range;
+                            range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
+                            if (range.first != range.second)
                             {
-                                if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
+                                bool found_it = false;
+                                for (ValueToSymbolIndexMap::const_iterator pos = range.first; pos != range.second; ++pos)
                                 {
-                                    m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
-                                    // We just need the flags from the linker symbol, so put these flags
-                                    // into the N_STSYM flags to avoid duplicate symbols in the symbol table
-                                    sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
-                                    sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
-                                    sym[sym_idx].Clear();
-                                    continue;
+                                    if (sym[sym_idx].GetMangled().GetName(Mangled::ePreferMangled) == sym[pos->second].GetMangled().GetName(Mangled::ePreferMangled))
+                                    {
+                                        m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
+                                        // We just need the flags from the linker symbol, so put these flags
+                                        // into the N_STSYM flags to avoid duplicate symbols in the symbol table
+                                        sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
+                                        sym[pos->second].SetFlags (nlist.n_type << 16 | nlist.n_desc);
+                                        sym[sym_idx].Clear();
+                                        found_it = true;
+                                        break;
+                                    }
                                 }
+                                if (found_it)
+                                    continue;
                             }
                             else
                             {