Lines Matching +full:keep +full:- +full:a +full:- +full:live
1 //===- MarkLive.cpp -------------------------------------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements --gc-sections, which is a feature to remove unused
11 // from known GC-root symbols or sections. Naturally the feature is
12 // implemented as a mark-sweep garbage collector.
14 // Here's how it works. Each InputSectionBase has a "Live" bit. The bit is off
15 // by default. Starting with GC-root symbols or sections, markLive function
16 // defined in this file visits all reachable sections to set their Live
17 // bits. Writer will then ignore sections whose Live bits are off, so that
20 //===----------------------------------------------------------------------===//
66 // A list of sections to visit.
70 // identifiers, so we just store a SmallVector instead of a multimap.
78 return target->getImplicitAddend(sec.content().begin() + rel.r_offset, in getAddend()
79 rel.getType(config->isMips64EL)); in getAddend()
99 // If a symbol is referenced in a live section, it is used. in resolveReloc()
100 Symbol &sym = sec.file->getRelocTargetSym(rel); in resolveReloc()
104 auto *relSec = dyn_cast_or_null<InputSectionBase>(d->section); in resolveReloc()
108 uint64_t offset = d->value; in resolveReloc()
109 if (d->isSection()) in resolveReloc()
112 // fromFDE being true means this is referenced by a FDE in a .eh_frame in resolveReloc()
113 // piece. The relocation points to the described function or to a LSDA. We in resolveReloc()
114 // only need to keep the LSDA live, so ignore anything that points to in resolveReloc()
115 // executable sections. If the LSDA is in a section group or has the in resolveReloc()
116 // SHF_LINK_ORDER flag, we ignore the relocation as well because (a) if the in resolveReloc()
117 // associated text section is live, the LSDA will be retained due to section in resolveReloc()
120 if (!(fromFDE && ((relSec->flags & (SHF_EXECINSTR | SHF_LINK_ORDER)) || in resolveReloc()
121 relSec->nextInSectionGroup))) in resolveReloc()
127 if (!ss->isWeak()) in resolveReloc()
128 cast<SharedFile>(ss->file)->isNeeded = true; in resolveReloc()
136 // * CIEs can refer to a personality function.
137 // * FDEs can refer to a LSDA
139 // The last kind of relocation cannot keep the referred section alive, or they
140 // would keep everything alive in a common object file. In fact, each FDE is
142 // To keep things simple, in here we just ignore the last relocation kind. The
143 // other two keep the referred section alive.
145 // A possible improvement would be to fully process .eh_frame in the middle of
153 if (cie.firstRelocation != unsigned(-1)) in scanEhFrameSection()
157 if (firstRelI == (unsigned)-1) in scanEhFrameSection()
167 // garbage-collected. This function returns true if a given section is such
170 switch (sec->type) { in isReserved()
176 // SHT_NOTE sections in a group are subject to garbage collection. in isReserved()
177 return !sec->nextInSectionGroup; in isReserved()
180 // .init_array.N (https://github.com/rust-lang/rust/issues/92181) for a in isReserved()
182 StringRef s = sec->name; in isReserved()
190 // Usually, a whole section is marked as live or dead, but in mergeable in enqueue()
194 ms->getSectionPiece(offset).live = true; in enqueue()
196 // Set Sec->Partition to the meet (i.e. the "minimum") of Partition and in enqueue()
197 // Sec->Partition in the following lattice: 1 < other < 0. If Sec->Partition in enqueue()
199 if (sec->partition == 1 || sec->partition == partition) in enqueue()
201 sec->partition = sec->partition ? 1 : partition; in enqueue()
210 if (auto *isec = dyn_cast_or_null<InputSectionBase>(d->section)) in markSymbol()
211 enqueue(isec, d->value); in markSymbol()
215 // Starting from GC-root sections, this function visits all reachable
216 // sections to set their "Live" bits.
220 // Preserve externally-visible symbols if the symbols defined by this in run()
223 if (sym->includeInDynsym() && sym->partition == partition) in run()
232 markSymbol(symtab.find(config->entry)); in run()
233 markSymbol(symtab.find(config->init)); in run()
234 markSymbol(symtab.find(config->fini)); in run()
235 for (StringRef s : config->undefined) in run()
237 for (StringRef s : script->referencedSymbols) in run()
244 // Mark .eh_frame sections as live because there are usually no relocations in run()
250 eh->template relsOrRelas<ELFT>(/*supportsCrel=*/false); in run()
257 if (sec->flags & SHF_GNU_RETAIN) { in run()
261 if (sec->flags & SHF_LINK_ORDER) in run()
264 // Usually, non-SHF_ALLOC sections are not removed even if they are in run()
265 // unreachable through relocations because reachability is not a good signal in run()
267 // referring to a .comment section, but we want to keep it.) When a in run()
268 // non-SHF_ALLOC section is retained, we also retain sections dependent on in run()
272 // have a reverse dependency on the InputSection they are linked with. in run()
275 // Note on SHF_REL{,A}: Such sections reach here only when -r in run()
276 // or --emit-reloc were given. And they are subject of garbage in run()
277 // collection because, if we remove a text section, we also in run()
281 // included or omitted as a unit. We take the interpretation that: in run()
283 // - Group members (nextInSectionGroup != nullptr) are subject to garbage in run()
285 // - Groups members are retained or discarded as a unit. in run()
286 if (!(sec->flags & SHF_ALLOC)) { in run()
287 if (!isStaticRelSecType(sec->type) && !sec->nextInSectionGroup) { in run()
288 sec->markLive(); in run()
289 for (InputSection *isec : sec->dependentSections) in run()
290 isec->markLive(); in run()
295 // script KEEP command. in run()
296 if (isReserved(sec) || script->shouldKeep(sec)) { in run()
298 } else if ((!config->zStartStopGC || sec->name.starts_with("__libc_")) && in run()
299 isValidCIdentifier(sec->name)) { in run()
300 // As a workaround for glibc libc.a before 2.34 in run()
303 cNamedSections[saver().save("__start_" + sec->name)].push_back(sec); in run()
304 cNamedSections[saver().save("__stop_" + sec->name)].push_back(sec); in run()
344 for (Symbol *s : file->getSymbols()) in moveToMain()
346 if ((d->type == STT_GNU_IFUNC || d->type == STT_TLS) && d->section && in moveToMain()
347 d->section->isLive()) in moveToMain()
351 if (!sec->isLive() || !isValidCIdentifier(sec->name)) in moveToMain()
353 if (symtab.find(("__start_" + sec->name).str()) || in moveToMain()
354 symtab.find(("__stop_" + sec->name).str())) in moveToMain()
361 // Before calling this function, Live bits are off for all
366 // If --gc-sections is not given, retain all input sections. in markLive()
367 if (!config->gcSections) { in markLive()
368 // If a DSO defines a symbol referenced in a regular object, it is needed. in markLive()
371 if (s->isUsedInRegularObj && !s->isWeak()) in markLive()
372 cast<SharedFile>(s->file)->isNeeded = true; in markLive()
377 sec->markDead(); in markLive()
379 // Follow the graph to mark all live sections. in markLive()
383 // If we have multiple partitions, some sections need to live in the main in markLive()
384 // partition even if they were allocated to a loadable partition. Move them in markLive()
389 // Report garbage-collected sections. in markLive()
390 if (config->printGcSections) in markLive()
392 if (!sec->isLive()) in markLive()