From 2b7a5219797e083d1824bb01c3a72ea96654da5a Mon Sep 17 00:00:00 2001 From: Michael Urman Date: Fri, 28 May 2004 13:19:48 +0000 Subject: [PATCH] Fixed handling of absolute sections in the elf object format, including preventing a segfault. The assumptions it had made for all sections were wrong with absolute sections, so added just as much special casing as necessary. I hope the output is correct as well - this way we should at least get bugs if it's wrong. Reported by: Ben Skeggs svn path=/trunk/yasm/; revision=1119 --- modules/objfmts/Makefile.inc | 2 + modules/objfmts/elf/elf-objfmt.c | 5 +- modules/objfmts/elf/elf.c | 34 +- modules/objfmts/elf/tests/Makefile.inc | 3 + modules/objfmts/elf/tests/elfabssect.asm | 9 + modules/objfmts/elf/tests/elfabssect.errwarn | 0 modules/objfmts/elf/tests/elfabssect.hex | 392 +++++++++++++++++++ 7 files changed, 430 insertions(+), 15 deletions(-) create mode 100644 modules/objfmts/elf/tests/elfabssect.asm create mode 100644 modules/objfmts/elf/tests/elfabssect.errwarn create mode 100644 modules/objfmts/elf/tests/elfabssect.hex diff --git a/modules/objfmts/Makefile.inc b/modules/objfmts/Makefile.inc index fc1896f8..b23b8c42 100644 --- a/modules/objfmts/Makefile.inc +++ b/modules/objfmts/Makefile.inc @@ -3,11 +3,13 @@ EXTRA_DIST += modules/objfmts/dbg/Makefile.inc EXTRA_DIST += modules/objfmts/bin/Makefile.inc EXTRA_DIST += modules/objfmts/elf/Makefile.inc +#!EXTRA_DIST += modules/objfmts/omf/Makefile.inc EXTRA_DIST += modules/objfmts/coff/Makefile.inc EXTRA_DIST += modules/objfmts/win32/Makefile.inc include modules/objfmts/dbg/Makefile.inc include modules/objfmts/bin/Makefile.inc include modules/objfmts/elf/Makefile.inc +#!include modules/objfmts/omf/Makefile.inc include modules/objfmts/coff/Makefile.inc include modules/objfmts/win32/Makefile.inc diff --git a/modules/objfmts/elf/elf-objfmt.c b/modules/objfmts/elf/elf-objfmt.c index e83a6039..eac4b8fb 100644 --- a/modules/objfmts/elf/elf-objfmt.c +++ b/modules/objfmts/elf/elf-objfmt.c @@ -114,8 +114,9 @@ elf_objfmt_append_local_sym(yasm_symrec *sym, /*@null@*/ void *d) if (!yasm_symrec_get_label(sym, &precbc)) return 1; sect = yasm_bc_get_section(precbc); - is_sect = strcmp(yasm_symrec_get_name(sym), - yasm_section_get_name(sect))==0; + if (!yasm_section_is_absolute(sect) && + strcmp(yasm_symrec_get_name(sym), yasm_section_get_name(sect))==0) + is_sect = 1; /* neither sections nor locals (except when debugging) need names */ entry = elf_symtab_insert_local_sym(info->objfmt_elf->elf_symtab, diff --git a/modules/objfmts/elf/elf.c b/modules/objfmts/elf/elf.c index b2c59088..c45f173d 100644 --- a/modules/objfmts/elf/elf.c +++ b/modules/objfmts/elf/elf.c @@ -489,14 +489,18 @@ elf_symtab_write_to_file(FILE *f, elf_symtab_head *symtab) YASM_WRITE_8(bufp, ELF32_ST_INFO(entry->bind, entry->type)); YASM_WRITE_8(bufp, 0); if (entry->sect) { - elf_secthead *shead = - yasm_section_get_data(entry->sect, &elf_section_data); - if (!shead) - yasm_internal_error( - N_("symbol references section without data")); - YASM_WRITE_16_L(bufp, shead->index); + if (yasm_section_is_absolute(entry->sect)) { + YASM_WRITE_16_L(bufp, SHN_ABS); + } else { + elf_secthead *shead = yasm_section_get_data(entry->sect, + &elf_section_data); + if (!shead) + yasm_internal_error( + N_("symbol references section without data")); + YASM_WRITE_16_L(bufp, shead->index); + } } else { - YASM_WRITE_16_L(bufp, entry->index); + YASM_WRITE_16_L(bufp, entry->index); } fwrite(buf, SYMTAB32_SIZE, 1, f); size += SYMTAB32_SIZE; @@ -507,12 +511,16 @@ elf_symtab_write_to_file(FILE *f, elf_symtab_head *symtab) YASM_WRITE_8(bufp, ELF64_ST_INFO(entry->bind, entry->type)); YASM_WRITE_8(bufp, 0); if (entry->sect) { - elf_secthead *shead = - yasm_section_get_data(entry->sect, &elf_section_data); - if (!shead) - yasm_internal_error( - N_("symbol references section without data")); - YASM_WRITE_16_L(bufp, shead->index); + if (yasm_section_is_absolute(entry->sect)) { + YASM_WRITE_16_L(bufp, SHN_ABS); + } else { + elf_secthead *shead = yasm_section_get_data(entry->sect, + &elf_section_data); + if (!shead) + yasm_internal_error( + N_("symbol references section without data")); + YASM_WRITE_16_L(bufp, shead->index); + } } else { YASM_WRITE_16_L(bufp, entry->index); } diff --git a/modules/objfmts/elf/tests/Makefile.inc b/modules/objfmts/elf/tests/Makefile.inc index 3dd0ec95..1087e295 100644 --- a/modules/objfmts/elf/tests/Makefile.inc +++ b/modules/objfmts/elf/tests/Makefile.inc @@ -27,6 +27,9 @@ EXTRA_DIST += modules/objfmts/elf/tests/elfglobext.errwarn EXTRA_DIST += modules/objfmts/elf/tests/elfglobext.hex EXTRA_DIST += modules/objfmts/elf/tests/elfreloc-err.asm EXTRA_DIST += modules/objfmts/elf/tests/elfreloc-err.errwarn +EXTRA_DIST += modules/objfmts/elf/tests/elfabssect.asm +EXTRA_DIST += modules/objfmts/elf/tests/elfabssect.errwarn +EXTRA_DIST += modules/objfmts/elf/tests/elfabssect.hex EXTRA_DIST += modules/objfmts/elf/tests/amd64/Makefile.inc diff --git a/modules/objfmts/elf/tests/elfabssect.asm b/modules/objfmts/elf/tests/elfabssect.asm new file mode 100644 index 00000000..cf0cb3ba --- /dev/null +++ b/modules/objfmts/elf/tests/elfabssect.asm @@ -0,0 +1,9 @@ +%line 1+1 elfabssect.asm +[absolute 0] +%line 1+0 elfabssect.asm +teststruc: +%line 2+1 elfabssect.asm + .testlabel resw 1 +teststruc_size: +%line 3+0 elfabssect.asm +[section .text] diff --git a/modules/objfmts/elf/tests/elfabssect.errwarn b/modules/objfmts/elf/tests/elfabssect.errwarn new file mode 100644 index 00000000..e69de29b diff --git a/modules/objfmts/elf/tests/elfabssect.hex b/modules/objfmts/elf/tests/elfabssect.hex new file mode 100644 index 00000000..bff75aa8 --- /dev/null +++ b/modules/objfmts/elf/tests/elfabssect.hex @@ -0,0 +1,392 @@ +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 +05 +00 +01 +00 +00 +2e +74 +65 +78 +74 +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 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +f1 +ff +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +f1 +ff +00 +00 +00 +00 +02 +00 +00 +00 +00 +00 +00 +00 +00 +00 +f1 +ff +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 +17 +00 +00 +00 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +34 +00 +00 +00 +21 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +07 +00 +00 +00 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +58 +00 +00 +00 +03 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +0f +00 +00 +00 +02 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +5c +00 +00 +00 +60 +00 +00 +00 +02 +00 +00 +00 +06 +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 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +00 +10 +00 +00 +00 +00 +00 +00 +00 -- 2.40.0