Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 1 | //===- OutputSections.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 "OutputSections.h" |
| 11 | #include "Config.h" |
Rui Ueyama | 95642b9 | 2016-11-01 23:09:07 +0000 | [diff] [blame] | 12 | #include "LinkerScript.h" |
Rui Ueyama | 9381eb1 | 2016-12-18 14:06:06 +0000 | [diff] [blame] | 13 | #include "Memory.h" |
Rui Ueyama | fbbde54 | 2016-06-29 09:08:02 +0000 | [diff] [blame] | 14 | #include "Strings.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 15 | #include "SymbolTable.h" |
Rui Ueyama | e8a6102 | 2016-11-05 23:05:47 +0000 | [diff] [blame] | 16 | #include "SyntheticSections.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 17 | #include "Target.h" |
Rui Ueyama | 244a435 | 2016-12-03 21:24:51 +0000 | [diff] [blame] | 18 | #include "Threads.h" |
George Rimar | dbf9339 | 2017-04-17 08:58:12 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compression.h" |
George Rimar | f6bc65a | 2016-01-15 13:34:52 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Dwarf.h" |
Rui Ueyama | 3a41be2 | 2016-04-07 22:49:21 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MD5.h" |
Igor Kudrin | 1b0d706 | 2015-10-22 08:21:35 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MathExtras.h" |
Rafael Espindola | a42b3bc | 2016-09-27 16:43:49 +0000 | [diff] [blame] | 23 | #include "llvm/Support/SHA1.h" |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 24 | |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Rui Ueyama | dbcfedb | 2016-02-09 21:46:11 +0000 | [diff] [blame] | 26 | using namespace llvm::dwarf; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 27 | using namespace llvm::object; |
Rafael Espindola | a662738 | 2015-10-06 23:56:53 +0000 | [diff] [blame] | 28 | using namespace llvm::support::endian; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 29 | using namespace llvm::ELF; |
| 30 | |
| 31 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 32 | using namespace lld::elf; |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 33 | |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 34 | uint8_t Out::First; |
Rui Ueyama | 9d1bacb1 | 2017-02-27 02:31:26 +0000 | [diff] [blame] | 35 | OutputSection *Out::Opd; |
| 36 | uint8_t *Out::OpdBuf; |
| 37 | PhdrEntry *Out::TlsPhdr; |
| 38 | OutputSection *Out::DebugInfo; |
| 39 | OutputSection *Out::ElfHeader; |
| 40 | OutputSection *Out::ProgramHeaders; |
| 41 | OutputSection *Out::PreinitArray; |
| 42 | OutputSection *Out::InitArray; |
| 43 | OutputSection *Out::FiniArray; |
| 44 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 45 | uint32_t OutputSection::getPhdrFlags() const { |
Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 46 | uint32_t Ret = PF_R; |
| 47 | if (Flags & SHF_WRITE) |
| 48 | Ret |= PF_W; |
| 49 | if (Flags & SHF_EXECINSTR) |
| 50 | Ret |= PF_X; |
| 51 | return Ret; |
| 52 | } |
| 53 | |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 54 | template <class ELFT> |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 55 | void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) { |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 56 | Shdr->sh_entsize = Entsize; |
Rafael Espindola | 3770763 | 2017-03-07 14:55:52 +0000 | [diff] [blame] | 57 | Shdr->sh_addralign = Alignment; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 58 | Shdr->sh_type = Type; |
| 59 | Shdr->sh_offset = Offset; |
| 60 | Shdr->sh_flags = Flags; |
| 61 | Shdr->sh_info = Info; |
| 62 | Shdr->sh_link = Link; |
Rafael Espindola | ea590d9 | 2017-02-08 15:19:03 +0000 | [diff] [blame] | 63 | Shdr->sh_addr = Addr; |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 64 | Shdr->sh_size = Size; |
| 65 | Shdr->sh_name = ShName; |
Rui Ueyama | c63c1db | 2016-03-13 06:50:33 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 68 | OutputSection::OutputSection(StringRef Name, uint32_t Type, uint64_t Flags) |
Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 69 | : SectionBase(Output, Name, Flags, /*Entsize*/ 0, /*Alignment*/ 1, Type, |
| 70 | /*Info*/ 0, |
Rafael Espindola | 660c9ab | 2017-05-05 21:34:26 +0000 | [diff] [blame] | 71 | /*Link*/ 0), |
| 72 | SectionIndex(INT_MAX) {} |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 73 | |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 74 | static bool compareByFilePosition(InputSection *A, InputSection *B) { |
Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 75 | // Synthetic doesn't have link order dependecy, stable_sort will keep it last |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 76 | if (A->kind() == InputSectionBase::Synthetic || |
| 77 | B->kind() == InputSectionBase::Synthetic) |
Peter Smith | 719eb8e | 2016-11-24 11:43:55 +0000 | [diff] [blame] | 78 | return false; |
George Rimar | 9353e2d | 2017-03-21 08:29:48 +0000 | [diff] [blame] | 79 | auto *LA = cast<InputSection>(A->getLinkOrderDep()); |
| 80 | auto *LB = cast<InputSection>(B->getLinkOrderDep()); |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 81 | OutputSection *AOut = LA->OutSec; |
| 82 | OutputSection *BOut = LB->OutSec; |
Rafael Espindola | 4d2d9c0 | 2016-11-18 19:02:15 +0000 | [diff] [blame] | 83 | if (AOut != BOut) |
| 84 | return AOut->SectionIndex < BOut->SectionIndex; |
| 85 | return LA->OutSecOff < LB->OutSecOff; |
| 86 | } |
| 87 | |
Rui Ueyama | 066c4ab | 2017-04-19 11:31:58 +0000 | [diff] [blame] | 88 | // Compress section contents if this section contains debug info. |
George Rimar | dbf9339 | 2017-04-17 08:58:12 +0000 | [diff] [blame] | 89 | template <class ELFT> void OutputSection::maybeCompress() { |
Rui Ueyama | 066c4ab | 2017-04-19 11:31:58 +0000 | [diff] [blame] | 90 | typedef typename ELFT::Chdr Elf_Chdr; |
| 91 | |
| 92 | // Compress only DWARF debug sections. |
Rui Ueyama | 196adec | 2017-04-19 11:58:59 +0000 | [diff] [blame] | 93 | if (!Config->CompressDebugSections || (Flags & SHF_ALLOC) || |
Rui Ueyama | 066c4ab | 2017-04-19 11:31:58 +0000 | [diff] [blame] | 94 | !Name.startswith(".debug_")) |
George Rimar | dbf9339 | 2017-04-17 08:58:12 +0000 | [diff] [blame] | 95 | return; |
| 96 | |
Rui Ueyama | 066c4ab | 2017-04-19 11:31:58 +0000 | [diff] [blame] | 97 | // Create a section header. |
Rui Ueyama | 07c62c1 | 2017-04-19 11:32:13 +0000 | [diff] [blame] | 98 | ZDebugHeader.resize(sizeof(Elf_Chdr)); |
| 99 | auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data()); |
Rui Ueyama | 066c4ab | 2017-04-19 11:31:58 +0000 | [diff] [blame] | 100 | Hdr->ch_type = ELFCOMPRESS_ZLIB; |
| 101 | Hdr->ch_size = Size; |
| 102 | Hdr->ch_addralign = Alignment; |
George Rimar | dbf9339 | 2017-04-17 08:58:12 +0000 | [diff] [blame] | 103 | |
Rui Ueyama | 066c4ab | 2017-04-19 11:31:58 +0000 | [diff] [blame] | 104 | // Write section contents to a temporary buffer and compress it. |
| 105 | std::vector<uint8_t> Buf(Size); |
| 106 | writeTo<ELFT>(Buf.data()); |
| 107 | if (Error E = zlib::compress(toStringRef(Buf), CompressedData)) |
George Rimar | dbf9339 | 2017-04-17 08:58:12 +0000 | [diff] [blame] | 108 | fatal("compress failed: " + llvm::toString(std::move(E))); |
| 109 | |
Rui Ueyama | 066c4ab | 2017-04-19 11:31:58 +0000 | [diff] [blame] | 110 | // Update section headers. |
| 111 | Size = sizeof(Elf_Chdr) + CompressedData.size(); |
| 112 | Flags |= SHF_COMPRESSED; |
George Rimar | dbf9339 | 2017-04-17 08:58:12 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 115 | template <class ELFT> void OutputSection::finalize() { |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 116 | if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) { |
George Rimar | 9353e2d | 2017-03-21 08:29:48 +0000 | [diff] [blame] | 117 | std::sort(Sections.begin(), Sections.end(), compareByFilePosition); |
George Rimar | f98c5c1 | 2017-03-16 10:24:54 +0000 | [diff] [blame] | 118 | assignOffsets(); |
Rafael Espindola | 4d2d9c0 | 2016-11-18 19:02:15 +0000 | [diff] [blame] | 119 | |
Rafael Espindola | 933fcab | 2016-11-17 23:16:39 +0000 | [diff] [blame] | 120 | // We must preserve the link order dependency of sections with the |
| 121 | // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We |
| 122 | // need to translate the InputSection sh_link to the OutputSection sh_link, |
| 123 | // all InputSections in the OutputSection have the same dependency. |
George Rimar | 9353e2d | 2017-03-21 08:29:48 +0000 | [diff] [blame] | 124 | if (auto *D = this->Sections.front()->getLinkOrderDep()) |
Eugene Leviant | a96d902 | 2016-11-16 10:02:27 +0000 | [diff] [blame] | 125 | this->Link = D->OutSec->SectionIndex; |
Peter Smith | 580ba95 | 2016-10-21 11:25:33 +0000 | [diff] [blame] | 126 | } |
Eugene Leviant | 22eb026 | 2016-11-14 09:16:00 +0000 | [diff] [blame] | 127 | |
Rafael Espindola | 933fcab | 2016-11-17 23:16:39 +0000 | [diff] [blame] | 128 | uint32_t Type = this->Type; |
Rui Ueyama | d57e74b7 | 2017-03-17 23:29:01 +0000 | [diff] [blame] | 129 | if (!Config->CopyRelocs || (Type != SHT_RELA && Type != SHT_REL)) |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 130 | return; |
Rafael Espindola | 933fcab | 2016-11-17 23:16:39 +0000 | [diff] [blame] | 131 | |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 132 | InputSection *First = Sections[0]; |
Rui Ueyama | 9320cb0 | 2017-02-27 02:56:02 +0000 | [diff] [blame] | 133 | if (isa<SyntheticSection>(First)) |
Rafael Espindola | 82f00ec | 2017-02-16 14:23:43 +0000 | [diff] [blame] | 134 | return; |
| 135 | |
Eugene Leviant | 9230db9 | 2016-11-17 09:16:34 +0000 | [diff] [blame] | 136 | this->Link = In<ELFT>::SymTab->OutSec->SectionIndex; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 137 | // sh_info for SHT_REL[A] sections should contain the section header index of |
| 138 | // the section to which the relocation applies. |
George Rimar | 1ec03e4 | 2017-03-21 09:13:27 +0000 | [diff] [blame] | 139 | InputSectionBase *S = First->getRelocatedSection(); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 140 | this->Info = S->OutSec->SectionIndex; |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 141 | } |
Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 142 | |
George Rimar | d86a4e5 | 2017-05-08 10:18:12 +0000 | [diff] [blame] | 143 | static uint64_t updateOffset(uint64_t Off, InputSection *S) { |
| 144 | Off = alignTo(Off, S->Alignment); |
| 145 | S->OutSecOff = Off; |
| 146 | return Off + S->getSize(); |
| 147 | } |
| 148 | |
Rafael Espindola | dc8eb81 | 2017-04-07 01:40:21 +0000 | [diff] [blame] | 149 | void OutputSection::addSection(InputSection *S) { |
| 150 | assert(S->Live); |
Rui Ueyama | 40845e6 | 2015-12-26 05:51:07 +0000 | [diff] [blame] | 151 | Sections.push_back(S); |
| 152 | S->OutSec = this; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 153 | this->updateAlignment(S->Alignment); |
Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 154 | |
George Rimar | d86a4e5 | 2017-05-08 10:18:12 +0000 | [diff] [blame] | 155 | // The actual offsets will be computed by assignAddresses. For now, use |
| 156 | // crude approximation so that it is at least easy for other code to know the |
| 157 | // section order. It is also used to calculate the output section size early |
| 158 | // for compressed debug sections. |
| 159 | this->Size = updateOffset(Size, S); |
| 160 | |
Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 161 | // If this section contains a table of fixed-size entries, sh_entsize |
| 162 | // holds the element size. Consequently, if this contains two or more |
| 163 | // input sections, all of them must have the same sh_entsize. However, |
| 164 | // you can put different types of input sections into one output |
| 165 | // sectin by using linker scripts. I don't know what to do here. |
| 166 | // Probably we sholuld handle that as an error. But for now we just |
| 167 | // pick the largest sh_entsize. |
| 168 | this->Entsize = std::max(this->Entsize, S->Entsize); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 171 | // This function is called after we sort input sections |
| 172 | // and scan relocations to setup sections' offsets. |
George Rimar | f98c5c1 | 2017-03-16 10:24:54 +0000 | [diff] [blame] | 173 | void OutputSection::assignOffsets() { |
George Rimar | efc31dd | 2017-03-01 11:10:53 +0000 | [diff] [blame] | 174 | uint64_t Off = 0; |
George Rimar | d86a4e5 | 2017-05-08 10:18:12 +0000 | [diff] [blame] | 175 | for (InputSection *S : Sections) |
| 176 | Off = updateOffset(Off, S); |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 177 | this->Size = Off; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 180 | void OutputSection::sort(std::function<int(InputSectionBase *S)> Order) { |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 181 | typedef std::pair<unsigned, InputSection *> Pair; |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 182 | auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; }; |
| 183 | |
| 184 | std::vector<Pair> V; |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 185 | for (InputSection *S : Sections) |
George Rimar | 1a33c0f | 2016-11-10 09:05:20 +0000 | [diff] [blame] | 186 | V.push_back({Order(S), S}); |
| 187 | std::stable_sort(V.begin(), V.end(), Comp); |
| 188 | Sections.clear(); |
| 189 | for (Pair &P : V) |
| 190 | Sections.push_back(P.second); |
| 191 | } |
| 192 | |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 193 | // Sorts input sections by section name suffixes, so that .foo.N comes |
| 194 | // before .foo.M if N < M. Used to sort .{init,fini}_array.N sections. |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 195 | // We want to keep the original order if the priorities are the same |
| 196 | // because the compiler keeps the original initialization order in a |
| 197 | // translation unit and we need to respect that. |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 198 | // For more detail, read the section of the GCC's manual about init_priority. |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 199 | void OutputSection::sortInitFini() { |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 200 | // Sort sections by priority. |
Rafael Espindola | c404d50 | 2017-02-23 02:32:18 +0000 | [diff] [blame] | 201 | sort([](InputSectionBase *S) { return getPriority(S->Name); }); |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 202 | } |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 203 | |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 204 | // Returns true if S matches /Filename.?\.o$/. |
| 205 | static bool isCrtBeginEnd(StringRef S, StringRef Filename) { |
| 206 | if (!S.endswith(".o")) |
| 207 | return false; |
| 208 | S = S.drop_back(2); |
| 209 | if (S.endswith(Filename)) |
| 210 | return true; |
| 211 | return !S.empty() && S.drop_back().endswith(Filename); |
| 212 | } |
| 213 | |
| 214 | static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); } |
| 215 | static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); } |
| 216 | |
| 217 | // .ctors and .dtors are sorted by this priority from highest to lowest. |
| 218 | // |
| 219 | // 1. The section was contained in crtbegin (crtbegin contains |
| 220 | // some sentinel value in its .ctors and .dtors so that the runtime |
| 221 | // can find the beginning of the sections.) |
| 222 | // |
| 223 | // 2. The section has an optional priority value in the form of ".ctors.N" |
| 224 | // or ".dtors.N" where N is a number. Unlike .{init,fini}_array, |
| 225 | // they are compared as string rather than number. |
| 226 | // |
| 227 | // 3. The section is just ".ctors" or ".dtors". |
| 228 | // |
| 229 | // 4. The section was contained in crtend, which contains an end marker. |
| 230 | // |
| 231 | // In an ideal world, we don't need this function because .init_array and |
| 232 | // .ctors are duplicate features (and .init_array is newer.) However, there |
| 233 | // are too many real-world use cases of .ctors, so we had no choice to |
| 234 | // support that with this rather ad-hoc semantics. |
Rafael Espindola | 774ea7d | 2017-02-23 16:49:07 +0000 | [diff] [blame] | 235 | static bool compCtors(const InputSection *A, const InputSection *B) { |
Rafael Espindola | a2e3fee | 2017-02-24 13:19:33 +0000 | [diff] [blame] | 236 | bool BeginA = isCrtbegin(A->File->getName()); |
| 237 | bool BeginB = isCrtbegin(B->File->getName()); |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 238 | if (BeginA != BeginB) |
| 239 | return BeginA; |
Rafael Espindola | a2e3fee | 2017-02-24 13:19:33 +0000 | [diff] [blame] | 240 | bool EndA = isCrtend(A->File->getName()); |
| 241 | bool EndB = isCrtend(B->File->getName()); |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 242 | if (EndA != EndB) |
| 243 | return EndB; |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 244 | StringRef X = A->Name; |
| 245 | StringRef Y = B->Name; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 246 | assert(X.startswith(".ctors") || X.startswith(".dtors")); |
| 247 | assert(Y.startswith(".ctors") || Y.startswith(".dtors")); |
| 248 | X = X.substr(6); |
| 249 | Y = Y.substr(6); |
Rui Ueyama | 24b794e | 2016-02-12 00:38:46 +0000 | [diff] [blame] | 250 | if (X.empty() && Y.empty()) |
| 251 | return false; |
Rui Ueyama | 5af8368 | 2016-02-11 23:41:38 +0000 | [diff] [blame] | 252 | return X < Y; |
| 253 | } |
| 254 | |
| 255 | // Sorts input sections by the special rules for .ctors and .dtors. |
| 256 | // Unfortunately, the rules are different from the one for .{init,fini}_array. |
| 257 | // Read the comment above. |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 258 | void OutputSection::sortCtorsDtors() { |
Rafael Espindola | a2e3fee | 2017-02-24 13:19:33 +0000 | [diff] [blame] | 259 | std::stable_sort(Sections.begin(), Sections.end(), compCtors); |
Rui Ueyama | c418570 | 2016-02-10 23:20:42 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Rui Ueyama | b58079d4 | 2017-04-11 22:45:57 +0000 | [diff] [blame] | 262 | // Fill [Buf, Buf + Size) with Filler. |
| 263 | // This is used for linker script "=fillexp" command. |
Rui Ueyama | c49bb0f | 2017-04-03 19:14:35 +0000 | [diff] [blame] | 264 | static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) { |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 265 | size_t I = 0; |
Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 266 | for (; I + 4 < Size; I += 4) |
Rui Ueyama | b58079d4 | 2017-04-11 22:45:57 +0000 | [diff] [blame] | 267 | memcpy(Buf + I, &Filler, 4); |
| 268 | memcpy(Buf + I, &Filler, Size - I); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Rui Ueyama | 8f8c2f9 | 2017-04-11 22:45:38 +0000 | [diff] [blame] | 271 | uint32_t OutputSection::getFiller() { |
James Henderson | 9d9a663 | 2017-04-07 10:36:42 +0000 | [diff] [blame] | 272 | // Determine what to fill gaps between InputSections with, as specified by the |
| 273 | // linker script. If nothing is specified and this is an executable section, |
| 274 | // fall back to trap instructions to prevent bad diassembly and detect invalid |
| 275 | // jumps to padding. |
Rafael Espindola | c5b612b | 2017-05-10 14:01:13 +0000 | [diff] [blame] | 276 | if (Optional<uint32_t> Filler = Script->getFiller(this)) |
James Henderson | 9d9a663 | 2017-04-07 10:36:42 +0000 | [diff] [blame] | 277 | return *Filler; |
| 278 | if (Flags & SHF_EXECINSTR) |
| 279 | return Target->TrapInstr; |
| 280 | return 0; |
| 281 | } |
| 282 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 283 | template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) { |
Eugene Leviant | 84569e6 | 2016-11-29 08:05:44 +0000 | [diff] [blame] | 284 | Loc = Buf; |
Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 285 | |
George Rimar | dbf9339 | 2017-04-17 08:58:12 +0000 | [diff] [blame] | 286 | // We may have already rendered compressed content when using |
| 287 | // -compress-debug-sections option. Write it together with header. |
| 288 | if (!CompressedData.empty()) { |
Rui Ueyama | 07c62c1 | 2017-04-19 11:32:13 +0000 | [diff] [blame] | 289 | memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size()); |
| 290 | memcpy(Buf + ZDebugHeader.size(), CompressedData.data(), |
| 291 | CompressedData.size()); |
George Rimar | dbf9339 | 2017-04-17 08:58:12 +0000 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
James Henderson | 9d9a663 | 2017-04-07 10:36:42 +0000 | [diff] [blame] | 295 | // Write leading padding. |
Rui Ueyama | 8f8c2f9 | 2017-04-11 22:45:38 +0000 | [diff] [blame] | 296 | uint32_t Filler = getFiller(); |
| 297 | if (Filler) |
| 298 | fill(Buf, Sections.empty() ? Size : Sections[0]->OutSecOff, Filler); |
James Henderson | 9d9a663 | 2017-04-07 10:36:42 +0000 | [diff] [blame] | 299 | |
| 300 | parallelFor(0, Sections.size(), [=](size_t I) { |
| 301 | InputSection *Sec = Sections[I]; |
| 302 | Sec->writeTo<ELFT>(Buf); |
| 303 | |
Rui Ueyama | 8f8c2f9 | 2017-04-11 22:45:38 +0000 | [diff] [blame] | 304 | // Fill gaps between sections. |
| 305 | if (Filler) { |
| 306 | uint8_t *Start = Buf + Sec->OutSecOff + Sec->getSize(); |
| 307 | uint8_t *End; |
| 308 | if (I + 1 == Sections.size()) |
| 309 | End = Buf + Size; |
| 310 | else |
| 311 | End = Buf + Sections[I + 1]->OutSecOff; |
| 312 | fill(Start, End - Start, Filler); |
| 313 | } |
James Henderson | 9d9a663 | 2017-04-07 10:36:42 +0000 | [diff] [blame] | 314 | }); |
Rui Ueyama | 16068ae | 2016-11-19 18:05:56 +0000 | [diff] [blame] | 315 | |
George Rimar | e38cbab | 2016-09-26 19:22:50 +0000 | [diff] [blame] | 316 | // Linker scripts may have BYTE()-family commands with which you |
| 317 | // can write arbitrary bytes to the output. Process them if any. |
Rafael Espindola | 660c9ab | 2017-05-05 21:34:26 +0000 | [diff] [blame] | 318 | Script->writeDataBytes(this, Buf); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 319 | } |
| 320 | |
George Rimar | 788fe38 | 2017-03-14 09:14:28 +0000 | [diff] [blame] | 321 | static uint64_t getOutFlags(InputSectionBase *S) { |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 322 | return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED; |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 325 | static SectionKey createKey(InputSectionBase *C, StringRef OutsecName) { |
Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 326 | // The ELF spec just says |
| 327 | // ---------------------------------------------------------------- |
| 328 | // In the first phase, input sections that match in name, type and |
| 329 | // attribute flags should be concatenated into single sections. |
| 330 | // ---------------------------------------------------------------- |
| 331 | // |
| 332 | // However, it is clear that at least some flags have to be ignored for |
| 333 | // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be |
| 334 | // ignored. We should not have two output .text sections just because one was |
| 335 | // in a group and another was not for example. |
| 336 | // |
| 337 | // It also seems that that wording was a late addition and didn't get the |
| 338 | // necessary scrutiny. |
| 339 | // |
| 340 | // Merging sections with different flags is expected by some users. One |
| 341 | // reason is that if one file has |
| 342 | // |
| 343 | // int *const bar __attribute__((section(".foo"))) = (int *)0; |
| 344 | // |
| 345 | // gcc with -fPIC will produce a read only .foo section. But if another |
| 346 | // file has |
| 347 | // |
| 348 | // int zed; |
| 349 | // int *const bar __attribute__((section(".foo"))) = (int *)&zed; |
| 350 | // |
| 351 | // gcc with -fPIC will produce a read write section. |
| 352 | // |
| 353 | // Last but not least, when using linker script the merge rules are forced by |
| 354 | // the script. Unfortunately, linker scripts are name based. This means that |
| 355 | // expressions like *(.foo*) can refer to multiple input sections with |
| 356 | // different flags. We cannot put them in different output sections or we |
| 357 | // would produce wrong results for |
| 358 | // |
| 359 | // start = .; *(.foo.*) end = .; *(.bar) |
| 360 | // |
| 361 | // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to |
| 362 | // another. The problem is that there is no way to layout those output |
| 363 | // sections such that the .foo sections are the only thing between the start |
| 364 | // and end symbols. |
| 365 | // |
| 366 | // Given the above issues, we instead merge sections by name and error on |
| 367 | // incompatible types and flags. |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 368 | |
Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 369 | uint32_t Alignment = 0; |
George Rimar | 09268b7 | 2017-03-14 09:25:03 +0000 | [diff] [blame] | 370 | uint64_t Flags = 0; |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 371 | if (Config->Relocatable && (C->Flags & SHF_MERGE)) { |
George Rimar | 09268b7 | 2017-03-14 09:25:03 +0000 | [diff] [blame] | 372 | Alignment = std::max<uint64_t>(C->Alignment, C->Entsize); |
Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 373 | Flags = C->Flags & (SHF_MERGE | SHF_STRINGS); |
| 374 | } |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 375 | |
Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 376 | return SectionKey{OutsecName, Flags, Alignment}; |
Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Rui Ueyama | 02a036f | 2017-02-27 02:31:48 +0000 | [diff] [blame] | 379 | OutputSectionFactory::OutputSectionFactory( |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 380 | std::vector<OutputSection *> &OutputSections) |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 381 | : OutputSections(OutputSections) {} |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 382 | |
Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 383 | static uint64_t getIncompatibleFlags(uint64_t Flags) { |
| 384 | return Flags & (SHF_ALLOC | SHF_TLS); |
| 385 | } |
| 386 | |
Eugene Leviant | 0c0789b | 2017-01-31 10:26:52 +0000 | [diff] [blame] | 387 | // We allow sections of types listed below to merged into a |
| 388 | // single progbits section. This is typically done by linker |
| 389 | // scripts. Merging nobits and progbits will force disk space |
| 390 | // to be allocated for nobits sections. Other ones don't require |
| 391 | // any special treatment on top of progbits, so there doesn't |
| 392 | // seem to be a harm in merging them. |
| 393 | static bool canMergeToProgbits(unsigned Type) { |
| 394 | return Type == SHT_NOBITS || Type == SHT_PROGBITS || Type == SHT_INIT_ARRAY || |
| 395 | Type == SHT_PREINIT_ARRAY || Type == SHT_FINI_ARRAY || |
| 396 | Type == SHT_NOTE; |
| 397 | } |
| 398 | |
George Rimar | f08b592 | 2017-03-14 09:19:34 +0000 | [diff] [blame] | 399 | static void reportDiscarded(InputSectionBase *IS) { |
Rafael Espindola | ecbfd87 | 2017-02-17 17:35:07 +0000 | [diff] [blame] | 400 | if (!Config->PrintGcSections) |
| 401 | return; |
Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 402 | message("removing unused section from '" + IS->Name + "' in file '" + |
George Rimar | f08b592 | 2017-03-14 09:19:34 +0000 | [diff] [blame] | 403 | IS->File->getName()); |
Rafael Espindola | ecbfd87 | 2017-02-17 17:35:07 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Rui Ueyama | 02a036f | 2017-02-27 02:31:48 +0000 | [diff] [blame] | 406 | void OutputSectionFactory::addInputSec(InputSectionBase *IS, |
| 407 | StringRef OutsecName) { |
Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 408 | SectionKey Key = createKey(IS, OutsecName); |
| 409 | OutputSection *&Sec = Map[Key]; |
| 410 | return addInputSec(IS, OutsecName, Sec); |
| 411 | } |
| 412 | |
| 413 | void OutputSectionFactory::addInputSec(InputSectionBase *IS, |
| 414 | StringRef OutsecName, |
| 415 | OutputSection *&Sec) { |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 416 | if (!IS->Live) { |
George Rimar | f08b592 | 2017-03-14 09:19:34 +0000 | [diff] [blame] | 417 | reportDiscarded(IS); |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 418 | return; |
| 419 | } |
| 420 | |
George Rimar | 788fe38 | 2017-03-14 09:14:28 +0000 | [diff] [blame] | 421 | uint64_t Flags = getOutFlags(IS); |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 422 | if (Sec) { |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 423 | if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags)) |
Rui Ueyama | 577168e | 2017-04-25 16:00:44 +0000 | [diff] [blame] | 424 | error("incompatible section flags for " + Sec->Name + |
| 425 | "\n>>> " + toString(IS) + ": 0x" + utohexstr(IS->Flags) + |
| 426 | "\n>>> output section " + Sec->Name + ": 0x" + |
| 427 | utohexstr(Sec->Flags)); |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 428 | if (Sec->Type != IS->Type) { |
| 429 | if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(IS->Type)) |
Eugene Leviant | 0c0789b | 2017-01-31 10:26:52 +0000 | [diff] [blame] | 430 | Sec->Type = SHT_PROGBITS; |
| 431 | else |
Rui Ueyama | 896cbc4 | 2017-05-10 16:57:50 +0000 | [diff] [blame^] | 432 | error("section type mismatch for " + IS->Name + |
| 433 | "\n>>> " + toString(IS) + ": " + |
| 434 | getELFSectionTypeName(Config->EMachine, IS->Type) + |
| 435 | "\n>>> output section " + Sec->Name + ": " + |
| 436 | getELFSectionTypeName(Config->EMachine, Sec->Type)); |
Eugene Leviant | 0c0789b | 2017-01-31 10:26:52 +0000 | [diff] [blame] | 437 | } |
Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 438 | Sec->Flags |= Flags; |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 439 | } else { |
Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 440 | Sec = make<OutputSection>(OutsecName, IS->Type, Flags); |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 441 | OutputSections.push_back(Sec); |
Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Rafael Espindola | dc8eb81 | 2017-04-07 01:40:21 +0000 | [diff] [blame] | 444 | Sec->addSection(cast<InputSection>(IS)); |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Rui Ueyama | 02a036f | 2017-02-27 02:31:48 +0000 | [diff] [blame] | 447 | OutputSectionFactory::~OutputSectionFactory() {} |
Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 448 | |
Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 449 | SectionKey DenseMapInfo<SectionKey>::getEmptyKey() { |
| 450 | return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0}; |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 453 | SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() { |
| 454 | return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0}; |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 457 | unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) { |
Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 458 | return hash_combine(Val.Name, Val.Flags, Val.Alignment); |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 461 | bool DenseMapInfo<SectionKey>::isEqual(const SectionKey &LHS, |
| 462 | const SectionKey &RHS) { |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 463 | return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) && |
Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 464 | LHS.Flags == RHS.Flags && LHS.Alignment == RHS.Alignment; |
George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 465 | } |
| 466 | |
George Rimar | 78aa270 | 2017-03-13 14:40:58 +0000 | [diff] [blame] | 467 | uint64_t elf::getHeaderSize() { |
| 468 | if (Config->OFormatBinary) |
| 469 | return 0; |
| 470 | return Out::ElfHeader->Size + Out::ProgramHeaders->Size; |
| 471 | } |
| 472 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 473 | template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr); |
| 474 | template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr); |
| 475 | template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr); |
| 476 | template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr); |
Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 477 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 478 | template void OutputSection::finalize<ELF32LE>(); |
| 479 | template void OutputSection::finalize<ELF32BE>(); |
| 480 | template void OutputSection::finalize<ELF64LE>(); |
| 481 | template void OutputSection::finalize<ELF64BE>(); |
| 482 | |
George Rimar | dbf9339 | 2017-04-17 08:58:12 +0000 | [diff] [blame] | 483 | template void OutputSection::maybeCompress<ELF32LE>(); |
| 484 | template void OutputSection::maybeCompress<ELF32BE>(); |
| 485 | template void OutputSection::maybeCompress<ELF64LE>(); |
| 486 | template void OutputSection::maybeCompress<ELF64BE>(); |
| 487 | |
Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 488 | template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf); |
| 489 | template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf); |
| 490 | template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf); |
| 491 | template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf); |