elfdump.c (05157fa0a1fbe7173f9996277a675f3caf67e75a) elfdump.c (8fe960a125c8d0f6eb5e2fe275f5ab91549c4d61)
1/*-
1/*-
2 * Copyright (c) 2003 David O'Brien. All rights reserved.
3 * Copyright (c) 2001 Jake Burkholder
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.

--- 15 unchanged lines hidden (view full) ---

26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31#include <sys/types.h>
32#include <sys/elf32.h>
33#include <sys/elf64.h>
2 * Copyright (c) 2001 Jake Burkholder
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.

--- 15 unchanged lines hidden (view full) ---

25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <sys/types.h>
31#include <sys/elf32.h>
32#include <sys/elf64.h>
34#include <sys/endian.h>
35#include <sys/mman.h>
36#include <sys/stat.h>
37#include <err.h>
38#include <fcntl.h>
33#include <sys/mman.h>
34#include <sys/stat.h>
35#include <err.h>
36#include <fcntl.h>
39#include <inttypes.h>
40#include <stddef.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45
46#define ED_DYN (1<<0)
47#define ED_EHDR (1<<1)

--- 64 unchanged lines hidden (view full) ---

112 offsetof(Elf32_Shdr, sh_flags), offsetof(Elf32_Shdr, sh_addr),
113 offsetof(Elf32_Shdr, sh_offset), offsetof(Elf32_Shdr, sh_size),
114 offsetof(Elf32_Shdr, sh_link), offsetof(Elf32_Shdr, sh_info),
115 offsetof(Elf32_Shdr, sh_addralign), offsetof(Elf32_Shdr, sh_entsize),
116
117 offsetof(Elf32_Sym, st_name), offsetof(Elf32_Sym, st_value),
118 offsetof(Elf32_Sym, st_size), offsetof(Elf32_Sym, st_info),
119 offsetof(Elf32_Sym, st_shndx),
37#include <stddef.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
43#define ED_DYN (1<<0)
44#define ED_EHDR (1<<1)

--- 64 unchanged lines hidden (view full) ---

109 offsetof(Elf32_Shdr, sh_flags), offsetof(Elf32_Shdr, sh_addr),
110 offsetof(Elf32_Shdr, sh_offset), offsetof(Elf32_Shdr, sh_size),
111 offsetof(Elf32_Shdr, sh_link), offsetof(Elf32_Shdr, sh_info),
112 offsetof(Elf32_Shdr, sh_addralign), offsetof(Elf32_Shdr, sh_entsize),
113
114 offsetof(Elf32_Sym, st_name), offsetof(Elf32_Sym, st_value),
115 offsetof(Elf32_Sym, st_size), offsetof(Elf32_Sym, st_info),
116 offsetof(Elf32_Sym, st_shndx),
120
117
121 offsetof(Elf32_Rel, r_offset), offsetof(Elf32_Rel, r_info),
122
123 offsetof(Elf32_Rela, r_offset), offsetof(Elf32_Rela, r_info),
124 offsetof(Elf32_Rela, r_addend)
125};
126
127int elf64_offsets[] = {
128 0,
129
130 offsetof(Elf64_Dyn, d_tag), offsetof(Elf64_Dyn, d_un.d_ptr),
131 offsetof(Elf64_Dyn, d_un.d_val),
118 offsetof(Elf32_Rel, r_offset), offsetof(Elf32_Rel, r_info),
119
120 offsetof(Elf32_Rela, r_offset), offsetof(Elf32_Rela, r_info),
121 offsetof(Elf32_Rela, r_addend)
122};
123
124int elf64_offsets[] = {
125 0,
126
127 offsetof(Elf64_Dyn, d_tag), offsetof(Elf64_Dyn, d_un.d_ptr),
128 offsetof(Elf64_Dyn, d_un.d_val),
132
129
133 offsetof(Elf32_Ehdr, e_ident[EI_CLASS]),
134 offsetof(Elf32_Ehdr, e_ident[EI_DATA]),
135 offsetof(Elf32_Ehdr, e_ident[EI_OSABI]),
136 offsetof(Elf64_Ehdr, e_type), offsetof(Elf64_Ehdr, e_machine),
137 offsetof(Elf64_Ehdr, e_version), offsetof(Elf64_Ehdr, e_entry),
138 offsetof(Elf64_Ehdr, e_phoff), offsetof(Elf64_Ehdr, e_shoff),
139 offsetof(Elf64_Ehdr, e_flags), offsetof(Elf64_Ehdr, e_ehsize),
140 offsetof(Elf64_Ehdr, e_phentsize), offsetof(Elf64_Ehdr, e_phnum),

--- 12 unchanged lines hidden (view full) ---

153 offsetof(Elf64_Shdr, sh_flags), offsetof(Elf64_Shdr, sh_addr),
154 offsetof(Elf64_Shdr, sh_offset), offsetof(Elf64_Shdr, sh_size),
155 offsetof(Elf64_Shdr, sh_link), offsetof(Elf64_Shdr, sh_info),
156 offsetof(Elf64_Shdr, sh_addralign), offsetof(Elf64_Shdr, sh_entsize),
157
158 offsetof(Elf64_Sym, st_name), offsetof(Elf64_Sym, st_value),
159 offsetof(Elf64_Sym, st_size), offsetof(Elf64_Sym, st_info),
160 offsetof(Elf64_Sym, st_shndx),
130 offsetof(Elf32_Ehdr, e_ident[EI_CLASS]),
131 offsetof(Elf32_Ehdr, e_ident[EI_DATA]),
132 offsetof(Elf32_Ehdr, e_ident[EI_OSABI]),
133 offsetof(Elf64_Ehdr, e_type), offsetof(Elf64_Ehdr, e_machine),
134 offsetof(Elf64_Ehdr, e_version), offsetof(Elf64_Ehdr, e_entry),
135 offsetof(Elf64_Ehdr, e_phoff), offsetof(Elf64_Ehdr, e_shoff),
136 offsetof(Elf64_Ehdr, e_flags), offsetof(Elf64_Ehdr, e_ehsize),
137 offsetof(Elf64_Ehdr, e_phentsize), offsetof(Elf64_Ehdr, e_phnum),

--- 12 unchanged lines hidden (view full) ---

150 offsetof(Elf64_Shdr, sh_flags), offsetof(Elf64_Shdr, sh_addr),
151 offsetof(Elf64_Shdr, sh_offset), offsetof(Elf64_Shdr, sh_size),
152 offsetof(Elf64_Shdr, sh_link), offsetof(Elf64_Shdr, sh_info),
153 offsetof(Elf64_Shdr, sh_addralign), offsetof(Elf64_Shdr, sh_entsize),
154
155 offsetof(Elf64_Sym, st_name), offsetof(Elf64_Sym, st_value),
156 offsetof(Elf64_Sym, st_size), offsetof(Elf64_Sym, st_info),
157 offsetof(Elf64_Sym, st_shndx),
161
158
162 offsetof(Elf64_Rel, r_offset), offsetof(Elf64_Rel, r_info),
163
164 offsetof(Elf64_Rela, r_offset), offsetof(Elf64_Rela, r_info),
165 offsetof(Elf64_Rela, r_addend)
166};
167
159 offsetof(Elf64_Rel, r_offset), offsetof(Elf64_Rel, r_info),
160
161 offsetof(Elf64_Rela, r_offset), offsetof(Elf64_Rela, r_info),
162 offsetof(Elf64_Rela, r_addend)
163};
164
168/* http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#tag_encodings */
169static const char *
170d_tags(u_int64_t tag) {
171 switch (tag) {
172 case 0: return "DT_NULL";
173 case 1: return "DT_NEEDED";
174 case 2: return "DT_PLTRELSZ";
175 case 3: return "DT_PLTGOT";
176 case 4: return "DT_HASH";
177 case 5: return "DT_STRTAB";
178 case 6: return "DT_SYMTAB";
179 case 7: return "DT_RELA";
180 case 8: return "DT_RELASZ";
181 case 9: return "DT_RELAENT";
182 case 10: return "DT_STRSZ";
183 case 11: return "DT_SYMENT";
184 case 12: return "DT_INIT";
185 case 13: return "DT_FINI";
186 case 14: return "DT_SONAME";
187 case 15: return "DT_RPATH";
188 case 16: return "DT_SYMBOLIC";
189 case 17: return "DT_REL";
190 case 18: return "DT_RELSZ";
191 case 19: return "DT_RELENT";
192 case 20: return "DT_PLTREL";
193 case 21: return "DT_DEBUG";
194 case 22: return "DT_TEXTREL";
195 case 23: return "DT_JMPREL";
196 case 24: return "DT_BIND_NOW";
197 case 25: return "DT_INIT_ARRAY";
198 case 26: return "DT_FINI_ARRAY";
199 case 27: return "DT_INIT_ARRAYSZ";
200 case 28: return "DT_FINI_ARRAYSZ";
201 case 29: return "DT_RUNPATH";
202 case 30: return "DT_FLAGS";
203 case 32: return "DT_PREINIT_ARRAY"; /* XXX: DT_ENCODING */
204 case 33: return "DT_PREINIT_ARRAYSZ";
205 /* 0x6000000D - 0x6ffff000 operating system-specific semantics */
206 case 0x6ffffdf5: return "DT_GNU_PRELINKED";
207 case 0x6ffffdf6: return "DT_GNU_CONFLICTSZ";
208 case 0x6ffffdf7: return "DT_GNU_LIBLISTSZ";
209 case 0x6ffffdf8: return "DT_SUNW_CHECKSUM";
210 case 0x6ffffdf9: return "DT_PLTPADSZ";
211 case 0x6ffffdfa: return "DT_MOVEENT";
212 case 0x6ffffdfb: return "DT_MOVESZ";
213 case 0x6ffffdfc: return "DT_FEATURE";
214 case 0x6ffffdfd: return "DT_POSFLAG_1";
215 case 0x6ffffdfe: return "DT_SYMINSZ";
216 case 0x6ffffdff: return "DT_SYMINENT (DT_VALRNGHI)";
217 case 0x6ffffe00: return "DT_ADDRRNGLO";
218 case 0x6ffffef8: return "DT_GNU_CONFLICT";
219 case 0x6ffffef9: return "DT_GNU_LIBLIST";
220 case 0x6ffffefa: return "DT_SUNW_CONFIG";
221 case 0x6ffffefb: return "DT_SUNW_DEPAUDIT";
222 case 0x6ffffefc: return "DT_SUNW_AUDIT";
223 case 0x6ffffefd: return "DT_SUNW_PLTPAD";
224 case 0x6ffffefe: return "DT_SUNW_MOVETAB";
225 case 0x6ffffeff: return "DT_SYMINFO (DT_ADDRRNGHI)";
226 case 0x6ffffff9: return "DT_RELACOUNT";
227 case 0x6ffffffa: return "DT_RELCOUNT";
228 case 0x6ffffffb: return "DT_FLAGS_1";
229 case 0x6ffffffc: return "DT_VERDEF";
230 case 0x6ffffffd: return "DT_VERDEFNUM";
231 case 0x6ffffffe: return "DT_VERNEED";
232 case 0x6fffffff: return "DT_VERNEEDNUM";
233 case 0x6ffffff0: return "DT_GNU_VERSYM";
234 /* 0x70000000 - 0x7fffffff processor-specific semantics */
235 case 0x70000000: return "DT_IA_64_PLT_RESERVE";
236 case 0x7ffffffd: return "DT_SUNW_AUXILIARY";
237 case 0x7ffffffe: return "DT_SUNW_USED";
238 case 0x7fffffff: return "DT_SUNW_FILTER";
239 default: return "ERROR: TAG NOT DEFINED";
240 }
241}
165char *d_tags[] = {
166 "DT_NULL", "DT_NEEDED", "DT_PLTRELSZ", "DT_PLTGOT", "DT_HASH",
167 "DT_STRTAB", "DT_SYMTAB", "DT_RELA", "DT_RELASZ", "DT_RELAENT",
168 "DT_STRSZ", "DT_SYMENT", "DT_INIT", "DT_FINI", "DT_SONAME",
169 "DT_RPATH", "DT_SYMBOLIC", "DT_REL", "DT_RELSZ", "DT_RELENT",
170 "DT_PLTREL", "DT_DEBUG", "DT_TEXTREL", "DT_JMPREL"
171};
242
172
243static const char *
244e_machines(u_int mach)
245{
246 static char machdesc[64];
173char *e_machines[] = {
174 "EM_NONE", "EM_M32", "EM_SPARC", "EM_386", "EM_68K", "EM_88K",
175 "EM_486", "EM_860", "EM_MIPS"
176};
247
177
248 switch (mach) {
249 case EM_NONE: return "EM_NONE";
250 case EM_M32: return "EM_M32";
251 case EM_SPARC: return "EM_SPARC";
252 case EM_386: return "EM_386";
253 case EM_68K: return "EM_68K";
254 case EM_88K: return "EM_88K";
255 case EM_860: return "EM_860";
256 case EM_MIPS: return "EM_MIPS";
257 case EM_PPC: return "EM_PPC";
258 case EM_ARM: return "EM_ARM";
259 case EM_ALPHA: return "EM_ALPHA (legacy)";
260 case EM_SPARCV9:return "EM_SPARCV9";
261 case EM_IA_64: return "EM_IA_64";
262 case EM_X86_64: return "EM_X86_64";
263 }
264 snprintf(machdesc, sizeof(machdesc),
265 "(unknown machine) -- type 0x%x", mach);
266 return (machdesc);
267}
268
269const char *e_types[] = {
178char *e_types[] = {
270 "ET_NONE", "ET_REL", "ET_EXEC", "ET_DYN", "ET_CORE"
271};
272
179 "ET_NONE", "ET_REL", "ET_EXEC", "ET_DYN", "ET_CORE"
180};
181
273const char *ei_versions[] = {
182char *ei_versions[] = {
274 "EV_NONE", "EV_CURRENT"
275};
276
183 "EV_NONE", "EV_CURRENT"
184};
185
277const char *ei_classes[] = {
186char *ei_classes[] = {
278 "ELFCLASSNONE", "ELFCLASS32", "ELFCLASS64"
279};
280
187 "ELFCLASSNONE", "ELFCLASS32", "ELFCLASS64"
188};
189
281const char *ei_data[] = {
190char *ei_data[] = {
282 "ELFDATANONE", "ELFDATA2LSB", "ELFDATA2MSB"
283};
284
191 "ELFDATANONE", "ELFDATA2LSB", "ELFDATA2MSB"
192};
193
285const char *ei_abis[] = {
194char *ei_abis[] = {
286 "ELFOSABI_SYSV", "ELFOSABI_HPUX", "ELFOSABI_NETBSD", "ELFOSABI_LINUX",
287 "ELFOSABI_HURD", "ELFOSABI_86OPEN", "ELFOSABI_SOLARIS",
288 "ELFOSABI_MONTEREY", "ELFOSABI_IRIX", "ELFOSABI_FREEBSD",
289 "ELFOSABI_TRU64", "ELFOSABI_MODESTO", "ELFOSABI_OPENBSD"
290};
291
195 "ELFOSABI_SYSV", "ELFOSABI_HPUX", "ELFOSABI_NETBSD", "ELFOSABI_LINUX",
196 "ELFOSABI_HURD", "ELFOSABI_86OPEN", "ELFOSABI_SOLARIS",
197 "ELFOSABI_MONTEREY", "ELFOSABI_IRIX", "ELFOSABI_FREEBSD",
198 "ELFOSABI_TRU64", "ELFOSABI_MODESTO", "ELFOSABI_OPENBSD"
199};
200
292const char *p_types[] = {
201char *p_types[] = {
293 "PT_NULL", "PT_LOAD", "PT_DYNAMIC", "PT_INTERP", "PT_NOTE",
202 "PT_NULL", "PT_LOAD", "PT_DYNAMIC", "PT_INTERP", "PT_NOTE",
294 "PT_SHLIB", "PT_PHDR", "PT_TLS"
203 "PT_SHLIB", "PT_PHDR"
295};
296
204};
205
297const char *p_flags[] = {
206char *p_flags[] = {
298 "", "PF_X", "PF_W", "PF_X|PF_W", "PF_R", "PF_X|PF_R", "PF_W|PF_R",
299 "PF_X|PF_W|PF_R"
300};
301
207 "", "PF_X", "PF_W", "PF_X|PF_W", "PF_R", "PF_X|PF_R", "PF_W|PF_R",
208 "PF_X|PF_W|PF_R"
209};
210
302/* http://www.sco.com/developers/gabi/latest/ch4.sheader.html#sh_type */
303static const char *
304sh_types(u_int64_t sht) {
305 switch (sht) {
306 case 0: return "SHT_NULL";
307 case 1: return "SHT_PROGBITS";
308 case 2: return "SHT_SYMTAB";
309 case 3: return "SHT_STRTAB";
310 case 4: return "SHT_RELA";
311 case 5: return "SHT_HASH";
312 case 6: return "SHT_DYNAMIC";
313 case 7: return "SHT_NOTE";
314 case 8: return "SHT_NOBITS";
315 case 9: return "SHT_REL";
316 case 10: return "SHT_SHLIB";
317 case 11: return "SHT_DYNSYM";
318 case 14: return "SHT_INIT_ARRAY";
319 case 15: return "SHT_FINI_ARRAY";
320 case 16: return "SHT_PREINIT_ARRAY";
321 case 17: return "SHT_GROUP";
322 case 18: return "SHT_SYMTAB_SHNDX";
323 /* 0x60000000 - 0x6fffffff operating system-specific semantics */
324 case 0x6ffffff0: return "XXX:VERSYM";
325 case 0x6ffffff7: return "SHT_GNU_LIBLIST";
326 case 0x6ffffffc: return "XXX:VERDEF";
327 case 0x6ffffffd: return "SHT_SUNW(GNU)_verdef";
328 case 0x6ffffffe: return "SHT_SUNW(GNU)_verneed";
329 case 0x6fffffff: return "SHT_SUNW(GNU)_versym";
330 /* 0x70000000 - 0x7fffffff processor-specific semantics */
331 case 0x70000000: return "SHT_IA_64_EXT";
332 case 0x70000001: return "SHT_IA_64_UNWIND";
333 case 0x7ffffffd: return "XXX:AUXILIARY";
334 case 0x7fffffff: return "XXX:FILTER";
335 /* 0x80000000 - 0xffffffff application programs */
336 default: return "ERROR: SHT NOT DEFINED";
337 }
338}
211char *sh_types[] = {
212 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
213 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", "SHT_NOBITS",
214 "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM"
215};
339
216
340const char *sh_flags[] = {
217char *sh_flags[] = {
341 "", "SHF_WRITE", "SHF_ALLOC", "SHF_WRITE|SHF_ALLOC", "SHF_EXECINSTR",
342 "SHF_WRITE|SHF_EXECINSTR", "SHF_ALLOC|SHF_EXECINSTR",
343 "SHF_WRITE|SHF_ALLOC|SHF_EXECINSTR"
344};
345
218 "", "SHF_WRITE", "SHF_ALLOC", "SHF_WRITE|SHF_ALLOC", "SHF_EXECINSTR",
219 "SHF_WRITE|SHF_EXECINSTR", "SHF_ALLOC|SHF_EXECINSTR",
220 "SHF_WRITE|SHF_ALLOC|SHF_EXECINSTR"
221};
222
346const char *st_types[] = {
223char *st_types[] = {
347 "STT_NOTYPE", "STT_OBJECT", "STT_FUNC", "STT_SECTION", "STT_FILE"
348};
349
224 "STT_NOTYPE", "STT_OBJECT", "STT_FUNC", "STT_SECTION", "STT_FILE"
225};
226
350const char *st_bindings[] = {
227char *st_bindings[] = {
351 "STB_LOCAL", "STB_GLOBAL", "STB_WEAK"
352};
353
354char *dynstr;
355char *shstrtab;
356char *strtab;
357FILE *out;
358
359u_int64_t elf_get_byte(Elf32_Ehdr *e, void *base, elf_member_t member);
360u_int64_t elf_get_quarter(Elf32_Ehdr *e, void *base, elf_member_t member);
361u_int64_t elf_get_half(Elf32_Ehdr *e, void *base, elf_member_t member);
362u_int64_t elf_get_word(Elf32_Ehdr *e, void *base, elf_member_t member);
363u_int64_t elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t member);
364
228 "STB_LOCAL", "STB_GLOBAL", "STB_WEAK"
229};
230
231char *dynstr;
232char *shstrtab;
233char *strtab;
234FILE *out;
235
236u_int64_t elf_get_byte(Elf32_Ehdr *e, void *base, elf_member_t member);
237u_int64_t elf_get_quarter(Elf32_Ehdr *e, void *base, elf_member_t member);
238u_int64_t elf_get_half(Elf32_Ehdr *e, void *base, elf_member_t member);
239u_int64_t elf_get_word(Elf32_Ehdr *e, void *base, elf_member_t member);
240u_int64_t elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t member);
241
365void elf_print_ehdr(Elf32_Ehdr *e);
366void elf_print_phdr(Elf32_Ehdr *e, void *p);
367void elf_print_shdr(Elf32_Ehdr *e, void *sh);
368void elf_print_symtab(Elf32_Ehdr *e, void *sh, char *str);
369void elf_print_dynamic(Elf32_Ehdr *e, void *sh);
370void elf_print_rel(Elf32_Ehdr *e, void *r);
371void elf_print_rela(Elf32_Ehdr *e, void *ra);
372void elf_print_interp(Elf32_Ehdr *e, void *p);
373void elf_print_got(Elf32_Ehdr *e, void *sh);
374void elf_print_hash(Elf32_Ehdr *e, void *sh);
375void elf_print_note(Elf32_Ehdr *e, void *sh);
242void elf_print_ehdr(void *e);
243void elf_print_phdr(void *e, void *p);
244void elf_print_shdr(void *e, void *sh);
245void elf_print_symtab(void *e, void *sh, char *str);
246void elf_print_dynamic(void *e, void *sh);
247void elf_print_rel(void *e, void *r);
248void elf_print_rela(void *e, void *ra);
249void elf_print_interp(void *e, void *p);
250void elf_print_got(void *e, void *sh);
251void elf_print_hash(void *e, void *sh);
252void elf_print_note(void *e, void *sh);
376
377void usage(void);
378
379int
380main(int ac, char **av)
381{
382 u_int64_t phoff;
383 u_int64_t shoff;
384 u_int64_t phentsize;
385 u_int64_t phnum;
386 u_int64_t shentsize;
387 u_int64_t shnum;
388 u_int64_t shstrndx;
389 u_int64_t offset;
390 u_int64_t name;
391 u_int64_t type;
392 struct stat sb;
393 u_int flags;
253
254void usage(void);
255
256int
257main(int ac, char **av)
258{
259 u_int64_t phoff;
260 u_int64_t shoff;
261 u_int64_t phentsize;
262 u_int64_t phnum;
263 u_int64_t shentsize;
264 u_int64_t shnum;
265 u_int64_t shstrndx;
266 u_int64_t offset;
267 u_int64_t name;
268 u_int64_t type;
269 struct stat sb;
270 u_int flags;
394 Elf32_Ehdr *e;
271 void *e;
395 void *p;
396 void *sh;
397 void *v;
398 int fd;
399 int ch;
400 int i;
401
402 out = stdout;

--- 42 unchanged lines hidden (view full) ---

445 usage();
446 }
447 ac -= optind;
448 av += optind;
449 if (ac == 0 || flags == 0)
450 usage();
451 if ((fd = open(*av, O_RDONLY)) < 0 ||
452 fstat(fd, &sb) < 0)
272 void *p;
273 void *sh;
274 void *v;
275 int fd;
276 int ch;
277 int i;
278
279 out = stdout;

--- 42 unchanged lines hidden (view full) ---

322 usage();
323 }
324 ac -= optind;
325 av += optind;
326 if (ac == 0 || flags == 0)
327 usage();
328 if ((fd = open(*av, O_RDONLY)) < 0 ||
329 fstat(fd, &sb) < 0)
453 err(1, "%s", *av);
330 err(1, NULL);
454 e = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
455 if (e == MAP_FAILED)
456 err(1, NULL);
457 if (!IS_ELF(*(Elf32_Ehdr *)e))
458 errx(1, "not an elf file");
459 phoff = elf_get_off(e, e, E_PHOFF);
460 shoff = elf_get_off(e, e, E_SHOFF);
461 phentsize = elf_get_quarter(e, e, E_PHENTSIZE);
462 phnum = elf_get_quarter(e, e, E_PHNUM);
463 shentsize = elf_get_quarter(e, e, E_SHENTSIZE);
464 shnum = elf_get_quarter(e, e, E_SHNUM);
465 shstrndx = elf_get_quarter(e, e, E_SHSTRNDX);
331 e = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
332 if (e == MAP_FAILED)
333 err(1, NULL);
334 if (!IS_ELF(*(Elf32_Ehdr *)e))
335 errx(1, "not an elf file");
336 phoff = elf_get_off(e, e, E_PHOFF);
337 shoff = elf_get_off(e, e, E_SHOFF);
338 phentsize = elf_get_quarter(e, e, E_PHENTSIZE);
339 phnum = elf_get_quarter(e, e, E_PHNUM);
340 shentsize = elf_get_quarter(e, e, E_SHENTSIZE);
341 shnum = elf_get_quarter(e, e, E_SHNUM);
342 shstrndx = elf_get_quarter(e, e, E_SHSTRNDX);
466 p = (char *)e + phoff;
467 sh = (char *)e + shoff;
468 offset = elf_get_off(e, (char *)sh + shstrndx * shentsize, SH_OFFSET);
469 shstrtab = (char *)e + offset;
470 for (i = 0; (u_int64_t)i < shnum; i++) {
471 name = elf_get_word(e, (char *)sh + i * shentsize, SH_NAME);
472 offset = elf_get_off(e, (char *)sh + i * shentsize, SH_OFFSET);
343 p = e + phoff;
344 sh = e + shoff;
345 offset = elf_get_off(e, sh + shstrndx * shentsize, SH_OFFSET);
346 shstrtab = e + offset;
347 for (i = 0; i < shnum; i++) {
348 name = elf_get_word(e, sh + i * shentsize, SH_NAME);
349 offset = elf_get_off(e, sh + i * shentsize, SH_OFFSET);
473 if (strcmp(shstrtab + name, ".strtab") == 0)
350 if (strcmp(shstrtab + name, ".strtab") == 0)
474 strtab = (char *)e + offset;
351 strtab = e + offset;
475 if (strcmp(shstrtab + name, ".dynstr") == 0)
352 if (strcmp(shstrtab + name, ".dynstr") == 0)
476 dynstr = (char *)e + offset;
353 dynstr = e + offset;
477 }
478 if (flags & ED_EHDR)
479 elf_print_ehdr(e);
480 if (flags & ED_PHDR)
481 elf_print_phdr(e, p);
482 if (flags & ED_SHDR)
483 elf_print_shdr(e, sh);
354 }
355 if (flags & ED_EHDR)
356 elf_print_ehdr(e);
357 if (flags & ED_PHDR)
358 elf_print_phdr(e, p);
359 if (flags & ED_SHDR)
360 elf_print_shdr(e, sh);
484 for (i = 0; (u_int64_t)i < phnum; i++) {
485 v = (char *)p + i * phentsize;
361 for (i = 0; i < phnum; i++) {
362 v = p + i * phentsize;
486 type = elf_get_word(e, v, P_TYPE);
487 switch (type) {
488 case PT_INTERP:
489 if (flags & ED_INTERP)
490 elf_print_interp(e, v);
491 break;
492 case PT_NULL:
493 case PT_LOAD:
494 case PT_DYNAMIC:
495 case PT_NOTE:
496 case PT_SHLIB:
497 case PT_PHDR:
498 break;
499 }
500 }
363 type = elf_get_word(e, v, P_TYPE);
364 switch (type) {
365 case PT_INTERP:
366 if (flags & ED_INTERP)
367 elf_print_interp(e, v);
368 break;
369 case PT_NULL:
370 case PT_LOAD:
371 case PT_DYNAMIC:
372 case PT_NOTE:
373 case PT_SHLIB:
374 case PT_PHDR:
375 break;
376 }
377 }
501 for (i = 0; (u_int64_t)i < shnum; i++) {
502 v = (char *)sh + i * shentsize;
378 for (i = 0; i < shnum; i++) {
379 v = sh + i * shentsize;
503 type = elf_get_word(e, v, SH_TYPE);
504 switch (type) {
505 case SHT_SYMTAB:
506 if (flags & ED_SYMTAB)
507 elf_print_symtab(e, v, strtab);
508 break;
509 case SHT_DYNAMIC:
510 if (flags & ED_DYN)

--- 34 unchanged lines hidden (view full) ---

545 break;
546 }
547 }
548
549 return 0;
550}
551
552void
380 type = elf_get_word(e, v, SH_TYPE);
381 switch (type) {
382 case SHT_SYMTAB:
383 if (flags & ED_SYMTAB)
384 elf_print_symtab(e, v, strtab);
385 break;
386 case SHT_DYNAMIC:
387 if (flags & ED_DYN)

--- 34 unchanged lines hidden (view full) ---

422 break;
423 }
424 }
425
426 return 0;
427}
428
429void
553elf_print_ehdr(Elf32_Ehdr *e)
430elf_print_ehdr(void *e)
554{
555 u_int64_t class;
556 u_int64_t data;
557 u_int64_t osabi;
558 u_int64_t type;
559 u_int64_t machine;
560 u_int64_t version;
561 u_int64_t entry;

--- 23 unchanged lines hidden (view full) ---

585 shentsize = elf_get_quarter(e, e, E_SHENTSIZE);
586 shnum = elf_get_quarter(e, e, E_SHNUM);
587 shstrndx = elf_get_quarter(e, e, E_SHSTRNDX);
588 fprintf(out, "\nelf header:\n");
589 fprintf(out, "\n");
590 fprintf(out, "\te_ident: %s %s %s\n", ei_classes[class], ei_data[data],
591 ei_abis[osabi]);
592 fprintf(out, "\te_type: %s\n", e_types[type]);
431{
432 u_int64_t class;
433 u_int64_t data;
434 u_int64_t osabi;
435 u_int64_t type;
436 u_int64_t machine;
437 u_int64_t version;
438 u_int64_t entry;

--- 23 unchanged lines hidden (view full) ---

462 shentsize = elf_get_quarter(e, e, E_SHENTSIZE);
463 shnum = elf_get_quarter(e, e, E_SHNUM);
464 shstrndx = elf_get_quarter(e, e, E_SHSTRNDX);
465 fprintf(out, "\nelf header:\n");
466 fprintf(out, "\n");
467 fprintf(out, "\te_ident: %s %s %s\n", ei_classes[class], ei_data[data],
468 ei_abis[osabi]);
469 fprintf(out, "\te_type: %s\n", e_types[type]);
593 fprintf(out, "\te_machine: %s\n", e_machines(machine));
470 if (machine < sizeof e_machines / sizeof *e_machines)
471 fprintf(out, "\te_machine: %s\n", e_machines[machine]);
472 else
473 fprintf(out, "\te_machine: %lld\n", machine);
594 fprintf(out, "\te_version: %s\n", ei_versions[version]);
474 fprintf(out, "\te_version: %s\n", ei_versions[version]);
595 fprintf(out, "\te_entry: %#jx\n", (intmax_t)entry);
596 fprintf(out, "\te_phoff: %jd\n", (intmax_t)phoff);
597 fprintf(out, "\te_shoff: %jd\n", (intmax_t)shoff);
598 fprintf(out, "\te_flags: %jd\n", (intmax_t)flags);
599 fprintf(out, "\te_ehsize: %jd\n", (intmax_t)ehsize);
600 fprintf(out, "\te_phentsize: %jd\n", (intmax_t)phentsize);
601 fprintf(out, "\te_phnum: %jd\n", (intmax_t)phnum);
602 fprintf(out, "\te_shentsize: %jd\n", (intmax_t)shentsize);
603 fprintf(out, "\te_shnum: %jd\n", (intmax_t)shnum);
604 fprintf(out, "\te_shstrndx: %jd\n", (intmax_t)shstrndx);
475 fprintf(out, "\te_entry: %#llx\n", entry);
476 fprintf(out, "\te_phoff: %lld\n", phoff);
477 fprintf(out, "\te_shoff: %lld\n", shoff);
478 fprintf(out, "\te_flags: %lld\n", flags);
479 fprintf(out, "\te_ehsize: %lld\n", ehsize);
480 fprintf(out, "\te_phentsize: %lld\n", phentsize);
481 fprintf(out, "\te_phnum: %lld\n", phnum);
482 fprintf(out, "\te_shentsize: %lld\n", shentsize);
483 fprintf(out, "\te_shnum: %lld\n", shnum);
484 fprintf(out, "\te_shstrndx: %lld\n", shstrndx);
605}
606
607void
485}
486
487void
608elf_print_phdr(Elf32_Ehdr *e, void *p)
488elf_print_phdr(void *e, void *p)
609{
610 u_int64_t phentsize;
611 u_int64_t phnum;
612 u_int64_t type;
613 u_int64_t offset;
614 u_int64_t vaddr;
615 u_int64_t paddr;
616 u_int64_t filesz;
617 u_int64_t memsz;
618 u_int64_t flags;
619 u_int64_t align;
620 void *v;
621 int i;
622
623 phentsize = elf_get_quarter(e, e, E_PHENTSIZE);
624 phnum = elf_get_quarter(e, e, E_PHNUM);
625 fprintf(out, "\nprogram header:\n");
489{
490 u_int64_t phentsize;
491 u_int64_t phnum;
492 u_int64_t type;
493 u_int64_t offset;
494 u_int64_t vaddr;
495 u_int64_t paddr;
496 u_int64_t filesz;
497 u_int64_t memsz;
498 u_int64_t flags;
499 u_int64_t align;
500 void *v;
501 int i;
502
503 phentsize = elf_get_quarter(e, e, E_PHENTSIZE);
504 phnum = elf_get_quarter(e, e, E_PHNUM);
505 fprintf(out, "\nprogram header:\n");
626 for (i = 0; (u_int64_t)i < phnum; i++) {
627 v = (char *)p + i * phentsize;
506 for (i = 0; i < phnum; i++) {
507 v = p + i * phentsize;
628 type = elf_get_word(e, v, P_TYPE);
629 offset = elf_get_off(e, v, P_OFFSET);
630 vaddr = elf_get_addr(e, v, P_VADDR);
631 paddr = elf_get_addr(e, v, P_PADDR);
632 filesz = elf_get_size(e, v, P_FILESZ);
633 memsz = elf_get_size(e, v, P_MEMSZ);
634 flags = elf_get_word(e, v, P_FLAGS);
635 align = elf_get_size(e, v, P_ALIGN);
636 fprintf(out, "\n");
637 fprintf(out, "entry: %d\n", i);
638 fprintf(out, "\tp_type: %s\n", p_types[type & 0x7]);
508 type = elf_get_word(e, v, P_TYPE);
509 offset = elf_get_off(e, v, P_OFFSET);
510 vaddr = elf_get_addr(e, v, P_VADDR);
511 paddr = elf_get_addr(e, v, P_PADDR);
512 filesz = elf_get_size(e, v, P_FILESZ);
513 memsz = elf_get_size(e, v, P_MEMSZ);
514 flags = elf_get_word(e, v, P_FLAGS);
515 align = elf_get_size(e, v, P_ALIGN);
516 fprintf(out, "\n");
517 fprintf(out, "entry: %d\n", i);
518 fprintf(out, "\tp_type: %s\n", p_types[type & 0x7]);
639 fprintf(out, "\tp_offset: %jd\n", (intmax_t)offset);
640 fprintf(out, "\tp_vaddr: %#jx\n", (intmax_t)vaddr);
641 fprintf(out, "\tp_paddr: %#jx\n", (intmax_t)paddr);
642 fprintf(out, "\tp_filesz: %jd\n", (intmax_t)filesz);
643 fprintf(out, "\tp_memsz: %jd\n", (intmax_t)memsz);
519 fprintf(out, "\tp_offset: %lld\n", offset);
520 fprintf(out, "\tp_vaddr: %#llx\n", vaddr);
521 fprintf(out, "\tp_paddr: %#llx\n", paddr);
522 fprintf(out, "\tp_filesz: %lld\n", filesz);
523 fprintf(out, "\tp_memsz: %lld\n", memsz);
644 fprintf(out, "\tp_flags: %s\n", p_flags[flags]);
524 fprintf(out, "\tp_flags: %s\n", p_flags[flags]);
645 fprintf(out, "\tp_align: %jd\n", (intmax_t)align);
525 fprintf(out, "\tp_align: %lld\n", align);
646 }
647}
648
649void
526 }
527}
528
529void
650elf_print_shdr(Elf32_Ehdr *e, void *sh)
530elf_print_shdr(void *e, void *sh)
651{
652 u_int64_t shentsize;
653 u_int64_t shnum;
654 u_int64_t name;
655 u_int64_t type;
656 u_int64_t flags;
657 u_int64_t addr;
658 u_int64_t offset;
659 u_int64_t size;
531{
532 u_int64_t shentsize;
533 u_int64_t shnum;
534 u_int64_t name;
535 u_int64_t type;
536 u_int64_t flags;
537 u_int64_t addr;
538 u_int64_t offset;
539 u_int64_t size;
660 u_int64_t shlink;
540 u_int64_t link;
661 u_int64_t info;
662 u_int64_t addralign;
663 u_int64_t entsize;
664 void *v;
665 int i;
666
667 shentsize = elf_get_quarter(e, e, E_SHENTSIZE);
668 shnum = elf_get_quarter(e, e, E_SHNUM);
669 fprintf(out, "\nsection header:\n");
541 u_int64_t info;
542 u_int64_t addralign;
543 u_int64_t entsize;
544 void *v;
545 int i;
546
547 shentsize = elf_get_quarter(e, e, E_SHENTSIZE);
548 shnum = elf_get_quarter(e, e, E_SHNUM);
549 fprintf(out, "\nsection header:\n");
670 for (i = 0; (u_int64_t)i < shnum; i++) {
671 v = (char *)sh + i * shentsize;
550 for (i = 0; i < shnum; i++) {
551 v = sh + i * shentsize;
672 name = elf_get_word(e, v, SH_NAME);
673 type = elf_get_word(e, v, SH_TYPE);
674 flags = elf_get_word(e, v, SH_FLAGS);
675 addr = elf_get_addr(e, v, SH_ADDR);
676 offset = elf_get_off(e, v, SH_OFFSET);
677 size = elf_get_size(e, v, SH_SIZE);
552 name = elf_get_word(e, v, SH_NAME);
553 type = elf_get_word(e, v, SH_TYPE);
554 flags = elf_get_word(e, v, SH_FLAGS);
555 addr = elf_get_addr(e, v, SH_ADDR);
556 offset = elf_get_off(e, v, SH_OFFSET);
557 size = elf_get_size(e, v, SH_SIZE);
678 shlink = elf_get_word(e, v, SH_LINK);
558 link = elf_get_word(e, v, SH_LINK);
679 info = elf_get_word(e, v, SH_INFO);
680 addralign = elf_get_size(e, v, SH_ADDRALIGN);
681 entsize = elf_get_size(e, v, SH_ENTSIZE);
682 fprintf(out, "\n");
683 fprintf(out, "entry: %d\n", i);
684 fprintf(out, "\tsh_name: %s\n", shstrtab + name);
559 info = elf_get_word(e, v, SH_INFO);
560 addralign = elf_get_size(e, v, SH_ADDRALIGN);
561 entsize = elf_get_size(e, v, SH_ENTSIZE);
562 fprintf(out, "\n");
563 fprintf(out, "entry: %d\n", i);
564 fprintf(out, "\tsh_name: %s\n", shstrtab + name);
685 fprintf(out, "\tsh_type: %s\n", sh_types(type));
565 fprintf(out, "\tsh_type: %s\n", sh_types[type]);
686 fprintf(out, "\tsh_flags: %s\n", sh_flags[flags & 0x7]);
566 fprintf(out, "\tsh_flags: %s\n", sh_flags[flags & 0x7]);
687 fprintf(out, "\tsh_addr: %#jx\n", addr);
688 fprintf(out, "\tsh_offset: %jd\n", (intmax_t)offset);
689 fprintf(out, "\tsh_size: %jd\n", (intmax_t)size);
690 fprintf(out, "\tsh_link: %jd\n", (intmax_t)shlink);
691 fprintf(out, "\tsh_info: %jd\n", (intmax_t)info);
692 fprintf(out, "\tsh_addralign: %jd\n", (intmax_t)addralign);
693 fprintf(out, "\tsh_entsize: %jd\n", (intmax_t)entsize);
567 fprintf(out, "\tsh_addr: %#llx\n", addr);
568 fprintf(out, "\tsh_offset: %lld\n", offset);
569 fprintf(out, "\tsh_size: %lld\n", size);
570 fprintf(out, "\tsh_link: %lld\n", link);
571 fprintf(out, "\tsh_info: %lld\n", info);
572 fprintf(out, "\tsh_addralign: %lld\n", addralign);
573 fprintf(out, "\tsh_entsize: %lld\n", entsize);
694 }
695}
696
697void
574 }
575}
576
577void
698elf_print_symtab(Elf32_Ehdr *e, void *sh, char *str)
578elf_print_symtab(void *e, void *sh, char *str)
699{
700 u_int64_t offset;
701 u_int64_t entsize;
702 u_int64_t size;
703 u_int64_t name;
704 u_int64_t value;
705 u_int64_t info;
706 u_int64_t shndx;
707 void *st;
708 int len;
709 int i;
710
711 offset = elf_get_off(e, sh, SH_OFFSET);
712 entsize = elf_get_size(e, sh, SH_ENTSIZE);
713 size = elf_get_size(e, sh, SH_SIZE);
714 name = elf_get_word(e, sh, SH_NAME);
715 len = size / entsize;
716 fprintf(out, "\nsymbol table (%s):\n", shstrtab + name);
717 for (i = 0; i < len; i++) {
579{
580 u_int64_t offset;
581 u_int64_t entsize;
582 u_int64_t size;
583 u_int64_t name;
584 u_int64_t value;
585 u_int64_t info;
586 u_int64_t shndx;
587 void *st;
588 int len;
589 int i;
590
591 offset = elf_get_off(e, sh, SH_OFFSET);
592 entsize = elf_get_size(e, sh, SH_ENTSIZE);
593 size = elf_get_size(e, sh, SH_SIZE);
594 name = elf_get_word(e, sh, SH_NAME);
595 len = size / entsize;
596 fprintf(out, "\nsymbol table (%s):\n", shstrtab + name);
597 for (i = 0; i < len; i++) {
718 st = (char *)e + offset + i * entsize;
598 st = e + offset + i * entsize;
719 name = elf_get_word(e, st, ST_NAME);
720 value = elf_get_addr(e, st, ST_VALUE);
721 size = elf_get_size(e, st, ST_SIZE);
722 info = elf_get_byte(e, st, ST_INFO);
723 shndx = elf_get_quarter(e, st, ST_SHNDX);
724 fprintf(out, "\n");
725 fprintf(out, "entry: %d\n", i);
726 fprintf(out, "\tst_name: %s\n", str + name);
599 name = elf_get_word(e, st, ST_NAME);
600 value = elf_get_addr(e, st, ST_VALUE);
601 size = elf_get_size(e, st, ST_SIZE);
602 info = elf_get_byte(e, st, ST_INFO);
603 shndx = elf_get_quarter(e, st, ST_SHNDX);
604 fprintf(out, "\n");
605 fprintf(out, "entry: %d\n", i);
606 fprintf(out, "\tst_name: %s\n", str + name);
727 fprintf(out, "\tst_value: %#jx\n", value);
728 fprintf(out, "\tst_size: %jd\n", (intmax_t)size);
607 fprintf(out, "\tst_value: %#llx\n", value);
608 fprintf(out, "\tst_size: %lld\n", size);
729 fprintf(out, "\tst_info: %s %s\n",
730 st_types[ELF32_ST_TYPE(info)],
731 st_bindings[ELF32_ST_BIND(info)]);
609 fprintf(out, "\tst_info: %s %s\n",
610 st_types[ELF32_ST_TYPE(info)],
611 st_bindings[ELF32_ST_BIND(info)]);
732 fprintf(out, "\tst_shndx: %jd\n", (intmax_t)shndx);
612 fprintf(out, "\tst_shndx: %lld\n", shndx);
733 }
734}
735
736void
613 }
614}
615
616void
737elf_print_dynamic(Elf32_Ehdr *e, void *sh)
617elf_print_dynamic(void *e, void *sh)
738{
739 u_int64_t offset;
740 u_int64_t entsize;
741 u_int64_t size;
742 int64_t tag;
743 u_int64_t ptr;
744 u_int64_t val;
745 void *d;
746 int i;
747
748 offset = elf_get_off(e, sh, SH_OFFSET);
749 entsize = elf_get_size(e, sh, SH_ENTSIZE);
750 size = elf_get_size(e, sh, SH_SIZE);
751 fprintf(out, "\ndynamic:\n");
618{
619 u_int64_t offset;
620 u_int64_t entsize;
621 u_int64_t size;
622 int64_t tag;
623 u_int64_t ptr;
624 u_int64_t val;
625 void *d;
626 int i;
627
628 offset = elf_get_off(e, sh, SH_OFFSET);
629 entsize = elf_get_size(e, sh, SH_ENTSIZE);
630 size = elf_get_size(e, sh, SH_SIZE);
631 fprintf(out, "\ndynamic:\n");
752 for (i = 0; (u_int64_t)i < size / entsize; i++) {
753 d = (char *)e + offset + i * entsize;
632 for (i = 0; i < size / entsize; i++) {
633 d = e + offset + i * entsize;
754 tag = elf_get_size(e, d, D_TAG);
755 ptr = elf_get_size(e, d, D_PTR);
756 val = elf_get_addr(e, d, D_VAL);
757 fprintf(out, "\n");
758 fprintf(out, "entry: %d\n", i);
634 tag = elf_get_size(e, d, D_TAG);
635 ptr = elf_get_size(e, d, D_PTR);
636 val = elf_get_addr(e, d, D_VAL);
637 fprintf(out, "\n");
638 fprintf(out, "entry: %d\n", i);
759 fprintf(out, "\td_tag: %s\n", d_tags(tag));
639 fprintf(out, "\td_tag: %s\n", d_tags[tag]);
760 switch (tag) {
761 case DT_NEEDED:
762 case DT_SONAME:
763 case DT_RPATH:
764 fprintf(out, "\td_val: %s\n", dynstr + val);
765 break;
766 case DT_PLTRELSZ:
767 case DT_RELA:
768 case DT_RELASZ:
769 case DT_RELAENT:
770 case DT_STRSZ:
771 case DT_SYMENT:
772 case DT_RELSZ:
773 case DT_RELENT:
774 case DT_PLTREL:
640 switch (tag) {
641 case DT_NEEDED:
642 case DT_SONAME:
643 case DT_RPATH:
644 fprintf(out, "\td_val: %s\n", dynstr + val);
645 break;
646 case DT_PLTRELSZ:
647 case DT_RELA:
648 case DT_RELASZ:
649 case DT_RELAENT:
650 case DT_STRSZ:
651 case DT_SYMENT:
652 case DT_RELSZ:
653 case DT_RELENT:
654 case DT_PLTREL:
775 fprintf(out, "\td_val: %jd\n", (intmax_t)val);
655 fprintf(out, "\td_val: %lld\n", val);
776 break;
777 case DT_PLTGOT:
778 case DT_HASH:
779 case DT_STRTAB:
780 case DT_SYMTAB:
781 case DT_INIT:
782 case DT_FINI:
783 case DT_REL:
784 case DT_JMPREL:
656 break;
657 case DT_PLTGOT:
658 case DT_HASH:
659 case DT_STRTAB:
660 case DT_SYMTAB:
661 case DT_INIT:
662 case DT_FINI:
663 case DT_REL:
664 case DT_JMPREL:
785 fprintf(out, "\td_ptr: %#jx\n", ptr);
665 fprintf(out, "\td_ptr: %#llx\n", ptr);
786 break;
787 case DT_NULL:
788 case DT_SYMBOLIC:
789 case DT_DEBUG:
790 case DT_TEXTREL:
791 break;
792 }
793 }
794}
795
796void
666 break;
667 case DT_NULL:
668 case DT_SYMBOLIC:
669 case DT_DEBUG:
670 case DT_TEXTREL:
671 break;
672 }
673 }
674}
675
676void
797elf_print_rela(Elf32_Ehdr *e, void *sh)
677elf_print_rela(void *e, void *sh)
798{
799 u_int64_t offset;
800 u_int64_t entsize;
801 u_int64_t size;
802 u_int64_t name;
803 u_int64_t info;
804 int64_t addend;
805 void *ra;
806 void *v;
807 int i;
808
809 offset = elf_get_off(e, sh, SH_OFFSET);
810 entsize = elf_get_size(e, sh, SH_ENTSIZE);
811 size = elf_get_size(e, sh, SH_SIZE);
812 name = elf_get_word(e, sh, SH_NAME);
678{
679 u_int64_t offset;
680 u_int64_t entsize;
681 u_int64_t size;
682 u_int64_t name;
683 u_int64_t info;
684 int64_t addend;
685 void *ra;
686 void *v;
687 int i;
688
689 offset = elf_get_off(e, sh, SH_OFFSET);
690 entsize = elf_get_size(e, sh, SH_ENTSIZE);
691 size = elf_get_size(e, sh, SH_SIZE);
692 name = elf_get_word(e, sh, SH_NAME);
813 v = (char *)e + offset;
693 v = e + offset;
814 fprintf(out, "\nrelocation with addend (%s):\n", shstrtab + name);
694 fprintf(out, "\nrelocation with addend (%s):\n", shstrtab + name);
815 for (i = 0; (u_int64_t)i < size / entsize; i++) {
816 ra = (char *)v + i * entsize;
695 for (i = 0; i < size / entsize; i++) {
696 ra = v + i * entsize;
817 offset = elf_get_addr(e, ra, RA_OFFSET);
818 info = elf_get_word(e, ra, RA_INFO);
819 addend = elf_get_off(e, ra, RA_ADDEND);
820 fprintf(out, "\n");
821 fprintf(out, "entry: %d\n", i);
697 offset = elf_get_addr(e, ra, RA_OFFSET);
698 info = elf_get_word(e, ra, RA_INFO);
699 addend = elf_get_off(e, ra, RA_ADDEND);
700 fprintf(out, "\n");
701 fprintf(out, "entry: %d\n", i);
822 fprintf(out, "\tr_offset: %#jx\n", offset);
823 fprintf(out, "\tr_info: %jd\n", (intmax_t)info);
824 fprintf(out, "\tr_addend: %jd\n", (intmax_t)addend);
702 fprintf(out, "\tr_offset: %#llx\n", offset);
703 fprintf(out, "\tr_info: %lld\n", info);
704 fprintf(out, "\tr_addend: %lld\n", addend);
825 }
826}
827
828void
705 }
706}
707
708void
829elf_print_rel(Elf32_Ehdr *e, void *sh)
709elf_print_rel(void *e, void *sh)
830{
831 u_int64_t offset;
832 u_int64_t entsize;
833 u_int64_t size;
834 u_int64_t name;
835 u_int64_t info;
836 void *r;
837 void *v;
838 int i;
839
840 offset = elf_get_off(e, sh, SH_OFFSET);
841 entsize = elf_get_size(e, sh, SH_ENTSIZE);
842 size = elf_get_size(e, sh, SH_SIZE);
843 name = elf_get_word(e, sh, SH_NAME);
710{
711 u_int64_t offset;
712 u_int64_t entsize;
713 u_int64_t size;
714 u_int64_t name;
715 u_int64_t info;
716 void *r;
717 void *v;
718 int i;
719
720 offset = elf_get_off(e, sh, SH_OFFSET);
721 entsize = elf_get_size(e, sh, SH_ENTSIZE);
722 size = elf_get_size(e, sh, SH_SIZE);
723 name = elf_get_word(e, sh, SH_NAME);
844 v = (char *)e + offset;
724 v = e + offset;
845 fprintf(out, "\nrelocation (%s):\n", shstrtab + name);
725 fprintf(out, "\nrelocation (%s):\n", shstrtab + name);
846 for (i = 0; (u_int64_t)i < size / entsize; i++) {
847 r = (char *)v + i * entsize;
726 for (i = 0; i < size / entsize; i++) {
727 r = v + i * entsize;
848 offset = elf_get_addr(e, r, R_OFFSET);
849 info = elf_get_word(e, r, R_INFO);
850 fprintf(out, "\n");
851 fprintf(out, "entry: %d\n", i);
728 offset = elf_get_addr(e, r, R_OFFSET);
729 info = elf_get_word(e, r, R_INFO);
730 fprintf(out, "\n");
731 fprintf(out, "entry: %d\n", i);
852 fprintf(out, "\tr_offset: %#jx\n", offset);
853 fprintf(out, "\tr_info: %jd\n", (intmax_t)info);
732 fprintf(out, "\tr_offset: %#llx\n", offset);
733 fprintf(out, "\tr_info: %lld\n", info);
854 }
855}
856
857void
734 }
735}
736
737void
858elf_print_interp(Elf32_Ehdr *e, void *p)
738elf_print_interp(void *e, void *p)
859{
860 u_int64_t offset;
861 char *s;
862
863 offset = elf_get_off(e, p, P_OFFSET);
739{
740 u_int64_t offset;
741 char *s;
742
743 offset = elf_get_off(e, p, P_OFFSET);
864 s = (char *)e + offset;
744 s = e + offset;
865 fprintf(out, "\ninterp:\n");
866 fprintf(out, "\t%s\n", s);
867}
868
869void
745 fprintf(out, "\ninterp:\n");
746 fprintf(out, "\t%s\n", s);
747}
748
749void
870elf_print_got(Elf32_Ehdr *e, void *sh)
750elf_print_got(void *e, void *sh)
871{
872 u_int64_t offset;
873 u_int64_t addralign;
874 u_int64_t size;
875 u_int64_t addr;
876 void *v;
877 int i;
878
879 offset = elf_get_off(e, sh, SH_OFFSET);
880 addralign = elf_get_size(e, sh, SH_ADDRALIGN);
881 size = elf_get_size(e, sh, SH_SIZE);
751{
752 u_int64_t offset;
753 u_int64_t addralign;
754 u_int64_t size;
755 u_int64_t addr;
756 void *v;
757 int i;
758
759 offset = elf_get_off(e, sh, SH_OFFSET);
760 addralign = elf_get_size(e, sh, SH_ADDRALIGN);
761 size = elf_get_size(e, sh, SH_SIZE);
882 v = (char *)e + offset;
762 v = e + offset;
883 fprintf(out, "\nglobal offset table:\n");
763 fprintf(out, "\nglobal offset table:\n");
884 for (i = 0; (u_int64_t)i < size / addralign; i++) {
885 addr = elf_get_addr(e, (char *)v + i * addralign, 0);
764 for (i = 0; i < size / addralign; i++) {
765 addr = elf_get_addr(e, v + i * addralign, 0);
886 fprintf(out, "\n");
887 fprintf(out, "entry: %d\n", i);
766 fprintf(out, "\n");
767 fprintf(out, "entry: %d\n", i);
888 fprintf(out, "\t%#jx\n", addr);
768 fprintf(out, "\t%#llx\n", addr);
889 }
890}
891
892void
769 }
770}
771
772void
893elf_print_hash(Elf32_Ehdr *e __unused, void *sh __unused)
773elf_print_hash(void *e, void *sh)
894{
895}
896
897void
774{
775}
776
777void
898elf_print_note(Elf32_Ehdr *e, void *sh)
778elf_print_note(void *e, void *sh)
899{
900 u_int64_t offset;
901 u_int64_t size;
902 u_int64_t name;
903 u_int32_t namesz;
904 u_int32_t descsz;
905 u_int32_t type;
906 u_int32_t desc;
779{
780 u_int64_t offset;
781 u_int64_t size;
782 u_int64_t name;
783 u_int32_t namesz;
784 u_int32_t descsz;
785 u_int32_t type;
786 u_int32_t desc;
907 char *n, *s;
787 char *s;
788 void *n;
908
909 offset = elf_get_off(e, sh, SH_OFFSET);
910 size = elf_get_size(e, sh, SH_SIZE);
911 name = elf_get_word(e, sh, SH_NAME);
789
790 offset = elf_get_off(e, sh, SH_OFFSET);
791 size = elf_get_size(e, sh, SH_SIZE);
792 name = elf_get_word(e, sh, SH_NAME);
912 n = (char *)e + offset;
793 n = e + offset;
913 fprintf(out, "\nnote (%s):\n", shstrtab + name);
794 fprintf(out, "\nnote (%s):\n", shstrtab + name);
914 while (n < ((char *)e + offset + size)) {
795 while (n < e + offset + size) {
915 namesz = elf_get_word(e, n, N_NAMESZ);
916 descsz = elf_get_word(e, n, N_DESCSZ);
917 type = elf_get_word(e, n, N_TYPE);
796 namesz = elf_get_word(e, n, N_NAMESZ);
797 descsz = elf_get_word(e, n, N_DESCSZ);
798 type = elf_get_word(e, n, N_TYPE);
918 s = n + sizeof(Elf_Note);
919 desc = elf_get_word(e, n + sizeof(Elf_Note) + namesz, 0);
799 s = n + sizeof(Elf_Note);
800 desc = elf_get_word(e, n + sizeof(Elf_Note) + namesz, 0);
920 fprintf(out, "\t%s %d\n", s, desc);
921 n += sizeof(Elf_Note) + namesz + descsz;
922 }
923}
924
925u_int64_t
926elf_get_byte(Elf32_Ehdr *e, void *base, elf_member_t member)
927{
928 u_int64_t val;
801 fprintf(out, "\t%s %d\n", s, desc);
802 n += sizeof(Elf_Note) + namesz + descsz;
803 }
804}
805
806u_int64_t
807elf_get_byte(Elf32_Ehdr *e, void *base, elf_member_t member)
808{
809 u_int64_t val;
810 u_char *p;
929
930 val = 0;
931 switch (e->e_ident[EI_CLASS]) {
932 case ELFCLASS32:
811
812 val = 0;
813 switch (e->e_ident[EI_CLASS]) {
814 case ELFCLASS32:
933 val = ((char *)base)[elf32_offsets[member]];
815 p = base + elf32_offsets[member];
816 val = *p;
934 break;
935 case ELFCLASS64:
817 break;
818 case ELFCLASS64:
936 val = ((char *)base)[elf64_offsets[member]];
819 p = base + elf64_offsets[member];
820 val = *p;
937 break;
938 case ELFCLASSNONE:
939 errx(1, "invalid class");
940 }
941
942 return val;
943}
944
945u_int64_t
946elf_get_quarter(Elf32_Ehdr *e, void *base, elf_member_t member)
947{
948 u_int64_t val;
821 break;
822 case ELFCLASSNONE:
823 errx(1, "invalid class");
824 }
825
826 return val;
827}
828
829u_int64_t
830elf_get_quarter(Elf32_Ehdr *e, void *base, elf_member_t member)
831{
832 u_int64_t val;
833 u_char *p;
949
950 val = 0;
951 switch (e->e_ident[EI_CLASS]) {
952 case ELFCLASS32:
834
835 val = 0;
836 switch (e->e_ident[EI_CLASS]) {
837 case ELFCLASS32:
953 base = (char *)base + elf32_offsets[member];
838 p = base + elf32_offsets[member];
954 switch (e->e_ident[EI_DATA]) {
955 case ELFDATA2MSB:
839 switch (e->e_ident[EI_DATA]) {
840 case ELFDATA2MSB:
956 val = be16dec(base);
841 val = p[0] << 8 | p[1];
957 break;
958 case ELFDATA2LSB:
842 break;
843 case ELFDATA2LSB:
959 val = le16dec(base);
844 val = p[1] << 8 | p[0];
960 break;
961 case ELFDATANONE:
962 errx(1, "invalid data format");
963 }
964 break;
965 case ELFCLASS64:
845 break;
846 case ELFDATANONE:
847 errx(1, "invalid data format");
848 }
849 break;
850 case ELFCLASS64:
966 base = (char *)base + elf64_offsets[member];
851 p = base + elf64_offsets[member];
967 switch (e->e_ident[EI_DATA]) {
968 case ELFDATA2MSB:
852 switch (e->e_ident[EI_DATA]) {
853 case ELFDATA2MSB:
969 val = be16dec(base);
854 val = p[0] << 8 | p[1];
970 break;
971 case ELFDATA2LSB:
855 break;
856 case ELFDATA2LSB:
972 val = le16dec(base);
857 val = p[1] << 8 | p[0];
973 break;
974 case ELFDATANONE:
975 errx(1, "invalid data format");
976 }
977 break;
978 case ELFCLASSNONE:
979 errx(1, "invalid class");
980 }
981
982 return val;
983}
984
985u_int64_t
986elf_get_half(Elf32_Ehdr *e, void *base, elf_member_t member)
987{
988 u_int64_t val;
858 break;
859 case ELFDATANONE:
860 errx(1, "invalid data format");
861 }
862 break;
863 case ELFCLASSNONE:
864 errx(1, "invalid class");
865 }
866
867 return val;
868}
869
870u_int64_t
871elf_get_half(Elf32_Ehdr *e, void *base, elf_member_t member)
872{
873 u_int64_t val;
874 u_char *p;
989
990 val = 0;
991 switch (e->e_ident[EI_CLASS]) {
992 case ELFCLASS32:
875
876 val = 0;
877 switch (e->e_ident[EI_CLASS]) {
878 case ELFCLASS32:
993 base = (char *)base + elf32_offsets[member];
879 p = base + elf32_offsets[member];
994 switch (e->e_ident[EI_DATA]) {
995 case ELFDATA2MSB:
880 switch (e->e_ident[EI_DATA]) {
881 case ELFDATA2MSB:
996 val = be16dec(base);
882 val = p[0] << 8 | p[1];
997 break;
998 case ELFDATA2LSB:
883 break;
884 case ELFDATA2LSB:
999 val = le16dec(base);
885 val = p[1] << 8 | p[0];
1000 break;
1001 case ELFDATANONE:
1002 errx(1, "invalid data format");
1003 }
1004 break;
1005 case ELFCLASS64:
886 break;
887 case ELFDATANONE:
888 errx(1, "invalid data format");
889 }
890 break;
891 case ELFCLASS64:
1006 base = (char *)base + elf64_offsets[member];
892 p = base + elf64_offsets[member];
1007 switch (e->e_ident[EI_DATA]) {
1008 case ELFDATA2MSB:
893 switch (e->e_ident[EI_DATA]) {
894 case ELFDATA2MSB:
1009 val = be32dec(base);
895 val = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
1010 break;
1011 case ELFDATA2LSB:
896 break;
897 case ELFDATA2LSB:
1012 val = le32dec(base);
898 val = p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
1013 break;
1014 case ELFDATANONE:
1015 errx(1, "invalid data format");
1016 }
1017 break;
1018 case ELFCLASSNONE:
1019 errx(1, "invalid class");
1020 }
1021
1022 return val;
1023}
1024
1025u_int64_t
1026elf_get_word(Elf32_Ehdr *e, void *base, elf_member_t member)
1027{
1028 u_int64_t val;
899 break;
900 case ELFDATANONE:
901 errx(1, "invalid data format");
902 }
903 break;
904 case ELFCLASSNONE:
905 errx(1, "invalid class");
906 }
907
908 return val;
909}
910
911u_int64_t
912elf_get_word(Elf32_Ehdr *e, void *base, elf_member_t member)
913{
914 u_int64_t val;
915 u_char *p;
1029
1030 val = 0;
1031 switch (e->e_ident[EI_CLASS]) {
1032 case ELFCLASS32:
916
917 val = 0;
918 switch (e->e_ident[EI_CLASS]) {
919 case ELFCLASS32:
1033 base = (char *)base + elf32_offsets[member];
920 p = base + elf32_offsets[member];
1034 switch (e->e_ident[EI_DATA]) {
1035 case ELFDATA2MSB:
921 switch (e->e_ident[EI_DATA]) {
922 case ELFDATA2MSB:
1036 val = be32dec(base);
923 val = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
1037 break;
1038 case ELFDATA2LSB:
924 break;
925 case ELFDATA2LSB:
1039 val = le32dec(base);
926 val = p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
1040 break;
1041 case ELFDATANONE:
1042 errx(1, "invalid data format");
1043 }
1044 break;
1045 case ELFCLASS64:
927 break;
928 case ELFDATANONE:
929 errx(1, "invalid data format");
930 }
931 break;
932 case ELFCLASS64:
1046 base = (char *)base + elf64_offsets[member];
933 p = base + elf64_offsets[member];
1047 switch (e->e_ident[EI_DATA]) {
1048 case ELFDATA2MSB:
934 switch (e->e_ident[EI_DATA]) {
935 case ELFDATA2MSB:
1049 val = be32dec(base);
936 val = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
1050 break;
1051 case ELFDATA2LSB:
937 break;
938 case ELFDATA2LSB:
1052 val = le32dec(base);
939 val = p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
1053 break;
1054 case ELFDATANONE:
1055 errx(1, "invalid data format");
1056 }
1057 break;
1058 case ELFCLASSNONE:
1059 errx(1, "invalid class");
1060 }
1061
1062 return val;
1063}
1064
1065u_int64_t
1066elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t member)
1067{
940 break;
941 case ELFDATANONE:
942 errx(1, "invalid data format");
943 }
944 break;
945 case ELFCLASSNONE:
946 errx(1, "invalid class");
947 }
948
949 return val;
950}
951
952u_int64_t
953elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t member)
954{
955 u_int64_t high;
956 u_int64_t low;
1068 u_int64_t val;
957 u_int64_t val;
958 u_char *p;
1069
1070 val = 0;
1071 switch (e->e_ident[EI_CLASS]) {
1072 case ELFCLASS32:
959
960 val = 0;
961 switch (e->e_ident[EI_CLASS]) {
962 case ELFCLASS32:
1073 base = (char *)base + elf32_offsets[member];
963 p = base + elf32_offsets[member];
1074 switch (e->e_ident[EI_DATA]) {
1075 case ELFDATA2MSB:
964 switch (e->e_ident[EI_DATA]) {
965 case ELFDATA2MSB:
1076 val = be32dec(base);
966 val = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
1077 break;
1078 case ELFDATA2LSB:
967 break;
968 case ELFDATA2LSB:
1079 val = le32dec(base);
969 val = p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
1080 break;
1081 case ELFDATANONE:
1082 errx(1, "invalid data format");
1083 }
1084 break;
1085 case ELFCLASS64:
970 break;
971 case ELFDATANONE:
972 errx(1, "invalid data format");
973 }
974 break;
975 case ELFCLASS64:
1086 base = (char *)base + elf64_offsets[member];
976 p = base + elf64_offsets[member];
1087 switch (e->e_ident[EI_DATA]) {
1088 case ELFDATA2MSB:
977 switch (e->e_ident[EI_DATA]) {
978 case ELFDATA2MSB:
1089 val = be64dec(base);
979 high = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
980 low = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
981 val = high << 32 | low;
1090 break;
1091 case ELFDATA2LSB:
982 break;
983 case ELFDATA2LSB:
1092 val = le64dec(base);
984 high = p[7] << 24 | p[6] << 16 | p[5] << 8 | p[4];
985 low = p[3] << 24 | p[2] << 16 | p[1] << 8 | p[0];
986 val = high << 32 | low;
1093 break;
1094 case ELFDATANONE:
1095 errx(1, "invalid data format");
1096 }
1097 break;
1098 case ELFCLASSNONE:
1099 errx(1, "invalid class");
1100 }
1101
1102 return val;
1103}
1104
1105void
1106usage(void)
1107{
987 break;
988 case ELFDATANONE:
989 errx(1, "invalid data format");
990 }
991 break;
992 case ELFCLASSNONE:
993 errx(1, "invalid class");
994 }
995
996 return val;
997}
998
999void
1000usage(void)
1001{
1108 fprintf(stderr, "usage: elfdump -a | -cdeGhinprs [-w file] file\n");
1002 fprintf(stderr, "usage: elfdump [-acdeiGhnprs] [-w file] filename\n");
1109 exit(1);
1110}
1003 exit(1);
1004}