| 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" |
| Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 19 | #include "llvm/BinaryFormat/Dwarf.h" |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compression.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 | f51c805 | 2017-06-13 23:26:31 +0000 | [diff] [blame] | 45 | std::vector<OutputSection *> elf::OutputSections; |
| Rafael Espindola | f51c805 | 2017-06-13 23:26:31 +0000 | [diff] [blame] | 46 | |
| Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 47 | uint32_t OutputSection::getPhdrFlags() const { |
| Rafael Espindola | 0b11367 | 2016-07-27 14:10:56 +0000 | [diff] [blame] | 48 | uint32_t Ret = PF_R; |
| 49 | if (Flags & SHF_WRITE) |
| 50 | Ret |= PF_W; |
| 51 | if (Flags & SHF_EXECINSTR) |
| 52 | Ret |= PF_X; |
| 53 | return Ret; |
| 54 | } |
| 55 | |
| Rafael Espindola | 35c6af3 | 2015-09-25 17:19:10 +0000 | [diff] [blame] | 56 | template <class ELFT> |
| Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 57 | void OutputSection::writeHeaderTo(typename ELFT::Shdr *Shdr) { |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 58 | Shdr->sh_entsize = Entsize; |
| Rafael Espindola | 3770763 | 2017-03-07 14:55:52 +0000 | [diff] [blame] | 59 | Shdr->sh_addralign = Alignment; |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 60 | Shdr->sh_type = Type; |
| 61 | Shdr->sh_offset = Offset; |
| 62 | Shdr->sh_flags = Flags; |
| 63 | Shdr->sh_info = Info; |
| 64 | Shdr->sh_link = Link; |
| Rafael Espindola | ea590d9 | 2017-02-08 15:19:03 +0000 | [diff] [blame] | 65 | Shdr->sh_addr = Addr; |
| Rafael Espindola | 04a2e34 | 2016-11-09 01:42:41 +0000 | [diff] [blame] | 66 | Shdr->sh_size = Size; |
| 67 | Shdr->sh_name = ShName; |
| Rui Ueyama | c63c1db | 2016-03-13 06:50:33 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 70 | OutputSection::OutputSection(StringRef Name, uint32_t Type, uint64_t Flags) |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 71 | : BaseCommand(OutputSectionKind), |
| 72 | SectionBase(Output, Name, Flags, /*Entsize*/ 0, /*Alignment*/ 1, Type, |
| Rafael Espindola | 5616adf | 2017-03-08 22:36:28 +0000 | [diff] [blame] | 73 | /*Info*/ 0, |
| Rafael Espindola | 660c9ab | 2017-05-05 21:34:26 +0000 | [diff] [blame] | 74 | /*Link*/ 0), |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 75 | SectionIndex(INT_MAX) { |
| 76 | Live = false; |
| 77 | } |
| George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 78 | |
| George Rimar | d86a4e5 | 2017-05-08 10:18:12 +0000 | [diff] [blame] | 79 | static uint64_t updateOffset(uint64_t Off, InputSection *S) { |
| 80 | Off = alignTo(Off, S->Alignment); |
| 81 | S->OutSecOff = Off; |
| 82 | return Off + S->getSize(); |
| 83 | } |
| 84 | |
| Rafael Espindola | dc8eb81 | 2017-04-07 01:40:21 +0000 | [diff] [blame] | 85 | void OutputSection::addSection(InputSection *S) { |
| 86 | assert(S->Live); |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 87 | Live = true; |
| Rafael Espindola | db5e56f | 2017-05-31 20:17:44 +0000 | [diff] [blame] | 88 | S->Parent = this; |
| Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 89 | this->updateAlignment(S->Alignment); |
| Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 90 | |
| George Rimar | d86a4e5 | 2017-05-08 10:18:12 +0000 | [diff] [blame] | 91 | // The actual offsets will be computed by assignAddresses. For now, use |
| 92 | // crude approximation so that it is at least easy for other code to know the |
| 93 | // section order. It is also used to calculate the output section size early |
| 94 | // for compressed debug sections. |
| 95 | this->Size = updateOffset(Size, S); |
| 96 | |
| Rui Ueyama | 2787664 | 2017-03-01 04:04:23 +0000 | [diff] [blame] | 97 | // If this section contains a table of fixed-size entries, sh_entsize |
| 98 | // holds the element size. Consequently, if this contains two or more |
| 99 | // input sections, all of them must have the same sh_entsize. However, |
| 100 | // you can put different types of input sections into one output |
| 101 | // sectin by using linker scripts. I don't know what to do here. |
| 102 | // Probably we sholuld handle that as an error. But for now we just |
| 103 | // pick the largest sh_entsize. |
| 104 | this->Entsize = std::max(this->Entsize, S->Entsize); |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 105 | |
| 106 | if (!S->Assigned) { |
| 107 | S->Assigned = true; |
| 108 | if (Commands.empty() || !isa<InputSectionDescription>(Commands.back())) |
| 109 | Commands.push_back(make<InputSectionDescription>("")); |
| 110 | auto *ISD = cast<InputSectionDescription>(Commands.back()); |
| 111 | ISD->Sections.push_back(S); |
| 112 | } |
| Rafael Espindola | 5805c4f | 2015-09-21 21:38:08 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| Rafael Espindola | b4c9b81 | 2017-02-23 02:28:28 +0000 | [diff] [blame] | 115 | static SectionKey createKey(InputSectionBase *C, StringRef OutsecName) { |
| Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 116 | // The ELF spec just says |
| 117 | // ---------------------------------------------------------------- |
| 118 | // In the first phase, input sections that match in name, type and |
| 119 | // attribute flags should be concatenated into single sections. |
| 120 | // ---------------------------------------------------------------- |
| 121 | // |
| 122 | // However, it is clear that at least some flags have to be ignored for |
| 123 | // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be |
| 124 | // ignored. We should not have two output .text sections just because one was |
| 125 | // in a group and another was not for example. |
| 126 | // |
| 127 | // It also seems that that wording was a late addition and didn't get the |
| 128 | // necessary scrutiny. |
| 129 | // |
| 130 | // Merging sections with different flags is expected by some users. One |
| 131 | // reason is that if one file has |
| 132 | // |
| 133 | // int *const bar __attribute__((section(".foo"))) = (int *)0; |
| 134 | // |
| 135 | // gcc with -fPIC will produce a read only .foo section. But if another |
| 136 | // file has |
| 137 | // |
| 138 | // int zed; |
| 139 | // int *const bar __attribute__((section(".foo"))) = (int *)&zed; |
| 140 | // |
| 141 | // gcc with -fPIC will produce a read write section. |
| 142 | // |
| 143 | // Last but not least, when using linker script the merge rules are forced by |
| 144 | // the script. Unfortunately, linker scripts are name based. This means that |
| 145 | // expressions like *(.foo*) can refer to multiple input sections with |
| 146 | // different flags. We cannot put them in different output sections or we |
| 147 | // would produce wrong results for |
| 148 | // |
| 149 | // start = .; *(.foo.*) end = .; *(.bar) |
| 150 | // |
| 151 | // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to |
| 152 | // another. The problem is that there is no way to layout those output |
| 153 | // sections such that the .foo sections are the only thing between the start |
| 154 | // and end symbols. |
| 155 | // |
| 156 | // Given the above issues, we instead merge sections by name and error on |
| 157 | // incompatible types and flags. |
| Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 158 | |
| Rafael Espindola | fcd208f | 2017-03-08 19:35:29 +0000 | [diff] [blame] | 159 | uint32_t Alignment = 0; |
| George Rimar | 09268b7 | 2017-03-14 09:25:03 +0000 | [diff] [blame] | 160 | uint64_t Flags = 0; |
| Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 161 | if (Config->Relocatable && (C->Flags & SHF_MERGE)) { |
| George Rimar | 09268b7 | 2017-03-14 09:25:03 +0000 | [diff] [blame] | 162 | Alignment = std::max<uint64_t>(C->Alignment, C->Entsize); |
| Rafael Espindola | 9e9754b | 2017-02-03 13:06:18 +0000 | [diff] [blame] | 163 | Flags = C->Flags & (SHF_MERGE | SHF_STRINGS); |
| 164 | } |
| Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 165 | |
| Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 166 | return SectionKey{OutsecName, Flags, Alignment}; |
| Rafael Espindola | 17c3583 | 2016-09-13 12:25:30 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| Rafael Espindola | 0553124 | 2017-07-06 16:40:44 +0000 | [diff] [blame] | 169 | OutputSectionFactory::OutputSectionFactory() {} |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 170 | |
| Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 171 | static uint64_t getIncompatibleFlags(uint64_t Flags) { |
| 172 | return Flags & (SHF_ALLOC | SHF_TLS); |
| 173 | } |
| 174 | |
| Eugene Leviant | 0c0789b | 2017-01-31 10:26:52 +0000 | [diff] [blame] | 175 | // We allow sections of types listed below to merged into a |
| 176 | // single progbits section. This is typically done by linker |
| 177 | // scripts. Merging nobits and progbits will force disk space |
| 178 | // to be allocated for nobits sections. Other ones don't require |
| 179 | // any special treatment on top of progbits, so there doesn't |
| 180 | // seem to be a harm in merging them. |
| 181 | static bool canMergeToProgbits(unsigned Type) { |
| 182 | return Type == SHT_NOBITS || Type == SHT_PROGBITS || Type == SHT_INIT_ARRAY || |
| 183 | Type == SHT_PREINIT_ARRAY || Type == SHT_FINI_ARRAY || |
| 184 | Type == SHT_NOTE; |
| 185 | } |
| 186 | |
| Rafael Espindola | e39709b | 2017-05-30 20:40:03 +0000 | [diff] [blame] | 187 | void elf::reportDiscarded(InputSectionBase *IS) { |
| Rafael Espindola | ecbfd87 | 2017-02-17 17:35:07 +0000 | [diff] [blame] | 188 | if (!Config->PrintGcSections) |
| 189 | return; |
| Rui Ueyama | e6e206d | 2017-02-21 23:22:56 +0000 | [diff] [blame] | 190 | message("removing unused section from '" + IS->Name + "' in file '" + |
| Igor Kudrin | 393563a | 2017-06-22 04:07:58 +0000 | [diff] [blame] | 191 | IS->File->getName() + "'"); |
| Rafael Espindola | ecbfd87 | 2017-02-17 17:35:07 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| Rui Ueyama | 02a036f | 2017-02-27 02:31:48 +0000 | [diff] [blame] | 194 | void OutputSectionFactory::addInputSec(InputSectionBase *IS, |
| 195 | StringRef OutsecName) { |
| George Rimar | 8cde9a7 | 2017-06-06 06:38:32 +0000 | [diff] [blame] | 196 | // Sections with the SHT_GROUP attribute reach here only when the - r option |
| 197 | // is given. Such sections define "section groups", and InputFiles.cpp has |
| 198 | // dedup'ed section groups by their signatures. For the -r, we want to pass |
| 199 | // through all SHT_GROUP sections without merging them because merging them |
| 200 | // creates broken section contents. |
| 201 | if (IS->Type == SHT_GROUP) { |
| 202 | OutputSection *Out = nullptr; |
| 203 | addInputSec(IS, OutsecName, Out); |
| 204 | return; |
| 205 | } |
| 206 | |
| George Rimar | 990c9cb | 2017-06-07 09:20:35 +0000 | [diff] [blame] | 207 | // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have |
| 208 | // relocation sections .rela.foo and .rela.bar for example. Most tools do |
| 209 | // not allow multiple REL[A] sections for output section. Hence we |
| 210 | // should combine these relocation sections into single output. |
| 211 | // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any |
| 212 | // other REL[A] sections created by linker itself. |
| 213 | if (!isa<SyntheticSection>(IS) && |
| 214 | (IS->Type == SHT_REL || IS->Type == SHT_RELA)) { |
| 215 | auto *Sec = cast<InputSection>(IS); |
| 216 | OutputSection *Out = Sec->getRelocatedSection()->getOutputSection(); |
| 217 | addInputSec(IS, OutsecName, Out->RelocationSection); |
| 218 | return; |
| 219 | } |
| 220 | |
| Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 221 | SectionKey Key = createKey(IS, OutsecName); |
| 222 | OutputSection *&Sec = Map[Key]; |
| Rui Ueyama | 71fab2f | 2017-06-26 16:52:16 +0000 | [diff] [blame] | 223 | addInputSec(IS, OutsecName, Sec); |
| Rafael Espindola | 4f013bb | 2017-04-26 22:30:15 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void OutputSectionFactory::addInputSec(InputSectionBase *IS, |
| 227 | StringRef OutsecName, |
| 228 | OutputSection *&Sec) { |
| Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 229 | if (!IS->Live) { |
| George Rimar | f08b592 | 2017-03-14 09:19:34 +0000 | [diff] [blame] | 230 | reportDiscarded(IS); |
| Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 234 | if (Sec && Sec->Live) { |
| Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 235 | if (getIncompatibleFlags(Sec->Flags) != getIncompatibleFlags(IS->Flags)) |
| George Rimar | 67c6072 | 2017-07-18 11:55:35 +0000 | [diff] [blame] | 236 | error("incompatible section flags for " + Sec->Name + "\n>>> " + |
| 237 | toString(IS) + ": 0x" + utohexstr(IS->Flags) + |
| Rui Ueyama | 577168e | 2017-04-25 16:00:44 +0000 | [diff] [blame] | 238 | "\n>>> output section " + Sec->Name + ": 0x" + |
| 239 | utohexstr(Sec->Flags)); |
| Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 240 | if (Sec->Type != IS->Type) { |
| 241 | if (canMergeToProgbits(Sec->Type) && canMergeToProgbits(IS->Type)) |
| Eugene Leviant | 0c0789b | 2017-01-31 10:26:52 +0000 | [diff] [blame] | 242 | Sec->Type = SHT_PROGBITS; |
| 243 | else |
| George Rimar | 67c6072 | 2017-07-18 11:55:35 +0000 | [diff] [blame] | 244 | error("section type mismatch for " + IS->Name + "\n>>> " + |
| 245 | toString(IS) + ": " + |
| Rui Ueyama | 896cbc4 | 2017-05-10 16:57:50 +0000 | [diff] [blame] | 246 | getELFSectionTypeName(Config->EMachine, IS->Type) + |
| 247 | "\n>>> output section " + Sec->Name + ": " + |
| 248 | getELFSectionTypeName(Config->EMachine, Sec->Type)); |
| Eugene Leviant | 0c0789b | 2017-01-31 10:26:52 +0000 | [diff] [blame] | 249 | } |
| Rui Ueyama | 1c837b5 | 2017-06-10 00:38:55 +0000 | [diff] [blame] | 250 | Sec->Flags |= IS->Flags; |
| Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 251 | } else { |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 252 | if (!Sec) { |
| 253 | Sec = Script->createOutputSection(OutsecName, "<internal>"); |
| 254 | Script->Opt.Commands.push_back(Sec); |
| 255 | } |
| 256 | Sec->Type = IS->Type; |
| 257 | Sec->Flags = IS->Flags; |
| Rafael Espindola | 10897f1 | 2016-09-13 14:23:14 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| Rafael Espindola | dc8eb81 | 2017-04-07 01:40:21 +0000 | [diff] [blame] | 260 | Sec->addSection(cast<InputSection>(IS)); |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| Rui Ueyama | 02a036f | 2017-02-27 02:31:48 +0000 | [diff] [blame] | 263 | OutputSectionFactory::~OutputSectionFactory() {} |
| Rafael Espindola | 8290274 | 2017-02-16 17:32:26 +0000 | [diff] [blame] | 264 | |
| Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 265 | SectionKey DenseMapInfo<SectionKey>::getEmptyKey() { |
| 266 | return SectionKey{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0}; |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 269 | SectionKey DenseMapInfo<SectionKey>::getTombstoneKey() { |
| 270 | return SectionKey{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0}; |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 273 | unsigned DenseMapInfo<SectionKey>::getHashValue(const SectionKey &Val) { |
| Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 274 | return hash_combine(Val.Name, Val.Flags, Val.Alignment); |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| Rafael Espindola | 7244708 | 2017-01-05 14:35:41 +0000 | [diff] [blame] | 277 | bool DenseMapInfo<SectionKey>::isEqual(const SectionKey &LHS, |
| 278 | const SectionKey &RHS) { |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 279 | return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) && |
| Rafael Espindola | 3371398 | 2017-01-05 14:20:35 +0000 | [diff] [blame] | 280 | LHS.Flags == RHS.Flags && LHS.Alignment == RHS.Alignment; |
| George Rimar | 6892afa | 2016-07-12 09:49:43 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| George Rimar | 78aa270 | 2017-03-13 14:40:58 +0000 | [diff] [blame] | 283 | uint64_t elf::getHeaderSize() { |
| 284 | if (Config->OFormatBinary) |
| 285 | return 0; |
| 286 | return Out::ElfHeader->Size + Out::ProgramHeaders->Size; |
| 287 | } |
| 288 | |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 289 | bool OutputSection::classof(const BaseCommand *C) { |
| 290 | return C->Kind == OutputSectionKind; |
| 291 | } |
| 292 | |
| 293 | void OutputSection::sort(std::function<int(InputSectionBase *S)> Order) { |
| Rafael Espindola | d21ac10 | 2017-07-28 15:36:15 +0000 | [diff] [blame] | 294 | typedef std::pair<int, InputSection *> Pair; |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 295 | auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; }; |
| 296 | |
| 297 | std::vector<Pair> V; |
| 298 | assert(Commands.size() == 1); |
| 299 | auto *ISD = cast<InputSectionDescription>(Commands[0]); |
| 300 | for (InputSection *S : ISD->Sections) |
| 301 | V.push_back({Order(S), S}); |
| 302 | std::stable_sort(V.begin(), V.end(), Comp); |
| 303 | ISD->Sections.clear(); |
| 304 | for (Pair &P : V) |
| 305 | ISD->Sections.push_back(P.second); |
| 306 | } |
| 307 | |
| 308 | // Fill [Buf, Buf + Size) with Filler. |
| 309 | // This is used for linker script "=fillexp" command. |
| 310 | static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) { |
| 311 | size_t I = 0; |
| 312 | for (; I + 4 < Size; I += 4) |
| 313 | memcpy(Buf + I, &Filler, 4); |
| 314 | memcpy(Buf + I, &Filler, Size - I); |
| 315 | } |
| 316 | |
| 317 | // Compress section contents if this section contains debug info. |
| 318 | template <class ELFT> void OutputSection::maybeCompress() { |
| 319 | typedef typename ELFT::Chdr Elf_Chdr; |
| 320 | |
| 321 | // Compress only DWARF debug sections. |
| 322 | if (!Config->CompressDebugSections || (Flags & SHF_ALLOC) || |
| 323 | !Name.startswith(".debug_")) |
| 324 | return; |
| 325 | |
| 326 | // Create a section header. |
| 327 | ZDebugHeader.resize(sizeof(Elf_Chdr)); |
| 328 | auto *Hdr = reinterpret_cast<Elf_Chdr *>(ZDebugHeader.data()); |
| 329 | Hdr->ch_type = ELFCOMPRESS_ZLIB; |
| 330 | Hdr->ch_size = Size; |
| 331 | Hdr->ch_addralign = Alignment; |
| 332 | |
| 333 | // Write section contents to a temporary buffer and compress it. |
| 334 | std::vector<uint8_t> Buf(Size); |
| 335 | writeTo<ELFT>(Buf.data()); |
| 336 | if (Error E = zlib::compress(toStringRef(Buf), CompressedData)) |
| 337 | fatal("compress failed: " + llvm::toString(std::move(E))); |
| 338 | |
| 339 | // Update section headers. |
| 340 | Size = sizeof(Elf_Chdr) + CompressedData.size(); |
| 341 | Flags |= SHF_COMPRESSED; |
| 342 | } |
| 343 | |
| 344 | static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) { |
| 345 | if (Size == 1) |
| 346 | *Buf = Data; |
| 347 | else if (Size == 2) |
| 348 | write16(Buf, Data, Config->Endianness); |
| 349 | else if (Size == 4) |
| 350 | write32(Buf, Data, Config->Endianness); |
| 351 | else if (Size == 8) |
| 352 | write64(Buf, Data, Config->Endianness); |
| 353 | else |
| 354 | llvm_unreachable("unsupported Size argument"); |
| 355 | } |
| 356 | |
| 357 | template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) { |
| 358 | if (Type == SHT_NOBITS) |
| 359 | return; |
| 360 | |
| 361 | Loc = Buf; |
| 362 | |
| 363 | // If -compress-debug-section is specified and if this is a debug seciton, |
| 364 | // we've already compressed section contents. If that's the case, |
| 365 | // just write it down. |
| 366 | if (!CompressedData.empty()) { |
| 367 | memcpy(Buf, ZDebugHeader.data(), ZDebugHeader.size()); |
| 368 | memcpy(Buf + ZDebugHeader.size(), CompressedData.data(), |
| 369 | CompressedData.size()); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | // Write leading padding. |
| 374 | std::vector<InputSection *> Sections; |
| 375 | for (BaseCommand *Cmd : Commands) |
| 376 | if (auto *ISD = dyn_cast<InputSectionDescription>(Cmd)) |
| 377 | for (InputSection *IS : ISD->Sections) |
| 378 | if (IS->Live) |
| 379 | Sections.push_back(IS); |
| 380 | uint32_t Filler = getFiller(); |
| 381 | if (Filler) |
| 382 | fill(Buf, Sections.empty() ? Size : Sections[0]->OutSecOff, Filler); |
| 383 | |
| 384 | parallelForEachN(0, Sections.size(), [=](size_t I) { |
| 385 | InputSection *IS = Sections[I]; |
| 386 | IS->writeTo<ELFT>(Buf); |
| 387 | |
| 388 | // Fill gaps between sections. |
| 389 | if (Filler) { |
| 390 | uint8_t *Start = Buf + IS->OutSecOff + IS->getSize(); |
| 391 | uint8_t *End; |
| 392 | if (I + 1 == Sections.size()) |
| 393 | End = Buf + Size; |
| 394 | else |
| 395 | End = Buf + Sections[I + 1]->OutSecOff; |
| 396 | fill(Start, End - Start, Filler); |
| 397 | } |
| 398 | }); |
| 399 | |
| 400 | // Linker scripts may have BYTE()-family commands with which you |
| 401 | // can write arbitrary bytes to the output. Process them if any. |
| 402 | for (BaseCommand *Base : Commands) |
| 403 | if (auto *Data = dyn_cast<BytesDataCommand>(Base)) |
| 404 | writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size); |
| 405 | } |
| 406 | |
| 407 | static bool compareByFilePosition(InputSection *A, InputSection *B) { |
| 408 | // Synthetic doesn't have link order dependecy, stable_sort will keep it last |
| 409 | if (A->kind() == InputSectionBase::Synthetic || |
| 410 | B->kind() == InputSectionBase::Synthetic) |
| 411 | return false; |
| 412 | InputSection *LA = A->getLinkOrderDep(); |
| 413 | InputSection *LB = B->getLinkOrderDep(); |
| 414 | OutputSection *AOut = LA->getParent(); |
| 415 | OutputSection *BOut = LB->getParent(); |
| 416 | if (AOut != BOut) |
| 417 | return AOut->SectionIndex < BOut->SectionIndex; |
| 418 | return LA->OutSecOff < LB->OutSecOff; |
| 419 | } |
| 420 | |
| 421 | template <class ELFT> |
| 422 | static void finalizeShtGroup(OutputSection *OS, |
| 423 | ArrayRef<InputSection *> Sections) { |
| 424 | assert(Config->Relocatable && Sections.size() == 1); |
| 425 | |
| 426 | // sh_link field for SHT_GROUP sections should contain the section index of |
| 427 | // the symbol table. |
| 428 | OS->Link = InX::SymTab->getParent()->SectionIndex; |
| 429 | |
| 430 | // sh_info then contain index of an entry in symbol table section which |
| 431 | // provides signature of the section group. |
| 432 | ObjFile<ELFT> *Obj = Sections[0]->getFile<ELFT>(); |
| 433 | ArrayRef<SymbolBody *> Symbols = Obj->getSymbols(); |
| 434 | OS->Info = InX::SymTab->getSymbolIndex(Symbols[Sections[0]->Info - 1]); |
| 435 | } |
| 436 | |
| 437 | template <class ELFT> void OutputSection::finalize() { |
| 438 | // Link order may be distributed across several InputSectionDescriptions |
| 439 | // but sort must consider them all at once. |
| 440 | std::vector<InputSection **> ScriptSections; |
| 441 | std::vector<InputSection *> Sections; |
| 442 | for (BaseCommand *Base : Commands) |
| 443 | if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) |
| 444 | for (InputSection *&IS : ISD->Sections) { |
| 445 | ScriptSections.push_back(&IS); |
| 446 | Sections.push_back(IS); |
| 447 | } |
| 448 | |
| 449 | if ((Flags & SHF_LINK_ORDER)) { |
| 450 | std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition); |
| 451 | for (int I = 0, N = Sections.size(); I < N; ++I) |
| 452 | *ScriptSections[I] = Sections[I]; |
| 453 | |
| 454 | // We must preserve the link order dependency of sections with the |
| 455 | // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We |
| 456 | // need to translate the InputSection sh_link to the OutputSection sh_link, |
| 457 | // all InputSections in the OutputSection have the same dependency. |
| 458 | if (auto *D = Sections.front()->getLinkOrderDep()) |
| 459 | Link = D->getParent()->SectionIndex; |
| 460 | } |
| 461 | |
| 462 | if (Type == SHT_GROUP) { |
| 463 | finalizeShtGroup<ELFT>(this, Sections); |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | if (!Config->CopyRelocs || (Type != SHT_RELA && Type != SHT_REL)) |
| 468 | return; |
| 469 | |
| 470 | InputSection *First = Sections[0]; |
| 471 | if (isa<SyntheticSection>(First)) |
| 472 | return; |
| 473 | |
| 474 | Link = InX::SymTab->getParent()->SectionIndex; |
| 475 | // sh_info for SHT_REL[A] sections should contain the section header index of |
| 476 | // the section to which the relocation applies. |
| 477 | InputSectionBase *S = First->getRelocatedSection(); |
| 478 | Info = S->getOutputSection()->SectionIndex; |
| 479 | Flags |= SHF_INFO_LINK; |
| 480 | } |
| 481 | |
| 482 | // Returns true if S matches /Filename.?\.o$/. |
| 483 | static bool isCrtBeginEnd(StringRef S, StringRef Filename) { |
| 484 | if (!S.endswith(".o")) |
| 485 | return false; |
| 486 | S = S.drop_back(2); |
| 487 | if (S.endswith(Filename)) |
| 488 | return true; |
| 489 | return !S.empty() && S.drop_back().endswith(Filename); |
| 490 | } |
| 491 | |
| 492 | static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); } |
| 493 | static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); } |
| 494 | |
| 495 | // .ctors and .dtors are sorted by this priority from highest to lowest. |
| 496 | // |
| 497 | // 1. The section was contained in crtbegin (crtbegin contains |
| 498 | // some sentinel value in its .ctors and .dtors so that the runtime |
| 499 | // can find the beginning of the sections.) |
| 500 | // |
| 501 | // 2. The section has an optional priority value in the form of ".ctors.N" |
| 502 | // or ".dtors.N" where N is a number. Unlike .{init,fini}_array, |
| 503 | // they are compared as string rather than number. |
| 504 | // |
| 505 | // 3. The section is just ".ctors" or ".dtors". |
| 506 | // |
| 507 | // 4. The section was contained in crtend, which contains an end marker. |
| 508 | // |
| 509 | // In an ideal world, we don't need this function because .init_array and |
| 510 | // .ctors are duplicate features (and .init_array is newer.) However, there |
| 511 | // are too many real-world use cases of .ctors, so we had no choice to |
| 512 | // support that with this rather ad-hoc semantics. |
| 513 | static bool compCtors(const InputSection *A, const InputSection *B) { |
| 514 | bool BeginA = isCrtbegin(A->File->getName()); |
| 515 | bool BeginB = isCrtbegin(B->File->getName()); |
| 516 | if (BeginA != BeginB) |
| 517 | return BeginA; |
| 518 | bool EndA = isCrtend(A->File->getName()); |
| 519 | bool EndB = isCrtend(B->File->getName()); |
| 520 | if (EndA != EndB) |
| 521 | return EndB; |
| 522 | StringRef X = A->Name; |
| 523 | StringRef Y = B->Name; |
| 524 | assert(X.startswith(".ctors") || X.startswith(".dtors")); |
| 525 | assert(Y.startswith(".ctors") || Y.startswith(".dtors")); |
| 526 | X = X.substr(6); |
| 527 | Y = Y.substr(6); |
| 528 | if (X.empty() && Y.empty()) |
| 529 | return false; |
| 530 | return X < Y; |
| 531 | } |
| 532 | |
| 533 | // Sorts input sections by the special rules for .ctors and .dtors. |
| 534 | // Unfortunately, the rules are different from the one for .{init,fini}_array. |
| 535 | // Read the comment above. |
| 536 | void OutputSection::sortCtorsDtors() { |
| 537 | assert(Commands.size() == 1); |
| 538 | auto *ISD = cast<InputSectionDescription>(Commands[0]); |
| 539 | std::stable_sort(ISD->Sections.begin(), ISD->Sections.end(), compCtors); |
| 540 | } |
| 541 | |
| 542 | // If an input string is in the form of "foo.N" where N is a number, |
| 543 | // return N. Otherwise, returns 65536, which is one greater than the |
| 544 | // lowest priority. |
| 545 | int elf::getPriority(StringRef S) { |
| 546 | size_t Pos = S.rfind('.'); |
| 547 | if (Pos == StringRef::npos) |
| 548 | return 65536; |
| 549 | int V; |
| 550 | if (!to_integer(S.substr(Pos + 1), V, 10)) |
| 551 | return 65536; |
| 552 | return V; |
| 553 | } |
| 554 | |
| 555 | // Sorts input sections by section name suffixes, so that .foo.N comes |
| 556 | // before .foo.M if N < M. Used to sort .{init,fini}_array.N sections. |
| 557 | // We want to keep the original order if the priorities are the same |
| 558 | // because the compiler keeps the original initialization order in a |
| 559 | // translation unit and we need to respect that. |
| 560 | // For more detail, read the section of the GCC's manual about init_priority. |
| 561 | void OutputSection::sortInitFini() { |
| 562 | // Sort sections by priority. |
| 563 | sort([](InputSectionBase *S) { return getPriority(S->Name); }); |
| 564 | } |
| 565 | |
| 566 | uint32_t OutputSection::getFiller() { |
| 567 | if (Filler) |
| 568 | return *Filler; |
| 569 | if (Flags & SHF_EXECINSTR) |
| 570 | return Target->TrapInstr; |
| 571 | return 0; |
| 572 | } |
| 573 | |
| Rafael Espindola | 24e6f36 | 2017-02-24 15:07:30 +0000 | [diff] [blame] | 574 | template void OutputSection::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr); |
| 575 | template void OutputSection::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr); |
| 576 | template void OutputSection::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr); |
| 577 | template void OutputSection::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr); |
| Rafael Espindola | 8c022ca | 2017-07-27 19:22:43 +0000 | [diff] [blame] | 578 | |
| 579 | template void OutputSection::writeTo<ELF32LE>(uint8_t *Buf); |
| 580 | template void OutputSection::writeTo<ELF32BE>(uint8_t *Buf); |
| 581 | template void OutputSection::writeTo<ELF64LE>(uint8_t *Buf); |
| 582 | template void OutputSection::writeTo<ELF64BE>(uint8_t *Buf); |
| 583 | |
| 584 | template void OutputSection::maybeCompress<ELF32LE>(); |
| 585 | template void OutputSection::maybeCompress<ELF32BE>(); |
| 586 | template void OutputSection::maybeCompress<ELF64LE>(); |
| 587 | template void OutputSection::maybeCompress<ELF64BE>(); |
| 588 | |
| 589 | template void OutputSection::finalize<ELF32LE>(); |
| 590 | template void OutputSection::finalize<ELF32BE>(); |
| 591 | template void OutputSection::finalize<ELF64LE>(); |
| 592 | template void OutputSection::finalize<ELF64BE>(); |