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 3203333823c75a1c1887e923828113a1b0fd12020cElliott Hughesint 3303333823c75a1c1887e923828113a1b0fd12020cElliott Hughesmain (int argc, char *argv[] __attribute__ ((unused))) 3403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{ 3503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes const char *fname = "xxx_update1"; 3603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes int fd; 3703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Elf *elf; 3803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Elf32_Ehdr *ehdr; 3903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes int i; 4003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 4103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666); 4203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (fd == -1) 4303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 4403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("cannot open `%s': %s\n", fname, strerror (errno)); 4503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 4603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 4703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 4803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes elf_version (EV_CURRENT); 4903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 5003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes elf = elf_begin (fd, ELF_C_WRITE, NULL); 5103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (elf == NULL) 5203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 5303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1)); 5403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 5503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 5603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 5703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Create an ELF header. */ 5803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr = elf32_newehdr (elf); 5903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (ehdr == NULL) 6003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 6103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("cannot create ELF header: %s\n", elf_errmsg (-1)); 6203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 6303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 6403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 6503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Print the ELF header values. */ 6603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (argc > 1) 6703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 6803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes for (i = 0; i < EI_NIDENT; ++i) 6903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf (" %02x", ehdr->e_ident[i]); 7003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("\ 7103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n" 7203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n" 7303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n", 7403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry, 7503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize, 7603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize, 7703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_shnum, ehdr->e_shstrndx); 7803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 7903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 8003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_ident[0] = 42; 8103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_ident[4] = 1; 8203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_ident[5] = 1; 8303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_ident[6] = 2; 8403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_ident[9] = 2; 8503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_version = 1; 8603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_ehsize = 1; 8703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 8803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Write out the file. */ 8903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (elf_update (elf, ELF_C_WRITE) < 0) 9003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 9103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("failure in elf_update: %s\n", elf_errmsg (-1)); 9203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 9303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 9403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 9503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Create an ELF header. */ 9603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr = elf32_newehdr (elf); 9703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (ehdr == NULL) 9803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 9903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("cannot create ELF header: %s\n", elf_errmsg (-1)); 10003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 10103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 10203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 10303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Print the ELF header values. */ 10403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (argc > 1) 10503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 10603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes for (i = 0; i < EI_NIDENT; ++i) 10703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf (" %02x", ehdr->e_ident[i]); 10803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("\ 10903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n" 11003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n" 11103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n", 11203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry, 11303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize, 11403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize, 11503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ehdr->e_shnum, ehdr->e_shstrndx); 11603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 11703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 11803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (elf_end (elf) != 0) 11903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 12003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes printf ("failure in elf_end: %s\n", elf_errmsg (-1)); 12103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes exit (1); 12203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 12303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 12403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes unlink (fname); 12503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 12603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return 0; 12703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes} 128