section.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 *obj_filename; /**< Object filename */
  48. /*@owned@*/ yasm_symtab *symtab; /**< Symbol table */
  49. /*@owned@*/ yasm_arch *arch; /**< Target architecture */
  50. /*@owned@*/ yasm_objfmt *objfmt; /**< Object format */
  51. /*@owned@*/ yasm_dbgfmt *dbgfmt; /**< Debug format */
  52. /** Currently active section. Used by some directives. NULL if no
  53. * section active.
  54. */
  55. /*@dependent@*/ /*@null@*/ yasm_section *cur_section;
  56. /** Linked list of sections. */
  57. /*@reldef@*/ STAILQ_HEAD(yasm_sectionhead, yasm_section) sections;
  58. /** Directives, organized as two level HAMT; first level is parser,
  59. * second level is directive name.
  60. */
  61. /*@owned@*/ struct HAMT *directives;
  62. /** Prefix prepended to externally-visible symbols (empty string if none) */
  63. /*@owned@*/ char *global_prefix;
  64. /** Suffix appended to externally-visible symbols (empty string if none) */
  65. /*@owned@*/ char *global_suffix;
  66. };
  67. /** Create a new object. A default section is created as the first section.
  68. * An empty symbol table (yasm_symtab) and line mapping (yasm_linemap) are
  69. * automatically created.
  70. * \param src_filename source filename (e.g. "file.asm")
  71. * \param obj_filename object filename (e.g. "file.o")
  72. * \param arch architecture
  73. * \param objfmt_module object format module
  74. * \param dbgfmt_module debug format module
  75. * \return Newly allocated object, or NULL on error.
  76. */
  77. YASM_LIB_DECL
  78. /*@null@*/ /*@only@*/ yasm_object *yasm_object_create
  79. (const char *src_filename, const char *obj_filename,
  80. /*@kept@*/ yasm_arch *arch,
  81. const yasm_objfmt_module *objfmt_module,
  82. const yasm_dbgfmt_module *dbgfmt_module);
  83. /** Create a new, or continue an existing, general section. The section is
  84. * added to the object if there's not already a section by that name.
  85. * \param object object
  86. * \param name section name
  87. * \param align alignment in bytes (0 if none)
  88. * \param code if nonzero, section is intended to contain code
  89. * (e.g. alignment should be made with NOP instructions, not 0)
  90. * \param res_only if nonzero, only space-reserving bytecodes are allowed in
  91. * the section (ignored if section already exists)
  92. * \param isnew output; set to nonzero if section did not already exist
  93. * \param line virtual line of section declaration (ignored if section
  94. * already exists)
  95. * \return New section.
  96. */
  97. YASM_LIB_DECL
  98. /*@dependent@*/ yasm_section *yasm_object_get_general
  99. (yasm_object *object, const char *name, unsigned long align, int code,
  100. int res_only, /*@out@*/ int *isnew, unsigned long line);
  101. /** Handle a directive. Passed down to object format, debug format, or
  102. * architecture as appropriate.
  103. * \param object object
  104. * \param name directive name
  105. * \param parser parser keyword
  106. * \param valparams value/parameters
  107. * \param objext_valparams "object format-specific" value/parameters
  108. * \param line virtual line (from yasm_linemap)
  109. * \return 0 if directive recognized, nonzero if unrecognized.
  110. */
  111. YASM_LIB_DECL
  112. int yasm_object_directive(yasm_object *object, const char *name,
  113. const char *parser, yasm_valparamhead *valparams,
  114. yasm_valparamhead *objext_valparams,
  115. unsigned long line);
  116. /** Delete (free allocated memory for) an object. All sections in the
  117. * object and all bytecodes within those sections are also deleted.
  118. * \param object object
  119. */
  120. YASM_LIB_DECL
  121. void yasm_object_destroy(/*@only@*/ yasm_object *object);
  122. /** Print an object. For debugging purposes.
  123. * \param object object
  124. * \param f file
  125. * \param indent_level indentation level
  126. */
  127. YASM_LIB_DECL
  128. void yasm_object_print(const yasm_object *object, FILE *f, int indent_level);
  129. /** Finalize an object after parsing.
  130. * \param object object
  131. * \param errwarns error/warning set
  132. * \note Errors/warnings are stored into errwarns.
  133. */
  134. YASM_LIB_DECL
  135. void yasm_object_finalize(yasm_object *object, yasm_errwarns *errwarns);
  136. /** Traverses all sections in an object, calling a function on each section.
  137. * \param object object
  138. * \param d data pointer passed to func on each call
  139. * \param func function
  140. * \return Stops early (and returns func's return value) if func returns a
  141. * nonzero value; otherwise 0.
  142. */
  143. YASM_LIB_DECL
  144. int yasm_object_sections_traverse
  145. (yasm_object *object, /*@null@*/ void *d,
  146. int (*func) (yasm_section *sect, /*@null@*/ void *d));
  147. /** Find a general section in an object, based on its name.
  148. * \param object object
  149. * \param name section name
  150. * \return Section matching name, or NULL if no match found.
  151. */
  152. YASM_LIB_DECL
  153. /*@dependent@*/ /*@null@*/ yasm_section *yasm_object_find_general
  154. (yasm_object *object, const char *name);
  155. /** Change the source filename for an object.
  156. * \param object object
  157. * \param src_filename new source filename (e.g. "file.asm")
  158. */
  159. YASM_LIB_DECL
  160. void yasm_object_set_source_fn(yasm_object *object, const char *src_filename);
  161. /** Change the prefix used for externally-visible symbols.
  162. * \param object object
  163. * \param prefix new prefix
  164. */
  165. YASM_LIB_DECL
  166. void yasm_object_set_global_prefix(yasm_object *object, const char *prefix);
  167. /** Change the suffix used for externally-visible symbols.
  168. * \param object object
  169. * \param suffix new suffix
  170. */
  171. YASM_LIB_DECL
  172. void yasm_object_set_global_suffix(yasm_object *object, const char *suffix);
  173. /** Optimize an object. Takes the unoptimized object and optimizes it.
  174. * If successful, the object is ready for output to an object file.
  175. * \param object object
  176. * \param errwarns error/warning set
  177. * \note Optimization failures are stored into errwarns.
  178. */
  179. YASM_LIB_DECL
  180. void yasm_object_optimize(yasm_object *object, yasm_errwarns *errwarns);
  181. /** Determine if a section is flagged to contain code.
  182. * \param sect section
  183. * \return Nonzero if section is flagged to contain code.
  184. */
  185. YASM_LIB_DECL
  186. int yasm_section_is_code(yasm_section *sect);
  187. /** Get yasm_optimizer-specific flags. For yasm_optimizer use only.
  188. * \param sect section
  189. * \return Optimizer-specific flags.
  190. */
  191. YASM_LIB_DECL
  192. unsigned long yasm_section_get_opt_flags(const yasm_section *sect);
  193. /** Set yasm_optimizer-specific flags. For yasm_optimizer use only.
  194. * \param sect section
  195. * \param opt_flags optimizer-specific flags.
  196. */
  197. YASM_LIB_DECL
  198. void yasm_section_set_opt_flags(yasm_section *sect, unsigned long opt_flags);
  199. /** Determine if a section was declared as the "default" section (e.g. not
  200. * created through a section directive).
  201. * \param sect section
  202. * \return Nonzero if section was declared as default.
  203. */
  204. YASM_LIB_DECL
  205. int yasm_section_is_default(const yasm_section *sect);
  206. /** Set section "default" flag to a new value.
  207. * \param sect section
  208. * \param def new value of default flag
  209. */
  210. YASM_LIB_DECL
  211. void yasm_section_set_default(yasm_section *sect, int def);
  212. /** Get object owner of a section.
  213. * \param sect section
  214. * \return Object this section is a part of.
  215. */
  216. YASM_LIB_DECL
  217. yasm_object *yasm_section_get_object(const yasm_section *sect);
  218. /** Get assocated data for a section and data callback.
  219. * \param sect section
  220. * \param callback callback used when adding data
  221. * \return Associated data (NULL if none).
  222. */
  223. YASM_LIB_DECL
  224. /*@dependent@*/ /*@null@*/ void *yasm_section_get_data
  225. (yasm_section *sect, const yasm_assoc_data_callback *callback);
  226. /** Add associated data to a section.
  227. * \attention Deletes any existing associated data for that data callback.
  228. * \param sect section
  229. * \param callback callback
  230. * \param data data to associate
  231. */
  232. YASM_LIB_DECL
  233. void yasm_section_add_data(yasm_section *sect,
  234. const yasm_assoc_data_callback *callback,
  235. /*@null@*/ /*@only@*/ void *data);
  236. /** Add a relocation to a section.
  237. * \param sect section
  238. * \param reloc relocation
  239. * \param destroy_func function that can destroy the relocation
  240. * \note Does not make a copy of reloc. The same destroy_func must be
  241. * used for all relocations in a section or an internal error will occur.
  242. * The section will destroy the relocation address; it is the caller's
  243. * responsibility to destroy any other allocated data.
  244. */
  245. YASM_LIB_DECL
  246. void yasm_section_add_reloc(yasm_section *sect, yasm_reloc *reloc,
  247. void (*destroy_func) (/*@only@*/ void *reloc));
  248. /** Get the first relocation for a section.
  249. * \param sect section
  250. * \return First relocation for section. NULL if no relocations.
  251. */
  252. YASM_LIB_DECL
  253. /*@null@*/ yasm_reloc *yasm_section_relocs_first(yasm_section *sect);
  254. /** Get the next relocation for a section.
  255. * \param reloc previous relocation
  256. * \return Next relocation for section. NULL if no more relocations.
  257. */
  258. /*@null@*/ yasm_reloc *yasm_section_reloc_next(yasm_reloc *reloc);
  259. #ifndef YASM_DOXYGEN
  260. #define yasm_section_reloc_next(x) STAILQ_NEXT((x), link)
  261. #endif
  262. /** Get the basic relocation information for a relocation.
  263. * \param reloc relocation
  264. * \param addrp address of relocation within section (returned)
  265. * \param symp relocated symbol (returned)
  266. */
  267. YASM_LIB_DECL
  268. void yasm_reloc_get(yasm_reloc *reloc, yasm_intnum **addrp,
  269. /*@dependent@*/ yasm_symrec **symp);
  270. /** Get the first bytecode in a section.
  271. * \param sect section
  272. * \return First bytecode in section (at least one empty bytecode is always
  273. * present).
  274. */
  275. YASM_LIB_DECL
  276. yasm_bytecode *yasm_section_bcs_first(yasm_section *sect);
  277. /** Get the last bytecode in a section.
  278. * \param sect section
  279. * \return Last bytecode in section (at least one empty bytecode is always
  280. * present).
  281. */
  282. YASM_LIB_DECL
  283. yasm_bytecode *yasm_section_bcs_last(yasm_section *sect);
  284. /** Add bytecode to the end of a section.
  285. * \note Does not make a copy of bc; so don't pass this function static or
  286. * local variables, and discard the bc pointer after calling this
  287. * function.
  288. * \param sect section
  289. * \param bc bytecode (may be NULL)
  290. * \return If bytecode was actually appended (it wasn't NULL or empty), the
  291. * bytecode; otherwise NULL.
  292. */
  293. YASM_LIB_DECL
  294. /*@only@*/ /*@null@*/ yasm_bytecode *yasm_section_bcs_append
  295. (yasm_section *sect,
  296. /*@returned@*/ /*@only@*/ /*@null@*/ yasm_bytecode *bc);
  297. /** Traverses all bytecodes in a section, calling a function on each bytecode.
  298. * \param sect section
  299. * \param errwarns error/warning set (may be NULL)
  300. * \param d data pointer passed to func on each call (may be NULL)
  301. * \param func function
  302. * \return Stops early (and returns func's return value) if func returns a
  303. * nonzero value; otherwise 0.
  304. * \note If errwarns is non-NULL, yasm_errwarn_propagate() is called after
  305. * each call to func (with the bytecode's line number).
  306. */
  307. YASM_LIB_DECL
  308. int yasm_section_bcs_traverse
  309. (yasm_section *sect, /*@null@*/ yasm_errwarns *errwarns,
  310. /*@null@*/ void *d, int (*func) (yasm_bytecode *bc, /*@null@*/ void *d));
  311. /** Get name of a section.
  312. * \param sect section
  313. * \return Section name.
  314. */
  315. YASM_LIB_DECL
  316. /*@observer@*/ const char *yasm_section_get_name(const yasm_section *sect);
  317. /** Change alignment of a section.
  318. * \param sect section
  319. * \param align alignment in bytes
  320. * \param line virtual line
  321. */
  322. YASM_LIB_DECL
  323. void yasm_section_set_align(yasm_section *sect, unsigned long align,
  324. unsigned long line);
  325. /** Get alignment of a section.
  326. * \param sect section
  327. * \return Alignment in bytes (0 if none).
  328. */
  329. YASM_LIB_DECL
  330. unsigned long yasm_section_get_align(const yasm_section *sect);
  331. /** Print a section. For debugging purposes.
  332. * \param f file
  333. * \param indent_level indentation level
  334. * \param sect section
  335. * \param print_bcs if nonzero, print bytecodes within section
  336. */
  337. YASM_LIB_DECL
  338. void yasm_section_print(/*@null@*/ const yasm_section *sect, FILE *f,
  339. int indent_level, int print_bcs);
  340. #endif