103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes/* Test program for elf_update function. 203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Copyright (C) 2000, 2002, 2005 Red Hat, Inc. 303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes This file is part of elfutils. 403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Written by Ulrich Drepper <drepper@redhat.com>, 2000. 503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes This file is free software; you can redistribute it and/or modify 703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes it under the terms of the GNU General Public License as published by 803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes the Free Software Foundation; either version 3 of the License, or 903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes (at your option) any later version. 1003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 1103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes elfutils is distributed in the hope that it will be useful, but 1203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes WITHOUT ANY WARRANTY; without even the implied warranty of 1303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes GNU General Public License for more details. 1503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 1603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes You should have received a copy of the GNU General Public License 1703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes along with this program. If not, see <http://www.gnu.org/licenses/>. */ 1803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 1903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#ifdef HAVE_CONFIG_H 2003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# include <config.h> 2103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#endif 2203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 2303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <errno.h> 2403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <fcntl.h> 2503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <libelf.h> 2603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <stdio.h> 2703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <stdlib.h> 2803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <string.h> 2903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <unistd.h> 3003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 3103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include ELFUTILS_HEADER(ebl) 3203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 3303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 3403333823c75a1c1887e923828113a1b0fd12020cElliott Hughesint 3503333823c75a1c1887e923828113a1b0fd12020cElliott Hughesmain (int argc, char *argv[] __attribute__ ((unused))) 3603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{ 3703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes const char *fname = "xxx_update3"; 3803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes int fd; 3903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Elf *elf; 4003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Elf32_Ehdr *ehdr; 4103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Elf32_Phdr *phdr; 4203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Elf_Scn *scn; 4303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Elf32_Shdr *shdr; 4403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Elf_Data *data; 4503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes struct Ebl_Strtab *shst; 4603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes struct Ebl_Strent *shstrtabse; 4703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes int i; 4803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 4903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666); 5003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (fd == -1) 5103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 5203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("cannot open `%s': %s\n", fname, strerror (errno)); 5303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 5403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 5503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 5603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes elf_version (EV_CURRENT); 5703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 5803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes elf_fill (0x42); 5903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 6003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes elf = elf_begin (fd, ELF_C_WRITE, NULL); 6103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (elf == NULL) 6203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 6303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1)); 6403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 6503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 6603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 6703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Create an ELF header. */ 6803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr = elf32_newehdr (elf); 6903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (ehdr == NULL) 7003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 7103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("cannot create ELF header: %s\n", elf_errmsg (-1)); 7203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 7303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 7403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 7503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Print the ELF header values. */ 7603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (argc > 1) 7703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 7803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes for (i = 0; i < EI_NIDENT; ++i) 7903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf (" %02x", ehdr->e_ident[i]); 8003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("\ 8103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n" 8203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n" 8303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n", 8403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry, 8503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize, 8603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize, 8703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_shnum, ehdr->e_shstrndx); 8803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 8903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 9003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_ident[0] = 42; 9103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_ident[4] = 1; 9203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_ident[5] = 1; 9303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_ident[6] = 2; 9403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_type = ET_EXEC; 9503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_version = 1; 9603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_ehsize = 1; 9703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY); 9803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 9903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Create the program header. */ 10003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes phdr = elf32_newphdr (elf, 1); 10103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (phdr == NULL) 10203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 10303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("cannot create program header: %s\n", elf_errmsg (-1)); 10403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 10503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 10603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 10703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes phdr[0].p_type = PT_PHDR; 10803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY); 10903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 11003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes shst = ebl_strtabinit (true); 11103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 11203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes scn = elf_newscn (elf); 11303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (scn == NULL) 11403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 11503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("cannot create SHSTRTAB section: %s\n", elf_errmsg (-1)); 11603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 11703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 11803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes shdr = elf32_getshdr (scn); 11903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (shdr == NULL) 12003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 12103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("cannot get header for SHSTRTAB section: %s\n", elf_errmsg (-1)); 12203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 12303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 12403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 12503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes shstrtabse = ebl_strtabadd (shst, ".shstrtab", 0); 12603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 12703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes shdr->sh_type = SHT_STRTAB; 12803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes shdr->sh_flags = 0; 12903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes shdr->sh_addr = 0; 13003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes shdr->sh_link = SHN_UNDEF; 13103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes shdr->sh_info = SHN_UNDEF; 13203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes shdr->sh_addralign = 1; 13303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes shdr->sh_entsize = 0; 13403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 13503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* We have to store the section index in the ELF header. */ 13603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_shstrndx = elf_ndxscn (scn); 13703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 13803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes data = elf_newdata (scn); 13903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (data == NULL) 14003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 14103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("cannot create data SHSTRTAB section: %s\n", elf_errmsg (-1)); 14203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 14303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 14403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 14503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* No more sections, finalize the section header string table. */ 14603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ebl_strtabfinalize (shst, data); 14703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 14803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes shdr->sh_name = ebl_strtaboffset (shstrtabse); 14903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 15003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Let the library compute the internal structure information. */ 15103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (elf_update (elf, ELF_C_NULL) < 0) 15203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 15303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1)); 15403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 15503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 15603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 15703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr = elf32_getehdr (elf); 15803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 15903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes phdr[0].p_offset = ehdr->e_phoff; 16003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes phdr[0].p_offset = ehdr->e_phoff; 16103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes phdr[0].p_vaddr = ehdr->e_phoff; 16203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes phdr[0].p_paddr = ehdr->e_phoff; 16303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes phdr[0].p_flags = PF_R | PF_X; 16403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT); 16503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT); 16603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes phdr[0].p_align = sizeof (Elf32_Word); 16703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 16803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Write out the file. */ 16903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (elf_update (elf, ELF_C_WRITE) < 0) 17003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 17103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1)); 17203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 17303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 17403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 17503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* We don't need the string table anymore. */ 17603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ebl_strtabfree (shst); 17703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 17803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* And the data allocated in the .shstrtab section. */ 17903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes free (data->d_buf); 18003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 18103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Print the ELF header values. */ 18203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (argc > 1) 18303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 18403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes for (i = 0; i < EI_NIDENT; ++i) 18503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf (" %02x", ehdr->e_ident[i]); 18603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("\ 18703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n" 18803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n" 18903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n", 19003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry, 19103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize, 19203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize, 19303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_shnum, ehdr->e_shstrndx); 19403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 19503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 19603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (elf_end (elf) != 0) 19703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 19803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("failure in elf_end: %s\n", elf_errmsg (-1)); 19903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 20003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 20103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 20203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes unlink (fname); 20303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 20403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return 0; 20503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes} 206