@@ -354,6 +354,7 @@ RewriteInstance::RewriteInstance(ELFObjectFileBase *File, const int Argc,
354354 }
355355 }
356356
357+ Relocation::Arch = TheTriple.getArch ();
357358 auto BCOrErr = BinaryContext::createBinaryContext (
358359 TheTriple, File->getFileName (), Features.get (), IsPIC,
359360 DWARFContext::create (*File, DWARFContext::ProcessDebugRelocations::Ignore,
@@ -955,13 +956,13 @@ void RewriteInstance::discoverFileObjects() {
955956 uint64_t SymbolSize = ELFSymbolRef (Symbol).getSize ();
956957 uint64_t SymbolAlignment = Symbol.getAlignment ();
957958
958- auto registerName = [&](uint64_t FinalSize) {
959+ auto registerName = [&](uint64_t FinalSize, BinarySection *Section = NULL ) {
959960 // Register names even if it's not a function, e.g. for an entry point.
960961 BC->registerNameAtAddress (UniqueName, SymbolAddress, FinalSize,
961- SymbolAlignment, SymbolFlags);
962+ SymbolAlignment, SymbolFlags, Section );
962963 if (!AlternativeName.empty ())
963964 BC->registerNameAtAddress (AlternativeName, SymbolAddress, FinalSize,
964- SymbolAlignment, SymbolFlags);
965+ SymbolAlignment, SymbolFlags, Section );
965966 };
966967
967968 section_iterator Section =
@@ -986,12 +987,25 @@ void RewriteInstance::discoverFileObjects() {
986987 << " for function\n " );
987988
988989 if (SymbolAddress == Section->getAddress () + Section->getSize ()) {
990+ ErrorOr<BinarySection &> SectionOrError =
991+ BC->getSectionForAddress (Section->getAddress ());
992+
993+ // Skip symbols from invalid sections
994+ if (!SectionOrError) {
995+ BC->errs () << " BOLT-WARNING: " << UniqueName << " (0x"
996+ << Twine::utohexstr (SymbolAddress)
997+ << " ) does not have any section\n " ;
998+ continue ;
999+ }
1000+
9891001 assert (SymbolSize == 0 &&
9901002 " unexpect non-zero sized symbol at end of section" );
991- LLVM_DEBUG (
992- dbgs ()
993- << " BOLT-DEBUG: rejecting as symbol points to end of its section\n " );
994- registerName (SymbolSize);
1003+ LLVM_DEBUG ({
1004+ dbgs () << " BOLT-DEBUG: rejecting as symbol " << UniqueName
1005+ << " points to end of " << SectionOrError->getName ()
1006+ << " section\n " ;
1007+ });
1008+ registerName (SymbolSize, &SectionOrError.get ());
9951009 continue ;
9961010 }
9971011
@@ -2143,6 +2157,14 @@ bool RewriteInstance::analyzeRelocation(
21432157 if (!Relocation::isSupported (RType))
21442158 return false ;
21452159
2160+ auto IsWeakReference = [](const SymbolRef &Symbol) {
2161+ Expected<uint32_t > SymFlagsOrErr = Symbol.getFlags ();
2162+ if (!SymFlagsOrErr)
2163+ return false ;
2164+ return (*SymFlagsOrErr & SymbolRef::SF_Undefined) &&
2165+ (*SymFlagsOrErr & SymbolRef::SF_Weak);
2166+ };
2167+
21462168 const bool IsAArch64 = BC->isAArch64 ();
21472169
21482170 const size_t RelSize = Relocation::getSizeForType (RType);
@@ -2174,7 +2196,8 @@ bool RewriteInstance::analyzeRelocation(
21742196 // Section symbols are marked as ST_Debug.
21752197 IsSectionRelocation = (cantFail (Symbol.getType ()) == SymbolRef::ST_Debug);
21762198 // Check for PLT entry registered with symbol name
2177- if (!SymbolAddress && (IsAArch64 || BC->isRISCV ())) {
2199+ if (!SymbolAddress && !IsWeakReference (Symbol) &&
2200+ (IsAArch64 || BC->isRISCV ())) {
21782201 const BinaryData *BD = BC->getPLTBinaryDataByName (SymbolName);
21792202 SymbolAddress = BD ? BD->getAddress () : 0 ;
21802203 }
@@ -2603,7 +2626,7 @@ void RewriteInstance::handleRelocation(const SectionRef &RelocatedSection,
26032626 Expected<StringRef> SectionName = Section->getName ();
26042627 if (SectionName && !SectionName->empty ())
26052628 ReferencedSection = BC->getUniqueSectionByName (*SectionName);
2606- } else if (ReferencedSymbol && ContainingBF &&
2629+ } else if (BC-> isRISCV () && ReferencedSymbol && ContainingBF &&
26072630 (cantFail (Symbol.getFlags ()) & SymbolRef::SF_Absolute)) {
26082631 // This might be a relocation for an ABS symbols like __global_pointer$ on
26092632 // RISC-V
@@ -2614,6 +2637,30 @@ void RewriteInstance::handleRelocation(const SectionRef &RelocatedSection,
26142637 }
26152638 }
26162639
2640+ if (Relocation::isGOT (RType) && !Relocation::isTLS (RType)) {
2641+ auto exitOnGotEndSymol = [&](StringRef Name) {
2642+ BC->errs () << " BOLT-ERROR: GOT table contains currently unsupported "
2643+ " section end symbol "
2644+ << Name << " \n " ;
2645+ exit (1 );
2646+ };
2647+
2648+ if (SymbolIter != InputFile->symbol_end () && ReferencedSection) {
2649+ if (cantFail (SymbolIter->getAddress ()) ==
2650+ ReferencedSection->getEndAddress ())
2651+ exitOnGotEndSymol (cantFail (SymbolIter->getName ()));
2652+ } else {
2653+ // If no section and symbol are provided by relocation, try to find the
2654+ // symbol by its name, including the possibility that the symbol is local.
2655+ BinaryData *BD = BC->getBinaryDataByName (SymbolName);
2656+ if (!BD && NR.getUniquifiedNameCount (SymbolName) == 1 )
2657+ BD = BC->getBinaryDataByName (NR.getUniqueName (SymbolName, 1 ));
2658+
2659+ if ((BD && BD->getAddress () == BD->getSection ().getEndAddress ()))
2660+ exitOnGotEndSymol (BD->getName ());
2661+ }
2662+ }
2663+
26172664 if (!ReferencedSection)
26182665 ReferencedSection = BC->getSectionForAddress (SymbolAddress);
26192666
@@ -5509,6 +5556,14 @@ uint64_t RewriteInstance::getNewFunctionOrDataAddress(uint64_t OldAddress) {
55095556 if (const BinaryFunction *BF =
55105557 BC->getBinaryFunctionContainingAddress (OldAddress)) {
55115558 if (BF->isEmitted ()) {
5559+ // If OldAddress is the another entry point of
5560+ // the function, then BOLT could get the new address.
5561+ if (BF->isMultiEntry ()) {
5562+ for (const BinaryBasicBlock &BB : *BF)
5563+ if (BB.isEntryPoint () &&
5564+ (BF->getAddress () + BB.getOffset ()) == OldAddress)
5565+ return BF->getOutputAddress () + BB.getOffset ();
5566+ }
55125567 BC->errs () << " BOLT-ERROR: unable to get new address corresponding to "
55135568 " input address 0x"
55145569 << Twine::utohexstr (OldAddress) << " in function " << *BF
0 commit comments