target_os_elf.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * netbsd ELF definitions
  3. *
  4. * Copyright (c) 2013 Stacey D. Son
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef TARGET_OS_ELF_H
  20. #define TARGET_OS_ELF_H
  21. #include "target_arch_elf.h"
  22. #include "elf.h"
  23. /* this flag is uneffective under linux too, should be deleted */
  24. #ifndef MAP_DENYWRITE
  25. #define MAP_DENYWRITE 0
  26. #endif
  27. /* should probably go in elf.h */
  28. #ifndef ELIBBAD
  29. #define ELIBBAD 80
  30. #endif
  31. #ifndef ELF_PLATFORM
  32. #define ELF_PLATFORM (NULL)
  33. #endif
  34. #ifndef ELF_HWCAP
  35. #define ELF_HWCAP 0
  36. #endif
  37. #ifdef TARGET_ABI32
  38. #undef ELF_CLASS
  39. #define ELF_CLASS ELFCLASS32
  40. #undef bswaptls
  41. #define bswaptls(ptr) bswap32s(ptr)
  42. #endif
  43. /* max code+data+bss space allocated to elf interpreter */
  44. #define INTERP_MAP_SIZE (32 * 1024 * 1024)
  45. /* max code+data+bss+brk space allocated to ET_DYN executables */
  46. #define ET_DYN_MAP_SIZE (128 * 1024 * 1024)
  47. /* Necessary parameters */
  48. #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
  49. #define TARGET_ELF_PAGESTART(_v) ((_v) & \
  50. ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE - 1))
  51. #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE - 1))
  52. #define DLINFO_ITEMS 12
  53. static abi_ulong target_create_elf_tables(abi_ulong p, int argc, int envc,
  54. abi_ulong stringp,
  55. struct elfhdr *exec,
  56. abi_ulong load_addr,
  57. abi_ulong load_bias,
  58. abi_ulong interp_load_addr,
  59. struct image_info *info)
  60. {
  61. abi_ulong sp;
  62. int size;
  63. abi_ulong u_platform;
  64. const char *k_platform;
  65. const int n = sizeof(elf_addr_t);
  66. sp = p;
  67. u_platform = 0;
  68. k_platform = ELF_PLATFORM;
  69. if (k_platform) {
  70. size_t len = strlen(k_platform) + 1;
  71. sp -= (len + n - 1) & ~(n - 1);
  72. u_platform = sp;
  73. /* FIXME - check return value of memcpy_to_target() for failure */
  74. memcpy_to_target(sp, k_platform, len);
  75. }
  76. /*
  77. * Force 16 byte _final_ alignment here for generality.
  78. */
  79. sp = sp & ~(abi_ulong)15;
  80. size = (DLINFO_ITEMS + 1) * 2;
  81. if (k_platform) {
  82. size += 2;
  83. }
  84. #ifdef DLINFO_ARCH_ITEMS
  85. size += DLINFO_ARCH_ITEMS * 2;
  86. #endif
  87. size += envc + argc + 2;
  88. size += 1; /* argc itself */
  89. size *= n;
  90. if (size & 15) {
  91. sp -= 16 - (size & 15);
  92. }
  93. /*
  94. * NetBSD defines elf_addr_t as Elf32_Off / Elf64_Off
  95. */
  96. #define NEW_AUX_ENT(id, val) do { \
  97. sp -= n; put_user_ual(val, sp); \
  98. sp -= n; put_user_ual(id, sp); \
  99. } while (0)
  100. NEW_AUX_ENT(AT_NULL, 0);
  101. /* There must be exactly DLINFO_ITEMS entries here. */
  102. NEW_AUX_ENT(AT_PHDR, (abi_ulong)(load_addr + exec->e_phoff));
  103. NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof(struct elf_phdr)));
  104. NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
  105. NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
  106. NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_load_addr));
  107. NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
  108. NEW_AUX_ENT(AT_ENTRY, load_bias + exec->e_entry);
  109. NEW_AUX_ENT(AT_UID, (abi_ulong)getuid());
  110. NEW_AUX_ENT(AT_EUID, (abi_ulong)geteuid());
  111. NEW_AUX_ENT(AT_GID, (abi_ulong)getgid());
  112. NEW_AUX_ENT(AT_EGID, (abi_ulong)getegid());
  113. NEW_AUX_ENT(AT_HWCAP, (abi_ulong)ELF_HWCAP);
  114. NEW_AUX_ENT(AT_CLKTCK, (abi_ulong)sysconf(_SC_CLK_TCK));
  115. if (k_platform) {
  116. NEW_AUX_ENT(AT_PLATFORM, u_platform);
  117. }
  118. #ifdef ARCH_DLINFO
  119. /*
  120. * ARCH_DLINFO must come last so platform specific code can enforce
  121. * special alignment requirements on the AUXV if necessary (eg. PPC).
  122. */
  123. ARCH_DLINFO;
  124. #endif
  125. #undef NEW_AUX_ENT
  126. sp = loader_build_argptr(envc, argc, sp, stringp);
  127. return sp;
  128. }
  129. #endif /* TARGET_OS_ELF_H */