flat.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * include/asm-v850/flat.h -- uClinux flat-format executables
  3. *
  4. * Copyright (C) 2002,03 NEC Electronics Corporation
  5. * Copyright (C) 2002,03 Miles Bader <miles@gnu.org>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file COPYING in the main directory of this
  9. * archive for more details.
  10. *
  11. * Written by Miles Bader <miles@gnu.org>
  12. */
  13. #ifndef __V850_FLAT_H__
  14. #define __V850_FLAT_H__
  15. /* The amount by which a relocation can exceed the program image limits
  16. without being regarded as an error. On the v850, the relocations of
  17. some base-pointers can be offset by 0x8000 (to allow better usage of the
  18. space offered by 16-bit signed offsets -- in most cases the offsets used
  19. with such a base-pointer will be negative). */
  20. #define flat_reloc_valid(reloc, size) ((reloc) <= (size + 0x8000))
  21. #define flat_stack_align(sp) /* nothing needed */
  22. #define flat_argvp_envp_on_stack() 0
  23. #define flat_old_ram_flag(flags) (flags)
  24. /* We store the type of relocation in the top 4 bits of the `relval.' */
  25. /* Convert a relocation entry into an address. */
  26. static inline unsigned long
  27. flat_get_relocate_addr (unsigned long relval)
  28. {
  29. return relval & 0x0fffffff; /* Mask out top 4-bits */
  30. }
  31. #define flat_v850_get_reloc_type(relval) ((relval) >> 28)
  32. #define FLAT_V850_R_32 0 /* Normal 32-bit reloc */
  33. #define FLAT_V850_R_HI16S_LO15 1 /* High 16-bits + signed 15-bit low field */
  34. #define FLAT_V850_R_HI16S_LO16 2 /* High 16-bits + signed 16-bit low field */
  35. /* Extract the address to be relocated from the symbol reference at RP;
  36. RELVAL is the raw relocation-table entry from which RP is derived.
  37. For the v850, RP should always be half-word aligned. */
  38. static inline unsigned long flat_get_addr_from_rp (unsigned long *rp,
  39. unsigned long relval,
  40. unsigned long flags)
  41. {
  42. short *srp = (short *)rp;
  43. switch (flat_v850_get_reloc_type (relval))
  44. {
  45. case FLAT_V850_R_32:
  46. /* Simple 32-bit address. */
  47. return srp[0] | (srp[1] << 16);
  48. case FLAT_V850_R_HI16S_LO16:
  49. /* The high and low halves of the address are in the 16
  50. bits at RP, and the 2nd word of the 32-bit instruction
  51. following that, respectively. The low half is _signed_
  52. so we have to sign-extend it and add it to the upper
  53. half instead of simply or-ing them together.
  54. Unlike most relocated address, this one is stored in
  55. native (little-endian) byte-order to avoid problems with
  56. trashing the low-order bit, so we have to convert to
  57. network-byte-order before returning, as that's what the
  58. caller expects. */
  59. return htonl ((srp[0] << 16) + srp[2]);
  60. case FLAT_V850_R_HI16S_LO15:
  61. /* The high and low halves of the address are in the 16
  62. bits at RP, and the upper 15 bits of the 2nd word of the
  63. 32-bit instruction following that, respectively. The
  64. low half is _signed_ so we have to sign-extend it and
  65. add it to the upper half instead of simply or-ing them
  66. together. The lowest bit is always zero.
  67. Unlike most relocated address, this one is stored in
  68. native (little-endian) byte-order to avoid problems with
  69. trashing the low-order bit, so we have to convert to
  70. network-byte-order before returning, as that's what the
  71. caller expects. */
  72. return htonl ((srp[0] << 16) + (srp[2] & ~0x1));
  73. default:
  74. return ~0; /* bogus value */
  75. }
  76. }
  77. /* Insert the address ADDR into the symbol reference at RP;
  78. RELVAL is the raw relocation-table entry from which RP is derived.
  79. For the v850, RP should always be half-word aligned. */
  80. static inline void flat_put_addr_at_rp (unsigned long *rp, unsigned long addr,
  81. unsigned long relval)
  82. {
  83. short *srp = (short *)rp;
  84. switch (flat_v850_get_reloc_type (relval)) {
  85. case FLAT_V850_R_32:
  86. /* Simple 32-bit address. */
  87. srp[0] = addr & 0xFFFF;
  88. srp[1] = (addr >> 16);
  89. break;
  90. case FLAT_V850_R_HI16S_LO16:
  91. /* The high and low halves of the address are in the 16
  92. bits at RP, and the 2nd word of the 32-bit instruction
  93. following that, respectively. The low half is _signed_
  94. so we must carry its sign bit to the upper half before
  95. writing the upper half. */
  96. srp[0] = (addr >> 16) + ((addr >> 15) & 0x1);
  97. srp[2] = addr & 0xFFFF;
  98. break;
  99. case FLAT_V850_R_HI16S_LO15:
  100. /* The high and low halves of the address are in the 16
  101. bits at RP, and the upper 15 bits of the 2nd word of the
  102. 32-bit instruction following that, respectively. The
  103. low half is _signed_ so we must carry its sign bit to
  104. the upper half before writing the upper half. The
  105. lowest bit we preserve from the existing instruction. */
  106. srp[0] = (addr >> 16) + ((addr >> 15) & 0x1);
  107. srp[2] = (addr & 0xFFFE) | (srp[2] & 0x1);
  108. break;
  109. }
  110. }
  111. #endif /* __V850_FLAT_H__ */