From 8870ad24399680811045a34d9b5438ebc4d812f8 Mon Sep 17 00:00:00 2001 From: Nick Kledzik Date: Wed, 12 Nov 2014 23:34:23 +0000 Subject: [mach-o] Sort GOT entries by name to make links reproducible The GOT slots were being laid out in a random order by the GOTPass which caused randomness in the output file. Note: With this change lld now bootstraps on darwin. That is: 1) link lld using system linker to make lld.1 2) link lld using lld.1 to make lld.2 3) link lld using lld.2 to make lld.3 Now lld.2 and lld.3 are identical. llvm-svn: 221831 --- lld/lib/ReaderWriter/MachO/GOTPass.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'lld/lib/ReaderWriter/MachO/GOTPass.cpp') diff --git a/lld/lib/ReaderWriter/MachO/GOTPass.cpp b/lld/lib/ReaderWriter/MachO/GOTPass.cpp index b86160efc24..b5381115c01 100644 --- a/lld/lib/ReaderWriter/MachO/GOTPass.cpp +++ b/lld/lib/ReaderWriter/MachO/GOTPass.cpp @@ -52,8 +52,8 @@ namespace mach_o { // class GOTEntryAtom : public SimpleDefinedAtom { public: - GOTEntryAtom(const File &file, bool is64) - : SimpleDefinedAtom(file), _is64(is64) { } + GOTEntryAtom(const File &file, bool is64, StringRef name) + : SimpleDefinedAtom(file), _is64(is64), _name(name) { } ContentType contentType() const override { return DefinedAtom::typeGOT; @@ -77,8 +77,13 @@ public: return llvm::makeArrayRef(zeros, size()); } + StringRef slotName() const { + return _name; + } + private: const bool _is64; + StringRef _name; }; @@ -116,9 +121,17 @@ private: } } - // add all created GOT Atoms to master file + // Sort and add all created GOT Atoms to master file + std::vector entries; + entries.reserve(_targetToGOT.size()); for (auto &it : _targetToGOT) - mergedFile->addAtom(*it.second); + entries.push_back(it.second); + std::sort(entries.begin(), entries.end(), + [](const GOTEntryAtom *left, const GOTEntryAtom *right) { + return (left->slotName().compare(right->slotName()) < 0); + }); + for (const GOTEntryAtom *slot : entries) + mergedFile->addAtom(*slot); } bool shouldReplaceTargetWithGOTAtom(const Atom *target, bool canBypassGOT) { @@ -142,7 +155,7 @@ private: auto pos = _targetToGOT.find(target); if (pos == _targetToGOT.end()) { GOTEntryAtom *gotEntry = new (_file.allocator()) - GOTEntryAtom(_file, _context.is64Bit()); + GOTEntryAtom(_file, _context.is64Bit(), target->name()); _targetToGOT[target] = gotEntry; const ArchHandler::ReferenceInfo &nlInfo = _archHandler.stubInfo(). nonLazyPointerReferenceToBinder; -- cgit v1.2.3