@@ -96,7 +96,7 @@ template <class ELFT> void elf::writeResult(Ctx &ctx) {
96
96
Writer<ELFT>(ctx).run ();
97
97
}
98
98
99
- static void removeEmptyPTLoad (SmallVector<PhdrEntry *, 0 > &phdrs) {
99
+ static void removeEmptyPTLoad (Ctx &ctx, SmallVector<PhdrEntry *, 0 > &phdrs) {
100
100
auto it = std::stable_partition (
101
101
phdrs.begin (), phdrs.end (), [&](const PhdrEntry *p) {
102
102
if (p->p_type != PT_LOAD)
@@ -116,7 +116,7 @@ static void removeEmptyPTLoad(SmallVector<PhdrEntry *, 0> &phdrs) {
116
116
phdrs.erase (it, phdrs.end ());
117
117
}
118
118
119
- void elf::copySectionsIntoPartitions () {
119
+ void elf::copySectionsIntoPartitions (Ctx &ctx ) {
120
120
SmallVector<InputSectionBase *, 0 > newSections;
121
121
const size_t ehSize = ctx.ehInputSections .size ();
122
122
for (unsigned part = 2 ; part != ctx.partitions .size () + 1 ; ++part) {
@@ -139,7 +139,7 @@ void elf::copySectionsIntoPartitions() {
139
139
newSections.end ());
140
140
}
141
141
142
- static Defined *addOptionalRegular (StringRef name, SectionBase *sec,
142
+ static Defined *addOptionalRegular (Ctx &ctx, StringRef name, SectionBase *sec,
143
143
uint64_t val, uint8_t stOther = STV_HIDDEN) {
144
144
Symbol *s = ctx.symtab ->find (name);
145
145
if (!s || s->isDefined () || s->isCommon ())
@@ -154,9 +154,9 @@ static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
154
154
155
155
// The linker is expected to define some symbols depending on
156
156
// the linking result. This function defines such symbols.
157
- void elf::addReservedSymbols () {
157
+ void elf::addReservedSymbols (Ctx &ctx ) {
158
158
if (ctx.arg .emachine == EM_MIPS) {
159
- auto addAbsolute = [](StringRef name) {
159
+ auto addAbsolute = [& ](StringRef name) {
160
160
Symbol *sym =
161
161
ctx.symtab ->addSymbol (Defined{ctx.internalFile , name, STB_GLOBAL,
162
162
STV_HIDDEN, STT_NOTYPE, 0 , 0 , nullptr });
@@ -184,7 +184,7 @@ void elf::addReservedSymbols() {
184
184
} else if (ctx.arg .emachine == EM_PPC) {
185
185
// glibc *crt1.o has a undefined reference to _SDA_BASE_. Since we don't
186
186
// support Small Data Area, define it arbitrarily as 0.
187
- addOptionalRegular (" _SDA_BASE_" , nullptr , 0 , STV_HIDDEN);
187
+ addOptionalRegular (ctx, " _SDA_BASE_" , nullptr , 0 , STV_HIDDEN);
188
188
} else if (ctx.arg .emachine == EM_PPC64) {
189
189
addPPC64SaveRestore ();
190
190
}
@@ -220,23 +220,24 @@ void elf::addReservedSymbols() {
220
220
// this symbol unconditionally even when using a linker script, which
221
221
// differs from the behavior implemented by GNU linker which only define
222
222
// this symbol if ELF headers are in the memory mapped segment.
223
- addOptionalRegular (" __ehdr_start" , ctx.out .elfHeader , 0 , STV_HIDDEN);
223
+ addOptionalRegular (ctx, " __ehdr_start" , ctx.out .elfHeader , 0 , STV_HIDDEN);
224
224
225
225
// __executable_start is not documented, but the expectation of at
226
226
// least the Android libc is that it points to the ELF header.
227
- addOptionalRegular (" __executable_start" , ctx.out .elfHeader , 0 , STV_HIDDEN);
227
+ addOptionalRegular (ctx, " __executable_start" , ctx.out .elfHeader , 0 ,
228
+ STV_HIDDEN);
228
229
229
230
// __dso_handle symbol is passed to cxa_finalize as a marker to identify
230
231
// each DSO. The address of the symbol doesn't matter as long as they are
231
232
// different in different DSOs, so we chose the start address of the DSO.
232
- addOptionalRegular (" __dso_handle" , ctx.out .elfHeader , 0 , STV_HIDDEN);
233
+ addOptionalRegular (ctx, " __dso_handle" , ctx.out .elfHeader , 0 , STV_HIDDEN);
233
234
234
235
// If linker script do layout we do not need to create any standard symbols.
235
236
if (ctx.script ->hasSectionsCommand )
236
237
return ;
237
238
238
- auto add = [](StringRef s, int64_t pos) {
239
- return addOptionalRegular (s, ctx.out .elfHeader , pos, STV_DEFAULT);
239
+ auto add = [& ](StringRef s, int64_t pos) {
240
+ return addOptionalRegular (ctx, s, ctx.out .elfHeader , pos, STV_DEFAULT);
240
241
};
241
242
242
243
ctx.sym .bss = add (" __bss_start" , 0 );
@@ -270,7 +271,7 @@ static void demoteDefined(Defined &sym, DenseMap<SectionBase *, size_t> &map) {
270
271
//
271
272
// In addition, demote symbols defined in discarded sections, so that
272
273
// references to /DISCARD/ discarded symbols will lead to errors.
273
- static void demoteSymbolsAndComputeIsPreemptible () {
274
+ static void demoteSymbolsAndComputeIsPreemptible (Ctx &ctx ) {
274
275
llvm::TimeTraceScope timeScope (" Demote symbols" );
275
276
DenseMap<InputFile *, DenseMap<SectionBase *, size_t >> sectionIndexMap;
276
277
for (Symbol *sym : ctx.symtab ->getSymbols ()) {
@@ -322,7 +323,7 @@ template <class ELFT> void Writer<ELFT>::run() {
322
323
// 0 sized region. This has to be done late since only after assignAddresses
323
324
// we know the size of the sections.
324
325
for (Partition &part : ctx.partitions )
325
- removeEmptyPTLoad (part.phdrs );
326
+ removeEmptyPTLoad (ctx, part.phdrs );
326
327
327
328
if (!ctx.arg .oFormatBinary )
328
329
assignFileOffsets ();
@@ -391,7 +392,7 @@ static void markUsedLocalSymbolsImpl(ObjFile<ELFT> *file,
391
392
392
393
// The function ensures that the "used" field of local symbols reflects the fact
393
394
// that the symbol is used in a relocation from a live section.
394
- template <class ELFT > static void markUsedLocalSymbols () {
395
+ template <class ELFT > static void markUsedLocalSymbols (Ctx &ctx ) {
395
396
// With --gc-sections, the field is already filled.
396
397
// See MarkLive<ELFT>::resolveReloc().
397
398
if (ctx.arg .gcSections )
@@ -419,7 +420,7 @@ template <class ELFT> static void markUsedLocalSymbols() {
419
420
}
420
421
}
421
422
422
- static bool shouldKeepInSymtab (const Defined &sym) {
423
+ static bool shouldKeepInSymtab (Ctx &ctx, const Defined &sym) {
423
424
if (sym.isSection ())
424
425
return false ;
425
426
@@ -474,7 +475,7 @@ bool lld::elf::includeInSymtab(const Symbol &b) {
474
475
// - demote symbols defined relative to /DISCARD/ discarded input sections so
475
476
// that relocations referencing them will lead to errors.
476
477
// - copy eligible symbols to .symTab
477
- static void demoteAndCopyLocalSymbols () {
478
+ static void demoteAndCopyLocalSymbols (Ctx &ctx ) {
478
479
llvm::TimeTraceScope timeScope (" Add local symbols" );
479
480
for (ELFFileBase *file : ctx.objectFiles ) {
480
481
DenseMap<SectionBase *, size_t > sectionIndexMap;
@@ -486,7 +487,8 @@ static void demoteAndCopyLocalSymbols() {
486
487
487
488
if (dr->section && !dr->section ->isLive ())
488
489
demoteDefined (*dr, sectionIndexMap);
489
- else if (ctx.in .symTab && includeInSymtab (*b) && shouldKeepInSymtab (*dr))
490
+ else if (ctx.in .symTab && includeInSymtab (*b) &&
491
+ shouldKeepInSymtab (ctx, *dr))
490
492
ctx.in .symTab ->addSymbol (b);
491
493
}
492
494
}
@@ -811,10 +813,10 @@ template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
811
813
// .rela.dyn will be present in the output.
812
814
std::string name = ctx.arg .isRela ? " __rela_iplt_start" : " __rel_iplt_start" ;
813
815
ctx.sym .relaIpltStart =
814
- addOptionalRegular (name, ctx.out .elfHeader , 0 , STV_HIDDEN);
816
+ addOptionalRegular (ctx, name, ctx.out .elfHeader , 0 , STV_HIDDEN);
815
817
name.replace (name.size () - 5 , 5 , " end" );
816
818
ctx.sym .relaIpltEnd =
817
- addOptionalRegular (name, ctx.out .elfHeader , 0 , STV_HIDDEN);
819
+ addOptionalRegular (ctx, name, ctx.out .elfHeader , 0 , STV_HIDDEN);
818
820
}
819
821
820
822
// This function generates assignments for predefined symbols (e.g. _end or
@@ -1661,7 +1663,7 @@ template <class ELFT> void Writer<ELFT>::optimizeBasicBlockJumps() {
1661
1663
// To deal with the above problem, this function is called after
1662
1664
// scanRelocations is called to remove synthetic sections that turn
1663
1665
// out to be empty.
1664
- static void removeUnusedSyntheticSections () {
1666
+ static void removeUnusedSyntheticSections (Ctx &ctx ) {
1665
1667
// All input synthetic sections that can be empty are placed after
1666
1668
// all regular ones. Reverse iterate to find the first synthetic section
1667
1669
// after a non-synthetic one which will be our starting point.
@@ -1740,8 +1742,8 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
1740
1742
if (ctx.arg .emachine == EM_RISCV) {
1741
1743
if (!ctx.arg .shared ) {
1742
1744
OutputSection *sec = findSection (" .sdata" );
1743
- addOptionalRegular (" __global_pointer$" , sec ? sec : ctx. out . elfHeader ,
1744
- 0x800 , STV_DEFAULT);
1745
+ addOptionalRegular (ctx, " __global_pointer$" ,
1746
+ sec ? sec : ctx. out . elfHeader , 0x800 , STV_DEFAULT);
1745
1747
// Set riscvGlobalPointer to be used by the optional global pointer
1746
1748
// relaxation.
1747
1749
if (ctx.arg .relaxGP ) {
@@ -1783,11 +1785,11 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
1783
1785
}
1784
1786
}
1785
1787
1786
- demoteSymbolsAndComputeIsPreemptible ();
1788
+ demoteSymbolsAndComputeIsPreemptible (ctx );
1787
1789
1788
1790
if (ctx.arg .copyRelocs && ctx.arg .discard != DiscardPolicy::None)
1789
- markUsedLocalSymbols<ELFT>();
1790
- demoteAndCopyLocalSymbols ();
1791
+ markUsedLocalSymbols<ELFT>(ctx );
1792
+ demoteAndCopyLocalSymbols (ctx );
1791
1793
1792
1794
if (ctx.arg .copyRelocs )
1793
1795
addSectionSymbols ();
@@ -1890,7 +1892,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
1890
1892
if (ctx.in .mipsGot )
1891
1893
ctx.in .mipsGot ->build ();
1892
1894
1893
- removeUnusedSyntheticSections ();
1895
+ removeUnusedSyntheticSections (ctx );
1894
1896
ctx.script ->diagnoseOrphanHandling ();
1895
1897
ctx.script ->diagnoseMissingSGSectionAddress ();
1896
1898
@@ -2111,13 +2113,13 @@ template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
2111
2113
// correct.
2112
2114
auto define = [=](StringRef start, StringRef end, OutputSection *os) {
2113
2115
if (os) {
2114
- Defined *startSym = addOptionalRegular (start, os, 0 );
2115
- Defined *stopSym = addOptionalRegular (end, os, -1 );
2116
+ Defined *startSym = addOptionalRegular (ctx, start, os, 0 );
2117
+ Defined *stopSym = addOptionalRegular (ctx, end, os, -1 );
2116
2118
if (startSym || stopSym)
2117
2119
os->usedInExpression = true ;
2118
2120
} else {
2119
- addOptionalRegular (start, ctx.out .elfHeader , 0 );
2120
- addOptionalRegular (end, ctx.out .elfHeader , 0 );
2121
+ addOptionalRegular (ctx, start, ctx.out .elfHeader , 0 );
2122
+ addOptionalRegular (ctx, end, ctx.out .elfHeader , 0 );
2121
2123
}
2122
2124
};
2123
2125
@@ -2141,10 +2143,11 @@ void Writer<ELFT>::addStartStopSymbols(OutputSection &osec) {
2141
2143
StringRef s = osec.name ;
2142
2144
if (!isValidCIdentifier (s))
2143
2145
return ;
2144
- Defined *startSym = addOptionalRegular (saver ().save (" __start_" + s), &osec, 0 ,
2145
- ctx.arg .zStartStopVisibility );
2146
- Defined *stopSym = addOptionalRegular (saver ().save (" __stop_" + s), &osec, -1 ,
2147
- ctx.arg .zStartStopVisibility );
2146
+ Defined *startSym =
2147
+ addOptionalRegular (ctx, saver ().save (" __start_" + s), &osec, 0 ,
2148
+ ctx.arg .zStartStopVisibility );
2149
+ Defined *stopSym = addOptionalRegular (ctx, saver ().save (" __stop_" + s), &osec,
2150
+ -1 , ctx.arg .zStartStopVisibility );
2148
2151
if (startSym || stopSym)
2149
2152
osec.usedInExpression = true ;
2150
2153
}
@@ -2162,7 +2165,7 @@ static bool needsPtLoad(OutputSection *sec) {
2162
2165
}
2163
2166
2164
2167
// Adjust phdr flags according to certain options.
2165
- static uint64_t computeFlags (uint64_t flags) {
2168
+ static uint64_t computeFlags (Ctx &ctx, uint64_t flags) {
2166
2169
if (ctx.arg .omagic )
2167
2170
return PF_R | PF_W | PF_X;
2168
2171
if (ctx.arg .executeOnly && (flags & PF_X))
@@ -2184,7 +2187,7 @@ SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
2184
2187
bool isMain = partNo == 1 ;
2185
2188
2186
2189
// Add the first PT_LOAD segment for regular output sections.
2187
- uint64_t flags = computeFlags (PF_R);
2190
+ uint64_t flags = computeFlags (ctx, PF_R);
2188
2191
PhdrEntry *load = nullptr ;
2189
2192
2190
2193
// nmagic or omagic output does not have PT_PHDR, PT_INTERP, or the readonly
@@ -2247,7 +2250,7 @@ SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
2247
2250
// partitions.
2248
2251
if (sec->partition != partNo) {
2249
2252
if (isMain && sec->partition == 255 )
2250
- addHdr (PT_LOAD, computeFlags (sec->getPhdrFlags ()))->add (sec);
2253
+ addHdr (PT_LOAD, computeFlags (ctx, sec->getPhdrFlags ()))->add (sec);
2251
2254
continue ;
2252
2255
}
2253
2256
@@ -2267,7 +2270,7 @@ SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
2267
2270
// supposed-to-be-NOBITS section to the output file. (However, we cannot do
2268
2271
// so when hasSectionsCommand, since we cannot introduce the extra alignment
2269
2272
// needed to create a new LOAD)
2270
- uint64_t newFlags = computeFlags (sec->getPhdrFlags ());
2273
+ uint64_t newFlags = computeFlags (ctx, sec->getPhdrFlags ());
2271
2274
// When --no-rosegment is specified, RO and RX sections are compatible.
2272
2275
uint32_t incompatible = flags ^ newFlags;
2273
2276
if (ctx.arg .singleRoRx && !(newFlags & PF_W))
0 commit comments