1//===- InputFiles.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 "InputFiles.h"
10#include "Config.h"
11#include "DWARF.h"
12#include "Driver.h"
13#include "InputSection.h"
14#include "LinkerScript.h"
15#include "SymbolTable.h"
16#include "Symbols.h"
17#include "SyntheticSections.h"
18#include "Target.h"
19#include "lld/Common/DWARF.h"
20#include "llvm/ADT/CachedHashString.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/LTO/LTO.h"
23#include "llvm/Object/IRObjectFile.h"
24#include "llvm/Support/AArch64AttributeParser.h"
25#include "llvm/Support/ARMAttributeParser.h"
26#include "llvm/Support/ARMBuildAttributes.h"
27#include "llvm/Support/Endian.h"
28#include "llvm/Support/FileSystem.h"
29#include "llvm/Support/Parallel.h"
30#include "llvm/Support/Path.h"
31#include "llvm/Support/TimeProfiler.h"
32#include "llvm/Support/raw_ostream.h"
33#include <optional>
34#include <thread>
35
36using namespace llvm;
37using namespace llvm::ELF;
38using namespace llvm::object;
39using namespace llvm::sys;
40using namespace llvm::sys::fs;
41using namespace llvm::support::endian;
42using namespace lld;
43using namespace lld::elf;
44
45// This function is explicitly instantiated in ARM.cpp, don't do it here to
46// avoid warnings with MSVC.
47extern template void ObjFile<ELF32LE>::importCmseSymbols();
48extern template void ObjFile<ELF32BE>::importCmseSymbols();
49extern template void ObjFile<ELF64LE>::importCmseSymbols();
50extern template void ObjFile<ELF64BE>::importCmseSymbols();
51
52// Returns "<internal>", "foo.a(bar.o)" or "baz.o".
53std::string elf::toStr(Ctx &ctx, const InputFile *f) {
54 static std::mutex mu;
55 if (!f)
56 return "<internal>";
57
58 {
59 std::lock_guard<std::mutex> lock(mu);
60 if (f->toStringCache.empty()) {
61 if (f->archiveName.empty())
62 f->toStringCache = f->getName();
63 else
64 (f->archiveName + "(" + f->getName() + ")").toVector(Out&: f->toStringCache);
65 }
66 }
67 return std::string(f->toStringCache);
68}
69
70const ELFSyncStream &elf::operator<<(const ELFSyncStream &s,
71 const InputFile *f) {
72 return s << toStr(ctx&: s.ctx, f);
73}
74
75static ELFKind getELFKind(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName) {
76 unsigned char size;
77 unsigned char endian;
78 std::tie(args&: size, args&: endian) = getElfArchType(Object: mb.getBuffer());
79
80 auto report = [&](StringRef msg) {
81 StringRef filename = mb.getBufferIdentifier();
82 if (archiveName.empty())
83 Fatal(ctx) << filename << ": " << msg;
84 else
85 Fatal(ctx) << archiveName << "(" << filename << "): " << msg;
86 };
87
88 if (!mb.getBuffer().starts_with(Prefix: ElfMagic))
89 report("not an ELF file");
90 if (endian != ELFDATA2LSB && endian != ELFDATA2MSB)
91 report("corrupted ELF file: invalid data encoding");
92 if (size != ELFCLASS32 && size != ELFCLASS64)
93 report("corrupted ELF file: invalid file class");
94
95 size_t bufSize = mb.getBuffer().size();
96 if ((size == ELFCLASS32 && bufSize < sizeof(Elf32_Ehdr)) ||
97 (size == ELFCLASS64 && bufSize < sizeof(Elf64_Ehdr)))
98 report("corrupted ELF file: file is too short");
99
100 if (size == ELFCLASS32)
101 return (endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind;
102 return (endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind;
103}
104
105// For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD
106// flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how
107// the input objects have been compiled.
108static void updateARMVFPArgs(Ctx &ctx, const ARMAttributeParser &attributes,
109 const InputFile *f) {
110 std::optional<unsigned> attr =
111 attributes.getAttributeValue(tag: ARMBuildAttrs::ABI_VFP_args);
112 if (!attr)
113 // If an ABI tag isn't present then it is implicitly given the value of 0
114 // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files,
115 // including some in glibc that don't use FP args (and should have value 3)
116 // don't have the attribute so we do not consider an implicit value of 0
117 // as a clash.
118 return;
119
120 unsigned vfpArgs = *attr;
121 ARMVFPArgKind arg;
122 switch (vfpArgs) {
123 case ARMBuildAttrs::BaseAAPCS:
124 arg = ARMVFPArgKind::Base;
125 break;
126 case ARMBuildAttrs::HardFPAAPCS:
127 arg = ARMVFPArgKind::VFP;
128 break;
129 case ARMBuildAttrs::ToolChainFPPCS:
130 // Tool chain specific convention that conforms to neither AAPCS variant.
131 arg = ARMVFPArgKind::ToolChain;
132 break;
133 case ARMBuildAttrs::CompatibleFPAAPCS:
134 // Object compatible with all conventions.
135 return;
136 default:
137 ErrAlways(ctx) << f << ": unknown Tag_ABI_VFP_args value: " << vfpArgs;
138 return;
139 }
140 // Follow ld.bfd and error if there is a mix of calling conventions.
141 if (ctx.arg.armVFPArgs != arg && ctx.arg.armVFPArgs != ARMVFPArgKind::Default)
142 ErrAlways(ctx) << f << ": incompatible Tag_ABI_VFP_args";
143 else
144 ctx.arg.armVFPArgs = arg;
145}
146
147// The ARM support in lld makes some use of instructions that are not available
148// on all ARM architectures. Namely:
149// - Use of BLX instruction for interworking between ARM and Thumb state.
150// - Use of the extended Thumb branch encoding in relocation.
151// - Use of the MOVT/MOVW instructions in Thumb Thunks.
152// The ARM Attributes section contains information about the architecture chosen
153// at compile time. We follow the convention that if at least one input object
154// is compiled with an architecture that supports these features then lld is
155// permitted to use them.
156static void updateSupportedARMFeatures(Ctx &ctx,
157 const ARMAttributeParser &attributes) {
158 std::optional<unsigned> attr =
159 attributes.getAttributeValue(tag: ARMBuildAttrs::CPU_arch);
160 if (!attr)
161 return;
162 auto arch = *attr;
163 switch (arch) {
164 case ARMBuildAttrs::Pre_v4:
165 case ARMBuildAttrs::v4:
166 case ARMBuildAttrs::v4T:
167 // Architectures prior to v5 do not support BLX instruction
168 break;
169 case ARMBuildAttrs::v5T:
170 case ARMBuildAttrs::v5TE:
171 case ARMBuildAttrs::v5TEJ:
172 case ARMBuildAttrs::v6:
173 case ARMBuildAttrs::v6KZ:
174 case ARMBuildAttrs::v6K:
175 ctx.arg.armHasBlx = true;
176 // Architectures used in pre-Cortex processors do not support
177 // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception
178 // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do.
179 break;
180 default:
181 // All other Architectures have BLX and extended branch encoding
182 ctx.arg.armHasBlx = true;
183 ctx.arg.armJ1J2BranchEncoding = true;
184 if (arch != ARMBuildAttrs::v6_M && arch != ARMBuildAttrs::v6S_M)
185 // All Architectures used in Cortex processors with the exception
186 // of v6-M and v6S-M have the MOVT and MOVW instructions.
187 ctx.arg.armHasMovtMovw = true;
188 break;
189 }
190
191 // Only ARMv8-M or later architectures have CMSE support.
192 std::optional<unsigned> profile =
193 attributes.getAttributeValue(tag: ARMBuildAttrs::CPU_arch_profile);
194 if (!profile)
195 return;
196 if (arch >= ARMBuildAttrs::CPUArch::v8_M_Base &&
197 profile == ARMBuildAttrs::MicroControllerProfile)
198 ctx.arg.armCMSESupport = true;
199
200 // The thumb PLT entries require Thumb2 which can be used on multiple archs.
201 // For now, let's limit it to ones where ARM isn't available and we know have
202 // Thumb2.
203 std::optional<unsigned> armISA =
204 attributes.getAttributeValue(tag: ARMBuildAttrs::ARM_ISA_use);
205 std::optional<unsigned> thumb =
206 attributes.getAttributeValue(tag: ARMBuildAttrs::THUMB_ISA_use);
207 ctx.arg.armHasArmISA |= armISA && *armISA >= ARMBuildAttrs::Allowed;
208 ctx.arg.armHasThumb2ISA |= thumb && *thumb >= ARMBuildAttrs::AllowThumb32;
209}
210
211InputFile::InputFile(Ctx &ctx, Kind k, MemoryBufferRef m)
212 : ctx(ctx), mb(m), fileKind(k) {}
213
214InputFile::~InputFile() {}
215
216std::optional<MemoryBufferRef> elf::readFile(Ctx &ctx, StringRef path) {
217 llvm::TimeTraceScope timeScope("Load input files", path);
218
219 // The --chroot option changes our virtual root directory.
220 // This is useful when you are dealing with files created by --reproduce.
221 if (!ctx.arg.chroot.empty() && path.starts_with(Prefix: "/"))
222 path = ctx.saver.save(S: ctx.arg.chroot + path);
223
224 bool remapped = false;
225 auto it = ctx.arg.remapInputs.find(Val: path);
226 if (it != ctx.arg.remapInputs.end()) {
227 path = it->second;
228 remapped = true;
229 } else {
230 for (const auto &[pat, toFile] : ctx.arg.remapInputsWildcards) {
231 if (pat.match(S: path)) {
232 path = toFile;
233 remapped = true;
234 break;
235 }
236 }
237 }
238 if (remapped) {
239 // Use /dev/null to indicate an input file that should be ignored. Change
240 // the path to NUL on Windows.
241#ifdef _WIN32
242 if (path == "/dev/null")
243 path = "NUL";
244#endif
245 }
246
247 Log(ctx) << path;
248 ctx.arg.dependencyFiles.insert(X: llvm::CachedHashString(path));
249
250 auto mbOrErr = MemoryBuffer::getFile(Filename: path, /*IsText=*/false,
251 /*RequiresNullTerminator=*/false);
252 if (auto ec = mbOrErr.getError()) {
253 ErrAlways(ctx) << "cannot open " << path << ": " << ec.message();
254 return std::nullopt;
255 }
256
257 MemoryBufferRef mbref = (*mbOrErr)->getMemBufferRef();
258 ctx.memoryBuffers.push_back(Elt: std::move(*mbOrErr)); // take MB ownership
259
260 if (ctx.tar)
261 ctx.tar->append(Path: relativeToRoot(path), Data: mbref.getBuffer());
262 return mbref;
263}
264
265// All input object files must be for the same architecture
266// (e.g. it does not make sense to link x86 object files with
267// MIPS object files.) This function checks for that error.
268static bool isCompatible(Ctx &ctx, InputFile *file) {
269 if (!file->isElf() && !isa<BitcodeFile>(Val: file))
270 return true;
271
272 if (file->ekind == ctx.arg.ekind && file->emachine == ctx.arg.emachine) {
273 if (ctx.arg.emachine != EM_MIPS)
274 return true;
275 if (isMipsN32Abi(ctx, f: *file) == ctx.arg.mipsN32Abi)
276 return true;
277 }
278
279 StringRef target =
280 !ctx.arg.bfdname.empty() ? ctx.arg.bfdname : ctx.arg.emulation;
281 if (!target.empty()) {
282 Err(ctx) << file << " is incompatible with " << target;
283 return false;
284 }
285
286 InputFile *existing = nullptr;
287 if (!ctx.objectFiles.empty())
288 existing = ctx.objectFiles[0];
289 else if (!ctx.sharedFiles.empty())
290 existing = ctx.sharedFiles[0];
291 else if (!ctx.bitcodeFiles.empty())
292 existing = ctx.bitcodeFiles[0];
293 auto diag = Err(ctx);
294 diag << file << " is incompatible";
295 if (existing)
296 diag << " with " << existing;
297 return false;
298}
299
300template <class ELFT> static void doParseFile(Ctx &ctx, InputFile *file) {
301 if (!isCompatible(ctx, file))
302 return;
303
304 // Lazy object file
305 if (file->lazy) {
306 if (auto *f = dyn_cast<BitcodeFile>(Val: file)) {
307 ctx.lazyBitcodeFiles.push_back(Elt: f);
308 f->parseLazy();
309 } else {
310 cast<ObjFile<ELFT>>(file)->parseLazy();
311 }
312 return;
313 }
314
315 if (ctx.arg.trace)
316 Msg(ctx) << file;
317
318 if (file->kind() == InputFile::ObjKind) {
319 ctx.objectFiles.push_back(Elt: cast<ELFFileBase>(Val: file));
320 cast<ObjFile<ELFT>>(file)->parse();
321 } else if (auto *f = dyn_cast<SharedFile>(Val: file)) {
322 f->parse<ELFT>();
323 } else if (auto *f = dyn_cast<BitcodeFile>(Val: file)) {
324 ctx.bitcodeFiles.push_back(Elt: f);
325 f->parse();
326 } else {
327 ctx.binaryFiles.push_back(Elt: cast<BinaryFile>(Val: file));
328 cast<BinaryFile>(Val: file)->parse();
329 }
330}
331
332// Add symbols in File to the symbol table.
333void elf::parseFile(Ctx &ctx, InputFile *file) {
334 invokeELFT(doParseFile, ctx, file);
335}
336
337// This function is explicitly instantiated in ARM.cpp. Mark it extern here,
338// to avoid warnings when building with MSVC.
339extern template void ObjFile<ELF32LE>::importCmseSymbols();
340extern template void ObjFile<ELF32BE>::importCmseSymbols();
341extern template void ObjFile<ELF64LE>::importCmseSymbols();
342extern template void ObjFile<ELF64BE>::importCmseSymbols();
343
344template <class ELFT>
345static void
346doParseFiles(Ctx &ctx,
347 const SmallVector<std::unique_ptr<InputFile>, 0> &files) {
348 // Use worker threads to intern global symbol names while the doParseFile loop
349 // below parses files. The workers use a copy of files as doParseFile may add
350 // additional files. If there is a Fatal error in doParseFile then control
351 // flow longjmps out of this frame while the worker threads are still running,
352 // so their state remains on the heap and leaks along with ctx.
353 struct InternQueue {
354 SmallVector<ObjFile<ELFT> *, 0> files;
355 std::atomic<size_t> next = 0;
356 parallel::TaskGroup tg;
357 };
358 auto queue = std::make_unique<InternQueue>();
359 if (ctx.arg.threadCount > 1) {
360 for (const std::unique_ptr<InputFile> &f : files) {
361 if (f->kind() == InputFile::ObjKind && f->ekind == ctx.arg.ekind)
362 queue->files.push_back(cast<ObjFile<ELFT>>(f.get()));
363 }
364 }
365 ctx.symtab->concurrent = !queue->files.empty();
366 for (size_t i = 1; i < ctx.arg.threadCount; ++i)
367 queue->tg.spawn([q = queue.get()] {
368 size_t j;
369 while ((j = q->next.fetch_add(1, std::memory_order_relaxed)) <
370 q->files.size())
371 q->files[j]->internSymbols();
372 });
373
374 // Add all files to the symbol table. This will add almost all symbols that we
375 // need to the symbol table. This process might add files to the link due to
376 // addDependentLibrary.
377 for (size_t i = 0, j = 0; i < files.size(); ++i) {
378 llvm::TimeTraceScope timeScope("Parse input files", files[i]->getName());
379 // Claim the file unless a worker has claimed it. If one has, wait for it.
380 if (j < queue->files.size() && files[i].get() == queue->files[j]) {
381 size_t expected = j;
382 if (!queue->next.compare_exchange_strong(expected, j + 1,
383 std::memory_order_relaxed))
384 while (!queue->files[j]->interned)
385 std::this_thread::yield();
386 ++j;
387 }
388 doParseFile<ELFT>(ctx, files[i].get());
389 }
390 queue.reset();
391 ctx.symtab->concurrent = false;
392 if (ctx.driver.armCmseImpLib)
393 cast<ObjFile<ELFT>>(*ctx.driver.armCmseImpLib).importCmseSymbols();
394}
395
396void elf::parseFiles(Ctx &ctx,
397 const SmallVector<std::unique_ptr<InputFile>, 0> &files) {
398 llvm::TimeTraceScope timeScope("Parse input files");
399 invokeELFT(doParseFiles, ctx, files);
400}
401
402// Concatenates arguments to construct a string representing an error location.
403StringRef InputFile::getNameForScript() const {
404 if (archiveName.empty())
405 return getName();
406
407 if (nameForScriptCache.empty())
408 nameForScriptCache = (archiveName + Twine(':') + getName()).str();
409
410 return nameForScriptCache;
411}
412
413// An ELF object file may contain a `.deplibs` section. If it exists, the
414// section contains a list of library specifiers such as `m` for libm. This
415// function resolves a given name by finding the first matching library checking
416// the various ways that a library can be specified to LLD. This ELF extension
417// is a form of autolinking and is called `dependent libraries`. It is currently
418// unique to LLVM and lld.
419static void addDependentLibrary(Ctx &ctx, StringRef specifier,
420 const InputFile *f) {
421 if (!ctx.arg.dependentLibraries)
422 return;
423 if (std::optional<std::string> s = searchLibraryBaseName(ctx, path: specifier))
424 ctx.driver.addFile(path: ctx.saver.save(S: *s), /*withLOption=*/true);
425 else if (std::optional<std::string> s = findFromSearchPaths(ctx, path: specifier))
426 ctx.driver.addFile(path: ctx.saver.save(S: *s), /*withLOption=*/true);
427 else if (fs::exists(Path: specifier))
428 ctx.driver.addFile(path: specifier, /*withLOption=*/false);
429 else
430 ErrAlways(ctx)
431 << f << ": unable to find library from dependent library specifier: "
432 << specifier;
433}
434
435// Record the membership of a section group so that in the garbage collection
436// pass, section group members are kept or discarded as a unit.
437template <class ELFT>
438static void handleSectionGroup(ArrayRef<InputSectionBase *> sections,
439 ArrayRef<typename ELFT::Word> entries) {
440 bool hasAlloc = false;
441 for (uint32_t index : entries.slice(1)) {
442 if (index >= sections.size())
443 return;
444 if (InputSectionBase *s = sections[index])
445 if (s != &InputSection::discarded && s->flags & SHF_ALLOC)
446 hasAlloc = true;
447 }
448
449 // If any member has the SHF_ALLOC flag, the whole group is subject to garbage
450 // collection. See the comment in markLive(). This rule retains .debug_types
451 // and .rela.debug_types.
452 if (!hasAlloc)
453 return;
454
455 // Connect the members in a circular doubly-linked list via
456 // nextInSectionGroup.
457 InputSectionBase *head;
458 InputSectionBase *prev = nullptr;
459 for (uint32_t index : entries.slice(1)) {
460 InputSectionBase *s = sections[index];
461 if (!s || s == &InputSection::discarded)
462 continue;
463 if (prev)
464 prev->nextInSectionGroup = s;
465 else
466 head = s;
467 prev = s;
468 }
469 if (prev)
470 prev->nextInSectionGroup = head;
471}
472
473template <class ELFT> void ObjFile<ELFT>::initDwarf() {
474 dwarf = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>(
475 std::make_unique<LLDDwarfObj<ELFT>>(this), "",
476 [&](Error err) { Warn(ctx) << getName() + ": " << std::move(err); },
477 [&](Error warning) {
478 Warn(ctx) << getName() << ": " << std::move(warning);
479 }));
480}
481
482DWARFCache *ELFFileBase::getDwarf() {
483 assert(fileKind == ObjKind);
484 llvm::call_once(flag&: initDwarf, F: [this]() {
485 switch (ekind) {
486 default:
487 llvm_unreachable("");
488 case ELF32LEKind:
489 return cast<ObjFile<ELF32LE>>(Val: this)->initDwarf();
490 case ELF32BEKind:
491 return cast<ObjFile<ELF32BE>>(Val: this)->initDwarf();
492 case ELF64LEKind:
493 return cast<ObjFile<ELF64LE>>(Val: this)->initDwarf();
494 case ELF64BEKind:
495 return cast<ObjFile<ELF64BE>>(Val: this)->initDwarf();
496 }
497 });
498 return dwarf.get();
499}
500
501ELFFileBase::ELFFileBase(Ctx &ctx, Kind k, ELFKind ekind, MemoryBufferRef mb)
502 : InputFile(ctx, k, mb) {
503 this->ekind = ekind;
504}
505
506ELFFileBase::~ELFFileBase() {}
507
508template <typename Elf_Shdr>
509static const Elf_Shdr *findSection(ArrayRef<Elf_Shdr> sections, uint32_t type) {
510 for (const Elf_Shdr &sec : sections)
511 if (sec.sh_type == type)
512 return &sec;
513 return nullptr;
514}
515
516void ELFFileBase::init() {
517 switch (ekind) {
518 case ELF32LEKind:
519 init<ELF32LE>(k: fileKind);
520 break;
521 case ELF32BEKind:
522 init<ELF32BE>(k: fileKind);
523 break;
524 case ELF64LEKind:
525 init<ELF64LE>(k: fileKind);
526 break;
527 case ELF64BEKind:
528 init<ELF64BE>(k: fileKind);
529 break;
530 default:
531 llvm_unreachable("getELFKind");
532 }
533}
534
535template <class ELFT> void ELFFileBase::init(InputFile::Kind k) {
536 using Elf_Shdr = typename ELFT::Shdr;
537 using Elf_Sym = typename ELFT::Sym;
538
539 // Initialize trivial attributes.
540 const ELFFile<ELFT> &obj = getObj<ELFT>();
541 emachine = obj.getHeader().e_machine;
542 osabi = obj.getHeader().e_ident[llvm::ELF::EI_OSABI];
543 abiVersion = obj.getHeader().e_ident[llvm::ELF::EI_ABIVERSION];
544
545 ArrayRef<Elf_Shdr> sections = CHECK2(obj.sections(), this);
546 elfShdrs = sections.data();
547 numELFShdrs = sections.size();
548
549 // Find a symbol table.
550 const Elf_Shdr *symtabSec =
551 findSection(sections, k == SharedKind ? SHT_DYNSYM : SHT_SYMTAB);
552
553 if (!symtabSec)
554 return;
555
556 // Initialize members corresponding to a symbol table.
557 firstGlobal = symtabSec->sh_info;
558
559 ArrayRef<Elf_Sym> eSyms = CHECK2(obj.symbols(symtabSec), this);
560 if (firstGlobal == 0 || firstGlobal > eSyms.size())
561 Fatal(ctx) << this << ": invalid sh_info in symbol table";
562
563 elfSyms = reinterpret_cast<const void *>(eSyms.data());
564 numSymbols = eSyms.size();
565 stringTable = CHECK2(obj.getStringTableForSymtab(*symtabSec, sections), this);
566}
567
568template <class ELFT> void ObjFile<ELFT>::internSymbols() {
569 assert(!interned);
570 // Leave names with '@' and errors to the serial parse loop.
571 ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
572 symbols = std::make_unique<Symbol *[]>(numSymbols);
573 for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {
574 Expected<StringRef> name = eSyms[i].getName(stringTable);
575 if (!name)
576 consumeError(Err: name.takeError());
577 else if (!name->contains(C: '@'))
578 symbols[i] = ctx.symtab->intern(name: CachedHashStringRef(*name));
579 }
580 interned = true;
581}
582
583template <class ELFT>
584uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &sym) const {
585 return CHECK2(
586 this->getObj().getSectionIndex(sym, getELFSyms<ELFT>(), shndxTable),
587 this);
588}
589
590template <class ELFT>
591static void
592handleAArch64BAAndGnuProperties(ObjFile<ELFT> *file, Ctx &ctx,
593 const AArch64BuildAttrSubsections &baInfo) {
594 // Missing subsections have zero-initialized data fields, so we must check
595 // presence before comparing against GNU properties.
596 bool baPauthInfoPresent = baInfo.Pauth.TagPlatform || baInfo.Pauth.TagSchema;
597
598 if (file->aarch64PauthAbiCoreInfo) {
599 // Check for data mismatch.
600 if (baPauthInfoPresent &&
601 (baInfo.Pauth.TagPlatform != file->aarch64PauthAbiCoreInfo->platform ||
602 baInfo.Pauth.TagSchema != file->aarch64PauthAbiCoreInfo->version))
603 Err(ctx) << file
604 << " GNU properties and build attributes have conflicting "
605 "AArch64 PAuth data";
606 if (baInfo.AndFeatures && baInfo.AndFeatures != file->andFeatures)
607 Err(ctx) << file
608 << " GNU properties and build attributes have conflicting "
609 "AArch64 PAuth data";
610 } else {
611 // When BuildAttributes are missing, PauthABI value defaults to (TagPlatform
612 // = 0, TagSchema = 0). GNU properties do not write PAuthAbiCoreInfo if GNU
613 // property is not present. To match this behaviour, we only write
614 // PAuthAbiCoreInfo when there is at least one non-zero value. The
615 // specification reserves TagPlatform = 0, TagSchema = 1 values to match the
616 // 'Invalid' GNU property section with platform = 0, version = 0.
617 if (baPauthInfoPresent) {
618 if (baInfo.Pauth.TagPlatform == 0 && baInfo.Pauth.TagSchema == 1)
619 file->aarch64PauthAbiCoreInfo = {0, 0};
620 else
621 file->aarch64PauthAbiCoreInfo = {baInfo.Pauth.TagPlatform,
622 baInfo.Pauth.TagSchema};
623 }
624 file->andFeatures |= baInfo.AndFeatures;
625 }
626}
627
628template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
629 object::ELFFile<ELFT> obj = this->getObj();
630 // Read a section table. justSymbols is usually false.
631 if (this->justSymbols) {
632 initializeJustSymbols();
633 initializeSymbols(obj);
634 return;
635 }
636
637 // Handle dependent libraries and selection of section groups as these are not
638 // done in parallel.
639 ArrayRef<Elf_Shdr> objSections = getELFShdrs<ELFT>();
640 StringRef shstrtab = CHECK2(obj.getSectionStringTable(objSections), this);
641 uint64_t size = objSections.size();
642 sections.resize(size);
643 for (size_t i = 0; i != size; ++i) {
644 const Elf_Shdr &sec = objSections[i];
645
646 if (LLVM_LIKELY(sec.sh_type == SHT_PROGBITS))
647 continue;
648 if (LLVM_LIKELY(sec.sh_type == SHT_GROUP)) {
649 StringRef signature = getShtGroupSignature(sections: objSections, sec);
650 ArrayRef<Elf_Word> entries =
651 CHECK2(obj.template getSectionContentsAsArray<Elf_Word>(sec), this);
652 if (entries.empty())
653 Fatal(ctx) << this << ": empty SHT_GROUP";
654
655 Elf_Word flag = entries[0];
656 if (flag && flag != GRP_COMDAT)
657 Fatal(ctx) << this << ": unsupported SHT_GROUP format";
658
659 bool keepGroup = !flag || ignoreComdats ||
660 ctx.symtab->comdatGroups
661 .try_emplace(CachedHashStringRef(signature), this)
662 .second;
663 if (keepGroup) {
664 keptGroups.push_back(Elt: i);
665 if (!ctx.arg.resolveGroups)
666 sections[i] = makeThreadLocal<InputSection>(
667 *this, sec, check(obj.getSectionName(sec, shstrtab)));
668 } else {
669 // Otherwise, discard group members.
670 for (uint32_t secIndex : entries.slice(1)) {
671 if (secIndex >= size)
672 Fatal(ctx) << this
673 << ": invalid section index in group: " << secIndex;
674 sections[secIndex] = &InputSection::discarded;
675 }
676 }
677 continue;
678 }
679
680 if (sec.sh_type == SHT_LLVM_DEPENDENT_LIBRARIES && !ctx.arg.relocatable) {
681 StringRef name = check(obj.getSectionName(sec, shstrtab));
682 ArrayRef<char> data = CHECK2(
683 this->getObj().template getSectionContentsAsArray<char>(sec), this);
684 if (!data.empty() && data.back() != '\0') {
685 Err(ctx)
686 << this
687 << ": corrupted dependent libraries section (unterminated string): "
688 << name;
689 } else {
690 for (const char *d = data.begin(), *e = data.end(); d < e;) {
691 StringRef s(d);
692 addDependentLibrary(ctx, s, this);
693 d += s.size() + 1;
694 }
695 }
696 sections[i] = &InputSection::discarded;
697 continue;
698 }
699
700 if (sec.sh_type == SHT_LLVM_DYNDBG_ELF) {
701 if (check(obj.getSectionName(sec, shstrtab)) == dynDbgSecName) {
702 sections[i] = &InputSection::discarded;
703 dynDbgSec = std::make_unique<InputSection>(*this, sec, dynDbgSecName);
704 ctx.hasDynDbg = true;
705 }
706 continue;
707 }
708
709 switch (ctx.arg.emachine) {
710 case EM_ARM:
711 if (sec.sh_type == SHT_ARM_ATTRIBUTES) {
712 ARMAttributeParser attributes;
713 ArrayRef<uint8_t> contents =
714 check(this->getObj().getSectionContents(sec));
715 StringRef name = check(obj.getSectionName(sec, shstrtab));
716 sections[i] = &InputSection::discarded;
717 if (Error e = attributes.parse(section: contents, endian: ekind == ELF32LEKind
718 ? llvm::endianness::little
719 : llvm::endianness::big)) {
720 InputSection isec(*this, sec, name);
721 Warn(ctx) << &isec << ": " << std::move(e);
722 } else {
723 updateSupportedARMFeatures(ctx, attributes);
724 updateARMVFPArgs(ctx, attributes, this);
725
726 // FIXME: Retain the first attribute section we see. The eglibc ARM
727 // dynamic loaders require the presence of an attribute section for
728 // dlopen to work. In a full implementation we would merge all
729 // attribute sections.
730 if (ctx.in.attributes == nullptr) {
731 ctx.in.attributes =
732 std::make_unique<InputSection>(*this, sec, name);
733 sections[i] = ctx.in.attributes.get();
734 }
735 }
736 }
737 break;
738 case EM_AARCH64:
739 // Producing a static binary with MTE globals is not currently supported,
740 // remove all SHT_AARCH64_MEMTAG_GLOBALS_STATIC sections as they're unused
741 // medatada, and we don't want them to end up in the output file for
742 // static executables.
743 if (sec.sh_type == SHT_AARCH64_MEMTAG_GLOBALS_STATIC &&
744 !canHaveMemtagGlobals(ctx))
745 sections[i] = &InputSection::discarded;
746 break;
747 }
748 }
749
750 // Read a symbol table.
751 initializeSymbols(obj);
752}
753
754// Sections with SHT_GROUP and comdat bits define comdat section groups.
755// They are identified and deduplicated by group name. This function
756// returns a group name.
757template <class ELFT>
758StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> sections,
759 const Elf_Shdr &sec) {
760 typename ELFT::SymRange symbols = this->getELFSyms<ELFT>();
761 if (sec.sh_info >= symbols.size())
762 Fatal(ctx) << this << ": invalid symbol index";
763 const typename ELFT::Sym &sym = symbols[sec.sh_info];
764 return CHECK2(sym.getName(this->stringTable), this);
765}
766
767template <class ELFT>
768bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) {
769 // On a regular link we don't merge sections if -O0 (default is -O1). This
770 // sometimes makes the linker significantly faster, although the output will
771 // be bigger.
772 //
773 // Doing the same for -r would create a problem as it would combine sections
774 // with different sh_entsize. One option would be to just copy every SHF_MERGE
775 // section as is to the output. While this would produce a valid ELF file with
776 // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when
777 // they see two .debug_str. We could have separate logic for combining
778 // SHF_MERGE sections based both on their name and sh_entsize, but that seems
779 // to be more trouble than it is worth. Instead, we just use the regular (-O1)
780 // logic for -r.
781 if (ctx.arg.optimize == 0 && !ctx.arg.relocatable)
782 return false;
783
784 // A mergeable section with size 0 is useless because they don't have
785 // any data to merge. A mergeable string section with size 0 can be
786 // argued as invalid because it doesn't end with a null character.
787 // We'll avoid a mess by handling them as if they were non-mergeable.
788 if (sec.sh_size == 0)
789 return false;
790
791 // Check for sh_entsize. The ELF spec is not clear about the zero
792 // sh_entsize. It says that "the member [sh_entsize] contains 0 if
793 // the section does not hold a table of fixed-size entries". We know
794 // that Rust 1.13 produces a string mergeable section with a zero
795 // sh_entsize. Here we just accept it rather than being picky about it.
796 uint64_t entSize = sec.sh_entsize;
797 if (entSize == 0)
798 return false;
799 if (sec.sh_size % entSize)
800 ErrAlways(ctx) << this << ":(" << name << "): SHF_MERGE section size ("
801 << uint64_t(sec.sh_size)
802 << ") must be a multiple of sh_entsize (" << entSize << ")";
803 if (sec.sh_flags & SHF_WRITE)
804 Err(ctx) << this << ":(" << name
805 << "): writable SHF_MERGE section is not supported";
806
807 return true;
808}
809
810// This is for --just-symbols.
811//
812// --just-symbols is a very minor feature that allows you to link your
813// output against other existing program, so that if you load both your
814// program and the other program into memory, your output can refer the
815// other program's symbols.
816//
817// When the option is given, we link "just symbols". The section table is
818// initialized with null pointers.
819template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() {
820 sections.resize(numELFShdrs);
821}
822
823static bool isKnownSpecificSectionType(uint32_t t, uint32_t flags) {
824 if (SHT_LOUSER <= t && t <= SHT_HIUSER && !(flags & SHF_ALLOC))
825 return true;
826 if (SHT_LOOS <= t && t <= SHT_HIOS && !(flags & SHF_OS_NONCONFORMING))
827 return true;
828 // Allow all processor-specific types. This is different from GNU ld.
829 return SHT_LOPROC <= t && t <= SHT_HIPROC;
830}
831
832template <class ELFT>
833void ObjFile<ELFT>::initializeSections(const llvm::object::ELFFile<ELFT> &obj) {
834 ArrayRef<Elf_Shdr> objSections = getELFShdrs<ELFT>();
835 StringRef shstrtab = CHECK2(obj.getSectionStringTable(objSections), this);
836 uint64_t size = objSections.size();
837 SmallVector<ArrayRef<Elf_Word>, 0> selectedGroups;
838 ArrayRef<uint32_t> keptGroups = this->keptGroups;
839 size_t keptIdx = 0;
840 AArch64BuildAttrSubsections aarch64BAsubSections;
841 bool hasAArch64BuildAttributes = false;
842 for (size_t i = 0; i != size; ++i) {
843 if (this->sections[i] == &InputSection::discarded)
844 continue;
845 const Elf_Shdr &sec = objSections[i];
846 const uint32_t type = sec.sh_type;
847
848 // SHF_EXCLUDE'ed sections are discarded by the linker. However,
849 // if -r is given, we'll let the final link discard such sections.
850 // This is compatible with GNU.
851 if ((sec.sh_flags & SHF_EXCLUDE) && !ctx.arg.relocatable) {
852 if (type == SHT_LLVM_CALL_GRAPH_PROFILE)
853 cgProfileSectionIndex = i;
854 if (type == SHT_LLVM_ADDRSIG) {
855 // We ignore the address-significance table if we know that the object
856 // file was created by objcopy or ld -r. This is because these tools
857 // will reorder the symbols in the symbol table, invalidating the data
858 // in the address-significance table, which refers to symbols by index.
859 if (sec.sh_link != 0)
860 this->addrsigSec = &sec;
861 else if (ctx.arg.icf == ICFLevel::Safe)
862 Warn(ctx) << this
863 << ": --icf=safe conservatively ignores "
864 "SHT_LLVM_ADDRSIG [index "
865 << i
866 << "] with sh_link=0 "
867 "(likely created using objcopy or ld -r)";
868 }
869 this->sections[i] = &InputSection::discarded;
870 continue;
871 }
872
873 // Processor-specific types that do not use the following switch statement.
874 //
875 // Extract Build Attributes section contents into aarch64BAsubSections.
876 // Input objects may contain both build Build Attributes and GNU
877 // properties. We delay processing Build Attributes until we have finished
878 // reading all sections so that we can check that these are consistent.
879 if (type == SHT_AARCH64_ATTRIBUTES && ctx.arg.emachine == EM_AARCH64) {
880 ArrayRef<uint8_t> contents = check(obj.getSectionContents(sec));
881 AArch64AttributeParser attributes;
882 if (Error e = attributes.parse(Section: contents, Endian: ELFT::Endianness)) {
883 StringRef name = check(obj.getSectionName(sec, shstrtab));
884 InputSection isec(*this, sec, name);
885 Warn(ctx) << &isec << ": " << std::move(e);
886 } else {
887 aarch64BAsubSections = extractBuildAttributesSubsections(attributes);
888 hasAArch64BuildAttributes = true;
889 }
890 this->sections[i] = &InputSection::discarded;
891 continue;
892 }
893 switch (type) {
894 case SHT_GROUP: {
895 if (!ctx.arg.relocatable)
896 sections[i] = &InputSection::discarded;
897 // Use the verdict parse() recorded for this group instead of repeating
898 // the signature hashing and comdatGroups lookup.
899 while (keptIdx != keptGroups.size() && keptGroups[keptIdx] < i)
900 ++keptIdx;
901 if (keptIdx != keptGroups.size() && keptGroups[keptIdx] == i)
902 selectedGroups.push_back(
903 cantFail(obj.template getSectionContentsAsArray<Elf_Word>(sec)));
904 break;
905 }
906 case SHT_SYMTAB_SHNDX:
907 shndxTable = CHECK2(obj.getSHNDXTable(sec, objSections), this);
908 break;
909 case SHT_SYMTAB:
910 case SHT_STRTAB:
911 case SHT_REL:
912 case SHT_RELA:
913 case SHT_CREL:
914 case SHT_NULL:
915 break;
916 case SHT_PROGBITS:
917 case SHT_NOTE:
918 case SHT_NOBITS:
919 case SHT_INIT_ARRAY:
920 case SHT_FINI_ARRAY:
921 case SHT_PREINIT_ARRAY:
922 this->sections[i] =
923 createInputSection(idx: i, sec, name: check(obj.getSectionName(sec, shstrtab)));
924 break;
925 case SHT_LLVM_LTO:
926 // Discard .llvm.lto in a relocatable link that does not use the bitcode.
927 // The concatenated output does not properly reflect the linking
928 // semantics. In addition, since we do not use the bitcode wrapper format,
929 // the concatenated raw bitcode would be invalid.
930 if (ctx.arg.relocatable && !ctx.arg.fatLTOObjects) {
931 sections[i] = &InputSection::discarded;
932 break;
933 }
934 [[fallthrough]];
935 default:
936 this->sections[i] =
937 createInputSection(idx: i, sec, name: check(obj.getSectionName(sec, shstrtab)));
938 if (ctx.arg.rejectMismatch &&
939 !isKnownSpecificSectionType(type, sec.sh_flags))
940 Err(ctx) << this->sections[i] << ": unknown section type 0x"
941 << Twine::utohexstr(Val: type);
942 break;
943 }
944 }
945
946 // We have a second loop. It is used to:
947 // 1) handle SHF_LINK_ORDER sections.
948 // 2) create relocation sections. In some cases the section header index of a
949 // relocation section may be smaller than that of the relocated section. In
950 // such cases, the relocation section would attempt to reference a target
951 // section that has not yet been created. For simplicity, delay creation of
952 // relocation sections until now.
953 for (size_t i = 0; i != size; ++i) {
954 if (this->sections[i] == &InputSection::discarded)
955 continue;
956 const Elf_Shdr &sec = objSections[i];
957
958 if (isStaticRelSecType(sec.sh_type)) {
959 // Find a relocation target section and associate this section with that.
960 // Target may have been discarded if it is in a different section group
961 // and the group is discarded, even though it's a violation of the spec.
962 // We handle that situation gracefully by discarding dangling relocation
963 // sections.
964 const uint32_t info = sec.sh_info;
965 InputSectionBase *s = getRelocTarget(idx: i, info);
966 if (!s)
967 continue;
968
969 // ELF spec allows mergeable sections with relocations, but they are rare,
970 // and it is in practice hard to merge such sections by contents, because
971 // applying relocations at end of linking changes section contents. So, we
972 // simply handle such sections as non-mergeable ones. Degrading like this
973 // is acceptable because section merging is optional.
974 if (auto *ms = dyn_cast<MergeInputSection>(Val: s)) {
975 s = makeThreadLocal<InputSection>(args&: ms->file, args&: ms->name, args&: ms->type,
976 args&: ms->flags, args&: ms->addralign, args&: ms->entsize,
977 args: ms->contentMaybeDecompress());
978 sections[info] = s;
979 }
980
981 if (s->relSecIdx != 0)
982 ErrAlways(ctx) << s
983 << ": multiple relocation sections to one section are "
984 "not supported";
985 s->relSecIdx = i;
986
987 // Relocation sections are usually removed from the output, so return
988 // `nullptr` for the normal case. However, if -r or --emit-relocs is
989 // specified, we need to copy them to the output. (Some post link analysis
990 // tools specify --emit-relocs to obtain the information.)
991 if (ctx.arg.copyRelocs) {
992 auto *isec = makeThreadLocal<InputSection>(
993 *this, sec, check(obj.getSectionName(sec, shstrtab)));
994 // If the relocated section is discarded (due to /DISCARD/ or
995 // --gc-sections), the relocation section should be discarded as well.
996 s->dependentSections.push_back(NewVal: isec);
997 sections[i] = isec;
998 }
999 continue;
1000 }
1001
1002 // A SHF_LINK_ORDER section with sh_link=0 is handled as if it did not have
1003 // the flag.
1004 if (!sec.sh_link || !(sec.sh_flags & SHF_LINK_ORDER))
1005 continue;
1006
1007 InputSectionBase *linkSec = nullptr;
1008 if (sec.sh_link < size)
1009 linkSec = this->sections[sec.sh_link];
1010 if (!linkSec) {
1011 ErrAlways(ctx) << this
1012 << ": invalid sh_link index: " << uint32_t(sec.sh_link);
1013 continue;
1014 }
1015
1016 // A SHF_LINK_ORDER section is discarded if its linked-to section is
1017 // discarded.
1018 InputSection *isec = cast<InputSection>(this->sections[i]);
1019 linkSec->dependentSections.push_back(NewVal: isec);
1020 if (!isa<InputSection>(Val: linkSec))
1021 ErrAlways(ctx)
1022 << "a section " << isec->name
1023 << " with SHF_LINK_ORDER should not refer a non-regular section: "
1024 << linkSec;
1025 }
1026
1027 // Handle AArch64 Build Attributes and GNU properties:
1028 // - Err on mismatched values.
1029 // - Store missing values as GNU properties.
1030 if (hasAArch64BuildAttributes)
1031 handleAArch64BAAndGnuProperties<ELFT>(this, ctx, aarch64BAsubSections);
1032
1033 for (ArrayRef<Elf_Word> entries : selectedGroups)
1034 handleSectionGroup<ELFT>(this->sections, entries);
1035}
1036
1037template <typename ELFT>
1038static void parseGnuPropertyNote(Ctx &ctx, ELFFileBase &f,
1039 uint32_t featureAndType,
1040 ArrayRef<uint8_t> &desc, const uint8_t *base,
1041 ArrayRef<uint8_t> *data = nullptr) {
1042 auto err = [&](const uint8_t *place) -> ELFSyncStream {
1043 auto diag = Err(ctx);
1044 diag << &f << ":(" << ".note.gnu.property+0x"
1045 << Twine::utohexstr(Val: place - base) << "): ";
1046 return diag;
1047 };
1048
1049 while (!desc.empty()) {
1050 const uint8_t *place = desc.data();
1051 if (desc.size() < 8)
1052 return void(err(place) << "program property is too short");
1053 uint32_t type = read32<ELFT::Endianness>(desc.data());
1054 uint32_t size = read32<ELFT::Endianness>(desc.data() + 4);
1055 desc = desc.slice(N: 8);
1056 if (desc.size() < alignTo<(ELFT::Is64Bits ? 8 : 4)>(size))
1057 return void(err(place) << "program property is too short");
1058
1059 if (type == featureAndType) {
1060 // We found a FEATURE_1_AND field. There may be more than one of these
1061 // in a .note.gnu.property section, for a relocatable object we
1062 // accumulate the bits set.
1063 if (size < 4)
1064 return void(err(place) << "FEATURE_1_AND entry is too short");
1065 f.andFeatures |= read32<ELFT::Endianness>(desc.data());
1066 } else if (ctx.arg.emachine == EM_AARCH64 &&
1067 type == GNU_PROPERTY_AARCH64_FEATURE_PAUTH) {
1068 ArrayRef<uint8_t> contents = data ? *data : desc;
1069 if (f.aarch64PauthAbiCoreInfo) {
1070 return void(
1071 err(contents.data())
1072 << "multiple GNU_PROPERTY_AARCH64_FEATURE_PAUTH entries are "
1073 "not supported");
1074 } else if (size != 16) {
1075 return void(err(contents.data())
1076 << "GNU_PROPERTY_AARCH64_FEATURE_PAUTH entry "
1077 "is invalid: expected 16 bytes, but got "
1078 << size);
1079 }
1080 f.aarch64PauthAbiCoreInfo = {
1081 support::endian::read64<ELFT::Endianness>(&desc[0]),
1082 support::endian::read64<ELFT::Endianness>(&desc[8])};
1083 }
1084
1085 // Padding is present in the note descriptor, if necessary.
1086 desc = desc.slice(alignTo<(ELFT::Is64Bits ? 8 : 4)>(size));
1087 }
1088}
1089// Read the following info from the .note.gnu.property section and write it to
1090// the corresponding fields in `ObjFile`:
1091// - Feature flags (32 bits) representing x86, AArch64 or RISC-V features for
1092// hardware-assisted call flow control;
1093// - AArch64 PAuth ABI core info (16 bytes).
1094template <class ELFT>
1095static void readGnuProperty(Ctx &ctx, const InputSection &sec,
1096 ObjFile<ELFT> &f) {
1097 using Elf_Nhdr = typename ELFT::Nhdr;
1098 using Elf_Note = typename ELFT::Note;
1099
1100 uint32_t featureAndType;
1101 switch (ctx.arg.emachine) {
1102 case EM_386:
1103 case EM_X86_64:
1104 featureAndType = GNU_PROPERTY_X86_FEATURE_1_AND;
1105 break;
1106 case EM_AARCH64:
1107 featureAndType = GNU_PROPERTY_AARCH64_FEATURE_1_AND;
1108 break;
1109 case EM_RISCV:
1110 featureAndType = GNU_PROPERTY_RISCV_FEATURE_1_AND;
1111 break;
1112 default:
1113 return;
1114 }
1115
1116 ArrayRef<uint8_t> data = sec.content();
1117 auto err = [&](const uint8_t *place) -> ELFSyncStream {
1118 auto diag = Err(ctx);
1119 diag << sec.file << ":(" << sec.name << "+0x"
1120 << Twine::utohexstr(Val: place - sec.content().data()) << "): ";
1121 return diag;
1122 };
1123 while (!data.empty()) {
1124 // Read one NOTE record.
1125 auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data());
1126 if (data.size() < sizeof(Elf_Nhdr) ||
1127 data.size() < nhdr->getSize(sec.addralign))
1128 return void(err(data.data()) << "data is too short");
1129
1130 Elf_Note note(*nhdr);
1131 if (nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU") {
1132 data = data.slice(nhdr->getSize(sec.addralign));
1133 continue;
1134 }
1135
1136 // Read a body of a NOTE record, which consists of type-length-value fields.
1137 ArrayRef<uint8_t> desc = note.getDesc(sec.addralign);
1138 const uint8_t *base = sec.content().data();
1139 parseGnuPropertyNote<ELFT>(ctx, f, featureAndType, desc, base, &data);
1140
1141 // Go to next NOTE record to look for more FEATURE_1_AND descriptions.
1142 data = data.slice(nhdr->getSize(sec.addralign));
1143 }
1144}
1145
1146template <class ELFT>
1147InputSectionBase *ObjFile<ELFT>::getRelocTarget(uint32_t idx, uint32_t info) {
1148 if (info < this->sections.size()) {
1149 InputSectionBase *target = this->sections[info];
1150
1151 // Strictly speaking, a relocation section must be included in the
1152 // group of the section it relocates. However, LLVM 3.3 and earlier
1153 // would fail to do so, so we gracefully handle that case.
1154 if (target == &InputSection::discarded)
1155 return nullptr;
1156
1157 if (target != nullptr)
1158 return target;
1159 }
1160
1161 Err(ctx) << this << ": relocation section (index " << idx
1162 << ") has invalid sh_info (" << info << ')';
1163 return nullptr;
1164}
1165
1166// The function may be called concurrently for different input files. For
1167// allocation, prefer makeThreadLocal which does not require holding a lock.
1168template <class ELFT>
1169InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,
1170 const Elf_Shdr &sec,
1171 StringRef name) {
1172 if (name.starts_with(Prefix: ".n")) {
1173 // The GNU linker uses .note.GNU-stack section as a marker indicating
1174 // that the code in the object file does not expect that the stack is
1175 // executable (in terms of NX bit). If all input files have the marker,
1176 // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
1177 // make the stack non-executable. Most object files have this section as
1178 // of 2017.
1179 //
1180 // But making the stack non-executable is a norm today for security
1181 // reasons. Failure to do so may result in a serious security issue.
1182 // Therefore, we make LLD always add PT_GNU_STACK unless it is
1183 // explicitly told to do otherwise (by -z execstack). Because the stack
1184 // executable-ness is controlled solely by command line options,
1185 // .note.GNU-stack sections are, with one exception, ignored. Report
1186 // an error if we encounter an executable .note.GNU-stack to force the
1187 // user to explicitly request an executable stack.
1188 if (name == ".note.GNU-stack") {
1189 if ((sec.sh_flags & SHF_EXECINSTR) && !ctx.arg.relocatable &&
1190 ctx.arg.zGnustack != GnuStackKind::Exec) {
1191 Err(ctx) << this
1192 << ": requires an executable stack, but -z execstack is not "
1193 "specified";
1194 }
1195 return &InputSection::discarded;
1196 }
1197
1198 // Object files that use processor features such as Intel Control-Flow
1199 // Enforcement (CET), AArch64 Branch Target Identification BTI or RISC-V
1200 // Zicfilp/Zicfiss extensions, use a .note.gnu.property section containing
1201 // a bitfield of feature bits like the GNU_PROPERTY_X86_FEATURE_1_IBT flag.
1202 //
1203 // Since we merge bitmaps from multiple object files to create a new
1204 // .note.gnu.property containing a single AND'ed bitmap, we discard an input
1205 // file's .note.gnu.property section.
1206 if (name == ".note.gnu.property") {
1207 readGnuProperty<ELFT>(ctx, InputSection(*this, sec, name), *this);
1208 return &InputSection::discarded;
1209 }
1210
1211 // Split stacks is a feature to support a discontiguous stack,
1212 // commonly used in the programming language Go. For the details,
1213 // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled
1214 // for split stack will include a .note.GNU-split-stack section.
1215 if (name == ".note.GNU-split-stack") {
1216 if (ctx.arg.relocatable) {
1217 ErrAlways(ctx) << "cannot mix split-stack and non-split-stack in a "
1218 "relocatable link";
1219 return &InputSection::discarded;
1220 }
1221 this->splitStack = true;
1222 return &InputSection::discarded;
1223 }
1224
1225 // An object file compiled for split stack, but where some of the
1226 // functions were compiled with the no_split_stack_attribute will
1227 // include a .note.GNU-no-split-stack section.
1228 if (name == ".note.GNU-no-split-stack") {
1229 this->someNoSplitStack = true;
1230 return &InputSection::discarded;
1231 }
1232
1233 // Strip existing .note.gnu.build-id sections so that the output won't have
1234 // more than one build-id. This is not usually a problem because input
1235 // object files normally don't have .build-id sections, but you can create
1236 // such files by "ld.{bfd,gold,lld} -r --build-id", and we want to guard
1237 // against it.
1238 if (name == ".note.gnu.build-id")
1239 return &InputSection::discarded;
1240 }
1241
1242 // The linker merges EH (exception handling) frames and creates a
1243 // .eh_frame_hdr section for runtime. So we handle them with a special
1244 // class. For relocatable outputs, they are just passed through.
1245 if (name == ".eh_frame" && !ctx.arg.relocatable)
1246 return makeThreadLocal<EhInputSection>(*this, sec, name);
1247
1248 if ((sec.sh_flags & SHF_MERGE) && shouldMerge(sec, name))
1249 return makeThreadLocal<MergeInputSection>(*this, sec, name);
1250 return makeThreadLocal<InputSection>(*this, sec, name);
1251}
1252
1253template <class ELFT> Symbol *ObjFile<ELFT>::insertGlobal(size_t i) {
1254 if (symbols[i])
1255 return ctx.symtab->insert(symbols[i]);
1256 return ctx.symtab->insert(
1257 CHECK2(this->getELFSyms<ELFT>()[i].getName(stringTable), this));
1258}
1259
1260// Initialize symbols. symbols is a parallel array to the corresponding ELF
1261// symbol table.
1262template <class ELFT>
1263void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
1264 ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
1265 if (!symbols)
1266 symbols = std::make_unique<Symbol *[]>(numSymbols);
1267
1268 for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i)
1269 symbols[i] = insertGlobal(i);
1270
1271 // Perform symbol resolution on non-local symbols.
1272 SmallVector<unsigned, 32> undefineds;
1273 for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {
1274 const Elf_Sym &eSym = eSyms[i];
1275 uint32_t secIdx = eSym.st_shndx;
1276 if (secIdx == SHN_UNDEF) {
1277 undefineds.push_back(Elt: i);
1278 continue;
1279 }
1280
1281 uint8_t binding = eSym.getBinding();
1282 uint8_t stOther = eSym.st_other;
1283 uint8_t type = eSym.getType();
1284 uint64_t value = eSym.st_value;
1285 uint64_t size = eSym.st_size;
1286
1287 Symbol *sym = symbols[i];
1288 sym->isUsedInRegularObj = true;
1289 if (LLVM_UNLIKELY(eSym.st_shndx == SHN_COMMON)) {
1290 if (value == 0 || value >= UINT32_MAX)
1291 Err(ctx) << this << ": common symbol '" << sym->getName()
1292 << "' has invalid alignment: " << value;
1293 hasCommonSyms = true;
1294 sym->resolve(ctx, CommonSymbol{this, StringRef(), binding, stOther, type,
1295 value, size});
1296 continue;
1297 }
1298
1299 // Handle global defined symbols. Defined::section will be set in postParse.
1300 sym->resolve(ctx, Defined{this, StringRef(), binding, stOther, type, value,
1301 size, nullptr});
1302 }
1303
1304 // Undefined symbols (excluding those defined relative to non-prevailing
1305 // sections) can trigger recursive extract. Process defined symbols first so
1306 // that the relative order between a defined symbol and an undefined symbol
1307 // does not change the symbol resolution behavior. In addition, a set of
1308 // interconnected symbols will all be resolved to the same file, instead of
1309 // being resolved to different files.
1310 for (unsigned i : undefineds) {
1311 const Elf_Sym &eSym = eSyms[i];
1312 Symbol *sym = symbols[i];
1313 sym->resolve(ctx, Undefined{this, StringRef(), eSym.getBinding(),
1314 eSym.st_other, eSym.getType()});
1315 sym->isUsedInRegularObj = true;
1316 sym->referenced = true;
1317 }
1318
1319 if (dynDbgSec)
1320 initDynDbgSymbols();
1321}
1322
1323// Add the undefined symbols of the embedded unoptimized dynamic debugging
1324// object so that the outer link resolves the inner link's dependencies. Tag
1325// those reached by an inner relocation against a SHF_ALLOC section with
1326// `isDynDbgRef`; the rest are only needed by debug sections.
1327template <class ELFT> void ObjFile<ELFT>::initDynDbgSymbols() {
1328 MemoryBufferRef dbgMb(toStringRef(Input: dynDbgSec->contentMaybeDecompress()),
1329 mb.getBufferIdentifier());
1330 std::unique_ptr<ELFFileBase> efb = createObjFile(ctx, dbgMb);
1331 // Compare ekind (note ObjFile<ELFT>::classof only tests InputFile::kind()).
1332 if (efb->ekind != ekind) {
1333 Err(ctx) << this << ": " << dynDbgSecName
1334 << " contains an incompatible ELF type";
1335 return;
1336 }
1337 auto &dbgObj = cast<ObjFile<ELFT>>(*efb);
1338 const object::ELFFile<ELFT> obj = dbgObj.getObj();
1339
1340 ArrayRef<Elf_Sym> dbgSyms = dbgObj.template getGlobalELFSyms<ELFT>();
1341 SmallVector<bool, 0> globalUsed(dbgSyms.size());
1342 auto setSymUsed = [&, firstGlobal = dbgObj.firstGlobal](uint32_t symIdx) {
1343 if (symIdx >= firstGlobal)
1344 globalUsed[symIdx - firstGlobal] = true;
1345 };
1346
1347 for (const Elf_Shdr &sh : dbgObj.template getELFShdrs<ELFT>()) {
1348 if (!isStaticRelSecType(sh.sh_type))
1349 continue;
1350 const Elf_Shdr &target = *CHECK2(obj.getSection(sh.sh_info), &dbgObj);
1351 if (!(target.sh_flags & SHF_ALLOC))
1352 continue;
1353 if (sh.sh_type == SHT_CREL) {
1354 auto [rels, relas] = CHECK2(obj.crels(sh), &dbgObj);
1355 for (const Elf_Rel &r : rels)
1356 setSymUsed(r.getSymbol(false));
1357 for (const Elf_Rela &r : relas)
1358 setSymUsed(r.getSymbol(false));
1359 } else if (sh.sh_type == SHT_RELA) {
1360 for (const Elf_Rela &r : CHECK2(obj.relas(sh), &dbgObj))
1361 setSymUsed(r.getSymbol(ctx.arg.isMips64EL));
1362 } else {
1363 for (const Elf_Rel &r : CHECK2(obj.rels(sh), &dbgObj))
1364 setSymUsed(r.getSymbol(ctx.arg.isMips64EL));
1365 }
1366 }
1367
1368 for (size_t i = 0, end = dbgSyms.size(); i != end; ++i) {
1369 const Elf_Sym &s = dbgSyms[i];
1370 if (s.st_shndx != SHN_UNDEF)
1371 continue;
1372 StringRef name = CHECK2(s.getName(dbgObj.stringTable), this);
1373 Symbol *sym = ctx.symtab->addSymbol(
1374 newSym: Undefined{this, name, s.getBinding(), s.st_other, s.getType()});
1375 sym->isUsedInRegularObj = true;
1376 sym->referenced = true;
1377 if (globalUsed[i]) {
1378 sym->isDynDbgRef = true;
1379 if (sym->traced)
1380 Msg(ctx) << this << ": dynamic debugging reference to " << name;
1381 }
1382 }
1383}
1384
1385template <class ELFT> void ObjFile<ELFT>::initSectionsAndLocalSyms() {
1386 if (!justSymbols)
1387 initializeSections(obj: getObj());
1388
1389 if (!firstGlobal)
1390 return;
1391 SymbolUnion *locals = makeThreadLocalN<SymbolUnion>(firstGlobal);
1392
1393 ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
1394 for (size_t i = 0, end = firstGlobal; i != end; ++i) {
1395 const Elf_Sym &eSym = eSyms[i];
1396 uint32_t secIdx = eSym.st_shndx;
1397 if (LLVM_UNLIKELY(secIdx == SHN_XINDEX))
1398 secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable));
1399 else if (secIdx >= SHN_LORESERVE)
1400 secIdx = 0;
1401 if (LLVM_UNLIKELY(secIdx >= sections.size())) {
1402 Err(ctx) << this << ": invalid section index: " << secIdx;
1403 secIdx = 0;
1404 }
1405 if (LLVM_UNLIKELY(eSym.getBinding() != STB_LOCAL))
1406 ErrAlways(ctx) << this << ": non-local symbol (" << i
1407 << ") found at index < .symtab's sh_info (" << end << ")";
1408
1409 InputSectionBase *sec = sections[secIdx];
1410 uint8_t type = eSym.getType();
1411 if (type == STT_FILE)
1412 sourceFile = CHECK2(eSym.getName(stringTable), this);
1413 unsigned stName = eSym.st_name;
1414 if (LLVM_UNLIKELY(stringTable.size() <= stName)) {
1415 Err(ctx) << this << ": invalid symbol name offset";
1416 stName = 0;
1417 }
1418 StringRef name(stringTable.data() + stName);
1419
1420 symbols[i] = reinterpret_cast<Symbol *>(locals + i);
1421 if (eSym.st_shndx == SHN_UNDEF || sec == &InputSection::discarded)
1422 new (symbols[i]) Undefined(this, name, STB_LOCAL, eSym.st_other, type,
1423 /*discardedSecIdx=*/secIdx);
1424 else
1425 new (symbols[i]) Defined(this, name, STB_LOCAL, eSym.st_other, type,
1426 eSym.st_value, eSym.st_size, sec);
1427 symbols[i]->isUsedInRegularObj = true;
1428 }
1429}
1430
1431// Called after all ObjFile::parse is called for all ObjFiles. This checks
1432// duplicate symbols and may do symbol property merge in the future.
1433template <class ELFT> void ObjFile<ELFT>::postParse() {
1434 static std::mutex mu;
1435 ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
1436 for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {
1437 const Elf_Sym &eSym = eSyms[i];
1438 Symbol &sym = *symbols[i];
1439 uint32_t secIdx = eSym.st_shndx;
1440 uint8_t binding = eSym.getBinding();
1441 if (LLVM_UNLIKELY(binding != STB_GLOBAL && binding != STB_WEAK &&
1442 binding != STB_GNU_UNIQUE))
1443 Err(ctx) << this << ": symbol (" << i
1444 << ") has invalid binding: " << (int)binding;
1445
1446 // st_value of STT_TLS represents the assigned offset, not the actual
1447 // address which is used by STT_FUNC and STT_OBJECT. Report a TLS/non-TLS
1448 // mismatch between the resolved symbol and this file's symbol. Exempt
1449 // STT_NOTYPE, which may come from hand-written assembly or bitcode module
1450 // asm (STT_NOTYPE before LTO).
1451 if (LLVM_UNLIKELY(sym.isTls() != (eSym.getType() == STT_TLS)) &&
1452 eSym.getType() != STT_NOTYPE &&
1453 !(sym.type == STT_NOTYPE && isa_and_nonnull<BitcodeFile>(Val: sym.file)))
1454 Err(ctx) << "TLS attribute mismatch: " << &sym << "\n>>> in " << sym.file
1455 << "\n>>> in " << this;
1456
1457 // Handle non-COMMON defined symbol below. !sym.file allows a symbol
1458 // assignment to redefine a symbol without an error.
1459 if (!sym.isDefined() || secIdx == SHN_UNDEF)
1460 continue;
1461 if (LLVM_UNLIKELY(secIdx >= SHN_LORESERVE)) {
1462 if (secIdx == SHN_COMMON)
1463 continue;
1464 if (secIdx == SHN_XINDEX)
1465 secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable));
1466 else
1467 secIdx = 0;
1468 }
1469
1470 if (LLVM_UNLIKELY(secIdx >= sections.size())) {
1471 Err(ctx) << this << ": invalid section index: " << secIdx;
1472 continue;
1473 }
1474 InputSectionBase *sec = sections[secIdx];
1475 if (sec == &InputSection::discarded) {
1476 if (sym.traced) {
1477 printTraceSymbol(sym: Undefined{this, sym.getName(), sym.binding,
1478 sym.stOther, sym.type, secIdx},
1479 name: sym.getName());
1480 }
1481 if (sym.file == this) {
1482 std::lock_guard<std::mutex> lock(mu);
1483 ctx.nonPrevailingSyms.emplace_back(&sym, secIdx);
1484 }
1485 continue;
1486 }
1487
1488 if (sym.file == this) {
1489 cast<Defined>(Val&: sym).section = sec;
1490 continue;
1491 }
1492
1493 if (sym.binding == STB_WEAK || binding == STB_WEAK)
1494 continue;
1495 std::lock_guard<std::mutex> lock(mu);
1496 ctx.duplicates.push_back(Elt: {&sym, this, sec, eSym.st_value});
1497 }
1498}
1499
1500SharedFile::SharedFile(Ctx &ctx, MemoryBufferRef m, StringRef defaultSoName)
1501 : ELFFileBase(ctx, SharedKind, getELFKind(ctx, mb: m, archiveName: ""), m),
1502 soName(defaultSoName), isNeeded(!ctx.arg.asNeeded) {}
1503
1504// Parse the version definitions in the object file if present, and return a
1505// vector whose nth element contains a pointer to the Elf_Verdef for version
1506// identifier n. Version identifiers that are not definitions map to nullptr.
1507template <typename ELFT>
1508static SmallVector<const void *, 0>
1509parseVerdefs(const uint8_t *base, const typename ELFT::Shdr *sec) {
1510 if (!sec)
1511 return {};
1512
1513 // Build the Verdefs array by following the chain of Elf_Verdef objects
1514 // from the start of the .gnu.version_d section.
1515 SmallVector<const void *, 0> verdefs;
1516 const uint8_t *verdef = base + sec->sh_offset;
1517 for (unsigned i = 0, e = sec->sh_info; i != e; ++i) {
1518 auto *curVerdef = reinterpret_cast<const typename ELFT::Verdef *>(verdef);
1519 verdef += curVerdef->vd_next;
1520 unsigned verdefIndex = curVerdef->vd_ndx;
1521 if (verdefIndex >= verdefs.size())
1522 verdefs.resize(N: verdefIndex + 1);
1523 verdefs[verdefIndex] = curVerdef;
1524 }
1525 return verdefs;
1526}
1527
1528// Parse SHT_GNU_verneed to properly set the name of a versioned undefined
1529// symbol. We detect fatal issues which would cause vulnerabilities, but do not
1530// implement sophisticated error checking like in llvm-readobj because the value
1531// of such diagnostics is low.
1532template <typename ELFT>
1533std::vector<uint32_t> SharedFile::parseVerneed(const ELFFile<ELFT> &obj,
1534 const typename ELFT::Shdr *sec) {
1535 if (!sec)
1536 return {};
1537 std::vector<uint32_t> verneeds;
1538 ArrayRef<uint8_t> data = CHECK2(obj.getSectionContents(*sec), this);
1539 const uint8_t *verneedBuf = data.begin();
1540 for (unsigned i = 0; i != sec->sh_info; ++i) {
1541 if (verneedBuf + sizeof(typename ELFT::Verneed) > data.end()) {
1542 Err(ctx) << this << " has an invalid Verneed";
1543 break;
1544 }
1545 auto *vn = reinterpret_cast<const typename ELFT::Verneed *>(verneedBuf);
1546 const uint8_t *vernauxBuf = verneedBuf + vn->vn_aux;
1547 for (unsigned j = 0; j != vn->vn_cnt; ++j) {
1548 if (vernauxBuf + sizeof(typename ELFT::Vernaux) > data.end()) {
1549 Err(ctx) << this << " has an invalid Vernaux";
1550 break;
1551 }
1552 auto *aux = reinterpret_cast<const typename ELFT::Vernaux *>(vernauxBuf);
1553 if (aux->vna_name >= this->stringTable.size()) {
1554 Err(ctx) << this << " has a Vernaux with an invalid vna_name";
1555 break;
1556 }
1557 uint16_t version = aux->vna_other & VERSYM_VERSION;
1558 if (version >= verneeds.size())
1559 verneeds.resize(new_size: version + 1);
1560 verneeds[version] = aux->vna_name;
1561 vernauxBuf += aux->vna_next;
1562 }
1563 verneedBuf += vn->vn_next;
1564 }
1565 return verneeds;
1566}
1567
1568// Parse PT_GNU_PROPERTY segments in DSO. The process is similar to
1569// readGnuProperty, but we don't have the InputSection information.
1570template <typename ELFT>
1571void SharedFile::parseGnuAndFeatures(const ELFFile<ELFT> &obj) {
1572 if (ctx.arg.emachine != EM_AARCH64)
1573 return;
1574 const uint8_t *base = obj.base();
1575 auto phdrs = CHECK2(obj.program_headers(), this);
1576 for (auto phdr : phdrs) {
1577 if (phdr.p_type != PT_GNU_PROPERTY)
1578 continue;
1579 typename ELFT::Note note(
1580 *reinterpret_cast<const typename ELFT::Nhdr *>(base + phdr.p_offset));
1581 if (note.getType() != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU")
1582 continue;
1583
1584 ArrayRef<uint8_t> desc = note.getDesc(phdr.p_align);
1585 parseGnuPropertyNote<ELFT>(ctx, *this, GNU_PROPERTY_AARCH64_FEATURE_1_AND,
1586 desc, base);
1587 }
1588}
1589
1590// We do not usually care about alignments of data in shared object
1591// files because the loader takes care of it. However, if we promote a
1592// DSO symbol to point to .bss due to copy relocation, we need to keep
1593// the original alignment requirements. We infer it in this function.
1594template <typename ELFT>
1595static uint64_t getAlignment(ArrayRef<typename ELFT::Shdr> sections,
1596 const typename ELFT::Sym &sym) {
1597 uint64_t ret = UINT64_MAX;
1598 if (sym.st_value)
1599 ret = 1ULL << llvm::countr_zero(Val: (uint64_t)sym.st_value);
1600 if (0 < sym.st_shndx && sym.st_shndx < sections.size())
1601 ret = std::min<uint64_t>(ret, sections[sym.st_shndx].sh_addralign);
1602 return (ret > UINT32_MAX) ? 0 : ret;
1603}
1604
1605// Fully parse the shared object file.
1606//
1607// This function parses symbol versions. If a DSO has version information,
1608// the file has a ".gnu.version_d" section which contains symbol version
1609// definitions. Each symbol is associated to one version through a table in
1610// ".gnu.version" section. That table is a parallel array for the symbol
1611// table, and each table entry contains an index in ".gnu.version_d".
1612//
1613// The special index 0 is reserved for VERF_NDX_LOCAL and 1 is for
1614// VER_NDX_GLOBAL. There's no table entry for these special versions in
1615// ".gnu.version_d".
1616//
1617// The file format for symbol versioning is perhaps a bit more complicated
1618// than necessary, but you can easily understand the code if you wrap your
1619// head around the data structure described above.
1620template <class ELFT> void SharedFile::parse() {
1621 using Elf_Dyn = typename ELFT::Dyn;
1622 using Elf_Shdr = typename ELFT::Shdr;
1623 using Elf_Sym = typename ELFT::Sym;
1624 using Elf_Verdef = typename ELFT::Verdef;
1625 using Elf_Versym = typename ELFT::Versym;
1626
1627 ArrayRef<Elf_Dyn> dynamicTags;
1628 const ELFFile<ELFT> obj = this->getObj<ELFT>();
1629 ArrayRef<Elf_Shdr> sections = getELFShdrs<ELFT>();
1630
1631 const Elf_Shdr *versymSec = nullptr;
1632 const Elf_Shdr *verdefSec = nullptr;
1633 const Elf_Shdr *verneedSec = nullptr;
1634 symbols = std::make_unique<Symbol *[]>(num: numSymbols);
1635
1636 // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
1637 for (const Elf_Shdr &sec : sections) {
1638 switch (sec.sh_type) {
1639 default:
1640 continue;
1641 case SHT_DYNAMIC:
1642 dynamicTags =
1643 CHECK2(obj.template getSectionContentsAsArray<Elf_Dyn>(sec), this);
1644 break;
1645 case SHT_GNU_versym:
1646 versymSec = &sec;
1647 break;
1648 case SHT_GNU_verdef:
1649 verdefSec = &sec;
1650 break;
1651 case SHT_GNU_verneed:
1652 verneedSec = &sec;
1653 break;
1654 }
1655 }
1656
1657 if (versymSec && numSymbols == 0) {
1658 ErrAlways(ctx) << "SHT_GNU_versym should be associated with symbol table";
1659 return;
1660 }
1661
1662 // Search for a DT_SONAME tag to initialize this->soName.
1663 for (const Elf_Dyn &dyn : dynamicTags) {
1664 if (dyn.d_tag == DT_NEEDED) {
1665 uint64_t val = dyn.getVal();
1666 if (val >= this->stringTable.size()) {
1667 Err(ctx) << this << ": invalid DT_NEEDED entry";
1668 return;
1669 }
1670 dtNeeded.push_back(Elt: this->stringTable.data() + val);
1671 } else if (dyn.d_tag == DT_SONAME) {
1672 uint64_t val = dyn.getVal();
1673 if (val >= this->stringTable.size()) {
1674 Err(ctx) << this << ": invalid DT_SONAME entry";
1675 return;
1676 }
1677 soName = this->stringTable.data() + val;
1678 }
1679 }
1680
1681 // DSOs are uniquified not by filename but by soname.
1682 StringSaver &ss = ctx.saver;
1683 DenseMap<CachedHashStringRef, SharedFile *>::iterator it;
1684 bool wasInserted;
1685 std::tie(args&: it, args&: wasInserted) =
1686 ctx.symtab->soNames.try_emplace(Key: CachedHashStringRef(soName), Args: this);
1687
1688 // If a DSO appears more than once on the command line with and without
1689 // --as-needed, --no-as-needed takes precedence over --as-needed because a
1690 // user can add an extra DSO with --no-as-needed to force it to be added to
1691 // the dependency list.
1692 if (isNeeded)
1693 it->second->isNeeded.store(i: true, m: std::memory_order_relaxed);
1694 if (!wasInserted)
1695 return;
1696
1697 ctx.sharedFiles.push_back(Elt: this);
1698
1699 verdefs = parseVerdefs<ELFT>(obj.base(), verdefSec);
1700 std::vector<uint32_t> verneeds = parseVerneed<ELFT>(obj, verneedSec);
1701 parseGnuAndFeatures<ELFT>(obj);
1702
1703 // Parse ".gnu.version" section which is a parallel array for the symbol
1704 // table. If a given file doesn't have a ".gnu.version" section, we use
1705 // VER_NDX_GLOBAL.
1706 size_t size = numSymbols - firstGlobal;
1707 std::vector<uint16_t> versyms(size, VER_NDX_GLOBAL);
1708 if (versymSec) {
1709 ArrayRef<Elf_Versym> versym =
1710 CHECK2(obj.template getSectionContentsAsArray<Elf_Versym>(*versymSec),
1711 this)
1712 .slice(firstGlobal);
1713 for (size_t i = 0; i < size; ++i)
1714 versyms[i] = versym[i].vs_index;
1715 }
1716
1717 // System libraries can have a lot of symbols with versions. Using a
1718 // fixed buffer for computing the versions name (foo@ver) can save a
1719 // lot of allocations.
1720 SmallString<0> versionedNameBuffer;
1721
1722 // Add symbols to the symbol table.
1723 ArrayRef<Elf_Sym> syms = this->getGlobalELFSyms<ELFT>();
1724 for (size_t i = 0, e = syms.size(); i != e; ++i) {
1725 const Elf_Sym &sym = syms[i];
1726
1727 // ELF spec requires that all local symbols precede weak or global
1728 // symbols in each symbol table, and the index of first non-local symbol
1729 // is stored to sh_info. If a local symbol appears after some non-local
1730 // symbol, that's a violation of the spec.
1731 StringRef name = CHECK2(sym.getName(stringTable), this);
1732 if (sym.getBinding() == STB_LOCAL) {
1733 Err(ctx) << this << ": invalid local symbol '" << name
1734 << "' in global part of symbol table";
1735 continue;
1736 }
1737
1738 const uint16_t ver = versyms[i], idx = ver & ~VERSYM_HIDDEN;
1739 if (sym.isUndefined()) {
1740 // Index 0 (VER_NDX_LOCAL) is used for unversioned undefined symbols.
1741 // GNU ld versions between 2.35 and 2.45 also generate VER_NDX_GLOBAL
1742 // for this case (https://sourceware.org/PR33577).
1743 if (ver != VER_NDX_LOCAL && ver != VER_NDX_GLOBAL) {
1744 if (idx >= verneeds.size()) {
1745 ErrAlways(ctx) << "corrupt input file: version need index " << idx
1746 << " for symbol " << name
1747 << " is out of bounds\n>>> defined in " << this;
1748 continue;
1749 }
1750 StringRef verName = stringTable.data() + verneeds[idx];
1751 versionedNameBuffer.clear();
1752 name = ss.save(S: (name + "@" + verName).toStringRef(Out&: versionedNameBuffer));
1753 }
1754 Symbol *s = ctx.symtab->addSymbol(
1755 newSym: Undefined{this, name, sym.getBinding(), sym.st_other, sym.getType()});
1756 s->isExported = true;
1757 if (sym.getBinding() != STB_WEAK &&
1758 ctx.arg.unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore)
1759 requiredSymbols.push_back(Elt: s);
1760 continue;
1761 }
1762
1763 if (ver == VER_NDX_LOCAL ||
1764 (ver != VER_NDX_GLOBAL && idx >= verdefs.size())) {
1765 // In GNU ld < 2.31 (before 3be08ea4728b56d35e136af4e6fd3086ade17764), the
1766 // MIPS port puts _gp_disp symbol into DSO files and incorrectly assigns
1767 // VER_NDX_LOCAL. Workaround this bug.
1768 if (ctx.arg.emachine == EM_MIPS && name == "_gp_disp")
1769 continue;
1770 ErrAlways(ctx) << "corrupt input file: version definition index " << idx
1771 << " for symbol " << name
1772 << " is out of bounds\n>>> defined in " << this;
1773 continue;
1774 }
1775
1776 uint32_t alignment = getAlignment<ELFT>(sections, sym);
1777 if (ver == idx) {
1778 auto *s = ctx.symtab->addSymbol(
1779 newSym: SharedSymbol{*this, name, sym.getBinding(), sym.st_other,
1780 sym.getType(), sym.st_value, sym.st_size, alignment});
1781 s->dsoDefined = true;
1782 if (s->file == this)
1783 s->versionId = ver;
1784 }
1785
1786 // Also add the symbol with the versioned name to handle undefined symbols
1787 // with explicit versions.
1788 if (ver == VER_NDX_GLOBAL)
1789 continue;
1790
1791 StringRef verName =
1792 stringTable.data() +
1793 reinterpret_cast<const Elf_Verdef *>(verdefs[idx])->getAux()->vda_name;
1794 versionedNameBuffer.clear();
1795 name = (name + "@" + verName).toStringRef(Out&: versionedNameBuffer);
1796 auto *s = ctx.symtab->addSymbol(
1797 newSym: SharedSymbol{*this, ss.save(S: name), sym.getBinding(), sym.st_other,
1798 sym.getType(), sym.st_value, sym.st_size, alignment});
1799 s->dsoDefined = true;
1800 if (s->file == this)
1801 s->versionId = idx;
1802 }
1803}
1804
1805static ELFKind getBitcodeELFKind(const Triple &t) {
1806 if (t.isLittleEndian())
1807 return t.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
1808 return t.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
1809}
1810
1811static uint16_t getBitcodeMachineKind(Ctx &ctx, StringRef path,
1812 const Triple &t) {
1813 switch (t.getArch()) {
1814 case Triple::aarch64:
1815 case Triple::aarch64_be:
1816 return EM_AARCH64;
1817 case Triple::amdgpu:
1818 case Triple::r600:
1819 return EM_AMDGPU;
1820 case Triple::arm:
1821 case Triple::armeb:
1822 case Triple::thumb:
1823 case Triple::thumbeb:
1824 return EM_ARM;
1825 case Triple::avr:
1826 return EM_AVR;
1827 case Triple::hexagon:
1828 return EM_HEXAGON;
1829 case Triple::loongarch32:
1830 case Triple::loongarch64:
1831 return EM_LOONGARCH;
1832 case Triple::mips:
1833 case Triple::mipsel:
1834 case Triple::mips64:
1835 case Triple::mips64el:
1836 return EM_MIPS;
1837 case Triple::msp430:
1838 return EM_MSP430;
1839 case Triple::ppc:
1840 case Triple::ppcle:
1841 return EM_PPC;
1842 case Triple::ppc64:
1843 case Triple::ppc64le:
1844 return EM_PPC64;
1845 case Triple::riscv32:
1846 case Triple::riscv64:
1847 return EM_RISCV;
1848 case Triple::sparcv9:
1849 return EM_SPARCV9;
1850 case Triple::systemz:
1851 return EM_S390;
1852 case Triple::x86:
1853 return t.isOSIAMCU() ? EM_IAMCU : EM_386;
1854 case Triple::x86_64:
1855 return EM_X86_64;
1856 case Triple::nvptx:
1857 case Triple::nvptx64:
1858 return EM_CUDA;
1859 default:
1860 ErrAlways(ctx) << path
1861 << ": could not infer e_machine from bitcode target triple "
1862 << t.str();
1863 return EM_NONE;
1864 }
1865}
1866
1867static uint8_t getOsAbi(const Triple &t) {
1868 switch (t.getOS()) {
1869 case Triple::AMDHSA:
1870 return ELF::ELFOSABI_AMDGPU_HSA;
1871 case Triple::AMDPAL:
1872 return ELF::ELFOSABI_AMDGPU_PAL;
1873 case Triple::Mesa3D:
1874 return ELF::ELFOSABI_AMDGPU_MESA3D;
1875 default:
1876 return ELF::ELFOSABI_NONE;
1877 }
1878}
1879
1880BitcodeFile::BitcodeFile(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName,
1881 uint64_t offsetInArchive, bool lazy)
1882 : InputFile(ctx, BitcodeKind, mb) {
1883 this->archiveName = archiveName;
1884 this->lazy = lazy;
1885
1886 std::string path = mb.getBufferIdentifier().str();
1887 if (ctx.arg.thinLTOIndexOnly)
1888 path = replaceThinLTOSuffix(ctx, path: mb.getBufferIdentifier());
1889
1890 // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
1891 // name. If two archives define two members with the same name, this
1892 // causes a collision which result in only one of the objects being taken
1893 // into consideration at LTO time (which very likely causes undefined
1894 // symbols later in the link stage). So we append file offset to make
1895 // filename unique.
1896 StringSaver &ss = ctx.saver;
1897 StringRef name = archiveName.empty()
1898 ? ss.save(S: path)
1899 : ss.save(S: archiveName + "(" + path::filename(path) +
1900 " at " + utostr(X: offsetInArchive) + ")");
1901
1902 MemoryBufferRef mbref(mb.getBuffer(), name);
1903
1904 obj = CHECK2(lto::InputFile::create(mbref), this);
1905 obj->setArchivePathAndName(Path: archiveName, Name: mb.getBufferIdentifier());
1906
1907 Triple t(obj->getTargetTriple());
1908 ekind = getBitcodeELFKind(t);
1909 emachine = getBitcodeMachineKind(ctx, path: mb.getBufferIdentifier(), t);
1910 osabi = getOsAbi(t);
1911}
1912
1913static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) {
1914 switch (gvVisibility) {
1915 case GlobalValue::DefaultVisibility:
1916 return STV_DEFAULT;
1917 case GlobalValue::HiddenVisibility:
1918 return STV_HIDDEN;
1919 case GlobalValue::ProtectedVisibility:
1920 return STV_PROTECTED;
1921 }
1922 llvm_unreachable("unknown visibility");
1923}
1924
1925static void createBitcodeSymbol(Ctx &ctx, Symbol *&sym,
1926 const lto::InputFile::Symbol &objSym,
1927 BitcodeFile &f) {
1928 uint8_t binding = objSym.isWeak() ? STB_WEAK : STB_GLOBAL;
1929 uint8_t type = objSym.isTLS() ? STT_TLS : STT_NOTYPE;
1930 uint8_t visibility = mapVisibility(gvVisibility: objSym.getVisibility());
1931
1932 if (!sym) {
1933 // Symbols can be duplicated in bitcode files because of '#include' and
1934 // linkonce_odr. Use uniqueSaver to save symbol names for de-duplication.
1935 // Update objSym.Name to reference (via StringRef) the string saver's copy;
1936 // this way LTO can reference the same string saver's copy rather than
1937 // keeping copies of its own.
1938 objSym.Name = ctx.uniqueSaver.save(S: objSym.getName());
1939 sym = ctx.symtab->insert(name: objSym.getName());
1940 }
1941
1942 if (objSym.isUndefined()) {
1943 Undefined newSym(&f, StringRef(), binding, visibility, type);
1944 sym->resolve(ctx, other: newSym);
1945 sym->referenced = true;
1946 return;
1947 }
1948
1949 if (objSym.isCommon()) {
1950 sym->resolve(ctx, other: CommonSymbol{&f, StringRef(), binding, visibility,
1951 STT_OBJECT, objSym.getCommonAlignment(),
1952 objSym.getCommonSize()});
1953 } else {
1954 Defined newSym(&f, StringRef(), binding, visibility, type, 0, 0, nullptr);
1955 // The definition can be omitted if all bitcode definitions satisfy
1956 // `canBeOmittedFromSymbolTable()` and isUsedInRegularObj is false.
1957 // The latter condition is tested in parseVersionAndComputeIsPreemptible.
1958 sym->ltoCanOmit = objSym.canBeOmittedFromSymbolTable() &&
1959 (!sym->isDefined() || sym->ltoCanOmit);
1960 sym->resolve(ctx, other: newSym);
1961 }
1962}
1963
1964void BitcodeFile::parse() {
1965 for (std::pair<StringRef, Comdat::SelectionKind> s : obj->getComdatTable()) {
1966 keptComdats.push_back(
1967 x: s.second == Comdat::NoDeduplicate ||
1968 ctx.symtab->comdatGroups.try_emplace(Key: CachedHashStringRef(s.first), Args: this)
1969 .second);
1970 }
1971
1972 if (numSymbols == 0) {
1973 numSymbols = obj->symbols().size();
1974 symbols = std::make_unique<Symbol *[]>(num: numSymbols);
1975 }
1976 // Process defined symbols first. See the comment in
1977 // ObjFile<ELFT>::initializeSymbols.
1978 for (auto [i, irSym] : llvm::enumerate(First: obj->symbols()))
1979 if (!irSym.isUndefined())
1980 createBitcodeSymbol(ctx, sym&: symbols[i], objSym: irSym, f&: *this);
1981 for (auto [i, irSym] : llvm::enumerate(First: obj->symbols()))
1982 if (irSym.isUndefined())
1983 createBitcodeSymbol(ctx, sym&: symbols[i], objSym: irSym, f&: *this);
1984
1985 for (auto l : obj->getDependentLibraries())
1986 addDependentLibrary(ctx, specifier: l, f: this);
1987}
1988
1989void BitcodeFile::parseLazy() {
1990 numSymbols = obj->symbols().size();
1991 symbols = std::make_unique<Symbol *[]>(num: numSymbols);
1992 for (auto [i, irSym] : llvm::enumerate(First: obj->symbols())) {
1993 // Symbols can be duplicated in bitcode files because of '#include' and
1994 // linkonce_odr. Use uniqueSaver to save symbol names for de-duplication.
1995 // Update objSym.Name to reference (via StringRef) the string saver's copy;
1996 // this way LTO can reference the same string saver's copy rather than
1997 // keeping copies of its own.
1998 irSym.Name = ctx.uniqueSaver.save(S: irSym.getName());
1999 if (!irSym.isUndefined()) {
2000 auto *sym = ctx.symtab->insert(name: irSym.getName());
2001 sym->resolve(ctx, other: LazySymbol{*this});
2002 symbols[i] = sym;
2003 }
2004 }
2005}
2006
2007void BitcodeFile::postParse() {
2008 for (auto [i, irSym] : llvm::enumerate(First: obj->symbols())) {
2009 const Symbol &sym = *symbols[i];
2010 if (sym.file == this || !sym.isDefined() || irSym.isUndefined() ||
2011 irSym.isCommon() || irSym.isWeak())
2012 continue;
2013 int c = irSym.getComdatIndex();
2014 if (c != -1 && !keptComdats[c])
2015 continue;
2016 reportDuplicate(ctx, sym, newFile: this, errSec: nullptr, errOffset: 0);
2017 }
2018}
2019
2020void BinaryFile::parse() {
2021 ArrayRef<uint8_t> data = arrayRefFromStringRef(Input: mb.getBuffer());
2022 auto *section =
2023 make<InputSection>(args: this, args: ".data", args: SHT_PROGBITS, args: SHF_ALLOC | SHF_WRITE,
2024 /*addralign=*/args: 8, /*entsize=*/args: 0, args&: data);
2025 sections.push_back(Elt: section);
2026
2027 // For each input file foo that is embedded to a result as a binary
2028 // blob, we define _binary_foo_{start,end,size} symbols, so that
2029 // user programs can access blobs by name. Non-alphanumeric
2030 // characters in a filename are replaced with underscore.
2031 std::string s = "_binary_" + mb.getBufferIdentifier().str();
2032 for (char &c : s)
2033 if (!isAlnum(C: c))
2034 c = '_';
2035
2036 llvm::StringSaver &ss = ctx.saver;
2037 ctx.symtab->addAndCheckDuplicate(newSym: Defined{this, ss.save(S: s + "_start"),
2038 STB_GLOBAL, STV_DEFAULT, STT_OBJECT,
2039 0, 0, section});
2040 ctx.symtab->addAndCheckDuplicate(newSym: Defined{this, ss.save(S: s + "_end"),
2041 STB_GLOBAL, STV_DEFAULT, STT_OBJECT,
2042 data.size(), 0, section});
2043 ctx.symtab->addAndCheckDuplicate(newSym: Defined{this, ss.save(S: s + "_size"),
2044 STB_GLOBAL, STV_DEFAULT, STT_OBJECT,
2045 data.size(), 0, nullptr});
2046}
2047
2048InputFile *elf::createInternalFile(Ctx &ctx, StringRef name) {
2049 auto *file =
2050 make<InputFile>(args&: ctx, args: InputFile::InternalKind, args: MemoryBufferRef("", name));
2051 // References from an internal file do not lead to --warn-backrefs
2052 // diagnostics.
2053 file->groupId = 0;
2054 return file;
2055}
2056
2057std::unique_ptr<ELFFileBase> elf::createObjFile(Ctx &ctx, MemoryBufferRef mb,
2058 StringRef archiveName,
2059 bool lazy) {
2060 std::unique_ptr<ELFFileBase> f;
2061 switch (getELFKind(ctx, mb, archiveName)) {
2062 case ELF32LEKind:
2063 f = std::make_unique<ObjFile<ELF32LE>>(args&: ctx, args: ELF32LEKind, args&: mb, args&: archiveName);
2064 break;
2065 case ELF32BEKind:
2066 f = std::make_unique<ObjFile<ELF32BE>>(args&: ctx, args: ELF32BEKind, args&: mb, args&: archiveName);
2067 break;
2068 case ELF64LEKind:
2069 f = std::make_unique<ObjFile<ELF64LE>>(args&: ctx, args: ELF64LEKind, args&: mb, args&: archiveName);
2070 break;
2071 case ELF64BEKind:
2072 f = std::make_unique<ObjFile<ELF64BE>>(args&: ctx, args: ELF64BEKind, args&: mb, args&: archiveName);
2073 break;
2074 default:
2075 llvm_unreachable("getELFKind");
2076 }
2077 f->init();
2078 f->lazy = lazy;
2079 return f;
2080}
2081
2082template <class ELFT> void ObjFile<ELFT>::parseLazy() {
2083 const ArrayRef<typename ELFT::Sym> eSyms = this->getELFSyms<ELFT>();
2084 if (!symbols)
2085 symbols = std::make_unique<Symbol *[]>(numSymbols);
2086
2087 // resolve() may trigger this->extract() if an existing symbol is an undefined
2088 // symbol. If that happens, this function has served its purpose, and we can
2089 // exit from the loop early.
2090 for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {
2091 if (eSyms[i].st_shndx == SHN_UNDEF)
2092 continue;
2093 symbols[i] = insertGlobal(i);
2094 symbols[i]->resolve(ctx, LazySymbol{*this});
2095 if (!lazy)
2096 break;
2097 }
2098}
2099
2100std::string elf::replaceThinLTOSuffix(Ctx &ctx, StringRef path) {
2101 auto [suffix, repl] = ctx.arg.thinLTOObjectSuffixReplace;
2102 if (path.consume_back(Suffix: suffix))
2103 return (path + repl).str();
2104 return std::string(path);
2105}
2106
2107template class elf::ObjFile<ELF32LE>;
2108template class elf::ObjFile<ELF32BE>;
2109template class elf::ObjFile<ELF64LE>;
2110template class elf::ObjFile<ELF64BE>;
2111
2112template void SharedFile::parse<ELF32LE>();
2113template void SharedFile::parse<ELF32BE>();
2114template void SharedFile::parse<ELF64LE>();
2115template void SharedFile::parse<ELF64BE>();
2116