1 | //===- InputSection.cpp ---------------------------------------------------===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "InputSection.h" |
10 | #include "Config.h" |
11 | #include "InputFiles.h" |
12 | #include "OutputSections.h" |
13 | #include "Relocations.h" |
14 | #include "SymbolTable.h" |
15 | #include "Symbols.h" |
16 | #include "SyntheticSections.h" |
17 | #include "Target.h" |
18 | #include "lld/Common/DWARF.h" |
19 | #include "llvm/Support/Compiler.h" |
20 | #include "llvm/Support/Compression.h" |
21 | #include "llvm/Support/Endian.h" |
22 | #include "llvm/Support/xxhash.h" |
23 | #include <algorithm> |
24 | #include <optional> |
25 | #include <vector> |
26 | |
27 | using namespace llvm; |
28 | using namespace llvm::ELF; |
29 | using namespace llvm::object; |
30 | using namespace llvm::support; |
31 | using namespace llvm::support::endian; |
32 | using namespace llvm::sys; |
33 | using namespace lld; |
34 | using namespace lld::elf; |
35 | |
36 | // Returns a string to construct an error message. |
37 | std::string elf::toStr(Ctx &ctx, const InputSectionBase *sec) { |
38 | return (toStr(ctx, f: sec->file) + ":("+ sec->name + ")").str(); |
39 | } |
40 | |
41 | const ELFSyncStream &elf::operator<<(const ELFSyncStream &s, |
42 | const InputSectionBase *sec) { |
43 | return s << toStr(ctx&: s.ctx, sec); |
44 | } |
45 | |
46 | template <class ELFT> |
47 | static ArrayRef<uint8_t> getSectionContents(ObjFile<ELFT> &file, |
48 | const typename ELFT::Shdr &hdr) { |
49 | if (hdr.sh_type == SHT_NOBITS) |
50 | return ArrayRef<uint8_t>(nullptr, hdr.sh_size); |
51 | return check(file.getObj().getSectionContents(hdr)); |
52 | } |
53 | |
54 | InputSectionBase::InputSectionBase(InputFile *file, StringRef name, |
55 | uint32_t type, uint64_t flags, uint32_t link, |
56 | uint32_t info, uint32_t addralign, |
57 | uint32_t entsize, ArrayRef<uint8_t> data, |
58 | Kind sectionKind) |
59 | : SectionBase(sectionKind, file, name, type, flags, link, info, addralign, |
60 | entsize), |
61 | bss(0), decodedCrel(0), keepUnique(0), nopFiller(0), |
62 | content_(data.data()), size(data.size()) { |
63 | // In order to reduce memory allocation, we assume that mergeable |
64 | // sections are smaller than 4 GiB, which is not an unreasonable |
65 | // assumption as of 2017. |
66 | if (sectionKind == SectionBase::Merge && content().size() > UINT32_MAX) |
67 | ErrAlways(ctx&: getCtx()) << this << ": section too large"; |
68 | |
69 | // The ELF spec states that a value of 0 means the section has |
70 | // no alignment constraints. |
71 | uint32_t v = std::max<uint32_t>(a: addralign, b: 1); |
72 | if (!isPowerOf2_64(Value: v)) { |
73 | Err(ctx&: getCtx()) << this << ": sh_addralign is not a power of 2"; |
74 | v = 1; |
75 | } |
76 | this->addralign = v; |
77 | |
78 | // If SHF_COMPRESSED is set, parse the header. The legacy .zdebug format is no |
79 | // longer supported. |
80 | if (flags & SHF_COMPRESSED) { |
81 | Ctx &ctx = file->ctx; |
82 | invokeELFT(parseCompressedHeader, ctx); |
83 | } |
84 | } |
85 | |
86 | // SHF_INFO_LINK and SHF_GROUP are normally resolved and not copied to the |
87 | // output section. However, for relocatable linking without |
88 | // --force-group-allocation, the SHF_GROUP flag and section groups are retained. |
89 | static uint64_t getFlags(Ctx &ctx, uint64_t flags) { |
90 | flags &= ~(uint64_t)SHF_INFO_LINK; |
91 | if (ctx.arg.resolveGroups) |
92 | flags &= ~(uint64_t)SHF_GROUP; |
93 | return flags; |
94 | } |
95 | |
96 | template <class ELFT> |
97 | InputSectionBase::InputSectionBase(ObjFile<ELFT> &file, |
98 | const typename ELFT::Shdr &hdr, |
99 | StringRef name, Kind sectionKind) |
100 | : InputSectionBase(&file, name, hdr.sh_type, |
101 | getFlags(file.ctx, hdr.sh_flags), hdr.sh_link, |
102 | hdr.sh_info, hdr.sh_addralign, hdr.sh_entsize, |
103 | getSectionContents(file, hdr), sectionKind) { |
104 | // We reject object files having insanely large alignments even though |
105 | // they are allowed by the spec. I think 4GB is a reasonable limitation. |
106 | // We might want to relax this in the future. |
107 | if (hdr.sh_addralign > UINT32_MAX) { |
108 | Err(ctx&: getCtx()) << &file << ": section sh_addralign is too large"; |
109 | addralign = 1; |
110 | } |
111 | } |
112 | |
113 | size_t InputSectionBase::getSize() const { |
114 | if (auto *s = dyn_cast<SyntheticSection>(Val: this)) |
115 | return s->getSize(); |
116 | return size - bytesDropped; |
117 | } |
118 | |
119 | template <class ELFT> |
120 | static void decompressAux(Ctx &ctx, const InputSectionBase &sec, uint8_t *out, |
121 | size_t size) { |
122 | auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(sec.content_); |
123 | auto compressed = ArrayRef<uint8_t>(sec.content_, sec.compressedSize) |
124 | .slice(N: sizeof(typename ELFT::Chdr)); |
125 | if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB |
126 | ? compression::zlib::decompress(Input: compressed, Output: out, UncompressedSize&: size) |
127 | : compression::zstd::decompress(Input: compressed, Output: out, UncompressedSize&: size)) |
128 | Err(ctx) << &sec << ": decompress failed: "<< std::move(e); |
129 | } |
130 | |
131 | void InputSectionBase::decompress() const { |
132 | Ctx &ctx = getCtx(); |
133 | uint8_t *buf = makeThreadLocalN<uint8_t>(n: size); |
134 | invokeELFT(decompressAux, ctx, *this, buf, size); |
135 | content_ = buf; |
136 | compressed = false; |
137 | } |
138 | |
139 | template <class ELFT> |
140 | RelsOrRelas<ELFT> InputSectionBase::relsOrRelas(bool supportsCrel) const { |
141 | if (relSecIdx == 0) |
142 | return {}; |
143 | RelsOrRelas<ELFT> ret; |
144 | auto *f = cast<ObjFile<ELFT>>(file); |
145 | typename ELFT::Shdr shdr = f->template getELFShdrs<ELFT>()[relSecIdx]; |
146 | if (shdr.sh_type == SHT_CREL) { |
147 | // Return an iterator if supported by caller. |
148 | if (supportsCrel) { |
149 | ret.crels = Relocs<typename ELFT::Crel>( |
150 | (const uint8_t *)f->mb.getBufferStart() + shdr.sh_offset); |
151 | return ret; |
152 | } |
153 | InputSectionBase *const &relSec = f->getSections()[relSecIdx]; |
154 | // Otherwise, allocate a buffer to hold the decoded RELA relocations. When |
155 | // called for the first time, relSec is null (without --emit-relocs) or an |
156 | // InputSection with false decodedCrel. |
157 | if (!relSec || !cast<InputSection>(Val: relSec)->decodedCrel) { |
158 | auto *sec = makeThreadLocal<InputSection>(*f, shdr, name); |
159 | f->cacheDecodedCrel(relSecIdx, sec); |
160 | sec->type = SHT_RELA; |
161 | sec->decodedCrel = true; |
162 | |
163 | RelocsCrel<ELFT::Is64Bits> entries(sec->content_); |
164 | sec->size = entries.size() * sizeof(typename ELFT::Rela); |
165 | auto *relas = makeThreadLocalN<typename ELFT::Rela>(entries.size()); |
166 | sec->content_ = reinterpret_cast<uint8_t *>(relas); |
167 | for (auto [i, r] : llvm::enumerate(entries)) { |
168 | relas[i].r_offset = r.r_offset; |
169 | relas[i].setSymbolAndType(r.r_symidx, r.r_type, false); |
170 | relas[i].r_addend = r.r_addend; |
171 | } |
172 | } |
173 | ret.relas = {ArrayRef( |
174 | reinterpret_cast<const typename ELFT::Rela *>(relSec->content_), |
175 | relSec->size / sizeof(typename ELFT::Rela))}; |
176 | return ret; |
177 | } |
178 | |
179 | const void *content = f->mb.getBufferStart() + shdr.sh_offset; |
180 | size_t size = shdr.sh_size; |
181 | if (shdr.sh_type == SHT_REL) { |
182 | ret.rels = {ArrayRef(reinterpret_cast<const typename ELFT::Rel *>(content), |
183 | size / sizeof(typename ELFT::Rel))}; |
184 | } else { |
185 | assert(shdr.sh_type == SHT_RELA); |
186 | ret.relas = { |
187 | ArrayRef(reinterpret_cast<const typename ELFT::Rela *>(content), |
188 | size / sizeof(typename ELFT::Rela))}; |
189 | } |
190 | return ret; |
191 | } |
192 | |
193 | Ctx &SectionBase::getCtx() const { return file->ctx; } |
194 | |
195 | uint64_t SectionBase::getOffset(uint64_t offset) const { |
196 | switch (kind()) { |
197 | case Output: { |
198 | auto *os = cast<OutputSection>(Val: this); |
199 | // For output sections we treat offset -1 as the end of the section. |
200 | return offset == uint64_t(-1) ? os->size : offset; |
201 | } |
202 | case Class: |
203 | llvm_unreachable("section classes do not have offsets"); |
204 | case Regular: |
205 | case Synthetic: |
206 | case Spill: |
207 | return cast<InputSection>(Val: this)->outSecOff + offset; |
208 | case EHFrame: { |
209 | // Two code paths may reach here. First, clang_rt.crtbegin.o and GCC |
210 | // crtbeginT.o may reference the start of an empty .eh_frame to identify the |
211 | // start of the output .eh_frame. Just return offset. |
212 | // |
213 | // Second, InputSection::copyRelocations on .eh_frame. Some pieces may be |
214 | // discarded due to GC/ICF. We should compute the output section offset. |
215 | const EhInputSection *es = cast<EhInputSection>(Val: this); |
216 | if (!es->content().empty()) |
217 | if (InputSection *isec = es->getParent()) |
218 | return isec->outSecOff + es->getParentOffset(offset); |
219 | return offset; |
220 | } |
221 | case Merge: |
222 | const MergeInputSection *ms = cast<MergeInputSection>(Val: this); |
223 | if (InputSection *isec = ms->getParent()) |
224 | return isec->outSecOff + ms->getParentOffset(offset); |
225 | return ms->getParentOffset(offset); |
226 | } |
227 | llvm_unreachable("invalid section kind"); |
228 | } |
229 | |
230 | uint64_t SectionBase::getVA(uint64_t offset) const { |
231 | const OutputSection *out = getOutputSection(); |
232 | return (out ? out->addr : 0) + getOffset(offset); |
233 | } |
234 | |
235 | OutputSection *SectionBase::getOutputSection() { |
236 | InputSection *sec; |
237 | if (auto *isec = dyn_cast<InputSection>(Val: this)) |
238 | sec = isec; |
239 | else if (auto *ms = dyn_cast<MergeInputSection>(Val: this)) |
240 | sec = ms->getParent(); |
241 | else if (auto *eh = dyn_cast<EhInputSection>(Val: this)) |
242 | sec = eh->getParent(); |
243 | else |
244 | return cast<OutputSection>(Val: this); |
245 | return sec ? sec->getParent() : nullptr; |
246 | } |
247 | |
248 | // When a section is compressed, `rawData` consists with a header followed |
249 | // by zlib-compressed data. This function parses a header to initialize |
250 | // `uncompressedSize` member and remove the header from `rawData`. |
251 | template <typename ELFT> |
252 | void InputSectionBase::parseCompressedHeader(Ctx &ctx) { |
253 | flags &= ~(uint64_t)SHF_COMPRESSED; |
254 | |
255 | // New-style header |
256 | if (content().size() < sizeof(typename ELFT::Chdr)) { |
257 | ErrAlways(ctx) << this << ": corrupted compressed section"; |
258 | return; |
259 | } |
260 | |
261 | auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content().data()); |
262 | if (hdr->ch_type == ELFCOMPRESS_ZLIB) { |
263 | if (!compression::zlib::isAvailable()) |
264 | ErrAlways(ctx) << this |
265 | << " is compressed with ELFCOMPRESS_ZLIB, but lld is " |
266 | "not built with zlib support"; |
267 | } else if (hdr->ch_type == ELFCOMPRESS_ZSTD) { |
268 | if (!compression::zstd::isAvailable()) |
269 | ErrAlways(ctx) << this |
270 | << " is compressed with ELFCOMPRESS_ZSTD, but lld is " |
271 | "not built with zstd support"; |
272 | } else { |
273 | ErrAlways(ctx) << this << ": unsupported compression type (" |
274 | << uint32_t(hdr->ch_type) << ")"; |
275 | return; |
276 | } |
277 | |
278 | compressed = true; |
279 | compressedSize = size; |
280 | size = hdr->ch_size; |
281 | addralign = std::max<uint32_t>(hdr->ch_addralign, 1); |
282 | } |
283 | |
284 | InputSection *InputSectionBase::getLinkOrderDep() const { |
285 | assert(flags & SHF_LINK_ORDER); |
286 | if (!link) |
287 | return nullptr; |
288 | return cast<InputSection>(Val: file->getSections()[link]); |
289 | } |
290 | |
291 | // Find a symbol that encloses a given location. |
292 | Defined *InputSectionBase::getEnclosingSymbol(uint64_t offset, |
293 | uint8_t type) const { |
294 | if (file->isInternal()) |
295 | return nullptr; |
296 | for (Symbol *b : file->getSymbols()) |
297 | if (Defined *d = dyn_cast<Defined>(Val: b)) |
298 | if (d->section == this && d->value <= offset && |
299 | offset < d->value + d->size && (type == 0 || type == d->type)) |
300 | return d; |
301 | return nullptr; |
302 | } |
303 | |
304 | // Returns an object file location string. Used to construct an error message. |
305 | std::string InputSectionBase::getLocation(uint64_t offset) const { |
306 | std::string secAndOffset = |
307 | (name + "+0x"+ Twine::utohexstr(Val: offset) + ")").str(); |
308 | |
309 | std::string filename = toStr(getCtx(), f: file); |
310 | if (Defined *d = getEnclosingFunction(offset)) |
311 | return filename + ":(function "+ toStr(getCtx(), *d) + ": "+ secAndOffset; |
312 | |
313 | return filename + ":("+ secAndOffset; |
314 | } |
315 | |
316 | static void printFileLine(const ELFSyncStream &s, StringRef path, |
317 | unsigned line) { |
318 | StringRef filename = path::filename(path); |
319 | s << filename << ':' << line; |
320 | if (filename != path) |
321 | s << " ("<< path << ':' << line << ')'; |
322 | } |
323 | |
324 | // Print an error message that looks like this: |
325 | // |
326 | // foo.c:42 (/home/alice/possibly/very/long/path/foo.c:42) |
327 | const ELFSyncStream &elf::operator<<(const ELFSyncStream &s, |
328 | InputSectionBase::SrcMsg &&msg) { |
329 | auto &sec = msg.sec; |
330 | if (sec.file->kind() != InputFile::ObjKind) |
331 | return s; |
332 | auto &file = cast<ELFFileBase>(Val&: *sec.file); |
333 | |
334 | // First, look up the DWARF line table. |
335 | ArrayRef<InputSectionBase *> sections = file.getSections(); |
336 | auto it = llvm::find(Range&: sections, Val: &sec); |
337 | uint64_t sectionIndex = it != sections.end() |
338 | ? it - sections.begin() |
339 | : object::SectionedAddress::UndefSection; |
340 | DWARFCache *dwarf = file.getDwarf(); |
341 | if (auto info = dwarf->getDILineInfo(offset: msg.offset, sectionIndex)) |
342 | printFileLine(s, path: info->FileName, line: info->Line); |
343 | else if (auto fileLine = dwarf->getVariableLoc(name: msg.sym.getName())) |
344 | // If it failed, look up again as a variable. |
345 | printFileLine(s, path: fileLine->first, line: fileLine->second); |
346 | else |
347 | // File.sourceFile contains STT_FILE symbol, and that is a last resort. |
348 | s << file.sourceFile; |
349 | return s; |
350 | } |
351 | |
352 | // Returns a filename string along with an optional section name. This |
353 | // function is intended to be used for constructing an error |
354 | // message. The returned message looks like this: |
355 | // |
356 | // path/to/foo.o:(function bar) |
357 | // |
358 | // or |
359 | // |
360 | // path/to/foo.o:(function bar) in archive path/to/bar.a |
361 | const ELFSyncStream &elf::operator<<(const ELFSyncStream &s, |
362 | InputSectionBase::ObjMsg &&msg) { |
363 | auto *sec = msg.sec; |
364 | s << sec->file->getName() << ":("; |
365 | |
366 | // Find a symbol that encloses a given location. getObjMsg may be called |
367 | // before ObjFile::initSectionsAndLocalSyms where local symbols are |
368 | // initialized. |
369 | if (Defined *d = sec->getEnclosingSymbol(offset: msg.offset)) |
370 | s << d; |
371 | else |
372 | s << sec->name << "+0x"<< Twine::utohexstr(Val: msg.offset); |
373 | s << ')'; |
374 | if (!sec->file->archiveName.empty()) |
375 | s << (" in archive "+ sec->file->archiveName).str(); |
376 | return s; |
377 | } |
378 | |
379 | PotentialSpillSection::PotentialSpillSection(const InputSectionBase &source, |
380 | InputSectionDescription &isd) |
381 | : InputSection(source.file, source.name, source.type, source.flags, |
382 | source.addralign, source.addralign, {}, SectionBase::Spill), |
383 | isd(&isd) {} |
384 | |
385 | InputSection InputSection::discarded(nullptr, "", 0, 0, 0, 0, |
386 | ArrayRef<uint8_t>()); |
387 | |
388 | InputSection::InputSection(InputFile *f, StringRef name, uint32_t type, |
389 | uint64_t flags, uint32_t addralign, uint32_t entsize, |
390 | ArrayRef<uint8_t> data, Kind k) |
391 | : InputSectionBase(f, name, type, flags, |
392 | /*link=*/0, /*info=*/0, addralign, /*entsize=*/entsize, |
393 | data, k) { |
394 | assert(f || this == &InputSection::discarded); |
395 | } |
396 | |
397 | template <class ELFT> |
398 | InputSection::InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header, |
399 | StringRef name) |
400 | : InputSectionBase(f, header, name, InputSectionBase::Regular) {} |
401 | |
402 | // Copy SHT_GROUP section contents. Used only for the -r option. |
403 | template <class ELFT> void InputSection::copyShtGroup(uint8_t *buf) { |
404 | // ELFT::Word is the 32-bit integral type in the target endianness. |
405 | using u32 = typename ELFT::Word; |
406 | ArrayRef<u32> from = getDataAs<u32>(); |
407 | auto *to = reinterpret_cast<u32 *>(buf); |
408 | |
409 | // The first entry is not a section number but a flag. |
410 | *to++ = from[0]; |
411 | |
412 | // Adjust section numbers because section numbers in an input object files are |
413 | // different in the output. We also need to handle combined or discarded |
414 | // members. |
415 | ArrayRef<InputSectionBase *> sections = file->getSections(); |
416 | DenseSet<uint32_t> seen; |
417 | for (uint32_t idx : from.slice(1)) { |
418 | OutputSection *osec = sections[idx]->getOutputSection(); |
419 | if (osec && seen.insert(V: osec->sectionIndex).second) |
420 | *to++ = osec->sectionIndex; |
421 | } |
422 | } |
423 | |
424 | InputSectionBase *InputSection::getRelocatedSection() const { |
425 | if (file->isInternal() || !isStaticRelSecType(type)) |
426 | return nullptr; |
427 | ArrayRef<InputSectionBase *> sections = file->getSections(); |
428 | return sections[info]; |
429 | } |
430 | |
431 | template <class ELFT, class RelTy> |
432 | void InputSection::copyRelocations(Ctx &ctx, uint8_t *buf) { |
433 | if (ctx.arg.relax && !ctx.arg.relocatable && |
434 | (ctx.arg.emachine == EM_RISCV || ctx.arg.emachine == EM_LOONGARCH)) { |
435 | // On LoongArch and RISC-V, relaxation might change relocations: copy |
436 | // from internal ones that are updated by relaxation. |
437 | InputSectionBase *sec = getRelocatedSection(); |
438 | copyRelocations<ELFT, RelTy>( |
439 | ctx, buf, |
440 | llvm::make_range(x: sec->relocations.begin(), y: sec->relocations.end())); |
441 | } else { |
442 | // Convert the raw relocations in the input section into Relocation objects |
443 | // suitable to be used by copyRelocations below. |
444 | struct MapRel { |
445 | Ctx &ctx; |
446 | const ObjFile<ELFT> &file; |
447 | Relocation operator()(const RelTy &rel) const { |
448 | // RelExpr is not used so set to a dummy value. |
449 | return Relocation{R_NONE, rel.getType(ctx.arg.isMips64EL), rel.r_offset, |
450 | getAddend<ELFT>(rel), &file.getRelocTargetSym(rel)}; |
451 | } |
452 | }; |
453 | |
454 | using RawRels = ArrayRef<RelTy>; |
455 | using MapRelIter = |
456 | llvm::mapped_iterator<typename RawRels::iterator, MapRel>; |
457 | auto mapRel = MapRel{ctx, *getFile<ELFT>()}; |
458 | RawRels rawRels = getDataAs<RelTy>(); |
459 | auto rels = llvm::make_range(MapRelIter(rawRels.begin(), mapRel), |
460 | MapRelIter(rawRels.end(), mapRel)); |
461 | copyRelocations<ELFT, RelTy>(ctx, buf, rels); |
462 | } |
463 | } |
464 | |
465 | // This is used for -r and --emit-relocs. We can't use memcpy to copy |
466 | // relocations because we need to update symbol table offset and section index |
467 | // for each relocation. So we copy relocations one by one. |
468 | template <class ELFT, class RelTy, class RelIt> |
469 | void InputSection::copyRelocations(Ctx &ctx, uint8_t *buf, |
470 | llvm::iterator_range<RelIt> rels) { |
471 | const TargetInfo &target = *ctx.target; |
472 | InputSectionBase *sec = getRelocatedSection(); |
473 | (void)sec->contentMaybeDecompress(); // uncompress if needed |
474 | |
475 | for (const Relocation &rel : rels) { |
476 | RelType type = rel.type; |
477 | const ObjFile<ELFT> *file = getFile<ELFT>(); |
478 | Symbol &sym = *rel.sym; |
479 | |
480 | auto *p = reinterpret_cast<typename ELFT::Rela *>(buf); |
481 | buf += sizeof(RelTy); |
482 | |
483 | if (RelTy::HasAddend) |
484 | p->r_addend = rel.addend; |
485 | |
486 | // Output section VA is zero for -r, so r_offset is an offset within the |
487 | // section, but for --emit-relocs it is a virtual address. |
488 | p->r_offset = sec->getVA(offset: rel.offset); |
489 | p->setSymbolAndType(ctx.in.symTab->getSymbolIndex(sym), type, |
490 | ctx.arg.isMips64EL); |
491 | |
492 | if (sym.type == STT_SECTION) { |
493 | // We combine multiple section symbols into only one per |
494 | // section. This means we have to update the addend. That is |
495 | // trivial for Elf_Rela, but for Elf_Rel we have to write to the |
496 | // section data. We do that by adding to the Relocation vector. |
497 | |
498 | // .eh_frame is horribly special and can reference discarded sections. To |
499 | // avoid having to parse and recreate .eh_frame, we just replace any |
500 | // relocation in it pointing to discarded sections with R_*_NONE, which |
501 | // hopefully creates a frame that is ignored at runtime. Also, don't warn |
502 | // on .gcc_except_table and debug sections. |
503 | // |
504 | // See the comment in maybeReportUndefined for PPC32 .got2 and PPC64 .toc |
505 | auto *d = dyn_cast<Defined>(Val: &sym); |
506 | if (!d) { |
507 | if (!isDebugSection(sec: *sec) && sec->name != ".eh_frame"&& |
508 | sec->name != ".gcc_except_table"&& sec->name != ".got2"&& |
509 | sec->name != ".toc") { |
510 | uint32_t secIdx = cast<Undefined>(Val&: sym).discardedSecIdx; |
511 | Elf_Shdr_Impl<ELFT> sec = file->template getELFShdrs<ELFT>()[secIdx]; |
512 | Warn(ctx) << "relocation refers to a discarded section: " |
513 | << CHECK2(file->getObj().getSectionName(sec), file) |
514 | << "\n>>> referenced by "<< getObjMsg(offset: p->r_offset); |
515 | } |
516 | p->setSymbolAndType(0, 0, false); |
517 | continue; |
518 | } |
519 | SectionBase *section = d->section; |
520 | assert(section->isLive()); |
521 | |
522 | int64_t addend = rel.addend; |
523 | const uint8_t *bufLoc = sec->content().begin() + rel.offset; |
524 | if (!RelTy::HasAddend) |
525 | addend = target.getImplicitAddend(buf: bufLoc, type); |
526 | |
527 | if (ctx.arg.emachine == EM_MIPS && |
528 | target.getRelExpr(type, s: sym, loc: bufLoc) == RE_MIPS_GOTREL) { |
529 | // Some MIPS relocations depend on "gp" value. By default, |
530 | // this value has 0x7ff0 offset from a .got section. But |
531 | // relocatable files produced by a compiler or a linker |
532 | // might redefine this default value and we must use it |
533 | // for a calculation of the relocation result. When we |
534 | // generate EXE or DSO it's trivial. Generating a relocatable |
535 | // output is more difficult case because the linker does |
536 | // not calculate relocations in this mode and loses |
537 | // individual "gp" values used by each input object file. |
538 | // As a workaround we add the "gp" value to the relocation |
539 | // addend and save it back to the file. |
540 | addend += sec->getFile<ELFT>()->mipsGp0; |
541 | } |
542 | |
543 | if (RelTy::HasAddend) |
544 | p->r_addend = |
545 | sym.getVA(ctx, addend) - section->getOutputSection()->addr; |
546 | // For SHF_ALLOC sections relocated by REL, append a relocation to |
547 | // sec->relocations so that relocateAlloc transitively called by |
548 | // writeSections will update the implicit addend. Non-SHF_ALLOC sections |
549 | // utilize relocateNonAlloc to process raw relocations and do not need |
550 | // this sec->relocations change. |
551 | else if (ctx.arg.relocatable && (sec->flags & SHF_ALLOC) && |
552 | type != target.noneRel) |
553 | sec->addReloc(r: {.expr: R_ABS, .type: type, .offset: rel.offset, .addend: addend, .sym: &sym}); |
554 | } else if (ctx.arg.emachine == EM_PPC && type == R_PPC_PLTREL24 && |
555 | p->r_addend >= 0x8000 && sec->file->ppc32Got2) { |
556 | // Similar to R_MIPS_GPREL{16,32}. If the addend of R_PPC_PLTREL24 |
557 | // indicates that r30 is relative to the input section .got2 |
558 | // (r_addend>=0x8000), after linking, r30 should be relative to the output |
559 | // section .got2 . To compensate for the shift, adjust r_addend by |
560 | // ppc32Got->outSecOff. |
561 | p->r_addend += sec->file->ppc32Got2->outSecOff; |
562 | } |
563 | } |
564 | } |
565 | |
566 | // The ARM and AArch64 ABI handle pc-relative relocations to undefined weak |
567 | // references specially. The general rule is that the value of the symbol in |
568 | // this context is the address of the place P. A further special case is that |
569 | // branch relocations to an undefined weak reference resolve to the next |
570 | // instruction. |
571 | static uint32_t getARMUndefinedRelativeWeakVA(RelType type, uint32_t a, |
572 | uint32_t p) { |
573 | switch (type) { |
574 | // Unresolved branch relocations to weak references resolve to next |
575 | // instruction, this will be either 2 or 4 bytes on from P. |
576 | case R_ARM_THM_JUMP8: |
577 | case R_ARM_THM_JUMP11: |
578 | return p + 2 + a; |
579 | case R_ARM_CALL: |
580 | case R_ARM_JUMP24: |
581 | case R_ARM_PC24: |
582 | case R_ARM_PLT32: |
583 | case R_ARM_PREL31: |
584 | case R_ARM_THM_JUMP19: |
585 | case R_ARM_THM_JUMP24: |
586 | return p + 4 + a; |
587 | case R_ARM_THM_CALL: |
588 | // We don't want an interworking BLX to ARM |
589 | return p + 5 + a; |
590 | // Unresolved non branch pc-relative relocations |
591 | // R_ARM_TARGET2 which can be resolved relatively is not present as it never |
592 | // targets a weak-reference. |
593 | case R_ARM_MOVW_PREL_NC: |
594 | case R_ARM_MOVT_PREL: |
595 | case R_ARM_REL32: |
596 | case R_ARM_THM_ALU_PREL_11_0: |
597 | case R_ARM_THM_MOVW_PREL_NC: |
598 | case R_ARM_THM_MOVT_PREL: |
599 | case R_ARM_THM_PC12: |
600 | return p + a; |
601 | // p + a is unrepresentable as negative immediates can't be encoded. |
602 | case R_ARM_THM_PC8: |
603 | return p; |
604 | } |
605 | llvm_unreachable("ARM pc-relative relocation expected\n"); |
606 | } |
607 | |
608 | // The comment above getARMUndefinedRelativeWeakVA applies to this function. |
609 | static uint64_t getAArch64UndefinedRelativeWeakVA(uint64_t type, uint64_t p) { |
610 | switch (type) { |
611 | // Unresolved branch relocations to weak references resolve to next |
612 | // instruction, this is 4 bytes on from P. |
613 | case R_AARCH64_CALL26: |
614 | case R_AARCH64_CONDBR19: |
615 | case R_AARCH64_JUMP26: |
616 | case R_AARCH64_TSTBR14: |
617 | return p + 4; |
618 | // Unresolved non branch pc-relative relocations |
619 | case R_AARCH64_PREL16: |
620 | case R_AARCH64_PREL32: |
621 | case R_AARCH64_PREL64: |
622 | case R_AARCH64_ADR_PREL_LO21: |
623 | case R_AARCH64_LD_PREL_LO19: |
624 | case R_AARCH64_PLT32: |
625 | return p; |
626 | } |
627 | llvm_unreachable("AArch64 pc-relative relocation expected\n"); |
628 | } |
629 | |
630 | static uint64_t getRISCVUndefinedRelativeWeakVA(uint64_t type, uint64_t p) { |
631 | switch (type) { |
632 | case R_RISCV_BRANCH: |
633 | case R_RISCV_JAL: |
634 | case R_RISCV_CALL: |
635 | case R_RISCV_CALL_PLT: |
636 | case R_RISCV_RVC_BRANCH: |
637 | case R_RISCV_RVC_JUMP: |
638 | case R_RISCV_PLT32: |
639 | return p; |
640 | default: |
641 | return 0; |
642 | } |
643 | } |
644 | |
645 | // ARM SBREL relocations are of the form S + A - B where B is the static base |
646 | // The ARM ABI defines base to be "addressing origin of the output segment |
647 | // defining the symbol S". We defined the "addressing origin"/static base to be |
648 | // the base of the PT_LOAD segment containing the Sym. |
649 | // The procedure call standard only defines a Read Write Position Independent |
650 | // RWPI variant so in practice we should expect the static base to be the base |
651 | // of the RW segment. |
652 | static uint64_t getARMStaticBase(const Symbol &sym) { |
653 | OutputSection *os = sym.getOutputSection(); |
654 | if (!os || !os->ptLoad || !os->ptLoad->firstSec) { |
655 | Err(ctx&: os->ctx) << "SBREL relocation to "<< sym.getName() |
656 | << " without static base"; |
657 | return 0; |
658 | } |
659 | return os->ptLoad->firstSec->addr; |
660 | } |
661 | |
662 | // For RE_RISCV_PC_INDIRECT (R_RISCV_PCREL_LO12_{I,S}), the symbol actually |
663 | // points the corresponding R_RISCV_PCREL_HI20 relocation, and the target VA |
664 | // is calculated using PCREL_HI20's symbol. |
665 | // |
666 | // This function returns the R_RISCV_PCREL_HI20 relocation from the |
667 | // R_RISCV_PCREL_LO12 relocation. |
668 | static Relocation *getRISCVPCRelHi20(Ctx &ctx, const InputSectionBase *loSec, |
669 | const Relocation &loReloc) { |
670 | uint64_t addend = loReloc.addend; |
671 | Symbol *sym = loReloc.sym; |
672 | |
673 | const Defined *d = cast<Defined>(Val: sym); |
674 | if (!d->section) { |
675 | Err(ctx) << loSec->getLocation(offset: loReloc.offset) |
676 | << ": R_RISCV_PCREL_LO12 relocation points to an absolute symbol: " |
677 | << sym->getName(); |
678 | return nullptr; |
679 | } |
680 | InputSection *hiSec = cast<InputSection>(Val: d->section); |
681 | |
682 | if (hiSec != loSec) |
683 | Err(ctx) << loSec->getLocation(offset: loReloc.offset) |
684 | << ": R_RISCV_PCREL_LO12 relocation points to a symbol '" |
685 | << sym->getName() << "' in a different section '"<< hiSec->name |
686 | << "'"; |
687 | |
688 | if (addend != 0) |
689 | Warn(ctx) << loSec->getLocation(offset: loReloc.offset) |
690 | << ": non-zero addend in R_RISCV_PCREL_LO12 relocation to " |
691 | << hiSec->getObjMsg(offset: d->value) << " is ignored"; |
692 | |
693 | // Relocations are sorted by offset, so we can use std::equal_range to do |
694 | // binary search. |
695 | Relocation hiReloc; |
696 | hiReloc.offset = d->value; |
697 | auto range = |
698 | std::equal_range(first: hiSec->relocs().begin(), last: hiSec->relocs().end(), val: hiReloc, |
699 | comp: [](const Relocation &lhs, const Relocation &rhs) { |
700 | return lhs.offset < rhs.offset; |
701 | }); |
702 | |
703 | for (auto it = range.first; it != range.second; ++it) |
704 | if (it->type == R_RISCV_PCREL_HI20 || it->type == R_RISCV_GOT_HI20 || |
705 | it->type == R_RISCV_TLS_GD_HI20 || it->type == R_RISCV_TLS_GOT_HI20) |
706 | return &*it; |
707 | |
708 | Err(ctx) << loSec->getLocation(offset: loReloc.offset) |
709 | << ": R_RISCV_PCREL_LO12 relocation points to " |
710 | << hiSec->getObjMsg(offset: d->value) |
711 | << " without an associated R_RISCV_PCREL_HI20 relocation"; |
712 | return nullptr; |
713 | } |
714 | |
715 | // A TLS symbol's virtual address is relative to the TLS segment. Add a |
716 | // target-specific adjustment to produce a thread-pointer-relative offset. |
717 | static int64_t getTlsTpOffset(Ctx &ctx, const Symbol &s) { |
718 | // On targets that support TLSDESC, _TLS_MODULE_BASE_@tpoff = 0. |
719 | if (&s == ctx.sym.tlsModuleBase) |
720 | return 0; |
721 | |
722 | // There are 2 TLS layouts. Among targets we support, x86 uses TLS Variant 2 |
723 | // while most others use Variant 1. At run time TP will be aligned to p_align. |
724 | |
725 | // Variant 1. TP will be followed by an optional gap (which is the size of 2 |
726 | // pointers on ARM/AArch64, 0 on other targets), followed by alignment |
727 | // padding, then the static TLS blocks. The alignment padding is added so that |
728 | // (TP + gap + padding) is congruent to p_vaddr modulo p_align. |
729 | // |
730 | // Variant 2. Static TLS blocks, followed by alignment padding are placed |
731 | // before TP. The alignment padding is added so that (TP - padding - |
732 | // p_memsz) is congruent to p_vaddr modulo p_align. |
733 | PhdrEntry *tls = ctx.tlsPhdr; |
734 | if (!tls) // Reported an error in getSymVA |
735 | return 0; |
736 | switch (ctx.arg.emachine) { |
737 | // Variant 1. |
738 | case EM_ARM: |
739 | case EM_AARCH64: |
740 | return s.getVA(ctx, addend: 0) + ctx.arg.wordsize * 2 + |
741 | ((tls->p_vaddr - ctx.arg.wordsize * 2) & (tls->p_align - 1)); |
742 | case EM_MIPS: |
743 | case EM_PPC: |
744 | case EM_PPC64: |
745 | // Adjusted Variant 1. TP is placed with a displacement of 0x7000, which is |
746 | // to allow a signed 16-bit offset to reach 0x1000 of TCB/thread-library |
747 | // data and 0xf000 of the program's TLS segment. |
748 | return s.getVA(ctx, addend: 0) + (tls->p_vaddr & (tls->p_align - 1)) - 0x7000; |
749 | case EM_LOONGARCH: |
750 | case EM_RISCV: |
751 | // See the comment in handleTlsRelocation. For TLSDESC=>IE, |
752 | // R_RISCV_TLSDESC_{LOAD_LO12,ADD_LO12_I,CALL} also reach here. While |
753 | // `tls` may be null, the return value is ignored. |
754 | if (s.type != STT_TLS) |
755 | return 0; |
756 | return s.getVA(ctx, addend: 0) + (tls->p_vaddr & (tls->p_align - 1)); |
757 | |
758 | // Variant 2. |
759 | case EM_HEXAGON: |
760 | case EM_S390: |
761 | case EM_SPARCV9: |
762 | case EM_386: |
763 | case EM_X86_64: |
764 | return s.getVA(ctx, addend: 0) - tls->p_memsz - |
765 | ((-tls->p_vaddr - tls->p_memsz) & (tls->p_align - 1)); |
766 | default: |
767 | llvm_unreachable("unhandled ctx.arg.emachine"); |
768 | } |
769 | } |
770 | |
771 | uint64_t InputSectionBase::getRelocTargetVA(Ctx &ctx, const Relocation &r, |
772 | uint64_t p) const { |
773 | int64_t a = r.addend; |
774 | switch (r.expr) { |
775 | case R_ABS: |
776 | case R_DTPREL: |
777 | case R_RELAX_TLS_LD_TO_LE_ABS: |
778 | case R_RELAX_GOT_PC_NOPIC: |
779 | case RE_AARCH64_AUTH: |
780 | case RE_RISCV_ADD: |
781 | case RE_RISCV_LEB128: |
782 | return r.sym->getVA(ctx, addend: a); |
783 | case R_ADDEND: |
784 | return a; |
785 | case R_RELAX_HINT: |
786 | return 0; |
787 | case RE_ARM_SBREL: |
788 | return r.sym->getVA(ctx, addend: a) - getARMStaticBase(sym: *r.sym); |
789 | case R_GOT: |
790 | case RE_AARCH64_AUTH_GOT: |
791 | case R_RELAX_TLS_GD_TO_IE_ABS: |
792 | return r.sym->getGotVA(ctx) + a; |
793 | case RE_LOONGARCH_GOT: |
794 | // The LoongArch TLS GD relocs reuse the R_LARCH_GOT_PC_LO12 reloc r.type |
795 | // for their page offsets. The arithmetics are different in the TLS case |
796 | // so we have to duplicate some logic here. |
797 | if (r.sym->hasFlag(bit: NEEDS_TLSGD) && r.type != R_LARCH_TLS_IE_PC_LO12) |
798 | // Like RE_LOONGARCH_TLSGD_PAGE_PC but taking the absolute value. |
799 | return ctx.in.got->getGlobalDynAddr(b: *r.sym) + a; |
800 | return r.sym->getGotVA(ctx) + a; |
801 | case R_GOTONLY_PC: |
802 | return ctx.in.got->getVA() + a - p; |
803 | case R_GOTPLTONLY_PC: |
804 | return ctx.in.gotPlt->getVA() + a - p; |
805 | case R_GOTREL: |
806 | case RE_PPC64_RELAX_TOC: |
807 | return r.sym->getVA(ctx, addend: a) - ctx.in.got->getVA(); |
808 | case R_GOTPLTREL: |
809 | return r.sym->getVA(ctx, addend: a) - ctx.in.gotPlt->getVA(); |
810 | case R_GOTPLT: |
811 | case R_RELAX_TLS_GD_TO_IE_GOTPLT: |
812 | return r.sym->getGotVA(ctx) + a - ctx.in.gotPlt->getVA(); |
813 | case R_TLSLD_GOT_OFF: |
814 | case R_GOT_OFF: |
815 | case R_RELAX_TLS_GD_TO_IE_GOT_OFF: |
816 | return r.sym->getGotOffset(ctx) + a; |
817 | case RE_AARCH64_GOT_PAGE_PC: |
818 | case RE_AARCH64_AUTH_GOT_PAGE_PC: |
819 | case RE_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC: |
820 | return getAArch64Page(expr: r.sym->getGotVA(ctx) + a) - getAArch64Page(expr: p); |
821 | case RE_AARCH64_GOT_PAGE: |
822 | return r.sym->getGotVA(ctx) + a - getAArch64Page(expr: ctx.in.got->getVA()); |
823 | case R_GOT_PC: |
824 | case RE_AARCH64_AUTH_GOT_PC: |
825 | case R_RELAX_TLS_GD_TO_IE: |
826 | return r.sym->getGotVA(ctx) + a - p; |
827 | case R_GOTPLT_GOTREL: |
828 | return r.sym->getGotPltVA(ctx) + a - ctx.in.got->getVA(); |
829 | case R_GOTPLT_PC: |
830 | return r.sym->getGotPltVA(ctx) + a - p; |
831 | case RE_LOONGARCH_GOT_PAGE_PC: |
832 | if (r.sym->hasFlag(bit: NEEDS_TLSGD)) |
833 | return getLoongArchPageDelta(dest: ctx.in.got->getGlobalDynAddr(b: *r.sym) + a, pc: p, |
834 | type: r.type); |
835 | return getLoongArchPageDelta(dest: r.sym->getGotVA(ctx) + a, pc: p, type: r.type); |
836 | case RE_MIPS_GOTREL: |
837 | return r.sym->getVA(ctx, addend: a) - ctx.in.mipsGot->getGp(f: file); |
838 | case RE_MIPS_GOT_GP: |
839 | return ctx.in.mipsGot->getGp(f: file) + a; |
840 | case RE_MIPS_GOT_GP_PC: { |
841 | // R_MIPS_LO16 expression has RE_MIPS_GOT_GP_PC r.type iif the target |
842 | // is _gp_disp symbol. In that case we should use the following |
843 | // formula for calculation "AHL + GP - P + 4". For details see p. 4-19 at |
844 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
845 | // microMIPS variants of these relocations use slightly different |
846 | // expressions: AHL + GP - P + 3 for %lo() and AHL + GP - P - 1 for %hi() |
847 | // to correctly handle less-significant bit of the microMIPS symbol. |
848 | uint64_t v = ctx.in.mipsGot->getGp(f: file) + a - p; |
849 | if (r.type == R_MIPS_LO16 || r.type == R_MICROMIPS_LO16) |
850 | v += 4; |
851 | if (r.type == R_MICROMIPS_LO16 || r.type == R_MICROMIPS_HI16) |
852 | v -= 1; |
853 | return v; |
854 | } |
855 | case RE_MIPS_GOT_LOCAL_PAGE: |
856 | // If relocation against MIPS local symbol requires GOT entry, this entry |
857 | // should be initialized by 'page address'. This address is high 16-bits |
858 | // of sum the symbol's value and the addend. |
859 | return ctx.in.mipsGot->getVA() + |
860 | ctx.in.mipsGot->getPageEntryOffset(f: file, s: *r.sym, addend: a) - |
861 | ctx.in.mipsGot->getGp(f: file); |
862 | case RE_MIPS_GOT_OFF: |
863 | case RE_MIPS_GOT_OFF32: |
864 | // In case of MIPS if a GOT relocation has non-zero addend this addend |
865 | // should be applied to the GOT entry content not to the GOT entry offset. |
866 | // That is why we use separate expression r.type. |
867 | return ctx.in.mipsGot->getVA() + |
868 | ctx.in.mipsGot->getSymEntryOffset(f: file, s: *r.sym, addend: a) - |
869 | ctx.in.mipsGot->getGp(f: file); |
870 | case RE_MIPS_TLSGD: |
871 | return ctx.in.mipsGot->getVA() + |
872 | ctx.in.mipsGot->getGlobalDynOffset(f: file, s: *r.sym) - |
873 | ctx.in.mipsGot->getGp(f: file); |
874 | case RE_MIPS_TLSLD: |
875 | return ctx.in.mipsGot->getVA() + ctx.in.mipsGot->getTlsIndexOffset(f: file) - |
876 | ctx.in.mipsGot->getGp(f: file); |
877 | case RE_AARCH64_PAGE_PC: { |
878 | uint64_t val = r.sym->isUndefWeak() ? p + a : r.sym->getVA(ctx, addend: a); |
879 | return getAArch64Page(expr: val) - getAArch64Page(expr: p); |
880 | } |
881 | case RE_RISCV_PC_INDIRECT: { |
882 | if (const Relocation *hiRel = getRISCVPCRelHi20(ctx, loSec: this, loReloc: r)) |
883 | return getRelocTargetVA(ctx, r: *hiRel, p: r.sym->getVA(ctx)); |
884 | return 0; |
885 | } |
886 | case RE_LOONGARCH_PAGE_PC: |
887 | return getLoongArchPageDelta(dest: r.sym->getVA(ctx, addend: a), pc: p, type: r.type); |
888 | case R_PC: |
889 | case RE_ARM_PCA: { |
890 | uint64_t dest; |
891 | if (r.expr == RE_ARM_PCA) |
892 | // Some PC relative ARM (Thumb) relocations align down the place. |
893 | p = p & 0xfffffffc; |
894 | if (r.sym->isUndefined()) { |
895 | // On ARM and AArch64 a branch to an undefined weak resolves to the next |
896 | // instruction, otherwise the place. On RISC-V, resolve an undefined weak |
897 | // to the same instruction to cause an infinite loop (making the user |
898 | // aware of the issue) while ensuring no overflow. |
899 | // Note: if the symbol is hidden, its binding has been converted to local, |
900 | // so we just check isUndefined() here. |
901 | if (ctx.arg.emachine == EM_ARM) |
902 | dest = getARMUndefinedRelativeWeakVA(type: r.type, a, p); |
903 | else if (ctx.arg.emachine == EM_AARCH64) |
904 | dest = getAArch64UndefinedRelativeWeakVA(type: r.type, p) + a; |
905 | else if (ctx.arg.emachine == EM_PPC) |
906 | dest = p; |
907 | else if (ctx.arg.emachine == EM_RISCV) |
908 | dest = getRISCVUndefinedRelativeWeakVA(type: r.type, p) + a; |
909 | else |
910 | dest = r.sym->getVA(ctx, addend: a); |
911 | } else { |
912 | dest = r.sym->getVA(ctx, addend: a); |
913 | } |
914 | return dest - p; |
915 | } |
916 | case R_PLT: |
917 | return r.sym->getPltVA(ctx) + a; |
918 | case R_PLT_PC: |
919 | case RE_PPC64_CALL_PLT: |
920 | return r.sym->getPltVA(ctx) + a - p; |
921 | case RE_LOONGARCH_PLT_PAGE_PC: |
922 | return getLoongArchPageDelta(dest: r.sym->getPltVA(ctx) + a, pc: p, type: r.type); |
923 | case R_PLT_GOTPLT: |
924 | return r.sym->getPltVA(ctx) + a - ctx.in.gotPlt->getVA(); |
925 | case R_PLT_GOTREL: |
926 | return r.sym->getPltVA(ctx) + a - ctx.in.got->getVA(); |
927 | case RE_PPC32_PLTREL: |
928 | // R_PPC_PLTREL24 uses the addend (usually 0 or 0x8000) to indicate r30 |
929 | // stores _GLOBAL_OFFSET_TABLE_ or .got2+0x8000. The addend is ignored for |
930 | // target VA computation. |
931 | return r.sym->getPltVA(ctx) - p; |
932 | case RE_PPC64_CALL: { |
933 | uint64_t symVA = r.sym->getVA(ctx, addend: a); |
934 | // If we have an undefined weak symbol, we might get here with a symbol |
935 | // address of zero. That could overflow, but the code must be unreachable, |
936 | // so don't bother doing anything at all. |
937 | if (!symVA) |
938 | return 0; |
939 | |
940 | // PPC64 V2 ABI describes two entry points to a function. The global entry |
941 | // point is used for calls where the caller and callee (may) have different |
942 | // TOC base pointers and r2 needs to be modified to hold the TOC base for |
943 | // the callee. For local calls the caller and callee share the same |
944 | // TOC base and so the TOC pointer initialization code should be skipped by |
945 | // branching to the local entry point. |
946 | return symVA - p + |
947 | getPPC64GlobalEntryToLocalEntryOffset(ctx, stOther: r.sym->stOther); |
948 | } |
949 | case RE_PPC64_TOCBASE: |
950 | return getPPC64TocBase(ctx) + a; |
951 | case R_RELAX_GOT_PC: |
952 | case RE_PPC64_RELAX_GOT_PC: |
953 | return r.sym->getVA(ctx, addend: a) - p; |
954 | case R_RELAX_TLS_GD_TO_LE: |
955 | case R_RELAX_TLS_IE_TO_LE: |
956 | case R_RELAX_TLS_LD_TO_LE: |
957 | case R_TPREL: |
958 | // It is not very clear what to return if the symbol is undefined. With |
959 | // --noinhibit-exec, even a non-weak undefined reference may reach here. |
960 | // Just return A, which matches R_ABS, and the behavior of some dynamic |
961 | // loaders. |
962 | if (r.sym->isUndefined()) |
963 | return a; |
964 | return getTlsTpOffset(ctx, s: *r.sym) + a; |
965 | case R_RELAX_TLS_GD_TO_LE_NEG: |
966 | case R_TPREL_NEG: |
967 | if (r.sym->isUndefined()) |
968 | return a; |
969 | return -getTlsTpOffset(ctx, s: *r.sym) + a; |
970 | case R_SIZE: |
971 | return r.sym->getSize() + a; |
972 | case R_TLSDESC: |
973 | case RE_AARCH64_AUTH_TLSDESC: |
974 | return ctx.in.got->getTlsDescAddr(sym: *r.sym) + a; |
975 | case R_TLSDESC_PC: |
976 | return ctx.in.got->getTlsDescAddr(sym: *r.sym) + a - p; |
977 | case R_TLSDESC_GOTPLT: |
978 | return ctx.in.got->getTlsDescAddr(sym: *r.sym) + a - ctx.in.gotPlt->getVA(); |
979 | case RE_AARCH64_TLSDESC_PAGE: |
980 | case RE_AARCH64_AUTH_TLSDESC_PAGE: |
981 | return getAArch64Page(expr: ctx.in.got->getTlsDescAddr(sym: *r.sym) + a) - |
982 | getAArch64Page(expr: p); |
983 | case RE_LOONGARCH_TLSDESC_PAGE_PC: |
984 | return getLoongArchPageDelta(dest: ctx.in.got->getTlsDescAddr(sym: *r.sym) + a, pc: p, |
985 | type: r.type); |
986 | case R_TLSGD_GOT: |
987 | return ctx.in.got->getGlobalDynOffset(b: *r.sym) + a; |
988 | case R_TLSGD_GOTPLT: |
989 | return ctx.in.got->getGlobalDynAddr(b: *r.sym) + a - ctx.in.gotPlt->getVA(); |
990 | case R_TLSGD_PC: |
991 | return ctx.in.got->getGlobalDynAddr(b: *r.sym) + a - p; |
992 | case RE_LOONGARCH_TLSGD_PAGE_PC: |
993 | return getLoongArchPageDelta(dest: ctx.in.got->getGlobalDynAddr(b: *r.sym) + a, pc: p, |
994 | type: r.type); |
995 | case R_TLSLD_GOTPLT: |
996 | return ctx.in.got->getVA() + ctx.in.got->getTlsIndexOff() + a - |
997 | ctx.in.gotPlt->getVA(); |
998 | case R_TLSLD_GOT: |
999 | return ctx.in.got->getTlsIndexOff() + a; |
1000 | case R_TLSLD_PC: |
1001 | return ctx.in.got->getTlsIndexVA() + a - p; |
1002 | default: |
1003 | llvm_unreachable("invalid expression"); |
1004 | } |
1005 | } |
1006 | |
1007 | // This function applies relocations to sections without SHF_ALLOC bit. |
1008 | // Such sections are never mapped to memory at runtime. Debug sections are |
1009 | // an example. Relocations in non-alloc sections are much easier to |
1010 | // handle than in allocated sections because it will never need complex |
1011 | // treatment such as GOT or PLT (because at runtime no one refers them). |
1012 | // So, we handle relocations for non-alloc sections directly in this |
1013 | // function as a performance optimization. |
1014 | template <class ELFT, class RelTy> |
1015 | void InputSection::relocateNonAlloc(Ctx &ctx, uint8_t *buf, |
1016 | Relocs<RelTy> rels) { |
1017 | const unsigned bits = sizeof(typename ELFT::uint) * 8; |
1018 | const TargetInfo &target = *ctx.target; |
1019 | const auto emachine = ctx.arg.emachine; |
1020 | const bool isDebug = isDebugSection(sec: *this); |
1021 | const bool isDebugLine = isDebug && name == ".debug_line"; |
1022 | std::optional<uint64_t> tombstone; |
1023 | if (isDebug) { |
1024 | if (name == ".debug_loc"|| name == ".debug_ranges") |
1025 | tombstone = 1; |
1026 | else if (name == ".debug_names") |
1027 | tombstone = UINT64_MAX; // tombstone value |
1028 | else |
1029 | tombstone = 0; |
1030 | } |
1031 | for (const auto &patAndValue : llvm::reverse(C&: ctx.arg.deadRelocInNonAlloc)) |
1032 | if (patAndValue.first.match(S: this->name)) { |
1033 | tombstone = patAndValue.second; |
1034 | break; |
1035 | } |
1036 | |
1037 | const InputFile *f = this->file; |
1038 | for (auto it = rels.begin(), end = rels.end(); it != end; ++it) { |
1039 | const RelTy &rel = *it; |
1040 | const RelType type = rel.getType(ctx.arg.isMips64EL); |
1041 | const uint64_t offset = rel.r_offset; |
1042 | uint8_t *bufLoc = buf + offset; |
1043 | int64_t addend = getAddend<ELFT>(rel); |
1044 | if (!RelTy::HasAddend) |
1045 | addend += target.getImplicitAddend(buf: bufLoc, type); |
1046 | |
1047 | Symbol &sym = f->getRelocTargetSym(rel); |
1048 | RelExpr expr = target.getRelExpr(type, s: sym, loc: bufLoc); |
1049 | if (expr == R_NONE) |
1050 | continue; |
1051 | auto *ds = dyn_cast<Defined>(Val: &sym); |
1052 | |
1053 | if (emachine == EM_RISCV && type == R_RISCV_SET_ULEB128) { |
1054 | if (++it != end && |
1055 | it->getType(/*isMips64EL=*/false) == R_RISCV_SUB_ULEB128 && |
1056 | it->r_offset == offset) { |
1057 | uint64_t val; |
1058 | if (!ds && tombstone) { |
1059 | val = *tombstone; |
1060 | } else { |
1061 | val = sym.getVA(ctx, addend) - |
1062 | (f->getRelocTargetSym(*it).getVA(ctx) + getAddend<ELFT>(*it)); |
1063 | } |
1064 | if (overwriteULEB128(bufLoc, val) >= 0x80) |
1065 | Err(ctx) << getLocation(offset) << ": ULEB128 value "<< val |
1066 | << " exceeds available space; references '"<< &sym << "'"; |
1067 | continue; |
1068 | } |
1069 | Err(ctx) << getLocation(offset) |
1070 | << ": R_RISCV_SET_ULEB128 not paired with R_RISCV_SUB_SET128"; |
1071 | return; |
1072 | } |
1073 | |
1074 | if (tombstone && (expr == R_ABS || expr == R_DTPREL)) { |
1075 | // Resolve relocations in .debug_* referencing (discarded symbols or ICF |
1076 | // folded section symbols) to a tombstone value. Resolving to addend is |
1077 | // unsatisfactory because the result address range may collide with a |
1078 | // valid range of low address, or leave multiple CUs claiming ownership of |
1079 | // the same range of code, which may confuse consumers. |
1080 | // |
1081 | // To address the problems, we use -1 as a tombstone value for most |
1082 | // .debug_* sections. We have to ignore the addend because we don't want |
1083 | // to resolve an address attribute (which may have a non-zero addend) to |
1084 | // -1+addend (wrap around to a low address). |
1085 | // |
1086 | // R_DTPREL type relocations represent an offset into the dynamic thread |
1087 | // vector. The computed value is st_value plus a non-negative offset. |
1088 | // Negative values are invalid, so -1 can be used as the tombstone value. |
1089 | // |
1090 | // If the referenced symbol is relative to a discarded section (due to |
1091 | // --gc-sections, COMDAT, etc), it has been converted to a Undefined. |
1092 | // `ds->folded` catches the ICF folded case. However, resolving a |
1093 | // relocation in .debug_line to -1 would stop debugger users from setting |
1094 | // breakpoints on the folded-in function, so exclude .debug_line. |
1095 | // |
1096 | // For pre-DWARF-v5 .debug_loc and .debug_ranges, -1 is a reserved value |
1097 | // (base address selection entry), use 1 (which is used by GNU ld for |
1098 | // .debug_ranges). |
1099 | // |
1100 | // TODO To reduce disruption, we use 0 instead of -1 as the tombstone |
1101 | // value. Enable -1 in a future release. |
1102 | if (!ds || (ds->folded && !isDebugLine)) { |
1103 | // If -z dead-reloc-in-nonalloc= is specified, respect it. |
1104 | uint64_t value = SignExtend64<bits>(*tombstone); |
1105 | // For a 32-bit local TU reference in .debug_names, X86_64::relocate |
1106 | // requires that the unsigned value for R_X86_64_32 is truncated to |
1107 | // 32-bit. Other 64-bit targets's don't discern signed/unsigned 32-bit |
1108 | // absolute relocations and do not need this change. |
1109 | if (emachine == EM_X86_64 && type == R_X86_64_32) |
1110 | value = static_cast<uint32_t>(value); |
1111 | target.relocateNoSym(loc: bufLoc, type, val: value); |
1112 | continue; |
1113 | } |
1114 | } |
1115 | |
1116 | // For a relocatable link, content relocated by relocation types with an |
1117 | // explicit addend, such as RELA, remain unchanged and we can stop here. |
1118 | // While content relocated by relocation types with an implicit addend, such |
1119 | // as REL, needs the implicit addend updated. |
1120 | if (ctx.arg.relocatable && (RelTy::HasAddend || sym.type != STT_SECTION)) |
1121 | continue; |
1122 | |
1123 | // R_ABS/R_DTPREL and some other relocations can be used from non-SHF_ALLOC |
1124 | // sections. |
1125 | if (LLVM_LIKELY(expr == R_ABS) || expr == R_DTPREL || expr == R_GOTPLTREL || |
1126 | expr == RE_RISCV_ADD || expr == RE_ARM_SBREL) { |
1127 | target.relocateNoSym(loc: bufLoc, type, |
1128 | val: SignExtend64<bits>(sym.getVA(ctx, addend))); |
1129 | continue; |
1130 | } |
1131 | |
1132 | if (expr == R_SIZE) { |
1133 | target.relocateNoSym(loc: bufLoc, type, |
1134 | val: SignExtend64<bits>(sym.getSize() + addend)); |
1135 | continue; |
1136 | } |
1137 | |
1138 | // If the control reaches here, we found a PC-relative relocation in a |
1139 | // non-ALLOC section. Since non-ALLOC section is not loaded into memory |
1140 | // at runtime, the notion of PC-relative doesn't make sense here. So, |
1141 | // this is a usage error. However, GNU linkers historically accept such |
1142 | // relocations without any errors and relocate them as if they were at |
1143 | // address 0. For bug-compatibility, we accept them with warnings. We |
1144 | // know Steel Bank Common Lisp as of 2018 have this bug. |
1145 | // |
1146 | // GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations |
1147 | // against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed in |
1148 | // 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we need to |
1149 | // keep this bug-compatible code for a while. |
1150 | bool isErr = expr != R_PC && !(emachine == EM_386 && type == R_386_GOTPC); |
1151 | { |
1152 | ELFSyncStream diag(ctx, isErr && !ctx.arg.noinhibitExec |
1153 | ? DiagLevel::Err |
1154 | : DiagLevel::Warn); |
1155 | diag << getLocation(offset) << ": has non-ABS relocation "<< type |
1156 | << " against symbol '"<< &sym << "'"; |
1157 | } |
1158 | if (!isErr) |
1159 | target.relocateNoSym( |
1160 | loc: bufLoc, type, |
1161 | val: SignExtend64<bits>(sym.getVA(ctx, addend: addend - offset - outSecOff))); |
1162 | } |
1163 | } |
1164 | |
1165 | template <class ELFT> |
1166 | void InputSectionBase::relocate(Ctx &ctx, uint8_t *buf, uint8_t *bufEnd) { |
1167 | if ((flags & SHF_EXECINSTR) && LLVM_UNLIKELY(getFile<ELFT>()->splitStack)) |
1168 | adjustSplitStackFunctionPrologues<ELFT>(ctx, buf, bufEnd); |
1169 | |
1170 | if (flags & SHF_ALLOC) { |
1171 | ctx.target->relocateAlloc(sec&: *this, buf); |
1172 | return; |
1173 | } |
1174 | |
1175 | auto *sec = cast<InputSection>(Val: this); |
1176 | // For a relocatable link, also call relocateNonAlloc() to rewrite applicable |
1177 | // locations with tombstone values. |
1178 | invokeOnRelocs(*sec, sec->relocateNonAlloc<ELFT>, ctx, buf); |
1179 | } |
1180 | |
1181 | // For each function-defining prologue, find any calls to __morestack, |
1182 | // and replace them with calls to __morestack_non_split. |
1183 | static void switchMorestackCallsToMorestackNonSplit( |
1184 | Ctx &ctx, DenseSet<Defined *> &prologues, |
1185 | SmallVector<Relocation *, 0> &morestackCalls) { |
1186 | |
1187 | // If the target adjusted a function's prologue, all calls to |
1188 | // __morestack inside that function should be switched to |
1189 | // __morestack_non_split. |
1190 | Symbol *moreStackNonSplit = ctx.symtab->find(name: "__morestack_non_split"); |
1191 | if (!moreStackNonSplit) { |
1192 | ErrAlways(ctx) << "mixing split-stack objects requires a definition of " |
1193 | "__morestack_non_split"; |
1194 | return; |
1195 | } |
1196 | |
1197 | // Sort both collections to compare addresses efficiently. |
1198 | llvm::sort(C&: morestackCalls, Comp: [](const Relocation *l, const Relocation *r) { |
1199 | return l->offset < r->offset; |
1200 | }); |
1201 | std::vector<Defined *> functions(prologues.begin(), prologues.end()); |
1202 | llvm::sort(C&: functions, Comp: [](const Defined *l, const Defined *r) { |
1203 | return l->value < r->value; |
1204 | }); |
1205 | |
1206 | auto it = morestackCalls.begin(); |
1207 | for (Defined *f : functions) { |
1208 | // Find the first call to __morestack within the function. |
1209 | while (it != morestackCalls.end() && (*it)->offset < f->value) |
1210 | ++it; |
1211 | // Adjust all calls inside the function. |
1212 | while (it != morestackCalls.end() && (*it)->offset < f->value + f->size) { |
1213 | (*it)->sym = moreStackNonSplit; |
1214 | ++it; |
1215 | } |
1216 | } |
1217 | } |
1218 | |
1219 | static bool enclosingPrologueAttempted(uint64_t offset, |
1220 | const DenseSet<Defined *> &prologues) { |
1221 | for (Defined *f : prologues) |
1222 | if (f->value <= offset && offset < f->value + f->size) |
1223 | return true; |
1224 | return false; |
1225 | } |
1226 | |
1227 | // If a function compiled for split stack calls a function not |
1228 | // compiled for split stack, then the caller needs its prologue |
1229 | // adjusted to ensure that the called function will have enough stack |
1230 | // available. Find those functions, and adjust their prologues. |
1231 | template <class ELFT> |
1232 | void InputSectionBase::adjustSplitStackFunctionPrologues(Ctx &ctx, uint8_t *buf, |
1233 | uint8_t *end) { |
1234 | DenseSet<Defined *> prologues; |
1235 | SmallVector<Relocation *, 0> morestackCalls; |
1236 | |
1237 | for (Relocation &rel : relocs()) { |
1238 | // Ignore calls into the split-stack api. |
1239 | if (rel.sym->getName().starts_with(Prefix: "__morestack")) { |
1240 | if (rel.sym->getName() == "__morestack") |
1241 | morestackCalls.push_back(Elt: &rel); |
1242 | continue; |
1243 | } |
1244 | |
1245 | // A relocation to non-function isn't relevant. Sometimes |
1246 | // __morestack is not marked as a function, so this check comes |
1247 | // after the name check. |
1248 | if (rel.sym->type != STT_FUNC) |
1249 | continue; |
1250 | |
1251 | // If the callee's-file was compiled with split stack, nothing to do. In |
1252 | // this context, a "Defined" symbol is one "defined by the binary currently |
1253 | // being produced". So an "undefined" symbol might be provided by a shared |
1254 | // library. It is not possible to tell how such symbols were compiled, so be |
1255 | // conservative. |
1256 | if (Defined *d = dyn_cast<Defined>(Val: rel.sym)) |
1257 | if (InputSection *isec = cast_or_null<InputSection>(Val: d->section)) |
1258 | if (!isec || !isec->getFile<ELFT>() || isec->getFile<ELFT>()->splitStack) |
1259 | continue; |
1260 | |
1261 | if (enclosingPrologueAttempted(offset: rel.offset, prologues)) |
1262 | continue; |
1263 | |
1264 | if (Defined *f = getEnclosingFunction(offset: rel.offset)) { |
1265 | prologues.insert(V: f); |
1266 | if (ctx.target->adjustPrologueForCrossSplitStack(loc: buf + f->value, end, |
1267 | stOther: f->stOther)) |
1268 | continue; |
1269 | if (!getFile<ELFT>()->someNoSplitStack) |
1270 | Err(ctx) |
1271 | << this << ": "<< f->getName() << " (with -fsplit-stack) calls " |
1272 | << rel.sym->getName() |
1273 | << " (without -fsplit-stack), but couldn't adjust its prologue"; |
1274 | } |
1275 | } |
1276 | |
1277 | if (ctx.target->needsMoreStackNonSplit) |
1278 | switchMorestackCallsToMorestackNonSplit(ctx, prologues, morestackCalls); |
1279 | } |
1280 | |
1281 | template <class ELFT> void InputSection::writeTo(Ctx &ctx, uint8_t *buf) { |
1282 | if (LLVM_UNLIKELY(type == SHT_NOBITS)) |
1283 | return; |
1284 | // If -r or --emit-relocs is given, then an InputSection |
1285 | // may be a relocation section. |
1286 | if (LLVM_UNLIKELY(type == SHT_RELA)) { |
1287 | copyRelocations<ELFT, typename ELFT::Rela>(ctx, buf); |
1288 | return; |
1289 | } |
1290 | if (LLVM_UNLIKELY(type == SHT_REL)) { |
1291 | copyRelocations<ELFT, typename ELFT::Rel>(ctx, buf); |
1292 | return; |
1293 | } |
1294 | |
1295 | // If -r is given, we may have a SHT_GROUP section. |
1296 | if (LLVM_UNLIKELY(type == SHT_GROUP)) { |
1297 | copyShtGroup<ELFT>(buf); |
1298 | return; |
1299 | } |
1300 | |
1301 | // If this is a compressed section, uncompress section contents directly |
1302 | // to the buffer. |
1303 | if (compressed) { |
1304 | auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content_); |
1305 | auto compressed = ArrayRef<uint8_t>(content_, compressedSize) |
1306 | .slice(N: sizeof(typename ELFT::Chdr)); |
1307 | size_t size = this->size; |
1308 | if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB |
1309 | ? compression::zlib::decompress(Input: compressed, Output: buf, UncompressedSize&: size) |
1310 | : compression::zstd::decompress(Input: compressed, Output: buf, UncompressedSize&: size)) |
1311 | Err(ctx) << this << ": decompress failed: "<< std::move(e); |
1312 | uint8_t *bufEnd = buf + size; |
1313 | relocate<ELFT>(ctx, buf, bufEnd); |
1314 | return; |
1315 | } |
1316 | |
1317 | // Copy section contents from source object file to output file |
1318 | // and then apply relocations. |
1319 | memcpy(dest: buf, src: content().data(), n: content().size()); |
1320 | relocate<ELFT>(ctx, buf, buf + content().size()); |
1321 | } |
1322 | |
1323 | void InputSection::replace(InputSection *other) { |
1324 | addralign = std::max(a: addralign, b: other->addralign); |
1325 | |
1326 | // When a section is replaced with another section that was allocated to |
1327 | // another partition, the replacement section (and its associated sections) |
1328 | // need to be placed in the main partition so that both partitions will be |
1329 | // able to access it. |
1330 | if (partition != other->partition) { |
1331 | partition = 1; |
1332 | for (InputSection *isec : dependentSections) |
1333 | isec->partition = 1; |
1334 | } |
1335 | |
1336 | other->repl = repl; |
1337 | other->markDead(); |
1338 | } |
1339 | |
1340 | template <class ELFT> |
1341 | EhInputSection::EhInputSection(ObjFile<ELFT> &f, |
1342 | const typename ELFT::Shdr &header, |
1343 | StringRef name) |
1344 | : InputSectionBase(f, header, name, InputSectionBase::EHFrame) {} |
1345 | |
1346 | SyntheticSection *EhInputSection::getParent() const { |
1347 | return cast_or_null<SyntheticSection>(Val: parent); |
1348 | } |
1349 | |
1350 | // .eh_frame is a sequence of CIE or FDE records. |
1351 | // This function splits an input section into records and returns them. |
1352 | template <class ELFT> void EhInputSection::split() { |
1353 | const RelsOrRelas<ELFT> rels = relsOrRelas<ELFT>(/*supportsCrel=*/false); |
1354 | // getReloc expects the relocations to be sorted by r_offset. See the comment |
1355 | // in scanRelocs. |
1356 | if (rels.areRelocsRel()) { |
1357 | SmallVector<typename ELFT::Rel, 0> storage; |
1358 | split<ELFT>(sortRels(rels.rels, storage)); |
1359 | } else { |
1360 | SmallVector<typename ELFT::Rela, 0> storage; |
1361 | split<ELFT>(sortRels(rels.relas, storage)); |
1362 | } |
1363 | } |
1364 | |
1365 | template <class ELFT, class RelTy> |
1366 | void EhInputSection::split(ArrayRef<RelTy> rels) { |
1367 | ArrayRef<uint8_t> d = content(); |
1368 | const char *msg = nullptr; |
1369 | unsigned relI = 0; |
1370 | while (!d.empty()) { |
1371 | if (d.size() < 4) { |
1372 | msg = "CIE/FDE too small"; |
1373 | break; |
1374 | } |
1375 | uint64_t size = endian::read32<ELFT::Endianness>(d.data()); |
1376 | if (size == 0) // ZERO terminator |
1377 | break; |
1378 | uint32_t id = endian::read32<ELFT::Endianness>(d.data() + 4); |
1379 | size += 4; |
1380 | if (LLVM_UNLIKELY(size > d.size())) { |
1381 | // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead, |
1382 | // but we do not support that format yet. |
1383 | msg = size == UINT32_MAX + uint64_t(4) |
1384 | ? "CIE/FDE too large" |
1385 | : "CIE/FDE ends past the end of the section"; |
1386 | break; |
1387 | } |
1388 | |
1389 | // Find the first relocation that points to [off,off+size). Relocations |
1390 | // have been sorted by r_offset. |
1391 | const uint64_t off = d.data() - content().data(); |
1392 | while (relI != rels.size() && rels[relI].r_offset < off) |
1393 | ++relI; |
1394 | unsigned firstRel = -1; |
1395 | if (relI != rels.size() && rels[relI].r_offset < off + size) |
1396 | firstRel = relI; |
1397 | (id == 0 ? cies : fdes).emplace_back(Args: off, Args: this, Args&: size, Args&: firstRel); |
1398 | d = d.slice(N: size); |
1399 | } |
1400 | if (msg) |
1401 | Err(ctx&: file->ctx) << "corrupted .eh_frame: "<< msg << "\n>>> defined in " |
1402 | << getObjMsg(offset: d.data() - content().data()); |
1403 | } |
1404 | |
1405 | // Return the offset in an output section for a given input offset. |
1406 | uint64_t EhInputSection::getParentOffset(uint64_t offset) const { |
1407 | auto it = partition_point( |
1408 | Range: fdes, P: [=](EhSectionPiece p) { return p.inputOff <= offset; }); |
1409 | if (it == fdes.begin() || it[-1].inputOff + it[-1].size <= offset) { |
1410 | it = partition_point( |
1411 | Range: cies, P: [=](EhSectionPiece p) { return p.inputOff <= offset; }); |
1412 | if (it == cies.begin()) // invalid piece |
1413 | return offset; |
1414 | } |
1415 | if (it[-1].outputOff == -1) // invalid piece |
1416 | return offset - it[-1].inputOff; |
1417 | return it[-1].outputOff + (offset - it[-1].inputOff); |
1418 | } |
1419 | |
1420 | static size_t findNull(StringRef s, size_t entSize) { |
1421 | for (unsigned i = 0, n = s.size(); i != n; i += entSize) { |
1422 | const char *b = s.begin() + i; |
1423 | if (std::all_of(first: b, last: b + entSize, pred: [](char c) { return c == 0; })) |
1424 | return i; |
1425 | } |
1426 | llvm_unreachable(""); |
1427 | } |
1428 | |
1429 | // Split SHF_STRINGS section. Such section is a sequence of |
1430 | // null-terminated strings. |
1431 | void MergeInputSection::splitStrings(StringRef s, size_t entSize) { |
1432 | const bool live = !(flags & SHF_ALLOC) || !getCtx().arg.gcSections; |
1433 | const char *p = s.data(), *end = s.data() + s.size(); |
1434 | if (!std::all_of(first: end - entSize, last: end, pred: [](char c) { return c == 0; })) { |
1435 | Err(ctx&: getCtx()) << this << ": string is not null terminated"; |
1436 | pieces.emplace_back(Args&: entSize, Args: 0, Args: false); |
1437 | return; |
1438 | } |
1439 | if (entSize == 1) { |
1440 | // Optimize the common case. |
1441 | do { |
1442 | size_t size = strlen(s: p); |
1443 | pieces.emplace_back(Args: p - s.begin(), Args: xxh3_64bits(data: StringRef(p, size)), Args: live); |
1444 | p += size + 1; |
1445 | } while (p != end); |
1446 | } else { |
1447 | do { |
1448 | size_t size = findNull(s: StringRef(p, end - p), entSize); |
1449 | pieces.emplace_back(Args: p - s.begin(), Args: xxh3_64bits(data: StringRef(p, size)), Args: live); |
1450 | p += size + entSize; |
1451 | } while (p != end); |
1452 | } |
1453 | } |
1454 | |
1455 | // Split non-SHF_STRINGS section. Such section is a sequence of |
1456 | // fixed size records. |
1457 | void MergeInputSection::splitNonStrings(ArrayRef<uint8_t> data, |
1458 | size_t entSize) { |
1459 | size_t size = data.size(); |
1460 | assert((size % entSize) == 0); |
1461 | const bool live = !(flags & SHF_ALLOC) || !getCtx().arg.gcSections; |
1462 | |
1463 | pieces.resize_for_overwrite(N: size / entSize); |
1464 | for (size_t i = 0, j = 0; i != size; i += entSize, j++) |
1465 | pieces[j] = {i, (uint32_t)xxh3_64bits(data: data.slice(N: i, M: entSize)), live}; |
1466 | } |
1467 | |
1468 | template <class ELFT> |
1469 | MergeInputSection::MergeInputSection(ObjFile<ELFT> &f, |
1470 | const typename ELFT::Shdr &header, |
1471 | StringRef name) |
1472 | : InputSectionBase(f, header, name, InputSectionBase::Merge) {} |
1473 | |
1474 | MergeInputSection::MergeInputSection(Ctx &ctx, StringRef name, uint32_t type, |
1475 | uint64_t flags, uint64_t entsize, |
1476 | ArrayRef<uint8_t> data) |
1477 | : InputSectionBase(ctx.internalFile, name, type, flags, /*link=*/0, |
1478 | /*info=*/0, |
1479 | /*addralign=*/entsize, entsize, data, |
1480 | SectionBase::Merge) {} |
1481 | |
1482 | // This function is called after we obtain a complete list of input sections |
1483 | // that need to be linked. This is responsible to split section contents |
1484 | // into small chunks for further processing. |
1485 | // |
1486 | // Note that this function is called from parallelForEach. This must be |
1487 | // thread-safe (i.e. no memory allocation from the pools). |
1488 | void MergeInputSection::splitIntoPieces() { |
1489 | assert(pieces.empty()); |
1490 | |
1491 | if (flags & SHF_STRINGS) |
1492 | splitStrings(s: toStringRef(Input: contentMaybeDecompress()), entSize: entsize); |
1493 | else |
1494 | splitNonStrings(data: contentMaybeDecompress(), entSize: entsize); |
1495 | } |
1496 | |
1497 | SectionPiece &MergeInputSection::getSectionPiece(uint64_t offset) { |
1498 | if (content().size() <= offset) { |
1499 | Err(ctx&: getCtx()) << this << ": offset is outside the section"; |
1500 | return pieces[0]; |
1501 | } |
1502 | return partition_point( |
1503 | Range&: pieces, P: [=](SectionPiece p) { return p.inputOff <= offset; })[-1]; |
1504 | } |
1505 | |
1506 | // Return the offset in an output section for a given input offset. |
1507 | uint64_t MergeInputSection::getParentOffset(uint64_t offset) const { |
1508 | const SectionPiece &piece = getSectionPiece(offset); |
1509 | return piece.outputOff + (offset - piece.inputOff); |
1510 | } |
1511 | |
1512 | template InputSection::InputSection(ObjFile<ELF32LE> &, const ELF32LE::Shdr &, |
1513 | StringRef); |
1514 | template InputSection::InputSection(ObjFile<ELF32BE> &, const ELF32BE::Shdr &, |
1515 | StringRef); |
1516 | template InputSection::InputSection(ObjFile<ELF64LE> &, const ELF64LE::Shdr &, |
1517 | StringRef); |
1518 | template InputSection::InputSection(ObjFile<ELF64BE> &, const ELF64BE::Shdr &, |
1519 | StringRef); |
1520 | |
1521 | template void InputSection::writeTo<ELF32LE>(Ctx &, uint8_t *); |
1522 | template void InputSection::writeTo<ELF32BE>(Ctx &, uint8_t *); |
1523 | template void InputSection::writeTo<ELF64LE>(Ctx &, uint8_t *); |
1524 | template void InputSection::writeTo<ELF64BE>(Ctx &, uint8_t *); |
1525 | |
1526 | template RelsOrRelas<ELF32LE> |
1527 | InputSectionBase::relsOrRelas<ELF32LE>(bool) const; |
1528 | template RelsOrRelas<ELF32BE> |
1529 | InputSectionBase::relsOrRelas<ELF32BE>(bool) const; |
1530 | template RelsOrRelas<ELF64LE> |
1531 | InputSectionBase::relsOrRelas<ELF64LE>(bool) const; |
1532 | template RelsOrRelas<ELF64BE> |
1533 | InputSectionBase::relsOrRelas<ELF64BE>(bool) const; |
1534 | |
1535 | template MergeInputSection::MergeInputSection(ObjFile<ELF32LE> &, |
1536 | const ELF32LE::Shdr &, StringRef); |
1537 | template MergeInputSection::MergeInputSection(ObjFile<ELF32BE> &, |
1538 | const ELF32BE::Shdr &, StringRef); |
1539 | template MergeInputSection::MergeInputSection(ObjFile<ELF64LE> &, |
1540 | const ELF64LE::Shdr &, StringRef); |
1541 | template MergeInputSection::MergeInputSection(ObjFile<ELF64BE> &, |
1542 | const ELF64BE::Shdr &, StringRef); |
1543 | |
1544 | template EhInputSection::EhInputSection(ObjFile<ELF32LE> &, |
1545 | const ELF32LE::Shdr &, StringRef); |
1546 | template EhInputSection::EhInputSection(ObjFile<ELF32BE> &, |
1547 | const ELF32BE::Shdr &, StringRef); |
1548 | template EhInputSection::EhInputSection(ObjFile<ELF64LE> &, |
1549 | const ELF64LE::Shdr &, StringRef); |
1550 | template EhInputSection::EhInputSection(ObjFile<ELF64BE> &, |
1551 | const ELF64BE::Shdr &, StringRef); |
1552 | |
1553 | template void EhInputSection::split<ELF32LE>(); |
1554 | template void EhInputSection::split<ELF32BE>(); |
1555 | template void EhInputSection::split<ELF64LE>(); |
1556 | template void EhInputSection::split<ELF64BE>(); |
1557 |
Definitions
- toStr
- operator<<
- getSectionContents
- InputSectionBase
- getFlags
- InputSectionBase
- getSize
- decompressAux
- decompress
- relsOrRelas
- getCtx
- getOffset
- getVA
- getOutputSection
- parseCompressedHeader
- getLinkOrderDep
- getEnclosingSymbol
- getLocation
- printFileLine
- operator<<
- operator<<
- PotentialSpillSection
- discarded
- InputSection
- InputSection
- copyShtGroup
- getRelocatedSection
- copyRelocations
- copyRelocations
- getARMUndefinedRelativeWeakVA
- getAArch64UndefinedRelativeWeakVA
- getRISCVUndefinedRelativeWeakVA
- getARMStaticBase
- getRISCVPCRelHi20
- getTlsTpOffset
- getRelocTargetVA
- relocateNonAlloc
- relocate
- switchMorestackCallsToMorestackNonSplit
- enclosingPrologueAttempted
- adjustSplitStackFunctionPrologues
- writeTo
- replace
- EhInputSection
- getParent
- split
- split
- getParentOffset
- findNull
- splitStrings
- splitNonStrings
- MergeInputSection
- MergeInputSection
- splitIntoPieces
- getSectionPiece
Improve your Profiling and Debugging skills
Find out more