Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b71533

Browse files
committed
[LLD][COFF] add __{data,bss}_{start,end}__ symbols for Cygwin support
Cygwin requires these symbols for its fork emulation to know what data to copy into the child. GNU ld defines these symbols for MinGW targets also, so do the same here. Signed-off-by: Jeremy Drake <[email protected]>
1 parent 2667845 commit 7b71533

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lld/COFF/Driver.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -2039,6 +2039,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
20392039
parseMerge(".ctors=.rdata");
20402040
parseMerge(".dtors=.rdata");
20412041
parseMerge(".CRT=.rdata");
2042+
parseMerge(".data_cygwin_nocopy=.data");
20422043
}
20432044

20442045
// Handle /section
@@ -2495,6 +2496,10 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
24952496
if (config->mingw) {
24962497
symtab.addAbsolute(symtab.mangle("__CTOR_LIST__"), 0);
24972498
symtab.addAbsolute(symtab.mangle("__DTOR_LIST__"), 0);
2499+
symtab.addAbsolute("__data_start__", 0);
2500+
symtab.addAbsolute("__data_end__", 0);
2501+
symtab.addAbsolute("__bss_start__", 0);
2502+
symtab.addAbsolute("__bss_end__", 0);
24982503
}
24992504
if (config->debug || config->buildIDHash != BuildIDHash::None)
25002505
if (symtab.findUnderscore("__buildid"))

lld/COFF/Writer.cpp

+31-2
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ class Writer {
239239
void createRuntimePseudoRelocs();
240240
void createECChunks();
241241
void insertCtorDtorSymbols();
242+
void insertBssDataStartEndSymbols();
242243
void markSymbolsWithRelocations(ObjFile *file, SymbolRVASet &usedSymbols);
243244
void createGuardCFTables();
244245
void markSymbolsForRVATable(ObjFile *file,
@@ -314,6 +315,7 @@ class Writer {
314315

315316
OutputSection *textSec;
316317
OutputSection *hexpthkSec;
318+
OutputSection *bssSec;
317319
OutputSection *rdataSec;
318320
OutputSection *buildidSec;
319321
OutputSection *dataSec;
@@ -1077,7 +1079,7 @@ void Writer::createSections() {
10771079
textSec = createSection(".text", code | r | x);
10781080
if (isArm64EC(ctx.config.machine))
10791081
hexpthkSec = createSection(".hexpthk", code | r | x);
1080-
createSection(".bss", bss | r | w);
1082+
bssSec = createSection(".bss", bss | r | w);
10811083
rdataSec = createSection(".rdata", data | r);
10821084
buildidSec = createSection(".buildid", data | r);
10831085
dataSec = createSection(".data", data | r | w);
@@ -1260,8 +1262,10 @@ void Writer::createMiscChunks() {
12601262
if (config->autoImport)
12611263
createRuntimePseudoRelocs();
12621264

1263-
if (config->mingw)
1265+
if (config->mingw) {
12641266
insertCtorDtorSymbols();
1267+
insertBssDataStartEndSymbols();
1268+
}
12651269
}
12661270

12671271
// Create .idata section for the DLL-imported symbol table.
@@ -2369,6 +2373,31 @@ void Writer::insertCtorDtorSymbols() {
23692373
}
23702374
}
23712375

2376+
// MinGW (really, Cygwin) specific.
2377+
// The Cygwin startup code uses __data_start__ __data_end__ __bss_start__
2378+
// and __bss_end__ to know what to copy during fork emulation.
2379+
void Writer::insertBssDataStartEndSymbols() {
2380+
if (!dataSec->chunks.empty()) {
2381+
Symbol *dataStartSym = ctx.symtab.find("__data_start__");
2382+
Symbol *dataEndSym = ctx.symtab.find("__data_end__");
2383+
Chunk *endChunk = dataSec->chunks.back();
2384+
replaceSymbol<DefinedSynthetic>(dataStartSym, dataStartSym->getName(),
2385+
dataSec->chunks.front());
2386+
replaceSymbol<DefinedSynthetic>(dataEndSym, dataEndSym->getName(), endChunk,
2387+
endChunk->getSize());
2388+
}
2389+
2390+
if (!bssSec->chunks.empty()) {
2391+
Symbol *bssStartSym = ctx.symtab.find("__bss_start__");
2392+
Symbol *bssEndSym = ctx.symtab.find("__bss_end__");
2393+
Chunk *endChunk = bssSec->chunks.back();
2394+
replaceSymbol<DefinedSynthetic>(bssStartSym, bssStartSym->getName(),
2395+
bssSec->chunks.front());
2396+
replaceSymbol<DefinedSynthetic>(bssEndSym, bssEndSym->getName(), endChunk,
2397+
endChunk->getSize());
2398+
}
2399+
}
2400+
23722401
// Handles /section options to allow users to overwrite
23732402
// section attributes.
23742403
void Writer::setSectionPermissions() {

0 commit comments

Comments
 (0)