From 494275c0f41dea71d8ec5b97d01581c95fdfbbdb Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Mon, 28 Mar 2011 16:00:38 +0200 Subject: [PATCH] New test for ebl_dynamic_tag_name. --- tests/ChangeLog | 9 +- tests/Makefile.am | 8 +- tests/alldts.c | 275 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/run-alldts.sh | 106 ++++++++++++++++++++ 4 files changed, 394 insertions(+), 4 deletions(-) create mode 100644 tests/alldts.c create mode 100755 tests/run-alldts.sh diff --git a/tests/ChangeLog b/tests/ChangeLog index 4ec3853..674fe96 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,4 +1,11 @@ -2011-04-02 Marek Polacek +2011-03-28 Marek Polacek + + * alldts.c: New file. + * run-alldts.sh: Use it. + * Makefile.am (TESTS, EXTRA_DIST, noinst_PROGRAMS): Add them. + (alldts_LDADD): New variable. + +2011-03-02 Marek Polacek * dwarf-getstring.c: New test. * run-dwarf-getstring.sh: And its wrapper. diff --git a/tests/Makefile.am b/tests/Makefile.am index 70029f7..35f55e3 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -57,7 +57,8 @@ noinst_PROGRAMS = arextract arsymtest newfile saridx scnnames sectiondump \ dwfl-bug-addr-overflow arls dwfl-bug-fd-leak \ dwfl-addr-sect dwfl-bug-report early-offscn \ dwfl-bug-getmodules dwarf-getmacros addrcfi \ - test-flag-nobits dwarf-getstring rerequest_tag + test-flag-nobits dwarf-getstring rerequest_tag \ + alldts asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \ asm-tst6 asm-tst7 asm-tst8 asm-tst9 @@ -70,7 +71,7 @@ TESTS = run-arextract.sh run-arsymtest.sh newfile test-nlist \ run-strip-test3.sh run-strip-test4.sh run-strip-test5.sh \ run-strip-test6.sh run-strip-test7.sh run-strip-test8.sh \ run-unstrip-test.sh run-unstrip-test2.sh \ - run-ecp-test.sh run-ecp-test2.sh \ + run-ecp-test.sh run-ecp-test2.sh run-alldts.sh \ run-elflint-test.sh run-elflint-self.sh run-ranlib-test.sh \ run-ranlib-test2.sh run-ranlib-test3.sh run-ranlib-test4.sh \ run-addrscopes.sh run-strings-test.sh run-funcscopes.sh \ @@ -118,7 +119,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh \ run-addrname-test.sh run-dwfl-bug-offline-rel.sh \ run-dwfl-addr-sect.sh run-early-offscn.sh \ run-dwarf-getmacros.sh run-test-flag-nobits.sh \ - run-dwarf-getstring.sh \ + run-dwarf-getstring.sh run-alldts.sh \ testfile15.bz2 testfile15.debug.bz2 \ testfile16.bz2 testfile16.debug.bz2 \ testfile17.bz2 testfile17.debug.bz2 \ @@ -253,6 +254,7 @@ dwarf_getstring_LDADD = $(libdw) $(libmudflap) addrcfi_LDADD = $(libdw) $(libebl) $(libelf) $(libmudflap) -ldl test_flag_nobits_LDADD = $(libelf) $(libmudflap) rerequest_tag_LDADD = $(libdw) $(libmudflap) +alldts_LDADD = $(libebl) $(libelf) $(libmudflap) if GCOV check: check-am coverage diff --git a/tests/alldts.c b/tests/alldts.c new file mode 100644 index 0000000..ed7624a --- /dev/null +++ b/tests/alldts.c @@ -0,0 +1,275 @@ +/* Create an ELF file with all the DT_* flags set. + Copyright (C) 2011 Red Hat, Inc. + This file is part of Red Hat elfutils. + Written by Marek Polacek , 2011. + + Red Hat elfutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by the + Free Software Foundation; version 2 of the License. + + Red Hat elfutils is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Red Hat elfutils; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. + + Red Hat elfutils is an included package of the Open Invention Network. + An included package of the Open Invention Network is a package for which + Open Invention Network licensees cross-license their patents. No patent + license is granted, either expressly or impliedly, by designation as an + included package. Should you wish to participate in the Open Invention + Network licensing program, please visit www.openinventionnetwork.com + . */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include ELFUTILS_HEADER(ebl) +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +int +main (void) +{ + static const char fname[] = "testfile-alldts"; + struct Ebl_Strtab *shst; + struct Ebl_Strent *dynscn; + struct Ebl_Strent *shstrtabse; + const Elf32_Sword dtflags[] = + { + DT_NULL, DT_NEEDED, DT_PLTRELSZ, DT_PLTGOT, + DT_HASH, DT_STRTAB, DT_SYMTAB, DT_RELA, + DT_RELASZ, DT_RELAENT, DT_STRSZ, DT_SYMENT, + DT_INIT, DT_FINI, DT_SONAME, DT_RPATH, + DT_SYMBOLIC, DT_REL, DT_RELSZ, DT_RELENT, + DT_PLTREL, DT_DEBUG, DT_TEXTREL, DT_JMPREL, + DT_BIND_NOW, DT_INIT_ARRAY, DT_FINI_ARRAY, + DT_INIT_ARRAYSZ, DT_FINI_ARRAYSZ, DT_RUNPATH, + DT_FLAGS, DT_ENCODING, DT_PREINIT_ARRAY, + DT_PREINIT_ARRAYSZ, DT_VERSYM, DT_GNU_PRELINKED, + DT_GNU_CONFLICTSZ, DT_GNU_LIBLISTSZ, DT_CHECKSUM, + DT_PLTPADSZ, DT_MOVEENT, DT_MOVESZ, DT_FEATURE_1, + DT_POSFLAG_1, DT_SYMINSZ, DT_SYMINENT, DT_GNU_HASH, + DT_TLSDESC_PLT, DT_TLSDESC_GOT, DT_GNU_CONFLICT, + DT_GNU_LIBLIST, DT_CONFIG, DT_DEPAUDIT, DT_AUDIT, + DT_PLTPAD, DT_MOVETAB, DT_SYMINFO, DT_RELACOUNT, + DT_RELCOUNT, DT_FLAGS_1, DT_VERDEF, DT_VERDEFNUM, + DT_VERNEED, DT_VERNEEDNUM, DT_AUXILIARY, DT_FILTER + }; + const int ndtflags = sizeof (dtflags) / sizeof (dtflags[0]); + + /* We use no threads here which can interfere with handling a stream. */ + (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER); + + /* Open the file. */ + int fd = open64 (fname, O_RDWR | O_CREAT | O_TRUNC, 0666); + if (fd == -1) + { + printf ("cannot open `%s': %m\n", fname); + return 1; + } + + /* Tell the library which version are we expecting. */ + elf_version (EV_CURRENT); + + /* Create an ELF descriptor. */ + Elf *elf = elf_begin (fd, ELF_C_WRITE, NULL); + if (elf == NULL) + { + printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1)); + return 1; + } + + /* Create an ELF header. */ + Elf32_Ehdr *ehdr = elf32_newehdr (elf); + if (ehdr == NULL) + { + printf ("cannot create ELF header: %s\n", elf_errmsg (-1)); + return 1; + } + + ehdr->e_ident[0] = 42; + ehdr->e_ident[5] = 1; + ehdr->e_ident[6] = 2; + ehdr->e_type = ET_EXEC; + ehdr->e_machine = EM_386; + ehdr->e_version = 1; + ehdr->e_ehsize = 1; + ehdr->e_shnum = 3; + + elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY); + + /* Create the program headers. */ + Elf32_Phdr *phdr = elf32_newphdr (elf, 2); + if (phdr == NULL) + { + printf ("cannot create program headers: %s\n", elf_errmsg (-1)); + return 1; + } + + phdr[0].p_type = PT_PHDR; + phdr[1].p_type = PT_DYNAMIC; + + elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY); + shst = ebl_strtabinit (true); + + /* Create the .dynamic section. */ + Elf_Scn *scn = elf_newscn (elf); + if (scn == NULL) + { + printf ("cannot create DYNAMIC section: %s\n", elf_errmsg (-1)); + return 1; + } + + Elf32_Shdr *shdr = elf32_getshdr (scn); + if (shdr == NULL) + { + printf ("cannot get header for DYNAMIC section: %s\n", elf_errmsg (-1)); + return 1; + } + + dynscn = ebl_strtabadd (shst, ".dynamic", 0); + + /* We'll need to know the section offset. But this will be set up + by elf_update later, so for now just store the address. */ + const Elf32_Off *const dynscn_offset = &shdr->sh_offset; + shdr->sh_type = SHT_DYNAMIC; + shdr->sh_flags = SHF_ALLOC | SHF_WRITE; + shdr->sh_link = SHN_UNDEF; + shdr->sh_info = SHN_UNDEF; + /* This section will start here. */ + shdr->sh_addr = 0x1a0; + + /* Create new section data. */ + Elf_Data *data = elf_newdata (scn); + if (data == NULL) + { + printf ("cannot create data for DYNAMIC section: %s\n", elf_errmsg (-1)); + return 1; + } + + /* Allocate memory for all the .dynamic entries. */ + Elf32_Dyn *dyn = malloc (ndtflags * sizeof (Elf32_Dyn)); + if (dyn == NULL) + { + printf ("malloc failed: %m\n"); + return 1; + } + + /* Now write all the DT_* flags. */ + for (int i = 0; i < ndtflags; ++i) + { + dyn[i].d_tag = dtflags[i]; + dyn[i].d_un.d_val = 0xdeadbeef; + } + + /* Set the pointer to allocated memory. */ + data->d_buf = dyn; + data->d_type = ELF_T_DYN; + data->d_version = EV_CURRENT; + data->d_size = ndtflags * sizeof (Elf32_Dyn); + data->d_align = 0x8; + + /* Create .shstrtab section. */ + scn = elf_newscn (elf); + if (scn == NULL) + { + printf ("cannot create SHSTRTAB section: %s\n", elf_errmsg (-1)); + return 1; + } + + shdr = elf32_getshdr (scn); + if (shdr == NULL) + { + printf ("cannot get header for SHSTRTAB section: %s\n", elf_errmsg (-1)); + return 1; + } + + shstrtabse = ebl_strtabadd (shst, ".shstrtab", 0); + + shdr->sh_type = SHT_STRTAB; + shdr->sh_flags = 0; + shdr->sh_addr = 0; + shdr->sh_link = SHN_UNDEF; + shdr->sh_info = SHN_UNDEF; + shdr->sh_entsize = 1; + + /* We have to store the section index in the ELF header. */ + ehdr->e_shstrndx = elf_ndxscn (scn); + + data = elf_newdata (scn); + if (data == NULL) + { + printf ("cannot create data SHSTRTAB section: %s\n", elf_errmsg (-1)); + return 1; + } + + /* No more sections, finalize the section header string table. */ + ebl_strtabfinalize (shst, data); + + elf32_getshdr (elf_getscn (elf, 1))->sh_name = ebl_strtaboffset (dynscn); + shdr->sh_name = ebl_strtaboffset (shstrtabse); + + /* Let the library compute the internal structure information. */ + if (elf_update (elf, ELF_C_NULL) < 0) + { + printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1)); + return 1; + } + + ehdr = elf32_getehdr (elf); + + phdr[0].p_offset = ehdr->e_phoff; + phdr[0].p_vaddr = ehdr->e_phoff; + phdr[0].p_paddr = ehdr->e_phoff; + phdr[0].p_flags = PF_R | PF_X; + phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT); + phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT); + phdr[0].p_align = sizeof (Elf32_Word); + + phdr[1].p_flags = PF_W | PF_R; + phdr[1].p_offset = *dynscn_offset; + /* Set up the start of this segment to equal start address of the + .dynamic section. */ + phdr[1].p_vaddr = 0x1a0; + phdr[1].p_paddr = 0x1a0; + phdr[1].p_align = 2 * sizeof (Elf32_Word); + phdr[1].p_filesz = ndtflags * sizeof (Elf32_Dyn); + phdr[1].p_memsz = ndtflags * sizeof (Elf32_Dyn); + + /* Write out the file. */ + if (elf_update (elf, ELF_C_WRITE) < 0) + { + printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1)); + return 1; + } + + /* We don't need the string table anymore. */ + ebl_strtabfree (shst); + + /* And the data allocated in the .shstrtab section. */ + free (data->d_buf); + + /* All done. */ + if (elf_end (elf) != 0) + { + printf ("failure in elf_end: %s\n", elf_errmsg (-1)); + return 1; + } + + return 0; +} diff --git a/tests/run-alldts.sh b/tests/run-alldts.sh new file mode 100755 index 0000000..327f2f1 --- /dev/null +++ b/tests/run-alldts.sh @@ -0,0 +1,106 @@ +#! /bin/sh +# Copyright (C) 2011 Red Hat, Inc. +# This file is part of Red Hat elfutils. +# +# Red Hat elfutils is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by the +# Free Software Foundation; version 2 of the License. +# +# Red Hat elfutils is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Red Hat elfutils; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. +# +# Red Hat elfutils is an included package of the Open Invention Network. +# An included package of the Open Invention Network is a package for which +# Open Invention Network licensees cross-license their patents. No patent +# license is granted, either expressly or impliedly, by designation as an +# included package. Should you wish to participate in the Open Invention +# Network licensing program, please visit www.openinventionnetwork.com +# . + +. $srcdir/test-subr.sh + +# This will produce "testfile-alldts" file +./alldts + +testrun_compare ../src/readelf -d testfile-alldts <<\EOF + +Dynamic segment contains 66 entries: + Addr: 0x000001a0 Offset: 0x000078 Link to section: [ 0] '' + Type Value + NULL + NEEDED Shared library: [(null)] + PLTRELSZ 3735928559 (bytes) + PLTGOT 0xdeadbeef + HASH 0xdeadbeef + STRTAB 0xdeadbeef + SYMTAB 0xdeadbeef + RELA 0xdeadbeef + RELASZ 3735928559 (bytes) + RELAENT 3735928559 (bytes) + STRSZ 3735928559 (bytes) + SYMENT 3735928559 (bytes) + INIT 0xdeadbeef + FINI 0xdeadbeef + SONAME Library soname: [(null)] + RPATH Library rpath: [(null)] + SYMBOLIC 0xdeadbeef + REL 0xdeadbeef + RELSZ 3735928559 (bytes) + RELENT 3735928559 (bytes) + PLTREL ??? + DEBUG + TEXTREL + JMPREL 0xdeadbeef + BIND_NOW + INIT_ARRAY 0xdeadbeef + FINI_ARRAY 0xdeadbeef + INIT_ARRAYSZ 3735928559 (bytes) + FINI_ARRAYSZ 3735928559 (bytes) + RUNPATH Library runpath: [(null)] + FLAGS ORIGIN SYMBOLIC TEXTREL BIND_NOW 0xdeadbee0 + PREINIT_ARRAY 0xdeadbeef + PREINIT_ARRAY 0xdeadbeef + PREINIT_ARRAYSZ 0xdeadbeef + VERSYM 0xdeadbeef + GNU_PRELINKED 0xdeadbeef + GNU_CONFLICTSZ 3735928559 (bytes) + GNU_LIBLISTSZ 3735928559 (bytes) + CHECKSUM 0xdeadbeef + PLTPADSZ 3735928559 (bytes) + MOVEENT 3735928559 (bytes) + MOVESZ 3735928559 (bytes) + FEATURE_1 PARINIT CONFEXP 0xdeadbeec + POSFLAG_1 LAZYLOAD GROUPPERM 0xdeadbeec + SYMINSZ 3735928559 (bytes) + SYMINENT 3735928559 (bytes) + GNU_HASH 0xdeadbeef + TLSDESC_PLT 0xdeadbeef + TLSDESC_GOT 0xdeadbeef + GNU_CONFLICT 0xdeadbeef + GNU_LIBLIST 0xdeadbeef + CONFIG 0xdeadbeef + DEPAUDIT 0xdeadbeef + AUDIT 0xdeadbeef + PLTPAD 0xdeadbeef + MOVETAB 0xdeadbeef + SYMINFO 0xdeadbeef + RELACOUNT 3735928559 + RELCOUNT 3735928559 + FLAGS_1 NOW GLOBAL GROUP NODELETE INITFIRST NOOPEN ORIGIN TRANS INTERPOSE NODEFLIB NODUMP CONFALT DISPRELDNE DISPRELPND 0xdeac0000 + VERDEF 0xdeadbeef + VERDEFNUM 3735928559 + VERNEED 0xdeadbeef + VERNEEDNUM 3735928559 + AUXILIARY 0xdeadbeef + FILTER 0xdeadbeef +EOF + +rm -f testfile-alldts + +exit 0 -- 2.7.4