Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc. |
| 2 | Written by Ulrich Drepper <drepper@redhat.com>, 1998. |
| 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 <fcntl.h> |
| 17 | #include <libelf.h> |
| 18 | #include <libdwarf.h> |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | int |
| 24 | main (int argc, char *argv[]) |
| 25 | { |
| 26 | int cnt; |
| 27 | |
| 28 | for (cnt = 1; cnt < argc; ++cnt) |
| 29 | { |
| 30 | int fd = open (argv[cnt], O_RDONLY); |
| 31 | Dwarf_Debug dbg; |
| 32 | Dwarf_Signed cie_cnt; |
| 33 | Dwarf_Cie *cie_data; |
| 34 | Dwarf_Signed fde_cnt; |
| 35 | Dwarf_Fde *fde_data; |
| 36 | Dwarf_Error err; |
| 37 | |
| 38 | if (dwarf_init (fd, DW_DLC_READ, NULL, NULL, &dbg, &err) != DW_DLV_OK) |
| 39 | { |
| 40 | printf ("%s not usable: %s\n", argv[cnt], dwarf_errmsg (err)); |
| 41 | continue; |
| 42 | } |
| 43 | else if (dwarf_get_fde_list_eh (dbg, &cie_data, &cie_cnt, &fde_data, |
| 44 | &fde_cnt, &err) != DW_DLV_OK) |
| 45 | printf ("cannot get CIEs and FDEs from %s: %s\n", argv[cnt], |
| 46 | dwarf_errmsg (err)); |
| 47 | else |
| 48 | { |
| 49 | Dwarf_Addr low_pc; |
| 50 | Dwarf_Addr high_pc; |
| 51 | Dwarf_Unsigned func_length; |
| 52 | Dwarf_Ptr fde_bytes; |
| 53 | Dwarf_Unsigned fde_byte_length; |
| 54 | Dwarf_Off cie_offset; |
| 55 | Dwarf_Signed cie_index; |
| 56 | Dwarf_Off fde_offset; |
| 57 | Dwarf_Fde fde; |
| 58 | int i; |
| 59 | |
| 60 | printf ("%s has %lld CIEs and %lld FDEs\n", |
| 61 | basename (argv[cnt]), |
| 62 | (long long int) cie_cnt, (long long int) fde_cnt); |
| 63 | |
| 64 | for (i = 0; i < cie_cnt; ++i) |
| 65 | { |
| 66 | Dwarf_Unsigned bytes_in_cie; |
| 67 | Dwarf_Small version; |
| 68 | char *augmenter; |
| 69 | Dwarf_Unsigned code_alignment_factor; |
| 70 | Dwarf_Signed data_alignment_factor; |
| 71 | Dwarf_Half return_address_register; |
| 72 | Dwarf_Ptr initial_instructions; |
| 73 | Dwarf_Unsigned initial_instructions_length; |
| 74 | |
| 75 | if (dwarf_get_cie_info (cie_data[i], &bytes_in_cie, &version, |
| 76 | &augmenter, &code_alignment_factor, |
| 77 | &data_alignment_factor, |
| 78 | &return_address_register, |
| 79 | &initial_instructions, |
| 80 | &initial_instructions_length, &err) |
| 81 | != DW_DLV_OK) |
| 82 | printf ("cannot get info for CIE %d: %s\n", i, |
| 83 | dwarf_errmsg (err)); |
| 84 | else |
| 85 | { |
| 86 | size_t j; |
| 87 | |
| 88 | printf ("CIE[%d]: bytes_in_cie = %llu, version = %hhd, augmenter = \"%s\"\n", |
| 89 | i, (unsigned long long int) bytes_in_cie, version, |
| 90 | augmenter); |
| 91 | printf ("CIE[%d]: code_alignment_factor = %llx\n" |
| 92 | "CIE[%d]: data_alignment_factor = %llx\n" |
| 93 | "CIE[%d]: return_address_register = %hu\n" |
| 94 | "CIE[%d]: bytes =", |
| 95 | i, (unsigned long long int) code_alignment_factor, |
| 96 | i, (unsigned long long int) data_alignment_factor, |
| 97 | i, return_address_register, i); |
| 98 | |
| 99 | for (j = 0; j < initial_instructions_length; ++j) |
| 100 | printf (" %02hhx", |
| 101 | ((unsigned char *) initial_instructions)[j]); |
| 102 | |
| 103 | putchar ('\n'); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | for (i = 0; i < fde_cnt; ++i) |
| 108 | { |
| 109 | Dwarf_Cie cie; |
| 110 | |
| 111 | if (dwarf_get_fde_range (fde_data[i], &low_pc, &func_length, |
| 112 | &fde_bytes, &fde_byte_length, |
| 113 | &cie_offset, &cie_index, &fde_offset, |
| 114 | &err) != DW_DLV_OK) |
| 115 | printf ("cannot get range of FDE %d: %s\n", i, |
| 116 | dwarf_errmsg (err)); |
| 117 | else |
| 118 | { |
| 119 | size_t j; |
| 120 | Dwarf_Ptr instrs; |
| 121 | Dwarf_Unsigned len; |
| 122 | |
| 123 | printf ("FDE[%d]: low_pc = %#llx, length = %llu\n", i, |
| 124 | (unsigned long long int) low_pc, |
| 125 | (unsigned long long int) func_length); |
| 126 | printf ("FDE[%d]: bytes =", i); |
| 127 | |
| 128 | for (j = 0; j < fde_byte_length; ++j) |
| 129 | printf (" %02hhx", ((unsigned char *) fde_bytes)[j]); |
| 130 | |
| 131 | printf ("\nFDE[%d]: cie_offset = %lld, cie_index = %lld, fde_offset = %lld\n", |
| 132 | i, (long long int) cie_offset, |
| 133 | (long long int) cie_index, |
| 134 | (long long int) fde_offset); |
| 135 | |
| 136 | if (dwarf_get_fde_instr_bytes (fde_data[i], &instrs, &len, |
| 137 | &err) != DW_DLV_OK) |
| 138 | printf ("cannot get instructions of FDE %d: %s\n", i, |
| 139 | dwarf_errmsg (err)); |
| 140 | else |
| 141 | { |
| 142 | printf ("FDE[%d]: instructions =", i); |
| 143 | |
| 144 | for (j = 0; j < len; ++j) |
| 145 | printf (" %02hhx", ((unsigned char *) instrs)[j]); |
| 146 | |
| 147 | putchar ('\n'); |
| 148 | } |
| 149 | |
| 150 | /* Consistency check. */ |
| 151 | if (dwarf_get_cie_of_fde (fde_data[i], &cie, &err) |
| 152 | != DW_DLV_OK) |
| 153 | printf ("cannot get CIE of FDE %d: %s\n", i, |
| 154 | dwarf_errmsg (err)); |
| 155 | else if (cie_data[cie_index] != cie) |
| 156 | puts ("cie_index for FDE[%d] does not match dwarf_get_cie_of_fde result"); |
| 157 | } |
| 158 | |
| 159 | if (dwarf_get_fde_n (fde_data, i, &fde, &err) != DW_DLV_OK) |
| 160 | printf ("dwarf_get_fde_n for FDE[%d] failed\n", i); |
| 161 | else if (fde != fde_data[i]) |
| 162 | printf ("dwarf_get_fde_n for FDE[%d] didn't return the right value\n", i); |
| 163 | } |
| 164 | |
| 165 | if (dwarf_get_fde_n (fde_data, fde_cnt, &fde, &err) |
| 166 | != DW_DLV_NO_ENTRY) |
| 167 | puts ("dwarf_get_fde_n for invalid index doesn't return DW_DLV_NO_ENTRY"); |
| 168 | |
| 169 | { |
| 170 | const unsigned int addrs[] = |
| 171 | { |
| 172 | 0x8048400, 0x804842c, 0x8048454, 0x8048455, 0x80493fc |
| 173 | }; |
| 174 | const int naddrs = sizeof (addrs) / sizeof (addrs[0]); |
| 175 | |
| 176 | for (i = 0; i < naddrs; ++i) |
| 177 | if (dwarf_get_fde_at_pc (fde_data, addrs[i], &fde, &low_pc, |
| 178 | &high_pc, &err) != DW_DLV_OK) |
| 179 | printf ("no FDE at %x\n", addrs[i]); |
| 180 | else |
| 181 | { |
| 182 | Dwarf_Addr other_low_pc; |
| 183 | |
| 184 | if (dwarf_get_fde_range (fde, &other_low_pc, &func_length, |
| 185 | &fde_bytes, &fde_byte_length, |
| 186 | &cie_offset, &cie_index, |
| 187 | &fde_offset, &err) != DW_DLV_OK) |
| 188 | printf ("cannot get range of FDE returned by dwarf_get_fde_at_pc for %u: %s\n", |
| 189 | addrs[i], dwarf_errmsg (err)); |
| 190 | else |
| 191 | { |
| 192 | printf ("FDE[@%x]: cie_offset = %lld, cie_index = %lld, fde_offset = %lld\n", |
| 193 | addrs[i], |
| 194 | (long long int) cie_offset, |
| 195 | (long long int) cie_index, |
| 196 | (long long int) fde_offset); |
| 197 | |
| 198 | if (low_pc != other_low_pc) |
| 199 | printf ("low_pc returned by dwarf_get_fde_at_pc for %x and dwarf_get_fde_range differs", |
| 200 | addrs[i]); |
| 201 | |
| 202 | if (high_pc != low_pc + func_length - 1) |
| 203 | printf ("high_pc returned by dwarf_get_fde_at_pc for %x and dwarf_get_fde_range differs", |
| 204 | addrs[i]); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (dwarf_finish (dbg, &err) != DW_DLV_OK) |
| 211 | printf ("dwarf_finish failed for %s: %s\n", argv[cnt], |
| 212 | dwarf_errmsg (err)); |
| 213 | |
| 214 | close (fd); |
| 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | } |