@@ -50,7 +50,7 @@ static bool isSectionPrefix(StringRef prefix, StringRef name) {
5050 return name.consume_front (prefix) && (name.empty () || name[0 ] == ' .' );
5151}
5252
53- static StringRef getOutputSectionName (const InputSectionBase *s) {
53+ StringRef LinkerScript:: getOutputSectionName (const InputSectionBase *s) const {
5454 // This is for --emit-relocs and -r. If .text.foo is emitted as .text.bar, we
5555 // want to emit .rela.text.foo as .rela.text.bar for consistency (this is not
5656 // technically required, but not doing it is odd). This code guarantees that.
@@ -77,7 +77,7 @@ static StringRef getOutputSectionName(const InputSectionBase *s) {
7777 if (s->name == " COMMON" )
7878 return " .bss" ;
7979
80- if (script-> hasSectionsCommand )
80+ if (hasSectionsCommand)
8181 return s->name ;
8282
8383 // When no SECTIONS is specified, emulate GNU ld's internal linker scripts
@@ -596,7 +596,7 @@ LinkerScript::computeInputSections(const InputSectionDescription *cmd,
596596 sortByPositionThenCommandLine (sizeAfterPrevSort, ret.size ());
597597 } else {
598598 SectionClassDesc *scd =
599- script-> sectionClasses .lookup (CachedHashStringRef (cmd->classRef ));
599+ sectionClasses.lookup (CachedHashStringRef (cmd->classRef ));
600600 if (!scd) {
601601 errorOrWarn (" undefined section class '" + cmd->classRef + " '" );
602602 return ret;
@@ -1160,7 +1160,7 @@ bool LinkerScript::assignOffsets(OutputSection *sec) {
11601160 }
11611161
11621162 state->outSec = sec;
1163- if (!(sec->addrExpr && script-> hasSectionsCommand )) {
1163+ if (!(sec->addrExpr && hasSectionsCommand)) {
11641164 // ALIGN is respected. sec->alignment is the max of ALIGN and the maximum of
11651165 // input section alignments.
11661166 const uint64_t pos = dot;
@@ -1419,15 +1419,6 @@ void LinkerScript::adjustSectionsAfterSorting() {
14191419 maybePropagatePhdrs (osd->osec , defPhdrs);
14201420}
14211421
1422- static uint64_t computeBase (uint64_t min, bool allocateHeaders) {
1423- // If there is no SECTIONS or if the linkerscript is explicit about program
1424- // headers, do our best to allocate them.
1425- if (!script->hasSectionsCommand || allocateHeaders)
1426- return 0 ;
1427- // Otherwise only allocate program headers if that would not add a page.
1428- return alignDown (min, config->maxPageSize );
1429- }
1430-
14311422// When the SECTIONS command is used, try to find an address for the file and
14321423// program headers output sections, which can be added to the first PT_LOAD
14331424// segment when program headers are created.
@@ -1453,8 +1444,13 @@ void LinkerScript::allocateHeaders(SmallVector<PhdrEntry *, 0> &phdrs) {
14531444 });
14541445 bool paged = !config->omagic && !config->nmagic ;
14551446 uint64_t headerSize = getHeaderSize ();
1456- if ((paged || hasExplicitHeaders) &&
1457- headerSize <= min - computeBase (min, hasExplicitHeaders)) {
1447+
1448+ uint64_t base = 0 ;
1449+ // If SECTIONS is present and the linkerscript is not explicit about program
1450+ // headers, only allocate program headers if that would not add a page.
1451+ if (hasSectionsCommand && !hasExplicitHeaders)
1452+ base = alignDown (min, config->maxPageSize );
1453+ if ((paged || hasExplicitHeaders) && headerSize <= min - base) {
14581454 min = alignDown (min - headerSize, config->maxPageSize );
14591455 ctx.out .elfHeader ->addr = min;
14601456 ctx.out .programHeaders ->addr = min + ctx.out .elfHeader ->size ;
@@ -1487,7 +1483,7 @@ LinkerScript::AddressState::AddressState() {
14871483// that has changed its section or value (or nullptr if no symbol has changed).
14881484std::pair<const OutputSection *, const Defined *>
14891485LinkerScript::assignAddresses () {
1490- if (script-> hasSectionsCommand ) {
1486+ if (hasSectionsCommand) {
14911487 // With a linker script, assignment of addresses to headers is covered by
14921488 // allocateHeaders().
14931489 dot = config->imageBase .value_or (0 );
@@ -1805,10 +1801,9 @@ void LinkerScript::addScriptReferencedSymbolsToSymTable() {
18051801 for (StringRef name : *symRefsVec.pop_back_val ()) {
18061802 reference (name);
18071803 // Prevent the symbol from being discarded by --gc-sections.
1808- script->referencedSymbols .push_back (name);
1809- auto it = script->provideMap .find (name);
1810- if (it != script->provideMap .end () &&
1811- LinkerScript::shouldAddProvideSym (name) &&
1804+ referencedSymbols.push_back (name);
1805+ auto it = provideMap.find (name);
1806+ if (it != provideMap.end () && shouldAddProvideSym (name) &&
18121807 added.insert (name).second ) {
18131808 symRefsVec.push_back (&it->second );
18141809 }
0 commit comments