blob: dfbde2f9556365022815b8086509f723df2a8b28 [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- 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 Ueyamaf5febef2016-05-24 02:55:45 +000012#include "EhFrame.h"
George Rimar58fa5242016-10-20 09:19:48 +000013#include "GdbIndex.h"
Rui Ueyama95642b92016-11-01 23:09:07 +000014#include "LinkerScript.h"
15#include "Memory.h"
Rui Ueyamafbbde542016-06-29 09:08:02 +000016#include "Strings.h"
George Rimar1a33c0f2016-11-10 09:05:20 +000017#include "SymbolListFile.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000018#include "SymbolTable.h"
Rui Ueyamae8a61022016-11-05 23:05:47 +000019#include "SyntheticSections.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000020#include "Target.h"
Rui Ueyamae9809502016-03-11 04:23:12 +000021#include "lld/Core/Parallel.h"
George Rimarf6bc65a2016-01-15 13:34:52 +000022#include "llvm/Support/Dwarf.h"
Rui Ueyama3a41be22016-04-07 22:49:21 +000023#include "llvm/Support/MD5.h"
Igor Kudrin1b0d7062015-10-22 08:21:35 +000024#include "llvm/Support/MathExtras.h"
Rafael Espindolaa42b3bc2016-09-27 16:43:49 +000025#include "llvm/Support/SHA1.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000026
Rafael Espindola5805c4f2015-09-21 21:38:08 +000027using namespace llvm;
Rui Ueyamadbcfedb2016-02-09 21:46:11 +000028using namespace llvm::dwarf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029using namespace llvm::object;
Rafael Espindolaa6627382015-10-06 23:56:53 +000030using namespace llvm::support::endian;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000031using namespace llvm::ELF;
32
33using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000034using namespace lld::elf;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000035
Rafael Espindolae08e78d2016-11-09 23:23:45 +000036OutputSectionBase::OutputSectionBase(StringRef Name, uint32_t Type,
37 uint64_t Flags)
Rafael Espindola5805c4f2015-09-21 21:38:08 +000038 : Name(Name) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000039 this->Type = Type;
40 this->Flags = Flags;
41 this->Addralign = 1;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000042}
43
Rafael Espindolae08e78d2016-11-09 23:23:45 +000044uint32_t OutputSectionBase::getPhdrFlags() const {
Rafael Espindola0b113672016-07-27 14:10:56 +000045 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 Espindola35c6af32015-09-25 17:19:10 +000053template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +000054void OutputSectionBase::writeHeaderTo(typename ELFT::Shdr *Shdr) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000055 Shdr->sh_entsize = Entsize;
56 Shdr->sh_addralign = Addralign;
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;
62 Shdr->sh_addr = Addr;
63 Shdr->sh_size = Size;
64 Shdr->sh_name = ShName;
Rui Ueyamac63c1db2016-03-13 06:50:33 +000065}
66
67template <class ELFT>
George Rimar58fa5242016-10-20 09:19:48 +000068GdbIndexSection<ELFT>::GdbIndexSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +000069 : OutputSectionBase(".gdb_index", SHT_PROGBITS, 0) {}
George Rimar58fa5242016-10-20 09:19:48 +000070
71template <class ELFT> void GdbIndexSection<ELFT>::parseDebugSections() {
72 std::vector<InputSection<ELFT> *> &IS =
73 static_cast<OutputSection<ELFT> *>(Out<ELFT>::DebugInfo)->Sections;
74
75 for (InputSection<ELFT> *I : IS)
76 readDwarf(I);
77}
78
79template <class ELFT>
80void GdbIndexSection<ELFT>::readDwarf(InputSection<ELFT> *I) {
81 std::vector<std::pair<uintX_t, uintX_t>> CuList = readCuList(I);
82 CompilationUnits.insert(CompilationUnits.end(), CuList.begin(), CuList.end());
83}
84
85template <class ELFT> void GdbIndexSection<ELFT>::finalize() {
86 parseDebugSections();
87
88 // GdbIndex header consist from version fields
89 // and 5 more fields with different kinds of offsets.
90 CuTypesOffset = CuListOffset + CompilationUnits.size() * CompilationUnitSize;
Rafael Espindola04a2e342016-11-09 01:42:41 +000091 this->Size = CuTypesOffset;
George Rimar58fa5242016-10-20 09:19:48 +000092}
93
94template <class ELFT> void GdbIndexSection<ELFT>::writeTo(uint8_t *Buf) {
95 write32le(Buf, 7); // Write Version
96 write32le(Buf + 4, CuListOffset); // CU list offset
97 write32le(Buf + 8, CuTypesOffset); // Types CU list offset
98 write32le(Buf + 12, CuTypesOffset); // Address area offset
99 write32le(Buf + 16, CuTypesOffset); // Symbol table offset
100 write32le(Buf + 20, CuTypesOffset); // Constant pool offset
101 Buf += 24;
102
103 // Write the CU list.
104 for (std::pair<uintX_t, uintX_t> CU : CompilationUnits) {
105 write64le(Buf, CU.first);
106 write64le(Buf + 8, CU.second);
107 Buf += 16;
108 }
109}
110
111template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000112PltSection<ELFT>::PltSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000113 : OutputSectionBase(".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000114 this->Addralign = 16;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000115}
116
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000117template <class ELFT> void PltSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000118 // At beginning of PLT, we have code to call the dynamic linker
119 // to resolve dynsyms at runtime. Write such code.
Rui Ueyama4a90f572016-06-16 16:28:50 +0000120 Target->writePltHeader(Buf);
121 size_t Off = Target->PltHeaderSize;
Rui Ueyamaf57a5902016-05-20 21:39:07 +0000122
George Rimarfb5d7f22015-11-26 19:58:51 +0000123 for (auto &I : Entries) {
Rui Ueyama9398f862016-01-29 04:15:02 +0000124 const SymbolBody *B = I.first;
George Rimar77b77792015-11-25 22:15:01 +0000125 unsigned RelOff = I.second;
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000126 uint64_t Got = B->getGotPltVA<ELFT>();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000127 uint64_t Plt = this->Addr + Off;
Rui Ueyama9398f862016-01-29 04:15:02 +0000128 Target->writePlt(Buf + Off, Got, Plt, B->PltIndex, RelOff);
Rui Ueyama724d6252016-01-29 01:49:32 +0000129 Off += Target->PltEntrySize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000130 }
131}
132
Rafael Espindola67d72c02016-03-11 12:06:30 +0000133template <class ELFT> void PltSection<ELFT>::addEntry(SymbolBody &Sym) {
134 Sym.PltIndex = Entries.size();
Eugene Levianta96d9022016-11-16 10:02:27 +0000135 unsigned RelOff = In<ELFT>::RelaPlt->getRelocOffset();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000136 Entries.push_back(std::make_pair(&Sym, RelOff));
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000137}
138
George Rimar648a2c32015-10-20 08:54:27 +0000139template <class ELFT> void PltSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000140 this->Size = Target->PltHeaderSize + Entries.size() * Target->PltEntrySize;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000141}
142
143template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000144HashTableSection<ELFT>::HashTableSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000145 : OutputSectionBase(".hash", SHT_HASH, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000146 this->Entsize = sizeof(Elf_Word);
147 this->Addralign = sizeof(Elf_Word);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000148}
149
Rui Ueyama0db335f2015-10-07 16:58:54 +0000150template <class ELFT> void HashTableSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000151 this->Link = Out<ELFT>::DynSymTab->SectionIndex;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000152
George Rimare9e1d322016-02-18 15:17:01 +0000153 unsigned NumEntries = 2; // nbucket and nchain.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000154 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols(); // The chain entries.
Rui Ueyama0db335f2015-10-07 16:58:54 +0000155
156 // Create as many buckets as there are symbols.
157 // FIXME: This is simplistic. We can try to optimize it, but implementing
158 // support for SHT_GNU_HASH is probably even more profitable.
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000159 NumEntries += Out<ELFT>::DynSymTab->getNumSymbols();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000160 this->Size = NumEntries * sizeof(Elf_Word);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000161}
162
163template <class ELFT> void HashTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000164 unsigned NumSymbols = Out<ELFT>::DynSymTab->getNumSymbols();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000165 auto *P = reinterpret_cast<Elf_Word *>(Buf);
166 *P++ = NumSymbols; // nbucket
167 *P++ = NumSymbols; // nchain
168
169 Elf_Word *Buckets = P;
170 Elf_Word *Chains = P + NumSymbols;
171
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000172 for (const SymbolTableEntry &S : Out<ELFT>::DynSymTab->getSymbols()) {
173 SymbolBody *Body = S.Symbol;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000174 StringRef Name = Body->getName();
Rui Ueyama572a6f72016-01-29 01:49:33 +0000175 unsigned I = Body->DynsymIndex;
Davide Italianoe7fd0be2016-11-07 21:56:56 +0000176 uint32_t Hash = hashSysV(Name) % NumSymbols;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000177 Chains[I] = Buckets[Hash];
178 Buckets[Hash] = I;
179 }
180}
181
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000182static uint32_t hashGnu(StringRef Name) {
183 uint32_t H = 5381;
184 for (uint8_t C : Name)
185 H = (H << 5) + H + C;
186 return H;
187}
188
189template <class ELFT>
190GnuHashTableSection<ELFT>::GnuHashTableSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000191 : OutputSectionBase(".gnu.hash", SHT_GNU_HASH, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000192 this->Entsize = ELFT::Is64Bits ? 0 : 4;
193 this->Addralign = sizeof(uintX_t);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000194}
195
196template <class ELFT>
197unsigned GnuHashTableSection<ELFT>::calcNBuckets(unsigned NumHashed) {
198 if (!NumHashed)
199 return 0;
200
201 // These values are prime numbers which are not greater than 2^(N-1) + 1.
202 // In result, for any particular NumHashed we return a prime number
203 // which is not greater than NumHashed.
204 static const unsigned Primes[] = {
205 1, 1, 3, 3, 7, 13, 31, 61, 127, 251,
206 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071};
207
208 return Primes[std::min<unsigned>(Log2_32_Ceil(NumHashed),
209 array_lengthof(Primes) - 1)];
210}
211
212// Bloom filter estimation: at least 8 bits for each hashed symbol.
213// GNU Hash table requirement: it should be a power of 2,
214// the minimum value is 1, even for an empty table.
215// Expected results for a 32-bit target:
216// calcMaskWords(0..4) = 1
217// calcMaskWords(5..8) = 2
218// calcMaskWords(9..16) = 4
219// For a 64-bit target:
220// calcMaskWords(0..8) = 1
221// calcMaskWords(9..16) = 2
222// calcMaskWords(17..32) = 4
223template <class ELFT>
224unsigned GnuHashTableSection<ELFT>::calcMaskWords(unsigned NumHashed) {
225 if (!NumHashed)
226 return 1;
227 return NextPowerOf2((NumHashed - 1) / sizeof(Elf_Off));
228}
229
230template <class ELFT> void GnuHashTableSection<ELFT>::finalize() {
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000231 unsigned NumHashed = Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000232 NBuckets = calcNBuckets(NumHashed);
233 MaskWords = calcMaskWords(NumHashed);
234 // Second hash shift estimation: just predefined values.
235 Shift2 = ELFT::Is64Bits ? 6 : 5;
236
Rafael Espindola04a2e342016-11-09 01:42:41 +0000237 this->Link = Out<ELFT>::DynSymTab->SectionIndex;
238 this->Size = sizeof(Elf_Word) * 4 // Header
239 + sizeof(Elf_Off) * MaskWords // Bloom Filter
240 + sizeof(Elf_Word) * NBuckets // Hash Buckets
241 + sizeof(Elf_Word) * NumHashed; // Hash Values
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000242}
243
244template <class ELFT> void GnuHashTableSection<ELFT>::writeTo(uint8_t *Buf) {
245 writeHeader(Buf);
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000246 if (Symbols.empty())
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000247 return;
248 writeBloomFilter(Buf);
249 writeHashTable(Buf);
250}
251
252template <class ELFT>
253void GnuHashTableSection<ELFT>::writeHeader(uint8_t *&Buf) {
254 auto *P = reinterpret_cast<Elf_Word *>(Buf);
255 *P++ = NBuckets;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000256 *P++ = Out<ELFT>::DynSymTab->getNumSymbols() - Symbols.size();
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000257 *P++ = MaskWords;
258 *P++ = Shift2;
259 Buf = reinterpret_cast<uint8_t *>(P);
260}
261
262template <class ELFT>
263void GnuHashTableSection<ELFT>::writeBloomFilter(uint8_t *&Buf) {
264 unsigned C = sizeof(Elf_Off) * 8;
265
266 auto *Masks = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama861c7312016-02-17 05:40:03 +0000267 for (const SymbolData &Sym : Symbols) {
268 size_t Pos = (Sym.Hash / C) & (MaskWords - 1);
269 uintX_t V = (uintX_t(1) << (Sym.Hash % C)) |
270 (uintX_t(1) << ((Sym.Hash >> Shift2) % C));
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000271 Masks[Pos] |= V;
272 }
273 Buf += sizeof(Elf_Off) * MaskWords;
274}
275
276template <class ELFT>
277void GnuHashTableSection<ELFT>::writeHashTable(uint8_t *Buf) {
278 Elf_Word *Buckets = reinterpret_cast<Elf_Word *>(Buf);
279 Elf_Word *Values = Buckets + NBuckets;
280
281 int PrevBucket = -1;
282 int I = 0;
Rui Ueyama861c7312016-02-17 05:40:03 +0000283 for (const SymbolData &Sym : Symbols) {
284 int Bucket = Sym.Hash % NBuckets;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000285 assert(PrevBucket <= Bucket);
286 if (Bucket != PrevBucket) {
Rui Ueyama861c7312016-02-17 05:40:03 +0000287 Buckets[Bucket] = Sym.Body->DynsymIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000288 PrevBucket = Bucket;
289 if (I > 0)
290 Values[I - 1] |= 1;
291 }
Rui Ueyama861c7312016-02-17 05:40:03 +0000292 Values[I] = Sym.Hash & ~1;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000293 ++I;
294 }
295 if (I > 0)
296 Values[I - 1] |= 1;
297}
298
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000299// Add symbols to this symbol hash table. Note that this function
300// destructively sort a given vector -- which is needed because
301// GNU-style hash table places some sorting requirements.
Rafael Espindola35c6af32015-09-25 17:19:10 +0000302template <class ELFT>
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000303void GnuHashTableSection<ELFT>::addSymbols(std::vector<SymbolTableEntry> &V) {
Davide Italianob4b68b62016-07-04 19:49:55 +0000304 // Ideally this will just be 'auto' but GCC 6.1 is not able
305 // to deduce it correctly.
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000306 std::vector<SymbolTableEntry>::iterator Mid =
307 std::stable_partition(V.begin(), V.end(), [](const SymbolTableEntry &S) {
308 return S.Symbol->isUndefined();
309 });
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000310 if (Mid == V.end())
Igor Kudrinf1d60292015-10-28 07:05:56 +0000311 return;
Davide Italianof8591cf2016-07-06 17:41:55 +0000312 for (auto I = Mid, E = V.end(); I != E; ++I) {
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000313 SymbolBody *B = I->Symbol;
Reid Kleckner2918d0b2016-10-20 00:13:34 +0000314 size_t StrOff = I->StrTabOffset;
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000315 Symbols.push_back({B, StrOff, hashGnu(B->getName())});
316 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000317
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000318 unsigned NBuckets = calcNBuckets(Symbols.size());
319 std::stable_sort(Symbols.begin(), Symbols.end(),
Rui Ueyama861c7312016-02-17 05:40:03 +0000320 [&](const SymbolData &L, const SymbolData &R) {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000321 return L.Hash % NBuckets < R.Hash % NBuckets;
322 });
323
Rui Ueyama91c0a5d2016-02-17 05:40:01 +0000324 V.erase(Mid, V.end());
Rui Ueyama861c7312016-02-17 05:40:03 +0000325 for (const SymbolData &Sym : Symbols)
326 V.push_back({Sym.Body, Sym.STName});
Igor Kudrinf1d60292015-10-28 07:05:56 +0000327}
328
George Rimard3566302016-06-20 11:55:12 +0000329// Returns the number of version definition entries. Because the first entry
330// is for the version definition itself, it is the number of versioned symbols
331// plus one. Note that we don't support multiple versions yet.
Rui Ueyamaaf469d42016-07-16 04:09:27 +0000332static unsigned getVerDefNum() { return Config->VersionDefinitions.size() + 1; }
George Rimard3566302016-06-20 11:55:12 +0000333
Igor Kudrinf1d60292015-10-28 07:05:56 +0000334template <class ELFT>
George Rimarf6bc65a2016-01-15 13:34:52 +0000335EhFrameHeader<ELFT>::EhFrameHeader()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000336 : OutputSectionBase(".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC) {}
George Rimarf6bc65a2016-01-15 13:34:52 +0000337
Rui Ueyama95a232e2016-05-23 01:45:05 +0000338// .eh_frame_hdr contains a binary search table of pointers to FDEs.
339// Each entry of the search table consists of two values,
340// the starting PC from where FDEs covers, and the FDE's address.
341// It is sorted by PC.
George Rimarf6bc65a2016-01-15 13:34:52 +0000342template <class ELFT> void EhFrameHeader<ELFT>::writeTo(uint8_t *Buf) {
343 const endianness E = ELFT::TargetEndianness;
344
Rui Ueyamae75e9332016-05-23 03:00:33 +0000345 // Sort the FDE list by their PC and uniqueify. Usually there is only
346 // one FDE for a PC (i.e. function), but if ICF merges two functions
347 // into one, there can be more than one FDEs pointing to the address.
348 auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
349 std::stable_sort(Fdes.begin(), Fdes.end(), Less);
350 auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
351 Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
Peter Collingbournec98de132016-04-11 16:40:08 +0000352
Rui Ueyama1b2936f2016-05-23 01:31:10 +0000353 Buf[0] = 1;
354 Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
355 Buf[2] = DW_EH_PE_udata4;
356 Buf[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000357 write32<E>(Buf + 4, Out<ELFT>::EhFrame->Addr - this->Addr - 4);
Rui Ueyamae75e9332016-05-23 03:00:33 +0000358 write32<E>(Buf + 8, Fdes.size());
George Rimarf6bc65a2016-01-15 13:34:52 +0000359 Buf += 12;
360
Rafael Espindola04a2e342016-11-09 01:42:41 +0000361 uintX_t VA = this->Addr;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000362 for (FdeData &Fde : Fdes) {
363 write32<E>(Buf, Fde.Pc - VA);
364 write32<E>(Buf + 4, Fde.FdeVA - VA);
George Rimarf6bc65a2016-01-15 13:34:52 +0000365 Buf += 8;
366 }
367}
368
Rui Ueyamade9777a2016-05-23 16:30:41 +0000369template <class ELFT> void EhFrameHeader<ELFT>::finalize() {
370 // .eh_frame_hdr has a 12 bytes header followed by an array of FDEs.
Rafael Espindola04a2e342016-11-09 01:42:41 +0000371 this->Size = 12 + Out<ELFT>::EhFrame->NumFdes * 8;
Rui Ueyamade9777a2016-05-23 16:30:41 +0000372}
373
George Rimarf6bc65a2016-01-15 13:34:52 +0000374template <class ELFT>
Rui Ueyamae75e9332016-05-23 03:00:33 +0000375void EhFrameHeader<ELFT>::addFde(uint32_t Pc, uint32_t FdeVA) {
376 Fdes.push_back({Pc, FdeVA});
George Rimarf6bc65a2016-01-15 13:34:52 +0000377}
378
Rui Ueyamadf5d14d2016-11-09 22:32:42 +0000379template <class ELFT> static uint64_t getEntsize(uint32_t Type) {
380 switch (Type) {
381 case SHT_RELA:
382 return sizeof(typename ELFT::Rela);
383 case SHT_REL:
384 return sizeof(typename ELFT::Rel);
385 case SHT_MIPS_REGINFO:
386 return sizeof(Elf_Mips_RegInfo<ELFT>);
387 case SHT_MIPS_OPTIONS:
388 return sizeof(Elf_Mips_Options<ELFT>) + sizeof(Elf_Mips_RegInfo<ELFT>);
389 case SHT_MIPS_ABIFLAGS:
390 return sizeof(Elf_Mips_ABIFlags<ELFT>);
391 default:
392 return 0;
393 }
394}
395
George Rimarf6bc65a2016-01-15 13:34:52 +0000396template <class ELFT>
George Rimar58941ee2016-02-25 08:23:37 +0000397OutputSection<ELFT>::OutputSection(StringRef Name, uint32_t Type, uintX_t Flags)
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000398 : OutputSectionBase(Name, Type, Flags) {
Rui Ueyamadf5d14d2016-11-09 22:32:42 +0000399 this->Entsize = getEntsize<ELFT>(Type);
George Rimar58941ee2016-02-25 08:23:37 +0000400}
401
402template <class ELFT> void OutputSection<ELFT>::finalize() {
Eugene Levianta96d9022016-11-16 10:02:27 +0000403 if (!Config->Relocatable) {
404 // SHF_LINK_ORDER only has meaning in relocatable objects
405 this->Flags &= ~SHF_LINK_ORDER;
406 return;
407 }
408
Rafael Espindola04a2e342016-11-09 01:42:41 +0000409 uint32_t Type = this->Type;
Eugene Levianta96d9022016-11-16 10:02:27 +0000410 if ((this->Flags & SHF_LINK_ORDER) && !this->Sections.empty()) {
411 // When doing a relocatable link we must preserve the link order
412 // dependency of sections with the SHF_LINK_ORDER flag. The dependency
413 // is indicated by the sh_link field. We need to translate the
414 // InputSection sh_link to the OutputSection sh_link, all InputSections
415 // in the OutputSection have the same dependency.
416 if (auto *D = this->Sections.front()->getLinkOrderDep())
417 this->Link = D->OutSec->SectionIndex;
Peter Smith580ba952016-10-21 11:25:33 +0000418 }
Eugene Leviant22eb0262016-11-14 09:16:00 +0000419
George Rimar58941ee2016-02-25 08:23:37 +0000420 if (Type != SHT_RELA && Type != SHT_REL)
421 return;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000422 this->Link = Out<ELFT>::SymTab->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000423 // sh_info for SHT_REL[A] sections should contain the section header index of
424 // the section to which the relocation applies.
425 InputSectionBase<ELFT> *S = Sections[0]->getRelocatedSection();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000426 this->Info = S->OutSec->SectionIndex;
George Rimar58941ee2016-02-25 08:23:37 +0000427}
Rafael Espindola35c6af32015-09-25 17:19:10 +0000428
429template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000430void OutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama0b289522016-02-25 18:43:51 +0000431 assert(C->Live);
Rui Ueyama40845e62015-12-26 05:51:07 +0000432 auto *S = cast<InputSection<ELFT>>(C);
433 Sections.push_back(S);
434 S->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000435 this->updateAlignment(S->Alignment);
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +0000436 // Keep sh_entsize value of the input section to be able to perform merging
437 // later during a final linking using the generated relocatable object.
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000438 if (Config->Relocatable && (S->Flags & SHF_MERGE))
Rafael Espindola04a2e342016-11-09 01:42:41 +0000439 this->Entsize = S->Entsize;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000440}
441
Rui Ueyama809d8e22016-06-23 04:33:42 +0000442// This function is called after we sort input sections
443// and scan relocations to setup sections' offsets.
444template <class ELFT> void OutputSection<ELFT>::assignOffsets() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000445 uintX_t Off = this->Size;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000446 for (InputSection<ELFT> *S : Sections) {
447 Off = alignTo(Off, S->Alignment);
448 S->OutSecOff = Off;
449 Off += S->getSize();
450 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000451 this->Size = Off;
Rui Ueyama5af83682016-02-11 23:41:38 +0000452}
453
George Rimar1a33c0f2016-11-10 09:05:20 +0000454template <class ELFT>
455void OutputSection<ELFT>::sort(
456 std::function<unsigned(InputSection<ELFT> *S)> Order) {
457 typedef std::pair<unsigned, InputSection<ELFT> *> Pair;
458 auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
459
460 std::vector<Pair> V;
461 for (InputSection<ELFT> *S : Sections)
462 V.push_back({Order(S), S});
463 std::stable_sort(V.begin(), V.end(), Comp);
464 Sections.clear();
465 for (Pair &P : V)
466 Sections.push_back(P.second);
467}
468
Rui Ueyamac4185702016-02-10 23:20:42 +0000469// Sorts input sections by section name suffixes, so that .foo.N comes
470// before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
Rui Ueyama5af83682016-02-11 23:41:38 +0000471// We want to keep the original order if the priorities are the same
472// because the compiler keeps the original initialization order in a
473// translation unit and we need to respect that.
Rui Ueyamac4185702016-02-10 23:20:42 +0000474// For more detail, read the section of the GCC's manual about init_priority.
Rui Ueyama5af83682016-02-11 23:41:38 +0000475template <class ELFT> void OutputSection<ELFT>::sortInitFini() {
Rui Ueyamac4185702016-02-10 23:20:42 +0000476 // Sort sections by priority.
George Rimar1a33c0f2016-11-10 09:05:20 +0000477 sort([](InputSection<ELFT> *S) { return getPriority(S->Name); });
Rui Ueyama5af83682016-02-11 23:41:38 +0000478}
Rui Ueyamac4185702016-02-10 23:20:42 +0000479
Rui Ueyama5af83682016-02-11 23:41:38 +0000480// Returns true if S matches /Filename.?\.o$/.
481static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
482 if (!S.endswith(".o"))
483 return false;
484 S = S.drop_back(2);
485 if (S.endswith(Filename))
486 return true;
487 return !S.empty() && S.drop_back().endswith(Filename);
488}
489
490static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
491static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
492
493// .ctors and .dtors are sorted by this priority from highest to lowest.
494//
495// 1. The section was contained in crtbegin (crtbegin contains
496// some sentinel value in its .ctors and .dtors so that the runtime
497// can find the beginning of the sections.)
498//
499// 2. The section has an optional priority value in the form of ".ctors.N"
500// or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
501// they are compared as string rather than number.
502//
503// 3. The section is just ".ctors" or ".dtors".
504//
505// 4. The section was contained in crtend, which contains an end marker.
506//
507// In an ideal world, we don't need this function because .init_array and
508// .ctors are duplicate features (and .init_array is newer.) However, there
509// are too many real-world use cases of .ctors, so we had no choice to
510// support that with this rather ad-hoc semantics.
511template <class ELFT>
512static bool compCtors(const InputSection<ELFT> *A,
513 const InputSection<ELFT> *B) {
514 bool BeginA = isCrtbegin(A->getFile()->getName());
515 bool BeginB = isCrtbegin(B->getFile()->getName());
516 if (BeginA != BeginB)
517 return BeginA;
518 bool EndA = isCrtend(A->getFile()->getName());
519 bool EndB = isCrtend(B->getFile()->getName());
520 if (EndA != EndB)
521 return EndB;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000522 StringRef X = A->Name;
523 StringRef Y = B->Name;
Rui Ueyama5af83682016-02-11 23:41:38 +0000524 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);
Rui Ueyama24b794e2016-02-12 00:38:46 +0000528 if (X.empty() && Y.empty())
529 return false;
Rui Ueyama5af83682016-02-11 23:41:38 +0000530 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.
536template <class ELFT> void OutputSection<ELFT>::sortCtorsDtors() {
537 std::stable_sort(Sections.begin(), Sections.end(), compCtors<ELFT>);
Rui Ueyamac4185702016-02-10 23:20:42 +0000538}
539
George Rimare2ee72b2016-02-26 14:48:31 +0000540static void fill(uint8_t *Buf, size_t Size, ArrayRef<uint8_t> A) {
541 size_t I = 0;
542 for (; I + A.size() < Size; I += A.size())
543 memcpy(Buf + I, A.data(), A.size());
544 memcpy(Buf + I, A.data(), Size - I);
545}
546
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000547template <class ELFT> void OutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000548 ArrayRef<uint8_t> Filler = Script<ELFT>::X->getFiller(this->Name);
George Rimare2ee72b2016-02-26 14:48:31 +0000549 if (!Filler.empty())
Rafael Espindola04a2e342016-11-09 01:42:41 +0000550 fill(Buf, this->Size, Filler);
Rui Ueyamaeb943e82016-11-04 18:22:36 +0000551 if (Config->Threads) {
552 parallel_for_each(Sections.begin(), Sections.end(),
553 [=](InputSection<ELFT> *C) { C->writeTo(Buf); });
554 } else {
555 for (InputSection<ELFT> *C : Sections)
556 C->writeTo(Buf);
557 }
George Rimare38cbab2016-09-26 19:22:50 +0000558 // Linker scripts may have BYTE()-family commands with which you
559 // can write arbitrary bytes to the output. Process them if any.
560 Script<ELFT>::X->writeDataBytes(this->Name, Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000561}
562
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000563template <class ELFT>
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000564EhOutputSection<ELFT>::EhOutputSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000565 : OutputSectionBase(".eh_frame", SHT_PROGBITS, SHF_ALLOC) {}
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000566
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000567// Search for an existing CIE record or create a new one.
568// CIE records from input object files are uniquified by their contents
569// and where their relocations point to.
570template <class ELFT>
571template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000572CieRecord *EhOutputSection<ELFT>::addCie(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000573 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +0000574 ArrayRef<RelTy> Rels) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000575 const endianness E = ELFT::TargetEndianness;
Rui Ueyamad8849272016-05-25 16:37:01 +0000576 if (read32<E>(Piece.data().data() + 4) != 0)
Rafael Espindola042a3f22016-09-08 14:06:08 +0000577 fatal("CIE expected at beginning of .eh_frame: " + Sec->Name);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000578
579 SymbolBody *Personality = nullptr;
Rafael Espindola2deeb602016-07-21 20:18:30 +0000580 unsigned FirstRelI = Piece.FirstRelocation;
581 if (FirstRelI != (unsigned)-1)
582 Personality = &Sec->getFile()->getRelocTargetSym(Rels[FirstRelI]);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000583
584 // Search for an existing CIE by CIE contents/relocation target pair.
Rui Ueyamad8849272016-05-25 16:37:01 +0000585 CieRecord *Cie = &CieMap[{Piece.data(), Personality}];
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000586
587 // If not found, create a new one.
Rui Ueyamae2060aa2016-05-22 23:52:56 +0000588 if (Cie->Piece == nullptr) {
589 Cie->Piece = &Piece;
Rui Ueyamae2060aa2016-05-22 23:52:56 +0000590 Cies.push_back(Cie);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000591 }
592 return Cie;
593}
594
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000595// There is one FDE per function. Returns true if a given FDE
596// points to a live function.
597template <class ELFT>
598template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000599bool EhOutputSection<ELFT>::isFdeLive(EhSectionPiece &Piece,
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000600 EhInputSection<ELFT> *Sec,
Rafael Espindola2deeb602016-07-21 20:18:30 +0000601 ArrayRef<RelTy> Rels) {
602 unsigned FirstRelI = Piece.FirstRelocation;
603 if (FirstRelI == (unsigned)-1)
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000604 fatal("FDE doesn't reference another section");
Rafael Espindola2deeb602016-07-21 20:18:30 +0000605 const RelTy &Rel = Rels[FirstRelI];
606 SymbolBody &B = Sec->getFile()->getRelocTargetSym(Rel);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000607 auto *D = dyn_cast<DefinedRegular<ELFT>>(&B);
608 if (!D || !D->Section)
609 return false;
610 InputSectionBase<ELFT> *Target = D->Section->Repl;
611 return Target && Target->Live;
612}
613
614// .eh_frame is a sequence of CIE or FDE records. In general, there
615// is one CIE record per input object file which is followed by
616// a list of FDEs. This function searches an existing CIE or create a new
617// one and associates FDEs to the CIE.
Rui Ueyamac0c92602016-02-05 22:56:03 +0000618template <class ELFT>
Rui Ueyamafc467e72016-03-13 05:06:50 +0000619template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000620void EhOutputSection<ELFT>::addSectionAux(EhInputSection<ELFT> *Sec,
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000621 ArrayRef<RelTy> Rels) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000622 const endianness E = ELFT::TargetEndianness;
Rafael Espindola820f4bb2016-05-24 15:17:47 +0000623
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000624 DenseMap<size_t, CieRecord *> OffsetToCie;
Rafael Espindola2deeb602016-07-21 20:18:30 +0000625 for (EhSectionPiece &Piece : Sec->Pieces) {
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000626 // The empty record is the end marker.
Rui Ueyamad8849272016-05-25 16:37:01 +0000627 if (Piece.size() == 4)
Rafael Espindola5ee9e7f2016-05-24 19:14:09 +0000628 return;
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000629
630 size_t Offset = Piece.InputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000631 uint32_t ID = read32<E>(Piece.data().data() + 4);
Rafael Espindola1f5696f2016-05-24 16:03:27 +0000632 if (ID == 0) {
633 OffsetToCie[Offset] = addCie(Piece, Sec, Rels);
634 continue;
635 }
636
637 uint32_t CieOffset = Offset + 4 - ID;
638 CieRecord *Cie = OffsetToCie[CieOffset];
639 if (!Cie)
640 fatal("invalid CIE reference");
641
642 if (!isFdeLive(Piece, Sec, Rels))
643 continue;
644 Cie->FdePieces.push_back(&Piece);
Rui Ueyamade9777a2016-05-23 16:30:41 +0000645 NumFdes++;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000646 }
647}
648
649template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000650void EhOutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000651 auto *Sec = cast<EhInputSection<ELFT>>(C);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000652 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000653 this->updateAlignment(Sec->Alignment);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000654 Sections.push_back(Sec);
655
656 // .eh_frame is a sequence of CIE or FDE records. This function
657 // splits it into pieces so that we can call
658 // SplitInputSection::getSectionPiece on the section.
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000659 Sec->split();
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000660 if (Sec->Pieces.empty())
661 return;
662
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000663 if (Sec->NumRelocations) {
664 if (Sec->AreRelocsRela)
665 addSectionAux(Sec, Sec->relas());
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000666 else
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000667 addSectionAux(Sec, Sec->rels());
Rui Ueyama0de86c12016-01-27 22:23:44 +0000668 return;
669 }
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000670 addSectionAux(Sec, makeArrayRef<Elf_Rela>(nullptr, nullptr));
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000671}
672
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000673template <class ELFT>
Rui Ueyama644ac652016-05-22 00:17:11 +0000674static void writeCieFde(uint8_t *Buf, ArrayRef<uint8_t> D) {
675 memcpy(Buf, D.data(), D.size());
Rui Ueyamac0449a62016-05-21 18:10:13 +0000676
677 // Fix the size field. -4 since size does not include the size field itself.
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000678 const endianness E = ELFT::TargetEndianness;
Rui Ueyama644ac652016-05-22 00:17:11 +0000679 write32<E>(Buf, alignTo(D.size(), sizeof(typename ELFT::uint)) - 4);
Rafael Espindola56004c52016-04-07 14:22:09 +0000680}
681
Rui Ueyama1e479c22016-05-23 15:07:59 +0000682template <class ELFT> void EhOutputSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000683 if (this->Size)
Rui Ueyamae9381bd2016-07-16 02:47:42 +0000684 return; // Already finalized.
Rafael Espindola56004c52016-04-07 14:22:09 +0000685
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000686 size_t Off = 0;
687 for (CieRecord *Cie : Cies) {
688 Cie->Piece->OutputOff = Off;
689 Off += alignTo(Cie->Piece->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +0000690
Rafael Espindola113860b2016-10-20 10:55:58 +0000691 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000692 Fde->OutputOff = Off;
693 Off += alignTo(Fde->size(), sizeof(uintX_t));
Rafael Espindola56004c52016-04-07 14:22:09 +0000694 }
695 }
Rafael Espindola04a2e342016-11-09 01:42:41 +0000696 this->Size = Off;
Rafael Espindola91bd48a2015-12-24 20:44:06 +0000697}
698
Rui Ueyamae75e9332016-05-23 03:00:33 +0000699template <class ELFT> static uint64_t readFdeAddr(uint8_t *Buf, int Size) {
700 const endianness E = ELFT::TargetEndianness;
701 switch (Size) {
702 case DW_EH_PE_udata2:
703 return read16<E>(Buf);
704 case DW_EH_PE_udata4:
705 return read32<E>(Buf);
706 case DW_EH_PE_udata8:
707 return read64<E>(Buf);
708 case DW_EH_PE_absptr:
709 if (ELFT::Is64Bits)
710 return read64<E>(Buf);
711 return read32<E>(Buf);
712 }
713 fatal("unknown FDE size encoding");
714}
715
716// Returns the VA to which a given FDE (on a mmap'ed buffer) is applied to.
717// We need it to create .eh_frame_hdr section.
718template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +0000719typename ELFT::uint EhOutputSection<ELFT>::getFdePc(uint8_t *Buf, size_t FdeOff,
Rui Ueyamae75e9332016-05-23 03:00:33 +0000720 uint8_t Enc) {
Rui Ueyama2ab3d202016-05-23 16:36:47 +0000721 // The starting address to which this FDE applies is
Rui Ueyamae75e9332016-05-23 03:00:33 +0000722 // stored at FDE + 8 byte.
723 size_t Off = FdeOff + 8;
724 uint64_t Addr = readFdeAddr<ELFT>(Buf + Off, Enc & 0x7);
725 if ((Enc & 0x70) == DW_EH_PE_absptr)
726 return Addr;
727 if ((Enc & 0x70) == DW_EH_PE_pcrel)
Rafael Espindola04a2e342016-11-09 01:42:41 +0000728 return Addr + this->Addr + Off;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000729 fatal("unknown FDE size relative encoding");
730}
731
Rui Ueyama1e479c22016-05-23 15:07:59 +0000732template <class ELFT> void EhOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000733 const endianness E = ELFT::TargetEndianness;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000734 for (CieRecord *Cie : Cies) {
735 size_t CieOffset = Cie->Piece->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000736 writeCieFde<ELFT>(Buf + CieOffset, Cie->Piece->data());
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000737
Rafael Espindola32aca872016-10-05 18:40:00 +0000738 for (EhSectionPiece *Fde : Cie->FdePieces) {
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000739 size_t Off = Fde->OutputOff;
Rui Ueyamad8849272016-05-25 16:37:01 +0000740 writeCieFde<ELFT>(Buf + Off, Fde->data());
Rafael Espindola56004c52016-04-07 14:22:09 +0000741
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000742 // FDE's second word should have the offset to an associated CIE.
743 // Write it.
744 write32<E>(Buf + Off + 4, Off + 4 - CieOffset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000745 }
746 }
747
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000748 for (EhInputSection<ELFT> *S : Sections)
Rafael Espindola22ef9562016-04-13 01:40:19 +0000749 S->relocate(Buf, nullptr);
Rui Ueyamae75e9332016-05-23 03:00:33 +0000750
751 // Construct .eh_frame_hdr. .eh_frame_hdr is a binary search table
Rui Ueyama2ab3d202016-05-23 16:36:47 +0000752 // to get a FDE from an address to which FDE is applied. So here
Rui Ueyamae75e9332016-05-23 03:00:33 +0000753 // we obtain two addresses and pass them to EhFrameHdr object.
Rui Ueyama3b31e672016-05-23 16:24:16 +0000754 if (Out<ELFT>::EhFrameHdr) {
755 for (CieRecord *Cie : Cies) {
Rui Ueyamad8849272016-05-25 16:37:01 +0000756 uint8_t Enc = getFdeEncoding<ELFT>(Cie->Piece->data());
Rui Ueyama3b31e672016-05-23 16:24:16 +0000757 for (SectionPiece *Fde : Cie->FdePieces) {
Rui Ueyama6de2e682016-05-24 02:08:38 +0000758 uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000759 uintX_t FdeVA = this->Addr + Fde->OutputOff;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000760 Out<ELFT>::EhFrameHdr->addFde(Pc, FdeVA);
761 }
Rui Ueyamae75e9332016-05-23 03:00:33 +0000762 }
763 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000764}
765
766template <class ELFT>
Rui Ueyamad97e5c42016-01-07 18:33:11 +0000767MergeOutputSection<ELFT>::MergeOutputSection(StringRef Name, uint32_t Type,
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000768 uintX_t Flags, uintX_t Alignment)
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000769 : OutputSectionBase(Name, Type, Flags),
Rui Ueyama818bb2f2016-07-16 18:55:47 +0000770 Builder(StringTableBuilder::RAW, Alignment) {}
Rafael Espindolac159c962015-10-19 21:00:02 +0000771
772template <class ELFT> void MergeOutputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola259d6452016-10-04 22:43:38 +0000773 Builder.write(Buf);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000774}
775
Rafael Espindolac159c962015-10-19 21:00:02 +0000776template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000777void MergeOutputSection<ELFT>::addSection(InputSectionData *C) {
Rui Ueyama10803512016-05-22 00:25:30 +0000778 auto *Sec = cast<MergeInputSection<ELFT>>(C);
779 Sec->OutSec = this;
Rui Ueyama424b4082016-06-17 01:18:46 +0000780 this->updateAlignment(Sec->Alignment);
Rafael Espindola04a2e342016-11-09 01:42:41 +0000781 this->Entsize = Sec->Entsize;
Rui Ueyama406b4692016-05-27 14:39:13 +0000782 Sections.push_back(Sec);
Rafael Espindolac159c962015-10-19 21:00:02 +0000783
Rafael Espindola113860b2016-10-20 10:55:58 +0000784 auto HashI = Sec->Hashes.begin();
785 for (auto I = Sec->Pieces.begin(), E = Sec->Pieces.end(); I != E; ++I) {
786 SectionPiece &Piece = *I;
787 uint32_t Hash = *HashI;
788 ++HashI;
Rui Ueyama3ea87272016-05-22 00:13:04 +0000789 if (!Piece.Live)
Rafael Espindola0b9531c2016-04-22 22:09:35 +0000790 continue;
Rafael Espindola113860b2016-10-20 10:55:58 +0000791 StringRef Data = toStringRef(Sec->getData(I));
792 CachedHashStringRef V(Data, Hash);
Rafael Espindola5fc2b1d2016-10-05 19:36:02 +0000793 uintX_t OutputOffset = Builder.add(V);
Rafael Espindola7b8bb532016-10-05 15:35:18 +0000794 if (!shouldTailMerge())
Rui Ueyamac6ebb022016-05-22 01:03:41 +0000795 Piece.OutputOff = OutputOffset;
Rafael Espindolac159c962015-10-19 21:00:02 +0000796 }
Rafael Espindolac159c962015-10-19 21:00:02 +0000797}
798
799template <class ELFT>
Justin Lebaree34a732016-10-17 22:24:36 +0000800unsigned MergeOutputSection<ELFT>::getOffset(CachedHashStringRef Val) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000801 return Builder.getOffset(Val);
802}
803
804template <class ELFT> bool MergeOutputSection<ELFT>::shouldTailMerge() const {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000805 return Config->Optimize >= 2 && this->Flags & SHF_STRINGS;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000806}
807
808template <class ELFT> void MergeOutputSection<ELFT>::finalize() {
809 if (shouldTailMerge())
810 Builder.finalize();
Rafael Espindola259d6452016-10-04 22:43:38 +0000811 else
812 Builder.finalizeInOrder();
Rafael Espindola04a2e342016-11-09 01:42:41 +0000813 this->Size = Builder.getSize();
Rafael Espindolac159c962015-10-19 21:00:02 +0000814}
815
Rui Ueyama406b4692016-05-27 14:39:13 +0000816template <class ELFT> void MergeOutputSection<ELFT>::finalizePieces() {
817 for (MergeInputSection<ELFT> *Sec : Sections)
818 Sec->finalizePieces();
819}
820
Rafael Espindolac159c962015-10-19 21:00:02 +0000821template <class ELFT>
Rafael Espindola35c6af32015-09-25 17:19:10 +0000822SymbolTableSection<ELFT>::SymbolTableSection(
Rui Ueyamaace4f902016-05-24 04:25:47 +0000823 StringTableSection<ELFT> &StrTabSec)
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000824 : OutputSectionBase(StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
825 StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
826 StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Rui Ueyamaace4f902016-05-24 04:25:47 +0000827 StrTabSec(StrTabSec) {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000828 this->Entsize = sizeof(Elf_Sym);
829 this->Addralign = sizeof(uintX_t);
Rafael Espindola35c6af32015-09-25 17:19:10 +0000830}
831
Igor Kudrinf4cdfe882015-11-12 04:08:12 +0000832// Orders symbols according to their positions in the GOT,
833// in compliance with MIPS ABI rules.
834// See "Global Offset Table" in Chapter 5 in the following document
835// for detailed description:
836// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000837static bool sortMipsSymbols(const SymbolBody *L, const SymbolBody *R) {
Simon Atanasyand2980d32016-03-29 14:07:22 +0000838 // Sort entries related to non-local preemptible symbols by GOT indexes.
839 // All other entries go to the first part of GOT in arbitrary order.
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000840 bool LIsInLocalGot = !L->IsInGlobalMipsGot;
841 bool RIsInLocalGot = !R->IsInGlobalMipsGot;
Simon Atanasyan7b8481b2016-06-20 11:37:56 +0000842 if (LIsInLocalGot || RIsInLocalGot)
843 return !RIsInLocalGot;
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000844 return L->GotIndex < R->GotIndex;
Igor Kudrinf4cdfe882015-11-12 04:08:12 +0000845}
846
Rafael Espindolabadd3972016-04-08 15:30:56 +0000847static uint8_t getSymbolBinding(SymbolBody *Body) {
Peter Collingbourne4f952702016-05-01 04:55:03 +0000848 Symbol *S = Body->symbol();
George Rimar11f83b72016-08-19 08:31:02 +0000849 if (Config->Relocatable)
850 return S->Binding;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +0000851 uint8_t Visibility = S->Visibility;
Rafael Espindolabadd3972016-04-08 15:30:56 +0000852 if (Visibility != STV_DEFAULT && Visibility != STV_PROTECTED)
853 return STB_LOCAL;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +0000854 if (Config->NoGnuUnique && S->Binding == STB_GNU_UNIQUE)
Rafael Espindolabadd3972016-04-08 15:30:56 +0000855 return STB_GLOBAL;
Rafael Espindola9e32e4f2016-04-26 13:50:46 +0000856 return S->Binding;
Rafael Espindolabadd3972016-04-08 15:30:56 +0000857}
858
Rui Ueyama0db335f2015-10-07 16:58:54 +0000859template <class ELFT> void SymbolTableSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +0000860 if (this->Size)
Igor Kudrin2169b1b2015-11-02 10:46:14 +0000861 return; // Already finalized.
862
Rafael Espindola04a2e342016-11-09 01:42:41 +0000863 this->Size = getNumSymbols() * sizeof(Elf_Sym);
Eugene Leviant22eb0262016-11-14 09:16:00 +0000864 this->Link = StrTabSec.OutSec->SectionIndex;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000865 this->Info = NumLocals + 1;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000866
George Rimar58941ee2016-02-25 08:23:37 +0000867 if (Config->Relocatable) {
868 size_t I = NumLocals;
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000869 for (const SymbolTableEntry &S : Symbols)
870 S.Symbol->DynsymIndex = ++I;
George Rimar58941ee2016-02-25 08:23:37 +0000871 return;
872 }
873
Igor Kudrinab665fc2015-10-20 21:47:58 +0000874 if (!StrTabSec.isDynamic()) {
875 std::stable_sort(Symbols.begin(), Symbols.end(),
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000876 [](const SymbolTableEntry &L, const SymbolTableEntry &R) {
877 return getSymbolBinding(L.Symbol) == STB_LOCAL &&
878 getSymbolBinding(R.Symbol) != STB_LOCAL;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000879 });
880 return;
881 }
Igor Kudrinf1d60292015-10-28 07:05:56 +0000882 if (Out<ELFT>::GnuHashTab)
883 // NB: It also sorts Symbols to meet the GNU hash table requirements.
884 Out<ELFT>::GnuHashTab->addSymbols(Symbols);
Igor Kudrinf4cdfe882015-11-12 04:08:12 +0000885 else if (Config->EMachine == EM_MIPS)
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000886 std::stable_sort(Symbols.begin(), Symbols.end(),
887 [](const SymbolTableEntry &L, const SymbolTableEntry &R) {
888 return sortMipsSymbols(L.Symbol, R.Symbol);
889 });
Igor Kudrinab665fc2015-10-20 21:47:58 +0000890 size_t I = 0;
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000891 for (const SymbolTableEntry &S : Symbols)
892 S.Symbol->DynsymIndex = ++I;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000893}
894
George Rimara4c7e742016-10-20 08:36:42 +0000895template <class ELFT> void SymbolTableSection<ELFT>::addSymbol(SymbolBody *B) {
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000896 Symbols.push_back({B, StrTabSec.addString(B->getName(), false)});
Rui Ueyama0db335f2015-10-07 16:58:54 +0000897}
898
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000899template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000900 Buf += sizeof(Elf_Sym);
901
902 // All symbols with STB_LOCAL binding precede the weak and global symbols.
903 // .dynsym only contains global symbols.
George Rimar9503f6d2016-08-31 08:46:30 +0000904 if (Config->Discard != DiscardPolicy::All && !StrTabSec.isDynamic())
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000905 writeLocalSymbols(Buf);
906
907 writeGlobalSymbols(Buf);
908}
909
910template <class ELFT>
911void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
912 // Iterate over all input object files to copy their local symbols
913 // to the output symbol table pointed by Buf.
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000914 for (ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000915 for (const std::pair<const DefinedRegular<ELFT> *, size_t> &P :
916 File->KeptLocalSyms) {
917 const DefinedRegular<ELFT> &Body = *P.first;
918 InputSectionBase<ELFT> *Section = Body.Section;
Rui Ueyamac55733e2015-09-30 00:54:29 +0000919 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000920
921 if (!Section) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000922 ESym->st_shndx = SHN_ABS;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000923 ESym->st_value = Body.Value;
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000924 } else {
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000925 const OutputSectionBase *OutSec = Section->OutSec;
Rafael Espindolac159c962015-10-19 21:00:02 +0000926 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000927 ESym->st_value = OutSec->Addr + Section->getOffset(Body);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000928 }
Rafael Espindolae2c24612016-01-29 01:24:25 +0000929 ESym->st_name = P.second;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000930 ESym->st_size = Body.template getSize<ELFT>();
Rafael Espindola9e32e4f2016-04-26 13:50:46 +0000931 ESym->setBindingAndType(STB_LOCAL, Body.Type);
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000932 Buf += sizeof(*ESym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000933 }
934 }
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000935}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000936
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000937template <class ELFT>
Igor Kudrinea6a8352015-10-19 08:01:51 +0000938void SymbolTableSection<ELFT>::writeGlobalSymbols(uint8_t *Buf) {
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000939 // Write the internal symbol table contents to the output symbol table
940 // pointed by Buf.
Igor Kudrinab665fc2015-10-20 21:47:58 +0000941 auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000942 for (const SymbolTableEntry &S : Symbols) {
943 SymbolBody *Body = S.Symbol;
944 size_t StrOff = S.StrTabOffset;
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000945
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000946 uint8_t Type = Body->Type;
947 uintX_t Size = Body->getSize<ELFT>();
Rafael Espindola8614c562015-10-06 14:33:58 +0000948
Igor Kudrin853b88d2015-10-20 20:52:14 +0000949 ESym->setBindingAndType(getSymbolBinding(Body), Type);
Rafael Espindola8614c562015-10-06 14:33:58 +0000950 ESym->st_size = Size;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000951 ESym->st_name = StrOff;
Peter Collingbourne4f952702016-05-01 04:55:03 +0000952 ESym->setVisibility(Body->symbol()->Visibility);
Rui Ueyamab5a69702016-02-01 21:00:35 +0000953 ESym->st_value = Body->getVA<ELFT>();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000954
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000955 if (const OutputSectionBase *OutSec = getOutputSection(Body))
Rui Ueyama2317d0d2015-10-15 20:55:22 +0000956 ESym->st_shndx = OutSec->SectionIndex;
Rafael Espindola02ce26a2015-12-24 14:22:24 +0000957 else if (isa<DefinedRegular<ELFT>>(Body))
958 ESym->st_shndx = SHN_ABS;
Simon Atanasyand040a582016-02-25 16:19:15 +0000959
Simon Atanasyanf967f092016-09-29 12:58:36 +0000960 if (Config->EMachine == EM_MIPS) {
961 // On MIPS we need to mark symbol which has a PLT entry and requires
962 // pointer equality by STO_MIPS_PLT flag. That is necessary to help
963 // dynamic linker distinguish such symbols and MIPS lazy-binding stubs.
964 // https://sourceware.org/ml/binutils/2008-07/txt00000.txt
965 if (Body->isInPlt() && Body->NeedsCopyOrPltAddr)
966 ESym->st_other |= STO_MIPS_PLT;
967 if (Config->Relocatable) {
968 auto *D = dyn_cast<DefinedRegular<ELFT>>(Body);
969 if (D && D->isMipsPIC())
970 ESym->st_other |= STO_MIPS_PIC;
971 }
972 }
Igor Kudrinab665fc2015-10-20 21:47:58 +0000973 ++ESym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000974 }
975}
976
Igor Kudrin853b88d2015-10-20 20:52:14 +0000977template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000978const OutputSectionBase *
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000979SymbolTableSection<ELFT>::getOutputSection(SymbolBody *Sym) {
980 switch (Sym->kind()) {
981 case SymbolBody::DefinedSyntheticKind:
Peter Collingbourne6a422592016-05-03 01:21:08 +0000982 return cast<DefinedSynthetic<ELFT>>(Sym)->Section;
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000983 case SymbolBody::DefinedRegularKind: {
Rafael Espindola5ffa13c2016-04-15 00:15:02 +0000984 auto &D = cast<DefinedRegular<ELFT>>(*Sym);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000985 if (D.Section)
986 return D.Section->OutSec;
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000987 break;
988 }
989 case SymbolBody::DefinedCommonKind:
Rui Ueyamae8a61022016-11-05 23:05:47 +0000990 return In<ELFT>::Common->OutSec;
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000991 case SymbolBody::SharedKind:
992 if (cast<SharedSymbol<ELFT>>(Sym)->needsCopy())
993 return Out<ELFT>::Bss;
994 break;
Peter Collingbourne60976ed2016-04-27 00:05:06 +0000995 case SymbolBody::UndefinedKind:
Rui Ueyamaf8baa662016-04-07 19:24:51 +0000996 case SymbolBody::LazyArchiveKind:
997 case SymbolBody::LazyObjectKind:
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000998 break;
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000999 }
1000 return nullptr;
1001}
1002
1003template <class ELFT>
George Rimard3566302016-06-20 11:55:12 +00001004VersionDefinitionSection<ELFT>::VersionDefinitionSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001005 : OutputSectionBase(".gnu.version_d", SHT_GNU_verdef, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001006 this->Addralign = sizeof(uint32_t);
Rui Ueyamaf5244642016-07-16 02:36:00 +00001007}
George Rimard3566302016-06-20 11:55:12 +00001008
1009static StringRef getFileDefName() {
1010 if (!Config->SoName.empty())
1011 return Config->SoName;
1012 return Config->OutputFile;
1013}
1014
1015template <class ELFT> void VersionDefinitionSection<ELFT>::finalize() {
Eugene Leviant22eb0262016-11-14 09:16:00 +00001016 FileDefNameOff = In<ELFT>::DynStrTab->addString(getFileDefName());
Rui Ueyamaaf469d42016-07-16 04:09:27 +00001017 for (VersionDefinition &V : Config->VersionDefinitions)
Eugene Leviant22eb0262016-11-14 09:16:00 +00001018 V.NameOff = In<ELFT>::DynStrTab->addString(V.Name);
George Rimard3566302016-06-20 11:55:12 +00001019
Rafael Espindola04a2e342016-11-09 01:42:41 +00001020 this->Size = (sizeof(Elf_Verdef) + sizeof(Elf_Verdaux)) * getVerDefNum();
Eugene Leviant22eb0262016-11-14 09:16:00 +00001021 this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
George Rimard3566302016-06-20 11:55:12 +00001022
1023 // sh_info should be set to the number of definitions. This fact is missed in
1024 // documentation, but confirmed by binutils community:
1025 // https://sourceware.org/ml/binutils/2014-11/msg00355.html
Rafael Espindola04a2e342016-11-09 01:42:41 +00001026 this->Info = getVerDefNum();
George Rimard3566302016-06-20 11:55:12 +00001027}
1028
Rui Ueyama9f619642016-07-16 02:29:45 +00001029template <class ELFT>
1030void VersionDefinitionSection<ELFT>::writeOne(uint8_t *Buf, uint32_t Index,
1031 StringRef Name, size_t NameOff) {
1032 auto *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
George Rimard3566302016-06-20 11:55:12 +00001033 Verdef->vd_version = 1;
George Rimar33b9de42016-07-01 11:45:10 +00001034 Verdef->vd_cnt = 1;
Rui Ueyama9f619642016-07-16 02:29:45 +00001035 Verdef->vd_aux = sizeof(Elf_Verdef);
1036 Verdef->vd_next = sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1037 Verdef->vd_flags = (Index == 1 ? VER_FLG_BASE : 0);
George Rimard3566302016-06-20 11:55:12 +00001038 Verdef->vd_ndx = Index;
Davide Italianoe7fd0be2016-11-07 21:56:56 +00001039 Verdef->vd_hash = hashSysV(Name);
George Rimard3566302016-06-20 11:55:12 +00001040
Rui Ueyama9f619642016-07-16 02:29:45 +00001041 auto *Verdaux = reinterpret_cast<Elf_Verdaux *>(Buf + sizeof(Elf_Verdef));
1042 Verdaux->vda_name = NameOff;
George Rimard3566302016-06-20 11:55:12 +00001043 Verdaux->vda_next = 0;
George Rimard3566302016-06-20 11:55:12 +00001044}
1045
1046template <class ELFT>
1047void VersionDefinitionSection<ELFT>::writeTo(uint8_t *Buf) {
Rui Ueyama9f619642016-07-16 02:29:45 +00001048 writeOne(Buf, 1, getFileDefName(), FileDefNameOff);
1049
Rui Ueyamaaf469d42016-07-16 04:09:27 +00001050 for (VersionDefinition &V : Config->VersionDefinitions) {
Rui Ueyama9f619642016-07-16 02:29:45 +00001051 Buf += sizeof(Elf_Verdef) + sizeof(Elf_Verdaux);
1052 writeOne(Buf, V.Id, V.Name, V.NameOff);
1053 }
1054
1055 // Need to terminate the last version definition.
George Rimard3566302016-06-20 11:55:12 +00001056 Elf_Verdef *Verdef = reinterpret_cast<Elf_Verdef *>(Buf);
Rui Ueyama9f619642016-07-16 02:29:45 +00001057 Verdef->vd_next = 0;
George Rimard3566302016-06-20 11:55:12 +00001058}
1059
1060template <class ELFT>
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001061VersionTableSection<ELFT>::VersionTableSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001062 : OutputSectionBase(".gnu.version", SHT_GNU_versym, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001063 this->Addralign = sizeof(uint16_t);
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001064}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001065
1066template <class ELFT> void VersionTableSection<ELFT>::finalize() {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001067 this->Size =
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001068 sizeof(Elf_Versym) * (Out<ELFT>::DynSymTab->getSymbols().size() + 1);
Rafael Espindola04a2e342016-11-09 01:42:41 +00001069 this->Entsize = sizeof(Elf_Versym);
George Rimar8b3c5f22016-06-06 08:04:53 +00001070 // At the moment of june 2016 GNU docs does not mention that sh_link field
1071 // should be set, but Sun docs do. Also readelf relies on this field.
Rafael Espindola04a2e342016-11-09 01:42:41 +00001072 this->Link = Out<ELFT>::DynSymTab->SectionIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001073}
1074
1075template <class ELFT> void VersionTableSection<ELFT>::writeTo(uint8_t *Buf) {
1076 auto *OutVersym = reinterpret_cast<Elf_Versym *>(Buf) + 1;
Michael J. Spencerf8a81482016-10-19 23:49:27 +00001077 for (const SymbolTableEntry &S : Out<ELFT>::DynSymTab->getSymbols()) {
1078 OutVersym->vs_index = S.Symbol->symbol()->VersionId;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001079 ++OutVersym;
1080 }
1081}
1082
1083template <class ELFT>
1084VersionNeedSection<ELFT>::VersionNeedSection()
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001085 : OutputSectionBase(".gnu.version_r", SHT_GNU_verneed, SHF_ALLOC) {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001086 this->Addralign = sizeof(uint32_t);
George Rimard3566302016-06-20 11:55:12 +00001087
1088 // Identifiers in verneed section start at 2 because 0 and 1 are reserved
1089 // for VER_NDX_LOCAL and VER_NDX_GLOBAL.
1090 // First identifiers are reserved by verdef section if it exist.
1091 NextIndex = getVerDefNum() + 1;
Rafael Espindolaeaaec4a2016-04-29 17:19:45 +00001092}
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001093
1094template <class ELFT>
1095void VersionNeedSection<ELFT>::addSymbol(SharedSymbol<ELFT> *SS) {
1096 if (!SS->Verdef) {
George Rimard3566302016-06-20 11:55:12 +00001097 SS->symbol()->VersionId = VER_NDX_GLOBAL;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001098 return;
1099 }
Rui Ueyama434b5612016-07-17 03:11:46 +00001100 SharedFile<ELFT> *F = SS->file();
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001101 // If we don't already know that we need an Elf_Verneed for this DSO, prepare
1102 // to create one by adding it to our needed list and creating a dynstr entry
1103 // for the soname.
1104 if (F->VerdefMap.empty())
Eugene Leviant22eb0262016-11-14 09:16:00 +00001105 Needed.push_back({F, In<ELFT>::DynStrTab->addString(F->getSoName())});
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001106 typename SharedFile<ELFT>::NeededVer &NV = F->VerdefMap[SS->Verdef];
1107 // If we don't already know that we need an Elf_Vernaux for this Elf_Verdef,
1108 // prepare to create one by allocating a version identifier and creating a
1109 // dynstr entry for the version name.
1110 if (NV.Index == 0) {
Eugene Leviant22eb0262016-11-14 09:16:00 +00001111 NV.StrTab = In<ELFT>::DynStrTab->addString(
Rui Ueyama434b5612016-07-17 03:11:46 +00001112 SS->file()->getStringTable().data() + SS->Verdef->getAux()->vda_name);
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001113 NV.Index = NextIndex++;
1114 }
George Rimard3566302016-06-20 11:55:12 +00001115 SS->symbol()->VersionId = NV.Index;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001116}
1117
1118template <class ELFT> void VersionNeedSection<ELFT>::writeTo(uint8_t *Buf) {
1119 // The Elf_Verneeds need to appear first, followed by the Elf_Vernauxs.
1120 auto *Verneed = reinterpret_cast<Elf_Verneed *>(Buf);
1121 auto *Vernaux = reinterpret_cast<Elf_Vernaux *>(Verneed + Needed.size());
1122
1123 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed) {
1124 // Create an Elf_Verneed for this DSO.
1125 Verneed->vn_version = 1;
1126 Verneed->vn_cnt = P.first->VerdefMap.size();
1127 Verneed->vn_file = P.second;
1128 Verneed->vn_aux =
1129 reinterpret_cast<char *>(Vernaux) - reinterpret_cast<char *>(Verneed);
1130 Verneed->vn_next = sizeof(Elf_Verneed);
1131 ++Verneed;
1132
1133 // Create the Elf_Vernauxs for this Elf_Verneed. The loop iterates over
1134 // VerdefMap, which will only contain references to needed version
1135 // definitions. Each Elf_Vernaux is based on the information contained in
1136 // the Elf_Verdef in the source DSO. This loop iterates over a std::map of
1137 // pointers, but is deterministic because the pointers refer to Elf_Verdef
1138 // data structures within a single input file.
1139 for (auto &NV : P.first->VerdefMap) {
1140 Vernaux->vna_hash = NV.first->vd_hash;
1141 Vernaux->vna_flags = 0;
1142 Vernaux->vna_other = NV.second.Index;
1143 Vernaux->vna_name = NV.second.StrTab;
1144 Vernaux->vna_next = sizeof(Elf_Vernaux);
1145 ++Vernaux;
1146 }
1147
1148 Vernaux[-1].vna_next = 0;
1149 }
1150 Verneed[-1].vn_next = 0;
1151}
1152
1153template <class ELFT> void VersionNeedSection<ELFT>::finalize() {
Eugene Leviant22eb0262016-11-14 09:16:00 +00001154 this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
Rafael Espindola04a2e342016-11-09 01:42:41 +00001155 this->Info = Needed.size();
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001156 unsigned Size = Needed.size() * sizeof(Elf_Verneed);
1157 for (std::pair<SharedFile<ELFT> *, size_t> &P : Needed)
1158 Size += P.first->VerdefMap.size() * sizeof(Elf_Vernaux);
Rafael Espindola04a2e342016-11-09 01:42:41 +00001159 this->Size = Size;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001160}
1161
1162template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +00001163static typename ELFT::uint getOutFlags(InputSectionBase<ELFT> *S) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +00001164 return S->Flags & ~SHF_GROUP & ~SHF_COMPRESSED;
Rafael Espindola10897f12016-09-13 14:23:14 +00001165}
1166
1167template <class ELFT>
Rafael Espindola17c35832016-09-13 12:25:30 +00001168static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
1169 StringRef OutsecName) {
Rafael Espindola17c35832016-09-13 12:25:30 +00001170 typedef typename ELFT::uint uintX_t;
Rafael Espindola10897f12016-09-13 14:23:14 +00001171 uintX_t Flags = getOutFlags(C);
Rafael Espindola17c35832016-09-13 12:25:30 +00001172
1173 // For SHF_MERGE we create different output sections for each alignment.
1174 // This makes each output section simple and keeps a single level mapping from
1175 // input to output.
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +00001176 // In case of relocatable object generation we do not try to perform merging
1177 // and treat SHF_MERGE sections as regular ones, but also create different
1178 // output sections for them to allow merging at final linking stage.
Rafael Espindola17c35832016-09-13 12:25:30 +00001179 uintX_t Alignment = 0;
Simon Atanasyan02b9c3f2016-10-05 07:49:18 +00001180 if (isa<MergeInputSection<ELFT>>(C) ||
Rafael Espindola1854a8e2016-10-26 12:36:56 +00001181 (Config->Relocatable && (C->Flags & SHF_MERGE)))
1182 Alignment = std::max<uintX_t>(C->Alignment, C->Entsize);
Rafael Espindola17c35832016-09-13 12:25:30 +00001183
Rafael Espindola1854a8e2016-10-26 12:36:56 +00001184 return SectionKey<ELFT::Is64Bits>{OutsecName, C->Type, Flags, Alignment};
Rafael Espindola17c35832016-09-13 12:25:30 +00001185}
1186
1187template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001188std::pair<OutputSectionBase *, bool>
George Rimar6892afa2016-07-12 09:49:43 +00001189OutputSectionFactory<ELFT>::create(InputSectionBase<ELFT> *C,
1190 StringRef OutsecName) {
1191 SectionKey<ELFT::Is64Bits> Key = createKey(C, OutsecName);
Rafael Espindola10897f12016-09-13 14:23:14 +00001192 return create(Key, C);
1193}
George Rimar6892afa2016-07-12 09:49:43 +00001194
Rafael Espindola10897f12016-09-13 14:23:14 +00001195template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001196std::pair<OutputSectionBase *, bool>
Rafael Espindola10897f12016-09-13 14:23:14 +00001197OutputSectionFactory<ELFT>::create(const SectionKey<ELFT::Is64Bits> &Key,
1198 InputSectionBase<ELFT> *C) {
1199 uintX_t Flags = getOutFlags(C);
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001200 OutputSectionBase *&Sec = Map[Key];
Rafael Espindola10897f12016-09-13 14:23:14 +00001201 if (Sec) {
Rafael Espindola04a2e342016-11-09 01:42:41 +00001202 Sec->Flags |= Flags;
Rafael Espindola10897f12016-09-13 14:23:14 +00001203 return {Sec, false};
1204 }
1205
Rafael Espindola1854a8e2016-10-26 12:36:56 +00001206 uint32_t Type = C->Type;
Rafael Espindola16853bb2016-09-08 12:33:41 +00001207 switch (C->kind()) {
George Rimar6892afa2016-07-12 09:49:43 +00001208 case InputSectionBase<ELFT>::Regular:
Eugene Leviant41ca3272016-11-10 09:48:29 +00001209 case InputSectionBase<ELFT>::Synthetic:
Rui Ueyama95642b92016-11-01 23:09:07 +00001210 Sec = make<OutputSection<ELFT>>(Key.Name, Type, Flags);
George Rimar6892afa2016-07-12 09:49:43 +00001211 break;
1212 case InputSectionBase<ELFT>::EHFrame:
1213 return {Out<ELFT>::EhFrame, false};
1214 case InputSectionBase<ELFT>::Merge:
Rui Ueyama95642b92016-11-01 23:09:07 +00001215 Sec = make<MergeOutputSection<ELFT>>(Key.Name, Type, Flags, Key.Alignment);
George Rimar6892afa2016-07-12 09:49:43 +00001216 break;
George Rimar6892afa2016-07-12 09:49:43 +00001217 }
1218 return {Sec, true};
1219}
1220
George Rimar6892afa2016-07-12 09:49:43 +00001221template <bool Is64Bits>
1222typename lld::elf::SectionKey<Is64Bits>
1223DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getEmptyKey() {
1224 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0, 0};
1225}
1226
1227template <bool Is64Bits>
1228typename lld::elf::SectionKey<Is64Bits>
1229DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getTombstoneKey() {
1230 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 0,
1231 0};
1232}
1233
1234template <bool Is64Bits>
1235unsigned
1236DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::getHashValue(const Key &Val) {
1237 return hash_combine(Val.Name, Val.Type, Val.Flags, Val.Alignment);
1238}
1239
1240template <bool Is64Bits>
1241bool DenseMapInfo<lld::elf::SectionKey<Is64Bits>>::isEqual(const Key &LHS,
1242 const Key &RHS) {
1243 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
1244 LHS.Type == RHS.Type && LHS.Flags == RHS.Flags &&
1245 LHS.Alignment == RHS.Alignment;
1246}
1247
1248namespace llvm {
1249template struct DenseMapInfo<SectionKey<true>>;
1250template struct DenseMapInfo<SectionKey<false>>;
1251}
1252
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001253namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +00001254namespace elf {
Rafael Espindolae08e78d2016-11-09 23:23:45 +00001255
1256template void OutputSectionBase::writeHeaderTo<ELF32LE>(ELF32LE::Shdr *Shdr);
1257template void OutputSectionBase::writeHeaderTo<ELF32BE>(ELF32BE::Shdr *Shdr);
1258template void OutputSectionBase::writeHeaderTo<ELF64LE>(ELF64LE::Shdr *Shdr);
1259template void OutputSectionBase::writeHeaderTo<ELF64BE>(ELF64BE::Shdr *Shdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001260
George Rimarf6bc65a2016-01-15 13:34:52 +00001261template class EhFrameHeader<ELF32LE>;
1262template class EhFrameHeader<ELF32BE>;
1263template class EhFrameHeader<ELF64LE>;
1264template class EhFrameHeader<ELF64BE>;
1265
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001266template class PltSection<ELF32LE>;
1267template class PltSection<ELF32BE>;
1268template class PltSection<ELF64LE>;
1269template class PltSection<ELF64BE>;
1270
Igor Kudrin1b0d7062015-10-22 08:21:35 +00001271template class GnuHashTableSection<ELF32LE>;
1272template class GnuHashTableSection<ELF32BE>;
1273template class GnuHashTableSection<ELF64LE>;
1274template class GnuHashTableSection<ELF64BE>;
1275
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001276template class HashTableSection<ELF32LE>;
1277template class HashTableSection<ELF32BE>;
1278template class HashTableSection<ELF64LE>;
1279template class HashTableSection<ELF64BE>;
1280
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001281template class OutputSection<ELF32LE>;
1282template class OutputSection<ELF32BE>;
1283template class OutputSection<ELF64LE>;
1284template class OutputSection<ELF64BE>;
1285
Rui Ueyama1e479c22016-05-23 15:07:59 +00001286template class EhOutputSection<ELF32LE>;
1287template class EhOutputSection<ELF32BE>;
1288template class EhOutputSection<ELF64LE>;
1289template class EhOutputSection<ELF64BE>;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +00001290
Rafael Espindolac159c962015-10-19 21:00:02 +00001291template class MergeOutputSection<ELF32LE>;
1292template class MergeOutputSection<ELF32BE>;
1293template class MergeOutputSection<ELF64LE>;
1294template class MergeOutputSection<ELF64BE>;
1295
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001296template class SymbolTableSection<ELF32LE>;
1297template class SymbolTableSection<ELF32BE>;
1298template class SymbolTableSection<ELF64LE>;
1299template class SymbolTableSection<ELF64BE>;
Rui Ueyama634ddf02016-03-11 20:51:53 +00001300
Peter Collingbourne21a12fc2016-04-27 20:22:31 +00001301template class VersionTableSection<ELF32LE>;
1302template class VersionTableSection<ELF32BE>;
1303template class VersionTableSection<ELF64LE>;
1304template class VersionTableSection<ELF64BE>;
1305
1306template class VersionNeedSection<ELF32LE>;
1307template class VersionNeedSection<ELF32BE>;
1308template class VersionNeedSection<ELF64LE>;
1309template class VersionNeedSection<ELF64BE>;
1310
George Rimard3566302016-06-20 11:55:12 +00001311template class VersionDefinitionSection<ELF32LE>;
1312template class VersionDefinitionSection<ELF32BE>;
1313template class VersionDefinitionSection<ELF64LE>;
1314template class VersionDefinitionSection<ELF64BE>;
1315
George Rimar58fa5242016-10-20 09:19:48 +00001316template class GdbIndexSection<ELF32LE>;
1317template class GdbIndexSection<ELF32BE>;
1318template class GdbIndexSection<ELF64LE>;
1319template class GdbIndexSection<ELF64BE>;
1320
George Rimar6892afa2016-07-12 09:49:43 +00001321template class OutputSectionFactory<ELF32LE>;
1322template class OutputSectionFactory<ELF32BE>;
1323template class OutputSectionFactory<ELF64LE>;
1324template class OutputSectionFactory<ELF64BE>;
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001325}
1326}