summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/PECOFF/OrderPass.h
diff options
context:
space:
mode:
authorRui Ueyama <ruiu@google.com>2015-01-29 22:12:50 +0000
committerRui Ueyama <ruiu@google.com>2015-01-29 22:12:50 +0000
commitc4038ab5bf3ee228a2bb0d3e1b2940e7c3318a0c (patch)
tree18df4ad248f296134f61ae2cbc31655e2c9e14fd /lld/lib/ReaderWriter/PECOFF/OrderPass.h
parent64419d414b468f5f1c49cbc4b4edbab1988fb7a8 (diff)
downloadbcm5719-llvm-c4038ab5bf3ee228a2bb0d3e1b2940e7c3318a0c.tar.gz
bcm5719-llvm-c4038ab5bf3ee228a2bb0d3e1b2940e7c3318a0c.zip
PECOFF: Do not use LayoutPass and instead use simpler one.
The LayoutPass is one of the slowest pass. This change is to skip that pass. This change not only improve performance but also improve maintainability of the code because the LayoutPass is pretty complex. Previously we used the LayoutPass to sort all atoms in a specific way, and reorder them again for PE/COFF in GroupedSectionPass. I spent time on improving and fixing bugs in the LayoutPass (e.g. r193029), but the pass is still hard to understand and hard to use. It's better not to depend on that if we don't need. For PE/COFF, we just wanted to sort atoms in the same order as the file order in the command line. The feature we used in the LayoutPass is now simplified to compareByPosition function in OrderPass.cpp. The function is just 5 lines. This patch changes the order of final output because it changes the sort order a bit. The output is still correct, though. llvm-svn: 227500
Diffstat (limited to 'lld/lib/ReaderWriter/PECOFF/OrderPass.h')
-rw-r--r--lld/lib/ReaderWriter/PECOFF/OrderPass.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/lld/lib/ReaderWriter/PECOFF/OrderPass.h b/lld/lib/ReaderWriter/PECOFF/OrderPass.h
new file mode 100644
index 00000000000..8f8b4082421
--- /dev/null
+++ b/lld/lib/ReaderWriter/PECOFF/OrderPass.h
@@ -0,0 +1,75 @@
+//===- lib/ReaderWriter/PECOFF/OrderPass.h -------------------------------===//
+//
+// The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file \brief This pass sorts atoms by section name, so that they will appear
+/// in the correct order in the output.
+///
+/// In COFF, sections will be merged into one section by the linker if their
+/// names are the same after discarding the "$" character and all characters
+/// follow it from their names. The characters following the "$" character
+/// determines the merge order. Assume there's an object file containing four
+/// data sections in the following order.
+///
+/// - .data$2
+/// - .data$3
+/// - .data$1
+/// - .data
+///
+/// In this case, the resulting binary should have ".data" section with the
+/// contents of ".data", ".data$1", ".data$2" and ".data$3" in that order.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLD_READER_WRITER_PE_COFF_ORDER_PASS_H
+#define LLD_READER_WRITER_PE_COFF_ORDER_PASS_H
+
+#include "Atoms.h"
+#include "lld/Core/Parallel.h"
+#include "lld/Core/Pass.h"
+#include <algorithm>
+
+namespace lld {
+namespace pecoff {
+
+static bool compareByPosition(const DefinedAtom *lhs, const DefinedAtom *rhs) {
+ const File *lhsFile = &lhs->file();
+ const File *rhsFile = &rhs->file();
+ if (lhsFile->ordinal() != rhsFile->ordinal())
+ return lhsFile->ordinal() < rhsFile->ordinal();
+ return lhs->ordinal() < rhs->ordinal();
+}
+
+static bool compare(const DefinedAtom *lhs, const DefinedAtom *rhs) {
+ bool lhsCustom = (lhs->sectionChoice() == DefinedAtom::sectionCustomRequired);
+ bool rhsCustom = (rhs->sectionChoice() == DefinedAtom::sectionCustomRequired);
+ if (lhsCustom && rhsCustom) {
+ int cmp = lhs->customSectionName().compare(rhs->customSectionName());
+ if (cmp != 0)
+ return cmp < 0;
+ return compareByPosition(lhs, rhs);
+ }
+ if (lhsCustom && !rhsCustom)
+ return true;
+ if (!lhsCustom && rhsCustom)
+ return false;
+ return compareByPosition(lhs, rhs);
+}
+
+class OrderPass : public lld::Pass {
+public:
+ void perform(std::unique_ptr<MutableFile> &file) override {
+ MutableFile::DefinedAtomRange defined = file->definedAtoms();
+ parallel_sort(defined.begin(), defined.end(), compare);
+ }
+};
+
+} // namespace pecoff
+} // namespace lld
+
+#endif
OpenPOWER on IntegriCloud