145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* 245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Incbin bytecode 345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Copyright (C) 2001-2007 Peter Johnson 545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Redistribution and use in source and binary forms, with or without 745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * modification, are permitted provided that the following conditions 845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * are met: 945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1. Redistributions of source code must retain the above copyright 1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * notice, this list of conditions and the following disclaimer. 1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 2. Redistributions in binary form must reproduce the above copyright 1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * notice, this list of conditions and the following disclaimer in the 1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * documentation and/or other materials provided with the distribution. 1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' 1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE 1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * POSSIBILITY OF SUCH DAMAGE. 2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "util.h" 2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "libyasm-stdint.h" 3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "coretype.h" 3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "linemap.h" 3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "errwarn.h" 3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "intnum.h" 3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "expr.h" 3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "value.h" 3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "bytecode.h" 4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "file.h" 4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtypedef struct bytecode_incbin { 4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /*@only@*/ char *filename; /* file to include data from */ 4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org const char *from; /* filename of what contained incbin */ 4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* starting offset to read from (NULL=0) */ 4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /*@only@*/ /*@null@*/ yasm_expr *start; 5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* maximum number of bytes to read (NULL=no limit) */ 5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /*@only@*/ /*@null@*/ yasm_expr *maxlen; 5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} bytecode_incbin; 5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void bc_incbin_destroy(void *contents); 5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void bc_incbin_print(const void *contents, FILE *f, int indent_level); 5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void bc_incbin_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc); 5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic int bc_incbin_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, 5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org void *add_span_data); 60d65182f1818d1c19e6f3866ab6e68a262fad5185hbono@chromium.orgstatic int bc_incbin_tobytes(yasm_bytecode *bc, unsigned char **bufp, 61d65182f1818d1c19e6f3866ab6e68a262fad5185hbono@chromium.org unsigned char *bufstart, void *d, 6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_output_value_func output_value, 6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /*@null@*/ yasm_output_reloc_func output_reloc); 6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic const yasm_bytecode_callback bc_incbin_callback = { 6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org bc_incbin_destroy, 6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org bc_incbin_print, 6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org bc_incbin_finalize, 6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org NULL, 7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org bc_incbin_calc_len, 7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_bc_expand_common, 7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org bc_incbin_tobytes, 7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 0 7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}; 7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void 7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgbc_incbin_destroy(void *contents) 7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{ 8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org bytecode_incbin *incbin = (bytecode_incbin *)contents; 8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_xfree(incbin->filename); 8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_expr_destroy(incbin->start); 8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_expr_destroy(incbin->maxlen); 8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_xfree(contents); 8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void 8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgbc_incbin_print(const void *contents, FILE *f, int indent_level) 8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{ 9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org const bytecode_incbin *incbin = (const bytecode_incbin *)contents; 9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org fprintf(f, "%*s_IncBin_\n", indent_level, ""); 9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org fprintf(f, "%*sFilename=`%s'\n", indent_level, "", 9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org incbin->filename); 9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org fprintf(f, "%*sStart=", indent_level, ""); 9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!incbin->start) 9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org fprintf(f, "nil (0)"); 9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org else 9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_expr_print(incbin->start, f); 9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org fprintf(f, "%*sMax Len=", indent_level, ""); 10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!incbin->maxlen) 10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org fprintf(f, "nil (unlimited)"); 10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org else 10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_expr_print(incbin->maxlen, f); 10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org fprintf(f, "\n"); 10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void 10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgbc_incbin_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc) 10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{ 11045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org bytecode_incbin *incbin = (bytecode_incbin *)bc->contents; 11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_value val; 11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (yasm_value_finalize_expr(&val, incbin->start, prev_bc, 0)) 11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_error_set(YASM_ERROR_TOO_COMPLEX, 11545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org N_("start expression too complex")); 11645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org else if (val.rel) 11745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_error_set(YASM_ERROR_NOT_ABSOLUTE, 11845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org N_("start expression not absolute")); 11945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org incbin->start = val.abs; 12045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 12145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (yasm_value_finalize_expr(&val, incbin->maxlen, prev_bc, 0)) 12245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_error_set(YASM_ERROR_TOO_COMPLEX, 12345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org N_("maximum length expression too complex")); 12445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org else if (val.rel) 12545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_error_set(YASM_ERROR_NOT_ABSOLUTE, 12645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org N_("maximum length expression not absolute")); 12745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org incbin->maxlen = val.abs; 12845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 12945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 13045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic int 13145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgbc_incbin_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, 13245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org void *add_span_data) 13345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{ 13445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org bytecode_incbin *incbin = (bytecode_incbin *)bc->contents; 13545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org FILE *f; 13645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /*@dependent@*/ /*@null@*/ const yasm_intnum *num; 13745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org unsigned long start = 0, maxlen = 0xFFFFFFFFUL, flen; 13845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 13945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* Try to convert start to integer value */ 14045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (incbin->start) { 14145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org num = yasm_expr_get_intnum(&incbin->start, 0); 14245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (num) 14345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org start = yasm_intnum_get_uint(num); 14445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!num) { 14545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* FIXME */ 14645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_error_set(YASM_ERROR_NOT_IMPLEMENTED, 14745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org N_("incbin does not yet understand non-constant")); 14845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return -1; 14945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 15045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 15145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 15245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* Try to convert maxlen to integer value */ 15345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (incbin->maxlen) { 15445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org num = yasm_expr_get_intnum(&incbin->maxlen, 0); 15545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (num) 15645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org maxlen = yasm_intnum_get_uint(num); 15745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!num) { 15845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* FIXME */ 15945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_error_set(YASM_ERROR_NOT_IMPLEMENTED, 16045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org N_("incbin does not yet understand non-constant")); 16145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return -1; 16245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 16345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 16445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 16545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* Open file and determine its length */ 16645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org f = yasm_fopen_include(incbin->filename, incbin->from, "rb", NULL); 16745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!f) { 16845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_error_set(YASM_ERROR_IO, 16945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org N_("`incbin': unable to open file `%s'"), 17045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org incbin->filename); 17145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return -1; 17245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 17345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (fseek(f, 0L, SEEK_END) < 0) { 17445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_error_set(YASM_ERROR_IO, 17545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org N_("`incbin': unable to seek on file `%s'"), 17645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org incbin->filename); 17745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return -1; 17845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 17945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org flen = (unsigned long)ftell(f); 18045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org fclose(f); 18145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 18245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* Compute length of incbin from start, maxlen, and len */ 18345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (start > flen) { 18445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_warn_set(YASM_WARN_GENERAL, 18545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org N_("`incbin': start past end of file `%s'"), 18645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org incbin->filename); 18745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org start = flen; 18845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 18945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org flen -= start; 19045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (incbin->maxlen) 19145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (maxlen < flen) 19245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org flen = maxlen; 19345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org bc->len += flen; 19445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return 0; 19545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 19645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 19745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic int 198d65182f1818d1c19e6f3866ab6e68a262fad5185hbono@chromium.orgbc_incbin_tobytes(yasm_bytecode *bc, unsigned char **bufp, 199d65182f1818d1c19e6f3866ab6e68a262fad5185hbono@chromium.org unsigned char *bufstart, void *d, 20045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_output_value_func output_value, 20145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /*@unused@*/ yasm_output_reloc_func output_reloc) 20245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{ 20345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org bytecode_incbin *incbin = (bytecode_incbin *)bc->contents; 20445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org FILE *f; 20545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /*@dependent@*/ /*@null@*/ const yasm_intnum *num; 20645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org unsigned long start = 0; 20745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 20845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* Convert start to integer value */ 20945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (incbin->start) { 21045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org num = yasm_expr_get_intnum(&incbin->start, 0); 21145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!num) 21245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_internal_error( 21345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org N_("could not determine start in bc_tobytes_incbin")); 21445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org start = yasm_intnum_get_uint(num); 21545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 21645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 21745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* Open file */ 21845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org f = yasm_fopen_include(incbin->filename, incbin->from, "rb", NULL); 21945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!f) { 22045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_error_set(YASM_ERROR_IO, N_("`incbin': unable to open file `%s'"), 22145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org incbin->filename); 22245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return 1; 22345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 22445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 22545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* Seek to start of data */ 22645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (fseek(f, (long)start, SEEK_SET) < 0) { 22745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_error_set(YASM_ERROR_IO, 22845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org N_("`incbin': unable to seek on file `%s'"), 22945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org incbin->filename); 23045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org fclose(f); 23145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return 1; 23245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 23345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 23445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* Read len bytes */ 23545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (fread(*bufp, 1, (size_t)bc->len, f) < (size_t)bc->len) { 23645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_error_set(YASM_ERROR_IO, 23745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org N_("`incbin': unable to read %lu bytes from file `%s'"), 23845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org bc->len, incbin->filename); 23945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org fclose(f); 24045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return 1; 24145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org } 24245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 24345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org *bufp += bc->len; 24445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org fclose(f); 24545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return 0; 24645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 24745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 24845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgyasm_bytecode * 24945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgyasm_bc_create_incbin(char *filename, yasm_expr *start, yasm_expr *maxlen, 25045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_linemap *linemap, unsigned long line) 25145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{ 25245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org bytecode_incbin *incbin = yasm_xmalloc(sizeof(bytecode_incbin)); 25345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org unsigned long xline; 25445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 25545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /* Find from filename based on line number */ 25645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm_linemap_lookup(linemap, line, &incbin->from, &xline); 25745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 25845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /*@-mustfree@*/ 25945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org incbin->filename = filename; 26045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org incbin->start = start; 26145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org incbin->maxlen = maxlen; 26245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /*@=mustfree@*/ 26345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 26445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return yasm_bc_create_common(&bc_incbin_callback, incbin, line); 26545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 266