@@ -73,6 +73,8 @@ class Writer {
7373 void addSection (OutputSection *Sec);
7474
7575 void addSections ();
76+ void addStartStopSymbols (const InputSegment *Seg);
77+
7678 void createCustomSections ();
7779 void createSyntheticSections ();
7880 void finalizeSections ();
@@ -293,6 +295,22 @@ void Writer::addSection(OutputSection *Sec) {
293295 OutputSections.push_back (Sec);
294296}
295297
298+ // If a section name is valid as a C identifier (which is rare because of
299+ // the leading '.'), linkers are expected to define __start_<secname> and
300+ // __stop_<secname> symbols. They are at beginning and end of the section,
301+ // respectively. This is not requested by the ELF standard, but GNU ld and
302+ // gold provide the feature, and used by many programs.
303+ void Writer::addStartStopSymbols (const InputSegment *Seg) {
304+ StringRef S = Seg->getName ();
305+ LLVM_DEBUG (dbgs () << " addStartStopSymbols: " << S << " \n " );
306+ if (!isValidCIdentifier (S))
307+ return ;
308+ uint32_t Start = Seg->OutputSeg ->StartVA + Seg->OutputSegmentOffset ;
309+ uint32_t Stop = Start + Seg->getSize ();
310+ Symtab->addOptionalDataSymbol (Saver.save (" __start_" + S), Start, 0 );
311+ Symtab->addOptionalDataSymbol (Saver.save (" __stop_" + S), Stop, 0 );
312+ }
313+
296314void Writer::addSections () {
297315 addSection (Out.DylinkSec );
298316 addSection (Out.TypeSec );
@@ -724,21 +742,40 @@ void Writer::run() {
724742 populateTargetFeatures ();
725743 log (" -- calculateImports" );
726744 calculateImports ();
745+ log (" -- layoutMemory" );
746+ layoutMemory ();
747+
748+ if (!Config->Relocatable ) {
749+ // Create linker synthesized __start_SECNAME/__stop_SECNAME symbols
750+ // This has to be done after memory layout is performed.
751+ for (const OutputSegment *Seg : Segments)
752+ for (const InputSegment *S : Seg->InputSegments )
753+ addStartStopSymbols (S);
754+ }
755+
727756 log (" -- scanRelocations" );
728757 scanRelocations ();
729758 log (" -- assignIndexes" );
730759 assignIndexes ();
731760 log (" -- calculateInitFunctions" );
732761 calculateInitFunctions ();
733- log (" -- calculateTypes" );
734- calculateTypes ();
735- log (" -- layoutMemory" );
736- layoutMemory ();
762+
737763 if (!Config->Relocatable ) {
764+ // Create linker synthesized functions
738765 if (Config->Pic )
739766 createApplyRelocationsFunction ();
740767 createCallCtorsFunction ();
768+
769+ // Make sure we have resolved all symbols.
770+ if (!Config->AllowUndefined )
771+ Symtab->reportRemainingUndefines ();
772+
773+ if (errorCount ())
774+ return ;
741775 }
776+
777+ log (" -- calculateTypes" );
778+ calculateTypes ();
742779 log (" -- calculateExports" );
743780 calculateExports ();
744781 log (" -- calculateCustomSections" );
0 commit comments