forked from OSchip/llvm-project
Revert r319090, "COFF: Do not create SectionChunks for discarded comdat sections."
Caused test failures in check-cfi on Windows. http://lab.llvm.org:8011/builders/sanitizer-windows/builds/20284 llvm-svn: 319100
This commit is contained in:
parent
06b891f693
commit
c8477b8234
|
@ -38,6 +38,9 @@ SectionChunk::SectionChunk(ObjFile *F, const coff_section *H)
|
||||||
|
|
||||||
Alignment = Header->getAlignment();
|
Alignment = Header->getAlignment();
|
||||||
|
|
||||||
|
// Chunks may be discarded during comdat merging.
|
||||||
|
Discarded = false;
|
||||||
|
|
||||||
// If linker GC is disabled, every chunk starts out alive. If linker GC is
|
// If linker GC is disabled, every chunk starts out alive. If linker GC is
|
||||||
// enabled, treat non-comdat sections as roots. Generally optimized object
|
// enabled, treat non-comdat sections as roots. Generally optimized object
|
||||||
// files will be built with -ffunction-sections or /Gy, so most things worth
|
// files will be built with -ffunction-sections or /Gy, so most things worth
|
||||||
|
@ -359,8 +362,12 @@ bool SectionChunk::isCOMDAT() const {
|
||||||
void SectionChunk::printDiscardedMessage() const {
|
void SectionChunk::printDiscardedMessage() const {
|
||||||
// Removed by dead-stripping. If it's removed by ICF, ICF already
|
// Removed by dead-stripping. If it's removed by ICF, ICF already
|
||||||
// printed out the name, so don't repeat that here.
|
// printed out the name, so don't repeat that here.
|
||||||
if (Sym && this == Repl)
|
if (Sym && this == Repl) {
|
||||||
message("Discarded " + Sym->getName());
|
if (Discarded)
|
||||||
|
message("Discarded comdat symbol " + Sym->getName());
|
||||||
|
else if (!Live)
|
||||||
|
message("Discarded " + Sym->getName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
StringRef SectionChunk::getDebugName() {
|
StringRef SectionChunk::getDebugName() {
|
||||||
|
|
|
@ -159,9 +159,10 @@ public:
|
||||||
void addAssociative(SectionChunk *Child);
|
void addAssociative(SectionChunk *Child);
|
||||||
|
|
||||||
StringRef getDebugName() override;
|
StringRef getDebugName() override;
|
||||||
|
void setSymbol(DefinedRegular *S) { if (!Sym) Sym = S; }
|
||||||
|
|
||||||
// Returns true if the chunk was not dropped by GC.
|
// Returns true if the chunk was not dropped by GC or COMDAT deduplication.
|
||||||
bool isLive() { return Live; }
|
bool isLive() { return Live && !Discarded; }
|
||||||
|
|
||||||
// Used by the garbage collector.
|
// Used by the garbage collector.
|
||||||
void markLive() {
|
void markLive() {
|
||||||
|
@ -170,6 +171,13 @@ public:
|
||||||
Live = true;
|
Live = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns true if this chunk was dropped by COMDAT deduplication.
|
||||||
|
bool isDiscarded() const { return Discarded; }
|
||||||
|
|
||||||
|
// Used by the SymbolTable when discarding unused comdat sections. This is
|
||||||
|
// redundant when GC is enabled, as all comdat sections will start out dead.
|
||||||
|
void markDiscarded() { Discarded = true; }
|
||||||
|
|
||||||
// True if this is a codeview debug info chunk. These will not be laid out in
|
// True if this is a codeview debug info chunk. These will not be laid out in
|
||||||
// the image. Instead they will end up in the PDB, if one is requested.
|
// the image. Instead they will end up in the PDB, if one is requested.
|
||||||
bool isCodeView() const {
|
bool isCodeView() const {
|
||||||
|
@ -205,21 +213,24 @@ public:
|
||||||
// The file that this chunk was created from.
|
// The file that this chunk was created from.
|
||||||
ObjFile *File;
|
ObjFile *File;
|
||||||
|
|
||||||
// The COMDAT leader symbol if this is a COMDAT chunk.
|
|
||||||
DefinedRegular *Sym = nullptr;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
StringRef SectionName;
|
StringRef SectionName;
|
||||||
std::vector<SectionChunk *> AssocChildren;
|
std::vector<SectionChunk *> AssocChildren;
|
||||||
llvm::iterator_range<const coff_relocation *> Relocs;
|
llvm::iterator_range<const coff_relocation *> Relocs;
|
||||||
size_t NumRelocs;
|
size_t NumRelocs;
|
||||||
|
|
||||||
|
// True if this chunk was discarded because it was a duplicate comdat section.
|
||||||
|
bool Discarded;
|
||||||
|
|
||||||
// Used by the garbage collector.
|
// Used by the garbage collector.
|
||||||
bool Live;
|
bool Live;
|
||||||
|
|
||||||
// Used for ICF (Identical COMDAT Folding)
|
// Used for ICF (Identical COMDAT Folding)
|
||||||
void replace(SectionChunk *Other);
|
void replace(SectionChunk *Other);
|
||||||
uint32_t Class[2] = {0, 0};
|
uint32_t Class[2] = {0, 0};
|
||||||
|
|
||||||
|
// Sym points to a section symbol if this is a COMDAT chunk.
|
||||||
|
DefinedRegular *Sym = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
// A chunk for common symbols. Common chunks don't have actual data.
|
// A chunk for common symbols. Common chunks don't have actual data.
|
||||||
|
|
|
@ -119,158 +119,86 @@ void ObjFile::parse() {
|
||||||
initializeSEH();
|
initializeSEH();
|
||||||
}
|
}
|
||||||
|
|
||||||
// We set SectionChunk pointers in the SparseChunks vector to this value
|
|
||||||
// temporarily to mark comdat sections as having an unknown resolution. As we
|
|
||||||
// walk the object file's symbol table, once we visit either a leader symbol or
|
|
||||||
// an associative section definition together with the parent comdat's leader,
|
|
||||||
// we set the pointer to either nullptr (to mark the section as discarded) or a
|
|
||||||
// valid SectionChunk for that section.
|
|
||||||
static SectionChunk *const PendingComdat = reinterpret_cast<SectionChunk *>(1);
|
|
||||||
|
|
||||||
void ObjFile::initializeChunks() {
|
void ObjFile::initializeChunks() {
|
||||||
uint32_t NumSections = COFFObj->getNumberOfSections();
|
uint32_t NumSections = COFFObj->getNumberOfSections();
|
||||||
Chunks.reserve(NumSections);
|
Chunks.reserve(NumSections);
|
||||||
SparseChunks.resize(NumSections + 1);
|
SparseChunks.resize(NumSections + 1);
|
||||||
for (uint32_t I = 1; I < NumSections + 1; ++I) {
|
for (uint32_t I = 1; I < NumSections + 1; ++I) {
|
||||||
const coff_section *Sec;
|
const coff_section *Sec;
|
||||||
|
StringRef Name;
|
||||||
if (auto EC = COFFObj->getSection(I, Sec))
|
if (auto EC = COFFObj->getSection(I, Sec))
|
||||||
fatal("getSection failed: #" + Twine(I) + ": " + EC.message());
|
fatal("getSection failed: #" + Twine(I) + ": " + EC.message());
|
||||||
|
if (auto EC = COFFObj->getSectionName(Sec, Name))
|
||||||
|
fatal("getSectionName failed: #" + Twine(I) + ": " + EC.message());
|
||||||
|
if (Name == ".sxdata") {
|
||||||
|
SXData = Sec;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (Name == ".drectve") {
|
||||||
|
ArrayRef<uint8_t> Data;
|
||||||
|
COFFObj->getSectionContents(Sec, Data);
|
||||||
|
Directives = std::string((const char *)Data.data(), Data.size());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (Sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
|
// Object files may have DWARF debug info or MS CodeView debug info
|
||||||
SparseChunks[I] = PendingComdat;
|
// (or both).
|
||||||
|
//
|
||||||
|
// DWARF sections don't need any special handling from the perspective
|
||||||
|
// of the linker; they are just a data section containing relocations.
|
||||||
|
// We can just link them to complete debug info.
|
||||||
|
//
|
||||||
|
// CodeView needs a linker support. We need to interpret and debug
|
||||||
|
// info, and then write it to a separate .pdb file.
|
||||||
|
|
||||||
|
// Ignore debug info unless /debug is given.
|
||||||
|
if (!Config->Debug && Name.startswith(".debug"))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (Sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
|
||||||
|
continue;
|
||||||
|
auto *C = make<SectionChunk>(this, Sec);
|
||||||
|
|
||||||
|
// CodeView sections are stored to a different vector because they are not
|
||||||
|
// linked in the regular manner.
|
||||||
|
if (C->isCodeView())
|
||||||
|
DebugChunks.push_back(C);
|
||||||
else
|
else
|
||||||
SparseChunks[I] = readSection(I, nullptr);
|
Chunks.push_back(C);
|
||||||
|
|
||||||
|
SparseChunks[I] = C;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SectionChunk *ObjFile::readSection(uint32_t SectionNumber,
|
|
||||||
const coff_aux_section_definition *Def) {
|
|
||||||
const coff_section *Sec;
|
|
||||||
StringRef Name;
|
|
||||||
if (auto EC = COFFObj->getSection(SectionNumber, Sec))
|
|
||||||
fatal("getSection failed: #" + Twine(SectionNumber) + ": " + EC.message());
|
|
||||||
if (auto EC = COFFObj->getSectionName(Sec, Name))
|
|
||||||
fatal("getSectionName failed: #" + Twine(SectionNumber) + ": " +
|
|
||||||
EC.message());
|
|
||||||
if (Name == ".sxdata") {
|
|
||||||
SXData = Sec;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
if (Name == ".drectve") {
|
|
||||||
ArrayRef<uint8_t> Data;
|
|
||||||
COFFObj->getSectionContents(Sec, Data);
|
|
||||||
Directives = std::string((const char *)Data.data(), Data.size());
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Object files may have DWARF debug info or MS CodeView debug info
|
|
||||||
// (or both).
|
|
||||||
//
|
|
||||||
// DWARF sections don't need any special handling from the perspective
|
|
||||||
// of the linker; they are just a data section containing relocations.
|
|
||||||
// We can just link them to complete debug info.
|
|
||||||
//
|
|
||||||
// CodeView needs a linker support. We need to interpret and debug
|
|
||||||
// info, and then write it to a separate .pdb file.
|
|
||||||
|
|
||||||
// Ignore debug info unless /debug is given.
|
|
||||||
if (!Config->Debug && Name.startswith(".debug"))
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
if (Sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
|
|
||||||
return nullptr;
|
|
||||||
auto *C = make<SectionChunk>(this, Sec);
|
|
||||||
if (Def)
|
|
||||||
C->Checksum = Def->CheckSum;
|
|
||||||
|
|
||||||
// CodeView sections are stored to a different vector because they are not
|
|
||||||
// linked in the regular manner.
|
|
||||||
if (C->isCodeView())
|
|
||||||
DebugChunks.push_back(C);
|
|
||||||
else
|
|
||||||
Chunks.push_back(C);
|
|
||||||
|
|
||||||
return C;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjFile::readAssociativeDefinition(
|
|
||||||
COFFSymbolRef Sym, const coff_aux_section_definition *Def) {
|
|
||||||
SectionChunk *Parent = SparseChunks[Def->getNumber(Sym.isBigObj())];
|
|
||||||
|
|
||||||
// If the parent is pending, it probably means that its section definition
|
|
||||||
// appears after us in the symbol table. Leave the associated section as
|
|
||||||
// pending; we will handle it during the second pass in initializeSymbols().
|
|
||||||
if (Parent == PendingComdat)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Check whether the parent is prevailing. If it is, so are we, and we read
|
|
||||||
// the section; otherwise mark it as discarded.
|
|
||||||
int32_t SectionNumber = Sym.getSectionNumber();
|
|
||||||
if (Parent) {
|
|
||||||
SparseChunks[SectionNumber] = readSection(SectionNumber, Def);
|
|
||||||
Parent->addAssociative(SparseChunks[SectionNumber]);
|
|
||||||
} else {
|
|
||||||
SparseChunks[SectionNumber] = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Symbol *ObjFile::createRegular(COFFSymbolRef Sym) {
|
|
||||||
SectionChunk *SC = SparseChunks[Sym.getSectionNumber()];
|
|
||||||
if (Sym.isExternal()) {
|
|
||||||
StringRef Name;
|
|
||||||
COFFObj->getSymbolName(Sym, Name);
|
|
||||||
if (SC)
|
|
||||||
return Symtab->addRegular(this, Name, Sym.getGeneric(), SC);
|
|
||||||
return Symtab->addUndefined(Name, this, false);
|
|
||||||
}
|
|
||||||
if (SC)
|
|
||||||
return make<DefinedRegular>(this, /*Name*/ "", false,
|
|
||||||
/*IsExternal*/ false, Sym.getGeneric(), SC);
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ObjFile::initializeSymbols() {
|
void ObjFile::initializeSymbols() {
|
||||||
uint32_t NumSymbols = COFFObj->getNumberOfSymbols();
|
uint32_t NumSymbols = COFFObj->getNumberOfSymbols();
|
||||||
Symbols.resize(NumSymbols);
|
Symbols.resize(NumSymbols);
|
||||||
|
|
||||||
SmallVector<std::pair<Symbol *, uint32_t>, 8> WeakAliases;
|
SmallVector<std::pair<Symbol *, uint32_t>, 8> WeakAliases;
|
||||||
std::vector<uint32_t> PendingIndexes;
|
int32_t LastSectionNumber = 0;
|
||||||
PendingIndexes.reserve(NumSymbols);
|
|
||||||
|
|
||||||
std::vector<const coff_aux_section_definition *> ComdatDefs(
|
|
||||||
COFFObj->getNumberOfSections() + 1);
|
|
||||||
|
|
||||||
for (uint32_t I = 0; I < NumSymbols; ++I) {
|
for (uint32_t I = 0; I < NumSymbols; ++I) {
|
||||||
COFFSymbolRef COFFSym = check(COFFObj->getSymbol(I));
|
COFFSymbolRef COFFSym = check(COFFObj->getSymbol(I));
|
||||||
if (COFFSym.isUndefined()) {
|
|
||||||
Symbols[I] = createUndefined(COFFSym);
|
|
||||||
} else if (COFFSym.isWeakExternal()) {
|
|
||||||
Symbols[I] = createUndefined(COFFSym);
|
|
||||||
uint32_t TagIndex = COFFSym.getAux<coff_aux_weak_external>()->TagIndex;
|
|
||||||
WeakAliases.emplace_back(Symbols[I], TagIndex);
|
|
||||||
} else if (Optional<Symbol *> OptSym = createDefined(COFFSym, ComdatDefs)) {
|
|
||||||
Symbols[I] = *OptSym;
|
|
||||||
} else {
|
|
||||||
// createDefined() returns None if a symbol belongs to a section that
|
|
||||||
// was pending at the point when the symbol was read. This can happen in
|
|
||||||
// two cases:
|
|
||||||
// 1) section definition symbol for a comdat leader;
|
|
||||||
// 2) symbol belongs to a comdat section associated with a section whose
|
|
||||||
// section definition symbol appears later in the symbol table.
|
|
||||||
// In both of these cases, we can expect the section to be resolved by
|
|
||||||
// the time we finish visiting the remaining symbols in the symbol
|
|
||||||
// table. So we postpone the handling of this symbol until that time.
|
|
||||||
PendingIndexes.push_back(I);
|
|
||||||
}
|
|
||||||
I += COFFSym.getNumberOfAuxSymbols();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (uint32_t I : PendingIndexes) {
|
const void *AuxP = nullptr;
|
||||||
COFFSymbolRef Sym = check(COFFObj->getSymbol(I));
|
if (COFFSym.getNumberOfAuxSymbols())
|
||||||
if (auto *Def = Sym.getSectionDefinition())
|
AuxP = check(COFFObj->getSymbol(I + 1)).getRawPtr();
|
||||||
if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
|
bool IsFirst = (LastSectionNumber != COFFSym.getSectionNumber());
|
||||||
readAssociativeDefinition(Sym, Def);
|
|
||||||
Symbols[I] = createRegular(Sym);
|
Symbol *Sym = nullptr;
|
||||||
|
if (COFFSym.isUndefined()) {
|
||||||
|
Sym = createUndefined(COFFSym);
|
||||||
|
} else if (COFFSym.isWeakExternal()) {
|
||||||
|
Sym = createUndefined(COFFSym);
|
||||||
|
uint32_t TagIndex =
|
||||||
|
static_cast<const coff_aux_weak_external *>(AuxP)->TagIndex;
|
||||||
|
WeakAliases.emplace_back(Sym, TagIndex);
|
||||||
|
} else {
|
||||||
|
Sym = createDefined(COFFSym, AuxP, IsFirst);
|
||||||
|
}
|
||||||
|
Symbols[I] = Sym;
|
||||||
|
I += COFFSym.getNumberOfAuxSymbols();
|
||||||
|
LastSectionNumber = COFFSym.getSectionNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &KV : WeakAliases) {
|
for (auto &KV : WeakAliases) {
|
||||||
|
@ -286,9 +214,8 @@ Symbol *ObjFile::createUndefined(COFFSymbolRef Sym) {
|
||||||
return Symtab->addUndefined(Name, this, Sym.isWeakExternal());
|
return Symtab->addUndefined(Name, this, Sym.isWeakExternal());
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<Symbol *> ObjFile::createDefined(
|
Symbol *ObjFile::createDefined(COFFSymbolRef Sym, const void *AuxP,
|
||||||
COFFSymbolRef Sym,
|
bool IsFirst) {
|
||||||
std::vector<const coff_aux_section_definition *> &ComdatDefs) {
|
|
||||||
StringRef Name;
|
StringRef Name;
|
||||||
if (Sym.isCommon()) {
|
if (Sym.isCommon()) {
|
||||||
auto *C = make<CommonChunk>(Sym);
|
auto *C = make<CommonChunk>(Sym);
|
||||||
|
@ -327,46 +254,37 @@ Optional<Symbol *> ObjFile::createDefined(
|
||||||
if ((uint32_t)SectionNumber >= SparseChunks.size())
|
if ((uint32_t)SectionNumber >= SparseChunks.size())
|
||||||
fatal("broken object file: " + toString(this));
|
fatal("broken object file: " + toString(this));
|
||||||
|
|
||||||
// Handle comdat leader symbols.
|
// Nothing else to do without a section chunk.
|
||||||
if (const coff_aux_section_definition *Def = ComdatDefs[SectionNumber]) {
|
auto *SC = SparseChunks[SectionNumber];
|
||||||
ComdatDefs[SectionNumber] = nullptr;
|
if (!SC)
|
||||||
Symbol *Leader;
|
return nullptr;
|
||||||
bool Prevailing;
|
|
||||||
if (Sym.isExternal()) {
|
// Handle section definitions
|
||||||
COFFObj->getSymbolName(Sym, Name);
|
if (IsFirst && AuxP) {
|
||||||
std::tie(Leader, Prevailing) =
|
auto *Aux = reinterpret_cast<const coff_aux_section_definition *>(AuxP);
|
||||||
Symtab->addComdat(this, Name, Sym.getGeneric());
|
if (Aux->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
|
||||||
} else {
|
if (auto *ParentSC = SparseChunks[Aux->getNumber(Sym.isBigObj())]) {
|
||||||
Leader = make<DefinedRegular>(this, /*Name*/ "", false,
|
ParentSC->addAssociative(SC);
|
||||||
/*IsExternal*/ false, Sym.getGeneric());
|
// If we already discarded the parent, discard the child.
|
||||||
Prevailing = true;
|
if (ParentSC->isDiscarded())
|
||||||
}
|
SC->markDiscarded();
|
||||||
if (Prevailing) {
|
}
|
||||||
SectionChunk *C = readSection(SectionNumber, Def);
|
SC->Checksum = Aux->CheckSum;
|
||||||
SparseChunks[SectionNumber] = C;
|
|
||||||
C->Sym = cast<DefinedRegular>(Leader);
|
|
||||||
cast<DefinedRegular>(Leader)->Data = &C->Repl;
|
|
||||||
} else {
|
|
||||||
SparseChunks[SectionNumber] = nullptr;
|
|
||||||
}
|
|
||||||
return Leader;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read associative section definitions and prepare to handle the comdat
|
DefinedRegular *B;
|
||||||
// leader symbol by setting the section's ComdatDefs pointer if we encounter a
|
if (Sym.isExternal()) {
|
||||||
// non-associative comdat.
|
COFFObj->getSymbolName(Sym, Name);
|
||||||
if (SparseChunks[SectionNumber] == PendingComdat) {
|
Symbol *S =
|
||||||
if (auto *Def = Sym.getSectionDefinition()) {
|
Symtab->addRegular(this, Name, SC->isCOMDAT(), Sym.getGeneric(), SC);
|
||||||
if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
|
B = cast<DefinedRegular>(S);
|
||||||
readAssociativeDefinition(Sym, Def);
|
} else
|
||||||
else
|
B = make<DefinedRegular>(this, /*Name*/ "", SC->isCOMDAT(),
|
||||||
ComdatDefs[SectionNumber] = Def;
|
/*IsExternal*/ false, Sym.getGeneric(), SC);
|
||||||
}
|
if (SC->isCOMDAT() && Sym.getValue() == 0 && !AuxP)
|
||||||
}
|
SC->setSymbol(B);
|
||||||
|
|
||||||
if (SparseChunks[SectionNumber] == PendingComdat)
|
return B;
|
||||||
return None;
|
|
||||||
return createRegular(Sym);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ObjFile::initializeSEH() {
|
void ObjFile::initializeSEH() {
|
||||||
|
@ -443,12 +361,8 @@ void ImportFile::parse() {
|
||||||
void BitcodeFile::parse() {
|
void BitcodeFile::parse() {
|
||||||
Obj = check(lto::InputFile::create(MemoryBufferRef(
|
Obj = check(lto::InputFile::create(MemoryBufferRef(
|
||||||
MB.getBuffer(), Saver.save(ParentName + MB.getBufferIdentifier()))));
|
MB.getBuffer(), Saver.save(ParentName + MB.getBufferIdentifier()))));
|
||||||
std::vector<std::pair<Symbol *, bool>> Comdat(Obj->getComdatTable().size());
|
|
||||||
for (size_t I = 0; I != Obj->getComdatTable().size(); ++I)
|
|
||||||
Comdat[I] = Symtab->addComdat(this, Saver.save(Obj->getComdatTable()[I]));
|
|
||||||
for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) {
|
for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) {
|
||||||
StringRef SymName = Saver.save(ObjSym.getName());
|
StringRef SymName = Saver.save(ObjSym.getName());
|
||||||
int ComdatIndex = ObjSym.getComdatIndex();
|
|
||||||
Symbol *Sym;
|
Symbol *Sym;
|
||||||
if (ObjSym.isUndefined()) {
|
if (ObjSym.isUndefined()) {
|
||||||
Sym = Symtab->addUndefined(SymName, this, false);
|
Sym = Symtab->addUndefined(SymName, this, false);
|
||||||
|
@ -460,15 +374,9 @@ void BitcodeFile::parse() {
|
||||||
std::string Fallback = ObjSym.getCOFFWeakExternalFallback();
|
std::string Fallback = ObjSym.getCOFFWeakExternalFallback();
|
||||||
Symbol *Alias = Symtab->addUndefined(Saver.save(Fallback));
|
Symbol *Alias = Symtab->addUndefined(Saver.save(Fallback));
|
||||||
checkAndSetWeakAlias(Symtab, this, Sym, Alias);
|
checkAndSetWeakAlias(Symtab, this, Sym, Alias);
|
||||||
} else if (ComdatIndex != -1) {
|
|
||||||
if (SymName == Obj->getComdatTable()[ComdatIndex])
|
|
||||||
Sym = Comdat[ComdatIndex].first;
|
|
||||||
else if (Comdat[ComdatIndex].second)
|
|
||||||
Sym = Symtab->addRegular(this, SymName);
|
|
||||||
else
|
|
||||||
Sym = Symtab->addUndefined(SymName, this, false);
|
|
||||||
} else {
|
} else {
|
||||||
Sym = Symtab->addRegular(this, SymName);
|
bool IsCOMDAT = ObjSym.getComdatIndex() != -1;
|
||||||
|
Sym = Symtab->addRegular(this, SymName, IsCOMDAT);
|
||||||
}
|
}
|
||||||
SymbolBodies.push_back(Sym);
|
SymbolBodies.push_back(Sym);
|
||||||
}
|
}
|
||||||
|
|
|
@ -142,19 +142,7 @@ private:
|
||||||
void initializeSymbols();
|
void initializeSymbols();
|
||||||
void initializeSEH();
|
void initializeSEH();
|
||||||
|
|
||||||
SectionChunk *
|
Symbol *createDefined(COFFSymbolRef Sym, const void *Aux, bool IsFirst);
|
||||||
readSection(uint32_t SectionNumber,
|
|
||||||
const llvm::object::coff_aux_section_definition *Def);
|
|
||||||
|
|
||||||
void readAssociativeDefinition(
|
|
||||||
COFFSymbolRef COFFSym,
|
|
||||||
const llvm::object::coff_aux_section_definition *Def);
|
|
||||||
|
|
||||||
llvm::Optional<Symbol *>
|
|
||||||
createDefined(COFFSymbolRef Sym,
|
|
||||||
std::vector<const llvm::object::coff_aux_section_definition *>
|
|
||||||
&ComdatDefs);
|
|
||||||
Symbol *createRegular(COFFSymbolRef Sym);
|
|
||||||
Symbol *createUndefined(COFFSymbolRef Sym);
|
Symbol *createUndefined(COFFSymbolRef Sym);
|
||||||
|
|
||||||
std::unique_ptr<COFFObjectFile> COFFObj;
|
std::unique_ptr<COFFObjectFile> COFFObj;
|
||||||
|
|
|
@ -52,6 +52,13 @@ void markLive(const std::vector<Chunk *> &Chunks) {
|
||||||
|
|
||||||
while (!Worklist.empty()) {
|
while (!Worklist.empty()) {
|
||||||
SectionChunk *SC = Worklist.pop_back_val();
|
SectionChunk *SC = Worklist.pop_back_val();
|
||||||
|
|
||||||
|
// If this section was discarded, there are relocations referring to
|
||||||
|
// discarded sections. Ignore these sections to avoid crashing. They will be
|
||||||
|
// diagnosed during relocation processing.
|
||||||
|
if (SC->isDiscarded())
|
||||||
|
continue;
|
||||||
|
|
||||||
assert(SC->isLive() && "We mark as live when pushing onto the worklist!");
|
assert(SC->isLive() && "We mark as live when pushing onto the worklist!");
|
||||||
|
|
||||||
// Mark all symbols listed in the relocation table for this section.
|
// Mark all symbols listed in the relocation table for this section.
|
||||||
|
|
|
@ -24,6 +24,36 @@ using namespace llvm;
|
||||||
namespace lld {
|
namespace lld {
|
||||||
namespace coff {
|
namespace coff {
|
||||||
|
|
||||||
|
enum SymbolPreference {
|
||||||
|
SP_EXISTING = -1,
|
||||||
|
SP_CONFLICT = 0,
|
||||||
|
SP_NEW = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Checks if an existing symbol S should be kept or replaced by a new symbol.
|
||||||
|
/// Returns SP_EXISTING when S should be kept, SP_NEW when the new symbol
|
||||||
|
/// should be kept, and SP_CONFLICT if no valid resolution exists.
|
||||||
|
static SymbolPreference compareDefined(Symbol *S, bool WasInserted,
|
||||||
|
bool NewIsCOMDAT) {
|
||||||
|
// If the symbol wasn't previously known, the new symbol wins by default.
|
||||||
|
if (WasInserted || !isa<Defined>(S))
|
||||||
|
return SP_NEW;
|
||||||
|
|
||||||
|
// If the existing symbol is a DefinedRegular, both it and the new symbol
|
||||||
|
// must be comdats. In that case, we have no reason to prefer one symbol
|
||||||
|
// over the other, and we keep the existing one. If one of the symbols
|
||||||
|
// is not a comdat, we report a conflict.
|
||||||
|
if (auto *R = dyn_cast<DefinedRegular>(S)) {
|
||||||
|
if (NewIsCOMDAT && R->isCOMDAT())
|
||||||
|
return SP_EXISTING;
|
||||||
|
else
|
||||||
|
return SP_CONFLICT;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Existing symbol is not a DefinedRegular; new symbol wins.
|
||||||
|
return SP_NEW;
|
||||||
|
}
|
||||||
|
|
||||||
SymbolTable *Symtab;
|
SymbolTable *Symtab;
|
||||||
|
|
||||||
void SymbolTable::addFile(InputFile *File) {
|
void SymbolTable::addFile(InputFile *File) {
|
||||||
|
@ -210,7 +240,7 @@ Symbol *SymbolTable::addSynthetic(StringRef N, Chunk *C) {
|
||||||
return S;
|
return S;
|
||||||
}
|
}
|
||||||
|
|
||||||
Symbol *SymbolTable::addRegular(InputFile *F, StringRef N,
|
Symbol *SymbolTable::addRegular(InputFile *F, StringRef N, bool IsCOMDAT,
|
||||||
const coff_symbol_generic *Sym,
|
const coff_symbol_generic *Sym,
|
||||||
SectionChunk *C) {
|
SectionChunk *C) {
|
||||||
Symbol *S;
|
Symbol *S;
|
||||||
|
@ -218,30 +248,20 @@ Symbol *SymbolTable::addRegular(InputFile *F, StringRef N,
|
||||||
std::tie(S, WasInserted) = insert(N);
|
std::tie(S, WasInserted) = insert(N);
|
||||||
if (!isa<BitcodeFile>(F))
|
if (!isa<BitcodeFile>(F))
|
||||||
S->IsUsedInRegularObj = true;
|
S->IsUsedInRegularObj = true;
|
||||||
if (WasInserted || !isa<DefinedRegular>(S))
|
SymbolPreference SP = compareDefined(S, WasInserted, IsCOMDAT);
|
||||||
replaceSymbol<DefinedRegular>(S, F, N, /*IsCOMDAT*/ false,
|
if (SP == SP_CONFLICT) {
|
||||||
/*IsExternal*/ true, Sym, C);
|
|
||||||
else
|
|
||||||
reportDuplicate(S, F);
|
reportDuplicate(S, F);
|
||||||
return S;
|
} else if (SP == SP_NEW) {
|
||||||
}
|
replaceSymbol<DefinedRegular>(S, F, N, IsCOMDAT, /*IsExternal*/ true, Sym,
|
||||||
|
C);
|
||||||
std::pair<Symbol *, bool>
|
} else if (SP == SP_EXISTING && IsCOMDAT && C) {
|
||||||
SymbolTable::addComdat(InputFile *F, StringRef N,
|
C->markDiscarded();
|
||||||
const coff_symbol_generic *Sym) {
|
// Discard associative chunks that we've parsed so far. No need to recurse
|
||||||
Symbol *S;
|
// because an associative section cannot have children.
|
||||||
bool WasInserted;
|
for (SectionChunk *Child : C->children())
|
||||||
std::tie(S, WasInserted) = insert(N);
|
Child->markDiscarded();
|
||||||
if (!isa<BitcodeFile>(F))
|
|
||||||
S->IsUsedInRegularObj = true;
|
|
||||||
if (WasInserted || !isa<DefinedRegular>(S)) {
|
|
||||||
replaceSymbol<DefinedRegular>(S, F, N, /*IsCOMDAT*/ true,
|
|
||||||
/*IsExternal*/ true, Sym, nullptr);
|
|
||||||
return {S, true};
|
|
||||||
}
|
}
|
||||||
if (!cast<DefinedRegular>(S)->isCOMDAT())
|
return S;
|
||||||
reportDuplicate(S, F);
|
|
||||||
return {S, false};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Symbol *SymbolTable::addCommon(InputFile *F, StringRef N, uint64_t Size,
|
Symbol *SymbolTable::addCommon(InputFile *F, StringRef N, uint64_t Size,
|
||||||
|
|
|
@ -83,12 +83,9 @@ public:
|
||||||
Symbol *addUndefined(StringRef Name, InputFile *F, bool IsWeakAlias);
|
Symbol *addUndefined(StringRef Name, InputFile *F, bool IsWeakAlias);
|
||||||
void addLazy(ArchiveFile *F, const Archive::Symbol Sym);
|
void addLazy(ArchiveFile *F, const Archive::Symbol Sym);
|
||||||
Symbol *addAbsolute(StringRef N, COFFSymbolRef S);
|
Symbol *addAbsolute(StringRef N, COFFSymbolRef S);
|
||||||
Symbol *addRegular(InputFile *F, StringRef N,
|
Symbol *addRegular(InputFile *F, StringRef N, bool IsCOMDAT,
|
||||||
const llvm::object::coff_symbol_generic *S = nullptr,
|
const llvm::object::coff_symbol_generic *S = nullptr,
|
||||||
SectionChunk *C = nullptr);
|
SectionChunk *C = nullptr);
|
||||||
std::pair<Symbol *, bool>
|
|
||||||
addComdat(InputFile *F, StringRef N,
|
|
||||||
const llvm::object::coff_symbol_generic *S = nullptr);
|
|
||||||
Symbol *addCommon(InputFile *F, StringRef N, uint64_t Size,
|
Symbol *addCommon(InputFile *F, StringRef N, uint64_t Size,
|
||||||
const llvm::object::coff_symbol_generic *S = nullptr,
|
const llvm::object::coff_symbol_generic *S = nullptr,
|
||||||
CommonChunk *C = nullptr);
|
CommonChunk *C = nullptr);
|
||||||
|
|
|
@ -169,6 +169,7 @@ public:
|
||||||
SectionChunk *getChunk() const { return *Data; }
|
SectionChunk *getChunk() const { return *Data; }
|
||||||
uint32_t getValue() const { return Sym->Value; }
|
uint32_t getValue() const { return Sym->Value; }
|
||||||
|
|
||||||
|
private:
|
||||||
SectionChunk **Data;
|
SectionChunk **Data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ main_global:
|
||||||
|
|
||||||
.section .CRT$XCU,"dr",associative,main_global
|
.section .CRT$XCU,"dr",associative,main_global
|
||||||
.p2align 3
|
.p2align 3
|
||||||
|
.globl assoc_global
|
||||||
assoc_global:
|
assoc_global:
|
||||||
.quad main_global
|
.quad main_global
|
||||||
|
|
||||||
|
|
|
@ -275,8 +275,6 @@ struct coff_symbol_generic {
|
||||||
support::ulittle32_t Value;
|
support::ulittle32_t Value;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct coff_aux_section_definition;
|
|
||||||
|
|
||||||
class COFFSymbolRef {
|
class COFFSymbolRef {
|
||||||
public:
|
public:
|
||||||
COFFSymbolRef() = default;
|
COFFSymbolRef() = default;
|
||||||
|
@ -348,18 +346,6 @@ public:
|
||||||
return (getType() & 0xF0) >> COFF::SCT_COMPLEX_TYPE_SHIFT;
|
return (getType() & 0xF0) >> COFF::SCT_COMPLEX_TYPE_SHIFT;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> const T *getAux() const {
|
|
||||||
return CS16 ? reinterpret_cast<const T *>(CS16 + 1)
|
|
||||||
: reinterpret_cast<const T *>(CS32 + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
const coff_aux_section_definition *getSectionDefinition() const {
|
|
||||||
if (!getNumberOfAuxSymbols() ||
|
|
||||||
getStorageClass() != COFF::IMAGE_SYM_CLASS_STATIC)
|
|
||||||
return nullptr;
|
|
||||||
return getAux<coff_aux_section_definition>();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isAbsolute() const {
|
bool isAbsolute() const {
|
||||||
return getSectionNumber() == -1;
|
return getSectionNumber() == -1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue