section.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**
  2. * \file libyasm/section.h
  3. * \brief YASM section interface.
  4. *
  5. * \license
  6. * Copyright (C) 2001-2007 Peter Johnson
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  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 copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. * \endlicense
  29. */
  30. #ifndef YASM_SECTION_H
  31. #define YASM_SECTION_H
  32. #ifndef YASM_LIB_DECL
  33. #define YASM_LIB_DECL
  34. #endif
  35. /** Basic YASM relocation. Object formats will need to extend this
  36. * structure with additional fields for relocation type, etc.
  37. */
  38. typedef struct yasm_reloc yasm_reloc;
  39. struct yasm_reloc {
  40. /*@reldef@*/ STAILQ_ENTRY(yasm_reloc) link; /**< Link to next reloc */
  41. yasm_intnum *addr; /**< Offset (address) within section */
  42. /*@dependent@*/ yasm_symrec *sym; /**< Relocated symbol */
  43. };
  44. /** An object. This is the internal representation of an object file. */
  45. struct yasm_object {
  46. /*@owned@*/ char *src_filename; /**< Source filename */
  47. /*@owned@*/ char *deb_filename; /**< Debug filename */
  48. /*@owned@*/ char *obj_filename; /**< Object filename */
  49. /*@owned@*/ yasm_symtab *symtab; /**< Symbol table */
  50. /*@owned@*/ yasm_arch *arch; /**< Target architecture */
  51. /*@owned@*/ yasm_objfmt *objfmt; /**< Object format */
  52. /*@owned@*/ yasm_dbgfmt *dbgfmt; /**< Debug format */
  53. /** Currently active section. Used by some directives. NULL if no
  54. * section active.
  55. */
  56. /*@dependent@*/ /*@null@*/ yasm_section *cur_section;
  57. /** Linked list of sections. */
  58. /*@reldef@*/ STAILQ_HEAD(yasm_sectionhead, yasm_section) sections;
  59. /** Directives, organized as two level HAMT; first level is parser,
  60. * second level is directive name.
  61. */
  62. /*@owned@*/ struct HAMT *directives;
  63. /** Prefix prepended to externally-visible symbols (empty string if none) */
  64. /*@owned@*/ char *global_prefix;
  65. /** Suffix appended to externally-visible symbols (empty string if none) */
  66. /*@owned@*/ char *global_suffix;
  67. };
  68. /** Create a new object. A default section is created as the first section.
  69. * An empty symbol table (yasm_symtab) and line mapping (yasm_linemap) are
  70. * automatically created.
  71. * \param src_filename source filename (e.g. "file.asm")
  72. * \param obj_filename object filename (e.g. "file.o")
  73. * \param arch architecture
  74. * \param objfmt_module object format module
  75. * \param dbgfmt_module debug format module
  76. * \return Newly allocated object, or NULL on error.
  77. */
  78. YASM_LIB_DECL
  79. /*@null@*/ /*@only@*/ yasm_object *yasm_object_create
  80. (const char *src_filename, const char *obj_filename,
  81. /*@kept@*/ yasm_arch *arch,
  82. const yasm_objfmt_module *objfmt_module,
  83. const yasm_dbgfmt_module *dbgfmt_module);
  84. /** Create a new, or continue an existing, general section. The section is
  85. * added to the object if there's not already a section by that name.
  86. * \param object object
  87. * \param name section name
  88. * \param align alignment in bytes (0 if none)
  89. * \param code if nonzero, section is intended to contain code
  90. * (e.g. alignment should be made with NOP instructions, not 0)
  91. * \param res_only if nonzero, only space-reserving bytecodes are allowed in
  92. * the section (ignored if section already exists)
  93. * \param isnew output; set to nonzero if section did not already exist
  94. * \param line virtual line of section declaration (ignored if section
  95. * already exists)
  96. * \return New section.
  97. */
  98. YASM_LIB_DECL
  99. /*@dependent@*/ yasm_section *yasm_object_get_general
  100. (yasm_object *object, const char *name, unsigned long align, int code,
  101. int res_only, /*@out@*/ int *isnew, unsigned long line);
  102. /** Handle a directive. Passed down to object format, debug format, or
  103. * architecture as appropriate.
  104. * \param object object
  105. * \param name directive name
  106. * \param parser parser keyword
  107. * \param valparams value/parameters
  108. * \param objext_valparams "object format-specific" value/parameters
  109. * \param line virtual line (from yasm_linemap)
  110. * \return 0 if directive recognized, nonzero if unrecognized.
  111. */
  112. YASM_LIB_DECL
  113. int yasm_object_directive(yasm_object *object, const char *name,
  114. const char *parser, yasm_valparamhead *valparams,
  115. yasm_valparamhead *objext_valparams,
  116. unsigned long line);
  117. /** Delete (free allocated memory for) an object. All sections in the
  118. * object and all bytecodes within those sections are also deleted.
  119. * \param object object
  120. */
  121. YASM_LIB_DECL
  122. void yasm_object_destroy(/*@only@*/ yasm_object *object);
  123. /** Print an object. For debugging purposes.
  124. * \param object object
  125. * \param f file
  126. * \param indent_level indentation level
  127. */
  128. YASM_LIB_DECL
  129. void yasm_object_print(const yasm_object *object, FILE *f, int indent_level);
  130. /** Finalize an object after parsing.
  131. * \param object object
  132. * \param errwarns error/warning set
  133. * \note Errors/warnings are stored into errwarns.
  134. */
  135. YASM_LIB_DECL
  136. void yasm_object_finalize(yasm_object *object, yasm_errwarns *errwarns);
  137. /** Traverses all sections in an object, calling a function on each section.
  138. * \param object object
  139. * \param d data pointer passed to func on each call
  140. * \param func function
  141. * \return Stops early (and returns func's return value) if func returns a
  142. * nonzero value; otherwise 0.
  143. */
  144. YASM_LIB_DECL
  145. int yasm_object_sections_traverse
  146. (yasm_object *object, /*@null@*/ void *d,
  147. int (*func) (yasm_section *sect, /*@null@*/ void *d));
  148. /** Find a general section in an object, based on its name.
  149. * \param object object
  150. * \param name section name
  151. * \return Section matching name, or NULL if no match found.
  152. */
  153. YASM_LIB_DECL
  154. /*@dependent@*/ /*@null@*/ yasm_section *yasm_object_find_general
  155. (yasm_object *object, const char *name);
  156. /** Change the source filename for an object.
  157. * \param object object
  158. * \param src_filename new source filename (e.g. "file.asm")
  159. */
  160. YASM_LIB_DECL
  161. void yasm_object_set_source_fn(yasm_object *object, const char *src_filename);
  162. /** Change the prefix used for externally-visible symbols.
  163. * \param object object
  164. * \param prefix new prefix
  165. */
  166. YASM_LIB_DECL
  167. void yasm_object_set_global_prefix(yasm_object *object, const char *prefix);
  168. /** Change the suffix used for externally-visible symbols.
  169. * \param object object
  170. * \param suffix new suffix
  171. */
  172. YASM_LIB_DECL
  173. void yasm_object_set_global_suffix(yasm_object *object, const char *suffix);
  174. /** Optimize an object. Takes the unoptimized object and optimizes it.
  175. * If successful, the object is ready for output to an object file.
  176. * \param object object
  177. * \param errwarns error/warning set
  178. * \note Optimization failures are stored into errwarns.
  179. */
  180. YASM_LIB_DECL
  181. void yasm_object_optimize(yasm_object *object, yasm_errwarns *errwarns);
  182. /** Determine if a section is flagged to contain code.
  183. * \param sect section
  184. * \return Nonzero if section is flagged to contain code.
  185. */
  186. YASM_LIB_DECL
  187. int yasm_section_is_code(yasm_section *sect);
  188. /** Get yasm_optimizer-specific flags. For yasm_optimizer use only.
  189. * \param sect section
  190. * \return Optimizer-specific flags.
  191. */
  192. YASM_LIB_DECL
  193. unsigned long yasm_section_get_opt_flags(const yasm_section *sect);
  194. /** Set yasm_optimizer-specific flags. For yasm_optimizer use only.
  195. * \param sect section
  196. * \param opt_flags optimizer-specific flags.
  197. */
  198. YASM_LIB_DECL
  199. void yasm_section_set_opt_flags(yasm_section *sect, unsigned long opt_flags);
  200. /** Determine if a section was declared as the "default" section (e.g. not
  201. * created through a section directive).
  202. * \param sect section
  203. * \return Nonzero if section was declared as default.
  204. */
  205. YASM_LIB_DECL
  206. int yasm_section_is_default(const yasm_section *sect);
  207. /** Set section "default" flag to a new value.
  208. * \param sect section
  209. * \param def new value of default flag
  210. */
  211. YASM_LIB_DECL
  212. void yasm_section_set_default(yasm_section *sect, int def);
  213. /** Get object owner of a section.
  214. * \param sect section
  215. * \return Object this section is a part of.
  216. */
  217. YASM_LIB_DECL
  218. yasm_object *yasm_section_get_object(const yasm_section *sect);
  219. /** Get assocated data for a section and data callback.
  220. * \param sect section
  221. * \param callback callback used when adding data
  222. * \return Associated data (NULL if none).
  223. */
  224. YASM_LIB_DECL
  225. /*@dependent@*/ /*@null@*/ void *yasm_section_get_data
  226. (yasm_section *sect, const yasm_assoc_data_callback *callback);
  227. /** Add associated data to a section.
  228. * \attention Deletes any existing associated data for that data callback.
  229. * \param sect section
  230. * \param callback callback
  231. * \param data data to associate
  232. */
  233. YASM_LIB_DECL
  234. void yasm_section_add_data(yasm_section *sect,
  235. const yasm_assoc_data_callback *callback,
  236. /*@null@*/ /*@only@*/ void *data);
  237. /** Add a relocation to a section.
  238. * \param sect section
  239. * \param reloc relocation
  240. * \param destroy_func function that can destroy the relocation
  241. * \note Does not make a copy of reloc. The same destroy_func must be
  242. * used for all relocations in a section or an internal error will occur.
  243. * The section will destroy the relocation address; it is the caller's
  244. * responsibility to destroy any other allocated data.
  245. */
  246. YASM_LIB_DECL
  247. void yasm_section_add_reloc(yasm_section *sect, yasm_reloc *reloc,
  248. void (*destroy_func) (/*@only@*/ void *reloc));
  249. /** Get the first relocation for a section.
  250. * \param sect section
  251. * \return First relocation for section. NULL if no relocations.
  252. */
  253. YASM_LIB_DECL
  254. /*@null@*/ yasm_reloc *yasm_section_relocs_first(yasm_section *sect);
  255. /** Get the next relocation for a section.
  256. * \param reloc previous relocation
  257. * \return Next relocation for section. NULL if no more relocations.
  258. */
  259. /*@null@*/ yasm_reloc *yasm_section_reloc_next(yasm_reloc *reloc);
  260. #ifndef YASM_DOXYGEN
  261. #define yasm_section_reloc_next(x) STAILQ_NEXT((x), link)
  262. #endif
  263. /** Get the basic relocation information for a relocation.
  264. * \param reloc relocation
  265. * \param addrp address of relocation within section (returned)
  266. * \param symp relocated symbol (returned)
  267. */
  268. YASM_LIB_DECL
  269. void yasm_reloc_get(yasm_reloc *reloc, yasm_intnum **addrp,
  270. /*@dependent@*/ yasm_symrec **symp);
  271. /** Get the first bytecode in a section.
  272. * \param sect section
  273. * \return First bytecode in section (at least one empty bytecode is always
  274. * present).
  275. */
  276. YASM_LIB_DECL
  277. yasm_bytecode *yasm_section_bcs_first(yasm_section *sect);
  278. /** Get the last bytecode in a section.
  279. * \param sect section
  280. * \return Last bytecode in section (at least one empty bytecode is always
  281. * present).
  282. */
  283. YASM_LIB_DECL
  284. yasm_bytecode *yasm_section_bcs_last(yasm_section *sect);
  285. /** Add bytecode to the end of a section.
  286. * \note Does not make a copy of bc; so don't pass this function static or
  287. * local variables, and discard the bc pointer after calling this
  288. * function.
  289. * \param sect section
  290. * \param bc bytecode (may be NULL)
  291. * \return If bytecode was actually appended (it wasn't NULL or empty), the
  292. * bytecode; otherwise NULL.
  293. */
  294. YASM_LIB_DECL
  295. /*@only@*/ /*@null@*/ yasm_bytecode *yasm_section_bcs_append
  296. (yasm_section *sect,
  297. /*@returned@*/ /*@only@*/ /*@null@*/ yasm_bytecode *bc);
  298. /** Traverses all bytecodes in a section, calling a function on each bytecode.
  299. * \param sect section
  300. * \param errwarns error/warning set (may be NULL)
  301. * \param d data pointer passed to func on each call (may be NULL)
  302. * \param func function
  303. * \return Stops early (and returns func's return value) if func returns a
  304. * nonzero value; otherwise 0.
  305. * \note If errwarns is non-NULL, yasm_errwarn_propagate() is called after
  306. * each call to func (with the bytecode's line number).
  307. */
  308. YASM_LIB_DECL
  309. int yasm_section_bcs_traverse
  310. (yasm_section *sect, /*@null@*/ yasm_errwarns *errwarns,
  311. /*@null@*/ void *d, int (*func) (yasm_bytecode *bc, /*@null@*/ void *d));
  312. /** Get name of a section.
  313. * \param sect section
  314. * \return Section name.
  315. */
  316. YASM_LIB_DECL
  317. /*@observer@*/ const char *yasm_section_get_name(const yasm_section *sect);
  318. /** Change alignment of a section.
  319. * \param sect section
  320. * \param align alignment in bytes
  321. * \param line virtual line
  322. */
  323. YASM_LIB_DECL
  324. void yasm_section_set_align(yasm_section *sect, unsigned long align,
  325. unsigned long line);
  326. /** Get alignment of a section.
  327. * \param sect section
  328. * \return Alignment in bytes (0 if none).
  329. */
  330. YASM_LIB_DECL
  331. unsigned long yasm_section_get_align(const yasm_section *sect);
  332. /** Print a section. For debugging purposes.
  333. * \param f file
  334. * \param indent_level indentation level
  335. * \param sect section
  336. * \param print_bcs if nonzero, print bytecodes within section
  337. */
  338. YASM_LIB_DECL
  339. void yasm_section_print(/*@null@*/ const yasm_section *sect, FILE *f,
  340. int indent_level, int print_bcs);
  341. #endif