summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/ELF/ArrayOrderPass.cpp
blob: be9cd4b167d9dad600f3542fbfb691c14448f51e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//===- lib/ReaderWriter/ELF/ArrayOrderPass.cpp ----------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "ArrayOrderPass.h"

#include <algorithm>

namespace lld {
namespace elf {
void ArrayOrderPass::perform(std::unique_ptr<MutableFile> &f) {
  auto definedAtoms = f->definedAtoms();
  std::stable_sort(definedAtoms.begin(), definedAtoms.end(),
                   [](const DefinedAtom *left, const DefinedAtom *right) {
    if (left->sectionChoice() != DefinedAtom::sectionCustomRequired ||
        right->sectionChoice() != DefinedAtom::sectionCustomRequired)
      return false;

    StringRef leftSec = left->customSectionName();
    StringRef rightSec = right->customSectionName();

    // Both sections start with the same array type.
    if (!(leftSec.startswith(".init_array") &&
          rightSec.startswith(".init_array")) &&
        !(leftSec.startswith(".fini_array") &&
          rightSec.startswith(".fini_array")))
      return false;

    // Get priortiy
    uint16_t leftPriority = 0;
    leftSec.rsplit('.').second.getAsInteger(10, leftPriority);
    uint16_t rightPriority = 0;
    rightSec.rsplit('.').second.getAsInteger(10, rightPriority);

    return leftPriority < rightPriority;
  });
}
} // end namespace elf
} // end namespace lld
OpenPOWER on IntegriCloud