The Android Open Source Project | 441f72d | 2009-03-03 19:29:28 -0800 | [diff] [blame^] | 1 | /* Copyright (C) 1999, 2000, 2001, 2002 Red Hat, Inc. |
| 2 | Written by Ulrich Drepper <drepper@redhat.com>, 1999. |
| 3 | |
| 4 | This program is Open Source software; you can redistribute it and/or |
| 5 | modify it under the terms of the Open Software License version 1.0 as |
| 6 | published by the Open Source Initiative. |
| 7 | |
| 8 | You should have received a copy of the Open Software License along |
| 9 | with this program; if not, you may obtain a copy of the Open Software |
| 10 | License version 1.0 from http://www.opensource.org/licenses/osl.php or |
| 11 | by writing the Open Source Initiative c/o Lawrence Rosen, Esq., |
| 12 | 3001 King Ranch Road, Ukiah, CA 95482. */ |
| 13 | |
| 14 | #include <config.h> |
| 15 | |
| 16 | #include <libelf.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | static void |
| 22 | print_ehdr (Elf32_Ehdr *ehdr) |
| 23 | { |
| 24 | int n; |
| 25 | |
| 26 | for (n = 0; n < EI_NIDENT; ++n) |
| 27 | printf (" %02x", ehdr->e_ident[n]); |
| 28 | |
| 29 | printf ("\ntype = %d\nmachine = %d\nversion = %d\nentry = %d\n" |
| 30 | "phoff = %d\nshoff = %d\nflags = %d\nehsize = %d\n" |
| 31 | "phentsize = %d\nphnum = %d\nshentsize = %d\nshnum = %d\n" |
| 32 | "shstrndx = %d\n", |
| 33 | ehdr->e_type, |
| 34 | ehdr->e_machine, |
| 35 | ehdr->e_version, |
| 36 | ehdr->e_entry, |
| 37 | ehdr->e_phoff, |
| 38 | ehdr->e_shoff, |
| 39 | ehdr->e_flags, |
| 40 | ehdr->e_ehsize, |
| 41 | ehdr->e_phentsize, |
| 42 | ehdr->e_phnum, |
| 43 | ehdr->e_shentsize, |
| 44 | ehdr->e_shnum, |
| 45 | ehdr->e_shstrndx); |
| 46 | } |
| 47 | |
| 48 | int |
| 49 | main (int argc, char *argv[]) |
| 50 | { |
| 51 | Elf *elf; |
| 52 | int result = 0; |
| 53 | int fd; |
| 54 | char fname[] = "newfile-XXXXXX"; |
| 55 | |
| 56 | fd = mkstemp (fname); |
| 57 | if (fd == -1) |
| 58 | { |
| 59 | printf ("cannot create temporary file: %m\n"); |
| 60 | exit (1); |
| 61 | } |
| 62 | /* Remove the file when we exit. */ |
| 63 | unlink (fname); |
| 64 | |
| 65 | elf_version (EV_CURRENT); |
| 66 | elf = elf_begin (fd, ELF_C_WRITE, NULL); |
| 67 | if (elf == NULL) |
| 68 | { |
| 69 | printf ("elf_begin: %s\n", elf_errmsg (-1)); |
| 70 | result = 1; |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | if (elf32_newehdr (elf) == NULL) |
| 75 | { |
| 76 | printf ("elf32_newehdr: %s\n", elf_errmsg (-1)); |
| 77 | result = 1; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | Elf32_Ehdr *ehdr = elf32_getehdr (elf); |
| 82 | |
| 83 | if (ehdr == NULL) |
| 84 | { |
| 85 | printf ("elf32_getehdr: %s\n", elf_errmsg (-1)); |
| 86 | result = 1; |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | int i; |
| 91 | |
| 92 | if (argc > 1) |
| 93 | /* Use argc as a debugging flag. */ |
| 94 | print_ehdr (ehdr); |
| 95 | |
| 96 | /* Some tests. */ |
| 97 | for (i = 0; i < EI_NIDENT; ++i) |
| 98 | if (ehdr->e_ident[i] != 0) |
| 99 | { |
| 100 | printf ("ehdr->e_ident[%d] != 0\n", i); |
| 101 | result = 1; |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | #define VALUE_TEST(name, val) \ |
| 106 | if (ehdr->name != val) \ |
| 107 | { \ |
| 108 | printf ("ehdr->%s != %d\n", #name, val); \ |
| 109 | result = 1; \ |
| 110 | } |
| 111 | #define ZERO_TEST(name) VALUE_TEST (name, 0) |
| 112 | ZERO_TEST (e_type); |
| 113 | ZERO_TEST (e_machine); |
| 114 | ZERO_TEST (e_version); |
| 115 | ZERO_TEST (e_entry); |
| 116 | ZERO_TEST (e_phoff); |
| 117 | ZERO_TEST (e_shoff); |
| 118 | ZERO_TEST (e_flags); |
| 119 | ZERO_TEST (e_ehsize); |
| 120 | ZERO_TEST (e_phentsize); |
| 121 | ZERO_TEST (e_phnum); |
| 122 | ZERO_TEST (e_shentsize); |
| 123 | ZERO_TEST (e_shnum); |
| 124 | ZERO_TEST (e_shstrndx); |
| 125 | |
| 126 | if (elf32_newphdr (elf, 10) == NULL) |
| 127 | { |
| 128 | printf ("elf32_newphdr: %s\n", elf_errmsg (-1)); |
| 129 | result = 1; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | if (argc > 1) |
| 134 | print_ehdr (ehdr); |
| 135 | |
| 136 | ehdr = elf32_getehdr (elf); |
| 137 | if (ehdr == NULL) |
| 138 | { |
| 139 | printf ("elf32_getehdr (#2): %s\n", elf_errmsg (-1)); |
| 140 | result = 1; |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | ZERO_TEST (e_type); |
| 145 | ZERO_TEST (e_machine); |
| 146 | ZERO_TEST (e_version); |
| 147 | ZERO_TEST (e_entry); |
| 148 | ZERO_TEST (e_phoff); |
| 149 | ZERO_TEST (e_shoff); |
| 150 | ZERO_TEST (e_flags); |
| 151 | ZERO_TEST (e_ehsize); |
| 152 | VALUE_TEST (e_phentsize, (int) sizeof (Elf32_Phdr)); |
| 153 | VALUE_TEST (e_phnum, 10); |
| 154 | ZERO_TEST (e_shentsize); |
| 155 | ZERO_TEST (e_shnum); |
| 156 | ZERO_TEST (e_shstrndx); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | (void) elf_end (elf); |
| 163 | } |
| 164 | |
| 165 | return result; |
| 166 | } |