summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/ELF/Mips/MipsCtorsOrderPass.cpp
blob: 8bf80257fc89f06c2f7d6361ceb94b997e3bad78 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//===- lib/ReaderWriter/ELF/Mips/Mips/CtorsOrderPass.cpp ------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "MipsCtorsOrderPass.h"
#include <algorithm>
#include <climits>

using namespace lld;
using namespace lld::elf;

static bool matchCrtObjName(StringRef objName, StringRef objPath) {
  if (!objPath.endswith(".o"))
    return false;

  // check *<objName> case
  objPath = objPath.drop_back(2);
  if (objPath.endswith(objName))
    return true;

  // check *<objName>? case
  return !objPath.empty() && objPath.drop_back(1).endswith(objName);
}

static int32_t getSectionPriority(StringRef path, StringRef sectionName) {
  // Arrange .ctors/.dtors sections in the following order:
  //   .ctors from crtbegin.o or crtbegin?.o
  //   .ctors from regular object files
  //   .ctors.* (sorted) from regular object files
  //   .ctors from crtend.o or crtend?.o

  if (matchCrtObjName("crtbegin", path))
    return std::numeric_limits<int32_t>::min();
  if (matchCrtObjName("crtend", path))
    return std::numeric_limits<int32_t>::max();

  StringRef num = sectionName.drop_front().rsplit('.').second;

  int32_t priority = std::numeric_limits<int32_t>::min() + 1;
  if (!num.empty())
    num.getAsInteger(10, priority);

  return priority;
}

void MipsCtorsOrderPass::perform(std::unique_ptr<MutableFile> &f) {
  auto definedAtoms = f->definedAtoms();

  auto last = std::stable_partition(definedAtoms.begin(), definedAtoms.end(),
                                    [](const DefinedAtom *atom) {
    if (atom->sectionChoice() != DefinedAtom::sectionCustomRequired)
      return false;

    StringRef name = atom->customSectionName();
    return name.startswith(".ctors") || name.startswith(".dtors");
  });

  std::stable_sort(definedAtoms.begin(), last,
                   [](const DefinedAtom *left, const DefinedAtom *right) {
    StringRef leftSec = left->customSectionName();
    StringRef rightSec = right->customSectionName();

    int32_t leftPriority = getSectionPriority(left->file().path(), leftSec);
    int32_t rightPriority = getSectionPriority(right->file().path(), rightSec);

    return leftPriority < rightPriority;
  });
}
OpenPOWER on IntegriCloud