comparison lld/MachO/OutputSection.h @ 173:0572611fdcc8 llvm10 llvm12

reorgnization done
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 11:55:54 +0900
parents
children 2e18cbf3894f
comparison
equal deleted inserted replaced
172:9fbae9c8bf63 173:0572611fdcc8
1 //===- OutputSection.h ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLD_MACHO_OUTPUT_SECTION_H
10 #define LLD_MACHO_OUTPUT_SECTION_H
11
12 #include "lld/Common/LLVM.h"
13 #include "llvm/ADT/DenseMap.h"
14
15 namespace lld {
16 namespace macho {
17
18 class InputSection;
19 class OutputSegment;
20
21 // Output sections represent the finalized sections present within the final
22 // linked executable. They can represent special sections (like the symbol
23 // table), or represent coalesced sections from the various inputs given to the
24 // linker with the same segment / section name.
25 class OutputSection {
26 public:
27 enum Kind {
28 MergedKind,
29 SyntheticKind,
30 };
31
32 OutputSection(Kind kind, StringRef name) : name(name), sectionKind(kind) {}
33 virtual ~OutputSection() = default;
34 Kind kind() const { return sectionKind; }
35
36 // These accessors will only be valid after finalizing the section.
37 uint64_t getSegmentOffset() const;
38
39 // How much space the section occupies in the address space.
40 virtual size_t getSize() const = 0;
41 // 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.
43 virtual uint64_t getFileSize() const { return getSize(); }
44
45 // Hidden sections omit header content, but body content may still be present.
46 virtual bool isHidden() const { return false; }
47 // Unneeded sections are omitted entirely (header and body).
48 virtual bool isNeeded() const { return true; }
49
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() {
55 // TODO investigate refactoring synthetic section finalization logic into
56 // overrides of this function.
57 }
58
59 virtual void writeTo(uint8_t *buf) const = 0;
60
61 StringRef name;
62 OutputSegment *parent = nullptr;
63
64 uint32_t index = 0;
65 uint64_t addr = 0;
66 uint64_t fileOff = 0;
67 uint32_t align = 1;
68 uint32_t flags = 0;
69
70 private:
71 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 };
105
106 } // namespace macho
107 } // namespace lld
108
109 #endif