4 * Copyright (C) 2001-2007 Peter Johnson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
29 #include "libyasm-stdint.h"
44 typedef struct bytecode_incbin {
45 /*@only@*/ char *filename; /* file to include data from */
46 const char *from; /* filename of what contained incbin */
48 /* starting offset to read from (NULL=0) */
49 /*@only@*/ /*@null@*/ yasm_expr *start;
51 /* maximum number of bytes to read (NULL=no limit) */
52 /*@only@*/ /*@null@*/ yasm_expr *maxlen;
55 static void bc_incbin_destroy(void *contents);
56 static void bc_incbin_print(const void *contents, FILE *f, int indent_level);
57 static void bc_incbin_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc);
58 static int bc_incbin_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
60 static int bc_incbin_tobytes(yasm_bytecode *bc, unsigned char **bufp,
61 unsigned char *bufstart, void *d,
62 yasm_output_value_func output_value,
63 /*@null@*/ yasm_output_reloc_func output_reloc);
65 static const yasm_bytecode_callback bc_incbin_callback = {
71 yasm_bc_expand_common,
78 bc_incbin_destroy(void *contents)
80 bytecode_incbin *incbin = (bytecode_incbin *)contents;
81 yasm_xfree(incbin->filename);
82 yasm_expr_destroy(incbin->start);
83 yasm_expr_destroy(incbin->maxlen);
88 bc_incbin_print(const void *contents, FILE *f, int indent_level)
90 const bytecode_incbin *incbin = (const bytecode_incbin *)contents;
91 fprintf(f, "%*s_IncBin_\n", indent_level, "");
92 fprintf(f, "%*sFilename=`%s'\n", indent_level, "",
94 fprintf(f, "%*sStart=", indent_level, "");
96 fprintf(f, "nil (0)");
98 yasm_expr_print(incbin->start, f);
99 fprintf(f, "%*sMax Len=", indent_level, "");
101 fprintf(f, "nil (unlimited)");
103 yasm_expr_print(incbin->maxlen, f);
108 bc_incbin_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc)
110 bytecode_incbin *incbin = (bytecode_incbin *)bc->contents;
113 if (yasm_value_finalize_expr(&val, incbin->start, prev_bc, 0))
114 yasm_error_set(YASM_ERROR_TOO_COMPLEX,
115 N_("start expression too complex"));
117 yasm_error_set(YASM_ERROR_NOT_ABSOLUTE,
118 N_("start expression not absolute"));
119 incbin->start = val.abs;
121 if (yasm_value_finalize_expr(&val, incbin->maxlen, prev_bc, 0))
122 yasm_error_set(YASM_ERROR_TOO_COMPLEX,
123 N_("maximum length expression too complex"));
125 yasm_error_set(YASM_ERROR_NOT_ABSOLUTE,
126 N_("maximum length expression not absolute"));
127 incbin->maxlen = val.abs;
131 bc_incbin_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
134 bytecode_incbin *incbin = (bytecode_incbin *)bc->contents;
136 /*@dependent@*/ /*@null@*/ const yasm_intnum *num;
137 unsigned long start = 0, maxlen = 0xFFFFFFFFUL, flen;
139 /* Try to convert start to integer value */
141 num = yasm_expr_get_intnum(&incbin->start, 0);
143 start = yasm_intnum_get_uint(num);
146 yasm_error_set(YASM_ERROR_NOT_IMPLEMENTED,
147 N_("incbin does not yet understand non-constant"));
152 /* Try to convert maxlen to integer value */
153 if (incbin->maxlen) {
154 num = yasm_expr_get_intnum(&incbin->maxlen, 0);
156 maxlen = yasm_intnum_get_uint(num);
159 yasm_error_set(YASM_ERROR_NOT_IMPLEMENTED,
160 N_("incbin does not yet understand non-constant"));
165 /* Open file and determine its length */
166 f = yasm_fopen_include(incbin->filename, incbin->from, "rb", NULL);
168 yasm_error_set(YASM_ERROR_IO,
169 N_("`incbin': unable to open file `%s'"),
173 if (fseek(f, 0L, SEEK_END) < 0) {
174 yasm_error_set(YASM_ERROR_IO,
175 N_("`incbin': unable to seek on file `%s'"),
179 flen = (unsigned long)ftell(f);
182 /* Compute length of incbin from start, maxlen, and len */
184 yasm_warn_set(YASM_WARN_GENERAL,
185 N_("`incbin': start past end of file `%s'"),
198 bc_incbin_tobytes(yasm_bytecode *bc, unsigned char **bufp,
199 unsigned char *bufstart, void *d,
200 yasm_output_value_func output_value,
201 /*@unused@*/ yasm_output_reloc_func output_reloc)
203 bytecode_incbin *incbin = (bytecode_incbin *)bc->contents;
205 /*@dependent@*/ /*@null@*/ const yasm_intnum *num;
206 unsigned long start = 0;
208 /* Convert start to integer value */
210 num = yasm_expr_get_intnum(&incbin->start, 0);
213 N_("could not determine start in bc_tobytes_incbin"));
214 start = yasm_intnum_get_uint(num);
218 f = yasm_fopen_include(incbin->filename, incbin->from, "rb", NULL);
220 yasm_error_set(YASM_ERROR_IO, N_("`incbin': unable to open file `%s'"),
225 /* Seek to start of data */
226 if (fseek(f, (long)start, SEEK_SET) < 0) {
227 yasm_error_set(YASM_ERROR_IO,
228 N_("`incbin': unable to seek on file `%s'"),
235 if (fread(*bufp, 1, (size_t)bc->len, f) < (size_t)bc->len) {
236 yasm_error_set(YASM_ERROR_IO,
237 N_("`incbin': unable to read %lu bytes from file `%s'"),
238 bc->len, incbin->filename);
249 yasm_bc_create_incbin(char *filename, yasm_expr *start, yasm_expr *maxlen,
250 yasm_linemap *linemap, unsigned long line)
252 bytecode_incbin *incbin = yasm_xmalloc(sizeof(bytecode_incbin));
255 /* Find from filename based on line number */
256 yasm_linemap_lookup(linemap, line, &incbin->from, &xline);
259 incbin->filename = filename;
260 incbin->start = start;
261 incbin->maxlen = maxlen;
264 return yasm_bc_create_common(&bc_incbin_callback, incbin, line);