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