@@ -191,7 +191,7 @@ uint64_t TextOutputSection::estimateStubsInRangeVA(size_t callIdx) const {
191191 auto itPostcallIdxThunks = std::partition_point (
192192 thunks.begin (), thunks.end (),
193193 [isecVA](const ConcatInputSection *t) { return t->getVA () <= isecVA; });
194- uint32_t existingForwardThunks = thunks.end () - itPostcallIdxThunks;
194+ uint64_t existingForwardThunks = thunks.end () - itPostcallIdxThunks;
195195
196196 // Estimate the address after which call sites can safely call stubs
197197 // directly rather than through intermediary thunks.
@@ -201,26 +201,30 @@ uint64_t TextOutputSection::estimateStubsInRangeVA(size_t callIdx) const {
201201 assert (isecEnd - isecVA <= forwardBranchRange &&
202202 " should only finalize sections in jump range" );
203203
204- // Estimate the maximum size of the code, right before the stubs section
205- uint32_t maxTextSize = 0 ;
204+ // Estimate the maximum size of the code, right before the stubs section.
205+ uint64_t maxTextSize = 0 ;
206206 // Add the size of all the inputs, including the unprocessed ones.
207207 maxTextSize += isecEnd;
208208
209- // Add the size of the thunks that may be created in the future
209+ // Add the size of the thunks that may be created in the future. Since
210+ // 'maxPotentialThunks' overcounts, this is an estimate of the upper limit.
210211 maxTextSize += maxPotentialThunks * target->thunkSize ;
211212
212213 // Add the size of the thunks that have already been created that are ahead
213214 maxTextSize += existingForwardThunks * target->thunkSize ;
214215
215- uint64_t stubsInRangeVA =
216- maxTextSize + in.stubs ->getSize () - forwardBranchRange;
216+ // Estimated maximum VA of last stub.
217+ uint64_t maxVAOfLastStub = maxTextSize + in.stubs ->getSize ();
218+
219+ // Calculaate the first address that is gueranteed to not need a thunk to
220+ // reach any stub.git
221+ uint64_t stubsInRangeVA = maxVAOfLastStub - forwardBranchRange;
217222
218223 log (" thunks = " + std::to_string (thunkMap.size ()) +
219- " , potential = " + std::to_string (maxPotentialThunks) +
220- " , stubs = " + std::to_string (in.stubs ->getSize ()) + " , isecVA = " +
221- utohexstr (isecVA) + " , threshold = " + utohexstr (stubsInRangeVA) +
222- " , isecEnd = " + utohexstr (isecEnd) +
223- " , tail = " + utohexstr (isecEnd - isecVA) +
224+ " , potential = " + std::to_string (maxPotentialThunks) + " , stubs = " +
225+ std::to_string (in.stubs ->getSize ()) + " , isecVA = " + utohexstr (isecVA) +
226+ " , threshold = " + utohexstr (stubsInRangeVA) + " , isecEnd = " +
227+ utohexstr (isecEnd) + " , tail = " + utohexstr (isecEnd - isecVA) +
224228 " , slop = " + utohexstr (forwardBranchRange - (isecEnd - isecVA)));
225229 return stubsInRangeVA;
226230}
0 commit comments