lld: Improve LMARegion handling.

This fixes the crash reported at [LLVM] PR36083.

The issue is that we were trying to put all the sections in the same
PT_LOAD and crashing trying to write past the end of the file.

This also adds accounting for used space in LMARegion, without it all
3 PT_LOADs would have the same physical address.

Obtained from:	LLVM r323449 by Rafael Espindola
This commit is contained in:
Ed Maste 2018-01-29 13:52:42 +00:00
parent 5a2ea37829
commit 2b8c81f1b9
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=328546
3 changed files with 11 additions and 2 deletions

View File

@ -589,6 +589,10 @@ void LinkerScript::output(InputSection *S) {
// If there is a memory region associated with this input section, then
// place the section in that region and update the region index.
if (Ctx->LMARegion)
Ctx->LMARegion->CurPos += Pos - Before;
// FIXME: should we also produce overflow errors for LMARegion?
if (Ctx->MemRegion) {
uint64_t &CurOffset = Ctx->MemRegion->CurPos;
CurOffset += Pos - Before;
@ -651,6 +655,7 @@ void LinkerScript::assignOffsets(OutputSection *Sec) {
setDot(Sec->AddrExpr, Sec->Location, false);
Ctx->MemRegion = Sec->MemRegion;
Ctx->LMARegion = Sec->LMARegion;
if (Ctx->MemRegion)
Dot = Ctx->MemRegion->CurPos;
@ -660,7 +665,7 @@ void LinkerScript::assignOffsets(OutputSection *Sec) {
Ctx->LMAOffset = Sec->LMAExpr().getValue() - Dot;
if (MemoryRegion *MR = Sec->LMARegion)
Ctx->LMAOffset = MR->Origin - Dot;
Ctx->LMAOffset = MR->CurPos - Dot;
// If neither AT nor AT> is specified for an allocatable section, the linker
// will set the LMA such that the difference between VMA and LMA for the
@ -690,6 +695,8 @@ void LinkerScript::assignOffsets(OutputSection *Sec) {
Dot += Cmd->Size;
if (Ctx->MemRegion)
Ctx->MemRegion->CurPos += Cmd->Size;
if (Ctx->LMARegion)
Ctx->LMARegion->CurPos += Cmd->Size;
Ctx->OutSec->Size = Dot - Ctx->OutSec->Addr;
continue;
}

View File

@ -206,6 +206,7 @@ class LinkerScript final {
uint64_t ThreadBssOffset = 0;
OutputSection *OutSec = nullptr;
MemoryRegion *MemRegion = nullptr;
MemoryRegion *LMARegion = nullptr;
uint64_t LMAOffset = 0;
};

View File

@ -1626,7 +1626,8 @@ template <class ELFT> std::vector<PhdrEntry *> Writer<ELFT>::createPhdrs() {
// different flags or is loaded at a discontiguous address using AT linker
// script command.
uint64_t NewFlags = computeFlags(Sec->getPhdrFlags());
if (Sec->LMAExpr || Flags != NewFlags) {
if (Sec->LMAExpr || Sec->MemRegion != Load->FirstSec->MemRegion ||
Flags != NewFlags) {
Load = AddHdr(PT_LOAD, NewFlags);
Flags = NewFlags;
}