rdfutils.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-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. * rdfutils.h
  35. *
  36. * Internal header file for RDOFF utilities
  37. */
  38. #ifndef RDOFF_RDFUTILS_H
  39. #define RDOFF_RDFUTILS_H 1
  40. #include "compiler.h"
  41. #include "nasmlib.h"
  42. #include "error.h"
  43. #include "rdoff.h"
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. typedef union RDFHeaderRec {
  47. char type; /* invariant throughout all below */
  48. struct GenericRec g; /* type 0 */
  49. struct RelocRec r; /* type == 1 / 6 */
  50. struct ImportRec i; /* type == 2 / 7 */
  51. struct ExportRec e; /* type == 3 */
  52. struct DLLRec d; /* type == 4 */
  53. struct BSSRec b; /* type == 5 */
  54. struct ModRec m; /* type == 8 */
  55. struct CommonRec c; /* type == 10 */
  56. } rdfheaderrec;
  57. struct SegmentHeaderRec {
  58. /* information from file */
  59. uint16_t type;
  60. uint16_t number;
  61. uint16_t reserved;
  62. int32_t length;
  63. /* information built up here */
  64. int32_t offset;
  65. uint8_t *data; /* pointer to segment data if it exists in memory */
  66. };
  67. typedef struct RDFFileInfo {
  68. FILE *fp; /* file descriptor; must be open to use this struct */
  69. int rdoff_ver; /* should be 1; any higher => not guaranteed to work */
  70. int32_t header_len;
  71. int32_t header_ofs;
  72. uint8_t *header_loc; /* keep location of header */
  73. int32_t header_fp; /* current location within header for reading */
  74. struct SegmentHeaderRec seg[RDF_MAXSEGS];
  75. int nsegs;
  76. int32_t eof_offset; /* offset of the first uint8_t beyond the end of this
  77. module */
  78. char *name; /* name of module in libraries */
  79. int *refcount; /* pointer to reference count on file, or NULL */
  80. } rdffile;
  81. #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
  82. * on 80x86 machines for efficiency */
  83. typedef struct memorybuffer {
  84. int length;
  85. uint8_t buffer[BUF_BLOCK_LEN];
  86. struct memorybuffer *next;
  87. } memorybuffer;
  88. typedef struct {
  89. memorybuffer *buf; /* buffer containing header records */
  90. int nsegments; /* number of segments to be written */
  91. int32_t seglength; /* total length of all the segments */
  92. } rdf_headerbuf;
  93. /* segments used by RDOFF, understood by rdoffloadseg */
  94. #define RDOFF_CODE 0
  95. #define RDOFF_DATA 1
  96. #define RDOFF_HEADER -1
  97. /* mask for 'segment' in relocation records to find if relative relocation */
  98. #define RDOFF_RELATIVEMASK 64
  99. /* mask to find actual segment value in relocation records */
  100. #define RDOFF_SEGMENTMASK 63
  101. extern int rdf_errno;
  102. /* rdf_errno can hold these error codes */
  103. enum {
  104. /* 0 */ RDF_OK,
  105. /* 1 */ RDF_ERR_OPEN,
  106. /* 2 */ RDF_ERR_FORMAT,
  107. /* 3 */ RDF_ERR_READ,
  108. /* 4 */ RDF_ERR_UNKNOWN,
  109. /* 5 */ RDF_ERR_HEADER,
  110. /* 6 */ RDF_ERR_NOMEM,
  111. /* 7 */ RDF_ERR_VER,
  112. /* 8 */ RDF_ERR_RECTYPE,
  113. /* 9 */ RDF_ERR_RECLEN,
  114. /* 10 */ RDF_ERR_SEGMENT
  115. };
  116. /* library init */
  117. void rdoff_init(void);
  118. /* utility functions */
  119. int32_t translateint32_t(int32_t in);
  120. uint16_t translateint16_t(uint16_t in);
  121. char *translatesegmenttype(uint16_t type);
  122. /* RDOFF file manipulation functions */
  123. int rdfopen(rdffile * f, const char *name);
  124. int rdfopenhere(rdffile * f, FILE * fp, int *refcount, const char *name);
  125. int rdfclose(rdffile * f);
  126. int rdffindsegment(rdffile * f, int segno);
  127. int rdfloadseg(rdffile * f, int segment, void *buffer);
  128. rdfheaderrec *rdfgetheaderrec(rdffile * f); /* returns static storage */
  129. void rdfheaderrewind(rdffile * f); /* back to start of header */
  130. void rdfperror(const char *app, const char *name);
  131. /* functions to write a new RDOFF header to a file -
  132. use rdfnewheader to allocate a header, rdfaddheader to add records to it,
  133. rdfaddsegment to notify the header routines that a segment exists, and
  134. to tell it how int32_t the segment will be.
  135. rdfwriteheader to write the file id, object length, and header
  136. to a file, and then rdfdoneheader to dispose of the header */
  137. rdf_headerbuf *rdfnewheader(void);
  138. int rdfaddheader(rdf_headerbuf * h, rdfheaderrec * r);
  139. int rdfaddsegment(rdf_headerbuf * h, int32_t seglength);
  140. int rdfwriteheader(FILE * fp, rdf_headerbuf * h);
  141. void rdfdoneheader(rdf_headerbuf * h);
  142. #endif /* RDOFF_RDFUTILS_H */