Skip to content

Commit 0068461

Browse files
arsvaswaterman
authored andcommitted
newlib: use brk(0) to get initial program break (riscv-collab#172)
There are no guarantees that the break going to be located exactly at the _end, but brk(0) is guaranteed to return its current value. QEMU ELF loader moves the initial break up to the next page boundary, disabling sbrk-based malloc for any allocations smaller than the adjustment made. And ASLR may do even worse.
1 parent bfbfdec commit 0068461

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

newlib/libgloss/riscv/syscalls.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,15 @@ long sysconf(int name)
436436

437437
void* sbrk(ptrdiff_t incr)
438438
{
439-
extern unsigned char _end[]; // Defined by linker
440439
static unsigned long heap_end;
441440

442-
if (heap_end == 0)
443-
heap_end = (long)_end;
441+
if (heap_end == 0) {
442+
long brk = syscall_errno(SYS_brk, 0, 0, 0, 0);
443+
if(brk == -1)
444+
return (void*)-1;
445+
heap_end = brk;
446+
}
447+
444448
if (syscall_errno(SYS_brk, heap_end + incr, 0, 0, 0) != heap_end + incr)
445449
return (void*)-1;
446450

0 commit comments

Comments
 (0)