Menu

[r2891]: / trunk / documentation / libelf-by-example / prog3.txt  Maximize  Restore  History

Download this file

103 lines (87 with data), 2.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
/*
* Print the ELF Program Header Table in an ELF object.
*
* $Id$
*/
#include <err.h>
#include <fcntl.h>
#include <gelf.h> @\co{1}@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <vis.h>
void
print_ptype(size_t pt) @\co{7}@
{
char *s;
#define C(V) case PT_##V: s = #V; break
switch (pt) {
C(NULL); C(LOAD); C(DYNAMIC);
C(INTERP); C(NOTE); C(SHLIB);
C(PHDR); C(TLS); C(SUNW_UNWIND);
C(SUNWBSS); C(SUNWSTACK); C(SUNWDTRACE);
C(SUNWCAP);
default:
s = "unknown";
break;
}
(void) printf(" \"%s\"", s);
#undef C
}
int
main(int argc, char **argv)
{
int i, fd;
Elf *e;
char *id, bytes[5];
size_t n;
GElf_Phdr phdr; @\co{2}@
if (argc != 2)
errx(EXIT_FAILURE, "usage: %s file-name", argv[0]);
if (elf_version(EV_CURRENT) == EV_NONE)
errx(EXIT_FAILURE, "ELF library initialization "
"failed: %s", elf_errmsg(-1));
if ((fd = open(argv[1], O_RDONLY, 0)) < 0)
err(EXIT_FAILURE, "open \"%s\" failed", argv[1]);
if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
errx(EXIT_FAILURE, "elf_begin() failed: %s.",
elf_errmsg(-1));
if (elf_kind(e) != ELF_K_ELF)
errx(EXIT_FAILURE, "\"%s\" is not an ELF object.",
argv[1]);
if (elf_getphdrnum(e, &n) != 0) @\co{3}@
errx(EXIT_FAILURE, "elf_getphdrnum() failed: %s.",
elf_errmsg(-1));
for (i = 0; i < n; i++) { @\co{4}@
if (gelf_getphdr(e, i, &phdr) != &phdr) @\co{5}@
errx(EXIT_FAILURE, "getphdr() failed: %s.",
elf_errmsg(-1));
(void) printf("PHDR %d:\n", i);
#define PRINT_FMT " %-20s 0x%jx"
#define PRINT_FIELD(N) do { \
(void) printf(PRINT_FMT, #N, (uintmax_t) phdr.N); \
} while (0)
#define NL() do { (void) printf("\n"); } while (0)
PRINT_FIELD(p_type); @\co{6}@
print_ptype(phdr.p_type); NL();
PRINT_FIELD(p_offset); NL();
PRINT_FIELD(p_vaddr); NL();
PRINT_FIELD(p_paddr); NL();
PRINT_FIELD(p_filesz); NL();
PRINT_FIELD(p_memsz); NL();
PRINT_FIELD(p_flags);
(void) printf(" [");
if (phdr.p_flags & PF_X)
(void) printf(" execute");
if (phdr.p_flags & PF_R)
(void) printf(" read");
if (phdr.p_flags & PF_W)
(void) printf(" write");
printf(" ]"); NL();
PRINT_FIELD(p_align); NL();
}
(void) elf_end(e);
(void) close(fd);
exit(EXIT_SUCCESS);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.