From: Peter Johnson Date: Fri, 11 Nov 2005 04:31:20 +0000 (-0000) Subject: In GAS mode, detect sections that are marked as contain code thanks to "x" X-Git-Tag: v0.5.0rc1~60 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=94265938fd7f87f1c8ecc6e02374b6ded6bd7b8b;p=yasm In GAS mode, detect sections that are marked as contain code thanks to "x" flag, not just sections named ".text". * section.h (yasm_object_get_general): Add code flag parameter to indicate if section is intended to contain code. (yasm_section_is_code): New, to get value of code flag. * section.c (yasm_section): Add flag to section structure. (yasm_object_get_general, yasm_section_is_code): Implement flag. * *-objfmt.c, stabs-dbgfmt.c: Update call to yasm_object_get_general, setting code flag appropriately (only elf *really* handles this correctly, coff still needs to handle GAS flags to work fully from GAS mode). * gas-parser.h (yasm_parser_gas): Remove code_section flag. * gas-bison.y (gas_switch_section): Don't set. (gas_parser_align): Use yasm_section_is_code() instead of code_section flag. * gas-parser.c: Don't initialize code_section flag. svn path=/trunk/yasm/; revision=1318 --- diff --git a/libyasm/section.c b/libyasm/section.c index 6ebbaffe..d691718a 100644 --- a/libyasm/section.c +++ b/libyasm/section.c @@ -73,6 +73,7 @@ struct yasm_section { unsigned long opt_flags; /* storage for optimizer flags */ + int code; /* section contains code (instructions) */ int res_only; /* allow only resb family of bytecodes? */ /* the bytecodes for the section's contents */ @@ -107,7 +108,7 @@ yasm_object_create(void) /*@-onlytrans@*/ yasm_section * yasm_object_get_general(yasm_object *object, const char *name, - yasm_expr *start, int res_only, int *isnew, + yasm_expr *start, int code, int res_only, int *isnew, unsigned long line) { yasm_section *s; @@ -153,6 +154,7 @@ yasm_object_get_general(yasm_object *object, const char *name, STAILQ_INIT(&s->relocs); s->destroy_reloc = NULL; + s->code = code; s->res_only = res_only; *isnew = 1; @@ -211,6 +213,12 @@ yasm_section_is_absolute(yasm_section *sect) return (sect->type == SECTION_ABSOLUTE); } +int +yasm_section_is_code(yasm_section *sect) +{ + return sect->code; +} + unsigned long yasm_section_get_opt_flags(const yasm_section *sect) { diff --git a/libyasm/section.h b/libyasm/section.h index 1ad9f2f9..6634da8f 100644 --- a/libyasm/section.h +++ b/libyasm/section.h @@ -60,6 +60,8 @@ struct yasm_reloc { * \param name section name * \param start starting address (ignored if section already exists), * NULL if 0 or don't care. + * \param code if nonzero, section is intended to contain code + * (e.g. alignment should be made with NOP instructions, not 0) * \param res_only if nonzero, only space-reserving bytecodes are allowed in * the section (ignored if section already exists) * \param isnew output; set to nonzero if section did not already exist @@ -69,7 +71,7 @@ struct yasm_reloc { */ /*@dependent@*/ yasm_section *yasm_object_get_general (yasm_object *object, const char *name, - /*@null@*/ /*@only@*/ yasm_expr *start, int res_only, + /*@null@*/ /*@only@*/ yasm_expr *start, int code, int res_only, /*@out@*/ int *isnew, unsigned long line); /** Create a new absolute section. No checking is performed at creation to @@ -138,6 +140,12 @@ int yasm_object_sections_traverse */ int yasm_section_is_absolute(yasm_section *sect); +/** Determine if a section is flagged to contain code. + * \param sect section + * \return Nonzero if section is flagged to contain code. + */ +int yasm_section_is_code(yasm_section *sect); + /** Get yasm_optimizer-specific flags. For yasm_optimizer use only. * \param sect section * \return Optimizer-specific flags. diff --git a/modules/dbgfmts/stabs/stabs-dbgfmt.c b/modules/dbgfmts/stabs/stabs-dbgfmt.c index 8fd8b9c4..578e1261 100644 --- a/modules/dbgfmts/stabs/stabs-dbgfmt.c +++ b/modules/dbgfmts/stabs/stabs-dbgfmt.c @@ -336,7 +336,7 @@ stabs_dbgfmt_generate(yasm_dbgfmt *dbgfmt) info.dbgfmt_stabs = dbgfmt_stabs; info.lastline = 0; info.stabcount = 0; - info.stab = yasm_object_get_general(dbgfmt_stabs->object, ".stab", 0, 0, + info.stab = yasm_object_get_general(dbgfmt_stabs->object, ".stab", 0, 0, 0, &new, 0); if (!new) { yasm_bytecode *last = yasm_section_bcs_last(info.stab); @@ -349,7 +349,7 @@ stabs_dbgfmt_generate(yasm_dbgfmt *dbgfmt) } info.stabstr = yasm_object_get_general(dbgfmt_stabs->object, ".stabstr", 0, - 0, &new, 0); + 0, 0, &new, 0); if (!new) { yasm_bytecode *last = yasm_section_bcs_last(info.stabstr); if (last == NULL) diff --git a/modules/objfmts/bin/bin-objfmt.c b/modules/objfmts/bin/bin-objfmt.c index e8102612..27fafb93 100644 --- a/modules/objfmts/bin/bin-objfmt.c +++ b/modules/objfmts/bin/bin-objfmt.c @@ -423,8 +423,8 @@ bin_objfmt_section_switch(yasm_objfmt *objfmt, yasm_valparamhead *valparams, retval = yasm_object_get_general(objfmt_bin->object, sectname, yasm_expr_create_ident( - yasm_expr_int(yasm_intnum_create_uint(start)), line), resonly, - &isnew, line); + yasm_expr_int(yasm_intnum_create_uint(start)), line), + strcmp(sectname, ".text") == 0, resonly, &isnew, line); if (isnew) { if (have_alignval) { diff --git a/modules/objfmts/coff/coff-objfmt.c b/modules/objfmts/coff/coff-objfmt.c index 8702a03c..fd18e3a3 100644 --- a/modules/objfmts/coff/coff-objfmt.c +++ b/modules/objfmts/coff/coff-objfmt.c @@ -1228,8 +1228,9 @@ coff_objfmt_section_switch(yasm_objfmt *objfmt, yasm_valparamhead *valparams, N_("Standard COFF does not support qualifier `%s'"), vp->val); } - retval = yasm_object_get_general(objfmt_coff->object, sectname, 0, resonly, - &isnew, line); + retval = yasm_object_get_general(objfmt_coff->object, sectname, 0, + (flags & COFF_STYP_EXECUTE) != 0, + resonly, &isnew, line); if (isnew) coff_objfmt_init_new_section(objfmt_coff, retval, sectname, flags, @@ -1417,7 +1418,7 @@ win32_objfmt_directive(yasm_objfmt *objfmt, const char *name, /* Add to end of linker directives */ sect = yasm_object_get_general(objfmt_coff->object, ".drectve", 0, 0, - &isnew, line); + 0, &isnew, line); /* Initialize directive section if needed */ if (isnew) diff --git a/modules/objfmts/dbg/dbg-objfmt.c b/modules/objfmts/dbg/dbg-objfmt.c index 80f40119..3b2b7fcc 100644 --- a/modules/objfmts/dbg/dbg-objfmt.c +++ b/modules/objfmts/dbg/dbg-objfmt.c @@ -118,7 +118,7 @@ dbg_objfmt_section_switch(yasm_objfmt *objfmt, yasm_valparamhead *valparams, if ((vp = yasm_vps_first(valparams)) && !vp->param && vp->val != NULL) { retval = yasm_object_get_general(objfmt_dbg->object, vp->val, yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(200)), - line), 0, &isnew, line); + line), 0, 0, &isnew, line); if (isnew) { fprintf(objfmt_dbg->dbgfile, "(new) "); yasm_symtab_define_label( diff --git a/modules/objfmts/elf/elf-objfmt.c b/modules/objfmts/elf/elf-objfmt.c index 05331343..4d14b76f 100644 --- a/modules/objfmts/elf/elf-objfmt.c +++ b/modules/objfmts/elf/elf-objfmt.c @@ -868,7 +868,8 @@ elf_objfmt_section_switch(yasm_objfmt *objfmt, yasm_valparamhead *valparams, N_("Unrecognized qualifier `%s'"), vp->val); } - retval = yasm_object_get_general(objfmt_elf->object, sectname, 0, resonly, + retval = yasm_object_get_general(objfmt_elf->object, sectname, 0, + (flags & SHF_EXECINSTR) != 0, resonly, &isnew, line); if (isnew) { diff --git a/modules/objfmts/xdf/xdf-objfmt.c b/modules/objfmts/xdf/xdf-objfmt.c index 7e932ed3..30f87c53 100644 --- a/modules/objfmts/xdf/xdf-objfmt.c +++ b/modules/objfmts/xdf/xdf-objfmt.c @@ -741,8 +741,8 @@ xdf_objfmt_section_switch(yasm_objfmt *objfmt, yasm_valparamhead *valparams, N_("Unrecognized qualifier `%s'"), vp->val); } - retval = yasm_object_get_general(objfmt_xdf->object, sectname, 0, resonly, - &isnew, line); + retval = yasm_object_get_general(objfmt_xdf->object, sectname, 0, 1, + resonly, &isnew, line); if (isnew) { xdf_section_data *data; diff --git a/modules/parsers/gas/gas-bison.y b/modules/parsers/gas/gas-bison.y index 8d132f48..0370a7d8 100644 --- a/modules/parsers/gas/gas-bison.y +++ b/modules/parsers/gas/gas-bison.y @@ -802,7 +802,6 @@ gas_switch_section(yasm_parser_gas *parser_gas, char *name, parser_gas->prev_bc = yasm_section_bcs_last(new_section); } else yasm__error(cur_line, N_("invalid section name `%s'"), name); - parser_gas->code_section = !strcmp(name, ".text"); } static yasm_bytecode * @@ -858,7 +857,7 @@ gas_parser_align(yasm_parser_gas *parser_gas, yasm_valparamhead *valparams, } return yasm_bc_create_align(boundval, fillval, maxskipval, - parser_gas->code_section ? + yasm_section_is_code(parser_gas->cur_section) ? yasm_arch_get_fill(parser_gas->arch) : NULL, cur_line); } diff --git a/modules/parsers/gas/gas-parser.c b/modules/parsers/gas/gas-parser.c index 9df1cecd..9e215a72 100644 --- a/modules/parsers/gas/gas-parser.c +++ b/modules/parsers/gas/gas-parser.c @@ -77,8 +77,6 @@ gas_parser_do_parse(yasm_object *object, yasm_preproc *pp, yasm_arch *a, parser_gas.state = INITIAL; - parser_gas.code_section = !strcmp(yasm_section_get_name(def_sect), ".text"); - parser_gas.rept = NULL; /* yacc debugging, needs YYDEBUG set in bison.y.in to work */ diff --git a/modules/parsers/gas/gas-parser.h b/modules/parsers/gas/gas-parser.h index df9421f2..23806c29 100644 --- a/modules/parsers/gas/gas-parser.h +++ b/modules/parsers/gas/gas-parser.h @@ -92,8 +92,6 @@ typedef struct yasm_parser_gas { INSTDIR } state; - int code_section; - /*@null@*/ gas_rept *rept; } yasm_parser_gas; diff --git a/modules/parsers/gas/tests/Makefile.inc b/modules/parsers/gas/tests/Makefile.inc index 5c940d8e..b463f8f1 100644 --- a/modules/parsers/gas/tests/Makefile.inc +++ b/modules/parsers/gas/tests/Makefile.inc @@ -12,6 +12,9 @@ EXTRA_DIST += modules/parsers/gas/tests/datavis.hex EXTRA_DIST += modules/parsers/gas/tests/datavis2.asm EXTRA_DIST += modules/parsers/gas/tests/datavis2.errwarn EXTRA_DIST += modules/parsers/gas/tests/datavis2.hex +EXTRA_DIST += modules/parsers/gas/tests/execsect.asm +EXTRA_DIST += modules/parsers/gas/tests/execsect.errwarn +EXTRA_DIST += modules/parsers/gas/tests/execsect.hex EXTRA_DIST += modules/parsers/gas/tests/gas-instlabel.asm EXTRA_DIST += modules/parsers/gas/tests/gas-instlabel.errwarn EXTRA_DIST += modules/parsers/gas/tests/gas-instlabel.hex diff --git a/modules/parsers/gas/tests/execsect.asm b/modules/parsers/gas/tests/execsect.asm new file mode 100644 index 00000000..48ff6cb7 --- /dev/null +++ b/modules/parsers/gas/tests/execsect.asm @@ -0,0 +1,4 @@ +.section .foobar, "ax",@progbits +xorl %eax, %eax +.p2align 3 +xorl %eax, %eax diff --git a/modules/parsers/gas/tests/execsect.errwarn b/modules/parsers/gas/tests/execsect.errwarn new file mode 100644 index 00000000..e69de29b diff --git a/modules/parsers/gas/tests/execsect.hex b/modules/parsers/gas/tests/execsect.hex new file mode 100644 index 00000000..f442222b --- /dev/null +++ b/modules/parsers/gas/tests/execsect.hex @@ -0,0 +1,432 @@ +7f +45 +4c +46 +01 +01 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +01 +00 +03 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +c0 +00 +00 +00 +00 +00 +00 +00 +34 +00 +00 +00 +00 +00 +28 +00 +06 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +31 +c0 +8d +b6 +00 +00 +00 +00 +31 +c0 +00 +00 +00 +2e +74 +65 +78 +74 +00 +2e +66 +6f +6f +62 +61 +72 +00 +2e +73 +74 +72 +74 +61 +62 +00 +2e +73 +79 +6d +74 +61 +62 +00 +2e +73 +68 +73 +74 +72 +74 +61 +62 +00 +00 +00 +00 +00 +2d +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +01 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +04 +00 +f1 +ff +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +03 +00 +05 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +03 +00 +04 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +1f +00 +00 +00 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +4c +00 +00 +00 +29 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +0f +00 +00 +00 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +78 +00 +00 +00 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +17 +00 +00 +00 +02 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +7c +00 +00 +00 +40 +00 +00 +00 +02 +00 +00 +00 +04 +00 +00 +00 +04 +00 +00 +00 +10 +00 +00 +00 +01 +00 +00 +00 +01 +00 +00 +00 +06 +00 +00 +00 +00 +00 +00 +00 +40 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +10 +00 +00 +00 +00 +00 +00 +00 +07 +00 +00 +00 +01 +00 +00 +00 +06 +00 +00 +00 +00 +00 +00 +00 +40 +00 +00 +00 +0a +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +08 +00 +00 +00 +00 +00 +00 +00