Skip to content

Commit 3aaeac5

Browse files
committed
Address some of Fangrui's review comments
1 parent 6c41c38 commit 3aaeac5

File tree

4 files changed

+50
-48
lines changed

4 files changed

+50
-48
lines changed

lld/ELF/ICF.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,13 @@ void ICF<ELFT>::segregate(size_t begin, size_t end, uint32_t eqClassBase,
245245
template <class ELFT>
246246
template <class RelTy>
247247
bool ICF<ELFT>::isTrivialRelocation(InputSection *a, Symbol &s, RelTy reloc) {
248-
OffsetGetter getter(*a);
249-
uint64_t offset = getter.get(ctx, reloc.r_offset);
248+
// For our use cases, we can get by without calculating exact location within
249+
// the section, and just use fake location array. We need to ensure validity
250+
// for loc[-1] to loc[3] as various targets' getRelExpr() reference them.
251+
std::array<uint8_t, 5> fakeLocArray;
252+
uint8_t *fakeLoc = fakeLocArray.data() + 1;
250253
RelExpr expr = ctx.target->getRelExpr(reloc.getType(ctx.arg.isMips64EL), s,
251-
a->content().data() + offset);
254+
fakeLoc);
252255

253256
if (needsGot(expr) || needsTls(s, expr))
254257
return false;
@@ -412,7 +415,7 @@ bool ICF<ELFT>::variableEq(InputSection *secA, Relocs<RelTy> ra,
412415
// getting merged into each other (done later in ICF). We do this as
413416
// post-ICF passes cannot handle duplicates when iterating over local
414417
// symbols. There are also assertions that prevent this.
415-
if ((!da->isGlobal() || !db->isGlobal()) &&
418+
if ((da->isLocal() || db->isLocal()) &&
416419
!isTrivialRelocation(secA, sa, *rai))
417420
return false;
418421

lld/ELF/InputSection.h

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -514,49 +514,6 @@ class SyntheticSection : public InputSection {
514514
}
515515
};
516516

517-
class OffsetGetter {
518-
public:
519-
OffsetGetter() = default;
520-
explicit OffsetGetter(InputSectionBase &sec) {
521-
if (auto *eh = dyn_cast<EhInputSection>(&sec)) {
522-
cies = eh->cies;
523-
fdes = eh->fdes;
524-
i = cies.begin();
525-
j = fdes.begin();
526-
}
527-
}
528-
529-
// Translates offsets in input sections to offsets in output sections.
530-
// Given offset must increase monotonically. We assume that Piece is
531-
// sorted by inputOff.
532-
uint64_t get(Ctx &ctx, uint64_t off) {
533-
if (cies.empty())
534-
return off;
535-
536-
while (j != fdes.end() && j->inputOff <= off)
537-
++j;
538-
auto it = j;
539-
if (j == fdes.begin() || j[-1].inputOff + j[-1].size <= off) {
540-
while (i != cies.end() && i->inputOff <= off)
541-
++i;
542-
if (i == cies.begin() || i[-1].inputOff + i[-1].size <= off) {
543-
Err(ctx) << ".eh_frame: relocation is not in any piece";
544-
return 0;
545-
}
546-
it = i;
547-
}
548-
549-
// Offset -1 means that the piece is dead (i.e. garbage collected).
550-
if (it[-1].outputOff == -1)
551-
return -1;
552-
return it[-1].outputOff + (off - it[-1].inputOff);
553-
}
554-
555-
private:
556-
ArrayRef<EhSectionPiece> cies, fdes;
557-
ArrayRef<EhSectionPiece>::iterator i, j;
558-
};
559-
560517
inline bool isStaticRelSecType(uint32_t type) {
561518
return type == llvm::ELF::SHT_RELA || type == llvm::ELF::SHT_CREL ||
562519
type == llvm::ELF::SHT_REL;

lld/ELF/Relocations.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,49 @@ template <class ELFT> static void addCopyRelSymbol(Ctx &ctx, SharedSymbol &ss) {
371371
//
372372
// For sections other than .eh_frame, this class doesn't do anything.
373373
namespace {
374+
class OffsetGetter {
375+
public:
376+
OffsetGetter() = default;
377+
explicit OffsetGetter(InputSectionBase &sec) {
378+
if (auto *eh = dyn_cast<EhInputSection>(&sec)) {
379+
cies = eh->cies;
380+
fdes = eh->fdes;
381+
i = cies.begin();
382+
j = fdes.begin();
383+
}
384+
}
385+
386+
// Translates offsets in input sections to offsets in output sections.
387+
// Given offset must increase monotonically. We assume that Piece is
388+
// sorted by inputOff.
389+
uint64_t get(Ctx &ctx, uint64_t off) {
390+
if (cies.empty())
391+
return off;
392+
393+
while (j != fdes.end() && j->inputOff <= off)
394+
++j;
395+
auto it = j;
396+
if (j == fdes.begin() || j[-1].inputOff + j[-1].size <= off) {
397+
while (i != cies.end() && i->inputOff <= off)
398+
++i;
399+
if (i == cies.begin() || i[-1].inputOff + i[-1].size <= off) {
400+
Err(ctx) << ".eh_frame: relocation is not in any piece";
401+
return 0;
402+
}
403+
it = i;
404+
}
405+
406+
// Offset -1 means that the piece is dead (i.e. garbage collected).
407+
if (it[-1].outputOff == -1)
408+
return -1;
409+
return it[-1].outputOff + (off - it[-1].inputOff);
410+
}
411+
412+
private:
413+
ArrayRef<EhSectionPiece> cies, fdes;
414+
ArrayRef<EhSectionPiece>::iterator i, j;
415+
};
416+
374417
// This class encapsulates states needed to scan relocations for one
375418
// InputSectionBase.
376419
class RelocationScanner {

lld/ELF/Target.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "Target.h"
2727
#include "InputFiles.h"
2828
#include "OutputSections.h"
29-
#include "Relocations.h"
3029
#include "SymbolTable.h"
3130
#include "Symbols.h"
3231
#include "SyntheticSections.h"

0 commit comments

Comments
 (0)