brk.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* brk system call for Linux/i386.
  2. Copyright (C) 1995, 1996, 2000 Free Software Foundation, Inc.
  3. Copyright (c) 2015 mirabilos <tg@mirbsd.org>
  4. This file is part of uclibc-ng, derived from the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include <sys/syscall.h>
  19. /* This must be initialized data because commons can't have aliases. */
  20. void *__curbrk attribute_hidden = 0;
  21. int brk(void *addr)
  22. {
  23. void *newbrk;
  24. /*
  25. * EBC is used in PIC code, we need to save/restore it manually.
  26. * Unfortunately, GCC won't do that for us even if we use con-
  27. * straints, and we cannot push it either as ESP clobbers are
  28. * silently ignored, but EDX is preserved, so it's scratch space.
  29. */
  30. __asm__("xchgl %%edx,%%ebx"
  31. "\n int $0x80"
  32. "\n xchgl %%edx,%%ebx"
  33. : "=a" (newbrk)
  34. : "0" (__NR_brk), "d" (addr)
  35. : "cc"
  36. );
  37. __curbrk = newbrk;
  38. if (newbrk < addr) {
  39. __set_errno(ENOMEM);
  40. return -1;
  41. }
  42. return 0;
  43. }
  44. libc_hidden_def(brk)