]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/ReaderWriter/ELF/Mips/MipsCtorsOrderPass.cpp
Vendor import of lld trunk r233088:
[FreeBSD/FreeBSD.git] / lib / ReaderWriter / ELF / Mips / MipsCtorsOrderPass.cpp
1 //===- lib/ReaderWriter/ELF/Mips/Mips/CtorsOrderPass.cpp ------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "MipsCtorsOrderPass.h"
11 #include <algorithm>
12 #include <climits>
13
14 using namespace lld;
15 using namespace lld::elf;
16
17 static bool matchCrtObjName(StringRef objName, StringRef objPath) {
18   if (!objPath.endswith(".o"))
19     return false;
20
21   // check *<objName> case
22   objPath = objPath.drop_back(2);
23   if (objPath.endswith(objName))
24     return true;
25
26   // check *<objName>? case
27   return !objPath.empty() && objPath.drop_back(1).endswith(objName);
28 }
29
30 static int32_t getSectionPriority(StringRef path, StringRef sectionName) {
31   // Arrange .ctors/.dtors sections in the following order:
32   //   .ctors from crtbegin.o or crtbegin?.o
33   //   .ctors from regular object files
34   //   .ctors.* (sorted) from regular object files
35   //   .ctors from crtend.o or crtend?.o
36
37   if (matchCrtObjName("crtbegin", path))
38     return std::numeric_limits<int32_t>::min();
39   if (matchCrtObjName("crtend", path))
40     return std::numeric_limits<int32_t>::max();
41
42   StringRef num = sectionName.drop_front().rsplit('.').second;
43
44   int32_t priority = std::numeric_limits<int32_t>::min() + 1;
45   if (!num.empty())
46     num.getAsInteger(10, priority);
47
48   return priority;
49 }
50
51 void MipsCtorsOrderPass::perform(std::unique_ptr<MutableFile> &f) {
52   auto definedAtoms = f->definedAtoms();
53
54   auto last = std::stable_partition(definedAtoms.begin(), definedAtoms.end(),
55                                     [](const DefinedAtom *atom) {
56     if (atom->sectionChoice() != DefinedAtom::sectionCustomRequired)
57       return false;
58
59     StringRef name = atom->customSectionName();
60     return name.startswith(".ctors") || name.startswith(".dtors");
61   });
62
63   std::stable_sort(definedAtoms.begin(), last,
64                    [](const DefinedAtom *left, const DefinedAtom *right) {
65     StringRef leftSec = left->customSectionName();
66     StringRef rightSec = right->customSectionName();
67
68     int32_t leftPriority = getSectionPriority(left->file().path(), leftSec);
69     int32_t rightPriority = getSectionPriority(right->file().path(), rightSec);
70
71     return leftPriority < rightPriority;
72   });
73 }