Skip to content

Commit 9015e41

Browse files
committed
[ELF] addRelIpltSymbols: make it explicit some passes are for non-relocatable links. NFC
and prepare for __global_pointer$ and _TLS_MODULE_BASE_ fix.
1 parent 6327d26 commit 9015e41

File tree

1 file changed

+62
-60
lines changed

1 file changed

+62
-60
lines changed

lld/ELF/Writer.cpp

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ void PhdrEntry::add(OutputSection *sec) {
10051005
// need these symbols, since IRELATIVE relocs are resolved through GOT
10061006
// and PLT. For details, see http://www.airs.com/blog/archives/403.
10071007
template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
1008-
if (config->relocatable || config->isPic)
1008+
if (config->isPic)
10091009
return;
10101010

10111011
// By default, __rela_iplt_{start,end} belong to a dummy section 0
@@ -1826,30 +1826,33 @@ static void removeUnusedSyntheticSections() {
18261826

18271827
// Create output section objects and add them to OutputSections.
18281828
template <class ELFT> void Writer<ELFT>::finalizeSections() {
1829-
Out::preinitArray = findSection(".preinit_array");
1830-
Out::initArray = findSection(".init_array");
1831-
Out::finiArray = findSection(".fini_array");
1832-
1833-
// The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1834-
// symbols for sections, so that the runtime can get the start and end
1835-
// addresses of each section by section name. Add such symbols.
18361829
if (!config->relocatable) {
1830+
Out::preinitArray = findSection(".preinit_array");
1831+
Out::initArray = findSection(".init_array");
1832+
Out::finiArray = findSection(".fini_array");
1833+
1834+
// The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1835+
// symbols for sections, so that the runtime can get the start and end
1836+
// addresses of each section by section name. Add such symbols.
18371837
addStartEndSymbols();
18381838
for (SectionCommand *cmd : script->sectionCommands)
18391839
if (auto *osd = dyn_cast<OutputDesc>(cmd))
18401840
addStartStopSymbols(osd->osec);
1841-
}
18421841

1843-
// Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1844-
// It should be okay as no one seems to care about the type.
1845-
// Even the author of gold doesn't remember why gold behaves that way.
1846-
// https://sourceware.org/ml/binutils/2002-03/msg00360.html
1847-
if (mainPart->dynamic->parent)
1848-
symtab.addSymbol(Defined{/*file=*/nullptr, "_DYNAMIC", STB_WEAK, STV_HIDDEN,
1849-
STT_NOTYPE, /*value=*/0, /*size=*/0, mainPart->dynamic.get()})->isUsedInRegularObj = true;
1842+
// Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1843+
// It should be okay as no one seems to care about the type.
1844+
// Even the author of gold doesn't remember why gold behaves that way.
1845+
// https://sourceware.org/ml/binutils/2002-03/msg00360.html
1846+
if (mainPart->dynamic->parent) {
1847+
Symbol *s = symtab.addSymbol(Defined{
1848+
/*file=*/nullptr, "_DYNAMIC", STB_WEAK, STV_HIDDEN, STT_NOTYPE,
1849+
/*value=*/0, /*size=*/0, mainPart->dynamic.get()});
1850+
s->isUsedInRegularObj = true;
1851+
}
18501852

1851-
// Define __rel[a]_iplt_{start,end} symbols if needed.
1852-
addRelIpltSymbols();
1853+
// Define __rel[a]_iplt_{start,end} symbols if needed.
1854+
addRelIpltSymbols();
1855+
}
18531856

18541857
// RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800. This symbol
18551858
// should only be defined in an executable. If .sdata does not exist, its
@@ -1883,67 +1886,66 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
18831886
}
18841887
}
18851888

1886-
{
1889+
if (!config->relocatable) {
18871890
llvm::TimeTraceScope timeScope("Finalize .eh_frame");
18881891
// This responsible for splitting up .eh_frame section into
18891892
// pieces. The relocation scan uses those pieces, so this has to be
18901893
// earlier.
18911894
for (Partition &part : partitions)
18921895
finalizeSynthetic(part.ehFrame.get());
1893-
}
18941896

1895-
if (config->hasDynSymTab) {
1896-
parallelForEach(symtab.getSymbols(), [](Symbol *sym) {
1897-
sym->isPreemptible = computeIsPreemptible(*sym);
1898-
});
1897+
if (config->hasDynSymTab) {
1898+
parallelForEach(symtab.getSymbols(), [](Symbol *sym) {
1899+
sym->isPreemptible = computeIsPreemptible(*sym);
1900+
});
1901+
}
18991902
}
19001903

19011904
// Change values of linker-script-defined symbols from placeholders (assigned
19021905
// by declareSymbols) to actual definitions.
19031906
script->processSymbolAssignments();
19041907

1905-
{
1908+
if (!config->relocatable) {
19061909
llvm::TimeTraceScope timeScope("Scan relocations");
19071910
// Scan relocations. This must be done after every symbol is declared so
19081911
// that we can correctly decide if a dynamic relocation is needed. This is
19091912
// called after processSymbolAssignments() because it needs to know whether
19101913
// a linker-script-defined symbol is absolute.
19111914
ppc64noTocRelax.clear();
1912-
if (!config->relocatable) {
1913-
scanRelocations<ELFT>();
1914-
reportUndefinedSymbols();
1915-
postScanRelocations();
1916-
}
1917-
}
1918-
1919-
if (in.plt && in.plt->isNeeded())
1920-
in.plt->addSymbols();
1921-
if (in.iplt && in.iplt->isNeeded())
1922-
in.iplt->addSymbols();
1923-
1924-
if (config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) {
1925-
auto diagnose =
1926-
config->unresolvedSymbolsInShlib == UnresolvedPolicy::ReportError
1927-
? errorOrWarn
1928-
: warn;
1929-
// Error on undefined symbols in a shared object, if all of its DT_NEEDED
1930-
// entries are seen. These cases would otherwise lead to runtime errors
1931-
// reported by the dynamic linker.
1932-
//
1933-
// ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker to
1934-
// catch more cases. That is too much for us. Our approach resembles the one
1935-
// used in ld.gold, achieves a good balance to be useful but not too smart.
1936-
for (SharedFile *file : ctx.sharedFiles) {
1937-
bool allNeededIsKnown =
1938-
llvm::all_of(file->dtNeeded, [&](StringRef needed) {
1939-
return symtab.soNames.count(CachedHashStringRef(needed));
1940-
});
1941-
if (!allNeededIsKnown)
1942-
continue;
1943-
for (Symbol *sym : file->requiredSymbols)
1944-
if (sym->isUndefined() && !sym->isWeak())
1945-
diagnose("undefined reference due to --no-allow-shlib-undefined: " +
1946-
toString(*sym) + "\n>>> referenced by " + toString(file));
1915+
scanRelocations<ELFT>();
1916+
reportUndefinedSymbols();
1917+
postScanRelocations();
1918+
1919+
if (in.plt && in.plt->isNeeded())
1920+
in.plt->addSymbols();
1921+
if (in.iplt && in.iplt->isNeeded())
1922+
in.iplt->addSymbols();
1923+
1924+
if (config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) {
1925+
auto diagnose =
1926+
config->unresolvedSymbolsInShlib == UnresolvedPolicy::ReportError
1927+
? errorOrWarn
1928+
: warn;
1929+
// Error on undefined symbols in a shared object, if all of its DT_NEEDED
1930+
// entries are seen. These cases would otherwise lead to runtime errors
1931+
// reported by the dynamic linker.
1932+
//
1933+
// ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker
1934+
// to catch more cases. That is too much for us. Our approach resembles
1935+
// the one used in ld.gold, achieves a good balance to be useful but not
1936+
// too smart.
1937+
for (SharedFile *file : ctx.sharedFiles) {
1938+
bool allNeededIsKnown =
1939+
llvm::all_of(file->dtNeeded, [&](StringRef needed) {
1940+
return symtab.soNames.count(CachedHashStringRef(needed));
1941+
});
1942+
if (!allNeededIsKnown)
1943+
continue;
1944+
for (Symbol *sym : file->requiredSymbols)
1945+
if (sym->isUndefined() && !sym->isWeak())
1946+
diagnose("undefined reference due to --no-allow-shlib-undefined: " +
1947+
toString(*sym) + "\n>>> referenced by " + toString(file));
1948+
}
19471949
}
19481950
}
19491951

0 commit comments

Comments
 (0)