comparison lld/MachO/OutputSection.h @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 0572611fdcc8
children 5f17cb93ff66
comparison
equal deleted inserted replaced
173:0572611fdcc8 207:2e18cbf3894f
10 #define LLD_MACHO_OUTPUT_SECTION_H 10 #define LLD_MACHO_OUTPUT_SECTION_H
11 11
12 #include "lld/Common/LLVM.h" 12 #include "lld/Common/LLVM.h"
13 #include "llvm/ADT/DenseMap.h" 13 #include "llvm/ADT/DenseMap.h"
14 14
15 #include <limits>
16
15 namespace lld { 17 namespace lld {
16 namespace macho { 18 namespace macho {
17 19
18 class InputSection; 20 class InputSection;
19 class OutputSegment; 21 class OutputSegment;
23 // table), or represent coalesced sections from the various inputs given to the 25 // table), or represent coalesced sections from the various inputs given to the
24 // linker with the same segment / section name. 26 // linker with the same segment / section name.
25 class OutputSection { 27 class OutputSection {
26 public: 28 public:
27 enum Kind { 29 enum Kind {
28 MergedKind, 30 ConcatKind,
29 SyntheticKind, 31 SyntheticKind,
30 }; 32 };
31 33
32 OutputSection(Kind kind, StringRef name) : name(name), sectionKind(kind) {} 34 OutputSection(Kind kind, StringRef name) : name(name), sectionKind(kind) {}
33 virtual ~OutputSection() = default; 35 virtual ~OutputSection() = default;
35 37
36 // These accessors will only be valid after finalizing the section. 38 // These accessors will only be valid after finalizing the section.
37 uint64_t getSegmentOffset() const; 39 uint64_t getSegmentOffset() const;
38 40
39 // How much space the section occupies in the address space. 41 // How much space the section occupies in the address space.
40 virtual size_t getSize() const = 0; 42 virtual uint64_t getSize() const = 0;
41 // How much space the section occupies in the file. Most sections are copied 43 // How much space the section occupies in the file. Most sections are copied
42 // as-is so their file size is the same as their address space size. 44 // as-is so their file size is the same as their address space size.
43 virtual uint64_t getFileSize() const { return getSize(); } 45 virtual uint64_t getFileSize() const { return getSize(); }
44 46
45 // Hidden sections omit header content, but body content may still be present. 47 // Hidden sections omit header content, but body content may still be present.
46 virtual bool isHidden() const { return false; } 48 virtual bool isHidden() const { return false; }
47 // Unneeded sections are omitted entirely (header and body). 49 // Unneeded sections are omitted entirely (header and body).
48 virtual bool isNeeded() const { return true; } 50 virtual bool isNeeded() const { return true; }
49 51
50 // Some sections may allow coalescing other raw input sections.
51 virtual void mergeInput(InputSection *input);
52
53 // Specifically finalizes addresses and section size, not content.
54 virtual void finalize() { 52 virtual void finalize() {
55 // TODO investigate refactoring synthetic section finalization logic into 53 // TODO investigate refactoring synthetic section finalization logic into
56 // overrides of this function. 54 // overrides of this function.
57 } 55 }
58 56
59 virtual void writeTo(uint8_t *buf) const = 0; 57 virtual void writeTo(uint8_t *buf) const = 0;
60 58
61 StringRef name; 59 StringRef name;
62 OutputSegment *parent = nullptr; 60 OutputSegment *parent = nullptr;
61 // For output sections that don't have explicit ordering requirements, their
62 // output order should be based on the order of the input sections they
63 // contain.
64 int inputOrder = std::numeric_limits<int>::max();
63 65
64 uint32_t index = 0; 66 uint32_t index = 0;
65 uint64_t addr = 0; 67 uint64_t addr = 0;
66 uint64_t fileOff = 0; 68 uint64_t fileOff = 0;
67 uint32_t align = 1; 69 uint32_t align = 1;
68 uint32_t flags = 0; 70 uint32_t flags = 0;
71 uint32_t reserved1 = 0;
72 uint32_t reserved2 = 0;
69 73
70 private: 74 private:
71 Kind sectionKind; 75 Kind sectionKind;
72 };
73
74 class OutputSectionComparator {
75 public:
76 OutputSectionComparator(uint32_t segmentOrder,
77 const std::vector<StringRef> &sectOrdering)
78 : segmentOrder(segmentOrder) {
79 for (uint32_t j = 0, m = sectOrdering.size(); j < m; ++j)
80 sectionOrdering[sectOrdering[j]] = j;
81 }
82
83 uint32_t sectionOrder(StringRef secname) {
84 auto sectIt = sectionOrdering.find(secname);
85 if (sectIt != sectionOrdering.end())
86 return sectIt->second;
87 return sectionOrdering.size();
88 }
89
90 // Sort sections within a common segment, which stores them in
91 // a MapVector of section name -> section
92 bool operator()(const std::pair<StringRef, OutputSection *> &a,
93 const std::pair<StringRef, OutputSection *> &b) {
94 return sectionOrder(a.first) < sectionOrder(b.first);
95 }
96
97 bool operator<(const OutputSectionComparator &b) {
98 return segmentOrder < b.segmentOrder;
99 }
100
101 private:
102 uint32_t segmentOrder;
103 llvm::DenseMap<StringRef, uint32_t> sectionOrdering;
104 }; 76 };
105 77
106 } // namespace macho 78 } // namespace macho
107 } // namespace lld 79 } // namespace lld
108 80