dl-startup.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Any assmbly language/system dependent hacks needed to setup boot1.c so it
  2. * will work as expected and cope with whatever platform specific wierdness is
  3. * needed for this architecture.
  4. */
  5. asm("" \
  6. " .section .text..SHmedia32,\"ax\"\n" \
  7. " .globl _dl_boot\n" \
  8. " .type _dl_boot, @function\n" \
  9. " .align 5\n" \
  10. "_dl_boot:\n" \
  11. " ! Set r12 to point to GOT\n" \
  12. " movi (((datalabel _GLOBAL_OFFSET_TABLE_-(.LZZZ3-.)) >> 16) & 65535), r12\n" \
  13. " shori ((datalabel _GLOBAL_OFFSET_TABLE_-(.LZZZ3-.)) & 65535), r12\n" \
  14. ".LZZZ3:\n" \
  15. " ptrel/u r12, tr0\n" \
  16. " gettr tr0, r12 ! GOT address\n" \
  17. " add r18, r63, r11 ! save return address - needed?\n" \
  18. " add r15, r63, r2 ! arg = stack pointer\n" \
  19. " pt _dl_boot2, tr0 ! should work even if PIC\n" \
  20. " blink tr0, r18 ! call _dl_boot2 - user EP is in r2\n" \
  21. );
  22. #define DL_BOOT(X) static void __attribute_used__ _dl_boot2 (X)
  23. /*
  24. * Get a pointer to the argv array. On many platforms this can be just
  25. * the address if the first argument, on other platforms we need to
  26. * do something a little more subtle here.
  27. */
  28. #define GET_ARGV(ARGVP, ARGS) ARGVP = ((unsigned long *)ARGS)
  29. /*
  30. * Here is a macro to perform a relocation. This is only used when
  31. * bootstrapping the dynamic loader. RELP is the relocation that we
  32. * are performing, REL is the pointer to the address we are relocating.
  33. * SYMBOL is the symbol involved in the relocation, and LOAD is the
  34. * load address.
  35. */
  36. /*
  37. * We need to do this stupidity here as the preprocessor will choke when
  38. * SYMTAB is NULL if we do this in PERFORM_BOOTSTRAP_RELOC().
  39. */
  40. #include <elf.h>
  41. static inline int __extract_lsb_from_symtab(Elf32_Sym *symtab)
  42. {
  43. static int lsb = 0;
  44. /* Check for SHmedia/SHcompact */
  45. if (symtab)
  46. lsb = symtab->st_other & 4;
  47. return lsb;
  48. }
  49. /*
  50. * While on the subject of stupidity, there appear to be some conflicts with
  51. * regards to several relocation types as far as binutils is concerned
  52. * (Barcelona and Madrid both appear to use an out of date elf.h, whereas
  53. * native Catalonia has all of the necessary definitions. As a workaround,
  54. * we'll just define them here for sanity..
  55. */
  56. #ifndef R_SH_RELATIVE_LOW16
  57. # define R_SH_RELATIVE_LOW16 197
  58. # define R_SH_RELATIVE_MEDLOW16 198
  59. # define R_SH_IMM_LOW16 246
  60. # define R_SH_IMM_LOW16_PCREL 247
  61. # define R_SH_IMM_MEDLOW16 248
  62. # define R_SH_IMM_MEDLOW16_PCREL 249
  63. #endif
  64. #define PERFORM_BOOTSTRAP_RELOC(RELP,REL,SYMBOL,LOAD,SYMTAB) \
  65. const unsigned int r_type = ELF32_R_TYPE((RELP)->r_info); \
  66. int lsb = __extract_lsb_from_symtab(SYMTAB); \
  67. \
  68. switch (r_type) { \
  69. case R_SH_REL32: \
  70. *(REL) = (SYMBOL) + (RELP)->r_addend \
  71. - (unsigned long)(REL); \
  72. break; \
  73. case R_SH_DIR32: \
  74. case R_SH_GLOB_DAT: \
  75. case R_SH_JMP_SLOT: \
  76. *(REL) = ((SYMBOL) + (RELP)->r_addend) | lsb; \
  77. break; \
  78. case R_SH_RELATIVE: \
  79. *(REL) = (LOAD) + (RELP)->r_addend; \
  80. break; \
  81. case R_SH_RELATIVE_LOW16: \
  82. case R_SH_RELATIVE_MEDLOW16: \
  83. { \
  84. unsigned long word, value; \
  85. \
  86. word = (unsigned long)(REL) & ~0x3fffc00; \
  87. value = (LOAD) + (RELP)->r_addend; \
  88. \
  89. if (r_type == R_SH_RELATIVE_MEDLOW16) \
  90. value >>= 16; \
  91. \
  92. word |= (value & 0xffff) << 10; \
  93. *(REL) = word; \
  94. break; \
  95. } \
  96. case R_SH_IMM_LOW16: \
  97. case R_SH_IMM_MEDLOW16: \
  98. { \
  99. unsigned long word, value; \
  100. \
  101. word = (unsigned long)(REL) & ~0x3fffc00; \
  102. value = ((SYMBOL) + (RELP)->r_addend) | lsb; \
  103. \
  104. if (r_type == R_SH_IMM_MEDLOW16) \
  105. value >>= 16; \
  106. \
  107. word |= (value & 0xffff) << 10; \
  108. *(REL) = word; \
  109. break; \
  110. } \
  111. case R_SH_IMM_LOW16_PCREL: \
  112. case R_SH_IMM_MEDLOW16_PCREL: \
  113. { \
  114. unsigned long word, value; \
  115. \
  116. word = (unsigned long)(REL) & ~0x3fffc00; \
  117. value = (SYMBOL) + (RELP)->r_addend \
  118. - (unsigned long)(REL); \
  119. \
  120. if (r_type == R_SH_IMM_MEDLOW16_PCREL) \
  121. value >>= 16; \
  122. \
  123. word |= (value & 0xffff) << 10; \
  124. *(REL) = word; \
  125. break; \
  126. } \
  127. case R_SH_NONE: \
  128. break; \
  129. default: \
  130. SEND_STDERR("BOOTSTRAP_RELOC: unhandled reloc type "); \
  131. SEND_NUMBER_STDERR(ELF32_R_TYPE((RELP)->r_info), 1); \
  132. SEND_STDERR("REL, SYMBOL, LOAD: "); \
  133. SEND_ADDRESS_STDERR(REL, 0); \
  134. SEND_STDERR(", "); \
  135. SEND_ADDRESS_STDERR(SYMBOL, 0); \
  136. SEND_STDERR(", "); \
  137. SEND_ADDRESS_STDERR(LOAD, 1); \
  138. _dl_exit(1); \
  139. }
  140. /*
  141. * Transfer control to the user's application, once the dynamic loader
  142. * is done. This routine has to exit the current function, then
  143. * call the _dl_elf_main function.
  144. */
  145. #define START() return _dl_elf_main;