legacy.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 2016-2017 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. /*
  34. * output/legacy.c
  35. *
  36. * Mangle a struct out_data to match the rather bizarre legacy
  37. * backend interface.
  38. *
  39. * The "data" parameter for the output function points to a "int64_t",
  40. * containing the address of the target in question, unless the type is
  41. * OUT_RAWDATA, in which case it points to an "uint8_t"
  42. * array.
  43. *
  44. * Exceptions are OUT_RELxADR, which denote an x-byte relocation
  45. * which will be a relative jump. For this we need to know the
  46. * distance in bytes from the start of the relocated record until
  47. * the end of the containing instruction. _This_ is what is stored
  48. * in the size part of the parameter, in this case.
  49. *
  50. * Also OUT_RESERVE denotes reservation of N bytes of BSS space,
  51. * and the contents of the "data" parameter is irrelevant.
  52. */
  53. #include "nasm.h"
  54. #include "outlib.h"
  55. void nasm_do_legacy_output(const struct out_data *data)
  56. {
  57. const void *dptr = data->data;
  58. enum out_type type = data->type;
  59. int32_t tsegment = data->tsegment;
  60. int32_t twrt = data->twrt;
  61. uint64_t size = data->size;
  62. switch (data->type) {
  63. case OUT_RELADDR:
  64. switch (data->size) {
  65. case 1:
  66. type = OUT_REL1ADR;
  67. break;
  68. case 2:
  69. type = OUT_REL2ADR;
  70. break;
  71. case 4:
  72. type = OUT_REL4ADR;
  73. break;
  74. case 8:
  75. type = OUT_REL8ADR;
  76. break;
  77. default:
  78. panic();
  79. break;
  80. }
  81. dptr = &data->toffset;
  82. size = data->relbase - data->offset;
  83. break;
  84. case OUT_SEGMENT:
  85. type = OUT_ADDRESS;
  86. dptr = zero_buffer;
  87. size = (data->sign == OUT_SIGNED) ? -data->size : data->size;
  88. tsegment |= 1;
  89. break;
  90. case OUT_ADDRESS:
  91. dptr = &data->toffset;
  92. size = (data->sign == OUT_SIGNED) ? -data->size : data->size;
  93. break;
  94. case OUT_RAWDATA:
  95. case OUT_RESERVE:
  96. tsegment = twrt = NO_SEG;
  97. break;
  98. case OUT_ZERODATA:
  99. tsegment = twrt = NO_SEG;
  100. type = OUT_RAWDATA;
  101. dptr = zero_buffer;
  102. while (size > ZERO_BUF_SIZE) {
  103. ofmt->legacy_output(data->segment, dptr, type,
  104. ZERO_BUF_SIZE, tsegment, twrt);
  105. size -= ZERO_BUF_SIZE;
  106. }
  107. break;
  108. default:
  109. panic();
  110. break;
  111. }
  112. ofmt->legacy_output(data->segment, dptr, type, size, tsegment, twrt);
  113. }