@@ -47,7 +47,7 @@ template <class ELFT> class MarkLive {
47
47
void run ();
48
48
49
49
private:
50
- void enqueue (InputSectionBase *Sec, uint64_t Offset);
50
+ void enqueue (InputSectionBase *Sec, uint64_t Offset, bool IsLSDA );
51
51
void markSymbol (Symbol *Sym);
52
52
53
53
template <class RelTy >
@@ -97,7 +97,7 @@ void MarkLive<ELFT>::resolveReloc(InputSectionBase &Sec, RelTy &Rel,
97
97
Offset += getAddend<ELFT>(Sec, Rel);
98
98
99
99
if (!IsLSDA || !(RelSec->Flags & SHF_EXECINSTR))
100
- enqueue (RelSec, Offset);
100
+ enqueue (RelSec, Offset, IsLSDA );
101
101
return ;
102
102
}
103
103
@@ -106,7 +106,7 @@ void MarkLive<ELFT>::resolveReloc(InputSectionBase &Sec, RelTy &Rel,
106
106
SS->getFile ().IsNeeded = true ;
107
107
108
108
for (InputSectionBase *Sec : CNamedSections.lookup (Sym.getName ()))
109
- enqueue (Sec, 0 );
109
+ enqueue (Sec, 0 , IsLSDA );
110
110
}
111
111
112
112
// The .eh_frame section is an unfortunate special case.
@@ -169,7 +169,8 @@ static bool isReserved(InputSectionBase *Sec) {
169
169
}
170
170
171
171
template <class ELFT >
172
- void MarkLive<ELFT>::enqueue(InputSectionBase *Sec, uint64_t Offset) {
172
+ void MarkLive<ELFT>::enqueue(InputSectionBase *Sec, uint64_t Offset,
173
+ bool IsLSDA) {
173
174
// Skip over discarded sections. This in theory shouldn't happen, because
174
175
// the ELF spec doesn't allow a relocation to point to a deduplicated
175
176
// COMDAT section directly. Unfortunately this happens in practice (e.g.
@@ -183,19 +184,26 @@ void MarkLive<ELFT>::enqueue(InputSectionBase *Sec, uint64_t Offset) {
183
184
if (auto *MS = dyn_cast<MergeInputSection>(Sec))
184
185
MS->getSectionPiece (Offset)->Live = true ;
185
186
187
+ InputSection *S = dyn_cast<InputSection>(Sec);
188
+ // LSDA does not count as "live code or data" in the object file.
189
+ // The section may already have been marked live for LSDA in which
190
+ // case we still need to mark the file.
191
+ if (S && !IsLSDA && Sec->File )
192
+ Sec->getFile <ELFT>()->HasLiveCodeOrData = true ;
193
+
186
194
if (Sec->Live )
187
195
return ;
188
- Sec->Live = true ;
189
196
197
+ Sec->Live = true ;
190
198
// Add input section to the queue.
191
- if (InputSection *S = dyn_cast<InputSection>(Sec) )
199
+ if (S )
192
200
Queue.push_back (S);
193
201
}
194
202
195
203
template <class ELFT > void MarkLive<ELFT>::markSymbol(Symbol *Sym) {
196
204
if (auto *D = dyn_cast_or_null<Defined>(Sym))
197
205
if (auto *IS = dyn_cast_or_null<InputSectionBase>(D->Section ))
198
- enqueue (IS, D->Value );
206
+ enqueue (IS, D->Value , false );
199
207
}
200
208
201
209
// This is the main function of the garbage collector.
@@ -239,7 +247,7 @@ template <class ELFT> void MarkLive<ELFT>::run() {
239
247
continue ;
240
248
241
249
if (isReserved (Sec) || Script->shouldKeep (Sec)) {
242
- enqueue (Sec, 0 );
250
+ enqueue (Sec, 0 , false );
243
251
} else if (isValidCIdentifier (Sec->Name )) {
244
252
CNamedSections[Saver.save (" __start_" + Sec->Name )].push_back (Sec);
245
253
CNamedSections[Saver.save (" __stop_" + Sec->Name )].push_back (Sec);
@@ -259,7 +267,7 @@ template <class ELFT> void MarkLive<ELFT>::run() {
259
267
}
260
268
261
269
for (InputSectionBase *IS : Sec.DependentSections )
262
- enqueue (IS, 0 );
270
+ enqueue (IS, 0 , false );
263
271
}
264
272
}
265
273
@@ -285,7 +293,7 @@ template <class ELFT> void elf::markLive() {
285
293
// The -gc-sections option works only for SHF_ALLOC sections
286
294
// (sections that are memory-mapped at runtime). So we can
287
295
// unconditionally make non-SHF_ALLOC sections alive except
288
- // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections.
296
+ // SHF_LINK_ORDER and SHT_REL/SHT_RELA sections and .debug sections .
289
297
//
290
298
// Usually, SHF_ALLOC sections are not removed even if they are
291
299
// unreachable through relocations because reachability is not
@@ -306,13 +314,19 @@ template <class ELFT> void elf::markLive() {
306
314
bool IsLinkOrder = (Sec->Flags & SHF_LINK_ORDER);
307
315
bool IsRel = (Sec->Type == SHT_REL || Sec->Type == SHT_RELA);
308
316
309
- if (!IsAlloc && !IsLinkOrder && !IsRel)
317
+ if (!IsAlloc && !IsLinkOrder && !IsRel && !Sec-> Debug )
310
318
Sec->Live = true ;
311
319
}
312
320
313
321
// Follow the graph to mark all live sections.
314
322
MarkLive<ELFT>().run ();
315
323
324
+ // Mark debug sections as live in any object file that has a live
325
+ // Regular or Merge section.
326
+ for (InputSectionBase *Sec : InputSections)
327
+ if (Sec->Debug && Sec->getFile <ELFT>()->HasLiveCodeOrData )
328
+ Sec->Live = true ;
329
+
316
330
// Report garbage-collected sections.
317
331
if (Config->PrintGcSections )
318
332
for (InputSectionBase *Sec : InputSections)
0 commit comments