Skip to content

Commit

Permalink
[ELF] - Fix calculation of memory region offset.
Browse files Browse the repository at this point in the history
This is PR33714.

Previously for each input section offset of memory region
was incremented on a size of output section.

That resulted in a wrong error message saying about
overflow. Patch fixes that.

Differential revision: https://reviews.llvm.org/D35803

llvm-svn: 308955
  • Loading branch information
George Rimar committed Jul 25, 2017
1 parent 97c8e09 commit f694d33
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lld/ELF/LinkerScript.cpp
Expand Up @@ -550,6 +550,7 @@ uint64_t LinkerScript::advance(uint64_t Size, unsigned Align) {
}

void LinkerScript::output(InputSection *S) {
uint64_t Before = advance(0, 1);
uint64_t Pos = advance(S->getSize(), S->Alignment);
S->OutSecOff = Pos - S->getSize() - CurAddressState->OutSec->Addr;

Expand All @@ -563,7 +564,7 @@ void LinkerScript::output(InputSection *S) {
if (CurAddressState->MemRegion) {
uint64_t &CurOffset =
CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
CurOffset += CurAddressState->OutSec->Size;
CurOffset += Pos - Before;
uint64_t CurSize = CurOffset - CurAddressState->MemRegion->Origin;
if (CurSize > CurAddressState->MemRegion->Length) {
uint64_t OverflowAmt = CurSize - CurAddressState->MemRegion->Length;
Expand Down
14 changes: 14 additions & 0 deletions lld/test/ELF/linkerscript/memory2.s
@@ -0,0 +1,14 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
# RUN: echo "MEMORY { ram (rwx) : ORIGIN = 0, LENGTH = 2K } \
# RUN: SECTIONS { .text : { *(.text*) } > ram }" > %t.script
# RUN: ld.lld -o %t2 --script %t.script %t

.text
.global _start
_start:
.zero 1024

.section .text.foo,"ax",%progbits
foo:
nop

0 comments on commit f694d33

Please sign in to comment.